code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
'''simple docstring''' import itertools from dataclasses import dataclass from typing import List, Optional import pyarrow as pa import pyarrow.parquet as pq import datasets from datasets.table import table_cast __lowerCAmelCase : Optional[int] = datasets.utils.logging.get_logger(__name__) @dataclass class UpperCAmelCase__ ( datasets.BuilderConfig ): """simple docstring""" __UpperCAmelCase : int = 1_0000 __UpperCAmelCase : Optional[List[str]] = None __UpperCAmelCase : Optional[datasets.Features] = None class UpperCAmelCase__ ( datasets.ArrowBasedBuilder ): """simple docstring""" __UpperCAmelCase : List[str] = ParquetConfig def __lowercase ( self : int ): '''simple docstring''' return datasets.DatasetInfo(features=self.config.features ) def __lowercase ( self : int ,_a : List[Any] ): '''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}""" ) _a : Tuple = dl_manager.download_and_extract(self.config.data_files ) if isinstance(_a ,(str, list, tuple) ): _a : int = data_files if isinstance(_a ,_a ): _a : Tuple = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive _a : int = [dl_manager.iter_files(_a ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN ,gen_kwargs={'files': files} )] _a : Dict = [] for split_name, files in data_files.items(): if isinstance(_a ,_a ): _a : Any = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive _a : Optional[int] = [dl_manager.iter_files(_a ) for file in files] # Infer features is they are stoed in the arrow schema if self.info.features is None: for file in itertools.chain.from_iterable(_a ): with open(_a ,'rb' ) as f: _a : Dict = datasets.Features.from_arrow_schema(pq.read_schema(_a ) ) break splits.append(datasets.SplitGenerator(name=_a ,gen_kwargs={'files': files} ) ) return splits def __lowercase ( self : List[str] ,_a : pa.Table ): '''simple docstring''' if self.info.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 _a : List[str] = table_cast(_a ,self.info.features.arrow_schema ) return pa_table def __lowercase ( self : Any ,_a : Optional[int] ): '''simple docstring''' _a : Optional[int] = self.info.features.arrow_schema if self.info.features is not None else None if self.info.features is not None and self.config.columns is not None: if sorted(field.name for field in schema ) != sorted(self.config.columns ): raise ValueError( F"""Tried to load parquet data with columns '{self.config.columns}' with mismatching features '{self.info.features}'""" ) for file_idx, file in enumerate(itertools.chain.from_iterable(_a ) ): with open(_a ,'rb' ) as f: _a : int = pq.ParquetFile(_a ) try: for batch_idx, record_batch in enumerate( parquet_file.iter_batches(batch_size=self.config.batch_size ,columns=self.config.columns ) ): _a : Tuple = pa.Table.from_batches([record_batch] ) # 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 F"""{file_idx}_{batch_idx}""", self._cast_table(_a ) except ValueError as e: logger.error(F"""Failed to read file '{file}' with error {type(_a )}: {e}""" ) raise
354
'''simple docstring''' import os import time import pytest from datasets.utils.filelock import FileLock, Timeout def UpperCAmelCase_ (__a : Optional[Any] ): """simple docstring""" _a : int = FileLock(str(tmpdir / 'foo.lock' ) ) _a : List[Any] = FileLock(str(tmpdir / 'foo.lock' ) ) _a : Any = 0.01 with locka.acquire(): with pytest.raises(__a ): _a : int = time.time() locka.acquire(__a ) assert time.time() - _start > timeout def UpperCAmelCase_ (__a : str ): """simple docstring""" _a : Dict = 'a' * 1_0_0_0 + '.lock' _a : int = FileLock(str(tmpdir / filename ) ) assert locka._lock_file.endswith('.lock' ) assert not locka._lock_file.endswith(__a ) assert len(os.path.basename(locka._lock_file ) ) <= 2_5_5 _a : Dict = FileLock(tmpdir / filename ) with locka.acquire(): with pytest.raises(__a ): locka.acquire(0 )
5
0
'''simple docstring''' import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = """▁""" __lowerCAmelCase = {"""vocab_file""": """sentencepiece.bpe.model"""} __lowerCAmelCase = { """vocab_file""": { """xlm-roberta-base""": """https://huggingface.co/xlm-roberta-base/resolve/main/sentencepiece.bpe.model""", """xlm-roberta-large""": """https://huggingface.co/xlm-roberta-large/resolve/main/sentencepiece.bpe.model""", """xlm-roberta-large-finetuned-conll02-dutch""": ( """https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/sentencepiece.bpe.model""" ), """xlm-roberta-large-finetuned-conll02-spanish""": ( """https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/sentencepiece.bpe.model""" ), """xlm-roberta-large-finetuned-conll03-english""": ( """https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/sentencepiece.bpe.model""" ), """xlm-roberta-large-finetuned-conll03-german""": ( """https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/sentencepiece.bpe.model""" ), } } __lowerCAmelCase = { """xlm-roberta-base""": 5_1_2, """xlm-roberta-large""": 5_1_2, """xlm-roberta-large-finetuned-conll02-dutch""": 5_1_2, """xlm-roberta-large-finetuned-conll02-spanish""": 5_1_2, """xlm-roberta-large-finetuned-conll03-english""": 5_1_2, """xlm-roberta-large-finetuned-conll03-german""": 5_1_2, } class UpperCAmelCase__ ( lowercase__ ): """simple docstring""" __UpperCAmelCase : List[Any] = VOCAB_FILES_NAMES __UpperCAmelCase : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP __UpperCAmelCase : Tuple = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCAmelCase : List[Any] = ['''input_ids''', '''attention_mask'''] def __init__( self : Tuple ,_a : str ,_a : Any="<s>" ,_a : Optional[Any]="</s>" ,_a : Union[str, Any]="</s>" ,_a : Union[str, Any]="<s>" ,_a : Optional[int]="<unk>" ,_a : Union[str, Any]="<pad>" ,_a : int="<mask>" ,_a : Optional[Dict[str, Any]] = None ,**_a : int ,): '''simple docstring''' _a : Optional[int] = AddedToken(_a ,lstrip=_a ,rstrip=_a ) if isinstance(_a ,_a ) else mask_token _a : Dict = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=_a ,eos_token=_a ,unk_token=_a ,sep_token=_a ,cls_token=_a ,pad_token=_a ,mask_token=_a ,sp_model_kwargs=self.sp_model_kwargs ,**_a ,) _a : Any = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(_a ) ) _a : Optional[int] = vocab_file # Original fairseq vocab and spm vocab must be "aligned": # Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ---- # fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's' | '▁de' | '-' # spm | '<unk>' | '<s>' | '</s>' | ',' | '.' | '▁' | 's' | '▁de' | '-' | '▁a' # Mimic fairseq token-to-id alignment for the first 4 token _a : List[str] = {'<s>': 0, '<pad>': 1, '</s>': 2, '<unk>': 3} # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab _a : List[str] = 1 _a : Tuple = len(self.sp_model ) + self.fairseq_offset _a : Dict = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def __getstate__( self : Dict ): '''simple docstring''' _a : List[Any] = self.__dict__.copy() _a : Optional[Any] = None _a : Dict = self.sp_model.serialized_model_proto() return state def __setstate__( self : Any ,_a : int ): '''simple docstring''' _a : Optional[Any] = d # for backward compatibility if not hasattr(self ,'sp_model_kwargs' ): _a : str = {} _a : List[str] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.LoadFromSerializedProto(self.sp_model_proto ) def __lowercase ( self : List[Any] ,_a : List[int] ,_a : Optional[List[int]] = None ): '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] _a : List[str] = [self.cls_token_id] _a : Any = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def __lowercase ( self : Any ,_a : List[int] ,_a : Optional[List[int]] = None ,_a : bool = False ): '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_a ,token_ids_a=_a ,already_has_special_tokens=_a ) if token_ids_a is None: return [1] + ([0] * len(_a )) + [1] return [1] + ([0] * len(_a )) + [1, 1] + ([0] * len(_a )) + [1] def __lowercase ( self : Any ,_a : List[int] ,_a : Optional[List[int]] = None ): '''simple docstring''' _a : List[Any] = [self.sep_token_id] _a : Optional[Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def __lowercase ( self : Optional[Any] ): '''simple docstring''' return len(self.sp_model ) + self.fairseq_offset + 1 # Add the <mask> token def __lowercase ( self : int ): '''simple docstring''' _a : Union[str, Any] = {self.convert_ids_to_tokens(_a ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __lowercase ( self : Tuple ,_a : str ): '''simple docstring''' return self.sp_model.encode(_a ,out_type=_a ) def __lowercase ( self : Optional[int] ,_a : Dict ): '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] _a : Optional[Any] = self.sp_model.PieceToId(_a ) # 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 __lowercase ( self : List[str] ,_a : int ): '''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 __lowercase ( self : Dict ,_a : Union[str, Any] ): '''simple docstring''' _a : Dict = ''.join(_a ).replace(_a ,' ' ).strip() return out_string def __lowercase ( self : Any ,_a : str ,_a : Optional[str] = None ): '''simple docstring''' if not os.path.isdir(_a ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return _a : Any = os.path.join( _a ,(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_a ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,_a ) elif not os.path.isfile(self.vocab_file ): with open(_a ,'wb' ) as fi: _a : Union[str, Any] = self.sp_model.serialized_model_proto() fi.write(_a ) return (out_vocab_file,)
355
'''simple docstring''' def UpperCAmelCase_ (__a : int = 1_0**1_2 ): """simple docstring""" _a : List[str] = 1 _a : Optional[int] = 0 _a : Any = 1 _a : List[str] = 1 while numerator <= 2 * min_total - 1: prev_numerator += 2 * numerator numerator += 2 * prev_numerator prev_denominator += 2 * denominator denominator += 2 * prev_denominator return (denominator + 1) // 2 if __name__ == "__main__": print(f'''{solution() = }''')
5
0
'''simple docstring''' import sys from typing import Tuple import numpy as np import torch from PIL import Image from torch import nn from transformers.image_utils import PILImageResampling from utils import img_tensorize class UpperCAmelCase__ : """simple docstring""" def __init__( self : str ,_a : List[str] ,_a : int=sys.maxsize ): '''simple docstring''' _a : Any = 'bilinear' _a : List[Any] = max_size _a : List[str] = short_edge_length def __call__( self : List[str] ,_a : int ): '''simple docstring''' _a : Optional[int] = [] for img in imgs: _a : List[Any] = img.shape[:2] # later: provide list and randomly choose index for resize _a : str = np.random.randint(self.short_edge_length[0] ,self.short_edge_length[1] + 1 ) if size == 0: return img _a : Dict = size * 1.0 / min(_a ,_a ) if h < w: _a : Optional[Any] = size, scale * w else: _a : List[str] = scale * h, size if max(_a ,_a ) > self.max_size: _a : Dict = self.max_size * 1.0 / max(_a ,_a ) _a : List[Any] = newh * scale _a : List[str] = neww * scale _a : List[str] = int(neww + 0.5 ) _a : Optional[Any] = int(newh + 0.5 ) if img.dtype == np.uinta: _a : Dict = Image.fromarray(_a ) _a : str = pil_image.resize((neww, newh) ,PILImageResampling.BILINEAR ) _a : List[str] = np.asarray(_a ) else: _a : int = img.permute(2 ,0 ,1 ).unsqueeze(0 ) # 3, 0, 1) # hw(c) -> nchw _a : Optional[Any] = nn.functional.interpolate( _a ,(newh, neww) ,mode=self.interp_method ,align_corners=_a ).squeeze(0 ) img_augs.append(_a ) return img_augs class UpperCAmelCase__ : """simple docstring""" def __init__( self : Union[str, Any] ,_a : List[str] ): '''simple docstring''' _a : List[Any] = ResizeShortestEdge([cfg.INPUT.MIN_SIZE_TEST, cfg.INPUT.MIN_SIZE_TEST] ,cfg.INPUT.MAX_SIZE_TEST ) _a : Optional[int] = cfg.INPUT.FORMAT _a : Optional[int] = cfg.SIZE_DIVISIBILITY _a : Optional[Any] = cfg.PAD_VALUE _a : Optional[Any] = cfg.INPUT.MAX_SIZE_TEST _a : Tuple = cfg.MODEL.DEVICE _a : Tuple = torch.tensor(cfg.MODEL.PIXEL_STD ).to(self.device ).view(len(cfg.MODEL.PIXEL_STD ) ,1 ,1 ) _a : Any = torch.tensor(cfg.MODEL.PIXEL_MEAN ).to(self.device ).view(len(cfg.MODEL.PIXEL_STD ) ,1 ,1 ) _a : Tuple = lambda _a : (x - self.pixel_mean) / self.pixel_std def __lowercase ( self : Optional[int] ,_a : Dict ): '''simple docstring''' _a : List[Any] = tuple(max(_a ) for s in zip(*[img.shape for img in images] ) ) _a : Tuple = [im.shape[-2:] for im in images] _a : str = [ nn.functional.pad( _a ,[0, max_size[-1] - size[1], 0, max_size[-2] - size[0]] ,value=self.pad_value ,) for size, im in zip(_a ,_a ) ] return torch.stack(_a ), torch.tensor(_a ) def __call__( self : Union[str, Any] ,_a : List[Any] ,_a : Optional[int]=False ): '''simple docstring''' with torch.no_grad(): if not isinstance(_a ,_a ): _a : str = [images] if single_image: assert len(_a ) == 1 for i in range(len(_a ) ): if isinstance(images[i] ,torch.Tensor ): images.insert(_a ,images.pop(_a ).to(self.device ).float() ) elif not isinstance(images[i] ,torch.Tensor ): images.insert( _a ,torch.as_tensor(img_tensorize(images.pop(_a ) ,input_format=self.input_format ) ) .to(self.device ) .float() ,) # resize smallest edge _a : Optional[int] = torch.tensor([im.shape[:2] for im in images] ) _a : int = self.aug(_a ) # transpose images and convert to torch tensors # images = [torch.as_tensor(i.astype("float32")).permute(2, 0, 1).to(self.device) for i in images] # now normalize before pad to avoid useless arithmetic _a : int = [self.normalizer(_a ) for x in images] # now pad them to do the following operations _a : Optional[Any] = self.pad(_a ) # Normalize if self.size_divisibility > 0: raise NotImplementedError() # pad _a : List[Any] = torch.true_divide(_a ,_a ) if single_image: return images[0], sizes[0], scales_yx[0] else: return images, sizes, scales_yx def UpperCAmelCase_ (__a : str , __a : Union[str, Any] ): """simple docstring""" boxes[:, 0::2] *= scale_yx[:, 1] boxes[:, 1::2] *= scale_yx[:, 0] return boxes def UpperCAmelCase_ (__a : Dict , __a : Tuple[int, int] ): """simple docstring""" assert torch.isfinite(__a ).all(), "Box tensor contains infinite or NaN!" _a : Dict = box_size tensor[:, 0].clamp_(min=0 , max=__a ) tensor[:, 1].clamp_(min=0 , max=__a ) tensor[:, 2].clamp_(min=0 , max=__a ) tensor[:, 3].clamp_(min=0 , max=__a )
356
'''simple docstring''' import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_mobilebert import MobileBertTokenizer __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""} __lowerCAmelCase = { """vocab_file""": {"""mobilebert-uncased""": """https://huggingface.co/google/mobilebert-uncased/resolve/main/vocab.txt"""}, """tokenizer_file""": { """mobilebert-uncased""": """https://huggingface.co/google/mobilebert-uncased/resolve/main/tokenizer.json""" }, } __lowerCAmelCase = {"""mobilebert-uncased""": 5_1_2} __lowerCAmelCase = {} class UpperCAmelCase__ ( lowercase__ ): """simple docstring""" __UpperCAmelCase : Any = VOCAB_FILES_NAMES __UpperCAmelCase : int = PRETRAINED_VOCAB_FILES_MAP __UpperCAmelCase : Tuple = PRETRAINED_INIT_CONFIGURATION __UpperCAmelCase : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCAmelCase : Optional[Any] = MobileBertTokenizer def __init__( self : Dict ,_a : List[Any]=None ,_a : Optional[Any]=None ,_a : Union[str, Any]=True ,_a : Dict="[UNK]" ,_a : Union[str, Any]="[SEP]" ,_a : Any="[PAD]" ,_a : Optional[int]="[CLS]" ,_a : Optional[Any]="[MASK]" ,_a : Dict=True ,_a : Any=None ,**_a : Optional[Any] ,): '''simple docstring''' super().__init__( _a ,tokenizer_file=_a ,do_lower_case=_a ,unk_token=_a ,sep_token=_a ,pad_token=_a ,cls_token=_a ,mask_token=_a ,tokenize_chinese_chars=_a ,strip_accents=_a ,**_a ,) _a : int = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('lowercase' ,_a ) != do_lower_case or normalizer_state.get('strip_accents' ,_a ) != strip_accents or normalizer_state.get('handle_chinese_chars' ,_a ) != tokenize_chinese_chars ): _a : Optional[Any] = getattr(_a ,normalizer_state.pop('type' ) ) _a : Dict = do_lower_case _a : str = strip_accents _a : Tuple = tokenize_chinese_chars _a : Optional[Any] = normalizer_class(**_a ) _a : str = do_lower_case def __lowercase ( self : Tuple ,_a : Union[str, Any] ,_a : List[str]=None ): '''simple docstring''' _a : Tuple = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def __lowercase ( self : List[str] ,_a : List[int] ,_a : Optional[List[int]] = None ): '''simple docstring''' _a : List[str] = [self.sep_token_id] _a : str = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __lowercase ( self : Union[str, Any] ,_a : str ,_a : Optional[str] = None ): '''simple docstring''' _a : int = self._tokenizer.model.save(_a ,name=_a ) return tuple(_a )
5
0
'''simple docstring''' import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_mobilebert import MobileBertTokenizer __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""} __lowerCAmelCase = { """vocab_file""": {"""mobilebert-uncased""": """https://huggingface.co/google/mobilebert-uncased/resolve/main/vocab.txt"""}, """tokenizer_file""": { """mobilebert-uncased""": """https://huggingface.co/google/mobilebert-uncased/resolve/main/tokenizer.json""" }, } __lowerCAmelCase = {"""mobilebert-uncased""": 5_1_2} __lowerCAmelCase = {} class UpperCAmelCase__ ( lowercase__ ): """simple docstring""" __UpperCAmelCase : Any = VOCAB_FILES_NAMES __UpperCAmelCase : int = PRETRAINED_VOCAB_FILES_MAP __UpperCAmelCase : Tuple = PRETRAINED_INIT_CONFIGURATION __UpperCAmelCase : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCAmelCase : Optional[Any] = MobileBertTokenizer def __init__( self : Dict ,_a : List[Any]=None ,_a : Optional[Any]=None ,_a : Union[str, Any]=True ,_a : Dict="[UNK]" ,_a : Union[str, Any]="[SEP]" ,_a : Any="[PAD]" ,_a : Optional[int]="[CLS]" ,_a : Optional[Any]="[MASK]" ,_a : Dict=True ,_a : Any=None ,**_a : Optional[Any] ,): '''simple docstring''' super().__init__( _a ,tokenizer_file=_a ,do_lower_case=_a ,unk_token=_a ,sep_token=_a ,pad_token=_a ,cls_token=_a ,mask_token=_a ,tokenize_chinese_chars=_a ,strip_accents=_a ,**_a ,) _a : int = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('lowercase' ,_a ) != do_lower_case or normalizer_state.get('strip_accents' ,_a ) != strip_accents or normalizer_state.get('handle_chinese_chars' ,_a ) != tokenize_chinese_chars ): _a : Optional[Any] = getattr(_a ,normalizer_state.pop('type' ) ) _a : Dict = do_lower_case _a : str = strip_accents _a : Tuple = tokenize_chinese_chars _a : Optional[Any] = normalizer_class(**_a ) _a : str = do_lower_case def __lowercase ( self : Tuple ,_a : Union[str, Any] ,_a : List[str]=None ): '''simple docstring''' _a : Tuple = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def __lowercase ( self : List[str] ,_a : List[int] ,_a : Optional[List[int]] = None ): '''simple docstring''' _a : List[str] = [self.sep_token_id] _a : str = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __lowercase ( self : Union[str, Any] ,_a : str ,_a : Optional[str] = None ): '''simple docstring''' _a : int = self._tokenizer.model.save(_a ,name=_a ) return tuple(_a )
357
'''simple docstring''' def UpperCAmelCase_ (__a : str ): """simple docstring""" _a : List[Any] = 0 # if input_string is "aba" than new_input_string become "a|b|a" _a : Optional[int] = '' _a : List[str] = '' # append each character + "|" in new_string for range(0, length-1) for i in input_string[: len(__a ) - 1]: new_input_string += i + "|" # append last character new_input_string += input_string[-1] # we will store the starting and ending of previous furthest ending palindromic # substring _a, _a : Optional[int] = 0, 0 # length[i] shows the length of palindromic substring with center i _a : Optional[Any] = [1 for i in range(len(__a ) )] # for each character in new_string find corresponding palindromic string _a : Dict = 0 for j in range(len(__a ) ): _a : Dict = 1 if j > r else min(length[l + r - j] // 2 , r - j + 1 ) while ( j - k >= 0 and j + k < len(__a ) and new_input_string[k + j] == new_input_string[j - k] ): k += 1 _a : Optional[int] = 2 * k - 1 # does this string is ending after the previously explored end (that is r) ? # if yes the update the new r to the last index of this if j + k - 1 > r: _a : str = j - k + 1 # noqa: E741 _a : Any = j + k - 1 # update max_length and start position if max_length < length[j]: _a : Union[str, Any] = length[j] _a : List[str] = j # create that string _a : Tuple = new_input_string[start - max_length // 2 : start + max_length // 2 + 1] for i in s: if i != "|": output_string += i return output_string if __name__ == "__main__": import doctest doctest.testmod()
5
0
'''simple docstring''' from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available() and is_transformers_version(""">=""", """4.25.0""")): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline else: from .pipeline_unclip import UnCLIPPipeline from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline from .text_proj import UnCLIPTextProjModel
358
'''simple docstring''' from functools import lru_cache @lru_cache def UpperCAmelCase_ (__a : int ): """simple docstring""" if num < 0: raise ValueError('Number should not be negative.' ) return 1 if num in (0, 1) else num * factorial(num - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
5
0
'''simple docstring''' from ...utils import deprecate from ..controlnet.pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline # noqa: F401 deprecate( """stable diffusion controlnet""", """0.22.0""", """Importing `FlaxStableDiffusionControlNetPipeline` from diffusers.pipelines.stable_diffusion.flax_pipeline_stable_diffusion_controlnet is deprecated. Please import `from diffusers import FlaxStableDiffusionControlNetPipeline` instead.""", standard_warn=False, stacklevel=3, )
359
'''simple docstring''' import functools import logging import os import sys import threading from logging import ( CRITICAL, # NOQA DEBUG, # NOQA ERROR, # NOQA FATAL, # NOQA INFO, # NOQA NOTSET, # NOQA WARN, # NOQA WARNING, # NOQA ) from typing import Optional import huggingface_hub.utils as hf_hub_utils from tqdm import auto as tqdm_lib __lowerCAmelCase = threading.Lock() __lowerCAmelCase = None __lowerCAmelCase = { """debug""": logging.DEBUG, """info""": logging.INFO, """warning""": logging.WARNING, """error""": logging.ERROR, """critical""": logging.CRITICAL, } __lowerCAmelCase = logging.WARNING __lowerCAmelCase = True def UpperCAmelCase_ (): """simple docstring""" _a : Dict = os.getenv('TRANSFORMERS_VERBOSITY' , __a ) if env_level_str: if env_level_str in log_levels: return log_levels[env_level_str] else: logging.getLogger().warning( f"""Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, """ f"""has to be one of: { ', '.join(log_levels.keys() ) }""" ) return _default_log_level def UpperCAmelCase_ (): """simple docstring""" return __name__.split('.' )[0] def UpperCAmelCase_ (): """simple docstring""" return logging.getLogger(_get_library_name() ) def UpperCAmelCase_ (): """simple docstring""" global _default_handler with _lock: if _default_handler: # This library has already configured the library root logger. return _a : str = logging.StreamHandler() # Set sys.stderr as stream. _a : Optional[Any] = sys.stderr.flush # Apply our default configuration to the library root logger. _a : List[Any] = _get_library_root_logger() library_root_logger.addHandler(_default_handler ) library_root_logger.setLevel(_get_default_logging_level() ) _a : List[str] = False def UpperCAmelCase_ (): """simple docstring""" global _default_handler with _lock: if not _default_handler: return _a : int = _get_library_root_logger() library_root_logger.removeHandler(_default_handler ) library_root_logger.setLevel(logging.NOTSET ) _a : str = None def UpperCAmelCase_ (): """simple docstring""" return log_levels def UpperCAmelCase_ (__a : Optional[str] = None ): """simple docstring""" if name is None: _a : List[Any] = _get_library_name() _configure_library_root_logger() return logging.getLogger(__a ) def UpperCAmelCase_ (): """simple docstring""" _configure_library_root_logger() return _get_library_root_logger().getEffectiveLevel() def UpperCAmelCase_ (__a : int ): """simple docstring""" _configure_library_root_logger() _get_library_root_logger().setLevel(__a ) def UpperCAmelCase_ (): """simple docstring""" return set_verbosity(__a ) def UpperCAmelCase_ (): """simple docstring""" return set_verbosity(__a ) def UpperCAmelCase_ (): """simple docstring""" return set_verbosity(__a ) def UpperCAmelCase_ (): """simple docstring""" return set_verbosity(__a ) def UpperCAmelCase_ (): """simple docstring""" _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().removeHandler(_default_handler ) def UpperCAmelCase_ (): """simple docstring""" _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().addHandler(_default_handler ) def UpperCAmelCase_ (__a : logging.Handler ): """simple docstring""" _configure_library_root_logger() assert handler is not None _get_library_root_logger().addHandler(__a ) def UpperCAmelCase_ (__a : logging.Handler ): """simple docstring""" _configure_library_root_logger() assert handler is not None and handler not in _get_library_root_logger().handlers _get_library_root_logger().removeHandler(__a ) def UpperCAmelCase_ (): """simple docstring""" _configure_library_root_logger() _a : Union[str, Any] = False def UpperCAmelCase_ (): """simple docstring""" _configure_library_root_logger() _a : Dict = True def UpperCAmelCase_ (): """simple docstring""" _a : Any = _get_library_root_logger().handlers for handler in handlers: _a : Union[str, Any] = logging.Formatter('[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s' ) handler.setFormatter(__a ) def UpperCAmelCase_ (): """simple docstring""" _a : Union[str, Any] = _get_library_root_logger().handlers for handler in handlers: handler.setFormatter(__a ) def UpperCAmelCase_ (self : Union[str, Any] , *__a : Union[str, Any] , **__a : Union[str, Any] ): """simple docstring""" _a : Union[str, Any] = os.getenv('TRANSFORMERS_NO_ADVISORY_WARNINGS' , __a ) if no_advisory_warnings: return self.warning(*__a , **__a ) __lowerCAmelCase = warning_advice @functools.lru_cache(__a ) def UpperCAmelCase_ (self : int , *__a : Optional[Any] , **__a : Any ): """simple docstring""" self.warning(*__a , **__a ) __lowerCAmelCase = warning_once class UpperCAmelCase__ : """simple docstring""" def __init__( self : Any ,*_a : Tuple ,**_a : int ): # pylint: disable=unused-argument '''simple docstring''' _a : int = args[0] if args else None def __iter__( self : str ): '''simple docstring''' return iter(self._iterator ) def __getattr__( self : List[Any] ,_a : int ): '''simple docstring''' def empty_fn(*_a : Optional[Any] ,**_a : Any ): # pylint: disable=unused-argument return return empty_fn def __enter__( self : List[str] ): '''simple docstring''' return self def __exit__( self : List[str] ,_a : str ,_a : List[Any] ,_a : str ): '''simple docstring''' return class UpperCAmelCase__ : """simple docstring""" def __call__( self : Union[str, Any] ,*_a : Tuple ,**_a : Tuple ): '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm(*_a ,**_a ) else: return EmptyTqdm(*_a ,**_a ) def __lowercase ( self : str ,*_a : List[Any] ,**_a : Any ): '''simple docstring''' _a : Any = None if _tqdm_active: return tqdm_lib.tqdm.set_lock(*_a ,**_a ) def __lowercase ( self : List[str] ): '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm.get_lock() __lowerCAmelCase = _tqdm_cls() def UpperCAmelCase_ (): """simple docstring""" global _tqdm_active return bool(_tqdm_active ) def UpperCAmelCase_ (): """simple docstring""" global _tqdm_active _a : str = True hf_hub_utils.enable_progress_bars() def UpperCAmelCase_ (): """simple docstring""" global _tqdm_active _a : Dict = False hf_hub_utils.disable_progress_bars()
5
0
'''simple docstring''' import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_base import BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import PaddingStrategy, logging from .tokenization_realm import RealmTokenizer __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""} __lowerCAmelCase = { """vocab_file""": { """google/realm-cc-news-pretrained-embedder""": ( """https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/vocab.txt""" ), """google/realm-cc-news-pretrained-encoder""": ( """https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/vocab.txt""" ), """google/realm-cc-news-pretrained-scorer""": ( """https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/vocab.txt""" ), """google/realm-cc-news-pretrained-openqa""": ( """https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/vocab.txt""" ), """google/realm-orqa-nq-openqa""": """https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/vocab.txt""", """google/realm-orqa-nq-reader""": """https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/vocab.txt""", """google/realm-orqa-wq-openqa""": """https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/vocab.txt""", """google/realm-orqa-wq-reader""": """https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/vocab.txt""", }, """tokenizer_file""": { """google/realm-cc-news-pretrained-embedder""": ( """https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/tokenizer.jsont""" ), """google/realm-cc-news-pretrained-encoder""": ( """https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/tokenizer.json""" ), """google/realm-cc-news-pretrained-scorer""": ( """https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/tokenizer.json""" ), """google/realm-cc-news-pretrained-openqa""": ( """https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/tokenizer.json""" ), """google/realm-orqa-nq-openqa""": ( """https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/tokenizer.json""" ), """google/realm-orqa-nq-reader""": ( """https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/tokenizer.json""" ), """google/realm-orqa-wq-openqa""": ( """https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/tokenizer.json""" ), """google/realm-orqa-wq-reader""": ( """https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/tokenizer.json""" ), }, } __lowerCAmelCase = { """google/realm-cc-news-pretrained-embedder""": 5_1_2, """google/realm-cc-news-pretrained-encoder""": 5_1_2, """google/realm-cc-news-pretrained-scorer""": 5_1_2, """google/realm-cc-news-pretrained-openqa""": 5_1_2, """google/realm-orqa-nq-openqa""": 5_1_2, """google/realm-orqa-nq-reader""": 5_1_2, """google/realm-orqa-wq-openqa""": 5_1_2, """google/realm-orqa-wq-reader""": 5_1_2, } __lowerCAmelCase = { """google/realm-cc-news-pretrained-embedder""": {"""do_lower_case""": True}, """google/realm-cc-news-pretrained-encoder""": {"""do_lower_case""": True}, """google/realm-cc-news-pretrained-scorer""": {"""do_lower_case""": True}, """google/realm-cc-news-pretrained-openqa""": {"""do_lower_case""": True}, """google/realm-orqa-nq-openqa""": {"""do_lower_case""": True}, """google/realm-orqa-nq-reader""": {"""do_lower_case""": True}, """google/realm-orqa-wq-openqa""": {"""do_lower_case""": True}, """google/realm-orqa-wq-reader""": {"""do_lower_case""": True}, } class UpperCAmelCase__ ( lowercase__ ): """simple docstring""" __UpperCAmelCase : Optional[int] = VOCAB_FILES_NAMES __UpperCAmelCase : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP __UpperCAmelCase : Dict = PRETRAINED_INIT_CONFIGURATION __UpperCAmelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCAmelCase : Union[str, Any] = RealmTokenizer def __init__( self : Optional[int] ,_a : Any=None ,_a : int=None ,_a : List[str]=True ,_a : Optional[int]="[UNK]" ,_a : Tuple="[SEP]" ,_a : Union[str, Any]="[PAD]" ,_a : List[Any]="[CLS]" ,_a : Dict="[MASK]" ,_a : Tuple=True ,_a : Tuple=None ,**_a : List[Any] ,): '''simple docstring''' super().__init__( _a ,tokenizer_file=_a ,do_lower_case=_a ,unk_token=_a ,sep_token=_a ,pad_token=_a ,cls_token=_a ,mask_token=_a ,tokenize_chinese_chars=_a ,strip_accents=_a ,**_a ,) _a : Union[str, Any] = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('lowercase' ,_a ) != do_lower_case or normalizer_state.get('strip_accents' ,_a ) != strip_accents or normalizer_state.get('handle_chinese_chars' ,_a ) != tokenize_chinese_chars ): _a : List[Any] = getattr(_a ,normalizer_state.pop('type' ) ) _a : Any = do_lower_case _a : int = strip_accents _a : Tuple = tokenize_chinese_chars _a : List[Any] = normalizer_class(**_a ) _a : Dict = do_lower_case def __lowercase ( self : Optional[int] ,_a : int ,**_a : List[Any] ): '''simple docstring''' _a : str = PaddingStrategy.MAX_LENGTH _a : Tuple = text _a : Tuple = kwargs.pop('text_pair' ,_a ) _a : Tuple = kwargs.pop('return_tensors' ,_a ) _a : Tuple = { 'input_ids': [], 'attention_mask': [], 'token_type_ids': [], } for idx, candidate_text in enumerate(_a ): if batch_text_pair is not None: _a : int = batch_text_pair[idx] else: _a : Tuple = None _a : List[Any] = super().__call__(_a ,_a ,return_tensors=_a ,**_a ) _a : List[str] = encoded_candidates.get('input_ids' ) _a : int = encoded_candidates.get('attention_mask' ) _a : Tuple = encoded_candidates.get('token_type_ids' ) if encoded_input_ids is not None: output_data["input_ids"].append(_a ) if encoded_attention_mask is not None: output_data["attention_mask"].append(_a ) if encoded_token_type_ids is not None: output_data["token_type_ids"].append(_a ) _a : List[Any] = {key: item for key, item in output_data.items() if len(_a ) != 0} return BatchEncoding(_a ,tensor_type=_a ) def __lowercase ( self : Any ,_a : Union[str, Any] ,_a : List[str]=None ): '''simple docstring''' _a : Optional[int] = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def __lowercase ( self : List[str] ,_a : List[int] ,_a : Optional[List[int]] = None ): '''simple docstring''' _a : Any = [self.sep_token_id] _a : Tuple = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __lowercase ( self : Union[str, Any] ,_a : str ,_a : Optional[str] = None ): '''simple docstring''' _a : Tuple = self._tokenizer.model.save(_a ,name=_a ) return tuple(_a )
360
'''simple docstring''' def UpperCAmelCase_ (__a : list[int] , __a : list[int] ): """simple docstring""" if not len(__a ) == len(__a ) == 3: raise ValueError('Please enter a valid equation.' ) if equationa[0] == equationa[1] == equationa[0] == equationa[1] == 0: raise ValueError('Both a & b of two equations can\'t be zero.' ) # Extract the coefficients _a, _a, _a : Tuple = equationa _a, _a, _a : str = equationa # Calculate the determinants of the matrices _a : Union[str, Any] = aa * ba - aa * ba _a : List[Any] = ca * ba - ca * ba _a : List[Any] = aa * ca - aa * ca # Check if the system of linear equations has a solution (using Cramer's rule) if determinant == 0: if determinant_x == determinant_y == 0: raise ValueError('Infinite solutions. (Consistent system)' ) else: raise ValueError('No solution. (Inconsistent system)' ) else: if determinant_x == determinant_y == 0: # Trivial solution (Inconsistent system) return (0.0, 0.0) else: _a : int = determinant_x / determinant _a : List[str] = determinant_y / determinant # Non-Trivial Solution (Consistent system) return (x, y)
5
0
'''simple docstring''' import argparse from collections import defaultdict import yaml __lowerCAmelCase = """docs/source/en/_toctree.yml""" def UpperCAmelCase_ (__a : str ): """simple docstring""" _a : Any = defaultdict(__a ) for doc in model_doc: counts[doc["local"]] += 1 _a : List[str] = [key for key, value in counts.items() if value > 1] _a : str = [] for duplicate_key in duplicates: _a : Union[str, Any] = list({doc['title'] for doc in model_doc if doc['local'] == duplicate_key} ) if len(__a ) > 1: raise ValueError( f"""{duplicate_key} is present several times in the documentation table of content at """ '`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the ' 'others.' ) # Only add this once new_doc.append({'local': duplicate_key, 'title': titles[0]} ) # Add none duplicate-keys new_doc.extend([doc for doc in model_doc if counts[doc['local']] == 1] ) # Sort return sorted(__a , key=lambda __a : s["title"].lower() ) def UpperCAmelCase_ (__a : Optional[int]=False ): """simple docstring""" with open(__a , encoding='utf-8' ) as f: _a : Tuple = yaml.safe_load(f.read() ) # Get to the API doc _a : Union[str, Any] = 0 while content[api_idx]["title"] != "API": api_idx += 1 _a : Union[str, Any] = content[api_idx]['sections'] # Then to the model doc _a : List[str] = 0 while api_doc[model_idx]["title"] != "Models": model_idx += 1 _a : List[str] = api_doc[model_idx]['sections'] _a : List[Any] = [(idx, section) for idx, section in enumerate(__a ) if 'sections' in section] _a : Tuple = False for idx, modality_doc in modalities_docs: _a : List[Any] = modality_doc['sections'] _a : Any = clean_model_doc_toc(__a ) if old_modality_doc != new_modality_doc: _a : Union[str, Any] = True if overwrite: _a : str = new_modality_doc if diff: if overwrite: _a : Dict = model_doc _a : Dict = api_doc with open(__a , 'w' , encoding='utf-8' ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) else: raise ValueError( 'The model doc part of the table of content is not properly sorted, run `make style` to fix this.' ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() parser.add_argument("""--fix_and_overwrite""", action="""store_true""", help="""Whether to fix inconsistencies.""") __lowerCAmelCase = parser.parse_args() check_model_doc(args.fix_and_overwrite)
361
'''simple docstring''' import inspect import unittest from transformers import ViTMSNConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTMSNForImageClassification, ViTMSNModel from transformers.models.vit_msn.modeling_vit_msn import VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class UpperCAmelCase__ : """simple docstring""" def __init__( self : int ,_a : List[str] ,_a : Optional[Any]=13 ,_a : str=30 ,_a : str=2 ,_a : Union[str, Any]=3 ,_a : Optional[Any]=True ,_a : int=True ,_a : Union[str, Any]=32 ,_a : List[Any]=5 ,_a : Union[str, Any]=4 ,_a : int=37 ,_a : Any="gelu" ,_a : Union[str, Any]=0.1 ,_a : str=0.1 ,_a : List[str]=10 ,_a : Dict=0.02 ,_a : Tuple=None ,): '''simple docstring''' _a : Any = parent _a : int = batch_size _a : List[Any] = image_size _a : Optional[int] = patch_size _a : List[str] = num_channels _a : Dict = is_training _a : Dict = use_labels _a : Optional[Any] = hidden_size _a : str = num_hidden_layers _a : Optional[int] = num_attention_heads _a : Dict = intermediate_size _a : Union[str, Any] = hidden_act _a : List[str] = hidden_dropout_prob _a : Any = attention_probs_dropout_prob _a : List[str] = type_sequence_label_size _a : int = initializer_range _a : List[Any] = scope # in ViT MSN, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) _a : Union[str, Any] = (image_size // patch_size) ** 2 _a : Tuple = num_patches + 1 def __lowercase ( self : Any ): '''simple docstring''' _a : Tuple = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _a : str = None if self.use_labels: _a : Tuple = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) _a : List[str] = self.get_config() return config, pixel_values, labels def __lowercase ( self : Optional[int] ): '''simple docstring''' return ViTMSNConfig( 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 ,initializer_range=self.initializer_range ,) def __lowercase ( self : Tuple ,_a : Any ,_a : List[Any] ,_a : int ): '''simple docstring''' _a : str = ViTMSNModel(config=_a ) model.to(_a ) model.eval() _a : int = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def __lowercase ( self : List[Any] ,_a : str ,_a : Tuple ,_a : Dict ): '''simple docstring''' _a : Tuple = self.type_sequence_label_size _a : int = ViTMSNForImageClassification(_a ) model.to(_a ) model.eval() _a : Dict = model(_a ,labels=_a ) print('Pixel and labels shape: {pixel_values.shape}, {labels.shape}' ) print('Labels: {labels}' ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.type_sequence_label_size) ) # test greyscale images _a : int = 1 _a : Optional[Any] = ViTMSNForImageClassification(_a ) model.to(_a ) model.eval() _a : Any = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) _a : Optional[int] = model(_a ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.type_sequence_label_size) ) def __lowercase ( self : Any ): '''simple docstring''' _a : Optional[int] = self.prepare_config_and_inputs() _a, _a, _a : int = config_and_inputs _a : List[Any] = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class UpperCAmelCase__ ( lowercase__ , lowercase__ , unittest.TestCase ): """simple docstring""" __UpperCAmelCase : Tuple = (ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else () __UpperCAmelCase : List[Any] = ( {'''feature-extraction''': ViTMSNModel, '''image-classification''': ViTMSNForImageClassification} if is_torch_available() else {} ) __UpperCAmelCase : str = False __UpperCAmelCase : Optional[Any] = False __UpperCAmelCase : List[str] = False __UpperCAmelCase : int = False def __lowercase ( self : Optional[int] ): '''simple docstring''' _a : List[str] = ViTMSNModelTester(self ) _a : Optional[int] = ConfigTester(self ,config_class=_a ,has_text_modality=_a ,hidden_size=37 ) def __lowercase ( self : str ): '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason='ViTMSN does not use inputs_embeds' ) def __lowercase ( self : List[str] ): '''simple docstring''' pass def __lowercase ( self : Union[str, Any] ): '''simple docstring''' _a, _a : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : List[Any] = model_class(_a ) self.assertIsInstance(model.get_input_embeddings() ,(nn.Module) ) _a : Dict = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_a ,nn.Linear ) ) def __lowercase ( self : Any ): '''simple docstring''' _a, _a : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : List[str] = model_class(_a ) _a : str = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a : List[Any] = [*signature.parameters.keys()] _a : int = ['pixel_values'] self.assertListEqual(arg_names[:1] ,_a ) def __lowercase ( self : List[str] ): '''simple docstring''' _a : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def __lowercase ( self : Optional[Any] ): '''simple docstring''' _a : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_a ) @slow def __lowercase ( self : int ): '''simple docstring''' for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a : Dict = ViTMSNModel.from_pretrained(_a ) self.assertIsNotNone(_a ) def UpperCAmelCase_ (): """simple docstring""" _a : List[str] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class UpperCAmelCase__ ( unittest.TestCase ): """simple docstring""" @cached_property def __lowercase ( self : Union[str, Any] ): '''simple docstring''' return ViTImageProcessor.from_pretrained('facebook/vit-msn-small' ) if is_vision_available() else None @slow def __lowercase ( self : Union[str, Any] ): '''simple docstring''' torch.manual_seed(2 ) _a : List[str] = ViTMSNForImageClassification.from_pretrained('facebook/vit-msn-small' ).to(_a ) _a : List[str] = self.default_image_processor _a : int = prepare_img() _a : Tuple = image_processor(images=_a ,return_tensors='pt' ).to(_a ) # forward pass with torch.no_grad(): _a : Optional[int] = model(**_a ) # verify the logits _a : Union[str, Any] = torch.Size((1, 1000) ) self.assertEqual(outputs.logits.shape ,_a ) _a : List[Any] = torch.tensor([-0.0803, -0.4454, -0.2375] ).to(_a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] ,_a ,atol=1E-4 ) )
5
0
'''simple docstring''' import argparse import json import numpy import torch from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() def UpperCAmelCase_ (__a : Any , __a : Optional[Any] ): """simple docstring""" _a : Any = torch.load(__a , map_location='cpu' ) _a : List[Any] = chkpt['model'] # We have the base model one level deeper than the original XLM repository _a : List[str] = {} for k, v in state_dict.items(): if "pred_layer" in k: _a : List[str] = v else: _a : Dict = v _a : Tuple = chkpt['params'] _a : List[str] = {n: v for n, v in config.items() if not isinstance(__a , (torch.FloatTensor, numpy.ndarray) )} _a : List[Any] = chkpt['dico_word2id'] _a : List[Any] = {s + '</w>' if s.find('@@' ) == -1 and i > 1_3 else s.replace('@@' , '' ): i for s, i in vocab.items()} # Save pytorch-model _a : Optional[Any] = pytorch_dump_folder_path + '/' + WEIGHTS_NAME _a : int = pytorch_dump_folder_path + '/' + CONFIG_NAME _a : Any = pytorch_dump_folder_path + '/' + VOCAB_FILES_NAMES['vocab_file'] print(f"""Save PyTorch model to {pytorch_weights_dump_path}""" ) torch.save(__a , __a ) print(f"""Save configuration file to {pytorch_config_dump_path}""" ) with open(__a , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(__a , indent=2 ) + '\n' ) print(f"""Save vocab file to {pytorch_config_dump_path}""" ) with open(__a , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(__a , indent=2 ) + '\n' ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( """--xlm_checkpoint_path""", default=None, type=str, required=True, help="""Path the official PyTorch dump.""" ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model.""" ) __lowerCAmelCase = parser.parse_args() convert_xlm_checkpoint_to_pytorch(args.xlm_checkpoint_path, args.pytorch_dump_folder_path)
362
'''simple docstring''' import requests from bsa import BeautifulSoup def UpperCAmelCase_ (__a : str = "https://www.worldometers.info/coronavirus" ): """simple docstring""" _a : List[str] = BeautifulSoup(requests.get(__a ).text , 'html.parser' ) _a : Dict = soup.findAll('h1' ) _a : Union[str, Any] = soup.findAll('div' , {'class': 'maincounter-number'} ) keys += soup.findAll('span' , {'class': 'panel-title'} ) values += soup.findAll('div' , {'class': 'number-table-main'} ) return {key.text.strip(): value.text.strip() for key, value in zip(__a , __a )} if __name__ == "__main__": print("""\033[1m""" + """COVID-19 Status of the World""" + """\033[0m\n""") for key, value in world_covidaa_stats().items(): print(f'''{key}\n{value}\n''')
5
0
'''simple docstring''' def UpperCAmelCase_ (__a : Optional[Any] ): """simple docstring""" _a : str = [False] * len(__a ) _a : Optional[int] = [-1] * len(__a ) def dfs(__a : Union[str, Any] , __a : str ): _a : List[str] = True _a : str = c for u in graph[v]: if not visited[u]: dfs(__a , 1 - c ) for i in range(len(__a ) ): if not visited[i]: dfs(__a , 0 ) for i in range(len(__a ) ): for j in graph[i]: if color[i] == color[j]: return False return True # Adjacency list of graph __lowerCAmelCase = {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: []} print(check_bipartite_dfs(graph))
363
'''simple docstring''' import argparse from collections import defaultdict import yaml __lowerCAmelCase = """docs/source/en/_toctree.yml""" def UpperCAmelCase_ (__a : str ): """simple docstring""" _a : Any = defaultdict(__a ) for doc in model_doc: counts[doc["local"]] += 1 _a : List[str] = [key for key, value in counts.items() if value > 1] _a : str = [] for duplicate_key in duplicates: _a : Union[str, Any] = list({doc['title'] for doc in model_doc if doc['local'] == duplicate_key} ) if len(__a ) > 1: raise ValueError( f"""{duplicate_key} is present several times in the documentation table of content at """ '`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the ' 'others.' ) # Only add this once new_doc.append({'local': duplicate_key, 'title': titles[0]} ) # Add none duplicate-keys new_doc.extend([doc for doc in model_doc if counts[doc['local']] == 1] ) # Sort return sorted(__a , key=lambda __a : s["title"].lower() ) def UpperCAmelCase_ (__a : Optional[int]=False ): """simple docstring""" with open(__a , encoding='utf-8' ) as f: _a : Tuple = yaml.safe_load(f.read() ) # Get to the API doc _a : Union[str, Any] = 0 while content[api_idx]["title"] != "API": api_idx += 1 _a : Union[str, Any] = content[api_idx]['sections'] # Then to the model doc _a : List[str] = 0 while api_doc[model_idx]["title"] != "Models": model_idx += 1 _a : List[str] = api_doc[model_idx]['sections'] _a : List[Any] = [(idx, section) for idx, section in enumerate(__a ) if 'sections' in section] _a : Tuple = False for idx, modality_doc in modalities_docs: _a : List[Any] = modality_doc['sections'] _a : Any = clean_model_doc_toc(__a ) if old_modality_doc != new_modality_doc: _a : Union[str, Any] = True if overwrite: _a : str = new_modality_doc if diff: if overwrite: _a : Dict = model_doc _a : Dict = api_doc with open(__a , 'w' , encoding='utf-8' ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) else: raise ValueError( 'The model doc part of the table of content is not properly sorted, run `make style` to fix this.' ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() parser.add_argument("""--fix_and_overwrite""", action="""store_true""", help="""Whether to fix inconsistencies.""") __lowerCAmelCase = parser.parse_args() check_model_doc(args.fix_and_overwrite)
5
0
'''simple docstring''' from typing import List, Optional, Union import numpy as np import torch import torchaudio.compliance.kaldi as ta_kaldi from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging __lowerCAmelCase = logging.get_logger(__name__) class UpperCAmelCase__ ( lowercase__ ): """simple docstring""" __UpperCAmelCase : List[str] = ['''input_features''', '''attention_mask'''] def __init__( self : str ,_a : Optional[Any]=80 ,_a : Tuple=1_6000 ,_a : Tuple=80 ,_a : Optional[Any]=0.0 ,_a : Optional[int]=True ,_a : Optional[int]=True ,_a : str=True ,**_a : Optional[Any] ,): '''simple docstring''' super().__init__(feature_size=_a ,sampling_rate=_a ,padding_value=_a ,**_a ) _a : str = num_mel_bins _a : Dict = do_ceptral_normalize _a : Dict = normalize_means _a : List[Any] = normalize_vars _a : Optional[Any] = True def __lowercase ( self : Dict ,_a : np.ndarray ,): '''simple docstring''' _a : int = waveform * (2**15) # Kaldi compliance: 16-bit signed integers _a : Any = torch.from_numpy(_a ).unsqueeze(0 ) _a : Tuple = ta_kaldi.fbank(_a ,num_mel_bins=self.num_mel_bins ,sample_frequency=self.sampling_rate ) return features.numpy() @staticmethod def __lowercase ( _a : np.ndarray ,_a : int ,_a : Optional[bool] = True ,_a : Optional[bool] = True ,_a : float = 0.0 ,): '''simple docstring''' if normalize_means: _a : Union[str, Any] = x[:input_length].mean(axis=0 ) _a : Any = np.subtract(_a ,_a ) if normalize_vars: _a : Union[str, Any] = x[:input_length].std(axis=0 ) _a : str = np.divide(_a ,_a ) if input_length < x.shape[0]: _a : List[Any] = padding_value # make sure array is in float32 _a : int = x.astype(np.floataa ) return x def __lowercase ( self : int ,_a : List[np.ndarray] ,_a : Optional[np.ndarray] = None ): '''simple docstring''' _a : Union[str, Any] = attention_mask.sum(-1 ) if attention_mask is not None else [x.shape[0] for x in input_features] return [ self.utterance_cmvn(_a ,_a ,self.normalize_means ,self.normalize_vars ,self.padding_value ) for x, n in zip(_a ,_a ) ] def __call__( self : Optional[Any] ,_a : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] ,_a : Union[bool, str, PaddingStrategy] = False ,_a : Optional[int] = None ,_a : bool = False ,_a : Optional[int] = None ,_a : Optional[Union[str, TensorType]] = None ,_a : Optional[int] = None ,_a : Optional[bool] = None ,**_a : List[str] ,): '''simple docstring''' if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F"""The model corresponding to this feature extractor: {self} was trained using a sampling rate of""" F""" {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with""" F""" {self.sampling_rate} and not {sampling_rate}.""" ) else: logger.warning( 'It is strongly recommended to pass the `sampling_rate` argument to this function. ' 'Failing to do so can result in silent errors that might be hard to debug.' ) _a : List[Any] = isinstance(_a ,np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(F"""Only mono-channel audio is supported for input to {self}""" ) _a : Dict = is_batched_numpy or ( isinstance(_a ,(list, tuple) ) and (isinstance(raw_speech[0] ,(np.ndarray, tuple, list) )) ) if is_batched: _a : Tuple = [np.asarray(_a ,dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(_a ,np.ndarray ): _a : Tuple = np.asarray(_a ,dtype=np.floataa ) elif isinstance(_a ,np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): _a : List[str] = raw_speech.astype(np.floataa ) # always return batch if not is_batched: _a : Any = [raw_speech] # extract fbank features _a : Union[str, Any] = [self._extract_fbank_features(_a ) for waveform in raw_speech] # convert into correct format for padding _a : Tuple = BatchFeature({'input_features': features} ) _a : int = self.pad( _a ,padding=_a ,max_length=_a ,truncation=_a ,pad_to_multiple_of=_a ,return_attention_mask=_a ,**_a ,) # make sure list is in array format _a : Union[str, Any] = padded_inputs.get('input_features' ) if isinstance(input_features[0] ,_a ): _a : int = [np.asarray(_a ,dtype=np.floataa ) for feature in input_features] _a : List[Any] = padded_inputs.get('attention_mask' ) if attention_mask is not None: _a : List[Any] = [np.asarray(_a ,dtype=np.intaa ) for array in attention_mask] # Utterance-level cepstral mean and variance normalization if self.do_ceptral_normalize: _a : Optional[Any] = ( np.array(_a ,dtype=np.intaa ) if self._get_padding_strategies(_a ,max_length=_a ) is not PaddingStrategy.DO_NOT_PAD else None ) _a : List[str] = self.normalize( padded_inputs['input_features'] ,attention_mask=_a ) if return_tensors is not None: _a : str = padded_inputs.convert_to_tensors(_a ) return padded_inputs
364
'''simple docstring''' from collections.abc import Generator from math import sin def UpperCAmelCase_ (__a : bytes ): """simple docstring""" if len(__a ) != 3_2: raise ValueError('Input must be of length 32' ) _a : Any = b'' for i in [3, 2, 1, 0]: little_endian += string_aa[8 * i : 8 * i + 8] return little_endian def UpperCAmelCase_ (__a : int ): """simple docstring""" if i < 0: raise ValueError('Input must be non-negative' ) _a : List[str] = format(__a , '08x' )[-8:] _a : str = b'' for i in [3, 2, 1, 0]: little_endian_hex += hex_rep[2 * i : 2 * i + 2].encode('utf-8' ) return little_endian_hex def UpperCAmelCase_ (__a : bytes ): """simple docstring""" _a : List[Any] = b'' for char in message: bit_string += format(__a , '08b' ).encode('utf-8' ) _a : int = format(len(__a ) , '064b' ).encode('utf-8' ) # Pad bit_string to a multiple of 512 chars bit_string += b"1" while len(__a ) % 5_1_2 != 4_4_8: bit_string += b"0" bit_string += to_little_endian(start_len[3_2:] ) + to_little_endian(start_len[:3_2] ) return bit_string def UpperCAmelCase_ (__a : bytes ): """simple docstring""" if len(__a ) % 5_1_2 != 0: raise ValueError('Input must have length that\'s a multiple of 512' ) for pos in range(0 , len(__a ) , 5_1_2 ): _a : List[Any] = bit_string[pos : pos + 5_1_2] _a : str = [] for i in range(0 , 5_1_2 , 3_2 ): block_words.append(int(to_little_endian(block[i : i + 3_2] ) , 2 ) ) yield block_words def UpperCAmelCase_ (__a : int ): """simple docstring""" if i < 0: raise ValueError('Input must be non-negative' ) _a : List[str] = format(__a , '032b' ) _a : int = '' for c in i_str: new_str += "1" if c == "0" else "0" return int(__a , 2 ) def UpperCAmelCase_ (__a : int , __a : int ): """simple docstring""" return (a + b) % 2**3_2 def UpperCAmelCase_ (__a : int , __a : int ): """simple docstring""" if i < 0: raise ValueError('Input must be non-negative' ) if shift < 0: raise ValueError('Shift must be non-negative' ) return ((i << shift) ^ (i >> (3_2 - shift))) % 2**3_2 def UpperCAmelCase_ (__a : bytes ): """simple docstring""" _a : str = preprocess(__a ) _a : Optional[int] = [int(2**3_2 * abs(sin(i + 1 ) ) ) for i in range(6_4 )] # Starting states _a : int = 0x67_45_23_01 _a : Union[str, Any] = 0xEF_CD_AB_89 _a : str = 0x98_BA_DC_FE _a : List[Any] = 0x10_32_54_76 _a : Optional[int] = [ 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, ] # Process bit string in chunks, each with 16 32-char words for block_words in get_block_words(__a ): _a : Union[str, Any] = aa _a : List[Any] = ba _a : List[Any] = ca _a : Dict = da # Hash current chunk for i in range(6_4 ): if i <= 1_5: # f = (b & c) | (not_32(b) & d) # Alternate definition for f _a : Optional[int] = d ^ (b & (c ^ d)) _a : Optional[Any] = i elif i <= 3_1: # f = (d & b) | (not_32(d) & c) # Alternate definition for f _a : Optional[Any] = c ^ (d & (b ^ c)) _a : Dict = (5 * i + 1) % 1_6 elif i <= 4_7: _a : Optional[Any] = b ^ c ^ d _a : Dict = (3 * i + 5) % 1_6 else: _a : int = c ^ (b | not_aa(__a )) _a : List[str] = (7 * i) % 1_6 _a : Optional[int] = (f + a + added_consts[i] + block_words[g]) % 2**3_2 _a : Union[str, Any] = d _a : Tuple = c _a : Optional[int] = b _a : Union[str, Any] = sum_aa(__a , left_rotate_aa(__a , shift_amounts[i] ) ) # Add hashed chunk to running total _a : Any = sum_aa(__a , __a ) _a : Dict = sum_aa(__a , __a ) _a : Union[str, Any] = sum_aa(__a , __a ) _a : str = sum_aa(__a , __a ) _a : Optional[Any] = reformat_hex(__a ) + reformat_hex(__a ) + reformat_hex(__a ) + reformat_hex(__a ) return digest if __name__ == "__main__": import doctest doctest.testmod()
5
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tensorflow_text_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __lowerCAmelCase = { """configuration_bert""": ["""BERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """BertConfig""", """BertOnnxConfig"""], """tokenization_bert""": ["""BasicTokenizer""", """BertTokenizer""", """WordpieceTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = ["""BertTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ """BERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """BertForMaskedLM""", """BertForMultipleChoice""", """BertForNextSentencePrediction""", """BertForPreTraining""", """BertForQuestionAnswering""", """BertForSequenceClassification""", """BertForTokenClassification""", """BertLayer""", """BertLMHeadModel""", """BertModel""", """BertPreTrainedModel""", """load_tf_weights_in_bert""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ """TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFBertEmbeddings""", """TFBertForMaskedLM""", """TFBertForMultipleChoice""", """TFBertForNextSentencePrediction""", """TFBertForPreTraining""", """TFBertForQuestionAnswering""", """TFBertForSequenceClassification""", """TFBertForTokenClassification""", """TFBertLMHeadModel""", """TFBertMainLayer""", """TFBertModel""", """TFBertPreTrainedModel""", ] try: if not is_tensorflow_text_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = ["""TFBertTokenizer"""] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ """FlaxBertForCausalLM""", """FlaxBertForMaskedLM""", """FlaxBertForMultipleChoice""", """FlaxBertForNextSentencePrediction""", """FlaxBertForPreTraining""", """FlaxBertForQuestionAnswering""", """FlaxBertForSequenceClassification""", """FlaxBertForTokenClassification""", """FlaxBertModel""", """FlaxBertPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_bert import BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, BertConfig, BertOnnxConfig from .tokenization_bert import BasicTokenizer, BertTokenizer, WordpieceTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bert_fast import BertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bert import ( BERT_PRETRAINED_MODEL_ARCHIVE_LIST, BertForMaskedLM, BertForMultipleChoice, BertForNextSentencePrediction, BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification, BertForTokenClassification, BertLayer, BertLMHeadModel, BertModel, BertPreTrainedModel, load_tf_weights_in_bert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_bert import ( TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFBertEmbeddings, TFBertForMaskedLM, TFBertForMultipleChoice, TFBertForNextSentencePrediction, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertForTokenClassification, TFBertLMHeadModel, TFBertMainLayer, TFBertModel, TFBertPreTrainedModel, ) try: if not is_tensorflow_text_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bert_tf import TFBertTokenizer try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_bert import ( FlaxBertForCausalLM, FlaxBertForMaskedLM, FlaxBertForMultipleChoice, FlaxBertForNextSentencePrediction, FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification, FlaxBertForTokenClassification, FlaxBertModel, FlaxBertPreTrainedModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
365
'''simple docstring''' import math from collections import defaultdict from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin, SchedulerOutput def UpperCAmelCase_ (__a : str , __a : Dict=0.999 , __a : List[str]="cosine" , ): """simple docstring""" if alpha_transform_type == "cosine": def alpha_bar_fn(__a : Union[str, Any] ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(__a : int ): return math.exp(t * -12.0 ) else: raise ValueError(f"""Unsupported alpha_tranform_type: {alpha_transform_type}""" ) _a : Tuple = [] for i in range(__a ): _a : Union[str, Any] = i / num_diffusion_timesteps _a : Any = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(__a ) / alpha_bar_fn(__a ) , __a ) ) return torch.tensor(__a , dtype=torch.floataa ) class UpperCAmelCase__ ( lowercase__ , lowercase__ ): """simple docstring""" __UpperCAmelCase : Optional[Any] = [e.name for e in KarrasDiffusionSchedulers] __UpperCAmelCase : Dict = 2 @register_to_config def __init__( self : str ,_a : int = 1000 ,_a : float = 0.0_0085 ,_a : float = 0.012 ,_a : str = "linear" ,_a : Optional[Union[np.ndarray, List[float]]] = None ,_a : str = "epsilon" ,_a : Optional[bool] = False ,_a : Optional[bool] = False ,_a : float = 1.0 ,_a : str = "linspace" ,_a : int = 0 ,): '''simple docstring''' if trained_betas is not None: _a : List[str] = torch.tensor(_a ,dtype=torch.floataa ) elif beta_schedule == "linear": _a : Tuple = torch.linspace(_a ,_a ,_a ,dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. _a : List[str] = ( torch.linspace(beta_start**0.5 ,beta_end**0.5 ,_a ,dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule _a : Dict = betas_for_alpha_bar(_a ,alpha_transform_type='cosine' ) elif beta_schedule == "exp": _a : Tuple = betas_for_alpha_bar(_a ,alpha_transform_type='exp' ) else: raise NotImplementedError(F"""{beta_schedule} does is not implemented for {self.__class__}""" ) _a : Optional[Any] = 1.0 - self.betas _a : Optional[int] = torch.cumprod(self.alphas ,dim=0 ) # set all values self.set_timesteps(_a ,_a ,_a ) _a : Optional[int] = use_karras_sigmas def __lowercase ( self : Any ,_a : Union[str, Any] ,_a : Optional[Any]=None ): '''simple docstring''' if schedule_timesteps is None: _a : List[Any] = self.timesteps _a : Dict = (schedule_timesteps == timestep).nonzero() # The sigma index that is taken for the **very** first `step` # is always the second index (or the last index if there is only 1) # This way we can ensure we don't accidentally skip a sigma in # case we start in the middle of the denoising schedule (e.g. for image-to-image) if len(self._index_counter ) == 0: _a : int = 1 if len(_a ) > 1 else 0 else: _a : str = timestep.cpu().item() if torch.is_tensor(_a ) else timestep _a : str = self._index_counter[timestep_int] return indices[pos].item() @property def __lowercase ( self : Optional[Any] ): '''simple docstring''' if self.config.timestep_spacing in ["linspace", "trailing"]: return self.sigmas.max() return (self.sigmas.max() ** 2 + 1) ** 0.5 def __lowercase ( self : int ,_a : torch.FloatTensor ,_a : Union[float, torch.FloatTensor] ,): '''simple docstring''' _a : List[Any] = self.index_for_timestep(_a ) _a : Tuple = self.sigmas[step_index] _a : Optional[Any] = sample / ((sigma**2 + 1) ** 0.5) return sample def __lowercase ( self : Any ,_a : int ,_a : Union[str, torch.device] = None ,_a : Optional[int] = None ,): '''simple docstring''' _a : Optional[Any] = num_inference_steps _a : Dict = num_train_timesteps or self.config.num_train_timesteps # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891 if self.config.timestep_spacing == "linspace": _a : Optional[Any] = np.linspace(0 ,num_train_timesteps - 1 ,_a ,dtype=_a )[::-1].copy() elif self.config.timestep_spacing == "leading": _a : str = num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 _a : int = (np.arange(0 ,_a ) * step_ratio).round()[::-1].copy().astype(_a ) timesteps += self.config.steps_offset elif self.config.timestep_spacing == "trailing": _a : Any = num_train_timesteps / self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 _a : Union[str, Any] = (np.arange(_a ,0 ,-step_ratio )).round().copy().astype(_a ) timesteps -= 1 else: raise ValueError( F"""{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'.""" ) _a : Tuple = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5 ) _a : Union[str, Any] = np.log(_a ) _a : str = np.interp(_a ,np.arange(0 ,len(_a ) ) ,_a ) if self.config.use_karras_sigmas: _a : List[Any] = self._convert_to_karras(in_sigmas=_a ,num_inference_steps=self.num_inference_steps ) _a : Dict = np.array([self._sigma_to_t(_a ,_a ) for sigma in sigmas] ) _a : int = np.concatenate([sigmas, [0.0]] ).astype(np.floataa ) _a : Union[str, Any] = torch.from_numpy(_a ).to(device=_a ) _a : Any = torch.cat([sigmas[:1], sigmas[1:-1].repeat_interleave(2 ), sigmas[-1:]] ) _a : List[Any] = torch.from_numpy(_a ) _a : List[str] = torch.cat([timesteps[:1], timesteps[1:].repeat_interleave(2 )] ) if str(_a ).startswith('mps' ): # mps does not support float64 _a : Tuple = timesteps.to(_a ,dtype=torch.floataa ) else: _a : Dict = timesteps.to(device=_a ) # empty dt and derivative _a : Tuple = None _a : Optional[Any] = None # for exp beta schedules, such as the one for `pipeline_shap_e.py` # we need an index counter _a : Union[str, Any] = defaultdict(_a ) def __lowercase ( self : str ,_a : Dict ,_a : Dict ): '''simple docstring''' _a : Optional[int] = np.log(_a ) # get distribution _a : Union[str, Any] = log_sigma - log_sigmas[:, np.newaxis] # get sigmas range _a : List[Any] = np.cumsum((dists >= 0) ,axis=0 ).argmax(axis=0 ).clip(max=log_sigmas.shape[0] - 2 ) _a : Tuple = low_idx + 1 _a : Union[str, Any] = log_sigmas[low_idx] _a : Optional[Any] = log_sigmas[high_idx] # interpolate sigmas _a : Optional[Any] = (low - log_sigma) / (low - high) _a : List[str] = np.clip(_a ,0 ,1 ) # transform interpolation to time range _a : Union[str, Any] = (1 - w) * low_idx + w * high_idx _a : List[str] = t.reshape(sigma.shape ) return t def __lowercase ( self : int ,_a : torch.FloatTensor ,_a : Tuple ): '''simple docstring''' _a : float = in_sigmas[-1].item() _a : float = in_sigmas[0].item() _a : Tuple = 7.0 # 7.0 is the value used in the paper _a : str = np.linspace(0 ,1 ,_a ) _a : Optional[Any] = sigma_min ** (1 / rho) _a : Union[str, Any] = sigma_max ** (1 / rho) _a : str = (max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho return sigmas @property def __lowercase ( self : Optional[Any] ): '''simple docstring''' return self.dt is None def __lowercase ( self : int ,_a : Union[torch.FloatTensor, np.ndarray] ,_a : Union[float, torch.FloatTensor] ,_a : Union[torch.FloatTensor, np.ndarray] ,_a : bool = True ,): '''simple docstring''' _a : Union[str, Any] = self.index_for_timestep(_a ) # advance index counter by 1 _a : Any = timestep.cpu().item() if torch.is_tensor(_a ) else timestep self._index_counter[timestep_int] += 1 if self.state_in_first_order: _a : Tuple = self.sigmas[step_index] _a : int = self.sigmas[step_index + 1] else: # 2nd order / Heun's method _a : List[str] = self.sigmas[step_index - 1] _a : List[Any] = self.sigmas[step_index] # currently only gamma=0 is supported. This usually works best anyways. # We can support gamma in the future but then need to scale the timestep before # passing it to the model which requires a change in API _a : Optional[int] = 0 _a : Tuple = sigma * (gamma + 1) # Note: sigma_hat == sigma for now # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise if self.config.prediction_type == "epsilon": _a : Dict = sigma_hat if self.state_in_first_order else sigma_next _a : Optional[int] = sample - sigma_input * model_output elif self.config.prediction_type == "v_prediction": _a : List[Any] = sigma_hat if self.state_in_first_order else sigma_next _a : List[Any] = model_output * (-sigma_input / (sigma_input**2 + 1) ** 0.5) + ( sample / (sigma_input**2 + 1) ) elif self.config.prediction_type == "sample": _a : Union[str, Any] = model_output else: raise ValueError( F"""prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`""" ) if self.config.clip_sample: _a : Optional[int] = pred_original_sample.clamp( -self.config.clip_sample_range ,self.config.clip_sample_range ) if self.state_in_first_order: # 2. Convert to an ODE derivative for 1st order _a : Optional[Any] = (sample - pred_original_sample) / sigma_hat # 3. delta timestep _a : Any = sigma_next - sigma_hat # store for 2nd order step _a : int = derivative _a : List[str] = dt _a : Union[str, Any] = sample else: # 2. 2nd order / Heun's method _a : Dict = (sample - pred_original_sample) / sigma_next _a : Tuple = (self.prev_derivative + derivative) / 2 # 3. take prev timestep & sample _a : Optional[Any] = self.dt _a : Union[str, Any] = self.sample # free dt and derivative # Note, this puts the scheduler in "first order mode" _a : List[Any] = None _a : Union[str, Any] = None _a : Dict = None _a : str = sample + derivative * dt if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=_a ) def __lowercase ( self : Optional[int] ,_a : torch.FloatTensor ,_a : torch.FloatTensor ,_a : torch.FloatTensor ,): '''simple docstring''' _a : str = self.sigmas.to(device=original_samples.device ,dtype=original_samples.dtype ) if original_samples.device.type == "mps" and torch.is_floating_point(_a ): # mps does not support float64 _a : Dict = self.timesteps.to(original_samples.device ,dtype=torch.floataa ) _a : Optional[Any] = timesteps.to(original_samples.device ,dtype=torch.floataa ) else: _a : int = self.timesteps.to(original_samples.device ) _a : Optional[Any] = timesteps.to(original_samples.device ) _a : Any = [self.index_for_timestep(_a ,_a ) for t in timesteps] _a : Optional[int] = sigmas[step_indices].flatten() while len(sigma.shape ) < len(original_samples.shape ): _a : Optional[Any] = sigma.unsqueeze(-1 ) _a : Any = original_samples + noise * sigma return noisy_samples def __len__( self : Optional[int] ): '''simple docstring''' return self.config.num_train_timesteps
5
0
'''simple docstring''' def UpperCAmelCase_ (__a : int = 1_0**1_2 ): """simple docstring""" _a : List[str] = 1 _a : Optional[int] = 0 _a : Any = 1 _a : List[str] = 1 while numerator <= 2 * min_total - 1: prev_numerator += 2 * numerator numerator += 2 * prev_numerator prev_denominator += 2 * denominator denominator += 2 * prev_denominator return (denominator + 1) // 2 if __name__ == "__main__": print(f'''{solution() = }''')
366
'''simple docstring''' import qiskit def UpperCAmelCase_ (__a : int , __a : int ): """simple docstring""" _a : Any = qiskit.Aer.get_backend('aer_simulator' ) # Create a Quantum Circuit acting on the q register _a : List[Any] = qiskit.QuantumCircuit(__a , __a ) # Map the quantum measurement to the classical bits circuit.measure([0] , [0] ) # Execute the circuit on the simulator _a : Tuple = qiskit.execute(__a , __a , shots=1_0_0_0 ) # Return the histogram data of the results of the experiment. return job.result().get_counts(__a ) if __name__ == "__main__": print(f'''Total count for various states are: {single_qubit_measure(1, 1)}''')
5
0
'''simple docstring''' __lowerCAmelCase = """2.13.1""" import platform import pyarrow from packaging import version if version.parse(platform.python_version()) < version.parse("""3.7"""): raise ImportWarning( """To use `datasets`, Python>=3.7 is required, and the current version of Python doesn't match this condition.""" ) if version.parse(pyarrow.__version__).major < 8: raise ImportWarning( """To use `datasets`, the module `pyarrow>=8.0.0` is required, and the current version of `pyarrow` doesn't match this condition.\n""" """If you are running this in a Google Colab, you should probably just restart the runtime to use the right version of `pyarrow`.""" ) del platform del pyarrow del version from .arrow_dataset import Dataset from .arrow_reader import ReadInstruction from .builder import ArrowBasedBuilder, BeamBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder from .combine import concatenate_datasets, interleave_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .download import * from .features import * from .fingerprint import disable_caching, enable_caching, is_caching_enabled, set_caching_enabled from .info import DatasetInfo, MetricInfo from .inspect import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, list_datasets, list_metrics, ) from .iterable_dataset import IterableDataset from .load import load_dataset, load_dataset_builder, load_from_disk, load_metric from .metric import Metric from .splits import ( NamedSplit, NamedSplitAll, Split, SplitBase, SplitDict, SplitGenerator, SplitInfo, SubSplitInfo, percent, ) from .tasks import * from .utils import * from .utils import logging # deprecated modules from datasets import arrow_dataset as _arrow_dataset # isort:skip from datasets import utils as _utils # isort:skip from datasets.utils import download_manager as _deprecated_download_manager # isort:skip __lowerCAmelCase = concatenate_datasets __lowerCAmelCase = DownloadConfig __lowerCAmelCase = DownloadManager __lowerCAmelCase = DownloadMode __lowerCAmelCase = DownloadConfig __lowerCAmelCase = DownloadMode __lowerCAmelCase = DownloadManager del _arrow_dataset, _utils, _deprecated_download_manager
367
'''simple docstring''' import gc import unittest from diffusers import FlaxStableDiffusionInpaintPipeline from diffusers.utils import is_flax_available, load_image, slow from diffusers.utils.testing_utils import require_flax if is_flax_available(): import jax import jax.numpy as jnp from flax.jax_utils import replicate from flax.training.common_utils import shard @slow @require_flax class UpperCAmelCase__ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self : Optional[Any] ): '''simple docstring''' super().tearDown() gc.collect() def __lowercase ( self : str ): '''simple docstring''' _a : Optional[Any] = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/sd2-inpaint/init_image.png' ) _a : Optional[int] = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png' ) _a : List[str] = 'xvjiarui/stable-diffusion-2-inpainting' _a, _a : str = FlaxStableDiffusionInpaintPipeline.from_pretrained(_a ,safety_checker=_a ) _a : str = 'Face of a yellow cat, high resolution, sitting on a park bench' _a : int = jax.random.PRNGKey(0 ) _a : Tuple = 50 _a : Any = jax.device_count() _a : Dict = num_samples * [prompt] _a : Optional[Any] = num_samples * [init_image] _a : str = num_samples * [mask_image] _a, _a, _a : Optional[Any] = pipeline.prepare_inputs(_a ,_a ,_a ) # shard inputs and rng _a : Optional[Any] = replicate(_a ) _a : str = jax.random.split(_a ,jax.device_count() ) _a : Dict = shard(_a ) _a : int = shard(_a ) _a : int = shard(_a ) _a : Union[str, Any] = pipeline( _a ,_a ,_a ,_a ,_a ,_a ,jit=_a ) _a : Union[str, Any] = output.images.reshape(_a ,512 ,512 ,3 ) _a : Union[str, Any] = images[0, 253:256, 253:256, -1] _a : str = jnp.asarray(jax.device_get(image_slice.flatten() ) ) _a : Union[str, Any] = jnp.array( [0.361_1307, 0.3764_9736, 0.375_7408, 0.3821_3953, 0.3929_5167, 0.384_1631, 0.4155_4978, 0.413_7475, 0.421_7084] ) print(F"""output_slice: {output_slice}""" ) assert jnp.abs(output_slice - expected_slice ).max() < 1E-2
5
0
'''simple docstring''' import json import os from typing import Optional import numpy as np from ...feature_extraction_utils import BatchFeature from ...processing_utils import ProcessorMixin from ...utils import logging from ...utils.hub import get_file_from_repo from ..auto import AutoTokenizer __lowerCAmelCase = logging.get_logger(__name__) class UpperCAmelCase__ ( lowercase__ ): """simple docstring""" __UpperCAmelCase : List[Any] = '''AutoTokenizer''' __UpperCAmelCase : str = ['''tokenizer'''] __UpperCAmelCase : List[str] = { '''semantic_prompt''': 1, '''coarse_prompt''': 2, '''fine_prompt''': 2, } def __init__( self : Dict ,_a : Any ,_a : Optional[int]=None ): '''simple docstring''' super().__init__(_a ) _a : Dict = speaker_embeddings @classmethod def __lowercase ( cls : Tuple ,_a : Any ,_a : Dict="speaker_embeddings_path.json" ,**_a : Any ): '''simple docstring''' if speaker_embeddings_dict_path is not None: _a : Optional[Any] = get_file_from_repo( _a ,_a ,subfolder=kwargs.pop('subfolder' ,_a ) ,cache_dir=kwargs.pop('cache_dir' ,_a ) ,force_download=kwargs.pop('force_download' ,_a ) ,proxies=kwargs.pop('proxies' ,_a ) ,resume_download=kwargs.pop('resume_download' ,_a ) ,local_files_only=kwargs.pop('local_files_only' ,_a ) ,use_auth_token=kwargs.pop('use_auth_token' ,_a ) ,revision=kwargs.pop('revision' ,_a ) ,) if speaker_embeddings_path is None: logger.warning( F"""`{os.path.join(_a ,_a )}` does not exists , no preloaded speaker embeddings will be used - Make sure to provide a correct path to the json dictionnary if wanted, otherwise set `speaker_embeddings_dict_path=None`.""" ) _a : Tuple = None else: with open(_a ) as speaker_embeddings_json: _a : Tuple = json.load(_a ) else: _a : Any = None _a : Dict = AutoTokenizer.from_pretrained(_a ,**_a ) return cls(tokenizer=_a ,speaker_embeddings=_a ) def __lowercase ( self : str ,_a : Tuple ,_a : List[str]="speaker_embeddings_path.json" ,_a : Tuple="speaker_embeddings" ,_a : bool = False ,**_a : List[Any] ,): '''simple docstring''' if self.speaker_embeddings is not None: os.makedirs(os.path.join(_a ,_a ,'v2' ) ,exist_ok=_a ) _a : Optional[int] = {} _a : int = save_directory for prompt_key in self.speaker_embeddings: if prompt_key != "repo_or_path": _a : Tuple = self._load_voice_preset(_a ) _a : List[Any] = {} for key in self.speaker_embeddings[prompt_key]: np.save( os.path.join( embeddings_dict['repo_or_path'] ,_a ,F"""{prompt_key}_{key}""" ) ,voice_preset[key] ,allow_pickle=_a ,) _a : Optional[Any] = os.path.join(_a ,F"""{prompt_key}_{key}.npy""" ) _a : Optional[int] = tmp_dict with open(os.path.join(_a ,_a ) ,'w' ) as fp: json.dump(_a ,_a ) super().save_pretrained(_a ,_a ,**_a ) def __lowercase ( self : Optional[Any] ,_a : str = None ,**_a : Dict ): '''simple docstring''' _a : Dict = self.speaker_embeddings[voice_preset] _a : int = {} for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]: if key not in voice_preset_paths: raise ValueError( F"""Voice preset unrecognized, missing {key} as a key in self.speaker_embeddings[{voice_preset}].""" ) _a : Optional[Any] = get_file_from_repo( self.speaker_embeddings.get('repo_or_path' ,'/' ) ,voice_preset_paths[key] ,subfolder=kwargs.pop('subfolder' ,_a ) ,cache_dir=kwargs.pop('cache_dir' ,_a ) ,force_download=kwargs.pop('force_download' ,_a ) ,proxies=kwargs.pop('proxies' ,_a ) ,resume_download=kwargs.pop('resume_download' ,_a ) ,local_files_only=kwargs.pop('local_files_only' ,_a ) ,use_auth_token=kwargs.pop('use_auth_token' ,_a ) ,revision=kwargs.pop('revision' ,_a ) ,) if path is None: raise ValueError( F"""`{os.path.join(self.speaker_embeddings.get('repo_or_path' ,'/' ) ,voice_preset_paths[key] )}` does not exists , no preloaded voice preset will be used - Make sure to provide correct paths to the {voice_preset} embeddings.""" ) _a : int = np.load(_a ) return voice_preset_dict def __lowercase ( self : Dict ,_a : Optional[dict] = None ): '''simple docstring''' for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]: if key not in voice_preset: raise ValueError(F"""Voice preset unrecognized, missing {key} as a key.""" ) if not isinstance(voice_preset[key] ,np.ndarray ): raise ValueError(F"""{key} voice preset must be a {str(self.preset_shape[key] )}D ndarray.""" ) if len(voice_preset[key].shape ) != self.preset_shape[key]: raise ValueError(F"""{key} voice preset must be a {str(self.preset_shape[key] )}D ndarray.""" ) def __call__( self : int ,_a : str=None ,_a : Optional[Any]=None ,_a : Optional[Any]="pt" ,_a : List[Any]=256 ,_a : List[Any]=False ,_a : str=True ,_a : Optional[Any]=False ,**_a : str ,): '''simple docstring''' if voice_preset is not None and not isinstance(_a ,_a ): if ( isinstance(_a ,_a ) and self.speaker_embeddings is not None and voice_preset in self.speaker_embeddings ): _a : Tuple = self._load_voice_preset(_a ) else: if isinstance(_a ,_a ) and not voice_preset.endswith('.npz' ): _a : int = voice_preset + '.npz' _a : Dict = np.load(_a ) if voice_preset is not None: self._validate_voice_preset_dict(_a ,**_a ) _a : List[str] = BatchFeature(data=_a ,tensor_type=_a ) _a : List[str] = self.tokenizer( _a ,return_tensors=_a ,padding='max_length' ,max_length=_a ,return_attention_mask=_a ,return_token_type_ids=_a ,add_special_tokens=_a ,**_a ,) if voice_preset is not None: _a : Any = voice_preset return encoded_text
368
'''simple docstring''' def UpperCAmelCase_ (__a : str , __a : str ): """simple docstring""" _a : int = len(__a ) + 1 _a : List[str] = len(__a ) + 1 # dp is a 2d matrix where dp[i][j] denotes whether prefix string of # length i of input_string matches with prefix string of length j of # given pattern. # "dp" stands for dynamic programming. _a : Optional[int] = [[0 for i in range(__a )] for j in range(__a )] # since string of zero length match pattern of zero length _a : str = 1 # since pattern of zero length will never match with string of non-zero length for i in range(1 , __a ): _a : Optional[Any] = 0 # since string of zero length will match with pattern where there # is at least one * alternatively for j in range(1 , __a ): _a : Dict = dp[0][j - 2] if pattern[j - 1] == '*' else 0 # now using bottom-up approach to find for all remaining lengths for i in range(1 , __a ): for j in range(1 , __a ): if input_string[i - 1] == pattern[j - 1] or pattern[j - 1] == ".": _a : Tuple = dp[i - 1][j - 1] elif pattern[j - 1] == "*": if dp[i][j - 2] == 1: _a : List[str] = 1 elif pattern[j - 2] in (input_string[i - 1], "."): _a : int = dp[i - 1][j] else: _a : Any = 0 else: _a : Optional[Any] = 0 return bool(dp[-1][-1] ) if __name__ == "__main__": import doctest doctest.testmod() # inputing the strings # input_string = input("input a string :") # pattern = input("input a pattern :") __lowerCAmelCase = """aab""" __lowerCAmelCase = """c*a*b""" # using function to check whether given string matches the given pattern if match_pattern(input_string, pattern): print(f'''{input_string} matches the given pattern {pattern}''') else: print(f'''{input_string} does not match with the given pattern {pattern}''')
5
0
'''simple docstring''' from __future__ import annotations def UpperCAmelCase_ (__a : list[int] ): """simple docstring""" if len(__a ) == 0: return array _a : Dict = min(__a ), max(__a ) # Compute the variables _a : Optional[int] = _max - _min + 1 _a : Optional[Any] = [0] * holes_range, [0] * holes_range # Make the sorting. for i in array: _a : Union[str, Any] = i - _min _a : Union[str, Any] = i holes_repeat[index] += 1 # Makes the array back by replacing the numbers. _a : Optional[Any] = 0 for i in range(__a ): while holes_repeat[i] > 0: _a : List[Any] = holes[i] index += 1 holes_repeat[i] -= 1 # Returns the sorted array. return array if __name__ == "__main__": import doctest doctest.testmod() __lowerCAmelCase = input("""Enter numbers separated by comma:\n""") __lowerCAmelCase = [int(x) for x in user_input.split(""",""")] print(pigeon_sort(unsorted))
369
'''simple docstring''' import json import os import unittest from transformers.models.blenderbot_small.tokenization_blenderbot_small import ( VOCAB_FILES_NAMES, BlenderbotSmallTokenizer, ) from ...test_tokenization_common import TokenizerTesterMixin class UpperCAmelCase__ ( lowercase__ , unittest.TestCase ): """simple docstring""" __UpperCAmelCase : Dict = BlenderbotSmallTokenizer __UpperCAmelCase : Tuple = False def __lowercase ( self : List[Any] ): '''simple docstring''' super().setUp() _a : List[str] = ['__start__', 'adapt', 'act', 'ap@@', 'te', '__end__', '__unk__'] _a : Tuple = dict(zip(_a ,range(len(_a ) ) ) ) _a : List[Any] = ['#version: 0.2', 'a p', 't e</w>', 'ap t</w>', 'a d', 'ad apt</w>', 'a c', 'ac t</w>', ''] _a : List[Any] = {'unk_token': '__unk__', 'bos_token': '__start__', 'eos_token': '__end__'} _a : List[Any] = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES['vocab_file'] ) _a : str = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file ,'w' ,encoding='utf-8' ) as fp: fp.write(json.dumps(_a ) + '\n' ) with open(self.merges_file ,'w' ,encoding='utf-8' ) as fp: fp.write('\n'.join(_a ) ) def __lowercase ( self : List[Any] ,**_a : int ): '''simple docstring''' kwargs.update(self.special_tokens_map ) return BlenderbotSmallTokenizer.from_pretrained(self.tmpdirname ,**_a ) def __lowercase ( self : Tuple ,_a : int ): '''simple docstring''' _a : Optional[Any] = 'adapt act apte' _a : Dict = 'adapt act apte' return input_text, output_text def __lowercase ( self : int ): '''simple docstring''' _a : Optional[int] = BlenderbotSmallTokenizer(self.vocab_file ,self.merges_file ,**self.special_tokens_map ) _a : Union[str, Any] = 'adapt act apte' _a : Dict = ['adapt', 'act', 'ap@@', 'te'] _a : Tuple = tokenizer.tokenize(_a ) self.assertListEqual(_a ,_a ) _a : List[str] = [tokenizer.bos_token] + tokens + [tokenizer.eos_token] _a : Dict = [0, 1, 2, 3, 4, 5] self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) ,_a ) def __lowercase ( self : Optional[int] ): '''simple docstring''' _a : str = BlenderbotSmallTokenizer.from_pretrained('facebook/blenderbot-90M' ) assert tok('sam' ).input_ids == [1384] _a : Union[str, Any] = 'I am a small frog.' _a : int = tok([src_text] ,padding=_a ,truncation=_a )['input_ids'] _a : str = tok.batch_decode(_a ,skip_special_tokens=_a ,clean_up_tokenization_spaces=_a )[0] assert src_text != decoded # I wish it did! assert decoded == "i am a small frog ." def __lowercase ( self : Any ): '''simple docstring''' _a : Optional[int] = BlenderbotSmallTokenizer.from_pretrained('facebook/blenderbot-90M' ) _a : Union[str, Any] = 'I am a small frog .' _a : Optional[Any] = '.' _a : Optional[Any] = tok(_a )['input_ids'] _a : Union[str, Any] = tok(_a )['input_ids'] assert encoded[-1] == encoded_dot[0]
5
0
'''simple docstring''' import math import time from transformers import Trainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput, speed_metrics if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class UpperCAmelCase__ ( lowercase__ ): """simple docstring""" def __init__( self : Union[str, Any] ,*_a : Optional[Any] ,_a : List[str]=None ,_a : Tuple=None ,**_a : int ): '''simple docstring''' super().__init__(*_a ,**_a ) _a : int = eval_examples _a : Optional[Any] = post_process_function def __lowercase ( self : Tuple ,_a : Optional[int]=None ,_a : Optional[Any]=None ,_a : int=None ,_a : str = "eval" ): '''simple docstring''' _a : Dict = self.eval_dataset if eval_dataset is None else eval_dataset _a : Union[str, Any] = self.get_eval_dataloader(_a ) _a : Tuple = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. _a : int = self.compute_metrics _a : List[str] = None _a : Tuple = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop _a : List[Any] = time.time() try: _a : Optional[Any] = eval_loop( _a ,description='Evaluation' ,prediction_loss_only=True if compute_metrics is None else None ,ignore_keys=_a ,metric_key_prefix=_a ,) finally: _a : List[Any] = compute_metrics _a : Optional[Any] = self.args.eval_batch_size * self.args.world_size if F"""{metric_key_prefix}_jit_compilation_time""" in output.metrics: start_time += output.metrics[F"""{metric_key_prefix}_jit_compilation_time"""] output.metrics.update( speed_metrics( _a ,_a ,num_samples=output.num_samples ,num_steps=math.ceil(output.num_samples / total_batch_size ) ,) ) if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save: # Only the main node write the results by default _a : List[Any] = self.post_process_function(_a ,_a ,output.predictions ) _a : str = self.compute_metrics(_a ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(F"""{metric_key_prefix}_""" ): _a : Optional[int] = metrics.pop(_a ) metrics.update(output.metrics ) else: _a : Dict = output.metrics if self.args.should_log: # Only the main node log the results by default self.log(_a ) if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report() ) _a : Optional[int] = self.callback_handler.on_evaluate(self.args ,self.state ,self.control ,_a ) return metrics def __lowercase ( self : Union[str, Any] ,_a : Optional[Any] ,_a : List[Any] ,_a : int=None ,_a : str = "test" ): '''simple docstring''' _a : Optional[int] = self.get_test_dataloader(_a ) # Temporarily disable metric computation, we will do it in the loop here. _a : int = self.compute_metrics _a : Tuple = None _a : Dict = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop _a : List[Any] = time.time() try: _a : str = eval_loop( _a ,description='Prediction' ,prediction_loss_only=True if compute_metrics is None else None ,ignore_keys=_a ,metric_key_prefix=_a ,) finally: _a : Optional[int] = compute_metrics _a : Any = self.args.eval_batch_size * self.args.world_size if F"""{metric_key_prefix}_jit_compilation_time""" in output.metrics: start_time += output.metrics[F"""{metric_key_prefix}_jit_compilation_time"""] output.metrics.update( speed_metrics( _a ,_a ,num_samples=output.num_samples ,num_steps=math.ceil(output.num_samples / total_batch_size ) ,) ) if self.post_process_function is None or self.compute_metrics is None: return output _a : Dict = self.post_process_function(_a ,_a ,output.predictions ,'predict' ) _a : Optional[Any] = self.compute_metrics(_a ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(F"""{metric_key_prefix}_""" ): _a : Tuple = metrics.pop(_a ) metrics.update(output.metrics ) return PredictionOutput(predictions=predictions.predictions ,label_ids=predictions.label_ids ,metrics=_a )
370
'''simple docstring''' __lowerCAmelCase = { """A""": """.-""", """B""": """-...""", """C""": """-.-.""", """D""": """-..""", """E""": """.""", """F""": """..-.""", """G""": """--.""", """H""": """....""", """I""": """..""", """J""": """.---""", """K""": """-.-""", """L""": """.-..""", """M""": """--""", """N""": """-.""", """O""": """---""", """P""": """.--.""", """Q""": """--.-""", """R""": """.-.""", """S""": """...""", """T""": """-""", """U""": """..-""", """V""": """...-""", """W""": """.--""", """X""": """-..-""", """Y""": """-.--""", """Z""": """--..""", """1""": """.----""", """2""": """..---""", """3""": """...--""", """4""": """....-""", """5""": """.....""", """6""": """-....""", """7""": """--...""", """8""": """---..""", """9""": """----.""", """0""": """-----""", """&""": """.-...""", """@""": """.--.-.""", """:""": """---...""", """,""": """--..--""", """.""": """.-.-.-""", """'""": """.----.""", """\"""": """.-..-.""", """?""": """..--..""", """/""": """-..-.""", """=""": """-...-""", """+""": """.-.-.""", """-""": """-....-""", """(""": """-.--.""", """)""": """-.--.-""", """!""": """-.-.--""", """ """: """/""" } # Exclamation mark is not in ITU-R recommendation # fmt: on __lowerCAmelCase = {value: key for key, value in MORSE_CODE_DICT.items()} def UpperCAmelCase_ (__a : str ): """simple docstring""" return " ".join(MORSE_CODE_DICT[char] for char in message.upper() ) def UpperCAmelCase_ (__a : str ): """simple docstring""" return "".join(REVERSE_DICT[char] for char in message.split() ) def UpperCAmelCase_ (): """simple docstring""" _a : List[str] = 'Morse code here!' print(__a ) _a : Tuple = encrypt(__a ) print(__a ) _a : str = decrypt(__a ) print(__a ) if __name__ == "__main__": main()
5
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) __lowerCAmelCase = { """configuration_mega""": ["""MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP""", """MegaConfig""", """MegaOnnxConfig"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ """MEGA_PRETRAINED_MODEL_ARCHIVE_LIST""", """MegaForCausalLM""", """MegaForMaskedLM""", """MegaForMultipleChoice""", """MegaForQuestionAnswering""", """MegaForSequenceClassification""", """MegaForTokenClassification""", """MegaModel""", """MegaPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_mega import MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP, MegaConfig, MegaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mega import ( MEGA_PRETRAINED_MODEL_ARCHIVE_LIST, MegaForCausalLM, MegaForMaskedLM, MegaForMultipleChoice, MegaForQuestionAnswering, MegaForSequenceClassification, MegaForTokenClassification, MegaModel, MegaPreTrainedModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
371
'''simple docstring''' import json import os import shutil import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoConfig, BertConfig, GPTaConfig from transformers.configuration_utils import PretrainedConfig from transformers.testing_utils import TOKEN, USER, is_staging_test sys.path.append(str(Path(__file__).parent.parent / """utils""")) from test_module.custom_configuration import CustomConfig # noqa E402 __lowerCAmelCase = { """return_dict""": False, """output_hidden_states""": True, """output_attentions""": True, """torchscript""": True, """torch_dtype""": """float16""", """use_bfloat16""": True, """tf_legacy_loss""": True, """pruned_heads""": {"""a""": 1}, """tie_word_embeddings""": False, """is_decoder""": True, """cross_attention_hidden_size""": 1_2_8, """add_cross_attention""": True, """tie_encoder_decoder""": True, """max_length""": 5_0, """min_length""": 3, """do_sample""": True, """early_stopping""": True, """num_beams""": 3, """num_beam_groups""": 3, """diversity_penalty""": 0.5, """temperature""": 2.0, """top_k""": 1_0, """top_p""": 0.7, """typical_p""": 0.2, """repetition_penalty""": 0.8, """length_penalty""": 0.8, """no_repeat_ngram_size""": 5, """encoder_no_repeat_ngram_size""": 5, """bad_words_ids""": [1, 2, 3], """num_return_sequences""": 3, """chunk_size_feed_forward""": 5, """output_scores""": True, """return_dict_in_generate""": True, """forced_bos_token_id""": 2, """forced_eos_token_id""": 3, """remove_invalid_values""": True, """architectures""": ["""BertModel"""], """finetuning_task""": """translation""", """id2label""": {0: """label"""}, """label2id""": {"""label""": """0"""}, """tokenizer_class""": """BertTokenizerFast""", """prefix""": """prefix""", """bos_token_id""": 6, """pad_token_id""": 7, """eos_token_id""": 8, """sep_token_id""": 9, """decoder_start_token_id""": 1_0, """exponential_decay_length_penalty""": (5, 1.01), """suppress_tokens""": [0, 1], """begin_suppress_tokens""": 2, """task_specific_params""": {"""translation""": """some_params"""}, """problem_type""": """regression""", } @is_staging_test class UpperCAmelCase__ ( unittest.TestCase ): """simple docstring""" @classmethod def __lowercase ( cls : Optional[Any] ): '''simple docstring''' _a : List[Any] = TOKEN HfFolder.save_token(_a ) @classmethod def __lowercase ( cls : List[Any] ): '''simple docstring''' try: delete_repo(token=cls._token ,repo_id='test-config' ) except HTTPError: pass try: delete_repo(token=cls._token ,repo_id='valid_org/test-config-org' ) except HTTPError: pass try: delete_repo(token=cls._token ,repo_id='test-dynamic-config' ) except HTTPError: pass def __lowercase ( self : List[str] ): '''simple docstring''' _a : Any = BertConfig( vocab_size=99 ,hidden_size=32 ,num_hidden_layers=5 ,num_attention_heads=4 ,intermediate_size=37 ) config.push_to_hub('test-config' ,use_auth_token=self._token ) _a : Optional[Any] = BertConfig.from_pretrained(F"""{USER}/test-config""" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(_a ,getattr(_a ,_a ) ) # Reset repo delete_repo(token=self._token ,repo_id='test-config' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(_a ,repo_id='test-config' ,push_to_hub=_a ,use_auth_token=self._token ) _a : Dict = BertConfig.from_pretrained(F"""{USER}/test-config""" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(_a ,getattr(_a ,_a ) ) def __lowercase ( self : Optional[Any] ): '''simple docstring''' _a : Tuple = BertConfig( vocab_size=99 ,hidden_size=32 ,num_hidden_layers=5 ,num_attention_heads=4 ,intermediate_size=37 ) config.push_to_hub('valid_org/test-config-org' ,use_auth_token=self._token ) _a : Union[str, Any] = BertConfig.from_pretrained('valid_org/test-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(_a ,getattr(_a ,_a ) ) # Reset repo delete_repo(token=self._token ,repo_id='valid_org/test-config-org' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( _a ,repo_id='valid_org/test-config-org' ,push_to_hub=_a ,use_auth_token=self._token ) _a : Tuple = BertConfig.from_pretrained('valid_org/test-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(_a ,getattr(_a ,_a ) ) def __lowercase ( self : List[Any] ): '''simple docstring''' CustomConfig.register_for_auto_class() _a : Optional[Any] = CustomConfig(attribute=42 ) config.push_to_hub('test-dynamic-config' ,use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual(config.auto_map ,{'AutoConfig': 'custom_configuration.CustomConfig'} ) _a : int = AutoConfig.from_pretrained(F"""{USER}/test-dynamic-config""" ,trust_remote_code=_a ) # Can't make an isinstance check because the new_config is from the FakeConfig class of a dynamic module self.assertEqual(new_config.__class__.__name__ ,'CustomConfig' ) self.assertEqual(new_config.attribute ,42 ) class UpperCAmelCase__ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self : Union[str, Any] ): '''simple docstring''' _a : Optional[Any] = GPTaConfig() # attempt to modify each of int/float/bool/str config records and verify they were updated _a : int = c.n_embd + 1 # int _a : str = c.resid_pdrop + 1.0 # float _a : Dict = not c.scale_attn_weights # bool _a : List[Any] = c.summary_type + 'foo' # str c.update_from_string( F"""n_embd={n_embd},resid_pdrop={resid_pdrop},scale_attn_weights={scale_attn_weights},summary_type={summary_type}""" ) self.assertEqual(_a ,c.n_embd ,'mismatch for key: n_embd' ) self.assertEqual(_a ,c.resid_pdrop ,'mismatch for key: resid_pdrop' ) self.assertEqual(_a ,c.scale_attn_weights ,'mismatch for key: scale_attn_weights' ) self.assertEqual(_a ,c.summary_type ,'mismatch for key: summary_type' ) def __lowercase ( self : List[str] ): '''simple docstring''' _a : int = PretrainedConfig() _a : int = [key for key in base_config.__dict__ if key not in config_common_kwargs] # If this part of the test fails, you have arguments to addin config_common_kwargs above. self.assertListEqual( _a ,['is_encoder_decoder', '_name_or_path', '_commit_hash', 'transformers_version'] ) _a : Dict = [key for key, value in config_common_kwargs.items() if value == getattr(_a ,_a )] if len(_a ) > 0: raise ValueError( 'The following keys are set with the default values in' ' `test_configuration_common.config_common_kwargs` pick another value for them:' F""" {', '.join(_a )}.""" ) def __lowercase ( self : Optional[Any] ): '''simple docstring''' with self.assertRaises(_a ): # config is in subfolder, the following should not work without specifying the subfolder _a : List[Any] = BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert-subfolder' ) _a : List[str] = BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert-subfolder' ,subfolder='bert' ) self.assertIsNotNone(_a ) def __lowercase ( self : List[Any] ): '''simple docstring''' _a : List[Any] = mock.Mock() _a : Any = 500 _a : Any = {} _a : Any = HTTPError _a : List[Any] = {} # Download this model to make sure it's in the cache. _a : Any = BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert' ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch('requests.Session.request' ,return_value=_a ) as mock_head: _a : Optional[int] = BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert' ) # This check we did call the fake head request mock_head.assert_called() def __lowercase ( self : Optional[Any] ): '''simple docstring''' _a : Optional[int] = BertConfig.from_pretrained( 'https://huggingface.co/hf-internal-testing/tiny-random-bert/resolve/main/config.json' ) def __lowercase ( self : Union[str, Any] ): '''simple docstring''' _a : int = AutoConfig.from_pretrained('bert-base-cased' ) _a : List[str] = ['config.4.0.0.json'] with tempfile.TemporaryDirectory() as tmp_dir: configuration.save_pretrained(_a ) _a : str = 2 json.dump(configuration.to_dict() ,open(os.path.join(_a ,'config.4.0.0.json' ) ,'w' ) ) # This should pick the new configuration file as the version of Transformers is > 4.0.0 _a : int = AutoConfig.from_pretrained(_a ) self.assertEqual(new_configuration.hidden_size ,2 ) # Will need to be adjusted if we reach v42 and this test is still here. # Should pick the old configuration file as the version of Transformers is < 4.42.0 _a : Tuple = ['config.42.0.0.json'] _a : int = 768 configuration.save_pretrained(_a ) shutil.move(os.path.join(_a ,'config.4.0.0.json' ) ,os.path.join(_a ,'config.42.0.0.json' ) ) _a : int = AutoConfig.from_pretrained(_a ) self.assertEqual(new_configuration.hidden_size ,768 ) def __lowercase ( self : str ): '''simple docstring''' _a : Tuple = 'hf-internal-testing/test-two-configs' import transformers as new_transformers _a : Optional[int] = 'v4.0.0' _a, _a : Tuple = new_transformers.models.auto.AutoConfig.from_pretrained( _a ,return_unused_kwargs=_a ) self.assertEqual(new_configuration.hidden_size ,2 ) # This checks `_configuration_file` ia not kept in the kwargs by mistake. self.assertDictEqual(_a ,{} ) # Testing an older version by monkey-patching the version in the module it's used. import transformers as old_transformers _a : str = 'v3.0.0' _a : Optional[Any] = old_transformers.models.auto.AutoConfig.from_pretrained(_a ) self.assertEqual(old_configuration.hidden_size ,768 )
5
0
'''simple docstring''' import argparse import json import torch from diffusers import DDPMScheduler, LDMPipeline, UNetaDModel, VQModel def UpperCAmelCase_ (__a : Optional[Any] , __a : Tuple=1 ): if n_shave_prefix_segments >= 0: return ".".join(path.split('.' )[n_shave_prefix_segments:] ) else: return ".".join(path.split('.' )[:n_shave_prefix_segments] ) def UpperCAmelCase_ (__a : Optional[int] , __a : Union[str, Any]=0 ): _a : Dict = [] for old_item in old_list: _a : List[str] = old_item.replace('in_layers.0' , 'norm1' ) _a : Any = new_item.replace('in_layers.2' , 'conv1' ) _a : Dict = new_item.replace('out_layers.0' , 'norm2' ) _a : str = new_item.replace('out_layers.3' , 'conv2' ) _a : Tuple = new_item.replace('emb_layers.1' , 'time_emb_proj' ) _a : Dict = new_item.replace('skip_connection' , 'conv_shortcut' ) _a : List[Any] = shave_segments(__a , n_shave_prefix_segments=__a ) mapping.append({'old': old_item, 'new': new_item} ) return mapping def UpperCAmelCase_ (__a : List[str] , __a : Tuple=0 ): _a : int = [] for old_item in old_list: _a : List[str] = old_item _a : Union[str, Any] = new_item.replace('norm.weight' , 'group_norm.weight' ) _a : Tuple = new_item.replace('norm.bias' , 'group_norm.bias' ) _a : Dict = new_item.replace('proj_out.weight' , 'proj_attn.weight' ) _a : Dict = new_item.replace('proj_out.bias' , 'proj_attn.bias' ) _a : Optional[Any] = shave_segments(__a , n_shave_prefix_segments=__a ) mapping.append({'old': old_item, 'new': new_item} ) return mapping def UpperCAmelCase_ (__a : Optional[Any] , __a : str , __a : Tuple , __a : Union[str, Any]=None , __a : Any=None , __a : Optional[Any]=None ): assert isinstance(__a , __a ), "Paths should be a list of dicts containing 'old' and 'new' keys." # Splits the attention layers into three variables. if attention_paths_to_split is not None: for path, path_map in attention_paths_to_split.items(): _a : str = old_checkpoint[path] _a : List[Any] = old_tensor.shape[0] // 3 _a : Union[str, Any] = (-1, channels) if len(old_tensor.shape ) == 3 else (-1) _a : List[Any] = old_tensor.shape[0] // config['num_head_channels'] // 3 _a : str = old_tensor.reshape((num_heads, 3 * channels // num_heads) + old_tensor.shape[1:] ) _a : Any = old_tensor.split(channels // num_heads , dim=1 ) _a : Dict = query.reshape(__a ) _a : Tuple = key.reshape(__a ) _a : List[str] = value.reshape(__a ) for path in paths: _a : List[str] = path['new'] # These have already been assigned if attention_paths_to_split is not None and new_path in attention_paths_to_split: continue # Global renaming happens here _a : int = new_path.replace('middle_block.0' , 'mid_block.resnets.0' ) _a : Optional[Any] = new_path.replace('middle_block.1' , 'mid_block.attentions.0' ) _a : Tuple = new_path.replace('middle_block.2' , 'mid_block.resnets.1' ) if additional_replacements is not None: for replacement in additional_replacements: _a : Optional[Any] = new_path.replace(replacement['old'] , replacement['new'] ) # proj_attn.weight has to be converted from conv 1D to linear if "proj_attn.weight" in new_path: _a : Dict = old_checkpoint[path['old']][:, :, 0] else: _a : int = old_checkpoint[path['old']] def UpperCAmelCase_ (__a : List[str] , __a : str ): _a : Tuple = {} _a : Any = checkpoint['time_embed.0.weight'] _a : Dict = checkpoint['time_embed.0.bias'] _a : Dict = checkpoint['time_embed.2.weight'] _a : List[Any] = checkpoint['time_embed.2.bias'] _a : Union[str, Any] = checkpoint['input_blocks.0.0.weight'] _a : Tuple = checkpoint['input_blocks.0.0.bias'] _a : int = checkpoint['out.0.weight'] _a : List[str] = checkpoint['out.0.bias'] _a : List[str] = checkpoint['out.2.weight'] _a : str = checkpoint['out.2.bias'] # Retrieves the keys for the input blocks only _a : int = len({'.'.join(layer.split('.' )[:2] ) for layer in checkpoint if 'input_blocks' in layer} ) _a : Optional[Any] = { layer_id: [key for key in checkpoint if f"""input_blocks.{layer_id}""" in key] for layer_id in range(__a ) } # Retrieves the keys for the middle blocks only _a : Dict = len({'.'.join(layer.split('.' )[:2] ) for layer in checkpoint if 'middle_block' in layer} ) _a : Union[str, Any] = { layer_id: [key for key in checkpoint if f"""middle_block.{layer_id}""" in key] for layer_id in range(__a ) } # Retrieves the keys for the output blocks only _a : List[str] = len({'.'.join(layer.split('.' )[:2] ) for layer in checkpoint if 'output_blocks' in layer} ) _a : Optional[Any] = { layer_id: [key for key in checkpoint if f"""output_blocks.{layer_id}""" in key] for layer_id in range(__a ) } for i in range(1 , __a ): _a : Union[str, Any] = (i - 1) // (config['num_res_blocks'] + 1) _a : int = (i - 1) % (config['num_res_blocks'] + 1) _a : List[Any] = [key for key in input_blocks[i] if f"""input_blocks.{i}.0""" in key] _a : Union[str, Any] = [key for key in input_blocks[i] if f"""input_blocks.{i}.1""" in key] if f"""input_blocks.{i}.0.op.weight""" in checkpoint: _a : List[Any] = checkpoint[ f"""input_blocks.{i}.0.op.weight""" ] _a : Optional[Any] = checkpoint[ f"""input_blocks.{i}.0.op.bias""" ] continue _a : Dict = renew_resnet_paths(__a ) _a : List[Any] = {'old': f"""input_blocks.{i}.0""", 'new': f"""down_blocks.{block_id}.resnets.{layer_in_block_id}"""} _a : Optional[int] = {'old': 'resnets.2.op', 'new': 'downsamplers.0.op'} assign_to_checkpoint( __a , __a , __a , additional_replacements=[meta_path, resnet_op] , config=__a ) if len(__a ): _a : List[str] = renew_attention_paths(__a ) _a : Optional[Any] = { 'old': f"""input_blocks.{i}.1""", 'new': f"""down_blocks.{block_id}.attentions.{layer_in_block_id}""", } _a : int = { f"""input_blocks.{i}.1.qkv.bias""": { 'key': f"""down_blocks.{block_id}.attentions.{layer_in_block_id}.key.bias""", 'query': f"""down_blocks.{block_id}.attentions.{layer_in_block_id}.query.bias""", 'value': f"""down_blocks.{block_id}.attentions.{layer_in_block_id}.value.bias""", }, f"""input_blocks.{i}.1.qkv.weight""": { 'key': f"""down_blocks.{block_id}.attentions.{layer_in_block_id}.key.weight""", 'query': f"""down_blocks.{block_id}.attentions.{layer_in_block_id}.query.weight""", 'value': f"""down_blocks.{block_id}.attentions.{layer_in_block_id}.value.weight""", }, } assign_to_checkpoint( __a , __a , __a , additional_replacements=[meta_path] , attention_paths_to_split=__a , config=__a , ) _a : List[str] = middle_blocks[0] _a : Any = middle_blocks[1] _a : Tuple = middle_blocks[2] _a : str = renew_resnet_paths(__a ) assign_to_checkpoint(__a , __a , __a , config=__a ) _a : Union[str, Any] = renew_resnet_paths(__a ) assign_to_checkpoint(__a , __a , __a , config=__a ) _a : Tuple = renew_attention_paths(__a ) _a : Optional[Any] = { 'middle_block.1.qkv.bias': { 'key': 'mid_block.attentions.0.key.bias', 'query': 'mid_block.attentions.0.query.bias', 'value': 'mid_block.attentions.0.value.bias', }, 'middle_block.1.qkv.weight': { 'key': 'mid_block.attentions.0.key.weight', 'query': 'mid_block.attentions.0.query.weight', 'value': 'mid_block.attentions.0.value.weight', }, } assign_to_checkpoint( __a , __a , __a , attention_paths_to_split=__a , config=__a ) for i in range(__a ): _a : Any = i // (config['num_res_blocks'] + 1) _a : List[str] = i % (config['num_res_blocks'] + 1) _a : Optional[Any] = [shave_segments(__a , 2 ) for name in output_blocks[i]] _a : Tuple = {} for layer in output_block_layers: _a : int = layer.split('.' )[0], shave_segments(__a , 1 ) if layer_id in output_block_list: output_block_list[layer_id].append(__a ) else: _a : Optional[int] = [layer_name] if len(__a ) > 1: _a : Any = [key for key in output_blocks[i] if f"""output_blocks.{i}.0""" in key] _a : Dict = [key for key in output_blocks[i] if f"""output_blocks.{i}.1""" in key] _a : Optional[Any] = renew_resnet_paths(__a ) _a : Union[str, Any] = renew_resnet_paths(__a ) _a : List[Any] = {'old': f"""output_blocks.{i}.0""", 'new': f"""up_blocks.{block_id}.resnets.{layer_in_block_id}"""} assign_to_checkpoint(__a , __a , __a , additional_replacements=[meta_path] , config=__a ) if ["conv.weight", "conv.bias"] in output_block_list.values(): _a : Dict = list(output_block_list.values() ).index(['conv.weight', 'conv.bias'] ) _a : Tuple = checkpoint[ f"""output_blocks.{i}.{index}.conv.weight""" ] _a : List[Any] = checkpoint[ f"""output_blocks.{i}.{index}.conv.bias""" ] # Clear attentions as they have been attributed above. if len(__a ) == 2: _a : Tuple = [] if len(__a ): _a : Optional[int] = renew_attention_paths(__a ) _a : Dict = { 'old': f"""output_blocks.{i}.1""", 'new': f"""up_blocks.{block_id}.attentions.{layer_in_block_id}""", } _a : str = { f"""output_blocks.{i}.1.qkv.bias""": { 'key': f"""up_blocks.{block_id}.attentions.{layer_in_block_id}.key.bias""", 'query': f"""up_blocks.{block_id}.attentions.{layer_in_block_id}.query.bias""", 'value': f"""up_blocks.{block_id}.attentions.{layer_in_block_id}.value.bias""", }, f"""output_blocks.{i}.1.qkv.weight""": { 'key': f"""up_blocks.{block_id}.attentions.{layer_in_block_id}.key.weight""", 'query': f"""up_blocks.{block_id}.attentions.{layer_in_block_id}.query.weight""", 'value': f"""up_blocks.{block_id}.attentions.{layer_in_block_id}.value.weight""", }, } assign_to_checkpoint( __a , __a , __a , additional_replacements=[meta_path] , attention_paths_to_split=to_split if any('qkv' in key for key in attentions ) else None , config=__a , ) else: _a : Dict = renew_resnet_paths(__a , n_shave_prefix_segments=1 ) for path in resnet_0_paths: _a : List[Any] = '.'.join(['output_blocks', str(__a ), path['old']] ) _a : Tuple = '.'.join(['up_blocks', str(__a ), 'resnets', str(__a ), path['new']] ) _a : Any = checkpoint[old_path] return new_checkpoint if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() parser.add_argument( """--checkpoint_path""", default=None, type=str, required=True, help="""Path to the checkpoint to convert.""" ) parser.add_argument( """--config_file""", default=None, type=str, required=True, help="""The config json file corresponding to the architecture.""", ) parser.add_argument("""--dump_path""", default=None, type=str, required=True, help="""Path to the output model.""") __lowerCAmelCase = parser.parse_args() __lowerCAmelCase = torch.load(args.checkpoint_path) with open(args.config_file) as f: __lowerCAmelCase = json.loads(f.read()) __lowerCAmelCase = convert_ldm_checkpoint(checkpoint, config) if "ldm" in config: del config["ldm"] __lowerCAmelCase = UNetaDModel(**config) model.load_state_dict(converted_checkpoint) try: __lowerCAmelCase = DDPMScheduler.from_config("""/""".join(args.checkpoint_path.split("""/""")[:-1])) __lowerCAmelCase = VQModel.from_pretrained("""/""".join(args.checkpoint_path.split("""/""")[:-1])) __lowerCAmelCase = LDMPipeline(unet=model, scheduler=scheduler, vae=vqvae) pipe.save_pretrained(args.dump_path) except: # noqa: E722 model.save_pretrained(args.dump_path)
350
'''simple docstring''' import argparse import gc import json import os import re import torch from huggingface_hub import hf_hub_download from transformers import AutoModelForCausalLM, AutoTokenizer, PreTrainedTokenizerFast, RwkvConfig from transformers.modeling_utils import WEIGHTS_INDEX_NAME, shard_checkpoint __lowerCAmelCase = { """169M""": 1_2, """430M""": 2_4, """1B5""": 2_4, """3B""": 3_2, """7B""": 3_2, """14B""": 4_0, } __lowerCAmelCase = { """169M""": 7_6_8, """430M""": 1_0_2_4, """1B5""": 2_0_4_8, """3B""": 2_5_6_0, """7B""": 4_0_9_6, """14B""": 5_1_2_0, } def UpperCAmelCase_ (__a : Dict ): """simple docstring""" _a : List[Any] = list(state_dict.keys() ) for name in state_dict_keys: _a : List[Any] = state_dict.pop(__a ) # emb -> embedding if name.startswith('emb.' ): _a : List[str] = name.replace('emb.' , 'embeddings.' ) # ln_0 -> pre_ln (only present at block 0) if name.startswith('blocks.0.ln0' ): _a : Dict = name.replace('blocks.0.ln0' , 'blocks.0.pre_ln' ) # att -> attention _a : int = re.sub(R'blocks\.(\d+)\.att' , R'blocks.\1.attention' , __a ) # ffn -> feed_forward _a : str = re.sub(R'blocks\.(\d+)\.ffn' , R'blocks.\1.feed_forward' , __a ) # time_mix_k -> time_mix_key and reshape if name.endswith('.time_mix_k' ): _a : Any = name.replace('.time_mix_k' , '.time_mix_key' ) # time_mix_v -> time_mix_value and reshape if name.endswith('.time_mix_v' ): _a : int = name.replace('.time_mix_v' , '.time_mix_value' ) # time_mix_r -> time_mix_key and reshape if name.endswith('.time_mix_r' ): _a : Tuple = name.replace('.time_mix_r' , '.time_mix_receptance' ) if name != "head.weight": _a : Tuple = 'rwkv.' + name _a : List[Any] = weight return state_dict def UpperCAmelCase_ (__a : Tuple , __a : Union[str, Any] , __a : List[str] , __a : str=None , __a : List[str]=None , __a : int=False , __a : int=None ): """simple docstring""" if tokenizer_file is None: print('No `--tokenizer_file` provided, we will use the default tokenizer.' ) _a : List[Any] = 5_0_2_7_7 _a : Optional[Any] = AutoTokenizer.from_pretrained('EleutherAI/gpt-neox-20b' ) else: _a : Optional[Any] = PreTrainedTokenizerFast(tokenizer_file=__a ) _a : List[Any] = len(__a ) tokenizer.save_pretrained(__a ) # 2. Build the config _a : List[str] = list(NUM_HIDDEN_LAYERS_MAPPING.keys() ) if size is None: # Try to infer size from the checkpoint name for candidate in possible_sizes: if candidate in checkpoint_file: _a : str = candidate break if size is None: raise ValueError('Could not infer the size, please provide it with the `--size` argument.' ) if size not in possible_sizes: raise ValueError(f"""`size` should be one of {possible_sizes}, got {size}.""" ) _a : str = RwkvConfig( vocab_size=__a , num_hidden_layers=NUM_HIDDEN_LAYERS_MAPPING[size] , hidden_size=HIDEN_SIZE_MAPPING[size] , ) config.save_pretrained(__a ) # 3. Download model file then convert state_dict _a : Tuple = hf_hub_download(__a , __a ) _a : Optional[int] = torch.load(__a , map_location='cpu' ) _a : Dict = convert_state_dict(__a ) # 4. Split in shards and save _a, _a : List[Any] = shard_checkpoint(__a ) for shard_file, shard in shards.items(): torch.save(__a , os.path.join(__a , __a ) ) if index is not None: _a : Dict = os.path.join(__a , __a ) # Save the index as well with open(__a , 'w' , encoding='utf-8' ) as f: _a : List[Any] = json.dumps(__a , indent=2 , sort_keys=__a ) + '\n' f.write(__a ) # 5. Clean up shards (for some reason the file PyTorch saves take the same space as the whole state_dict print( 'Cleaning up shards. This may error with an OOM error, it this is the case don\'t worry you still have converted the model.' ) _a : List[Any] = list(shards.keys() ) del state_dict del shards gc.collect() for shard_file in shard_files: _a : Optional[Any] = torch.load(os.path.join(__a , __a ) ) torch.save({k: v.cpu().clone() for k, v in state_dict.items()} , os.path.join(__a , __a ) ) del state_dict gc.collect() if push_to_hub: if model_name is None: raise ValueError('Please provide a `model_name` to push the model to the Hub.' ) _a : List[str] = AutoModelForCausalLM.from_pretrained(__a ) model.push_to_hub(__a , max_shard_size='2GB' ) tokenizer.push_to_hub(__a ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( """--repo_id""", default=None, type=str, required=True, help="""Repo ID from which to pull the checkpoint.""" ) parser.add_argument( """--checkpoint_file""", default=None, type=str, required=True, help="""Name of the checkpoint file in the repo.""" ) parser.add_argument( """--output_dir""", default=None, type=str, required=True, help="""Where to save the converted model.""" ) parser.add_argument( """--tokenizer_file""", default=None, type=str, help="""Path to the tokenizer file to use (if not provided, only the model is converted).""", ) parser.add_argument( """--size""", default=None, type=str, help="""Size of the model. Will be inferred from the `checkpoint_file` if not passed.""", ) parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Push to the Hub the converted model.""", ) parser.add_argument( """--model_name""", default=None, type=str, help="""Name of the pushed model on the Hub, including the username / organization.""", ) __lowerCAmelCase = parser.parse_args() convert_rmkv_checkpoint_to_hf_format( args.repo_id, args.checkpoint_file, args.output_dir, size=args.size, tokenizer_file=args.tokenizer_file, push_to_hub=args.push_to_hub, model_name=args.model_name, )
5
0
'''simple docstring''' from __future__ import annotations __lowerCAmelCase = [ [-1, 0], # left [0, -1], # down [1, 0], # right [0, 1], # up ] def UpperCAmelCase_ (__a : list[list[int]] , __a : list[int] , __a : list[int] , __a : int , __a : list[list[int]] , ): """simple docstring""" _a : Any = [ [0 for col in range(len(grid[0] ) )] for row in range(len(__a ) ) ] # the reference grid _a : Optional[Any] = 1 _a : Tuple = [ [0 for col in range(len(grid[0] ) )] for row in range(len(__a ) ) ] # the action grid _a : List[str] = init[0] _a : Dict = init[1] _a : List[str] = 0 _a : Optional[int] = g + heuristic[x][y] # cost from starting cell to destination cell _a : Optional[int] = [[f, g, x, y]] _a : Optional[int] = False # flag that is set when search is complete _a : Dict = False # flag set if we can't find expand while not found and not resign: if len(__a ) == 0: raise ValueError('Algorithm is unable to find solution' ) else: # to choose the least costliest action so as to move closer to the goal cell.sort() cell.reverse() _a : List[Any] = cell.pop() _a : Dict = next_cell[2] _a : Union[str, Any] = next_cell[3] _a : List[Any] = next_cell[1] if x == goal[0] and y == goal[1]: _a : Dict = True else: for i in range(len(__a ) ): # to try out different valid actions _a : Any = x + DIRECTIONS[i][0] _a : Union[str, Any] = y + DIRECTIONS[i][1] if xa >= 0 and xa < len(__a ) and ya >= 0 and ya < len(grid[0] ): if closed[xa][ya] == 0 and grid[xa][ya] == 0: _a : Optional[int] = g + cost _a : List[Any] = ga + heuristic[xa][ya] cell.append([fa, ga, xa, ya] ) _a : str = 1 _a : Optional[int] = i _a : Union[str, Any] = [] _a : Union[str, Any] = goal[0] _a : Dict = goal[1] invpath.append([x, y] ) # we get the reverse path from here while x != init[0] or y != init[1]: _a : str = x - DIRECTIONS[action[x][y]][0] _a : int = y - DIRECTIONS[action[x][y]][1] _a : Dict = xa _a : Tuple = ya invpath.append([x, y] ) _a : Union[str, Any] = [] for i in range(len(__a ) ): path.append(invpath[len(__a ) - 1 - i] ) return path, action if __name__ == "__main__": __lowerCAmelCase = [ [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0], [0, 0, 0, 0, 1, 0], ] __lowerCAmelCase = [0, 0] # all coordinates are given in format [y,x] __lowerCAmelCase = [len(grid) - 1, len(grid[0]) - 1] __lowerCAmelCase = 1 # the cost map which pushes the path closer to the goal __lowerCAmelCase = [[0 for row in range(len(grid[0]))] for col in range(len(grid))] for i in range(len(grid)): for j in range(len(grid[0])): __lowerCAmelCase = abs(i - goal[0]) + abs(j - goal[1]) if grid[i][j] == 1: # added extra penalty in the heuristic map __lowerCAmelCase = 9_9 __lowerCAmelCase , __lowerCAmelCase = search(grid, init, goal, cost, heuristic) print("""ACTION MAP""") for i in range(len(action)): print(action[i]) for i in range(len(path)): print(path[i])
351
'''simple docstring''' import comet # From: unbabel-comet import torch import datasets __lowerCAmelCase = datasets.logging.get_logger(__name__) __lowerCAmelCase = """\ @inproceedings{rei-EtAl:2020:WMT, author = {Rei, Ricardo and Stewart, Craig and Farinha, Ana C and Lavie, Alon}, title = {Unbabel's Participation in the WMT20 Metrics Shared Task}, booktitle = {Proceedings of the Fifth Conference on Machine Translation}, month = {November}, year = {2020}, address = {Online}, publisher = {Association for Computational Linguistics}, pages = {909--918}, } @inproceedings{rei-etal-2020-comet, title = \"{COMET}: A Neural Framework for {MT} Evaluation\", author = \"Rei, Ricardo and Stewart, Craig and Farinha, Ana C and Lavie, Alon\", booktitle = \"Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP)\", month = nov, year = \"2020\", address = \"Online\", publisher = \"Association for Computational Linguistics\", url = \"https://www.aclweb.org/anthology/2020.emnlp-main.213\", pages = \"2685--2702\", } """ __lowerCAmelCase = """\ Crosslingual Optimized Metric for Evaluation of Translation (COMET) is an open-source framework used to train Machine Translation metrics that achieve high levels of correlation with different types of human judgments (HTER, DA's or MQM). With the release of the framework the authors also released fully trained models that were used to compete in the WMT20 Metrics Shared Task achieving SOTA in that years competition. See the [README.md] file at https://unbabel.github.io/COMET/html/models.html for more information. """ __lowerCAmelCase = """ COMET score. Args: `sources` (list of str): Source sentences `predictions` (list of str): candidate translations `references` (list of str): reference translations `cuda` (bool): If set to True, runs COMET using GPU `show_progress` (bool): Shows progress `model`: COMET model to be used. Will default to `wmt-large-da-estimator-1719` if None. Returns: `samples`: List of dictionaries with `src`, `mt`, `ref` and `score`. `scores`: List of scores. Examples: >>> comet_metric = datasets.load_metric('comet') >>> # comet_metric = load_metric('comet', 'wmt20-comet-da') # you can also choose which model to use >>> source = [\"Dem Feuer konnte Einhalt geboten werden\", \"Schulen und Kindergärten wurden eröffnet.\"] >>> hypothesis = [\"The fire could be stopped\", \"Schools and kindergartens were open\"] >>> reference = [\"They were able to control the fire.\", \"Schools and kindergartens opened\"] >>> results = comet_metric.compute(predictions=hypothesis, references=reference, sources=source) >>> print([round(v, 2) for v in results[\"scores\"]]) [0.19, 0.92] """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase__ ( datasets.Metric ): """simple docstring""" def __lowercase ( self : Optional[int] ): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,homepage='https://unbabel.github.io/COMET/html/index.html' ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { 'sources': datasets.Value('string' ,id='sequence' ), 'predictions': datasets.Value('string' ,id='sequence' ), 'references': datasets.Value('string' ,id='sequence' ), } ) ,codebase_urls=['https://github.com/Unbabel/COMET'] ,reference_urls=[ 'https://github.com/Unbabel/COMET', 'https://www.aclweb.org/anthology/2020.emnlp-main.213/', 'http://www.statmt.org/wmt20/pdf/2020.wmt-1.101.pdf6', ] ,) def __lowercase ( self : int ,_a : int ): '''simple docstring''' if self.config_name == "default": _a : List[Any] = comet.load_from_checkpoint(comet.download_model('wmt20-comet-da' ) ) else: _a : List[str] = comet.load_from_checkpoint(comet.download_model(self.config_name ) ) def __lowercase ( self : Tuple ,_a : List[Any] ,_a : Dict ,_a : Optional[Any] ,_a : List[str]=None ,_a : Tuple=False ): '''simple docstring''' if gpus is None: _a : str = 1 if torch.cuda.is_available() else 0 _a : Optional[Any] = {'src': sources, 'mt': predictions, 'ref': references} _a : Optional[Any] = [dict(zip(_a ,_a ) ) for t in zip(*data.values() )] _a, _a : Tuple = self.scorer.predict(_a ,gpus=_a ,progress_bar=_a ) return {"mean_score": mean_score, "scores": scores}
5
0
'''simple docstring''' def UpperCAmelCase_ (__a : int = 1_0_0 ): """simple docstring""" _a : Any = (n * (n + 1) // 2) ** 2 _a : Tuple = n * (n + 1) * (2 * n + 1) // 6 return sum_cubes - sum_squares if __name__ == "__main__": print(f'''{solution() = }''')
352
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __lowerCAmelCase = { """configuration_squeezebert""": [ """SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SqueezeBertConfig""", """SqueezeBertOnnxConfig""", ], """tokenization_squeezebert""": ["""SqueezeBertTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = ["""SqueezeBertTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ """SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """SqueezeBertForMaskedLM""", """SqueezeBertForMultipleChoice""", """SqueezeBertForQuestionAnswering""", """SqueezeBertForSequenceClassification""", """SqueezeBertForTokenClassification""", """SqueezeBertModel""", """SqueezeBertModule""", """SqueezeBertPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_squeezebert import ( SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, SqueezeBertConfig, SqueezeBertOnnxConfig, ) from .tokenization_squeezebert import SqueezeBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_squeezebert_fast import SqueezeBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_squeezebert import ( SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, SqueezeBertModel, SqueezeBertModule, SqueezeBertPreTrainedModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
5
0
'''simple docstring''' from __future__ import annotations import unittest from transformers import RoFormerConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForMultipleChoice, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerModel, ) from transformers.models.roformer.modeling_tf_roformer import ( TFRoFormerSelfAttention, TFRoFormerSinusoidalPositionalEmbedding, ) class UpperCAmelCase__ : """simple docstring""" def __init__( self : int ,_a : Optional[Any] ,_a : Optional[int]=13 ,_a : List[Any]=7 ,_a : Tuple=True ,_a : List[Any]=True ,_a : int=True ,_a : List[str]=True ,_a : List[str]=99 ,_a : Optional[int]=32 ,_a : Dict=2 ,_a : Optional[int]=4 ,_a : List[str]=37 ,_a : Any="gelu" ,_a : str=0.1 ,_a : List[Any]=0.1 ,_a : List[str]=512 ,_a : Union[str, Any]=16 ,_a : Tuple=2 ,_a : Any=0.02 ,_a : int=3 ,_a : Union[str, Any]=4 ,_a : List[Any]=None ,): '''simple docstring''' _a : Optional[Any] = parent _a : str = 13 _a : Optional[int] = 7 _a : Optional[int] = True _a : str = True _a : int = True _a : int = True _a : Optional[Any] = 99 _a : Dict = 32 _a : Any = 2 _a : int = 4 _a : Optional[int] = 37 _a : Any = 'gelu' _a : Optional[Any] = 0.1 _a : Optional[int] = 0.1 _a : Tuple = 512 _a : Tuple = 16 _a : Optional[Any] = 2 _a : Dict = 0.02 _a : Tuple = 3 _a : Optional[Any] = 4 _a : Any = None def __lowercase ( self : str ): '''simple docstring''' _a : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) _a : Union[str, Any] = None if self.use_input_mask: _a : int = random_attention_mask([self.batch_size, self.seq_length] ) _a : Any = None if self.use_token_type_ids: _a : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] ,self.type_vocab_size ) _a : Tuple = None _a : Tuple = None _a : Tuple = None if self.use_labels: _a : Tuple = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) _a : int = ids_tensor([self.batch_size, self.seq_length] ,self.num_labels ) _a : List[Any] = ids_tensor([self.batch_size] ,self.num_choices ) _a : Any = RoFormerConfig( vocab_size=self.vocab_size ,hidden_size=self.hidden_size ,num_hidden_layers=self.num_hidden_layers ,num_attention_heads=self.num_attention_heads ,intermediate_size=self.intermediate_size ,hidden_act=self.hidden_act ,hidden_dropout_prob=self.hidden_dropout_prob ,attention_probs_dropout_prob=self.attention_probs_dropout_prob ,max_position_embeddings=self.max_position_embeddings ,type_vocab_size=self.type_vocab_size ,initializer_range=self.initializer_range ,return_dict=_a ,) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def __lowercase ( self : Dict ,_a : int ,_a : Optional[int] ,_a : List[str] ,_a : Tuple ,_a : Union[str, Any] ,_a : Dict ,_a : Optional[int] ): '''simple docstring''' _a : Optional[int] = TFRoFormerModel(config=_a ) _a : List[str] = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} _a : Any = [input_ids, input_mask] _a : Optional[Any] = model(_a ) _a : str = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def __lowercase ( self : Tuple ,_a : int ,_a : Optional[int] ,_a : List[str] ,_a : List[Any] ,_a : Tuple ,_a : Dict ,_a : int ): '''simple docstring''' _a : str = True _a : List[Any] = TFRoFormerForCausalLM(config=_a ) _a : Dict = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } _a : Optional[Any] = model(_a )['logits'] self.parent.assertListEqual( list(prediction_scores.numpy().shape ) ,[self.batch_size, self.seq_length, self.vocab_size] ) def __lowercase ( self : Tuple ,_a : List[Any] ,_a : int ,_a : Any ,_a : List[str] ,_a : Dict ,_a : int ,_a : List[str] ): '''simple docstring''' _a : List[Any] = TFRoFormerForMaskedLM(config=_a ) _a : Dict = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } _a : Tuple = model(_a ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) def __lowercase ( self : Optional[int] ,_a : str ,_a : Dict ,_a : Dict ,_a : int ,_a : List[Any] ,_a : List[str] ,_a : Optional[int] ): '''simple docstring''' _a : Dict = self.num_labels _a : List[str] = TFRoFormerForSequenceClassification(config=_a ) _a : Union[str, Any] = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } _a : str = model(_a ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_labels) ) def __lowercase ( self : Optional[int] ,_a : Union[str, Any] ,_a : Tuple ,_a : Optional[Any] ,_a : Union[str, Any] ,_a : List[str] ,_a : int ,_a : Any ): '''simple docstring''' _a : Optional[int] = self.num_choices _a : Tuple = TFRoFormerForMultipleChoice(config=_a ) _a : Dict = tf.tile(tf.expand_dims(_a ,1 ) ,(1, self.num_choices, 1) ) _a : Optional[Any] = tf.tile(tf.expand_dims(_a ,1 ) ,(1, self.num_choices, 1) ) _a : int = tf.tile(tf.expand_dims(_a ,1 ) ,(1, self.num_choices, 1) ) _a : int = { 'input_ids': multiple_choice_inputs_ids, 'attention_mask': multiple_choice_input_mask, 'token_type_ids': multiple_choice_token_type_ids, } _a : Any = model(_a ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_choices) ) def __lowercase ( self : Dict ,_a : Optional[int] ,_a : List[str] ,_a : Optional[int] ,_a : Union[str, Any] ,_a : int ,_a : List[str] ,_a : List[str] ): '''simple docstring''' _a : Optional[int] = self.num_labels _a : Union[str, Any] = TFRoFormerForTokenClassification(config=_a ) _a : Any = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } _a : Any = model(_a ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.num_labels) ) def __lowercase ( self : int ,_a : Any ,_a : Any ,_a : Any ,_a : Optional[Any] ,_a : Any ,_a : Union[str, Any] ,_a : Any ): '''simple docstring''' _a : Dict = TFRoFormerForQuestionAnswering(config=_a ) _a : Optional[int] = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } _a : Tuple = model(_a ) self.parent.assertEqual(result.start_logits.shape ,(self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape ,(self.batch_size, self.seq_length) ) def __lowercase ( self : Any ): '''simple docstring''' _a : Optional[Any] = self.prepare_config_and_inputs() ( _a ) : Tuple = config_and_inputs _a : Tuple = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask} return config, inputs_dict @require_tf class UpperCAmelCase__ ( lowercase__ , lowercase__ , unittest.TestCase ): """simple docstring""" __UpperCAmelCase : Tuple = ( ( TFRoFormerModel, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerForMultipleChoice, ) if is_tf_available() else () ) __UpperCAmelCase : Union[str, Any] = ( { '''feature-extraction''': TFRoFormerModel, '''fill-mask''': TFRoFormerForMaskedLM, '''question-answering''': TFRoFormerForQuestionAnswering, '''text-classification''': TFRoFormerForSequenceClassification, '''text-generation''': TFRoFormerForCausalLM, '''token-classification''': TFRoFormerForTokenClassification, '''zero-shot''': TFRoFormerForSequenceClassification, } if is_tf_available() else {} ) __UpperCAmelCase : int = False __UpperCAmelCase : Optional[int] = False def __lowercase ( self : str ,_a : str ,_a : List[Any] ,_a : Any ,_a : str ,_a : Any ): '''simple docstring''' if pipeline_test_casse_name == "TextGenerationPipelineTests": return True return False def __lowercase ( self : Dict ): '''simple docstring''' _a : Optional[Any] = TFRoFormerModelTester(self ) _a : Dict = ConfigTester(self ,config_class=_a ,hidden_size=37 ) def __lowercase ( self : List[str] ): '''simple docstring''' self.config_tester.run_common_tests() def __lowercase ( self : Tuple ): '''simple docstring''' _a : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def __lowercase ( self : Optional[Any] ): '''simple docstring''' _a : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*_a ) def __lowercase ( self : str ): '''simple docstring''' _a : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head(*_a ) def __lowercase ( self : Any ): '''simple docstring''' _a : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*_a ) def __lowercase ( self : List[Any] ): '''simple docstring''' _a : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*_a ) def __lowercase ( self : List[str] ): '''simple docstring''' _a : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*_a ) def __lowercase ( self : str ): '''simple docstring''' _a : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*_a ) @slow def __lowercase ( self : Dict ): '''simple docstring''' _a : List[Any] = TFRoFormerModel.from_pretrained('junnyu/roformer_chinese_base' ) self.assertIsNotNone(_a ) @require_tf class UpperCAmelCase__ ( unittest.TestCase ): """simple docstring""" @slow def __lowercase ( self : List[str] ): '''simple docstring''' _a : Dict = TFRoFormerForMaskedLM.from_pretrained('junnyu/roformer_chinese_base' ) _a : List[Any] = tf.constant([[0, 1, 2, 3, 4, 5]] ) _a : str = model(_a )[0] # TODO Replace vocab size _a : List[str] = 5_0000 _a : int = [1, 6, vocab_size] self.assertEqual(output.shape ,_a ) print(output[:, :3, :3] ) # TODO Replace values below with what was printed above. _a : str = tf.constant( [ [ [-0.1205_3341, -1.026_4901, 0.2922_1946], [-1.513_3783, 0.19_7433, 0.1519_0607], [-5.013_5403, -3.90_0256, -0.8403_8764], ] ] ) tf.debugging.assert_near(output[:, :3, :3] ,_a ,atol=1E-4 ) @require_tf class UpperCAmelCase__ ( unittest.TestCase ): """simple docstring""" __UpperCAmelCase : Union[str, Any] = 1e-4 def __lowercase ( self : Union[str, Any] ): '''simple docstring''' _a : Union[str, Any] = tf.constant([[4, 10]] ) _a : Union[str, Any] = TFRoFormerSinusoidalPositionalEmbedding(num_positions=6 ,embedding_dim=6 ) _a : str = emba(input_ids.shape ) _a : str = tf.constant( [[0.0000, 0.0000, 0.0000, 1.0000, 1.0000, 1.0000], [0.8415, 0.0464, 0.0022, 0.5403, 0.9989, 1.0000]] ) tf.debugging.assert_near(_a ,_a ,atol=self.tolerance ) def __lowercase ( self : str ): '''simple docstring''' _a : Dict = tf.constant( [ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000], [0.8415, 0.8219, 0.8020, 0.7819, 0.7617], [0.9093, 0.9364, 0.9581, 0.9749, 0.9870], ] ) _a : Any = TFRoFormerSinusoidalPositionalEmbedding(num_positions=512 ,embedding_dim=512 ) emba([2, 16, 512] ) _a : Optional[Any] = emba.weight[:3, :5] tf.debugging.assert_near(_a ,_a ,atol=self.tolerance ) @require_tf class UpperCAmelCase__ ( unittest.TestCase ): """simple docstring""" __UpperCAmelCase : Union[str, Any] = 1e-4 def __lowercase ( self : Tuple ): '''simple docstring''' _a : List[Any] = tf.reshape(tf.range(2 * 12 * 16 * 64 ,dtype=tf.floataa ) ,shape=(2, 12, 16, 64) ) / 100 _a : List[str] = -tf.reshape(tf.range(2 * 12 * 16 * 64 ,dtype=tf.floataa ) ,shape=(2, 12, 16, 64) ) / 100 _a : Any = TFRoFormerSinusoidalPositionalEmbedding(num_positions=32 ,embedding_dim=64 ) _a : str = embed_positions([2, 16, 768] )[None, None, :, :] _a : Union[str, Any] = TFRoFormerSelfAttention.apply_rotary_position_embeddings( _a ,_a ,_a ) _a : Tuple = tf.constant( [ [0.0000, 0.0100, 0.0200, 0.0300, 0.0400, 0.0500, 0.0600, 0.0700], [-0.2012, 0.8897, 0.0263, 0.9401, 0.2074, 0.9463, 0.3481, 0.9343], [-1.7057, 0.6271, -1.2145, 1.3897, -0.6303, 1.7647, -0.1173, 1.8985], [-2.1731, -1.6397, -2.7358, 0.2854, -2.1840, 1.7183, -1.3018, 2.4871], [0.2717, -3.6173, -2.9206, -2.1988, -3.6638, 0.3858, -2.9155, 2.2980], [3.9859, -2.1580, -0.7984, -4.4904, -4.1181, -2.0252, -4.4782, 1.1253], ] ) _a : List[str] = tf.constant( [ [0.0000, -0.0100, -0.0200, -0.0300, -0.0400, -0.0500, -0.0600, -0.0700], [0.2012, -0.8897, -0.0263, -0.9401, -0.2074, -0.9463, -0.3481, -0.9343], [1.7057, -0.6271, 1.2145, -1.3897, 0.6303, -1.7647, 0.1173, -1.8985], [2.1731, 1.6397, 2.7358, -0.2854, 2.1840, -1.7183, 1.3018, -2.4871], [-0.2717, 3.6173, 2.9206, 2.1988, 3.6638, -0.3858, 2.9155, -2.2980], [-3.9859, 2.1580, 0.7984, 4.4904, 4.1181, 2.0252, 4.4782, -1.1253], ] ) tf.debugging.assert_near(query_layer[0, 0, :6, :8] ,_a ,atol=self.tolerance ) tf.debugging.assert_near(key_layer[0, 0, :6, :8] ,_a ,atol=self.tolerance )
353
'''simple docstring''' import sys def UpperCAmelCase_ (__a : List[str] ): """simple docstring""" _a : List[str] = len(__a ) _a : Dict = [[0 for x in range(__a )] for x in range(__a )] _a : Union[str, Any] = [[0 for x in range(__a )] for x in range(__a )] for chain_length in range(2 , __a ): for a in range(1 , n - chain_length + 1 ): _a : Tuple = a + chain_length - 1 _a : Any = sys.maxsize for c in range(__a , __a ): _a : Optional[Any] = ( matrix[a][c] + matrix[c + 1][b] + array[a - 1] * array[c] * array[b] ) if cost < matrix[a][b]: _a : Dict = cost _a : Any = c return matrix, sol def UpperCAmelCase_ (__a : Tuple , __a : List[str] , __a : Dict ): """simple docstring""" if i == j: print('A' + str(__a ) , end=' ' ) else: print('(' , end=' ' ) print_optiomal_solution(__a , __a , optimal_solution[i][j] ) print_optiomal_solution(__a , optimal_solution[i][j] + 1 , __a ) print(')' , end=' ' ) def UpperCAmelCase_ (): """simple docstring""" _a : Any = [3_0, 3_5, 1_5, 5, 1_0, 2_0, 2_5] _a : Any = len(__a ) # Size of matrix created from above array will be # 30*35 35*15 15*5 5*10 10*20 20*25 _a, _a : Union[str, Any] = matrix_chain_order(__a ) print('No. of Operation required: ' + str(matrix[1][n - 1] ) ) print_optiomal_solution(__a , 1 , n - 1 ) if __name__ == "__main__": main()
5
0
'''simple docstring''' import warnings from ...utils import logging from .image_processing_imagegpt import ImageGPTImageProcessor __lowerCAmelCase : List[Any] = logging.get_logger(__name__) class UpperCAmelCase__ ( lowercase__ ): """simple docstring""" def __init__( self : str ,*_a : str ,**_a : Optional[int] ): '''simple docstring''' warnings.warn( 'The class ImageGPTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use ImageGPTImageProcessor instead.' ,_a ,) super().__init__(*_a ,**_a )
354
'''simple docstring''' import os import time import pytest from datasets.utils.filelock import FileLock, Timeout def UpperCAmelCase_ (__a : Optional[Any] ): """simple docstring""" _a : int = FileLock(str(tmpdir / 'foo.lock' ) ) _a : List[Any] = FileLock(str(tmpdir / 'foo.lock' ) ) _a : Any = 0.01 with locka.acquire(): with pytest.raises(__a ): _a : int = time.time() locka.acquire(__a ) assert time.time() - _start > timeout def UpperCAmelCase_ (__a : str ): """simple docstring""" _a : Dict = 'a' * 1_0_0_0 + '.lock' _a : int = FileLock(str(tmpdir / filename ) ) assert locka._lock_file.endswith('.lock' ) assert not locka._lock_file.endswith(__a ) assert len(os.path.basename(locka._lock_file ) ) <= 2_5_5 _a : Dict = FileLock(tmpdir / filename ) with locka.acquire(): with pytest.raises(__a ): locka.acquire(0 )
5
0
'''simple docstring''' import requests from bsa import BeautifulSoup def UpperCAmelCase_ (__a : str = "https://www.worldometers.info/coronavirus" ): """simple docstring""" _a : List[str] = BeautifulSoup(requests.get(__a ).text , 'html.parser' ) _a : Dict = soup.findAll('h1' ) _a : Union[str, Any] = soup.findAll('div' , {'class': 'maincounter-number'} ) keys += soup.findAll('span' , {'class': 'panel-title'} ) values += soup.findAll('div' , {'class': 'number-table-main'} ) return {key.text.strip(): value.text.strip() for key, value in zip(__a , __a )} if __name__ == "__main__": print("""\033[1m""" + """COVID-19 Status of the World""" + """\033[0m\n""") for key, value in world_covidaa_stats().items(): print(f'''{key}\n{value}\n''')
355
'''simple docstring''' def UpperCAmelCase_ (__a : int = 1_0**1_2 ): """simple docstring""" _a : List[str] = 1 _a : Optional[int] = 0 _a : Any = 1 _a : List[str] = 1 while numerator <= 2 * min_total - 1: prev_numerator += 2 * numerator numerator += 2 * prev_numerator prev_denominator += 2 * denominator denominator += 2 * prev_denominator return (denominator + 1) // 2 if __name__ == "__main__": print(f'''{solution() = }''')
5
0
'''simple docstring''' import math def UpperCAmelCase_ (__a : int ): """simple docstring""" return math.sqrt(__a ) * math.sqrt(__a ) == num def UpperCAmelCase_ (__a : int ): """simple docstring""" _a : Dict = 0 _a : Tuple = n while left <= right: _a : List[Any] = (left + right) // 2 if mid**2 == n: return True elif mid**2 > n: _a : str = mid - 1 else: _a : str = mid + 1 return False if __name__ == "__main__": import doctest doctest.testmod()
356
'''simple docstring''' import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_mobilebert import MobileBertTokenizer __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""} __lowerCAmelCase = { """vocab_file""": {"""mobilebert-uncased""": """https://huggingface.co/google/mobilebert-uncased/resolve/main/vocab.txt"""}, """tokenizer_file""": { """mobilebert-uncased""": """https://huggingface.co/google/mobilebert-uncased/resolve/main/tokenizer.json""" }, } __lowerCAmelCase = {"""mobilebert-uncased""": 5_1_2} __lowerCAmelCase = {} class UpperCAmelCase__ ( lowercase__ ): """simple docstring""" __UpperCAmelCase : Any = VOCAB_FILES_NAMES __UpperCAmelCase : int = PRETRAINED_VOCAB_FILES_MAP __UpperCAmelCase : Tuple = PRETRAINED_INIT_CONFIGURATION __UpperCAmelCase : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCAmelCase : Optional[Any] = MobileBertTokenizer def __init__( self : Dict ,_a : List[Any]=None ,_a : Optional[Any]=None ,_a : Union[str, Any]=True ,_a : Dict="[UNK]" ,_a : Union[str, Any]="[SEP]" ,_a : Any="[PAD]" ,_a : Optional[int]="[CLS]" ,_a : Optional[Any]="[MASK]" ,_a : Dict=True ,_a : Any=None ,**_a : Optional[Any] ,): '''simple docstring''' super().__init__( _a ,tokenizer_file=_a ,do_lower_case=_a ,unk_token=_a ,sep_token=_a ,pad_token=_a ,cls_token=_a ,mask_token=_a ,tokenize_chinese_chars=_a ,strip_accents=_a ,**_a ,) _a : int = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('lowercase' ,_a ) != do_lower_case or normalizer_state.get('strip_accents' ,_a ) != strip_accents or normalizer_state.get('handle_chinese_chars' ,_a ) != tokenize_chinese_chars ): _a : Optional[Any] = getattr(_a ,normalizer_state.pop('type' ) ) _a : Dict = do_lower_case _a : str = strip_accents _a : Tuple = tokenize_chinese_chars _a : Optional[Any] = normalizer_class(**_a ) _a : str = do_lower_case def __lowercase ( self : Tuple ,_a : Union[str, Any] ,_a : List[str]=None ): '''simple docstring''' _a : Tuple = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def __lowercase ( self : List[str] ,_a : List[int] ,_a : Optional[List[int]] = None ): '''simple docstring''' _a : List[str] = [self.sep_token_id] _a : str = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __lowercase ( self : Union[str, Any] ,_a : str ,_a : Optional[str] = None ): '''simple docstring''' _a : int = self._tokenizer.model.save(_a ,name=_a ) return tuple(_a )
5
0
'''simple docstring''' def UpperCAmelCase_ (__a : str , __a : str ): """simple docstring""" _a : int = len(__a ) + 1 _a : List[str] = len(__a ) + 1 # dp is a 2d matrix where dp[i][j] denotes whether prefix string of # length i of input_string matches with prefix string of length j of # given pattern. # "dp" stands for dynamic programming. _a : Optional[int] = [[0 for i in range(__a )] for j in range(__a )] # since string of zero length match pattern of zero length _a : str = 1 # since pattern of zero length will never match with string of non-zero length for i in range(1 , __a ): _a : Optional[Any] = 0 # since string of zero length will match with pattern where there # is at least one * alternatively for j in range(1 , __a ): _a : Dict = dp[0][j - 2] if pattern[j - 1] == '*' else 0 # now using bottom-up approach to find for all remaining lengths for i in range(1 , __a ): for j in range(1 , __a ): if input_string[i - 1] == pattern[j - 1] or pattern[j - 1] == ".": _a : Tuple = dp[i - 1][j - 1] elif pattern[j - 1] == "*": if dp[i][j - 2] == 1: _a : List[str] = 1 elif pattern[j - 2] in (input_string[i - 1], "."): _a : int = dp[i - 1][j] else: _a : Any = 0 else: _a : Optional[Any] = 0 return bool(dp[-1][-1] ) if __name__ == "__main__": import doctest doctest.testmod() # inputing the strings # input_string = input("input a string :") # pattern = input("input a pattern :") __lowerCAmelCase = """aab""" __lowerCAmelCase = """c*a*b""" # using function to check whether given string matches the given pattern if match_pattern(input_string, pattern): print(f'''{input_string} matches the given pattern {pattern}''') else: print(f'''{input_string} does not match with the given pattern {pattern}''')
357
'''simple docstring''' def UpperCAmelCase_ (__a : str ): """simple docstring""" _a : List[Any] = 0 # if input_string is "aba" than new_input_string become "a|b|a" _a : Optional[int] = '' _a : List[str] = '' # append each character + "|" in new_string for range(0, length-1) for i in input_string[: len(__a ) - 1]: new_input_string += i + "|" # append last character new_input_string += input_string[-1] # we will store the starting and ending of previous furthest ending palindromic # substring _a, _a : Optional[int] = 0, 0 # length[i] shows the length of palindromic substring with center i _a : Optional[Any] = [1 for i in range(len(__a ) )] # for each character in new_string find corresponding palindromic string _a : Dict = 0 for j in range(len(__a ) ): _a : Dict = 1 if j > r else min(length[l + r - j] // 2 , r - j + 1 ) while ( j - k >= 0 and j + k < len(__a ) and new_input_string[k + j] == new_input_string[j - k] ): k += 1 _a : Optional[int] = 2 * k - 1 # does this string is ending after the previously explored end (that is r) ? # if yes the update the new r to the last index of this if j + k - 1 > r: _a : str = j - k + 1 # noqa: E741 _a : Any = j + k - 1 # update max_length and start position if max_length < length[j]: _a : Union[str, Any] = length[j] _a : List[str] = j # create that string _a : Tuple = new_input_string[start - max_length // 2 : start + max_length // 2 + 1] for i in s: if i != "|": output_string += i return output_string if __name__ == "__main__": import doctest doctest.testmod()
5
0
'''simple docstring''' import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, BatchEncoding, MBartaaTokenizer, MBartaaTokenizerFast, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, slow, ) from ...test_tokenization_common import TokenizerTesterMixin __lowerCAmelCase = get_tests_dir("""fixtures/test_sentencepiece.model""") if is_torch_available(): from transformers.models.mbart.modeling_mbart import shift_tokens_right __lowerCAmelCase = 2_5_0_0_0_4 __lowerCAmelCase = 2_5_0_0_2_0 @require_sentencepiece @require_tokenizers class UpperCAmelCase__ ( lowercase__ , unittest.TestCase ): """simple docstring""" __UpperCAmelCase : List[str] = MBartaaTokenizer __UpperCAmelCase : str = MBartaaTokenizerFast __UpperCAmelCase : Tuple = True __UpperCAmelCase : List[Any] = True def __lowercase ( self : Tuple ): '''simple docstring''' super().setUp() # We have a SentencePiece fixture for testing _a : int = MBartaaTokenizer(_a ,src_lang='en_XX' ,tgt_lang='ro_RO' ,keep_accents=_a ) tokenizer.save_pretrained(self.tmpdirname ) def __lowercase ( self : str ): '''simple docstring''' _a : Union[str, Any] = '<s>' _a : int = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_a ) ,_a ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_a ) ,_a ) def __lowercase ( self : Optional[int] ): '''simple docstring''' _a : Union[str, Any] = 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(_a ) ,1054 ) def __lowercase ( self : Any ): '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size ,1054 ) def __lowercase ( self : Optional[int] ): '''simple docstring''' _a : Tuple = MBartaaTokenizer(_a ,src_lang='en_XX' ,tgt_lang='ro_RO' ,keep_accents=_a ) _a : Union[str, Any] = tokenizer.tokenize('This is a test' ) self.assertListEqual(_a ,['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_a ) ,[value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] ,) _a : Tuple = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( _a ,[SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '9', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', 'é', '.'] ,) _a : int = tokenizer.convert_tokens_to_ids(_a ) self.assertListEqual( _a ,[ 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] ] ,) _a : int = tokenizer.convert_ids_to_tokens(_a ) self.assertListEqual( _a ,[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 __lowercase ( self : List[str] ): '''simple docstring''' _a : Any = {'input_ids': [[25_0004, 1_1062, 8_2772, 7, 15, 8_2772, 538, 5_1529, 237, 1_7198, 1290, 206, 9, 21_5175, 1314, 136, 1_7198, 1290, 206, 9, 5_6359, 42, 12_2009, 9, 1_6466, 16, 8_7344, 4537, 9, 4717, 7_8381, 6, 15_9958, 7, 15, 2_4480, 618, 4, 527, 2_2693, 5428, 4, 2777, 2_4480, 9874, 4, 4_3523, 594, 4, 803, 1_8392, 3_3189, 18, 4, 4_3523, 2_4447, 1_2399, 100, 2_4955, 8_3658, 9626, 14_4057, 15, 839, 2_2335, 16, 136, 2_4955, 8_3658, 8_3479, 15, 3_9102, 724, 16, 678, 645, 2789, 1328, 4589, 42, 12_2009, 11_5774, 23, 805, 1328, 4_6876, 7, 136, 5_3894, 1940, 4_2227, 4_1159, 1_7721, 823, 425, 4, 2_7512, 9_8722, 206, 136, 5531, 4970, 919, 1_7336, 5, 2], [25_0004, 2_0080, 618, 83, 8_2775, 47, 479, 9, 1517, 73, 5_3894, 333, 8_0581, 11_0117, 1_8811, 5256, 1295, 51, 15_2526, 297, 7986, 390, 12_4416, 538, 3_5431, 214, 98, 1_5044, 2_5737, 136, 7108, 4_3701, 23, 756, 13_5355, 7, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [25_0004, 581, 6_3773, 11_9455, 6, 14_7797, 8_8203, 7, 645, 70, 21, 3285, 1_0269, 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=_a ,model_name='facebook/mbart-large-50' ,revision='d3913889c59cd5c9e456b269c376325eabad57e2' ,) def __lowercase ( self : Union[str, Any] ): '''simple docstring''' if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return _a : Optional[int] = (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})""" ): _a : Optional[Any] = self.rust_tokenizer_class.from_pretrained(_a ,**_a ) _a : Tuple = self.tokenizer_class.from_pretrained(_a ,**_a ) _a : Optional[int] = tempfile.mkdtemp() _a : Tuple = tokenizer_r.save_pretrained(_a ) _a : Optional[int] = tokenizer_p.save_pretrained(_a ) # 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 ) ) _a : Any = tuple(f for f in tokenizer_r_files if 'tokenizer.json' not in f ) self.assertSequenceEqual(_a ,_a ) # Checks everything loads correctly in the same way _a : Optional[int] = tokenizer_r.from_pretrained(_a ) _a : str = tokenizer_p.from_pretrained(_a ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(_a ,_a ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(_a ) # Save tokenizer rust, legacy_format=True _a : Union[str, Any] = tempfile.mkdtemp() _a : Optional[Any] = tokenizer_r.save_pretrained(_a ,legacy_format=_a ) _a : List[str] = tokenizer_p.save_pretrained(_a ) # Checks it save with the same files self.assertSequenceEqual(_a ,_a ) # Checks everything loads correctly in the same way _a : Optional[Any] = tokenizer_r.from_pretrained(_a ) _a : Optional[int] = tokenizer_p.from_pretrained(_a ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(_a ,_a ) ) shutil.rmtree(_a ) # Save tokenizer rust, legacy_format=False _a : int = tempfile.mkdtemp() _a : str = tokenizer_r.save_pretrained(_a ,legacy_format=_a ) _a : Optional[int] = tokenizer_p.save_pretrained(_a ) # 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 _a : int = tokenizer_r.from_pretrained(_a ) _a : Tuple = tokenizer_p.from_pretrained(_a ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(_a ,_a ) ) shutil.rmtree(_a ) @require_torch @require_sentencepiece @require_tokenizers class UpperCAmelCase__ ( unittest.TestCase ): """simple docstring""" __UpperCAmelCase : Any = '''facebook/mbart-large-50-one-to-many-mmt''' __UpperCAmelCase : Dict = [ ''' UN Chief Says There Is No Military Solution in Syria''', ''' Secretary-General Ban Ki-moon says his response to Russia\'s stepped up military support for Syria is that "there is no military solution" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.''', ] __UpperCAmelCase : Union[str, Any] = [ '''Şeful ONU declară că nu există o soluţie militară în Siria''', '''Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei''' ''' pentru Siria este că "nu există o soluţie militară" la conflictul de aproape cinci ani şi că noi arme nu vor''' ''' face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.''', ] __UpperCAmelCase : Any = [EN_CODE, 8274, 12_7873, 2_5916, 7, 8622, 2071, 438, 6_7485, 53, 18_7895, 23, 5_1712, 2] @classmethod def __lowercase ( cls : Optional[int] ): '''simple docstring''' _a : MBartaaTokenizer = MBartaaTokenizer.from_pretrained( cls.checkpoint_name ,src_lang='en_XX' ,tgt_lang='ro_RO' ) _a : Union[str, Any] = 1 return cls def __lowercase ( self : Optional[Any] ): '''simple docstring''' self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['ar_AR'] ,25_0001 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['en_EN'] ,25_0004 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['ro_RO'] ,25_0020 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['mr_IN'] ,25_0038 ) def __lowercase ( self : int ): '''simple docstring''' _a : int = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens ,_a ) def __lowercase ( self : str ): '''simple docstring''' self.assertIn(_a ,self.tokenizer.all_special_ids ) _a : Optional[int] = [RO_CODE, 884, 9019, 96, 9, 916, 8_6792, 36, 1_8743, 1_5596, 5, 2] _a : Dict = self.tokenizer.decode(_a ,skip_special_tokens=_a ) _a : Union[str, Any] = self.tokenizer.decode(generated_ids[1:] ,skip_special_tokens=_a ) self.assertEqual(_a ,_a ) self.assertNotIn(self.tokenizer.eos_token ,_a ) def __lowercase ( self : Any ): '''simple docstring''' _a : Optional[Any] = ['this is gunna be a long sentence ' * 20] assert isinstance(src_text[0] ,_a ) _a : str = 10 _a : Tuple = self.tokenizer(_a ,max_length=_a ,truncation=_a ).input_ids[0] self.assertEqual(ids[0] ,_a ) self.assertEqual(ids[-1] ,2 ) self.assertEqual(len(_a ) ,_a ) def __lowercase ( self : List[str] ): '''simple docstring''' self.assertListEqual(self.tokenizer.convert_tokens_to_ids(['<mask>', 'ar_AR'] ) ,[25_0053, 25_0001] ) def __lowercase ( self : int ): '''simple docstring''' _a : Dict = tempfile.mkdtemp() _a : Tuple = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(_a ) _a : Optional[int] = MBartaaTokenizer.from_pretrained(_a ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids ,_a ) @require_torch def __lowercase ( self : int ): '''simple docstring''' _a : Any = self.tokenizer(self.src_text ,text_target=self.tgt_text ,padding=_a ,return_tensors='pt' ) _a : List[str] = 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 __lowercase ( self : Any ): '''simple docstring''' _a : Tuple = self.tokenizer( self.src_text ,text_target=self.tgt_text ,padding=_a ,truncation=_a ,max_length=len(self.expected_src_tokens ) ,return_tensors='pt' ,) _a : Optional[int] = shift_tokens_right(batch['labels'] ,self.tokenizer.pad_token_id ) self.assertIsInstance(_a ,_a ) self.assertEqual((2, 14) ,batch.input_ids.shape ) self.assertEqual((2, 14) ,batch.attention_mask.shape ) _a : str = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens ,_a ) 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 __lowercase ( self : List[Any] ): '''simple docstring''' _a : Optional[int] = self.tokenizer(self.src_text ,padding=_a ,truncation=_a ,max_length=3 ,return_tensors='pt' ) _a : Union[str, Any] = self.tokenizer( text_target=self.tgt_text ,padding=_a ,truncation=_a ,max_length=10 ,return_tensors='pt' ) _a : Optional[int] = targets['input_ids'] _a : Any = shift_tokens_right(_a ,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 __lowercase ( self : int ): '''simple docstring''' _a : Any = self.tokenizer._build_translation_inputs( 'A test' ,return_tensors='pt' ,src_lang='en_XX' ,tgt_lang='ar_AR' ) self.assertEqual( nested_simplify(_a ) ,{ # en_XX, A, test, EOS 'input_ids': [[25_0004, 62, 3034, 2]], 'attention_mask': [[1, 1, 1, 1]], # ar_AR 'forced_bos_token_id': 25_0001, } ,)
358
'''simple docstring''' from functools import lru_cache @lru_cache def UpperCAmelCase_ (__a : int ): """simple docstring""" if num < 0: raise ValueError('Number should not be negative.' ) return 1 if num in (0, 1) else num * factorial(num - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
5
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import _LazyModule __lowerCAmelCase = {"""tokenization_byt5""": ["""ByT5Tokenizer"""]} if TYPE_CHECKING: from .tokenization_byta import ByTaTokenizer else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
359
'''simple docstring''' import functools import logging import os import sys import threading from logging import ( CRITICAL, # NOQA DEBUG, # NOQA ERROR, # NOQA FATAL, # NOQA INFO, # NOQA NOTSET, # NOQA WARN, # NOQA WARNING, # NOQA ) from typing import Optional import huggingface_hub.utils as hf_hub_utils from tqdm import auto as tqdm_lib __lowerCAmelCase = threading.Lock() __lowerCAmelCase = None __lowerCAmelCase = { """debug""": logging.DEBUG, """info""": logging.INFO, """warning""": logging.WARNING, """error""": logging.ERROR, """critical""": logging.CRITICAL, } __lowerCAmelCase = logging.WARNING __lowerCAmelCase = True def UpperCAmelCase_ (): """simple docstring""" _a : Dict = os.getenv('TRANSFORMERS_VERBOSITY' , __a ) if env_level_str: if env_level_str in log_levels: return log_levels[env_level_str] else: logging.getLogger().warning( f"""Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, """ f"""has to be one of: { ', '.join(log_levels.keys() ) }""" ) return _default_log_level def UpperCAmelCase_ (): """simple docstring""" return __name__.split('.' )[0] def UpperCAmelCase_ (): """simple docstring""" return logging.getLogger(_get_library_name() ) def UpperCAmelCase_ (): """simple docstring""" global _default_handler with _lock: if _default_handler: # This library has already configured the library root logger. return _a : str = logging.StreamHandler() # Set sys.stderr as stream. _a : Optional[Any] = sys.stderr.flush # Apply our default configuration to the library root logger. _a : List[Any] = _get_library_root_logger() library_root_logger.addHandler(_default_handler ) library_root_logger.setLevel(_get_default_logging_level() ) _a : List[str] = False def UpperCAmelCase_ (): """simple docstring""" global _default_handler with _lock: if not _default_handler: return _a : int = _get_library_root_logger() library_root_logger.removeHandler(_default_handler ) library_root_logger.setLevel(logging.NOTSET ) _a : str = None def UpperCAmelCase_ (): """simple docstring""" return log_levels def UpperCAmelCase_ (__a : Optional[str] = None ): """simple docstring""" if name is None: _a : List[Any] = _get_library_name() _configure_library_root_logger() return logging.getLogger(__a ) def UpperCAmelCase_ (): """simple docstring""" _configure_library_root_logger() return _get_library_root_logger().getEffectiveLevel() def UpperCAmelCase_ (__a : int ): """simple docstring""" _configure_library_root_logger() _get_library_root_logger().setLevel(__a ) def UpperCAmelCase_ (): """simple docstring""" return set_verbosity(__a ) def UpperCAmelCase_ (): """simple docstring""" return set_verbosity(__a ) def UpperCAmelCase_ (): """simple docstring""" return set_verbosity(__a ) def UpperCAmelCase_ (): """simple docstring""" return set_verbosity(__a ) def UpperCAmelCase_ (): """simple docstring""" _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().removeHandler(_default_handler ) def UpperCAmelCase_ (): """simple docstring""" _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().addHandler(_default_handler ) def UpperCAmelCase_ (__a : logging.Handler ): """simple docstring""" _configure_library_root_logger() assert handler is not None _get_library_root_logger().addHandler(__a ) def UpperCAmelCase_ (__a : logging.Handler ): """simple docstring""" _configure_library_root_logger() assert handler is not None and handler not in _get_library_root_logger().handlers _get_library_root_logger().removeHandler(__a ) def UpperCAmelCase_ (): """simple docstring""" _configure_library_root_logger() _a : Union[str, Any] = False def UpperCAmelCase_ (): """simple docstring""" _configure_library_root_logger() _a : Dict = True def UpperCAmelCase_ (): """simple docstring""" _a : Any = _get_library_root_logger().handlers for handler in handlers: _a : Union[str, Any] = logging.Formatter('[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s' ) handler.setFormatter(__a ) def UpperCAmelCase_ (): """simple docstring""" _a : Union[str, Any] = _get_library_root_logger().handlers for handler in handlers: handler.setFormatter(__a ) def UpperCAmelCase_ (self : Union[str, Any] , *__a : Union[str, Any] , **__a : Union[str, Any] ): """simple docstring""" _a : Union[str, Any] = os.getenv('TRANSFORMERS_NO_ADVISORY_WARNINGS' , __a ) if no_advisory_warnings: return self.warning(*__a , **__a ) __lowerCAmelCase = warning_advice @functools.lru_cache(__a ) def UpperCAmelCase_ (self : int , *__a : Optional[Any] , **__a : Any ): """simple docstring""" self.warning(*__a , **__a ) __lowerCAmelCase = warning_once class UpperCAmelCase__ : """simple docstring""" def __init__( self : Any ,*_a : Tuple ,**_a : int ): # pylint: disable=unused-argument '''simple docstring''' _a : int = args[0] if args else None def __iter__( self : str ): '''simple docstring''' return iter(self._iterator ) def __getattr__( self : List[Any] ,_a : int ): '''simple docstring''' def empty_fn(*_a : Optional[Any] ,**_a : Any ): # pylint: disable=unused-argument return return empty_fn def __enter__( self : List[str] ): '''simple docstring''' return self def __exit__( self : List[str] ,_a : str ,_a : List[Any] ,_a : str ): '''simple docstring''' return class UpperCAmelCase__ : """simple docstring""" def __call__( self : Union[str, Any] ,*_a : Tuple ,**_a : Tuple ): '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm(*_a ,**_a ) else: return EmptyTqdm(*_a ,**_a ) def __lowercase ( self : str ,*_a : List[Any] ,**_a : Any ): '''simple docstring''' _a : Any = None if _tqdm_active: return tqdm_lib.tqdm.set_lock(*_a ,**_a ) def __lowercase ( self : List[str] ): '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm.get_lock() __lowerCAmelCase = _tqdm_cls() def UpperCAmelCase_ (): """simple docstring""" global _tqdm_active return bool(_tqdm_active ) def UpperCAmelCase_ (): """simple docstring""" global _tqdm_active _a : str = True hf_hub_utils.enable_progress_bars() def UpperCAmelCase_ (): """simple docstring""" global _tqdm_active _a : Dict = False hf_hub_utils.disable_progress_bars()
5
0
'''simple docstring''' import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( UniSpeechConfig, UniSpeechForCTC, UniSpeechForPreTraining, WavaVecaFeatureExtractor, WavaVecaPhonemeCTCTokenizer, WavaVecaProcessor, logging, ) logging.set_verbosity_info() __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = { """post_extract_proj""": """feature_projection.projection""", """encoder.pos_conv.0""": """encoder.pos_conv_embed.conv""", """self_attn.k_proj""": """encoder.layers.*.attention.k_proj""", """self_attn.v_proj""": """encoder.layers.*.attention.v_proj""", """self_attn.q_proj""": """encoder.layers.*.attention.q_proj""", """self_attn.out_proj""": """encoder.layers.*.attention.out_proj""", """self_attn_layer_norm""": """encoder.layers.*.layer_norm""", """fc1""": """encoder.layers.*.feed_forward.intermediate_dense""", """fc2""": """encoder.layers.*.feed_forward.output_dense""", """final_layer_norm""": """encoder.layers.*.final_layer_norm""", """encoder.layer_norm""": """encoder.layer_norm""", """w2v_model.layer_norm""": """feature_projection.layer_norm""", """quantizer.weight_proj""": """quantizer.weight_proj""", """quantizer.vars""": """quantizer.codevectors""", """project_q""": """project_q""", """final_proj""": """project_hid""", """w2v_encoder.proj""": """ctc_proj""", """mask_emb""": """masked_spec_embed""", } __lowerCAmelCase = [ """ctc_proj""", """quantizer.weight_proj""", """quantizer.codevectors""", """project_q""", """project_hid""", ] def UpperCAmelCase_ (__a : Optional[Any] , __a : Any , __a : Optional[Any] , __a : Tuple , __a : Dict , __a : List[Any] ): """simple docstring""" for attribute in key.split('.' ): if is_finetuned: if attribute in ["quantizer", "project_q", "project_hid"]: # those layers are only relevant for pretraining and should be dropped return if attribute == "ctc_proj": # we should rename `ctc_proj` to `lm_head` for fine-tuned phoneme models _a : Optional[int] = 'lm_head' _a : Any = getattr(__a , __a ) if weight_type is not None: _a : List[str] = getattr(__a , __a ).shape else: _a : Union[str, Any] = hf_pointer.shape assert hf_shape == value.shape, ( f"""Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be""" f""" {value.shape} for {full_name}""" ) if weight_type == "weight": _a : Optional[int] = value elif weight_type == "weight_g": _a : str = value elif weight_type == "weight_v": _a : int = value elif weight_type == "bias": _a : Optional[Any] = value else: _a : Any = value logger.info(f"""{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}.""" ) def UpperCAmelCase_ (__a : str , __a : List[Any] , __a : Any ): """simple docstring""" _a : str = [] _a : int = fairseq_model.state_dict() _a : str = hf_model.unispeech.feature_extractor for name, value in fairseq_dict.items(): _a : Union[str, Any] = False if "conv_layers" in name: load_conv_layer( __a , __a , __a , __a , hf_model.config.feat_extract_norm == 'group' , ) _a : List[str] = True else: for key, mapped_key in MAPPING.items(): _a : List[str] = 'unispeech.' + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if key in name or key.split('w2v_model.' )[-1] == name.split('.' )[0]: _a : Any = True if "*" in mapped_key: _a : Optional[int] = name.split(__a )[0].split('.' )[-2] _a : int = mapped_key.replace('*' , __a ) if "weight_g" in name: _a : Any = 'weight_g' elif "weight_v" in name: _a : Optional[Any] = 'weight_v' elif "bias" in name: _a : Any = 'bias' elif "weight" in name: # TODO: don't match quantizer.weight_proj _a : Any = 'weight' else: _a : List[str] = None set_recursively(__a , __a , __a , __a , __a , __a ) continue if not is_used: unused_weights.append(__a ) logger.warning(f"""Unused weights: {unused_weights}""" ) def UpperCAmelCase_ (__a : List[Any] , __a : Optional[int] , __a : int , __a : Optional[Any] , __a : Optional[int] ): """simple docstring""" _a : Optional[Any] = full_name.split('conv_layers.' )[-1] _a : str = name.split('.' ) _a : Tuple = int(items[0] ) _a : List[Any] = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( f"""{full_name} has size {value.shape}, but""" f""" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.""" ) _a : Tuple = value logger.info(f"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( f"""{full_name} has size {value.shape}, but""" f""" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.""" ) _a : Tuple = value logger.info(f"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( f"""{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was""" " found." ) _a : Tuple = value logger.info(f"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( f"""{full_name} has size {value.shape}, but""" f""" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.""" ) _a : int = value logger.info(f"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" ) else: unused_weights.append(__a ) @torch.no_grad() def UpperCAmelCase_ (__a : List[Any] , __a : int , __a : Dict=None , __a : Tuple=None , __a : List[Any]=True ): """simple docstring""" if config_path is not None: _a : Any = UniSpeechConfig.from_pretrained(__a ) else: _a : Optional[int] = UniSpeechConfig() if is_finetuned: if dict_path: _a : str = Dictionary.load_from_json(__a ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq _a : int = target_dict.pad_index _a : Tuple = target_dict.bos_index _a : List[str] = target_dict.eos_index _a : Union[str, Any] = len(target_dict.symbols ) _a : Tuple = os.path.join(__a , 'vocab.json' ) if not os.path.isdir(__a ): logger.error('--pytorch_dump_folder_path ({}) should be a directory'.format(__a ) ) return os.makedirs(__a , exist_ok=__a ) _a : Dict = target_dict.indices # fairseq has the <pad> and <s> switched _a : List[Any] = 4_2 _a : Union[str, Any] = 4_3 with open(__a , 'w' , encoding='utf-8' ) as vocab_handle: json.dump(__a , __a ) _a : List[str] = WavaVecaPhonemeCTCTokenizer( __a , unk_token=target_dict.unk_word , pad_token=target_dict.pad_word , bos_token=target_dict.bos_word , eos_token=target_dict.eos_word , word_delimiter_token='|' , do_lower_case=__a , ) _a : str = True if config.feat_extract_norm == 'layer' else False _a : Optional[Any] = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_6_0_0_0 , padding_value=0 , do_normalize=__a , return_attention_mask=__a , ) _a : Tuple = WavaVecaProcessor(feature_extractor=__a , tokenizer=__a ) processor.save_pretrained(__a ) _a : List[str] = UniSpeechForCTC(__a ) else: _a : Any = UniSpeechForPreTraining(__a ) if is_finetuned: _a : Optional[int] = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={'data': '/'.join(dict_path.split('/' )[:-1] ), 'w2v_path': checkpoint_path} ) else: _a : Any = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] ) _a : Union[str, Any] = model[0].eval() recursively_load_weights(__a , __a , __a ) hf_unispeech.save_pretrained(__a ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument("""--checkpoint_path""", default=None, type=str, help="""Path to fairseq checkpoint""") parser.add_argument("""--dict_path""", default=None, type=str, help="""Path to dict of fine-tuned model""") parser.add_argument("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""") parser.add_argument( """--not_finetuned""", action="""store_true""", help="""Whether the model to convert is a fine-tuned model or not""" ) __lowerCAmelCase = parser.parse_args() convert_unispeech_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned )
360
'''simple docstring''' def UpperCAmelCase_ (__a : list[int] , __a : list[int] ): """simple docstring""" if not len(__a ) == len(__a ) == 3: raise ValueError('Please enter a valid equation.' ) if equationa[0] == equationa[1] == equationa[0] == equationa[1] == 0: raise ValueError('Both a & b of two equations can\'t be zero.' ) # Extract the coefficients _a, _a, _a : Tuple = equationa _a, _a, _a : str = equationa # Calculate the determinants of the matrices _a : Union[str, Any] = aa * ba - aa * ba _a : List[Any] = ca * ba - ca * ba _a : List[Any] = aa * ca - aa * ca # Check if the system of linear equations has a solution (using Cramer's rule) if determinant == 0: if determinant_x == determinant_y == 0: raise ValueError('Infinite solutions. (Consistent system)' ) else: raise ValueError('No solution. (Inconsistent system)' ) else: if determinant_x == determinant_y == 0: # Trivial solution (Inconsistent system) return (0.0, 0.0) else: _a : int = determinant_x / determinant _a : List[str] = determinant_y / determinant # Non-Trivial Solution (Consistent system) return (x, y)
5
0
'''simple docstring''' import itertools import os import re __lowerCAmelCase = re.compile(r"""([A-Z]+)([A-Z][a-z])""") __lowerCAmelCase = re.compile(r"""([a-z\d])([A-Z])""") __lowerCAmelCase = re.compile(r"""(?<!_)_(?!_)""") __lowerCAmelCase = re.compile(r"""(_{2,})""") __lowerCAmelCase = r"""^\w+(\.\w+)*$""" __lowerCAmelCase = r"""<>:/\|?*""" def UpperCAmelCase_ (__a : Union[str, Any] ): """simple docstring""" _a : Optional[int] = _uppercase_uppercase_re.sub(R'\1_\2' , __a ) _a : Dict = _lowercase_uppercase_re.sub(R'\1_\2' , __a ) return name.lower() def UpperCAmelCase_ (__a : Optional[int] ): """simple docstring""" _a : str = _single_underscore_re.split(__a ) _a : int = [_multiple_underscores_re.split(__a ) for n in name] return "".join(n.capitalize() for n in itertools.chain.from_iterable(__a ) if n != '' ) def UpperCAmelCase_ (__a : List[str] ): """simple docstring""" if os.path.basename(__a ) != name: raise ValueError(f"""Should be a dataset name, not a path: {name}""" ) return camelcase_to_snakecase(__a ) def UpperCAmelCase_ (__a : Union[str, Any] , __a : List[Any] ): """simple docstring""" if os.path.basename(__a ) != name: raise ValueError(f"""Should be a dataset name, not a path: {name}""" ) if not re.match(_split_re , __a ): raise ValueError(f"""Split name should match '{_split_re}'' but got '{split}'.""" ) return f"""{filename_prefix_for_name(__a )}-{split}""" def UpperCAmelCase_ (__a : Any , __a : Dict , __a : Tuple , __a : Optional[Any]=None ): """simple docstring""" _a : Optional[int] = filename_prefix_for_split(__a , __a ) if filetype_suffix: prefix += f""".{filetype_suffix}""" _a : str = os.path.join(__a , __a ) return f"""{filepath}*""" def UpperCAmelCase_ (__a : Optional[Any] , __a : List[Any] , __a : int , __a : int=None , __a : List[Any]=None ): """simple docstring""" _a : Dict = filename_prefix_for_split(__a , __a ) _a : Union[str, Any] = os.path.join(__a , __a ) if shard_lengths: _a : Optional[int] = len(__a ) _a : List[str] = [f"""{prefix}-{shard_id:05d}-of-{num_shards:05d}""" for shard_id in range(__a )] if filetype_suffix: _a : List[str] = [filename + f""".{filetype_suffix}""" for filename in filenames] return filenames else: _a : Any = prefix if filetype_suffix: filename += f""".{filetype_suffix}""" return [filename]
361
'''simple docstring''' import inspect import unittest from transformers import ViTMSNConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTMSNForImageClassification, ViTMSNModel from transformers.models.vit_msn.modeling_vit_msn import VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class UpperCAmelCase__ : """simple docstring""" def __init__( self : int ,_a : List[str] ,_a : Optional[Any]=13 ,_a : str=30 ,_a : str=2 ,_a : Union[str, Any]=3 ,_a : Optional[Any]=True ,_a : int=True ,_a : Union[str, Any]=32 ,_a : List[Any]=5 ,_a : Union[str, Any]=4 ,_a : int=37 ,_a : Any="gelu" ,_a : Union[str, Any]=0.1 ,_a : str=0.1 ,_a : List[str]=10 ,_a : Dict=0.02 ,_a : Tuple=None ,): '''simple docstring''' _a : Any = parent _a : int = batch_size _a : List[Any] = image_size _a : Optional[int] = patch_size _a : List[str] = num_channels _a : Dict = is_training _a : Dict = use_labels _a : Optional[Any] = hidden_size _a : str = num_hidden_layers _a : Optional[int] = num_attention_heads _a : Dict = intermediate_size _a : Union[str, Any] = hidden_act _a : List[str] = hidden_dropout_prob _a : Any = attention_probs_dropout_prob _a : List[str] = type_sequence_label_size _a : int = initializer_range _a : List[Any] = scope # in ViT MSN, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) _a : Union[str, Any] = (image_size // patch_size) ** 2 _a : Tuple = num_patches + 1 def __lowercase ( self : Any ): '''simple docstring''' _a : Tuple = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _a : str = None if self.use_labels: _a : Tuple = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) _a : List[str] = self.get_config() return config, pixel_values, labels def __lowercase ( self : Optional[int] ): '''simple docstring''' return ViTMSNConfig( 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 ,initializer_range=self.initializer_range ,) def __lowercase ( self : Tuple ,_a : Any ,_a : List[Any] ,_a : int ): '''simple docstring''' _a : str = ViTMSNModel(config=_a ) model.to(_a ) model.eval() _a : int = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def __lowercase ( self : List[Any] ,_a : str ,_a : Tuple ,_a : Dict ): '''simple docstring''' _a : Tuple = self.type_sequence_label_size _a : int = ViTMSNForImageClassification(_a ) model.to(_a ) model.eval() _a : Dict = model(_a ,labels=_a ) print('Pixel and labels shape: {pixel_values.shape}, {labels.shape}' ) print('Labels: {labels}' ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.type_sequence_label_size) ) # test greyscale images _a : int = 1 _a : Optional[Any] = ViTMSNForImageClassification(_a ) model.to(_a ) model.eval() _a : Any = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) _a : Optional[int] = model(_a ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.type_sequence_label_size) ) def __lowercase ( self : Any ): '''simple docstring''' _a : Optional[int] = self.prepare_config_and_inputs() _a, _a, _a : int = config_and_inputs _a : List[Any] = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class UpperCAmelCase__ ( lowercase__ , lowercase__ , unittest.TestCase ): """simple docstring""" __UpperCAmelCase : Tuple = (ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else () __UpperCAmelCase : List[Any] = ( {'''feature-extraction''': ViTMSNModel, '''image-classification''': ViTMSNForImageClassification} if is_torch_available() else {} ) __UpperCAmelCase : str = False __UpperCAmelCase : Optional[Any] = False __UpperCAmelCase : List[str] = False __UpperCAmelCase : int = False def __lowercase ( self : Optional[int] ): '''simple docstring''' _a : List[str] = ViTMSNModelTester(self ) _a : Optional[int] = ConfigTester(self ,config_class=_a ,has_text_modality=_a ,hidden_size=37 ) def __lowercase ( self : str ): '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason='ViTMSN does not use inputs_embeds' ) def __lowercase ( self : List[str] ): '''simple docstring''' pass def __lowercase ( self : Union[str, Any] ): '''simple docstring''' _a, _a : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : List[Any] = model_class(_a ) self.assertIsInstance(model.get_input_embeddings() ,(nn.Module) ) _a : Dict = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_a ,nn.Linear ) ) def __lowercase ( self : Any ): '''simple docstring''' _a, _a : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : List[str] = model_class(_a ) _a : str = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a : List[Any] = [*signature.parameters.keys()] _a : int = ['pixel_values'] self.assertListEqual(arg_names[:1] ,_a ) def __lowercase ( self : List[str] ): '''simple docstring''' _a : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def __lowercase ( self : Optional[Any] ): '''simple docstring''' _a : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_a ) @slow def __lowercase ( self : int ): '''simple docstring''' for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a : Dict = ViTMSNModel.from_pretrained(_a ) self.assertIsNotNone(_a ) def UpperCAmelCase_ (): """simple docstring""" _a : List[str] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class UpperCAmelCase__ ( unittest.TestCase ): """simple docstring""" @cached_property def __lowercase ( self : Union[str, Any] ): '''simple docstring''' return ViTImageProcessor.from_pretrained('facebook/vit-msn-small' ) if is_vision_available() else None @slow def __lowercase ( self : Union[str, Any] ): '''simple docstring''' torch.manual_seed(2 ) _a : List[str] = ViTMSNForImageClassification.from_pretrained('facebook/vit-msn-small' ).to(_a ) _a : List[str] = self.default_image_processor _a : int = prepare_img() _a : Tuple = image_processor(images=_a ,return_tensors='pt' ).to(_a ) # forward pass with torch.no_grad(): _a : Optional[int] = model(**_a ) # verify the logits _a : Union[str, Any] = torch.Size((1, 1000) ) self.assertEqual(outputs.logits.shape ,_a ) _a : List[Any] = torch.tensor([-0.0803, -0.4454, -0.2375] ).to(_a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] ,_a ,atol=1E-4 ) )
5
0
'''simple docstring''' 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 UpperCAmelCase__ : """simple docstring""" __UpperCAmelCase : Union[str, Any] = XGLMConfig __UpperCAmelCase : Optional[int] = {} __UpperCAmelCase : List[str] = '''gelu''' def __init__( self : Optional[Any] ,_a : Tuple ,_a : Any=14 ,_a : str=7 ,_a : List[Any]=True ,_a : Dict=True ,_a : Optional[int]=True ,_a : Dict=99 ,_a : str=32 ,_a : List[str]=2 ,_a : List[str]=4 ,_a : str=37 ,_a : Tuple="gelu" ,_a : Any=0.1 ,_a : List[Any]=0.1 ,_a : Optional[Any]=512 ,_a : Optional[int]=0.02 ,): '''simple docstring''' _a : Tuple = parent _a : Union[str, Any] = batch_size _a : Optional[Any] = seq_length _a : str = is_training _a : List[Any] = use_input_mask _a : Dict = use_labels _a : List[str] = vocab_size _a : Optional[int] = d_model _a : Union[str, Any] = num_hidden_layers _a : Any = num_attention_heads _a : List[str] = ffn_dim _a : str = activation_function _a : Any = activation_dropout _a : Dict = attention_dropout _a : Optional[int] = max_position_embeddings _a : List[Any] = initializer_range _a : Union[str, Any] = None _a : List[Any] = 0 _a : Optional[int] = 2 _a : List[Any] = 1 def __lowercase ( self : Any ): '''simple docstring''' return XGLMConfig.from_pretrained('facebook/xglm-564M' ) def __lowercase ( self : Optional[int] ): '''simple docstring''' _a : List[Any] = tf.clip_by_value( ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) ,clip_value_min=0 ,clip_value_max=3 ) _a : List[Any] = None if self.use_input_mask: _a : Tuple = random_attention_mask([self.batch_size, self.seq_length] ) _a : Dict = self.get_config() _a : Any = floats_tensor([self.num_hidden_layers, self.num_attention_heads] ,2 ) return ( config, input_ids, input_mask, head_mask, ) def __lowercase ( self : Tuple ): '''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=_a ,bos_token_id=self.bos_token_id ,eos_token_id=self.eos_token_id ,pad_token_id=self.pad_token_id ,return_dict=_a ,) def __lowercase ( self : Any ): '''simple docstring''' _a : int = self.prepare_config_and_inputs() ( _a ) : Tuple = config_and_inputs _a : Tuple = { 'input_ids': input_ids, 'head_mask': head_mask, } return config, inputs_dict @require_tf class UpperCAmelCase__ ( lowercase__ , lowercase__ , unittest.TestCase ): """simple docstring""" __UpperCAmelCase : int = (TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else () __UpperCAmelCase : Optional[int] = (TFXGLMForCausalLM,) if is_tf_available() else () __UpperCAmelCase : Optional[int] = ( {'''feature-extraction''': TFXGLMModel, '''text-generation''': TFXGLMForCausalLM} if is_tf_available() else {} ) __UpperCAmelCase : Any = False __UpperCAmelCase : Dict = False __UpperCAmelCase : Optional[int] = False def __lowercase ( self : List[str] ): '''simple docstring''' _a : int = TFXGLMModelTester(self ) _a : List[str] = ConfigTester(self ,config_class=_a ,n_embd=37 ) def __lowercase ( self : str ): '''simple docstring''' self.config_tester.run_common_tests() @slow def __lowercase ( self : Optional[int] ): '''simple docstring''' for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a : int = TFXGLMModel.from_pretrained(_a ) self.assertIsNotNone(_a ) @unittest.skip(reason='Currently, model embeddings are going to undergo a major refactor.' ) def __lowercase ( self : Union[str, Any] ): '''simple docstring''' super().test_resize_token_embeddings() @require_tf class UpperCAmelCase__ ( unittest.TestCase ): """simple docstring""" @slow def __lowercase ( self : Optional[Any] ,_a : Union[str, Any]=True ): '''simple docstring''' _a : Union[str, Any] = TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) _a : str = 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 _a : str = [2, 268, 9865, 67, 11, 1988, 5_7252, 9865, 5, 984, 67, 1988, 21_3838, 1658, 53, 7_0446, 33, 6657, 278, 1581] # fmt: on _a : List[str] = model.generate(_a ,do_sample=_a ,num_beams=1 ) if verify_outputs: self.assertListEqual(output_ids[0].numpy().tolist() ,_a ) @slow def __lowercase ( self : Union[str, Any] ): '''simple docstring''' _a : Union[str, Any] = XGLMTokenizer.from_pretrained('facebook/xglm-564M' ) _a : Optional[Any] = TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) tf.random.set_seed(0 ) _a : int = tokenizer('Today is a nice day and' ,return_tensors='tf' ) _a : Optional[int] = 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' ): _a : Optional[int] = model.generate(_a ,do_sample=_a ,seed=[7, 0] ) _a : Any = tokenizer.decode(output_ids[0] ,skip_special_tokens=_a ) _a : List[str] = ( 'Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due' ) self.assertEqual(_a ,_a ) @slow def __lowercase ( self : str ): '''simple docstring''' _a : int = TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) _a : Union[str, Any] = XGLMTokenizer.from_pretrained('facebook/xglm-564M' ) _a : Any = 'left' # use different length sentences to test batching _a : Union[str, Any] = [ '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', ] _a : Union[str, Any] = tokenizer(_a ,return_tensors='tf' ,padding=_a ) _a : Dict = inputs['input_ids'] _a : Dict = model.generate(input_ids=_a ,attention_mask=inputs['attention_mask'] ,max_new_tokens=12 ) _a : List[str] = tokenizer(sentences[0] ,return_tensors='tf' ).input_ids _a : List[str] = model.generate(input_ids=_a ,max_new_tokens=12 ) _a : Optional[Any] = tokenizer(sentences[1] ,return_tensors='tf' ).input_ids _a : Union[str, Any] = model.generate(input_ids=_a ,max_new_tokens=12 ) _a : Dict = tokenizer.batch_decode(_a ,skip_special_tokens=_a ) _a : Union[str, Any] = tokenizer.decode(output_non_padded[0] ,skip_special_tokens=_a ) _a : Dict = tokenizer.decode(output_padded[0] ,skip_special_tokens=_a ) _a : Any = [ '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(_a ,_a ) self.assertListEqual(_a ,[non_padded_sentence, padded_sentence] )
362
'''simple docstring''' import requests from bsa import BeautifulSoup def UpperCAmelCase_ (__a : str = "https://www.worldometers.info/coronavirus" ): """simple docstring""" _a : List[str] = BeautifulSoup(requests.get(__a ).text , 'html.parser' ) _a : Dict = soup.findAll('h1' ) _a : Union[str, Any] = soup.findAll('div' , {'class': 'maincounter-number'} ) keys += soup.findAll('span' , {'class': 'panel-title'} ) values += soup.findAll('div' , {'class': 'number-table-main'} ) return {key.text.strip(): value.text.strip() for key, value in zip(__a , __a )} if __name__ == "__main__": print("""\033[1m""" + """COVID-19 Status of the World""" + """\033[0m\n""") for key, value in world_covidaa_stats().items(): print(f'''{key}\n{value}\n''')
5
0
'''simple docstring''' from queue import Queue from typing import TYPE_CHECKING, Optional if TYPE_CHECKING: from ..models.auto import AutoTokenizer class UpperCAmelCase__ : """simple docstring""" def __lowercase ( self : Any ,_a : List[Any] ): '''simple docstring''' raise NotImplementedError() def __lowercase ( self : List[str] ): '''simple docstring''' raise NotImplementedError() class UpperCAmelCase__ ( lowercase__ ): """simple docstring""" def __init__( self : Tuple ,_a : "AutoTokenizer" ,_a : bool = False ,**_a : Tuple ): '''simple docstring''' _a : Union[str, Any] = tokenizer _a : Any = skip_prompt _a : List[str] = decode_kwargs # variables used in the streaming process _a : Tuple = [] _a : int = 0 _a : List[Any] = True def __lowercase ( self : List[str] ,_a : Tuple ): '''simple docstring''' if len(value.shape ) > 1 and value.shape[0] > 1: raise ValueError('TextStreamer only supports batch size 1' ) elif len(value.shape ) > 1: _a : int = value[0] if self.skip_prompt and self.next_tokens_are_prompt: _a : Tuple = False return # Add the new token to the cache and decodes the entire thing. self.token_cache.extend(value.tolist() ) _a : Any = self.tokenizer.decode(self.token_cache ,**self.decode_kwargs ) # After the symbol for a new line, we flush the cache. if text.endswith('\n' ): _a : str = text[self.print_len :] _a : Optional[Any] = [] _a : List[str] = 0 # If the last token is a CJK character, we print the characters. elif len(_a ) > 0 and self._is_chinese_char(ord(text[-1] ) ): _a : List[str] = text[self.print_len :] self.print_len += len(_a ) # Otherwise, prints until the last space char (simple heuristic to avoid printing incomplete words, # which may change with the subsequent token -- there are probably smarter ways to do this!) else: _a : int = text[self.print_len : text.rfind(' ' ) + 1] self.print_len += len(_a ) self.on_finalized_text(_a ) def __lowercase ( self : List[Any] ): '''simple docstring''' if len(self.token_cache ) > 0: _a : Dict = self.tokenizer.decode(self.token_cache ,**self.decode_kwargs ) _a : Tuple = text[self.print_len :] _a : str = [] _a : str = 0 else: _a : Tuple = '' _a : str = True self.on_finalized_text(_a ,stream_end=_a ) def __lowercase ( self : Dict ,_a : str ,_a : bool = False ): '''simple docstring''' print(_a ,flush=_a ,end='' if not stream_end else None ) def __lowercase ( self : List[str] ,_a : Optional[Any] ): '''simple docstring''' if ( (cp >= 0X4e00 and cp <= 0X9fff) or (cp >= 0X3400 and cp <= 0X4dbf) # or (cp >= 0X2_0000 and cp <= 0X2_a6df) # or (cp >= 0X2_a700 and cp <= 0X2_b73f) # or (cp >= 0X2_b740 and cp <= 0X2_b81f) # or (cp >= 0X2_b820 and cp <= 0X2_ceaf) # or (cp >= 0Xf900 and cp <= 0Xfaff) or (cp >= 0X2_f800 and cp <= 0X2_fa1f) # ): # return True return False class UpperCAmelCase__ ( lowercase__ ): """simple docstring""" def __init__( self : int ,_a : "AutoTokenizer" ,_a : bool = False ,_a : Optional[float] = None ,**_a : Union[str, Any] ): '''simple docstring''' super().__init__(_a ,_a ,**_a ) _a : List[str] = Queue() _a : Union[str, Any] = None _a : Optional[Any] = timeout def __lowercase ( self : str ,_a : str ,_a : bool = False ): '''simple docstring''' self.text_queue.put(_a ,timeout=self.timeout ) if stream_end: self.text_queue.put(self.stop_signal ,timeout=self.timeout ) def __iter__( self : List[str] ): '''simple docstring''' return self def __lowercase ( self : List[Any] ): '''simple docstring''' _a : Tuple = self.text_queue.get(timeout=self.timeout ) if value == self.stop_signal: raise StopIteration() else: return value
363
'''simple docstring''' import argparse from collections import defaultdict import yaml __lowerCAmelCase = """docs/source/en/_toctree.yml""" def UpperCAmelCase_ (__a : str ): """simple docstring""" _a : Any = defaultdict(__a ) for doc in model_doc: counts[doc["local"]] += 1 _a : List[str] = [key for key, value in counts.items() if value > 1] _a : str = [] for duplicate_key in duplicates: _a : Union[str, Any] = list({doc['title'] for doc in model_doc if doc['local'] == duplicate_key} ) if len(__a ) > 1: raise ValueError( f"""{duplicate_key} is present several times in the documentation table of content at """ '`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the ' 'others.' ) # Only add this once new_doc.append({'local': duplicate_key, 'title': titles[0]} ) # Add none duplicate-keys new_doc.extend([doc for doc in model_doc if counts[doc['local']] == 1] ) # Sort return sorted(__a , key=lambda __a : s["title"].lower() ) def UpperCAmelCase_ (__a : Optional[int]=False ): """simple docstring""" with open(__a , encoding='utf-8' ) as f: _a : Tuple = yaml.safe_load(f.read() ) # Get to the API doc _a : Union[str, Any] = 0 while content[api_idx]["title"] != "API": api_idx += 1 _a : Union[str, Any] = content[api_idx]['sections'] # Then to the model doc _a : List[str] = 0 while api_doc[model_idx]["title"] != "Models": model_idx += 1 _a : List[str] = api_doc[model_idx]['sections'] _a : List[Any] = [(idx, section) for idx, section in enumerate(__a ) if 'sections' in section] _a : Tuple = False for idx, modality_doc in modalities_docs: _a : List[Any] = modality_doc['sections'] _a : Any = clean_model_doc_toc(__a ) if old_modality_doc != new_modality_doc: _a : Union[str, Any] = True if overwrite: _a : str = new_modality_doc if diff: if overwrite: _a : Dict = model_doc _a : Dict = api_doc with open(__a , 'w' , encoding='utf-8' ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) else: raise ValueError( 'The model doc part of the table of content is not properly sorted, run `make style` to fix this.' ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() parser.add_argument("""--fix_and_overwrite""", action="""store_true""", help="""Whether to fix inconsistencies.""") __lowerCAmelCase = parser.parse_args() check_model_doc(args.fix_and_overwrite)
5
0
'''simple docstring''' from torch import nn def UpperCAmelCase_ (__a : Optional[Any] ): """simple docstring""" if act_fn in ["swish", "silu"]: return nn.SiLU() elif act_fn == "mish": return nn.Mish() elif act_fn == "gelu": return nn.GELU() else: raise ValueError(f"""Unsupported activation function: {act_fn}""" )
364
'''simple docstring''' from collections.abc import Generator from math import sin def UpperCAmelCase_ (__a : bytes ): """simple docstring""" if len(__a ) != 3_2: raise ValueError('Input must be of length 32' ) _a : Any = b'' for i in [3, 2, 1, 0]: little_endian += string_aa[8 * i : 8 * i + 8] return little_endian def UpperCAmelCase_ (__a : int ): """simple docstring""" if i < 0: raise ValueError('Input must be non-negative' ) _a : List[str] = format(__a , '08x' )[-8:] _a : str = b'' for i in [3, 2, 1, 0]: little_endian_hex += hex_rep[2 * i : 2 * i + 2].encode('utf-8' ) return little_endian_hex def UpperCAmelCase_ (__a : bytes ): """simple docstring""" _a : List[Any] = b'' for char in message: bit_string += format(__a , '08b' ).encode('utf-8' ) _a : int = format(len(__a ) , '064b' ).encode('utf-8' ) # Pad bit_string to a multiple of 512 chars bit_string += b"1" while len(__a ) % 5_1_2 != 4_4_8: bit_string += b"0" bit_string += to_little_endian(start_len[3_2:] ) + to_little_endian(start_len[:3_2] ) return bit_string def UpperCAmelCase_ (__a : bytes ): """simple docstring""" if len(__a ) % 5_1_2 != 0: raise ValueError('Input must have length that\'s a multiple of 512' ) for pos in range(0 , len(__a ) , 5_1_2 ): _a : List[Any] = bit_string[pos : pos + 5_1_2] _a : str = [] for i in range(0 , 5_1_2 , 3_2 ): block_words.append(int(to_little_endian(block[i : i + 3_2] ) , 2 ) ) yield block_words def UpperCAmelCase_ (__a : int ): """simple docstring""" if i < 0: raise ValueError('Input must be non-negative' ) _a : List[str] = format(__a , '032b' ) _a : int = '' for c in i_str: new_str += "1" if c == "0" else "0" return int(__a , 2 ) def UpperCAmelCase_ (__a : int , __a : int ): """simple docstring""" return (a + b) % 2**3_2 def UpperCAmelCase_ (__a : int , __a : int ): """simple docstring""" if i < 0: raise ValueError('Input must be non-negative' ) if shift < 0: raise ValueError('Shift must be non-negative' ) return ((i << shift) ^ (i >> (3_2 - shift))) % 2**3_2 def UpperCAmelCase_ (__a : bytes ): """simple docstring""" _a : str = preprocess(__a ) _a : Optional[int] = [int(2**3_2 * abs(sin(i + 1 ) ) ) for i in range(6_4 )] # Starting states _a : int = 0x67_45_23_01 _a : Union[str, Any] = 0xEF_CD_AB_89 _a : str = 0x98_BA_DC_FE _a : List[Any] = 0x10_32_54_76 _a : Optional[int] = [ 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, ] # Process bit string in chunks, each with 16 32-char words for block_words in get_block_words(__a ): _a : Union[str, Any] = aa _a : List[Any] = ba _a : List[Any] = ca _a : Dict = da # Hash current chunk for i in range(6_4 ): if i <= 1_5: # f = (b & c) | (not_32(b) & d) # Alternate definition for f _a : Optional[int] = d ^ (b & (c ^ d)) _a : Optional[Any] = i elif i <= 3_1: # f = (d & b) | (not_32(d) & c) # Alternate definition for f _a : Optional[Any] = c ^ (d & (b ^ c)) _a : Dict = (5 * i + 1) % 1_6 elif i <= 4_7: _a : Optional[Any] = b ^ c ^ d _a : Dict = (3 * i + 5) % 1_6 else: _a : int = c ^ (b | not_aa(__a )) _a : List[str] = (7 * i) % 1_6 _a : Optional[int] = (f + a + added_consts[i] + block_words[g]) % 2**3_2 _a : Union[str, Any] = d _a : Tuple = c _a : Optional[int] = b _a : Union[str, Any] = sum_aa(__a , left_rotate_aa(__a , shift_amounts[i] ) ) # Add hashed chunk to running total _a : Any = sum_aa(__a , __a ) _a : Dict = sum_aa(__a , __a ) _a : Union[str, Any] = sum_aa(__a , __a ) _a : str = sum_aa(__a , __a ) _a : Optional[Any] = reformat_hex(__a ) + reformat_hex(__a ) + reformat_hex(__a ) + reformat_hex(__a ) return digest if __name__ == "__main__": import doctest doctest.testmod()
5
0
'''simple docstring''' from __future__ import annotations import typing from collections import Counter def UpperCAmelCase_ (__a : int ): """simple docstring""" _a : typing.Counter[int] = Counter() for base in range(1 , max_perimeter + 1 ): for perpendicular in range(__a , max_perimeter + 1 ): _a : Any = (base * base + perpendicular * perpendicular) ** 0.5 if hypotenuse == int(__a ): _a : Tuple = int(base + perpendicular + hypotenuse ) if perimeter > max_perimeter: continue triplets[perimeter] += 1 return triplets def UpperCAmelCase_ (__a : int = 1_0_0_0 ): """simple docstring""" _a : Any = pythagorean_triple(__a ) return triplets.most_common(1 )[0][0] if __name__ == "__main__": print(f'''Perimeter {solution()} has maximum solutions''')
365
'''simple docstring''' import math from collections import defaultdict from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin, SchedulerOutput def UpperCAmelCase_ (__a : str , __a : Dict=0.999 , __a : List[str]="cosine" , ): """simple docstring""" if alpha_transform_type == "cosine": def alpha_bar_fn(__a : Union[str, Any] ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(__a : int ): return math.exp(t * -12.0 ) else: raise ValueError(f"""Unsupported alpha_tranform_type: {alpha_transform_type}""" ) _a : Tuple = [] for i in range(__a ): _a : Union[str, Any] = i / num_diffusion_timesteps _a : Any = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(__a ) / alpha_bar_fn(__a ) , __a ) ) return torch.tensor(__a , dtype=torch.floataa ) class UpperCAmelCase__ ( lowercase__ , lowercase__ ): """simple docstring""" __UpperCAmelCase : Optional[Any] = [e.name for e in KarrasDiffusionSchedulers] __UpperCAmelCase : Dict = 2 @register_to_config def __init__( self : str ,_a : int = 1000 ,_a : float = 0.0_0085 ,_a : float = 0.012 ,_a : str = "linear" ,_a : Optional[Union[np.ndarray, List[float]]] = None ,_a : str = "epsilon" ,_a : Optional[bool] = False ,_a : Optional[bool] = False ,_a : float = 1.0 ,_a : str = "linspace" ,_a : int = 0 ,): '''simple docstring''' if trained_betas is not None: _a : List[str] = torch.tensor(_a ,dtype=torch.floataa ) elif beta_schedule == "linear": _a : Tuple = torch.linspace(_a ,_a ,_a ,dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. _a : List[str] = ( torch.linspace(beta_start**0.5 ,beta_end**0.5 ,_a ,dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule _a : Dict = betas_for_alpha_bar(_a ,alpha_transform_type='cosine' ) elif beta_schedule == "exp": _a : Tuple = betas_for_alpha_bar(_a ,alpha_transform_type='exp' ) else: raise NotImplementedError(F"""{beta_schedule} does is not implemented for {self.__class__}""" ) _a : Optional[Any] = 1.0 - self.betas _a : Optional[int] = torch.cumprod(self.alphas ,dim=0 ) # set all values self.set_timesteps(_a ,_a ,_a ) _a : Optional[int] = use_karras_sigmas def __lowercase ( self : Any ,_a : Union[str, Any] ,_a : Optional[Any]=None ): '''simple docstring''' if schedule_timesteps is None: _a : List[Any] = self.timesteps _a : Dict = (schedule_timesteps == timestep).nonzero() # The sigma index that is taken for the **very** first `step` # is always the second index (or the last index if there is only 1) # This way we can ensure we don't accidentally skip a sigma in # case we start in the middle of the denoising schedule (e.g. for image-to-image) if len(self._index_counter ) == 0: _a : int = 1 if len(_a ) > 1 else 0 else: _a : str = timestep.cpu().item() if torch.is_tensor(_a ) else timestep _a : str = self._index_counter[timestep_int] return indices[pos].item() @property def __lowercase ( self : Optional[Any] ): '''simple docstring''' if self.config.timestep_spacing in ["linspace", "trailing"]: return self.sigmas.max() return (self.sigmas.max() ** 2 + 1) ** 0.5 def __lowercase ( self : int ,_a : torch.FloatTensor ,_a : Union[float, torch.FloatTensor] ,): '''simple docstring''' _a : List[Any] = self.index_for_timestep(_a ) _a : Tuple = self.sigmas[step_index] _a : Optional[Any] = sample / ((sigma**2 + 1) ** 0.5) return sample def __lowercase ( self : Any ,_a : int ,_a : Union[str, torch.device] = None ,_a : Optional[int] = None ,): '''simple docstring''' _a : Optional[Any] = num_inference_steps _a : Dict = num_train_timesteps or self.config.num_train_timesteps # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891 if self.config.timestep_spacing == "linspace": _a : Optional[Any] = np.linspace(0 ,num_train_timesteps - 1 ,_a ,dtype=_a )[::-1].copy() elif self.config.timestep_spacing == "leading": _a : str = num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 _a : int = (np.arange(0 ,_a ) * step_ratio).round()[::-1].copy().astype(_a ) timesteps += self.config.steps_offset elif self.config.timestep_spacing == "trailing": _a : Any = num_train_timesteps / self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 _a : Union[str, Any] = (np.arange(_a ,0 ,-step_ratio )).round().copy().astype(_a ) timesteps -= 1 else: raise ValueError( F"""{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'.""" ) _a : Tuple = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5 ) _a : Union[str, Any] = np.log(_a ) _a : str = np.interp(_a ,np.arange(0 ,len(_a ) ) ,_a ) if self.config.use_karras_sigmas: _a : List[Any] = self._convert_to_karras(in_sigmas=_a ,num_inference_steps=self.num_inference_steps ) _a : Dict = np.array([self._sigma_to_t(_a ,_a ) for sigma in sigmas] ) _a : int = np.concatenate([sigmas, [0.0]] ).astype(np.floataa ) _a : Union[str, Any] = torch.from_numpy(_a ).to(device=_a ) _a : Any = torch.cat([sigmas[:1], sigmas[1:-1].repeat_interleave(2 ), sigmas[-1:]] ) _a : List[Any] = torch.from_numpy(_a ) _a : List[str] = torch.cat([timesteps[:1], timesteps[1:].repeat_interleave(2 )] ) if str(_a ).startswith('mps' ): # mps does not support float64 _a : Tuple = timesteps.to(_a ,dtype=torch.floataa ) else: _a : Dict = timesteps.to(device=_a ) # empty dt and derivative _a : Tuple = None _a : Optional[Any] = None # for exp beta schedules, such as the one for `pipeline_shap_e.py` # we need an index counter _a : Union[str, Any] = defaultdict(_a ) def __lowercase ( self : str ,_a : Dict ,_a : Dict ): '''simple docstring''' _a : Optional[int] = np.log(_a ) # get distribution _a : Union[str, Any] = log_sigma - log_sigmas[:, np.newaxis] # get sigmas range _a : List[Any] = np.cumsum((dists >= 0) ,axis=0 ).argmax(axis=0 ).clip(max=log_sigmas.shape[0] - 2 ) _a : Tuple = low_idx + 1 _a : Union[str, Any] = log_sigmas[low_idx] _a : Optional[Any] = log_sigmas[high_idx] # interpolate sigmas _a : Optional[Any] = (low - log_sigma) / (low - high) _a : List[str] = np.clip(_a ,0 ,1 ) # transform interpolation to time range _a : Union[str, Any] = (1 - w) * low_idx + w * high_idx _a : List[str] = t.reshape(sigma.shape ) return t def __lowercase ( self : int ,_a : torch.FloatTensor ,_a : Tuple ): '''simple docstring''' _a : float = in_sigmas[-1].item() _a : float = in_sigmas[0].item() _a : Tuple = 7.0 # 7.0 is the value used in the paper _a : str = np.linspace(0 ,1 ,_a ) _a : Optional[Any] = sigma_min ** (1 / rho) _a : Union[str, Any] = sigma_max ** (1 / rho) _a : str = (max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho return sigmas @property def __lowercase ( self : Optional[Any] ): '''simple docstring''' return self.dt is None def __lowercase ( self : int ,_a : Union[torch.FloatTensor, np.ndarray] ,_a : Union[float, torch.FloatTensor] ,_a : Union[torch.FloatTensor, np.ndarray] ,_a : bool = True ,): '''simple docstring''' _a : Union[str, Any] = self.index_for_timestep(_a ) # advance index counter by 1 _a : Any = timestep.cpu().item() if torch.is_tensor(_a ) else timestep self._index_counter[timestep_int] += 1 if self.state_in_first_order: _a : Tuple = self.sigmas[step_index] _a : int = self.sigmas[step_index + 1] else: # 2nd order / Heun's method _a : List[str] = self.sigmas[step_index - 1] _a : List[Any] = self.sigmas[step_index] # currently only gamma=0 is supported. This usually works best anyways. # We can support gamma in the future but then need to scale the timestep before # passing it to the model which requires a change in API _a : Optional[int] = 0 _a : Tuple = sigma * (gamma + 1) # Note: sigma_hat == sigma for now # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise if self.config.prediction_type == "epsilon": _a : Dict = sigma_hat if self.state_in_first_order else sigma_next _a : Optional[int] = sample - sigma_input * model_output elif self.config.prediction_type == "v_prediction": _a : List[Any] = sigma_hat if self.state_in_first_order else sigma_next _a : List[Any] = model_output * (-sigma_input / (sigma_input**2 + 1) ** 0.5) + ( sample / (sigma_input**2 + 1) ) elif self.config.prediction_type == "sample": _a : Union[str, Any] = model_output else: raise ValueError( F"""prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`""" ) if self.config.clip_sample: _a : Optional[int] = pred_original_sample.clamp( -self.config.clip_sample_range ,self.config.clip_sample_range ) if self.state_in_first_order: # 2. Convert to an ODE derivative for 1st order _a : Optional[Any] = (sample - pred_original_sample) / sigma_hat # 3. delta timestep _a : Any = sigma_next - sigma_hat # store for 2nd order step _a : int = derivative _a : List[str] = dt _a : Union[str, Any] = sample else: # 2. 2nd order / Heun's method _a : Dict = (sample - pred_original_sample) / sigma_next _a : Tuple = (self.prev_derivative + derivative) / 2 # 3. take prev timestep & sample _a : Optional[Any] = self.dt _a : Union[str, Any] = self.sample # free dt and derivative # Note, this puts the scheduler in "first order mode" _a : List[Any] = None _a : Union[str, Any] = None _a : Dict = None _a : str = sample + derivative * dt if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=_a ) def __lowercase ( self : Optional[int] ,_a : torch.FloatTensor ,_a : torch.FloatTensor ,_a : torch.FloatTensor ,): '''simple docstring''' _a : str = self.sigmas.to(device=original_samples.device ,dtype=original_samples.dtype ) if original_samples.device.type == "mps" and torch.is_floating_point(_a ): # mps does not support float64 _a : Dict = self.timesteps.to(original_samples.device ,dtype=torch.floataa ) _a : Optional[Any] = timesteps.to(original_samples.device ,dtype=torch.floataa ) else: _a : int = self.timesteps.to(original_samples.device ) _a : Optional[Any] = timesteps.to(original_samples.device ) _a : Any = [self.index_for_timestep(_a ,_a ) for t in timesteps] _a : Optional[int] = sigmas[step_indices].flatten() while len(sigma.shape ) < len(original_samples.shape ): _a : Optional[Any] = sigma.unsqueeze(-1 ) _a : Any = original_samples + noise * sigma return noisy_samples def __len__( self : Optional[int] ): '''simple docstring''' return self.config.num_train_timesteps
5
0
'''simple docstring''' import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = { """microsoft/wavlm-base""": """https://huggingface.co/microsoft/wavlm-base/resolve/main/config.json""", # See all WavLM models at https://huggingface.co/models?filter=wavlm } class UpperCAmelCase__ ( lowercase__ ): """simple docstring""" __UpperCAmelCase : Dict = '''wavlm''' def __init__( self : List[Any] ,_a : Any=32 ,_a : str=768 ,_a : List[str]=12 ,_a : Optional[Any]=12 ,_a : Union[str, Any]=3072 ,_a : Dict="gelu" ,_a : Union[str, Any]=0.1 ,_a : str=0.1 ,_a : List[Any]=0.1 ,_a : Optional[Any]=0.0 ,_a : Dict=0.1 ,_a : Optional[int]=0.1 ,_a : List[str]=0.02 ,_a : Any=1E-5 ,_a : str="group" ,_a : int="gelu" ,_a : str=(512, 512, 512, 512, 512, 512, 512) ,_a : Any=(5, 2, 2, 2, 2, 2, 2) ,_a : List[str]=(10, 3, 3, 3, 3, 2, 2) ,_a : List[str]=False ,_a : int=128 ,_a : Optional[Any]=16 ,_a : Tuple=320 ,_a : Optional[int]=800 ,_a : Tuple=False ,_a : Any=True ,_a : str=0.05 ,_a : Optional[Any]=10 ,_a : Any=2 ,_a : int=0.0 ,_a : int=10 ,_a : Optional[int]=320 ,_a : List[Any]=2 ,_a : Any=0.1 ,_a : Tuple=100 ,_a : Tuple=256 ,_a : int=256 ,_a : Tuple=0.1 ,_a : Optional[int]="mean" ,_a : List[str]=False ,_a : Union[str, Any]=False ,_a : Optional[int]=256 ,_a : Any=(512, 512, 512, 512, 1500) ,_a : Union[str, Any]=(5, 3, 3, 1, 1) ,_a : Union[str, Any]=(1, 2, 3, 1, 1) ,_a : Tuple=512 ,_a : Union[str, Any]=80 ,_a : int=0 ,_a : Dict=1 ,_a : Tuple=2 ,_a : str=False ,_a : Any=3 ,_a : Any=2 ,_a : Union[str, Any]=3 ,_a : List[str]=None ,**_a : Optional[int] ,): '''simple docstring''' super().__init__(**_a ,pad_token_id=_a ,bos_token_id=_a ,eos_token_id=_a ) _a : Any = hidden_size _a : Union[str, Any] = feat_extract_norm _a : List[str] = feat_extract_activation _a : Optional[int] = list(_a ) _a : str = list(_a ) _a : Dict = list(_a ) _a : int = conv_bias _a : Dict = num_buckets _a : Optional[Any] = max_bucket_distance _a : List[str] = num_conv_pos_embeddings _a : int = num_conv_pos_embedding_groups _a : Union[str, Any] = len(self.conv_dim ) _a : Optional[int] = num_hidden_layers _a : Any = intermediate_size _a : Any = hidden_act _a : Optional[int] = num_attention_heads _a : str = hidden_dropout _a : Tuple = attention_dropout _a : Union[str, Any] = activation_dropout _a : str = feat_proj_dropout _a : str = final_dropout _a : List[str] = layerdrop _a : List[str] = layer_norm_eps _a : str = initializer_range _a : str = num_ctc_classes _a : Union[str, Any] = vocab_size _a : Dict = do_stable_layer_norm _a : Optional[int] = use_weighted_layer_sum _a : List[str] = classifier_proj_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( 'Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==' ' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =' F""" {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,""" F""" `len(config.conv_kernel) = {len(self.conv_kernel )}`.""" ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 _a : List[str] = apply_spec_augment _a : Tuple = mask_time_prob _a : Optional[Any] = mask_time_length _a : List[str] = mask_time_min_masks _a : Tuple = mask_feature_prob _a : List[Any] = mask_feature_length # parameters for pretraining with codevector quantized representations _a : List[Any] = num_codevectors_per_group _a : Any = num_codevector_groups _a : Any = contrastive_logits_temperature _a : Union[str, Any] = num_negatives _a : Tuple = codevector_dim _a : Tuple = proj_codevector_dim _a : List[Any] = diversity_loss_weight # ctc loss _a : int = ctc_loss_reduction _a : List[Any] = ctc_zero_infinity # adapter _a : Optional[Any] = add_adapter _a : int = adapter_kernel_size _a : List[Any] = adapter_stride _a : Tuple = num_adapter_layers _a : Dict = output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. _a : Tuple = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. _a : Optional[int] = list(_a ) _a : Dict = list(_a ) _a : str = list(_a ) _a : List[str] = xvector_output_dim @property def __lowercase ( self : Union[str, Any] ): '''simple docstring''' return functools.reduce(operator.mul ,self.conv_stride ,1 )
366
'''simple docstring''' import qiskit def UpperCAmelCase_ (__a : int , __a : int ): """simple docstring""" _a : Any = qiskit.Aer.get_backend('aer_simulator' ) # Create a Quantum Circuit acting on the q register _a : List[Any] = qiskit.QuantumCircuit(__a , __a ) # Map the quantum measurement to the classical bits circuit.measure([0] , [0] ) # Execute the circuit on the simulator _a : Tuple = qiskit.execute(__a , __a , shots=1_0_0_0 ) # Return the histogram data of the results of the experiment. return job.result().get_counts(__a ) if __name__ == "__main__": print(f'''Total count for various states are: {single_qubit_measure(1, 1)}''')
5
0
'''simple docstring''' import os def UpperCAmelCase_ () -> Optional[int]: """simple docstring""" _a : Any = os.path.dirname(os.path.realpath(__a ) ) _a : Optional[int] = os.path.join(__a , 'triangle.txt' ) with open(__a ) as f: _a : int = f.readlines() _a : str = [] for line in triangle: _a : str = [] for number in line.strip().split(' ' ): numbers_from_line.append(int(__a ) ) a.append(__a ) for i in range(1 , len(__a ) ): for j in range(len(a[i] ) ): _a : List[Any] = a[i - 1][j] if j != len(a[i - 1] ) else 0 _a : int = a[i - 1][j - 1] if j > 0 else 0 a[i][j] += max(__a , __a ) return max(a[-1] ) if __name__ == "__main__": print(solution())
367
'''simple docstring''' import gc import unittest from diffusers import FlaxStableDiffusionInpaintPipeline from diffusers.utils import is_flax_available, load_image, slow from diffusers.utils.testing_utils import require_flax if is_flax_available(): import jax import jax.numpy as jnp from flax.jax_utils import replicate from flax.training.common_utils import shard @slow @require_flax class UpperCAmelCase__ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self : Optional[Any] ): '''simple docstring''' super().tearDown() gc.collect() def __lowercase ( self : str ): '''simple docstring''' _a : Optional[Any] = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/sd2-inpaint/init_image.png' ) _a : Optional[int] = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png' ) _a : List[str] = 'xvjiarui/stable-diffusion-2-inpainting' _a, _a : str = FlaxStableDiffusionInpaintPipeline.from_pretrained(_a ,safety_checker=_a ) _a : str = 'Face of a yellow cat, high resolution, sitting on a park bench' _a : int = jax.random.PRNGKey(0 ) _a : Tuple = 50 _a : Any = jax.device_count() _a : Dict = num_samples * [prompt] _a : Optional[Any] = num_samples * [init_image] _a : str = num_samples * [mask_image] _a, _a, _a : Optional[Any] = pipeline.prepare_inputs(_a ,_a ,_a ) # shard inputs and rng _a : Optional[Any] = replicate(_a ) _a : str = jax.random.split(_a ,jax.device_count() ) _a : Dict = shard(_a ) _a : int = shard(_a ) _a : int = shard(_a ) _a : Union[str, Any] = pipeline( _a ,_a ,_a ,_a ,_a ,_a ,jit=_a ) _a : Union[str, Any] = output.images.reshape(_a ,512 ,512 ,3 ) _a : Union[str, Any] = images[0, 253:256, 253:256, -1] _a : str = jnp.asarray(jax.device_get(image_slice.flatten() ) ) _a : Union[str, Any] = jnp.array( [0.361_1307, 0.3764_9736, 0.375_7408, 0.3821_3953, 0.3929_5167, 0.384_1631, 0.4155_4978, 0.413_7475, 0.421_7084] ) print(F"""output_slice: {output_slice}""" ) assert jnp.abs(output_slice - expected_slice ).max() < 1E-2
5
0
'''simple docstring''' from __future__ import annotations import unittest from transformers import AutoTokenizer, MBartConfig, is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFMBartForConditionalGeneration, TFMBartModel @require_tf class UpperCAmelCase__ : """simple docstring""" __UpperCAmelCase : Optional[Any] = MBartConfig __UpperCAmelCase : Union[str, Any] = {} __UpperCAmelCase : List[Any] = '''gelu''' def __init__( self : Dict ,_a : Union[str, Any] ,_a : List[str]=13 ,_a : List[str]=7 ,_a : List[str]=True ,_a : List[Any]=False ,_a : Any=99 ,_a : int=32 ,_a : Any=2 ,_a : List[Any]=4 ,_a : Optional[Any]=37 ,_a : Tuple=0.1 ,_a : str=0.1 ,_a : int=20 ,_a : Tuple=2 ,_a : Tuple=1 ,_a : Union[str, Any]=0 ,): '''simple docstring''' _a : Any = parent _a : int = batch_size _a : Optional[int] = seq_length _a : int = is_training _a : Dict = use_labels _a : Optional[Any] = vocab_size _a : Union[str, Any] = hidden_size _a : Any = num_hidden_layers _a : Dict = num_attention_heads _a : List[Any] = intermediate_size _a : Dict = hidden_dropout_prob _a : Optional[Any] = attention_probs_dropout_prob _a : Union[str, Any] = max_position_embeddings _a : Optional[int] = eos_token_id _a : List[str] = pad_token_id _a : str = bos_token_id def __lowercase ( self : Dict ): '''simple docstring''' _a : List[Any] = ids_tensor([self.batch_size, self.seq_length - 1] ,self.vocab_size ) _a : int = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) ,1 ) _a : str = tf.concat([input_ids, eos_tensor] ,axis=1 ) _a : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) _a : Optional[Any] = self.config_cls( vocab_size=self.vocab_size ,d_model=self.hidden_size ,encoder_layers=self.num_hidden_layers ,decoder_layers=self.num_hidden_layers ,encoder_attention_heads=self.num_attention_heads ,decoder_attention_heads=self.num_attention_heads ,encoder_ffn_dim=self.intermediate_size ,decoder_ffn_dim=self.intermediate_size ,dropout=self.hidden_dropout_prob ,attention_dropout=self.attention_probs_dropout_prob ,max_position_embeddings=self.max_position_embeddings ,eos_token_ids=[2] ,bos_token_id=self.bos_token_id ,pad_token_id=self.pad_token_id ,decoder_start_token_id=self.pad_token_id ,**self.config_updates ,) _a : int = prepare_mbart_inputs_dict(_a ,_a ,_a ) return config, inputs_dict def __lowercase ( self : List[Any] ,_a : Optional[Any] ,_a : Optional[Any] ): '''simple docstring''' _a : Any = TFMBartModel(config=_a ).get_decoder() _a : Tuple = inputs_dict['input_ids'] _a : Optional[int] = input_ids[:1, :] _a : int = inputs_dict['attention_mask'][:1, :] _a : List[str] = inputs_dict['head_mask'] _a : List[Any] = 1 # first forward pass _a : List[Any] = model(_a ,attention_mask=_a ,head_mask=_a ,use_cache=_a ) _a : List[str] = outputs.to_tuple() _a : Union[str, Any] = past_key_values[1] def UpperCAmelCase_ (__a : Tuple , __a : List[Any] , __a : List[str] , __a : int=None , __a : Optional[int]=None , __a : Dict=None , __a : List[str]=None , __a : Union[str, Any]=None , ): if attention_mask is None: _a : Union[str, Any] = tf.cast(tf.math.not_equal(__a , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: _a : Tuple = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: _a : str = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: _a : str = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: _a : Optional[Any] = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class UpperCAmelCase__ ( lowercase__ , lowercase__ , unittest.TestCase ): """simple docstring""" __UpperCAmelCase : str = (TFMBartForConditionalGeneration, TFMBartModel) if is_tf_available() else () __UpperCAmelCase : Tuple = (TFMBartForConditionalGeneration,) if is_tf_available() else () __UpperCAmelCase : Any = ( { '''conversational''': TFMBartForConditionalGeneration, '''feature-extraction''': TFMBartModel, '''summarization''': TFMBartForConditionalGeneration, '''text2text-generation''': TFMBartForConditionalGeneration, '''translation''': TFMBartForConditionalGeneration, } if is_tf_available() else {} ) __UpperCAmelCase : Tuple = True __UpperCAmelCase : Optional[Any] = False __UpperCAmelCase : Dict = False def __lowercase ( self : int ,_a : List[str] ,_a : List[str] ,_a : Optional[int] ,_a : Any ,_a : Union[str, Any] ): '''simple docstring''' if pipeline_test_casse_name != "FeatureExtractionPipelineTests": # Exception encountered when calling layer '...' return True return False def __lowercase ( self : Union[str, Any] ): '''simple docstring''' _a : Tuple = TFMBartModelTester(self ) _a : List[Any] = ConfigTester(self ,config_class=_a ) def __lowercase ( self : Tuple ): '''simple docstring''' self.config_tester.run_common_tests() def __lowercase ( self : Union[str, Any] ): '''simple docstring''' _a : str = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*_a ) @require_sentencepiece @require_tokenizers @require_tf class UpperCAmelCase__ ( unittest.TestCase ): """simple docstring""" __UpperCAmelCase : int = [ ''' UN Chief Says There Is No Military Solution in Syria''', ] __UpperCAmelCase : Union[str, Any] = [ '''Şeful ONU declară că nu există o soluţie militară în Siria''', ] __UpperCAmelCase : Dict = '''facebook/mbart-large-en-ro''' @cached_property def __lowercase ( self : List[str] ): '''simple docstring''' return AutoTokenizer.from_pretrained(self.model_name ) @cached_property def __lowercase ( self : Tuple ): '''simple docstring''' _a : List[Any] = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model def __lowercase ( self : List[Any] ,**_a : Tuple ): '''simple docstring''' _a : int = self.translate_src_text(**_a ) self.assertListEqual(self.expected_text ,_a ) def __lowercase ( self : Optional[int] ,**_a : Any ): '''simple docstring''' _a : Dict = self.tokenizer(self.src_text ,**_a ,return_tensors='tf' ) _a : Dict = self.model.generate( model_inputs.input_ids ,attention_mask=model_inputs.attention_mask ,num_beams=2 ) _a : Optional[int] = self.tokenizer.batch_decode(_a ,skip_special_tokens=_a ) return generated_words @slow def __lowercase ( self : Union[str, Any] ): '''simple docstring''' self._assert_generated_batch_equal_expected()
368
'''simple docstring''' def UpperCAmelCase_ (__a : str , __a : str ): """simple docstring""" _a : int = len(__a ) + 1 _a : List[str] = len(__a ) + 1 # dp is a 2d matrix where dp[i][j] denotes whether prefix string of # length i of input_string matches with prefix string of length j of # given pattern. # "dp" stands for dynamic programming. _a : Optional[int] = [[0 for i in range(__a )] for j in range(__a )] # since string of zero length match pattern of zero length _a : str = 1 # since pattern of zero length will never match with string of non-zero length for i in range(1 , __a ): _a : Optional[Any] = 0 # since string of zero length will match with pattern where there # is at least one * alternatively for j in range(1 , __a ): _a : Dict = dp[0][j - 2] if pattern[j - 1] == '*' else 0 # now using bottom-up approach to find for all remaining lengths for i in range(1 , __a ): for j in range(1 , __a ): if input_string[i - 1] == pattern[j - 1] or pattern[j - 1] == ".": _a : Tuple = dp[i - 1][j - 1] elif pattern[j - 1] == "*": if dp[i][j - 2] == 1: _a : List[str] = 1 elif pattern[j - 2] in (input_string[i - 1], "."): _a : int = dp[i - 1][j] else: _a : Any = 0 else: _a : Optional[Any] = 0 return bool(dp[-1][-1] ) if __name__ == "__main__": import doctest doctest.testmod() # inputing the strings # input_string = input("input a string :") # pattern = input("input a pattern :") __lowerCAmelCase = """aab""" __lowerCAmelCase = """c*a*b""" # using function to check whether given string matches the given pattern if match_pattern(input_string, pattern): print(f'''{input_string} matches the given pattern {pattern}''') else: print(f'''{input_string} does not match with the given pattern {pattern}''')
5
0
'''simple docstring''' from math import factorial __lowerCAmelCase = {str(digit): factorial(digit) for digit in range(1_0)} def UpperCAmelCase_ (__a : int ): """simple docstring""" if not isinstance(__a , __a ): raise TypeError('Parameter number must be int' ) if number < 0: raise ValueError('Parameter number must be greater than or equal to 0' ) # Converts number in string to iterate on its digits and adds its factorial. return sum(DIGIT_FACTORIAL[digit] for digit in str(__a ) ) def UpperCAmelCase_ (__a : int = 6_0 , __a : int = 1_0_0_0_0_0_0 ): """simple docstring""" if not isinstance(__a , __a ) or not isinstance(__a , __a ): raise TypeError('Parameters chain_length and number_limit must be int' ) if chain_length <= 0 or number_limit <= 0: raise ValueError( 'Parameters chain_length and number_limit must be greater than 0' ) # the counter for the chains with the exact desired length _a : int = 0 # the cached sizes of the previous chains _a : dict[int, int] = {} for start_chain_element in range(1 , __a ): # The temporary set will contain the elements of the chain _a : str = set() _a : Union[str, Any] = 0 # Stop computing the chain when you find a cached size, a repeating item or the # length is greater then the desired one. _a : Dict = start_chain_element while ( chain_element not in chain_sets_lengths and chain_element not in chain_set and chain_set_length <= chain_length ): chain_set.add(__a ) chain_set_length += 1 _a : List[Any] = digit_factorial_sum(__a ) if chain_element in chain_sets_lengths: chain_set_length += chain_sets_lengths[chain_element] _a : Tuple = chain_set_length # If chain contains the exact amount of elements increase the counter if chain_set_length == chain_length: chains_counter += 1 return chains_counter if __name__ == "__main__": import doctest doctest.testmod() print(f'''{solution()}''')
369
'''simple docstring''' import json import os import unittest from transformers.models.blenderbot_small.tokenization_blenderbot_small import ( VOCAB_FILES_NAMES, BlenderbotSmallTokenizer, ) from ...test_tokenization_common import TokenizerTesterMixin class UpperCAmelCase__ ( lowercase__ , unittest.TestCase ): """simple docstring""" __UpperCAmelCase : Dict = BlenderbotSmallTokenizer __UpperCAmelCase : Tuple = False def __lowercase ( self : List[Any] ): '''simple docstring''' super().setUp() _a : List[str] = ['__start__', 'adapt', 'act', 'ap@@', 'te', '__end__', '__unk__'] _a : Tuple = dict(zip(_a ,range(len(_a ) ) ) ) _a : List[Any] = ['#version: 0.2', 'a p', 't e</w>', 'ap t</w>', 'a d', 'ad apt</w>', 'a c', 'ac t</w>', ''] _a : List[Any] = {'unk_token': '__unk__', 'bos_token': '__start__', 'eos_token': '__end__'} _a : List[Any] = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES['vocab_file'] ) _a : str = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file ,'w' ,encoding='utf-8' ) as fp: fp.write(json.dumps(_a ) + '\n' ) with open(self.merges_file ,'w' ,encoding='utf-8' ) as fp: fp.write('\n'.join(_a ) ) def __lowercase ( self : List[Any] ,**_a : int ): '''simple docstring''' kwargs.update(self.special_tokens_map ) return BlenderbotSmallTokenizer.from_pretrained(self.tmpdirname ,**_a ) def __lowercase ( self : Tuple ,_a : int ): '''simple docstring''' _a : Optional[Any] = 'adapt act apte' _a : Dict = 'adapt act apte' return input_text, output_text def __lowercase ( self : int ): '''simple docstring''' _a : Optional[int] = BlenderbotSmallTokenizer(self.vocab_file ,self.merges_file ,**self.special_tokens_map ) _a : Union[str, Any] = 'adapt act apte' _a : Dict = ['adapt', 'act', 'ap@@', 'te'] _a : Tuple = tokenizer.tokenize(_a ) self.assertListEqual(_a ,_a ) _a : List[str] = [tokenizer.bos_token] + tokens + [tokenizer.eos_token] _a : Dict = [0, 1, 2, 3, 4, 5] self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) ,_a ) def __lowercase ( self : Optional[int] ): '''simple docstring''' _a : str = BlenderbotSmallTokenizer.from_pretrained('facebook/blenderbot-90M' ) assert tok('sam' ).input_ids == [1384] _a : Union[str, Any] = 'I am a small frog.' _a : int = tok([src_text] ,padding=_a ,truncation=_a )['input_ids'] _a : str = tok.batch_decode(_a ,skip_special_tokens=_a ,clean_up_tokenization_spaces=_a )[0] assert src_text != decoded # I wish it did! assert decoded == "i am a small frog ." def __lowercase ( self : Any ): '''simple docstring''' _a : Optional[int] = BlenderbotSmallTokenizer.from_pretrained('facebook/blenderbot-90M' ) _a : Union[str, Any] = 'I am a small frog .' _a : Optional[Any] = '.' _a : Optional[Any] = tok(_a )['input_ids'] _a : Union[str, Any] = tok(_a )['input_ids'] assert encoded[-1] == encoded_dot[0]
5
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __lowerCAmelCase = { """configuration_squeezebert""": [ """SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SqueezeBertConfig""", """SqueezeBertOnnxConfig""", ], """tokenization_squeezebert""": ["""SqueezeBertTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = ["""SqueezeBertTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ """SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """SqueezeBertForMaskedLM""", """SqueezeBertForMultipleChoice""", """SqueezeBertForQuestionAnswering""", """SqueezeBertForSequenceClassification""", """SqueezeBertForTokenClassification""", """SqueezeBertModel""", """SqueezeBertModule""", """SqueezeBertPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_squeezebert import ( SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, SqueezeBertConfig, SqueezeBertOnnxConfig, ) from .tokenization_squeezebert import SqueezeBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_squeezebert_fast import SqueezeBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_squeezebert import ( SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, SqueezeBertModel, SqueezeBertModule, SqueezeBertPreTrainedModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
370
'''simple docstring''' __lowerCAmelCase = { """A""": """.-""", """B""": """-...""", """C""": """-.-.""", """D""": """-..""", """E""": """.""", """F""": """..-.""", """G""": """--.""", """H""": """....""", """I""": """..""", """J""": """.---""", """K""": """-.-""", """L""": """.-..""", """M""": """--""", """N""": """-.""", """O""": """---""", """P""": """.--.""", """Q""": """--.-""", """R""": """.-.""", """S""": """...""", """T""": """-""", """U""": """..-""", """V""": """...-""", """W""": """.--""", """X""": """-..-""", """Y""": """-.--""", """Z""": """--..""", """1""": """.----""", """2""": """..---""", """3""": """...--""", """4""": """....-""", """5""": """.....""", """6""": """-....""", """7""": """--...""", """8""": """---..""", """9""": """----.""", """0""": """-----""", """&""": """.-...""", """@""": """.--.-.""", """:""": """---...""", """,""": """--..--""", """.""": """.-.-.-""", """'""": """.----.""", """\"""": """.-..-.""", """?""": """..--..""", """/""": """-..-.""", """=""": """-...-""", """+""": """.-.-.""", """-""": """-....-""", """(""": """-.--.""", """)""": """-.--.-""", """!""": """-.-.--""", """ """: """/""" } # Exclamation mark is not in ITU-R recommendation # fmt: on __lowerCAmelCase = {value: key for key, value in MORSE_CODE_DICT.items()} def UpperCAmelCase_ (__a : str ): """simple docstring""" return " ".join(MORSE_CODE_DICT[char] for char in message.upper() ) def UpperCAmelCase_ (__a : str ): """simple docstring""" return "".join(REVERSE_DICT[char] for char in message.split() ) def UpperCAmelCase_ (): """simple docstring""" _a : List[str] = 'Morse code here!' print(__a ) _a : Tuple = encrypt(__a ) print(__a ) _a : str = decrypt(__a ) print(__a ) if __name__ == "__main__": main()
5
0
'''simple docstring''' import inspect import unittest from transformers import YolosConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import YolosForObjectDetection, YolosModel from transformers.models.yolos.modeling_yolos import YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class UpperCAmelCase__ : """simple docstring""" def __init__( self : List[str] ,_a : Optional[Any] ,_a : Any=13 ,_a : str=[30, 30] ,_a : Dict=2 ,_a : Optional[Any]=3 ,_a : int=True ,_a : List[Any]=True ,_a : Union[str, Any]=32 ,_a : Dict=5 ,_a : List[str]=4 ,_a : int=37 ,_a : List[str]="gelu" ,_a : Dict=0.1 ,_a : Union[str, Any]=0.1 ,_a : List[str]=10 ,_a : Union[str, Any]=0.02 ,_a : Union[str, Any]=3 ,_a : Any=None ,_a : Optional[Any]=8 ,_a : Tuple=10 ,): '''simple docstring''' _a : List[str] = parent _a : int = batch_size _a : Optional[int] = image_size _a : Optional[int] = patch_size _a : int = num_channels _a : Dict = is_training _a : Tuple = use_labels _a : str = hidden_size _a : Dict = num_hidden_layers _a : List[Any] = num_attention_heads _a : List[str] = intermediate_size _a : int = hidden_act _a : Any = hidden_dropout_prob _a : Optional[Any] = attention_probs_dropout_prob _a : Optional[Any] = type_sequence_label_size _a : Tuple = initializer_range _a : Tuple = num_labels _a : Tuple = scope _a : List[str] = n_targets _a : str = num_detection_tokens # we set the expected sequence length (which is used in several tests) # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) + num_detection_tokens _a : List[Any] = (image_size[1] // patch_size) * (image_size[0] // patch_size) _a : str = num_patches + 1 + self.num_detection_tokens def __lowercase ( self : Optional[int] ): '''simple docstring''' _a : List[str] = floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]] ) _a : Union[str, Any] = None if self.use_labels: # labels is a list of Dict (each Dict being the labels for a given example in the batch) _a : Dict = [] for i in range(self.batch_size ): _a : Optional[Any] = {} _a : Union[str, Any] = torch.randint( high=self.num_labels ,size=(self.n_targets,) ,device=_a ) _a : str = torch.rand(self.n_targets ,4 ,device=_a ) labels.append(_a ) _a : Union[str, Any] = self.get_config() return config, pixel_values, labels def __lowercase ( self : Any ): '''simple docstring''' return YolosConfig( image_size=self.image_size ,patch_size=self.patch_size ,num_channels=self.num_channels ,hidden_size=self.hidden_size ,num_hidden_layers=self.num_hidden_layers ,num_attention_heads=self.num_attention_heads ,intermediate_size=self.intermediate_size ,hidden_act=self.hidden_act ,hidden_dropout_prob=self.hidden_dropout_prob ,attention_probs_dropout_prob=self.attention_probs_dropout_prob ,is_decoder=_a ,initializer_range=self.initializer_range ,num_detection_tokens=self.num_detection_tokens ,num_labels=self.num_labels ,) def __lowercase ( self : List[str] ,_a : Optional[int] ,_a : Any ,_a : Dict ): '''simple docstring''' _a : List[str] = YolosModel(config=_a ) model.to(_a ) model.eval() _a : Tuple = model(_a ) self.parent.assertEqual( result.last_hidden_state.shape ,(self.batch_size, self.expected_seq_len, self.hidden_size) ) def __lowercase ( self : Optional[Any] ,_a : Union[str, Any] ,_a : str ,_a : Optional[int] ): '''simple docstring''' _a : Tuple = YolosForObjectDetection(_a ) model.to(_a ) model.eval() _a : str = model(pixel_values=_a ) _a : Any = model(_a ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_detection_tokens, self.num_labels + 1) ) self.parent.assertEqual(result.pred_boxes.shape ,(self.batch_size, self.num_detection_tokens, 4) ) _a : Tuple = model(pixel_values=_a ,labels=_a ) self.parent.assertEqual(result.loss.shape ,() ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_detection_tokens, self.num_labels + 1) ) self.parent.assertEqual(result.pred_boxes.shape ,(self.batch_size, self.num_detection_tokens, 4) ) def __lowercase ( self : List[str] ): '''simple docstring''' _a : Optional[Any] = self.prepare_config_and_inputs() _a : Union[str, Any] = config_and_inputs _a : str = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class UpperCAmelCase__ ( lowercase__ , lowercase__ , unittest.TestCase ): """simple docstring""" __UpperCAmelCase : Any = (YolosModel, YolosForObjectDetection) if is_torch_available() else () __UpperCAmelCase : List[Any] = ( {'''feature-extraction''': YolosModel, '''object-detection''': YolosForObjectDetection} if is_torch_available() else {} ) __UpperCAmelCase : Optional[int] = False __UpperCAmelCase : Union[str, Any] = False __UpperCAmelCase : Optional[int] = False __UpperCAmelCase : Dict = False def __lowercase ( self : List[Any] ,_a : Optional[int] ,_a : Tuple ,_a : List[str]=False ): '''simple docstring''' _a : List[Any] = super()._prepare_for_class(_a ,_a ,return_labels=_a ) if return_labels: if model_class.__name__ == "YolosForObjectDetection": _a : Optional[Any] = [] for i in range(self.model_tester.batch_size ): _a : Tuple = {} _a : str = torch.ones( size=(self.model_tester.n_targets,) ,device=_a ,dtype=torch.long ) _a : Any = torch.ones( self.model_tester.n_targets ,4 ,device=_a ,dtype=torch.float ) labels.append(_a ) _a : Any = labels return inputs_dict def __lowercase ( self : List[str] ): '''simple docstring''' _a : Union[str, Any] = YolosModelTester(self ) _a : str = ConfigTester(self ,config_class=_a ,has_text_modality=_a ,hidden_size=37 ) def __lowercase ( self : Any ): '''simple docstring''' self.config_tester.run_common_tests() def __lowercase ( self : Optional[int] ): '''simple docstring''' pass def __lowercase ( self : Any ): '''simple docstring''' _a : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : List[str] = model_class(_a ) self.assertIsInstance(model.get_input_embeddings() ,(nn.Module) ) _a : List[str] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_a ,nn.Linear ) ) def __lowercase ( self : str ): '''simple docstring''' _a : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : str = model_class(_a ) _a : Optional[int] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a : Optional[int] = [*signature.parameters.keys()] _a : Optional[int] = ['pixel_values'] self.assertListEqual(arg_names[:1] ,_a ) def __lowercase ( self : Dict ): '''simple docstring''' _a : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def __lowercase ( self : Optional[int] ): '''simple docstring''' _a : int = self.model_tester.prepare_config_and_inputs_for_common() _a : Tuple = True # in YOLOS, the seq_len is different _a : str = self.model_tester.expected_seq_len for model_class in self.all_model_classes: _a : Union[str, Any] = True _a : List[Any] = False _a : List[str] = True _a : Tuple = model_class(_a ) model.to(_a ) model.eval() with torch.no_grad(): _a : Optional[int] = model(**self._prepare_for_class(_a ,_a ) ) _a : List[Any] = outputs.attentions self.assertEqual(len(_a ) ,self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] _a : Tuple = True _a : Union[str, Any] = model_class(_a ) model.to(_a ) model.eval() with torch.no_grad(): _a : List[Any] = model(**self._prepare_for_class(_a ,_a ) ) _a : int = outputs.attentions self.assertEqual(len(_a ) ,self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) ,[self.model_tester.num_attention_heads, seq_len, seq_len] ,) _a : Any = len(_a ) # Check attention is always last and order is fine _a : int = True _a : List[Any] = True _a : str = model_class(_a ) model.to(_a ) model.eval() with torch.no_grad(): _a : Tuple = model(**self._prepare_for_class(_a ,_a ) ) _a : List[str] = 1 self.assertEqual(out_len + added_hidden_states ,len(_a ) ) _a : Union[str, Any] = outputs.attentions self.assertEqual(len(_a ) ,self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) ,[self.model_tester.num_attention_heads, seq_len, seq_len] ,) def __lowercase ( self : List[Any] ): '''simple docstring''' def check_hidden_states_output(_a : List[Any] ,_a : Optional[int] ,_a : int ): _a : List[str] = model_class(_a ) model.to(_a ) model.eval() with torch.no_grad(): _a : int = model(**self._prepare_for_class(_a ,_a ) ) _a : Any = outputs.hidden_states _a : Union[str, Any] = getattr( self.model_tester ,'expected_num_hidden_layers' ,self.model_tester.num_hidden_layers + 1 ) self.assertEqual(len(_a ) ,_a ) # YOLOS has a different seq_length _a : Union[str, Any] = self.model_tester.expected_seq_len self.assertListEqual( list(hidden_states[0].shape[-2:] ) ,[seq_length, self.model_tester.hidden_size] ,) _a : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : Dict = True check_hidden_states_output(_a ,_a ,_a ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _a : Optional[int] = True check_hidden_states_output(_a ,_a ,_a ) def __lowercase ( self : Tuple ): '''simple docstring''' _a : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_object_detection(*_a ) @slow def __lowercase ( self : Any ): '''simple docstring''' for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a : Union[str, Any] = YolosModel.from_pretrained(_a ) self.assertIsNotNone(_a ) def UpperCAmelCase_ (): """simple docstring""" _a : List[Any] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class UpperCAmelCase__ ( unittest.TestCase ): """simple docstring""" @cached_property def __lowercase ( self : int ): '''simple docstring''' return AutoImageProcessor.from_pretrained('hustvl/yolos-small' ) if is_vision_available() else None @slow def __lowercase ( self : Optional[int] ): '''simple docstring''' _a : List[Any] = YolosForObjectDetection.from_pretrained('hustvl/yolos-small' ).to(_a ) _a : Tuple = self.default_image_processor _a : Optional[int] = prepare_img() _a : Any = image_processor(images=_a ,return_tensors='pt' ).to(_a ) # forward pass with torch.no_grad(): _a : List[str] = model(inputs.pixel_values ) # verify outputs _a : List[Any] = torch.Size((1, 100, 92) ) self.assertEqual(outputs.logits.shape ,_a ) _a : str = torch.tensor( [[-24.0248, -10.3024, -14.8290], [-42.0392, -16.8200, -27.4334], [-27.2743, -11.8154, -18.7148]] ,device=_a ,) _a : Tuple = torch.tensor( [[0.2559, 0.5455, 0.4706], [0.2989, 0.7279, 0.1875], [0.7732, 0.4017, 0.4462]] ,device=_a ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] ,_a ,atol=1E-4 ) ) self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3] ,_a ,atol=1E-4 ) ) # verify postprocessing _a : str = image_processor.post_process_object_detection( _a ,threshold=0.3 ,target_sizes=[image.size[::-1]] )[0] _a : Union[str, Any] = torch.tensor([0.9994, 0.9790, 0.9964, 0.9972, 0.9861] ).to(_a ) _a : Dict = [75, 75, 17, 63, 17] _a : List[str] = torch.tensor([335.0609, 79.3848, 375.4216, 187.2495] ).to(_a ) self.assertEqual(len(results['scores'] ) ,5 ) self.assertTrue(torch.allclose(results['scores'] ,_a ,atol=1E-4 ) ) self.assertSequenceEqual(results['labels'].tolist() ,_a ) self.assertTrue(torch.allclose(results['boxes'][0, :] ,_a ) )
371
'''simple docstring''' import json import os import shutil import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoConfig, BertConfig, GPTaConfig from transformers.configuration_utils import PretrainedConfig from transformers.testing_utils import TOKEN, USER, is_staging_test sys.path.append(str(Path(__file__).parent.parent / """utils""")) from test_module.custom_configuration import CustomConfig # noqa E402 __lowerCAmelCase = { """return_dict""": False, """output_hidden_states""": True, """output_attentions""": True, """torchscript""": True, """torch_dtype""": """float16""", """use_bfloat16""": True, """tf_legacy_loss""": True, """pruned_heads""": {"""a""": 1}, """tie_word_embeddings""": False, """is_decoder""": True, """cross_attention_hidden_size""": 1_2_8, """add_cross_attention""": True, """tie_encoder_decoder""": True, """max_length""": 5_0, """min_length""": 3, """do_sample""": True, """early_stopping""": True, """num_beams""": 3, """num_beam_groups""": 3, """diversity_penalty""": 0.5, """temperature""": 2.0, """top_k""": 1_0, """top_p""": 0.7, """typical_p""": 0.2, """repetition_penalty""": 0.8, """length_penalty""": 0.8, """no_repeat_ngram_size""": 5, """encoder_no_repeat_ngram_size""": 5, """bad_words_ids""": [1, 2, 3], """num_return_sequences""": 3, """chunk_size_feed_forward""": 5, """output_scores""": True, """return_dict_in_generate""": True, """forced_bos_token_id""": 2, """forced_eos_token_id""": 3, """remove_invalid_values""": True, """architectures""": ["""BertModel"""], """finetuning_task""": """translation""", """id2label""": {0: """label"""}, """label2id""": {"""label""": """0"""}, """tokenizer_class""": """BertTokenizerFast""", """prefix""": """prefix""", """bos_token_id""": 6, """pad_token_id""": 7, """eos_token_id""": 8, """sep_token_id""": 9, """decoder_start_token_id""": 1_0, """exponential_decay_length_penalty""": (5, 1.01), """suppress_tokens""": [0, 1], """begin_suppress_tokens""": 2, """task_specific_params""": {"""translation""": """some_params"""}, """problem_type""": """regression""", } @is_staging_test class UpperCAmelCase__ ( unittest.TestCase ): """simple docstring""" @classmethod def __lowercase ( cls : Optional[Any] ): '''simple docstring''' _a : List[Any] = TOKEN HfFolder.save_token(_a ) @classmethod def __lowercase ( cls : List[Any] ): '''simple docstring''' try: delete_repo(token=cls._token ,repo_id='test-config' ) except HTTPError: pass try: delete_repo(token=cls._token ,repo_id='valid_org/test-config-org' ) except HTTPError: pass try: delete_repo(token=cls._token ,repo_id='test-dynamic-config' ) except HTTPError: pass def __lowercase ( self : List[str] ): '''simple docstring''' _a : Any = BertConfig( vocab_size=99 ,hidden_size=32 ,num_hidden_layers=5 ,num_attention_heads=4 ,intermediate_size=37 ) config.push_to_hub('test-config' ,use_auth_token=self._token ) _a : Optional[Any] = BertConfig.from_pretrained(F"""{USER}/test-config""" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(_a ,getattr(_a ,_a ) ) # Reset repo delete_repo(token=self._token ,repo_id='test-config' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(_a ,repo_id='test-config' ,push_to_hub=_a ,use_auth_token=self._token ) _a : Dict = BertConfig.from_pretrained(F"""{USER}/test-config""" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(_a ,getattr(_a ,_a ) ) def __lowercase ( self : Optional[Any] ): '''simple docstring''' _a : Tuple = BertConfig( vocab_size=99 ,hidden_size=32 ,num_hidden_layers=5 ,num_attention_heads=4 ,intermediate_size=37 ) config.push_to_hub('valid_org/test-config-org' ,use_auth_token=self._token ) _a : Union[str, Any] = BertConfig.from_pretrained('valid_org/test-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(_a ,getattr(_a ,_a ) ) # Reset repo delete_repo(token=self._token ,repo_id='valid_org/test-config-org' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( _a ,repo_id='valid_org/test-config-org' ,push_to_hub=_a ,use_auth_token=self._token ) _a : Tuple = BertConfig.from_pretrained('valid_org/test-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(_a ,getattr(_a ,_a ) ) def __lowercase ( self : List[Any] ): '''simple docstring''' CustomConfig.register_for_auto_class() _a : Optional[Any] = CustomConfig(attribute=42 ) config.push_to_hub('test-dynamic-config' ,use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual(config.auto_map ,{'AutoConfig': 'custom_configuration.CustomConfig'} ) _a : int = AutoConfig.from_pretrained(F"""{USER}/test-dynamic-config""" ,trust_remote_code=_a ) # Can't make an isinstance check because the new_config is from the FakeConfig class of a dynamic module self.assertEqual(new_config.__class__.__name__ ,'CustomConfig' ) self.assertEqual(new_config.attribute ,42 ) class UpperCAmelCase__ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self : Union[str, Any] ): '''simple docstring''' _a : Optional[Any] = GPTaConfig() # attempt to modify each of int/float/bool/str config records and verify they were updated _a : int = c.n_embd + 1 # int _a : str = c.resid_pdrop + 1.0 # float _a : Dict = not c.scale_attn_weights # bool _a : List[Any] = c.summary_type + 'foo' # str c.update_from_string( F"""n_embd={n_embd},resid_pdrop={resid_pdrop},scale_attn_weights={scale_attn_weights},summary_type={summary_type}""" ) self.assertEqual(_a ,c.n_embd ,'mismatch for key: n_embd' ) self.assertEqual(_a ,c.resid_pdrop ,'mismatch for key: resid_pdrop' ) self.assertEqual(_a ,c.scale_attn_weights ,'mismatch for key: scale_attn_weights' ) self.assertEqual(_a ,c.summary_type ,'mismatch for key: summary_type' ) def __lowercase ( self : List[str] ): '''simple docstring''' _a : int = PretrainedConfig() _a : int = [key for key in base_config.__dict__ if key not in config_common_kwargs] # If this part of the test fails, you have arguments to addin config_common_kwargs above. self.assertListEqual( _a ,['is_encoder_decoder', '_name_or_path', '_commit_hash', 'transformers_version'] ) _a : Dict = [key for key, value in config_common_kwargs.items() if value == getattr(_a ,_a )] if len(_a ) > 0: raise ValueError( 'The following keys are set with the default values in' ' `test_configuration_common.config_common_kwargs` pick another value for them:' F""" {', '.join(_a )}.""" ) def __lowercase ( self : Optional[Any] ): '''simple docstring''' with self.assertRaises(_a ): # config is in subfolder, the following should not work without specifying the subfolder _a : List[Any] = BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert-subfolder' ) _a : List[str] = BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert-subfolder' ,subfolder='bert' ) self.assertIsNotNone(_a ) def __lowercase ( self : List[Any] ): '''simple docstring''' _a : List[Any] = mock.Mock() _a : Any = 500 _a : Any = {} _a : Any = HTTPError _a : List[Any] = {} # Download this model to make sure it's in the cache. _a : Any = BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert' ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch('requests.Session.request' ,return_value=_a ) as mock_head: _a : Optional[int] = BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert' ) # This check we did call the fake head request mock_head.assert_called() def __lowercase ( self : Optional[Any] ): '''simple docstring''' _a : Optional[int] = BertConfig.from_pretrained( 'https://huggingface.co/hf-internal-testing/tiny-random-bert/resolve/main/config.json' ) def __lowercase ( self : Union[str, Any] ): '''simple docstring''' _a : int = AutoConfig.from_pretrained('bert-base-cased' ) _a : List[str] = ['config.4.0.0.json'] with tempfile.TemporaryDirectory() as tmp_dir: configuration.save_pretrained(_a ) _a : str = 2 json.dump(configuration.to_dict() ,open(os.path.join(_a ,'config.4.0.0.json' ) ,'w' ) ) # This should pick the new configuration file as the version of Transformers is > 4.0.0 _a : int = AutoConfig.from_pretrained(_a ) self.assertEqual(new_configuration.hidden_size ,2 ) # Will need to be adjusted if we reach v42 and this test is still here. # Should pick the old configuration file as the version of Transformers is < 4.42.0 _a : Tuple = ['config.42.0.0.json'] _a : int = 768 configuration.save_pretrained(_a ) shutil.move(os.path.join(_a ,'config.4.0.0.json' ) ,os.path.join(_a ,'config.42.0.0.json' ) ) _a : int = AutoConfig.from_pretrained(_a ) self.assertEqual(new_configuration.hidden_size ,768 ) def __lowercase ( self : str ): '''simple docstring''' _a : Tuple = 'hf-internal-testing/test-two-configs' import transformers as new_transformers _a : Optional[int] = 'v4.0.0' _a, _a : Tuple = new_transformers.models.auto.AutoConfig.from_pretrained( _a ,return_unused_kwargs=_a ) self.assertEqual(new_configuration.hidden_size ,2 ) # This checks `_configuration_file` ia not kept in the kwargs by mistake. self.assertDictEqual(_a ,{} ) # Testing an older version by monkey-patching the version in the module it's used. import transformers as old_transformers _a : str = 'v3.0.0' _a : Optional[Any] = old_transformers.models.auto.AutoConfig.from_pretrained(_a ) self.assertEqual(old_configuration.hidden_size ,768 )
5
0
'''simple docstring''' from math import log from scipy.constants import Boltzmann, physical_constants a_ : str = 3_00 # TEMPERATURE (unit = K) def a_ ( __snake_case : float , __snake_case : float , __snake_case : float , ) -> float: """simple docstring""" if donor_conc <= 0: raise ValueError('''Donor concentration should be positive''' ) elif acceptor_conc <= 0: raise ValueError('''Acceptor concentration should be positive''' ) elif intrinsic_conc <= 0: raise ValueError('''Intrinsic concentration should be positive''' ) elif donor_conc <= intrinsic_conc: raise ValueError( '''Donor concentration should be greater than intrinsic concentration''' ) elif acceptor_conc <= intrinsic_conc: raise ValueError( '''Acceptor concentration should be greater than intrinsic concentration''' ) else: return ( Boltzmann * T * log((donor_conc * acceptor_conc) / intrinsic_conc**2 ) / physical_constants["electron volt"][0] ) if __name__ == "__main__": import doctest doctest.testmod()
6
'''simple docstring''' import datasets from .evaluate import evaluate a_ : List[Any] = """\ @article{hendrycks2021cuad, title={CUAD: An Expert-Annotated NLP Dataset for Legal Contract Review}, author={Dan Hendrycks and Collin Burns and Anya Chen and Spencer Ball}, journal={arXiv preprint arXiv:2103.06268}, year={2021} } """ a_ : List[Any] = """ This metric wrap the official scoring script for version 1 of the Contract Understanding Atticus Dataset (CUAD). Contract Understanding Atticus Dataset (CUAD) v1 is a corpus of more than 13,000 labels in 510 commercial legal contracts that have been manually labeled to identify 41 categories of important clauses that lawyers look for when reviewing contracts in connection with corporate transactions. """ a_ : Any = """ Computes CUAD scores (EM, F1, AUPR, Precision@80%Recall, and Precision@90%Recall). Args: predictions: List of question-answers dictionaries with the following key-values: - 'id': id of the question-answer pair as given in the references (see below) - 'prediction_text': list of possible texts for the answer, as a list of strings depending on a threshold on the confidence probability of each prediction. references: List of question-answers dictionaries with the following key-values: - 'id': id of the question-answer pair (see above), - 'answers': a Dict in the CUAD dataset format { 'text': list of possible texts for the answer, as a list of strings 'answer_start': list of start positions for the answer, as a list of ints } Note that answer_start values are not taken into account to compute the metric. Returns: 'exact_match': Exact match (the normalized answer exactly match the gold answer) 'f1': The F-score of predicted tokens versus the gold answer 'aupr': Area Under the Precision-Recall curve 'prec_at_80_recall': Precision at 80% recall 'prec_at_90_recall': Precision at 90% recall Examples: >>> predictions = [{'prediction_text': ['The seller:', 'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.'], 'id': 'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties'}] >>> references = [{'answers': {'answer_start': [143, 49], 'text': ['The seller:', 'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.']}, 'id': 'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties'}] >>> cuad_metric = datasets.load_metric(\"cuad\") >>> results = cuad_metric.compute(predictions=predictions, references=references) >>> print(results) {'exact_match': 100.0, 'f1': 100.0, 'aupr': 0.0, 'prec_at_80_recall': 1.0, 'prec_at_90_recall': 1.0} """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __UpperCamelCase ( datasets.Metric ): def lowercase__ ( self ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION, citation=_CITATION, inputs_description=_KWARGS_DESCRIPTION, features=datasets.Features( { '''predictions''': { '''id''': datasets.Value('''string''' ), '''prediction_text''': datasets.features.Sequence(datasets.Value('''string''' ) ), }, '''references''': { '''id''': datasets.Value('''string''' ), '''answers''': datasets.features.Sequence( { '''text''': datasets.Value('''string''' ), '''answer_start''': datasets.Value('''int32''' ), } ), }, } ), codebase_urls=['''https://www.atticusprojectai.org/cuad'''], reference_urls=['''https://www.atticusprojectai.org/cuad'''], ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ ={prediction['''id''']: prediction['''prediction_text'''] for prediction in predictions} lowerCamelCase_ =[ { '''paragraphs''': [ { '''qas''': [ { '''answers''': [{'''text''': answer_text} for answer_text in ref['''answers''']['''text''']], '''id''': ref['''id'''], } for ref in references ] } ] } ] lowerCamelCase_ =evaluate(dataset=lowerCAmelCase, predictions=lowerCAmelCase ) return score
6
1
'''simple docstring''' from math import sqrt def a_ ( __snake_case : int ) -> bool: """simple docstring""" if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(sqrt(__snake_case ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def a_ ( __snake_case : int = 1_0001 ) -> int: """simple docstring""" lowerCamelCase_ =0 lowerCamelCase_ =1 while count != nth and number < 3: number += 1 if is_prime(__snake_case ): count += 1 while count != nth: number += 2 if is_prime(__snake_case ): count += 1 return number if __name__ == "__main__": print(F"""{solution() = }""")
6
'''simple docstring''' import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert_fast import BertTokenizerFast from .tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer, DPRReaderTokenizer a_ : Tuple = logging.get_logger(__name__) a_ : int = {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""} a_ : Tuple = { """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""" ), }, } a_ : Union[str, 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""" ), }, } a_ : str = { """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""" ), }, } a_ : int = { """facebook/dpr-ctx_encoder-single-nq-base""": 5_12, """facebook/dpr-ctx_encoder-multiset-base""": 5_12, } a_ : List[Any] = { """facebook/dpr-question_encoder-single-nq-base""": 5_12, """facebook/dpr-question_encoder-multiset-base""": 5_12, } a_ : Optional[Any] = { """facebook/dpr-reader-single-nq-base""": 5_12, """facebook/dpr-reader-multiset-base""": 5_12, } a_ : Optional[int] = { """facebook/dpr-ctx_encoder-single-nq-base""": {"""do_lower_case""": True}, """facebook/dpr-ctx_encoder-multiset-base""": {"""do_lower_case""": True}, } a_ : List[str] = { """facebook/dpr-question_encoder-single-nq-base""": {"""do_lower_case""": True}, """facebook/dpr-question_encoder-multiset-base""": {"""do_lower_case""": True}, } a_ : Dict = { """facebook/dpr-reader-single-nq-base""": {"""do_lower_case""": True}, """facebook/dpr-reader-multiset-base""": {"""do_lower_case""": True}, } class __UpperCamelCase ( lowerCamelCase__ ): lowercase : List[Any] =VOCAB_FILES_NAMES lowercase : Any =CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP lowercase : Optional[Any] =CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase : List[str] =CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION lowercase : Dict =DPRContextEncoderTokenizer class __UpperCamelCase ( lowerCamelCase__ ): lowercase : Optional[int] =VOCAB_FILES_NAMES lowercase : List[Any] =QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP lowercase : Optional[Any] =QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase : List[Any] =QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION lowercase : List[Any] =DPRQuestionEncoderTokenizer a_ : Union[str, Any] = collections.namedtuple( """DPRSpanPrediction""", ["""span_score""", """relevance_score""", """doc_id""", """start_index""", """end_index""", """text"""] ) a_ : Dict = collections.namedtuple("""DPRReaderOutput""", ["""start_logits""", """end_logits""", """relevance_logits"""]) a_ : Dict = 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 __UpperCamelCase : def __call__( self, lowerCAmelCase, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = False, lowerCAmelCase = False, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, **lowerCAmelCase, ): """simple docstring""" if titles is None and texts is None: return super().__call__( lowerCAmelCase, padding=lowerCAmelCase, truncation=lowerCAmelCase, max_length=lowerCAmelCase, return_tensors=lowerCAmelCase, return_attention_mask=lowerCAmelCase, **lowerCAmelCase, ) elif titles is None or texts is None: lowerCamelCase_ =titles if texts is None else texts return super().__call__( lowerCAmelCase, lowerCAmelCase, padding=lowerCAmelCase, truncation=lowerCAmelCase, max_length=lowerCAmelCase, return_tensors=lowerCAmelCase, return_attention_mask=lowerCAmelCase, **lowerCAmelCase, ) lowerCamelCase_ =titles if not isinstance(lowerCAmelCase, lowerCAmelCase ) else [titles] lowerCamelCase_ =texts if not isinstance(lowerCAmelCase, lowerCAmelCase ) else [texts] lowerCamelCase_ =len(lowerCAmelCase ) lowerCamelCase_ =questions if not isinstance(lowerCAmelCase, lowerCAmelCase ) else [questions] * n_passages assert len(lowerCAmelCase ) == len( lowerCAmelCase ), f'''There should be as many titles than texts but got {len(lowerCAmelCase )} titles and {len(lowerCAmelCase )} texts.''' lowerCamelCase_ =super().__call__(lowerCAmelCase, lowerCAmelCase, padding=lowerCAmelCase, truncation=lowerCAmelCase )['''input_ids'''] lowerCamelCase_ =super().__call__(lowerCAmelCase, add_special_tokens=lowerCAmelCase, padding=lowerCAmelCase, truncation=lowerCAmelCase )['''input_ids'''] lowerCamelCase_ ={ '''input_ids''': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(lowerCAmelCase, lowerCAmelCase ) ] } if return_attention_mask is not False: lowerCamelCase_ =[] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) lowerCamelCase_ =attention_mask return self.pad(lowerCAmelCase, padding=lowerCAmelCase, max_length=lowerCAmelCase, return_tensors=lowerCAmelCase ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase = 16, lowerCAmelCase = 64, lowerCAmelCase = 4, ): """simple docstring""" lowerCamelCase_ =reader_input['''input_ids'''] lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ =reader_output[:3] lowerCamelCase_ =len(lowerCAmelCase ) lowerCamelCase_ =sorted(range(lowerCAmelCase ), reverse=lowerCAmelCase, key=relevance_logits.__getitem__ ) lowerCamelCase_ =[] for doc_id in sorted_docs: lowerCamelCase_ =list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence lowerCamelCase_ =sequence_ids.index(self.sep_token_id, 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: lowerCamelCase_ =sequence_ids.index(self.pad_token_id ) else: lowerCamelCase_ =len(lowerCAmelCase ) lowerCamelCase_ =self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len], end_logits=end_logits[doc_id][passage_offset:sequence_len], max_answer_length=lowerCAmelCase, top_spans=lowerCAmelCase, ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index], relevance_score=relevance_logits[doc_id], doc_id=lowerCAmelCase, start_index=lowerCAmelCase, end_index=lowerCAmelCase, text=self.decode(sequence_ids[start_index : end_index + 1] ), ) ) if len(lowerCAmelCase ) >= num_spans: break return nbest_spans_predictions[:num_spans] def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, ): """simple docstring""" lowerCamelCase_ =[] for start_index, start_score in enumerate(lowerCAmelCase ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) lowerCamelCase_ =sorted(lowerCAmelCase, key=lambda lowerCAmelCase : x[1], reverse=lowerCAmelCase ) lowerCamelCase_ =[] for (start_index, end_index), score in scores: assert start_index <= end_index, f'''Wrong span indices: [{start_index}:{end_index}]''' lowerCamelCase_ =end_index - start_index + 1 assert length <= max_answer_length, f'''Span is too long: {length} > {max_answer_length}''' if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(lowerCAmelCase ) == top_spans: break return chosen_span_intervals @add_end_docstrings(lowerCamelCase__ ) class __UpperCamelCase ( lowerCamelCase__ , lowerCamelCase__ ): lowercase : int =VOCAB_FILES_NAMES lowercase : Tuple =READER_PRETRAINED_VOCAB_FILES_MAP lowercase : Tuple =READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase : List[str] =READER_PRETRAINED_INIT_CONFIGURATION lowercase : int =['input_ids', 'attention_mask'] lowercase : Dict =DPRReaderTokenizer
6
1
'''simple docstring''' import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers from ...tokenization_utils_base import BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation a_ : List[str] = logging.get_logger(__name__) a_ : str = {"""tokenizer_file""": """tokenizer.json"""} a_ : Dict = { """tokenizer_file""": { """bigscience/tokenizer""": """https://huggingface.co/bigscience/tokenizer/blob/main/tokenizer.json""", """bigscience/bloom-560m""": """https://huggingface.co/bigscience/bloom-560m/blob/main/tokenizer.json""", """bigscience/bloom-1b1""": """https://huggingface.co/bigscience/bloom-1b1/blob/main/tokenizer.json""", """bigscience/bloom-1b7""": """https://huggingface.co/bigscience/bloom-1b7/blob/main/tokenizer.json""", """bigscience/bloom-3b""": """https://huggingface.co/bigscience/bloom-3b/blob/main/tokenizer.json""", """bigscience/bloom-7b1""": """https://huggingface.co/bigscience/bloom-7b1/blob/main/tokenizer.json""", """bigscience/bloom""": """https://huggingface.co/bigscience/bloom/blob/main/tokenizer.json""", }, } class __UpperCamelCase ( lowerCamelCase__ ): lowercase : Optional[Any] =VOCAB_FILES_NAMES lowercase : Tuple =PRETRAINED_VOCAB_FILES_MAP lowercase : List[str] =['input_ids', 'attention_mask'] lowercase : List[str] =None def __init__( self, lowerCAmelCase=None, lowerCAmelCase=None, lowerCAmelCase=None, lowerCAmelCase="<unk>", lowerCAmelCase="<s>", lowerCAmelCase="</s>", lowerCAmelCase="<pad>", lowerCAmelCase=False, lowerCAmelCase=False, **lowerCAmelCase, ): """simple docstring""" super().__init__( lowerCAmelCase, lowerCAmelCase, tokenizer_file=lowerCAmelCase, unk_token=lowerCAmelCase, bos_token=lowerCAmelCase, eos_token=lowerCAmelCase, pad_token=lowerCAmelCase, add_prefix_space=lowerCAmelCase, clean_up_tokenization_spaces=lowerCAmelCase, **lowerCAmelCase, ) lowerCamelCase_ =json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''', lowerCAmelCase ) != add_prefix_space: lowerCamelCase_ =getattr(lowerCAmelCase, pre_tok_state.pop('''type''' ) ) lowerCamelCase_ =add_prefix_space lowerCamelCase_ =pre_tok_class(**lowerCAmelCase ) lowerCamelCase_ =add_prefix_space def lowercase__ ( self, *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =kwargs.get('''is_split_into_words''', lowerCAmelCase ) if not (self.add_prefix_space or not is_split_into_words): raise Exception( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True to use it with''' ''' pretokenized inputs.''' ) return super()._batch_encode_plus(*lowerCAmelCase, **lowerCAmelCase ) def lowercase__ ( self, *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =kwargs.get('''is_split_into_words''', lowerCAmelCase ) if not (self.add_prefix_space or not is_split_into_words): raise Exception( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True to use it with''' ''' pretokenized inputs.''' ) return super()._encode_plus(*lowerCAmelCase, **lowerCAmelCase ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase = None ): """simple docstring""" lowerCamelCase_ =self._tokenizer.model.save(lowerCAmelCase, name=lowerCAmelCase ) return tuple(lowerCAmelCase ) def lowercase__ ( self, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =[] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(lowerCAmelCase, add_special_tokens=lowerCAmelCase ) + [self.eos_token_id] ) if len(lowerCAmelCase ) > self.model_max_length: lowerCamelCase_ =input_ids[-self.model_max_length :] return input_ids
6
'''simple docstring''' from unittest import TestCase from datasets import Dataset from minhash_deduplication import deduplicate_dataset, make_duplicate_clusters def a_ ( ) -> Tuple: """simple docstring""" lowerCamelCase_ ={ '''repo_name''': ['''test_repo1''', '''test_repo2''', '''test_repo3'''], '''path''': ['''test_1.py''', '''test_2.py''', '''unit_test.py'''], '''content''': ['''a ''' * 20, '''a ''' * 30, '''b ''' * 7], } lowerCamelCase_ =Dataset.from_dict(__snake_case ) return dataset class __UpperCamelCase ( lowerCamelCase__ ): def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =get_dataset() lowerCamelCase_ =make_duplicate_clusters(lowerCAmelCase, 0.8_5 ) self.assertEqual(len(duplicate_clusters[0] ), 2 ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =get_dataset() lowerCamelCase_, lowerCamelCase_ =deduplicate_dataset(lowerCAmelCase ) self.assertEqual(len(lowerCAmelCase ), 2 ) print(lowerCAmelCase ) self.assertEqual(duplicate_clusters[0][0]['''copies'''], 2 ) self.assertEqual(duplicate_clusters[0][0]['''is_extreme'''], lowerCAmelCase )
6
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging a_ : List[str] = logging.get_logger(__name__) a_ : int = { """RWKV/rwkv-4-169m-pile""": """https://huggingface.co/RWKV/rwkv-4-169m-pile/resolve/main/config.json""", """RWKV/rwkv-4-430m-pile""": """https://huggingface.co/RWKV/rwkv-4-430m-pile/resolve/main/config.json""", """RWKV/rwkv-4-1b5-pile""": """https://huggingface.co/RWKV/rwkv-4-1b5-pile/resolve/main/config.json""", """RWKV/rwkv-4-3b-pile""": """https://huggingface.co/RWKV/rwkv-4-3b-pile/resolve/main/config.json""", """RWKV/rwkv-4-7b-pile""": """https://huggingface.co/RWKV/rwkv-4-7b-pile/resolve/main/config.json""", """RWKV/rwkv-4-14b-pile""": """https://huggingface.co/RWKV/rwkv-4-14b-pile/resolve/main/config.json""", """RWKV/rwkv-raven-1b5""": """https://huggingface.co/RWKV/rwkv-raven-1b5/resolve/main/config.json""", """RWKV/rwkv-raven-3b""": """https://huggingface.co/RWKV/rwkv-raven-3b/resolve/main/config.json""", """RWKV/rwkv-raven-7b""": """https://huggingface.co/RWKV/rwkv-raven-7b/resolve/main/config.json""", """RWKV/rwkv-raven-14b""": """https://huggingface.co/RWKV/rwkv-raven-14b/resolve/main/config.json""", } class __UpperCamelCase ( lowerCamelCase__ ): lowercase : Union[str, Any] ='rwkv' lowercase : Dict ={'max_position_embeddings': 'context_length'} def __init__( self, lowerCAmelCase=50_277, lowerCAmelCase=1_024, lowerCAmelCase=4_096, lowerCAmelCase=32, lowerCAmelCase=None, lowerCAmelCase=None, lowerCAmelCase=1e-5, lowerCAmelCase=0, lowerCAmelCase=0, lowerCAmelCase=6, lowerCAmelCase=False, lowerCAmelCase=True, **lowerCAmelCase, ): """simple docstring""" lowerCamelCase_ =vocab_size lowerCamelCase_ =context_length lowerCamelCase_ =hidden_size lowerCamelCase_ =num_hidden_layers lowerCamelCase_ =attention_hidden_size if attention_hidden_size is not None else hidden_size lowerCamelCase_ =intermediate_size if intermediate_size is not None else 4 * hidden_size lowerCamelCase_ =layer_norm_epsilon lowerCamelCase_ =rescale_every lowerCamelCase_ =use_cache lowerCamelCase_ =bos_token_id lowerCamelCase_ =eos_token_id super().__init__( tie_word_embeddings=lowerCAmelCase, bos_token_id=lowerCAmelCase, eos_token_id=lowerCAmelCase, **lowerCAmelCase )
6
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_torch_available, ) a_ : Any = { """configuration_trocr""": ["""TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP""", """TrOCRConfig"""], """processing_trocr""": ["""TrOCRProcessor"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ : Optional[int] = [ """TROCR_PRETRAINED_MODEL_ARCHIVE_LIST""", """TrOCRForCausalLM""", """TrOCRPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_trocr import TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP, TrOCRConfig from .processing_trocr import TrOCRProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_trocr import TROCR_PRETRAINED_MODEL_ARCHIVE_LIST, TrOCRForCausalLM, TrOCRPreTrainedModel else: import sys a_ : int = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
6
1
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, StableDiffusionAttendAndExcitePipeline, UNetaDConditionModel, ) from diffusers.utils import load_numpy, skip_mps, slow from diffusers.utils.testing_utils import require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin a_ : Tuple = False @skip_mps class __UpperCamelCase ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): lowercase : Union[str, Any] =StableDiffusionAttendAndExcitePipeline lowercase : Tuple =False lowercase : Optional[Any] =TEXT_TO_IMAGE_PARAMS lowercase : Dict =TEXT_TO_IMAGE_BATCH_PARAMS.union({'token_indices'} ) lowercase : Optional[Any] =TEXT_TO_IMAGE_IMAGE_PARAMS lowercase : Optional[Any] =TEXT_TO_IMAGE_IMAGE_PARAMS @classmethod def lowercase__ ( cls ): """simple docstring""" super().setUpClass() torch.use_deterministic_algorithms(lowerCAmelCase ) @classmethod def lowercase__ ( cls ): """simple docstring""" super().tearDownClass() torch.use_deterministic_algorithms(lowerCAmelCase ) def lowercase__ ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCamelCase_ =UNetaDConditionModel( block_out_channels=(32, 64), layers_per_block=1, sample_size=32, in_channels=4, out_channels=4, down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D'''), up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D'''), cross_attention_dim=32, attention_head_dim=(2, 4), use_linear_projection=lowerCAmelCase, ) lowerCamelCase_ =DDIMScheduler( beta_start=0.0_0_0_8_5, beta_end=0.0_1_2, beta_schedule='''scaled_linear''', clip_sample=lowerCAmelCase, set_alpha_to_one=lowerCAmelCase, ) torch.manual_seed(0 ) lowerCamelCase_ =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, sample_size=128, ) torch.manual_seed(0 ) lowerCamelCase_ =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=1_000, hidden_act='''gelu''', projection_dim=512, ) lowerCamelCase_ =CLIPTextModel(lowerCAmelCase ) lowerCamelCase_ =CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) lowerCamelCase_ ={ '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase=0 ): """simple docstring""" if str(lowerCAmelCase ).startswith('''mps''' ): lowerCamelCase_ =torch.manual_seed(lowerCAmelCase ) else: lowerCamelCase_ =torch.Generator(device=lowerCAmelCase ).manual_seed(lowerCAmelCase ) lowerCamelCase_ =lowerCamelCase_ ={ '''prompt''': '''a cat and a frog''', '''token_indices''': [2, 5], '''generator''': generator, '''num_inference_steps''': 1, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', '''max_iter_to_alter''': 2, '''thresholds''': {0: 0.7}, } return inputs def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ ='''cpu''' lowerCamelCase_ =self.get_dummy_components() lowerCamelCase_ =self.pipeline_class(**lowerCAmelCase ) pipe.to(lowerCAmelCase ) pipe.set_progress_bar_config(disable=lowerCAmelCase ) lowerCamelCase_ =self.get_dummy_inputs(lowerCAmelCase ) lowerCamelCase_ =pipe(**lowerCAmelCase ).images lowerCamelCase_ =image[0, -3:, -3:, -1] self.assertEqual(image.shape, (1, 64, 64, 3) ) lowerCamelCase_ =np.array( [0.6_3_9_0_5_3_6_4, 0.6_2_8_9_7_3_0_7, 0.4_8_5_9_9_0_1_7, 0.5_1_3_3_6_2_4, 0.5_5_5_0_0_4_8, 0.4_5_7_6_9_5_1_6, 0.5_0_3_2_6_9_7_3, 0.5_0_2_3_1_3_9, 0.4_5_3_8_4_4_9_6] ) lowerCamelCase_ =np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(lowerCAmelCase, 1e-3 ) def lowercase__ ( self ): """simple docstring""" super().test_cpu_offload_forward_pass(expected_max_diff=5e-4 ) def lowercase__ ( self ): """simple docstring""" self._test_inference_batch_consistent(batch_sizes=[1, 2] ) def lowercase__ ( self ): """simple docstring""" self._test_inference_batch_single_identical(batch_size=2, expected_max_diff=7e-4 ) def lowercase__ ( self ): """simple docstring""" super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3 ) def lowercase__ ( self ): """simple docstring""" super().test_pt_np_pil_outputs_equivalent(expected_max_diff=5e-4 ) def lowercase__ ( self ): """simple docstring""" super().test_save_load_local(expected_max_difference=5e-4 ) def lowercase__ ( self ): """simple docstring""" super().test_save_load_optional_components(expected_max_difference=4e-4 ) @require_torch_gpu @slow class __UpperCamelCase ( unittest.TestCase ): @classmethod def lowercase__ ( cls ): """simple docstring""" super().setUpClass() torch.use_deterministic_algorithms(lowerCAmelCase ) @classmethod def lowercase__ ( cls ): """simple docstring""" super().tearDownClass() torch.use_deterministic_algorithms(lowerCAmelCase ) def lowercase__ ( self ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =torch.manual_seed(51 ) lowerCamelCase_ =StableDiffusionAttendAndExcitePipeline.from_pretrained( '''CompVis/stable-diffusion-v1-4''', safety_checker=lowerCAmelCase, torch_dtype=torch.floataa ) pipe.to('''cuda''' ) lowerCamelCase_ ='''a painting of an elephant with glasses''' lowerCamelCase_ =[5, 7] lowerCamelCase_ =pipe( prompt=lowerCAmelCase, token_indices=lowerCAmelCase, guidance_scale=7.5, generator=lowerCAmelCase, num_inference_steps=5, max_iter_to_alter=5, output_type='''numpy''', ).images[0] lowerCamelCase_ =load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/attend-and-excite/elephant_glasses.npy''' ) assert np.abs((expected_image - image).max() ) < 5e-1
6
'''simple docstring''' from collections import defaultdict from math import gcd def a_ ( __snake_case : int = 150_0000 ) -> int: """simple docstring""" lowerCamelCase_ =defaultdict(__snake_case ) lowerCamelCase_ =2 while 2 * euclid_m * (euclid_m + 1) <= limit: for euclid_n in range((euclid_m % 2) + 1 , __snake_case , 2 ): if gcd(__snake_case , __snake_case ) > 1: continue lowerCamelCase_ =2 * euclid_m * (euclid_m + euclid_n) for perimeter in range(__snake_case , limit + 1 , __snake_case ): frequencies[perimeter] += 1 euclid_m += 1 return sum(1 for frequency in frequencies.values() if frequency == 1 ) if __name__ == "__main__": print(F"""{solution() = }""")
6
1
'''simple docstring''' import math import flax.linen as nn import jax.numpy as jnp def a_ ( __snake_case : jnp.ndarray , __snake_case : int , __snake_case : float = 1 , __snake_case : float = 1 , __snake_case : float = 1.0e4 , __snake_case : bool = False , __snake_case : float = 1.0 , ) -> jnp.ndarray: """simple docstring""" assert timesteps.ndim == 1, "Timesteps should be a 1d-array" assert embedding_dim % 2 == 0, F'''Embedding dimension {embedding_dim} should be even''' lowerCamelCase_ =float(embedding_dim // 2 ) lowerCamelCase_ =math.log(max_timescale / min_timescale ) / (num_timescales - freq_shift) lowerCamelCase_ =min_timescale * jnp.exp(jnp.arange(__snake_case , dtype=jnp.floataa ) * -log_timescale_increment ) lowerCamelCase_ =jnp.expand_dims(__snake_case , 1 ) * jnp.expand_dims(__snake_case , 0 ) # scale embeddings lowerCamelCase_ =scale * emb if flip_sin_to_cos: lowerCamelCase_ =jnp.concatenate([jnp.cos(__snake_case ), jnp.sin(__snake_case )] , axis=1 ) else: lowerCamelCase_ =jnp.concatenate([jnp.sin(__snake_case ), jnp.cos(__snake_case )] , axis=1 ) lowerCamelCase_ =jnp.reshape(__snake_case , [jnp.shape(__snake_case )[0], embedding_dim] ) return signal class __UpperCamelCase ( nn.Module ): lowercase : int =32 lowercase : jnp.dtype =jnp.floataa @nn.compact def __call__( self, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =nn.Dense(self.time_embed_dim, dtype=self.dtype, name='''linear_1''' )(lowerCAmelCase ) lowerCamelCase_ =nn.silu(lowerCAmelCase ) lowerCamelCase_ =nn.Dense(self.time_embed_dim, dtype=self.dtype, name='''linear_2''' )(lowerCAmelCase ) return temb class __UpperCamelCase ( nn.Module ): lowercase : int =32 lowercase : bool =False lowercase : float =1 @nn.compact def __call__( self, lowerCAmelCase ): """simple docstring""" return get_sinusoidal_embeddings( lowerCAmelCase, embedding_dim=self.dim, flip_sin_to_cos=self.flip_sin_to_cos, freq_shift=self.freq_shift )
6
'''simple docstring''' import argparse import os # New Code # 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 import find_executable_batch_size ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to ensure out-of-memory errors never # interrupt training, 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) # # 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 # ######################################################################## a_ : Tuple = 16 a_ : Optional[int] = 32 def a_ ( __snake_case : Accelerator , __snake_case : int = 16 ) -> str: """simple docstring""" lowerCamelCase_ =AutoTokenizer.from_pretrained('''bert-base-cased''' ) lowerCamelCase_ =load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(__snake_case : int ): # max_length=None => use the model max length (it's actually the default) lowerCamelCase_ =tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=__snake_case , max_length=__snake_case ) 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(): lowerCamelCase_ =datasets.map( __snake_case , batched=__snake_case , 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 lowerCamelCase_ =tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(__snake_case : Any ): # On TPU it's best to pad everything to the same length or training will be very slow. lowerCamelCase_ =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": lowerCamelCase_ =16 elif accelerator.mixed_precision != "no": lowerCamelCase_ =8 else: lowerCamelCase_ =None return tokenizer.pad( __snake_case , padding='''longest''' , max_length=__snake_case , pad_to_multiple_of=__snake_case , return_tensors='''pt''' , ) # Instantiate dataloaders. lowerCamelCase_ =DataLoader( tokenized_datasets['''train'''] , shuffle=__snake_case , collate_fn=__snake_case , batch_size=__snake_case ) lowerCamelCase_ =DataLoader( tokenized_datasets['''validation'''] , shuffle=__snake_case , collate_fn=__snake_case , batch_size=__snake_case ) 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 a_ : Tuple = mocked_dataloaders # noqa: F811 def a_ ( __snake_case : List[str] , __snake_case : Tuple ) -> Optional[Any]: """simple docstring""" # For testing only if os.environ.get('''TESTING_MOCKED_DATALOADERS''' , __snake_case ) == "1": lowerCamelCase_ =2 # Initialize accelerator lowerCamelCase_ =Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs lowerCamelCase_ =config['''lr'''] lowerCamelCase_ =int(config['''num_epochs'''] ) lowerCamelCase_ =int(config['''seed'''] ) lowerCamelCase_ =int(config['''batch_size'''] ) lowerCamelCase_ =evaluate.load('''glue''' , '''mrpc''' ) # New Code # # We now can define an inner training loop function. It should take a batch size as the only parameter, # and build the dataloaders in there. # It also gets our decorator @find_executable_batch_size(starting_batch_size=__snake_case ) def inner_training_loop(__snake_case : Union[str, Any] ): # And now just move everything below under this function # We need to bring in the Accelerator object from earlier nonlocal accelerator # And reset all of its attributes that could hold onto any memory: accelerator.free_memory() # Then we can declare the model, optimizer, and everything else: set_seed(__snake_case ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) lowerCamelCase_ =AutoModelForSequenceClassification.from_pretrained('''bert-base-cased''' , return_dict=__snake_case ) # 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). lowerCamelCase_ =model.to(accelerator.device ) # Instantiate optimizer lowerCamelCase_ =AdamW(params=model.parameters() , lr=__snake_case ) lowerCamelCase_, lowerCamelCase_ =get_dataloaders(__snake_case , __snake_case ) # Instantiate scheduler lowerCamelCase_ =get_linear_schedule_with_warmup( optimizer=__snake_case , num_warmup_steps=100 , num_training_steps=(len(__snake_case ) * num_epochs) , ) # 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. lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ =accelerator.prepare( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ) # Now we train the model for epoch in range(__snake_case ): model.train() for step, batch in enumerate(__snake_case ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) lowerCamelCase_ =model(**__snake_case ) lowerCamelCase_ =outputs.loss accelerator.backward(__snake_case ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(__snake_case ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): lowerCamelCase_ =model(**__snake_case ) lowerCamelCase_ =outputs.logits.argmax(dim=-1 ) lowerCamelCase_, lowerCamelCase_ =accelerator.gather_for_metrics((predictions, batch['''labels''']) ) metric.add_batch( predictions=__snake_case , references=__snake_case , ) lowerCamelCase_ =metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F'''epoch {epoch}:''' , __snake_case ) # New Code # # And call it at the end with no arguments # Note: You could also refactor this outside of your training loop function inner_training_loop() def a_ ( ) -> Dict: """simple docstring""" lowerCamelCase_ =argparse.ArgumentParser(description='''Simple example of training script.''' ) parser.add_argument( '''--mixed_precision''' , type=__snake_case , default=__snake_case , 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.''' ) lowerCamelCase_ =parser.parse_args() lowerCamelCase_ ={'''lr''': 2e-5, '''num_epochs''': 3, '''seed''': 42, '''batch_size''': 16} training_function(__snake_case , __snake_case ) if __name__ == "__main__": main()
6
1
'''simple docstring''' import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES from ...utils import logging from ..auto import CONFIG_MAPPING a_ : int = logging.get_logger(__name__) a_ : Tuple = { """Salesforce/instruct-blip-flan-t5""": """https://huggingface.co/Salesforce/instruct-blip-flan-t5/resolve/main/config.json""", } class __UpperCamelCase ( lowerCamelCase__ ): lowercase : Any ='instructblip_vision_model' def __init__( self, lowerCAmelCase=1_408, lowerCAmelCase=6_144, lowerCAmelCase=39, lowerCAmelCase=16, lowerCAmelCase=224, lowerCAmelCase=14, lowerCAmelCase="gelu", lowerCAmelCase=1e-6, lowerCAmelCase=0.0, lowerCAmelCase=1e-10, lowerCAmelCase=True, **lowerCAmelCase, ): """simple docstring""" super().__init__(**lowerCAmelCase ) lowerCamelCase_ =hidden_size lowerCamelCase_ =intermediate_size lowerCamelCase_ =num_hidden_layers lowerCamelCase_ =num_attention_heads lowerCamelCase_ =patch_size lowerCamelCase_ =image_size lowerCamelCase_ =initializer_range lowerCamelCase_ =attention_dropout lowerCamelCase_ =layer_norm_eps lowerCamelCase_ =hidden_act lowerCamelCase_ =qkv_bias @classmethod def lowercase__ ( cls, lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" cls._set_token_in_kwargs(lowerCAmelCase ) lowerCamelCase_, lowerCamelCase_ =cls.get_config_dict(lowerCAmelCase, **lowerCAmelCase ) # get the vision config dict if we are loading from InstructBlipConfig if config_dict.get('''model_type''' ) == "instructblip": lowerCamelCase_ =config_dict['''vision_config'''] if "model_type" in config_dict and hasattr(cls, '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f'''You are using a model of type {config_dict['model_type']} to instantiate a model of type ''' f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(lowerCAmelCase, **lowerCAmelCase ) class __UpperCamelCase ( lowerCamelCase__ ): lowercase : List[Any] ='instructblip_qformer' def __init__( self, lowerCAmelCase=30_522, lowerCAmelCase=768, lowerCAmelCase=12, lowerCAmelCase=12, lowerCAmelCase=3_072, lowerCAmelCase="gelu", lowerCAmelCase=0.1, lowerCAmelCase=0.1, lowerCAmelCase=512, lowerCAmelCase=0.0_2, lowerCAmelCase=1e-12, lowerCAmelCase=0, lowerCAmelCase="absolute", lowerCAmelCase=2, lowerCAmelCase=1_408, **lowerCAmelCase, ): """simple docstring""" super().__init__(pad_token_id=lowerCAmelCase, **lowerCAmelCase ) lowerCamelCase_ =vocab_size lowerCamelCase_ =hidden_size lowerCamelCase_ =num_hidden_layers lowerCamelCase_ =num_attention_heads lowerCamelCase_ =hidden_act lowerCamelCase_ =intermediate_size lowerCamelCase_ =hidden_dropout_prob lowerCamelCase_ =attention_probs_dropout_prob lowerCamelCase_ =max_position_embeddings lowerCamelCase_ =initializer_range lowerCamelCase_ =layer_norm_eps lowerCamelCase_ =position_embedding_type lowerCamelCase_ =cross_attention_frequency lowerCamelCase_ =encoder_hidden_size @classmethod def lowercase__ ( cls, lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" cls._set_token_in_kwargs(lowerCAmelCase ) lowerCamelCase_, lowerCamelCase_ =cls.get_config_dict(lowerCAmelCase, **lowerCAmelCase ) # get the qformer config dict if we are loading from InstructBlipConfig if config_dict.get('''model_type''' ) == "instructblip": lowerCamelCase_ =config_dict['''qformer_config'''] if "model_type" in config_dict and hasattr(cls, '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f'''You are using a model of type {config_dict['model_type']} to instantiate a model of type ''' f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(lowerCAmelCase, **lowerCAmelCase ) class __UpperCamelCase ( lowerCamelCase__ ): lowercase : Dict ='instructblip' lowercase : Dict =True def __init__( self, lowerCAmelCase=None, lowerCAmelCase=None, lowerCAmelCase=None, lowerCAmelCase=32, **lowerCAmelCase ): """simple docstring""" super().__init__(**lowerCAmelCase ) if vision_config is None: lowerCamelCase_ ={} logger.info('''vision_config is None. initializing the InstructBlipVisionConfig with default values.''' ) if qformer_config is None: lowerCamelCase_ ={} logger.info('''qformer_config is None. Initializing the InstructBlipQFormerConfig with default values.''' ) if text_config is None: lowerCamelCase_ ={} logger.info('''text_config is None. Initializing the text config with default values (`OPTConfig`).''' ) lowerCamelCase_ =InstructBlipVisionConfig(**lowerCAmelCase ) lowerCamelCase_ =InstructBlipQFormerConfig(**lowerCAmelCase ) lowerCamelCase_ =text_config['''model_type'''] if '''model_type''' in text_config else '''opt''' lowerCamelCase_ =CONFIG_MAPPING[text_model_type](**lowerCAmelCase ) lowerCamelCase_ =self.text_config.tie_word_embeddings lowerCamelCase_ =self.text_config.is_encoder_decoder lowerCamelCase_ =num_query_tokens lowerCamelCase_ =self.vision_config.hidden_size lowerCamelCase_ =self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES lowerCamelCase_ =1.0 lowerCamelCase_ =0.0_2 @classmethod def lowercase__ ( cls, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, **lowerCAmelCase, ): """simple docstring""" return cls( vision_config=vision_config.to_dict(), qformer_config=qformer_config.to_dict(), text_config=text_config.to_dict(), **lowerCAmelCase, ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =copy.deepcopy(self.__dict__ ) lowerCamelCase_ =self.vision_config.to_dict() lowerCamelCase_ =self.qformer_config.to_dict() lowerCamelCase_ =self.text_config.to_dict() lowerCamelCase_ =self.__class__.model_type return output
6
'''simple docstring''' import argparse import os import re # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_dummies.py a_ : List[str] = """src/diffusers""" # Matches is_xxx_available() a_ : int = re.compile(R"""is\_([a-z_]*)_available\(\)""") # Matches from xxx import bla a_ : List[str] = re.compile(R"""\s+from\s+\S*\s+import\s+([^\(\s].*)\n""") a_ : Optional[Any] = """ {0} = None """ a_ : List[Any] = """ class {0}(metaclass=DummyObject): _backends = {1} def __init__(self, *args, **kwargs): requires_backends(self, {1}) @classmethod def from_config(cls, *args, **kwargs): requires_backends(cls, {1}) @classmethod def from_pretrained(cls, *args, **kwargs): requires_backends(cls, {1}) """ a_ : Optional[Any] = """ def {0}(*args, **kwargs): requires_backends({0}, {1}) """ def a_ ( __snake_case : Union[str, Any] ) -> List[Any]: """simple docstring""" lowerCamelCase_ =_re_backend.findall(__snake_case ) if len(__snake_case ) == 0: return None return "_and_".join(__snake_case ) def a_ ( ) -> Optional[int]: """simple docstring""" with open(os.path.join(__snake_case , '''__init__.py''' ) , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: lowerCamelCase_ =f.readlines() # Get to the point we do the actual imports for type checking lowerCamelCase_ =0 lowerCamelCase_ ={} # Go through the end of the file while line_index < len(__snake_case ): # If the line contains is_backend_available, we grab all objects associated with the `else` block lowerCamelCase_ =find_backend(lines[line_index] ) if backend is not None: while not lines[line_index].startswith('''else:''' ): line_index += 1 line_index += 1 lowerCamelCase_ =[] # Until we unindent, add backend objects to the list while line_index < len(__snake_case ) and len(lines[line_index] ) > 1: lowerCamelCase_ =lines[line_index] lowerCamelCase_ =_re_single_line_import.search(__snake_case ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(''', ''' ) ) elif line.startswith(''' ''' * 8 ): objects.append(line[8:-2] ) line_index += 1 if len(__snake_case ) > 0: lowerCamelCase_ =objects else: line_index += 1 return backend_specific_objects def a_ ( __snake_case : Dict , __snake_case : int ) -> Union[str, Any]: """simple docstring""" if name.isupper(): return DUMMY_CONSTANT.format(__snake_case ) elif name.islower(): return DUMMY_FUNCTION.format(__snake_case , __snake_case ) else: return DUMMY_CLASS.format(__snake_case , __snake_case ) def a_ ( __snake_case : Tuple=None ) -> List[str]: """simple docstring""" if backend_specific_objects is None: lowerCamelCase_ =read_init() # For special correspondence backend to module name as used in the function requires_modulename lowerCamelCase_ ={} for backend, objects in backend_specific_objects.items(): lowerCamelCase_ ='''[''' + ''', '''.join(F'''"{b}"''' for b in backend.split('''_and_''' ) ) + ''']''' lowerCamelCase_ ='''# This file is autogenerated by the command `make fix-copies`, do not edit.\n''' dummy_file += "from ..utils import DummyObject, requires_backends\n\n" dummy_file += "\n".join([create_dummy_object(__snake_case , __snake_case ) for o in objects] ) lowerCamelCase_ =dummy_file return dummy_files def a_ ( __snake_case : Dict=False ) -> Union[str, Any]: """simple docstring""" lowerCamelCase_ =create_dummy_files() # For special correspondence backend to shortcut as used in utils/dummy_xxx_objects.py lowerCamelCase_ ={'''torch''': '''pt'''} # Locate actual dummy modules and read their content. lowerCamelCase_ =os.path.join(__snake_case , '''utils''' ) lowerCamelCase_ ={ backend: os.path.join(__snake_case , F'''dummy_{short_names.get(__snake_case , __snake_case )}_objects.py''' ) for backend in dummy_files.keys() } lowerCamelCase_ ={} for backend, file_path in dummy_file_paths.items(): if os.path.isfile(__snake_case ): with open(__snake_case , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: lowerCamelCase_ =f.read() else: lowerCamelCase_ ='''''' for backend in dummy_files.keys(): if dummy_files[backend] != actual_dummies[backend]: if overwrite: print( F'''Updating diffusers.utils.dummy_{short_names.get(__snake_case , __snake_case )}_objects.py as the main ''' '''__init__ has new objects.''' ) with open(dummy_file_paths[backend] , '''w''' , encoding='''utf-8''' , newline='''\n''' ) as f: f.write(dummy_files[backend] ) else: raise ValueError( '''The main __init__ has objects that are not present in ''' F'''diffusers.utils.dummy_{short_names.get(__snake_case , __snake_case )}_objects.py. Run `make fix-copies` ''' '''to fix this.''' ) if __name__ == "__main__": a_ : Tuple = argparse.ArgumentParser() parser.add_argument("""--fix_and_overwrite""", action="""store_true""", help="""Whether to fix inconsistencies.""") a_ : Tuple = parser.parse_args() check_dummies(args.fix_and_overwrite)
6
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_torch_available, ) a_ : Any = { """configuration_trocr""": ["""TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP""", """TrOCRConfig"""], """processing_trocr""": ["""TrOCRProcessor"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ : Optional[int] = [ """TROCR_PRETRAINED_MODEL_ARCHIVE_LIST""", """TrOCRForCausalLM""", """TrOCRPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_trocr import TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP, TrOCRConfig from .processing_trocr import TrOCRProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_trocr import TROCR_PRETRAINED_MODEL_ARCHIVE_LIST, TrOCRForCausalLM, TrOCRPreTrainedModel else: import sys a_ : int = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
6
'''simple docstring''' a_ : List[Any] = [sum(int(c, 10) ** 2 for c in i.__str__()) for i in range(10_00_00)] def a_ ( __snake_case : int ) -> int: """simple docstring""" lowerCamelCase_ =0 while number: # Increased Speed Slightly by checking every 5 digits together. sum_of_digits_squared += DIGITS_SQUARED[number % 10_0000] number //= 10_0000 return sum_of_digits_squared # There are 2 Chains made, # One ends with 89 with the chain member 58 being the one which when declared first, # there will be the least number of iterations for all the members to be checked. # The other one ends with 1 and has only one element 1. # So 58 and 1 are chosen to be declared at the starting. # Changed dictionary to an array to quicken the solution a_ : list[bool | None] = [None] * 10_00_00_00 a_ : List[Any] = True a_ : Optional[Any] = False def a_ ( __snake_case : int ) -> bool: """simple docstring""" if CHAINS[number - 1] is not None: return CHAINS[number - 1] # type: ignore lowerCamelCase_ =chain(next_number(__snake_case ) ) lowerCamelCase_ =number_chain while number < 1000_0000: lowerCamelCase_ =number_chain number *= 10 return number_chain def a_ ( __snake_case : int = 1000_0000 ) -> int: """simple docstring""" for i in range(1 , __snake_case ): if CHAINS[i] is None: chain(i + 1 ) return CHAINS[:number].count(__snake_case ) if __name__ == "__main__": import doctest doctest.testmod() print(F"""{solution() = }""")
6
1
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging a_ : Union[str, Any] = logging.get_logger(__name__) a_ : Optional[int] = { """xlm-roberta-base""": """https://huggingface.co/xlm-roberta-base/resolve/main/config.json""", """xlm-roberta-large""": """https://huggingface.co/xlm-roberta-large/resolve/main/config.json""", """xlm-roberta-large-finetuned-conll02-dutch""": ( """https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json""" ), """xlm-roberta-large-finetuned-conll02-spanish""": ( """https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json""" ), """xlm-roberta-large-finetuned-conll03-english""": ( """https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json""" ), """xlm-roberta-large-finetuned-conll03-german""": ( """https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json""" ), } class __UpperCamelCase ( lowerCamelCase__ ): lowercase : Tuple ='xlm-roberta' def __init__( self, lowerCAmelCase=30_522, lowerCAmelCase=768, lowerCAmelCase=12, lowerCAmelCase=12, lowerCAmelCase=3_072, lowerCAmelCase="gelu", lowerCAmelCase=0.1, lowerCAmelCase=0.1, lowerCAmelCase=512, lowerCAmelCase=2, lowerCAmelCase=0.0_2, lowerCAmelCase=1e-12, lowerCAmelCase=1, lowerCAmelCase=0, lowerCAmelCase=2, lowerCAmelCase="absolute", lowerCAmelCase=True, lowerCAmelCase=None, **lowerCAmelCase, ): """simple docstring""" super().__init__(pad_token_id=lowerCAmelCase, bos_token_id=lowerCAmelCase, eos_token_id=lowerCAmelCase, **lowerCAmelCase ) lowerCamelCase_ =vocab_size lowerCamelCase_ =hidden_size lowerCamelCase_ =num_hidden_layers lowerCamelCase_ =num_attention_heads lowerCamelCase_ =hidden_act lowerCamelCase_ =intermediate_size lowerCamelCase_ =hidden_dropout_prob lowerCamelCase_ =attention_probs_dropout_prob lowerCamelCase_ =max_position_embeddings lowerCamelCase_ =type_vocab_size lowerCamelCase_ =initializer_range lowerCamelCase_ =layer_norm_eps lowerCamelCase_ =position_embedding_type lowerCamelCase_ =use_cache lowerCamelCase_ =classifier_dropout class __UpperCamelCase ( lowerCamelCase__ ): @property def lowercase__ ( self ): """simple docstring""" if self.task == "multiple-choice": lowerCamelCase_ ={0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: lowerCamelCase_ ={0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ] )
6
'''simple docstring''' from argparse import ArgumentParser from . import BaseTransformersCLICommand def a_ ( __snake_case : Tuple ) -> str: """simple docstring""" return DownloadCommand(args.model , args.cache_dir , args.force , args.trust_remote_code ) class __UpperCamelCase ( lowerCamelCase__ ): @staticmethod def lowercase__ ( lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =parser.add_parser('''download''' ) download_parser.add_argument( '''--cache-dir''', type=lowerCAmelCase, default=lowerCAmelCase, help='''Path to location to store the models''' ) download_parser.add_argument( '''--force''', action='''store_true''', help='''Force the model to be download even if already in cache-dir''' ) download_parser.add_argument( '''--trust-remote-code''', action='''store_true''', help='''Whether or not to allow for custom models defined on the Hub in their own modeling files. Use only if you\'ve reviewed the code as it will execute on your local machine''', ) download_parser.add_argument('''model''', type=lowerCAmelCase, help='''Name of the model to download''' ) download_parser.set_defaults(func=lowerCAmelCase ) def __init__( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =model lowerCamelCase_ =cache lowerCamelCase_ =force lowerCamelCase_ =trust_remote_code def lowercase__ ( self ): """simple docstring""" from ..models.auto import AutoModel, AutoTokenizer AutoModel.from_pretrained( self._model, cache_dir=self._cache, force_download=self._force, trust_remote_code=self._trust_remote_code ) AutoTokenizer.from_pretrained( self._model, cache_dir=self._cache, force_download=self._force, trust_remote_code=self._trust_remote_code )
6
1
'''simple docstring''' def a_ ( __snake_case : str , __snake_case : str ) -> float: """simple docstring""" def get_matched_characters(__snake_case : str , __snake_case : str ) -> str: lowerCamelCase_ =[] lowerCamelCase_ =min(len(_stra ) , len(_stra ) ) // 2 for i, l in enumerate(_stra ): lowerCamelCase_ =int(max(0 , i - limit ) ) lowerCamelCase_ =int(min(i + limit + 1 , len(_stra ) ) ) if l in _stra[left:right]: matched.append(__snake_case ) lowerCamelCase_ =F'''{_stra[0:_stra.index(__snake_case )]} {_stra[_stra.index(__snake_case ) + 1:]}''' return "".join(__snake_case ) # matching characters lowerCamelCase_ =get_matched_characters(__snake_case , __snake_case ) lowerCamelCase_ =get_matched_characters(__snake_case , __snake_case ) lowerCamelCase_ =len(__snake_case ) # transposition lowerCamelCase_ =( len([(ca, ca) for ca, ca in zip(__snake_case , __snake_case ) if ca != ca] ) // 2 ) if not match_count: lowerCamelCase_ =0.0 else: lowerCamelCase_ =( 1 / 3 * ( match_count / len(__snake_case ) + match_count / len(__snake_case ) + (match_count - transpositions) / match_count ) ) # common prefix up to 4 characters lowerCamelCase_ =0 for ca, ca in zip(stra[:4] , stra[:4] ): if ca == ca: prefix_len += 1 else: break return jaro + 0.1 * prefix_len * (1 - jaro) if __name__ == "__main__": import doctest doctest.testmod() print(jaro_winkler("""hello""", """world"""))
6
'''simple docstring''' import os import time from dataclasses import dataclass, field from enum import Enum from typing import Dict, List, Optional, Union import torch from filelock import FileLock from torch.utils.data import Dataset from ...models.auto.modeling_auto import MODEL_FOR_QUESTION_ANSWERING_MAPPING from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging from ..processors.squad import SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features a_ : List[str] = logging.get_logger(__name__) a_ : Optional[Any] = list(MODEL_FOR_QUESTION_ANSWERING_MAPPING.keys()) a_ : Optional[Any] = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class __UpperCamelCase : lowercase : str =field( default=lowerCamelCase__ , metadata={'help': 'Model type selected in the list: ' + ', '.join(lowerCamelCase__ )} ) lowercase : str =field( default=lowerCamelCase__ , metadata={'help': 'The input data dir. Should contain the .json files for the SQuAD task.'} ) lowercase : int =field( default=1_28 , metadata={ 'help': ( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) } , ) lowercase : int =field( default=1_28 , metadata={'help': 'When splitting up a long document into chunks, how much stride to take between chunks.'} , ) lowercase : int =field( default=64 , metadata={ 'help': ( 'The maximum number of tokens for the question. Questions longer than this will ' 'be truncated to this length.' ) } , ) lowercase : int =field( default=30 , metadata={ 'help': ( 'The maximum length of an answer that can be generated. This is needed because the start ' 'and end predictions are not conditioned on one another.' ) } , ) lowercase : bool =field( default=lowerCamelCase__ , metadata={'help': 'Overwrite the cached training and evaluation sets'} ) lowercase : bool =field( default=lowerCamelCase__ , metadata={'help': 'If true, the SQuAD examples contain some that do not have an answer.'} ) lowercase : float =field( default=0.0 , metadata={'help': 'If null_score - best_non_null is greater than the threshold predict null.'} ) lowercase : int =field( default=20 , metadata={'help': 'If null_score - best_non_null is greater than the threshold predict null.'} ) lowercase : int =field( default=0 , metadata={ 'help': ( 'language id of input for language-specific xlm models (see' ' tokenization_xlm.PRETRAINED_INIT_CONFIGURATION)' ) } , ) lowercase : int =field(default=1 , metadata={'help': 'multiple threads for converting example to features'} ) class __UpperCamelCase ( lowerCamelCase__ ): lowercase : Optional[Any] ='train' lowercase : Any ='dev' class __UpperCamelCase ( lowerCamelCase__ ): lowercase : SquadDataTrainingArguments lowercase : List[SquadFeatures] lowercase : Split lowercase : bool def __init__( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase = None, lowerCAmelCase = Split.train, lowerCAmelCase = False, lowerCAmelCase = None, lowerCAmelCase = "pt", ): """simple docstring""" lowerCamelCase_ =args lowerCamelCase_ =is_language_sensitive lowerCamelCase_ =SquadVaProcessor() if args.version_2_with_negative else SquadVaProcessor() if isinstance(lowerCAmelCase, lowerCAmelCase ): try: lowerCamelCase_ =Split[mode] except KeyError: raise KeyError('''mode is not a valid split name''' ) lowerCamelCase_ =mode # Load data features from cache or dataset file lowerCamelCase_ ='''v2''' if args.version_2_with_negative else '''v1''' lowerCamelCase_ =os.path.join( cache_dir if cache_dir is not None else args.data_dir, f'''cached_{mode.value}_{tokenizer.__class__.__name__}_{args.max_seq_length}_{version_tag}''', ) # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. lowerCamelCase_ =cached_features_file + '''.lock''' with FileLock(lowerCAmelCase ): if os.path.exists(lowerCAmelCase ) and not args.overwrite_cache: lowerCamelCase_ =time.time() lowerCamelCase_ =torch.load(lowerCAmelCase ) # Legacy cache files have only features, while new cache files # will have dataset and examples also. lowerCamelCase_ =self.old_features['''features'''] lowerCamelCase_ =self.old_features.get('''dataset''', lowerCAmelCase ) lowerCamelCase_ =self.old_features.get('''examples''', lowerCAmelCase ) logger.info( f'''Loading features from cached file {cached_features_file} [took %.3f s]''', time.time() - start ) if self.dataset is None or self.examples is None: logger.warning( f'''Deleting cached file {cached_features_file} will allow dataset and examples to be cached in''' ''' future run''' ) else: if mode == Split.dev: lowerCamelCase_ =self.processor.get_dev_examples(args.data_dir ) else: lowerCamelCase_ =self.processor.get_train_examples(args.data_dir ) lowerCamelCase_, lowerCamelCase_ =squad_convert_examples_to_features( examples=self.examples, tokenizer=lowerCAmelCase, max_seq_length=args.max_seq_length, doc_stride=args.doc_stride, max_query_length=args.max_query_length, is_training=mode == Split.train, threads=args.threads, return_dataset=lowerCAmelCase, ) lowerCamelCase_ =time.time() torch.save( {'''features''': self.features, '''dataset''': self.dataset, '''examples''': self.examples}, lowerCAmelCase, ) # ^ This seems to take a lot of time so I want to investigate why and how we can improve. logger.info( f'''Saving features into cached file {cached_features_file} [took {time.time() - start:.3f} s]''' ) def __len__( self ): """simple docstring""" return len(self.features ) def __getitem__( self, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =self.features[i] lowerCamelCase_ =torch.tensor(feature.input_ids, dtype=torch.long ) lowerCamelCase_ =torch.tensor(feature.attention_mask, dtype=torch.long ) lowerCamelCase_ =torch.tensor(feature.token_type_ids, dtype=torch.long ) lowerCamelCase_ =torch.tensor(feature.cls_index, dtype=torch.long ) lowerCamelCase_ =torch.tensor(feature.p_mask, dtype=torch.float ) lowerCamelCase_ =torch.tensor(feature.is_impossible, dtype=torch.float ) lowerCamelCase_ ={ '''input_ids''': input_ids, '''attention_mask''': attention_mask, '''token_type_ids''': token_type_ids, } if self.args.model_type in ["xlm", "roberta", "distilbert", "camembert"]: del inputs["token_type_ids"] if self.args.model_type in ["xlnet", "xlm"]: inputs.update({'''cls_index''': cls_index, '''p_mask''': p_mask} ) if self.args.version_2_with_negative: inputs.update({'''is_impossible''': is_impossible} ) if self.is_language_sensitive: inputs.update({'''langs''': (torch.ones(input_ids.shape, dtype=torch.intaa ) * self.args.lang_id)} ) if self.mode == Split.train: lowerCamelCase_ =torch.tensor(feature.start_position, dtype=torch.long ) lowerCamelCase_ =torch.tensor(feature.end_position, dtype=torch.long ) inputs.update({'''start_positions''': start_positions, '''end_positions''': end_positions} ) return inputs
6
1
'''simple docstring''' from __future__ import annotations from functools import lru_cache from math import ceil a_ : Tuple = 1_00 a_ : List[Any] = set(range(3, NUM_PRIMES, 2)) primes.add(2) a_ : int for prime in range(3, ceil(NUM_PRIMES**0.5), 2): if prime not in primes: continue primes.difference_update(set(range(prime * prime, NUM_PRIMES, prime))) @lru_cache(maxsize=100 ) def a_ ( __snake_case : int ) -> set[int]: """simple docstring""" if number_to_partition < 0: return set() elif number_to_partition == 0: return {1} lowerCamelCase_ =set() lowerCamelCase_ =42 lowerCamelCase_ =42 for prime in primes: if prime > number_to_partition: continue for sub in partition(number_to_partition - prime ): ret.add(sub * prime ) return ret def a_ ( __snake_case : int = 5000 ) -> int | None: """simple docstring""" for number_to_partition in range(1 , __snake_case ): if len(partition(__snake_case ) ) > number_unique_partitions: return number_to_partition return None if __name__ == "__main__": print(F"""{solution() = }""")
6
'''simple docstring''' import argparse import json import os from pathlib import Path import requests import torch from transformers import JukeboxConfig, JukeboxModel from transformers.utils import logging logging.set_verbosity_info() a_ : Any = logging.get_logger(__name__) a_ : Optional[int] = """https://openaipublic.azureedge.net/jukebox/models/""" a_ : Any = { """jukebox-1b-lyrics""": [ """5b/vqvae.pth.tar""", """5b/prior_level_0.pth.tar""", """5b/prior_level_1.pth.tar""", """1b_lyrics/prior_level_2.pth.tar""", ], """jukebox-5b-lyrics""": [ """5b/vqvae.pth.tar""", """5b/prior_level_0.pth.tar""", """5b/prior_level_1.pth.tar""", """5b_lyrics/prior_level_2.pth.tar""", ], } def a_ ( __snake_case : int ) -> Any: """simple docstring""" if key.endswith('''.model.1.bias''' ) and len(key.split('''.''' ) ) > 10: lowerCamelCase_ =key.replace('''.model.1.bias''' , '''.conv1d_1.bias''' ) elif key.endswith('''.model.1.weight''' ) and len(key.split('''.''' ) ) > 10: lowerCamelCase_ =key.replace('''.model.1.weight''' , '''.conv1d_1.weight''' ) elif key.endswith('''.model.3.bias''' ) and len(key.split('''.''' ) ) > 10: lowerCamelCase_ =key.replace('''.model.3.bias''' , '''.conv1d_2.bias''' ) elif key.endswith('''.model.3.weight''' ) and len(key.split('''.''' ) ) > 10: lowerCamelCase_ =key.replace('''.model.3.weight''' , '''.conv1d_2.weight''' ) if "conditioner_blocks.0." in key: lowerCamelCase_ =key.replace('''conditioner_blocks.0''' , '''conditioner_blocks''' ) if "prime_prior" in key: lowerCamelCase_ =key.replace('''prime_prior''' , '''encoder''' ) if ".emb." in key and "total" not in key and "absolute" not in key and "relative" not in key: lowerCamelCase_ =key.replace('''.emb.''' , '''.''' ) if key.endswith('''k''' ): # replace vqvae.X.k with vqvae.X.codebook return key.replace('''.k''' , '''.codebook''' ) if "y_emb." in key: return key.replace('''y_emb.''' , '''metadata_embedding.''' ) if "x_emb.emb." in key: lowerCamelCase_ =key.replace('''0.x_emb.emb''' , '''embed_tokens''' ) if "prime_state_ln" in key: return key.replace('''prime_state_ln''' , '''encoder.final_layer_norm''' ) if ".ln" in key: return key.replace('''.ln''' , '''.layer_norm''' ) if "_ln" in key: return key.replace('''_ln''' , '''_layer_norm''' ) if "prime_state_proj" in key: return key.replace('''prime_state_proj''' , '''encoder.proj_in''' ) if "prime_x_out" in key: return key.replace('''prime_x_out''' , '''encoder.lm_head''' ) if "prior.x_out" in key: return key.replace('''x_out''' , '''fc_proj_out''' ) if "x_emb" in key: return key.replace('''x_emb''' , '''embed_tokens''' ) return key def a_ ( __snake_case : Dict , __snake_case : int , __snake_case : Dict , __snake_case : Optional[Any] ) -> Optional[Any]: """simple docstring""" lowerCamelCase_ ={} import re lowerCamelCase_ =re.compile(r'''encoders.(\d*).level_blocks.(\d*).model.(\d*).(\d).(bias|weight)''' ) lowerCamelCase_ =re.compile( r'''encoders.(\d*).level_blocks.(\d*).model.(\d*).(\d).model.(\d*).model.(\d*).(bias|weight)''' ) lowerCamelCase_ =re.compile(r'''encoders.(\d*).level_blocks.(\d*).model.(\d*).(bias|weight)''' ) lowerCamelCase_ =re.compile(r'''decoders.(\d*).level_blocks.(\d*).model.(\d*).(\d).(bias|weight)''' ) lowerCamelCase_ =re.compile( r'''decoders.(\d*).level_blocks.(\d*).model.(\d*).(\d).model.(\d*).model.(\d*).(bias|weight)''' ) lowerCamelCase_ =re.compile(r'''decoders.(\d*).level_blocks.(\d*).model.(\d*).(bias|weight)''' ) lowerCamelCase_ =re.compile(r'''conditioner_blocks.(\d*).cond.model.(\d*).(\d).(bias|weight)''' ) lowerCamelCase_ =re.compile( r'''conditioner_blocks.(\d*).cond.model.(\d*).(\d).model.(\d*).model.(\d*).(bias|weight)''' ) lowerCamelCase_ =re.compile(r'''conditioner_blocks.(\d*).cond.model.(\d*).(bias|weight)''' ) for original_key, value in state_dict.items(): # rename vqvae.encoder keys if re_encoder_block_conv_in.fullmatch(__snake_case ): lowerCamelCase_ =re_encoder_block_conv_in.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =int(groups[2] ) * 2 + int(groups[3] ) lowerCamelCase_ =F'''encoders.{groups[0]}.level_blocks.{groups[1]}.downsample_block.{block_index}.{groups[-1]}''' lowerCamelCase_ =re_encoder_block_conv_in.sub(__snake_case , __snake_case ) elif re_encoder_block_resnet.fullmatch(__snake_case ): lowerCamelCase_ =re_encoder_block_resnet.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =int(groups[2] ) * 2 + int(groups[3] ) lowerCamelCase_ ={'''1''': 1, '''3''': 2}[groups[-2]] lowerCamelCase_ =F'''encoders.{groups[0]}.level_blocks.{groups[1]}.downsample_block.{block_index}.''' lowerCamelCase_ =F'''resnet_block.{groups[-3]}.conv1d_{conv_index}.{groups[-1]}''' lowerCamelCase_ =prefix + resnet_block lowerCamelCase_ =re_encoder_block_resnet.sub(__snake_case , __snake_case ) elif re_encoder_block_proj_out.fullmatch(__snake_case ): lowerCamelCase_ =re_encoder_block_proj_out.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =F'''encoders.{groups[0]}.level_blocks.{groups[1]}.proj_out.{groups[-1]}''' lowerCamelCase_ =re_encoder_block_proj_out.sub(__snake_case , __snake_case ) # rename vqvae.decoder keys elif re_decoder_block_conv_out.fullmatch(__snake_case ): lowerCamelCase_ =re_decoder_block_conv_out.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =int(groups[2] ) * 2 + int(groups[3] ) - 2 lowerCamelCase_ =F'''decoders.{groups[0]}.level_blocks.{groups[1]}.upsample_block.{block_index}.{groups[-1]}''' lowerCamelCase_ =re_decoder_block_conv_out.sub(__snake_case , __snake_case ) elif re_decoder_block_resnet.fullmatch(__snake_case ): lowerCamelCase_ =re_decoder_block_resnet.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =int(groups[2] ) * 2 + int(groups[3] ) - 2 lowerCamelCase_ ={'''1''': 1, '''3''': 2}[groups[-2]] lowerCamelCase_ =F'''decoders.{groups[0]}.level_blocks.{groups[1]}.upsample_block.{block_index}.''' lowerCamelCase_ =F'''resnet_block.{groups[-3]}.conv1d_{conv_index}.{groups[-1]}''' lowerCamelCase_ =prefix + resnet_block lowerCamelCase_ =re_decoder_block_resnet.sub(__snake_case , __snake_case ) elif re_decoder_block_proj_in.fullmatch(__snake_case ): lowerCamelCase_ =re_decoder_block_proj_in.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =F'''decoders.{groups[0]}.level_blocks.{groups[1]}.proj_in.{groups[-1]}''' lowerCamelCase_ =re_decoder_block_proj_in.sub(__snake_case , __snake_case ) # rename prior cond.model to upsampler.upsample_block and resnet elif re_prior_cond_conv_out.fullmatch(__snake_case ): lowerCamelCase_ =re_prior_cond_conv_out.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =int(groups[1] ) * 2 + int(groups[2] ) - 2 lowerCamelCase_ =F'''conditioner_blocks.upsampler.upsample_block.{block_index}.{groups[-1]}''' lowerCamelCase_ =re_prior_cond_conv_out.sub(__snake_case , __snake_case ) elif re_prior_cond_resnet.fullmatch(__snake_case ): lowerCamelCase_ =re_prior_cond_resnet.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =int(groups[1] ) * 2 + int(groups[2] ) - 2 lowerCamelCase_ ={'''1''': 1, '''3''': 2}[groups[-2]] lowerCamelCase_ =F'''conditioner_blocks.upsampler.upsample_block.{block_index}.''' lowerCamelCase_ =F'''resnet_block.{groups[-3]}.conv1d_{conv_index}.{groups[-1]}''' lowerCamelCase_ =prefix + resnet_block lowerCamelCase_ =re_prior_cond_resnet.sub(__snake_case , __snake_case ) elif re_prior_cond_proj_in.fullmatch(__snake_case ): lowerCamelCase_ =re_prior_cond_proj_in.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =F'''conditioner_blocks.upsampler.proj_in.{groups[-1]}''' lowerCamelCase_ =re_prior_cond_proj_in.sub(__snake_case , __snake_case ) # keep original key else: lowerCamelCase_ =original_key lowerCamelCase_ =replace_key(__snake_case ) if F'''{key_prefix}.{key}''' not in model_state_dict or key is None: print(F'''failed converting {original_key} to {key}, does not match''' ) # handle missmatched shape elif value.shape != model_state_dict[F'''{key_prefix}.{key}'''].shape: lowerCamelCase_ =model_state_dict[F'''{key_prefix}.{key}'''] print(F'''{original_key}-> {key} : \nshape {val.shape} and { value.shape}, do not match''' ) lowerCamelCase_ =original_key lowerCamelCase_ =original_key lowerCamelCase_ =value return new_dict @torch.no_grad() def a_ ( __snake_case : List[str]=None , __snake_case : Tuple=None ) -> Union[str, Any]: """simple docstring""" for file in MODEL_MAPPING[model_name]: if not os.path.isfile(F'''{pytorch_dump_folder_path}/{file.split('/' )[-1]}''' ): lowerCamelCase_ =requests.get(F'''{PREFIX}{file}''' , allow_redirects=__snake_case ) os.makedirs(F'''{pytorch_dump_folder_path}/''' , exist_ok=__snake_case ) open(F'''{pytorch_dump_folder_path}/{file.split('/' )[-1]}''' , '''wb''' ).write(r.content ) lowerCamelCase_ =MODEL_MAPPING[model_name.split('''/''' )[-1]] lowerCamelCase_ =JukeboxConfig.from_pretrained(__snake_case ) lowerCamelCase_ =JukeboxModel(__snake_case ) lowerCamelCase_ =[] lowerCamelCase_ ={} for i, dict_name in enumerate(__snake_case ): lowerCamelCase_ =torch.load(F'''{pytorch_dump_folder_path}/{dict_name.split('/' )[-1]}''' )['''model'''] lowerCamelCase_ ={} for k in old_dic.keys(): if k.endswith('''.b''' ): lowerCamelCase_ =old_dic[k] elif k.endswith('''.w''' ): lowerCamelCase_ =old_dic[k] elif "level_2" not in dict_name and "cond.model." in k: lowerCamelCase_ =old_dic[k] else: lowerCamelCase_ =old_dic[k] lowerCamelCase_ ='''vqvae''' if i == 0 else F'''priors.{3 - i}''' lowerCamelCase_ =fix_jukebox_keys(__snake_case , model.state_dict() , __snake_case , __snake_case ) weight_dict.append(__snake_case ) lowerCamelCase_ =weight_dict.pop(0 ) model.vqvae.load_state_dict(__snake_case ) for i in range(len(__snake_case ) ): model.priors[i].load_state_dict(weight_dict[2 - i] ) Path(__snake_case ).mkdir(exist_ok=__snake_case ) with open(F'''{pytorch_dump_folder_path}/mapping.json''' , '''w''' ) as txtfile: json.dump(__snake_case , __snake_case ) print(F'''Saving model {model_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(__snake_case ) return weight_dict if __name__ == "__main__": a_ : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default="""jukebox-5b-lyrics""", type=str, help="""Name of the model you'd like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default="""jukebox-5b-lyrics-converted""", type=str, help="""Path to the output PyTorch model directory.""", ) a_ : Optional[int] = parser.parse_args() convert_openai_checkpoint(args.model_name, args.pytorch_dump_folder_path)
6
1
'''simple docstring''' from ..utils import is_flax_available, is_torch_available if is_torch_available(): from .autoencoder_kl import AutoencoderKL from .controlnet import ControlNetModel from .dual_transformer_ad import DualTransformeraDModel from .modeling_utils import ModelMixin from .prior_transformer import PriorTransformer from .ta_film_transformer import TaFilmDecoder from .transformer_ad import TransformeraDModel from .unet_ad import UNetaDModel from .unet_ad import UNetaDModel from .unet_ad_condition import UNetaDConditionModel from .unet_ad_condition import UNetaDConditionModel from .vq_model import VQModel if is_flax_available(): from .controlnet_flax import FlaxControlNetModel from .unet_ad_condition_flax import FlaxUNetaDConditionModel from .vae_flax import FlaxAutoencoderKL
6
'''simple docstring''' def a_ ( __snake_case : int = 1000 ) -> int: """simple docstring""" lowerCamelCase_, lowerCamelCase_ =1, 1 lowerCamelCase_ =2 while True: lowerCamelCase_ =0 lowerCamelCase_ =fa + fa lowerCamelCase_, lowerCamelCase_ =fa, f index += 1 for _ in str(__snake_case ): i += 1 if i == n: break return index if __name__ == "__main__": print(solution(int(str(input()).strip())))
6
1
'''simple docstring''' from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import pyarrow as pa if TYPE_CHECKING: from .features import FeatureType @dataclass class __UpperCamelCase : lowercase : List[str] lowercase : Optional[str] =None # Automatically constructed lowercase : ClassVar[str] ="dict" lowercase : ClassVar[Any] =None lowercase : str =field(default='Translation' , init=lowerCamelCase__ , repr=lowerCamelCase__ ) def __call__( self ): """simple docstring""" return pa.struct({lang: pa.string() for lang in sorted(self.languages )} ) def lowercase__ ( self ): """simple docstring""" from .features import Value return {k: Value('''string''' ) for k in sorted(self.languages )} @dataclass class __UpperCamelCase : lowercase : Optional[List] =None lowercase : Optional[int] =None lowercase : Optional[str] =None # Automatically constructed lowercase : ClassVar[str] ="dict" lowercase : ClassVar[Any] =None lowercase : str =field(default='TranslationVariableLanguages' , init=lowerCamelCase__ , repr=lowerCamelCase__ ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =sorted(set(self.languages ) ) if self.languages else None lowerCamelCase_ =len(self.languages ) if self.languages else None def __call__( self ): """simple docstring""" return pa.struct({'''language''': pa.list_(pa.string() ), '''translation''': pa.list_(pa.string() )} ) def lowercase__ ( self, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =set(self.languages ) if self.languages and set(lowerCAmelCase ) - lang_set: raise ValueError( f'''Some languages in example ({', '.join(sorted(set(lowerCAmelCase ) - lang_set ) )}) are not in valid set ({', '.join(lowerCAmelCase )}).''' ) # Convert dictionary into tuples, splitting out cases where there are # multiple translations for a single language. lowerCamelCase_ =[] for lang, text in translation_dict.items(): if isinstance(lowerCAmelCase, lowerCAmelCase ): translation_tuples.append((lang, text) ) else: translation_tuples.extend([(lang, el) for el in text] ) # Ensure translations are in ascending order by language code. lowerCamelCase_, lowerCamelCase_ =zip(*sorted(lowerCAmelCase ) ) return {"language": languages, "translation": translations} def lowercase__ ( self ): """simple docstring""" from .features import Sequence, Value return { "language": Sequence(Value('''string''' ) ), "translation": Sequence(Value('''string''' ) ), }
6
'''simple docstring''' import importlib import os import sys # This is required to make the module import works (when the python process is running from the root of the repo) sys.path.append(""".""") def a_ ( __snake_case : Any ) -> Tuple: """simple docstring""" lowerCamelCase_ =test_file.split(os.path.sep ) if components[0:2] != ["tests", "models"]: raise ValueError( '''`test_file` should start with `tests/models/` (with `/` being the OS specific path separator). Got ''' F'''{test_file} instead.''' ) lowerCamelCase_ =components[-1] if not test_fn.endswith('''py''' ): raise ValueError(F'''`test_file` should be a python file. Got {test_fn} instead.''' ) if not test_fn.startswith('''test_modeling_''' ): raise ValueError( F'''`test_file` should point to a file name of the form `test_modeling_*.py`. Got {test_fn} instead.''' ) lowerCamelCase_ =components[:-1] + [test_fn.replace('''.py''' , '''''' )] lowerCamelCase_ ='''.'''.join(__snake_case ) return test_module_path def a_ ( __snake_case : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" lowerCamelCase_ =get_module_path(__snake_case ) lowerCamelCase_ =importlib.import_module(__snake_case ) return test_module def a_ ( __snake_case : Dict ) -> Tuple: """simple docstring""" lowerCamelCase_ =[] lowerCamelCase_ =get_test_module(__snake_case ) for attr in dir(__snake_case ): if attr.endswith('''ModelTester''' ): tester_classes.append(getattr(__snake_case , __snake_case ) ) # sort with class names return sorted(__snake_case , key=lambda __snake_case : x.__name__ ) def a_ ( __snake_case : List[Any] ) -> Optional[int]: """simple docstring""" lowerCamelCase_ =[] lowerCamelCase_ =get_test_module(__snake_case ) for attr in dir(__snake_case ): lowerCamelCase_ =getattr(__snake_case , __snake_case ) # (TF/Flax)ModelTesterMixin is also an attribute in specific model test module. Let's exclude them by checking # `all_model_classes` is not empty (which also excludes other special classes). lowerCamelCase_ =getattr(__snake_case , '''all_model_classes''' , [] ) if len(__snake_case ) > 0: test_classes.append(__snake_case ) # sort with class names return sorted(__snake_case , key=lambda __snake_case : x.__name__ ) def a_ ( __snake_case : List[Any] ) -> Optional[int]: """simple docstring""" lowerCamelCase_ =get_test_classes(__snake_case ) lowerCamelCase_ =set() for test_class in test_classes: model_classes.update(test_class.all_model_classes ) # sort with class names return sorted(__snake_case , key=lambda __snake_case : x.__name__ ) def a_ ( __snake_case : str ) -> str: """simple docstring""" lowerCamelCase_ =test_class() if hasattr(__snake_case , '''setUp''' ): test.setUp() lowerCamelCase_ =None if hasattr(__snake_case , '''model_tester''' ): # `(TF/Flax)ModelTesterMixin` has this attribute default to `None`. Let's skip this case. if test.model_tester is not None: lowerCamelCase_ =test.model_tester.__class__ return model_tester def a_ ( __snake_case : Dict , __snake_case : List[str] ) -> List[Any]: """simple docstring""" lowerCamelCase_ =get_test_classes(__snake_case ) lowerCamelCase_ =[] for test_class in test_classes: if model_class in test_class.all_model_classes: target_test_classes.append(__snake_case ) # sort with class names return sorted(__snake_case , key=lambda __snake_case : x.__name__ ) def a_ ( __snake_case : Optional[Any] , __snake_case : List[str] ) -> Any: """simple docstring""" lowerCamelCase_ =get_test_classes_for_model(__snake_case , __snake_case ) lowerCamelCase_ =[] for test_class in test_classes: lowerCamelCase_ =get_model_tester_from_test_class(__snake_case ) if tester_class is not None: tester_classes.append(__snake_case ) # sort with class names return sorted(__snake_case , key=lambda __snake_case : x.__name__ ) def a_ ( __snake_case : Tuple ) -> Tuple: """simple docstring""" lowerCamelCase_ =get_test_classes(__snake_case ) lowerCamelCase_ ={test_class: get_model_tester_from_test_class(__snake_case ) for test_class in test_classes} return test_tester_mapping def a_ ( __snake_case : Dict ) -> Optional[Any]: """simple docstring""" lowerCamelCase_ =get_model_classes(__snake_case ) lowerCamelCase_ ={ model_class: get_test_classes_for_model(__snake_case , __snake_case ) for model_class in model_classes } return model_test_mapping def a_ ( __snake_case : Optional[Any] ) -> Dict: """simple docstring""" lowerCamelCase_ =get_model_classes(__snake_case ) lowerCamelCase_ ={ model_class: get_tester_classes_for_model(__snake_case , __snake_case ) for model_class in model_classes } return model_to_tester_mapping def a_ ( __snake_case : List[str] ) -> List[Any]: """simple docstring""" if isinstance(__snake_case , __snake_case ): return o elif isinstance(__snake_case , __snake_case ): return o.__name__ elif isinstance(__snake_case , (list, tuple) ): return [to_json(__snake_case ) for x in o] elif isinstance(__snake_case , __snake_case ): return {to_json(__snake_case ): to_json(__snake_case ) for k, v in o.items()} else: return o
6
1
'''simple docstring''' from __future__ import annotations def a_ ( __snake_case : list[float] , __snake_case : list[float] ) -> float: """simple docstring""" lowerCamelCase_ =sorted(numsa + numsa ) lowerCamelCase_, lowerCamelCase_ =divmod(len(__snake_case ) , 2 ) if mod == 1: return all_numbers[div] else: return (all_numbers[div] + all_numbers[div - 1]) / 2 if __name__ == "__main__": import doctest doctest.testmod() a_ : Tuple = [float(x) for x in input("""Enter the elements of first array: """).split()] a_ : Union[str, Any] = [float(x) for x in input("""Enter the elements of second array: """).split()] print(F"""The median of two arrays is: {median_of_two_arrays(array_a, array_a)}""")
6
'''simple docstring''' from ..utils import DummyObject, requires_backends class __UpperCamelCase ( metaclass=lowerCamelCase__ ): lowercase : str =['speech'] def __init__( self, *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" requires_backends(self, ['''speech'''] ) class __UpperCamelCase ( metaclass=lowerCamelCase__ ): lowercase : Any =['speech'] def __init__( self, *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" requires_backends(self, ['''speech'''] )
6
1
'''simple docstring''' from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import convert_to_rgb, normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL a_ : str = logging.get_logger(__name__) class __UpperCamelCase ( lowerCamelCase__ ): lowercase : Union[str, Any] =['pixel_values'] def __init__( self, lowerCAmelCase = True, lowerCAmelCase = None, lowerCAmelCase = PILImageResampling.BICUBIC, lowerCAmelCase = True, lowerCAmelCase = 1 / 255, lowerCAmelCase = True, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = True, **lowerCAmelCase, ): """simple docstring""" super().__init__(**lowerCAmelCase ) lowerCamelCase_ =size if size is not None else {'''height''': 384, '''width''': 384} lowerCamelCase_ =get_size_dict(lowerCAmelCase, default_to_square=lowerCAmelCase ) lowerCamelCase_ =do_resize lowerCamelCase_ =size lowerCamelCase_ =resample lowerCamelCase_ =do_rescale lowerCamelCase_ =rescale_factor lowerCamelCase_ =do_normalize lowerCamelCase_ =image_mean if image_mean is not None else OPENAI_CLIP_MEAN lowerCamelCase_ =image_std if image_std is not None else OPENAI_CLIP_STD lowerCamelCase_ =do_convert_rgb def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase = PILImageResampling.BICUBIC, lowerCAmelCase = None, **lowerCAmelCase, ): """simple docstring""" lowerCamelCase_ =get_size_dict(lowerCAmelCase, default_to_square=lowerCAmelCase ) if "height" not in size or "width" not in size: raise ValueError(f'''The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}''' ) lowerCamelCase_ =(size['''height'''], size['''width''']) return resize(lowerCAmelCase, size=lowerCAmelCase, resample=lowerCAmelCase, data_format=lowerCAmelCase, **lowerCAmelCase ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase = None, **lowerCAmelCase, ): """simple docstring""" return rescale(lowerCAmelCase, scale=lowerCAmelCase, data_format=lowerCAmelCase, **lowerCAmelCase ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase = None, **lowerCAmelCase, ): """simple docstring""" return normalize(lowerCAmelCase, mean=lowerCAmelCase, std=lowerCAmelCase, data_format=lowerCAmelCase, **lowerCAmelCase ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = ChannelDimension.FIRST, **lowerCAmelCase, ): """simple docstring""" lowerCamelCase_ =do_resize if do_resize is not None else self.do_resize lowerCamelCase_ =resample if resample is not None else self.resample lowerCamelCase_ =do_rescale if do_rescale is not None else self.do_rescale lowerCamelCase_ =rescale_factor if rescale_factor is not None else self.rescale_factor lowerCamelCase_ =do_normalize if do_normalize is not None else self.do_normalize lowerCamelCase_ =image_mean if image_mean is not None else self.image_mean lowerCamelCase_ =image_std if image_std is not None else self.image_std lowerCamelCase_ =do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb lowerCamelCase_ =size if size is not None else self.size lowerCamelCase_ =get_size_dict(lowerCAmelCase, default_to_square=lowerCAmelCase ) lowerCamelCase_ =make_list_of_images(lowerCAmelCase ) 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.''' ) 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_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.''' ) # PIL RGBA images are converted to RGB if do_convert_rgb: lowerCamelCase_ =[convert_to_rgb(lowerCAmelCase ) for image in images] # All transformations expect numpy arrays. lowerCamelCase_ =[to_numpy_array(lowerCAmelCase ) for image in images] if do_resize: lowerCamelCase_ =[self.resize(image=lowerCAmelCase, size=lowerCAmelCase, resample=lowerCAmelCase ) for image in images] if do_rescale: lowerCamelCase_ =[self.rescale(image=lowerCAmelCase, scale=lowerCAmelCase ) for image in images] if do_normalize: lowerCamelCase_ =[self.normalize(image=lowerCAmelCase, mean=lowerCAmelCase, std=lowerCAmelCase ) for image in images] lowerCamelCase_ =[to_channel_dimension_format(lowerCAmelCase, lowerCAmelCase ) for image in images] lowerCamelCase_ =BatchFeature(data={'''pixel_values''': images}, tensor_type=lowerCAmelCase ) return encoded_outputs
6
'''simple docstring''' import re import warnings from contextlib import contextmanager from ...processing_utils import ProcessorMixin class __UpperCamelCase ( lowerCamelCase__ ): lowercase : List[str] =['image_processor', 'tokenizer'] lowercase : Optional[int] ='AutoImageProcessor' lowercase : List[str] ='AutoTokenizer' def __init__( self, lowerCAmelCase=None, lowerCAmelCase=None, **lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =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, ) lowerCamelCase_ =kwargs.pop('''feature_extractor''' ) lowerCamelCase_ =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 ) lowerCamelCase_ =self.image_processor lowerCamelCase_ =False def __call__( self, *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" if self._in_target_context_manager: return self.current_processor(*lowerCAmelCase, **lowerCAmelCase ) lowerCamelCase_ =kwargs.pop('''images''', lowerCAmelCase ) lowerCamelCase_ =kwargs.pop('''text''', lowerCAmelCase ) if len(lowerCAmelCase ) > 0: lowerCamelCase_ =args[0] lowerCamelCase_ =args[1:] if images is None and text is None: raise ValueError('''You need to specify either an `images` or `text` input to process.''' ) if images is not None: lowerCamelCase_ =self.image_processor(lowerCAmelCase, *lowerCAmelCase, **lowerCAmelCase ) if text is not None: lowerCamelCase_ =self.tokenizer(lowerCAmelCase, **lowerCAmelCase ) if text is None: return inputs elif images is None: return encodings else: lowerCamelCase_ =encodings['''input_ids'''] return inputs def lowercase__ ( self, *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" return self.tokenizer.batch_decode(*lowerCAmelCase, **lowerCAmelCase ) def lowercase__ ( self, *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" return self.tokenizer.decode(*lowerCAmelCase, **lowerCAmelCase ) @contextmanager def lowercase__ ( self ): """simple docstring""" warnings.warn( '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your ''' '''labels by using the argument `text` of the regular `__call__` method (either in the same call as ''' '''your images inputs, or in a separate call.''' ) lowerCamelCase_ =True lowerCamelCase_ =self.tokenizer yield lowerCamelCase_ =self.image_processor lowerCamelCase_ =False def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase=False, lowerCAmelCase=None ): """simple docstring""" if added_vocab is None: lowerCamelCase_ =self.tokenizer.get_added_vocab() lowerCamelCase_ ={} while tokens: lowerCamelCase_ =re.search(R'''<s_(.*?)>''', lowerCAmelCase, re.IGNORECASE ) if start_token is None: break lowerCamelCase_ =start_token.group(1 ) lowerCamelCase_ =re.search(Rf'''</s_{key}>''', lowerCAmelCase, re.IGNORECASE ) lowerCamelCase_ =start_token.group() if end_token is None: lowerCamelCase_ =tokens.replace(lowerCAmelCase, '''''' ) else: lowerCamelCase_ =end_token.group() lowerCamelCase_ =re.escape(lowerCAmelCase ) lowerCamelCase_ =re.escape(lowerCAmelCase ) lowerCamelCase_ =re.search(f'''{start_token_escaped}(.*?){end_token_escaped}''', lowerCAmelCase, re.IGNORECASE ) if content is not None: lowerCamelCase_ =content.group(1 ).strip() if r"<s_" in content and r"</s_" in content: # non-leaf node lowerCamelCase_ =self.tokenajson(lowerCAmelCase, is_inner_value=lowerCAmelCase, added_vocab=lowerCAmelCase ) if value: if len(lowerCAmelCase ) == 1: lowerCamelCase_ =value[0] lowerCamelCase_ =value else: # leaf nodes lowerCamelCase_ =[] for leaf in content.split(R'''<sep/>''' ): lowerCamelCase_ =leaf.strip() if leaf in added_vocab and leaf[0] == "<" and leaf[-2:] == "/>": lowerCamelCase_ =leaf[1:-2] # for categorical special tokens output[key].append(lowerCAmelCase ) if len(output[key] ) == 1: lowerCamelCase_ =output[key][0] lowerCamelCase_ =tokens[tokens.find(lowerCAmelCase ) + len(lowerCAmelCase ) :].strip() if tokens[:6] == r"<sep/>": # non-leaf nodes return [output] + self.tokenajson(tokens[6:], is_inner_value=lowerCAmelCase, added_vocab=lowerCAmelCase ) if len(lowerCAmelCase ): return [output] if is_inner_value else output else: return [] if is_inner_value else {"text_sequence": tokens} @property def lowercase__ ( self ): """simple docstring""" warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''', lowerCAmelCase, ) return self.image_processor_class @property def lowercase__ ( self ): """simple docstring""" warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''', lowerCAmelCase, ) return self.image_processor
6
1
'''simple docstring''' from sklearn.metrics import matthews_corrcoef import datasets a_ : Tuple = """ Compute the Matthews correlation coefficient (MCC) The Matthews correlation coefficient is used in machine learning as a measure of the quality of binary and multiclass classifications. It takes into account true and false positives and negatives and is generally regarded as a balanced measure which can be used even if the classes are of very different sizes. The MCC is in essence a correlation coefficient value between -1 and +1. A coefficient of +1 represents a perfect prediction, 0 an average random prediction and -1 an inverse prediction. The statistic is also known as the phi coefficient. [source: Wikipedia] """ a_ : Any = """ Args: predictions (list of int): Predicted labels, as returned by a model. references (list of int): Ground truth labels. sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`. Returns: matthews_correlation (dict containing float): Matthews correlation. Examples: Example 1, a basic example with only predictions and references as inputs: >>> matthews_metric = datasets.load_metric(\"matthews_correlation\") >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2], ... predictions=[1, 2, 2, 0, 3, 3]) >>> print(round(results['matthews_correlation'], 2)) 0.54 Example 2, the same example as above, but also including sample weights: >>> matthews_metric = datasets.load_metric(\"matthews_correlation\") >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2], ... predictions=[1, 2, 2, 0, 3, 3], ... sample_weight=[0.5, 3, 1, 1, 1, 2]) >>> print(round(results['matthews_correlation'], 2)) 0.1 Example 3, the same example as above, but with sample weights that cause a negative correlation: >>> matthews_metric = datasets.load_metric(\"matthews_correlation\") >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2], ... predictions=[1, 2, 2, 0, 3, 3], ... sample_weight=[0.5, 1, 0, 0, 0, 1]) >>> print(round(results['matthews_correlation'], 2)) -0.25 """ a_ : Dict = """\ @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} } """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __UpperCamelCase ( datasets.Metric ): def lowercase__ ( self ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION, citation=_CITATION, inputs_description=_KWARGS_DESCRIPTION, features=datasets.Features( { '''predictions''': datasets.Value('''int32''' ), '''references''': datasets.Value('''int32''' ), } ), reference_urls=[ '''https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html''' ], ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase=None ): """simple docstring""" return { "matthews_correlation": float(matthews_corrcoef(lowerCAmelCase, lowerCAmelCase, sample_weight=lowerCAmelCase ) ), }
6
'''simple docstring''' import gc import random import unittest import numpy as np import torch from transformers import CLIPImageProcessor, CLIPVisionConfig, CLIPVisionModel from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEImgaImgPipeline from diffusers.pipelines.shap_e import ShapERenderer from diffusers.utils import floats_tensor, load_image, load_numpy, slow from diffusers.utils.testing_utils import require_torch_gpu, torch_device from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference class __UpperCamelCase ( lowerCamelCase__ , unittest.TestCase ): lowercase : Union[str, Any] =ShapEImgaImgPipeline lowercase : Dict =['image'] lowercase : str =['image'] lowercase : int =[ 'num_images_per_prompt', 'num_inference_steps', 'generator', 'latents', 'guidance_scale', 'frame_size', 'output_type', 'return_dict', ] lowercase : int =False @property def lowercase__ ( self ): """simple docstring""" return 32 @property def lowercase__ ( self ): """simple docstring""" return 32 @property def lowercase__ ( self ): """simple docstring""" return self.time_input_dim * 4 @property def lowercase__ ( self ): """simple docstring""" return 8 @property def lowercase__ ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCamelCase_ =CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size, image_size=64, projection_dim=self.text_embedder_hidden_size, intermediate_size=37, num_attention_heads=4, num_channels=3, num_hidden_layers=5, patch_size=1, ) lowerCamelCase_ =CLIPVisionModel(lowerCAmelCase ) return model @property def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =CLIPImageProcessor( crop_size=224, do_center_crop=lowerCAmelCase, do_normalize=lowerCAmelCase, do_resize=lowerCAmelCase, image_mean=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3], image_std=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1], resample=3, size=224, ) return image_processor @property def lowercase__ ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCamelCase_ ={ '''num_attention_heads''': 2, '''attention_head_dim''': 16, '''embedding_dim''': self.time_input_dim, '''num_embeddings''': 32, '''embedding_proj_dim''': self.text_embedder_hidden_size, '''time_embed_dim''': self.time_embed_dim, '''num_layers''': 1, '''clip_embed_dim''': self.time_input_dim * 2, '''additional_embeddings''': 0, '''time_embed_act_fn''': '''gelu''', '''norm_in_type''': '''layer''', '''embedding_proj_norm_type''': '''layer''', '''encoder_hid_proj_type''': None, '''added_emb_type''': None, } lowerCamelCase_ =PriorTransformer(**lowerCAmelCase ) return model @property def lowercase__ ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCamelCase_ ={ '''param_shapes''': ( (self.renderer_dim, 93), (self.renderer_dim, 8), (self.renderer_dim, 8), (self.renderer_dim, 8), ), '''d_latent''': self.time_input_dim, '''d_hidden''': self.renderer_dim, '''n_output''': 12, '''background''': ( 0.1, 0.1, 0.1, ), } lowerCamelCase_ =ShapERenderer(**lowerCAmelCase ) return model def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.dummy_prior lowerCamelCase_ =self.dummy_image_encoder lowerCamelCase_ =self.dummy_image_processor lowerCamelCase_ =self.dummy_renderer lowerCamelCase_ =HeunDiscreteScheduler( beta_schedule='''exp''', num_train_timesteps=1_024, prediction_type='''sample''', use_karras_sigmas=lowerCAmelCase, clip_sample=lowerCAmelCase, clip_sample_range=1.0, ) lowerCamelCase_ ={ '''prior''': prior, '''image_encoder''': image_encoder, '''image_processor''': image_processor, '''renderer''': renderer, '''scheduler''': scheduler, } return components def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase=0 ): """simple docstring""" lowerCamelCase_ =floats_tensor((1, 3, 64, 64), rng=random.Random(lowerCAmelCase ) ).to(lowerCAmelCase ) if str(lowerCAmelCase ).startswith('''mps''' ): lowerCamelCase_ =torch.manual_seed(lowerCAmelCase ) else: lowerCamelCase_ =torch.Generator(device=lowerCAmelCase ).manual_seed(lowerCAmelCase ) lowerCamelCase_ ={ '''image''': input_image, '''generator''': generator, '''num_inference_steps''': 1, '''frame_size''': 32, '''output_type''': '''np''', } return inputs def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ ='''cpu''' lowerCamelCase_ =self.get_dummy_components() lowerCamelCase_ =self.pipeline_class(**lowerCAmelCase ) lowerCamelCase_ =pipe.to(lowerCAmelCase ) pipe.set_progress_bar_config(disable=lowerCAmelCase ) lowerCamelCase_ =pipe(**self.get_dummy_inputs(lowerCAmelCase ) ) lowerCamelCase_ =output.images[0] lowerCamelCase_ =image[0, -3:, -3:, -1] assert image.shape == (20, 32, 32, 3) lowerCamelCase_ =np.array( [ 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def lowercase__ ( self ): """simple docstring""" self._test_inference_batch_consistent(batch_sizes=[1, 2] ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =torch_device == '''cpu''' lowerCamelCase_ =True self._test_inference_batch_single_identical( batch_size=2, test_max_difference=lowerCAmelCase, relax_max_difference=lowerCAmelCase, ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.get_dummy_components() lowerCamelCase_ =self.pipeline_class(**lowerCAmelCase ) lowerCamelCase_ =pipe.to(lowerCAmelCase ) pipe.set_progress_bar_config(disable=lowerCAmelCase ) lowerCamelCase_ =1 lowerCamelCase_ =2 lowerCamelCase_ =self.get_dummy_inputs(lowerCAmelCase ) for key in inputs.keys(): if key in self.batch_params: lowerCamelCase_ =batch_size * [inputs[key]] lowerCamelCase_ =pipe(**lowerCAmelCase, num_images_per_prompt=lowerCAmelCase )[0] assert images.shape[0] == batch_size * num_images_per_prompt @slow @require_torch_gpu class __UpperCamelCase ( unittest.TestCase ): def lowercase__ ( self ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/shap_e/corgi.png''' ) lowerCamelCase_ =load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/shap_e/test_shap_e_img2img_out.npy''' ) lowerCamelCase_ =ShapEImgaImgPipeline.from_pretrained('''openai/shap-e-img2img''' ) lowerCamelCase_ =pipe.to(lowerCAmelCase ) pipe.set_progress_bar_config(disable=lowerCAmelCase ) lowerCamelCase_ =torch.Generator(device=lowerCAmelCase ).manual_seed(0 ) lowerCamelCase_ =pipe( lowerCAmelCase, generator=lowerCAmelCase, guidance_scale=3.0, num_inference_steps=64, frame_size=64, output_type='''np''', ).images[0] assert images.shape == (20, 64, 64, 3) assert_mean_pixel_difference(lowerCAmelCase, lowerCAmelCase )
6
1
'''simple docstring''' import pytest import datasets.config from datasets.utils.info_utils import is_small_dataset @pytest.mark.parametrize('''dataset_size''' , [None, 400 * 2**20, 600 * 2**20] ) @pytest.mark.parametrize('''input_in_memory_max_size''' , ['''default''', 0, 100 * 2**20, 900 * 2**20] ) def a_ ( __snake_case : Optional[Any] , __snake_case : List[Any] , __snake_case : List[str] ) -> int: """simple docstring""" if input_in_memory_max_size != "default": monkeypatch.setattr(datasets.config , '''IN_MEMORY_MAX_SIZE''' , __snake_case ) lowerCamelCase_ =datasets.config.IN_MEMORY_MAX_SIZE if input_in_memory_max_size == "default": assert in_memory_max_size == 0 else: assert in_memory_max_size == input_in_memory_max_size if dataset_size and in_memory_max_size: lowerCamelCase_ =dataset_size < in_memory_max_size else: lowerCamelCase_ =False lowerCamelCase_ =is_small_dataset(__snake_case ) assert result == expected
6
'''simple docstring''' from itertools import product def a_ ( __snake_case : int , __snake_case : int ) -> list[int]: """simple docstring""" lowerCamelCase_ =sides_number lowerCamelCase_ =max_face_number * dice_number lowerCamelCase_ =[0] * (max_total + 1) lowerCamelCase_ =1 lowerCamelCase_ =range(__snake_case , max_face_number + 1 ) for dice_numbers in product(__snake_case , repeat=__snake_case ): lowerCamelCase_ =sum(__snake_case ) totals_frequencies[total] += 1 return totals_frequencies def a_ ( ) -> float: """simple docstring""" lowerCamelCase_ =total_frequency_distribution( sides_number=4 , dice_number=9 ) lowerCamelCase_ =total_frequency_distribution( sides_number=6 , dice_number=6 ) lowerCamelCase_ =0 lowerCamelCase_ =9 lowerCamelCase_ =4 * 9 lowerCamelCase_ =6 for peter_total in range(__snake_case , max_peter_total + 1 ): peter_wins_count += peter_totals_frequencies[peter_total] * sum( colin_totals_frequencies[min_colin_total:peter_total] ) lowerCamelCase_ =(4**9) * (6**6) lowerCamelCase_ =peter_wins_count / total_games_number lowerCamelCase_ =round(__snake_case , ndigits=7 ) return rounded_peter_win_probability if __name__ == "__main__": print(F"""{solution() = }""")
6
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) a_ : Optional[int] = { """configuration_blenderbot""": [ """BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """BlenderbotConfig""", """BlenderbotOnnxConfig""", ], """tokenization_blenderbot""": ["""BlenderbotTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ : List[Any] = ["""BlenderbotTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ : Tuple = [ """BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST""", """BlenderbotForCausalLM""", """BlenderbotForConditionalGeneration""", """BlenderbotModel""", """BlenderbotPreTrainedModel""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ : List[Any] = [ """TFBlenderbotForConditionalGeneration""", """TFBlenderbotModel""", """TFBlenderbotPreTrainedModel""", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ : str = [ """FlaxBlenderbotForConditionalGeneration""", """FlaxBlenderbotModel""", """FlaxBlenderbotPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_blenderbot import ( BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotConfig, BlenderbotOnnxConfig, ) from .tokenization_blenderbot import BlenderbotTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_blenderbot_fast import BlenderbotTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blenderbot import ( BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST, BlenderbotForCausalLM, BlenderbotForConditionalGeneration, BlenderbotModel, BlenderbotPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blenderbot import ( TFBlenderbotForConditionalGeneration, TFBlenderbotModel, TFBlenderbotPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, FlaxBlenderbotPreTrainedModel, ) else: import sys a_ : int = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
6
'''simple docstring''' import os from typing import Dict, List, Tuple, TypeVar, Union a_ : Tuple = TypeVar("""T""") a_ : Dict = Union[List[T], Tuple[T, ...]] a_ : int = Union[T, List[T], Dict[str, T]] a_ : Optional[Any] = Union[str, bytes, os.PathLike]
6
1
'''simple docstring''' import argparse import torch from ...utils import logging from . import AlbertConfig, AlbertForPreTraining, load_tf_weights_in_albert logging.set_verbosity_info() def a_ ( __snake_case : Optional[int] , __snake_case : Optional[int] , __snake_case : Union[str, Any] ) -> Optional[int]: """simple docstring""" # Initialise PyTorch model lowerCamelCase_ =AlbertConfig.from_json_file(__snake_case ) print(F'''Building PyTorch model from configuration: {config}''' ) lowerCamelCase_ =AlbertForPreTraining(__snake_case ) # Load weights from tf checkpoint load_tf_weights_in_albert(__snake_case , __snake_case , __snake_case ) # Save pytorch-model print(F'''Save PyTorch model to {pytorch_dump_path}''' ) torch.save(model.state_dict() , __snake_case ) if __name__ == "__main__": a_ : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument( """--tf_checkpoint_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path.""" ) parser.add_argument( """--albert_config_file""", default=None, type=str, required=True, help=( """The config json file corresponding to the pre-trained ALBERT model. \n""" """This specifies the model architecture.""" ), ) parser.add_argument( """--pytorch_dump_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model.""" ) a_ : List[Any] = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.albert_config_file, args.pytorch_dump_path)
6
'''simple docstring''' import math import random from typing import Any from .hill_climbing import SearchProblem def a_ ( __snake_case : str , __snake_case : bool = True , __snake_case : float = math.inf , __snake_case : float = -math.inf , __snake_case : float = math.inf , __snake_case : float = -math.inf , __snake_case : bool = False , __snake_case : float = 100 , __snake_case : float = 0.0_1 , __snake_case : float = 1 , ) -> Any: """simple docstring""" lowerCamelCase_ =False lowerCamelCase_ =search_prob lowerCamelCase_ =start_temperate lowerCamelCase_ =[] lowerCamelCase_ =0 lowerCamelCase_ =None while not search_end: lowerCamelCase_ =current_state.score() if best_state is None or current_score > best_state.score(): lowerCamelCase_ =current_state scores.append(__snake_case ) iterations += 1 lowerCamelCase_ =None lowerCamelCase_ =current_state.get_neighbors() while ( next_state is None and neighbors ): # till we do not find a neighbor that we can move to lowerCamelCase_ =random.randint(0 , len(__snake_case ) - 1 ) # picking a random neighbor lowerCamelCase_ =neighbors.pop(__snake_case ) lowerCamelCase_ =picked_neighbor.score() - current_score if ( picked_neighbor.x > max_x or picked_neighbor.x < min_x or picked_neighbor.y > max_y or picked_neighbor.y < min_y ): continue # neighbor outside our bounds if not find_max: lowerCamelCase_ =change * -1 # in case we are finding minimum if change > 0: # improves the solution lowerCamelCase_ =picked_neighbor else: lowerCamelCase_ =(math.e) ** ( change / current_temp ) # probability generation function if random.random() < probability: # random number within probability lowerCamelCase_ =picked_neighbor lowerCamelCase_ =current_temp - (current_temp * rate_of_decrease) if current_temp < threshold_temp or next_state is None: # temperature below threshold, or could not find a suitable neighbor lowerCamelCase_ =True else: lowerCamelCase_ =next_state if visualization: from matplotlib import pyplot as plt plt.plot(range(__snake_case ) , __snake_case ) plt.xlabel('''Iterations''' ) plt.ylabel('''Function values''' ) plt.show() return best_state if __name__ == "__main__": def a_ ( __snake_case : List[str] , __snake_case : Optional[int] ) -> str: """simple docstring""" return (x**2) + (y**2) # starting the problem with initial coordinates (12, 47) a_ : str = SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_fa) a_ : Optional[int] = simulated_annealing( prob, find_max=False, max_x=1_00, min_x=5, max_y=50, min_y=-5, visualization=True ) print( """The minimum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 """ F"""and 50 > y > - 5 found via hill climbing: {local_min.score()}""" ) # starting the problem with initial coordinates (12, 47) a_ : str = SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_fa) a_ : List[str] = simulated_annealing( prob, find_max=True, max_x=1_00, min_x=5, max_y=50, min_y=-5, visualization=True ) print( """The maximum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 """ F"""and 50 > y > - 5 found via hill climbing: {local_min.score()}""" ) def a_ ( __snake_case : Dict , __snake_case : Optional[Any] ) -> Union[str, Any]: """simple docstring""" return (3 * x**2) - (6 * y) a_ : Tuple = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa) a_ : Optional[Any] = simulated_annealing(prob, find_max=False, visualization=True) print( """The minimum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: """ F"""{local_min.score()}""" ) a_ : Dict = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa) a_ : Optional[int] = simulated_annealing(prob, find_max=True, visualization=True) print( """The maximum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: """ F"""{local_min.score()}""" )
6
1
'''simple docstring''' import logging import os import sys from dataclasses import dataclass, field from typing import Optional import torch from datasets import load_dataset from torchvision.transforms import Compose, Lambda, Normalize, RandomHorizontalFlip, RandomResizedCrop, ToTensor from torchvision.transforms.functional import InterpolationMode import transformers from transformers import ( HfArgumentParser, Trainer, TrainingArguments, ViTImageProcessor, ViTMAEConfig, ViTMAEForPreTraining, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version a_ : List[str] = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("""4.31.0""") require_version("""datasets>=1.8.0""", """To fix: pip install -r examples/pytorch/image-pretraining/requirements.txt""") @dataclass class __UpperCamelCase : lowercase : Optional[str] =field( default='cifar10' , metadata={'help': 'Name of a dataset from the datasets package'} ) lowercase : Optional[str] =field( default=lowerCamelCase__ , metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'} ) lowercase : Optional[str] =field( default=lowerCamelCase__ , metadata={'help': 'The column name of the images in the files.'} ) lowercase : Optional[str] =field(default=lowerCamelCase__ , metadata={'help': 'A folder containing the training data.'} ) lowercase : Optional[str] =field(default=lowerCamelCase__ , metadata={'help': 'A folder containing the validation data.'} ) lowercase : Optional[float] =field( default=0.15 , metadata={'help': 'Percent to split off of train for validation.'} ) lowercase : Optional[int] =field( default=lowerCamelCase__ , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of training examples to this ' 'value if set.' ) } , ) lowercase : Optional[int] =field( default=lowerCamelCase__ , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of evaluation examples to this ' 'value if set.' ) } , ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ ={} if self.train_dir is not None: lowerCamelCase_ =self.train_dir if self.validation_dir is not None: lowerCamelCase_ =self.validation_dir lowerCamelCase_ =data_files if data_files else None @dataclass class __UpperCamelCase : lowercase : str =field( default=lowerCamelCase__ , metadata={ 'help': ( 'The model checkpoint for weights initialization.Don\'t set if you want to train a model from scratch.' ) } , ) lowercase : Optional[str] =field( default=lowerCamelCase__ , metadata={'help': 'Pretrained config name or path if not the same as model_name_or_path'} ) lowercase : Optional[str] =field( default=lowerCamelCase__ , metadata={ 'help': ( 'Override some existing default config settings when a model is trained from scratch. Example: ' 'n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index' ) } , ) lowercase : Optional[str] =field( default=lowerCamelCase__ , metadata={'help': 'Where do you want to store the pretrained models downloaded from s3'} ) lowercase : str =field( default='main' , metadata={'help': 'The specific model version to use (can be a branch name, tag name or commit id).'} , ) lowercase : str =field(default=lowerCamelCase__ , metadata={'help': 'Name or path of preprocessor config.'} ) lowercase : bool =field( default=lowerCamelCase__ , metadata={ 'help': ( 'Will use the token generated when running `huggingface-cli login` (necessary to use this script ' 'with private models).' ) } , ) lowercase : float =field( default=0.75 , metadata={'help': 'The ratio of the number of masked tokens in the input sequence.'} ) lowercase : bool =field( default=lowerCamelCase__ , metadata={'help': 'Whether or not to train with normalized pixel values as target.'} ) @dataclass class __UpperCamelCase ( lowerCamelCase__ ): lowercase : float =field( default=1E-3 , metadata={'help': 'Base learning rate: absolute_lr = base_lr * total_batch_size / 256.'} ) def a_ ( __snake_case : List[str] ) -> int: """simple docstring""" lowerCamelCase_ =torch.stack([example['''pixel_values'''] for example in examples] ) return {"pixel_values": pixel_values} def a_ ( ) -> str: """simple docstring""" # 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. lowerCamelCase_ =HfArgumentParser((ModelArguments, DataTrainingArguments, CustomTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('''.json''' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ =parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ =parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry('''run_mae''' , __snake_case , __snake_case ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' , datefmt='''%m/%d/%Y %H:%M:%S''' , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() lowerCamelCase_ =training_args.get_process_log_level() logger.setLevel(__snake_case ) transformers.utils.logging.set_verbosity(__snake_case ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F'''Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}''' + F'''distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}''' ) logger.info(F'''Training/evaluation parameters {training_args}''' ) # Detecting last checkpoint. lowerCamelCase_ =None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: lowerCamelCase_ =get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F'''Output directory ({training_args.output_dir}) already exists and is not empty. ''' '''Use --overwrite_output_dir to overcome.''' ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F'''Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change ''' '''the `--output_dir` or add `--overwrite_output_dir` to train from scratch.''' ) # Initialize our dataset. lowerCamelCase_ =load_dataset( data_args.dataset_name , data_args.dataset_config_name , data_files=data_args.data_files , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) # If we don't have a validation split, split off a percentage of train as validation. lowerCamelCase_ =None if '''validation''' in ds.keys() else data_args.train_val_split if isinstance(data_args.train_val_split , __snake_case ) and data_args.train_val_split > 0.0: lowerCamelCase_ =ds['''train'''].train_test_split(data_args.train_val_split ) lowerCamelCase_ =split['''train'''] lowerCamelCase_ =split['''test'''] # Load pretrained model and image processor # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. lowerCamelCase_ ={ '''cache_dir''': model_args.cache_dir, '''revision''': model_args.model_revision, '''use_auth_token''': True if model_args.use_auth_token else None, } if model_args.config_name: lowerCamelCase_ =ViTMAEConfig.from_pretrained(model_args.config_name , **__snake_case ) elif model_args.model_name_or_path: lowerCamelCase_ =ViTMAEConfig.from_pretrained(model_args.model_name_or_path , **__snake_case ) else: lowerCamelCase_ =ViTMAEConfig() logger.warning('''You are instantiating a new config instance from scratch.''' ) if model_args.config_overrides is not None: logger.info(F'''Overriding config: {model_args.config_overrides}''' ) config.update_from_string(model_args.config_overrides ) logger.info(F'''New config: {config}''' ) # adapt config config.update( { '''mask_ratio''': model_args.mask_ratio, '''norm_pix_loss''': model_args.norm_pix_loss, } ) # create image processor if model_args.image_processor_name: lowerCamelCase_ =ViTImageProcessor.from_pretrained(model_args.image_processor_name , **__snake_case ) elif model_args.model_name_or_path: lowerCamelCase_ =ViTImageProcessor.from_pretrained(model_args.model_name_or_path , **__snake_case ) else: lowerCamelCase_ =ViTImageProcessor() # create model if model_args.model_name_or_path: lowerCamelCase_ =ViTMAEForPreTraining.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=__snake_case , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info('''Training new model from scratch''' ) lowerCamelCase_ =ViTMAEForPreTraining(__snake_case ) if training_args.do_train: lowerCamelCase_ =ds['''train'''].column_names else: lowerCamelCase_ =ds['''validation'''].column_names if data_args.image_column_name is not None: lowerCamelCase_ =data_args.image_column_name elif "image" in column_names: lowerCamelCase_ ='''image''' elif "img" in column_names: lowerCamelCase_ ='''img''' else: lowerCamelCase_ =column_names[0] # transformations as done in original MAE paper # source: https://github.com/facebookresearch/mae/blob/main/main_pretrain.py if "shortest_edge" in image_processor.size: lowerCamelCase_ =image_processor.size['''shortest_edge'''] else: lowerCamelCase_ =(image_processor.size['''height'''], image_processor.size['''width''']) lowerCamelCase_ =Compose( [ Lambda(lambda __snake_case : img.convert('''RGB''' ) if img.mode != "RGB" else img ), RandomResizedCrop(__snake_case , scale=(0.2, 1.0) , interpolation=InterpolationMode.BICUBIC ), RandomHorizontalFlip(), ToTensor(), Normalize(mean=image_processor.image_mean , std=image_processor.image_std ), ] ) def preprocess_images(__snake_case : List[Any] ): lowerCamelCase_ =[transforms(__snake_case ) for image in examples[image_column_name]] return examples if training_args.do_train: if "train" not in ds: raise ValueError('''--do_train requires a train dataset''' ) if data_args.max_train_samples is not None: lowerCamelCase_ =ds['''train'''].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) # Set the training transforms ds["train"].set_transform(__snake_case ) if training_args.do_eval: if "validation" not in ds: raise ValueError('''--do_eval requires a validation dataset''' ) if data_args.max_eval_samples is not None: lowerCamelCase_ =( ds['''validation'''].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms ds["validation"].set_transform(__snake_case ) # Compute absolute learning rate lowerCamelCase_ =( training_args.train_batch_size * training_args.gradient_accumulation_steps * training_args.world_size ) if training_args.base_learning_rate is not None: lowerCamelCase_ =training_args.base_learning_rate * total_train_batch_size / 256 # Initialize our trainer lowerCamelCase_ =Trainer( model=__snake_case , args=__snake_case , train_dataset=ds['''train'''] if training_args.do_train else None , eval_dataset=ds['''validation'''] if training_args.do_eval else None , tokenizer=__snake_case , data_collator=__snake_case , ) # Training if training_args.do_train: lowerCamelCase_ =None if training_args.resume_from_checkpoint is not None: lowerCamelCase_ =training_args.resume_from_checkpoint elif last_checkpoint is not None: lowerCamelCase_ =last_checkpoint lowerCamelCase_ =trainer.train(resume_from_checkpoint=__snake_case ) trainer.save_model() trainer.log_metrics('''train''' , train_result.metrics ) trainer.save_metrics('''train''' , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: lowerCamelCase_ =trainer.evaluate() trainer.log_metrics('''eval''' , __snake_case ) trainer.save_metrics('''eval''' , __snake_case ) # Write model card and (optionally) push to hub lowerCamelCase_ ={ '''tasks''': '''masked-auto-encoding''', '''dataset''': data_args.dataset_name, '''tags''': ['''masked-auto-encoding'''], } if training_args.push_to_hub: trainer.push_to_hub(**__snake_case ) else: trainer.create_model_card(**__snake_case ) def a_ ( __snake_case : Optional[int] ) -> List[Any]: """simple docstring""" # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
6
'''simple docstring''' import argparse import json import os import torch from torch import nn from transformers import NllbMoeConfig, NllbMoeModel from transformers.modeling_utils import dtype_byte_size from transformers.utils import WEIGHTS_INDEX_NAME, WEIGHTS_NAME def a_ ( __snake_case : Optional[int] ) -> List[str]: """simple docstring""" lowerCamelCase_ =[ '''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(__snake_case , __snake_case ) def a_ ( __snake_case : List[Any] ) -> int: """simple docstring""" lowerCamelCase_, lowerCamelCase_ =emb.weight.shape lowerCamelCase_ =nn.Linear(__snake_case , __snake_case , bias=__snake_case ) lowerCamelCase_ =emb.weight.data return lin_layer def a_ ( __snake_case : Union[str, Any] , __snake_case : Tuple=None ) -> Dict: """simple docstring""" lowerCamelCase_ ={} for old_key in state_dict.keys(): lowerCamelCase_ =old_key if "moe_layer.experts." in key: if expert_idx is not None: lowerCamelCase_ =key.replace('''moe_layer.experts.0''' , F'''ffn.experts.expert_{expert_idx}''' ) else: lowerCamelCase_ =key.replace('''moe_layer.experts.''' , '''ffn.experts.expert_''' ) if "gate" in key: lowerCamelCase_ =key.replace('''.moe_layer.gate.wg''' , '''.ffn.router.classifier''' ) if "fc2" and "experts" not in key: lowerCamelCase_ =key.replace('''.fc2.''' , '''.ffn.fc2.''' ) if "fc1" and "experts" not in key: lowerCamelCase_ =key.replace('''.fc1.''' , '''.ffn.fc1.''' ) if ".encoder_attn." in key: lowerCamelCase_ =key.replace('''.encoder_attn.''' , '''.cross_attention.''' ) if "encoder_attn_layer_norm" in key: lowerCamelCase_ =key.replace('''encoder_attn_layer_norm''' , '''cross_attention_layer_norm''' ) if "final_layer_norm" in key: lowerCamelCase_ =key.replace('''final_layer_norm''' , '''ff_layer_norm''' ) lowerCamelCase_ =state_dict[old_key] return new_dict def a_ ( __snake_case : Optional[Any] , __snake_case : Any , __snake_case : Optional[int] , __snake_case : Tuple , __snake_case : str = WEIGHTS_NAME ) -> Dict: """simple docstring""" lowerCamelCase_ =[] lowerCamelCase_ =0 os.makedirs(__snake_case , exist_ok=__snake_case ) for expert in range(__snake_case ): lowerCamelCase_ =switch_checkpoint_path + F'''-rank-{expert}.pt''' if os.path.isfile(__snake_case ): lowerCamelCase_ =torch.load(__snake_case )['''model'''] remove_ignore_keys_(__snake_case ) lowerCamelCase_ =rename_fairseq_keys(__snake_case , __snake_case ) lowerCamelCase_ =os.path.join( __snake_case , weights_name.replace('''.bin''' , F'''-{len(__snake_case )+1:05d}-of-???.bin''' ) ) torch.save(__snake_case , __snake_case ) 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(__snake_case )[0]].dtype ) # Add the last block lowerCamelCase_ =os.path.join(__snake_case , weights_name.replace('''.bin''' , F'''-{len(__snake_case )+1:05d}-of-???.bin''' ) ) lowerCamelCase_ =torch.load(switch_checkpoint_path + '''-shared.pt''' )['''model'''] remove_ignore_keys_(__snake_case ) lowerCamelCase_ =rename_fairseq_keys(__snake_case , __snake_case ) lowerCamelCase_ =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(__snake_case ) == 1: lowerCamelCase_ =os.path.join(__snake_case , __snake_case ) torch.save(__snake_case , __snake_case ) return {weights_name: sharded_state_dicts[0]}, None else: torch.save(__snake_case , __snake_case ) # Otherwise, let's build the index lowerCamelCase_ ={} for idx, shard in enumerate(__snake_case ): lowerCamelCase_ =weights_name.replace('''.bin''' , F'''-{idx+1:05d}-of-{len(__snake_case ):05d}.bin''' ) lowerCamelCase_ =os.path.join(__snake_case , weights_name.replace('''.bin''' , F'''-{idx+1:05d}-of-???.bin''' ) ) os.rename(__snake_case , os.path.join(__snake_case , __snake_case ) ) for key in shard: lowerCamelCase_ =shard_file # Add the metadata lowerCamelCase_ ={'''total_size''': total_size} lowerCamelCase_ ={'''metadata''': metadata, '''weight_map''': weight_map} with open(os.path.join(__snake_case , __snake_case ) , '''w''' , encoding='''utf-8''' ) as f: lowerCamelCase_ =json.dumps(__snake_case , indent=2 , sort_keys=__snake_case ) + '''\n''' f.write(__snake_case ) return metadata, index if __name__ == "__main__": a_ : Union[str, Any] = 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_ : Tuple = parser.parse_args() a_ , a_ : int = shard_on_the_fly( args.nllb_moe_checkpoint_path, args.pytorch_dump_folder_path, 1_28, args.dtype, ) a_ : Tuple = NllbMoeConfig.from_pretrained( """facebook/nllb-200-3.3B""", encoder_sparse_step=4, decoder_sparse_step=4, num_experts=1_28 ) 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)
6
1
'''simple docstring''' from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class __UpperCamelCase : def __init__( self, lowerCAmelCase, lowerCAmelCase=12, lowerCAmelCase=7, lowerCAmelCase=True, lowerCAmelCase=True, lowerCAmelCase=True, lowerCAmelCase=99, lowerCAmelCase=32, lowerCAmelCase=32, lowerCAmelCase=2, lowerCAmelCase=4, lowerCAmelCase=37, lowerCAmelCase=0.1, lowerCAmelCase=0.1, lowerCAmelCase=512, lowerCAmelCase=0.0_2, lowerCAmelCase=0, lowerCAmelCase=None, ): """simple docstring""" lowerCamelCase_ =parent lowerCamelCase_ =batch_size lowerCamelCase_ =seq_length lowerCamelCase_ =is_training lowerCamelCase_ =use_input_mask lowerCamelCase_ =use_labels lowerCamelCase_ =vocab_size lowerCamelCase_ =hidden_size lowerCamelCase_ =projection_dim lowerCamelCase_ =num_hidden_layers lowerCamelCase_ =num_attention_heads lowerCamelCase_ =intermediate_size lowerCamelCase_ =dropout lowerCamelCase_ =attention_dropout lowerCamelCase_ =max_position_embeddings lowerCamelCase_ =initializer_range lowerCamelCase_ =scope lowerCamelCase_ =bos_token_id def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =ids_tensor([self.batch_size, self.seq_length], self.vocab_size ) lowerCamelCase_ =None if self.use_input_mask: lowerCamelCase_ =random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: lowerCamelCase_ =input_mask.numpy() lowerCamelCase_, lowerCamelCase_ =input_mask.shape lowerCamelCase_ =np.random.randint(1, seq_length - 1, size=(batch_size,) ) for batch_idx, start_index in enumerate(lowerCAmelCase ): lowerCamelCase_ =1 lowerCamelCase_ =0 lowerCamelCase_ =self.get_config() return config, input_ids, tf.convert_to_tensor(lowerCAmelCase ) def lowercase__ ( self ): """simple docstring""" return BlipTextConfig( vocab_size=self.vocab_size, hidden_size=self.hidden_size, projection_dim=self.projection_dim, num_hidden_layers=self.num_hidden_layers, num_attention_heads=self.num_attention_heads, intermediate_size=self.intermediate_size, dropout=self.dropout, attention_dropout=self.attention_dropout, max_position_embeddings=self.max_position_embeddings, initializer_range=self.initializer_range, bos_token_id=self.bos_token_id, ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =TFBlipTextModel(config=lowerCAmelCase ) lowerCamelCase_ =model(lowerCAmelCase, attention_mask=lowerCAmelCase, training=lowerCAmelCase ) lowerCamelCase_ =model(lowerCAmelCase, training=lowerCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape, (self.batch_size, self.hidden_size) ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.prepare_config_and_inputs() lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ =config_and_inputs lowerCamelCase_ ={'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class __UpperCamelCase ( lowerCamelCase__ , unittest.TestCase ): lowercase : List[str] =(TFBlipTextModel,) if is_tf_available() else () lowercase : Union[str, Any] =False lowercase : Tuple =False lowercase : Optional[int] =False def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =BlipTextModelTester(self ) lowerCamelCase_ =ConfigTester(self, config_class=lowerCAmelCase, hidden_size=37 ) def lowercase__ ( self ): """simple docstring""" self.config_tester.run_common_tests() def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCAmelCase ) def lowercase__ ( self ): """simple docstring""" pass def lowercase__ ( self ): """simple docstring""" pass @unittest.skip(reason='''Blip does not use inputs_embeds''' ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def lowercase__ ( self ): """simple docstring""" pass @slow def lowercase__ ( self ): """simple docstring""" for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase_ =TFBlipTextModel.from_pretrained(lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) def lowercase__ ( self, lowerCAmelCase=True ): """simple docstring""" super().test_pt_tf_model_equivalence(allow_missing_keys=lowerCAmelCase )
6
'''simple docstring''' import warnings from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class __UpperCamelCase ( lowerCamelCase__ ): lowercase : int =['image_processor', 'tokenizer'] lowercase : int ='LayoutLMv2ImageProcessor' lowercase : Any =('LayoutXLMTokenizer', 'LayoutXLMTokenizerFast') def __init__( self, lowerCAmelCase=None, lowerCAmelCase=None, **lowerCAmelCase ): """simple docstring""" if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''', lowerCAmelCase, ) lowerCamelCase_ =kwargs.pop('''feature_extractor''' ) lowerCamelCase_ =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, lowerCAmelCase, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = True, lowerCAmelCase = False, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = 0, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = False, lowerCAmelCase = False, lowerCAmelCase = False, lowerCAmelCase = False, lowerCAmelCase = True, lowerCAmelCase = None, **lowerCAmelCase, ): """simple docstring""" if self.image_processor.apply_ocr and (boxes is not None): raise ValueError( '''You cannot provide bounding boxes ''' '''if you initialized the image processor with apply_ocr set to True.''' ) if self.image_processor.apply_ocr and (word_labels is not None): raise ValueError( '''You cannot provide word labels if you initialized the image processor with apply_ocr set to True.''' ) if return_overflowing_tokens is True and return_offsets_mapping is False: raise ValueError('''You cannot return overflowing tokens without returning the offsets mapping.''' ) # first, apply the image processor lowerCamelCase_ =self.image_processor(images=lowerCAmelCase, return_tensors=lowerCAmelCase ) # second, apply the tokenizer if text is not None and self.image_processor.apply_ocr and text_pair is None: if isinstance(lowerCAmelCase, lowerCAmelCase ): lowerCamelCase_ =[text] # add batch dimension (as the image processor always adds a batch dimension) lowerCamelCase_ =features['''words'''] lowerCamelCase_ =self.tokenizer( text=text if text is not None else features['''words'''], text_pair=text_pair if text_pair is not None else None, boxes=boxes if boxes is not None else features['''boxes'''], word_labels=lowerCAmelCase, add_special_tokens=lowerCAmelCase, padding=lowerCAmelCase, truncation=lowerCAmelCase, max_length=lowerCAmelCase, stride=lowerCAmelCase, pad_to_multiple_of=lowerCAmelCase, return_token_type_ids=lowerCAmelCase, return_attention_mask=lowerCAmelCase, return_overflowing_tokens=lowerCAmelCase, return_special_tokens_mask=lowerCAmelCase, return_offsets_mapping=lowerCAmelCase, return_length=lowerCAmelCase, verbose=lowerCAmelCase, return_tensors=lowerCAmelCase, **lowerCAmelCase, ) # add pixel values lowerCamelCase_ =features.pop('''pixel_values''' ) if return_overflowing_tokens is True: lowerCamelCase_ =self.get_overflowing_images(lowerCAmelCase, encoded_inputs['''overflow_to_sample_mapping'''] ) lowerCamelCase_ =images return encoded_inputs def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =[] for sample_idx in overflow_to_sample_mapping: images_with_overflow.append(images[sample_idx] ) if len(lowerCAmelCase ) != len(lowerCAmelCase ): raise ValueError( '''Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got''' f''' {len(lowerCAmelCase )} and {len(lowerCAmelCase )}''' ) return images_with_overflow def lowercase__ ( self, *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" return self.tokenizer.batch_decode(*lowerCAmelCase, **lowerCAmelCase ) def lowercase__ ( self, *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" return self.tokenizer.decode(*lowerCAmelCase, **lowerCAmelCase ) @property def lowercase__ ( self ): """simple docstring""" return ["input_ids", "bbox", "attention_mask", "image"] @property def lowercase__ ( self ): """simple docstring""" warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''', lowerCAmelCase, ) return self.image_processor_class @property def lowercase__ ( self ): """simple docstring""" warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''', lowerCAmelCase, ) return self.image_processor
6
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) a_ : Tuple = { """configuration_vision_encoder_decoder""": ["""VisionEncoderDecoderConfig""", """VisionEncoderDecoderOnnxConfig"""] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ : Tuple = ["""VisionEncoderDecoderModel"""] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ : Optional[int] = ["""TFVisionEncoderDecoderModel"""] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ : List[Any] = ["""FlaxVisionEncoderDecoderModel"""] if TYPE_CHECKING: from .configuration_vision_encoder_decoder import VisionEncoderDecoderConfig, VisionEncoderDecoderOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vision_encoder_decoder import VisionEncoderDecoderModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vision_encoder_decoder import TFVisionEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vision_encoder_decoder import FlaxVisionEncoderDecoderModel else: import sys a_ : List[str] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
6
'''simple docstring''' import unittest import torch from diffusers import VQModel from diffusers.utils import floats_tensor, torch_device from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin, UNetTesterMixin enable_full_determinism() class __UpperCamelCase ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): lowercase : Union[str, Any] =VQModel lowercase : Union[str, Any] ='sample' @property def lowercase__ ( self, lowerCAmelCase=(32, 32) ): """simple docstring""" lowerCamelCase_ =4 lowerCamelCase_ =3 lowerCamelCase_ =floats_tensor((batch_size, num_channels) + sizes ).to(lowerCAmelCase ) return {"sample": image} @property def lowercase__ ( self ): """simple docstring""" return (3, 32, 32) @property def lowercase__ ( self ): """simple docstring""" return (3, 32, 32) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ ={ '''block_out_channels''': [32, 64], '''in_channels''': 3, '''out_channels''': 3, '''down_block_types''': ['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''], '''up_block_types''': ['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''], '''latent_channels''': 3, } lowerCamelCase_ =self.dummy_input return init_dict, inputs_dict def lowercase__ ( self ): """simple docstring""" pass def lowercase__ ( self ): """simple docstring""" pass def lowercase__ ( self ): """simple docstring""" lowerCamelCase_, lowerCamelCase_ =VQModel.from_pretrained('''fusing/vqgan-dummy''', output_loading_info=lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertEqual(len(loading_info['''missing_keys'''] ), 0 ) model.to(lowerCAmelCase ) lowerCamelCase_ =model(**self.dummy_input ) assert image is not None, "Make sure output is not None" def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =VQModel.from_pretrained('''fusing/vqgan-dummy''' ) model.to(lowerCAmelCase ).eval() torch.manual_seed(0 ) if torch.cuda.is_available(): torch.cuda.manual_seed_all(0 ) lowerCamelCase_ =torch.randn(1, model.config.in_channels, model.config.sample_size, model.config.sample_size ) lowerCamelCase_ =image.to(lowerCAmelCase ) with torch.no_grad(): lowerCamelCase_ =model(lowerCAmelCase ).sample lowerCamelCase_ =output[0, -1, -3:, -3:].flatten().cpu() # fmt: off lowerCamelCase_ =torch.tensor([-0.0_1_5_3, -0.4_0_4_4, -0.1_8_8_0, -0.5_1_6_1, -0.2_4_1_8, -0.4_0_7_2, -0.1_6_1_2, -0.0_6_3_3, -0.0_1_4_3] ) # fmt: on self.assertTrue(torch.allclose(lowerCAmelCase, lowerCAmelCase, atol=1e-3 ) )
6
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available a_ : Any = { """configuration_graphormer""": ["""GRAPHORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GraphormerConfig"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ : Optional[Any] = [ """GRAPHORMER_PRETRAINED_MODEL_ARCHIVE_LIST""", """GraphormerForGraphClassification""", """GraphormerModel""", """GraphormerPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_graphormer import GRAPHORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, GraphormerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_graphormer import ( GRAPHORMER_PRETRAINED_MODEL_ARCHIVE_LIST, GraphormerForGraphClassification, GraphormerModel, GraphormerPreTrainedModel, ) else: import sys a_ : Optional[int] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
6
'''simple docstring''' import datasets from .evaluate import evaluate a_ : List[Any] = """\ @article{hendrycks2021cuad, title={CUAD: An Expert-Annotated NLP Dataset for Legal Contract Review}, author={Dan Hendrycks and Collin Burns and Anya Chen and Spencer Ball}, journal={arXiv preprint arXiv:2103.06268}, year={2021} } """ a_ : List[Any] = """ This metric wrap the official scoring script for version 1 of the Contract Understanding Atticus Dataset (CUAD). Contract Understanding Atticus Dataset (CUAD) v1 is a corpus of more than 13,000 labels in 510 commercial legal contracts that have been manually labeled to identify 41 categories of important clauses that lawyers look for when reviewing contracts in connection with corporate transactions. """ a_ : Any = """ Computes CUAD scores (EM, F1, AUPR, Precision@80%Recall, and Precision@90%Recall). Args: predictions: List of question-answers dictionaries with the following key-values: - 'id': id of the question-answer pair as given in the references (see below) - 'prediction_text': list of possible texts for the answer, as a list of strings depending on a threshold on the confidence probability of each prediction. references: List of question-answers dictionaries with the following key-values: - 'id': id of the question-answer pair (see above), - 'answers': a Dict in the CUAD dataset format { 'text': list of possible texts for the answer, as a list of strings 'answer_start': list of start positions for the answer, as a list of ints } Note that answer_start values are not taken into account to compute the metric. Returns: 'exact_match': Exact match (the normalized answer exactly match the gold answer) 'f1': The F-score of predicted tokens versus the gold answer 'aupr': Area Under the Precision-Recall curve 'prec_at_80_recall': Precision at 80% recall 'prec_at_90_recall': Precision at 90% recall Examples: >>> predictions = [{'prediction_text': ['The seller:', 'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.'], 'id': 'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties'}] >>> references = [{'answers': {'answer_start': [143, 49], 'text': ['The seller:', 'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.']}, 'id': 'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties'}] >>> cuad_metric = datasets.load_metric(\"cuad\") >>> results = cuad_metric.compute(predictions=predictions, references=references) >>> print(results) {'exact_match': 100.0, 'f1': 100.0, 'aupr': 0.0, 'prec_at_80_recall': 1.0, 'prec_at_90_recall': 1.0} """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __UpperCamelCase ( datasets.Metric ): def lowercase__ ( self ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION, citation=_CITATION, inputs_description=_KWARGS_DESCRIPTION, features=datasets.Features( { '''predictions''': { '''id''': datasets.Value('''string''' ), '''prediction_text''': datasets.features.Sequence(datasets.Value('''string''' ) ), }, '''references''': { '''id''': datasets.Value('''string''' ), '''answers''': datasets.features.Sequence( { '''text''': datasets.Value('''string''' ), '''answer_start''': datasets.Value('''int32''' ), } ), }, } ), codebase_urls=['''https://www.atticusprojectai.org/cuad'''], reference_urls=['''https://www.atticusprojectai.org/cuad'''], ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ ={prediction['''id''']: prediction['''prediction_text'''] for prediction in predictions} lowerCamelCase_ =[ { '''paragraphs''': [ { '''qas''': [ { '''answers''': [{'''text''': answer_text} for answer_text in ref['''answers''']['''text''']], '''id''': ref['''id'''], } for ref in references ] } ] } ] lowerCamelCase_ =evaluate(dataset=lowerCAmelCase, predictions=lowerCAmelCase ) return score
6
1
'''simple docstring''' import unittest from knapsack import greedy_knapsack as kp class __UpperCamelCase ( unittest.TestCase ): def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =[10, 20, 30, 40, 50, 60] lowerCamelCase_ =[2, 4, 6, 8, 10, 12] lowerCamelCase_ =100 self.assertEqual(kp.calc_profit(lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ), 210 ) def lowercase__ ( self ): """simple docstring""" self.assertRaisesRegex(lowerCAmelCase, '''max_weight must greater than zero.''' ) def lowercase__ ( self ): """simple docstring""" self.assertRaisesRegex(lowerCAmelCase, '''Weight can not be negative.''' ) def lowercase__ ( self ): """simple docstring""" self.assertRaisesRegex(lowerCAmelCase, '''Profit can not be negative.''' ) def lowercase__ ( self ): """simple docstring""" self.assertRaisesRegex(lowerCAmelCase, '''max_weight must greater than zero.''' ) def lowercase__ ( self ): """simple docstring""" self.assertRaisesRegex( lowerCAmelCase, '''The length of profit and weight must be same.''' ) if __name__ == "__main__": unittest.main()
6
'''simple docstring''' import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert_fast import BertTokenizerFast from .tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer, DPRReaderTokenizer a_ : Tuple = logging.get_logger(__name__) a_ : int = {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""} a_ : Tuple = { """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""" ), }, } a_ : Union[str, 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""" ), }, } a_ : str = { """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""" ), }, } a_ : int = { """facebook/dpr-ctx_encoder-single-nq-base""": 5_12, """facebook/dpr-ctx_encoder-multiset-base""": 5_12, } a_ : List[Any] = { """facebook/dpr-question_encoder-single-nq-base""": 5_12, """facebook/dpr-question_encoder-multiset-base""": 5_12, } a_ : Optional[Any] = { """facebook/dpr-reader-single-nq-base""": 5_12, """facebook/dpr-reader-multiset-base""": 5_12, } a_ : Optional[int] = { """facebook/dpr-ctx_encoder-single-nq-base""": {"""do_lower_case""": True}, """facebook/dpr-ctx_encoder-multiset-base""": {"""do_lower_case""": True}, } a_ : List[str] = { """facebook/dpr-question_encoder-single-nq-base""": {"""do_lower_case""": True}, """facebook/dpr-question_encoder-multiset-base""": {"""do_lower_case""": True}, } a_ : Dict = { """facebook/dpr-reader-single-nq-base""": {"""do_lower_case""": True}, """facebook/dpr-reader-multiset-base""": {"""do_lower_case""": True}, } class __UpperCamelCase ( lowerCamelCase__ ): lowercase : List[Any] =VOCAB_FILES_NAMES lowercase : Any =CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP lowercase : Optional[Any] =CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase : List[str] =CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION lowercase : Dict =DPRContextEncoderTokenizer class __UpperCamelCase ( lowerCamelCase__ ): lowercase : Optional[int] =VOCAB_FILES_NAMES lowercase : List[Any] =QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP lowercase : Optional[Any] =QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase : List[Any] =QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION lowercase : List[Any] =DPRQuestionEncoderTokenizer a_ : Union[str, Any] = collections.namedtuple( """DPRSpanPrediction""", ["""span_score""", """relevance_score""", """doc_id""", """start_index""", """end_index""", """text"""] ) a_ : Dict = collections.namedtuple("""DPRReaderOutput""", ["""start_logits""", """end_logits""", """relevance_logits"""]) a_ : Dict = 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 __UpperCamelCase : def __call__( self, lowerCAmelCase, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = False, lowerCAmelCase = False, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, **lowerCAmelCase, ): """simple docstring""" if titles is None and texts is None: return super().__call__( lowerCAmelCase, padding=lowerCAmelCase, truncation=lowerCAmelCase, max_length=lowerCAmelCase, return_tensors=lowerCAmelCase, return_attention_mask=lowerCAmelCase, **lowerCAmelCase, ) elif titles is None or texts is None: lowerCamelCase_ =titles if texts is None else texts return super().__call__( lowerCAmelCase, lowerCAmelCase, padding=lowerCAmelCase, truncation=lowerCAmelCase, max_length=lowerCAmelCase, return_tensors=lowerCAmelCase, return_attention_mask=lowerCAmelCase, **lowerCAmelCase, ) lowerCamelCase_ =titles if not isinstance(lowerCAmelCase, lowerCAmelCase ) else [titles] lowerCamelCase_ =texts if not isinstance(lowerCAmelCase, lowerCAmelCase ) else [texts] lowerCamelCase_ =len(lowerCAmelCase ) lowerCamelCase_ =questions if not isinstance(lowerCAmelCase, lowerCAmelCase ) else [questions] * n_passages assert len(lowerCAmelCase ) == len( lowerCAmelCase ), f'''There should be as many titles than texts but got {len(lowerCAmelCase )} titles and {len(lowerCAmelCase )} texts.''' lowerCamelCase_ =super().__call__(lowerCAmelCase, lowerCAmelCase, padding=lowerCAmelCase, truncation=lowerCAmelCase )['''input_ids'''] lowerCamelCase_ =super().__call__(lowerCAmelCase, add_special_tokens=lowerCAmelCase, padding=lowerCAmelCase, truncation=lowerCAmelCase )['''input_ids'''] lowerCamelCase_ ={ '''input_ids''': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(lowerCAmelCase, lowerCAmelCase ) ] } if return_attention_mask is not False: lowerCamelCase_ =[] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) lowerCamelCase_ =attention_mask return self.pad(lowerCAmelCase, padding=lowerCAmelCase, max_length=lowerCAmelCase, return_tensors=lowerCAmelCase ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase = 16, lowerCAmelCase = 64, lowerCAmelCase = 4, ): """simple docstring""" lowerCamelCase_ =reader_input['''input_ids'''] lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ =reader_output[:3] lowerCamelCase_ =len(lowerCAmelCase ) lowerCamelCase_ =sorted(range(lowerCAmelCase ), reverse=lowerCAmelCase, key=relevance_logits.__getitem__ ) lowerCamelCase_ =[] for doc_id in sorted_docs: lowerCamelCase_ =list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence lowerCamelCase_ =sequence_ids.index(self.sep_token_id, 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: lowerCamelCase_ =sequence_ids.index(self.pad_token_id ) else: lowerCamelCase_ =len(lowerCAmelCase ) lowerCamelCase_ =self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len], end_logits=end_logits[doc_id][passage_offset:sequence_len], max_answer_length=lowerCAmelCase, top_spans=lowerCAmelCase, ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index], relevance_score=relevance_logits[doc_id], doc_id=lowerCAmelCase, start_index=lowerCAmelCase, end_index=lowerCAmelCase, text=self.decode(sequence_ids[start_index : end_index + 1] ), ) ) if len(lowerCAmelCase ) >= num_spans: break return nbest_spans_predictions[:num_spans] def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, ): """simple docstring""" lowerCamelCase_ =[] for start_index, start_score in enumerate(lowerCAmelCase ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) lowerCamelCase_ =sorted(lowerCAmelCase, key=lambda lowerCAmelCase : x[1], reverse=lowerCAmelCase ) lowerCamelCase_ =[] for (start_index, end_index), score in scores: assert start_index <= end_index, f'''Wrong span indices: [{start_index}:{end_index}]''' lowerCamelCase_ =end_index - start_index + 1 assert length <= max_answer_length, f'''Span is too long: {length} > {max_answer_length}''' if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(lowerCAmelCase ) == top_spans: break return chosen_span_intervals @add_end_docstrings(lowerCamelCase__ ) class __UpperCamelCase ( lowerCamelCase__ , lowerCamelCase__ ): lowercase : int =VOCAB_FILES_NAMES lowercase : Tuple =READER_PRETRAINED_VOCAB_FILES_MAP lowercase : Tuple =READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase : List[str] =READER_PRETRAINED_INIT_CONFIGURATION lowercase : int =['input_ids', 'attention_mask'] lowercase : Dict =DPRReaderTokenizer
6
1
'''simple docstring''' import math from typing import Callable, List, Optional, Union import numpy as np import PIL import torch from PIL import Image from transformers import CLIPTextModel, CLIPTokenizer from diffusers.models import AutoencoderKL, UNetaDConditionModel from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_upscale import StableDiffusionUpscalePipeline from diffusers.schedulers import DDIMScheduler, DDPMScheduler, LMSDiscreteScheduler, PNDMScheduler def a_ ( __snake_case : Dict , __snake_case : Optional[Any] , __snake_case : Union[str, Any]=[] ) -> List[Any]: """simple docstring""" lowerCamelCase_ =size[0] - overlap_pixels * 2 lowerCamelCase_ =size[1] - overlap_pixels * 2 for letter in ["l", "r"]: if letter in remove_borders: size_x += overlap_pixels for letter in ["t", "b"]: if letter in remove_borders: size_y += overlap_pixels lowerCamelCase_ =np.ones((size_y, size_x) , dtype=np.uinta ) * 255 lowerCamelCase_ =np.pad(__snake_case , mode='''linear_ramp''' , pad_width=__snake_case , end_values=0 ) if "l" in remove_borders: lowerCamelCase_ =mask[:, overlap_pixels : mask.shape[1]] if "r" in remove_borders: lowerCamelCase_ =mask[:, 0 : mask.shape[1] - overlap_pixels] if "t" in remove_borders: lowerCamelCase_ =mask[overlap_pixels : mask.shape[0], :] if "b" in remove_borders: lowerCamelCase_ =mask[0 : mask.shape[0] - overlap_pixels, :] return mask def a_ ( __snake_case : Optional[Any] , __snake_case : List[Any] , __snake_case : str ) -> Any: """simple docstring""" return max(__snake_case , min(__snake_case , __snake_case ) ) def a_ ( __snake_case : [int] , __snake_case : [int] , __snake_case : [int] ) -> Tuple: """simple docstring""" return ( clamp(rect[0] , min[0] , max[0] ), clamp(rect[1] , min[1] , max[1] ), clamp(rect[2] , min[0] , max[0] ), clamp(rect[3] , min[1] , max[1] ), ) def a_ ( __snake_case : [int] , __snake_case : int , __snake_case : [int] ) -> Dict: """simple docstring""" lowerCamelCase_ =list(__snake_case ) rect[0] -= overlap rect[1] -= overlap rect[2] += overlap rect[3] += overlap lowerCamelCase_ =clamp_rect(__snake_case , [0, 0] , [image_size[0], image_size[1]] ) return rect def a_ ( __snake_case : Optional[Any] , __snake_case : Any , __snake_case : Optional[Any] , __snake_case : Optional[int] ) -> Dict: """simple docstring""" lowerCamelCase_ =Image.new('''RGB''' , (tile.size[0] + original_slice, tile.size[1]) ) result.paste( original_image.resize((tile.size[0], tile.size[1]) , Image.BICUBIC ).crop( (slice_x, 0, slice_x + original_slice, tile.size[1]) ) , (0, 0) , ) result.paste(__snake_case , (original_slice, 0) ) return result def a_ ( __snake_case : Dict , __snake_case : Optional[int] ) -> Optional[Any]: """simple docstring""" lowerCamelCase_ =(original_image_slice * 4, 0, tile.size[0], tile.size[1]) lowerCamelCase_ =tile.crop(__snake_case ) return tile def a_ ( __snake_case : Any , __snake_case : Union[str, Any] ) -> List[Any]: """simple docstring""" lowerCamelCase_ =n % d return n - divisor class __UpperCamelCase ( lowerCamelCase__ ): def __init__( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase = 350, ): """simple docstring""" super().__init__( vae=lowerCAmelCase, text_encoder=lowerCAmelCase, tokenizer=lowerCAmelCase, unet=lowerCAmelCase, low_res_scheduler=lowerCAmelCase, scheduler=lowerCAmelCase, max_noise_level=lowerCAmelCase, ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" torch.manual_seed(0 ) lowerCamelCase_ =( min(image.size[0] - (tile_size + original_image_slice), x * tile_size ), min(image.size[1] - (tile_size + original_image_slice), y * tile_size ), min(image.size[0], (x + 1) * tile_size ), min(image.size[1], (y + 1) * tile_size ), ) lowerCamelCase_ =add_overlap_rect(lowerCAmelCase, lowerCAmelCase, image.size ) lowerCamelCase_ =image.crop(lowerCAmelCase ) lowerCamelCase_ =((crop_rect[0] + ((crop_rect[2] - crop_rect[0]) / 2)) / image.size[0]) * tile.size[0] lowerCamelCase_ =translated_slice_x - (original_image_slice / 2) lowerCamelCase_ =max(0, lowerCAmelCase ) lowerCamelCase_ =squeeze_tile(lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =to_input.size lowerCamelCase_ =to_input.resize((tile_size, tile_size), Image.BICUBIC ) lowerCamelCase_ =super(lowerCAmelCase, self ).__call__(image=lowerCAmelCase, **lowerCAmelCase ).images[0] lowerCamelCase_ =upscaled_tile.resize((orig_input_size[0] * 4, orig_input_size[1] * 4), Image.BICUBIC ) lowerCamelCase_ =unsqueeze_tile(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =upscaled_tile.resize((tile.size[0] * 4, tile.size[1] * 4), Image.BICUBIC ) lowerCamelCase_ =[] if x == 0: remove_borders.append('''l''' ) elif crop_rect[2] == image.size[0]: remove_borders.append('''r''' ) if y == 0: remove_borders.append('''t''' ) elif crop_rect[3] == image.size[1]: remove_borders.append('''b''' ) lowerCamelCase_ =Image.fromarray( make_transparency_mask( (upscaled_tile.size[0], upscaled_tile.size[1]), tile_border * 4, remove_borders=lowerCAmelCase ), mode='''L''', ) final_image.paste( lowerCAmelCase, (crop_rect_with_overlap[0] * 4, crop_rect_with_overlap[1] * 4), lowerCAmelCase ) @torch.no_grad() def __call__( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase = 75, lowerCAmelCase = 9.0, lowerCAmelCase = 50, lowerCAmelCase = None, lowerCAmelCase = 1, lowerCAmelCase = 0.0, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = 1, lowerCAmelCase = 128, lowerCAmelCase = 32, lowerCAmelCase = 32, ): """simple docstring""" lowerCamelCase_ =Image.new('''RGB''', (image.size[0] * 4, image.size[1] * 4) ) lowerCamelCase_ =math.ceil(image.size[0] / tile_size ) lowerCamelCase_ =math.ceil(image.size[1] / tile_size ) lowerCamelCase_ =tcx * tcy lowerCamelCase_ =0 for y in range(lowerCAmelCase ): for x in range(lowerCAmelCase ): self._process_tile( lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, prompt=lowerCAmelCase, num_inference_steps=lowerCAmelCase, guidance_scale=lowerCAmelCase, noise_level=lowerCAmelCase, negative_prompt=lowerCAmelCase, num_images_per_prompt=lowerCAmelCase, eta=lowerCAmelCase, generator=lowerCAmelCase, latents=lowerCAmelCase, ) current_count += 1 if callback is not None: callback({'''progress''': current_count / total_tile_count, '''image''': final_image} ) return final_image def a_ ( ) -> Optional[Any]: """simple docstring""" # Run a demo lowerCamelCase_ ='''stabilityai/stable-diffusion-x4-upscaler''' lowerCamelCase_ =StableDiffusionTiledUpscalePipeline.from_pretrained(__snake_case , revision='''fp16''' , torch_dtype=torch.floataa ) lowerCamelCase_ =pipe.to('''cuda''' ) lowerCamelCase_ =Image.open('''../../docs/source/imgs/diffusers_library.jpg''' ) def callback(__snake_case : str ): print(F'''progress: {obj['progress']:.4f}''' ) obj["image"].save('''diffusers_library_progress.jpg''' ) lowerCamelCase_ =pipe(image=__snake_case , prompt='''Black font, white background, vector''' , noise_level=40 , callback=__snake_case ) final_image.save('''diffusers_library.jpg''' ) if __name__ == "__main__": main()
6
'''simple docstring''' from unittest import TestCase from datasets import Dataset from minhash_deduplication import deduplicate_dataset, make_duplicate_clusters def a_ ( ) -> Tuple: """simple docstring""" lowerCamelCase_ ={ '''repo_name''': ['''test_repo1''', '''test_repo2''', '''test_repo3'''], '''path''': ['''test_1.py''', '''test_2.py''', '''unit_test.py'''], '''content''': ['''a ''' * 20, '''a ''' * 30, '''b ''' * 7], } lowerCamelCase_ =Dataset.from_dict(__snake_case ) return dataset class __UpperCamelCase ( lowerCamelCase__ ): def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =get_dataset() lowerCamelCase_ =make_duplicate_clusters(lowerCAmelCase, 0.8_5 ) self.assertEqual(len(duplicate_clusters[0] ), 2 ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =get_dataset() lowerCamelCase_, lowerCamelCase_ =deduplicate_dataset(lowerCAmelCase ) self.assertEqual(len(lowerCAmelCase ), 2 ) print(lowerCAmelCase ) self.assertEqual(duplicate_clusters[0][0]['''copies'''], 2 ) self.assertEqual(duplicate_clusters[0][0]['''is_extreme'''], lowerCAmelCase )
6
1
'''simple docstring''' import argparse import os.path as osp import re import torch from safetensors.torch import load_file, save_file # =================# # UNet Conversion # # =================# a_ : Optional[Any] = [ # (stable-diffusion, HF Diffusers) ("""time_embed.0.weight""", """time_embedding.linear_1.weight"""), ("""time_embed.0.bias""", """time_embedding.linear_1.bias"""), ("""time_embed.2.weight""", """time_embedding.linear_2.weight"""), ("""time_embed.2.bias""", """time_embedding.linear_2.bias"""), ("""input_blocks.0.0.weight""", """conv_in.weight"""), ("""input_blocks.0.0.bias""", """conv_in.bias"""), ("""out.0.weight""", """conv_norm_out.weight"""), ("""out.0.bias""", """conv_norm_out.bias"""), ("""out.2.weight""", """conv_out.weight"""), ("""out.2.bias""", """conv_out.bias"""), ] a_ : int = [ # (stable-diffusion, HF Diffusers) ("""in_layers.0""", """norm1"""), ("""in_layers.2""", """conv1"""), ("""out_layers.0""", """norm2"""), ("""out_layers.3""", """conv2"""), ("""emb_layers.1""", """time_emb_proj"""), ("""skip_connection""", """conv_shortcut"""), ] a_ : Dict = [] # hardcoded number of downblocks and resnets/attentions... # would need smarter logic for other networks. for i in range(4): # loop over downblocks/upblocks for j in range(2): # loop over resnets/attentions for downblocks a_ : Tuple = F"""down_blocks.{i}.resnets.{j}.""" a_ : Union[str, Any] = F"""input_blocks.{3*i + j + 1}.0.""" unet_conversion_map_layer.append((sd_down_res_prefix, hf_down_res_prefix)) if i < 3: # no attention layers in down_blocks.3 a_ : Dict = F"""down_blocks.{i}.attentions.{j}.""" a_ : int = F"""input_blocks.{3*i + j + 1}.1.""" unet_conversion_map_layer.append((sd_down_atn_prefix, hf_down_atn_prefix)) for j in range(3): # loop over resnets/attentions for upblocks a_ : Optional[int] = F"""up_blocks.{i}.resnets.{j}.""" a_ : Tuple = F"""output_blocks.{3*i + j}.0.""" unet_conversion_map_layer.append((sd_up_res_prefix, hf_up_res_prefix)) if i > 0: # no attention layers in up_blocks.0 a_ : Optional[Any] = F"""up_blocks.{i}.attentions.{j}.""" a_ : Optional[Any] = F"""output_blocks.{3*i + j}.1.""" unet_conversion_map_layer.append((sd_up_atn_prefix, hf_up_atn_prefix)) if i < 3: # no downsample in down_blocks.3 a_ : Optional[int] = F"""down_blocks.{i}.downsamplers.0.conv.""" a_ : Union[str, Any] = F"""input_blocks.{3*(i+1)}.0.op.""" unet_conversion_map_layer.append((sd_downsample_prefix, hf_downsample_prefix)) # no upsample in up_blocks.3 a_ : List[str] = F"""up_blocks.{i}.upsamplers.0.""" a_ : Any = F"""output_blocks.{3*i + 2}.{1 if i == 0 else 2}.""" unet_conversion_map_layer.append((sd_upsample_prefix, hf_upsample_prefix)) a_ : Any = """mid_block.attentions.0.""" a_ : List[Any] = """middle_block.1.""" unet_conversion_map_layer.append((sd_mid_atn_prefix, hf_mid_atn_prefix)) for j in range(2): a_ : str = F"""mid_block.resnets.{j}.""" a_ : Tuple = F"""middle_block.{2*j}.""" unet_conversion_map_layer.append((sd_mid_res_prefix, hf_mid_res_prefix)) def a_ ( __snake_case : Optional[int] ) -> List[Any]: """simple docstring""" # buyer beware: this is a *brittle* function, # and correct output requires that all of these pieces interact in # the exact order in which I have arranged them. lowerCamelCase_ ={k: k for k in unet_state_dict.keys()} for sd_name, hf_name in unet_conversion_map: lowerCamelCase_ =sd_name for k, v in mapping.items(): if "resnets" in k: for sd_part, hf_part in unet_conversion_map_resnet: lowerCamelCase_ =v.replace(__snake_case , __snake_case ) lowerCamelCase_ =v for k, v in mapping.items(): for sd_part, hf_part in unet_conversion_map_layer: lowerCamelCase_ =v.replace(__snake_case , __snake_case ) lowerCamelCase_ =v lowerCamelCase_ ={v: unet_state_dict[k] for k, v in mapping.items()} return new_state_dict # ================# # VAE Conversion # # ================# a_ : str = [ # (stable-diffusion, HF Diffusers) ("""nin_shortcut""", """conv_shortcut"""), ("""norm_out""", """conv_norm_out"""), ("""mid.attn_1.""", """mid_block.attentions.0."""), ] for i in range(4): # down_blocks have two resnets for j in range(2): a_ : Dict = F"""encoder.down_blocks.{i}.resnets.{j}.""" a_ : int = F"""encoder.down.{i}.block.{j}.""" vae_conversion_map.append((sd_down_prefix, hf_down_prefix)) if i < 3: a_ : int = F"""down_blocks.{i}.downsamplers.0.""" a_ : Any = F"""down.{i}.downsample.""" vae_conversion_map.append((sd_downsample_prefix, hf_downsample_prefix)) a_ : Dict = F"""up_blocks.{i}.upsamplers.0.""" a_ : Optional[Any] = F"""up.{3-i}.upsample.""" vae_conversion_map.append((sd_upsample_prefix, hf_upsample_prefix)) # up_blocks have three resnets # also, up blocks in hf are numbered in reverse from sd for j in range(3): a_ : List[Any] = F"""decoder.up_blocks.{i}.resnets.{j}.""" a_ : List[Any] = F"""decoder.up.{3-i}.block.{j}.""" vae_conversion_map.append((sd_up_prefix, hf_up_prefix)) # this part accounts for mid blocks in both the encoder and the decoder for i in range(2): a_ : Optional[Any] = F"""mid_block.resnets.{i}.""" a_ : List[Any] = F"""mid.block_{i+1}.""" vae_conversion_map.append((sd_mid_res_prefix, hf_mid_res_prefix)) a_ : Dict = [ # (stable-diffusion, HF Diffusers) ("""norm.""", """group_norm."""), ("""q.""", """query."""), ("""k.""", """key."""), ("""v.""", """value."""), ("""proj_out.""", """proj_attn."""), ] def a_ ( __snake_case : List[Any] ) -> Dict: """simple docstring""" # convert HF linear weights to SD conv2d weights return w.reshape(*w.shape , 1 , 1 ) def a_ ( __snake_case : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" lowerCamelCase_ ={k: k for k in vae_state_dict.keys()} for k, v in mapping.items(): for sd_part, hf_part in vae_conversion_map: lowerCamelCase_ =v.replace(__snake_case , __snake_case ) lowerCamelCase_ =v for k, v in mapping.items(): if "attentions" in k: for sd_part, hf_part in vae_conversion_map_attn: lowerCamelCase_ =v.replace(__snake_case , __snake_case ) lowerCamelCase_ =v lowerCamelCase_ ={v: vae_state_dict[k] for k, v in mapping.items()} lowerCamelCase_ =['''q''', '''k''', '''v''', '''proj_out'''] for k, v in new_state_dict.items(): for weight_name in weights_to_convert: if F'''mid.attn_1.{weight_name}.weight''' in k: print(F'''Reshaping {k} for SD format''' ) lowerCamelCase_ =reshape_weight_for_sd(__snake_case ) return new_state_dict # =========================# # Text Encoder Conversion # # =========================# a_ : Tuple = [ # (stable-diffusion, HF Diffusers) ("""resblocks.""", """text_model.encoder.layers."""), ("""ln_1""", """layer_norm1"""), ("""ln_2""", """layer_norm2"""), (""".c_fc.""", """.fc1."""), (""".c_proj.""", """.fc2."""), (""".attn""", """.self_attn"""), ("""ln_final.""", """transformer.text_model.final_layer_norm."""), ("""token_embedding.weight""", """transformer.text_model.embeddings.token_embedding.weight"""), ("""positional_embedding""", """transformer.text_model.embeddings.position_embedding.weight"""), ] a_ : Any = {re.escape(x[1]): x[0] for x in textenc_conversion_lst} a_ : int = re.compile("""|""".join(protected.keys())) # Ordering is from https://github.com/pytorch/pytorch/blob/master/test/cpp/api/modules.cpp a_ : List[Any] = {"""q""": 0, """k""": 1, """v""": 2} def a_ ( __snake_case : Union[str, Any] ) -> Optional[int]: """simple docstring""" lowerCamelCase_ ={} lowerCamelCase_ ={} lowerCamelCase_ ={} for k, v in text_enc_dict.items(): if ( k.endswith('''.self_attn.q_proj.weight''' ) or k.endswith('''.self_attn.k_proj.weight''' ) or k.endswith('''.self_attn.v_proj.weight''' ) ): lowerCamelCase_ =k[: -len('''.q_proj.weight''' )] lowerCamelCase_ =k[-len('''q_proj.weight''' )] if k_pre not in capture_qkv_weight: lowerCamelCase_ =[None, None, None] lowerCamelCase_ =v continue if ( k.endswith('''.self_attn.q_proj.bias''' ) or k.endswith('''.self_attn.k_proj.bias''' ) or k.endswith('''.self_attn.v_proj.bias''' ) ): lowerCamelCase_ =k[: -len('''.q_proj.bias''' )] lowerCamelCase_ =k[-len('''q_proj.bias''' )] if k_pre not in capture_qkv_bias: lowerCamelCase_ =[None, None, None] lowerCamelCase_ =v continue lowerCamelCase_ =textenc_pattern.sub(lambda __snake_case : protected[re.escape(m.group(0 ) )] , __snake_case ) lowerCamelCase_ =v for k_pre, tensors in capture_qkv_weight.items(): if None in tensors: raise Exception('''CORRUPTED MODEL: one of the q-k-v values for the text encoder was missing''' ) lowerCamelCase_ =textenc_pattern.sub(lambda __snake_case : protected[re.escape(m.group(0 ) )] , __snake_case ) lowerCamelCase_ =torch.cat(__snake_case ) for k_pre, tensors in capture_qkv_bias.items(): if None in tensors: raise Exception('''CORRUPTED MODEL: one of the q-k-v values for the text encoder was missing''' ) lowerCamelCase_ =textenc_pattern.sub(lambda __snake_case : protected[re.escape(m.group(0 ) )] , __snake_case ) lowerCamelCase_ =torch.cat(__snake_case ) return new_state_dict def a_ ( __snake_case : Tuple ) -> Optional[int]: """simple docstring""" return text_enc_dict if __name__ == "__main__": a_ : str = argparse.ArgumentParser() parser.add_argument("""--model_path""", default=None, type=str, required=True, help="""Path to the model to convert.""") parser.add_argument("""--checkpoint_path""", default=None, type=str, required=True, help="""Path to the output model.""") parser.add_argument("""--half""", action="""store_true""", help="""Save weights in half precision.""") parser.add_argument( """--use_safetensors""", action="""store_true""", help="""Save weights use safetensors, default is ckpt.""" ) a_ : int = parser.parse_args() assert args.model_path is not None, "Must provide a model path!" assert args.checkpoint_path is not None, "Must provide a checkpoint path!" # Path for safetensors a_ : Any = osp.join(args.model_path, """unet""", """diffusion_pytorch_model.safetensors""") a_ : List[str] = osp.join(args.model_path, """vae""", """diffusion_pytorch_model.safetensors""") a_ : List[str] = osp.join(args.model_path, """text_encoder""", """model.safetensors""") # Load models from safetensors if it exists, if it doesn't pytorch if osp.exists(unet_path): a_ : str = load_file(unet_path, device="""cpu""") else: a_ : List[Any] = osp.join(args.model_path, """unet""", """diffusion_pytorch_model.bin""") a_ : Any = torch.load(unet_path, map_location="""cpu""") if osp.exists(vae_path): a_ : Optional[Any] = load_file(vae_path, device="""cpu""") else: a_ : int = osp.join(args.model_path, """vae""", """diffusion_pytorch_model.bin""") a_ : List[Any] = torch.load(vae_path, map_location="""cpu""") if osp.exists(text_enc_path): a_ : Any = load_file(text_enc_path, device="""cpu""") else: a_ : Optional[Any] = osp.join(args.model_path, """text_encoder""", """pytorch_model.bin""") a_ : List[Any] = torch.load(text_enc_path, map_location="""cpu""") # Convert the UNet model a_ : List[Any] = convert_unet_state_dict(unet_state_dict) a_ : List[str] = {"""model.diffusion_model.""" + k: v for k, v in unet_state_dict.items()} # Convert the VAE model a_ : Optional[Any] = convert_vae_state_dict(vae_state_dict) a_ : Union[str, Any] = {"""first_stage_model.""" + k: v for k, v in vae_state_dict.items()} # Easiest way to identify v2.0 model seems to be that the text encoder (OpenCLIP) is deeper a_ : Optional[Any] = """text_model.encoder.layers.22.layer_norm2.bias""" in text_enc_dict if is_vaa_model: # Need to add the tag 'transformer' in advance so we can knock it out from the final layer-norm a_ : Optional[Any] = {"""transformer.""" + k: v for k, v in text_enc_dict.items()} a_ : Any = convert_text_enc_state_dict_vaa(text_enc_dict) a_ : int = {"""cond_stage_model.model.""" + k: v for k, v in text_enc_dict.items()} else: a_ : int = convert_text_enc_state_dict(text_enc_dict) a_ : Optional[int] = {"""cond_stage_model.transformer.""" + k: v for k, v in text_enc_dict.items()} # Put together new checkpoint a_ : Tuple = {**unet_state_dict, **vae_state_dict, **text_enc_dict} if args.half: a_ : Dict = {k: v.half() for k, v in state_dict.items()} if args.use_safetensors: save_file(state_dict, args.checkpoint_path) else: a_ : Tuple = {"""state_dict""": state_dict} torch.save(state_dict, args.checkpoint_path)
6
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_torch_available, ) a_ : Any = { """configuration_trocr""": ["""TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP""", """TrOCRConfig"""], """processing_trocr""": ["""TrOCRProcessor"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ : Optional[int] = [ """TROCR_PRETRAINED_MODEL_ARCHIVE_LIST""", """TrOCRForCausalLM""", """TrOCRPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_trocr import TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP, TrOCRConfig from .processing_trocr import TrOCRProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_trocr import TROCR_PRETRAINED_MODEL_ARCHIVE_LIST, TrOCRForCausalLM, TrOCRPreTrainedModel else: import sys a_ : int = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
6
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging a_ : List[Any] = logging.get_logger(__name__) a_ : Union[str, Any] = { """vinvino02/glpn-kitti""": """https://huggingface.co/vinvino02/glpn-kitti/resolve/main/config.json""", # See all GLPN models at https://huggingface.co/models?filter=glpn } class __UpperCamelCase ( lowerCamelCase__ ): lowercase : str ='glpn' def __init__( self, lowerCAmelCase=3, lowerCAmelCase=4, lowerCAmelCase=[2, 2, 2, 2], lowerCAmelCase=[8, 4, 2, 1], lowerCAmelCase=[32, 64, 160, 256], lowerCAmelCase=[7, 3, 3, 3], lowerCAmelCase=[4, 2, 2, 2], lowerCAmelCase=[1, 2, 5, 8], lowerCAmelCase=[4, 4, 4, 4], lowerCAmelCase="gelu", lowerCAmelCase=0.0, lowerCAmelCase=0.0, lowerCAmelCase=0.0_2, lowerCAmelCase=0.1, lowerCAmelCase=1e-6, lowerCAmelCase=64, lowerCAmelCase=10, lowerCAmelCase=-1, **lowerCAmelCase, ): """simple docstring""" super().__init__(**lowerCAmelCase ) lowerCamelCase_ =num_channels lowerCamelCase_ =num_encoder_blocks lowerCamelCase_ =depths lowerCamelCase_ =sr_ratios lowerCamelCase_ =hidden_sizes lowerCamelCase_ =patch_sizes lowerCamelCase_ =strides lowerCamelCase_ =mlp_ratios lowerCamelCase_ =num_attention_heads lowerCamelCase_ =hidden_act lowerCamelCase_ =hidden_dropout_prob lowerCamelCase_ =attention_probs_dropout_prob lowerCamelCase_ =initializer_range lowerCamelCase_ =drop_path_rate lowerCamelCase_ =layer_norm_eps lowerCamelCase_ =decoder_hidden_size lowerCamelCase_ =max_depth lowerCamelCase_ =head_in_index
6
'''simple docstring''' from collections import defaultdict from math import gcd def a_ ( __snake_case : int = 150_0000 ) -> int: """simple docstring""" lowerCamelCase_ =defaultdict(__snake_case ) lowerCamelCase_ =2 while 2 * euclid_m * (euclid_m + 1) <= limit: for euclid_n in range((euclid_m % 2) + 1 , __snake_case , 2 ): if gcd(__snake_case , __snake_case ) > 1: continue lowerCamelCase_ =2 * euclid_m * (euclid_m + euclid_n) for perimeter in range(__snake_case , limit + 1 , __snake_case ): frequencies[perimeter] += 1 euclid_m += 1 return sum(1 for frequency in frequencies.values() if frequency == 1 ) if __name__ == "__main__": print(F"""{solution() = }""")
6
1
'''simple docstring''' import tempfile import torch from diffusers import PNDMScheduler from .test_schedulers import SchedulerCommonTest class __UpperCamelCase ( lowerCamelCase__ ): lowercase : Tuple =(PNDMScheduler,) lowercase : int =(('num_inference_steps', 50),) def lowercase__ ( self, **lowerCAmelCase ): """simple docstring""" lowerCamelCase_ ={ '''num_train_timesteps''': 1_000, '''beta_start''': 0.0_0_0_1, '''beta_end''': 0.0_2, '''beta_schedule''': '''linear''', } config.update(**lowerCAmelCase ) return config def lowercase__ ( self, lowerCAmelCase=0, **lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =dict(self.forward_default_kwargs ) lowerCamelCase_ =kwargs.pop('''num_inference_steps''', lowerCAmelCase ) lowerCamelCase_ =self.dummy_sample lowerCamelCase_ =0.1 * sample lowerCamelCase_ =[residual + 0.2, residual + 0.1_5, residual + 0.1, residual + 0.0_5] for scheduler_class in self.scheduler_classes: lowerCamelCase_ =self.get_scheduler_config(**lowerCAmelCase ) lowerCamelCase_ =scheduler_class(**lowerCAmelCase ) scheduler.set_timesteps(lowerCAmelCase ) # copy over dummy past residuals lowerCamelCase_ =dummy_past_residuals[:] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(lowerCAmelCase ) lowerCamelCase_ =scheduler_class.from_pretrained(lowerCAmelCase ) new_scheduler.set_timesteps(lowerCAmelCase ) # copy over dummy past residuals lowerCamelCase_ =dummy_past_residuals[:] lowerCamelCase_ =scheduler.step_prk(lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, **lowerCAmelCase ).prev_sample lowerCamelCase_ =new_scheduler.step_prk(lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, **lowerCAmelCase ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" lowerCamelCase_ =scheduler.step_plms(lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, **lowerCAmelCase ).prev_sample lowerCamelCase_ =new_scheduler.step_plms(lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, **lowerCAmelCase ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def lowercase__ ( self ): """simple docstring""" pass def lowercase__ ( self, lowerCAmelCase=0, **lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =dict(self.forward_default_kwargs ) lowerCamelCase_ =kwargs.pop('''num_inference_steps''', lowerCAmelCase ) lowerCamelCase_ =self.dummy_sample lowerCamelCase_ =0.1 * sample lowerCamelCase_ =[residual + 0.2, residual + 0.1_5, residual + 0.1, residual + 0.0_5] for scheduler_class in self.scheduler_classes: lowerCamelCase_ =self.get_scheduler_config() lowerCamelCase_ =scheduler_class(**lowerCAmelCase ) scheduler.set_timesteps(lowerCAmelCase ) # copy over dummy past residuals (must be after setting timesteps) lowerCamelCase_ =dummy_past_residuals[:] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(lowerCAmelCase ) lowerCamelCase_ =scheduler_class.from_pretrained(lowerCAmelCase ) # copy over dummy past residuals new_scheduler.set_timesteps(lowerCAmelCase ) # copy over dummy past residual (must be after setting timesteps) lowerCamelCase_ =dummy_past_residuals[:] lowerCamelCase_ =scheduler.step_prk(lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, **lowerCAmelCase ).prev_sample lowerCamelCase_ =new_scheduler.step_prk(lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, **lowerCAmelCase ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" lowerCamelCase_ =scheduler.step_plms(lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, **lowerCAmelCase ).prev_sample lowerCamelCase_ =new_scheduler.step_plms(lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, **lowerCAmelCase ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def lowercase__ ( self, **lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =self.scheduler_classes[0] lowerCamelCase_ =self.get_scheduler_config(**lowerCAmelCase ) lowerCamelCase_ =scheduler_class(**lowerCAmelCase ) lowerCamelCase_ =10 lowerCamelCase_ =self.dummy_model() lowerCamelCase_ =self.dummy_sample_deter scheduler.set_timesteps(lowerCAmelCase ) for i, t in enumerate(scheduler.prk_timesteps ): lowerCamelCase_ =model(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =scheduler.step_prk(lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ).prev_sample for i, t in enumerate(scheduler.plms_timesteps ): lowerCamelCase_ =model(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =scheduler.step_plms(lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ).prev_sample return sample def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =dict(self.forward_default_kwargs ) lowerCamelCase_ =kwargs.pop('''num_inference_steps''', lowerCAmelCase ) for scheduler_class in self.scheduler_classes: lowerCamelCase_ =self.get_scheduler_config() lowerCamelCase_ =scheduler_class(**lowerCAmelCase ) lowerCamelCase_ =self.dummy_sample lowerCamelCase_ =0.1 * sample if num_inference_steps is not None and hasattr(lowerCAmelCase, '''set_timesteps''' ): scheduler.set_timesteps(lowerCAmelCase ) elif num_inference_steps is not None and not hasattr(lowerCAmelCase, '''set_timesteps''' ): lowerCamelCase_ =num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) lowerCamelCase_ =[residual + 0.2, residual + 0.1_5, residual + 0.1, residual + 0.0_5] lowerCamelCase_ =dummy_past_residuals[:] lowerCamelCase_ =scheduler.step_prk(lowerCAmelCase, 0, lowerCAmelCase, **lowerCAmelCase ).prev_sample lowerCamelCase_ =scheduler.step_prk(lowerCAmelCase, 1, lowerCAmelCase, **lowerCAmelCase ).prev_sample self.assertEqual(output_a.shape, sample.shape ) self.assertEqual(output_a.shape, output_a.shape ) lowerCamelCase_ =scheduler.step_plms(lowerCAmelCase, 0, lowerCAmelCase, **lowerCAmelCase ).prev_sample lowerCamelCase_ =scheduler.step_plms(lowerCAmelCase, 1, lowerCAmelCase, **lowerCAmelCase ).prev_sample self.assertEqual(output_a.shape, sample.shape ) self.assertEqual(output_a.shape, output_a.shape ) def lowercase__ ( self ): """simple docstring""" for timesteps in [100, 1_000]: self.check_over_configs(num_train_timesteps=lowerCAmelCase ) def lowercase__ ( self ): """simple docstring""" for steps_offset in [0, 1]: self.check_over_configs(steps_offset=lowerCAmelCase ) lowerCamelCase_ =self.scheduler_classes[0] lowerCamelCase_ =self.get_scheduler_config(steps_offset=1 ) lowerCamelCase_ =scheduler_class(**lowerCAmelCase ) scheduler.set_timesteps(10 ) assert torch.equal( scheduler.timesteps, torch.LongTensor( [901, 851, 851, 801, 801, 751, 751, 701, 701, 651, 651, 601, 601, 501, 401, 301, 201, 101, 1] ), ) def lowercase__ ( self ): """simple docstring""" for beta_start, beta_end in zip([0.0_0_0_1, 0.0_0_1], [0.0_0_2, 0.0_2] ): self.check_over_configs(beta_start=lowerCAmelCase, beta_end=lowerCAmelCase ) def lowercase__ ( self ): """simple docstring""" for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=lowerCAmelCase ) def lowercase__ ( self ): """simple docstring""" for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=lowerCAmelCase ) def lowercase__ ( self ): """simple docstring""" for t in [1, 5, 10]: self.check_over_forward(time_step=lowerCAmelCase ) def lowercase__ ( self ): """simple docstring""" for t, num_inference_steps in zip([1, 5, 10], [10, 50, 100] ): self.check_over_forward(num_inference_steps=lowerCAmelCase ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =27 for scheduler_class in self.scheduler_classes: lowerCamelCase_ =self.dummy_sample lowerCamelCase_ =0.1 * sample lowerCamelCase_ =self.get_scheduler_config() lowerCamelCase_ =scheduler_class(**lowerCAmelCase ) scheduler.set_timesteps(lowerCAmelCase ) # before power of 3 fix, would error on first step, so we only need to do two for i, t in enumerate(scheduler.prk_timesteps[:2] ): lowerCamelCase_ =scheduler.step_prk(lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ).prev_sample def lowercase__ ( self ): """simple docstring""" with self.assertRaises(lowerCAmelCase ): lowerCamelCase_ =self.scheduler_classes[0] lowerCamelCase_ =self.get_scheduler_config() lowerCamelCase_ =scheduler_class(**lowerCAmelCase ) scheduler.step_plms(self.dummy_sample, 1, self.dummy_sample ).prev_sample def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.full_loop() lowerCamelCase_ =torch.sum(torch.abs(lowerCAmelCase ) ) lowerCamelCase_ =torch.mean(torch.abs(lowerCAmelCase ) ) assert abs(result_sum.item() - 1_9_8.1_3_1_8 ) < 1e-2 assert abs(result_mean.item() - 0.2_5_8_0 ) < 1e-3 def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.full_loop(prediction_type='''v_prediction''' ) lowerCamelCase_ =torch.sum(torch.abs(lowerCAmelCase ) ) lowerCamelCase_ =torch.mean(torch.abs(lowerCAmelCase ) ) assert abs(result_sum.item() - 6_7.3_9_8_6 ) < 1e-2 assert abs(result_mean.item() - 0.0_8_7_8 ) < 1e-3 def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.full_loop(set_alpha_to_one=lowerCAmelCase, beta_start=0.0_1 ) lowerCamelCase_ =torch.sum(torch.abs(lowerCAmelCase ) ) lowerCamelCase_ =torch.mean(torch.abs(lowerCAmelCase ) ) assert abs(result_sum.item() - 2_3_0.0_3_9_9 ) < 1e-2 assert abs(result_mean.item() - 0.2_9_9_5 ) < 1e-3 def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.full_loop(set_alpha_to_one=lowerCAmelCase, beta_start=0.0_1 ) lowerCamelCase_ =torch.sum(torch.abs(lowerCAmelCase ) ) lowerCamelCase_ =torch.mean(torch.abs(lowerCAmelCase ) ) assert abs(result_sum.item() - 1_8_6.9_4_8_2 ) < 1e-2 assert abs(result_mean.item() - 0.2_4_3_4 ) < 1e-3
6
'''simple docstring''' import argparse import os # New Code # 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 import find_executable_batch_size ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to ensure out-of-memory errors never # interrupt training, 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) # # 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 # ######################################################################## a_ : Tuple = 16 a_ : Optional[int] = 32 def a_ ( __snake_case : Accelerator , __snake_case : int = 16 ) -> str: """simple docstring""" lowerCamelCase_ =AutoTokenizer.from_pretrained('''bert-base-cased''' ) lowerCamelCase_ =load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(__snake_case : int ): # max_length=None => use the model max length (it's actually the default) lowerCamelCase_ =tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=__snake_case , max_length=__snake_case ) 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(): lowerCamelCase_ =datasets.map( __snake_case , batched=__snake_case , 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 lowerCamelCase_ =tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(__snake_case : Any ): # On TPU it's best to pad everything to the same length or training will be very slow. lowerCamelCase_ =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": lowerCamelCase_ =16 elif accelerator.mixed_precision != "no": lowerCamelCase_ =8 else: lowerCamelCase_ =None return tokenizer.pad( __snake_case , padding='''longest''' , max_length=__snake_case , pad_to_multiple_of=__snake_case , return_tensors='''pt''' , ) # Instantiate dataloaders. lowerCamelCase_ =DataLoader( tokenized_datasets['''train'''] , shuffle=__snake_case , collate_fn=__snake_case , batch_size=__snake_case ) lowerCamelCase_ =DataLoader( tokenized_datasets['''validation'''] , shuffle=__snake_case , collate_fn=__snake_case , batch_size=__snake_case ) 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 a_ : Tuple = mocked_dataloaders # noqa: F811 def a_ ( __snake_case : List[str] , __snake_case : Tuple ) -> Optional[Any]: """simple docstring""" # For testing only if os.environ.get('''TESTING_MOCKED_DATALOADERS''' , __snake_case ) == "1": lowerCamelCase_ =2 # Initialize accelerator lowerCamelCase_ =Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs lowerCamelCase_ =config['''lr'''] lowerCamelCase_ =int(config['''num_epochs'''] ) lowerCamelCase_ =int(config['''seed'''] ) lowerCamelCase_ =int(config['''batch_size'''] ) lowerCamelCase_ =evaluate.load('''glue''' , '''mrpc''' ) # New Code # # We now can define an inner training loop function. It should take a batch size as the only parameter, # and build the dataloaders in there. # It also gets our decorator @find_executable_batch_size(starting_batch_size=__snake_case ) def inner_training_loop(__snake_case : Union[str, Any] ): # And now just move everything below under this function # We need to bring in the Accelerator object from earlier nonlocal accelerator # And reset all of its attributes that could hold onto any memory: accelerator.free_memory() # Then we can declare the model, optimizer, and everything else: set_seed(__snake_case ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) lowerCamelCase_ =AutoModelForSequenceClassification.from_pretrained('''bert-base-cased''' , return_dict=__snake_case ) # 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). lowerCamelCase_ =model.to(accelerator.device ) # Instantiate optimizer lowerCamelCase_ =AdamW(params=model.parameters() , lr=__snake_case ) lowerCamelCase_, lowerCamelCase_ =get_dataloaders(__snake_case , __snake_case ) # Instantiate scheduler lowerCamelCase_ =get_linear_schedule_with_warmup( optimizer=__snake_case , num_warmup_steps=100 , num_training_steps=(len(__snake_case ) * num_epochs) , ) # 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. lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ =accelerator.prepare( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ) # Now we train the model for epoch in range(__snake_case ): model.train() for step, batch in enumerate(__snake_case ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) lowerCamelCase_ =model(**__snake_case ) lowerCamelCase_ =outputs.loss accelerator.backward(__snake_case ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(__snake_case ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): lowerCamelCase_ =model(**__snake_case ) lowerCamelCase_ =outputs.logits.argmax(dim=-1 ) lowerCamelCase_, lowerCamelCase_ =accelerator.gather_for_metrics((predictions, batch['''labels''']) ) metric.add_batch( predictions=__snake_case , references=__snake_case , ) lowerCamelCase_ =metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F'''epoch {epoch}:''' , __snake_case ) # New Code # # And call it at the end with no arguments # Note: You could also refactor this outside of your training loop function inner_training_loop() def a_ ( ) -> Dict: """simple docstring""" lowerCamelCase_ =argparse.ArgumentParser(description='''Simple example of training script.''' ) parser.add_argument( '''--mixed_precision''' , type=__snake_case , default=__snake_case , 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.''' ) lowerCamelCase_ =parser.parse_args() lowerCamelCase_ ={'''lr''': 2e-5, '''num_epochs''': 3, '''seed''': 42, '''batch_size''': 16} training_function(__snake_case , __snake_case ) if __name__ == "__main__": main()
6
1
'''simple docstring''' from argparse import ArgumentParser from . import BaseTransformersCLICommand def a_ ( __snake_case : Tuple ) -> str: """simple docstring""" return DownloadCommand(args.model , args.cache_dir , args.force , args.trust_remote_code ) class __UpperCamelCase ( lowerCamelCase__ ): @staticmethod def lowercase__ ( lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =parser.add_parser('''download''' ) download_parser.add_argument( '''--cache-dir''', type=lowerCAmelCase, default=lowerCAmelCase, help='''Path to location to store the models''' ) download_parser.add_argument( '''--force''', action='''store_true''', help='''Force the model to be download even if already in cache-dir''' ) download_parser.add_argument( '''--trust-remote-code''', action='''store_true''', help='''Whether or not to allow for custom models defined on the Hub in their own modeling files. Use only if you\'ve reviewed the code as it will execute on your local machine''', ) download_parser.add_argument('''model''', type=lowerCAmelCase, help='''Name of the model to download''' ) download_parser.set_defaults(func=lowerCAmelCase ) def __init__( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =model lowerCamelCase_ =cache lowerCamelCase_ =force lowerCamelCase_ =trust_remote_code def lowercase__ ( self ): """simple docstring""" from ..models.auto import AutoModel, AutoTokenizer AutoModel.from_pretrained( self._model, cache_dir=self._cache, force_download=self._force, trust_remote_code=self._trust_remote_code ) AutoTokenizer.from_pretrained( self._model, cache_dir=self._cache, force_download=self._force, trust_remote_code=self._trust_remote_code )
6
'''simple docstring''' import argparse import os import re # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_dummies.py a_ : List[str] = """src/diffusers""" # Matches is_xxx_available() a_ : int = re.compile(R"""is\_([a-z_]*)_available\(\)""") # Matches from xxx import bla a_ : List[str] = re.compile(R"""\s+from\s+\S*\s+import\s+([^\(\s].*)\n""") a_ : Optional[Any] = """ {0} = None """ a_ : List[Any] = """ class {0}(metaclass=DummyObject): _backends = {1} def __init__(self, *args, **kwargs): requires_backends(self, {1}) @classmethod def from_config(cls, *args, **kwargs): requires_backends(cls, {1}) @classmethod def from_pretrained(cls, *args, **kwargs): requires_backends(cls, {1}) """ a_ : Optional[Any] = """ def {0}(*args, **kwargs): requires_backends({0}, {1}) """ def a_ ( __snake_case : Union[str, Any] ) -> List[Any]: """simple docstring""" lowerCamelCase_ =_re_backend.findall(__snake_case ) if len(__snake_case ) == 0: return None return "_and_".join(__snake_case ) def a_ ( ) -> Optional[int]: """simple docstring""" with open(os.path.join(__snake_case , '''__init__.py''' ) , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: lowerCamelCase_ =f.readlines() # Get to the point we do the actual imports for type checking lowerCamelCase_ =0 lowerCamelCase_ ={} # Go through the end of the file while line_index < len(__snake_case ): # If the line contains is_backend_available, we grab all objects associated with the `else` block lowerCamelCase_ =find_backend(lines[line_index] ) if backend is not None: while not lines[line_index].startswith('''else:''' ): line_index += 1 line_index += 1 lowerCamelCase_ =[] # Until we unindent, add backend objects to the list while line_index < len(__snake_case ) and len(lines[line_index] ) > 1: lowerCamelCase_ =lines[line_index] lowerCamelCase_ =_re_single_line_import.search(__snake_case ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(''', ''' ) ) elif line.startswith(''' ''' * 8 ): objects.append(line[8:-2] ) line_index += 1 if len(__snake_case ) > 0: lowerCamelCase_ =objects else: line_index += 1 return backend_specific_objects def a_ ( __snake_case : Dict , __snake_case : int ) -> Union[str, Any]: """simple docstring""" if name.isupper(): return DUMMY_CONSTANT.format(__snake_case ) elif name.islower(): return DUMMY_FUNCTION.format(__snake_case , __snake_case ) else: return DUMMY_CLASS.format(__snake_case , __snake_case ) def a_ ( __snake_case : Tuple=None ) -> List[str]: """simple docstring""" if backend_specific_objects is None: lowerCamelCase_ =read_init() # For special correspondence backend to module name as used in the function requires_modulename lowerCamelCase_ ={} for backend, objects in backend_specific_objects.items(): lowerCamelCase_ ='''[''' + ''', '''.join(F'''"{b}"''' for b in backend.split('''_and_''' ) ) + ''']''' lowerCamelCase_ ='''# This file is autogenerated by the command `make fix-copies`, do not edit.\n''' dummy_file += "from ..utils import DummyObject, requires_backends\n\n" dummy_file += "\n".join([create_dummy_object(__snake_case , __snake_case ) for o in objects] ) lowerCamelCase_ =dummy_file return dummy_files def a_ ( __snake_case : Dict=False ) -> Union[str, Any]: """simple docstring""" lowerCamelCase_ =create_dummy_files() # For special correspondence backend to shortcut as used in utils/dummy_xxx_objects.py lowerCamelCase_ ={'''torch''': '''pt'''} # Locate actual dummy modules and read their content. lowerCamelCase_ =os.path.join(__snake_case , '''utils''' ) lowerCamelCase_ ={ backend: os.path.join(__snake_case , F'''dummy_{short_names.get(__snake_case , __snake_case )}_objects.py''' ) for backend in dummy_files.keys() } lowerCamelCase_ ={} for backend, file_path in dummy_file_paths.items(): if os.path.isfile(__snake_case ): with open(__snake_case , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: lowerCamelCase_ =f.read() else: lowerCamelCase_ ='''''' for backend in dummy_files.keys(): if dummy_files[backend] != actual_dummies[backend]: if overwrite: print( F'''Updating diffusers.utils.dummy_{short_names.get(__snake_case , __snake_case )}_objects.py as the main ''' '''__init__ has new objects.''' ) with open(dummy_file_paths[backend] , '''w''' , encoding='''utf-8''' , newline='''\n''' ) as f: f.write(dummy_files[backend] ) else: raise ValueError( '''The main __init__ has objects that are not present in ''' F'''diffusers.utils.dummy_{short_names.get(__snake_case , __snake_case )}_objects.py. Run `make fix-copies` ''' '''to fix this.''' ) if __name__ == "__main__": a_ : Tuple = argparse.ArgumentParser() parser.add_argument("""--fix_and_overwrite""", action="""store_true""", help="""Whether to fix inconsistencies.""") a_ : Tuple = parser.parse_args() check_dummies(args.fix_and_overwrite)
6
1
'''simple docstring''' import random class __UpperCamelCase : @staticmethod def lowercase__ ( lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =[ord(lowerCAmelCase ) for i in text] lowerCamelCase_ =[] lowerCamelCase_ =[] for i in plain: lowerCamelCase_ =random.randint(1, 300 ) lowerCamelCase_ =(i + k) * k cipher.append(lowerCAmelCase ) key.append(lowerCAmelCase ) return cipher, key @staticmethod def lowercase__ ( lowerCAmelCase, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =[] for i in range(len(lowerCAmelCase ) ): lowerCamelCase_ =int((cipher[i] - (key[i]) ** 2) / key[i] ) plain.append(chr(lowerCAmelCase ) ) return "".join(lowerCAmelCase ) if __name__ == "__main__": a_ , a_ : Tuple = Onepad().encrypt("""Hello""") print(c, k) print(Onepad().decrypt(c, k))
6
'''simple docstring''' a_ : List[Any] = [sum(int(c, 10) ** 2 for c in i.__str__()) for i in range(10_00_00)] def a_ ( __snake_case : int ) -> int: """simple docstring""" lowerCamelCase_ =0 while number: # Increased Speed Slightly by checking every 5 digits together. sum_of_digits_squared += DIGITS_SQUARED[number % 10_0000] number //= 10_0000 return sum_of_digits_squared # There are 2 Chains made, # One ends with 89 with the chain member 58 being the one which when declared first, # there will be the least number of iterations for all the members to be checked. # The other one ends with 1 and has only one element 1. # So 58 and 1 are chosen to be declared at the starting. # Changed dictionary to an array to quicken the solution a_ : list[bool | None] = [None] * 10_00_00_00 a_ : List[Any] = True a_ : Optional[Any] = False def a_ ( __snake_case : int ) -> bool: """simple docstring""" if CHAINS[number - 1] is not None: return CHAINS[number - 1] # type: ignore lowerCamelCase_ =chain(next_number(__snake_case ) ) lowerCamelCase_ =number_chain while number < 1000_0000: lowerCamelCase_ =number_chain number *= 10 return number_chain def a_ ( __snake_case : int = 1000_0000 ) -> int: """simple docstring""" for i in range(1 , __snake_case ): if CHAINS[i] is None: chain(i + 1 ) return CHAINS[:number].count(__snake_case ) if __name__ == "__main__": import doctest doctest.testmod() print(F"""{solution() = }""")
6
1
'''simple docstring''' # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import json import os from ...utils.constants import SAGEMAKER_PARALLEL_EC2_INSTANCES, TORCH_DYNAMO_MODES from ...utils.dataclasses import ComputeEnvironment, SageMakerDistributedType from ...utils.imports import is_botoa_available from .config_args import SageMakerConfig from .config_utils import ( DYNAMO_BACKENDS, _ask_field, _ask_options, _convert_dynamo_backend, _convert_mixed_precision, _convert_sagemaker_distributed_mode, _convert_yes_no_to_bool, ) if is_botoa_available(): import botoa # noqa: F401 def a_ ( __snake_case : Dict ) -> str: """simple docstring""" lowerCamelCase_ =botoa.client('''iam''' ) lowerCamelCase_ ={ '''Version''': '''2012-10-17''', '''Statement''': [ {'''Effect''': '''Allow''', '''Principal''': {'''Service''': '''sagemaker.amazonaws.com'''}, '''Action''': '''sts:AssumeRole'''} ], } try: # create the role, associated with the chosen trust policy iam_client.create_role( RoleName=__snake_case , AssumeRolePolicyDocument=json.dumps(__snake_case , indent=2 ) ) lowerCamelCase_ ={ '''Version''': '''2012-10-17''', '''Statement''': [ { '''Effect''': '''Allow''', '''Action''': [ '''sagemaker:*''', '''ecr:GetDownloadUrlForLayer''', '''ecr:BatchGetImage''', '''ecr:BatchCheckLayerAvailability''', '''ecr:GetAuthorizationToken''', '''cloudwatch:PutMetricData''', '''cloudwatch:GetMetricData''', '''cloudwatch:GetMetricStatistics''', '''cloudwatch:ListMetrics''', '''logs:CreateLogGroup''', '''logs:CreateLogStream''', '''logs:DescribeLogStreams''', '''logs:PutLogEvents''', '''logs:GetLogEvents''', '''s3:CreateBucket''', '''s3:ListBucket''', '''s3:GetBucketLocation''', '''s3:GetObject''', '''s3:PutObject''', ], '''Resource''': '''*''', } ], } # attach policy to role iam_client.put_role_policy( RoleName=__snake_case , PolicyName=F'''{role_name}_policy_permission''' , PolicyDocument=json.dumps(__snake_case , indent=2 ) , ) except iam_client.exceptions.EntityAlreadyExistsException: print(F'''role {role_name} already exists. Using existing one''' ) def a_ ( __snake_case : Optional[int] ) -> int: """simple docstring""" lowerCamelCase_ =botoa.client('''iam''' ) return iam_client.get_role(RoleName=__snake_case )["Role"]["Arn"] def a_ ( ) -> int: """simple docstring""" lowerCamelCase_ =_ask_options( '''How do you want to authorize?''' , ['''AWS Profile''', '''Credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) '''] , __snake_case , ) lowerCamelCase_ =None if credentials_configuration == 0: lowerCamelCase_ =_ask_field('''Enter your AWS Profile name: [default] ''' , default='''default''' ) lowerCamelCase_ =aws_profile else: print( '''Note you will need to provide AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY when you launch you training script with,''' '''`accelerate launch --aws_access_key_id XXX --aws_secret_access_key YYY`''' ) lowerCamelCase_ =_ask_field('''AWS Access Key ID: ''' ) lowerCamelCase_ =aws_access_key_id lowerCamelCase_ =_ask_field('''AWS Secret Access Key: ''' ) lowerCamelCase_ =aws_secret_access_key lowerCamelCase_ =_ask_field('''Enter your AWS Region: [us-east-1]''' , default='''us-east-1''' ) lowerCamelCase_ =aws_region lowerCamelCase_ =_ask_options( '''Do you already have an IAM Role for executing Amazon SageMaker Training Jobs?''' , ['''Provide IAM Role name''', '''Create new IAM role using credentials'''] , __snake_case , ) if role_management == 0: lowerCamelCase_ =_ask_field('''Enter your IAM role name: ''' ) else: lowerCamelCase_ ='''accelerate_sagemaker_execution_role''' print(F'''Accelerate will create an iam role "{iam_role_name}" using the provided credentials''' ) _create_iam_role_for_sagemaker(__snake_case ) lowerCamelCase_ =_ask_field( '''Do you want to use custom Docker image? [yes/NO]: ''' , _convert_yes_no_to_bool , default=__snake_case , error_message='''Please enter yes or no.''' , ) lowerCamelCase_ =None if is_custom_docker_image: lowerCamelCase_ =_ask_field('''Enter your Docker image: ''' , lambda __snake_case : str(__snake_case ).lower() ) lowerCamelCase_ =_ask_field( '''Do you want to provide SageMaker input channels with data locations? [yes/NO]: ''' , _convert_yes_no_to_bool , default=__snake_case , error_message='''Please enter yes or no.''' , ) lowerCamelCase_ =None if is_sagemaker_inputs_enabled: lowerCamelCase_ =_ask_field( '''Enter the path to the SageMaker inputs TSV file with columns (channel_name, data_location): ''' , lambda __snake_case : str(__snake_case ).lower() , ) lowerCamelCase_ =_ask_field( '''Do you want to enable SageMaker metrics? [yes/NO]: ''' , _convert_yes_no_to_bool , default=__snake_case , error_message='''Please enter yes or no.''' , ) lowerCamelCase_ =None if is_sagemaker_metrics_enabled: lowerCamelCase_ =_ask_field( '''Enter the path to the SageMaker metrics TSV file with columns (metric_name, metric_regex): ''' , lambda __snake_case : str(__snake_case ).lower() , ) lowerCamelCase_ =_ask_options( '''What is the distributed mode?''' , ['''No distributed training''', '''Data parallelism'''] , _convert_sagemaker_distributed_mode , ) lowerCamelCase_ ={} lowerCamelCase_ =_ask_field( '''Do you wish to optimize your script with torch dynamo?[yes/NO]:''' , _convert_yes_no_to_bool , default=__snake_case , error_message='''Please enter yes or no.''' , ) if use_dynamo: lowerCamelCase_ ='''dynamo_''' lowerCamelCase_ =_ask_options( '''Which dynamo backend would you like to use?''' , [x.lower() for x in DYNAMO_BACKENDS] , _convert_dynamo_backend , default=2 , ) lowerCamelCase_ =_ask_field( '''Do you want to customize the defaults sent to torch.compile? [yes/NO]: ''' , _convert_yes_no_to_bool , default=__snake_case , error_message='''Please enter yes or no.''' , ) if use_custom_options: lowerCamelCase_ =_ask_options( '''Which mode do you want to use?''' , __snake_case , lambda __snake_case : TORCH_DYNAMO_MODES[int(__snake_case )] , default='''default''' , ) lowerCamelCase_ =_ask_field( '''Do you want the fullgraph mode or it is ok to break model into several subgraphs? [yes/NO]: ''' , _convert_yes_no_to_bool , default=__snake_case , error_message='''Please enter yes or no.''' , ) lowerCamelCase_ =_ask_field( '''Do you want to enable dynamic shape tracing? [yes/NO]: ''' , _convert_yes_no_to_bool , default=__snake_case , error_message='''Please enter yes or no.''' , ) lowerCamelCase_ ='''Which EC2 instance type you want to use for your training?''' if distributed_type != SageMakerDistributedType.NO: lowerCamelCase_ =_ask_options( __snake_case , __snake_case , lambda __snake_case : SAGEMAKER_PARALLEL_EC2_INSTANCES[int(__snake_case )] ) else: eca_instance_query += "? [ml.p3.2xlarge]:" lowerCamelCase_ =_ask_field(__snake_case , lambda __snake_case : str(__snake_case ).lower() , default='''ml.p3.2xlarge''' ) lowerCamelCase_ =1 if distributed_type in (SageMakerDistributedType.DATA_PARALLEL, SageMakerDistributedType.MODEL_PARALLEL): lowerCamelCase_ =_ask_field( '''How many machines do you want use? [1]: ''' , __snake_case , default=1 , ) lowerCamelCase_ =_ask_options( '''Do you wish to use FP16 or BF16 (mixed precision)?''' , ['''no''', '''fp16''', '''bf16''', '''fp8'''] , _convert_mixed_precision , ) if use_dynamo and mixed_precision == "no": print( '''Torch dynamo used without mixed precision requires TF32 to be efficient. Accelerate will enable it by default when launching your scripts.''' ) return SageMakerConfig( image_uri=__snake_case , compute_environment=ComputeEnvironment.AMAZON_SAGEMAKER , distributed_type=__snake_case , use_cpu=__snake_case , dynamo_config=__snake_case , eca_instance_type=__snake_case , profile=__snake_case , region=__snake_case , iam_role_name=__snake_case , mixed_precision=__snake_case , num_machines=__snake_case , sagemaker_inputs_file=__snake_case , sagemaker_metrics_file=__snake_case , )
6
'''simple docstring''' from argparse import ArgumentParser from . import BaseTransformersCLICommand def a_ ( __snake_case : Tuple ) -> str: """simple docstring""" return DownloadCommand(args.model , args.cache_dir , args.force , args.trust_remote_code ) class __UpperCamelCase ( lowerCamelCase__ ): @staticmethod def lowercase__ ( lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =parser.add_parser('''download''' ) download_parser.add_argument( '''--cache-dir''', type=lowerCAmelCase, default=lowerCAmelCase, help='''Path to location to store the models''' ) download_parser.add_argument( '''--force''', action='''store_true''', help='''Force the model to be download even if already in cache-dir''' ) download_parser.add_argument( '''--trust-remote-code''', action='''store_true''', help='''Whether or not to allow for custom models defined on the Hub in their own modeling files. Use only if you\'ve reviewed the code as it will execute on your local machine''', ) download_parser.add_argument('''model''', type=lowerCAmelCase, help='''Name of the model to download''' ) download_parser.set_defaults(func=lowerCAmelCase ) def __init__( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =model lowerCamelCase_ =cache lowerCamelCase_ =force lowerCamelCase_ =trust_remote_code def lowercase__ ( self ): """simple docstring""" from ..models.auto import AutoModel, AutoTokenizer AutoModel.from_pretrained( self._model, cache_dir=self._cache, force_download=self._force, trust_remote_code=self._trust_remote_code ) AutoTokenizer.from_pretrained( self._model, cache_dir=self._cache, force_download=self._force, trust_remote_code=self._trust_remote_code )
6
1
'''simple docstring''' # Note: if you intend to run this script make sure you look under scripts/fsmt/ # to locate the appropriate script to do the work correctly. There is a set of scripts to: # - download and prepare data and run the conversion script # - perform eval to get the best hparam into the config # - generate model_cards - useful if you have multiple models from the same paper import argparse import json import os import re from collections import OrderedDict from os.path import basename, dirname import fairseq import torch from fairseq import hub_utils from fairseq.data.dictionary import Dictionary from transformers import FSMTConfig, FSMTForConditionalGeneration from transformers.models.fsmt.tokenization_fsmt import VOCAB_FILES_NAMES from transformers.tokenization_utils_base import TOKENIZER_CONFIG_FILE from transformers.utils import WEIGHTS_NAME, logging logging.set_verbosity_warning() a_ : Any = 2 # based on the results of a search on a range of `num_beams`, `length_penalty` and `early_stopping` # values against wmt19 test data to obtain the best BLEU scores, we will use the following defaults: # # * `num_beams`: 5 (higher scores better, but requires more memory/is slower, can be adjusted by users) # * `early_stopping`: `False` consistently scored better # * `length_penalty` varied, so will assign the best one depending on the model a_ : Optional[int] = { # fairseq: """wmt19-ru-en""": {"""length_penalty""": 1.1}, """wmt19-en-ru""": {"""length_penalty""": 1.15}, """wmt19-en-de""": {"""length_penalty""": 1.0}, """wmt19-de-en""": {"""length_penalty""": 1.1}, # allenai: """wmt16-en-de-dist-12-1""": {"""length_penalty""": 0.6}, """wmt16-en-de-dist-6-1""": {"""length_penalty""": 0.6}, """wmt16-en-de-12-1""": {"""length_penalty""": 0.8}, """wmt19-de-en-6-6-base""": {"""length_penalty""": 0.6}, """wmt19-de-en-6-6-big""": {"""length_penalty""": 0.6}, } # this remaps the different models to their organization names a_ : Union[str, Any] = {} for m in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: a_ : Dict = """facebook""" for m in [ "wmt16-en-de-dist-12-1", "wmt16-en-de-dist-6-1", "wmt16-en-de-12-1", "wmt19-de-en-6-6-base", "wmt19-de-en-6-6-big", ]: a_ : Any = """allenai""" def a_ ( __snake_case : Optional[Any] ) -> str: """simple docstring""" # (1) remove word breaking symbol, (2) add word ending symbol where the word is not broken up, # e.g.: d = {'le@@': 5, 'tt@@': 6, 'er': 7} => {'le': 5, 'tt': 6, 'er</w>': 7} lowerCamelCase_ =dict((re.sub(r'''@@$''' , '''''' , __snake_case ), v) if k.endswith('''@@''' ) else (re.sub(r'''$''' , '''</w>''' , __snake_case ), v) for k, v in d.items() ) lowerCamelCase_ ='''<s> <pad> </s> <unk>'''.split() # restore the special tokens for k in keep_keys: del da[F'''{k}</w>'''] lowerCamelCase_ =d[k] # restore return da def a_ ( __snake_case : Optional[Any] , __snake_case : Optional[int] ) -> int: """simple docstring""" # prep assert os.path.exists(__snake_case ) os.makedirs(__snake_case , exist_ok=__snake_case ) print(F'''Writing results to {pytorch_dump_folder_path}''' ) # handle various types of models lowerCamelCase_ =basename(__snake_case ) lowerCamelCase_ =dirname(__snake_case ) lowerCamelCase_ =fairseq.model_parallel.models.transformer.ModelParallelTransformerModel lowerCamelCase_ =cls.hub_models() lowerCamelCase_ ={'''bpe''': '''fastbpe''', '''tokenizer''': '''moses'''} lowerCamelCase_ ='''.''' # note: since the model dump is old, fairseq has upgraded its model some # time later, and it does a whole lot of rewrites and splits on the saved # weights, therefore we can't use torch.load() directly on the model file. # see: upgrade_state_dict(state_dict) in fairseq_model.py print(F'''using checkpoint {checkpoint_file}''' ) lowerCamelCase_ =hub_utils.from_pretrained( __snake_case , __snake_case , __snake_case , archive_map=__snake_case , **__snake_case ) lowerCamelCase_ =vars(chkpt['''args''']['''model'''] ) lowerCamelCase_ =args['''source_lang'''] lowerCamelCase_ =args['''target_lang'''] lowerCamelCase_ =dirname(__snake_case ) lowerCamelCase_ =basename(__snake_case ) # dicts lowerCamelCase_ =os.path.join(__snake_case , F'''dict.{src_lang}.txt''' ) lowerCamelCase_ =os.path.join(__snake_case , F'''dict.{tgt_lang}.txt''' ) lowerCamelCase_ =Dictionary.load(__snake_case ) lowerCamelCase_ =rewrite_dict_keys(src_dict.indices ) lowerCamelCase_ =len(__snake_case ) lowerCamelCase_ =os.path.join(__snake_case , '''vocab-src.json''' ) print(F'''Generating {src_vocab_file} of {src_vocab_size} of {src_lang} records''' ) with open(__snake_case , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(__snake_case , ensure_ascii=__snake_case , indent=__snake_case ) ) # detect whether this is a do_lower_case situation, which can be derived by checking whether we # have at least one uppercase letter in the source vocab lowerCamelCase_ =True for k in src_vocab.keys(): if not k.islower(): lowerCamelCase_ =False break lowerCamelCase_ =Dictionary.load(__snake_case ) lowerCamelCase_ =rewrite_dict_keys(tgt_dict.indices ) lowerCamelCase_ =len(__snake_case ) lowerCamelCase_ =os.path.join(__snake_case , '''vocab-tgt.json''' ) print(F'''Generating {tgt_vocab_file} of {tgt_vocab_size} of {tgt_lang} records''' ) with open(__snake_case , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(__snake_case , ensure_ascii=__snake_case , indent=__snake_case ) ) # merges_file (bpecodes) lowerCamelCase_ =os.path.join(__snake_case , VOCAB_FILES_NAMES['''merges_file'''] ) for fn in ["bpecodes", "code"]: # older fairseq called the merges file "code" lowerCamelCase_ =os.path.join(__snake_case , __snake_case ) if os.path.exists(__snake_case ): break with open(__snake_case , encoding='''utf-8''' ) as fin: lowerCamelCase_ =fin.read() lowerCamelCase_ =re.sub(r''' \d+$''' , '''''' , __snake_case , 0 , re.M ) # remove frequency number print(F'''Generating {merges_file}''' ) with open(__snake_case , '''w''' , encoding='''utf-8''' ) as fout: fout.write(__snake_case ) # model config lowerCamelCase_ =os.path.join(__snake_case , '''config.json''' ) # validate bpe/tokenizer config, as currently it's hardcoded to moses+fastbpe - # may have to modify the tokenizer if a different type is used by a future model assert args["bpe"] == "fastbpe", F'''need to extend tokenizer to support bpe={args['bpe']}''' assert args["tokenizer"] == "moses", F'''need to extend tokenizer to support bpe={args['tokenizer']}''' lowerCamelCase_ ={ '''architectures''': ['''FSMTForConditionalGeneration'''], '''model_type''': '''fsmt''', '''activation_dropout''': args['''activation_dropout'''], '''activation_function''': '''relu''', '''attention_dropout''': args['''attention_dropout'''], '''d_model''': args['''decoder_embed_dim'''], '''dropout''': args['''dropout'''], '''init_std''': 0.0_2, '''max_position_embeddings''': args['''max_source_positions'''], '''num_hidden_layers''': args['''encoder_layers'''], '''src_vocab_size''': src_vocab_size, '''tgt_vocab_size''': tgt_vocab_size, '''langs''': [src_lang, tgt_lang], '''encoder_attention_heads''': args['''encoder_attention_heads'''], '''encoder_ffn_dim''': args['''encoder_ffn_embed_dim'''], '''encoder_layerdrop''': args['''encoder_layerdrop'''], '''encoder_layers''': args['''encoder_layers'''], '''decoder_attention_heads''': args['''decoder_attention_heads'''], '''decoder_ffn_dim''': args['''decoder_ffn_embed_dim'''], '''decoder_layerdrop''': args['''decoder_layerdrop'''], '''decoder_layers''': args['''decoder_layers'''], '''bos_token_id''': 0, '''pad_token_id''': 1, '''eos_token_id''': 2, '''is_encoder_decoder''': True, '''scale_embedding''': not args['''no_scale_embedding'''], '''tie_word_embeddings''': args['''share_all_embeddings'''], } # good hparam defaults to start with lowerCamelCase_ =5 lowerCamelCase_ =False if model_dir in best_score_hparams and "length_penalty" in best_score_hparams[model_dir]: lowerCamelCase_ =best_score_hparams[model_dir]['''length_penalty'''] else: lowerCamelCase_ =1.0 print(F'''Generating {fsmt_model_config_file}''' ) with open(__snake_case , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(__snake_case , ensure_ascii=__snake_case , indent=__snake_case ) ) # tokenizer config lowerCamelCase_ =os.path.join(__snake_case , __snake_case ) lowerCamelCase_ ={ '''langs''': [src_lang, tgt_lang], '''model_max_length''': 1024, '''do_lower_case''': do_lower_case, } print(F'''Generating {fsmt_tokenizer_config_file}''' ) with open(__snake_case , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(__snake_case , ensure_ascii=__snake_case , indent=__snake_case ) ) # model lowerCamelCase_ =chkpt['''models'''][0] lowerCamelCase_ =model.state_dict() # rename keys to start with 'model.' lowerCamelCase_ =OrderedDict(('''model.''' + k, v) for k, v in model_state_dict.items() ) # remove unneeded keys lowerCamelCase_ =[ '''model.model''', '''model.encoder.version''', '''model.decoder.version''', '''model.encoder_embed_tokens.weight''', '''model.decoder_embed_tokens.weight''', '''model.encoder.embed_positions._float_tensor''', '''model.decoder.embed_positions._float_tensor''', ] for k in ignore_keys: model_state_dict.pop(__snake_case , __snake_case ) lowerCamelCase_ =FSMTConfig.from_pretrained(__snake_case ) lowerCamelCase_ =FSMTForConditionalGeneration(__snake_case ) # check that it loads ok model_new.load_state_dict(__snake_case , strict=__snake_case ) # save lowerCamelCase_ =os.path.join(__snake_case , __snake_case ) print(F'''Generating {pytorch_weights_dump_path}''' ) torch.save(__snake_case , __snake_case ) print('''Conversion is done!''' ) print('''\nLast step is to upload the files to s3''' ) print(F'''cd {data_root}''' ) print(F'''transformers-cli upload {model_dir}''' ) if __name__ == "__main__": a_ : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--fsmt_checkpoint_path""", default=None, type=str, required=True, help=( """Path to the official PyTorch checkpoint file which is expected to reside in the dump dir with dicts,""" """ bpecodes, etc.""" ), ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model.""" ) a_ : Any = parser.parse_args() convert_fsmt_checkpoint_to_pytorch(args.fsmt_checkpoint_path, args.pytorch_dump_folder_path)
6
'''simple docstring''' import os import time from dataclasses import dataclass, field from enum import Enum from typing import Dict, List, Optional, Union import torch from filelock import FileLock from torch.utils.data import Dataset from ...models.auto.modeling_auto import MODEL_FOR_QUESTION_ANSWERING_MAPPING from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging from ..processors.squad import SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features a_ : List[str] = logging.get_logger(__name__) a_ : Optional[Any] = list(MODEL_FOR_QUESTION_ANSWERING_MAPPING.keys()) a_ : Optional[Any] = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class __UpperCamelCase : lowercase : str =field( default=lowerCamelCase__ , metadata={'help': 'Model type selected in the list: ' + ', '.join(lowerCamelCase__ )} ) lowercase : str =field( default=lowerCamelCase__ , metadata={'help': 'The input data dir. Should contain the .json files for the SQuAD task.'} ) lowercase : int =field( default=1_28 , metadata={ 'help': ( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) } , ) lowercase : int =field( default=1_28 , metadata={'help': 'When splitting up a long document into chunks, how much stride to take between chunks.'} , ) lowercase : int =field( default=64 , metadata={ 'help': ( 'The maximum number of tokens for the question. Questions longer than this will ' 'be truncated to this length.' ) } , ) lowercase : int =field( default=30 , metadata={ 'help': ( 'The maximum length of an answer that can be generated. This is needed because the start ' 'and end predictions are not conditioned on one another.' ) } , ) lowercase : bool =field( default=lowerCamelCase__ , metadata={'help': 'Overwrite the cached training and evaluation sets'} ) lowercase : bool =field( default=lowerCamelCase__ , metadata={'help': 'If true, the SQuAD examples contain some that do not have an answer.'} ) lowercase : float =field( default=0.0 , metadata={'help': 'If null_score - best_non_null is greater than the threshold predict null.'} ) lowercase : int =field( default=20 , metadata={'help': 'If null_score - best_non_null is greater than the threshold predict null.'} ) lowercase : int =field( default=0 , metadata={ 'help': ( 'language id of input for language-specific xlm models (see' ' tokenization_xlm.PRETRAINED_INIT_CONFIGURATION)' ) } , ) lowercase : int =field(default=1 , metadata={'help': 'multiple threads for converting example to features'} ) class __UpperCamelCase ( lowerCamelCase__ ): lowercase : Optional[Any] ='train' lowercase : Any ='dev' class __UpperCamelCase ( lowerCamelCase__ ): lowercase : SquadDataTrainingArguments lowercase : List[SquadFeatures] lowercase : Split lowercase : bool def __init__( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase = None, lowerCAmelCase = Split.train, lowerCAmelCase = False, lowerCAmelCase = None, lowerCAmelCase = "pt", ): """simple docstring""" lowerCamelCase_ =args lowerCamelCase_ =is_language_sensitive lowerCamelCase_ =SquadVaProcessor() if args.version_2_with_negative else SquadVaProcessor() if isinstance(lowerCAmelCase, lowerCAmelCase ): try: lowerCamelCase_ =Split[mode] except KeyError: raise KeyError('''mode is not a valid split name''' ) lowerCamelCase_ =mode # Load data features from cache or dataset file lowerCamelCase_ ='''v2''' if args.version_2_with_negative else '''v1''' lowerCamelCase_ =os.path.join( cache_dir if cache_dir is not None else args.data_dir, f'''cached_{mode.value}_{tokenizer.__class__.__name__}_{args.max_seq_length}_{version_tag}''', ) # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. lowerCamelCase_ =cached_features_file + '''.lock''' with FileLock(lowerCAmelCase ): if os.path.exists(lowerCAmelCase ) and not args.overwrite_cache: lowerCamelCase_ =time.time() lowerCamelCase_ =torch.load(lowerCAmelCase ) # Legacy cache files have only features, while new cache files # will have dataset and examples also. lowerCamelCase_ =self.old_features['''features'''] lowerCamelCase_ =self.old_features.get('''dataset''', lowerCAmelCase ) lowerCamelCase_ =self.old_features.get('''examples''', lowerCAmelCase ) logger.info( f'''Loading features from cached file {cached_features_file} [took %.3f s]''', time.time() - start ) if self.dataset is None or self.examples is None: logger.warning( f'''Deleting cached file {cached_features_file} will allow dataset and examples to be cached in''' ''' future run''' ) else: if mode == Split.dev: lowerCamelCase_ =self.processor.get_dev_examples(args.data_dir ) else: lowerCamelCase_ =self.processor.get_train_examples(args.data_dir ) lowerCamelCase_, lowerCamelCase_ =squad_convert_examples_to_features( examples=self.examples, tokenizer=lowerCAmelCase, max_seq_length=args.max_seq_length, doc_stride=args.doc_stride, max_query_length=args.max_query_length, is_training=mode == Split.train, threads=args.threads, return_dataset=lowerCAmelCase, ) lowerCamelCase_ =time.time() torch.save( {'''features''': self.features, '''dataset''': self.dataset, '''examples''': self.examples}, lowerCAmelCase, ) # ^ This seems to take a lot of time so I want to investigate why and how we can improve. logger.info( f'''Saving features into cached file {cached_features_file} [took {time.time() - start:.3f} s]''' ) def __len__( self ): """simple docstring""" return len(self.features ) def __getitem__( self, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =self.features[i] lowerCamelCase_ =torch.tensor(feature.input_ids, dtype=torch.long ) lowerCamelCase_ =torch.tensor(feature.attention_mask, dtype=torch.long ) lowerCamelCase_ =torch.tensor(feature.token_type_ids, dtype=torch.long ) lowerCamelCase_ =torch.tensor(feature.cls_index, dtype=torch.long ) lowerCamelCase_ =torch.tensor(feature.p_mask, dtype=torch.float ) lowerCamelCase_ =torch.tensor(feature.is_impossible, dtype=torch.float ) lowerCamelCase_ ={ '''input_ids''': input_ids, '''attention_mask''': attention_mask, '''token_type_ids''': token_type_ids, } if self.args.model_type in ["xlm", "roberta", "distilbert", "camembert"]: del inputs["token_type_ids"] if self.args.model_type in ["xlnet", "xlm"]: inputs.update({'''cls_index''': cls_index, '''p_mask''': p_mask} ) if self.args.version_2_with_negative: inputs.update({'''is_impossible''': is_impossible} ) if self.is_language_sensitive: inputs.update({'''langs''': (torch.ones(input_ids.shape, dtype=torch.intaa ) * self.args.lang_id)} ) if self.mode == Split.train: lowerCamelCase_ =torch.tensor(feature.start_position, dtype=torch.long ) lowerCamelCase_ =torch.tensor(feature.end_position, dtype=torch.long ) inputs.update({'''start_positions''': start_positions, '''end_positions''': end_positions} ) return inputs
6
1
'''simple docstring''' from collections import Counter import numpy as np from sklearn import datasets from sklearn.model_selection import train_test_split a_ : Tuple = datasets.load_iris() a_ : List[str] = np.array(data["""data"""]) a_ : Union[str, Any] = np.array(data["""target"""]) a_ : int = data["""target_names"""] a_ , a_ , a_ , a_ : Optional[Any] = train_test_split(X, y) def a_ ( __snake_case : Union[str, Any] , __snake_case : Tuple ) -> Tuple: """simple docstring""" return np.linalg.norm(np.array(__snake_case ) - np.array(__snake_case ) ) def a_ ( __snake_case : int , __snake_case : Optional[int] , __snake_case : Optional[Any] , __snake_case : Any , __snake_case : Tuple=5 ) -> Union[str, Any]: """simple docstring""" lowerCamelCase_ =zip(__snake_case , __snake_case ) # List of distances of all points from the point to be classified lowerCamelCase_ =[] for data_point in data: lowerCamelCase_ =euclidean_distance(data_point[0] , __snake_case ) distances.append((distance, data_point[1]) ) # Choosing 'k' points with the least distances. lowerCamelCase_ =[i[1] for i in sorted(__snake_case )[:k]] # Most commonly occurring class among them # is the class into which the point is classified lowerCamelCase_ =Counter(__snake_case ).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]))
6
'''simple docstring''' import argparse import json import os from pathlib import Path import requests import torch from transformers import JukeboxConfig, JukeboxModel from transformers.utils import logging logging.set_verbosity_info() a_ : Any = logging.get_logger(__name__) a_ : Optional[int] = """https://openaipublic.azureedge.net/jukebox/models/""" a_ : Any = { """jukebox-1b-lyrics""": [ """5b/vqvae.pth.tar""", """5b/prior_level_0.pth.tar""", """5b/prior_level_1.pth.tar""", """1b_lyrics/prior_level_2.pth.tar""", ], """jukebox-5b-lyrics""": [ """5b/vqvae.pth.tar""", """5b/prior_level_0.pth.tar""", """5b/prior_level_1.pth.tar""", """5b_lyrics/prior_level_2.pth.tar""", ], } def a_ ( __snake_case : int ) -> Any: """simple docstring""" if key.endswith('''.model.1.bias''' ) and len(key.split('''.''' ) ) > 10: lowerCamelCase_ =key.replace('''.model.1.bias''' , '''.conv1d_1.bias''' ) elif key.endswith('''.model.1.weight''' ) and len(key.split('''.''' ) ) > 10: lowerCamelCase_ =key.replace('''.model.1.weight''' , '''.conv1d_1.weight''' ) elif key.endswith('''.model.3.bias''' ) and len(key.split('''.''' ) ) > 10: lowerCamelCase_ =key.replace('''.model.3.bias''' , '''.conv1d_2.bias''' ) elif key.endswith('''.model.3.weight''' ) and len(key.split('''.''' ) ) > 10: lowerCamelCase_ =key.replace('''.model.3.weight''' , '''.conv1d_2.weight''' ) if "conditioner_blocks.0." in key: lowerCamelCase_ =key.replace('''conditioner_blocks.0''' , '''conditioner_blocks''' ) if "prime_prior" in key: lowerCamelCase_ =key.replace('''prime_prior''' , '''encoder''' ) if ".emb." in key and "total" not in key and "absolute" not in key and "relative" not in key: lowerCamelCase_ =key.replace('''.emb.''' , '''.''' ) if key.endswith('''k''' ): # replace vqvae.X.k with vqvae.X.codebook return key.replace('''.k''' , '''.codebook''' ) if "y_emb." in key: return key.replace('''y_emb.''' , '''metadata_embedding.''' ) if "x_emb.emb." in key: lowerCamelCase_ =key.replace('''0.x_emb.emb''' , '''embed_tokens''' ) if "prime_state_ln" in key: return key.replace('''prime_state_ln''' , '''encoder.final_layer_norm''' ) if ".ln" in key: return key.replace('''.ln''' , '''.layer_norm''' ) if "_ln" in key: return key.replace('''_ln''' , '''_layer_norm''' ) if "prime_state_proj" in key: return key.replace('''prime_state_proj''' , '''encoder.proj_in''' ) if "prime_x_out" in key: return key.replace('''prime_x_out''' , '''encoder.lm_head''' ) if "prior.x_out" in key: return key.replace('''x_out''' , '''fc_proj_out''' ) if "x_emb" in key: return key.replace('''x_emb''' , '''embed_tokens''' ) return key def a_ ( __snake_case : Dict , __snake_case : int , __snake_case : Dict , __snake_case : Optional[Any] ) -> Optional[Any]: """simple docstring""" lowerCamelCase_ ={} import re lowerCamelCase_ =re.compile(r'''encoders.(\d*).level_blocks.(\d*).model.(\d*).(\d).(bias|weight)''' ) lowerCamelCase_ =re.compile( r'''encoders.(\d*).level_blocks.(\d*).model.(\d*).(\d).model.(\d*).model.(\d*).(bias|weight)''' ) lowerCamelCase_ =re.compile(r'''encoders.(\d*).level_blocks.(\d*).model.(\d*).(bias|weight)''' ) lowerCamelCase_ =re.compile(r'''decoders.(\d*).level_blocks.(\d*).model.(\d*).(\d).(bias|weight)''' ) lowerCamelCase_ =re.compile( r'''decoders.(\d*).level_blocks.(\d*).model.(\d*).(\d).model.(\d*).model.(\d*).(bias|weight)''' ) lowerCamelCase_ =re.compile(r'''decoders.(\d*).level_blocks.(\d*).model.(\d*).(bias|weight)''' ) lowerCamelCase_ =re.compile(r'''conditioner_blocks.(\d*).cond.model.(\d*).(\d).(bias|weight)''' ) lowerCamelCase_ =re.compile( r'''conditioner_blocks.(\d*).cond.model.(\d*).(\d).model.(\d*).model.(\d*).(bias|weight)''' ) lowerCamelCase_ =re.compile(r'''conditioner_blocks.(\d*).cond.model.(\d*).(bias|weight)''' ) for original_key, value in state_dict.items(): # rename vqvae.encoder keys if re_encoder_block_conv_in.fullmatch(__snake_case ): lowerCamelCase_ =re_encoder_block_conv_in.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =int(groups[2] ) * 2 + int(groups[3] ) lowerCamelCase_ =F'''encoders.{groups[0]}.level_blocks.{groups[1]}.downsample_block.{block_index}.{groups[-1]}''' lowerCamelCase_ =re_encoder_block_conv_in.sub(__snake_case , __snake_case ) elif re_encoder_block_resnet.fullmatch(__snake_case ): lowerCamelCase_ =re_encoder_block_resnet.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =int(groups[2] ) * 2 + int(groups[3] ) lowerCamelCase_ ={'''1''': 1, '''3''': 2}[groups[-2]] lowerCamelCase_ =F'''encoders.{groups[0]}.level_blocks.{groups[1]}.downsample_block.{block_index}.''' lowerCamelCase_ =F'''resnet_block.{groups[-3]}.conv1d_{conv_index}.{groups[-1]}''' lowerCamelCase_ =prefix + resnet_block lowerCamelCase_ =re_encoder_block_resnet.sub(__snake_case , __snake_case ) elif re_encoder_block_proj_out.fullmatch(__snake_case ): lowerCamelCase_ =re_encoder_block_proj_out.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =F'''encoders.{groups[0]}.level_blocks.{groups[1]}.proj_out.{groups[-1]}''' lowerCamelCase_ =re_encoder_block_proj_out.sub(__snake_case , __snake_case ) # rename vqvae.decoder keys elif re_decoder_block_conv_out.fullmatch(__snake_case ): lowerCamelCase_ =re_decoder_block_conv_out.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =int(groups[2] ) * 2 + int(groups[3] ) - 2 lowerCamelCase_ =F'''decoders.{groups[0]}.level_blocks.{groups[1]}.upsample_block.{block_index}.{groups[-1]}''' lowerCamelCase_ =re_decoder_block_conv_out.sub(__snake_case , __snake_case ) elif re_decoder_block_resnet.fullmatch(__snake_case ): lowerCamelCase_ =re_decoder_block_resnet.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =int(groups[2] ) * 2 + int(groups[3] ) - 2 lowerCamelCase_ ={'''1''': 1, '''3''': 2}[groups[-2]] lowerCamelCase_ =F'''decoders.{groups[0]}.level_blocks.{groups[1]}.upsample_block.{block_index}.''' lowerCamelCase_ =F'''resnet_block.{groups[-3]}.conv1d_{conv_index}.{groups[-1]}''' lowerCamelCase_ =prefix + resnet_block lowerCamelCase_ =re_decoder_block_resnet.sub(__snake_case , __snake_case ) elif re_decoder_block_proj_in.fullmatch(__snake_case ): lowerCamelCase_ =re_decoder_block_proj_in.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =F'''decoders.{groups[0]}.level_blocks.{groups[1]}.proj_in.{groups[-1]}''' lowerCamelCase_ =re_decoder_block_proj_in.sub(__snake_case , __snake_case ) # rename prior cond.model to upsampler.upsample_block and resnet elif re_prior_cond_conv_out.fullmatch(__snake_case ): lowerCamelCase_ =re_prior_cond_conv_out.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =int(groups[1] ) * 2 + int(groups[2] ) - 2 lowerCamelCase_ =F'''conditioner_blocks.upsampler.upsample_block.{block_index}.{groups[-1]}''' lowerCamelCase_ =re_prior_cond_conv_out.sub(__snake_case , __snake_case ) elif re_prior_cond_resnet.fullmatch(__snake_case ): lowerCamelCase_ =re_prior_cond_resnet.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =int(groups[1] ) * 2 + int(groups[2] ) - 2 lowerCamelCase_ ={'''1''': 1, '''3''': 2}[groups[-2]] lowerCamelCase_ =F'''conditioner_blocks.upsampler.upsample_block.{block_index}.''' lowerCamelCase_ =F'''resnet_block.{groups[-3]}.conv1d_{conv_index}.{groups[-1]}''' lowerCamelCase_ =prefix + resnet_block lowerCamelCase_ =re_prior_cond_resnet.sub(__snake_case , __snake_case ) elif re_prior_cond_proj_in.fullmatch(__snake_case ): lowerCamelCase_ =re_prior_cond_proj_in.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =F'''conditioner_blocks.upsampler.proj_in.{groups[-1]}''' lowerCamelCase_ =re_prior_cond_proj_in.sub(__snake_case , __snake_case ) # keep original key else: lowerCamelCase_ =original_key lowerCamelCase_ =replace_key(__snake_case ) if F'''{key_prefix}.{key}''' not in model_state_dict or key is None: print(F'''failed converting {original_key} to {key}, does not match''' ) # handle missmatched shape elif value.shape != model_state_dict[F'''{key_prefix}.{key}'''].shape: lowerCamelCase_ =model_state_dict[F'''{key_prefix}.{key}'''] print(F'''{original_key}-> {key} : \nshape {val.shape} and { value.shape}, do not match''' ) lowerCamelCase_ =original_key lowerCamelCase_ =original_key lowerCamelCase_ =value return new_dict @torch.no_grad() def a_ ( __snake_case : List[str]=None , __snake_case : Tuple=None ) -> Union[str, Any]: """simple docstring""" for file in MODEL_MAPPING[model_name]: if not os.path.isfile(F'''{pytorch_dump_folder_path}/{file.split('/' )[-1]}''' ): lowerCamelCase_ =requests.get(F'''{PREFIX}{file}''' , allow_redirects=__snake_case ) os.makedirs(F'''{pytorch_dump_folder_path}/''' , exist_ok=__snake_case ) open(F'''{pytorch_dump_folder_path}/{file.split('/' )[-1]}''' , '''wb''' ).write(r.content ) lowerCamelCase_ =MODEL_MAPPING[model_name.split('''/''' )[-1]] lowerCamelCase_ =JukeboxConfig.from_pretrained(__snake_case ) lowerCamelCase_ =JukeboxModel(__snake_case ) lowerCamelCase_ =[] lowerCamelCase_ ={} for i, dict_name in enumerate(__snake_case ): lowerCamelCase_ =torch.load(F'''{pytorch_dump_folder_path}/{dict_name.split('/' )[-1]}''' )['''model'''] lowerCamelCase_ ={} for k in old_dic.keys(): if k.endswith('''.b''' ): lowerCamelCase_ =old_dic[k] elif k.endswith('''.w''' ): lowerCamelCase_ =old_dic[k] elif "level_2" not in dict_name and "cond.model." in k: lowerCamelCase_ =old_dic[k] else: lowerCamelCase_ =old_dic[k] lowerCamelCase_ ='''vqvae''' if i == 0 else F'''priors.{3 - i}''' lowerCamelCase_ =fix_jukebox_keys(__snake_case , model.state_dict() , __snake_case , __snake_case ) weight_dict.append(__snake_case ) lowerCamelCase_ =weight_dict.pop(0 ) model.vqvae.load_state_dict(__snake_case ) for i in range(len(__snake_case ) ): model.priors[i].load_state_dict(weight_dict[2 - i] ) Path(__snake_case ).mkdir(exist_ok=__snake_case ) with open(F'''{pytorch_dump_folder_path}/mapping.json''' , '''w''' ) as txtfile: json.dump(__snake_case , __snake_case ) print(F'''Saving model {model_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(__snake_case ) return weight_dict if __name__ == "__main__": a_ : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default="""jukebox-5b-lyrics""", type=str, help="""Name of the model you'd like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default="""jukebox-5b-lyrics-converted""", type=str, help="""Path to the output PyTorch model directory.""", ) a_ : Optional[int] = parser.parse_args() convert_openai_checkpoint(args.model_name, args.pytorch_dump_folder_path)
6
1
'''simple docstring''' from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, logging if is_torch_available(): import torch a_ : List[Any] = logging.get_logger(__name__) class __UpperCamelCase ( lowerCamelCase__ ): lowercase : Tuple =['pixel_values'] def __init__( self, lowerCAmelCase = True, lowerCAmelCase = None, lowerCAmelCase = PILImageResampling.BILINEAR, lowerCAmelCase = True, lowerCAmelCase = None, lowerCAmelCase = True, lowerCAmelCase = 1 / 255, lowerCAmelCase = True, lowerCAmelCase = None, lowerCAmelCase = None, **lowerCAmelCase, ): """simple docstring""" super().__init__(**lowerCAmelCase ) lowerCamelCase_ =size if size is not None else {'''shortest_edge''': 256} lowerCamelCase_ =get_size_dict(lowerCAmelCase, default_to_square=lowerCAmelCase ) lowerCamelCase_ =crop_size if crop_size is not None else {'''height''': 224, '''width''': 224} lowerCamelCase_ =get_size_dict(lowerCAmelCase, param_name='''crop_size''' ) 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 if image_mean is not None else IMAGENET_STANDARD_MEAN lowerCamelCase_ =image_std if image_std is not None else IMAGENET_STANDARD_STD def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase = PILImageResampling.BICUBIC, lowerCAmelCase = None, **lowerCAmelCase, ): """simple docstring""" lowerCamelCase_ =get_size_dict(lowerCAmelCase, default_to_square=lowerCAmelCase ) if "shortest_edge" not in size: raise ValueError(f'''The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}''' ) lowerCamelCase_ =get_resize_output_image_size(lowerCAmelCase, size=size['''shortest_edge'''], default_to_square=lowerCAmelCase ) return resize(lowerCAmelCase, size=lowerCAmelCase, resample=lowerCAmelCase, data_format=lowerCAmelCase, **lowerCAmelCase ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase = None, **lowerCAmelCase, ): """simple docstring""" lowerCamelCase_ =get_size_dict(lowerCAmelCase ) if "height" not in size or "width" not in size: raise ValueError(f'''The `size` parameter must contain the keys `height` and `width`. Got {size.keys()}''' ) return center_crop(lowerCAmelCase, size=(size['''height'''], size['''width''']), data_format=lowerCAmelCase, **lowerCAmelCase ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase = None, **lowerCAmelCase ): """simple docstring""" return rescale(lowerCAmelCase, scale=lowerCAmelCase, data_format=lowerCAmelCase, **lowerCAmelCase ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase = None, **lowerCAmelCase, ): """simple docstring""" return normalize(lowerCAmelCase, mean=lowerCAmelCase, std=lowerCAmelCase, data_format=lowerCAmelCase, **lowerCAmelCase ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = ChannelDimension.FIRST, **lowerCAmelCase, ): """simple docstring""" lowerCamelCase_ =do_resize if do_resize is not None else self.do_resize lowerCamelCase_ =size if size is not None else self.size lowerCamelCase_ =get_size_dict(lowerCAmelCase, default_to_square=lowerCAmelCase ) lowerCamelCase_ =resample if resample is not None else self.resample lowerCamelCase_ =do_center_crop if do_center_crop is not None else self.do_center_crop lowerCamelCase_ =crop_size if crop_size is not None else self.crop_size lowerCamelCase_ =get_size_dict(lowerCAmelCase, param_name='''crop_size''' ) lowerCamelCase_ =do_rescale if do_rescale is not None else self.do_rescale lowerCamelCase_ =rescale_factor if rescale_factor is not None else self.rescale_factor lowerCamelCase_ =do_normalize if do_normalize is not None else self.do_normalize lowerCamelCase_ =image_mean if image_mean is not None else self.image_mean lowerCamelCase_ =image_std if image_std is not None else self.image_std lowerCamelCase_ =make_list_of_images(lowerCAmelCase ) 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.''' ) if do_resize and size is None: raise ValueError('''Size must be specified if do_resize is True.''' ) if do_center_crop and crop_size is None: raise ValueError('''Crop size must be specified if do_center_crop is True.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) # All transformations expect numpy arrays. lowerCamelCase_ =[to_numpy_array(lowerCAmelCase ) for image in images] if do_resize: lowerCamelCase_ =[self.resize(image=lowerCAmelCase, size=lowerCAmelCase, resample=lowerCAmelCase ) for image in images] if do_center_crop: lowerCamelCase_ =[self.center_crop(image=lowerCAmelCase, size=lowerCAmelCase ) for image in images] if do_rescale: lowerCamelCase_ =[self.rescale(image=lowerCAmelCase, scale=lowerCAmelCase ) for image in images] if do_normalize: lowerCamelCase_ =[self.normalize(image=lowerCAmelCase, mean=lowerCAmelCase, std=lowerCAmelCase ) for image in images] lowerCamelCase_ =[to_channel_dimension_format(lowerCAmelCase, lowerCAmelCase ) for image in images] lowerCamelCase_ ={'''pixel_values''': images} return BatchFeature(data=lowerCAmelCase, tensor_type=lowerCAmelCase ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase = None ): """simple docstring""" lowerCamelCase_ =outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(lowerCAmelCase ) != len(lowerCAmelCase ): raise ValueError( '''Make sure that you pass in as many target sizes as the batch dimension of the logits''' ) if is_torch_tensor(lowerCAmelCase ): lowerCamelCase_ =target_sizes.numpy() lowerCamelCase_ =[] for idx in range(len(lowerCAmelCase ) ): lowerCamelCase_ =torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ), size=target_sizes[idx], mode='''bilinear''', align_corners=lowerCAmelCase ) lowerCamelCase_ =resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(lowerCAmelCase ) else: lowerCamelCase_ =logits.argmax(dim=1 ) lowerCamelCase_ =[semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
6
'''simple docstring''' def a_ ( __snake_case : int = 1000 ) -> int: """simple docstring""" lowerCamelCase_, lowerCamelCase_ =1, 1 lowerCamelCase_ =2 while True: lowerCamelCase_ =0 lowerCamelCase_ =fa + fa lowerCamelCase_, lowerCamelCase_ =fa, f index += 1 for _ in str(__snake_case ): i += 1 if i == n: break return index if __name__ == "__main__": print(solution(int(str(input()).strip())))
6
1
'''simple docstring''' # flake8: noqa # Lint as: python3 a_ : int = [ """VerificationMode""", """Version""", """disable_progress_bar""", """enable_progress_bar""", """is_progress_bar_enabled""", """experimental""", ] from .info_utils import VerificationMode from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled from .version import Version from .experimental import experimental
6
'''simple docstring''' import importlib import os import sys # This is required to make the module import works (when the python process is running from the root of the repo) sys.path.append(""".""") def a_ ( __snake_case : Any ) -> Tuple: """simple docstring""" lowerCamelCase_ =test_file.split(os.path.sep ) if components[0:2] != ["tests", "models"]: raise ValueError( '''`test_file` should start with `tests/models/` (with `/` being the OS specific path separator). Got ''' F'''{test_file} instead.''' ) lowerCamelCase_ =components[-1] if not test_fn.endswith('''py''' ): raise ValueError(F'''`test_file` should be a python file. Got {test_fn} instead.''' ) if not test_fn.startswith('''test_modeling_''' ): raise ValueError( F'''`test_file` should point to a file name of the form `test_modeling_*.py`. Got {test_fn} instead.''' ) lowerCamelCase_ =components[:-1] + [test_fn.replace('''.py''' , '''''' )] lowerCamelCase_ ='''.'''.join(__snake_case ) return test_module_path def a_ ( __snake_case : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" lowerCamelCase_ =get_module_path(__snake_case ) lowerCamelCase_ =importlib.import_module(__snake_case ) return test_module def a_ ( __snake_case : Dict ) -> Tuple: """simple docstring""" lowerCamelCase_ =[] lowerCamelCase_ =get_test_module(__snake_case ) for attr in dir(__snake_case ): if attr.endswith('''ModelTester''' ): tester_classes.append(getattr(__snake_case , __snake_case ) ) # sort with class names return sorted(__snake_case , key=lambda __snake_case : x.__name__ ) def a_ ( __snake_case : List[Any] ) -> Optional[int]: """simple docstring""" lowerCamelCase_ =[] lowerCamelCase_ =get_test_module(__snake_case ) for attr in dir(__snake_case ): lowerCamelCase_ =getattr(__snake_case , __snake_case ) # (TF/Flax)ModelTesterMixin is also an attribute in specific model test module. Let's exclude them by checking # `all_model_classes` is not empty (which also excludes other special classes). lowerCamelCase_ =getattr(__snake_case , '''all_model_classes''' , [] ) if len(__snake_case ) > 0: test_classes.append(__snake_case ) # sort with class names return sorted(__snake_case , key=lambda __snake_case : x.__name__ ) def a_ ( __snake_case : List[Any] ) -> Optional[int]: """simple docstring""" lowerCamelCase_ =get_test_classes(__snake_case ) lowerCamelCase_ =set() for test_class in test_classes: model_classes.update(test_class.all_model_classes ) # sort with class names return sorted(__snake_case , key=lambda __snake_case : x.__name__ ) def a_ ( __snake_case : str ) -> str: """simple docstring""" lowerCamelCase_ =test_class() if hasattr(__snake_case , '''setUp''' ): test.setUp() lowerCamelCase_ =None if hasattr(__snake_case , '''model_tester''' ): # `(TF/Flax)ModelTesterMixin` has this attribute default to `None`. Let's skip this case. if test.model_tester is not None: lowerCamelCase_ =test.model_tester.__class__ return model_tester def a_ ( __snake_case : Dict , __snake_case : List[str] ) -> List[Any]: """simple docstring""" lowerCamelCase_ =get_test_classes(__snake_case ) lowerCamelCase_ =[] for test_class in test_classes: if model_class in test_class.all_model_classes: target_test_classes.append(__snake_case ) # sort with class names return sorted(__snake_case , key=lambda __snake_case : x.__name__ ) def a_ ( __snake_case : Optional[Any] , __snake_case : List[str] ) -> Any: """simple docstring""" lowerCamelCase_ =get_test_classes_for_model(__snake_case , __snake_case ) lowerCamelCase_ =[] for test_class in test_classes: lowerCamelCase_ =get_model_tester_from_test_class(__snake_case ) if tester_class is not None: tester_classes.append(__snake_case ) # sort with class names return sorted(__snake_case , key=lambda __snake_case : x.__name__ ) def a_ ( __snake_case : Tuple ) -> Tuple: """simple docstring""" lowerCamelCase_ =get_test_classes(__snake_case ) lowerCamelCase_ ={test_class: get_model_tester_from_test_class(__snake_case ) for test_class in test_classes} return test_tester_mapping def a_ ( __snake_case : Dict ) -> Optional[Any]: """simple docstring""" lowerCamelCase_ =get_model_classes(__snake_case ) lowerCamelCase_ ={ model_class: get_test_classes_for_model(__snake_case , __snake_case ) for model_class in model_classes } return model_test_mapping def a_ ( __snake_case : Optional[Any] ) -> Dict: """simple docstring""" lowerCamelCase_ =get_model_classes(__snake_case ) lowerCamelCase_ ={ model_class: get_tester_classes_for_model(__snake_case , __snake_case ) for model_class in model_classes } return model_to_tester_mapping def a_ ( __snake_case : List[str] ) -> List[Any]: """simple docstring""" if isinstance(__snake_case , __snake_case ): return o elif isinstance(__snake_case , __snake_case ): return o.__name__ elif isinstance(__snake_case , (list, tuple) ): return [to_json(__snake_case ) for x in o] elif isinstance(__snake_case , __snake_case ): return {to_json(__snake_case ): to_json(__snake_case ) for k, v in o.items()} else: return o
6
1
'''simple docstring''' import hashlib import unittest from transformers import MODEL_FOR_DEPTH_ESTIMATION_MAPPING, is_torch_available, is_vision_available from transformers.pipelines import DepthEstimationPipeline, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_timm, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_torch_available(): import torch if is_vision_available(): from PIL import Image else: class __UpperCamelCase : @staticmethod def lowercase__ ( *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" pass def a_ ( __snake_case : Image ) -> str: """simple docstring""" lowerCamelCase_ =hashlib.mda(image.tobytes() ) return m.hexdigest() @is_pipeline_test @require_vision @require_timm @require_torch class __UpperCamelCase ( unittest.TestCase ): lowercase : Optional[Any] =MODEL_FOR_DEPTH_ESTIMATION_MAPPING def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =DepthEstimationPipeline(model=lowerCAmelCase, image_processor=lowerCAmelCase ) return depth_estimator, [ "./tests/fixtures/tests_samples/COCO/000000039769.png", "./tests/fixtures/tests_samples/COCO/000000039769.png", ] def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =depth_estimator('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) self.assertEqual({'''predicted_depth''': ANY(torch.Tensor ), '''depth''': ANY(Image.Image )}, lowerCAmelCase ) import datasets lowerCamelCase_ =datasets.load_dataset('''hf-internal-testing/fixtures_image_utils''', '''image''', split='''test''' ) lowerCamelCase_ =depth_estimator( [ Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ), '''http://images.cocodataset.org/val2017/000000039769.jpg''', # RGBA dataset[0]['''file'''], # LA dataset[1]['''file'''], # L dataset[2]['''file'''], ] ) self.assertEqual( [ {'''predicted_depth''': ANY(torch.Tensor ), '''depth''': ANY(Image.Image )}, {'''predicted_depth''': ANY(torch.Tensor ), '''depth''': ANY(Image.Image )}, {'''predicted_depth''': ANY(torch.Tensor ), '''depth''': ANY(Image.Image )}, {'''predicted_depth''': ANY(torch.Tensor ), '''depth''': ANY(Image.Image )}, {'''predicted_depth''': ANY(torch.Tensor ), '''depth''': ANY(Image.Image )}, ], lowerCAmelCase, ) @require_tf @unittest.skip('''Depth estimation is not implemented in TF''' ) def lowercase__ ( self ): """simple docstring""" pass @slow @require_torch def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ ='''Intel/dpt-large''' lowerCamelCase_ =pipeline('''depth-estimation''', model=lowerCAmelCase ) lowerCamelCase_ =depth_estimator('''http://images.cocodataset.org/val2017/000000039769.jpg''' ) lowerCamelCase_ =hashimage(outputs['''depth'''] ) # This seems flaky. # self.assertEqual(outputs["depth"], "1a39394e282e9f3b0741a90b9f108977") self.assertEqual(nested_simplify(outputs['''predicted_depth'''].max().item() ), 2_9.3_0_4 ) self.assertEqual(nested_simplify(outputs['''predicted_depth'''].min().item() ), 2.6_6_2 ) @require_torch def lowercase__ ( self ): """simple docstring""" self.skipTest('''There is not hf-internal-testing tiny model for either GLPN nor DPT''' )
6
'''simple docstring''' from ..utils import DummyObject, requires_backends class __UpperCamelCase ( metaclass=lowerCamelCase__ ): lowercase : str =['speech'] def __init__( self, *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" requires_backends(self, ['''speech'''] ) class __UpperCamelCase ( metaclass=lowerCamelCase__ ): lowercase : Any =['speech'] def __init__( self, *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" requires_backends(self, ['''speech'''] )
6
1
'''simple docstring''' import inspect import unittest from datasets import load_dataset from packaging import version from transformers import BeitConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_MAPPING, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, ) from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): import PIL from PIL import Image from transformers import BeitImageProcessor class __UpperCamelCase : def __init__( self, lowerCAmelCase, lowerCAmelCase=100, lowerCAmelCase=13, lowerCAmelCase=30, lowerCAmelCase=2, lowerCAmelCase=3, lowerCAmelCase=True, lowerCAmelCase=True, lowerCAmelCase=32, lowerCAmelCase=4, lowerCAmelCase=4, lowerCAmelCase=37, lowerCAmelCase="gelu", lowerCAmelCase=0.1, lowerCAmelCase=0.1, lowerCAmelCase=10, lowerCAmelCase=0.0_2, lowerCAmelCase=3, lowerCAmelCase=None, lowerCAmelCase=[0, 1, 2, 3], ): """simple docstring""" lowerCamelCase_ =parent lowerCamelCase_ =100 lowerCamelCase_ =batch_size lowerCamelCase_ =image_size lowerCamelCase_ =patch_size lowerCamelCase_ =num_channels lowerCamelCase_ =is_training lowerCamelCase_ =use_labels lowerCamelCase_ =hidden_size lowerCamelCase_ =num_hidden_layers lowerCamelCase_ =num_attention_heads lowerCamelCase_ =intermediate_size lowerCamelCase_ =hidden_act lowerCamelCase_ =hidden_dropout_prob lowerCamelCase_ =attention_probs_dropout_prob lowerCamelCase_ =type_sequence_label_size lowerCamelCase_ =initializer_range lowerCamelCase_ =scope lowerCamelCase_ =out_indices lowerCamelCase_ =num_labels # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) lowerCamelCase_ =(image_size // patch_size) ** 2 lowerCamelCase_ =num_patches + 1 def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCamelCase_ =None lowerCamelCase_ =None if self.use_labels: lowerCamelCase_ =ids_tensor([self.batch_size], self.type_sequence_label_size ) lowerCamelCase_ =ids_tensor([self.batch_size, self.image_size, self.image_size], self.num_labels ) lowerCamelCase_ =self.get_config() return config, pixel_values, labels, pixel_labels def lowercase__ ( self ): """simple docstring""" return BeitConfig( vocab_size=self.vocab_size, image_size=self.image_size, patch_size=self.patch_size, num_channels=self.num_channels, hidden_size=self.hidden_size, num_hidden_layers=self.num_hidden_layers, num_attention_heads=self.num_attention_heads, intermediate_size=self.intermediate_size, hidden_act=self.hidden_act, hidden_dropout_prob=self.hidden_dropout_prob, attention_probs_dropout_prob=self.attention_probs_dropout_prob, is_decoder=lowerCAmelCase, initializer_range=self.initializer_range, out_indices=self.out_indices, ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =BeitModel(config=lowerCAmelCase ) model.to(lowerCAmelCase ) model.eval() lowerCamelCase_ =model(lowerCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size) ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =BeitForMaskedImageModeling(config=lowerCAmelCase ) model.to(lowerCAmelCase ) model.eval() lowerCamelCase_ =model(lowerCAmelCase ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length - 1, self.vocab_size) ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =self.type_sequence_label_size lowerCamelCase_ =BeitForImageClassification(lowerCAmelCase ) model.to(lowerCAmelCase ) model.eval() lowerCamelCase_ =model(lowerCAmelCase, labels=lowerCAmelCase ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.type_sequence_label_size) ) # test greyscale images lowerCamelCase_ =1 lowerCamelCase_ =BeitForImageClassification(lowerCAmelCase ) model.to(lowerCAmelCase ) model.eval() lowerCamelCase_ =floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowerCamelCase_ =model(lowerCAmelCase, labels=lowerCAmelCase ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.type_sequence_label_size) ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =self.num_labels lowerCamelCase_ =BeitForSemanticSegmentation(lowerCAmelCase ) model.to(lowerCAmelCase ) model.eval() lowerCamelCase_ =model(lowerCAmelCase ) self.parent.assertEqual( result.logits.shape, (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) lowerCamelCase_ =model(lowerCAmelCase, labels=lowerCAmelCase ) self.parent.assertEqual( result.logits.shape, (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.prepare_config_and_inputs() lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ =config_and_inputs lowerCamelCase_ ={'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class __UpperCamelCase ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): lowercase : Tuple =( (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation) if is_torch_available() else () ) lowercase : List[str] =( { 'feature-extraction': BeitModel, 'image-classification': BeitForImageClassification, 'image-segmentation': BeitForSemanticSegmentation, } if is_torch_available() else {} ) lowercase : List[Any] =False lowercase : Optional[int] =False lowercase : List[str] =False def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =BeitModelTester(self ) lowerCamelCase_ =ConfigTester(self, config_class=lowerCAmelCase, has_text_modality=lowerCAmelCase, hidden_size=37 ) def lowercase__ ( self ): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='''BEiT does not use inputs_embeds''' ) def lowercase__ ( self ): """simple docstring""" pass @require_torch_multi_gpu @unittest.skip(reason='''BEiT has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`''' ) def lowercase__ ( self ): """simple docstring""" pass def lowercase__ ( self ): """simple docstring""" lowerCamelCase_, lowerCamelCase_ =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase_ =model_class(lowerCAmelCase ) self.assertIsInstance(model.get_input_embeddings(), (nn.Module) ) lowerCamelCase_ =model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCAmelCase, nn.Linear ) ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_, lowerCamelCase_ =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase_ =model_class(lowerCAmelCase ) lowerCamelCase_ =inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCamelCase_ =[*signature.parameters.keys()] lowerCamelCase_ =['''pixel_values'''] self.assertListEqual(arg_names[:1], lowerCAmelCase ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCAmelCase ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*lowerCAmelCase ) def lowercase__ ( self ): """simple docstring""" if not self.model_tester.is_training: return lowerCamelCase_, lowerCamelCase_ =self.model_tester.prepare_config_and_inputs_for_common() lowerCamelCase_ =True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if model_class in [*get_values(lowerCAmelCase ), BeitForMaskedImageModeling]: continue lowerCamelCase_ =model_class(lowerCAmelCase ) model.to(lowerCAmelCase ) model.train() lowerCamelCase_ =self._prepare_for_class(lowerCAmelCase, lowerCAmelCase, return_labels=lowerCAmelCase ) lowerCamelCase_ =model(**lowerCAmelCase ).loss loss.backward() def lowercase__ ( self ): """simple docstring""" lowerCamelCase_, lowerCamelCase_ =self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return lowerCamelCase_ =False lowerCamelCase_ =True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if ( model_class in [*get_values(lowerCAmelCase ), BeitForMaskedImageModeling] or not model_class.supports_gradient_checkpointing ): continue lowerCamelCase_ =model_class(lowerCAmelCase ) model.gradient_checkpointing_enable() model.to(lowerCAmelCase ) model.train() lowerCamelCase_ =self._prepare_for_class(lowerCAmelCase, lowerCAmelCase, return_labels=lowerCAmelCase ) lowerCamelCase_ =model(**lowerCAmelCase ).loss loss.backward() def lowercase__ ( self ): """simple docstring""" lowerCamelCase_, lowerCamelCase_ =self.model_tester.prepare_config_and_inputs_for_common() lowerCamelCase_ =_config_zero_init(lowerCAmelCase ) for model_class in self.all_model_classes: lowerCamelCase_ =model_class(config=lowerCAmelCase ) for name, param in model.named_parameters(): # we skip lambda parameters as these require special initial values # determined by config.layer_scale_init_value if "lambda" in name: continue if param.requires_grad: self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item(), [0.0, 1.0], msg=f'''Parameter {name} of model {model_class} seems not properly initialized''', ) @slow def lowercase__ ( self ): """simple docstring""" for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase_ =BeitModel.from_pretrained(lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) def a_ ( ) -> Optional[int]: """simple docstring""" lowerCamelCase_ =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class __UpperCamelCase ( unittest.TestCase ): @cached_property def lowercase__ ( self ): """simple docstring""" return BeitImageProcessor.from_pretrained('''microsoft/beit-base-patch16-224''' ) if is_vision_available() else None @slow def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =BeitForMaskedImageModeling.from_pretrained('''microsoft/beit-base-patch16-224-pt22k''' ).to(lowerCAmelCase ) lowerCamelCase_ =self.default_image_processor lowerCamelCase_ =prepare_img() lowerCamelCase_ =image_processor(images=lowerCAmelCase, return_tensors='''pt''' ).pixel_values.to(lowerCAmelCase ) # prepare bool_masked_pos lowerCamelCase_ =torch.ones((1, 196), dtype=torch.bool ).to(lowerCAmelCase ) # forward pass with torch.no_grad(): lowerCamelCase_ =model(pixel_values=lowerCAmelCase, bool_masked_pos=lowerCAmelCase ) lowerCamelCase_ =outputs.logits # verify the logits lowerCamelCase_ =torch.Size((1, 196, 8_192) ) self.assertEqual(logits.shape, lowerCAmelCase ) lowerCamelCase_ =torch.tensor( [[-3.2_4_3_7, 0.5_0_7_2, -1_3.9_1_7_4], [-3.2_4_5_6, 0.4_9_4_8, -1_3.9_4_0_1], [-3.2_0_3_3, 0.5_1_2_1, -1_3.8_5_5_0]] ).to(lowerCAmelCase ) self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3], lowerCAmelCase, atol=1e-2 ) ) @slow def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =BeitForImageClassification.from_pretrained('''microsoft/beit-base-patch16-224''' ).to(lowerCAmelCase ) lowerCamelCase_ =self.default_image_processor lowerCamelCase_ =prepare_img() lowerCamelCase_ =image_processor(images=lowerCAmelCase, return_tensors='''pt''' ).to(lowerCAmelCase ) # forward pass with torch.no_grad(): lowerCamelCase_ =model(**lowerCAmelCase ) lowerCamelCase_ =outputs.logits # verify the logits lowerCamelCase_ =torch.Size((1, 1_000) ) self.assertEqual(logits.shape, lowerCAmelCase ) lowerCamelCase_ =torch.tensor([-1.2_3_8_5, -1.0_9_8_7, -1.0_1_0_8] ).to(lowerCAmelCase ) self.assertTrue(torch.allclose(logits[0, :3], lowerCAmelCase, atol=1e-4 ) ) lowerCamelCase_ =281 self.assertEqual(logits.argmax(-1 ).item(), lowerCAmelCase ) @slow def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =BeitForImageClassification.from_pretrained('''microsoft/beit-large-patch16-224-pt22k-ft22k''' ).to( lowerCAmelCase ) lowerCamelCase_ =self.default_image_processor lowerCamelCase_ =prepare_img() lowerCamelCase_ =image_processor(images=lowerCAmelCase, return_tensors='''pt''' ).to(lowerCAmelCase ) # forward pass with torch.no_grad(): lowerCamelCase_ =model(**lowerCAmelCase ) lowerCamelCase_ =outputs.logits # verify the logits lowerCamelCase_ =torch.Size((1, 21_841) ) self.assertEqual(logits.shape, lowerCAmelCase ) lowerCamelCase_ =torch.tensor([1.6_8_8_1, -0.2_7_8_7, 0.5_9_0_1] ).to(lowerCAmelCase ) self.assertTrue(torch.allclose(logits[0, :3], lowerCAmelCase, atol=1e-4 ) ) lowerCamelCase_ =2_396 self.assertEqual(logits.argmax(-1 ).item(), lowerCAmelCase ) @slow def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =BeitForSemanticSegmentation.from_pretrained('''microsoft/beit-base-finetuned-ade-640-640''' ) lowerCamelCase_ =model.to(lowerCAmelCase ) lowerCamelCase_ =BeitImageProcessor(do_resize=lowerCAmelCase, size=640, do_center_crop=lowerCAmelCase ) lowerCamelCase_ =load_dataset('''hf-internal-testing/fixtures_ade20k''', split='''test''' ) lowerCamelCase_ =Image.open(ds[0]['''file'''] ) lowerCamelCase_ =image_processor(images=lowerCAmelCase, return_tensors='''pt''' ).to(lowerCAmelCase ) # forward pass with torch.no_grad(): lowerCamelCase_ =model(**lowerCAmelCase ) lowerCamelCase_ =outputs.logits # verify the logits lowerCamelCase_ =torch.Size((1, 150, 160, 160) ) self.assertEqual(logits.shape, lowerCAmelCase ) lowerCamelCase_ =version.parse(PIL.__version__ ) < version.parse('''9.0.0''' ) if is_pillow_less_than_a: lowerCamelCase_ =torch.tensor( [ [[-4.9_2_2_5, -2.3_9_5_4, -3.0_5_2_2], [-2.8_8_2_2, -1.0_0_4_6, -1.7_5_6_1], [-2.9_5_4_9, -1.3_2_2_8, -2.1_3_4_7]], [[-5.8_1_6_8, -3.4_1_2_9, -4.0_7_7_8], [-3.8_6_5_1, -2.2_2_1_4, -3.0_2_7_7], [-3.8_3_5_6, -2.4_6_4_3, -3.3_5_3_5]], [[-0.0_0_7_8, 3.9_9_5_2, 4.0_7_5_4], [2.9_8_5_6, 4.6_9_4_4, 5.0_0_3_5], [3.2_4_1_3, 4.7_8_1_3, 4.9_9_6_9]], ], device=lowerCAmelCase, ) else: lowerCamelCase_ =torch.tensor( [ [[-4.8_9_6_0, -2.3_6_8_8, -3.0_3_5_5], [-2.8_4_7_8, -0.9_8_3_6, -1.7_4_1_8], [-2.9_4_4_9, -1.3_3_3_2, -2.1_4_5_6]], [[-5.8_0_8_1, -3.4_1_2_4, -4.1_0_0_6], [-3.8_5_6_1, -2.2_0_8_1, -3.0_3_2_3], [-3.8_3_6_5, -2.4_6_0_1, -3.3_6_6_9]], [[-0.0_3_0_9, 3.9_8_6_8, 4.0_5_4_0], [2.9_6_4_0, 4.6_8_7_7, 4.9_9_7_6], [3.2_0_8_1, 4.7_6_9_0, 4.9_9_4_2]], ], device=lowerCAmelCase, ) self.assertTrue(torch.allclose(logits[0, :3, :3, :3], lowerCAmelCase, atol=1e-4 ) ) @slow def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =BeitForSemanticSegmentation.from_pretrained('''microsoft/beit-base-finetuned-ade-640-640''' ) lowerCamelCase_ =model.to(lowerCAmelCase ) lowerCamelCase_ =BeitImageProcessor(do_resize=lowerCAmelCase, size=640, do_center_crop=lowerCAmelCase ) lowerCamelCase_ =load_dataset('''hf-internal-testing/fixtures_ade20k''', split='''test''' ) lowerCamelCase_ =Image.open(ds[0]['''file'''] ) lowerCamelCase_ =image_processor(images=lowerCAmelCase, return_tensors='''pt''' ).to(lowerCAmelCase ) # forward pass with torch.no_grad(): lowerCamelCase_ =model(**lowerCAmelCase ) lowerCamelCase_ =outputs.logits.detach().cpu() lowerCamelCase_ =image_processor.post_process_semantic_segmentation(outputs=lowerCAmelCase, target_sizes=[(500, 300)] ) lowerCamelCase_ =torch.Size((500, 300) ) self.assertEqual(segmentation[0].shape, lowerCAmelCase ) lowerCamelCase_ =image_processor.post_process_semantic_segmentation(outputs=lowerCAmelCase ) lowerCamelCase_ =torch.Size((160, 160) ) self.assertEqual(segmentation[0].shape, lowerCAmelCase )
6
'''simple docstring''' import re import warnings from contextlib import contextmanager from ...processing_utils import ProcessorMixin class __UpperCamelCase ( lowerCamelCase__ ): lowercase : List[str] =['image_processor', 'tokenizer'] lowercase : Optional[int] ='AutoImageProcessor' lowercase : List[str] ='AutoTokenizer' def __init__( self, lowerCAmelCase=None, lowerCAmelCase=None, **lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =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, ) lowerCamelCase_ =kwargs.pop('''feature_extractor''' ) lowerCamelCase_ =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 ) lowerCamelCase_ =self.image_processor lowerCamelCase_ =False def __call__( self, *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" if self._in_target_context_manager: return self.current_processor(*lowerCAmelCase, **lowerCAmelCase ) lowerCamelCase_ =kwargs.pop('''images''', lowerCAmelCase ) lowerCamelCase_ =kwargs.pop('''text''', lowerCAmelCase ) if len(lowerCAmelCase ) > 0: lowerCamelCase_ =args[0] lowerCamelCase_ =args[1:] if images is None and text is None: raise ValueError('''You need to specify either an `images` or `text` input to process.''' ) if images is not None: lowerCamelCase_ =self.image_processor(lowerCAmelCase, *lowerCAmelCase, **lowerCAmelCase ) if text is not None: lowerCamelCase_ =self.tokenizer(lowerCAmelCase, **lowerCAmelCase ) if text is None: return inputs elif images is None: return encodings else: lowerCamelCase_ =encodings['''input_ids'''] return inputs def lowercase__ ( self, *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" return self.tokenizer.batch_decode(*lowerCAmelCase, **lowerCAmelCase ) def lowercase__ ( self, *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" return self.tokenizer.decode(*lowerCAmelCase, **lowerCAmelCase ) @contextmanager def lowercase__ ( self ): """simple docstring""" warnings.warn( '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your ''' '''labels by using the argument `text` of the regular `__call__` method (either in the same call as ''' '''your images inputs, or in a separate call.''' ) lowerCamelCase_ =True lowerCamelCase_ =self.tokenizer yield lowerCamelCase_ =self.image_processor lowerCamelCase_ =False def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase=False, lowerCAmelCase=None ): """simple docstring""" if added_vocab is None: lowerCamelCase_ =self.tokenizer.get_added_vocab() lowerCamelCase_ ={} while tokens: lowerCamelCase_ =re.search(R'''<s_(.*?)>''', lowerCAmelCase, re.IGNORECASE ) if start_token is None: break lowerCamelCase_ =start_token.group(1 ) lowerCamelCase_ =re.search(Rf'''</s_{key}>''', lowerCAmelCase, re.IGNORECASE ) lowerCamelCase_ =start_token.group() if end_token is None: lowerCamelCase_ =tokens.replace(lowerCAmelCase, '''''' ) else: lowerCamelCase_ =end_token.group() lowerCamelCase_ =re.escape(lowerCAmelCase ) lowerCamelCase_ =re.escape(lowerCAmelCase ) lowerCamelCase_ =re.search(f'''{start_token_escaped}(.*?){end_token_escaped}''', lowerCAmelCase, re.IGNORECASE ) if content is not None: lowerCamelCase_ =content.group(1 ).strip() if r"<s_" in content and r"</s_" in content: # non-leaf node lowerCamelCase_ =self.tokenajson(lowerCAmelCase, is_inner_value=lowerCAmelCase, added_vocab=lowerCAmelCase ) if value: if len(lowerCAmelCase ) == 1: lowerCamelCase_ =value[0] lowerCamelCase_ =value else: # leaf nodes lowerCamelCase_ =[] for leaf in content.split(R'''<sep/>''' ): lowerCamelCase_ =leaf.strip() if leaf in added_vocab and leaf[0] == "<" and leaf[-2:] == "/>": lowerCamelCase_ =leaf[1:-2] # for categorical special tokens output[key].append(lowerCAmelCase ) if len(output[key] ) == 1: lowerCamelCase_ =output[key][0] lowerCamelCase_ =tokens[tokens.find(lowerCAmelCase ) + len(lowerCAmelCase ) :].strip() if tokens[:6] == r"<sep/>": # non-leaf nodes return [output] + self.tokenajson(tokens[6:], is_inner_value=lowerCAmelCase, added_vocab=lowerCAmelCase ) if len(lowerCAmelCase ): return [output] if is_inner_value else output else: return [] if is_inner_value else {"text_sequence": tokens} @property def lowercase__ ( self ): """simple docstring""" warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''', lowerCAmelCase, ) return self.image_processor_class @property def lowercase__ ( self ): """simple docstring""" warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''', lowerCAmelCase, ) return self.image_processor
6
1
'''simple docstring''' import os from typing import Dict, List, Tuple, TypeVar, Union a_ : Tuple = TypeVar("""T""") a_ : Dict = Union[List[T], Tuple[T, ...]] a_ : int = Union[T, List[T], Dict[str, T]] a_ : Optional[Any] = Union[str, bytes, os.PathLike]
6
'''simple docstring''' import gc import random import unittest import numpy as np import torch from transformers import CLIPImageProcessor, CLIPVisionConfig, CLIPVisionModel from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEImgaImgPipeline from diffusers.pipelines.shap_e import ShapERenderer from diffusers.utils import floats_tensor, load_image, load_numpy, slow from diffusers.utils.testing_utils import require_torch_gpu, torch_device from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference class __UpperCamelCase ( lowerCamelCase__ , unittest.TestCase ): lowercase : Union[str, Any] =ShapEImgaImgPipeline lowercase : Dict =['image'] lowercase : str =['image'] lowercase : int =[ 'num_images_per_prompt', 'num_inference_steps', 'generator', 'latents', 'guidance_scale', 'frame_size', 'output_type', 'return_dict', ] lowercase : int =False @property def lowercase__ ( self ): """simple docstring""" return 32 @property def lowercase__ ( self ): """simple docstring""" return 32 @property def lowercase__ ( self ): """simple docstring""" return self.time_input_dim * 4 @property def lowercase__ ( self ): """simple docstring""" return 8 @property def lowercase__ ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCamelCase_ =CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size, image_size=64, projection_dim=self.text_embedder_hidden_size, intermediate_size=37, num_attention_heads=4, num_channels=3, num_hidden_layers=5, patch_size=1, ) lowerCamelCase_ =CLIPVisionModel(lowerCAmelCase ) return model @property def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =CLIPImageProcessor( crop_size=224, do_center_crop=lowerCAmelCase, do_normalize=lowerCAmelCase, do_resize=lowerCAmelCase, image_mean=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3], image_std=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1], resample=3, size=224, ) return image_processor @property def lowercase__ ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCamelCase_ ={ '''num_attention_heads''': 2, '''attention_head_dim''': 16, '''embedding_dim''': self.time_input_dim, '''num_embeddings''': 32, '''embedding_proj_dim''': self.text_embedder_hidden_size, '''time_embed_dim''': self.time_embed_dim, '''num_layers''': 1, '''clip_embed_dim''': self.time_input_dim * 2, '''additional_embeddings''': 0, '''time_embed_act_fn''': '''gelu''', '''norm_in_type''': '''layer''', '''embedding_proj_norm_type''': '''layer''', '''encoder_hid_proj_type''': None, '''added_emb_type''': None, } lowerCamelCase_ =PriorTransformer(**lowerCAmelCase ) return model @property def lowercase__ ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCamelCase_ ={ '''param_shapes''': ( (self.renderer_dim, 93), (self.renderer_dim, 8), (self.renderer_dim, 8), (self.renderer_dim, 8), ), '''d_latent''': self.time_input_dim, '''d_hidden''': self.renderer_dim, '''n_output''': 12, '''background''': ( 0.1, 0.1, 0.1, ), } lowerCamelCase_ =ShapERenderer(**lowerCAmelCase ) return model def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.dummy_prior lowerCamelCase_ =self.dummy_image_encoder lowerCamelCase_ =self.dummy_image_processor lowerCamelCase_ =self.dummy_renderer lowerCamelCase_ =HeunDiscreteScheduler( beta_schedule='''exp''', num_train_timesteps=1_024, prediction_type='''sample''', use_karras_sigmas=lowerCAmelCase, clip_sample=lowerCAmelCase, clip_sample_range=1.0, ) lowerCamelCase_ ={ '''prior''': prior, '''image_encoder''': image_encoder, '''image_processor''': image_processor, '''renderer''': renderer, '''scheduler''': scheduler, } return components def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase=0 ): """simple docstring""" lowerCamelCase_ =floats_tensor((1, 3, 64, 64), rng=random.Random(lowerCAmelCase ) ).to(lowerCAmelCase ) if str(lowerCAmelCase ).startswith('''mps''' ): lowerCamelCase_ =torch.manual_seed(lowerCAmelCase ) else: lowerCamelCase_ =torch.Generator(device=lowerCAmelCase ).manual_seed(lowerCAmelCase ) lowerCamelCase_ ={ '''image''': input_image, '''generator''': generator, '''num_inference_steps''': 1, '''frame_size''': 32, '''output_type''': '''np''', } return inputs def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ ='''cpu''' lowerCamelCase_ =self.get_dummy_components() lowerCamelCase_ =self.pipeline_class(**lowerCAmelCase ) lowerCamelCase_ =pipe.to(lowerCAmelCase ) pipe.set_progress_bar_config(disable=lowerCAmelCase ) lowerCamelCase_ =pipe(**self.get_dummy_inputs(lowerCAmelCase ) ) lowerCamelCase_ =output.images[0] lowerCamelCase_ =image[0, -3:, -3:, -1] assert image.shape == (20, 32, 32, 3) lowerCamelCase_ =np.array( [ 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def lowercase__ ( self ): """simple docstring""" self._test_inference_batch_consistent(batch_sizes=[1, 2] ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =torch_device == '''cpu''' lowerCamelCase_ =True self._test_inference_batch_single_identical( batch_size=2, test_max_difference=lowerCAmelCase, relax_max_difference=lowerCAmelCase, ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.get_dummy_components() lowerCamelCase_ =self.pipeline_class(**lowerCAmelCase ) lowerCamelCase_ =pipe.to(lowerCAmelCase ) pipe.set_progress_bar_config(disable=lowerCAmelCase ) lowerCamelCase_ =1 lowerCamelCase_ =2 lowerCamelCase_ =self.get_dummy_inputs(lowerCAmelCase ) for key in inputs.keys(): if key in self.batch_params: lowerCamelCase_ =batch_size * [inputs[key]] lowerCamelCase_ =pipe(**lowerCAmelCase, num_images_per_prompt=lowerCAmelCase )[0] assert images.shape[0] == batch_size * num_images_per_prompt @slow @require_torch_gpu class __UpperCamelCase ( unittest.TestCase ): def lowercase__ ( self ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/shap_e/corgi.png''' ) lowerCamelCase_ =load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/shap_e/test_shap_e_img2img_out.npy''' ) lowerCamelCase_ =ShapEImgaImgPipeline.from_pretrained('''openai/shap-e-img2img''' ) lowerCamelCase_ =pipe.to(lowerCAmelCase ) pipe.set_progress_bar_config(disable=lowerCAmelCase ) lowerCamelCase_ =torch.Generator(device=lowerCAmelCase ).manual_seed(0 ) lowerCamelCase_ =pipe( lowerCAmelCase, generator=lowerCAmelCase, guidance_scale=3.0, num_inference_steps=64, frame_size=64, output_type='''np''', ).images[0] assert images.shape == (20, 64, 64, 3) assert_mean_pixel_difference(lowerCAmelCase, lowerCAmelCase )
6
1
'''simple docstring''' import unittest import numpy as np import torch from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class __UpperCamelCase ( lowerCamelCase__ , unittest.TestCase ): lowercase : Optional[Any] =DDIMPipeline lowercase : Tuple =UNCONDITIONAL_IMAGE_GENERATION_PARAMS lowercase : Dict =PipelineTesterMixin.required_optional_params - { 'num_images_per_prompt', 'latents', 'callback', 'callback_steps', } lowercase : str =UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS lowercase : Optional[Any] =False def lowercase__ ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCamelCase_ =UNetaDModel( block_out_channels=(32, 64), layers_per_block=2, sample_size=32, in_channels=3, out_channels=3, down_block_types=('''DownBlock2D''', '''AttnDownBlock2D'''), up_block_types=('''AttnUpBlock2D''', '''UpBlock2D'''), ) lowerCamelCase_ =DDIMScheduler() lowerCamelCase_ ={'''unet''': unet, '''scheduler''': scheduler} return components def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase=0 ): """simple docstring""" if str(lowerCAmelCase ).startswith('''mps''' ): lowerCamelCase_ =torch.manual_seed(lowerCAmelCase ) else: lowerCamelCase_ =torch.Generator(device=lowerCAmelCase ).manual_seed(lowerCAmelCase ) lowerCamelCase_ ={ '''batch_size''': 1, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ ='''cpu''' lowerCamelCase_ =self.get_dummy_components() lowerCamelCase_ =self.pipeline_class(**lowerCAmelCase ) pipe.to(lowerCAmelCase ) pipe.set_progress_bar_config(disable=lowerCAmelCase ) lowerCamelCase_ =self.get_dummy_inputs(lowerCAmelCase ) lowerCamelCase_ =pipe(**lowerCAmelCase ).images lowerCamelCase_ =image[0, -3:, -3:, -1] self.assertEqual(image.shape, (1, 32, 32, 3) ) lowerCamelCase_ =np.array( [1.000e00, 5.717e-01, 4.717e-01, 1.000e00, 0.000e00, 1.000e00, 3.000e-04, 0.000e00, 9.000e-04] ) lowerCamelCase_ =np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(lowerCAmelCase, 1e-3 ) def lowercase__ ( self ): """simple docstring""" super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3 ) def lowercase__ ( self ): """simple docstring""" super().test_save_load_local(expected_max_difference=3e-3 ) def lowercase__ ( self ): """simple docstring""" super().test_save_load_optional_components(expected_max_difference=3e-3 ) def lowercase__ ( self ): """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class __UpperCamelCase ( unittest.TestCase ): def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ ='''google/ddpm-cifar10-32''' lowerCamelCase_ =UNetaDModel.from_pretrained(lowerCAmelCase ) lowerCamelCase_ =DDIMScheduler() lowerCamelCase_ =DDIMPipeline(unet=lowerCAmelCase, scheduler=lowerCAmelCase ) ddim.to(lowerCAmelCase ) ddim.set_progress_bar_config(disable=lowerCAmelCase ) lowerCamelCase_ =torch.manual_seed(0 ) lowerCamelCase_ =ddim(generator=lowerCAmelCase, eta=0.0, output_type='''numpy''' ).images lowerCamelCase_ =image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) lowerCamelCase_ =np.array([0.1_7_2_3, 0.1_6_1_7, 0.1_6_0_0, 0.1_6_2_6, 0.1_4_9_7, 0.1_5_1_3, 0.1_5_0_5, 0.1_4_4_2, 0.1_4_5_3] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ ='''google/ddpm-ema-bedroom-256''' lowerCamelCase_ =UNetaDModel.from_pretrained(lowerCAmelCase ) lowerCamelCase_ =DDIMScheduler.from_pretrained(lowerCAmelCase ) lowerCamelCase_ =DDIMPipeline(unet=lowerCAmelCase, scheduler=lowerCAmelCase ) ddpm.to(lowerCAmelCase ) ddpm.set_progress_bar_config(disable=lowerCAmelCase ) lowerCamelCase_ =torch.manual_seed(0 ) lowerCamelCase_ =ddpm(generator=lowerCAmelCase, output_type='''numpy''' ).images lowerCamelCase_ =image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) lowerCamelCase_ =np.array([0.0_0_6_0, 0.0_2_0_1, 0.0_3_4_4, 0.0_0_2_4, 0.0_0_1_8, 0.0_0_0_2, 0.0_0_2_2, 0.0_0_0_0, 0.0_0_6_9] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
6
'''simple docstring''' from itertools import product def a_ ( __snake_case : int , __snake_case : int ) -> list[int]: """simple docstring""" lowerCamelCase_ =sides_number lowerCamelCase_ =max_face_number * dice_number lowerCamelCase_ =[0] * (max_total + 1) lowerCamelCase_ =1 lowerCamelCase_ =range(__snake_case , max_face_number + 1 ) for dice_numbers in product(__snake_case , repeat=__snake_case ): lowerCamelCase_ =sum(__snake_case ) totals_frequencies[total] += 1 return totals_frequencies def a_ ( ) -> float: """simple docstring""" lowerCamelCase_ =total_frequency_distribution( sides_number=4 , dice_number=9 ) lowerCamelCase_ =total_frequency_distribution( sides_number=6 , dice_number=6 ) lowerCamelCase_ =0 lowerCamelCase_ =9 lowerCamelCase_ =4 * 9 lowerCamelCase_ =6 for peter_total in range(__snake_case , max_peter_total + 1 ): peter_wins_count += peter_totals_frequencies[peter_total] * sum( colin_totals_frequencies[min_colin_total:peter_total] ) lowerCamelCase_ =(4**9) * (6**6) lowerCamelCase_ =peter_wins_count / total_games_number lowerCamelCase_ =round(__snake_case , ndigits=7 ) return rounded_peter_win_probability if __name__ == "__main__": print(F"""{solution() = }""")
6
1
'''simple docstring''' def a_ ( __snake_case : int = 1000 ) -> int: """simple docstring""" lowerCamelCase_ =3 lowerCamelCase_ =0 while a < n: if a % 3 == 0 or a % 5 == 0: result += a elif a % 15 == 0: result -= a a += 1 return result if __name__ == "__main__": print(F"""{solution() = }""")
6
'''simple docstring''' import os from typing import Dict, List, Tuple, TypeVar, Union a_ : Tuple = TypeVar("""T""") a_ : Dict = Union[List[T], Tuple[T, ...]] a_ : int = Union[T, List[T], Dict[str, T]] a_ : Optional[Any] = Union[str, bytes, os.PathLike]
6
1
'''simple docstring''' from collections.abc import Generator def a_ ( ) -> Generator[int, None, None]: """simple docstring""" lowerCamelCase_, lowerCamelCase_ =0, 1 while True: lowerCamelCase_, lowerCamelCase_ =b, a + b yield b def a_ ( __snake_case : int = 1000 ) -> int: """simple docstring""" lowerCamelCase_ =1 lowerCamelCase_ =fibonacci_generator() while len(str(next(__snake_case ) ) ) < n: answer += 1 return answer + 1 if __name__ == "__main__": print(solution(int(str(input()).strip())))
6
'''simple docstring''' import math import random from typing import Any from .hill_climbing import SearchProblem def a_ ( __snake_case : str , __snake_case : bool = True , __snake_case : float = math.inf , __snake_case : float = -math.inf , __snake_case : float = math.inf , __snake_case : float = -math.inf , __snake_case : bool = False , __snake_case : float = 100 , __snake_case : float = 0.0_1 , __snake_case : float = 1 , ) -> Any: """simple docstring""" lowerCamelCase_ =False lowerCamelCase_ =search_prob lowerCamelCase_ =start_temperate lowerCamelCase_ =[] lowerCamelCase_ =0 lowerCamelCase_ =None while not search_end: lowerCamelCase_ =current_state.score() if best_state is None or current_score > best_state.score(): lowerCamelCase_ =current_state scores.append(__snake_case ) iterations += 1 lowerCamelCase_ =None lowerCamelCase_ =current_state.get_neighbors() while ( next_state is None and neighbors ): # till we do not find a neighbor that we can move to lowerCamelCase_ =random.randint(0 , len(__snake_case ) - 1 ) # picking a random neighbor lowerCamelCase_ =neighbors.pop(__snake_case ) lowerCamelCase_ =picked_neighbor.score() - current_score if ( picked_neighbor.x > max_x or picked_neighbor.x < min_x or picked_neighbor.y > max_y or picked_neighbor.y < min_y ): continue # neighbor outside our bounds if not find_max: lowerCamelCase_ =change * -1 # in case we are finding minimum if change > 0: # improves the solution lowerCamelCase_ =picked_neighbor else: lowerCamelCase_ =(math.e) ** ( change / current_temp ) # probability generation function if random.random() < probability: # random number within probability lowerCamelCase_ =picked_neighbor lowerCamelCase_ =current_temp - (current_temp * rate_of_decrease) if current_temp < threshold_temp or next_state is None: # temperature below threshold, or could not find a suitable neighbor lowerCamelCase_ =True else: lowerCamelCase_ =next_state if visualization: from matplotlib import pyplot as plt plt.plot(range(__snake_case ) , __snake_case ) plt.xlabel('''Iterations''' ) plt.ylabel('''Function values''' ) plt.show() return best_state if __name__ == "__main__": def a_ ( __snake_case : List[str] , __snake_case : Optional[int] ) -> str: """simple docstring""" return (x**2) + (y**2) # starting the problem with initial coordinates (12, 47) a_ : str = SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_fa) a_ : Optional[int] = simulated_annealing( prob, find_max=False, max_x=1_00, min_x=5, max_y=50, min_y=-5, visualization=True ) print( """The minimum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 """ F"""and 50 > y > - 5 found via hill climbing: {local_min.score()}""" ) # starting the problem with initial coordinates (12, 47) a_ : str = SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_fa) a_ : List[str] = simulated_annealing( prob, find_max=True, max_x=1_00, min_x=5, max_y=50, min_y=-5, visualization=True ) print( """The maximum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 """ F"""and 50 > y > - 5 found via hill climbing: {local_min.score()}""" ) def a_ ( __snake_case : Dict , __snake_case : Optional[Any] ) -> Union[str, Any]: """simple docstring""" return (3 * x**2) - (6 * y) a_ : Tuple = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa) a_ : Optional[Any] = simulated_annealing(prob, find_max=False, visualization=True) print( """The minimum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: """ F"""{local_min.score()}""" ) a_ : Dict = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa) a_ : Optional[int] = simulated_annealing(prob, find_max=True, visualization=True) print( """The maximum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: """ F"""{local_min.score()}""" )
6
1
'''simple docstring''' import datasets from .nmt_bleu import compute_bleu # From: https://github.com/tensorflow/nmt/blob/master/nmt/scripts/bleu.py a_ : Dict = """\ @INPROCEEDINGS{Papineni02bleu:a, author = {Kishore Papineni and Salim Roukos and Todd Ward and Wei-jing Zhu}, title = {BLEU: a Method for Automatic Evaluation of Machine Translation}, booktitle = {}, year = {2002}, pages = {311--318} } @inproceedings{lin-och-2004-orange, title = \"{ORANGE}: a Method for Evaluating Automatic Evaluation Metrics for Machine Translation\", author = \"Lin, Chin-Yew and Och, Franz Josef\", booktitle = \"{COLING} 2004: Proceedings of the 20th International Conference on Computational Linguistics\", month = \"aug 23{--}aug 27\", year = \"2004\", address = \"Geneva, Switzerland\", publisher = \"COLING\", url = \"https://www.aclweb.org/anthology/C04-1072\", pages = \"501--507\", } """ a_ : int = """\ BLEU (bilingual evaluation understudy) is an algorithm for evaluating the quality of text which has been machine-translated from one natural language to another. Quality is considered to be the correspondence between a machine's output and that of a human: \"the closer a machine translation is to a professional human translation, the better it is\" – this is the central idea behind BLEU. BLEU was one of the first metrics to claim a high correlation with human judgements of quality, and remains one of the most popular automated and inexpensive metrics. Scores are calculated for individual translated segments—generally sentences—by comparing them with a set of good quality reference translations. Those scores are then averaged over the whole corpus to reach an estimate of the translation's overall quality. Intelligibility or grammatical correctness are not taken into account[citation needed]. BLEU's output is always a number between 0 and 1. This value indicates how similar the candidate text is to the reference texts, with values closer to 1 representing more similar texts. Few human translations will attain a score of 1, since this would indicate that the candidate is identical to one of the reference translations. For this reason, it is not necessary to attain a score of 1. Because there are more opportunities to match, adding additional reference translations will increase the BLEU score. """ a_ : Optional[int] = """ Computes BLEU score of translated segments against one or more references. Args: predictions: list of translations to score. Each translation should be tokenized into a list of tokens. references: list of lists of references for each translation. Each reference should be tokenized into a list of tokens. max_order: Maximum n-gram order to use when computing BLEU score. smooth: Whether or not to apply Lin et al. 2004 smoothing. Returns: 'bleu': bleu score, 'precisions': geometric mean of n-gram precisions, 'brevity_penalty': brevity penalty, 'length_ratio': ratio of lengths, 'translation_length': translation_length, 'reference_length': reference_length Examples: >>> predictions = [ ... [\"hello\", \"there\", \"general\", \"kenobi\"], # tokenized prediction of the first sample ... [\"foo\", \"bar\", \"foobar\"] # tokenized prediction of the second sample ... ] >>> references = [ ... [[\"hello\", \"there\", \"general\", \"kenobi\"], [\"hello\", \"there\", \"!\"]], # tokenized references for the first sample (2 references) ... [[\"foo\", \"bar\", \"foobar\"]] # tokenized references for the second sample (1 reference) ... ] >>> bleu = datasets.load_metric(\"bleu\") >>> results = bleu.compute(predictions=predictions, references=references) >>> print(results[\"bleu\"]) 1.0 """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __UpperCamelCase ( datasets.Metric ): def lowercase__ ( self ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION, citation=_CITATION, inputs_description=_KWARGS_DESCRIPTION, features=datasets.Features( { '''predictions''': datasets.Sequence(datasets.Value('''string''', id='''token''' ), id='''sequence''' ), '''references''': datasets.Sequence( datasets.Sequence(datasets.Value('''string''', id='''token''' ), id='''sequence''' ), id='''references''' ), } ), codebase_urls=['''https://github.com/tensorflow/nmt/blob/master/nmt/scripts/bleu.py'''], reference_urls=[ '''https://en.wikipedia.org/wiki/BLEU''', '''https://towardsdatascience.com/evaluating-text-output-in-nlp-bleu-at-your-own-risk-e8609665a213''', ], ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase=4, lowerCAmelCase=False ): """simple docstring""" lowerCamelCase_ =compute_bleu( reference_corpus=lowerCAmelCase, translation_corpus=lowerCAmelCase, max_order=lowerCAmelCase, smooth=lowerCAmelCase ) ((lowerCamelCase_), (lowerCamelCase_), (lowerCamelCase_), (lowerCamelCase_), (lowerCamelCase_), (lowerCamelCase_)) =score return { "bleu": bleu, "precisions": precisions, "brevity_penalty": bp, "length_ratio": ratio, "translation_length": translation_length, "reference_length": reference_length, }
6
'''simple docstring''' import argparse import json import os import torch from torch import nn from transformers import NllbMoeConfig, NllbMoeModel from transformers.modeling_utils import dtype_byte_size from transformers.utils import WEIGHTS_INDEX_NAME, WEIGHTS_NAME def a_ ( __snake_case : Optional[int] ) -> List[str]: """simple docstring""" lowerCamelCase_ =[ '''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(__snake_case , __snake_case ) def a_ ( __snake_case : List[Any] ) -> int: """simple docstring""" lowerCamelCase_, lowerCamelCase_ =emb.weight.shape lowerCamelCase_ =nn.Linear(__snake_case , __snake_case , bias=__snake_case ) lowerCamelCase_ =emb.weight.data return lin_layer def a_ ( __snake_case : Union[str, Any] , __snake_case : Tuple=None ) -> Dict: """simple docstring""" lowerCamelCase_ ={} for old_key in state_dict.keys(): lowerCamelCase_ =old_key if "moe_layer.experts." in key: if expert_idx is not None: lowerCamelCase_ =key.replace('''moe_layer.experts.0''' , F'''ffn.experts.expert_{expert_idx}''' ) else: lowerCamelCase_ =key.replace('''moe_layer.experts.''' , '''ffn.experts.expert_''' ) if "gate" in key: lowerCamelCase_ =key.replace('''.moe_layer.gate.wg''' , '''.ffn.router.classifier''' ) if "fc2" and "experts" not in key: lowerCamelCase_ =key.replace('''.fc2.''' , '''.ffn.fc2.''' ) if "fc1" and "experts" not in key: lowerCamelCase_ =key.replace('''.fc1.''' , '''.ffn.fc1.''' ) if ".encoder_attn." in key: lowerCamelCase_ =key.replace('''.encoder_attn.''' , '''.cross_attention.''' ) if "encoder_attn_layer_norm" in key: lowerCamelCase_ =key.replace('''encoder_attn_layer_norm''' , '''cross_attention_layer_norm''' ) if "final_layer_norm" in key: lowerCamelCase_ =key.replace('''final_layer_norm''' , '''ff_layer_norm''' ) lowerCamelCase_ =state_dict[old_key] return new_dict def a_ ( __snake_case : Optional[Any] , __snake_case : Any , __snake_case : Optional[int] , __snake_case : Tuple , __snake_case : str = WEIGHTS_NAME ) -> Dict: """simple docstring""" lowerCamelCase_ =[] lowerCamelCase_ =0 os.makedirs(__snake_case , exist_ok=__snake_case ) for expert in range(__snake_case ): lowerCamelCase_ =switch_checkpoint_path + F'''-rank-{expert}.pt''' if os.path.isfile(__snake_case ): lowerCamelCase_ =torch.load(__snake_case )['''model'''] remove_ignore_keys_(__snake_case ) lowerCamelCase_ =rename_fairseq_keys(__snake_case , __snake_case ) lowerCamelCase_ =os.path.join( __snake_case , weights_name.replace('''.bin''' , F'''-{len(__snake_case )+1:05d}-of-???.bin''' ) ) torch.save(__snake_case , __snake_case ) 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(__snake_case )[0]].dtype ) # Add the last block lowerCamelCase_ =os.path.join(__snake_case , weights_name.replace('''.bin''' , F'''-{len(__snake_case )+1:05d}-of-???.bin''' ) ) lowerCamelCase_ =torch.load(switch_checkpoint_path + '''-shared.pt''' )['''model'''] remove_ignore_keys_(__snake_case ) lowerCamelCase_ =rename_fairseq_keys(__snake_case , __snake_case ) lowerCamelCase_ =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(__snake_case ) == 1: lowerCamelCase_ =os.path.join(__snake_case , __snake_case ) torch.save(__snake_case , __snake_case ) return {weights_name: sharded_state_dicts[0]}, None else: torch.save(__snake_case , __snake_case ) # Otherwise, let's build the index lowerCamelCase_ ={} for idx, shard in enumerate(__snake_case ): lowerCamelCase_ =weights_name.replace('''.bin''' , F'''-{idx+1:05d}-of-{len(__snake_case ):05d}.bin''' ) lowerCamelCase_ =os.path.join(__snake_case , weights_name.replace('''.bin''' , F'''-{idx+1:05d}-of-???.bin''' ) ) os.rename(__snake_case , os.path.join(__snake_case , __snake_case ) ) for key in shard: lowerCamelCase_ =shard_file # Add the metadata lowerCamelCase_ ={'''total_size''': total_size} lowerCamelCase_ ={'''metadata''': metadata, '''weight_map''': weight_map} with open(os.path.join(__snake_case , __snake_case ) , '''w''' , encoding='''utf-8''' ) as f: lowerCamelCase_ =json.dumps(__snake_case , indent=2 , sort_keys=__snake_case ) + '''\n''' f.write(__snake_case ) return metadata, index if __name__ == "__main__": a_ : Union[str, Any] = 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_ : Tuple = parser.parse_args() a_ , a_ : int = shard_on_the_fly( args.nllb_moe_checkpoint_path, args.pytorch_dump_folder_path, 1_28, args.dtype, ) a_ : Tuple = NllbMoeConfig.from_pretrained( """facebook/nllb-200-3.3B""", encoder_sparse_step=4, decoder_sparse_step=4, num_experts=1_28 ) 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)
6
1
'''simple docstring''' from __future__ import annotations from collections import deque from collections.abc import Sequence from dataclasses import dataclass from typing import Any @dataclass class __UpperCamelCase : lowercase : int lowercase : Node | None =None lowercase : Node | None =None def a_ ( ) -> Node | None: """simple docstring""" lowerCamelCase_ =Node(1 ) lowerCamelCase_ =Node(2 ) lowerCamelCase_ =Node(3 ) lowerCamelCase_ =Node(4 ) lowerCamelCase_ =Node(5 ) return tree def a_ ( __snake_case : Node | None ) -> list[int]: """simple docstring""" return [root.data, *preorder(root.left ), *preorder(root.right )] if root else [] def a_ ( __snake_case : Node | None ) -> list[int]: """simple docstring""" return postorder(root.left ) + postorder(root.right ) + [root.data] if root else [] def a_ ( __snake_case : Node | None ) -> list[int]: """simple docstring""" return [*inorder(root.left ), root.data, *inorder(root.right )] if root else [] def a_ ( __snake_case : Node | None ) -> int: """simple docstring""" return (max(height(root.left ) , height(root.right ) ) + 1) if root else 0 def a_ ( __snake_case : Node | None ) -> Sequence[Node | None]: """simple docstring""" lowerCamelCase_ =[] if root is None: return output lowerCamelCase_ =deque([root] ) while process_queue: lowerCamelCase_ =process_queue.popleft() output.append(node.data ) if node.left: process_queue.append(node.left ) if node.right: process_queue.append(node.right ) return output def a_ ( __snake_case : Node | None , __snake_case : int ) -> Sequence[Node | None]: """simple docstring""" lowerCamelCase_ =[] def populate_output(__snake_case : Node | None , __snake_case : int ) -> None: if not root: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.left , level - 1 ) populate_output(root.right , level - 1 ) populate_output(__snake_case , __snake_case ) return output def a_ ( __snake_case : Node | None , __snake_case : int ) -> Sequence[Node | None]: """simple docstring""" lowerCamelCase_ =[] def populate_output(__snake_case : Node | None , __snake_case : int ) -> None: if root is None: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.right , level - 1 ) populate_output(root.left , level - 1 ) populate_output(__snake_case , __snake_case ) return output def a_ ( __snake_case : Node | None ) -> Sequence[Node | None] | list[Any]: """simple docstring""" if root is None: return [] lowerCamelCase_ =[] lowerCamelCase_ =0 lowerCamelCase_ =height(__snake_case ) for h in range(1 , height_tree + 1 ): if not flag: output.append(get_nodes_from_left_to_right(__snake_case , __snake_case ) ) lowerCamelCase_ =1 else: output.append(get_nodes_from_right_to_left(__snake_case , __snake_case ) ) lowerCamelCase_ =0 return output def a_ ( ) -> None: # Main function for testing. """simple docstring""" lowerCamelCase_ =make_tree() print(F'''In-order Traversal: {inorder(__snake_case )}''' ) print(F'''Pre-order Traversal: {preorder(__snake_case )}''' ) print(F'''Post-order Traversal: {postorder(__snake_case )}''' , '''\n''' ) print(F'''Height of Tree: {height(__snake_case )}''' , '''\n''' ) print('''Complete Level Order Traversal: ''' ) print(level_order(__snake_case ) , '''\n''' ) print('''Level-wise order Traversal: ''' ) for level in range(1 , height(__snake_case ) + 1 ): print(F'''Level {level}:''' , get_nodes_from_left_to_right(__snake_case , level=__snake_case ) ) print('''\nZigZag order Traversal: ''' ) print(zigzag(__snake_case ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
6
'''simple docstring''' import warnings from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class __UpperCamelCase ( lowerCamelCase__ ): lowercase : int =['image_processor', 'tokenizer'] lowercase : int ='LayoutLMv2ImageProcessor' lowercase : Any =('LayoutXLMTokenizer', 'LayoutXLMTokenizerFast') def __init__( self, lowerCAmelCase=None, lowerCAmelCase=None, **lowerCAmelCase ): """simple docstring""" if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''', lowerCAmelCase, ) lowerCamelCase_ =kwargs.pop('''feature_extractor''' ) lowerCamelCase_ =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, lowerCAmelCase, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = True, lowerCAmelCase = False, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = 0, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = False, lowerCAmelCase = False, lowerCAmelCase = False, lowerCAmelCase = False, lowerCAmelCase = True, lowerCAmelCase = None, **lowerCAmelCase, ): """simple docstring""" if self.image_processor.apply_ocr and (boxes is not None): raise ValueError( '''You cannot provide bounding boxes ''' '''if you initialized the image processor with apply_ocr set to True.''' ) if self.image_processor.apply_ocr and (word_labels is not None): raise ValueError( '''You cannot provide word labels if you initialized the image processor with apply_ocr set to True.''' ) if return_overflowing_tokens is True and return_offsets_mapping is False: raise ValueError('''You cannot return overflowing tokens without returning the offsets mapping.''' ) # first, apply the image processor lowerCamelCase_ =self.image_processor(images=lowerCAmelCase, return_tensors=lowerCAmelCase ) # second, apply the tokenizer if text is not None and self.image_processor.apply_ocr and text_pair is None: if isinstance(lowerCAmelCase, lowerCAmelCase ): lowerCamelCase_ =[text] # add batch dimension (as the image processor always adds a batch dimension) lowerCamelCase_ =features['''words'''] lowerCamelCase_ =self.tokenizer( text=text if text is not None else features['''words'''], text_pair=text_pair if text_pair is not None else None, boxes=boxes if boxes is not None else features['''boxes'''], word_labels=lowerCAmelCase, add_special_tokens=lowerCAmelCase, padding=lowerCAmelCase, truncation=lowerCAmelCase, max_length=lowerCAmelCase, stride=lowerCAmelCase, pad_to_multiple_of=lowerCAmelCase, return_token_type_ids=lowerCAmelCase, return_attention_mask=lowerCAmelCase, return_overflowing_tokens=lowerCAmelCase, return_special_tokens_mask=lowerCAmelCase, return_offsets_mapping=lowerCAmelCase, return_length=lowerCAmelCase, verbose=lowerCAmelCase, return_tensors=lowerCAmelCase, **lowerCAmelCase, ) # add pixel values lowerCamelCase_ =features.pop('''pixel_values''' ) if return_overflowing_tokens is True: lowerCamelCase_ =self.get_overflowing_images(lowerCAmelCase, encoded_inputs['''overflow_to_sample_mapping'''] ) lowerCamelCase_ =images return encoded_inputs def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =[] for sample_idx in overflow_to_sample_mapping: images_with_overflow.append(images[sample_idx] ) if len(lowerCAmelCase ) != len(lowerCAmelCase ): raise ValueError( '''Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got''' f''' {len(lowerCAmelCase )} and {len(lowerCAmelCase )}''' ) return images_with_overflow def lowercase__ ( self, *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" return self.tokenizer.batch_decode(*lowerCAmelCase, **lowerCAmelCase ) def lowercase__ ( self, *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" return self.tokenizer.decode(*lowerCAmelCase, **lowerCAmelCase ) @property def lowercase__ ( self ): """simple docstring""" return ["input_ids", "bbox", "attention_mask", "image"] @property def lowercase__ ( self ): """simple docstring""" warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''', lowerCAmelCase, ) return self.image_processor_class @property def lowercase__ ( self ): """simple docstring""" warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''', lowerCAmelCase, ) return self.image_processor
6
1
'''simple docstring''' import argparse from pathlib import Path import fairseq import torch from fairseq.models.xmod import XMODModel as FairseqXmodModel from packaging import version from transformers import XmodConfig, XmodForMaskedLM, XmodForSequenceClassification from transformers.utils import logging if version.parse(fairseq.__version__) < version.parse("""0.12.2"""): raise Exception("""requires fairseq >= 0.12.2""") if version.parse(fairseq.__version__) > version.parse("""2"""): raise Exception("""requires fairseq < v2""") logging.set_verbosity_info() a_ : Any = logging.get_logger(__name__) a_ : List[str] = """Hello, World!""" a_ : Any = """en_XX""" def a_ ( __snake_case : str , __snake_case : str , __snake_case : bool ) -> str: """simple docstring""" lowerCamelCase_ =Path('''data_bin''' ) lowerCamelCase_ =FairseqXmodModel.from_pretrained( model_name_or_path=str(Path(__snake_case ).parent ) , checkpoint_file=Path(__snake_case ).name , _name='''xmod_base''' , arch='''xmod_base''' , task='''multilingual_masked_lm''' , data_name_or_path=str(__snake_case ) , bpe='''sentencepiece''' , sentencepiece_model=str(Path(__snake_case ).parent / '''sentencepiece.bpe.model''' ) , src_dict=str(data_dir / '''dict.txt''' ) , ) xmod.eval() # disable dropout print(__snake_case ) lowerCamelCase_ =xmod.model.encoder.sentence_encoder lowerCamelCase_ =XmodConfig( vocab_size=xmod_sent_encoder.embed_tokens.num_embeddings , hidden_size=xmod.cfg.model.encoder_embed_dim , num_hidden_layers=xmod.cfg.model.encoder_layers , num_attention_heads=xmod.cfg.model.encoder_attention_heads , intermediate_size=xmod.cfg.model.encoder_ffn_embed_dim , max_position_embeddings=514 , type_vocab_size=1 , layer_norm_eps=1e-5 , pre_norm=xmod.cfg.model.encoder_normalize_before , adapter_reduction_factor=getattr(xmod.cfg.model , '''bottleneck''' , 2 ) , adapter_layer_norm=xmod.cfg.model.adapter_layer_norm , adapter_reuse_layer_norm=xmod.cfg.model.adapter_reuse_layer_norm , ln_before_adapter=xmod.cfg.model.ln_before_adapter , languages=xmod.cfg.model.languages , ) if classification_head: lowerCamelCase_ =xmod.model.classification_heads['''mnli'''].out_proj.weight.shape[0] print('''Our X-MOD config:''' , __snake_case ) lowerCamelCase_ =XmodForSequenceClassification(__snake_case ) if classification_head else XmodForMaskedLM(__snake_case ) model.eval() # Now let's copy all the weights. # Embeddings lowerCamelCase_ =xmod_sent_encoder.embed_tokens.weight lowerCamelCase_ =xmod_sent_encoder.embed_positions.weight lowerCamelCase_ =torch.zeros_like( model.roberta.embeddings.token_type_embeddings.weight ) # just zero them out b/c xmod doesn't use them. lowerCamelCase_ =xmod_sent_encoder.layernorm_embedding.weight lowerCamelCase_ =xmod_sent_encoder.layernorm_embedding.bias for i in range(config.num_hidden_layers ): # Encoder: start of layer lowerCamelCase_ =model.roberta.encoder.layer[i] lowerCamelCase_ =xmod_sent_encoder.layers[i] # self attention lowerCamelCase_ =layer.attention.self if not ( xmod_layer.self_attn.k_proj.weight.data.shape == xmod_layer.self_attn.q_proj.weight.data.shape == xmod_layer.self_attn.v_proj.weight.data.shape == torch.Size((config.hidden_size, config.hidden_size) ) ): raise AssertionError('''Dimensions of self-attention weights do not match.''' ) lowerCamelCase_ =xmod_layer.self_attn.q_proj.weight lowerCamelCase_ =xmod_layer.self_attn.q_proj.bias lowerCamelCase_ =xmod_layer.self_attn.k_proj.weight lowerCamelCase_ =xmod_layer.self_attn.k_proj.bias lowerCamelCase_ =xmod_layer.self_attn.v_proj.weight lowerCamelCase_ =xmod_layer.self_attn.v_proj.bias # self-attention output lowerCamelCase_ =layer.attention.output if self_output.dense.weight.shape != xmod_layer.self_attn.out_proj.weight.shape: raise AssertionError('''Dimensions of self-attention output weights do not match.''' ) lowerCamelCase_ =xmod_layer.self_attn.out_proj.weight lowerCamelCase_ =xmod_layer.self_attn.out_proj.bias lowerCamelCase_ =xmod_layer.self_attn_layer_norm.weight lowerCamelCase_ =xmod_layer.self_attn_layer_norm.bias # intermediate lowerCamelCase_ =layer.intermediate if intermediate.dense.weight.shape != xmod_layer.fca.weight.shape: raise AssertionError('''Dimensions of intermediate weights do not match.''' ) lowerCamelCase_ =xmod_layer.fca.weight lowerCamelCase_ =xmod_layer.fca.bias # output lowerCamelCase_ =layer.output if bert_output.dense.weight.shape != xmod_layer.fca.weight.shape: raise AssertionError('''Dimensions of feed-forward weights do not match.''' ) lowerCamelCase_ =xmod_layer.fca.weight lowerCamelCase_ =xmod_layer.fca.bias lowerCamelCase_ =xmod_layer.final_layer_norm.weight lowerCamelCase_ =xmod_layer.final_layer_norm.bias if bert_output.adapter_layer_norm is not None: lowerCamelCase_ =xmod_layer.adapter_layer_norm.weight lowerCamelCase_ =xmod_layer.adapter_layer_norm.bias if sorted(bert_output.adapter_modules.keys() ) != sorted(xmod_layer.adapter_modules.keys() ): raise AssertionError('''Lists of language adapters do not match.''' ) for lang_code, adapter in xmod_layer.adapter_modules.items(): lowerCamelCase_ =bert_output.adapter_modules[lang_code] lowerCamelCase_ =xmod_layer.adapter_modules[lang_code] lowerCamelCase_ =from_adapter.fca.weight lowerCamelCase_ =from_adapter.fca.bias lowerCamelCase_ =from_adapter.fca.weight lowerCamelCase_ =from_adapter.fca.bias # end of layer if xmod_sent_encoder.layer_norm is not None: lowerCamelCase_ =xmod_sent_encoder.layer_norm.weight lowerCamelCase_ =xmod_sent_encoder.layer_norm.bias if classification_head: lowerCamelCase_ =xmod.model.classification_heads['''mnli'''].dense.weight lowerCamelCase_ =xmod.model.classification_heads['''mnli'''].dense.bias lowerCamelCase_ =xmod.model.classification_heads['''mnli'''].out_proj.weight lowerCamelCase_ =xmod.model.classification_heads['''mnli'''].out_proj.bias else: # LM Head lowerCamelCase_ =xmod.model.encoder.lm_head.dense.weight lowerCamelCase_ =xmod.model.encoder.lm_head.dense.bias lowerCamelCase_ =xmod.model.encoder.lm_head.layer_norm.weight lowerCamelCase_ =xmod.model.encoder.lm_head.layer_norm.bias lowerCamelCase_ =xmod.model.encoder.lm_head.weight lowerCamelCase_ =xmod.model.encoder.lm_head.bias # Let's check that we get the same results. lowerCamelCase_ =xmod.encode(__snake_case ).unsqueeze(0 ) # batch of size 1 model.roberta.set_default_language(__snake_case ) lowerCamelCase_ =model(__snake_case )[0] if classification_head: lowerCamelCase_ =xmod.model.classification_heads['''mnli'''](xmod.extract_features(__snake_case ) ) else: lowerCamelCase_ =xmod.model(__snake_case , lang_id=[SAMPLE_LANGUAGE] )[0] print(our_output.shape , their_output.shape ) lowerCamelCase_ =torch.max(torch.abs(our_output - their_output ) ).item() print(F'''max_absolute_diff = {max_absolute_diff}''' ) # ~ 1e-7 lowerCamelCase_ =torch.allclose(__snake_case , __snake_case , atol=1e-3 ) print('''Do both models output the same tensors?''' , '''🔥''' if success else '''💩''' ) if not success: raise Exception('''Something went wRoNg''' ) Path(__snake_case ).mkdir(parents=__snake_case , exist_ok=__snake_case ) print(F'''Saving model to {pytorch_dump_folder_path}''' ) model.save_pretrained(__snake_case ) if __name__ == "__main__": a_ : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--xmod_checkpoint_path""", default=None, type=str, required=True, help="""Path the official PyTorch dump.""" ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model.""" ) parser.add_argument( """--classification_head""", action="""store_true""", help="""Whether to convert a final classification head.""" ) a_ : int = parser.parse_args() convert_xmod_checkpoint_to_pytorch( args.xmod_checkpoint_path, args.pytorch_dump_folder_path, args.classification_head )
6
'''simple docstring''' import unittest import torch from diffusers import VQModel from diffusers.utils import floats_tensor, torch_device from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin, UNetTesterMixin enable_full_determinism() class __UpperCamelCase ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): lowercase : Union[str, Any] =VQModel lowercase : Union[str, Any] ='sample' @property def lowercase__ ( self, lowerCAmelCase=(32, 32) ): """simple docstring""" lowerCamelCase_ =4 lowerCamelCase_ =3 lowerCamelCase_ =floats_tensor((batch_size, num_channels) + sizes ).to(lowerCAmelCase ) return {"sample": image} @property def lowercase__ ( self ): """simple docstring""" return (3, 32, 32) @property def lowercase__ ( self ): """simple docstring""" return (3, 32, 32) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ ={ '''block_out_channels''': [32, 64], '''in_channels''': 3, '''out_channels''': 3, '''down_block_types''': ['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''], '''up_block_types''': ['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''], '''latent_channels''': 3, } lowerCamelCase_ =self.dummy_input return init_dict, inputs_dict def lowercase__ ( self ): """simple docstring""" pass def lowercase__ ( self ): """simple docstring""" pass def lowercase__ ( self ): """simple docstring""" lowerCamelCase_, lowerCamelCase_ =VQModel.from_pretrained('''fusing/vqgan-dummy''', output_loading_info=lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) self.assertEqual(len(loading_info['''missing_keys'''] ), 0 ) model.to(lowerCAmelCase ) lowerCamelCase_ =model(**self.dummy_input ) assert image is not None, "Make sure output is not None" def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =VQModel.from_pretrained('''fusing/vqgan-dummy''' ) model.to(lowerCAmelCase ).eval() torch.manual_seed(0 ) if torch.cuda.is_available(): torch.cuda.manual_seed_all(0 ) lowerCamelCase_ =torch.randn(1, model.config.in_channels, model.config.sample_size, model.config.sample_size ) lowerCamelCase_ =image.to(lowerCAmelCase ) with torch.no_grad(): lowerCamelCase_ =model(lowerCAmelCase ).sample lowerCamelCase_ =output[0, -1, -3:, -3:].flatten().cpu() # fmt: off lowerCamelCase_ =torch.tensor([-0.0_1_5_3, -0.4_0_4_4, -0.1_8_8_0, -0.5_1_6_1, -0.2_4_1_8, -0.4_0_7_2, -0.1_6_1_2, -0.0_6_3_3, -0.0_1_4_3] ) # fmt: on self.assertTrue(torch.allclose(lowerCAmelCase, lowerCAmelCase, atol=1e-3 ) )
6
1
'''simple docstring''' import argparse import torch from transformers import GPTaConfig, GPTaModel, load_tf_weights_in_gpta from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() def a_ ( __snake_case : str , __snake_case : Optional[int] , __snake_case : int ) -> Dict: """simple docstring""" # Construct model if gpta_config_file == "": lowerCamelCase_ =GPTaConfig() else: lowerCamelCase_ =GPTaConfig.from_json_file(__snake_case ) lowerCamelCase_ =GPTaModel(__snake_case ) # Load weights from numpy load_tf_weights_in_gpta(__snake_case , __snake_case , __snake_case ) # Save pytorch-model lowerCamelCase_ =pytorch_dump_folder_path + '''/''' + WEIGHTS_NAME lowerCamelCase_ =pytorch_dump_folder_path + '''/''' + CONFIG_NAME print(F'''Save PyTorch model to {pytorch_weights_dump_path}''' ) torch.save(model.state_dict() , __snake_case ) print(F'''Save configuration file to {pytorch_config_dump_path}''' ) with open(__snake_case , '''w''' , encoding='''utf-8''' ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": a_ : Any = argparse.ArgumentParser() # Required parameters parser.add_argument( """--gpt2_checkpoint_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path.""" ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model.""" ) parser.add_argument( """--gpt2_config_file""", default="""""", type=str, help=( """An optional config json file corresponding to the pre-trained OpenAI model. \n""" """This specifies the model architecture.""" ), ) a_ : List[Any] = parser.parse_args() convert_gpta_checkpoint_to_pytorch(args.gpta_checkpoint_path, args.gpta_config_file, args.pytorch_dump_folder_path)
6
'''simple docstring''' import datasets from .evaluate import evaluate a_ : List[Any] = """\ @article{hendrycks2021cuad, title={CUAD: An Expert-Annotated NLP Dataset for Legal Contract Review}, author={Dan Hendrycks and Collin Burns and Anya Chen and Spencer Ball}, journal={arXiv preprint arXiv:2103.06268}, year={2021} } """ a_ : List[Any] = """ This metric wrap the official scoring script for version 1 of the Contract Understanding Atticus Dataset (CUAD). Contract Understanding Atticus Dataset (CUAD) v1 is a corpus of more than 13,000 labels in 510 commercial legal contracts that have been manually labeled to identify 41 categories of important clauses that lawyers look for when reviewing contracts in connection with corporate transactions. """ a_ : Any = """ Computes CUAD scores (EM, F1, AUPR, Precision@80%Recall, and Precision@90%Recall). Args: predictions: List of question-answers dictionaries with the following key-values: - 'id': id of the question-answer pair as given in the references (see below) - 'prediction_text': list of possible texts for the answer, as a list of strings depending on a threshold on the confidence probability of each prediction. references: List of question-answers dictionaries with the following key-values: - 'id': id of the question-answer pair (see above), - 'answers': a Dict in the CUAD dataset format { 'text': list of possible texts for the answer, as a list of strings 'answer_start': list of start positions for the answer, as a list of ints } Note that answer_start values are not taken into account to compute the metric. Returns: 'exact_match': Exact match (the normalized answer exactly match the gold answer) 'f1': The F-score of predicted tokens versus the gold answer 'aupr': Area Under the Precision-Recall curve 'prec_at_80_recall': Precision at 80% recall 'prec_at_90_recall': Precision at 90% recall Examples: >>> predictions = [{'prediction_text': ['The seller:', 'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.'], 'id': 'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties'}] >>> references = [{'answers': {'answer_start': [143, 49], 'text': ['The seller:', 'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.']}, 'id': 'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties'}] >>> cuad_metric = datasets.load_metric(\"cuad\") >>> results = cuad_metric.compute(predictions=predictions, references=references) >>> print(results) {'exact_match': 100.0, 'f1': 100.0, 'aupr': 0.0, 'prec_at_80_recall': 1.0, 'prec_at_90_recall': 1.0} """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __UpperCamelCase ( datasets.Metric ): def lowercase__ ( self ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION, citation=_CITATION, inputs_description=_KWARGS_DESCRIPTION, features=datasets.Features( { '''predictions''': { '''id''': datasets.Value('''string''' ), '''prediction_text''': datasets.features.Sequence(datasets.Value('''string''' ) ), }, '''references''': { '''id''': datasets.Value('''string''' ), '''answers''': datasets.features.Sequence( { '''text''': datasets.Value('''string''' ), '''answer_start''': datasets.Value('''int32''' ), } ), }, } ), codebase_urls=['''https://www.atticusprojectai.org/cuad'''], reference_urls=['''https://www.atticusprojectai.org/cuad'''], ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ ={prediction['''id''']: prediction['''prediction_text'''] for prediction in predictions} lowerCamelCase_ =[ { '''paragraphs''': [ { '''qas''': [ { '''answers''': [{'''text''': answer_text} for answer_text in ref['''answers''']['''text''']], '''id''': ref['''id'''], } for ref in references ] } ] } ] lowerCamelCase_ =evaluate(dataset=lowerCAmelCase, predictions=lowerCAmelCase ) return score
6
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, is_vision_available, ) a_ : List[Any] = {"""processing_layoutxlm""": ["""LayoutXLMProcessor"""]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ : int = ["""LayoutXLMTokenizer"""] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ : Tuple = ["""LayoutXLMTokenizerFast"""] if TYPE_CHECKING: from .processing_layoutxlm import LayoutXLMProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutxlm import LayoutXLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutxlm_fast import LayoutXLMTokenizerFast else: import sys a_ : Union[str, Any] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
6
'''simple docstring''' import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert_fast import BertTokenizerFast from .tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer, DPRReaderTokenizer a_ : Tuple = logging.get_logger(__name__) a_ : int = {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""} a_ : Tuple = { """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""" ), }, } a_ : Union[str, 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""" ), }, } a_ : str = { """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""" ), }, } a_ : int = { """facebook/dpr-ctx_encoder-single-nq-base""": 5_12, """facebook/dpr-ctx_encoder-multiset-base""": 5_12, } a_ : List[Any] = { """facebook/dpr-question_encoder-single-nq-base""": 5_12, """facebook/dpr-question_encoder-multiset-base""": 5_12, } a_ : Optional[Any] = { """facebook/dpr-reader-single-nq-base""": 5_12, """facebook/dpr-reader-multiset-base""": 5_12, } a_ : Optional[int] = { """facebook/dpr-ctx_encoder-single-nq-base""": {"""do_lower_case""": True}, """facebook/dpr-ctx_encoder-multiset-base""": {"""do_lower_case""": True}, } a_ : List[str] = { """facebook/dpr-question_encoder-single-nq-base""": {"""do_lower_case""": True}, """facebook/dpr-question_encoder-multiset-base""": {"""do_lower_case""": True}, } a_ : Dict = { """facebook/dpr-reader-single-nq-base""": {"""do_lower_case""": True}, """facebook/dpr-reader-multiset-base""": {"""do_lower_case""": True}, } class __UpperCamelCase ( lowerCamelCase__ ): lowercase : List[Any] =VOCAB_FILES_NAMES lowercase : Any =CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP lowercase : Optional[Any] =CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase : List[str] =CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION lowercase : Dict =DPRContextEncoderTokenizer class __UpperCamelCase ( lowerCamelCase__ ): lowercase : Optional[int] =VOCAB_FILES_NAMES lowercase : List[Any] =QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP lowercase : Optional[Any] =QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase : List[Any] =QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION lowercase : List[Any] =DPRQuestionEncoderTokenizer a_ : Union[str, Any] = collections.namedtuple( """DPRSpanPrediction""", ["""span_score""", """relevance_score""", """doc_id""", """start_index""", """end_index""", """text"""] ) a_ : Dict = collections.namedtuple("""DPRReaderOutput""", ["""start_logits""", """end_logits""", """relevance_logits"""]) a_ : Dict = 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 __UpperCamelCase : def __call__( self, lowerCAmelCase, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = False, lowerCAmelCase = False, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, **lowerCAmelCase, ): """simple docstring""" if titles is None and texts is None: return super().__call__( lowerCAmelCase, padding=lowerCAmelCase, truncation=lowerCAmelCase, max_length=lowerCAmelCase, return_tensors=lowerCAmelCase, return_attention_mask=lowerCAmelCase, **lowerCAmelCase, ) elif titles is None or texts is None: lowerCamelCase_ =titles if texts is None else texts return super().__call__( lowerCAmelCase, lowerCAmelCase, padding=lowerCAmelCase, truncation=lowerCAmelCase, max_length=lowerCAmelCase, return_tensors=lowerCAmelCase, return_attention_mask=lowerCAmelCase, **lowerCAmelCase, ) lowerCamelCase_ =titles if not isinstance(lowerCAmelCase, lowerCAmelCase ) else [titles] lowerCamelCase_ =texts if not isinstance(lowerCAmelCase, lowerCAmelCase ) else [texts] lowerCamelCase_ =len(lowerCAmelCase ) lowerCamelCase_ =questions if not isinstance(lowerCAmelCase, lowerCAmelCase ) else [questions] * n_passages assert len(lowerCAmelCase ) == len( lowerCAmelCase ), f'''There should be as many titles than texts but got {len(lowerCAmelCase )} titles and {len(lowerCAmelCase )} texts.''' lowerCamelCase_ =super().__call__(lowerCAmelCase, lowerCAmelCase, padding=lowerCAmelCase, truncation=lowerCAmelCase )['''input_ids'''] lowerCamelCase_ =super().__call__(lowerCAmelCase, add_special_tokens=lowerCAmelCase, padding=lowerCAmelCase, truncation=lowerCAmelCase )['''input_ids'''] lowerCamelCase_ ={ '''input_ids''': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(lowerCAmelCase, lowerCAmelCase ) ] } if return_attention_mask is not False: lowerCamelCase_ =[] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) lowerCamelCase_ =attention_mask return self.pad(lowerCAmelCase, padding=lowerCAmelCase, max_length=lowerCAmelCase, return_tensors=lowerCAmelCase ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase = 16, lowerCAmelCase = 64, lowerCAmelCase = 4, ): """simple docstring""" lowerCamelCase_ =reader_input['''input_ids'''] lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ =reader_output[:3] lowerCamelCase_ =len(lowerCAmelCase ) lowerCamelCase_ =sorted(range(lowerCAmelCase ), reverse=lowerCAmelCase, key=relevance_logits.__getitem__ ) lowerCamelCase_ =[] for doc_id in sorted_docs: lowerCamelCase_ =list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence lowerCamelCase_ =sequence_ids.index(self.sep_token_id, 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: lowerCamelCase_ =sequence_ids.index(self.pad_token_id ) else: lowerCamelCase_ =len(lowerCAmelCase ) lowerCamelCase_ =self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len], end_logits=end_logits[doc_id][passage_offset:sequence_len], max_answer_length=lowerCAmelCase, top_spans=lowerCAmelCase, ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index], relevance_score=relevance_logits[doc_id], doc_id=lowerCAmelCase, start_index=lowerCAmelCase, end_index=lowerCAmelCase, text=self.decode(sequence_ids[start_index : end_index + 1] ), ) ) if len(lowerCAmelCase ) >= num_spans: break return nbest_spans_predictions[:num_spans] def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, ): """simple docstring""" lowerCamelCase_ =[] for start_index, start_score in enumerate(lowerCAmelCase ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) lowerCamelCase_ =sorted(lowerCAmelCase, key=lambda lowerCAmelCase : x[1], reverse=lowerCAmelCase ) lowerCamelCase_ =[] for (start_index, end_index), score in scores: assert start_index <= end_index, f'''Wrong span indices: [{start_index}:{end_index}]''' lowerCamelCase_ =end_index - start_index + 1 assert length <= max_answer_length, f'''Span is too long: {length} > {max_answer_length}''' if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(lowerCAmelCase ) == top_spans: break return chosen_span_intervals @add_end_docstrings(lowerCamelCase__ ) class __UpperCamelCase ( lowerCamelCase__ , lowerCamelCase__ ): lowercase : int =VOCAB_FILES_NAMES lowercase : Tuple =READER_PRETRAINED_VOCAB_FILES_MAP lowercase : Tuple =READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase : List[str] =READER_PRETRAINED_INIT_CONFIGURATION lowercase : int =['input_ids', 'attention_mask'] lowercase : Dict =DPRReaderTokenizer
6
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging a_ : Optional[int] = logging.get_logger(__name__) a_ : List[Any] = { """tiiuae/falcon-40b""": """https://huggingface.co/tiiuae/falcon-40b/resolve/main/config.json""", """tiiuae/falcon-7b""": """https://huggingface.co/tiiuae/falcon-7b/resolve/main/config.json""", } class __UpperCamelCase ( lowerCamelCase__ ): lowercase : str ='falcon' lowercase : List[str] =['past_key_values'] def __init__( self, lowerCAmelCase=65_024, lowerCAmelCase=4_544, lowerCAmelCase=32, lowerCAmelCase=71, lowerCAmelCase=1e-5, lowerCAmelCase=0.0_2, lowerCAmelCase=True, lowerCAmelCase=0.0, lowerCAmelCase=0.0, lowerCAmelCase=None, lowerCAmelCase=False, lowerCAmelCase=False, lowerCAmelCase=True, lowerCAmelCase=True, lowerCAmelCase=False, lowerCAmelCase=11, lowerCAmelCase=11, **lowerCAmelCase, ): """simple docstring""" lowerCamelCase_ =vocab_size # Backward compatibility with n_embed kwarg lowerCamelCase_ =kwargs.pop('''n_embed''', lowerCAmelCase ) lowerCamelCase_ =hidden_size if n_embed is None else n_embed lowerCamelCase_ =num_hidden_layers lowerCamelCase_ =num_attention_heads lowerCamelCase_ =layer_norm_epsilon lowerCamelCase_ =initializer_range lowerCamelCase_ =use_cache lowerCamelCase_ =hidden_dropout lowerCamelCase_ =attention_dropout lowerCamelCase_ =bos_token_id lowerCamelCase_ =eos_token_id lowerCamelCase_ =num_attention_heads if num_kv_heads is None else num_kv_heads lowerCamelCase_ =alibi lowerCamelCase_ =new_decoder_architecture lowerCamelCase_ =multi_query # Ignored when new_decoder_architecture is True lowerCamelCase_ =parallel_attn lowerCamelCase_ =bias super().__init__(bos_token_id=lowerCAmelCase, eos_token_id=lowerCAmelCase, **lowerCAmelCase ) @property def lowercase__ ( self ): """simple docstring""" return self.hidden_size // self.num_attention_heads @property def lowercase__ ( self ): """simple docstring""" return not self.alibi
6
'''simple docstring''' from unittest import TestCase from datasets import Dataset from minhash_deduplication import deduplicate_dataset, make_duplicate_clusters def a_ ( ) -> Tuple: """simple docstring""" lowerCamelCase_ ={ '''repo_name''': ['''test_repo1''', '''test_repo2''', '''test_repo3'''], '''path''': ['''test_1.py''', '''test_2.py''', '''unit_test.py'''], '''content''': ['''a ''' * 20, '''a ''' * 30, '''b ''' * 7], } lowerCamelCase_ =Dataset.from_dict(__snake_case ) return dataset class __UpperCamelCase ( lowerCamelCase__ ): def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =get_dataset() lowerCamelCase_ =make_duplicate_clusters(lowerCAmelCase, 0.8_5 ) self.assertEqual(len(duplicate_clusters[0] ), 2 ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =get_dataset() lowerCamelCase_, lowerCamelCase_ =deduplicate_dataset(lowerCAmelCase ) self.assertEqual(len(lowerCAmelCase ), 2 ) print(lowerCAmelCase ) self.assertEqual(duplicate_clusters[0][0]['''copies'''], 2 ) self.assertEqual(duplicate_clusters[0][0]['''is_extreme'''], lowerCAmelCase )
6
1
'''simple docstring''' import copy from ...configuration_utils import PretrainedConfig from ...utils import add_start_docstrings a_ : Any = R""" [`RagConfig`] stores the configuration of a *RagModel*. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information. Args: title_sep (`str`, *optional*, defaults to `\" / \"`): Separator inserted between the title and the text of the retrieved document when calling [`RagRetriever`]. doc_sep (`str`, *optional*, defaults to `\" // \"`): Separator inserted between the text of the retrieved document and the original input when calling [`RagRetriever`]. n_docs (`int`, *optional*, defaults to 5): Number of documents to retrieve. max_combined_length (`int`, *optional*, defaults to 300): Max length of contextualized input returned by [`~RagRetriever.__call__`]. retrieval_vector_size (`int`, *optional*, defaults to 768): Dimensionality of the document embeddings indexed by [`RagRetriever`]. retrieval_batch_size (`int`, *optional*, defaults to 8): Retrieval batch size, defined as the number of queries issues concurrently to the faiss index encapsulated [`RagRetriever`]. dataset (`str`, *optional*, defaults to `\"wiki_dpr\"`): A dataset identifier of the indexed dataset in HuggingFace Datasets (list all available datasets and ids using `datasets.list_datasets()`). dataset_split (`str`, *optional*, defaults to `\"train\"`) Which split of the `dataset` to load. index_name (`str`, *optional*, defaults to `\"compressed\"`) The index name of the index associated with the `dataset`. One can choose between `\"legacy\"`, `\"exact\"` and `\"compressed\"`. index_path (`str`, *optional*) The path to the serialized faiss index on disk. passages_path (`str`, *optional*): A path to text passages compatible with the faiss index. Required if using [`~models.rag.retrieval_rag.LegacyIndex`] use_dummy_dataset (`bool`, *optional*, defaults to `False`) Whether to load a \"dummy\" variant of the dataset specified by `dataset`. label_smoothing (`float`, *optional*, defaults to 0.0): Only relevant if `return_loss` is set to `True`. Controls the `epsilon` parameter value for label smoothing in the loss calculation. If set to 0, no label smoothing is performed. do_marginalize (`bool`, *optional*, defaults to `False`): If `True`, the logits are marginalized over all documents by making use of `torch.nn.functional.log_softmax`. reduce_loss (`bool`, *optional*, defaults to `False`): Whether or not to reduce the NLL loss using the `torch.Tensor.sum` operation. do_deduplication (`bool`, *optional*, defaults to `True`): Whether or not to deduplicate the generations from different context documents for a given input. Has to be set to `False` if used while training with distributed backend. exclude_bos_score (`bool`, *optional*, defaults to `False`): Whether or not to disregard the BOS token when computing the loss. output_retrieved(`bool`, *optional*, defaults to `False`): If set to `True`, `retrieved_doc_embeds`, `retrieved_doc_ids`, `context_input_ids` and `context_attention_mask` are returned. See returned tensors for more detail. use_cache (`bool`, *optional*, defaults to `True`): Whether or not the model should return the last key/values attentions (not used by all models). forced_eos_token_id (`int`, *optional*): The id of the token to force as the last generated token when `max_length` is reached. Usually set to `eos_token_id`. """ @add_start_docstrings(lowerCamelCase__ ) class __UpperCamelCase ( lowerCamelCase__ ): lowercase : Tuple ='rag' lowercase : List[Any] =True def __init__( self, lowerCAmelCase=None, lowerCAmelCase=True, lowerCAmelCase=None, lowerCAmelCase=None, lowerCAmelCase=None, lowerCAmelCase=None, lowerCAmelCase=None, lowerCAmelCase=" / ", lowerCAmelCase=" // ", lowerCAmelCase=5, lowerCAmelCase=300, lowerCAmelCase=768, lowerCAmelCase=8, lowerCAmelCase="wiki_dpr", lowerCAmelCase="train", lowerCAmelCase="compressed", lowerCAmelCase=None, lowerCAmelCase=None, lowerCAmelCase=False, lowerCAmelCase=False, lowerCAmelCase=0.0, lowerCAmelCase=True, lowerCAmelCase=False, lowerCAmelCase=False, lowerCAmelCase=False, lowerCAmelCase=True, lowerCAmelCase=None, **lowerCAmelCase, ): """simple docstring""" super().__init__( bos_token_id=lowerCAmelCase, pad_token_id=lowerCAmelCase, eos_token_id=lowerCAmelCase, decoder_start_token_id=lowerCAmelCase, forced_eos_token_id=lowerCAmelCase, is_encoder_decoder=lowerCAmelCase, prefix=lowerCAmelCase, vocab_size=lowerCAmelCase, **lowerCAmelCase, ) assert ( "question_encoder" in kwargs and "generator" in kwargs ), "Config has to be initialized with question_encoder and generator config" lowerCamelCase_ =kwargs.pop('''question_encoder''' ) lowerCamelCase_ =question_encoder_config.pop('''model_type''' ) lowerCamelCase_ =kwargs.pop('''generator''' ) lowerCamelCase_ =decoder_config.pop('''model_type''' ) from ..auto.configuration_auto import AutoConfig lowerCamelCase_ =AutoConfig.for_model(lowerCAmelCase, **lowerCAmelCase ) lowerCamelCase_ =AutoConfig.for_model(lowerCAmelCase, **lowerCAmelCase ) lowerCamelCase_ =reduce_loss lowerCamelCase_ =label_smoothing lowerCamelCase_ =exclude_bos_score lowerCamelCase_ =do_marginalize lowerCamelCase_ =title_sep lowerCamelCase_ =doc_sep lowerCamelCase_ =n_docs lowerCamelCase_ =max_combined_length lowerCamelCase_ =dataset lowerCamelCase_ =dataset_split lowerCamelCase_ =index_name lowerCamelCase_ =retrieval_vector_size lowerCamelCase_ =retrieval_batch_size lowerCamelCase_ =passages_path lowerCamelCase_ =index_path lowerCamelCase_ =use_dummy_dataset lowerCamelCase_ =output_retrieved lowerCamelCase_ =do_deduplication lowerCamelCase_ =use_cache if self.forced_eos_token_id is None: lowerCamelCase_ =getattr(self.generator, '''forced_eos_token_id''', lowerCAmelCase ) @classmethod def lowercase__ ( cls, lowerCAmelCase, lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" return cls(question_encoder=question_encoder_config.to_dict(), generator=generator_config.to_dict(), **lowerCAmelCase ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =copy.deepcopy(self.__dict__ ) lowerCamelCase_ =self.question_encoder.to_dict() lowerCamelCase_ =self.generator.to_dict() lowerCamelCase_ =self.__class__.model_type return output
6
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_torch_available, ) a_ : Any = { """configuration_trocr""": ["""TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP""", """TrOCRConfig"""], """processing_trocr""": ["""TrOCRProcessor"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ : Optional[int] = [ """TROCR_PRETRAINED_MODEL_ARCHIVE_LIST""", """TrOCRForCausalLM""", """TrOCRPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_trocr import TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP, TrOCRConfig from .processing_trocr import TrOCRProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_trocr import TROCR_PRETRAINED_MODEL_ARCHIVE_LIST, TrOCRForCausalLM, TrOCRPreTrainedModel else: import sys a_ : int = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
6
1
'''simple docstring''' from __future__ import annotations import collections import pprint from pathlib import Path def a_ ( __snake_case : str ) -> str: """simple docstring""" return "".join(sorted(__snake_case ) ) def a_ ( __snake_case : str ) -> list[str]: """simple docstring""" return word_by_signature[signature(__snake_case )] a_ : str = Path(__file__).parent.joinpath("""words.txt""").read_text(encoding="""utf-8""") a_ : Union[str, Any] = sorted({word.strip().lower() for word in data.splitlines()}) a_ : List[Any] = collections.defaultdict(list) for word in word_list: word_by_signature[signature(word)].append(word) if __name__ == "__main__": a_ : str = {word: anagram(word) for word in word_list if len(anagram(word)) > 1} with open("""anagrams.txt""", """w""") as file: file.write("""all_anagrams = \n """) file.write(pprint.pformat(all_anagrams))
6
'''simple docstring''' from collections import defaultdict from math import gcd def a_ ( __snake_case : int = 150_0000 ) -> int: """simple docstring""" lowerCamelCase_ =defaultdict(__snake_case ) lowerCamelCase_ =2 while 2 * euclid_m * (euclid_m + 1) <= limit: for euclid_n in range((euclid_m % 2) + 1 , __snake_case , 2 ): if gcd(__snake_case , __snake_case ) > 1: continue lowerCamelCase_ =2 * euclid_m * (euclid_m + euclid_n) for perimeter in range(__snake_case , limit + 1 , __snake_case ): frequencies[perimeter] += 1 euclid_m += 1 return sum(1 for frequency in frequencies.values() if frequency == 1 ) if __name__ == "__main__": print(F"""{solution() = }""")
6
1
'''simple docstring''' from argparse import ArgumentParser from datasets.commands.convert import ConvertCommand from datasets.commands.dummy_data import DummyDataCommand from datasets.commands.env import EnvironmentCommand from datasets.commands.run_beam import RunBeamCommand from datasets.commands.test import TestCommand from datasets.utils.logging import set_verbosity_info def a_ ( __snake_case : Tuple ) -> Dict: """simple docstring""" return {key.lstrip('''-''' ): value for key, value in zip(unknown_args[::2] , unknown_args[1::2] )} def a_ ( ) -> str: """simple docstring""" lowerCamelCase_ =ArgumentParser( '''HuggingFace Datasets CLI tool''' , usage='''datasets-cli <command> [<args>]''' , allow_abbrev=__snake_case ) lowerCamelCase_ =parser.add_subparsers(help='''datasets-cli command helpers''' ) set_verbosity_info() # Register commands ConvertCommand.register_subcommand(__snake_case ) EnvironmentCommand.register_subcommand(__snake_case ) TestCommand.register_subcommand(__snake_case ) RunBeamCommand.register_subcommand(__snake_case ) DummyDataCommand.register_subcommand(__snake_case ) # Parse args lowerCamelCase_, lowerCamelCase_ =parser.parse_known_args() if not hasattr(__snake_case , '''func''' ): parser.print_help() exit(1 ) lowerCamelCase_ =parse_unknown_args(__snake_case ) # Run lowerCamelCase_ =args.func(__snake_case , **__snake_case ) service.run() if __name__ == "__main__": main()
6
'''simple docstring''' import argparse import os # New Code # 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 import find_executable_batch_size ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to ensure out-of-memory errors never # interrupt training, 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) # # 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 # ######################################################################## a_ : Tuple = 16 a_ : Optional[int] = 32 def a_ ( __snake_case : Accelerator , __snake_case : int = 16 ) -> str: """simple docstring""" lowerCamelCase_ =AutoTokenizer.from_pretrained('''bert-base-cased''' ) lowerCamelCase_ =load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(__snake_case : int ): # max_length=None => use the model max length (it's actually the default) lowerCamelCase_ =tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=__snake_case , max_length=__snake_case ) 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(): lowerCamelCase_ =datasets.map( __snake_case , batched=__snake_case , 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 lowerCamelCase_ =tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(__snake_case : Any ): # On TPU it's best to pad everything to the same length or training will be very slow. lowerCamelCase_ =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": lowerCamelCase_ =16 elif accelerator.mixed_precision != "no": lowerCamelCase_ =8 else: lowerCamelCase_ =None return tokenizer.pad( __snake_case , padding='''longest''' , max_length=__snake_case , pad_to_multiple_of=__snake_case , return_tensors='''pt''' , ) # Instantiate dataloaders. lowerCamelCase_ =DataLoader( tokenized_datasets['''train'''] , shuffle=__snake_case , collate_fn=__snake_case , batch_size=__snake_case ) lowerCamelCase_ =DataLoader( tokenized_datasets['''validation'''] , shuffle=__snake_case , collate_fn=__snake_case , batch_size=__snake_case ) 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 a_ : Tuple = mocked_dataloaders # noqa: F811 def a_ ( __snake_case : List[str] , __snake_case : Tuple ) -> Optional[Any]: """simple docstring""" # For testing only if os.environ.get('''TESTING_MOCKED_DATALOADERS''' , __snake_case ) == "1": lowerCamelCase_ =2 # Initialize accelerator lowerCamelCase_ =Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs lowerCamelCase_ =config['''lr'''] lowerCamelCase_ =int(config['''num_epochs'''] ) lowerCamelCase_ =int(config['''seed'''] ) lowerCamelCase_ =int(config['''batch_size'''] ) lowerCamelCase_ =evaluate.load('''glue''' , '''mrpc''' ) # New Code # # We now can define an inner training loop function. It should take a batch size as the only parameter, # and build the dataloaders in there. # It also gets our decorator @find_executable_batch_size(starting_batch_size=__snake_case ) def inner_training_loop(__snake_case : Union[str, Any] ): # And now just move everything below under this function # We need to bring in the Accelerator object from earlier nonlocal accelerator # And reset all of its attributes that could hold onto any memory: accelerator.free_memory() # Then we can declare the model, optimizer, and everything else: set_seed(__snake_case ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) lowerCamelCase_ =AutoModelForSequenceClassification.from_pretrained('''bert-base-cased''' , return_dict=__snake_case ) # 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). lowerCamelCase_ =model.to(accelerator.device ) # Instantiate optimizer lowerCamelCase_ =AdamW(params=model.parameters() , lr=__snake_case ) lowerCamelCase_, lowerCamelCase_ =get_dataloaders(__snake_case , __snake_case ) # Instantiate scheduler lowerCamelCase_ =get_linear_schedule_with_warmup( optimizer=__snake_case , num_warmup_steps=100 , num_training_steps=(len(__snake_case ) * num_epochs) , ) # 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. lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ =accelerator.prepare( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ) # Now we train the model for epoch in range(__snake_case ): model.train() for step, batch in enumerate(__snake_case ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) lowerCamelCase_ =model(**__snake_case ) lowerCamelCase_ =outputs.loss accelerator.backward(__snake_case ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(__snake_case ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): lowerCamelCase_ =model(**__snake_case ) lowerCamelCase_ =outputs.logits.argmax(dim=-1 ) lowerCamelCase_, lowerCamelCase_ =accelerator.gather_for_metrics((predictions, batch['''labels''']) ) metric.add_batch( predictions=__snake_case , references=__snake_case , ) lowerCamelCase_ =metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F'''epoch {epoch}:''' , __snake_case ) # New Code # # And call it at the end with no arguments # Note: You could also refactor this outside of your training loop function inner_training_loop() def a_ ( ) -> Dict: """simple docstring""" lowerCamelCase_ =argparse.ArgumentParser(description='''Simple example of training script.''' ) parser.add_argument( '''--mixed_precision''' , type=__snake_case , default=__snake_case , 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.''' ) lowerCamelCase_ =parser.parse_args() lowerCamelCase_ ={'''lr''': 2e-5, '''num_epochs''': 3, '''seed''': 42, '''batch_size''': 16} training_function(__snake_case , __snake_case ) if __name__ == "__main__": main()
6
1
'''simple docstring''' import argparse import os # New Code # 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 import find_executable_batch_size ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to ensure out-of-memory errors never # interrupt training, 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) # # 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 # ######################################################################## a_ : Tuple = 16 a_ : Optional[int] = 32 def a_ ( __snake_case : Accelerator , __snake_case : int = 16 ) -> str: """simple docstring""" lowerCamelCase_ =AutoTokenizer.from_pretrained('''bert-base-cased''' ) lowerCamelCase_ =load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(__snake_case : int ): # max_length=None => use the model max length (it's actually the default) lowerCamelCase_ =tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=__snake_case , max_length=__snake_case ) 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(): lowerCamelCase_ =datasets.map( __snake_case , batched=__snake_case , 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 lowerCamelCase_ =tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(__snake_case : Any ): # On TPU it's best to pad everything to the same length or training will be very slow. lowerCamelCase_ =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": lowerCamelCase_ =16 elif accelerator.mixed_precision != "no": lowerCamelCase_ =8 else: lowerCamelCase_ =None return tokenizer.pad( __snake_case , padding='''longest''' , max_length=__snake_case , pad_to_multiple_of=__snake_case , return_tensors='''pt''' , ) # Instantiate dataloaders. lowerCamelCase_ =DataLoader( tokenized_datasets['''train'''] , shuffle=__snake_case , collate_fn=__snake_case , batch_size=__snake_case ) lowerCamelCase_ =DataLoader( tokenized_datasets['''validation'''] , shuffle=__snake_case , collate_fn=__snake_case , batch_size=__snake_case ) 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 a_ : Tuple = mocked_dataloaders # noqa: F811 def a_ ( __snake_case : List[str] , __snake_case : Tuple ) -> Optional[Any]: """simple docstring""" # For testing only if os.environ.get('''TESTING_MOCKED_DATALOADERS''' , __snake_case ) == "1": lowerCamelCase_ =2 # Initialize accelerator lowerCamelCase_ =Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs lowerCamelCase_ =config['''lr'''] lowerCamelCase_ =int(config['''num_epochs'''] ) lowerCamelCase_ =int(config['''seed'''] ) lowerCamelCase_ =int(config['''batch_size'''] ) lowerCamelCase_ =evaluate.load('''glue''' , '''mrpc''' ) # New Code # # We now can define an inner training loop function. It should take a batch size as the only parameter, # and build the dataloaders in there. # It also gets our decorator @find_executable_batch_size(starting_batch_size=__snake_case ) def inner_training_loop(__snake_case : Union[str, Any] ): # And now just move everything below under this function # We need to bring in the Accelerator object from earlier nonlocal accelerator # And reset all of its attributes that could hold onto any memory: accelerator.free_memory() # Then we can declare the model, optimizer, and everything else: set_seed(__snake_case ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) lowerCamelCase_ =AutoModelForSequenceClassification.from_pretrained('''bert-base-cased''' , return_dict=__snake_case ) # 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). lowerCamelCase_ =model.to(accelerator.device ) # Instantiate optimizer lowerCamelCase_ =AdamW(params=model.parameters() , lr=__snake_case ) lowerCamelCase_, lowerCamelCase_ =get_dataloaders(__snake_case , __snake_case ) # Instantiate scheduler lowerCamelCase_ =get_linear_schedule_with_warmup( optimizer=__snake_case , num_warmup_steps=100 , num_training_steps=(len(__snake_case ) * num_epochs) , ) # 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. lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ =accelerator.prepare( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ) # Now we train the model for epoch in range(__snake_case ): model.train() for step, batch in enumerate(__snake_case ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) lowerCamelCase_ =model(**__snake_case ) lowerCamelCase_ =outputs.loss accelerator.backward(__snake_case ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(__snake_case ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): lowerCamelCase_ =model(**__snake_case ) lowerCamelCase_ =outputs.logits.argmax(dim=-1 ) lowerCamelCase_, lowerCamelCase_ =accelerator.gather_for_metrics((predictions, batch['''labels''']) ) metric.add_batch( predictions=__snake_case , references=__snake_case , ) lowerCamelCase_ =metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F'''epoch {epoch}:''' , __snake_case ) # New Code # # And call it at the end with no arguments # Note: You could also refactor this outside of your training loop function inner_training_loop() def a_ ( ) -> Dict: """simple docstring""" lowerCamelCase_ =argparse.ArgumentParser(description='''Simple example of training script.''' ) parser.add_argument( '''--mixed_precision''' , type=__snake_case , default=__snake_case , 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.''' ) lowerCamelCase_ =parser.parse_args() lowerCamelCase_ ={'''lr''': 2e-5, '''num_epochs''': 3, '''seed''': 42, '''batch_size''': 16} training_function(__snake_case , __snake_case ) if __name__ == "__main__": main()
6
'''simple docstring''' import argparse import os import re # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_dummies.py a_ : List[str] = """src/diffusers""" # Matches is_xxx_available() a_ : int = re.compile(R"""is\_([a-z_]*)_available\(\)""") # Matches from xxx import bla a_ : List[str] = re.compile(R"""\s+from\s+\S*\s+import\s+([^\(\s].*)\n""") a_ : Optional[Any] = """ {0} = None """ a_ : List[Any] = """ class {0}(metaclass=DummyObject): _backends = {1} def __init__(self, *args, **kwargs): requires_backends(self, {1}) @classmethod def from_config(cls, *args, **kwargs): requires_backends(cls, {1}) @classmethod def from_pretrained(cls, *args, **kwargs): requires_backends(cls, {1}) """ a_ : Optional[Any] = """ def {0}(*args, **kwargs): requires_backends({0}, {1}) """ def a_ ( __snake_case : Union[str, Any] ) -> List[Any]: """simple docstring""" lowerCamelCase_ =_re_backend.findall(__snake_case ) if len(__snake_case ) == 0: return None return "_and_".join(__snake_case ) def a_ ( ) -> Optional[int]: """simple docstring""" with open(os.path.join(__snake_case , '''__init__.py''' ) , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: lowerCamelCase_ =f.readlines() # Get to the point we do the actual imports for type checking lowerCamelCase_ =0 lowerCamelCase_ ={} # Go through the end of the file while line_index < len(__snake_case ): # If the line contains is_backend_available, we grab all objects associated with the `else` block lowerCamelCase_ =find_backend(lines[line_index] ) if backend is not None: while not lines[line_index].startswith('''else:''' ): line_index += 1 line_index += 1 lowerCamelCase_ =[] # Until we unindent, add backend objects to the list while line_index < len(__snake_case ) and len(lines[line_index] ) > 1: lowerCamelCase_ =lines[line_index] lowerCamelCase_ =_re_single_line_import.search(__snake_case ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(''', ''' ) ) elif line.startswith(''' ''' * 8 ): objects.append(line[8:-2] ) line_index += 1 if len(__snake_case ) > 0: lowerCamelCase_ =objects else: line_index += 1 return backend_specific_objects def a_ ( __snake_case : Dict , __snake_case : int ) -> Union[str, Any]: """simple docstring""" if name.isupper(): return DUMMY_CONSTANT.format(__snake_case ) elif name.islower(): return DUMMY_FUNCTION.format(__snake_case , __snake_case ) else: return DUMMY_CLASS.format(__snake_case , __snake_case ) def a_ ( __snake_case : Tuple=None ) -> List[str]: """simple docstring""" if backend_specific_objects is None: lowerCamelCase_ =read_init() # For special correspondence backend to module name as used in the function requires_modulename lowerCamelCase_ ={} for backend, objects in backend_specific_objects.items(): lowerCamelCase_ ='''[''' + ''', '''.join(F'''"{b}"''' for b in backend.split('''_and_''' ) ) + ''']''' lowerCamelCase_ ='''# This file is autogenerated by the command `make fix-copies`, do not edit.\n''' dummy_file += "from ..utils import DummyObject, requires_backends\n\n" dummy_file += "\n".join([create_dummy_object(__snake_case , __snake_case ) for o in objects] ) lowerCamelCase_ =dummy_file return dummy_files def a_ ( __snake_case : Dict=False ) -> Union[str, Any]: """simple docstring""" lowerCamelCase_ =create_dummy_files() # For special correspondence backend to shortcut as used in utils/dummy_xxx_objects.py lowerCamelCase_ ={'''torch''': '''pt'''} # Locate actual dummy modules and read their content. lowerCamelCase_ =os.path.join(__snake_case , '''utils''' ) lowerCamelCase_ ={ backend: os.path.join(__snake_case , F'''dummy_{short_names.get(__snake_case , __snake_case )}_objects.py''' ) for backend in dummy_files.keys() } lowerCamelCase_ ={} for backend, file_path in dummy_file_paths.items(): if os.path.isfile(__snake_case ): with open(__snake_case , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: lowerCamelCase_ =f.read() else: lowerCamelCase_ ='''''' for backend in dummy_files.keys(): if dummy_files[backend] != actual_dummies[backend]: if overwrite: print( F'''Updating diffusers.utils.dummy_{short_names.get(__snake_case , __snake_case )}_objects.py as the main ''' '''__init__ has new objects.''' ) with open(dummy_file_paths[backend] , '''w''' , encoding='''utf-8''' , newline='''\n''' ) as f: f.write(dummy_files[backend] ) else: raise ValueError( '''The main __init__ has objects that are not present in ''' F'''diffusers.utils.dummy_{short_names.get(__snake_case , __snake_case )}_objects.py. Run `make fix-copies` ''' '''to fix this.''' ) if __name__ == "__main__": a_ : Tuple = argparse.ArgumentParser() parser.add_argument("""--fix_and_overwrite""", action="""store_true""", help="""Whether to fix inconsistencies.""") a_ : Tuple = parser.parse_args() check_dummies(args.fix_and_overwrite)
6
1
'''simple docstring''' from binascii import hexlify from hashlib import shaaaa from os import urandom # RFC 3526 - More Modular Exponential (MODP) Diffie-Hellman groups for # Internet Key Exchange (IKE) https://tools.ietf.org/html/rfc3526 a_ : int = { # 1536-bit 5: { """prime""": int( """FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1""" + """29024E088A67CC74020BBEA63B139B22514A08798E3404DD""" + """EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245""" + """E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED""" + """EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D""" + """C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F""" + """83655D23DCA3AD961C62F356208552BB9ED529077096966D""" + """670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF""", base=16, ), """generator""": 2, }, # 2048-bit 14: { """prime""": int( """FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1""" + """29024E088A67CC74020BBEA63B139B22514A08798E3404DD""" + """EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245""" + """E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED""" + """EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D""" + """C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F""" + """83655D23DCA3AD961C62F356208552BB9ED529077096966D""" + """670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B""" + """E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9""" + """DE2BCBF6955817183995497CEA956AE515D2261898FA0510""" + """15728E5A8AACAA68FFFFFFFFFFFFFFFF""", base=16, ), """generator""": 2, }, # 3072-bit 15: { """prime""": int( """FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1""" + """29024E088A67CC74020BBEA63B139B22514A08798E3404DD""" + """EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245""" + """E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED""" + """EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D""" + """C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F""" + """83655D23DCA3AD961C62F356208552BB9ED529077096966D""" + """670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B""" + """E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9""" + """DE2BCBF6955817183995497CEA956AE515D2261898FA0510""" + """15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64""" + """ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7""" + """ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B""" + """F12FFA06D98A0864D87602733EC86A64521F2B18177B200C""" + """BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31""" + """43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF""", base=16, ), """generator""": 2, }, # 4096-bit 16: { """prime""": int( """FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1""" + """29024E088A67CC74020BBEA63B139B22514A08798E3404DD""" + """EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245""" + """E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED""" + """EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D""" + """C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F""" + """83655D23DCA3AD961C62F356208552BB9ED529077096966D""" + """670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B""" + """E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9""" + """DE2BCBF6955817183995497CEA956AE515D2261898FA0510""" + """15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64""" + """ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7""" + """ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B""" + """F12FFA06D98A0864D87602733EC86A64521F2B18177B200C""" + """BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31""" + """43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7""" + """88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA""" + """2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6""" + """287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED""" + """1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9""" + """93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199""" + """FFFFFFFFFFFFFFFF""", base=16, ), """generator""": 2, }, # 6144-bit 17: { """prime""": int( """FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08""" + """8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B""" + """302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9""" + """A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6""" + """49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8""" + """FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D""" + """670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C""" + """180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718""" + """3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D""" + """04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D""" + """B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226""" + """1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C""" + """BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC""" + """E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26""" + """99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB""" + """04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2""" + """233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127""" + """D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492""" + """36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406""" + """AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918""" + """DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151""" + """2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03""" + """F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F""" + """BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA""" + """CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B""" + """B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632""" + """387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E""" + """6DCC4024FFFFFFFFFFFFFFFF""", base=16, ), """generator""": 2, }, # 8192-bit 18: { """prime""": int( """FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1""" + """29024E088A67CC74020BBEA63B139B22514A08798E3404DD""" + """EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245""" + """E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED""" + """EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D""" + """C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F""" + """83655D23DCA3AD961C62F356208552BB9ED529077096966D""" + """670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B""" + """E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9""" + """DE2BCBF6955817183995497CEA956AE515D2261898FA0510""" + """15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64""" + """ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7""" + """ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B""" + """F12FFA06D98A0864D87602733EC86A64521F2B18177B200C""" + """BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31""" + """43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7""" + """88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA""" + """2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6""" + """287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED""" + """1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9""" + """93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492""" + """36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD""" + """F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831""" + """179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B""" + """DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF""" + """5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6""" + """D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3""" + """23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA""" + """CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328""" + """06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C""" + """DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE""" + """12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4""" + """38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300""" + """741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568""" + """3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9""" + """22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B""" + """4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A""" + """062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36""" + """4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1""" + """B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92""" + """4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47""" + """9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71""" + """60C980DD98EDD3DFFFFFFFFFFFFFFFFF""", base=16, ), """generator""": 2, }, } class __UpperCamelCase : def __init__( self, lowerCAmelCase = 14 ): """simple docstring""" if group not in primes: raise ValueError('''Unsupported Group''' ) lowerCamelCase_ =primes[group]['''prime'''] lowerCamelCase_ =primes[group]['''generator'''] lowerCamelCase_ =int(hexlify(urandom(32 ) ), base=16 ) def lowercase__ ( self ): """simple docstring""" return hex(self.__private_key )[2:] def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =pow(self.generator, self.__private_key, self.prime ) return hex(lowerCAmelCase )[2:] def lowercase__ ( self, lowerCAmelCase ): """simple docstring""" return ( 2 <= key <= self.prime - 2 and pow(lowerCAmelCase, (self.prime - 1) // 2, self.prime ) == 1 ) def lowercase__ ( self, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =int(lowerCAmelCase, base=16 ) if not self.is_valid_public_key(lowerCAmelCase ): raise ValueError('''Invalid public key''' ) lowerCamelCase_ =pow(lowerCAmelCase, self.__private_key, self.prime ) return shaaaa(str(lowerCAmelCase ).encode() ).hexdigest() @staticmethod def lowercase__ ( lowerCAmelCase, lowerCAmelCase ): """simple docstring""" return ( 2 <= remote_public_key_str <= prime - 2 and pow(lowerCAmelCase, (prime - 1) // 2, lowerCAmelCase ) == 1 ) @staticmethod def lowercase__ ( lowerCAmelCase, lowerCAmelCase, lowerCAmelCase = 14 ): """simple docstring""" lowerCamelCase_ =int(lowerCAmelCase, base=16 ) lowerCamelCase_ =int(lowerCAmelCase, base=16 ) lowerCamelCase_ =primes[group]['''prime'''] if not DiffieHellman.is_valid_public_key_static(lowerCAmelCase, lowerCAmelCase ): raise ValueError('''Invalid public key''' ) lowerCamelCase_ =pow(lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ) return shaaaa(str(lowerCAmelCase ).encode() ).hexdigest() if __name__ == "__main__": import doctest doctest.testmod()
6
'''simple docstring''' a_ : List[Any] = [sum(int(c, 10) ** 2 for c in i.__str__()) for i in range(10_00_00)] def a_ ( __snake_case : int ) -> int: """simple docstring""" lowerCamelCase_ =0 while number: # Increased Speed Slightly by checking every 5 digits together. sum_of_digits_squared += DIGITS_SQUARED[number % 10_0000] number //= 10_0000 return sum_of_digits_squared # There are 2 Chains made, # One ends with 89 with the chain member 58 being the one which when declared first, # there will be the least number of iterations for all the members to be checked. # The other one ends with 1 and has only one element 1. # So 58 and 1 are chosen to be declared at the starting. # Changed dictionary to an array to quicken the solution a_ : list[bool | None] = [None] * 10_00_00_00 a_ : List[Any] = True a_ : Optional[Any] = False def a_ ( __snake_case : int ) -> bool: """simple docstring""" if CHAINS[number - 1] is not None: return CHAINS[number - 1] # type: ignore lowerCamelCase_ =chain(next_number(__snake_case ) ) lowerCamelCase_ =number_chain while number < 1000_0000: lowerCamelCase_ =number_chain number *= 10 return number_chain def a_ ( __snake_case : int = 1000_0000 ) -> int: """simple docstring""" for i in range(1 , __snake_case ): if CHAINS[i] is None: chain(i + 1 ) return CHAINS[:number].count(__snake_case ) if __name__ == "__main__": import doctest doctest.testmod() print(F"""{solution() = }""")
6
1
'''simple docstring''' import re import warnings from contextlib import contextmanager from ...processing_utils import ProcessorMixin class __UpperCamelCase ( lowerCamelCase__ ): lowercase : List[str] =['image_processor', 'tokenizer'] lowercase : Optional[int] ='AutoImageProcessor' lowercase : List[str] ='AutoTokenizer' def __init__( self, lowerCAmelCase=None, lowerCAmelCase=None, **lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =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, ) lowerCamelCase_ =kwargs.pop('''feature_extractor''' ) lowerCamelCase_ =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 ) lowerCamelCase_ =self.image_processor lowerCamelCase_ =False def __call__( self, *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" if self._in_target_context_manager: return self.current_processor(*lowerCAmelCase, **lowerCAmelCase ) lowerCamelCase_ =kwargs.pop('''images''', lowerCAmelCase ) lowerCamelCase_ =kwargs.pop('''text''', lowerCAmelCase ) if len(lowerCAmelCase ) > 0: lowerCamelCase_ =args[0] lowerCamelCase_ =args[1:] if images is None and text is None: raise ValueError('''You need to specify either an `images` or `text` input to process.''' ) if images is not None: lowerCamelCase_ =self.image_processor(lowerCAmelCase, *lowerCAmelCase, **lowerCAmelCase ) if text is not None: lowerCamelCase_ =self.tokenizer(lowerCAmelCase, **lowerCAmelCase ) if text is None: return inputs elif images is None: return encodings else: lowerCamelCase_ =encodings['''input_ids'''] return inputs def lowercase__ ( self, *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" return self.tokenizer.batch_decode(*lowerCAmelCase, **lowerCAmelCase ) def lowercase__ ( self, *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" return self.tokenizer.decode(*lowerCAmelCase, **lowerCAmelCase ) @contextmanager def lowercase__ ( self ): """simple docstring""" warnings.warn( '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your ''' '''labels by using the argument `text` of the regular `__call__` method (either in the same call as ''' '''your images inputs, or in a separate call.''' ) lowerCamelCase_ =True lowerCamelCase_ =self.tokenizer yield lowerCamelCase_ =self.image_processor lowerCamelCase_ =False def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase=False, lowerCAmelCase=None ): """simple docstring""" if added_vocab is None: lowerCamelCase_ =self.tokenizer.get_added_vocab() lowerCamelCase_ ={} while tokens: lowerCamelCase_ =re.search(R'''<s_(.*?)>''', lowerCAmelCase, re.IGNORECASE ) if start_token is None: break lowerCamelCase_ =start_token.group(1 ) lowerCamelCase_ =re.search(Rf'''</s_{key}>''', lowerCAmelCase, re.IGNORECASE ) lowerCamelCase_ =start_token.group() if end_token is None: lowerCamelCase_ =tokens.replace(lowerCAmelCase, '''''' ) else: lowerCamelCase_ =end_token.group() lowerCamelCase_ =re.escape(lowerCAmelCase ) lowerCamelCase_ =re.escape(lowerCAmelCase ) lowerCamelCase_ =re.search(f'''{start_token_escaped}(.*?){end_token_escaped}''', lowerCAmelCase, re.IGNORECASE ) if content is not None: lowerCamelCase_ =content.group(1 ).strip() if r"<s_" in content and r"</s_" in content: # non-leaf node lowerCamelCase_ =self.tokenajson(lowerCAmelCase, is_inner_value=lowerCAmelCase, added_vocab=lowerCAmelCase ) if value: if len(lowerCAmelCase ) == 1: lowerCamelCase_ =value[0] lowerCamelCase_ =value else: # leaf nodes lowerCamelCase_ =[] for leaf in content.split(R'''<sep/>''' ): lowerCamelCase_ =leaf.strip() if leaf in added_vocab and leaf[0] == "<" and leaf[-2:] == "/>": lowerCamelCase_ =leaf[1:-2] # for categorical special tokens output[key].append(lowerCAmelCase ) if len(output[key] ) == 1: lowerCamelCase_ =output[key][0] lowerCamelCase_ =tokens[tokens.find(lowerCAmelCase ) + len(lowerCAmelCase ) :].strip() if tokens[:6] == r"<sep/>": # non-leaf nodes return [output] + self.tokenajson(tokens[6:], is_inner_value=lowerCAmelCase, added_vocab=lowerCAmelCase ) if len(lowerCAmelCase ): return [output] if is_inner_value else output else: return [] if is_inner_value else {"text_sequence": tokens} @property def lowercase__ ( self ): """simple docstring""" warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''', lowerCAmelCase, ) return self.image_processor_class @property def lowercase__ ( self ): """simple docstring""" warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''', lowerCAmelCase, ) return self.image_processor
6
'''simple docstring''' from argparse import ArgumentParser from . import BaseTransformersCLICommand def a_ ( __snake_case : Tuple ) -> str: """simple docstring""" return DownloadCommand(args.model , args.cache_dir , args.force , args.trust_remote_code ) class __UpperCamelCase ( lowerCamelCase__ ): @staticmethod def lowercase__ ( lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =parser.add_parser('''download''' ) download_parser.add_argument( '''--cache-dir''', type=lowerCAmelCase, default=lowerCAmelCase, help='''Path to location to store the models''' ) download_parser.add_argument( '''--force''', action='''store_true''', help='''Force the model to be download even if already in cache-dir''' ) download_parser.add_argument( '''--trust-remote-code''', action='''store_true''', help='''Whether or not to allow for custom models defined on the Hub in their own modeling files. Use only if you\'ve reviewed the code as it will execute on your local machine''', ) download_parser.add_argument('''model''', type=lowerCAmelCase, help='''Name of the model to download''' ) download_parser.set_defaults(func=lowerCAmelCase ) def __init__( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =model lowerCamelCase_ =cache lowerCamelCase_ =force lowerCamelCase_ =trust_remote_code def lowercase__ ( self ): """simple docstring""" from ..models.auto import AutoModel, AutoTokenizer AutoModel.from_pretrained( self._model, cache_dir=self._cache, force_download=self._force, trust_remote_code=self._trust_remote_code ) AutoTokenizer.from_pretrained( self._model, cache_dir=self._cache, force_download=self._force, trust_remote_code=self._trust_remote_code )
6
1
'''simple docstring''' from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, convert_to_rgb, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging a_ : int = logging.get_logger(__name__) if is_vision_available(): import PIL class __UpperCamelCase ( lowerCamelCase__ ): lowercase : List[Any] =['pixel_values'] def __init__( self, lowerCAmelCase = True, lowerCAmelCase = None, lowerCAmelCase = PILImageResampling.BICUBIC, lowerCAmelCase = True, lowerCAmelCase = None, lowerCAmelCase = True, lowerCAmelCase = 1 / 255, lowerCAmelCase = True, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = True, **lowerCAmelCase, ): """simple docstring""" super().__init__(**lowerCAmelCase ) lowerCamelCase_ =size if size is not None else {'''shortest_edge''': 224} lowerCamelCase_ =get_size_dict(lowerCAmelCase, default_to_square=lowerCAmelCase ) lowerCamelCase_ =crop_size if crop_size is not None else {'''height''': 224, '''width''': 224} lowerCamelCase_ =get_size_dict(lowerCAmelCase, default_to_square=lowerCAmelCase, param_name='''crop_size''' ) 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 if image_mean is not None else OPENAI_CLIP_MEAN lowerCamelCase_ =image_std if image_std is not None else OPENAI_CLIP_STD lowerCamelCase_ =do_convert_rgb def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase = PILImageResampling.BICUBIC, lowerCAmelCase = None, **lowerCAmelCase, ): """simple docstring""" lowerCamelCase_ =get_size_dict(lowerCAmelCase, default_to_square=lowerCAmelCase ) if "shortest_edge" not in size: raise ValueError(f'''The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}''' ) lowerCamelCase_ =get_resize_output_image_size(lowerCAmelCase, size=size['''shortest_edge'''], default_to_square=lowerCAmelCase ) return resize(lowerCAmelCase, size=lowerCAmelCase, resample=lowerCAmelCase, data_format=lowerCAmelCase, **lowerCAmelCase ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase = None, **lowerCAmelCase, ): """simple docstring""" lowerCamelCase_ =get_size_dict(lowerCAmelCase ) if "height" not in size or "width" not in size: raise ValueError(f'''The `size` parameter must contain the keys (height, width). Got {size.keys()}''' ) return center_crop(lowerCAmelCase, size=(size['''height'''], size['''width''']), data_format=lowerCAmelCase, **lowerCAmelCase ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase = None, **lowerCAmelCase, ): """simple docstring""" return rescale(lowerCAmelCase, scale=lowerCAmelCase, data_format=lowerCAmelCase, **lowerCAmelCase ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase = None, **lowerCAmelCase, ): """simple docstring""" return normalize(lowerCAmelCase, mean=lowerCAmelCase, std=lowerCAmelCase, data_format=lowerCAmelCase, **lowerCAmelCase ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = ChannelDimension.FIRST, **lowerCAmelCase, ): """simple docstring""" lowerCamelCase_ =do_resize if do_resize is not None else self.do_resize lowerCamelCase_ =size if size is not None else self.size lowerCamelCase_ =get_size_dict(lowerCAmelCase, param_name='''size''', default_to_square=lowerCAmelCase ) lowerCamelCase_ =resample if resample is not None else self.resample lowerCamelCase_ =do_center_crop if do_center_crop is not None else self.do_center_crop lowerCamelCase_ =crop_size if crop_size is not None else self.crop_size lowerCamelCase_ =get_size_dict(lowerCAmelCase, param_name='''crop_size''', default_to_square=lowerCAmelCase ) lowerCamelCase_ =do_rescale if do_rescale is not None else self.do_rescale lowerCamelCase_ =rescale_factor if rescale_factor is not None else self.rescale_factor lowerCamelCase_ =do_normalize if do_normalize is not None else self.do_normalize lowerCamelCase_ =image_mean if image_mean is not None else self.image_mean lowerCamelCase_ =image_std if image_std is not None else self.image_std lowerCamelCase_ =do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb lowerCamelCase_ =make_list_of_images(lowerCAmelCase ) 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.''' ) if do_resize and size is None: raise ValueError('''Size must be specified if do_resize is True.''' ) if do_center_crop and crop_size is None: raise ValueError('''Crop size must be specified if do_center_crop is True.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) # PIL RGBA images are converted to RGB if do_convert_rgb: lowerCamelCase_ =[convert_to_rgb(lowerCAmelCase ) for image in images] # All transformations expect numpy arrays. lowerCamelCase_ =[to_numpy_array(lowerCAmelCase ) for image in images] if do_resize: lowerCamelCase_ =[self.resize(image=lowerCAmelCase, size=lowerCAmelCase, resample=lowerCAmelCase ) for image in images] if do_center_crop: lowerCamelCase_ =[self.center_crop(image=lowerCAmelCase, size=lowerCAmelCase ) for image in images] if do_rescale: lowerCamelCase_ =[self.rescale(image=lowerCAmelCase, scale=lowerCAmelCase ) for image in images] if do_normalize: lowerCamelCase_ =[self.normalize(image=lowerCAmelCase, mean=lowerCAmelCase, std=lowerCAmelCase ) for image in images] lowerCamelCase_ =[to_channel_dimension_format(lowerCAmelCase, lowerCAmelCase ) for image in images] lowerCamelCase_ ={'''pixel_values''': images} return BatchFeature(data=lowerCAmelCase, tensor_type=lowerCAmelCase )
6
'''simple docstring''' import os import time from dataclasses import dataclass, field from enum import Enum from typing import Dict, List, Optional, Union import torch from filelock import FileLock from torch.utils.data import Dataset from ...models.auto.modeling_auto import MODEL_FOR_QUESTION_ANSWERING_MAPPING from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging from ..processors.squad import SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features a_ : List[str] = logging.get_logger(__name__) a_ : Optional[Any] = list(MODEL_FOR_QUESTION_ANSWERING_MAPPING.keys()) a_ : Optional[Any] = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class __UpperCamelCase : lowercase : str =field( default=lowerCamelCase__ , metadata={'help': 'Model type selected in the list: ' + ', '.join(lowerCamelCase__ )} ) lowercase : str =field( default=lowerCamelCase__ , metadata={'help': 'The input data dir. Should contain the .json files for the SQuAD task.'} ) lowercase : int =field( default=1_28 , metadata={ 'help': ( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) } , ) lowercase : int =field( default=1_28 , metadata={'help': 'When splitting up a long document into chunks, how much stride to take between chunks.'} , ) lowercase : int =field( default=64 , metadata={ 'help': ( 'The maximum number of tokens for the question. Questions longer than this will ' 'be truncated to this length.' ) } , ) lowercase : int =field( default=30 , metadata={ 'help': ( 'The maximum length of an answer that can be generated. This is needed because the start ' 'and end predictions are not conditioned on one another.' ) } , ) lowercase : bool =field( default=lowerCamelCase__ , metadata={'help': 'Overwrite the cached training and evaluation sets'} ) lowercase : bool =field( default=lowerCamelCase__ , metadata={'help': 'If true, the SQuAD examples contain some that do not have an answer.'} ) lowercase : float =field( default=0.0 , metadata={'help': 'If null_score - best_non_null is greater than the threshold predict null.'} ) lowercase : int =field( default=20 , metadata={'help': 'If null_score - best_non_null is greater than the threshold predict null.'} ) lowercase : int =field( default=0 , metadata={ 'help': ( 'language id of input for language-specific xlm models (see' ' tokenization_xlm.PRETRAINED_INIT_CONFIGURATION)' ) } , ) lowercase : int =field(default=1 , metadata={'help': 'multiple threads for converting example to features'} ) class __UpperCamelCase ( lowerCamelCase__ ): lowercase : Optional[Any] ='train' lowercase : Any ='dev' class __UpperCamelCase ( lowerCamelCase__ ): lowercase : SquadDataTrainingArguments lowercase : List[SquadFeatures] lowercase : Split lowercase : bool def __init__( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase = None, lowerCAmelCase = Split.train, lowerCAmelCase = False, lowerCAmelCase = None, lowerCAmelCase = "pt", ): """simple docstring""" lowerCamelCase_ =args lowerCamelCase_ =is_language_sensitive lowerCamelCase_ =SquadVaProcessor() if args.version_2_with_negative else SquadVaProcessor() if isinstance(lowerCAmelCase, lowerCAmelCase ): try: lowerCamelCase_ =Split[mode] except KeyError: raise KeyError('''mode is not a valid split name''' ) lowerCamelCase_ =mode # Load data features from cache or dataset file lowerCamelCase_ ='''v2''' if args.version_2_with_negative else '''v1''' lowerCamelCase_ =os.path.join( cache_dir if cache_dir is not None else args.data_dir, f'''cached_{mode.value}_{tokenizer.__class__.__name__}_{args.max_seq_length}_{version_tag}''', ) # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. lowerCamelCase_ =cached_features_file + '''.lock''' with FileLock(lowerCAmelCase ): if os.path.exists(lowerCAmelCase ) and not args.overwrite_cache: lowerCamelCase_ =time.time() lowerCamelCase_ =torch.load(lowerCAmelCase ) # Legacy cache files have only features, while new cache files # will have dataset and examples also. lowerCamelCase_ =self.old_features['''features'''] lowerCamelCase_ =self.old_features.get('''dataset''', lowerCAmelCase ) lowerCamelCase_ =self.old_features.get('''examples''', lowerCAmelCase ) logger.info( f'''Loading features from cached file {cached_features_file} [took %.3f s]''', time.time() - start ) if self.dataset is None or self.examples is None: logger.warning( f'''Deleting cached file {cached_features_file} will allow dataset and examples to be cached in''' ''' future run''' ) else: if mode == Split.dev: lowerCamelCase_ =self.processor.get_dev_examples(args.data_dir ) else: lowerCamelCase_ =self.processor.get_train_examples(args.data_dir ) lowerCamelCase_, lowerCamelCase_ =squad_convert_examples_to_features( examples=self.examples, tokenizer=lowerCAmelCase, max_seq_length=args.max_seq_length, doc_stride=args.doc_stride, max_query_length=args.max_query_length, is_training=mode == Split.train, threads=args.threads, return_dataset=lowerCAmelCase, ) lowerCamelCase_ =time.time() torch.save( {'''features''': self.features, '''dataset''': self.dataset, '''examples''': self.examples}, lowerCAmelCase, ) # ^ This seems to take a lot of time so I want to investigate why and how we can improve. logger.info( f'''Saving features into cached file {cached_features_file} [took {time.time() - start:.3f} s]''' ) def __len__( self ): """simple docstring""" return len(self.features ) def __getitem__( self, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =self.features[i] lowerCamelCase_ =torch.tensor(feature.input_ids, dtype=torch.long ) lowerCamelCase_ =torch.tensor(feature.attention_mask, dtype=torch.long ) lowerCamelCase_ =torch.tensor(feature.token_type_ids, dtype=torch.long ) lowerCamelCase_ =torch.tensor(feature.cls_index, dtype=torch.long ) lowerCamelCase_ =torch.tensor(feature.p_mask, dtype=torch.float ) lowerCamelCase_ =torch.tensor(feature.is_impossible, dtype=torch.float ) lowerCamelCase_ ={ '''input_ids''': input_ids, '''attention_mask''': attention_mask, '''token_type_ids''': token_type_ids, } if self.args.model_type in ["xlm", "roberta", "distilbert", "camembert"]: del inputs["token_type_ids"] if self.args.model_type in ["xlnet", "xlm"]: inputs.update({'''cls_index''': cls_index, '''p_mask''': p_mask} ) if self.args.version_2_with_negative: inputs.update({'''is_impossible''': is_impossible} ) if self.is_language_sensitive: inputs.update({'''langs''': (torch.ones(input_ids.shape, dtype=torch.intaa ) * self.args.lang_id)} ) if self.mode == Split.train: lowerCamelCase_ =torch.tensor(feature.start_position, dtype=torch.long ) lowerCamelCase_ =torch.tensor(feature.end_position, dtype=torch.long ) inputs.update({'''start_positions''': start_positions, '''end_positions''': end_positions} ) return inputs
6
1
'''simple docstring''' import torch from diffusers import DPMSolverSDEScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import require_torchsde from .test_schedulers import SchedulerCommonTest @require_torchsde class __UpperCamelCase ( lowerCamelCase__ ): lowercase : Optional[Any] =(DPMSolverSDEScheduler,) lowercase : Optional[int] =10 def lowercase__ ( self, **lowerCAmelCase ): """simple docstring""" lowerCamelCase_ ={ '''num_train_timesteps''': 1_100, '''beta_start''': 0.0_0_0_1, '''beta_end''': 0.0_2, '''beta_schedule''': '''linear''', '''noise_sampler_seed''': 0, } config.update(**lowerCAmelCase ) return config def lowercase__ ( self ): """simple docstring""" for timesteps in [10, 50, 100, 1_000]: self.check_over_configs(num_train_timesteps=lowerCAmelCase ) def lowercase__ ( self ): """simple docstring""" for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1], [0.0_0_0_2, 0.0_0_2, 0.0_2] ): self.check_over_configs(beta_start=lowerCAmelCase, beta_end=lowerCAmelCase ) def lowercase__ ( self ): """simple docstring""" for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=lowerCAmelCase ) def lowercase__ ( self ): """simple docstring""" for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=lowerCAmelCase ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.scheduler_classes[0] lowerCamelCase_ =self.get_scheduler_config() lowerCamelCase_ =scheduler_class(**lowerCAmelCase ) scheduler.set_timesteps(self.num_inference_steps ) lowerCamelCase_ =self.dummy_model() lowerCamelCase_ =self.dummy_sample_deter * scheduler.init_noise_sigma lowerCamelCase_ =sample.to(lowerCAmelCase ) for i, t in enumerate(scheduler.timesteps ): lowerCamelCase_ =scheduler.scale_model_input(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =model(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =scheduler.step(lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =output.prev_sample lowerCamelCase_ =torch.sum(torch.abs(lowerCAmelCase ) ) lowerCamelCase_ =torch.mean(torch.abs(lowerCAmelCase ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_6_7.4_7_8_2_1_0_4_4_9_2_1_8_7_5 ) < 1e-2 assert abs(result_mean.item() - 0.2_1_7_8_7_0_5_9_6_4_5_6_5_2_7_7 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_7_1.5_9_3_5_2_1_1_1_8_1_6_4_0_6 ) < 1e-2 assert abs(result_mean.item() - 0.2_2_3_4_2_9_0_6_8_9_2_2_9_9_6_5_2 ) < 1e-3 else: assert abs(result_sum.item() - 1_6_2.5_2_3_8_3_4_2_2_8_5_1_5_6_2 ) < 1e-2 assert abs(result_mean.item() - 0.2_1_1_6_1_9_5_7_0_8_5_1_3_2_6 ) < 1e-3 def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.scheduler_classes[0] lowerCamelCase_ =self.get_scheduler_config(prediction_type='''v_prediction''' ) lowerCamelCase_ =scheduler_class(**lowerCAmelCase ) scheduler.set_timesteps(self.num_inference_steps ) lowerCamelCase_ =self.dummy_model() lowerCamelCase_ =self.dummy_sample_deter * scheduler.init_noise_sigma lowerCamelCase_ =sample.to(lowerCAmelCase ) for i, t in enumerate(scheduler.timesteps ): lowerCamelCase_ =scheduler.scale_model_input(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =model(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =scheduler.step(lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =output.prev_sample lowerCamelCase_ =torch.sum(torch.abs(lowerCAmelCase ) ) lowerCamelCase_ =torch.mean(torch.abs(lowerCAmelCase ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_2_4.7_7_1_4_9_2_0_0_4_3_9_4_5_3 ) < 1e-2 assert abs(result_mean.item() - 0.1_6_2_2_6_2_8_9_0_1_4_8_1_6_2_8_4 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_2_8.1_6_6_3_3_6_0_5_9_5_7_0_3 ) < 1e-2 assert abs(result_mean.item() - 0.1_6_6_8_8_3_2_6_0_0_1_1_6_7_2_9_7 ) < 1e-3 else: assert abs(result_sum.item() - 1_1_9.8_4_8_7_5_4_8_8_2_8_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.1_5_6_0_5_3_0_6_6_2_5_3_6_6_2_1 ) < 1e-3 def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.scheduler_classes[0] lowerCamelCase_ =self.get_scheduler_config() lowerCamelCase_ =scheduler_class(**lowerCAmelCase ) scheduler.set_timesteps(self.num_inference_steps, device=lowerCAmelCase ) lowerCamelCase_ =self.dummy_model() lowerCamelCase_ =self.dummy_sample_deter.to(lowerCAmelCase ) * scheduler.init_noise_sigma for t in scheduler.timesteps: lowerCamelCase_ =scheduler.scale_model_input(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =model(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =scheduler.step(lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =output.prev_sample lowerCamelCase_ =torch.sum(torch.abs(lowerCAmelCase ) ) lowerCamelCase_ =torch.mean(torch.abs(lowerCAmelCase ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_6_7.4_6_9_5_7_3_9_7_4_6_0_9_3_8 ) < 1e-2 assert abs(result_mean.item() - 0.2_1_8_0_5_9_3_4_6_0_7_9_8_2_6_3_5 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_7_1.5_9_3_5_3_6_3_7_6_9_5_3_1_2 ) < 1e-2 assert abs(result_mean.item() - 0.2_2_3_4_2_9_0_8_3_8_2_4_1_5_7_7_1 ) < 1e-3 else: assert abs(result_sum.item() - 1_6_2.5_2_3_8_3_4_2_2_8_5_1_5_6_2 ) < 1e-2 assert abs(result_mean.item() - 0.2_1_1_6_1_9_5_7_0_8_5_1_3_2_6 ) < 1e-3 def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.scheduler_classes[0] lowerCamelCase_ =self.get_scheduler_config() lowerCamelCase_ =scheduler_class(**lowerCAmelCase, use_karras_sigmas=lowerCAmelCase ) scheduler.set_timesteps(self.num_inference_steps, device=lowerCAmelCase ) lowerCamelCase_ =self.dummy_model() lowerCamelCase_ =self.dummy_sample_deter.to(lowerCAmelCase ) * scheduler.init_noise_sigma lowerCamelCase_ =sample.to(lowerCAmelCase ) for t in scheduler.timesteps: lowerCamelCase_ =scheduler.scale_model_input(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =model(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =scheduler.step(lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =output.prev_sample lowerCamelCase_ =torch.sum(torch.abs(lowerCAmelCase ) ) lowerCamelCase_ =torch.mean(torch.abs(lowerCAmelCase ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_7_6.6_6_9_7_4_1_3_5_7_4_2_1_8_8 ) < 1e-2 assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1 ) < 1e-2 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_7_7.6_3_6_5_3_5_6_4_4_5_3_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1 ) < 1e-2 else: assert abs(result_sum.item() - 1_7_0.3_1_3_5_2_2_3_3_8_8_6_7_2 ) < 1e-2 assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1 ) < 1e-2
6
'''simple docstring''' import argparse import json import os from pathlib import Path import requests import torch from transformers import JukeboxConfig, JukeboxModel from transformers.utils import logging logging.set_verbosity_info() a_ : Any = logging.get_logger(__name__) a_ : Optional[int] = """https://openaipublic.azureedge.net/jukebox/models/""" a_ : Any = { """jukebox-1b-lyrics""": [ """5b/vqvae.pth.tar""", """5b/prior_level_0.pth.tar""", """5b/prior_level_1.pth.tar""", """1b_lyrics/prior_level_2.pth.tar""", ], """jukebox-5b-lyrics""": [ """5b/vqvae.pth.tar""", """5b/prior_level_0.pth.tar""", """5b/prior_level_1.pth.tar""", """5b_lyrics/prior_level_2.pth.tar""", ], } def a_ ( __snake_case : int ) -> Any: """simple docstring""" if key.endswith('''.model.1.bias''' ) and len(key.split('''.''' ) ) > 10: lowerCamelCase_ =key.replace('''.model.1.bias''' , '''.conv1d_1.bias''' ) elif key.endswith('''.model.1.weight''' ) and len(key.split('''.''' ) ) > 10: lowerCamelCase_ =key.replace('''.model.1.weight''' , '''.conv1d_1.weight''' ) elif key.endswith('''.model.3.bias''' ) and len(key.split('''.''' ) ) > 10: lowerCamelCase_ =key.replace('''.model.3.bias''' , '''.conv1d_2.bias''' ) elif key.endswith('''.model.3.weight''' ) and len(key.split('''.''' ) ) > 10: lowerCamelCase_ =key.replace('''.model.3.weight''' , '''.conv1d_2.weight''' ) if "conditioner_blocks.0." in key: lowerCamelCase_ =key.replace('''conditioner_blocks.0''' , '''conditioner_blocks''' ) if "prime_prior" in key: lowerCamelCase_ =key.replace('''prime_prior''' , '''encoder''' ) if ".emb." in key and "total" not in key and "absolute" not in key and "relative" not in key: lowerCamelCase_ =key.replace('''.emb.''' , '''.''' ) if key.endswith('''k''' ): # replace vqvae.X.k with vqvae.X.codebook return key.replace('''.k''' , '''.codebook''' ) if "y_emb." in key: return key.replace('''y_emb.''' , '''metadata_embedding.''' ) if "x_emb.emb." in key: lowerCamelCase_ =key.replace('''0.x_emb.emb''' , '''embed_tokens''' ) if "prime_state_ln" in key: return key.replace('''prime_state_ln''' , '''encoder.final_layer_norm''' ) if ".ln" in key: return key.replace('''.ln''' , '''.layer_norm''' ) if "_ln" in key: return key.replace('''_ln''' , '''_layer_norm''' ) if "prime_state_proj" in key: return key.replace('''prime_state_proj''' , '''encoder.proj_in''' ) if "prime_x_out" in key: return key.replace('''prime_x_out''' , '''encoder.lm_head''' ) if "prior.x_out" in key: return key.replace('''x_out''' , '''fc_proj_out''' ) if "x_emb" in key: return key.replace('''x_emb''' , '''embed_tokens''' ) return key def a_ ( __snake_case : Dict , __snake_case : int , __snake_case : Dict , __snake_case : Optional[Any] ) -> Optional[Any]: """simple docstring""" lowerCamelCase_ ={} import re lowerCamelCase_ =re.compile(r'''encoders.(\d*).level_blocks.(\d*).model.(\d*).(\d).(bias|weight)''' ) lowerCamelCase_ =re.compile( r'''encoders.(\d*).level_blocks.(\d*).model.(\d*).(\d).model.(\d*).model.(\d*).(bias|weight)''' ) lowerCamelCase_ =re.compile(r'''encoders.(\d*).level_blocks.(\d*).model.(\d*).(bias|weight)''' ) lowerCamelCase_ =re.compile(r'''decoders.(\d*).level_blocks.(\d*).model.(\d*).(\d).(bias|weight)''' ) lowerCamelCase_ =re.compile( r'''decoders.(\d*).level_blocks.(\d*).model.(\d*).(\d).model.(\d*).model.(\d*).(bias|weight)''' ) lowerCamelCase_ =re.compile(r'''decoders.(\d*).level_blocks.(\d*).model.(\d*).(bias|weight)''' ) lowerCamelCase_ =re.compile(r'''conditioner_blocks.(\d*).cond.model.(\d*).(\d).(bias|weight)''' ) lowerCamelCase_ =re.compile( r'''conditioner_blocks.(\d*).cond.model.(\d*).(\d).model.(\d*).model.(\d*).(bias|weight)''' ) lowerCamelCase_ =re.compile(r'''conditioner_blocks.(\d*).cond.model.(\d*).(bias|weight)''' ) for original_key, value in state_dict.items(): # rename vqvae.encoder keys if re_encoder_block_conv_in.fullmatch(__snake_case ): lowerCamelCase_ =re_encoder_block_conv_in.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =int(groups[2] ) * 2 + int(groups[3] ) lowerCamelCase_ =F'''encoders.{groups[0]}.level_blocks.{groups[1]}.downsample_block.{block_index}.{groups[-1]}''' lowerCamelCase_ =re_encoder_block_conv_in.sub(__snake_case , __snake_case ) elif re_encoder_block_resnet.fullmatch(__snake_case ): lowerCamelCase_ =re_encoder_block_resnet.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =int(groups[2] ) * 2 + int(groups[3] ) lowerCamelCase_ ={'''1''': 1, '''3''': 2}[groups[-2]] lowerCamelCase_ =F'''encoders.{groups[0]}.level_blocks.{groups[1]}.downsample_block.{block_index}.''' lowerCamelCase_ =F'''resnet_block.{groups[-3]}.conv1d_{conv_index}.{groups[-1]}''' lowerCamelCase_ =prefix + resnet_block lowerCamelCase_ =re_encoder_block_resnet.sub(__snake_case , __snake_case ) elif re_encoder_block_proj_out.fullmatch(__snake_case ): lowerCamelCase_ =re_encoder_block_proj_out.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =F'''encoders.{groups[0]}.level_blocks.{groups[1]}.proj_out.{groups[-1]}''' lowerCamelCase_ =re_encoder_block_proj_out.sub(__snake_case , __snake_case ) # rename vqvae.decoder keys elif re_decoder_block_conv_out.fullmatch(__snake_case ): lowerCamelCase_ =re_decoder_block_conv_out.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =int(groups[2] ) * 2 + int(groups[3] ) - 2 lowerCamelCase_ =F'''decoders.{groups[0]}.level_blocks.{groups[1]}.upsample_block.{block_index}.{groups[-1]}''' lowerCamelCase_ =re_decoder_block_conv_out.sub(__snake_case , __snake_case ) elif re_decoder_block_resnet.fullmatch(__snake_case ): lowerCamelCase_ =re_decoder_block_resnet.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =int(groups[2] ) * 2 + int(groups[3] ) - 2 lowerCamelCase_ ={'''1''': 1, '''3''': 2}[groups[-2]] lowerCamelCase_ =F'''decoders.{groups[0]}.level_blocks.{groups[1]}.upsample_block.{block_index}.''' lowerCamelCase_ =F'''resnet_block.{groups[-3]}.conv1d_{conv_index}.{groups[-1]}''' lowerCamelCase_ =prefix + resnet_block lowerCamelCase_ =re_decoder_block_resnet.sub(__snake_case , __snake_case ) elif re_decoder_block_proj_in.fullmatch(__snake_case ): lowerCamelCase_ =re_decoder_block_proj_in.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =F'''decoders.{groups[0]}.level_blocks.{groups[1]}.proj_in.{groups[-1]}''' lowerCamelCase_ =re_decoder_block_proj_in.sub(__snake_case , __snake_case ) # rename prior cond.model to upsampler.upsample_block and resnet elif re_prior_cond_conv_out.fullmatch(__snake_case ): lowerCamelCase_ =re_prior_cond_conv_out.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =int(groups[1] ) * 2 + int(groups[2] ) - 2 lowerCamelCase_ =F'''conditioner_blocks.upsampler.upsample_block.{block_index}.{groups[-1]}''' lowerCamelCase_ =re_prior_cond_conv_out.sub(__snake_case , __snake_case ) elif re_prior_cond_resnet.fullmatch(__snake_case ): lowerCamelCase_ =re_prior_cond_resnet.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =int(groups[1] ) * 2 + int(groups[2] ) - 2 lowerCamelCase_ ={'''1''': 1, '''3''': 2}[groups[-2]] lowerCamelCase_ =F'''conditioner_blocks.upsampler.upsample_block.{block_index}.''' lowerCamelCase_ =F'''resnet_block.{groups[-3]}.conv1d_{conv_index}.{groups[-1]}''' lowerCamelCase_ =prefix + resnet_block lowerCamelCase_ =re_prior_cond_resnet.sub(__snake_case , __snake_case ) elif re_prior_cond_proj_in.fullmatch(__snake_case ): lowerCamelCase_ =re_prior_cond_proj_in.match(__snake_case ) lowerCamelCase_ =regex_match.groups() lowerCamelCase_ =F'''conditioner_blocks.upsampler.proj_in.{groups[-1]}''' lowerCamelCase_ =re_prior_cond_proj_in.sub(__snake_case , __snake_case ) # keep original key else: lowerCamelCase_ =original_key lowerCamelCase_ =replace_key(__snake_case ) if F'''{key_prefix}.{key}''' not in model_state_dict or key is None: print(F'''failed converting {original_key} to {key}, does not match''' ) # handle missmatched shape elif value.shape != model_state_dict[F'''{key_prefix}.{key}'''].shape: lowerCamelCase_ =model_state_dict[F'''{key_prefix}.{key}'''] print(F'''{original_key}-> {key} : \nshape {val.shape} and { value.shape}, do not match''' ) lowerCamelCase_ =original_key lowerCamelCase_ =original_key lowerCamelCase_ =value return new_dict @torch.no_grad() def a_ ( __snake_case : List[str]=None , __snake_case : Tuple=None ) -> Union[str, Any]: """simple docstring""" for file in MODEL_MAPPING[model_name]: if not os.path.isfile(F'''{pytorch_dump_folder_path}/{file.split('/' )[-1]}''' ): lowerCamelCase_ =requests.get(F'''{PREFIX}{file}''' , allow_redirects=__snake_case ) os.makedirs(F'''{pytorch_dump_folder_path}/''' , exist_ok=__snake_case ) open(F'''{pytorch_dump_folder_path}/{file.split('/' )[-1]}''' , '''wb''' ).write(r.content ) lowerCamelCase_ =MODEL_MAPPING[model_name.split('''/''' )[-1]] lowerCamelCase_ =JukeboxConfig.from_pretrained(__snake_case ) lowerCamelCase_ =JukeboxModel(__snake_case ) lowerCamelCase_ =[] lowerCamelCase_ ={} for i, dict_name in enumerate(__snake_case ): lowerCamelCase_ =torch.load(F'''{pytorch_dump_folder_path}/{dict_name.split('/' )[-1]}''' )['''model'''] lowerCamelCase_ ={} for k in old_dic.keys(): if k.endswith('''.b''' ): lowerCamelCase_ =old_dic[k] elif k.endswith('''.w''' ): lowerCamelCase_ =old_dic[k] elif "level_2" not in dict_name and "cond.model." in k: lowerCamelCase_ =old_dic[k] else: lowerCamelCase_ =old_dic[k] lowerCamelCase_ ='''vqvae''' if i == 0 else F'''priors.{3 - i}''' lowerCamelCase_ =fix_jukebox_keys(__snake_case , model.state_dict() , __snake_case , __snake_case ) weight_dict.append(__snake_case ) lowerCamelCase_ =weight_dict.pop(0 ) model.vqvae.load_state_dict(__snake_case ) for i in range(len(__snake_case ) ): model.priors[i].load_state_dict(weight_dict[2 - i] ) Path(__snake_case ).mkdir(exist_ok=__snake_case ) with open(F'''{pytorch_dump_folder_path}/mapping.json''' , '''w''' ) as txtfile: json.dump(__snake_case , __snake_case ) print(F'''Saving model {model_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(__snake_case ) return weight_dict if __name__ == "__main__": a_ : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default="""jukebox-5b-lyrics""", type=str, help="""Name of the model you'd like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default="""jukebox-5b-lyrics-converted""", type=str, help="""Path to the output PyTorch model directory.""", ) a_ : Optional[int] = parser.parse_args() convert_openai_checkpoint(args.model_name, args.pytorch_dump_folder_path)
6
1
'''simple docstring''' from argparse import ArgumentParser, Namespace from typing import Any, List, Optional from ..pipelines import Pipeline, get_supported_tasks, pipeline from ..utils import logging from . import BaseTransformersCLICommand try: from fastapi import Body, FastAPI, HTTPException from fastapi.routing import APIRoute from pydantic import BaseModel from starlette.responses import JSONResponse from uvicorn import run a_ : Dict = True except (ImportError, AttributeError): a_ : Any = object def a_ ( *__snake_case : Optional[Any] , **__snake_case : List[Any] ) -> Union[str, Any]: """simple docstring""" pass a_ : Optional[Any] = False a_ : Tuple = logging.get_logger("""transformers-cli/serving""") def a_ ( __snake_case : Namespace ) -> Optional[Any]: """simple docstring""" lowerCamelCase_ =pipeline( task=args.task , model=args.model if args.model else None , config=args.config , tokenizer=args.tokenizer , device=args.device , ) return ServeCommand(__snake_case , args.host , args.port , args.workers ) class __UpperCamelCase ( lowerCamelCase__ ): lowercase : dict class __UpperCamelCase ( lowerCamelCase__ ): lowercase : List[str] lowercase : Optional[List[int]] class __UpperCamelCase ( lowerCamelCase__ ): lowercase : str class __UpperCamelCase ( lowerCamelCase__ ): lowercase : Any class __UpperCamelCase ( lowerCamelCase__ ): @staticmethod def lowercase__ ( lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =parser.add_parser( '''serve''', help='''CLI tool to run inference requests through REST and GraphQL endpoints.''' ) serve_parser.add_argument( '''--task''', type=lowerCAmelCase, choices=get_supported_tasks(), help='''The task to run the pipeline on''', ) serve_parser.add_argument('''--host''', type=lowerCAmelCase, default='''localhost''', help='''Interface the server will listen on.''' ) serve_parser.add_argument('''--port''', type=lowerCAmelCase, default=8_888, help='''Port the serving will listen to.''' ) serve_parser.add_argument('''--workers''', type=lowerCAmelCase, default=1, help='''Number of http workers''' ) serve_parser.add_argument('''--model''', type=lowerCAmelCase, help='''Model\'s name or path to stored model.''' ) serve_parser.add_argument('''--config''', type=lowerCAmelCase, help='''Model\'s config name or path to stored model.''' ) serve_parser.add_argument('''--tokenizer''', type=lowerCAmelCase, help='''Tokenizer name to use.''' ) serve_parser.add_argument( '''--device''', type=lowerCAmelCase, default=-1, help='''Indicate the device to run onto, -1 indicates CPU, >= 0 indicates GPU (default: -1)''', ) serve_parser.set_defaults(func=lowerCAmelCase ) def __init__( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =pipeline lowerCamelCase_ =host lowerCamelCase_ =port lowerCamelCase_ =workers if not _serve_dependencies_installed: raise RuntimeError( '''Using serve command requires FastAPI and uvicorn. ''' '''Please install transformers with [serving]: pip install "transformers[serving]".''' '''Or install FastAPI and uvicorn separately.''' ) else: logger.info(f'''Serving model over {host}:{port}''' ) lowerCamelCase_ =FastAPI( routes=[ APIRoute( '''/''', self.model_info, response_model=lowerCAmelCase, response_class=lowerCAmelCase, methods=['''GET'''], ), APIRoute( '''/tokenize''', self.tokenize, response_model=lowerCAmelCase, response_class=lowerCAmelCase, methods=['''POST'''], ), APIRoute( '''/detokenize''', self.detokenize, response_model=lowerCAmelCase, response_class=lowerCAmelCase, methods=['''POST'''], ), APIRoute( '''/forward''', self.forward, response_model=lowerCAmelCase, response_class=lowerCAmelCase, methods=['''POST'''], ), ], timeout=600, ) def lowercase__ ( self ): """simple docstring""" run(self._app, host=self.host, port=self.port, workers=self.workers ) def lowercase__ ( self ): """simple docstring""" return ServeModelInfoResult(infos=vars(self._pipeline.model.config ) ) def lowercase__ ( self, lowerCAmelCase = Body(lowerCAmelCase, embed=lowerCAmelCase ), lowerCAmelCase = Body(lowerCAmelCase, embed=lowerCAmelCase ) ): """simple docstring""" try: lowerCamelCase_ =self._pipeline.tokenizer.tokenize(lowerCAmelCase ) if return_ids: lowerCamelCase_ =self._pipeline.tokenizer.convert_tokens_to_ids(lowerCAmelCase ) return ServeTokenizeResult(tokens=lowerCAmelCase, tokens_ids=lowerCAmelCase ) else: return ServeTokenizeResult(tokens=lowerCAmelCase ) except Exception as e: raise HTTPException(status_code=500, detail={'''model''': '''''', '''error''': str(lowerCAmelCase )} ) def lowercase__ ( self, lowerCAmelCase = Body(lowerCAmelCase, embed=lowerCAmelCase ), lowerCAmelCase = Body(lowerCAmelCase, embed=lowerCAmelCase ), lowerCAmelCase = Body(lowerCAmelCase, embed=lowerCAmelCase ), ): """simple docstring""" try: lowerCamelCase_ =self._pipeline.tokenizer.decode(lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ) return ServeDeTokenizeResult(model='''''', text=lowerCAmelCase ) except Exception as e: raise HTTPException(status_code=500, detail={'''model''': '''''', '''error''': str(lowerCAmelCase )} ) async def lowercase__ ( self, lowerCAmelCase=Body(lowerCAmelCase, embed=lowerCAmelCase ) ): """simple docstring""" if len(lowerCAmelCase ) == 0: return ServeForwardResult(output=[], attention=[] ) try: # Forward through the model lowerCamelCase_ =self._pipeline(lowerCAmelCase ) return ServeForwardResult(output=lowerCAmelCase ) except Exception as e: raise HTTPException(500, {'''error''': str(lowerCAmelCase )} )
6
'''simple docstring''' def a_ ( __snake_case : int = 1000 ) -> int: """simple docstring""" lowerCamelCase_, lowerCamelCase_ =1, 1 lowerCamelCase_ =2 while True: lowerCamelCase_ =0 lowerCamelCase_ =fa + fa lowerCamelCase_, lowerCamelCase_ =fa, f index += 1 for _ in str(__snake_case ): i += 1 if i == n: break return index if __name__ == "__main__": print(solution(int(str(input()).strip())))
6
1
'''simple docstring''' from scipy.stats import pearsonr import datasets a_ : Dict = """ Pearson correlation coefficient and p-value for testing non-correlation. The Pearson correlation coefficient measures the linear relationship between two datasets. The calculation of the p-value relies on the assumption that each dataset is normally distributed. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Correlations of -1 or +1 imply an exact linear relationship. Positive correlations imply that as x increases, so does y. Negative correlations imply that as x increases, y decreases. The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets. """ a_ : Dict = """ Args: predictions (`list` of `int`): Predicted class labels, as returned by a model. references (`list` of `int`): Ground truth labels. return_pvalue (`boolean`): If `True`, returns the p-value, along with the correlation coefficient. If `False`, returns only the correlation coefficient. Defaults to `False`. Returns: pearsonr (`float`): Pearson correlation coefficient. Minimum possible value is -1. Maximum possible value is 1. Values of 1 and -1 indicate exact linear positive and negative relationships, respectively. A value of 0 implies no correlation. p-value (`float`): P-value, which roughly indicates the probability of an The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets. Minimum possible value is 0. Maximum possible value is 1. Higher values indicate higher probabilities. Examples: Example 1-A simple example using only predictions and references. >>> pearsonr_metric = datasets.load_metric(\"pearsonr\") >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5]) >>> print(round(results['pearsonr'], 2)) -0.74 Example 2-The same as Example 1, but that also returns the `p-value`. >>> pearsonr_metric = datasets.load_metric(\"pearsonr\") >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5], return_pvalue=True) >>> print(sorted(list(results.keys()))) ['p-value', 'pearsonr'] >>> print(round(results['pearsonr'], 2)) -0.74 >>> print(round(results['p-value'], 2)) 0.15 """ a_ : Optional[int] = """ @article{2020SciPy-NMeth, author = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and Haberland, Matt and Reddy, Tyler and Cournapeau, David and Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and Bright, Jonathan and {van der Walt}, St{\'e}fan J. and Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and Kern, Robert and Larson, Eric and Carey, C J and Polat, Ilhan and Feng, Yu and Moore, Eric W. and {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and Harris, Charles R. and Archibald, Anne M. and Ribeiro, Antonio H. and Pedregosa, Fabian and {van Mulbregt}, Paul and {SciPy 1.0 Contributors}}, title = {{{SciPy} 1.0: Fundamental Algorithms for Scientific Computing in Python}}, journal = {Nature Methods}, year = {2020}, volume = {17}, pages = {261--272}, adsurl = {https://rdcu.be/b08Wh}, doi = {10.1038/s41592-019-0686-2}, } """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __UpperCamelCase ( datasets.Metric ): def lowercase__ ( self ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION, citation=_CITATION, inputs_description=_KWARGS_DESCRIPTION, features=datasets.Features( { '''predictions''': datasets.Value('''float''' ), '''references''': datasets.Value('''float''' ), } ), reference_urls=['''https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.pearsonr.html'''], ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase=False ): """simple docstring""" if return_pvalue: lowerCamelCase_ =pearsonr(lowerCAmelCase, lowerCAmelCase ) return {"pearsonr": results[0], "p-value": results[1]} else: return {"pearsonr": float(pearsonr(lowerCAmelCase, lowerCAmelCase )[0] )}
6
'''simple docstring''' import importlib import os import sys # This is required to make the module import works (when the python process is running from the root of the repo) sys.path.append(""".""") def a_ ( __snake_case : Any ) -> Tuple: """simple docstring""" lowerCamelCase_ =test_file.split(os.path.sep ) if components[0:2] != ["tests", "models"]: raise ValueError( '''`test_file` should start with `tests/models/` (with `/` being the OS specific path separator). Got ''' F'''{test_file} instead.''' ) lowerCamelCase_ =components[-1] if not test_fn.endswith('''py''' ): raise ValueError(F'''`test_file` should be a python file. Got {test_fn} instead.''' ) if not test_fn.startswith('''test_modeling_''' ): raise ValueError( F'''`test_file` should point to a file name of the form `test_modeling_*.py`. Got {test_fn} instead.''' ) lowerCamelCase_ =components[:-1] + [test_fn.replace('''.py''' , '''''' )] lowerCamelCase_ ='''.'''.join(__snake_case ) return test_module_path def a_ ( __snake_case : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" lowerCamelCase_ =get_module_path(__snake_case ) lowerCamelCase_ =importlib.import_module(__snake_case ) return test_module def a_ ( __snake_case : Dict ) -> Tuple: """simple docstring""" lowerCamelCase_ =[] lowerCamelCase_ =get_test_module(__snake_case ) for attr in dir(__snake_case ): if attr.endswith('''ModelTester''' ): tester_classes.append(getattr(__snake_case , __snake_case ) ) # sort with class names return sorted(__snake_case , key=lambda __snake_case : x.__name__ ) def a_ ( __snake_case : List[Any] ) -> Optional[int]: """simple docstring""" lowerCamelCase_ =[] lowerCamelCase_ =get_test_module(__snake_case ) for attr in dir(__snake_case ): lowerCamelCase_ =getattr(__snake_case , __snake_case ) # (TF/Flax)ModelTesterMixin is also an attribute in specific model test module. Let's exclude them by checking # `all_model_classes` is not empty (which also excludes other special classes). lowerCamelCase_ =getattr(__snake_case , '''all_model_classes''' , [] ) if len(__snake_case ) > 0: test_classes.append(__snake_case ) # sort with class names return sorted(__snake_case , key=lambda __snake_case : x.__name__ ) def a_ ( __snake_case : List[Any] ) -> Optional[int]: """simple docstring""" lowerCamelCase_ =get_test_classes(__snake_case ) lowerCamelCase_ =set() for test_class in test_classes: model_classes.update(test_class.all_model_classes ) # sort with class names return sorted(__snake_case , key=lambda __snake_case : x.__name__ ) def a_ ( __snake_case : str ) -> str: """simple docstring""" lowerCamelCase_ =test_class() if hasattr(__snake_case , '''setUp''' ): test.setUp() lowerCamelCase_ =None if hasattr(__snake_case , '''model_tester''' ): # `(TF/Flax)ModelTesterMixin` has this attribute default to `None`. Let's skip this case. if test.model_tester is not None: lowerCamelCase_ =test.model_tester.__class__ return model_tester def a_ ( __snake_case : Dict , __snake_case : List[str] ) -> List[Any]: """simple docstring""" lowerCamelCase_ =get_test_classes(__snake_case ) lowerCamelCase_ =[] for test_class in test_classes: if model_class in test_class.all_model_classes: target_test_classes.append(__snake_case ) # sort with class names return sorted(__snake_case , key=lambda __snake_case : x.__name__ ) def a_ ( __snake_case : Optional[Any] , __snake_case : List[str] ) -> Any: """simple docstring""" lowerCamelCase_ =get_test_classes_for_model(__snake_case , __snake_case ) lowerCamelCase_ =[] for test_class in test_classes: lowerCamelCase_ =get_model_tester_from_test_class(__snake_case ) if tester_class is not None: tester_classes.append(__snake_case ) # sort with class names return sorted(__snake_case , key=lambda __snake_case : x.__name__ ) def a_ ( __snake_case : Tuple ) -> Tuple: """simple docstring""" lowerCamelCase_ =get_test_classes(__snake_case ) lowerCamelCase_ ={test_class: get_model_tester_from_test_class(__snake_case ) for test_class in test_classes} return test_tester_mapping def a_ ( __snake_case : Dict ) -> Optional[Any]: """simple docstring""" lowerCamelCase_ =get_model_classes(__snake_case ) lowerCamelCase_ ={ model_class: get_test_classes_for_model(__snake_case , __snake_case ) for model_class in model_classes } return model_test_mapping def a_ ( __snake_case : Optional[Any] ) -> Dict: """simple docstring""" lowerCamelCase_ =get_model_classes(__snake_case ) lowerCamelCase_ ={ model_class: get_tester_classes_for_model(__snake_case , __snake_case ) for model_class in model_classes } return model_to_tester_mapping def a_ ( __snake_case : List[str] ) -> List[Any]: """simple docstring""" if isinstance(__snake_case , __snake_case ): return o elif isinstance(__snake_case , __snake_case ): return o.__name__ elif isinstance(__snake_case , (list, tuple) ): return [to_json(__snake_case ) for x in o] elif isinstance(__snake_case , __snake_case ): return {to_json(__snake_case ): to_json(__snake_case ) for k, v in o.items()} else: return o
6
1
'''simple docstring''' class __UpperCamelCase : def __init__( self ): """simple docstring""" lowerCamelCase_ ='''''' lowerCamelCase_ ='''''' lowerCamelCase_ =[] def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase ): """simple docstring""" if m == -1: return n + 1 elif n == -1: return m + 1 elif self.dp[m][n] > -1: return self.dp[m][n] else: if self.worda[m] == self.worda[n]: lowerCamelCase_ =self.__min_dist_top_down_dp(m - 1, n - 1 ) else: lowerCamelCase_ =self.__min_dist_top_down_dp(lowerCAmelCase, n - 1 ) lowerCamelCase_ =self.__min_dist_top_down_dp(m - 1, lowerCAmelCase ) lowerCamelCase_ =self.__min_dist_top_down_dp(m - 1, n - 1 ) lowerCamelCase_ =1 + min(lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ) return self.dp[m][n] def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =worda lowerCamelCase_ =worda lowerCamelCase_ =[[-1 for _ in range(len(lowerCAmelCase ) )] for _ in range(len(lowerCAmelCase ) )] return self.__min_dist_top_down_dp(len(lowerCAmelCase ) - 1, len(lowerCAmelCase ) - 1 ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =worda lowerCamelCase_ =worda lowerCamelCase_ =len(lowerCAmelCase ) lowerCamelCase_ =len(lowerCAmelCase ) lowerCamelCase_ =[[0 for _ in range(n + 1 )] for _ in range(m + 1 )] for i in range(m + 1 ): for j in range(n + 1 ): if i == 0: # first string is empty lowerCamelCase_ =j elif j == 0: # second string is empty lowerCamelCase_ =i elif worda[i - 1] == worda[j - 1]: # last characters are equal lowerCamelCase_ =self.dp[i - 1][j - 1] else: lowerCamelCase_ =self.dp[i][j - 1] lowerCamelCase_ =self.dp[i - 1][j] lowerCamelCase_ =self.dp[i - 1][j - 1] lowerCamelCase_ =1 + min(lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ) return self.dp[m][n] if __name__ == "__main__": a_ : str = EditDistance() print("""****************** Testing Edit Distance DP Algorithm ******************""") print() a_ : List[str] = input("""Enter the first string: """).strip() a_ : Union[str, Any] = input("""Enter the second string: """).strip() print() print(F"""The minimum edit distance is: {solver.min_dist_top_down(Sa, Sa)}""") print(F"""The minimum edit distance is: {solver.min_dist_bottom_up(Sa, Sa)}""") print() print("""*************** End of Testing Edit Distance DP Algorithm ***************""")
6
'''simple docstring''' from ..utils import DummyObject, requires_backends class __UpperCamelCase ( metaclass=lowerCamelCase__ ): lowercase : str =['speech'] def __init__( self, *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" requires_backends(self, ['''speech'''] ) class __UpperCamelCase ( metaclass=lowerCamelCase__ ): lowercase : Any =['speech'] def __init__( self, *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" requires_backends(self, ['''speech'''] )
6
1
'''simple docstring''' def a_ ( __snake_case : List[str] ) -> Optional[int]: """simple docstring""" lowerCamelCase_ =[] lowerCamelCase_ =set({'''(''', '''[''', '''{'''} ) lowerCamelCase_ =set({''')''', ''']''', '''}'''} ) lowerCamelCase_ ={'''{''': '''}''', '''[''': ''']''', '''(''': ''')'''} for i in range(len(__snake_case ) ): if s[i] in open_brackets: stack.append(s[i] ) elif s[i] in closed_brackets and ( len(__snake_case ) == 0 or (len(__snake_case ) > 0 and open_to_closed[stack.pop()] != s[i]) ): return False return len(__snake_case ) == 0 def a_ ( ) -> str: """simple docstring""" lowerCamelCase_ =input('''Enter sequence of brackets: ''' ) if is_balanced(__snake_case ): print(__snake_case , '''is balanced''' ) else: print(__snake_case , '''is not balanced''' ) if __name__ == "__main__": main()
6
'''simple docstring''' import re import warnings from contextlib import contextmanager from ...processing_utils import ProcessorMixin class __UpperCamelCase ( lowerCamelCase__ ): lowercase : List[str] =['image_processor', 'tokenizer'] lowercase : Optional[int] ='AutoImageProcessor' lowercase : List[str] ='AutoTokenizer' def __init__( self, lowerCAmelCase=None, lowerCAmelCase=None, **lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =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, ) lowerCamelCase_ =kwargs.pop('''feature_extractor''' ) lowerCamelCase_ =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 ) lowerCamelCase_ =self.image_processor lowerCamelCase_ =False def __call__( self, *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" if self._in_target_context_manager: return self.current_processor(*lowerCAmelCase, **lowerCAmelCase ) lowerCamelCase_ =kwargs.pop('''images''', lowerCAmelCase ) lowerCamelCase_ =kwargs.pop('''text''', lowerCAmelCase ) if len(lowerCAmelCase ) > 0: lowerCamelCase_ =args[0] lowerCamelCase_ =args[1:] if images is None and text is None: raise ValueError('''You need to specify either an `images` or `text` input to process.''' ) if images is not None: lowerCamelCase_ =self.image_processor(lowerCAmelCase, *lowerCAmelCase, **lowerCAmelCase ) if text is not None: lowerCamelCase_ =self.tokenizer(lowerCAmelCase, **lowerCAmelCase ) if text is None: return inputs elif images is None: return encodings else: lowerCamelCase_ =encodings['''input_ids'''] return inputs def lowercase__ ( self, *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" return self.tokenizer.batch_decode(*lowerCAmelCase, **lowerCAmelCase ) def lowercase__ ( self, *lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" return self.tokenizer.decode(*lowerCAmelCase, **lowerCAmelCase ) @contextmanager def lowercase__ ( self ): """simple docstring""" warnings.warn( '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your ''' '''labels by using the argument `text` of the regular `__call__` method (either in the same call as ''' '''your images inputs, or in a separate call.''' ) lowerCamelCase_ =True lowerCamelCase_ =self.tokenizer yield lowerCamelCase_ =self.image_processor lowerCamelCase_ =False def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase=False, lowerCAmelCase=None ): """simple docstring""" if added_vocab is None: lowerCamelCase_ =self.tokenizer.get_added_vocab() lowerCamelCase_ ={} while tokens: lowerCamelCase_ =re.search(R'''<s_(.*?)>''', lowerCAmelCase, re.IGNORECASE ) if start_token is None: break lowerCamelCase_ =start_token.group(1 ) lowerCamelCase_ =re.search(Rf'''</s_{key}>''', lowerCAmelCase, re.IGNORECASE ) lowerCamelCase_ =start_token.group() if end_token is None: lowerCamelCase_ =tokens.replace(lowerCAmelCase, '''''' ) else: lowerCamelCase_ =end_token.group() lowerCamelCase_ =re.escape(lowerCAmelCase ) lowerCamelCase_ =re.escape(lowerCAmelCase ) lowerCamelCase_ =re.search(f'''{start_token_escaped}(.*?){end_token_escaped}''', lowerCAmelCase, re.IGNORECASE ) if content is not None: lowerCamelCase_ =content.group(1 ).strip() if r"<s_" in content and r"</s_" in content: # non-leaf node lowerCamelCase_ =self.tokenajson(lowerCAmelCase, is_inner_value=lowerCAmelCase, added_vocab=lowerCAmelCase ) if value: if len(lowerCAmelCase ) == 1: lowerCamelCase_ =value[0] lowerCamelCase_ =value else: # leaf nodes lowerCamelCase_ =[] for leaf in content.split(R'''<sep/>''' ): lowerCamelCase_ =leaf.strip() if leaf in added_vocab and leaf[0] == "<" and leaf[-2:] == "/>": lowerCamelCase_ =leaf[1:-2] # for categorical special tokens output[key].append(lowerCAmelCase ) if len(output[key] ) == 1: lowerCamelCase_ =output[key][0] lowerCamelCase_ =tokens[tokens.find(lowerCAmelCase ) + len(lowerCAmelCase ) :].strip() if tokens[:6] == r"<sep/>": # non-leaf nodes return [output] + self.tokenajson(tokens[6:], is_inner_value=lowerCAmelCase, added_vocab=lowerCAmelCase ) if len(lowerCAmelCase ): return [output] if is_inner_value else output else: return [] if is_inner_value else {"text_sequence": tokens} @property def lowercase__ ( self ): """simple docstring""" warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''', lowerCAmelCase, ) return self.image_processor_class @property def lowercase__ ( self ): """simple docstring""" warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''', lowerCAmelCase, ) return self.image_processor
6
1
'''simple docstring''' from __future__ import annotations import numpy as np def a_ ( __snake_case : np.ndarray ) -> tuple[np.ndarray, np.ndarray]: """simple docstring""" lowerCamelCase_, lowerCamelCase_ =np.shape(__snake_case ) if rows != columns: lowerCamelCase_ =( '''\'table\' has to be of square shaped array but got a ''' F'''{rows}x{columns} array:\n{table}''' ) raise ValueError(__snake_case ) lowerCamelCase_ =np.zeros((rows, columns) ) lowerCamelCase_ =np.zeros((rows, columns) ) for i in range(__snake_case ): for j in range(__snake_case ): lowerCamelCase_ =sum(lower[i][k] * upper[k][j] for k in range(__snake_case ) ) if upper[j][j] == 0: raise ArithmeticError('''No LU decomposition exists''' ) lowerCamelCase_ =(table[i][j] - total) / upper[j][j] lowerCamelCase_ =1 for j in range(__snake_case , __snake_case ): lowerCamelCase_ =sum(lower[i][k] * upper[k][j] for k in range(__snake_case ) ) lowerCamelCase_ =table[i][j] - total return lower, upper if __name__ == "__main__": import doctest doctest.testmod()
6
'''simple docstring''' import gc import random import unittest import numpy as np import torch from transformers import CLIPImageProcessor, CLIPVisionConfig, CLIPVisionModel from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEImgaImgPipeline from diffusers.pipelines.shap_e import ShapERenderer from diffusers.utils import floats_tensor, load_image, load_numpy, slow from diffusers.utils.testing_utils import require_torch_gpu, torch_device from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference class __UpperCamelCase ( lowerCamelCase__ , unittest.TestCase ): lowercase : Union[str, Any] =ShapEImgaImgPipeline lowercase : Dict =['image'] lowercase : str =['image'] lowercase : int =[ 'num_images_per_prompt', 'num_inference_steps', 'generator', 'latents', 'guidance_scale', 'frame_size', 'output_type', 'return_dict', ] lowercase : int =False @property def lowercase__ ( self ): """simple docstring""" return 32 @property def lowercase__ ( self ): """simple docstring""" return 32 @property def lowercase__ ( self ): """simple docstring""" return self.time_input_dim * 4 @property def lowercase__ ( self ): """simple docstring""" return 8 @property def lowercase__ ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCamelCase_ =CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size, image_size=64, projection_dim=self.text_embedder_hidden_size, intermediate_size=37, num_attention_heads=4, num_channels=3, num_hidden_layers=5, patch_size=1, ) lowerCamelCase_ =CLIPVisionModel(lowerCAmelCase ) return model @property def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =CLIPImageProcessor( crop_size=224, do_center_crop=lowerCAmelCase, do_normalize=lowerCAmelCase, do_resize=lowerCAmelCase, image_mean=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3], image_std=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1], resample=3, size=224, ) return image_processor @property def lowercase__ ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCamelCase_ ={ '''num_attention_heads''': 2, '''attention_head_dim''': 16, '''embedding_dim''': self.time_input_dim, '''num_embeddings''': 32, '''embedding_proj_dim''': self.text_embedder_hidden_size, '''time_embed_dim''': self.time_embed_dim, '''num_layers''': 1, '''clip_embed_dim''': self.time_input_dim * 2, '''additional_embeddings''': 0, '''time_embed_act_fn''': '''gelu''', '''norm_in_type''': '''layer''', '''embedding_proj_norm_type''': '''layer''', '''encoder_hid_proj_type''': None, '''added_emb_type''': None, } lowerCamelCase_ =PriorTransformer(**lowerCAmelCase ) return model @property def lowercase__ ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCamelCase_ ={ '''param_shapes''': ( (self.renderer_dim, 93), (self.renderer_dim, 8), (self.renderer_dim, 8), (self.renderer_dim, 8), ), '''d_latent''': self.time_input_dim, '''d_hidden''': self.renderer_dim, '''n_output''': 12, '''background''': ( 0.1, 0.1, 0.1, ), } lowerCamelCase_ =ShapERenderer(**lowerCAmelCase ) return model def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.dummy_prior lowerCamelCase_ =self.dummy_image_encoder lowerCamelCase_ =self.dummy_image_processor lowerCamelCase_ =self.dummy_renderer lowerCamelCase_ =HeunDiscreteScheduler( beta_schedule='''exp''', num_train_timesteps=1_024, prediction_type='''sample''', use_karras_sigmas=lowerCAmelCase, clip_sample=lowerCAmelCase, clip_sample_range=1.0, ) lowerCamelCase_ ={ '''prior''': prior, '''image_encoder''': image_encoder, '''image_processor''': image_processor, '''renderer''': renderer, '''scheduler''': scheduler, } return components def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase=0 ): """simple docstring""" lowerCamelCase_ =floats_tensor((1, 3, 64, 64), rng=random.Random(lowerCAmelCase ) ).to(lowerCAmelCase ) if str(lowerCAmelCase ).startswith('''mps''' ): lowerCamelCase_ =torch.manual_seed(lowerCAmelCase ) else: lowerCamelCase_ =torch.Generator(device=lowerCAmelCase ).manual_seed(lowerCAmelCase ) lowerCamelCase_ ={ '''image''': input_image, '''generator''': generator, '''num_inference_steps''': 1, '''frame_size''': 32, '''output_type''': '''np''', } return inputs def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ ='''cpu''' lowerCamelCase_ =self.get_dummy_components() lowerCamelCase_ =self.pipeline_class(**lowerCAmelCase ) lowerCamelCase_ =pipe.to(lowerCAmelCase ) pipe.set_progress_bar_config(disable=lowerCAmelCase ) lowerCamelCase_ =pipe(**self.get_dummy_inputs(lowerCAmelCase ) ) lowerCamelCase_ =output.images[0] lowerCamelCase_ =image[0, -3:, -3:, -1] assert image.shape == (20, 32, 32, 3) lowerCamelCase_ =np.array( [ 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def lowercase__ ( self ): """simple docstring""" self._test_inference_batch_consistent(batch_sizes=[1, 2] ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =torch_device == '''cpu''' lowerCamelCase_ =True self._test_inference_batch_single_identical( batch_size=2, test_max_difference=lowerCAmelCase, relax_max_difference=lowerCAmelCase, ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.get_dummy_components() lowerCamelCase_ =self.pipeline_class(**lowerCAmelCase ) lowerCamelCase_ =pipe.to(lowerCAmelCase ) pipe.set_progress_bar_config(disable=lowerCAmelCase ) lowerCamelCase_ =1 lowerCamelCase_ =2 lowerCamelCase_ =self.get_dummy_inputs(lowerCAmelCase ) for key in inputs.keys(): if key in self.batch_params: lowerCamelCase_ =batch_size * [inputs[key]] lowerCamelCase_ =pipe(**lowerCAmelCase, num_images_per_prompt=lowerCAmelCase )[0] assert images.shape[0] == batch_size * num_images_per_prompt @slow @require_torch_gpu class __UpperCamelCase ( unittest.TestCase ): def lowercase__ ( self ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/shap_e/corgi.png''' ) lowerCamelCase_ =load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/shap_e/test_shap_e_img2img_out.npy''' ) lowerCamelCase_ =ShapEImgaImgPipeline.from_pretrained('''openai/shap-e-img2img''' ) lowerCamelCase_ =pipe.to(lowerCAmelCase ) pipe.set_progress_bar_config(disable=lowerCAmelCase ) lowerCamelCase_ =torch.Generator(device=lowerCAmelCase ).manual_seed(0 ) lowerCamelCase_ =pipe( lowerCAmelCase, generator=lowerCAmelCase, guidance_scale=3.0, num_inference_steps=64, frame_size=64, output_type='''np''', ).images[0] assert images.shape == (20, 64, 64, 3) assert_mean_pixel_difference(lowerCAmelCase, lowerCAmelCase )
6
1