code
stringlengths
87
55.2k
code_codestyle
int64
0
349
style_context
stringlengths
135
49.1k
style_context_codestyle
int64
0
349
label
int64
0
1
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import BertTokenizer, BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import AlignProcessor, EfficientNetImageProcessor @require_vision class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: int = tempfile.mkdtemp() SCREAMING_SNAKE_CASE_: Dict = [ "[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "want", "##want", "##ed", "wa", "un", "runn", "##ing", ",", "low", "lowest", ] SCREAMING_SNAKE_CASE_: str = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"]) with open(self.vocab_file , "w" , encoding="utf-8") as vocab_writer: vocab_writer.write("".join([x + "\n" for x in vocab_tokens])) SCREAMING_SNAKE_CASE_: Any = { "do_resize": True, "size": 20, "do_center_crop": True, "crop_size": 18, "do_normalize": True, "image_mean": [0.4814_5466, 0.457_8275, 0.4082_1073], "image_std": [0.2686_2954, 0.2613_0258, 0.2757_7711], } SCREAMING_SNAKE_CASE_: Optional[int] = os.path.join(self.tmpdirname , lowerCAmelCase__) with open(self.image_processor_file , "w" , encoding="utf-8") as fp: json.dump(lowerCAmelCase__ , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[Any] , **lowerCAmelCase__ : Optional[int]): return BertTokenizer.from_pretrained(self.tmpdirname , **lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[int] , **lowerCAmelCase__ : str): return BertTokenizerFast.from_pretrained(self.tmpdirname , **lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str] , **lowerCAmelCase__ : Tuple): return EfficientNetImageProcessor.from_pretrained(self.tmpdirname , **lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str]): shutil.rmtree(self.tmpdirname) def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: int = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta)] SCREAMING_SNAKE_CASE_: Optional[int] = [Image.fromarray(np.moveaxis(lowerCAmelCase__ , 0 , -1)) for x in image_inputs] return image_inputs def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: Tuple = self.get_tokenizer() SCREAMING_SNAKE_CASE_: List[Any] = self.get_rust_tokenizer() SCREAMING_SNAKE_CASE_: List[Any] = self.get_image_processor() SCREAMING_SNAKE_CASE_: Optional[Any] = AlignProcessor(tokenizer=lowerCAmelCase__ , image_processor=lowerCAmelCase__) processor_slow.save_pretrained(self.tmpdirname) SCREAMING_SNAKE_CASE_: Optional[int] = AlignProcessor.from_pretrained(self.tmpdirname , use_fast=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = AlignProcessor(tokenizer=lowerCAmelCase__ , image_processor=lowerCAmelCase__) processor_fast.save_pretrained(self.tmpdirname) SCREAMING_SNAKE_CASE_: Tuple = AlignProcessor.from_pretrained(self.tmpdirname) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab()) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab()) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab()) self.assertIsInstance(processor_slow.tokenizer , lowerCAmelCase__) self.assertIsInstance(processor_fast.tokenizer , lowerCAmelCase__) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string()) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string()) self.assertIsInstance(processor_slow.image_processor , lowerCAmelCase__) self.assertIsInstance(processor_fast.image_processor , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: List[Any] = AlignProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor()) processor.save_pretrained(self.tmpdirname) SCREAMING_SNAKE_CASE_: Dict = self.get_tokenizer(bos_token="(BOS)" , eos_token="(EOS)") SCREAMING_SNAKE_CASE_: List[str] = self.get_image_processor(do_normalize=lowerCAmelCase__ , padding_value=1.0) SCREAMING_SNAKE_CASE_: Dict = AlignProcessor.from_pretrained( self.tmpdirname , bos_token="(BOS)" , eos_token="(EOS)" , do_normalize=lowerCAmelCase__ , padding_value=1.0) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab()) self.assertIsInstance(processor.tokenizer , lowerCAmelCase__) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string()) self.assertIsInstance(processor.image_processor , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: List[Any] = self.get_image_processor() SCREAMING_SNAKE_CASE_: Any = self.get_tokenizer() SCREAMING_SNAKE_CASE_: List[str] = AlignProcessor(tokenizer=lowerCAmelCase__ , image_processor=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = self.prepare_image_inputs() SCREAMING_SNAKE_CASE_: List[Any] = image_processor(lowerCAmelCase__ , return_tensors="np") SCREAMING_SNAKE_CASE_: int = processor(images=lowerCAmelCase__ , return_tensors="np") for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1E-2) def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_: Optional[Any] = self.get_image_processor() SCREAMING_SNAKE_CASE_: List[str] = self.get_tokenizer() SCREAMING_SNAKE_CASE_: Any = AlignProcessor(tokenizer=lowerCAmelCase__ , image_processor=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = "lower newer" SCREAMING_SNAKE_CASE_: Tuple = processor(text=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = tokenizer(lowerCAmelCase__ , padding="max_length" , max_length=64) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key]) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: List[str] = self.get_image_processor() SCREAMING_SNAKE_CASE_: str = self.get_tokenizer() SCREAMING_SNAKE_CASE_: Dict = AlignProcessor(tokenizer=lowerCAmelCase__ , image_processor=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = "lower newer" SCREAMING_SNAKE_CASE_: Tuple = self.prepare_image_inputs() SCREAMING_SNAKE_CASE_: Optional[int] = processor(text=lowerCAmelCase__ , images=lowerCAmelCase__) self.assertListEqual(list(inputs.keys()) , ["input_ids", "token_type_ids", "attention_mask", "pixel_values"]) # test if it raises when no input is passed with pytest.raises(lowerCAmelCase__): processor() def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Union[str, Any] = self.get_image_processor() SCREAMING_SNAKE_CASE_: int = self.get_tokenizer() SCREAMING_SNAKE_CASE_: Optional[Any] = AlignProcessor(tokenizer=lowerCAmelCase__ , image_processor=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] SCREAMING_SNAKE_CASE_: Optional[Any] = processor.batch_decode(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = tokenizer.batch_decode(lowerCAmelCase__) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: Dict = self.get_image_processor() SCREAMING_SNAKE_CASE_: Tuple = self.get_tokenizer() SCREAMING_SNAKE_CASE_: Any = AlignProcessor(tokenizer=lowerCAmelCase__ , image_processor=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = "lower newer" SCREAMING_SNAKE_CASE_: str = self.prepare_image_inputs() SCREAMING_SNAKE_CASE_: int = processor(text=lowerCAmelCase__ , images=lowerCAmelCase__) self.assertListEqual(list(inputs.keys()) , processor.model_input_names)
13
import math import sys def A_ ( _UpperCAmelCase ): if number != int(_UpperCAmelCase ): raise ValueError("the value of input must be a natural number" ) if number < 0: raise ValueError("the value of input must not be a negative number" ) if number == 0: return 1 SCREAMING_SNAKE_CASE_: List[str] = [-1] * (number + 1) SCREAMING_SNAKE_CASE_: str = 0 for i in range(1 , number + 1 ): SCREAMING_SNAKE_CASE_: str = sys.maxsize SCREAMING_SNAKE_CASE_: List[Any] = int(math.sqrt(_UpperCAmelCase ) ) for j in range(1 , root + 1 ): SCREAMING_SNAKE_CASE_: List[str] = 1 + answers[i - (j**2)] SCREAMING_SNAKE_CASE_: Optional[Any] = min(_UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Dict = answer return answers[number] if __name__ == "__main__": import doctest doctest.testmod()
13
1
def A_ ( _UpperCAmelCase = 3 , _UpperCAmelCase = 7 , _UpperCAmelCase = 1_00_00_00 ): SCREAMING_SNAKE_CASE_: Union[str, Any] = 0 SCREAMING_SNAKE_CASE_: int = 1 for current_denominator in range(1 , limit + 1 ): SCREAMING_SNAKE_CASE_: int = current_denominator * numerator // denominator if current_denominator % denominator == 0: current_numerator -= 1 if current_numerator * max_denominator > current_denominator * max_numerator: SCREAMING_SNAKE_CASE_: Tuple = current_numerator SCREAMING_SNAKE_CASE_: Tuple = current_denominator return max_numerator if __name__ == "__main__": print(solution(numerator=3, denominator=7, limit=1000000))
13
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase : Optional[int] = {"""configuration_wavlm""": ["""WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """WavLMConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Any = [ """WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST""", """WavLMForAudioFrameClassification""", """WavLMForCTC""", """WavLMForSequenceClassification""", """WavLMForXVector""", """WavLMModel""", """WavLMPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_wavlm import WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP, WavLMConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_wavlm import ( WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST, WavLMForAudioFrameClassification, WavLMForCTC, WavLMForSequenceClassification, WavLMForXVector, WavLMModel, WavLMPreTrainedModel, ) else: import sys lowerCAmelCase : Union[str, Any] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
13
1
import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( """The `image_to_image.py` script is outdated. Please use directly `from diffusers import""" """ StableDiffusionImg2ImgPipeline` instead.""" )
13
import unittest from transformers import ( MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TextaTextGenerationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, require_tf, require_torch from transformers.utils import is_torch_available from .test_pipelines_common import ANY if is_torch_available(): import torch @is_pipeline_test class __lowercase ( unittest.TestCase ): """simple docstring""" _UpperCAmelCase : List[Any] = MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING _UpperCAmelCase : str = TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING def _SCREAMING_SNAKE_CASE ( self : Any , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Dict): SCREAMING_SNAKE_CASE_: Any = TextaTextGenerationPipeline(model=lowerCAmelCase__ , tokenizer=lowerCAmelCase__) return generator, ["Something to write", "Something else"] def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Any): SCREAMING_SNAKE_CASE_: List[Any] = generator("Something there") self.assertEqual(lowerCAmelCase__ , [{"generated_text": ANY(lowerCAmelCase__)}]) # These are encoder decoder, they don't just append to incoming string self.assertFalse(outputs[0]["generated_text"].startswith("Something there")) SCREAMING_SNAKE_CASE_: List[Any] = generator(["This is great !", "Something else"] , num_return_sequences=2 , do_sample=lowerCAmelCase__) self.assertEqual( lowerCAmelCase__ , [ [{"generated_text": ANY(lowerCAmelCase__)}, {"generated_text": ANY(lowerCAmelCase__)}], [{"generated_text": ANY(lowerCAmelCase__)}, {"generated_text": ANY(lowerCAmelCase__)}], ] , ) SCREAMING_SNAKE_CASE_: Dict = generator( ["This is great !", "Something else"] , num_return_sequences=2 , batch_size=2 , do_sample=lowerCAmelCase__) self.assertEqual( lowerCAmelCase__ , [ [{"generated_text": ANY(lowerCAmelCase__)}, {"generated_text": ANY(lowerCAmelCase__)}], [{"generated_text": ANY(lowerCAmelCase__)}, {"generated_text": ANY(lowerCAmelCase__)}], ] , ) with self.assertRaises(lowerCAmelCase__): generator(4) @require_torch def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Optional[int] = pipeline("text2text-generation" , model="patrickvonplaten/t5-tiny-random" , framework="pt") # do_sample=False necessary for reproducibility SCREAMING_SNAKE_CASE_: Union[str, Any] = generator("Something there" , do_sample=lowerCAmelCase__) self.assertEqual(lowerCAmelCase__ , [{"generated_text": ""}]) SCREAMING_SNAKE_CASE_: Union[str, Any] = 3 SCREAMING_SNAKE_CASE_: Any = generator( "Something there" , num_return_sequences=lowerCAmelCase__ , num_beams=lowerCAmelCase__ , ) SCREAMING_SNAKE_CASE_: Any = [ {"generated_text": "Beide Beide Beide Beide Beide Beide Beide Beide Beide"}, {"generated_text": "Beide Beide Beide Beide Beide Beide Beide Beide"}, {"generated_text": ""}, ] self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = generator("This is a test" , do_sample=lowerCAmelCase__ , num_return_sequences=2 , return_tensors=lowerCAmelCase__) self.assertEqual( lowerCAmelCase__ , [ {"generated_token_ids": ANY(torch.Tensor)}, {"generated_token_ids": ANY(torch.Tensor)}, ] , ) SCREAMING_SNAKE_CASE_: str = generator.model.config.eos_token_id SCREAMING_SNAKE_CASE_: Union[str, Any] = "<pad>" SCREAMING_SNAKE_CASE_: Tuple = generator( ["This is a test", "This is a second test"] , do_sample=lowerCAmelCase__ , num_return_sequences=2 , batch_size=2 , return_tensors=lowerCAmelCase__ , ) self.assertEqual( lowerCAmelCase__ , [ [ {"generated_token_ids": ANY(torch.Tensor)}, {"generated_token_ids": ANY(torch.Tensor)}, ], [ {"generated_token_ids": ANY(torch.Tensor)}, {"generated_token_ids": ANY(torch.Tensor)}, ], ] , ) @require_tf def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = pipeline("text2text-generation" , model="patrickvonplaten/t5-tiny-random" , framework="tf") # do_sample=False necessary for reproducibility SCREAMING_SNAKE_CASE_: List[Any] = generator("Something there" , do_sample=lowerCAmelCase__) self.assertEqual(lowerCAmelCase__ , [{"generated_text": ""}])
13
1
import itertools import string from collections.abc import Generator, Iterable def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: str = iter(_UpperCAmelCase ) while True: SCREAMING_SNAKE_CASE_: Dict = tuple(itertools.islice(_UpperCAmelCase , _UpperCAmelCase ) ) if not chunk: return yield chunk def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = "".join([c.upper() for c in dirty if c in string.ascii_letters] ) SCREAMING_SNAKE_CASE_: Any = "" if len(_UpperCAmelCase ) < 2: return dirty for i in range(len(_UpperCAmelCase ) - 1 ): clean += dirty[i] if dirty[i] == dirty[i + 1]: clean += "X" clean += dirty[-1] if len(_UpperCAmelCase ) & 1: clean += "X" return clean def A_ ( _UpperCAmelCase ): # I and J are used interchangeably to allow # us to use a 5x5 table (25 letters) SCREAMING_SNAKE_CASE_: Tuple = "ABCDEFGHIKLMNOPQRSTUVWXYZ" # we're using a list instead of a '2d' array because it makes the math # for setting up the table and doing the actual encoding/decoding simpler SCREAMING_SNAKE_CASE_: List[str] = [] # copy key chars into the table if they are in `alphabet` ignoring duplicates for char in key.upper(): if char not in table and char in alphabet: table.append(_UpperCAmelCase ) # fill the rest of the table in with the remaining alphabet chars for char in alphabet: if char not in table: table.append(_UpperCAmelCase ) return table def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: str = generate_table(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Optional[Any] = prepare_input(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Dict = "" # https://en.wikipedia.org/wiki/Playfair_cipher#Description for chara, chara in chunker(_UpperCAmelCase , 2 ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = divmod(table.index(_UpperCAmelCase ) , 5 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = divmod(table.index(_UpperCAmelCase ) , 5 ) if rowa == rowa: ciphertext += table[rowa * 5 + (cola + 1) % 5] ciphertext += table[rowa * 5 + (cola + 1) % 5] elif cola == cola: ciphertext += table[((rowa + 1) % 5) * 5 + cola] ciphertext += table[((rowa + 1) % 5) * 5 + cola] else: # rectangle ciphertext += table[rowa * 5 + cola] ciphertext += table[rowa * 5 + cola] return ciphertext def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = generate_table(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: int = "" # https://en.wikipedia.org/wiki/Playfair_cipher#Description for chara, chara in chunker(_UpperCAmelCase , 2 ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = divmod(table.index(_UpperCAmelCase ) , 5 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = divmod(table.index(_UpperCAmelCase ) , 5 ) if rowa == rowa: plaintext += table[rowa * 5 + (cola - 1) % 5] plaintext += table[rowa * 5 + (cola - 1) % 5] elif cola == cola: plaintext += table[((rowa - 1) % 5) * 5 + cola] plaintext += table[((rowa - 1) % 5) * 5 + cola] else: # rectangle plaintext += table[rowa * 5 + cola] plaintext += table[rowa * 5 + cola] return plaintext
13
def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: List[str] = [0] * len(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[Any] = [] SCREAMING_SNAKE_CASE_: str = [] SCREAMING_SNAKE_CASE_: List[str] = 0 for values in graph.values(): for i in values: indegree[i] += 1 for i in range(len(_UpperCAmelCase ) ): if indegree[i] == 0: queue.append(_UpperCAmelCase ) while queue: SCREAMING_SNAKE_CASE_: Optional[int] = queue.pop(0 ) cnt += 1 topo.append(_UpperCAmelCase ) for x in graph[vertex]: indegree[x] -= 1 if indegree[x] == 0: queue.append(_UpperCAmelCase ) if cnt != len(_UpperCAmelCase ): print("Cycle exists" ) else: print(_UpperCAmelCase ) # Adjacency List of Graph lowerCAmelCase : Any = {0: [1, 2], 1: [3], 2: [3], 3: [4, 5], 4: [], 5: []} topological_sort(graph)
13
1
import dataclasses import re import string from typing import Any, Dict, Iterator, List, Mapping, Optional, Sequence, Tuple import numpy as np from . import residue_constants lowerCAmelCase : Any = Mapping[str, np.ndarray] lowerCAmelCase : int = Mapping[str, Any] # Is a nested dict. lowerCAmelCase : Optional[Any] = 0.01 @dataclasses.dataclass(frozen=UpperCAmelCase_ ) class __lowercase : """simple docstring""" _UpperCAmelCase : np.ndarray # [num_res, num_atom_type, 3] # Amino-acid type for each residue represented as an integer between 0 and # 20, where 20 is 'X'. _UpperCAmelCase : np.ndarray # [num_res] # Binary float mask to indicate presence of a particular atom. 1.0 if an atom # is present and 0.0 if not. This should be used for loss masking. _UpperCAmelCase : np.ndarray # [num_res, num_atom_type] # Residue index as used in PDB. It is not necessarily continuous or 0-indexed. _UpperCAmelCase : np.ndarray # [num_res] # B-factors, or temperature factors, of each residue (in sq. angstroms units), # representing the displacement of the residue from its ground truth mean # value. _UpperCAmelCase : np.ndarray # [num_res, num_atom_type] # Chain indices for multi-chain predictions _UpperCAmelCase : Optional[np.ndarray] = None # Optional remark about the protein. Included as a comment in output PDB # files _UpperCAmelCase : Optional[str] = None # Templates used to generate this protein (prediction-only) _UpperCAmelCase : Optional[Sequence[str]] = None # Chain corresponding to each parent _UpperCAmelCase : Optional[Sequence[int]] = None def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Optional[Any] = R"(\[[A-Z]+\]\n)" SCREAMING_SNAKE_CASE_: List[str] = [tag.strip() for tag in re.split(_UpperCAmelCase , _UpperCAmelCase ) if len(_UpperCAmelCase ) > 0] SCREAMING_SNAKE_CASE_: Iterator[Tuple[str, List[str]]] = zip(tags[0::2] , [l.split("\n" ) for l in tags[1::2]] ) SCREAMING_SNAKE_CASE_: List[str] = ["N", "CA", "C"] SCREAMING_SNAKE_CASE_: Any = None SCREAMING_SNAKE_CASE_: Optional[Any] = None SCREAMING_SNAKE_CASE_: List[str] = None for g in groups: if "[PRIMARY]" == g[0]: SCREAMING_SNAKE_CASE_: Optional[int] = g[1][0].strip() for i in range(len(_UpperCAmelCase ) ): if seq[i] not in residue_constants.restypes: SCREAMING_SNAKE_CASE_: Union[str, Any] = "X" # FIXME: strings are immutable SCREAMING_SNAKE_CASE_: Tuple = np.array( [residue_constants.restype_order.get(_UpperCAmelCase , residue_constants.restype_num ) for res_symbol in seq] ) elif "[TERTIARY]" == g[0]: SCREAMING_SNAKE_CASE_: List[List[float]] = [] for axis in range(3 ): tertiary.append(list(map(_UpperCAmelCase , g[1][axis].split() ) ) ) SCREAMING_SNAKE_CASE_: List[Any] = np.array(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: int = np.zeros((len(tertiary[0] ) // 3, residue_constants.atom_type_num, 3) ).astype(np.floataa ) for i, atom in enumerate(_UpperCAmelCase ): SCREAMING_SNAKE_CASE_: str = np.transpose(tertiary_np[:, i::3] ) atom_positions *= PICO_TO_ANGSTROM elif "[MASK]" == g[0]: SCREAMING_SNAKE_CASE_: Optional[int] = np.array(list(map({"-": 0, "+": 1}.get , g[1][0].strip() ) ) ) SCREAMING_SNAKE_CASE_: Any = np.zeros( ( len(_UpperCAmelCase ), residue_constants.atom_type_num, ) ).astype(np.floataa ) for i, atom in enumerate(_UpperCAmelCase ): SCREAMING_SNAKE_CASE_: str = 1 atom_mask *= mask[..., None] assert aatype is not None return Protein( atom_positions=_UpperCAmelCase , atom_mask=_UpperCAmelCase , aatype=_UpperCAmelCase , residue_index=np.arange(len(_UpperCAmelCase ) ) , b_factors=_UpperCAmelCase , ) def A_ ( _UpperCAmelCase , _UpperCAmelCase = 0 ): SCREAMING_SNAKE_CASE_: List[str] = [] SCREAMING_SNAKE_CASE_: Any = prot.remark if remark is not None: pdb_headers.append(f"REMARK {remark}" ) SCREAMING_SNAKE_CASE_: Any = prot.parents SCREAMING_SNAKE_CASE_: Dict = prot.parents_chain_index if parents is not None and parents_chain_index is not None: SCREAMING_SNAKE_CASE_: Optional[int] = [p for i, p in zip(_UpperCAmelCase , _UpperCAmelCase ) if i == chain_id] if parents is None or len(_UpperCAmelCase ) == 0: SCREAMING_SNAKE_CASE_: Optional[int] = ["N/A"] pdb_headers.append(f"PARENT {' '.join(_UpperCAmelCase )}" ) return pdb_headers def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: List[str] = [] SCREAMING_SNAKE_CASE_: List[str] = pdb_str.split("\n" ) SCREAMING_SNAKE_CASE_: Optional[int] = prot.remark if remark is not None: out_pdb_lines.append(f"REMARK {remark}" ) SCREAMING_SNAKE_CASE_: List[List[str]] if prot.parents is not None and len(prot.parents ) > 0: SCREAMING_SNAKE_CASE_: Optional[int] = [] if prot.parents_chain_index is not None: SCREAMING_SNAKE_CASE_: Dict[str, List[str]] = {} for p, i in zip(prot.parents , prot.parents_chain_index ): parent_dict.setdefault(str(_UpperCAmelCase ) , [] ) parent_dict[str(_UpperCAmelCase )].append(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[str] = max([int(_UpperCAmelCase ) for chain_idx in parent_dict] ) for i in range(max_idx + 1 ): SCREAMING_SNAKE_CASE_: List[str] = parent_dict.get(str(_UpperCAmelCase ) , ["N/A"] ) parents_per_chain.append(_UpperCAmelCase ) else: parents_per_chain.append(list(prot.parents ) ) else: SCREAMING_SNAKE_CASE_: List[Any] = [["N/A"]] def make_parent_line(_UpperCAmelCase ) -> str: return f"PARENT {' '.join(_UpperCAmelCase )}" out_pdb_lines.append(make_parent_line(parents_per_chain[0] ) ) SCREAMING_SNAKE_CASE_: Union[str, Any] = 0 for i, l in enumerate(_UpperCAmelCase ): if "PARENT" not in l and "REMARK" not in l: out_pdb_lines.append(_UpperCAmelCase ) if "TER" in l and "END" not in lines[i + 1]: chain_counter += 1 if not chain_counter >= len(_UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = parents_per_chain[chain_counter] else: SCREAMING_SNAKE_CASE_: Union[str, Any] = ["N/A"] out_pdb_lines.append(make_parent_line(_UpperCAmelCase ) ) return "\n".join(_UpperCAmelCase ) def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: int = residue_constants.restypes + ["X"] def res_atoa(_UpperCAmelCase ) -> str: return residue_constants.restype_atoa.get(restypes[r] , "UNK" ) SCREAMING_SNAKE_CASE_: int = residue_constants.atom_types SCREAMING_SNAKE_CASE_: List[str] = [] SCREAMING_SNAKE_CASE_: Optional[int] = prot.atom_mask SCREAMING_SNAKE_CASE_: Optional[Any] = prot.aatype SCREAMING_SNAKE_CASE_: Optional[Any] = prot.atom_positions SCREAMING_SNAKE_CASE_: int = prot.residue_index.astype(np.intaa ) SCREAMING_SNAKE_CASE_: Dict = prot.b_factors SCREAMING_SNAKE_CASE_: str = prot.chain_index if np.any(aatype > residue_constants.restype_num ): raise ValueError("Invalid aatypes." ) SCREAMING_SNAKE_CASE_: Optional[int] = get_pdb_headers(_UpperCAmelCase ) if len(_UpperCAmelCase ) > 0: pdb_lines.extend(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[Any] = aatype.shape[0] SCREAMING_SNAKE_CASE_: str = 1 SCREAMING_SNAKE_CASE_: List[Any] = 0 SCREAMING_SNAKE_CASE_: List[Any] = string.ascii_uppercase SCREAMING_SNAKE_CASE_: int = None # Add all atom sites. for i in range(_UpperCAmelCase ): SCREAMING_SNAKE_CASE_: List[str] = res_atoa(aatype[i] ) for atom_name, pos, mask, b_factor in zip(_UpperCAmelCase , atom_positions[i] , atom_mask[i] , b_factors[i] ): if mask < 0.5: continue SCREAMING_SNAKE_CASE_: List[Any] = "ATOM" SCREAMING_SNAKE_CASE_: Optional[Any] = atom_name if len(_UpperCAmelCase ) == 4 else f" {atom_name}" SCREAMING_SNAKE_CASE_: List[str] = "" SCREAMING_SNAKE_CASE_: Optional[int] = "" SCREAMING_SNAKE_CASE_: List[str] = 1.0_0 SCREAMING_SNAKE_CASE_: int = atom_name[0] # Protein supports only C, N, O, S, this works. SCREAMING_SNAKE_CASE_: Optional[Any] = "" SCREAMING_SNAKE_CASE_: Dict = "A" if chain_index is not None: SCREAMING_SNAKE_CASE_: int = chain_tags[chain_index[i]] # PDB is a columnar format, every space matters here! SCREAMING_SNAKE_CASE_: Tuple = ( f"{record_type:<6}{atom_index:>5} {name:<4}{alt_loc:>1}" f"{res_name_a:>3} {chain_tag:>1}" f"{residue_index[i]:>4}{insertion_code:>1} " f"{pos[0]:>8.3f}{pos[1]:>8.3f}{pos[2]:>8.3f}" f"{occupancy:>6.2f}{b_factor:>6.2f} " f"{element:>2}{charge:>2}" ) pdb_lines.append(_UpperCAmelCase ) atom_index += 1 SCREAMING_SNAKE_CASE_: Optional[Any] = i == n - 1 if chain_index is not None: if i != n - 1 and chain_index[i + 1] != prev_chain_index: SCREAMING_SNAKE_CASE_: Dict = True SCREAMING_SNAKE_CASE_: List[str] = chain_index[i + 1] if should_terminate: # Close the chain. SCREAMING_SNAKE_CASE_: int = "TER" SCREAMING_SNAKE_CASE_: int = ( f"{chain_end:<6}{atom_index:>5} {res_atoa(aatype[i] ):>3} {chain_tag:>1}{residue_index[i]:>4}" ) pdb_lines.append(_UpperCAmelCase ) atom_index += 1 if i != n - 1: # "prev" is a misnomer here. This happens at the beginning of # each new chain. pdb_lines.extend(get_pdb_headers(_UpperCAmelCase , _UpperCAmelCase ) ) pdb_lines.append("END" ) pdb_lines.append("" ) return "\n".join(_UpperCAmelCase ) def A_ ( _UpperCAmelCase ): return residue_constants.STANDARD_ATOM_MASK[prot.aatype] def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = None , ): return Protein( aatype=features["aatype"] , atom_positions=result["final_atom_positions"] , atom_mask=result["final_atom_mask"] , residue_index=features["residue_index"] + 1 , b_factors=b_factors if b_factors is not None else np.zeros_like(result["final_atom_mask"] ) , chain_index=_UpperCAmelCase , remark=_UpperCAmelCase , parents=_UpperCAmelCase , parents_chain_index=_UpperCAmelCase , )
13
import argparse import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## lowerCAmelCase : Optional[Any] = 16 lowerCAmelCase : List[str] = 32 def A_ ( _UpperCAmelCase , _UpperCAmelCase = 16 ): SCREAMING_SNAKE_CASE_: Tuple = AutoTokenizer.from_pretrained("bert-base-cased" ) SCREAMING_SNAKE_CASE_: List[Any] = load_dataset("glue" , "mrpc" ) def tokenize_function(_UpperCAmelCase ): # max_length=None => use the model max length (it's actually the default) SCREAMING_SNAKE_CASE_: Any = tokenizer(examples["sentence1"] , examples["sentence2"] , truncation=_UpperCAmelCase , max_length=_UpperCAmelCase ) 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(): SCREAMING_SNAKE_CASE_: Tuple = datasets.map( _UpperCAmelCase , batched=_UpperCAmelCase , 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 SCREAMING_SNAKE_CASE_: Union[str, Any] = tokenized_datasets.rename_column("label" , "labels" ) def collate_fn(_UpperCAmelCase ): # On TPU it's best to pad everything to the same length or training will be very slow. SCREAMING_SNAKE_CASE_: List[str] = 1_28 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": SCREAMING_SNAKE_CASE_: List[Any] = 16 elif accelerator.mixed_precision != "no": SCREAMING_SNAKE_CASE_: Optional[Any] = 8 else: SCREAMING_SNAKE_CASE_: List[str] = None return tokenizer.pad( _UpperCAmelCase , padding="longest" , max_length=_UpperCAmelCase , pad_to_multiple_of=_UpperCAmelCase , return_tensors="pt" , ) # Instantiate dataloaders. SCREAMING_SNAKE_CASE_: Dict = DataLoader( tokenized_datasets["train"] , shuffle=_UpperCAmelCase , collate_fn=_UpperCAmelCase , batch_size=_UpperCAmelCase , drop_last=_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Union[str, Any] = DataLoader( tokenized_datasets["validation"] , shuffle=_UpperCAmelCase , collate_fn=_UpperCAmelCase , batch_size=_UpperCAmelCase , drop_last=(accelerator.mixed_precision == "fp8") , ) return train_dataloader, eval_dataloader def A_ ( _UpperCAmelCase , _UpperCAmelCase ): # Initialize accelerator SCREAMING_SNAKE_CASE_: str = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs SCREAMING_SNAKE_CASE_: int = config["lr"] SCREAMING_SNAKE_CASE_: Any = int(config["num_epochs"] ) SCREAMING_SNAKE_CASE_: Optional[int] = int(config["seed"] ) SCREAMING_SNAKE_CASE_: List[Any] = int(config["batch_size"] ) SCREAMING_SNAKE_CASE_: List[str] = evaluate.load("glue" , "mrpc" ) # If the batch size is too big we use gradient accumulation SCREAMING_SNAKE_CASE_: Optional[int] = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: SCREAMING_SNAKE_CASE_: Tuple = batch_size // MAX_GPU_BATCH_SIZE SCREAMING_SNAKE_CASE_: Dict = MAX_GPU_BATCH_SIZE set_seed(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = get_dataloaders(_UpperCAmelCase , _UpperCAmelCase ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) SCREAMING_SNAKE_CASE_: List[Any] = AutoModelForSequenceClassification.from_pretrained("bert-base-cased" , return_dict=_UpperCAmelCase ) # 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). SCREAMING_SNAKE_CASE_: Tuple = model.to(accelerator.device ) # Instantiate optimizer SCREAMING_SNAKE_CASE_: Optional[int] = AdamW(params=model.parameters() , lr=_UpperCAmelCase ) # Instantiate scheduler SCREAMING_SNAKE_CASE_: Optional[int] = get_linear_schedule_with_warmup( optimizer=_UpperCAmelCase , num_warmup_steps=1_00 , num_training_steps=(len(_UpperCAmelCase ) * num_epochs) // gradient_accumulation_steps , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = accelerator.prepare( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) # Now we train the model for epoch in range(_UpperCAmelCase ): model.train() for step, batch in enumerate(_UpperCAmelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) SCREAMING_SNAKE_CASE_: Tuple = model(**_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Optional[int] = outputs.loss SCREAMING_SNAKE_CASE_: Tuple = loss / gradient_accumulation_steps accelerator.backward(_UpperCAmelCase ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(_UpperCAmelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): SCREAMING_SNAKE_CASE_: Optional[int] = model(**_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: int = outputs.logits.argmax(dim=-1 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = accelerator.gather_for_metrics((predictions, batch["labels"]) ) metric.add_batch( predictions=_UpperCAmelCase , references=_UpperCAmelCase , ) SCREAMING_SNAKE_CASE_: List[Any] = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f"epoch {epoch}:" , _UpperCAmelCase ) def A_ ( ): SCREAMING_SNAKE_CASE_: Any = argparse.ArgumentParser(description="Simple example of training script." ) parser.add_argument( "--mixed_precision" , type=_UpperCAmelCase , default=_UpperCAmelCase , 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." ) SCREAMING_SNAKE_CASE_: Optional[Any] = parser.parse_args() SCREAMING_SNAKE_CASE_: Optional[int] = {"lr": 2e-5, "num_epochs": 3, "seed": 42, "batch_size": 16} training_function(_UpperCAmelCase , _UpperCAmelCase ) if __name__ == "__main__": main()
13
1
def A_ ( _UpperCAmelCase = 2_00_00_00 ): SCREAMING_SNAKE_CASE_: Tuple = [0 for i in range(n + 1 )] SCREAMING_SNAKE_CASE_: Any = 1 SCREAMING_SNAKE_CASE_: List[str] = 1 for i in range(2 , int(n**0.5 ) + 1 ): if primality_list[i] == 0: for j in range(i * i , n + 1 , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Tuple = 1 SCREAMING_SNAKE_CASE_: Optional[Any] = 0 for i in range(_UpperCAmelCase ): if primality_list[i] == 0: sum_of_primes += i return sum_of_primes if __name__ == "__main__": print(f'''{solution() = }''')
13
from collections.abc import Callable class __lowercase : """simple docstring""" def __init__( self : Tuple , lowerCAmelCase__ : Callable | None = None): # Stores actual heap items. SCREAMING_SNAKE_CASE_: list = [] # Stores indexes of each item for supporting updates and deletion. SCREAMING_SNAKE_CASE_: dict = {} # Stores current size of heap. SCREAMING_SNAKE_CASE_: Optional[Any] = 0 # Stores function used to evaluate the score of an item on which basis ordering # will be done. SCREAMING_SNAKE_CASE_: Any = key or (lambda lowerCAmelCase__: x) def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : int): return int((i - 1) / 2) if i > 0 else None def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Union[str, Any] = int(2 * i + 1) return left if 0 < left < self.size else None def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Union[str, Any] = int(2 * i + 2) return right if 0 < right < self.size else None def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : int , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = ( self.pos_map[self.arr[j][0]], self.pos_map[self.arr[i][0]], ) # Then swap the items in the list. SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = self.arr[j], self.arr[i] def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : int , lowerCAmelCase__ : int): return self.arr[i][1] < self.arr[j][1] def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Any = self._left(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = self._right(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = i if left is not None and not self._cmp(lowerCAmelCase__ , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Optional[int] = left if right is not None and not self._cmp(lowerCAmelCase__ , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Tuple = right return valid_parent def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: List[Any] = self._parent(lowerCAmelCase__) while parent is not None and not self._cmp(lowerCAmelCase__ , lowerCAmelCase__): self._swap(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = parent, self._parent(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Optional[int] = self._get_valid_parent(lowerCAmelCase__) while valid_parent != index: self._swap(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = valid_parent, self._get_valid_parent(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : int , lowerCAmelCase__ : int): if item not in self.pos_map: return SCREAMING_SNAKE_CASE_: Any = self.pos_map[item] SCREAMING_SNAKE_CASE_: int = [item, self.key(lowerCAmelCase__)] # Make sure heap is right in both up and down direction. # Ideally only one of them will make any change. self._heapify_up(lowerCAmelCase__) self._heapify_down(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : int): if item not in self.pos_map: return SCREAMING_SNAKE_CASE_: Optional[Any] = self.pos_map[item] del self.pos_map[item] SCREAMING_SNAKE_CASE_: List[str] = self.arr[self.size - 1] SCREAMING_SNAKE_CASE_: Tuple = index self.size -= 1 # Make sure heap is right in both up and down direction. Ideally only one # of them will make any change- so no performance loss in calling both. if self.size > index: self._heapify_up(lowerCAmelCase__) self._heapify_down(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Optional[int] = len(self.arr) if arr_len == self.size: self.arr.append([item, self.key(lowerCAmelCase__)]) else: SCREAMING_SNAKE_CASE_: str = [item, self.key(lowerCAmelCase__)] SCREAMING_SNAKE_CASE_: List[Any] = self.size self.size += 1 self._heapify_up(self.size - 1) def _SCREAMING_SNAKE_CASE ( self : List[Any]): return self.arr[0] if self.size else None def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Dict = self.get_top() if top_item_tuple: self.delete_item(top_item_tuple[0]) return top_item_tuple def A_ ( ): pass if __name__ == "__main__": import doctest doctest.testmod()
13
1
import os import re import warnings from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_ta import TaTokenizer else: lowerCAmelCase : List[Any] = None lowerCAmelCase : str = logging.get_logger(__name__) lowerCAmelCase : Union[str, Any] = {"""vocab_file""": """spiece.model""", """tokenizer_file""": """tokenizer.json"""} lowerCAmelCase : Optional[Any] = { """vocab_file""": { """t5-small""": """https://huggingface.co/t5-small/resolve/main/spiece.model""", """t5-base""": """https://huggingface.co/t5-base/resolve/main/spiece.model""", """t5-large""": """https://huggingface.co/t5-large/resolve/main/spiece.model""", """t5-3b""": """https://huggingface.co/t5-3b/resolve/main/spiece.model""", """t5-11b""": """https://huggingface.co/t5-11b/resolve/main/spiece.model""", }, """tokenizer_file""": { """t5-small""": """https://huggingface.co/t5-small/resolve/main/tokenizer.json""", """t5-base""": """https://huggingface.co/t5-base/resolve/main/tokenizer.json""", """t5-large""": """https://huggingface.co/t5-large/resolve/main/tokenizer.json""", """t5-3b""": """https://huggingface.co/t5-3b/resolve/main/tokenizer.json""", """t5-11b""": """https://huggingface.co/t5-11b/resolve/main/tokenizer.json""", }, } # TODO(PVP) - this should be removed in Transformers v5 lowerCAmelCase : str = { """t5-small""": 512, """t5-base""": 512, """t5-large""": 512, """t5-3b""": 512, """t5-11b""": 512, } class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : Any = VOCAB_FILES_NAMES _UpperCAmelCase : Dict = PRETRAINED_VOCAB_FILES_MAP _UpperCAmelCase : int = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _UpperCAmelCase : str = ['''input_ids''', '''attention_mask'''] _UpperCAmelCase : Optional[int] = TaTokenizer _UpperCAmelCase : List[int] = [] def __init__( self : Any , lowerCAmelCase__ : Optional[int]=None , lowerCAmelCase__ : Dict=None , lowerCAmelCase__ : int="</s>" , lowerCAmelCase__ : List[Any]="<unk>" , lowerCAmelCase__ : str="<pad>" , lowerCAmelCase__ : List[Any]=100 , lowerCAmelCase__ : List[Any]=None , **lowerCAmelCase__ : Tuple , ): # Add extra_ids to the special token list if extra_ids > 0 and additional_special_tokens is None: SCREAMING_SNAKE_CASE_: Optional[int] = [F"<extra_id_{i}>" for i in range(lowerCAmelCase__)] elif extra_ids > 0 and additional_special_tokens is not None: # Check that we have the right number of extra special tokens SCREAMING_SNAKE_CASE_: List[str] = len(set(filter(lambda lowerCAmelCase__: bool("extra_id_" in str(lowerCAmelCase__)) , lowerCAmelCase__))) if extra_tokens != extra_ids: raise ValueError( F"Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are" " provided to T5Tokenizer. In this case the additional_special_tokens must include the extra_ids" " tokens") super().__init__( lowerCAmelCase__ , tokenizer_file=lowerCAmelCase__ , eos_token=lowerCAmelCase__ , unk_token=lowerCAmelCase__ , pad_token=lowerCAmelCase__ , extra_ids=lowerCAmelCase__ , additional_special_tokens=lowerCAmelCase__ , **lowerCAmelCase__ , ) SCREAMING_SNAKE_CASE_: Dict = vocab_file SCREAMING_SNAKE_CASE_: Dict = False if not self.vocab_file else True SCREAMING_SNAKE_CASE_: List[str] = extra_ids @staticmethod def _SCREAMING_SNAKE_CASE ( lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Any): if pretrained_model_name_or_path in TaTokenizerFast.max_model_input_sizes: SCREAMING_SNAKE_CASE_: List[str] = TaTokenizerFast.max_model_input_sizes[pretrained_model_name_or_path] if init_max_model_length is not None and init_max_model_length != max_model_length: return init_max_model_length elif init_max_model_length is None: warnings.warn( "This tokenizer was incorrectly instantiated with a model max length of" F" {deprecated_max_model_length} which will be corrected in Transformers v5.\nFor now, this" " behavior is kept to avoid breaking backwards compatibility when padding/encoding with" " `truncation is True`.\n- Be aware that you SHOULD NOT rely on" F" {pretrained_model_name_or_path} automatically truncating your input to" F" {deprecated_max_model_length} when padding/encoding.\n- If you want to encode/pad to sequences" F" longer than {deprecated_max_model_length} you can either instantiate this tokenizer with" " `model_max_length` or pass `max_length` when encoding/padding.\n- To avoid this warning, please" " instantiate this tokenizer with `model_max_length` set to your preferred value." , lowerCAmelCase__ , ) return max_model_length def _SCREAMING_SNAKE_CASE ( self : Any , lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[str] = None): if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " "tokenizer.") if not os.path.isdir(lowerCAmelCase__): logger.error(F"Vocabulary path ({save_directory}) should be a directory") return SCREAMING_SNAKE_CASE_: Tuple = os.path.join( lowerCAmelCase__ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]) if os.path.abspath(self.vocab_file) != os.path.abspath(lowerCAmelCase__): copyfile(self.vocab_file , lowerCAmelCase__) logger.info(F"Copy vocab file to {out_vocab_file}") return (out_vocab_file,) def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None): SCREAMING_SNAKE_CASE_: Optional[int] = token_ids_a + [self.eos_token_id] if token_ids_a is None: return self.prefix_tokens + token_ids_a else: SCREAMING_SNAKE_CASE_: Optional[int] = token_ids_a + [self.eos_token_id] return self.prefix_tokens + token_ids_a + token_ids_a def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None): SCREAMING_SNAKE_CASE_: List[Any] = [self.eos_token_id] if token_ids_a is None: return len(token_ids_a + eos) * [0] return len(token_ids_a + eos + token_ids_a + eos) * [0] def _SCREAMING_SNAKE_CASE ( self : List[str]): return list( set(filter(lambda lowerCAmelCase__: bool(re.search(R"<extra_id_\d+>" , lowerCAmelCase__)) is not None , self.additional_special_tokens))) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): return [self.convert_tokens_to_ids(lowerCAmelCase__) for token in self.get_sentinel_tokens()]
13
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) if is_sentencepiece_available(): from ..ta.tokenization_ta import TaTokenizer else: from ...utils.dummy_sentencepiece_objects import TaTokenizer lowerCAmelCase : Any = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast lowerCAmelCase : Dict = TaTokenizerFast lowerCAmelCase : Optional[int] = {"""configuration_mt5""": ["""MT5Config""", """MT5OnnxConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : int = [ """MT5EncoderModel""", """MT5ForConditionalGeneration""", """MT5ForQuestionAnswering""", """MT5Model""", """MT5PreTrainedModel""", """MT5Stack""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Tuple = ["""TFMT5EncoderModel""", """TFMT5ForConditionalGeneration""", """TFMT5Model"""] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : List[str] = ["""FlaxMT5EncoderModel""", """FlaxMT5ForConditionalGeneration""", """FlaxMT5Model"""] if TYPE_CHECKING: from .configuration_mta import MTaConfig, MTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mta import ( MTaEncoderModel, MTaForConditionalGeneration, MTaForQuestionAnswering, MTaModel, MTaPreTrainedModel, MTaStack, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel else: import sys lowerCAmelCase : Optional[Any] = _LazyModule( __name__, globals()["""__file__"""], _import_structure, extra_objects={"""MT5Tokenizer""": MTaTokenizer, """MT5TokenizerFast""": MTaTokenizerFast}, module_spec=__spec__, )
13
1
import collections import inspect import unittest from typing import Dict, List, Tuple from transformers import MaskFormerSwinConfig from transformers.testing_utils import require_torch, require_torch_multi_gpu, torch_device from transformers.utils import is_torch_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import MaskFormerSwinBackbone from transformers.models.maskformer import MaskFormerSwinModel class __lowercase : """simple docstring""" def __init__( self : Optional[Any] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : List[Any]=13 , lowerCAmelCase__ : List[str]=32 , lowerCAmelCase__ : List[str]=2 , lowerCAmelCase__ : Optional[int]=3 , lowerCAmelCase__ : List[Any]=16 , lowerCAmelCase__ : int=[1, 2, 1] , lowerCAmelCase__ : Optional[Any]=[2, 2, 4] , lowerCAmelCase__ : List[Any]=2 , lowerCAmelCase__ : str=2.0 , lowerCAmelCase__ : List[Any]=True , lowerCAmelCase__ : Tuple=0.0 , lowerCAmelCase__ : Tuple=0.0 , lowerCAmelCase__ : Dict=0.1 , lowerCAmelCase__ : Union[str, Any]="gelu" , lowerCAmelCase__ : Union[str, Any]=False , lowerCAmelCase__ : Tuple=True , lowerCAmelCase__ : List[str]=0.02 , lowerCAmelCase__ : List[str]=1E-5 , lowerCAmelCase__ : int=True , lowerCAmelCase__ : List[Any]=None , lowerCAmelCase__ : str=True , lowerCAmelCase__ : Tuple=10 , lowerCAmelCase__ : Any=8 , lowerCAmelCase__ : List[str]=["stage1", "stage2", "stage3"] , lowerCAmelCase__ : Optional[int]=[1, 2, 3] , ): SCREAMING_SNAKE_CASE_: Optional[int] = parent SCREAMING_SNAKE_CASE_: Optional[int] = batch_size SCREAMING_SNAKE_CASE_: List[str] = image_size SCREAMING_SNAKE_CASE_: int = patch_size SCREAMING_SNAKE_CASE_: Optional[int] = num_channels SCREAMING_SNAKE_CASE_: Optional[Any] = embed_dim SCREAMING_SNAKE_CASE_: List[str] = depths SCREAMING_SNAKE_CASE_: Any = num_heads SCREAMING_SNAKE_CASE_: Any = window_size SCREAMING_SNAKE_CASE_: str = mlp_ratio SCREAMING_SNAKE_CASE_: Optional[Any] = qkv_bias SCREAMING_SNAKE_CASE_: Any = hidden_dropout_prob SCREAMING_SNAKE_CASE_: Any = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_: Optional[int] = drop_path_rate SCREAMING_SNAKE_CASE_: Union[str, Any] = hidden_act SCREAMING_SNAKE_CASE_: int = use_absolute_embeddings SCREAMING_SNAKE_CASE_: Tuple = patch_norm SCREAMING_SNAKE_CASE_: Union[str, Any] = layer_norm_eps SCREAMING_SNAKE_CASE_: Union[str, Any] = initializer_range SCREAMING_SNAKE_CASE_: Optional[Any] = is_training SCREAMING_SNAKE_CASE_: List[str] = scope SCREAMING_SNAKE_CASE_: List[Any] = use_labels SCREAMING_SNAKE_CASE_: Any = type_sequence_label_size SCREAMING_SNAKE_CASE_: int = encoder_stride SCREAMING_SNAKE_CASE_: List[str] = out_features SCREAMING_SNAKE_CASE_: Optional[Any] = out_indices def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Union[str, Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]) SCREAMING_SNAKE_CASE_: List[str] = None if self.use_labels: SCREAMING_SNAKE_CASE_: Tuple = ids_tensor([self.batch_size] , self.type_sequence_label_size) SCREAMING_SNAKE_CASE_: Optional[int] = self.get_config() return config, pixel_values, labels def _SCREAMING_SNAKE_CASE ( self : List[Any]): return MaskFormerSwinConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , embed_dim=self.embed_dim , depths=self.depths , num_heads=self.num_heads , window_size=self.window_size , mlp_ratio=self.mlp_ratio , qkv_bias=self.qkv_bias , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , drop_path_rate=self.drop_path_rate , hidden_act=self.hidden_act , use_absolute_embeddings=self.use_absolute_embeddings , path_norm=self.patch_norm , layer_norm_eps=self.layer_norm_eps , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , out_features=self.out_features , out_indices=self.out_indices , ) def _SCREAMING_SNAKE_CASE ( self : Any , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : str): SCREAMING_SNAKE_CASE_: Optional[int] = MaskFormerSwinModel(config=lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: Tuple = model(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths) - 1)) SCREAMING_SNAKE_CASE_: Dict = int(config.embed_dim * 2 ** (len(config.depths) - 1)) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, expected_seq_len, expected_dim)) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : str): SCREAMING_SNAKE_CASE_: List[Any] = MaskFormerSwinBackbone(config=lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: List[Any] = model(lowerCAmelCase__) # verify feature maps self.parent.assertEqual(len(result.feature_maps) , len(config.out_features)) self.parent.assertListEqual(list(result.feature_maps[0].shape) , [13, 16, 16, 16]) # verify channels self.parent.assertEqual(len(model.channels) , len(config.out_features)) self.parent.assertListEqual(model.channels , [16, 32, 64]) # verify ValueError with self.parent.assertRaises(lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Optional[Any] = ["stem"] SCREAMING_SNAKE_CASE_: Dict = MaskFormerSwinBackbone(config=lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Tuple = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = config_and_inputs SCREAMING_SNAKE_CASE_: int = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class __lowercase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : Optional[int] = ( ( MaskFormerSwinModel, MaskFormerSwinBackbone, ) if is_torch_available() else () ) _UpperCAmelCase : List[Any] = {'''feature-extraction''': MaskFormerSwinModel} if is_torch_available() else {} _UpperCAmelCase : str = False _UpperCAmelCase : Optional[Any] = False _UpperCAmelCase : Union[str, Any] = False _UpperCAmelCase : Optional[Any] = False _UpperCAmelCase : Optional[int] = False def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: Dict = MaskFormerSwinModelTester(self) SCREAMING_SNAKE_CASE_: Tuple = ConfigTester(self , config_class=lowerCAmelCase__ , embed_dim=37) @require_torch_multi_gpu @unittest.skip( reason=( "`MaskFormerSwinModel` outputs `hidden_states_spatial_dimensions` which doesn't work well with" " `nn.DataParallel`" )) def _SCREAMING_SNAKE_CASE ( self : List[str]): pass def _SCREAMING_SNAKE_CASE ( self : Any): self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): return def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*lowerCAmelCase__) @unittest.skip("Swin does not use inputs_embeds") def _SCREAMING_SNAKE_CASE ( self : Optional[int]): pass @unittest.skip("Swin does not support feedforward chunking") def _SCREAMING_SNAKE_CASE ( self : List[str]): pass def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_: List[Any] = model_class(lowerCAmelCase__) self.assertIsInstance(model.get_input_embeddings() , (nn.Module)) SCREAMING_SNAKE_CASE_: Union[str, Any] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCAmelCase__ , nn.Linear)) def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_: Tuple = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = inspect.signature(model.forward) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE_: List[Any] = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE_: Dict = ["pixel_values"] self.assertListEqual(arg_names[:1] , lowerCAmelCase__) @unittest.skip(reason="MaskFormerSwin is only used as backbone and doesn't support output_attentions") def _SCREAMING_SNAKE_CASE ( self : List[Any]): pass @unittest.skip(reason="MaskFormerSwin is only used as an internal backbone") def _SCREAMING_SNAKE_CASE ( self : Tuple): pass def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Optional[Any]): SCREAMING_SNAKE_CASE_: List[str] = model_class(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE_: List[Any] = model(**self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__)) SCREAMING_SNAKE_CASE_: List[Any] = outputs.hidden_states SCREAMING_SNAKE_CASE_: int = getattr( self.model_tester , "expected_num_hidden_layers" , len(self.model_tester.depths) + 1) self.assertEqual(len(lowerCAmelCase__) , lowerCAmelCase__) # Swin has a different seq_length SCREAMING_SNAKE_CASE_: Dict = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable) else (config.patch_size, config.patch_size) ) SCREAMING_SNAKE_CASE_: str = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.assertListEqual( list(hidden_states[0].shape[-2:]) , [num_patches, self.model_tester.embed_dim] , ) def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_: List[Any] = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable) else (self.model_tester.image_size, self.model_tester.image_size) ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_: Optional[Any] = True self.check_hidden_states_output(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE_: Optional[Any] = True self.check_hidden_states_output(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_: int = 3 SCREAMING_SNAKE_CASE_: Tuple = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable) else (self.model_tester.image_size, self.model_tester.image_size) ) SCREAMING_SNAKE_CASE_: Tuple = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable) else (config.patch_size, config.patch_size) ) SCREAMING_SNAKE_CASE_: Dict = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0]) SCREAMING_SNAKE_CASE_: Union[str, Any] = image_size[1] + patch_size[1] - (image_size[1] % patch_size[1]) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_: Optional[Any] = True self.check_hidden_states_output(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , (padded_height, padded_width)) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE_: Optional[Any] = True self.check_hidden_states_output(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , (padded_height, padded_width)) @unittest.skip(reason="MaskFormerSwin doesn't have pretrained checkpoints") def _SCREAMING_SNAKE_CASE ( self : Any): pass @unittest.skip(reason="This will be fixed once MaskFormerSwin is replaced by native Swin") def _SCREAMING_SNAKE_CASE ( self : str): pass @unittest.skip(reason="This will be fixed once MaskFormerSwin is replaced by native Swin") def _SCREAMING_SNAKE_CASE ( self : Any): pass def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() def set_nan_tensor_to_zero(lowerCAmelCase__ : List[str]): SCREAMING_SNAKE_CASE_: Optional[int] = 0 return t def check_equivalence(lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : int , lowerCAmelCase__ : Union[str, Any]={}): with torch.no_grad(): SCREAMING_SNAKE_CASE_: Optional[Any] = model(**lowerCAmelCase__ , return_dict=lowerCAmelCase__ , **lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = model(**lowerCAmelCase__ , return_dict=lowerCAmelCase__ , **lowerCAmelCase__).to_tuple() def recursive_check(lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Optional[int]): if isinstance(lowerCAmelCase__ , (List, Tuple)): for tuple_iterable_value, dict_iterable_value in zip(lowerCAmelCase__ , lowerCAmelCase__): recursive_check(lowerCAmelCase__ , lowerCAmelCase__) elif isinstance(lowerCAmelCase__ , lowerCAmelCase__): for tuple_iterable_value, dict_iterable_value in zip( tuple_object.values() , dict_object.values()): recursive_check(lowerCAmelCase__ , lowerCAmelCase__) elif tuple_object is None: return else: self.assertTrue( torch.allclose( set_nan_tensor_to_zero(lowerCAmelCase__) , set_nan_tensor_to_zero(lowerCAmelCase__) , atol=1E-5) , msg=( "Tuple and dict output are not equal. Difference:" F" {torch.max(torch.abs(tuple_object - dict_object))}. Tuple has `nan`:" F" {torch.isnan(lowerCAmelCase__).any()} and `inf`: {torch.isinf(lowerCAmelCase__)}. Dict has" F" `nan`: {torch.isnan(lowerCAmelCase__).any()} and `inf`: {torch.isinf(lowerCAmelCase__)}." ) , ) recursive_check(lowerCAmelCase__ , lowerCAmelCase__) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_: Optional[int] = model_class(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: Optional[int] = self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__) check_equivalence(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__ , return_labels=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__ , return_labels=lowerCAmelCase__) check_equivalence(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__) check_equivalence(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , {"output_hidden_states": True}) SCREAMING_SNAKE_CASE_: List[Any] = self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__ , return_labels=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__ , return_labels=lowerCAmelCase__) check_equivalence(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , {"output_hidden_states": True}) @require_torch class __lowercase ( unittest.TestCase , UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : Union[str, Any] = (MaskFormerSwinBackbone,) if is_torch_available() else () _UpperCAmelCase : str = MaskFormerSwinConfig def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_: List[Any] = MaskFormerSwinModelTester(self) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_: Any = inputs_dict["pixel_values"].shape[0] for backbone_class in self.all_model_classes: SCREAMING_SNAKE_CASE_: List[str] = backbone_class(lowerCAmelCase__) backbone.to(lowerCAmelCase__) backbone.eval() SCREAMING_SNAKE_CASE_: Any = backbone(**lowerCAmelCase__) # Test default outputs and verify feature maps self.assertIsInstance(outputs.feature_maps , lowerCAmelCase__) self.assertTrue(len(outputs.feature_maps) == len(backbone.channels)) for feature_map, n_channels in zip(outputs.feature_maps , backbone.channels): self.assertTrue(feature_map.shape[:2] , (batch_size, n_channels)) self.assertIsNone(outputs.hidden_states) self.assertIsNone(outputs.attentions) # Test output_hidden_states=True SCREAMING_SNAKE_CASE_: str = backbone(**lowerCAmelCase__ , output_hidden_states=lowerCAmelCase__) self.assertIsNotNone(outputs.hidden_states) self.assertTrue(len(outputs.hidden_states) , len(backbone.stage_names)) # We skip the stem layer for hidden_states, n_channels in zip(outputs.hidden_states[1:] , backbone.channels): for hidden_state in hidden_states: # Hidden states are in the format (batch_size, (height * width), n_channels) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = hidden_state.shape self.assertTrue((h_batch_size, h_n_channels) , (batch_size, n_channels)) # Test output_attentions=True if self.has_attentions: SCREAMING_SNAKE_CASE_: Dict = backbone(**lowerCAmelCase__ , output_attentions=lowerCAmelCase__) self.assertIsNotNone(outputs.attentions)
13
import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEPipeline from diffusers.pipelines.shap_e import ShapERenderer from diffusers.utils import 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 __lowercase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : List[str] = ShapEPipeline _UpperCAmelCase : Tuple = ['''prompt'''] _UpperCAmelCase : Dict = ['''prompt'''] _UpperCAmelCase : Any = [ '''num_images_per_prompt''', '''num_inference_steps''', '''generator''', '''latents''', '''guidance_scale''', '''frame_size''', '''output_type''', '''return_dict''', ] _UpperCAmelCase : Optional[int] = False @property def _SCREAMING_SNAKE_CASE ( self : List[str]): return 32 @property def _SCREAMING_SNAKE_CASE ( self : List[str]): return 32 @property def _SCREAMING_SNAKE_CASE ( self : int): return self.time_input_dim * 4 @property def _SCREAMING_SNAKE_CASE ( self : Optional[int]): return 8 @property def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: str = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip") return tokenizer @property def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Optional[int] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) return CLIPTextModelWithProjection(lowerCAmelCase__) @property def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Tuple = { "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", "encoder_hid_proj_type": None, "added_emb_type": None, } SCREAMING_SNAKE_CASE_: Any = PriorTransformer(**lowerCAmelCase__) return model @property def _SCREAMING_SNAKE_CASE ( self : Dict): torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Union[str, Any] = { "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, ), } SCREAMING_SNAKE_CASE_: Optional[int] = ShapERenderer(**lowerCAmelCase__) return model def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Dict = self.dummy_prior SCREAMING_SNAKE_CASE_: Optional[Any] = self.dummy_text_encoder SCREAMING_SNAKE_CASE_: Union[str, Any] = self.dummy_tokenizer SCREAMING_SNAKE_CASE_: List[str] = self.dummy_renderer SCREAMING_SNAKE_CASE_: Any = HeunDiscreteScheduler( beta_schedule="exp" , num_train_timesteps=1024 , prediction_type="sample" , use_karras_sigmas=lowerCAmelCase__ , clip_sample=lowerCAmelCase__ , clip_sample_range=1.0 , ) SCREAMING_SNAKE_CASE_: Optional[int] = { "prior": prior, "text_encoder": text_encoder, "tokenizer": tokenizer, "renderer": renderer, "scheduler": scheduler, } return components def _SCREAMING_SNAKE_CASE ( self : Any , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[Any]=0): if str(lowerCAmelCase__).startswith("mps"): SCREAMING_SNAKE_CASE_: Optional[Any] = torch.manual_seed(lowerCAmelCase__) else: SCREAMING_SNAKE_CASE_: Any = torch.Generator(device=lowerCAmelCase__).manual_seed(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = { "prompt": "horse", "generator": generator, "num_inference_steps": 1, "frame_size": 32, "output_type": "np", } return inputs def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: str = "cpu" SCREAMING_SNAKE_CASE_: Tuple = self.get_dummy_components() SCREAMING_SNAKE_CASE_: Dict = self.pipeline_class(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = pipe(**self.get_dummy_inputs(lowerCAmelCase__)) SCREAMING_SNAKE_CASE_: Optional[Any] = output.images[0] SCREAMING_SNAKE_CASE_: Any = image[0, -3:, -3:, -1] assert image.shape == (20, 32, 32, 3) SCREAMING_SNAKE_CASE_: Union[str, Any] = np.array( [ 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, ]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-2 def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches self._test_inference_batch_consistent(batch_sizes=[1, 2]) def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_: Dict = torch_device == "cpu" SCREAMING_SNAKE_CASE_: List[Any] = True self._test_inference_batch_single_identical( batch_size=2 , test_max_difference=lowerCAmelCase__ , relax_max_difference=lowerCAmelCase__ , ) def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Dict = self.get_dummy_components() SCREAMING_SNAKE_CASE_: str = self.pipeline_class(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = 1 SCREAMING_SNAKE_CASE_: Any = 2 SCREAMING_SNAKE_CASE_: Dict = self.get_dummy_inputs(lowerCAmelCase__) for key in inputs.keys(): if key in self.batch_params: SCREAMING_SNAKE_CASE_: List[Any] = batch_size * [inputs[key]] SCREAMING_SNAKE_CASE_: Tuple = pipe(**lowerCAmelCase__ , num_images_per_prompt=lowerCAmelCase__)[0] assert images.shape[0] == batch_size * num_images_per_prompt @slow @require_torch_gpu class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : Any): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: List[str] = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/shap_e/test_shap_e_np_out.npy") SCREAMING_SNAKE_CASE_: List[str] = ShapEPipeline.from_pretrained("openai/shap-e") SCREAMING_SNAKE_CASE_: Optional[int] = pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = torch.Generator(device=lowerCAmelCase__).manual_seed(0) SCREAMING_SNAKE_CASE_: int = pipe( "a shark" , generator=lowerCAmelCase__ , guidance_scale=15.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__)
13
1
import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, EulerAncestralDiscreteScheduler, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionPanoramaPipeline, UNetaDConditionModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() @skip_mps class __lowercase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : int = StableDiffusionPanoramaPipeline _UpperCAmelCase : str = TEXT_TO_IMAGE_PARAMS _UpperCAmelCase : Optional[int] = TEXT_TO_IMAGE_BATCH_PARAMS _UpperCAmelCase : str = TEXT_TO_IMAGE_IMAGE_PARAMS _UpperCAmelCase : List[str] = TEXT_TO_IMAGE_IMAGE_PARAMS def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): torch.manual_seed(0) SCREAMING_SNAKE_CASE_: int = 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 , ) SCREAMING_SNAKE_CASE_: Union[str, Any] = DDIMScheduler() torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Union[str, Any] = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"] , up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"] , latent_channels=4 , ) torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Any = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) SCREAMING_SNAKE_CASE_: str = CLIPTextModel(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip") SCREAMING_SNAKE_CASE_: int = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, "safety_checker": None, "feature_extractor": None, } return components def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Optional[int]=0): SCREAMING_SNAKE_CASE_: Optional[int] = torch.manual_seed(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = { "prompt": "a photo of the dolomites", "generator": generator, # Setting height and width to None to prevent OOMs on CPU. "height": None, "width": None, "num_inference_steps": 1, "guidance_scale": 6.0, "output_type": "numpy", } return inputs def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Optional[Any] = "cpu" # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_: Tuple = self.get_dummy_components() SCREAMING_SNAKE_CASE_: Tuple = StableDiffusionPanoramaPipeline(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = sd_pipe.to(lowerCAmelCase__) sd_pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = self.get_dummy_inputs(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = sd_pipe(**lowerCAmelCase__).images SCREAMING_SNAKE_CASE_: List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) SCREAMING_SNAKE_CASE_: Any = np.array([0.6186, 0.5374, 0.4915, 0.4135, 0.4114, 0.4563, 0.5128, 0.4977, 0.4757]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-2 def _SCREAMING_SNAKE_CASE ( self : Tuple): super().test_inference_batch_consistent(batch_sizes=[1, 2]) def _SCREAMING_SNAKE_CASE ( self : List[str]): super().test_inference_batch_single_identical(batch_size=2 , expected_max_diff=3.25E-3) def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_: Optional[Any] = "cpu" # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_: str = self.get_dummy_components() SCREAMING_SNAKE_CASE_: Union[str, Any] = StableDiffusionPanoramaPipeline(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = sd_pipe.to(lowerCAmelCase__) sd_pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = self.get_dummy_inputs(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = "french fries" SCREAMING_SNAKE_CASE_: List[str] = sd_pipe(**lowerCAmelCase__ , negative_prompt=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = output.images SCREAMING_SNAKE_CASE_: int = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) SCREAMING_SNAKE_CASE_: int = np.array([0.6187, 0.5375, 0.4915, 0.4136, 0.4114, 0.4563, 0.5128, 0.4976, 0.4757]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-2 def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Tuple = "cpu" # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_: List[str] = self.get_dummy_components() SCREAMING_SNAKE_CASE_: Any = StableDiffusionPanoramaPipeline(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = sd_pipe.to(lowerCAmelCase__) sd_pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = self.get_dummy_inputs(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = sd_pipe(**lowerCAmelCase__ , view_batch_size=2) SCREAMING_SNAKE_CASE_: List[Any] = output.images SCREAMING_SNAKE_CASE_: int = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) SCREAMING_SNAKE_CASE_: Any = np.array([0.6187, 0.5375, 0.4915, 0.4136, 0.4114, 0.4563, 0.5128, 0.4976, 0.4757]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-2 def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Optional[Any] = "cpu" # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_: str = self.get_dummy_components() SCREAMING_SNAKE_CASE_: Any = EulerAncestralDiscreteScheduler( beta_start=0.0_0085 , beta_end=0.012 , beta_schedule="scaled_linear") SCREAMING_SNAKE_CASE_: Tuple = StableDiffusionPanoramaPipeline(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = sd_pipe.to(lowerCAmelCase__) sd_pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = self.get_dummy_inputs(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = sd_pipe(**lowerCAmelCase__).images SCREAMING_SNAKE_CASE_: int = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) SCREAMING_SNAKE_CASE_: List[Any] = np.array([0.4024, 0.6510, 0.4901, 0.5378, 0.5813, 0.5622, 0.4795, 0.4467, 0.4952]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-2 def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: List[str] = "cpu" # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_: str = self.get_dummy_components() SCREAMING_SNAKE_CASE_: Optional[int] = PNDMScheduler( beta_start=0.0_0085 , beta_end=0.012 , beta_schedule="scaled_linear" , skip_prk_steps=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = StableDiffusionPanoramaPipeline(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = sd_pipe.to(lowerCAmelCase__) sd_pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = self.get_dummy_inputs(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = sd_pipe(**lowerCAmelCase__).images SCREAMING_SNAKE_CASE_: List[str] = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) SCREAMING_SNAKE_CASE_: Optional[int] = np.array([0.6391, 0.6291, 0.4861, 0.5134, 0.5552, 0.4578, 0.5032, 0.5023, 0.4539]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-2 @slow @require_torch_gpu class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : List[str]): super().tearDown() gc.collect() torch.cuda.empty_cache() def _SCREAMING_SNAKE_CASE ( self : Any , lowerCAmelCase__ : Optional[Any]=0): SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.manual_seed(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = { "prompt": "a photo of the dolomites", "generator": generator, "num_inference_steps": 3, "guidance_scale": 7.5, "output_type": "numpy", } return inputs def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: List[Any] = "stabilityai/stable-diffusion-2-base" SCREAMING_SNAKE_CASE_: Any = DDIMScheduler.from_pretrained(lowerCAmelCase__ , subfolder="scheduler") SCREAMING_SNAKE_CASE_: int = StableDiffusionPanoramaPipeline.from_pretrained(lowerCAmelCase__ , scheduler=lowerCAmelCase__ , safety_checker=lowerCAmelCase__) pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_: List[str] = self.get_inputs() SCREAMING_SNAKE_CASE_: Tuple = pipe(**lowerCAmelCase__).images SCREAMING_SNAKE_CASE_: int = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 2048, 3) SCREAMING_SNAKE_CASE_: List[str] = np.array( [ 0.3696_8392, 0.2702_5372, 0.3244_6766, 0.2837_9387, 0.3636_3274, 0.3073_3347, 0.2710_0027, 0.2705_4125, 0.2553_6096, ]) assert np.abs(expected_slice - image_slice).max() < 1E-2 def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Dict = StableDiffusionPanoramaPipeline.from_pretrained( "stabilityai/stable-diffusion-2-base" , safety_checker=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = LMSDiscreteScheduler.from_config(pipe.scheduler.config) pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_: Optional[int] = self.get_inputs() SCREAMING_SNAKE_CASE_: List[str] = pipe(**lowerCAmelCase__).images SCREAMING_SNAKE_CASE_: Optional[Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 2048, 3) SCREAMING_SNAKE_CASE_: int = np.array( [ [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ] ]) assert np.abs(expected_slice - image_slice).max() < 1E-3 def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Tuple = 0 def callback_fn(lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : torch.FloatTensor) -> None: SCREAMING_SNAKE_CASE_: str = True nonlocal number_of_steps number_of_steps += 1 if step == 1: SCREAMING_SNAKE_CASE_: int = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 256) SCREAMING_SNAKE_CASE_: str = latents[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE_: Any = np.array( [ 0.1868_1869, 0.3390_7816, 0.536_1276, 0.1443_2865, -0.0285_6611, -0.7394_1123, 0.2339_7987, 0.4732_2682, -0.3782_3164, ]) assert np.abs(latents_slice.flatten() - expected_slice).max() < 5E-2 elif step == 2: SCREAMING_SNAKE_CASE_: str = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 256) SCREAMING_SNAKE_CASE_: Dict = latents[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE_: List[str] = np.array( [ 0.1853_9645, 0.3398_7248, 0.537_8559, 0.1443_7142, -0.0245_5261, -0.733_8317, 0.2399_0755, 0.4735_6272, -0.378_6505, ]) assert np.abs(latents_slice.flatten() - expected_slice).max() < 5E-2 SCREAMING_SNAKE_CASE_: Optional[int] = False SCREAMING_SNAKE_CASE_: Optional[int] = "stabilityai/stable-diffusion-2-base" SCREAMING_SNAKE_CASE_: Dict = DDIMScheduler.from_pretrained(lowerCAmelCase__ , subfolder="scheduler") SCREAMING_SNAKE_CASE_: List[str] = StableDiffusionPanoramaPipeline.from_pretrained(lowerCAmelCase__ , scheduler=lowerCAmelCase__ , safety_checker=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_: List[Any] = self.get_inputs() pipe(**lowerCAmelCase__ , callback=lowerCAmelCase__ , callback_steps=1) assert callback_fn.has_been_called assert number_of_steps == 3 def _SCREAMING_SNAKE_CASE ( self : Dict): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() SCREAMING_SNAKE_CASE_: Optional[Any] = "stabilityai/stable-diffusion-2-base" SCREAMING_SNAKE_CASE_: Optional[Any] = DDIMScheduler.from_pretrained(lowerCAmelCase__ , subfolder="scheduler") SCREAMING_SNAKE_CASE_: Dict = StableDiffusionPanoramaPipeline.from_pretrained(lowerCAmelCase__ , scheduler=lowerCAmelCase__ , safety_checker=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) pipe.enable_attention_slicing(1) pipe.enable_sequential_cpu_offload() SCREAMING_SNAKE_CASE_: Union[str, Any] = self.get_inputs() SCREAMING_SNAKE_CASE_: int = pipe(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.cuda.max_memory_allocated() # make sure that less than 5.2 GB is allocated assert mem_bytes < 5.5 * 10**9
13
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
13
1
from __future__ import annotations def A_ ( _UpperCAmelCase ): if len(_UpperCAmelCase ) == 0: return [] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = min(_UpperCAmelCase ), max(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Dict = int(max_value - min_value ) + 1 SCREAMING_SNAKE_CASE_: list[list] = [[] for _ in range(_UpperCAmelCase )] for i in my_list: buckets[int(i - min_value )].append(_UpperCAmelCase ) return [v for bucket in buckets for v in sorted(_UpperCAmelCase )] if __name__ == "__main__": from doctest import testmod testmod() assert bucket_sort([4, 5, 3, 2, 1]) == [1, 2, 3, 4, 5] assert bucket_sort([0, 1, -10, 15, 2, -2]) == [-10, -2, 0, 1, 2, 15]
13
class __lowercase : """simple docstring""" def __init__( self : List[Any] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[Any]): SCREAMING_SNAKE_CASE_: List[str] = name SCREAMING_SNAKE_CASE_: Union[str, Any] = val def __str__( self : Dict): return F"{self.__class__.__name__}({self.name}, {self.val})" def __lt__( self : List[str] , lowerCAmelCase__ : Any): return self.val < other.val class __lowercase : """simple docstring""" def __init__( self : Tuple , lowerCAmelCase__ : Dict): SCREAMING_SNAKE_CASE_: str = {} SCREAMING_SNAKE_CASE_: int = {} SCREAMING_SNAKE_CASE_: Any = self.build_heap(lowerCAmelCase__) def __getitem__( self : List[Any] , lowerCAmelCase__ : Dict): return self.get_value(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : Dict): return (idx - 1) // 2 def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Optional[Any]): return idx * 2 + 1 def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Tuple): return idx * 2 + 2 def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Optional[int]): return self.heap_dict[key] def _SCREAMING_SNAKE_CASE ( self : List[Any] , lowerCAmelCase__ : Union[str, Any]): SCREAMING_SNAKE_CASE_: Tuple = len(lowerCAmelCase__) - 1 SCREAMING_SNAKE_CASE_: List[str] = self.get_parent_idx(lowerCAmelCase__) for idx, i in enumerate(lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Union[str, Any] = idx SCREAMING_SNAKE_CASE_: str = i.val for i in range(lowerCAmelCase__ , -1 , -1): self.sift_down(lowerCAmelCase__ , lowerCAmelCase__) return array def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[str]): while True: SCREAMING_SNAKE_CASE_: Optional[Any] = self.get_left_child_idx(lowerCAmelCase__) # noqa: E741 SCREAMING_SNAKE_CASE_: Dict = self.get_right_child_idx(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = idx if l < len(lowerCAmelCase__) and array[l] < array[idx]: SCREAMING_SNAKE_CASE_: List[str] = l if r < len(lowerCAmelCase__) and array[r] < array[smallest]: SCREAMING_SNAKE_CASE_: str = r if smallest != idx: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = array[smallest], array[idx] ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ): Optional[Any] = ( self.idx_of_element[array[smallest]], self.idx_of_element[array[idx]], ) SCREAMING_SNAKE_CASE_: Optional[int] = smallest else: break def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : str): SCREAMING_SNAKE_CASE_: Any = self.get_parent_idx(lowerCAmelCase__) while p >= 0 and self.heap[p] > self.heap[idx]: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = self.heap[idx], self.heap[p] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = ( self.idx_of_element[self.heap[idx]], self.idx_of_element[self.heap[p]], ) SCREAMING_SNAKE_CASE_: Union[str, Any] = p SCREAMING_SNAKE_CASE_: Optional[int] = self.get_parent_idx(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[Any]): return self.heap[0] def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self.heap[-1], self.heap[0] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = ( self.idx_of_element[self.heap[-1]], self.idx_of_element[self.heap[0]], ) SCREAMING_SNAKE_CASE_: int = self.heap.pop() del self.idx_of_element[x] self.sift_down(0 , self.heap) return x def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Tuple): self.heap.append(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = len(self.heap) - 1 SCREAMING_SNAKE_CASE_: List[str] = node.val self.sift_up(len(self.heap) - 1) def _SCREAMING_SNAKE_CASE ( self : List[Any]): return len(self.heap) == 0 def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Optional[int]): assert ( self.heap[self.idx_of_element[node]].val > new_value ), "newValue must be less that current value" SCREAMING_SNAKE_CASE_: Any = new_value SCREAMING_SNAKE_CASE_: Tuple = new_value self.sift_up(self.idx_of_element[node]) lowerCAmelCase : int = Node("""R""", -1) lowerCAmelCase : str = Node("""B""", 6) lowerCAmelCase : str = Node("""A""", 3) lowerCAmelCase : List[str] = Node("""X""", 1) lowerCAmelCase : Union[str, Any] = Node("""E""", 4) # Use one of these two ways to generate Min-Heap # Generating Min-Heap from array lowerCAmelCase : Optional[Any] = MinHeap([r, b, a, x, e]) # Generating Min-Heap by Insert method # myMinHeap.insert(a) # myMinHeap.insert(b) # myMinHeap.insert(x) # myMinHeap.insert(r) # myMinHeap.insert(e) # Before print("""Min Heap - before decrease key""") for i in my_min_heap.heap: print(i) print("""Min Heap - After decrease key of node [B -> -17]""") my_min_heap.decrease_key(b, -17) # After for i in my_min_heap.heap: print(i) if __name__ == "__main__": import doctest doctest.testmod()
13
1
import copy from typing import TYPE_CHECKING, Any, Mapping, Optional, OrderedDict from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto.configuration_auto import AutoConfig if TYPE_CHECKING: from ... import PreTrainedTokenizerBase, TensorType lowerCAmelCase : Union[str, Any] = logging.get_logger(__name__) class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : Tuple = '''vision-encoder-decoder''' _UpperCAmelCase : Union[str, Any] = True def __init__( self : Union[str, Any] , **lowerCAmelCase__ : Union[str, Any]): super().__init__(**lowerCAmelCase__) if "encoder" not in kwargs or "decoder" not in kwargs: raise ValueError( F"A configuraton of type {self.model_type} cannot be instantiated because " F"not both `encoder` and `decoder` sub-configurations are passed, but only {kwargs}") SCREAMING_SNAKE_CASE_: Tuple = kwargs.pop("encoder") SCREAMING_SNAKE_CASE_: List[str] = encoder_config.pop("model_type") SCREAMING_SNAKE_CASE_: Union[str, Any] = kwargs.pop("decoder") SCREAMING_SNAKE_CASE_: Any = decoder_config.pop("model_type") SCREAMING_SNAKE_CASE_: Optional[Any] = AutoConfig.for_model(lowerCAmelCase__ , **lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = AutoConfig.for_model(lowerCAmelCase__ , **lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = True @classmethod def _SCREAMING_SNAKE_CASE ( cls : Optional[Any] , lowerCAmelCase__ : PretrainedConfig , lowerCAmelCase__ : PretrainedConfig , **lowerCAmelCase__ : Optional[Any]): logger.info("Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config") SCREAMING_SNAKE_CASE_: int = True SCREAMING_SNAKE_CASE_: List[Any] = True return cls(encoder=encoder_config.to_dict() , decoder=decoder_config.to_dict() , **lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Tuple = copy.deepcopy(self.__dict__) SCREAMING_SNAKE_CASE_: List[str] = self.encoder.to_dict() SCREAMING_SNAKE_CASE_: Tuple = self.decoder.to_dict() SCREAMING_SNAKE_CASE_: List[Any] = self.__class__.model_type return output class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : int = version.parse('''1.11''' ) @property def _SCREAMING_SNAKE_CASE ( self : Any): return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ]) @property def _SCREAMING_SNAKE_CASE ( self : str): return 1E-4 @property def _SCREAMING_SNAKE_CASE ( self : Tuple): return OrderedDict({"last_hidden_state": {0: "batch", 1: "encoder_sequence"}}) class __lowercase ( UpperCAmelCase_ ): """simple docstring""" @property def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: Tuple = OrderedDict() SCREAMING_SNAKE_CASE_: Optional[Any] = {0: "batch", 1: "past_decoder_sequence + sequence"} SCREAMING_SNAKE_CASE_: List[str] = {0: "batch", 1: "past_decoder_sequence + sequence"} SCREAMING_SNAKE_CASE_: Union[str, Any] = {0: "batch", 1: "encoder_sequence"} return common_inputs def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : "PreTrainedTokenizerBase" , lowerCAmelCase__ : int = -1 , lowerCAmelCase__ : int = -1 , lowerCAmelCase__ : bool = False , lowerCAmelCase__ : Optional["TensorType"] = None , ): import torch SCREAMING_SNAKE_CASE_: int = OrderedDict() SCREAMING_SNAKE_CASE_: str = super().generate_dummy_inputs( lowerCAmelCase__ , batch_size=lowerCAmelCase__ , seq_length=lowerCAmelCase__ , is_pair=lowerCAmelCase__ , framework=lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = dummy_input["input_ids"].shape SCREAMING_SNAKE_CASE_: List[Any] = (batch, encoder_sequence, self._config.encoder_hidden_size) SCREAMING_SNAKE_CASE_: Any = dummy_input.pop("input_ids") SCREAMING_SNAKE_CASE_: int = dummy_input.pop("attention_mask") SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.zeros(lowerCAmelCase__) return common_inputs class __lowercase ( UpperCAmelCase_ ): """simple docstring""" @property def _SCREAMING_SNAKE_CASE ( self : Optional[int]): pass def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : PretrainedConfig): return VisionEncoderDecoderEncoderOnnxConfig(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Any , lowerCAmelCase__ : PretrainedConfig , lowerCAmelCase__ : PretrainedConfig , lowerCAmelCase__ : str = "default"): SCREAMING_SNAKE_CASE_: Optional[int] = encoder_config.hidden_size return VisionEncoderDecoderDecoderOnnxConfig(lowerCAmelCase__ , lowerCAmelCase__)
13
import random import unittest import numpy as np import transformers from transformers import is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax if is_flax_available(): import os import jax.numpy as jnp from jax import jit from transformers import AutoTokenizer, FlaxAutoModelForCausalLM from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model lowerCAmelCase : Any = """0.12""" # assumed parallelism: 8 if is_torch_available(): import torch def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=None ): if rng is None: SCREAMING_SNAKE_CASE_: List[Any] = random.Random() SCREAMING_SNAKE_CASE_: Optional[Any] = 1 for dim in shape: total_dims *= dim SCREAMING_SNAKE_CASE_: Optional[Any] = [] for _ in range(_UpperCAmelCase ): values.append(rng.randint(0 , vocab_size - 1 ) ) SCREAMING_SNAKE_CASE_: List[Any] = np.array(_UpperCAmelCase , dtype=jnp.intaa ).reshape(_UpperCAmelCase ) return output def A_ ( _UpperCAmelCase , _UpperCAmelCase=None ): SCREAMING_SNAKE_CASE_: Optional[int] = ids_tensor(_UpperCAmelCase , vocab_size=2 , rng=_UpperCAmelCase ) # make sure that at least one token is attended to for each batch SCREAMING_SNAKE_CASE_: Optional[Any] = 1 return attn_mask @require_flax class __lowercase : """simple docstring""" _UpperCAmelCase : Any = None _UpperCAmelCase : List[Any] = () def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self.model_tester.prepare_config_and_inputs_for_common() # cut to half length & take max batch_size 3 SCREAMING_SNAKE_CASE_: str = 2 SCREAMING_SNAKE_CASE_: Optional[int] = inputs["input_ids"].shape[-1] // 2 SCREAMING_SNAKE_CASE_: List[str] = inputs["input_ids"][:max_batch_size, :sequence_length] SCREAMING_SNAKE_CASE_: Any = jnp.ones_like(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = attention_mask[:max_batch_size, :sequence_length] # generate max 5 tokens SCREAMING_SNAKE_CASE_: Optional[Any] = input_ids.shape[-1] + 5 if config.eos_token_id is not None and config.pad_token_id is None: # hack to allow generate for models such as GPT2 as is done in `generate()` SCREAMING_SNAKE_CASE_: Optional[Any] = config.eos_token_id return config, input_ids, attention_mask, max_length @is_pt_flax_cross_test def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Union[str, Any] = False SCREAMING_SNAKE_CASE_: Dict = max_length SCREAMING_SNAKE_CASE_: List[Any] = 0 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: int = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = model_class.__name__[4:] # Skip the "Flax" at the beginning SCREAMING_SNAKE_CASE_: List[Any] = getattr(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = pt_model_class(lowerCAmelCase__).eval() SCREAMING_SNAKE_CASE_: str = load_flax_weights_in_pytorch_model(lowerCAmelCase__ , flax_model.params) SCREAMING_SNAKE_CASE_: List[Any] = flax_model.generate(lowerCAmelCase__).sequences SCREAMING_SNAKE_CASE_: str = pt_model.generate(torch.tensor(lowerCAmelCase__ , dtype=torch.long)) if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]: SCREAMING_SNAKE_CASE_: List[Any] = flax_generation_outputs[:, : pt_generation_outputs.shape[-1]] self.assertListEqual(pt_generation_outputs.numpy().tolist() , flax_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Optional[int] = False SCREAMING_SNAKE_CASE_: Optional[int] = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Union[str, Any] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = jit(model.generate) SCREAMING_SNAKE_CASE_: Union[str, Any] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Optional[Any] = True SCREAMING_SNAKE_CASE_: Dict = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Tuple = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = jit(model.generate) SCREAMING_SNAKE_CASE_: Dict = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: int = False SCREAMING_SNAKE_CASE_: Optional[int] = max_length SCREAMING_SNAKE_CASE_: Optional[int] = 2 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: List[str] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = jit(model.generate) SCREAMING_SNAKE_CASE_: Optional[int] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: str = False SCREAMING_SNAKE_CASE_: int = max_length SCREAMING_SNAKE_CASE_: str = 2 SCREAMING_SNAKE_CASE_: Optional[Any] = 2 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: str = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[0] , input_ids.shape[0] * config.num_return_sequences) def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Tuple = True SCREAMING_SNAKE_CASE_: List[str] = max_length SCREAMING_SNAKE_CASE_: Any = 0.8 SCREAMING_SNAKE_CASE_: Any = 10 SCREAMING_SNAKE_CASE_: List[str] = 0.3 SCREAMING_SNAKE_CASE_: Tuple = 1 SCREAMING_SNAKE_CASE_: Union[str, Any] = 8 SCREAMING_SNAKE_CASE_: int = 9 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: List[str] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = jit(model.generate) SCREAMING_SNAKE_CASE_: List[Any] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Any = max_length SCREAMING_SNAKE_CASE_: int = 1 SCREAMING_SNAKE_CASE_: Union[str, Any] = 8 SCREAMING_SNAKE_CASE_: List[Any] = 9 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: int = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = jit(model.generate) SCREAMING_SNAKE_CASE_: List[str] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Any = max_length SCREAMING_SNAKE_CASE_: List[str] = 2 SCREAMING_SNAKE_CASE_: str = 1 SCREAMING_SNAKE_CASE_: Tuple = 8 SCREAMING_SNAKE_CASE_: List[Any] = 9 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Optional[int] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = jit(model.generate) SCREAMING_SNAKE_CASE_: List[str] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self._get_input_ids_and_config() # pad attention mask on the left SCREAMING_SNAKE_CASE_: Dict = attention_mask.at[(0, 0)].set(0) SCREAMING_SNAKE_CASE_: Dict = False SCREAMING_SNAKE_CASE_: Optional[int] = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Any = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = model.generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = jit(model.generate) SCREAMING_SNAKE_CASE_: List[Any] = jit_generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = self._get_input_ids_and_config() # pad attention mask on the left SCREAMING_SNAKE_CASE_: List[Any] = attention_mask.at[(0, 0)].set(0) SCREAMING_SNAKE_CASE_: Optional[int] = True SCREAMING_SNAKE_CASE_: Union[str, Any] = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: str = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = model.generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = jit(model.generate) SCREAMING_SNAKE_CASE_: Optional[Any] = jit_generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self._get_input_ids_and_config() # pad attention mask on the left SCREAMING_SNAKE_CASE_: Dict = attention_mask.at[(0, 0)].set(0) SCREAMING_SNAKE_CASE_: Optional[Any] = 2 SCREAMING_SNAKE_CASE_: Any = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Tuple = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = model.generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = jit(model.generate) SCREAMING_SNAKE_CASE_: Union[str, Any] = jit_generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) @require_flax class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: Tuple = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-bert") SCREAMING_SNAKE_CASE_: List[Any] = FlaxAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-bert-flax-only") SCREAMING_SNAKE_CASE_: Optional[int] = "Hello world" SCREAMING_SNAKE_CASE_: List[Any] = tokenizer(lowerCAmelCase__ , return_tensors="np").input_ids # typos are quickly detected (the correct argument is `do_sample`) with self.assertRaisesRegex(lowerCAmelCase__ , "do_samples"): model.generate(lowerCAmelCase__ , do_samples=lowerCAmelCase__) # arbitrary arguments that will not be used anywhere are also not accepted with self.assertRaisesRegex(lowerCAmelCase__ , "foo"): SCREAMING_SNAKE_CASE_: str = {"foo": "bar"} model.generate(lowerCAmelCase__ , **lowerCAmelCase__)
13
1
import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : int = (KDPMaDiscreteScheduler,) _UpperCAmelCase : Optional[Any] = 10 def _SCREAMING_SNAKE_CASE ( self : Dict , **lowerCAmelCase__ : Optional[Any]): SCREAMING_SNAKE_CASE_: Tuple = { "num_train_timesteps": 1100, "beta_start": 0.0001, "beta_end": 0.02, "beta_schedule": "linear", } config.update(**lowerCAmelCase__) return config def _SCREAMING_SNAKE_CASE ( self : str): for timesteps in [10, 50, 100, 1000]: self.check_over_configs(num_train_timesteps=lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): for beta_start, beta_end in zip([0.0_0001, 0.0001, 0.001] , [0.0002, 0.002, 0.02]): self.check_over_configs(beta_start=lowerCAmelCase__ , beta_end=lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str]): for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str]): for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: int = self.scheduler_classes[0] SCREAMING_SNAKE_CASE_: Optional[int] = self.get_scheduler_config(prediction_type="v_prediction") SCREAMING_SNAKE_CASE_: Optional[int] = scheduler_class(**lowerCAmelCase__) scheduler.set_timesteps(self.num_inference_steps) SCREAMING_SNAKE_CASE_: Any = self.dummy_model() SCREAMING_SNAKE_CASE_: int = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE_: Optional[int] = sample.to(lowerCAmelCase__) for i, t in enumerate(scheduler.timesteps): SCREAMING_SNAKE_CASE_: Tuple = scheduler.scale_model_input(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = model(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = scheduler.step(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = output.prev_sample SCREAMING_SNAKE_CASE_: Tuple = torch.sum(torch.abs(lowerCAmelCase__)) SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.mean(torch.abs(lowerCAmelCase__)) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.6934E-07) < 1E-2 assert abs(result_mean.item() - 6.1112E-10) < 1E-3 else: # CUDA assert abs(result_sum.item() - 4.693_4286_5017_0972E-07) < 1E-2 assert abs(result_mean.item() - 0.0002) < 1E-3 def _SCREAMING_SNAKE_CASE ( self : int): if torch_device == "mps": return SCREAMING_SNAKE_CASE_: List[str] = self.scheduler_classes[0] SCREAMING_SNAKE_CASE_: Dict = self.get_scheduler_config() SCREAMING_SNAKE_CASE_: Any = scheduler_class(**lowerCAmelCase__) scheduler.set_timesteps(self.num_inference_steps) SCREAMING_SNAKE_CASE_: Dict = self.dummy_model() SCREAMING_SNAKE_CASE_: str = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE_: Any = sample.to(lowerCAmelCase__) for i, t in enumerate(scheduler.timesteps): SCREAMING_SNAKE_CASE_: Union[str, Any] = scheduler.scale_model_input(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = model(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = scheduler.step(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = output.prev_sample SCREAMING_SNAKE_CASE_: List[Any] = torch.sum(torch.abs(lowerCAmelCase__)) SCREAMING_SNAKE_CASE_: str = torch.mean(torch.abs(lowerCAmelCase__)) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 20.4125) < 1E-2 assert abs(result_mean.item() - 0.0266) < 1E-3 else: # CUDA assert abs(result_sum.item() - 20.4125) < 1E-2 assert abs(result_mean.item() - 0.0266) < 1E-3 def _SCREAMING_SNAKE_CASE ( self : str): if torch_device == "mps": return SCREAMING_SNAKE_CASE_: List[str] = self.scheduler_classes[0] SCREAMING_SNAKE_CASE_: Optional[Any] = self.get_scheduler_config() SCREAMING_SNAKE_CASE_: List[Any] = scheduler_class(**lowerCAmelCase__) scheduler.set_timesteps(self.num_inference_steps , device=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = self.dummy_model() SCREAMING_SNAKE_CASE_: Dict = self.dummy_sample_deter.to(lowerCAmelCase__) * scheduler.init_noise_sigma for t in scheduler.timesteps: SCREAMING_SNAKE_CASE_: Tuple = scheduler.scale_model_input(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = scheduler.step(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = output.prev_sample SCREAMING_SNAKE_CASE_: List[Any] = torch.sum(torch.abs(lowerCAmelCase__)) SCREAMING_SNAKE_CASE_: Dict = torch.mean(torch.abs(lowerCAmelCase__)) if str(lowerCAmelCase__).startswith("cpu"): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 20.4125) < 1E-2 assert abs(result_mean.item() - 0.0266) < 1E-3 else: # CUDA assert abs(result_sum.item() - 20.4125) < 1E-2 assert abs(result_mean.item() - 0.0266) < 1E-3
13
import argparse import re import torch from CLAP import create_model from transformers import AutoFeatureExtractor, ClapConfig, ClapModel lowerCAmelCase : Union[str, Any] = { """text_branch""": """text_model""", """audio_branch""": """audio_model.audio_encoder""", """attn""": """attention.self""", """self.proj""": """output.dense""", """attention.self_mask""": """attn_mask""", """mlp.fc1""": """intermediate.dense""", """mlp.fc2""": """output.dense""", """norm1""": """layernorm_before""", """norm2""": """layernorm_after""", """bn0""": """batch_norm""", } lowerCAmelCase : int = AutoFeatureExtractor.from_pretrained("""laion/clap-htsat-unfused""", truncation="""rand_trunc""") def A_ ( _UpperCAmelCase , _UpperCAmelCase=False ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = create_model( "HTSAT-tiny" , "roberta" , _UpperCAmelCase , precision="fp32" , device="cuda:0" if torch.cuda.is_available() else "cpu" , enable_fusion=_UpperCAmelCase , fusion_type="aff_2d" if enable_fusion else None , ) return model, model_cfg def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = {} SCREAMING_SNAKE_CASE_: Tuple = R".*sequential.(\d+).*" SCREAMING_SNAKE_CASE_: Dict = R".*_projection.(\d+).*" for key, value in state_dict.items(): # check if any key needs to be modified for key_to_modify, new_key in KEYS_TO_MODIFY_MAPPING.items(): if key_to_modify in key: SCREAMING_SNAKE_CASE_: Any = key.replace(_UpperCAmelCase , _UpperCAmelCase ) if re.match(_UpperCAmelCase , _UpperCAmelCase ): # replace sequential layers with list SCREAMING_SNAKE_CASE_: Optional[int] = re.match(_UpperCAmelCase , _UpperCAmelCase ).group(1 ) SCREAMING_SNAKE_CASE_: Dict = key.replace(f"sequential.{sequential_layer}." , f"layers.{int(_UpperCAmelCase )//3}.linear." ) elif re.match(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = int(re.match(_UpperCAmelCase , _UpperCAmelCase ).group(1 ) ) # Because in CLAP they use `nn.Sequential`... SCREAMING_SNAKE_CASE_: Optional[int] = 1 if projecton_layer == 0 else 2 SCREAMING_SNAKE_CASE_: Dict = key.replace(f"_projection.{projecton_layer}." , f"_projection.linear{transformers_projection_layer}." ) if "audio" and "qkv" in key: # split qkv into query key and value SCREAMING_SNAKE_CASE_: Tuple = value SCREAMING_SNAKE_CASE_: List[str] = mixed_qkv.size(0 ) // 3 SCREAMING_SNAKE_CASE_: Any = mixed_qkv[:qkv_dim] SCREAMING_SNAKE_CASE_: Optional[int] = mixed_qkv[qkv_dim : qkv_dim * 2] SCREAMING_SNAKE_CASE_: Optional[Any] = mixed_qkv[qkv_dim * 2 :] SCREAMING_SNAKE_CASE_: str = query_layer SCREAMING_SNAKE_CASE_: int = key_layer SCREAMING_SNAKE_CASE_: List[Any] = value_layer else: SCREAMING_SNAKE_CASE_: int = value return model_state_dict def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=False ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = init_clap(_UpperCAmelCase , enable_fusion=_UpperCAmelCase ) clap_model.eval() SCREAMING_SNAKE_CASE_: Union[str, Any] = clap_model.state_dict() SCREAMING_SNAKE_CASE_: Optional[int] = rename_state_dict(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[str] = ClapConfig() SCREAMING_SNAKE_CASE_: Tuple = enable_fusion SCREAMING_SNAKE_CASE_: Tuple = ClapModel(_UpperCAmelCase ) # ignore the spectrogram embedding layer model.load_state_dict(_UpperCAmelCase , strict=_UpperCAmelCase ) model.save_pretrained(_UpperCAmelCase ) transformers_config.save_pretrained(_UpperCAmelCase ) if __name__ == "__main__": lowerCAmelCase : Tuple = 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("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""") parser.add_argument("""--enable_fusion""", action="""store_true""", help="""Whether to enable fusion or not""") lowerCAmelCase : int = parser.parse_args() convert_clap_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.enable_fusion)
13
1
import fire from transformers import AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer def A_ ( _UpperCAmelCase , _UpperCAmelCase , **_UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = AutoConfig.from_pretrained(_UpperCAmelCase , **_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Dict = AutoModelForSeqaSeqLM.from_config(_UpperCAmelCase ) model.save_pretrained(_UpperCAmelCase ) AutoTokenizer.from_pretrained(_UpperCAmelCase ).save_pretrained(_UpperCAmelCase ) return model if __name__ == "__main__": fire.Fire(save_randomly_initialized_version)
13
import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_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, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __lowercase : """simple docstring""" def __init__( self : Any , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Any=13 , lowerCAmelCase__ : Tuple=30 , lowerCAmelCase__ : List[str]=2 , lowerCAmelCase__ : int=3 , lowerCAmelCase__ : Optional[int]=True , lowerCAmelCase__ : List[str]=True , lowerCAmelCase__ : str=32 , lowerCAmelCase__ : Any=5 , lowerCAmelCase__ : str=4 , lowerCAmelCase__ : int=37 , lowerCAmelCase__ : Optional[Any]="gelu" , lowerCAmelCase__ : Optional[int]=0.1 , lowerCAmelCase__ : Dict=0.1 , lowerCAmelCase__ : Tuple=10 , lowerCAmelCase__ : Optional[Any]=0.02 , lowerCAmelCase__ : List[str]=None , lowerCAmelCase__ : Union[str, Any]=2 , ): SCREAMING_SNAKE_CASE_: str = parent SCREAMING_SNAKE_CASE_: Optional[Any] = batch_size SCREAMING_SNAKE_CASE_: str = image_size SCREAMING_SNAKE_CASE_: Tuple = patch_size SCREAMING_SNAKE_CASE_: int = num_channels SCREAMING_SNAKE_CASE_: List[str] = is_training SCREAMING_SNAKE_CASE_: str = use_labels SCREAMING_SNAKE_CASE_: int = hidden_size SCREAMING_SNAKE_CASE_: List[Any] = num_hidden_layers SCREAMING_SNAKE_CASE_: Union[str, Any] = num_attention_heads SCREAMING_SNAKE_CASE_: Any = intermediate_size SCREAMING_SNAKE_CASE_: str = hidden_act SCREAMING_SNAKE_CASE_: str = hidden_dropout_prob SCREAMING_SNAKE_CASE_: List[str] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_: int = type_sequence_label_size SCREAMING_SNAKE_CASE_: Dict = initializer_range SCREAMING_SNAKE_CASE_: Dict = scope SCREAMING_SNAKE_CASE_: Dict = encoder_stride # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) SCREAMING_SNAKE_CASE_: List[Any] = (image_size // patch_size) ** 2 SCREAMING_SNAKE_CASE_: Dict = num_patches + 1 def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]) SCREAMING_SNAKE_CASE_: str = None if self.use_labels: SCREAMING_SNAKE_CASE_: Union[str, Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size) SCREAMING_SNAKE_CASE_: Optional[Any] = self.get_config() return config, pixel_values, labels def _SCREAMING_SNAKE_CASE ( self : Optional[int]): return ViTConfig( 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 , encoder_stride=self.encoder_stride , ) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : Tuple): SCREAMING_SNAKE_CASE_: Union[str, Any] = ViTModel(config=lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: Optional[int] = model(lowerCAmelCase__) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Dict): SCREAMING_SNAKE_CASE_: Optional[int] = ViTForMaskedImageModeling(config=lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: str = model(lowerCAmelCase__) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size)) # test greyscale images SCREAMING_SNAKE_CASE_: Dict = 1 SCREAMING_SNAKE_CASE_: List[str] = ViTForMaskedImageModeling(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: List[str] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]) SCREAMING_SNAKE_CASE_: str = model(lowerCAmelCase__) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size)) def _SCREAMING_SNAKE_CASE ( self : Any , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Union[str, Any]): SCREAMING_SNAKE_CASE_: Tuple = self.type_sequence_label_size SCREAMING_SNAKE_CASE_: List[str] = ViTForImageClassification(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: Any = model(lowerCAmelCase__ , labels=lowerCAmelCase__) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) # test greyscale images SCREAMING_SNAKE_CASE_: Union[str, Any] = 1 SCREAMING_SNAKE_CASE_: List[str] = ViTForImageClassification(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: Union[str, Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]) SCREAMING_SNAKE_CASE_: Dict = model(lowerCAmelCase__) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ): List[str] = config_and_inputs SCREAMING_SNAKE_CASE_: Optional[Any] = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class __lowercase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : List[Any] = ( ( ViTModel, ViTForImageClassification, ViTForMaskedImageModeling, ) if is_torch_available() else () ) _UpperCAmelCase : Tuple = ( {'''feature-extraction''': ViTModel, '''image-classification''': ViTForImageClassification} if is_torch_available() else {} ) _UpperCAmelCase : List[str] = True _UpperCAmelCase : List[Any] = False _UpperCAmelCase : Optional[Any] = False _UpperCAmelCase : Tuple = False def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: List[str] = ViTModelTester(self) SCREAMING_SNAKE_CASE_: Union[str, Any] = ConfigTester(self , config_class=lowerCAmelCase__ , has_text_modality=lowerCAmelCase__ , hidden_size=37) def _SCREAMING_SNAKE_CASE ( self : Any): self.config_tester.run_common_tests() @unittest.skip(reason="ViT does not use inputs_embeds") def _SCREAMING_SNAKE_CASE ( self : str): pass def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_: Dict = model_class(lowerCAmelCase__) self.assertIsInstance(model.get_input_embeddings() , (nn.Module)) SCREAMING_SNAKE_CASE_: List[Any] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCAmelCase__ , nn.Linear)) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_: List[Any] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = inspect.signature(model.forward) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE_: Optional[Any] = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE_: Optional[int] = ["pixel_values"] self.assertListEqual(arg_names[:1] , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__) @slow def _SCREAMING_SNAKE_CASE ( self : int): for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_: Union[str, Any] = ViTModel.from_pretrained(lowerCAmelCase__) self.assertIsNotNone(lowerCAmelCase__) def A_ ( ): SCREAMING_SNAKE_CASE_: List[Any] = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class __lowercase ( unittest.TestCase ): """simple docstring""" @cached_property def _SCREAMING_SNAKE_CASE ( self : int): return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224") if is_vision_available() else None @slow def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: int = ViTForImageClassification.from_pretrained("google/vit-base-patch16-224").to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = self.default_image_processor SCREAMING_SNAKE_CASE_: str = prepare_img() SCREAMING_SNAKE_CASE_: Optional[Any] = image_processor(images=lowerCAmelCase__ , return_tensors="pt").to(lowerCAmelCase__) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_: Optional[int] = model(**lowerCAmelCase__) # verify the logits SCREAMING_SNAKE_CASE_: Any = torch.Size((1, 1000)) self.assertEqual(outputs.logits.shape , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = torch.tensor([-0.2744, 0.8215, -0.0836]).to(lowerCAmelCase__) self.assertTrue(torch.allclose(outputs.logits[0, :3] , lowerCAmelCase__ , atol=1E-4)) @slow def _SCREAMING_SNAKE_CASE ( self : List[Any]): # ViT models have an `interpolate_pos_encoding` argument in their forward method, # allowing to interpolate the pre-trained position embeddings in order to use # the model on higher resolutions. The DINO model by Facebook AI leverages this # to visualize self-attention on higher resolution images. SCREAMING_SNAKE_CASE_: str = ViTModel.from_pretrained("facebook/dino-vits8").to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = ViTImageProcessor.from_pretrained("facebook/dino-vits8" , size=480) SCREAMING_SNAKE_CASE_: List[Any] = prepare_img() SCREAMING_SNAKE_CASE_: List[Any] = image_processor(images=lowerCAmelCase__ , return_tensors="pt") SCREAMING_SNAKE_CASE_: int = inputs.pixel_values.to(lowerCAmelCase__) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_: Optional[int] = model(lowerCAmelCase__ , interpolate_pos_encoding=lowerCAmelCase__) # verify the logits SCREAMING_SNAKE_CASE_: Tuple = torch.Size((1, 3601, 384)) self.assertEqual(outputs.last_hidden_state.shape , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.tensor( [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]]).to(lowerCAmelCase__) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , lowerCAmelCase__ , atol=1E-4)) @slow @require_accelerate @require_torch_gpu def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Dict = ViTModel.from_pretrained("facebook/dino-vits8" , torch_dtype=torch.floataa , device_map="auto") SCREAMING_SNAKE_CASE_: int = self.default_image_processor SCREAMING_SNAKE_CASE_: Union[str, Any] = prepare_img() SCREAMING_SNAKE_CASE_: Dict = image_processor(images=lowerCAmelCase__ , return_tensors="pt") SCREAMING_SNAKE_CASE_: str = inputs.pixel_values.to(lowerCAmelCase__) # forward pass to make sure inference works in fp16 with torch.no_grad(): SCREAMING_SNAKE_CASE_: str = model(lowerCAmelCase__)
13
1
import random import unittest import numpy as np import transformers from transformers import is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax if is_flax_available(): import os import jax.numpy as jnp from jax import jit from transformers import AutoTokenizer, FlaxAutoModelForCausalLM from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model lowerCAmelCase : Any = """0.12""" # assumed parallelism: 8 if is_torch_available(): import torch def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=None ): if rng is None: SCREAMING_SNAKE_CASE_: List[Any] = random.Random() SCREAMING_SNAKE_CASE_: Optional[Any] = 1 for dim in shape: total_dims *= dim SCREAMING_SNAKE_CASE_: Optional[Any] = [] for _ in range(_UpperCAmelCase ): values.append(rng.randint(0 , vocab_size - 1 ) ) SCREAMING_SNAKE_CASE_: List[Any] = np.array(_UpperCAmelCase , dtype=jnp.intaa ).reshape(_UpperCAmelCase ) return output def A_ ( _UpperCAmelCase , _UpperCAmelCase=None ): SCREAMING_SNAKE_CASE_: Optional[int] = ids_tensor(_UpperCAmelCase , vocab_size=2 , rng=_UpperCAmelCase ) # make sure that at least one token is attended to for each batch SCREAMING_SNAKE_CASE_: Optional[Any] = 1 return attn_mask @require_flax class __lowercase : """simple docstring""" _UpperCAmelCase : Any = None _UpperCAmelCase : List[Any] = () def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self.model_tester.prepare_config_and_inputs_for_common() # cut to half length & take max batch_size 3 SCREAMING_SNAKE_CASE_: str = 2 SCREAMING_SNAKE_CASE_: Optional[int] = inputs["input_ids"].shape[-1] // 2 SCREAMING_SNAKE_CASE_: List[str] = inputs["input_ids"][:max_batch_size, :sequence_length] SCREAMING_SNAKE_CASE_: Any = jnp.ones_like(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = attention_mask[:max_batch_size, :sequence_length] # generate max 5 tokens SCREAMING_SNAKE_CASE_: Optional[Any] = input_ids.shape[-1] + 5 if config.eos_token_id is not None and config.pad_token_id is None: # hack to allow generate for models such as GPT2 as is done in `generate()` SCREAMING_SNAKE_CASE_: Optional[Any] = config.eos_token_id return config, input_ids, attention_mask, max_length @is_pt_flax_cross_test def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Union[str, Any] = False SCREAMING_SNAKE_CASE_: Dict = max_length SCREAMING_SNAKE_CASE_: List[Any] = 0 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: int = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = model_class.__name__[4:] # Skip the "Flax" at the beginning SCREAMING_SNAKE_CASE_: List[Any] = getattr(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = pt_model_class(lowerCAmelCase__).eval() SCREAMING_SNAKE_CASE_: str = load_flax_weights_in_pytorch_model(lowerCAmelCase__ , flax_model.params) SCREAMING_SNAKE_CASE_: List[Any] = flax_model.generate(lowerCAmelCase__).sequences SCREAMING_SNAKE_CASE_: str = pt_model.generate(torch.tensor(lowerCAmelCase__ , dtype=torch.long)) if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]: SCREAMING_SNAKE_CASE_: List[Any] = flax_generation_outputs[:, : pt_generation_outputs.shape[-1]] self.assertListEqual(pt_generation_outputs.numpy().tolist() , flax_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Optional[int] = False SCREAMING_SNAKE_CASE_: Optional[int] = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Union[str, Any] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = jit(model.generate) SCREAMING_SNAKE_CASE_: Union[str, Any] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Optional[Any] = True SCREAMING_SNAKE_CASE_: Dict = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Tuple = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = jit(model.generate) SCREAMING_SNAKE_CASE_: Dict = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: int = False SCREAMING_SNAKE_CASE_: Optional[int] = max_length SCREAMING_SNAKE_CASE_: Optional[int] = 2 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: List[str] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = jit(model.generate) SCREAMING_SNAKE_CASE_: Optional[int] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: str = False SCREAMING_SNAKE_CASE_: int = max_length SCREAMING_SNAKE_CASE_: str = 2 SCREAMING_SNAKE_CASE_: Optional[Any] = 2 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: str = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[0] , input_ids.shape[0] * config.num_return_sequences) def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Tuple = True SCREAMING_SNAKE_CASE_: List[str] = max_length SCREAMING_SNAKE_CASE_: Any = 0.8 SCREAMING_SNAKE_CASE_: Any = 10 SCREAMING_SNAKE_CASE_: List[str] = 0.3 SCREAMING_SNAKE_CASE_: Tuple = 1 SCREAMING_SNAKE_CASE_: Union[str, Any] = 8 SCREAMING_SNAKE_CASE_: int = 9 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: List[str] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = jit(model.generate) SCREAMING_SNAKE_CASE_: List[Any] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Any = max_length SCREAMING_SNAKE_CASE_: int = 1 SCREAMING_SNAKE_CASE_: Union[str, Any] = 8 SCREAMING_SNAKE_CASE_: List[Any] = 9 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: int = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = jit(model.generate) SCREAMING_SNAKE_CASE_: List[str] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Any = max_length SCREAMING_SNAKE_CASE_: List[str] = 2 SCREAMING_SNAKE_CASE_: str = 1 SCREAMING_SNAKE_CASE_: Tuple = 8 SCREAMING_SNAKE_CASE_: List[Any] = 9 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Optional[int] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = jit(model.generate) SCREAMING_SNAKE_CASE_: List[str] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self._get_input_ids_and_config() # pad attention mask on the left SCREAMING_SNAKE_CASE_: Dict = attention_mask.at[(0, 0)].set(0) SCREAMING_SNAKE_CASE_: Dict = False SCREAMING_SNAKE_CASE_: Optional[int] = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Any = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = model.generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = jit(model.generate) SCREAMING_SNAKE_CASE_: List[Any] = jit_generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = self._get_input_ids_and_config() # pad attention mask on the left SCREAMING_SNAKE_CASE_: List[Any] = attention_mask.at[(0, 0)].set(0) SCREAMING_SNAKE_CASE_: Optional[int] = True SCREAMING_SNAKE_CASE_: Union[str, Any] = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: str = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = model.generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = jit(model.generate) SCREAMING_SNAKE_CASE_: Optional[Any] = jit_generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self._get_input_ids_and_config() # pad attention mask on the left SCREAMING_SNAKE_CASE_: Dict = attention_mask.at[(0, 0)].set(0) SCREAMING_SNAKE_CASE_: Optional[Any] = 2 SCREAMING_SNAKE_CASE_: Any = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Tuple = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = model.generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = jit(model.generate) SCREAMING_SNAKE_CASE_: Union[str, Any] = jit_generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) @require_flax class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: Tuple = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-bert") SCREAMING_SNAKE_CASE_: List[Any] = FlaxAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-bert-flax-only") SCREAMING_SNAKE_CASE_: Optional[int] = "Hello world" SCREAMING_SNAKE_CASE_: List[Any] = tokenizer(lowerCAmelCase__ , return_tensors="np").input_ids # typos are quickly detected (the correct argument is `do_sample`) with self.assertRaisesRegex(lowerCAmelCase__ , "do_samples"): model.generate(lowerCAmelCase__ , do_samples=lowerCAmelCase__) # arbitrary arguments that will not be used anywhere are also not accepted with self.assertRaisesRegex(lowerCAmelCase__ , "foo"): SCREAMING_SNAKE_CASE_: str = {"foo": "bar"} model.generate(lowerCAmelCase__ , **lowerCAmelCase__)
13
import json import os from functools import lru_cache from typing import List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging lowerCAmelCase : Any = logging.get_logger(__name__) lowerCAmelCase : Tuple = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt"""} lowerCAmelCase : Optional[int] = { """vocab_file""": { """allenai/longformer-base-4096""": """https://huggingface.co/allenai/longformer-base-4096/resolve/main/vocab.json""", """allenai/longformer-large-4096""": ( """https://huggingface.co/allenai/longformer-large-4096/resolve/main/vocab.json""" ), """allenai/longformer-large-4096-finetuned-triviaqa""": ( """https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/vocab.json""" ), """allenai/longformer-base-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/vocab.json""" ), """allenai/longformer-large-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/vocab.json""" ), }, """merges_file""": { """allenai/longformer-base-4096""": """https://huggingface.co/allenai/longformer-base-4096/resolve/main/merges.txt""", """allenai/longformer-large-4096""": ( """https://huggingface.co/allenai/longformer-large-4096/resolve/main/merges.txt""" ), """allenai/longformer-large-4096-finetuned-triviaqa""": ( """https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/merges.txt""" ), """allenai/longformer-base-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/merges.txt""" ), """allenai/longformer-large-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/merges.txt""" ), }, } lowerCAmelCase : Optional[Any] = { """allenai/longformer-base-4096""": 4096, """allenai/longformer-large-4096""": 4096, """allenai/longformer-large-4096-finetuned-triviaqa""": 4096, """allenai/longformer-base-4096-extra.pos.embd.only""": 4096, """allenai/longformer-large-4096-extra.pos.embd.only""": 4096, } @lru_cache() # Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode def A_ ( ): SCREAMING_SNAKE_CASE_: Any = ( list(range(ord("!" ) , ord("~" ) + 1 ) ) + list(range(ord("¡" ) , ord("¬" ) + 1 ) ) + list(range(ord("®" ) , ord("ÿ" ) + 1 ) ) ) SCREAMING_SNAKE_CASE_: Tuple = bs[:] SCREAMING_SNAKE_CASE_: str = 0 for b in range(2**8 ): if b not in bs: bs.append(_UpperCAmelCase ) cs.append(2**8 + n ) n += 1 SCREAMING_SNAKE_CASE_: Optional[int] = [chr(_UpperCAmelCase ) for n in cs] return dict(zip(_UpperCAmelCase , _UpperCAmelCase ) ) def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: str = set() SCREAMING_SNAKE_CASE_: Union[str, Any] = word[0] for char in word[1:]: pairs.add((prev_char, char) ) SCREAMING_SNAKE_CASE_: Tuple = char return pairs class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : Any = VOCAB_FILES_NAMES _UpperCAmelCase : Dict = PRETRAINED_VOCAB_FILES_MAP _UpperCAmelCase : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _UpperCAmelCase : List[str] = ['''input_ids''', '''attention_mask'''] def __init__( self : str , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[Any]="replace" , lowerCAmelCase__ : Optional[Any]="<s>" , lowerCAmelCase__ : int="</s>" , lowerCAmelCase__ : Optional[Any]="</s>" , lowerCAmelCase__ : int="<s>" , lowerCAmelCase__ : Optional[Any]="<unk>" , lowerCAmelCase__ : List[Any]="<pad>" , lowerCAmelCase__ : Any="<mask>" , lowerCAmelCase__ : Union[str, Any]=False , **lowerCAmelCase__ : Tuple , ): SCREAMING_SNAKE_CASE_: int = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else bos_token SCREAMING_SNAKE_CASE_: str = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else eos_token SCREAMING_SNAKE_CASE_: Optional[int] = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else sep_token SCREAMING_SNAKE_CASE_: Union[str, Any] = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else cls_token SCREAMING_SNAKE_CASE_: int = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else unk_token SCREAMING_SNAKE_CASE_: Any = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else pad_token # Mask token behave like a normal word, i.e. include the space before it SCREAMING_SNAKE_CASE_: Optional[int] = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else mask_token super().__init__( errors=lowerCAmelCase__ , bos_token=lowerCAmelCase__ , eos_token=lowerCAmelCase__ , unk_token=lowerCAmelCase__ , sep_token=lowerCAmelCase__ , cls_token=lowerCAmelCase__ , pad_token=lowerCAmelCase__ , mask_token=lowerCAmelCase__ , add_prefix_space=lowerCAmelCase__ , **lowerCAmelCase__ , ) with open(lowerCAmelCase__ , encoding="utf-8") as vocab_handle: SCREAMING_SNAKE_CASE_: Tuple = json.load(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = {v: k for k, v in self.encoder.items()} SCREAMING_SNAKE_CASE_: Optional[Any] = errors # how to handle errors in decoding SCREAMING_SNAKE_CASE_: List[Any] = bytes_to_unicode() SCREAMING_SNAKE_CASE_: Optional[Any] = {v: k for k, v in self.byte_encoder.items()} with open(lowerCAmelCase__ , encoding="utf-8") as merges_handle: SCREAMING_SNAKE_CASE_: List[Any] = merges_handle.read().split("\n")[1:-1] SCREAMING_SNAKE_CASE_: str = [tuple(merge.split()) for merge in bpe_merges] SCREAMING_SNAKE_CASE_: List[Any] = dict(zip(lowerCAmelCase__ , range(len(lowerCAmelCase__)))) SCREAMING_SNAKE_CASE_: str = {} SCREAMING_SNAKE_CASE_: Optional[Any] = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions SCREAMING_SNAKE_CASE_: List[Any] = re.compile(R"'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+") @property def _SCREAMING_SNAKE_CASE ( self : int): return len(self.encoder) def _SCREAMING_SNAKE_CASE ( self : int): return dict(self.encoder , **self.added_tokens_encoder) def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : List[str]): if token in self.cache: return self.cache[token] SCREAMING_SNAKE_CASE_: Optional[int] = tuple(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = get_pairs(lowerCAmelCase__) if not pairs: return token while True: SCREAMING_SNAKE_CASE_: int = min(lowerCAmelCase__ , key=lambda lowerCAmelCase__: self.bpe_ranks.get(lowerCAmelCase__ , float("inf"))) if bigram not in self.bpe_ranks: break SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = bigram SCREAMING_SNAKE_CASE_: Optional[int] = [] SCREAMING_SNAKE_CASE_: List[Any] = 0 while i < len(lowerCAmelCase__): try: SCREAMING_SNAKE_CASE_: List[Any] = word.index(lowerCAmelCase__ , lowerCAmelCase__) except ValueError: new_word.extend(word[i:]) break else: new_word.extend(word[i:j]) SCREAMING_SNAKE_CASE_: Tuple = j if word[i] == first and i < len(lowerCAmelCase__) - 1 and word[i + 1] == second: new_word.append(first + second) i += 2 else: new_word.append(word[i]) i += 1 SCREAMING_SNAKE_CASE_: str = tuple(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = new_word if len(lowerCAmelCase__) == 1: break else: SCREAMING_SNAKE_CASE_: Dict = get_pairs(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = " ".join(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = word return word def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Tuple): SCREAMING_SNAKE_CASE_: Optional[Any] = [] for token in re.findall(self.pat , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: str = "".join( self.byte_encoder[b] for b in token.encode("utf-8")) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(lowerCAmelCase__).split(" ")) return bpe_tokens def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Tuple): return self.encoder.get(lowerCAmelCase__ , self.encoder.get(self.unk_token)) def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : Union[str, Any]): return self.decoder.get(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Optional[int]): SCREAMING_SNAKE_CASE_: Any = "".join(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = bytearray([self.byte_decoder[c] for c in text]).decode("utf-8" , errors=self.errors) return text def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[str] = None): if not os.path.isdir(lowerCAmelCase__): logger.error(F"Vocabulary path ({save_directory}) should be a directory") return SCREAMING_SNAKE_CASE_: Any = os.path.join( lowerCAmelCase__ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]) SCREAMING_SNAKE_CASE_: Any = os.path.join( lowerCAmelCase__ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"]) with open(lowerCAmelCase__ , "w" , encoding="utf-8") as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=lowerCAmelCase__ , ensure_ascii=lowerCAmelCase__) + "\n") SCREAMING_SNAKE_CASE_: List[Any] = 0 with open(lowerCAmelCase__ , "w" , encoding="utf-8") as writer: writer.write("#version: 0.2\n") for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda lowerCAmelCase__: kv[1]): if index != token_index: logger.warning( F"Saving vocabulary to {merge_file}: BPE merge indices are not consecutive." " Please check that the tokenizer is not corrupted!") SCREAMING_SNAKE_CASE_: List[Any] = token_index writer.write(" ".join(lowerCAmelCase__) + "\n") index += 1 return vocab_file, merge_file def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] SCREAMING_SNAKE_CASE_: Optional[int] = [self.cls_token_id] SCREAMING_SNAKE_CASE_: Dict = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None , lowerCAmelCase__ : bool = False): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=lowerCAmelCase__ , token_ids_a=lowerCAmelCase__ , already_has_special_tokens=lowerCAmelCase__) if token_ids_a is None: return [1] + ([0] * len(lowerCAmelCase__)) + [1] return [1] + ([0] * len(lowerCAmelCase__)) + [1, 1] + ([0] * len(lowerCAmelCase__)) + [1] def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None): SCREAMING_SNAKE_CASE_: Optional[int] = [self.sep_token_id] SCREAMING_SNAKE_CASE_: int = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep) * [0] def _SCREAMING_SNAKE_CASE ( self : int , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : List[str]=False , **lowerCAmelCase__ : str): SCREAMING_SNAKE_CASE_: List[Any] = kwargs.pop("add_prefix_space" , self.add_prefix_space) if (is_split_into_words or add_prefix_space) and (len(lowerCAmelCase__) > 0 and not text[0].isspace()): SCREAMING_SNAKE_CASE_: Optional[Any] = " " + text return (text, kwargs)
13
1
def A_ ( _UpperCAmelCase ): if not all(char in "01" for char in bin_string ): raise ValueError("Non-binary value was passed to the function" ) if not bin_string: raise ValueError("Empty string was passed to the function" ) SCREAMING_SNAKE_CASE_: Tuple = "" while len(_UpperCAmelCase ) % 3 != 0: SCREAMING_SNAKE_CASE_: Tuple = "0" + bin_string SCREAMING_SNAKE_CASE_: Tuple = [ bin_string[index : index + 3] for index in range(len(_UpperCAmelCase ) ) if index % 3 == 0 ] for bin_group in bin_string_in_3_list: SCREAMING_SNAKE_CASE_: Optional[int] = 0 for index, val in enumerate(_UpperCAmelCase ): oct_val += int(2 ** (2 - index) * int(_UpperCAmelCase ) ) oct_string += str(_UpperCAmelCase ) return oct_string if __name__ == "__main__": from doctest import testmod testmod()
13
import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from transformers.generation import DisjunctiveConstraint @require_torch class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : List[str]): # For consistency across different places the DisjunctiveConstraint is called, # dc.token_ids is a list of integers. It is also initialized only by integers. SCREAMING_SNAKE_CASE_: Optional[Any] = [[1, 2, 4], [1, 2, 3, 4]] SCREAMING_SNAKE_CASE_: Any = DisjunctiveConstraint(lowerCAmelCase__) self.assertTrue(isinstance(dc.token_ids , lowerCAmelCase__)) with self.assertRaises(lowerCAmelCase__): DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]])) with self.assertRaises(lowerCAmelCase__): DisjunctiveConstraint([torch.LongTensor([1, 2, 4]), torch.LongTensor([1, 2, 3, 4, 5])]) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): # We can't have constraints that are complete subsets of another. This leads to a preverse # interpretation of "constraint fulfillment": does generating [1,2,3] fulfill the constraint? # It would mean that it generated [1,2] which fulfills it, but it's in the middle of potentially # fulfilling [1,2,3,4]. If we believe that [1,2,3] does fulfill the constraint, then the algorithm # will necessarily never reach [1,2,3,4], giving users a false sense of control (better to just not allow it). SCREAMING_SNAKE_CASE_: Union[str, Any] = [[1, 2], [1, 2, 3, 4]] with self.assertRaises(lowerCAmelCase__): DisjunctiveConstraint(lowerCAmelCase__) # fails here def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: List[str] = [[1, 2, 3], [1, 2, 4]] SCREAMING_SNAKE_CASE_: Tuple = DisjunctiveConstraint(lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = dc.update(1) SCREAMING_SNAKE_CASE_: Dict = stepped is True and completed is False and reset is False self.assertTrue(lowerCAmelCase__) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = dc.update(2) SCREAMING_SNAKE_CASE_: Optional[Any] = stepped is True and completed is False and reset is False self.assertTrue(lowerCAmelCase__) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1, 2]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = dc.update(3) SCREAMING_SNAKE_CASE_: Tuple = stepped is True and completed is True and reset is False self.assertTrue(lowerCAmelCase__) self.assertTrue(dc.completed) # Completed! self.assertTrue(dc.current_seq == [1, 2, 3]) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]] SCREAMING_SNAKE_CASE_: List[Any] = DisjunctiveConstraint(lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = dc.update(1) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = dc.update(2) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1, 2]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = dc.update(4) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1, 2, 4]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = dc.update(5) self.assertTrue(dc.completed) # Completed! self.assertTrue(dc.current_seq == [1, 2, 4, 5]) dc.reset() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = dc.update(1) self.assertTrue(not dc.completed) self.assertTrue(dc.remaining() == 3) self.assertTrue(dc.current_seq == [1]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = dc.update(2) self.assertTrue(not dc.completed) self.assertTrue(dc.remaining() == 2) self.assertTrue(dc.current_seq == [1, 2]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = dc.update(5) self.assertTrue(dc.completed) # Completed! self.assertTrue(dc.remaining() == 0) self.assertTrue(dc.current_seq == [1, 2, 5])
13
1
import argparse import torch from transformers import BlenderbotConfig, BlenderbotForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase : Optional[Any] = logging.get_logger(__name__) lowerCAmelCase : str = [ ["""attention""", """attn"""], ["""encoder_attention""", """encoder_attn"""], ["""q_lin""", """q_proj"""], ["""k_lin""", """k_proj"""], ["""v_lin""", """v_proj"""], ["""out_lin""", """out_proj"""], ["""norm_embeddings""", """layernorm_embedding"""], ["""position_embeddings""", """embed_positions"""], ["""embeddings""", """embed_tokens"""], ["""ffn.lin""", """fc"""], ] def A_ ( _UpperCAmelCase ): if k == "embeddings.weight": return "shared.weight" for parlai_name, hf_name in PATTERNS: SCREAMING_SNAKE_CASE_: Optional[Any] = k.replace(_UpperCAmelCase , _UpperCAmelCase ) if k.startswith("encoder" ): SCREAMING_SNAKE_CASE_: int = k.replace(".attn" , ".self_attn" ) SCREAMING_SNAKE_CASE_: Any = k.replace("norm1" , "self_attn_layer_norm" ) SCREAMING_SNAKE_CASE_: Any = k.replace("norm2" , "final_layer_norm" ) elif k.startswith("decoder" ): SCREAMING_SNAKE_CASE_: List[Any] = k.replace("norm1" , "self_attn_layer_norm" ) SCREAMING_SNAKE_CASE_: Any = k.replace("norm2" , "encoder_attn_layer_norm" ) SCREAMING_SNAKE_CASE_: int = k.replace("norm3" , "final_layer_norm" ) return k def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Union[str, Any] = [ "model.encoder.layernorm_embedding.weight", "model.encoder.layernorm_embedding.bias", "model.decoder.layernorm_embedding.weight", "model.decoder.layernorm_embedding.bias", ] for k in keys: SCREAMING_SNAKE_CASE_: List[str] = sd.pop(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Union[str, Any] = k.replace("layernorm_embedding" , "layer_norm" ) assert new_k not in sd SCREAMING_SNAKE_CASE_: str = v lowerCAmelCase : str = ["""START"""] @torch.no_grad() def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Optional[Any] = torch.load(_UpperCAmelCase , map_location="cpu" ) SCREAMING_SNAKE_CASE_: Union[str, Any] = model["model"] SCREAMING_SNAKE_CASE_: List[str] = BlenderbotConfig.from_json_file(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Optional[int] = BlenderbotForConditionalGeneration(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Optional[int] = m.model.state_dict().keys() SCREAMING_SNAKE_CASE_: List[str] = [] SCREAMING_SNAKE_CASE_: int = {} for k, v in sd.items(): if k in IGNORE_KEYS: continue SCREAMING_SNAKE_CASE_: str = rename_state_dict_key(_UpperCAmelCase ) if new_k not in valid_keys: failures.append([k, new_k] ) else: SCREAMING_SNAKE_CASE_: Dict = v if cfg.normalize_before: # Blenderbot-3B checkpoints. Rename layernorm_embedding -> layer_norm rename_layernorm_keys(_UpperCAmelCase ) m.model.load_state_dict(_UpperCAmelCase , strict=_UpperCAmelCase ) m.half() m.save_pretrained(_UpperCAmelCase ) if __name__ == "__main__": lowerCAmelCase : Any = argparse.ArgumentParser() # Required parameters parser.add_argument("""--src_path""", type=str, help="""like blenderbot-model.bin""") parser.add_argument("""--save_dir""", default="""hf_blenderbot""", type=str, help="""Where to save converted model.""") parser.add_argument( """--hf_config_json""", default="""blenderbot-3b-config.json""", type=str, help="""Path to config to use""" ) lowerCAmelCase : Tuple = parser.parse_args() convert_parlai_checkpoint(args.src_path, args.save_dir, args.hf_config_json)
13
import pickle import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, XGLMTokenizer, XGLMTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin lowerCAmelCase : Optional[Any] = get_tests_dir("""fixtures/test_sentencepiece.model""") @require_sentencepiece @require_tokenizers class __lowercase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : Optional[Any] = XGLMTokenizer _UpperCAmelCase : List[Any] = XGLMTokenizerFast _UpperCAmelCase : Optional[int] = True _UpperCAmelCase : Tuple = True def _SCREAMING_SNAKE_CASE ( self : Tuple): super().setUp() # We have a SentencePiece fixture for testing SCREAMING_SNAKE_CASE_: List[Any] = XGLMTokenizer(lowerCAmelCase__ , keep_accents=lowerCAmelCase__) tokenizer.save_pretrained(self.tmpdirname) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_: Optional[Any] = "<pad>" SCREAMING_SNAKE_CASE_: int = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCAmelCase__) , lowerCAmelCase__) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCAmelCase__) , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Optional[int] = list(self.get_tokenizer().get_vocab().keys()) self.assertEqual(vocab_keys[0] , "<s>") self.assertEqual(vocab_keys[1] , "<pad>") self.assertEqual(len(lowerCAmelCase__) , 1008) def _SCREAMING_SNAKE_CASE ( self : Any): self.assertEqual(self.get_tokenizer().vocab_size , 1008) def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Optional[int] = XGLMTokenizer(lowerCAmelCase__ , keep_accents=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = tokenizer.tokenize("This is a test") self.assertListEqual(lowerCAmelCase__ , ["▁This", "▁is", "▁a", "▁t", "est"]) self.assertListEqual( tokenizer.convert_tokens_to_ids(lowerCAmelCase__) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) SCREAMING_SNAKE_CASE_: List[str] = tokenizer.tokenize("I was born in 92000, and this is falsé.") self.assertListEqual( lowerCAmelCase__ , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "9", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "é", ".", ] , ) SCREAMING_SNAKE_CASE_: Optional[Any] = tokenizer.convert_tokens_to_ids(lowerCAmelCase__) self.assertListEqual( lowerCAmelCase__ , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) SCREAMING_SNAKE_CASE_: List[Any] = tokenizer.convert_ids_to_tokens(lowerCAmelCase__) self.assertListEqual( lowerCAmelCase__ , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "<unk>", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "<unk>", ".", ] , ) @cached_property def _SCREAMING_SNAKE_CASE ( self : Any): return XGLMTokenizer.from_pretrained("facebook/xglm-564M") def _SCREAMING_SNAKE_CASE ( self : str): with tempfile.NamedTemporaryFile() as f: shutil.copyfile(lowerCAmelCase__ , f.name) SCREAMING_SNAKE_CASE_: Tuple = XGLMTokenizer(f.name , keep_accents=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = pickle.dumps(lowerCAmelCase__) pickle.loads(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : str): if not self.test_rust_tokenizer: return SCREAMING_SNAKE_CASE_: Dict = self.get_tokenizer() SCREAMING_SNAKE_CASE_: List[str] = self.get_rust_tokenizer() SCREAMING_SNAKE_CASE_: Any = "I was born in 92000, and this is falsé." SCREAMING_SNAKE_CASE_: Union[str, Any] = tokenizer.tokenize(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = rust_tokenizer.tokenize(lowerCAmelCase__) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = rust_tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = self.get_rust_tokenizer() SCREAMING_SNAKE_CASE_: str = tokenizer.encode(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = rust_tokenizer.encode(lowerCAmelCase__) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__) @slow def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: Dict = "Hello World!" SCREAMING_SNAKE_CASE_: Union[str, Any] = [2, 3_1227, 4447, 35] self.assertListEqual(lowerCAmelCase__ , self.big_tokenizer.encode(lowerCAmelCase__)) @slow def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Union[str, Any] = ( "This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will" " add words that should not exsist and be tokenized to unk, such as saoneuhaoesuth" ) # fmt: off SCREAMING_SNAKE_CASE_: Optional[Any] = [2, 1018, 67, 11, 1988, 2617, 5631, 278, 11, 3407, 48, 7_1630, 2_8085, 4, 3234, 157, 13, 6, 5, 6, 4, 3526, 768, 15, 659, 57, 298, 3983, 864, 129, 21, 6, 5, 1_3675, 377, 652, 7580, 1_0341, 155, 2817, 422, 1666, 7, 1674, 53, 113, 20_2277, 1_7892, 33, 60, 87, 4, 3234, 157, 61, 2667, 5_2376, 19, 88, 23, 735] # fmt: on self.assertListEqual(lowerCAmelCase__ , self.big_tokenizer.encode(lowerCAmelCase__)) @slow def _SCREAMING_SNAKE_CASE ( self : int): # fmt: off SCREAMING_SNAKE_CASE_: str = { "input_ids": [[2, 10_8825, 1163, 15, 8_8010, 473, 1_5898, 157, 1_3672, 1857, 312, 8, 23_8021, 1163, 53, 1_3672, 1857, 312, 8, 5_3283, 18_2396, 8, 1_8566, 16, 3_6733, 4101, 8, 230, 24_4017, 12_2553, 7, 15, 13_2597, 4, 293, 1_2511, 7610, 4, 3414, 13_2597, 9, 4, 3_2361, 362, 4, 734, 2_8512, 3_2569, 18, 4, 3_2361, 2_6096, 1_4982, 73, 1_8715, 2_1433, 23_5261, 15, 492, 1_2427, 16, 53, 1_8715, 2_1433, 6_5454, 15, 2_3659, 563, 16, 278, 597, 2843, 595, 7931, 18_2396, 6_4186, 22, 886, 595, 13_2981, 53, 2_5540, 3449, 4_3982, 3_9901, 5951, 878, 330, 4, 2_7694, 8_0269, 312, 53, 6517, 1_1780, 611, 2_0408, 5], [2, 6, 13_2597, 67, 4_2897, 33, 592, 8, 16_3729, 2_5540, 361, 13_6997, 10_9514, 17_3230, 7, 501, 60, 10_2913, 196, 5631, 235, 6_3243, 473, 6, 23_1757, 74, 5277, 7905, 53, 3095, 3_7317, 22, 454, 18_3874, 5], [2, 268, 3_1298, 4_6530, 6, 13_2935, 4_3831, 7, 597, 32, 24, 3688, 9865, 5]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] } # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=lowerCAmelCase__ , model_name="facebook/xglm-564M" , padding=lowerCAmelCase__ , )
13
1
import argparse import gdown import numpy as np import torch from huggingface_hub import hf_hub_download from transformers import ( CLIPTokenizer, CLIPTokenizerFast, VideoMAEImageProcessor, XCLIPConfig, XCLIPModel, XCLIPProcessor, XCLIPTextConfig, XCLIPVisionConfig, ) def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: List[str] = XCLIPTextConfig() # derive patch size from model name SCREAMING_SNAKE_CASE_: int = model_name.find("patch" ) SCREAMING_SNAKE_CASE_: List[str] = int(model_name[start_idx + len("patch" ) : start_idx + len("patch" ) + 2] ) SCREAMING_SNAKE_CASE_: Union[str, Any] = XCLIPVisionConfig(patch_size=_UpperCAmelCase , num_frames=_UpperCAmelCase ) if "large" in model_name: SCREAMING_SNAKE_CASE_: Union[str, Any] = 7_68 SCREAMING_SNAKE_CASE_: List[str] = 30_72 SCREAMING_SNAKE_CASE_: str = 12 SCREAMING_SNAKE_CASE_: int = 10_24 SCREAMING_SNAKE_CASE_: List[Any] = 40_96 SCREAMING_SNAKE_CASE_: str = 16 SCREAMING_SNAKE_CASE_: Dict = 24 SCREAMING_SNAKE_CASE_: Optional[Any] = 7_68 SCREAMING_SNAKE_CASE_: Any = 30_72 if model_name == "xclip-large-patch14-16-frames": SCREAMING_SNAKE_CASE_: List[Any] = 3_36 SCREAMING_SNAKE_CASE_: Optional[int] = XCLIPConfig.from_text_vision_configs(_UpperCAmelCase , _UpperCAmelCase ) if "large" in model_name: SCREAMING_SNAKE_CASE_: Optional[int] = 7_68 return config def A_ ( _UpperCAmelCase ): # text encoder if name == "token_embedding.weight": SCREAMING_SNAKE_CASE_: int = name.replace("token_embedding.weight" , "text_model.embeddings.token_embedding.weight" ) if name == "positional_embedding": SCREAMING_SNAKE_CASE_: Any = name.replace("positional_embedding" , "text_model.embeddings.position_embedding.weight" ) if "ln_1" in name: SCREAMING_SNAKE_CASE_: Union[str, Any] = name.replace("ln_1" , "layer_norm1" ) if "ln_2" in name: SCREAMING_SNAKE_CASE_: List[str] = name.replace("ln_2" , "layer_norm2" ) if "c_fc" in name: SCREAMING_SNAKE_CASE_: Optional[Any] = name.replace("c_fc" , "fc1" ) if "c_proj" in name: SCREAMING_SNAKE_CASE_: Dict = name.replace("c_proj" , "fc2" ) if name.startswith("transformer.resblocks" ): SCREAMING_SNAKE_CASE_: Any = name.replace("transformer.resblocks" , "text_model.encoder.layers" ) if "attn.out_proj" in name and "message" not in name: SCREAMING_SNAKE_CASE_: str = name.replace("attn.out_proj" , "self_attn.out_proj" ) if "ln_final" in name: SCREAMING_SNAKE_CASE_: int = name.replace("ln_final" , "text_model.final_layer_norm" ) # visual encoder if name == "visual.class_embedding": SCREAMING_SNAKE_CASE_: Optional[int] = name.replace("visual.class_embedding" , "vision_model.embeddings.class_embedding" ) if name == "visual.positional_embedding": SCREAMING_SNAKE_CASE_: int = name.replace("visual.positional_embedding" , "vision_model.embeddings.position_embedding.weight" ) if name.startswith("visual.transformer.resblocks" ): SCREAMING_SNAKE_CASE_: str = name.replace("visual.transformer.resblocks" , "vision_model.encoder.layers" ) if "visual.conv1" in name: SCREAMING_SNAKE_CASE_: List[Any] = name.replace("visual.conv1" , "vision_model.embeddings.patch_embedding" ) if "visual.ln_pre" in name: SCREAMING_SNAKE_CASE_: Tuple = name.replace("visual.ln_pre" , "vision_model.pre_layernorm" ) if "visual.ln_post" in name: SCREAMING_SNAKE_CASE_: Any = name.replace("visual.ln_post" , "vision_model.post_layernorm" ) if "visual.proj" in name: SCREAMING_SNAKE_CASE_: Optional[Any] = name.replace("visual.proj" , "visual_projection.weight" ) if "text_projection" in name: SCREAMING_SNAKE_CASE_: Dict = name.replace("text_projection" , "text_projection.weight" ) # things on top if "prompts_visual_proj" in name: SCREAMING_SNAKE_CASE_: List[Any] = name.replace("prompts_visual_proj" , "prompts_visual_projection" ) if "prompts_visual_ln" in name: SCREAMING_SNAKE_CASE_: List[Any] = name.replace("prompts_visual_ln" , "prompts_visual_layernorm" ) # mit if name == "mit.positional_embedding": SCREAMING_SNAKE_CASE_: str = name.replace("positional" , "position" ) if name.startswith("mit.resblocks" ): SCREAMING_SNAKE_CASE_: Optional[int] = name.replace("mit.resblocks" , "mit.encoder.layers" ) # prompts generator if name.startswith("prompts_generator.norm" ): SCREAMING_SNAKE_CASE_: Tuple = name.replace("prompts_generator.norm" , "prompts_generator.layernorm" ) return name def A_ ( _UpperCAmelCase , _UpperCAmelCase ): for key in orig_state_dict.copy().keys(): SCREAMING_SNAKE_CASE_: Optional[Any] = orig_state_dict.pop(_UpperCAmelCase ) if "attn.in_proj" in key: SCREAMING_SNAKE_CASE_: Optional[int] = key.split("." ) if key.startswith("visual" ): SCREAMING_SNAKE_CASE_: Optional[int] = key_split[3] SCREAMING_SNAKE_CASE_: Dict = config.vision_config.hidden_size if "message_attn" in key: if "weight" in key: SCREAMING_SNAKE_CASE_: Union[str, Any] = val[ :dim, : ] SCREAMING_SNAKE_CASE_: Dict = val[ dim : dim * 2, : ] SCREAMING_SNAKE_CASE_: Any = val[ -dim:, : ] else: SCREAMING_SNAKE_CASE_: Dict = val[ :dim ] SCREAMING_SNAKE_CASE_: Optional[Any] = val[ dim : dim * 2 ] SCREAMING_SNAKE_CASE_: List[Any] = val[ -dim: ] else: if "weight" in key: SCREAMING_SNAKE_CASE_: List[str] = val[ :dim, : ] SCREAMING_SNAKE_CASE_: str = val[ dim : dim * 2, : ] SCREAMING_SNAKE_CASE_: int = val[ -dim:, : ] else: SCREAMING_SNAKE_CASE_: Dict = val[:dim] SCREAMING_SNAKE_CASE_: int = val[ dim : dim * 2 ] SCREAMING_SNAKE_CASE_: Union[str, Any] = val[-dim:] elif key.startswith("mit" ): SCREAMING_SNAKE_CASE_: List[Any] = key_split[2] SCREAMING_SNAKE_CASE_: Tuple = config.vision_config.mit_hidden_size if "weight" in key: SCREAMING_SNAKE_CASE_: Tuple = val[:dim, :] SCREAMING_SNAKE_CASE_: Dict = val[dim : dim * 2, :] SCREAMING_SNAKE_CASE_: Optional[int] = val[-dim:, :] else: SCREAMING_SNAKE_CASE_: List[Any] = val[:dim] SCREAMING_SNAKE_CASE_: List[str] = val[dim : dim * 2] SCREAMING_SNAKE_CASE_: str = val[-dim:] else: SCREAMING_SNAKE_CASE_: Any = key_split[2] SCREAMING_SNAKE_CASE_: Optional[Any] = config.text_config.hidden_size if "weight" in key: SCREAMING_SNAKE_CASE_: List[str] = val[:dim, :] SCREAMING_SNAKE_CASE_: List[Any] = val[ dim : dim * 2, : ] SCREAMING_SNAKE_CASE_: List[str] = val[-dim:, :] else: SCREAMING_SNAKE_CASE_: Tuple = val[:dim] SCREAMING_SNAKE_CASE_: List[str] = val[ dim : dim * 2 ] SCREAMING_SNAKE_CASE_: Optional[Any] = val[-dim:] else: SCREAMING_SNAKE_CASE_: Union[str, Any] = rename_key(_UpperCAmelCase ) if new_key_name in ["visual_projection.weight", "text_projection.weight"]: SCREAMING_SNAKE_CASE_: Dict = val.T SCREAMING_SNAKE_CASE_: Any = val return orig_state_dict def A_ ( _UpperCAmelCase ): if num_frames == 8: SCREAMING_SNAKE_CASE_: Union[str, Any] = "eating_spaghetti_8_frames.npy" elif num_frames == 16: SCREAMING_SNAKE_CASE_: str = "eating_spaghetti.npy" elif num_frames == 32: SCREAMING_SNAKE_CASE_: Union[str, Any] = "eating_spaghetti_32_frames.npy" SCREAMING_SNAKE_CASE_: List[Any] = hf_hub_download( repo_id="hf-internal-testing/spaghetti-video" , filename=_UpperCAmelCase , repo_type="dataset" , ) SCREAMING_SNAKE_CASE_: str = np.load(_UpperCAmelCase ) return list(_UpperCAmelCase ) def A_ ( _UpperCAmelCase , _UpperCAmelCase=None , _UpperCAmelCase=False ): SCREAMING_SNAKE_CASE_: Optional[Any] = { # fully supervised kinetics-400 checkpoints "xclip-base-patch32": "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_8.pth", "xclip-base-patch32-16-frames": ( "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_16.pth" ), "xclip-base-patch16": "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_8.pth", "xclip-base-patch16-16-frames": ( "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_16.pth" ), "xclip-large-patch14": "https://drive.google.com/u/0/uc?id=1NUOImq0o5DlQTST17iIP3vG7DgmHQuCx&amp;export=download&amp;confirm=t&amp;uuid=b26caedc-88e2-473e-830a-9d158b653cdb", "xclip-large-patch14-16-frames": "https://drive.google.com/u/0/uc?id=1FOYgnJc097OJ4lGwtRCCydQyVPJEOH7d&amp;export=download&amp;confirm=t&amp;uuid=538fa810-e671-4050-b385-9a623f89804f", # fully supervised kinetics-600 checkpoints "xclip-base-patch16-kinetics-600": ( "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_8.pth" ), "xclip-base-patch16-kinetics-600-16-frames": ( "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_16.pth" ), "xclip-large-patch14-kinetics-600": "https://drive.google.com/u/0/uc?id=1FV8C1INuM91sLAN4ImjzePLIlpMSihwV&amp;export=download&amp;confirm=t&amp;uuid=141d4977-4a65-44ae-864f-4b0c19f838be", # few shot "xclip-base-patch16-hmdb-2-shot": ( "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_2.pth" ), "xclip-base-patch16-hmdb-4-shot": ( "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_4.pth" ), "xclip-base-patch16-hmdb-8-shot": ( "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_8.pth" ), "xclip-base-patch16-hmdb-16-shot": ( "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_16.pth" ), "xclip-base-patch16-ucf-2-shot": ( "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_2.pth" ), "xclip-base-patch16-ucf-4-shot": ( "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_4.pth" ), "xclip-base-patch16-ucf-8-shot": ( "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_8.pth" ), "xclip-base-patch16-ucf-16-shot": ( "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_16.pth" ), # zero shot "xclip-base-patch16-zero-shot": "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/zero.pth", } SCREAMING_SNAKE_CASE_: Tuple = model_to_url[model_name] SCREAMING_SNAKE_CASE_: int = 8 if "16-frames" in model_name: SCREAMING_SNAKE_CASE_: int = 16 elif "shot" in model_name: SCREAMING_SNAKE_CASE_: int = 32 SCREAMING_SNAKE_CASE_: int = get_xclip_config(_UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Union[str, Any] = XCLIPModel(_UpperCAmelCase ) model.eval() if "drive" in checkpoint_url: SCREAMING_SNAKE_CASE_: List[str] = "pytorch_model.bin" gdown.cached_download(_UpperCAmelCase , _UpperCAmelCase , quiet=_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Tuple = torch.load(_UpperCAmelCase , map_location="cpu" )["model"] else: SCREAMING_SNAKE_CASE_: str = torch.hub.load_state_dict_from_url(_UpperCAmelCase )["model"] SCREAMING_SNAKE_CASE_: Union[str, Any] = convert_state_dict(_UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[Any] = XCLIPModel(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = model.load_state_dict(_UpperCAmelCase , strict=_UpperCAmelCase ) assert missing_keys == ["text_model.embeddings.position_ids", "vision_model.embeddings.position_ids"] model.eval() SCREAMING_SNAKE_CASE_: Optional[int] = 3_36 if model_name == "xclip-large-patch14-16-frames" else 2_24 SCREAMING_SNAKE_CASE_: int = VideoMAEImageProcessor(size=_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Optional[int] = CLIPTokenizer.from_pretrained("openai/clip-vit-base-patch32" ) SCREAMING_SNAKE_CASE_: Tuple = CLIPTokenizerFast.from_pretrained("openai/clip-vit-base-patch32" ) SCREAMING_SNAKE_CASE_: Optional[Any] = XCLIPProcessor(image_processor=_UpperCAmelCase , tokenizer=_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: int = prepare_video(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[Any] = processor( text=["playing sports", "eating spaghetti", "go shopping"] , videos=_UpperCAmelCase , return_tensors="pt" , padding=_UpperCAmelCase ) print("Shape of pixel values:" , inputs.pixel_values.shape ) with torch.no_grad(): SCREAMING_SNAKE_CASE_: List[str] = model(**_UpperCAmelCase ) # Verify outputs SCREAMING_SNAKE_CASE_: Any = outputs.logits_per_video SCREAMING_SNAKE_CASE_: str = logits_per_video.softmax(dim=1 ) print("Probs:" , _UpperCAmelCase ) # kinetics-400 if model_name == "xclip-base-patch32": SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.tensor([[0.0_0_1_9, 0.9_9_5_1, 0.0_0_3_0]] ) elif model_name == "xclip-base-patch32-16-frames": SCREAMING_SNAKE_CASE_: Any = torch.tensor([[7.0_999e-04, 9.9_883e-01, 4.5_580e-04]] ) elif model_name == "xclip-base-patch16": SCREAMING_SNAKE_CASE_: int = torch.tensor([[0.0_0_8_3, 0.9_6_8_1, 0.0_2_3_6]] ) elif model_name == "xclip-base-patch16-16-frames": SCREAMING_SNAKE_CASE_: Dict = torch.tensor([[7.6_937e-04, 9.9_728e-01, 1.9_473e-03]] ) elif model_name == "xclip-large-patch14": SCREAMING_SNAKE_CASE_: Optional[int] = torch.tensor([[0.0_0_6_2, 0.9_8_6_4, 0.0_0_7_5]] ) elif model_name == "xclip-large-patch14-16-frames": SCREAMING_SNAKE_CASE_: List[Any] = torch.tensor([[3.3_877e-04, 9.9_937e-01, 2.8_888e-04]] ) # kinetics-600 elif model_name == "xclip-base-patch16-kinetics-600": SCREAMING_SNAKE_CASE_: int = torch.tensor([[0.0_5_5_5, 0.8_9_1_4, 0.0_5_3_1]] ) elif model_name == "xclip-base-patch16-kinetics-600-16-frames": SCREAMING_SNAKE_CASE_: int = torch.tensor([[3.8_554e-04, 9.9_929e-01, 3.2_754e-04]] ) elif model_name == "xclip-large-patch14-kinetics-600": SCREAMING_SNAKE_CASE_: List[Any] = torch.tensor([[0.0_0_3_6, 0.9_9_2_0, 0.0_0_4_5]] ) # few shot elif model_name == "xclip-base-patch16-hmdb-2-shot": SCREAMING_SNAKE_CASE_: int = torch.tensor([[7.1_890e-06, 9.9_994e-01, 5.6_559e-05]] ) elif model_name == "xclip-base-patch16-hmdb-4-shot": SCREAMING_SNAKE_CASE_: int = torch.tensor([[1.0_320e-05, 9.9_993e-01, 6.2_435e-05]] ) elif model_name == "xclip-base-patch16-hmdb-8-shot": SCREAMING_SNAKE_CASE_: Optional[Any] = torch.tensor([[4.1_377e-06, 9.9_990e-01, 9.8_386e-05]] ) elif model_name == "xclip-base-patch16-hmdb-16-shot": SCREAMING_SNAKE_CASE_: List[Any] = torch.tensor([[4.1_347e-05, 9.9_962e-01, 3.3_411e-04]] ) elif model_name == "xclip-base-patch16-ucf-2-shot": SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.tensor([[8.5_857e-05, 9.9_928e-01, 6.3_291e-04]] ) elif model_name == "xclip-base-patch16-ucf-4-shot": SCREAMING_SNAKE_CASE_: List[str] = torch.tensor([[8.5_857e-05, 9.9_928e-01, 6.3_291e-04]] ) elif model_name == "xclip-base-patch16-ucf-8-shot": SCREAMING_SNAKE_CASE_: List[str] = torch.tensor([[0.0_0_2_7, 0.9_9_0_4, 0.0_0_7_0]] ) elif model_name == "xclip-base-patch16-ucf-16-shot": SCREAMING_SNAKE_CASE_: Tuple = torch.tensor([[9.8_219e-04, 9.9_593e-01, 3.0_863e-03]] ) # zero shot elif model_name == "xclip-base-patch16-zero-shot": SCREAMING_SNAKE_CASE_: int = torch.tensor([[3.5_082e-04, 9.9_785e-01, 1.7_966e-03]] ) else: raise ValueError(f"Model name {model_name} not supported" ) assert torch.allclose(_UpperCAmelCase , _UpperCAmelCase , atol=1e-3 ) print("Looks ok!" ) if pytorch_dump_folder_path is not None: print(f"Saving model {model_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(_UpperCAmelCase ) if push_to_hub: print("Pushing model, processor and slow tokenizer files to the hub..." ) model.push_to_hub(_UpperCAmelCase , organization="nielsr" ) processor.push_to_hub(_UpperCAmelCase , organization="nielsr" ) slow_tokenizer.push_to_hub(_UpperCAmelCase , organization="nielsr" ) if __name__ == "__main__": lowerCAmelCase : str = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default="""xclip-base-patch32""", type=str, help="""Name of the model.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether or not to push the converted model to the 🤗 hub.""" ) lowerCAmelCase : Dict = parser.parse_args() convert_xclip_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
13
def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , ): SCREAMING_SNAKE_CASE_: Optional[int] = [redshift, radiation_density, matter_density, dark_energy] if any(p < 0 for p in parameters ): raise ValueError("All input parameters must be positive" ) if any(p > 1 for p in parameters[1:4] ): raise ValueError("Relative densities cannot be greater than one" ) else: SCREAMING_SNAKE_CASE_: int = 1 - (matter_density + radiation_density + dark_energy) SCREAMING_SNAKE_CASE_: Dict = ( radiation_density * (redshift + 1) ** 4 + matter_density * (redshift + 1) ** 3 + curvature * (redshift + 1) ** 2 + dark_energy ) SCREAMING_SNAKE_CASE_: Any = hubble_constant * e_a ** (1 / 2) return hubble if __name__ == "__main__": import doctest # run doctest doctest.testmod() # demo LCDM approximation lowerCAmelCase : List[Any] = 0.3 print( hubble_parameter( hubble_constant=68.3, radiation_density=1E-4, matter_density=matter_density, dark_energy=1 - matter_density, redshift=0, ) )
13
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) lowerCAmelCase : List[str] = { """configuration_vision_encoder_decoder""": ["""VisionEncoderDecoderConfig""", """VisionEncoderDecoderOnnxConfig"""] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Any = ["""VisionEncoderDecoderModel"""] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Optional[Any] = ["""TFVisionEncoderDecoderModel"""] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Tuple = ["""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 lowerCAmelCase : Optional[Any] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
13
from typing import Optional, Union import torch from torch import nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention from ...modeling_utils import PreTrainedModel from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging from .configuration_mobilenet_va import MobileNetVaConfig lowerCAmelCase : int = logging.get_logger(__name__) # General docstring lowerCAmelCase : int = """MobileNetV1Config""" # Base docstring lowerCAmelCase : List[Any] = """google/mobilenet_v1_1.0_224""" lowerCAmelCase : Dict = [1, 1024, 7, 7] # Image classification docstring lowerCAmelCase : Union[str, Any] = """google/mobilenet_v1_1.0_224""" lowerCAmelCase : Any = """tabby, tabby cat""" lowerCAmelCase : List[Any] = [ """google/mobilenet_v1_1.0_224""", """google/mobilenet_v1_0.75_192""", # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 ] def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=None ): SCREAMING_SNAKE_CASE_: List[str] = {} if isinstance(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = model.mobilenet_va else: SCREAMING_SNAKE_CASE_: int = model SCREAMING_SNAKE_CASE_: Dict = "MobilenetV1/Conv2d_0/" SCREAMING_SNAKE_CASE_: str = backbone.conv_stem.convolution.weight SCREAMING_SNAKE_CASE_: List[str] = backbone.conv_stem.normalization.bias SCREAMING_SNAKE_CASE_: int = backbone.conv_stem.normalization.weight SCREAMING_SNAKE_CASE_: List[str] = backbone.conv_stem.normalization.running_mean SCREAMING_SNAKE_CASE_: Optional[int] = backbone.conv_stem.normalization.running_var for i in range(13 ): SCREAMING_SNAKE_CASE_: List[str] = i + 1 SCREAMING_SNAKE_CASE_: Optional[int] = i * 2 SCREAMING_SNAKE_CASE_: Any = backbone.layer[pt_index] SCREAMING_SNAKE_CASE_: Any = f"MobilenetV1/Conv2d_{tf_index}_depthwise/" SCREAMING_SNAKE_CASE_: Any = pointer.convolution.weight SCREAMING_SNAKE_CASE_: Any = pointer.normalization.bias SCREAMING_SNAKE_CASE_: str = pointer.normalization.weight SCREAMING_SNAKE_CASE_: Dict = pointer.normalization.running_mean SCREAMING_SNAKE_CASE_: Optional[Any] = pointer.normalization.running_var SCREAMING_SNAKE_CASE_: Tuple = backbone.layer[pt_index + 1] SCREAMING_SNAKE_CASE_: List[str] = f"MobilenetV1/Conv2d_{tf_index}_pointwise/" SCREAMING_SNAKE_CASE_: int = pointer.convolution.weight SCREAMING_SNAKE_CASE_: Any = pointer.normalization.bias SCREAMING_SNAKE_CASE_: Optional[int] = pointer.normalization.weight SCREAMING_SNAKE_CASE_: Optional[Any] = pointer.normalization.running_mean SCREAMING_SNAKE_CASE_: Dict = pointer.normalization.running_var if isinstance(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: List[str] = "MobilenetV1/Logits/Conv2d_1c_1x1/" SCREAMING_SNAKE_CASE_: Optional[Any] = model.classifier.weight SCREAMING_SNAKE_CASE_: Tuple = model.classifier.bias return tf_to_pt_map def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): try: import numpy as np import tensorflow as tf except ImportError: logger.error( "Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see " "https://www.tensorflow.org/install/ for installation instructions." ) raise # Load weights from TF model SCREAMING_SNAKE_CASE_: int = tf.train.list_variables(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: int = {} for name, shape in init_vars: logger.info(f"Loading TF weight {name} with shape {shape}" ) SCREAMING_SNAKE_CASE_: Any = tf.train.load_variable(_UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Union[str, Any] = array # Build TF to PyTorch weights loading map SCREAMING_SNAKE_CASE_: Optional[Any] = _build_tf_to_pytorch_map(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) for name, pointer in tf_to_pt_map.items(): logger.info(f"Importing {name}" ) if name not in tf_weights: logger.info(f"{name} not in tf pre-trained weights, skipping" ) continue SCREAMING_SNAKE_CASE_: int = tf_weights[name] if "depthwise_weights" in name: logger.info("Transposing depthwise" ) SCREAMING_SNAKE_CASE_: int = np.transpose(_UpperCAmelCase , (2, 3, 0, 1) ) elif "weights" in name: logger.info("Transposing" ) if len(pointer.shape ) == 2: # copying into linear layer SCREAMING_SNAKE_CASE_: List[str] = array.squeeze().transpose() else: SCREAMING_SNAKE_CASE_: Any = np.transpose(_UpperCAmelCase , (3, 2, 0, 1) ) if pointer.shape != array.shape: raise ValueError(f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched" ) logger.info(f"Initialize PyTorch weight {name} {array.shape}" ) SCREAMING_SNAKE_CASE_: int = torch.from_numpy(_UpperCAmelCase ) tf_weights.pop(_UpperCAmelCase , _UpperCAmelCase ) tf_weights.pop(name + "/RMSProp" , _UpperCAmelCase ) tf_weights.pop(name + "/RMSProp_1" , _UpperCAmelCase ) tf_weights.pop(name + "/ExponentialMovingAverage" , _UpperCAmelCase ) logger.info(f"Weights not copied to PyTorch model: {', '.join(tf_weights.keys() )}" ) return model def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = features.shape[-2:] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = conv_layer.stride SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = conv_layer.kernel_size if in_height % stride_height == 0: SCREAMING_SNAKE_CASE_: int = max(kernel_height - stride_height , 0 ) else: SCREAMING_SNAKE_CASE_: Tuple = max(kernel_height - (in_height % stride_height) , 0 ) if in_width % stride_width == 0: SCREAMING_SNAKE_CASE_: str = max(kernel_width - stride_width , 0 ) else: SCREAMING_SNAKE_CASE_: Dict = max(kernel_width - (in_width % stride_width) , 0 ) SCREAMING_SNAKE_CASE_: str = pad_along_width // 2 SCREAMING_SNAKE_CASE_: Union[str, Any] = pad_along_width - pad_left SCREAMING_SNAKE_CASE_: int = pad_along_height // 2 SCREAMING_SNAKE_CASE_: Tuple = pad_along_height - pad_top SCREAMING_SNAKE_CASE_: Union[str, Any] = (pad_left, pad_right, pad_top, pad_bottom) return nn.functional.pad(_UpperCAmelCase , _UpperCAmelCase , "constant" , 0.0 ) class __lowercase ( nn.Module ): """simple docstring""" def __init__( self : Optional[int] , lowerCAmelCase__ : MobileNetVaConfig , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : Optional[int] = 1 , lowerCAmelCase__ : Optional[int] = 1 , lowerCAmelCase__ : bool = False , lowerCAmelCase__ : Optional[bool] = True , lowerCAmelCase__ : Optional[bool or str] = True , ): super().__init__() SCREAMING_SNAKE_CASE_: Optional[int] = config if in_channels % groups != 0: raise ValueError(F"Input channels ({in_channels}) are not divisible by {groups} groups.") if out_channels % groups != 0: raise ValueError(F"Output channels ({out_channels}) are not divisible by {groups} groups.") SCREAMING_SNAKE_CASE_: int = 0 if config.tf_padding else int((kernel_size - 1) / 2) SCREAMING_SNAKE_CASE_: Union[str, Any] = nn.Convad( in_channels=lowerCAmelCase__ , out_channels=lowerCAmelCase__ , kernel_size=lowerCAmelCase__ , stride=lowerCAmelCase__ , padding=lowerCAmelCase__ , groups=lowerCAmelCase__ , bias=lowerCAmelCase__ , padding_mode="zeros" , ) if use_normalization: SCREAMING_SNAKE_CASE_: str = nn.BatchNormad( num_features=lowerCAmelCase__ , eps=config.layer_norm_eps , momentum=0.9997 , affine=lowerCAmelCase__ , track_running_stats=lowerCAmelCase__ , ) else: SCREAMING_SNAKE_CASE_: str = None if use_activation: if isinstance(lowerCAmelCase__ , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Dict = ACTaFN[use_activation] elif isinstance(config.hidden_act , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Dict = ACTaFN[config.hidden_act] else: SCREAMING_SNAKE_CASE_: Any = config.hidden_act else: SCREAMING_SNAKE_CASE_: int = None def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : torch.Tensor): if self.config.tf_padding: SCREAMING_SNAKE_CASE_: Union[str, Any] = apply_tf_padding(lowerCAmelCase__ , self.convolution) SCREAMING_SNAKE_CASE_: Optional[int] = self.convolution(lowerCAmelCase__) if self.normalization is not None: SCREAMING_SNAKE_CASE_: int = self.normalization(lowerCAmelCase__) if self.activation is not None: SCREAMING_SNAKE_CASE_: List[Any] = self.activation(lowerCAmelCase__) return features class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : List[str] = MobileNetVaConfig _UpperCAmelCase : List[Any] = load_tf_weights_in_mobilenet_va _UpperCAmelCase : List[Any] = '''mobilenet_v1''' _UpperCAmelCase : int = '''pixel_values''' _UpperCAmelCase : List[Any] = False def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : Union[nn.Linear, nn.Convad]): if isinstance(lowerCAmelCase__ , (nn.Linear, nn.Convad)): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range) if module.bias is not None: module.bias.data.zero_() elif isinstance(lowerCAmelCase__ , nn.BatchNormad): module.bias.data.zero_() module.weight.data.fill_(1.0) lowerCAmelCase : Any = R""" This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior. Parameters: config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights. """ lowerCAmelCase : List[str] = R""" Args: pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`MobileNetV1ImageProcessor.__call__`] for details. output_hidden_states (`bool`, *optional*): Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for more detail. return_dict (`bool`, *optional*): Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. """ @add_start_docstrings( '''The bare MobileNetV1 model outputting raw hidden-states without any specific head on top.''' , UpperCAmelCase_ , ) class __lowercase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Dict , lowerCAmelCase__ : MobileNetVaConfig , lowerCAmelCase__ : bool = True): super().__init__(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = config SCREAMING_SNAKE_CASE_: Union[str, Any] = 32 SCREAMING_SNAKE_CASE_: Dict = max(int(depth * config.depth_multiplier) , config.min_depth) SCREAMING_SNAKE_CASE_: Tuple = MobileNetVaConvLayer( lowerCAmelCase__ , in_channels=config.num_channels , out_channels=lowerCAmelCase__ , kernel_size=3 , stride=2 , ) SCREAMING_SNAKE_CASE_: Optional[int] = [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1] SCREAMING_SNAKE_CASE_: str = nn.ModuleList() for i in range(13): SCREAMING_SNAKE_CASE_: List[Any] = out_channels if strides[i] == 2 or i == 0: depth *= 2 SCREAMING_SNAKE_CASE_: str = max(int(depth * config.depth_multiplier) , config.min_depth) self.layer.append( MobileNetVaConvLayer( lowerCAmelCase__ , in_channels=lowerCAmelCase__ , out_channels=lowerCAmelCase__ , kernel_size=3 , stride=strides[i] , groups=lowerCAmelCase__ , )) self.layer.append( MobileNetVaConvLayer( lowerCAmelCase__ , in_channels=lowerCAmelCase__ , out_channels=lowerCAmelCase__ , kernel_size=1 , )) SCREAMING_SNAKE_CASE_: List[str] = nn.AdaptiveAvgPoolad((1, 1)) if add_pooling_layer else None # Initialize weights and apply final processing self.post_init() def _SCREAMING_SNAKE_CASE ( self : int , lowerCAmelCase__ : str): raise NotImplementedError @add_start_docstrings_to_model_forward(lowerCAmelCase__) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=lowerCAmelCase__ , config_class=_CONFIG_FOR_DOC , modality="vision" , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : Optional[torch.Tensor] = None , lowerCAmelCase__ : Optional[bool] = None , lowerCAmelCase__ : Optional[bool] = None , ): SCREAMING_SNAKE_CASE_: Optional[int] = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) SCREAMING_SNAKE_CASE_: Any = return_dict if return_dict is not None else self.config.use_return_dict if pixel_values is None: raise ValueError("You have to specify pixel_values") SCREAMING_SNAKE_CASE_: Optional[Any] = self.conv_stem(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = () if output_hidden_states else None for i, layer_module in enumerate(self.layer): SCREAMING_SNAKE_CASE_: Tuple = layer_module(lowerCAmelCase__) if output_hidden_states: SCREAMING_SNAKE_CASE_: Optional[int] = all_hidden_states + (hidden_states,) SCREAMING_SNAKE_CASE_: Optional[Any] = hidden_states if self.pooler is not None: SCREAMING_SNAKE_CASE_: int = torch.flatten(self.pooler(lowerCAmelCase__) , start_dim=1) else: SCREAMING_SNAKE_CASE_: List[str] = None if not return_dict: return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None) return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=lowerCAmelCase__ , pooler_output=lowerCAmelCase__ , hidden_states=lowerCAmelCase__ , ) @add_start_docstrings( ''' MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. ''' , UpperCAmelCase_ , ) class __lowercase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Any , lowerCAmelCase__ : MobileNetVaConfig): super().__init__(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = config.num_labels SCREAMING_SNAKE_CASE_: Dict = MobileNetVaModel(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = self.mobilenet_va.layer[-1].convolution.out_channels # Classifier head SCREAMING_SNAKE_CASE_: str = nn.Dropout(config.classifier_dropout_prob , inplace=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = nn.Linear(lowerCAmelCase__ , config.num_labels) if config.num_labels > 0 else nn.Identity() # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(lowerCAmelCase__) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=lowerCAmelCase__ , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : Optional[torch.Tensor] = None , lowerCAmelCase__ : Optional[bool] = None , lowerCAmelCase__ : Optional[torch.Tensor] = None , lowerCAmelCase__ : Optional[bool] = None , ): SCREAMING_SNAKE_CASE_: List[str] = return_dict if return_dict is not None else self.config.use_return_dict SCREAMING_SNAKE_CASE_: List[str] = self.mobilenet_va(lowerCAmelCase__ , output_hidden_states=lowerCAmelCase__ , return_dict=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = outputs.pooler_output if return_dict else outputs[1] SCREAMING_SNAKE_CASE_: Tuple = self.classifier(self.dropout(lowerCAmelCase__)) SCREAMING_SNAKE_CASE_: Optional[int] = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: SCREAMING_SNAKE_CASE_: List[Any] = "regression" elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): SCREAMING_SNAKE_CASE_: int = "single_label_classification" else: SCREAMING_SNAKE_CASE_: str = "multi_label_classification" if self.config.problem_type == "regression": SCREAMING_SNAKE_CASE_: Dict = MSELoss() if self.num_labels == 1: SCREAMING_SNAKE_CASE_: Any = loss_fct(logits.squeeze() , labels.squeeze()) else: SCREAMING_SNAKE_CASE_: int = loss_fct(lowerCAmelCase__ , lowerCAmelCase__) elif self.config.problem_type == "single_label_classification": SCREAMING_SNAKE_CASE_: Any = CrossEntropyLoss() SCREAMING_SNAKE_CASE_: Dict = loss_fct(logits.view(-1 , self.num_labels) , labels.view(-1)) elif self.config.problem_type == "multi_label_classification": SCREAMING_SNAKE_CASE_: Dict = BCEWithLogitsLoss() SCREAMING_SNAKE_CASE_: Dict = loss_fct(lowerCAmelCase__ , lowerCAmelCase__) if not return_dict: SCREAMING_SNAKE_CASE_: int = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return ImageClassifierOutputWithNoAttention( loss=lowerCAmelCase__ , logits=lowerCAmelCase__ , hidden_states=outputs.hidden_states , )
13
1
def A_ ( ): SCREAMING_SNAKE_CASE_: List[Any] = 0 for i in range(1 , 10_01 ): total += i**i return str(_UpperCAmelCase )[-10:] if __name__ == "__main__": print(solution())
13
def A_ ( _UpperCAmelCase , _UpperCAmelCase = False ): if not isinstance(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: str = f"Expected string as input, found {type(_UpperCAmelCase )}" raise ValueError(_UpperCAmelCase ) if not isinstance(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Optional[Any] = f"Expected boolean as use_pascal parameter, found {type(_UpperCAmelCase )}" raise ValueError(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Tuple = input_str.split("_" ) SCREAMING_SNAKE_CASE_: str = 0 if use_pascal else 1 SCREAMING_SNAKE_CASE_: int = words[start_index:] SCREAMING_SNAKE_CASE_: List[str] = [word[0].upper() + word[1:] for word in words_to_capitalize] SCREAMING_SNAKE_CASE_: List[Any] = "" if use_pascal else words[0] return "".join([initial_word, *capitalized_words] ) if __name__ == "__main__": from doctest import testmod testmod()
13
1
import warnings from functools import wraps from typing import Callable def A_ ( _UpperCAmelCase ): @wraps(_UpperCAmelCase ) def _inner_fn(*_UpperCAmelCase , **_UpperCAmelCase ): warnings.warn( (f"'{fn.__name__}' is experimental and might be subject to breaking changes in the future.") , _UpperCAmelCase , ) return fn(*_UpperCAmelCase , **_UpperCAmelCase ) return _inner_fn
13
import os import tempfile import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from torch import nn from transformers import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_inverse_sqrt_schedule, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) def A_ ( _UpperCAmelCase , _UpperCAmelCase=10 ): SCREAMING_SNAKE_CASE_: Union[str, Any] = [] for _ in range(_UpperCAmelCase ): lrs.append(scheduler.get_lr()[0] ) scheduler.step() return lrs def A_ ( _UpperCAmelCase , _UpperCAmelCase=10 ): SCREAMING_SNAKE_CASE_: List[str] = [] for step in range(_UpperCAmelCase ): lrs.append(scheduler.get_lr()[0] ) scheduler.step() if step == num_steps // 2: with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE_: Optional[int] = os.path.join(_UpperCAmelCase , "schedule.bin" ) torch.save(scheduler.state_dict() , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Optional[Any] = torch.load(_UpperCAmelCase ) scheduler.load_state_dict(_UpperCAmelCase ) return lrs @require_torch class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : int , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Tuple): self.assertEqual(len(lowerCAmelCase__) , len(lowerCAmelCase__)) for a, b in zip(lowerCAmelCase__ , lowerCAmelCase__): self.assertAlmostEqual(lowerCAmelCase__ , lowerCAmelCase__ , delta=lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.tensor([0.1, -0.2, -0.1] , requires_grad=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = torch.tensor([0.4, 0.2, -0.5]) SCREAMING_SNAKE_CASE_: Optional[Any] = nn.MSELoss() # No warmup, constant schedule, no gradient clipping SCREAMING_SNAKE_CASE_: int = AdamW(params=[w] , lr=2E-1 , weight_decay=0.0) for _ in range(100): SCREAMING_SNAKE_CASE_: Dict = criterion(lowerCAmelCase__ , lowerCAmelCase__) loss.backward() optimizer.step() w.grad.detach_() # No zero_grad() function on simple tensors. we do it ourselves. w.grad.zero_() self.assertListAlmostEqual(w.tolist() , [0.4, 0.2, -0.5] , tol=1E-2) def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.tensor([0.1, -0.2, -0.1] , requires_grad=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = torch.tensor([0.4, 0.2, -0.5]) SCREAMING_SNAKE_CASE_: Any = nn.MSELoss() # No warmup, constant schedule, no gradient clipping SCREAMING_SNAKE_CASE_: int = Adafactor( params=[w] , lr=1E-2 , eps=(1E-30, 1E-3) , clip_threshold=1.0 , decay_rate=-0.8 , betaa=lowerCAmelCase__ , weight_decay=0.0 , relative_step=lowerCAmelCase__ , scale_parameter=lowerCAmelCase__ , warmup_init=lowerCAmelCase__ , ) for _ in range(1000): SCREAMING_SNAKE_CASE_: List[Any] = criterion(lowerCAmelCase__ , lowerCAmelCase__) loss.backward() optimizer.step() w.grad.detach_() # No zero_grad() function on simple tensors. we do it ourselves. w.grad.zero_() self.assertListAlmostEqual(w.tolist() , [0.4, 0.2, -0.5] , tol=1E-2) @require_torch class __lowercase ( unittest.TestCase ): """simple docstring""" _UpperCAmelCase : Union[str, Any] = nn.Linear(50 , 50 ) if is_torch_available() else None _UpperCAmelCase : List[Any] = AdamW(m.parameters() , lr=10.0 ) if is_torch_available() else None _UpperCAmelCase : Optional[Any] = 10 def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : Optional[Any]=None): self.assertEqual(len(lowerCAmelCase__) , len(lowerCAmelCase__)) for a, b in zip(lowerCAmelCase__ , lowerCAmelCase__): self.assertAlmostEqual(lowerCAmelCase__ , lowerCAmelCase__ , delta=lowerCAmelCase__ , msg=lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Dict = {"num_warmup_steps": 2, "num_training_steps": 10} # schedulers doct format # function: (sched_args_dict, expected_learning_rates) SCREAMING_SNAKE_CASE_: Dict = { get_constant_schedule: ({}, [10.0] * self.num_steps), get_constant_schedule_with_warmup: ( {"num_warmup_steps": 4}, [0.0, 2.5, 5.0, 7.5, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0], ), get_linear_schedule_with_warmup: ( {**common_kwargs}, [0.0, 5.0, 10.0, 8.75, 7.5, 6.25, 5.0, 3.75, 2.5, 1.25], ), get_cosine_schedule_with_warmup: ( {**common_kwargs}, [0.0, 5.0, 10.0, 9.61, 8.53, 6.91, 5.0, 3.08, 1.46, 0.38], ), get_cosine_with_hard_restarts_schedule_with_warmup: ( {**common_kwargs, "num_cycles": 2}, [0.0, 5.0, 10.0, 8.53, 5.0, 1.46, 10.0, 8.53, 5.0, 1.46], ), get_polynomial_decay_schedule_with_warmup: ( {**common_kwargs, "power": 2.0, "lr_end": 1E-7}, [0.0, 5.0, 10.0, 7.656, 5.625, 3.906, 2.5, 1.406, 0.625, 0.156], ), get_inverse_sqrt_schedule: ( {"num_warmup_steps": 2}, [0.0, 5.0, 10.0, 8.165, 7.071, 6.325, 5.774, 5.345, 5.0, 4.714], ), } for scheduler_func, data in scheds.items(): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = data SCREAMING_SNAKE_CASE_: List[Any] = scheduler_func(self.optimizer , **lowerCAmelCase__) self.assertEqual(len([scheduler.get_lr()[0]]) , 1) SCREAMING_SNAKE_CASE_: int = unwrap_schedule(lowerCAmelCase__ , self.num_steps) self.assertListAlmostEqual( lowerCAmelCase__ , lowerCAmelCase__ , tol=1E-2 , msg=F"failed for {scheduler_func} in normal scheduler" , ) SCREAMING_SNAKE_CASE_: List[str] = scheduler_func(self.optimizer , **lowerCAmelCase__) if scheduler_func.__name__ != "get_constant_schedule": LambdaScheduleWrapper.wrap_scheduler(lowerCAmelCase__) # wrap to test picklability of the schedule SCREAMING_SNAKE_CASE_: Tuple = unwrap_and_save_reload_schedule(lowerCAmelCase__ , self.num_steps) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ , msg=F"failed for {scheduler_func} in save and reload") class __lowercase : """simple docstring""" def __init__( self : str , lowerCAmelCase__ : List[str]): SCREAMING_SNAKE_CASE_: List[Any] = fn def __call__( self : Optional[int] , *lowerCAmelCase__ : List[Any] , **lowerCAmelCase__ : Tuple): return self.fn(*lowerCAmelCase__ , **lowerCAmelCase__) @classmethod def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : str): SCREAMING_SNAKE_CASE_: str = list(map(self , scheduler.lr_lambdas))
13
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_torch_available, ) lowerCAmelCase : str = { """configuration_speecht5""": [ """SPEECHT5_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SPEECHT5_PRETRAINED_HIFIGAN_CONFIG_ARCHIVE_MAP""", """SpeechT5Config""", """SpeechT5HifiGanConfig""", ], """feature_extraction_speecht5""": ["""SpeechT5FeatureExtractor"""], """processing_speecht5""": ["""SpeechT5Processor"""], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Optional[int] = ["""SpeechT5Tokenizer"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : int = [ """SPEECHT5_PRETRAINED_MODEL_ARCHIVE_LIST""", """SpeechT5ForSpeechToText""", """SpeechT5ForSpeechToSpeech""", """SpeechT5ForTextToSpeech""", """SpeechT5Model""", """SpeechT5PreTrainedModel""", """SpeechT5HifiGan""", ] if TYPE_CHECKING: from .configuration_speechta import ( SPEECHT5_PRETRAINED_CONFIG_ARCHIVE_MAP, SPEECHT5_PRETRAINED_HIFIGAN_CONFIG_ARCHIVE_MAP, SpeechTaConfig, SpeechTaHifiGanConfig, ) from .feature_extraction_speechta import SpeechTaFeatureExtractor from .processing_speechta import SpeechTaProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_speechta import SpeechTaTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speechta import ( SPEECHT5_PRETRAINED_MODEL_ARCHIVE_LIST, SpeechTaForSpeechToSpeech, SpeechTaForSpeechToText, SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaModel, SpeechTaPreTrainedModel, ) else: import sys lowerCAmelCase : str = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
13
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, Features, Value from .base import TaskTemplate @dataclass(frozen=UpperCAmelCase_ ) class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : str = field(default='''automatic-speech-recognition''' , metadata={'''include_in_asdict_even_if_is_default''': True} ) _UpperCAmelCase : ClassVar[Features] = Features({'''audio''': Audio()} ) _UpperCAmelCase : ClassVar[Features] = Features({'''transcription''': Value('''string''' )} ) _UpperCAmelCase : str = "audio" _UpperCAmelCase : str = "transcription" def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int): if self.audio_column not in features: raise ValueError(F"Column {self.audio_column} is not present in features.") if not isinstance(features[self.audio_column] , lowerCAmelCase__): raise ValueError(F"Column {self.audio_column} is not an Audio type.") SCREAMING_SNAKE_CASE_: Tuple = copy.deepcopy(self) SCREAMING_SNAKE_CASE_: Optional[int] = self.input_schema.copy() SCREAMING_SNAKE_CASE_: Dict = features[self.audio_column] SCREAMING_SNAKE_CASE_: int = input_schema return task_template @property def _SCREAMING_SNAKE_CASE ( self : int): return {self.audio_column: "audio", self.transcription_column: "transcription"}
13
1
import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_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, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __lowercase : """simple docstring""" def __init__( self : Any , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Any=13 , lowerCAmelCase__ : Tuple=30 , lowerCAmelCase__ : List[str]=2 , lowerCAmelCase__ : int=3 , lowerCAmelCase__ : Optional[int]=True , lowerCAmelCase__ : List[str]=True , lowerCAmelCase__ : str=32 , lowerCAmelCase__ : Any=5 , lowerCAmelCase__ : str=4 , lowerCAmelCase__ : int=37 , lowerCAmelCase__ : Optional[Any]="gelu" , lowerCAmelCase__ : Optional[int]=0.1 , lowerCAmelCase__ : Dict=0.1 , lowerCAmelCase__ : Tuple=10 , lowerCAmelCase__ : Optional[Any]=0.02 , lowerCAmelCase__ : List[str]=None , lowerCAmelCase__ : Union[str, Any]=2 , ): SCREAMING_SNAKE_CASE_: str = parent SCREAMING_SNAKE_CASE_: Optional[Any] = batch_size SCREAMING_SNAKE_CASE_: str = image_size SCREAMING_SNAKE_CASE_: Tuple = patch_size SCREAMING_SNAKE_CASE_: int = num_channels SCREAMING_SNAKE_CASE_: List[str] = is_training SCREAMING_SNAKE_CASE_: str = use_labels SCREAMING_SNAKE_CASE_: int = hidden_size SCREAMING_SNAKE_CASE_: List[Any] = num_hidden_layers SCREAMING_SNAKE_CASE_: Union[str, Any] = num_attention_heads SCREAMING_SNAKE_CASE_: Any = intermediate_size SCREAMING_SNAKE_CASE_: str = hidden_act SCREAMING_SNAKE_CASE_: str = hidden_dropout_prob SCREAMING_SNAKE_CASE_: List[str] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_: int = type_sequence_label_size SCREAMING_SNAKE_CASE_: Dict = initializer_range SCREAMING_SNAKE_CASE_: Dict = scope SCREAMING_SNAKE_CASE_: Dict = encoder_stride # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) SCREAMING_SNAKE_CASE_: List[Any] = (image_size // patch_size) ** 2 SCREAMING_SNAKE_CASE_: Dict = num_patches + 1 def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]) SCREAMING_SNAKE_CASE_: str = None if self.use_labels: SCREAMING_SNAKE_CASE_: Union[str, Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size) SCREAMING_SNAKE_CASE_: Optional[Any] = self.get_config() return config, pixel_values, labels def _SCREAMING_SNAKE_CASE ( self : Optional[int]): return ViTConfig( 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 , encoder_stride=self.encoder_stride , ) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : Tuple): SCREAMING_SNAKE_CASE_: Union[str, Any] = ViTModel(config=lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: Optional[int] = model(lowerCAmelCase__) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Dict): SCREAMING_SNAKE_CASE_: Optional[int] = ViTForMaskedImageModeling(config=lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: str = model(lowerCAmelCase__) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size)) # test greyscale images SCREAMING_SNAKE_CASE_: Dict = 1 SCREAMING_SNAKE_CASE_: List[str] = ViTForMaskedImageModeling(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: List[str] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]) SCREAMING_SNAKE_CASE_: str = model(lowerCAmelCase__) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size)) def _SCREAMING_SNAKE_CASE ( self : Any , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Union[str, Any]): SCREAMING_SNAKE_CASE_: Tuple = self.type_sequence_label_size SCREAMING_SNAKE_CASE_: List[str] = ViTForImageClassification(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: Any = model(lowerCAmelCase__ , labels=lowerCAmelCase__) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) # test greyscale images SCREAMING_SNAKE_CASE_: Union[str, Any] = 1 SCREAMING_SNAKE_CASE_: List[str] = ViTForImageClassification(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: Union[str, Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]) SCREAMING_SNAKE_CASE_: Dict = model(lowerCAmelCase__) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ): List[str] = config_and_inputs SCREAMING_SNAKE_CASE_: Optional[Any] = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class __lowercase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : List[Any] = ( ( ViTModel, ViTForImageClassification, ViTForMaskedImageModeling, ) if is_torch_available() else () ) _UpperCAmelCase : Tuple = ( {'''feature-extraction''': ViTModel, '''image-classification''': ViTForImageClassification} if is_torch_available() else {} ) _UpperCAmelCase : List[str] = True _UpperCAmelCase : List[Any] = False _UpperCAmelCase : Optional[Any] = False _UpperCAmelCase : Tuple = False def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: List[str] = ViTModelTester(self) SCREAMING_SNAKE_CASE_: Union[str, Any] = ConfigTester(self , config_class=lowerCAmelCase__ , has_text_modality=lowerCAmelCase__ , hidden_size=37) def _SCREAMING_SNAKE_CASE ( self : Any): self.config_tester.run_common_tests() @unittest.skip(reason="ViT does not use inputs_embeds") def _SCREAMING_SNAKE_CASE ( self : str): pass def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_: Dict = model_class(lowerCAmelCase__) self.assertIsInstance(model.get_input_embeddings() , (nn.Module)) SCREAMING_SNAKE_CASE_: List[Any] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCAmelCase__ , nn.Linear)) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_: List[Any] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = inspect.signature(model.forward) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE_: Optional[Any] = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE_: Optional[int] = ["pixel_values"] self.assertListEqual(arg_names[:1] , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__) @slow def _SCREAMING_SNAKE_CASE ( self : int): for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_: Union[str, Any] = ViTModel.from_pretrained(lowerCAmelCase__) self.assertIsNotNone(lowerCAmelCase__) def A_ ( ): SCREAMING_SNAKE_CASE_: List[Any] = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class __lowercase ( unittest.TestCase ): """simple docstring""" @cached_property def _SCREAMING_SNAKE_CASE ( self : int): return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224") if is_vision_available() else None @slow def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: int = ViTForImageClassification.from_pretrained("google/vit-base-patch16-224").to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = self.default_image_processor SCREAMING_SNAKE_CASE_: str = prepare_img() SCREAMING_SNAKE_CASE_: Optional[Any] = image_processor(images=lowerCAmelCase__ , return_tensors="pt").to(lowerCAmelCase__) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_: Optional[int] = model(**lowerCAmelCase__) # verify the logits SCREAMING_SNAKE_CASE_: Any = torch.Size((1, 1000)) self.assertEqual(outputs.logits.shape , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = torch.tensor([-0.2744, 0.8215, -0.0836]).to(lowerCAmelCase__) self.assertTrue(torch.allclose(outputs.logits[0, :3] , lowerCAmelCase__ , atol=1E-4)) @slow def _SCREAMING_SNAKE_CASE ( self : List[Any]): # ViT models have an `interpolate_pos_encoding` argument in their forward method, # allowing to interpolate the pre-trained position embeddings in order to use # the model on higher resolutions. The DINO model by Facebook AI leverages this # to visualize self-attention on higher resolution images. SCREAMING_SNAKE_CASE_: str = ViTModel.from_pretrained("facebook/dino-vits8").to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = ViTImageProcessor.from_pretrained("facebook/dino-vits8" , size=480) SCREAMING_SNAKE_CASE_: List[Any] = prepare_img() SCREAMING_SNAKE_CASE_: List[Any] = image_processor(images=lowerCAmelCase__ , return_tensors="pt") SCREAMING_SNAKE_CASE_: int = inputs.pixel_values.to(lowerCAmelCase__) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_: Optional[int] = model(lowerCAmelCase__ , interpolate_pos_encoding=lowerCAmelCase__) # verify the logits SCREAMING_SNAKE_CASE_: Tuple = torch.Size((1, 3601, 384)) self.assertEqual(outputs.last_hidden_state.shape , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.tensor( [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]]).to(lowerCAmelCase__) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , lowerCAmelCase__ , atol=1E-4)) @slow @require_accelerate @require_torch_gpu def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Dict = ViTModel.from_pretrained("facebook/dino-vits8" , torch_dtype=torch.floataa , device_map="auto") SCREAMING_SNAKE_CASE_: int = self.default_image_processor SCREAMING_SNAKE_CASE_: Union[str, Any] = prepare_img() SCREAMING_SNAKE_CASE_: Dict = image_processor(images=lowerCAmelCase__ , return_tensors="pt") SCREAMING_SNAKE_CASE_: str = inputs.pixel_values.to(lowerCAmelCase__) # forward pass to make sure inference works in fp16 with torch.no_grad(): SCREAMING_SNAKE_CASE_: str = model(lowerCAmelCase__)
13
import unittest import numpy as np from transformers import is_flax_available from transformers.testing_utils import require_flax from ..test_modeling_flax_common import ids_tensor if is_flax_available(): import jax import jax.numpy as jnp from transformers.generation import ( FlaxForcedBOSTokenLogitsProcessor, FlaxForcedEOSTokenLogitsProcessor, FlaxLogitsProcessorList, FlaxMinLengthLogitsProcessor, FlaxTemperatureLogitsWarper, FlaxTopKLogitsWarper, FlaxTopPLogitsWarper, ) @require_flax class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : int , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: str = jnp.ones((batch_size, length)) / length return scores def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Dict = None SCREAMING_SNAKE_CASE_: str = 20 SCREAMING_SNAKE_CASE_: List[Any] = self._get_uniform_logits(batch_size=2 , length=lowerCAmelCase__) # tweak scores to not be uniform anymore SCREAMING_SNAKE_CASE_: List[str] = scores.at[1, 5].set((1 / length) + 0.1) # peak, 1st batch SCREAMING_SNAKE_CASE_: Any = scores.at[1, 10].set((1 / length) - 0.4) # valley, 1st batch # compute softmax SCREAMING_SNAKE_CASE_: Dict = jax.nn.softmax(lowerCAmelCase__ , axis=-1) SCREAMING_SNAKE_CASE_: Optional[Any] = FlaxTemperatureLogitsWarper(temperature=0.5) SCREAMING_SNAKE_CASE_: List[str] = FlaxTemperatureLogitsWarper(temperature=1.3) SCREAMING_SNAKE_CASE_: str = jax.nn.softmax(temp_dist_warper_sharper(lowerCAmelCase__ , scores.copy() , cur_len=lowerCAmelCase__) , axis=-1) SCREAMING_SNAKE_CASE_: int = jax.nn.softmax(temp_dist_warper_smoother(lowerCAmelCase__ , scores.copy() , cur_len=lowerCAmelCase__) , axis=-1) # uniform distribution stays uniform self.assertTrue(jnp.allclose(probs[0, :] , warped_prob_sharp[0, :] , atol=1E-3)) self.assertTrue(jnp.allclose(probs[0, :] , warped_prob_smooth[0, :] , atol=1E-3)) # sharp peaks get higher, valleys get lower self.assertLess(probs[1, :].max() , warped_prob_sharp[1, :].max()) self.assertGreater(probs[1, :].min() , warped_prob_sharp[1, :].min()) # smooth peaks get lower, valleys get higher self.assertGreater(probs[1, :].max() , warped_prob_smooth[1, :].max()) self.assertLess(probs[1, :].min() , warped_prob_smooth[1, :].min()) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: List[str] = None SCREAMING_SNAKE_CASE_: str = 10 SCREAMING_SNAKE_CASE_: Tuple = 2 # create ramp distribution SCREAMING_SNAKE_CASE_: Optional[Any] = np.broadcast_to(np.arange(lowerCAmelCase__)[None, :] , (batch_size, vocab_size)).copy() SCREAMING_SNAKE_CASE_: Dict = ramp_logits[1:, : vocab_size // 2] + vocab_size SCREAMING_SNAKE_CASE_: Union[str, Any] = FlaxTopKLogitsWarper(3) SCREAMING_SNAKE_CASE_: Dict = top_k_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) # check that correct tokens are filtered self.assertListEqual(jnp.isinf(scores[0]).tolist() , 7 * [True] + 3 * [False]) self.assertListEqual(jnp.isinf(scores[1]).tolist() , 2 * [True] + 3 * [False] + 5 * [True]) # check special case SCREAMING_SNAKE_CASE_: Any = 5 SCREAMING_SNAKE_CASE_: str = FlaxTopKLogitsWarper(top_k=1 , filter_value=0.0 , min_tokens_to_keep=3) SCREAMING_SNAKE_CASE_: Any = np.broadcast_to(np.arange(lowerCAmelCase__)[None, :] , (batch_size, length)).copy() SCREAMING_SNAKE_CASE_: Any = top_k_warp_safety_check(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) # min_tokens overwrites k: 3 tokens are kept => 2 tokens are nullified self.assertListEqual((scores == 0.0).sum(axis=-1).tolist() , [2, 2]) def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Tuple = None SCREAMING_SNAKE_CASE_: Dict = 10 SCREAMING_SNAKE_CASE_: Dict = 2 # create distribution and take log (inverse to Softmax as taken in TopPLogitsWarper) SCREAMING_SNAKE_CASE_: Tuple = np.log(np.array([[0.3, 0.1, 0.1, 0.5], [0.15, 0.3, 0.3, 0.25]])) SCREAMING_SNAKE_CASE_: int = FlaxTopPLogitsWarper(0.8) SCREAMING_SNAKE_CASE_: Optional[Any] = np.exp(top_p_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__)) # dist should be filtered to keep min num values so that sum is >= top_p # exp (-inf) => 0 SCREAMING_SNAKE_CASE_: Dict = np.array([[0.3, 0.0, 0.0, 0.5], [0.0, 0.3, 0.3, 0.25]]) self.assertTrue(np.allclose(lowerCAmelCase__ , lowerCAmelCase__ , atol=1E-3)) # check edge cases with negative and extreme logits SCREAMING_SNAKE_CASE_: Union[str, Any] = np.broadcast_to(np.arange(lowerCAmelCase__)[None, :] , (batch_size, vocab_size)).copy() - ( vocab_size // 2 ) # make ramp_logits more extreme SCREAMING_SNAKE_CASE_: Dict = ramp_logits[1] * 100.0 # make sure at least 2 tokens are kept SCREAMING_SNAKE_CASE_: str = FlaxTopPLogitsWarper(0.9 , min_tokens_to_keep=2 , filter_value=0.0) SCREAMING_SNAKE_CASE_: Any = top_p_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) # first batch should keep three tokens, second batch would keep only 1, but due to `min_tokens_to_keep=2` keeps 2. self.assertListEqual((filtered_dist != 0.0).sum(axis=-1).tolist() , [3, 2]) def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Tuple = 20 SCREAMING_SNAKE_CASE_: List[str] = 4 SCREAMING_SNAKE_CASE_: Optional[int] = 0 SCREAMING_SNAKE_CASE_: str = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=lowerCAmelCase__) # check that min length is applied at length 5 SCREAMING_SNAKE_CASE_: str = ids_tensor((batch_size, 20) , vocab_size=20) SCREAMING_SNAKE_CASE_: int = 5 SCREAMING_SNAKE_CASE_: List[Any] = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = min_dist_processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) self.assertListEqual(scores_before_min_length[:, eos_token_id].tolist() , 4 * [-float("inf")]) # check that min length is not applied anymore at length 15 SCREAMING_SNAKE_CASE_: List[str] = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = 15 SCREAMING_SNAKE_CASE_: Any = min_dist_processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) self.assertFalse(jnp.isinf(lowerCAmelCase__).any()) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: int = 20 SCREAMING_SNAKE_CASE_: str = 4 SCREAMING_SNAKE_CASE_: List[Any] = 0 SCREAMING_SNAKE_CASE_: Optional[Any] = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=lowerCAmelCase__) # check that all scores are -inf except the bos_token_id score SCREAMING_SNAKE_CASE_: int = ids_tensor((batch_size, 1) , vocab_size=20) SCREAMING_SNAKE_CASE_: List[str] = 1 SCREAMING_SNAKE_CASE_: Union[str, Any] = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = logits_processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) self.assertTrue(jnp.isneginf(scores[:, bos_token_id + 1 :]).all()) self.assertListEqual(scores[:, bos_token_id].tolist() , 4 * [0]) # score for bos_token_id shold be zero # check that bos_token_id is not forced if current length is greater than 1 SCREAMING_SNAKE_CASE_: List[Any] = 3 SCREAMING_SNAKE_CASE_: Optional[Any] = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = logits_processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) self.assertFalse(jnp.isinf(lowerCAmelCase__).any()) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Any = 20 SCREAMING_SNAKE_CASE_: Optional[Any] = 4 SCREAMING_SNAKE_CASE_: Dict = 0 SCREAMING_SNAKE_CASE_: List[Any] = 5 SCREAMING_SNAKE_CASE_: Union[str, Any] = FlaxForcedEOSTokenLogitsProcessor(max_length=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__) # check that all scores are -inf except the eos_token_id when max_length is reached SCREAMING_SNAKE_CASE_: List[Any] = ids_tensor((batch_size, 4) , vocab_size=20) SCREAMING_SNAKE_CASE_: Optional[int] = 4 SCREAMING_SNAKE_CASE_: Dict = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = logits_processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) self.assertTrue(jnp.isneginf(scores[:, eos_token_id + 1 :]).all()) self.assertListEqual(scores[:, eos_token_id].tolist() , 4 * [0]) # score for eos_token_id should be zero # check that eos_token_id is not forced if max_length is not reached SCREAMING_SNAKE_CASE_: List[str] = 3 SCREAMING_SNAKE_CASE_: str = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = logits_processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) self.assertFalse(jnp.isinf(lowerCAmelCase__).any()) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: int = 4 SCREAMING_SNAKE_CASE_: List[Any] = 10 SCREAMING_SNAKE_CASE_: int = 15 SCREAMING_SNAKE_CASE_: Dict = 2 SCREAMING_SNAKE_CASE_: int = 1 SCREAMING_SNAKE_CASE_: List[Any] = 15 # dummy input_ids and scores SCREAMING_SNAKE_CASE_: int = ids_tensor((batch_size, sequence_length) , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = input_ids.copy() SCREAMING_SNAKE_CASE_: List[Any] = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = scores.copy() # instantiate all dist processors SCREAMING_SNAKE_CASE_: Optional[int] = FlaxTemperatureLogitsWarper(temperature=0.5) SCREAMING_SNAKE_CASE_: Tuple = FlaxTopKLogitsWarper(3) SCREAMING_SNAKE_CASE_: Optional[int] = FlaxTopPLogitsWarper(0.8) # instantiate all logits processors SCREAMING_SNAKE_CASE_: Optional[int] = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = FlaxForcedEOSTokenLogitsProcessor(max_length=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = 10 # no processor list SCREAMING_SNAKE_CASE_: Dict = temp_dist_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = top_k_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = top_p_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = min_dist_proc(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = bos_dist_proc(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = eos_dist_proc(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) # with processor list SCREAMING_SNAKE_CASE_: str = FlaxLogitsProcessorList( [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc]) SCREAMING_SNAKE_CASE_: Tuple = processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) # scores should be equal self.assertTrue(jnp.allclose(lowerCAmelCase__ , lowerCAmelCase__ , atol=1E-3)) # input_ids should never be changed self.assertListEqual(input_ids.tolist() , input_ids_comp.tolist()) def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_: Optional[int] = 4 SCREAMING_SNAKE_CASE_: int = 10 SCREAMING_SNAKE_CASE_: List[str] = 15 SCREAMING_SNAKE_CASE_: List[Any] = 2 SCREAMING_SNAKE_CASE_: Union[str, Any] = 1 SCREAMING_SNAKE_CASE_: str = 15 # dummy input_ids and scores SCREAMING_SNAKE_CASE_: Tuple = ids_tensor((batch_size, sequence_length) , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = input_ids.copy() SCREAMING_SNAKE_CASE_: List[Any] = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = scores.copy() # instantiate all dist processors SCREAMING_SNAKE_CASE_: Dict = FlaxTemperatureLogitsWarper(temperature=0.5) SCREAMING_SNAKE_CASE_: Union[str, Any] = FlaxTopKLogitsWarper(3) SCREAMING_SNAKE_CASE_: Dict = FlaxTopPLogitsWarper(0.8) # instantiate all logits processors SCREAMING_SNAKE_CASE_: int = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = FlaxForcedEOSTokenLogitsProcessor(max_length=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = 10 # no processor list def run_no_processor_list(lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Dict): SCREAMING_SNAKE_CASE_: Any = temp_dist_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = top_k_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = top_p_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = min_dist_proc(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = bos_dist_proc(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = eos_dist_proc(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) return scores # with processor list def run_processor_list(lowerCAmelCase__ : int , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Union[str, Any]): SCREAMING_SNAKE_CASE_: List[str] = FlaxLogitsProcessorList( [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc]) SCREAMING_SNAKE_CASE_: Dict = processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) return scores SCREAMING_SNAKE_CASE_: str = jax.jit(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = jax.jit(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = jitted_run_no_processor_list(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = jitted_run_processor_list(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) # scores should be equal self.assertTrue(jnp.allclose(lowerCAmelCase__ , lowerCAmelCase__ , atol=1E-3)) # input_ids should never be changed self.assertListEqual(input_ids.tolist() , input_ids_comp.tolist())
13
1
from __future__ import annotations import os import tempfile import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers import is_tensorflow_text_available, is_tf_available from transformers.testing_utils import require_tensorflow_text, require_tf, slow from ..test_modeling_tf_common import floats_tensor from .test_framework_agnostic import GenerationIntegrationTestsMixin if is_tf_available(): import tensorflow as tf from transformers import ( AutoTokenizer, TFAutoModelForCausalLM, TFAutoModelForSeqaSeqLM, TFAutoModelForSpeechSeqaSeq, TFAutoModelForVisionaSeq, TFBartForConditionalGeneration, TFLogitsProcessorList, TFMinLengthLogitsProcessor, tf_top_k_top_p_filtering, ) if is_tensorflow_text_available(): import tensorflow_text as text @require_tf class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: Union[str, Any] = tf.convert_to_tensor( [ [ 8.222_0991, # 3rd highest value; idx. 0 -0.562_0044, 5.2322_9752, 4.038_6393, -6.879_8378, -0.5478_5802, -3.201_2153, 2.9277_7176, 1.8817_1953, 7.3534_1276, # 5th highest value; idx. 9 8.4320_7833, # 2nd highest value; idx. 10 -9.8571_1836, -5.9620_9236, -1.1303_9161, -7.111_5294, -0.836_9633, -5.318_6408, 7.0642_7407, 0.8136_9344, -0.8202_3817, -5.917_9796, 0.5881_3443, -6.9977_8438, 4.7155_1189, -0.1877_1637, 7.4402_0759, # 4th highest value; idx. 25 9.3845_0987, # 1st highest value; idx. 26 2.1266_2941, -9.3256_2038, 2.3565_2522, ], # cummulative prob of 5 highest values <= 0.6 [ 0.5842_5518, 4.5313_9238, -5.5751_0464, -6.2803_0699, -7.1952_9503, -4.0212_2551, 1.3933_7037, -6.0670_7057, 1.5948_0517, -9.64_3119, 0.0390_7799, 0.6723_1762, -8.8820_6726, 6.2711_5922, # 4th highest value; idx. 13 2.2852_0723, 4.8276_7506, 4.3042_1368, 8.827_5313, # 2nd highest value; idx. 17 5.4402_9958, # 5th highest value; idx. 18 -4.473_5794, 7.3857_9536, # 3rd highest value; idx. 20 -2.9105_1663, 2.6194_6077, -2.567_4762, -9.4895_9302, -4.0292_2645, -1.3541_6918, 9.6770_2323, # 1st highest value; idx. 27 -5.8947_8553, 1.8537_0467, ], # cummulative prob of 5 highest values <= 0.6 ] , dtype=tf.floataa , ) SCREAMING_SNAKE_CASE_: Union[str, Any] = tf.convert_to_tensor( [[0, 0], [0, 9], [0, 10], [0, 25], [0, 26], [1, 13], [1, 17], [1, 18], [1, 20], [1, 27]] , dtype=tf.intaa , ) # expected non filtered idx as noted above SCREAMING_SNAKE_CASE_: List[str] = tf.convert_to_tensor( [8.22_2099, 7.353_4126, 8.43_2078, 7.440_2075, 9.3_8451, 6.27_1159, 8.82_7531, 5.440_2995, 7.385_7956, 9.67_7023] , dtype=tf.floataa , ) # expected non filtered values as noted above SCREAMING_SNAKE_CASE_: List[Any] = tf_top_k_top_p_filtering(lowerCAmelCase__ , top_k=10 , top_p=0.6 , min_tokens_to_keep=4) SCREAMING_SNAKE_CASE_: List[str] = output[output != -float("inf")] SCREAMING_SNAKE_CASE_: Optional[Any] = tf.cast( tf.where(tf.not_equal(lowerCAmelCase__ , tf.constant(-float("inf") , dtype=tf.floataa))) , dtype=tf.intaa , ) tf.debugging.assert_near(lowerCAmelCase__ , lowerCAmelCase__ , rtol=1E-12) tf.debugging.assert_equal(lowerCAmelCase__ , lowerCAmelCase__) @require_tf class __lowercase ( unittest.TestCase , UpperCAmelCase_ ): """simple docstring""" if is_tf_available(): _UpperCAmelCase : List[Any] = { '''AutoModelForCausalLM''': TFAutoModelForCausalLM, '''AutoModelForSpeechSeq2Seq''': TFAutoModelForSpeechSeqaSeq, '''AutoModelForSeq2SeqLM''': TFAutoModelForSeqaSeqLM, '''AutoModelForVision2Seq''': TFAutoModelForVisionaSeq, '''LogitsProcessorList''': TFLogitsProcessorList, '''MinLengthLogitsProcessor''': TFMinLengthLogitsProcessor, '''create_tensor_fn''': tf.convert_to_tensor, '''floats_tensor''': floats_tensor, '''return_tensors''': '''tf''', } @slow def _SCREAMING_SNAKE_CASE ( self : List[Any]): # TF-only test: tf.saved_model export SCREAMING_SNAKE_CASE_: Any = TFAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2") SCREAMING_SNAKE_CASE_: Union[str, Any] = 2 SCREAMING_SNAKE_CASE_: int = 2 class __lowercase ( tf.Module ): """simple docstring""" def __init__( self : Union[str, Any] , lowerCAmelCase__ : str): super(lowerCAmelCase__ , self).__init__() SCREAMING_SNAKE_CASE_: Any = model @tf.function( input_signature=( tf.TensorSpec((None, input_length) , tf.intaa , name="input_ids"), tf.TensorSpec((None, input_length) , tf.intaa , name="attention_mask"), ) , jit_compile=lowerCAmelCase__ , ) def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : List[str]): SCREAMING_SNAKE_CASE_: Any = self.model.generate( input_ids=lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , max_new_tokens=lowerCAmelCase__ , return_dict_in_generate=lowerCAmelCase__ , ) return {"sequences": outputs["sequences"]} SCREAMING_SNAKE_CASE_: Union[str, Any] = [[2, 0], [102, 103]] SCREAMING_SNAKE_CASE_: int = [[1, 0], [1, 1]] SCREAMING_SNAKE_CASE_: List[str] = DummyModel(model=lowerCAmelCase__) with tempfile.TemporaryDirectory() as tmp_dir: tf.saved_model.save(lowerCAmelCase__ , lowerCAmelCase__ , signatures={"serving_default": dummy_model.serving}) SCREAMING_SNAKE_CASE_: Optional[Any] = tf.saved_model.load(lowerCAmelCase__).signatures["serving_default"] for batch_size in range(1 , len(lowerCAmelCase__) + 1): SCREAMING_SNAKE_CASE_: List[Any] = { "input_ids": tf.constant(dummy_input_ids[:batch_size]), "attention_mask": tf.constant(dummy_attention_masks[:batch_size]), } SCREAMING_SNAKE_CASE_: List[str] = serving_func(**lowerCAmelCase__)["sequences"] SCREAMING_SNAKE_CASE_: Optional[Any] = test_model.generate(**lowerCAmelCase__ , max_new_tokens=lowerCAmelCase__) tf.debugging.assert_equal(lowerCAmelCase__ , lowerCAmelCase__) @slow def _SCREAMING_SNAKE_CASE ( self : List[str]): # TF-only test: tf.saved_model export SCREAMING_SNAKE_CASE_: Optional[int] = TFAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2") SCREAMING_SNAKE_CASE_: Any = 1 SCREAMING_SNAKE_CASE_: Optional[int] = 2 class __lowercase ( tf.Module ): """simple docstring""" def __init__( self : str , lowerCAmelCase__ : Tuple): super(lowerCAmelCase__ , self).__init__() SCREAMING_SNAKE_CASE_: Optional[Any] = model @tf.function( input_signature=( tf.TensorSpec((batch_size, None) , tf.intaa , name="input_ids"), tf.TensorSpec((batch_size, None) , tf.intaa , name="attention_mask"), ) , jit_compile=lowerCAmelCase__ , ) def _SCREAMING_SNAKE_CASE ( self : List[Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[Any]): SCREAMING_SNAKE_CASE_: Optional[int] = self.model.generate( input_ids=lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , max_new_tokens=lowerCAmelCase__ , return_dict_in_generate=lowerCAmelCase__ , ) return {"sequences": outputs["sequences"]} SCREAMING_SNAKE_CASE_: List[str] = [[2], [102, 103]] SCREAMING_SNAKE_CASE_: Dict = [[1], [1, 1]] SCREAMING_SNAKE_CASE_: Dict = DummyModel(model=lowerCAmelCase__) with tempfile.TemporaryDirectory() as tmp_dir: tf.saved_model.save(lowerCAmelCase__ , lowerCAmelCase__ , signatures={"serving_default": dummy_model.serving}) SCREAMING_SNAKE_CASE_: List[str] = tf.saved_model.load(lowerCAmelCase__).signatures["serving_default"] for input_row in range(len(lowerCAmelCase__)): SCREAMING_SNAKE_CASE_: Dict = { "input_ids": tf.constant([dummy_input_ids[input_row]]), "attention_mask": tf.constant([dummy_attention_masks[input_row]]), } SCREAMING_SNAKE_CASE_: Optional[int] = serving_func(**lowerCAmelCase__)["sequences"] SCREAMING_SNAKE_CASE_: Dict = test_model.generate(**lowerCAmelCase__ , max_new_tokens=lowerCAmelCase__) tf.debugging.assert_equal(lowerCAmelCase__ , lowerCAmelCase__) @slow @require_tensorflow_text def _SCREAMING_SNAKE_CASE ( self : Any): # TF-only test: tf.saved_model export with tempfile.TemporaryDirectory() as tmp_dir: # file needed to load the TF tokenizer hf_hub_download(repo_id="google/flan-t5-small" , filename="spiece.model" , local_dir=lowerCAmelCase__) class __lowercase ( tf.keras.layers.Layer ): """simple docstring""" def __init__( self : List[Any]): super().__init__() SCREAMING_SNAKE_CASE_: int = text.SentencepieceTokenizer( model=tf.io.gfile.GFile(os.path.join(lowerCAmelCase__ , "spiece.model") , "rb").read()) SCREAMING_SNAKE_CASE_: int = TFAutoModelForSeqaSeqLM.from_pretrained("hf-internal-testing/tiny-random-t5") def _SCREAMING_SNAKE_CASE ( self : Tuple , lowerCAmelCase__ : Tuple , *lowerCAmelCase__ : List[Any] , **lowerCAmelCase__ : Dict): SCREAMING_SNAKE_CASE_: Tuple = self.tokenizer.tokenize(lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = text.pad_model_inputs( lowerCAmelCase__ , max_seq_length=64 , pad_value=self.model.config.pad_token_id) SCREAMING_SNAKE_CASE_: Optional[int] = self.model.generate(input_ids=lowerCAmelCase__ , attention_mask=lowerCAmelCase__) return self.tokenizer.detokenize(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = CompleteSentenceTransformer() SCREAMING_SNAKE_CASE_: Any = tf.keras.layers.Input(shape=(1,) , dtype=tf.string , name="inputs") SCREAMING_SNAKE_CASE_: Any = complete_model(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = tf.keras.Model(lowerCAmelCase__ , lowerCAmelCase__) keras_model.save(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : str): # Has PT equivalent: this test relies on random sampling SCREAMING_SNAKE_CASE_: Tuple = { "do_sample": True, "num_beams": 1, "top_p": 0.7, "top_k": 10, "temperature": 0.7, } SCREAMING_SNAKE_CASE_: List[str] = 14 SCREAMING_SNAKE_CASE_: Any = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2") SCREAMING_SNAKE_CASE_: Optional[Any] = "Hello, my dog is cute and" SCREAMING_SNAKE_CASE_: Any = tokenizer(lowerCAmelCase__ , return_tensors="tf") SCREAMING_SNAKE_CASE_: Optional[int] = TFAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2") SCREAMING_SNAKE_CASE_: List[Any] = 638 # forces the generation to happen on CPU, to avoid GPU-related quirks with tf.device(":/CPU:0"): tf.random.set_seed(0) SCREAMING_SNAKE_CASE_: Optional[int] = model.generate(**lowerCAmelCase__ , eos_token_id=lowerCAmelCase__ , **lowerCAmelCase__) self.assertTrue(expectation == len(generated_tokens[0])) SCREAMING_SNAKE_CASE_: int = [638, 198] with tf.device(":/CPU:0"): tf.random.set_seed(0) SCREAMING_SNAKE_CASE_: Union[str, Any] = model.generate(**lowerCAmelCase__ , eos_token_id=lowerCAmelCase__ , **lowerCAmelCase__) self.assertTrue(expectation == len(generated_tokens[0])) def _SCREAMING_SNAKE_CASE ( self : Tuple): # Has PT equivalent: ample use of framework-specific code SCREAMING_SNAKE_CASE_: Any = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-bart") SCREAMING_SNAKE_CASE_: Tuple = "Hugging Face is a technology company based in New York and Paris." SCREAMING_SNAKE_CASE_: Union[str, Any] = bart_tokenizer(lowerCAmelCase__ , return_tensors="tf").input_ids SCREAMING_SNAKE_CASE_: Any = TFBartForConditionalGeneration.from_pretrained("hf-internal-testing/tiny-random-bart") SCREAMING_SNAKE_CASE_: List[Any] = bart_model.generate(lowerCAmelCase__).numpy() class __lowercase ( UpperCAmelCase_ ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : Tuple , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Tuple=None , **lowerCAmelCase__ : List[Any]): return super().call(lowerCAmelCase__ , **lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = FakeBart.from_pretrained("hf-internal-testing/tiny-random-bart") SCREAMING_SNAKE_CASE_: Dict = bart_model.generate(lowerCAmelCase__ , foo="bar").numpy() self.assertTrue(np.array_equal(lowerCAmelCase__ , lowerCAmelCase__)) class __lowercase ( bart_model.model.encoder.__class__ ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : Tuple , **lowerCAmelCase__ : Tuple): return super().call(lowerCAmelCase__ , **lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = FakeEncoder(bart_model.config , bart_model.model.shared) SCREAMING_SNAKE_CASE_: Dict = fake_encoder # Normal generation still works (the output will be different because the encoder weights are different) SCREAMING_SNAKE_CASE_: Optional[int] = bart_model.generate(lowerCAmelCase__).numpy() with self.assertRaises(lowerCAmelCase__): # FakeEncoder.call() accepts **kwargs -> no filtering -> value error due to unexpected input "foo" bart_model.generate(lowerCAmelCase__ , foo="bar")
13
import math import sys def A_ ( _UpperCAmelCase ): if number != int(_UpperCAmelCase ): raise ValueError("the value of input must be a natural number" ) if number < 0: raise ValueError("the value of input must not be a negative number" ) if number == 0: return 1 SCREAMING_SNAKE_CASE_: List[str] = [-1] * (number + 1) SCREAMING_SNAKE_CASE_: str = 0 for i in range(1 , number + 1 ): SCREAMING_SNAKE_CASE_: str = sys.maxsize SCREAMING_SNAKE_CASE_: List[Any] = int(math.sqrt(_UpperCAmelCase ) ) for j in range(1 , root + 1 ): SCREAMING_SNAKE_CASE_: List[str] = 1 + answers[i - (j**2)] SCREAMING_SNAKE_CASE_: Optional[Any] = min(_UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Dict = answer return answers[number] if __name__ == "__main__": import doctest doctest.testmod()
13
1
lowerCAmelCase : Optional[Any] = range(2, 20 + 1) lowerCAmelCase : Dict = [10**k for k in range(ks[-1] + 1)] lowerCAmelCase : dict[int, dict[int, list[list[int]]]] = {} def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Optional[Any] = sum(a_i[j] for j in range(_UpperCAmelCase , len(_UpperCAmelCase ) ) ) SCREAMING_SNAKE_CASE_: Optional[int] = sum(a_i[j] * base[j] for j in range(min(len(_UpperCAmelCase ) , _UpperCAmelCase ) ) ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = 0, 0 SCREAMING_SNAKE_CASE_: int = n - i SCREAMING_SNAKE_CASE_: str = memo.get(_UpperCAmelCase ) if sub_memo is not None: SCREAMING_SNAKE_CASE_: int = sub_memo.get(_UpperCAmelCase ) if jumps is not None and len(_UpperCAmelCase ) > 0: # find and make the largest jump without going over SCREAMING_SNAKE_CASE_: str = -1 for _k in range(len(_UpperCAmelCase ) - 1 , -1 , -1 ): if jumps[_k][2] <= k and jumps[_k][1] <= max_dn: SCREAMING_SNAKE_CASE_: Optional[Any] = _k break if max_jump >= 0: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = jumps[max_jump] # since the difference between jumps is cached, add c SCREAMING_SNAKE_CASE_: Union[str, Any] = diff + c for j in range(min(_UpperCAmelCase , len(_UpperCAmelCase ) ) ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = divmod(_UpperCAmelCase , 10 ) if new_c > 0: add(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) else: SCREAMING_SNAKE_CASE_: List[Any] = [] else: SCREAMING_SNAKE_CASE_: Union[str, Any] = {c: []} SCREAMING_SNAKE_CASE_: List[str] = sub_memo if dn >= max_dn or c + diff >= base[k]: return diff, dn if k > ks[0]: while True: # keep doing smaller jumps SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = next_term(_UpperCAmelCase , k - 1 , i + dn , _UpperCAmelCase ) diff += _diff dn += terms_jumped if dn >= max_dn or c + diff >= base[k]: break else: # would be too small a jump, just compute sequential terms instead SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = compute(_UpperCAmelCase , _UpperCAmelCase , i + dn , _UpperCAmelCase ) diff += _diff dn += terms_jumped SCREAMING_SNAKE_CASE_: Dict = sub_memo[c] # keep jumps sorted by # of terms skipped SCREAMING_SNAKE_CASE_: Tuple = 0 while j < len(_UpperCAmelCase ): if jumps[j][1] > dn: break j += 1 # cache the jump for this value digitsum(b) and c sub_memo[c].insert(_UpperCAmelCase , (diff, dn, k) ) return (diff, dn) def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): if i >= n: return 0, i if k > len(_UpperCAmelCase ): a_i.extend([0 for _ in range(k - len(_UpperCAmelCase ) )] ) # note: a_i -> b * 10^k + c # ds_b -> digitsum(b) # ds_c -> digitsum(c) SCREAMING_SNAKE_CASE_: Tuple = i SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = 0, 0, 0 for j in range(len(_UpperCAmelCase ) ): if j >= k: ds_b += a_i[j] else: ds_c += a_i[j] while i < n: i += 1 SCREAMING_SNAKE_CASE_: Optional[int] = ds_c + ds_b diff += addend SCREAMING_SNAKE_CASE_: Dict = 0 for j in range(_UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Optional[int] = a_i[j] + addend SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = divmod(_UpperCAmelCase , 10 ) ds_c += a_i[j] if addend > 0: break if addend > 0: add(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) return diff, i - start_i def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): for j in range(_UpperCAmelCase , len(_UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_: str = digits[j] + addend if s >= 10: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = divmod(_UpperCAmelCase , 10 ) SCREAMING_SNAKE_CASE_: List[str] = addend // 10 + quotient else: SCREAMING_SNAKE_CASE_: Tuple = s SCREAMING_SNAKE_CASE_: List[Any] = addend // 10 if addend == 0: break while addend > 0: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = divmod(_UpperCAmelCase , 10 ) digits.append(_UpperCAmelCase ) def A_ ( _UpperCAmelCase = 10**15 ): SCREAMING_SNAKE_CASE_: str = [1] SCREAMING_SNAKE_CASE_: List[str] = 1 SCREAMING_SNAKE_CASE_: Dict = 0 while True: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = next_term(_UpperCAmelCase , 20 , i + dn , _UpperCAmelCase ) dn += terms_jumped if dn == n - i: break SCREAMING_SNAKE_CASE_: Union[str, Any] = 0 for j in range(len(_UpperCAmelCase ) ): a_n += digits[j] * 10**j return a_n if __name__ == "__main__": print(f'''{solution() = }''')
13
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase : Optional[int] = {"""configuration_wavlm""": ["""WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """WavLMConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Any = [ """WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST""", """WavLMForAudioFrameClassification""", """WavLMForCTC""", """WavLMForSequenceClassification""", """WavLMForXVector""", """WavLMModel""", """WavLMPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_wavlm import WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP, WavLMConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_wavlm import ( WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST, WavLMForAudioFrameClassification, WavLMForCTC, WavLMForSequenceClassification, WavLMForXVector, WavLMModel, WavLMPreTrainedModel, ) else: import sys lowerCAmelCase : Union[str, Any] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
13
1
from __future__ import annotations import copy import inspect import unittest import numpy as np from transformers import is_tf_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from transformers.utils import cached_property 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 import ( TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST, TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, TFLayoutLMvaModel, ) if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class __lowercase : """simple docstring""" def __init__( self : Dict , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : List[Any]=2 , lowerCAmelCase__ : Union[str, Any]=3 , lowerCAmelCase__ : int=4 , lowerCAmelCase__ : int=2 , lowerCAmelCase__ : Any=7 , lowerCAmelCase__ : Tuple=True , lowerCAmelCase__ : List[Any]=True , lowerCAmelCase__ : Dict=True , lowerCAmelCase__ : Union[str, Any]=True , lowerCAmelCase__ : Optional[Any]=99 , lowerCAmelCase__ : List[str]=36 , lowerCAmelCase__ : int=2 , lowerCAmelCase__ : str=4 , lowerCAmelCase__ : Optional[Any]=37 , lowerCAmelCase__ : Union[str, Any]="gelu" , lowerCAmelCase__ : Optional[Any]=0.1 , lowerCAmelCase__ : Optional[Any]=0.1 , lowerCAmelCase__ : Tuple=512 , lowerCAmelCase__ : Any=16 , lowerCAmelCase__ : Optional[int]=2 , lowerCAmelCase__ : str=0.02 , lowerCAmelCase__ : List[Any]=6 , lowerCAmelCase__ : Dict=6 , lowerCAmelCase__ : int=3 , lowerCAmelCase__ : int=4 , lowerCAmelCase__ : List[Any]=None , lowerCAmelCase__ : Optional[int]=1000 , ): SCREAMING_SNAKE_CASE_: Union[str, Any] = parent SCREAMING_SNAKE_CASE_: Any = batch_size SCREAMING_SNAKE_CASE_: str = num_channels SCREAMING_SNAKE_CASE_: List[str] = image_size SCREAMING_SNAKE_CASE_: Dict = patch_size SCREAMING_SNAKE_CASE_: Union[str, Any] = is_training SCREAMING_SNAKE_CASE_: int = use_input_mask SCREAMING_SNAKE_CASE_: Optional[int] = use_token_type_ids SCREAMING_SNAKE_CASE_: Optional[int] = use_labels SCREAMING_SNAKE_CASE_: List[Any] = vocab_size SCREAMING_SNAKE_CASE_: Any = hidden_size SCREAMING_SNAKE_CASE_: List[Any] = num_hidden_layers SCREAMING_SNAKE_CASE_: Dict = num_attention_heads SCREAMING_SNAKE_CASE_: Optional[Any] = intermediate_size SCREAMING_SNAKE_CASE_: Any = hidden_act SCREAMING_SNAKE_CASE_: Any = hidden_dropout_prob SCREAMING_SNAKE_CASE_: Union[str, Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_: str = max_position_embeddings SCREAMING_SNAKE_CASE_: List[Any] = type_vocab_size SCREAMING_SNAKE_CASE_: List[str] = type_sequence_label_size SCREAMING_SNAKE_CASE_: Tuple = initializer_range SCREAMING_SNAKE_CASE_: Union[str, Any] = coordinate_size SCREAMING_SNAKE_CASE_: Optional[Any] = shape_size SCREAMING_SNAKE_CASE_: str = num_labels SCREAMING_SNAKE_CASE_: str = num_choices SCREAMING_SNAKE_CASE_: Tuple = scope SCREAMING_SNAKE_CASE_: Optional[int] = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) SCREAMING_SNAKE_CASE_: str = text_seq_length SCREAMING_SNAKE_CASE_: str = (image_size // patch_size) ** 2 + 1 SCREAMING_SNAKE_CASE_: str = self.text_seq_length + self.image_seq_length def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: List[Any] = ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size) SCREAMING_SNAKE_CASE_: Dict = ids_tensor([self.batch_size, self.text_seq_length, 4] , self.range_bbox) SCREAMING_SNAKE_CASE_: Union[str, Any] = bbox.numpy() # Ensure that bbox is legal for i in range(bbox.shape[0]): for j in range(bbox.shape[1]): if bbox[i, j, 3] < bbox[i, j, 1]: SCREAMING_SNAKE_CASE_: Dict = bbox[i, j, 3] SCREAMING_SNAKE_CASE_: List[str] = bbox[i, j, 1] SCREAMING_SNAKE_CASE_: str = tmp_coordinate if bbox[i, j, 2] < bbox[i, j, 0]: SCREAMING_SNAKE_CASE_: Optional[Any] = bbox[i, j, 2] SCREAMING_SNAKE_CASE_: Optional[int] = bbox[i, j, 0] SCREAMING_SNAKE_CASE_: Optional[int] = tmp_coordinate SCREAMING_SNAKE_CASE_: Optional[int] = tf.constant(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]) SCREAMING_SNAKE_CASE_: int = None if self.use_input_mask: SCREAMING_SNAKE_CASE_: Tuple = random_attention_mask([self.batch_size, self.text_seq_length]) SCREAMING_SNAKE_CASE_: Optional[int] = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE_: int = ids_tensor([self.batch_size, self.text_seq_length] , self.type_vocab_size) SCREAMING_SNAKE_CASE_: Optional[int] = None SCREAMING_SNAKE_CASE_: Union[str, Any] = None if self.use_labels: SCREAMING_SNAKE_CASE_: List[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size) SCREAMING_SNAKE_CASE_: Union[str, Any] = ids_tensor([self.batch_size, self.text_seq_length] , self.num_labels) SCREAMING_SNAKE_CASE_: Optional[Any] = LayoutLMvaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , coordinate_size=self.coordinate_size , shape_size=self.shape_size , input_size=self.image_size , patch_size=self.patch_size , ) return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels def _SCREAMING_SNAKE_CASE ( self : int , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[Any]): SCREAMING_SNAKE_CASE_: Optional[Any] = TFLayoutLMvaModel(config=lowerCAmelCase__) # text + image SCREAMING_SNAKE_CASE_: List[Any] = model(lowerCAmelCase__ , pixel_values=lowerCAmelCase__ , training=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = model( lowerCAmelCase__ , bbox=lowerCAmelCase__ , pixel_values=lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , training=lowerCAmelCase__ , ) SCREAMING_SNAKE_CASE_: List[str] = model(lowerCAmelCase__ , bbox=lowerCAmelCase__ , pixel_values=lowerCAmelCase__ , training=lowerCAmelCase__) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) # text only SCREAMING_SNAKE_CASE_: Union[str, Any] = model(lowerCAmelCase__ , training=lowerCAmelCase__) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size)) # image only SCREAMING_SNAKE_CASE_: Union[str, Any] = model({"pixel_values": pixel_values} , training=lowerCAmelCase__) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size)) def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Union[str, Any] = self.num_labels SCREAMING_SNAKE_CASE_: Optional[int] = TFLayoutLMvaForSequenceClassification(config=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = model( lowerCAmelCase__ , bbox=lowerCAmelCase__ , pixel_values=lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , labels=lowerCAmelCase__ , training=lowerCAmelCase__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels)) def _SCREAMING_SNAKE_CASE ( self : List[Any] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : int , lowerCAmelCase__ : Any): SCREAMING_SNAKE_CASE_: str = self.num_labels SCREAMING_SNAKE_CASE_: str = TFLayoutLMvaForTokenClassification(config=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = model( lowerCAmelCase__ , bbox=lowerCAmelCase__ , pixel_values=lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , labels=lowerCAmelCase__ , training=lowerCAmelCase__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels)) def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Optional[Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = 2 SCREAMING_SNAKE_CASE_: Optional[Any] = TFLayoutLMvaForQuestionAnswering(config=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = model( lowerCAmelCase__ , bbox=lowerCAmelCase__ , pixel_values=lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , start_positions=lowerCAmelCase__ , end_positions=lowerCAmelCase__ , training=lowerCAmelCase__ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length)) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length)) def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: List[str] = self.prepare_config_and_inputs() ((SCREAMING_SNAKE_CASE_) , (SCREAMING_SNAKE_CASE_) , (SCREAMING_SNAKE_CASE_) , (SCREAMING_SNAKE_CASE_) , (SCREAMING_SNAKE_CASE_) , (SCREAMING_SNAKE_CASE_) , (SCREAMING_SNAKE_CASE_) , (SCREAMING_SNAKE_CASE_)): Union[str, Any] = config_and_inputs SCREAMING_SNAKE_CASE_: int = { "input_ids": input_ids, "bbox": bbox, "pixel_values": pixel_values, "token_type_ids": token_type_ids, "attention_mask": input_mask, } return config, inputs_dict @require_tf class __lowercase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : str = ( ( TFLayoutLMvaModel, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, ) if is_tf_available() else () ) _UpperCAmelCase : Dict = ( {'''document-question-answering''': TFLayoutLMvaForQuestionAnswering, '''feature-extraction''': TFLayoutLMvaModel} if is_tf_available() else {} ) _UpperCAmelCase : List[str] = False _UpperCAmelCase : List[str] = False _UpperCAmelCase : Optional[Any] = False def _SCREAMING_SNAKE_CASE ( self : int , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Tuple): return True def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : str , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Tuple=False): SCREAMING_SNAKE_CASE_: Optional[int] = copy.deepcopy(lowerCAmelCase__) if model_class in get_values(lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Union[str, Any] = { k: tf.tile(tf.expand_dims(lowerCAmelCase__ , 1) , (1, self.model_tester.num_choices) + (1,) * (v.ndim - 1)) if isinstance(lowerCAmelCase__ , tf.Tensor) and v.ndim > 0 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Optional[Any] = tf.ones(self.model_tester.batch_size , dtype=tf.intaa) elif model_class in get_values(lowerCAmelCase__): SCREAMING_SNAKE_CASE_: str = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa) SCREAMING_SNAKE_CASE_: Optional[int] = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa) elif model_class in get_values(lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Tuple = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa) elif model_class in get_values(lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Dict = tf.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=tf.intaa) return inputs_dict def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Tuple = TFLayoutLMvaModelTester(self) SCREAMING_SNAKE_CASE_: int = ConfigTester(self , config_class=lowerCAmelCase__ , hidden_size=37) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): self.config_tester.run_common_tests() def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_: Tuple = model_class(lowerCAmelCase__) if getattr(lowerCAmelCase__ , "hf_compute_loss" , lowerCAmelCase__): # The number of elements in the loss should be the same as the number of elements in the label SCREAMING_SNAKE_CASE_: Optional[int] = self._prepare_for_class(inputs_dict.copy() , lowerCAmelCase__ , return_labels=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = prepared_for_class[ sorted(prepared_for_class.keys() - inputs_dict.keys() , reverse=lowerCAmelCase__)[0] ] SCREAMING_SNAKE_CASE_: List[Any] = added_label.shape.as_list()[:1] # Test that model correctly compute the loss with kwargs SCREAMING_SNAKE_CASE_: List[Any] = self._prepare_for_class(inputs_dict.copy() , lowerCAmelCase__ , return_labels=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = prepared_for_class.pop("input_ids") SCREAMING_SNAKE_CASE_: List[Any] = model(lowerCAmelCase__ , **lowerCAmelCase__)[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]) # Test that model correctly compute the loss when we mask some positions SCREAMING_SNAKE_CASE_: Dict = self._prepare_for_class(inputs_dict.copy() , lowerCAmelCase__ , return_labels=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = prepared_for_class.pop("input_ids") if "labels" in prepared_for_class: SCREAMING_SNAKE_CASE_: Any = prepared_for_class["labels"].numpy() if len(labels.shape) > 1 and labels.shape[1] != 1: SCREAMING_SNAKE_CASE_: Union[str, Any] = -100 SCREAMING_SNAKE_CASE_: Union[str, Any] = tf.convert_to_tensor(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = model(lowerCAmelCase__ , **lowerCAmelCase__)[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]) self.assertTrue(not np.any(np.isnan(loss.numpy()))) # Test that model correctly compute the loss with a dict SCREAMING_SNAKE_CASE_: List[str] = self._prepare_for_class(inputs_dict.copy() , lowerCAmelCase__ , return_labels=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = model(lowerCAmelCase__)[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]) # Test that model correctly compute the loss with a tuple SCREAMING_SNAKE_CASE_: Union[str, Any] = self._prepare_for_class(inputs_dict.copy() , lowerCAmelCase__ , return_labels=lowerCAmelCase__) # Get keys that were added with the _prepare_for_class function SCREAMING_SNAKE_CASE_: Any = prepared_for_class.keys() - inputs_dict.keys() SCREAMING_SNAKE_CASE_: str = inspect.signature(model.call).parameters SCREAMING_SNAKE_CASE_: Union[str, Any] = list(signature.keys()) # Create a dictionary holding the location of the tensors in the tuple SCREAMING_SNAKE_CASE_: int = {0: "input_ids"} for label_key in label_keys: SCREAMING_SNAKE_CASE_: Dict = signature_names.index(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = label_key SCREAMING_SNAKE_CASE_: Any = sorted(tuple_index_mapping.items()) # Initialize a list with their default values, update the values and convert to a tuple SCREAMING_SNAKE_CASE_: List[str] = [] for name in signature_names: if name != "kwargs": list_input.append(signature[name].default) for index, value in sorted_tuple_index_mapping: SCREAMING_SNAKE_CASE_: Tuple = prepared_for_class[value] SCREAMING_SNAKE_CASE_: int = tuple(lowerCAmelCase__) # Send to model SCREAMING_SNAKE_CASE_: List[str] = model(tuple_input[:-1])[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]) def _SCREAMING_SNAKE_CASE ( self : Tuple): ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ): Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ): int = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: SCREAMING_SNAKE_CASE_: Tuple = type self.model_tester.create_and_check_model(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Tuple): ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ): Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ): Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str]): ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ): Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) @slow def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_: List[str] = TFLayoutLMvaModel.from_pretrained(lowerCAmelCase__) self.assertIsNotNone(lowerCAmelCase__) def A_ ( ): SCREAMING_SNAKE_CASE_: Optional[int] = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_tf class __lowercase ( unittest.TestCase ): """simple docstring""" @cached_property def _SCREAMING_SNAKE_CASE ( self : List[str]): return LayoutLMvaImageProcessor(apply_ocr=lowerCAmelCase__) if is_vision_available() else None @slow def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: Optional[Any] = TFLayoutLMvaModel.from_pretrained("microsoft/layoutlmv3-base") SCREAMING_SNAKE_CASE_: List[str] = self.default_image_processor SCREAMING_SNAKE_CASE_: Dict = prepare_img() SCREAMING_SNAKE_CASE_: Optional[int] = image_processor(images=lowerCAmelCase__ , return_tensors="tf").pixel_values SCREAMING_SNAKE_CASE_: str = tf.constant([[1, 2]]) SCREAMING_SNAKE_CASE_: int = tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]]) , axis=0) # forward pass SCREAMING_SNAKE_CASE_: Any = model(input_ids=lowerCAmelCase__ , bbox=lowerCAmelCase__ , pixel_values=lowerCAmelCase__ , training=lowerCAmelCase__) # verify the logits SCREAMING_SNAKE_CASE_: Dict = (1, 199, 768) self.assertEqual(outputs.last_hidden_state.shape , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = tf.constant( [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]]) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] , lowerCAmelCase__ , atol=1E-4))
13
import unittest from transformers import ( MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TextaTextGenerationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, require_tf, require_torch from transformers.utils import is_torch_available from .test_pipelines_common import ANY if is_torch_available(): import torch @is_pipeline_test class __lowercase ( unittest.TestCase ): """simple docstring""" _UpperCAmelCase : List[Any] = MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING _UpperCAmelCase : str = TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING def _SCREAMING_SNAKE_CASE ( self : Any , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Dict): SCREAMING_SNAKE_CASE_: Any = TextaTextGenerationPipeline(model=lowerCAmelCase__ , tokenizer=lowerCAmelCase__) return generator, ["Something to write", "Something else"] def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Any): SCREAMING_SNAKE_CASE_: List[Any] = generator("Something there") self.assertEqual(lowerCAmelCase__ , [{"generated_text": ANY(lowerCAmelCase__)}]) # These are encoder decoder, they don't just append to incoming string self.assertFalse(outputs[0]["generated_text"].startswith("Something there")) SCREAMING_SNAKE_CASE_: List[Any] = generator(["This is great !", "Something else"] , num_return_sequences=2 , do_sample=lowerCAmelCase__) self.assertEqual( lowerCAmelCase__ , [ [{"generated_text": ANY(lowerCAmelCase__)}, {"generated_text": ANY(lowerCAmelCase__)}], [{"generated_text": ANY(lowerCAmelCase__)}, {"generated_text": ANY(lowerCAmelCase__)}], ] , ) SCREAMING_SNAKE_CASE_: Dict = generator( ["This is great !", "Something else"] , num_return_sequences=2 , batch_size=2 , do_sample=lowerCAmelCase__) self.assertEqual( lowerCAmelCase__ , [ [{"generated_text": ANY(lowerCAmelCase__)}, {"generated_text": ANY(lowerCAmelCase__)}], [{"generated_text": ANY(lowerCAmelCase__)}, {"generated_text": ANY(lowerCAmelCase__)}], ] , ) with self.assertRaises(lowerCAmelCase__): generator(4) @require_torch def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Optional[int] = pipeline("text2text-generation" , model="patrickvonplaten/t5-tiny-random" , framework="pt") # do_sample=False necessary for reproducibility SCREAMING_SNAKE_CASE_: Union[str, Any] = generator("Something there" , do_sample=lowerCAmelCase__) self.assertEqual(lowerCAmelCase__ , [{"generated_text": ""}]) SCREAMING_SNAKE_CASE_: Union[str, Any] = 3 SCREAMING_SNAKE_CASE_: Any = generator( "Something there" , num_return_sequences=lowerCAmelCase__ , num_beams=lowerCAmelCase__ , ) SCREAMING_SNAKE_CASE_: Any = [ {"generated_text": "Beide Beide Beide Beide Beide Beide Beide Beide Beide"}, {"generated_text": "Beide Beide Beide Beide Beide Beide Beide Beide"}, {"generated_text": ""}, ] self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = generator("This is a test" , do_sample=lowerCAmelCase__ , num_return_sequences=2 , return_tensors=lowerCAmelCase__) self.assertEqual( lowerCAmelCase__ , [ {"generated_token_ids": ANY(torch.Tensor)}, {"generated_token_ids": ANY(torch.Tensor)}, ] , ) SCREAMING_SNAKE_CASE_: str = generator.model.config.eos_token_id SCREAMING_SNAKE_CASE_: Union[str, Any] = "<pad>" SCREAMING_SNAKE_CASE_: Tuple = generator( ["This is a test", "This is a second test"] , do_sample=lowerCAmelCase__ , num_return_sequences=2 , batch_size=2 , return_tensors=lowerCAmelCase__ , ) self.assertEqual( lowerCAmelCase__ , [ [ {"generated_token_ids": ANY(torch.Tensor)}, {"generated_token_ids": ANY(torch.Tensor)}, ], [ {"generated_token_ids": ANY(torch.Tensor)}, {"generated_token_ids": ANY(torch.Tensor)}, ], ] , ) @require_tf def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = pipeline("text2text-generation" , model="patrickvonplaten/t5-tiny-random" , framework="tf") # do_sample=False necessary for reproducibility SCREAMING_SNAKE_CASE_: List[Any] = generator("Something there" , do_sample=lowerCAmelCase__) self.assertEqual(lowerCAmelCase__ , [{"generated_text": ""}])
13
1
from collections import OrderedDict from typing import Any, List, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import logging lowerCAmelCase : List[str] = logging.get_logger(__name__) lowerCAmelCase : Dict = { """Salesforce/codegen-350M-nl""": """https://huggingface.co/Salesforce/codegen-350M-nl/resolve/main/config.json""", """Salesforce/codegen-350M-multi""": """https://huggingface.co/Salesforce/codegen-350M-multi/resolve/main/config.json""", """Salesforce/codegen-350M-mono""": """https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/config.json""", """Salesforce/codegen-2B-nl""": """https://huggingface.co/Salesforce/codegen-2B-nl/resolve/main/config.json""", """Salesforce/codegen-2B-multi""": """https://huggingface.co/Salesforce/codegen-2B-multi/resolve/main/config.json""", """Salesforce/codegen-2B-mono""": """https://huggingface.co/Salesforce/codegen-2B-mono/resolve/main/config.json""", """Salesforce/codegen-6B-nl""": """https://huggingface.co/Salesforce/codegen-6B-nl/resolve/main/config.json""", """Salesforce/codegen-6B-multi""": """https://huggingface.co/Salesforce/codegen-6B-multi/resolve/main/config.json""", """Salesforce/codegen-6B-mono""": """https://huggingface.co/Salesforce/codegen-6B-mono/resolve/main/config.json""", """Salesforce/codegen-16B-nl""": """https://huggingface.co/Salesforce/codegen-16B-nl/resolve/main/config.json""", """Salesforce/codegen-16B-multi""": """https://huggingface.co/Salesforce/codegen-16B-multi/resolve/main/config.json""", """Salesforce/codegen-16B-mono""": """https://huggingface.co/Salesforce/codegen-16B-mono/resolve/main/config.json""", } class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : Tuple = '''codegen''' _UpperCAmelCase : List[Any] = { '''max_position_embeddings''': '''n_positions''', '''hidden_size''': '''n_embd''', '''num_attention_heads''': '''n_head''', '''num_hidden_layers''': '''n_layer''', } def __init__( self : List[Any] , lowerCAmelCase__ : int=5_0400 , lowerCAmelCase__ : Dict=2048 , lowerCAmelCase__ : Optional[int]=2048 , lowerCAmelCase__ : Union[str, Any]=4096 , lowerCAmelCase__ : Optional[int]=28 , lowerCAmelCase__ : str=16 , lowerCAmelCase__ : Union[str, Any]=64 , lowerCAmelCase__ : List[Any]=None , lowerCAmelCase__ : Tuple="gelu_new" , lowerCAmelCase__ : List[Any]=0.0 , lowerCAmelCase__ : Any=0.0 , lowerCAmelCase__ : Any=0.0 , lowerCAmelCase__ : Any=1E-5 , lowerCAmelCase__ : Optional[int]=0.02 , lowerCAmelCase__ : Any=True , lowerCAmelCase__ : Any=5_0256 , lowerCAmelCase__ : int=5_0256 , lowerCAmelCase__ : Union[str, Any]=False , **lowerCAmelCase__ : str , ): SCREAMING_SNAKE_CASE_: Optional[int] = vocab_size SCREAMING_SNAKE_CASE_: List[str] = n_ctx SCREAMING_SNAKE_CASE_: List[Any] = n_positions SCREAMING_SNAKE_CASE_: List[str] = n_embd SCREAMING_SNAKE_CASE_: Optional[int] = n_layer SCREAMING_SNAKE_CASE_: Optional[Any] = n_head SCREAMING_SNAKE_CASE_: Union[str, Any] = n_inner SCREAMING_SNAKE_CASE_: List[str] = rotary_dim SCREAMING_SNAKE_CASE_: Dict = activation_function SCREAMING_SNAKE_CASE_: Dict = resid_pdrop SCREAMING_SNAKE_CASE_: List[str] = embd_pdrop SCREAMING_SNAKE_CASE_: List[Any] = attn_pdrop SCREAMING_SNAKE_CASE_: int = layer_norm_epsilon SCREAMING_SNAKE_CASE_: str = initializer_range SCREAMING_SNAKE_CASE_: List[Any] = use_cache SCREAMING_SNAKE_CASE_: Any = bos_token_id SCREAMING_SNAKE_CASE_: Optional[Any] = eos_token_id super().__init__( bos_token_id=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__ , tie_word_embeddings=lowerCAmelCase__ , **lowerCAmelCase__) class __lowercase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : List[Any] , lowerCAmelCase__ : PretrainedConfig , lowerCAmelCase__ : str = "default" , lowerCAmelCase__ : List[PatchingSpec] = None , lowerCAmelCase__ : bool = False , ): super().__init__(lowerCAmelCase__ , task=lowerCAmelCase__ , patching_specs=lowerCAmelCase__ , use_past=lowerCAmelCase__) if not getattr(self._config , "pad_token_id" , lowerCAmelCase__): # TODO: how to do that better? SCREAMING_SNAKE_CASE_: Optional[int] = 0 @property def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: Tuple = OrderedDict({"input_ids": {0: "batch", 1: "sequence"}}) if self.use_past: self.fill_with_past_key_values_(lowerCAmelCase__ , direction="inputs") SCREAMING_SNAKE_CASE_: Tuple = {0: "batch", 1: "past_sequence + sequence"} else: SCREAMING_SNAKE_CASE_: int = {0: "batch", 1: "sequence"} return common_inputs @property def _SCREAMING_SNAKE_CASE ( self : Optional[int]): return self._config.n_layer @property def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): return self._config.n_head def _SCREAMING_SNAKE_CASE ( self : List[Any] , lowerCAmelCase__ : PreTrainedTokenizer , lowerCAmelCase__ : int = -1 , lowerCAmelCase__ : int = -1 , lowerCAmelCase__ : bool = False , lowerCAmelCase__ : Optional[TensorType] = None , ): SCREAMING_SNAKE_CASE_: str = super(lowerCAmelCase__ , self).generate_dummy_inputs( lowerCAmelCase__ , batch_size=lowerCAmelCase__ , seq_length=lowerCAmelCase__ , is_pair=lowerCAmelCase__ , framework=lowerCAmelCase__) # We need to order the input in the way they appears in the forward() SCREAMING_SNAKE_CASE_: str = OrderedDict({"input_ids": common_inputs["input_ids"]}) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed.") else: import torch SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = common_inputs["input_ids"].shape # Not using the same length for past_key_values SCREAMING_SNAKE_CASE_: Union[str, Any] = seqlen + 2 SCREAMING_SNAKE_CASE_: Optional[Any] = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) SCREAMING_SNAKE_CASE_: Tuple = [ (torch.zeros(lowerCAmelCase__), torch.zeros(lowerCAmelCase__)) for _ in range(self.num_layers) ] SCREAMING_SNAKE_CASE_: Dict = common_inputs["attention_mask"] if self.use_past: SCREAMING_SNAKE_CASE_: Union[str, Any] = ordered_inputs["attention_mask"].dtype SCREAMING_SNAKE_CASE_: List[str] = torch.cat( [ordered_inputs["attention_mask"], torch.ones(lowerCAmelCase__ , lowerCAmelCase__ , dtype=lowerCAmelCase__)] , dim=1) return ordered_inputs @property def _SCREAMING_SNAKE_CASE ( self : Tuple): return 13
13
def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: List[str] = [0] * len(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[Any] = [] SCREAMING_SNAKE_CASE_: str = [] SCREAMING_SNAKE_CASE_: List[str] = 0 for values in graph.values(): for i in values: indegree[i] += 1 for i in range(len(_UpperCAmelCase ) ): if indegree[i] == 0: queue.append(_UpperCAmelCase ) while queue: SCREAMING_SNAKE_CASE_: Optional[int] = queue.pop(0 ) cnt += 1 topo.append(_UpperCAmelCase ) for x in graph[vertex]: indegree[x] -= 1 if indegree[x] == 0: queue.append(_UpperCAmelCase ) if cnt != len(_UpperCAmelCase ): print("Cycle exists" ) else: print(_UpperCAmelCase ) # Adjacency List of Graph lowerCAmelCase : Any = {0: [1, 2], 1: [3], 2: [3], 3: [4, 5], 4: [], 5: []} topological_sort(graph)
13
1
from __future__ import annotations from sys import maxsize from typing import Generic, TypeVar lowerCAmelCase : Dict = TypeVar("""T""") def A_ ( _UpperCAmelCase ): return (position - 1) // 2 def A_ ( _UpperCAmelCase ): return (2 * position) + 1 def A_ ( _UpperCAmelCase ): return (2 * position) + 2 class __lowercase ( Generic[T] ): """simple docstring""" def __init__( self : str): SCREAMING_SNAKE_CASE_: list[tuple[T, int]] = [] SCREAMING_SNAKE_CASE_: dict[T, int] = {} SCREAMING_SNAKE_CASE_: int = 0 def __len__( self : Tuple): return self.elements def __repr__( self : List[Any]): return str(self.heap) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): # Check if the priority queue is empty return self.elements == 0 def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : T , lowerCAmelCase__ : int): # Add an element with given priority to the queue self.heap.append((elem, weight)) SCREAMING_SNAKE_CASE_: Dict = self.elements self.elements += 1 self._bubble_up(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[Any]): # Remove and return the element with lowest weight (highest priority) if self.elements > 1: self._swap_nodes(0 , self.elements - 1) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = self.heap.pop() del self.position_map[elem] self.elements -= 1 if self.elements > 0: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = self.heap[0] self._bubble_down(lowerCAmelCase__) return elem def _SCREAMING_SNAKE_CASE ( self : int , lowerCAmelCase__ : T , lowerCAmelCase__ : int): # Update the weight of the given key SCREAMING_SNAKE_CASE_: Any = self.position_map[elem] SCREAMING_SNAKE_CASE_: Dict = (elem, weight) if position > 0: SCREAMING_SNAKE_CASE_: Tuple = get_parent_position(lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = self.heap[parent_position] if parent_weight > weight: self._bubble_up(lowerCAmelCase__) else: self._bubble_down(lowerCAmelCase__) else: self._bubble_down(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : T): # Place a node at the proper position (upward movement) [to be used internally # only] SCREAMING_SNAKE_CASE_: Optional[int] = self.position_map[elem] if curr_pos == 0: return None SCREAMING_SNAKE_CASE_: Dict = get_parent_position(lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = self.heap[curr_pos] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = self.heap[parent_position] if parent_weight > weight: self._swap_nodes(lowerCAmelCase__ , lowerCAmelCase__) return self._bubble_up(lowerCAmelCase__) return None def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : T): # Place a node at the proper position (downward movement) [to be used # internally only] SCREAMING_SNAKE_CASE_: int = self.position_map[elem] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = self.heap[curr_pos] SCREAMING_SNAKE_CASE_: Optional[Any] = get_child_left_position(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = get_child_right_position(lowerCAmelCase__) if child_left_position < self.elements and child_right_position < self.elements: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = self.heap[child_left_position] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = self.heap[child_right_position] if child_right_weight < child_left_weight and child_right_weight < weight: self._swap_nodes(lowerCAmelCase__ , lowerCAmelCase__) return self._bubble_down(lowerCAmelCase__) if child_left_position < self.elements: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self.heap[child_left_position] if child_left_weight < weight: self._swap_nodes(lowerCAmelCase__ , lowerCAmelCase__) return self._bubble_down(lowerCAmelCase__) else: return None if child_right_position < self.elements: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = self.heap[child_right_position] if child_right_weight < weight: self._swap_nodes(lowerCAmelCase__ , lowerCAmelCase__) return self._bubble_down(lowerCAmelCase__) return None def _SCREAMING_SNAKE_CASE ( self : Tuple , lowerCAmelCase__ : int , lowerCAmelCase__ : int): # Swap the nodes at the given positions SCREAMING_SNAKE_CASE_: Any = self.heap[nodea_pos][0] SCREAMING_SNAKE_CASE_: Union[str, Any] = self.heap[nodea_pos][0] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = ( self.heap[nodea_pos], self.heap[nodea_pos], ) SCREAMING_SNAKE_CASE_: Any = nodea_pos SCREAMING_SNAKE_CASE_: int = nodea_pos class __lowercase ( Generic[T] ): """simple docstring""" def __init__( self : Any): SCREAMING_SNAKE_CASE_: dict[T, dict[T, int]] = {} SCREAMING_SNAKE_CASE_: int = 0 def __repr__( self : Optional[Any]): return str(self.connections) def __len__( self : Any): return self.nodes def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : T): # Add a node in the graph if it is not in the graph if node not in self.connections: SCREAMING_SNAKE_CASE_: int = {} self.nodes += 1 def _SCREAMING_SNAKE_CASE ( self : List[Any] , lowerCAmelCase__ : T , lowerCAmelCase__ : T , lowerCAmelCase__ : int): # Add an edge between 2 nodes in the graph self.add_node(lowerCAmelCase__) self.add_node(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = weight SCREAMING_SNAKE_CASE_: List[str] = weight def A_ ( _UpperCAmelCase , ): SCREAMING_SNAKE_CASE_: dict[T, int] = {node: maxsize for node in graph.connections} SCREAMING_SNAKE_CASE_: dict[T, T | None] = {node: None for node in graph.connections} SCREAMING_SNAKE_CASE_: MinPriorityQueue[T] = MinPriorityQueue() for node, weight in dist.items(): priority_queue.push(_UpperCAmelCase , _UpperCAmelCase ) if priority_queue.is_empty(): return dist, parent # initialization SCREAMING_SNAKE_CASE_: List[str] = priority_queue.extract_min() SCREAMING_SNAKE_CASE_: Optional[Any] = 0 for neighbour in graph.connections[node]: if dist[neighbour] > dist[node] + graph.connections[node][neighbour]: SCREAMING_SNAKE_CASE_: Any = dist[node] + graph.connections[node][neighbour] priority_queue.update_key(_UpperCAmelCase , dist[neighbour] ) SCREAMING_SNAKE_CASE_: List[Any] = node # running prim's algorithm while not priority_queue.is_empty(): SCREAMING_SNAKE_CASE_: Tuple = priority_queue.extract_min() for neighbour in graph.connections[node]: if dist[neighbour] > dist[node] + graph.connections[node][neighbour]: SCREAMING_SNAKE_CASE_: List[str] = dist[node] + graph.connections[node][neighbour] priority_queue.update_key(_UpperCAmelCase , dist[neighbour] ) SCREAMING_SNAKE_CASE_: int = node return dist, parent
13
import argparse import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## lowerCAmelCase : Optional[Any] = 16 lowerCAmelCase : List[str] = 32 def A_ ( _UpperCAmelCase , _UpperCAmelCase = 16 ): SCREAMING_SNAKE_CASE_: Tuple = AutoTokenizer.from_pretrained("bert-base-cased" ) SCREAMING_SNAKE_CASE_: List[Any] = load_dataset("glue" , "mrpc" ) def tokenize_function(_UpperCAmelCase ): # max_length=None => use the model max length (it's actually the default) SCREAMING_SNAKE_CASE_: Any = tokenizer(examples["sentence1"] , examples["sentence2"] , truncation=_UpperCAmelCase , max_length=_UpperCAmelCase ) 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(): SCREAMING_SNAKE_CASE_: Tuple = datasets.map( _UpperCAmelCase , batched=_UpperCAmelCase , 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 SCREAMING_SNAKE_CASE_: Union[str, Any] = tokenized_datasets.rename_column("label" , "labels" ) def collate_fn(_UpperCAmelCase ): # On TPU it's best to pad everything to the same length or training will be very slow. SCREAMING_SNAKE_CASE_: List[str] = 1_28 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": SCREAMING_SNAKE_CASE_: List[Any] = 16 elif accelerator.mixed_precision != "no": SCREAMING_SNAKE_CASE_: Optional[Any] = 8 else: SCREAMING_SNAKE_CASE_: List[str] = None return tokenizer.pad( _UpperCAmelCase , padding="longest" , max_length=_UpperCAmelCase , pad_to_multiple_of=_UpperCAmelCase , return_tensors="pt" , ) # Instantiate dataloaders. SCREAMING_SNAKE_CASE_: Dict = DataLoader( tokenized_datasets["train"] , shuffle=_UpperCAmelCase , collate_fn=_UpperCAmelCase , batch_size=_UpperCAmelCase , drop_last=_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Union[str, Any] = DataLoader( tokenized_datasets["validation"] , shuffle=_UpperCAmelCase , collate_fn=_UpperCAmelCase , batch_size=_UpperCAmelCase , drop_last=(accelerator.mixed_precision == "fp8") , ) return train_dataloader, eval_dataloader def A_ ( _UpperCAmelCase , _UpperCAmelCase ): # Initialize accelerator SCREAMING_SNAKE_CASE_: str = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs SCREAMING_SNAKE_CASE_: int = config["lr"] SCREAMING_SNAKE_CASE_: Any = int(config["num_epochs"] ) SCREAMING_SNAKE_CASE_: Optional[int] = int(config["seed"] ) SCREAMING_SNAKE_CASE_: List[Any] = int(config["batch_size"] ) SCREAMING_SNAKE_CASE_: List[str] = evaluate.load("glue" , "mrpc" ) # If the batch size is too big we use gradient accumulation SCREAMING_SNAKE_CASE_: Optional[int] = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: SCREAMING_SNAKE_CASE_: Tuple = batch_size // MAX_GPU_BATCH_SIZE SCREAMING_SNAKE_CASE_: Dict = MAX_GPU_BATCH_SIZE set_seed(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = get_dataloaders(_UpperCAmelCase , _UpperCAmelCase ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) SCREAMING_SNAKE_CASE_: List[Any] = AutoModelForSequenceClassification.from_pretrained("bert-base-cased" , return_dict=_UpperCAmelCase ) # 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). SCREAMING_SNAKE_CASE_: Tuple = model.to(accelerator.device ) # Instantiate optimizer SCREAMING_SNAKE_CASE_: Optional[int] = AdamW(params=model.parameters() , lr=_UpperCAmelCase ) # Instantiate scheduler SCREAMING_SNAKE_CASE_: Optional[int] = get_linear_schedule_with_warmup( optimizer=_UpperCAmelCase , num_warmup_steps=1_00 , num_training_steps=(len(_UpperCAmelCase ) * num_epochs) // gradient_accumulation_steps , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = accelerator.prepare( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) # Now we train the model for epoch in range(_UpperCAmelCase ): model.train() for step, batch in enumerate(_UpperCAmelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) SCREAMING_SNAKE_CASE_: Tuple = model(**_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Optional[int] = outputs.loss SCREAMING_SNAKE_CASE_: Tuple = loss / gradient_accumulation_steps accelerator.backward(_UpperCAmelCase ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(_UpperCAmelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): SCREAMING_SNAKE_CASE_: Optional[int] = model(**_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: int = outputs.logits.argmax(dim=-1 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = accelerator.gather_for_metrics((predictions, batch["labels"]) ) metric.add_batch( predictions=_UpperCAmelCase , references=_UpperCAmelCase , ) SCREAMING_SNAKE_CASE_: List[Any] = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f"epoch {epoch}:" , _UpperCAmelCase ) def A_ ( ): SCREAMING_SNAKE_CASE_: Any = argparse.ArgumentParser(description="Simple example of training script." ) parser.add_argument( "--mixed_precision" , type=_UpperCAmelCase , default=_UpperCAmelCase , 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." ) SCREAMING_SNAKE_CASE_: Optional[Any] = parser.parse_args() SCREAMING_SNAKE_CASE_: Optional[int] = {"lr": 2e-5, "num_epochs": 3, "seed": 42, "batch_size": 16} training_function(_UpperCAmelCase , _UpperCAmelCase ) if __name__ == "__main__": main()
13
1
def A_ ( _UpperCAmelCase = 1_00_00_00 ): SCREAMING_SNAKE_CASE_: List[Any] = [i - 1 for i in range(limit + 1 )] for i in range(2 , limit + 1 ): if phi[i] == i - 1: for j in range(2 * i , limit + 1 , _UpperCAmelCase ): phi[j] -= phi[j] // i return sum(phi[2 : limit + 1] ) if __name__ == "__main__": print(solution())
13
from collections.abc import Callable class __lowercase : """simple docstring""" def __init__( self : Tuple , lowerCAmelCase__ : Callable | None = None): # Stores actual heap items. SCREAMING_SNAKE_CASE_: list = [] # Stores indexes of each item for supporting updates and deletion. SCREAMING_SNAKE_CASE_: dict = {} # Stores current size of heap. SCREAMING_SNAKE_CASE_: Optional[Any] = 0 # Stores function used to evaluate the score of an item on which basis ordering # will be done. SCREAMING_SNAKE_CASE_: Any = key or (lambda lowerCAmelCase__: x) def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : int): return int((i - 1) / 2) if i > 0 else None def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Union[str, Any] = int(2 * i + 1) return left if 0 < left < self.size else None def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Union[str, Any] = int(2 * i + 2) return right if 0 < right < self.size else None def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : int , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = ( self.pos_map[self.arr[j][0]], self.pos_map[self.arr[i][0]], ) # Then swap the items in the list. SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = self.arr[j], self.arr[i] def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : int , lowerCAmelCase__ : int): return self.arr[i][1] < self.arr[j][1] def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Any = self._left(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = self._right(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = i if left is not None and not self._cmp(lowerCAmelCase__ , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Optional[int] = left if right is not None and not self._cmp(lowerCAmelCase__ , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Tuple = right return valid_parent def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: List[Any] = self._parent(lowerCAmelCase__) while parent is not None and not self._cmp(lowerCAmelCase__ , lowerCAmelCase__): self._swap(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = parent, self._parent(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Optional[int] = self._get_valid_parent(lowerCAmelCase__) while valid_parent != index: self._swap(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = valid_parent, self._get_valid_parent(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : int , lowerCAmelCase__ : int): if item not in self.pos_map: return SCREAMING_SNAKE_CASE_: Any = self.pos_map[item] SCREAMING_SNAKE_CASE_: int = [item, self.key(lowerCAmelCase__)] # Make sure heap is right in both up and down direction. # Ideally only one of them will make any change. self._heapify_up(lowerCAmelCase__) self._heapify_down(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : int): if item not in self.pos_map: return SCREAMING_SNAKE_CASE_: Optional[Any] = self.pos_map[item] del self.pos_map[item] SCREAMING_SNAKE_CASE_: List[str] = self.arr[self.size - 1] SCREAMING_SNAKE_CASE_: Tuple = index self.size -= 1 # Make sure heap is right in both up and down direction. Ideally only one # of them will make any change- so no performance loss in calling both. if self.size > index: self._heapify_up(lowerCAmelCase__) self._heapify_down(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Optional[int] = len(self.arr) if arr_len == self.size: self.arr.append([item, self.key(lowerCAmelCase__)]) else: SCREAMING_SNAKE_CASE_: str = [item, self.key(lowerCAmelCase__)] SCREAMING_SNAKE_CASE_: List[Any] = self.size self.size += 1 self._heapify_up(self.size - 1) def _SCREAMING_SNAKE_CASE ( self : List[Any]): return self.arr[0] if self.size else None def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Dict = self.get_top() if top_item_tuple: self.delete_item(top_item_tuple[0]) return top_item_tuple def A_ ( ): pass if __name__ == "__main__": import doctest doctest.testmod()
13
1
from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel from ...schedulers import KarrasVeScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : UNetaDModel _UpperCAmelCase : KarrasVeScheduler def __init__( self : Union[str, Any] , lowerCAmelCase__ : UNetaDModel , lowerCAmelCase__ : KarrasVeScheduler): super().__init__() self.register_modules(unet=lowerCAmelCase__ , scheduler=lowerCAmelCase__) @torch.no_grad() def __call__( self : Union[str, Any] , lowerCAmelCase__ : int = 1 , lowerCAmelCase__ : int = 50 , lowerCAmelCase__ : Optional[Union[torch.Generator, List[torch.Generator]]] = None , lowerCAmelCase__ : Optional[str] = "pil" , lowerCAmelCase__ : bool = True , **lowerCAmelCase__ : Optional[int] , ): SCREAMING_SNAKE_CASE_: List[Any] = self.unet.config.sample_size SCREAMING_SNAKE_CASE_: str = (batch_size, 3, img_size, img_size) SCREAMING_SNAKE_CASE_: Union[str, Any] = self.unet # sample x_0 ~ N(0, sigma_0^2 * I) SCREAMING_SNAKE_CASE_: str = randn_tensor(lowerCAmelCase__ , generator=lowerCAmelCase__ , device=self.device) * self.scheduler.init_noise_sigma self.scheduler.set_timesteps(lowerCAmelCase__) for t in self.progress_bar(self.scheduler.timesteps): # here sigma_t == t_i from the paper SCREAMING_SNAKE_CASE_: List[str] = self.scheduler.schedule[t] SCREAMING_SNAKE_CASE_: Union[str, Any] = self.scheduler.schedule[t - 1] if t > 0 else 0 # 1. Select temporarily increased noise level sigma_hat # 2. Add new noise to move from sample_i to sample_hat SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = self.scheduler.add_noise_to_input(lowerCAmelCase__ , lowerCAmelCase__ , generator=lowerCAmelCase__) # 3. Predict the noise residual given the noise magnitude `sigma_hat` # The model inputs and output are adjusted by following eq. (213) in [1]. SCREAMING_SNAKE_CASE_: Optional[Any] = (sigma_hat / 2) * model((sample_hat + 1) / 2 , sigma_hat / 2).sample # 4. Evaluate dx/dt at sigma_hat # 5. Take Euler step from sigma to sigma_prev SCREAMING_SNAKE_CASE_: Any = self.scheduler.step(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) if sigma_prev != 0: # 6. Apply 2nd order correction # The model inputs and output are adjusted by following eq. (213) in [1]. SCREAMING_SNAKE_CASE_: Any = (sigma_prev / 2) * model((step_output.prev_sample + 1) / 2 , sigma_prev / 2).sample SCREAMING_SNAKE_CASE_: List[str] = self.scheduler.step_correct( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , step_output.prev_sample , step_output["derivative"] , ) SCREAMING_SNAKE_CASE_: Tuple = step_output.prev_sample SCREAMING_SNAKE_CASE_: Optional[int] = (sample / 2 + 0.5).clamp(0 , 1) SCREAMING_SNAKE_CASE_: Optional[Any] = sample.cpu().permute(0 , 2 , 3 , 1).numpy() if output_type == "pil": SCREAMING_SNAKE_CASE_: List[str] = self.numpy_to_pil(lowerCAmelCase__) if not return_dict: return (image,) return ImagePipelineOutput(images=lowerCAmelCase__)
13
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) if is_sentencepiece_available(): from ..ta.tokenization_ta import TaTokenizer else: from ...utils.dummy_sentencepiece_objects import TaTokenizer lowerCAmelCase : Any = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast lowerCAmelCase : Dict = TaTokenizerFast lowerCAmelCase : Optional[int] = {"""configuration_mt5""": ["""MT5Config""", """MT5OnnxConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : int = [ """MT5EncoderModel""", """MT5ForConditionalGeneration""", """MT5ForQuestionAnswering""", """MT5Model""", """MT5PreTrainedModel""", """MT5Stack""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Tuple = ["""TFMT5EncoderModel""", """TFMT5ForConditionalGeneration""", """TFMT5Model"""] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : List[str] = ["""FlaxMT5EncoderModel""", """FlaxMT5ForConditionalGeneration""", """FlaxMT5Model"""] if TYPE_CHECKING: from .configuration_mta import MTaConfig, MTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mta import ( MTaEncoderModel, MTaForConditionalGeneration, MTaForQuestionAnswering, MTaModel, MTaPreTrainedModel, MTaStack, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel else: import sys lowerCAmelCase : Optional[Any] = _LazyModule( __name__, globals()["""__file__"""], _import_structure, extra_objects={"""MT5Tokenizer""": MTaTokenizer, """MT5TokenizerFast""": MTaTokenizerFast}, module_spec=__spec__, )
13
1
def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Optional[Any] = len(_UpperCAmelCase ) for i in range(length - 1 ): SCREAMING_SNAKE_CASE_: Dict = i for k in range(i + 1 , _UpperCAmelCase ): if collection[k] < collection[least]: SCREAMING_SNAKE_CASE_: Tuple = k if least != i: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = (collection[i], collection[least]) return collection if __name__ == "__main__": lowerCAmelCase : Optional[Any] = input("""Enter numbers separated by a comma:\n""").strip() lowerCAmelCase : List[str] = [int(item) for item in user_input.split(""",""")] print(selection_sort(unsorted))
13
import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEPipeline from diffusers.pipelines.shap_e import ShapERenderer from diffusers.utils import 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 __lowercase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : List[str] = ShapEPipeline _UpperCAmelCase : Tuple = ['''prompt'''] _UpperCAmelCase : Dict = ['''prompt'''] _UpperCAmelCase : Any = [ '''num_images_per_prompt''', '''num_inference_steps''', '''generator''', '''latents''', '''guidance_scale''', '''frame_size''', '''output_type''', '''return_dict''', ] _UpperCAmelCase : Optional[int] = False @property def _SCREAMING_SNAKE_CASE ( self : List[str]): return 32 @property def _SCREAMING_SNAKE_CASE ( self : List[str]): return 32 @property def _SCREAMING_SNAKE_CASE ( self : int): return self.time_input_dim * 4 @property def _SCREAMING_SNAKE_CASE ( self : Optional[int]): return 8 @property def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: str = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip") return tokenizer @property def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Optional[int] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) return CLIPTextModelWithProjection(lowerCAmelCase__) @property def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Tuple = { "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", "encoder_hid_proj_type": None, "added_emb_type": None, } SCREAMING_SNAKE_CASE_: Any = PriorTransformer(**lowerCAmelCase__) return model @property def _SCREAMING_SNAKE_CASE ( self : Dict): torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Union[str, Any] = { "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, ), } SCREAMING_SNAKE_CASE_: Optional[int] = ShapERenderer(**lowerCAmelCase__) return model def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Dict = self.dummy_prior SCREAMING_SNAKE_CASE_: Optional[Any] = self.dummy_text_encoder SCREAMING_SNAKE_CASE_: Union[str, Any] = self.dummy_tokenizer SCREAMING_SNAKE_CASE_: List[str] = self.dummy_renderer SCREAMING_SNAKE_CASE_: Any = HeunDiscreteScheduler( beta_schedule="exp" , num_train_timesteps=1024 , prediction_type="sample" , use_karras_sigmas=lowerCAmelCase__ , clip_sample=lowerCAmelCase__ , clip_sample_range=1.0 , ) SCREAMING_SNAKE_CASE_: Optional[int] = { "prior": prior, "text_encoder": text_encoder, "tokenizer": tokenizer, "renderer": renderer, "scheduler": scheduler, } return components def _SCREAMING_SNAKE_CASE ( self : Any , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[Any]=0): if str(lowerCAmelCase__).startswith("mps"): SCREAMING_SNAKE_CASE_: Optional[Any] = torch.manual_seed(lowerCAmelCase__) else: SCREAMING_SNAKE_CASE_: Any = torch.Generator(device=lowerCAmelCase__).manual_seed(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = { "prompt": "horse", "generator": generator, "num_inference_steps": 1, "frame_size": 32, "output_type": "np", } return inputs def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: str = "cpu" SCREAMING_SNAKE_CASE_: Tuple = self.get_dummy_components() SCREAMING_SNAKE_CASE_: Dict = self.pipeline_class(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = pipe(**self.get_dummy_inputs(lowerCAmelCase__)) SCREAMING_SNAKE_CASE_: Optional[Any] = output.images[0] SCREAMING_SNAKE_CASE_: Any = image[0, -3:, -3:, -1] assert image.shape == (20, 32, 32, 3) SCREAMING_SNAKE_CASE_: Union[str, Any] = np.array( [ 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, ]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-2 def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches self._test_inference_batch_consistent(batch_sizes=[1, 2]) def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_: Dict = torch_device == "cpu" SCREAMING_SNAKE_CASE_: List[Any] = True self._test_inference_batch_single_identical( batch_size=2 , test_max_difference=lowerCAmelCase__ , relax_max_difference=lowerCAmelCase__ , ) def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Dict = self.get_dummy_components() SCREAMING_SNAKE_CASE_: str = self.pipeline_class(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = 1 SCREAMING_SNAKE_CASE_: Any = 2 SCREAMING_SNAKE_CASE_: Dict = self.get_dummy_inputs(lowerCAmelCase__) for key in inputs.keys(): if key in self.batch_params: SCREAMING_SNAKE_CASE_: List[Any] = batch_size * [inputs[key]] SCREAMING_SNAKE_CASE_: Tuple = pipe(**lowerCAmelCase__ , num_images_per_prompt=lowerCAmelCase__)[0] assert images.shape[0] == batch_size * num_images_per_prompt @slow @require_torch_gpu class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : Any): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: List[str] = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/shap_e/test_shap_e_np_out.npy") SCREAMING_SNAKE_CASE_: List[str] = ShapEPipeline.from_pretrained("openai/shap-e") SCREAMING_SNAKE_CASE_: Optional[int] = pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = torch.Generator(device=lowerCAmelCase__).manual_seed(0) SCREAMING_SNAKE_CASE_: int = pipe( "a shark" , generator=lowerCAmelCase__ , guidance_scale=15.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__)
13
1
from __future__ import annotations from collections import Counter from random import random class __lowercase : """simple docstring""" def __init__( self : List[str]): SCREAMING_SNAKE_CASE_: Optional[Any] = {} def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : str): SCREAMING_SNAKE_CASE_: Union[str, Any] = {} def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : str , lowerCAmelCase__ : str , lowerCAmelCase__ : float): if nodea not in self.connections: self.add_node(lowerCAmelCase__) if nodea not in self.connections: self.add_node(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = probability def _SCREAMING_SNAKE_CASE ( self : List[str]): return list(self.connections) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : str): SCREAMING_SNAKE_CASE_: Dict = 0 SCREAMING_SNAKE_CASE_: Tuple = random() for dest in self.connections[node]: current_probability += self.connections[node][dest] if current_probability > random_value: return dest return "" def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: List[Any] = MarkovChainGraphUndirectedUnweighted() for nodea, nodea, probability in transitions: graph.add_transition_probability(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Tuple = Counter(graph.get_nodes() ) SCREAMING_SNAKE_CASE_: Optional[Any] = start for _ in range(_UpperCAmelCase ): SCREAMING_SNAKE_CASE_: int = graph.transition(_UpperCAmelCase ) visited[node] += 1 return visited if __name__ == "__main__": import doctest doctest.testmod()
13
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
13
1
import argparse import json import os import fairseq import torch from fairseq.data import Dictionary # Register SEW's fairseq modules from sew_asapp import tasks # noqa: F401 from transformers import ( SEWConfig, SEWForCTC, SEWModel, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaProcessor, logging, ) logging.set_verbosity_info() lowerCAmelCase : List[str] = logging.get_logger(__name__) lowerCAmelCase : Optional[Any] = { """post_extract_proj""": """feature_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.upsample.0""": """encoder.upsample.projection""", """encoder.layer_norm""": """encoder.layer_norm""", """w2v_model.layer_norm""": """layer_norm""", """w2v_encoder.proj""": """lm_head""", """mask_emb""": """masked_spec_embed""", } def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): for attribute in key.split("." ): SCREAMING_SNAKE_CASE_: Optional[Any] = getattr(_UpperCAmelCase , _UpperCAmelCase ) if weight_type is not None: SCREAMING_SNAKE_CASE_: Optional[int] = getattr(_UpperCAmelCase , _UpperCAmelCase ).shape else: SCREAMING_SNAKE_CASE_: List[str] = hf_pointer.shape assert hf_shape == value.shape, ( f"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be" f" {value.shape} for {full_name}" ) if weight_type == "weight": SCREAMING_SNAKE_CASE_: Optional[Any] = value elif weight_type == "weight_g": SCREAMING_SNAKE_CASE_: int = value elif weight_type == "weight_v": SCREAMING_SNAKE_CASE_: Any = value elif weight_type == "bias": SCREAMING_SNAKE_CASE_: Optional[int] = value else: SCREAMING_SNAKE_CASE_: List[Any] = value logger.info(f"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." ) def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Union[str, Any] = [] SCREAMING_SNAKE_CASE_: int = fairseq_model.state_dict() SCREAMING_SNAKE_CASE_: Tuple = hf_model.sew.feature_extractor if is_finetuned else hf_model.feature_extractor for name, value in fairseq_dict.items(): SCREAMING_SNAKE_CASE_: Optional[int] = False if "conv_layers" in name: load_conv_layer( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , hf_model.config.feat_extract_norm == "group" , ) SCREAMING_SNAKE_CASE_: Any = True else: for key, mapped_key in MAPPING.items(): SCREAMING_SNAKE_CASE_: Dict = "sew." + mapped_key if (is_finetuned and mapped_key != "lm_head") else mapped_key if key in name or key.split("w2v_model." )[-1] == name.split("." )[0]: SCREAMING_SNAKE_CASE_: List[Any] = True if "*" in mapped_key: SCREAMING_SNAKE_CASE_: Any = name.split(_UpperCAmelCase )[0].split("." )[-2] SCREAMING_SNAKE_CASE_: Tuple = mapped_key.replace("*" , _UpperCAmelCase ) if "weight_g" in name: SCREAMING_SNAKE_CASE_: Tuple = "weight_g" elif "weight_v" in name: SCREAMING_SNAKE_CASE_: Optional[int] = "weight_v" elif "weight" in name: SCREAMING_SNAKE_CASE_: Optional[int] = "weight" elif "bias" in name: SCREAMING_SNAKE_CASE_: Any = "bias" else: SCREAMING_SNAKE_CASE_: Tuple = None set_recursively(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) continue if not is_used: unused_weights.append(_UpperCAmelCase ) logger.warning(f"Unused weights: {unused_weights}" ) def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Optional[int] = full_name.split("conv_layers." )[-1] SCREAMING_SNAKE_CASE_: List[Any] = name.split("." ) SCREAMING_SNAKE_CASE_: int = int(items[0] ) SCREAMING_SNAKE_CASE_: str = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( f"{full_name} has size {value.shape}, but" f" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found." ) SCREAMING_SNAKE_CASE_: 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." ) SCREAMING_SNAKE_CASE_: Optional[Any] = value logger.info(f"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( f"{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was" " found." ) SCREAMING_SNAKE_CASE_: int = value logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( f"{full_name} has size {value.shape}, but" f" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found." ) SCREAMING_SNAKE_CASE_: Tuple = value logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) else: unused_weights.append(_UpperCAmelCase ) def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = SEWConfig() if is_finetuned: SCREAMING_SNAKE_CASE_: str = model.wav_encoder.wav_model.cfg else: SCREAMING_SNAKE_CASE_: Union[str, Any] = model.cfg SCREAMING_SNAKE_CASE_: int = fs_config.conv_bias SCREAMING_SNAKE_CASE_: List[str] = eval(fs_config.conv_feature_layers ) SCREAMING_SNAKE_CASE_: int = [x[0] for x in conv_layers] SCREAMING_SNAKE_CASE_: str = [x[1] for x in conv_layers] SCREAMING_SNAKE_CASE_: Union[str, Any] = [x[2] for x in conv_layers] SCREAMING_SNAKE_CASE_: int = "gelu" SCREAMING_SNAKE_CASE_: Dict = "layer" if fs_config.extractor_mode == "layer_norm" else "group" SCREAMING_SNAKE_CASE_: Union[str, Any] = 0.0 SCREAMING_SNAKE_CASE_: Optional[int] = fs_config.activation_fn.name SCREAMING_SNAKE_CASE_: Tuple = fs_config.encoder_embed_dim SCREAMING_SNAKE_CASE_: List[str] = 0.0_2 SCREAMING_SNAKE_CASE_: List[Any] = fs_config.encoder_ffn_embed_dim SCREAMING_SNAKE_CASE_: List[str] = 1e-5 SCREAMING_SNAKE_CASE_: Tuple = fs_config.encoder_layerdrop SCREAMING_SNAKE_CASE_: Dict = fs_config.encoder_attention_heads SCREAMING_SNAKE_CASE_: Optional[Any] = fs_config.conv_pos_groups SCREAMING_SNAKE_CASE_: Any = fs_config.conv_pos SCREAMING_SNAKE_CASE_: Tuple = len(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: int = fs_config.encoder_layers SCREAMING_SNAKE_CASE_: List[str] = fs_config.squeeze_factor # take care of any params that are overridden by the Wav2VecCtc model if is_finetuned: SCREAMING_SNAKE_CASE_: Any = model.cfg SCREAMING_SNAKE_CASE_: Tuple = fs_config.final_dropout SCREAMING_SNAKE_CASE_: List[str] = fs_config.layerdrop SCREAMING_SNAKE_CASE_: Union[str, Any] = fs_config.activation_dropout SCREAMING_SNAKE_CASE_: str = fs_config.mask_prob > 0 or fs_config.mask_channel_prob > 0 SCREAMING_SNAKE_CASE_: Dict = fs_config.attention_dropout SCREAMING_SNAKE_CASE_: int = fs_config.dropout_input SCREAMING_SNAKE_CASE_: Optional[int] = fs_config.dropout SCREAMING_SNAKE_CASE_: Optional[int] = fs_config.mask_channel_length SCREAMING_SNAKE_CASE_: List[Any] = fs_config.mask_channel_prob SCREAMING_SNAKE_CASE_: str = fs_config.mask_length SCREAMING_SNAKE_CASE_: Any = fs_config.mask_prob SCREAMING_SNAKE_CASE_: List[str] = "Wav2Vec2FeatureExtractor" SCREAMING_SNAKE_CASE_: Optional[int] = "Wav2Vec2CTCTokenizer" return config @torch.no_grad() def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=True ): if is_finetuned: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"data": "/".join(dict_path.split("/" )[:-1] )} ) else: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] ) if config_path is not None: SCREAMING_SNAKE_CASE_: int = SEWConfig.from_pretrained(_UpperCAmelCase ) else: SCREAMING_SNAKE_CASE_: int = convert_config(model[0] , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[str] = model[0].eval() SCREAMING_SNAKE_CASE_: str = True if config.feat_extract_norm == "layer" else False SCREAMING_SNAKE_CASE_: Tuple = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_60_00 , padding_value=0 , do_normalize=_UpperCAmelCase , return_attention_mask=_UpperCAmelCase , ) if is_finetuned: if dict_path: SCREAMING_SNAKE_CASE_: Any = Dictionary.load(_UpperCAmelCase ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq SCREAMING_SNAKE_CASE_: Optional[int] = target_dict.pad_index SCREAMING_SNAKE_CASE_: Any = target_dict.bos_index SCREAMING_SNAKE_CASE_: Any = target_dict.pad_index SCREAMING_SNAKE_CASE_: str = target_dict.bos_index SCREAMING_SNAKE_CASE_: Union[str, Any] = target_dict.eos_index SCREAMING_SNAKE_CASE_: str = len(target_dict.symbols ) SCREAMING_SNAKE_CASE_: List[Any] = os.path.join(_UpperCAmelCase , "vocab.json" ) if not os.path.isdir(_UpperCAmelCase ): logger.error("--pytorch_dump_folder_path ({}) should be a directory".format(_UpperCAmelCase ) ) return os.makedirs(_UpperCAmelCase , exist_ok=_UpperCAmelCase ) with open(_UpperCAmelCase , "w" , encoding="utf-8" ) as vocab_handle: json.dump(target_dict.indices , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: int = WavaVecaCTCTokenizer( _UpperCAmelCase , unk_token=target_dict.unk_word , pad_token=target_dict.pad_word , bos_token=target_dict.bos_word , eos_token=target_dict.eos_word , word_delimiter_token="|" , do_lower_case=_UpperCAmelCase , ) SCREAMING_SNAKE_CASE_: Dict = WavaVecaProcessor(feature_extractor=_UpperCAmelCase , tokenizer=_UpperCAmelCase ) processor.save_pretrained(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Any = SEWForCTC(_UpperCAmelCase ) else: SCREAMING_SNAKE_CASE_: Tuple = SEWModel(_UpperCAmelCase ) feature_extractor.save_pretrained(_UpperCAmelCase ) recursively_load_weights(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) hf_model.save_pretrained(_UpperCAmelCase ) if __name__ == "__main__": lowerCAmelCase : List[Any] = argparse.ArgumentParser() parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument("""--checkpoint_path""", default=None, type=str, help="""Path to fairseq checkpoint""") parser.add_argument("""--dict_path""", default=None, type=str, help="""Path to dict of fine-tuned model""") parser.add_argument("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""") parser.add_argument( """--is_finetuned""", action="""store_true""", help="""Whether the model to convert is a fine-tuned model or not""" ) lowerCAmelCase : Any = parser.parse_args() convert_sew_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, args.is_finetuned )
13
class __lowercase : """simple docstring""" def __init__( self : List[Any] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[Any]): SCREAMING_SNAKE_CASE_: List[str] = name SCREAMING_SNAKE_CASE_: Union[str, Any] = val def __str__( self : Dict): return F"{self.__class__.__name__}({self.name}, {self.val})" def __lt__( self : List[str] , lowerCAmelCase__ : Any): return self.val < other.val class __lowercase : """simple docstring""" def __init__( self : Tuple , lowerCAmelCase__ : Dict): SCREAMING_SNAKE_CASE_: str = {} SCREAMING_SNAKE_CASE_: int = {} SCREAMING_SNAKE_CASE_: Any = self.build_heap(lowerCAmelCase__) def __getitem__( self : List[Any] , lowerCAmelCase__ : Dict): return self.get_value(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : Dict): return (idx - 1) // 2 def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Optional[Any]): return idx * 2 + 1 def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Tuple): return idx * 2 + 2 def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Optional[int]): return self.heap_dict[key] def _SCREAMING_SNAKE_CASE ( self : List[Any] , lowerCAmelCase__ : Union[str, Any]): SCREAMING_SNAKE_CASE_: Tuple = len(lowerCAmelCase__) - 1 SCREAMING_SNAKE_CASE_: List[str] = self.get_parent_idx(lowerCAmelCase__) for idx, i in enumerate(lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Union[str, Any] = idx SCREAMING_SNAKE_CASE_: str = i.val for i in range(lowerCAmelCase__ , -1 , -1): self.sift_down(lowerCAmelCase__ , lowerCAmelCase__) return array def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[str]): while True: SCREAMING_SNAKE_CASE_: Optional[Any] = self.get_left_child_idx(lowerCAmelCase__) # noqa: E741 SCREAMING_SNAKE_CASE_: Dict = self.get_right_child_idx(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = idx if l < len(lowerCAmelCase__) and array[l] < array[idx]: SCREAMING_SNAKE_CASE_: List[str] = l if r < len(lowerCAmelCase__) and array[r] < array[smallest]: SCREAMING_SNAKE_CASE_: str = r if smallest != idx: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = array[smallest], array[idx] ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ): Optional[Any] = ( self.idx_of_element[array[smallest]], self.idx_of_element[array[idx]], ) SCREAMING_SNAKE_CASE_: Optional[int] = smallest else: break def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : str): SCREAMING_SNAKE_CASE_: Any = self.get_parent_idx(lowerCAmelCase__) while p >= 0 and self.heap[p] > self.heap[idx]: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = self.heap[idx], self.heap[p] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = ( self.idx_of_element[self.heap[idx]], self.idx_of_element[self.heap[p]], ) SCREAMING_SNAKE_CASE_: Union[str, Any] = p SCREAMING_SNAKE_CASE_: Optional[int] = self.get_parent_idx(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[Any]): return self.heap[0] def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self.heap[-1], self.heap[0] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = ( self.idx_of_element[self.heap[-1]], self.idx_of_element[self.heap[0]], ) SCREAMING_SNAKE_CASE_: int = self.heap.pop() del self.idx_of_element[x] self.sift_down(0 , self.heap) return x def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Tuple): self.heap.append(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = len(self.heap) - 1 SCREAMING_SNAKE_CASE_: List[str] = node.val self.sift_up(len(self.heap) - 1) def _SCREAMING_SNAKE_CASE ( self : List[Any]): return len(self.heap) == 0 def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Optional[int]): assert ( self.heap[self.idx_of_element[node]].val > new_value ), "newValue must be less that current value" SCREAMING_SNAKE_CASE_: Any = new_value SCREAMING_SNAKE_CASE_: Tuple = new_value self.sift_up(self.idx_of_element[node]) lowerCAmelCase : int = Node("""R""", -1) lowerCAmelCase : str = Node("""B""", 6) lowerCAmelCase : str = Node("""A""", 3) lowerCAmelCase : List[str] = Node("""X""", 1) lowerCAmelCase : Union[str, Any] = Node("""E""", 4) # Use one of these two ways to generate Min-Heap # Generating Min-Heap from array lowerCAmelCase : Optional[Any] = MinHeap([r, b, a, x, e]) # Generating Min-Heap by Insert method # myMinHeap.insert(a) # myMinHeap.insert(b) # myMinHeap.insert(x) # myMinHeap.insert(r) # myMinHeap.insert(e) # Before print("""Min Heap - before decrease key""") for i in my_min_heap.heap: print(i) print("""Min Heap - After decrease key of node [B -> -17]""") my_min_heap.decrease_key(b, -17) # After for i in my_min_heap.heap: print(i) if __name__ == "__main__": import doctest doctest.testmod()
13
1
def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Union[str, Any] = word.split() def justify(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) -> str: SCREAMING_SNAKE_CASE_: Dict = max_width - width SCREAMING_SNAKE_CASE_: str = len(_UpperCAmelCase ) if len(_UpperCAmelCase ) == 1: # if there is only word in line # just insert overall_spaces_count for the remainder of line return line[0] + " " * overall_spaces_count else: SCREAMING_SNAKE_CASE_: List[Any] = words_count - 1 # num_spaces_between_words_list[i] : tells you to insert # num_spaces_between_words_list[i] spaces # after word on line[i] SCREAMING_SNAKE_CASE_: List[Any] = spaces_to_insert_between_words * [ overall_spaces_count // spaces_to_insert_between_words ] SCREAMING_SNAKE_CASE_: Tuple = ( overall_spaces_count % spaces_to_insert_between_words ) # distribute spaces via round robin to the left words for i in range(_UpperCAmelCase ): num_spaces_between_words_list[i] += 1 SCREAMING_SNAKE_CASE_: Optional[int] = [] for i in range(_UpperCAmelCase ): # add the word aligned_words_list.append(line[i] ) # add the spaces to insert aligned_words_list.append(num_spaces_between_words_list[i] * " " ) # just add the last word to the sentence aligned_words_list.append(line[-1] ) # join the aligned words list to form a justified line return "".join(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Any = [] SCREAMING_SNAKE_CASE_: list[str] = [] SCREAMING_SNAKE_CASE_: Tuple = 0 for word in words: if width + len(_UpperCAmelCase ) + len(_UpperCAmelCase ) <= max_width: # keep adding words until we can fill out max_width # width = sum of length of all words (without overall_spaces_count) # len(word) = length of current word # len(line) = number of overall_spaces_count to insert between words line.append(_UpperCAmelCase ) width += len(_UpperCAmelCase ) else: # justify the line and add it to result answer.append(justify(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) ) # reset new line and new width SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = [word], len(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[Any] = max_width - width - len(_UpperCAmelCase ) answer.append(" ".join(_UpperCAmelCase ) + (remaining_spaces + 1) * " " ) return answer if __name__ == "__main__": from doctest import testmod testmod()
13
import random import unittest import numpy as np import transformers from transformers import is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax if is_flax_available(): import os import jax.numpy as jnp from jax import jit from transformers import AutoTokenizer, FlaxAutoModelForCausalLM from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model lowerCAmelCase : Any = """0.12""" # assumed parallelism: 8 if is_torch_available(): import torch def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=None ): if rng is None: SCREAMING_SNAKE_CASE_: List[Any] = random.Random() SCREAMING_SNAKE_CASE_: Optional[Any] = 1 for dim in shape: total_dims *= dim SCREAMING_SNAKE_CASE_: Optional[Any] = [] for _ in range(_UpperCAmelCase ): values.append(rng.randint(0 , vocab_size - 1 ) ) SCREAMING_SNAKE_CASE_: List[Any] = np.array(_UpperCAmelCase , dtype=jnp.intaa ).reshape(_UpperCAmelCase ) return output def A_ ( _UpperCAmelCase , _UpperCAmelCase=None ): SCREAMING_SNAKE_CASE_: Optional[int] = ids_tensor(_UpperCAmelCase , vocab_size=2 , rng=_UpperCAmelCase ) # make sure that at least one token is attended to for each batch SCREAMING_SNAKE_CASE_: Optional[Any] = 1 return attn_mask @require_flax class __lowercase : """simple docstring""" _UpperCAmelCase : Any = None _UpperCAmelCase : List[Any] = () def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self.model_tester.prepare_config_and_inputs_for_common() # cut to half length & take max batch_size 3 SCREAMING_SNAKE_CASE_: str = 2 SCREAMING_SNAKE_CASE_: Optional[int] = inputs["input_ids"].shape[-1] // 2 SCREAMING_SNAKE_CASE_: List[str] = inputs["input_ids"][:max_batch_size, :sequence_length] SCREAMING_SNAKE_CASE_: Any = jnp.ones_like(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = attention_mask[:max_batch_size, :sequence_length] # generate max 5 tokens SCREAMING_SNAKE_CASE_: Optional[Any] = input_ids.shape[-1] + 5 if config.eos_token_id is not None and config.pad_token_id is None: # hack to allow generate for models such as GPT2 as is done in `generate()` SCREAMING_SNAKE_CASE_: Optional[Any] = config.eos_token_id return config, input_ids, attention_mask, max_length @is_pt_flax_cross_test def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Union[str, Any] = False SCREAMING_SNAKE_CASE_: Dict = max_length SCREAMING_SNAKE_CASE_: List[Any] = 0 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: int = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = model_class.__name__[4:] # Skip the "Flax" at the beginning SCREAMING_SNAKE_CASE_: List[Any] = getattr(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = pt_model_class(lowerCAmelCase__).eval() SCREAMING_SNAKE_CASE_: str = load_flax_weights_in_pytorch_model(lowerCAmelCase__ , flax_model.params) SCREAMING_SNAKE_CASE_: List[Any] = flax_model.generate(lowerCAmelCase__).sequences SCREAMING_SNAKE_CASE_: str = pt_model.generate(torch.tensor(lowerCAmelCase__ , dtype=torch.long)) if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]: SCREAMING_SNAKE_CASE_: List[Any] = flax_generation_outputs[:, : pt_generation_outputs.shape[-1]] self.assertListEqual(pt_generation_outputs.numpy().tolist() , flax_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Optional[int] = False SCREAMING_SNAKE_CASE_: Optional[int] = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Union[str, Any] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = jit(model.generate) SCREAMING_SNAKE_CASE_: Union[str, Any] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Optional[Any] = True SCREAMING_SNAKE_CASE_: Dict = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Tuple = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = jit(model.generate) SCREAMING_SNAKE_CASE_: Dict = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: int = False SCREAMING_SNAKE_CASE_: Optional[int] = max_length SCREAMING_SNAKE_CASE_: Optional[int] = 2 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: List[str] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = jit(model.generate) SCREAMING_SNAKE_CASE_: Optional[int] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: str = False SCREAMING_SNAKE_CASE_: int = max_length SCREAMING_SNAKE_CASE_: str = 2 SCREAMING_SNAKE_CASE_: Optional[Any] = 2 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: str = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[0] , input_ids.shape[0] * config.num_return_sequences) def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Tuple = True SCREAMING_SNAKE_CASE_: List[str] = max_length SCREAMING_SNAKE_CASE_: Any = 0.8 SCREAMING_SNAKE_CASE_: Any = 10 SCREAMING_SNAKE_CASE_: List[str] = 0.3 SCREAMING_SNAKE_CASE_: Tuple = 1 SCREAMING_SNAKE_CASE_: Union[str, Any] = 8 SCREAMING_SNAKE_CASE_: int = 9 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: List[str] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = jit(model.generate) SCREAMING_SNAKE_CASE_: List[Any] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Any = max_length SCREAMING_SNAKE_CASE_: int = 1 SCREAMING_SNAKE_CASE_: Union[str, Any] = 8 SCREAMING_SNAKE_CASE_: List[Any] = 9 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: int = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = jit(model.generate) SCREAMING_SNAKE_CASE_: List[str] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Any = max_length SCREAMING_SNAKE_CASE_: List[str] = 2 SCREAMING_SNAKE_CASE_: str = 1 SCREAMING_SNAKE_CASE_: Tuple = 8 SCREAMING_SNAKE_CASE_: List[Any] = 9 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Optional[int] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = jit(model.generate) SCREAMING_SNAKE_CASE_: List[str] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self._get_input_ids_and_config() # pad attention mask on the left SCREAMING_SNAKE_CASE_: Dict = attention_mask.at[(0, 0)].set(0) SCREAMING_SNAKE_CASE_: Dict = False SCREAMING_SNAKE_CASE_: Optional[int] = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Any = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = model.generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = jit(model.generate) SCREAMING_SNAKE_CASE_: List[Any] = jit_generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = self._get_input_ids_and_config() # pad attention mask on the left SCREAMING_SNAKE_CASE_: List[Any] = attention_mask.at[(0, 0)].set(0) SCREAMING_SNAKE_CASE_: Optional[int] = True SCREAMING_SNAKE_CASE_: Union[str, Any] = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: str = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = model.generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = jit(model.generate) SCREAMING_SNAKE_CASE_: Optional[Any] = jit_generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self._get_input_ids_and_config() # pad attention mask on the left SCREAMING_SNAKE_CASE_: Dict = attention_mask.at[(0, 0)].set(0) SCREAMING_SNAKE_CASE_: Optional[Any] = 2 SCREAMING_SNAKE_CASE_: Any = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Tuple = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = model.generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = jit(model.generate) SCREAMING_SNAKE_CASE_: Union[str, Any] = jit_generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) @require_flax class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: Tuple = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-bert") SCREAMING_SNAKE_CASE_: List[Any] = FlaxAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-bert-flax-only") SCREAMING_SNAKE_CASE_: Optional[int] = "Hello world" SCREAMING_SNAKE_CASE_: List[Any] = tokenizer(lowerCAmelCase__ , return_tensors="np").input_ids # typos are quickly detected (the correct argument is `do_sample`) with self.assertRaisesRegex(lowerCAmelCase__ , "do_samples"): model.generate(lowerCAmelCase__ , do_samples=lowerCAmelCase__) # arbitrary arguments that will not be used anywhere are also not accepted with self.assertRaisesRegex(lowerCAmelCase__ , "foo"): SCREAMING_SNAKE_CASE_: str = {"foo": "bar"} model.generate(lowerCAmelCase__ , **lowerCAmelCase__)
13
1
import doctest import logging import os import unittest from pathlib import Path from typing import List, Union import transformers from transformers.testing_utils import require_tf, require_torch, slow lowerCAmelCase : Optional[Any] = logging.getLogger() @unittest.skip('''Temporarily disable the doc tests.''' ) @require_torch @require_tf @slow class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Path , lowerCAmelCase__ : Union[str, None] = None , lowerCAmelCase__ : Union[List[str], None] = None , lowerCAmelCase__ : Union[str, List[str], None] = None , lowerCAmelCase__ : bool = True , ): SCREAMING_SNAKE_CASE_: Optional[Any] = [file for file in os.listdir(lowerCAmelCase__) if os.path.isfile(os.path.join(lowerCAmelCase__ , lowerCAmelCase__))] if identifier is not None: SCREAMING_SNAKE_CASE_: List[Any] = [file for file in files if identifier in file] if n_identifier is not None: if isinstance(lowerCAmelCase__ , lowerCAmelCase__): for n_ in n_identifier: SCREAMING_SNAKE_CASE_: Any = [file for file in files if n_ not in file] else: SCREAMING_SNAKE_CASE_: Dict = [file for file in files if n_identifier not in file] SCREAMING_SNAKE_CASE_: List[Any] = ignore_files or [] ignore_files.append("__init__.py") SCREAMING_SNAKE_CASE_: List[str] = [file for file in files if file not in ignore_files] for file in files: # Open all files print("Testing" , lowerCAmelCase__) if only_modules: SCREAMING_SNAKE_CASE_: List[str] = file.split(".")[0] try: SCREAMING_SNAKE_CASE_: Optional[int] = getattr(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = doctest.DocTestSuite(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = unittest.TextTestRunner().run(lowerCAmelCase__) self.assertIs(len(result.failures) , 0) except AttributeError: logger.info(F"{module_identifier} is not a module.") else: SCREAMING_SNAKE_CASE_: Tuple = doctest.testfile(str(".." / directory / file) , optionflags=doctest.ELLIPSIS) self.assertIs(result.failed , 0) def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: str = Path("src/transformers") SCREAMING_SNAKE_CASE_: Union[str, Any] = "modeling" SCREAMING_SNAKE_CASE_: Union[str, Any] = [ "modeling_ctrl.py", "modeling_tf_ctrl.py", ] self.analyze_directory(lowerCAmelCase__ , identifier=lowerCAmelCase__ , ignore_files=lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: List[Any] = Path("src/transformers") SCREAMING_SNAKE_CASE_: List[str] = "tokenization" self.analyze_directory(lowerCAmelCase__ , identifier=lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: str = Path("src/transformers") SCREAMING_SNAKE_CASE_: Optional[int] = "configuration" self.analyze_directory(lowerCAmelCase__ , identifier=lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Optional[int] = Path("src/transformers") SCREAMING_SNAKE_CASE_: int = ["configuration", "modeling", "tokenization"] self.analyze_directory(lowerCAmelCase__ , n_identifier=lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Optional[int] = Path("docs/source") SCREAMING_SNAKE_CASE_: str = ["favicon.ico"] self.analyze_directory(lowerCAmelCase__ , ignore_files=lowerCAmelCase__ , only_modules=lowerCAmelCase__)
13
import argparse import re import torch from CLAP import create_model from transformers import AutoFeatureExtractor, ClapConfig, ClapModel lowerCAmelCase : Union[str, Any] = { """text_branch""": """text_model""", """audio_branch""": """audio_model.audio_encoder""", """attn""": """attention.self""", """self.proj""": """output.dense""", """attention.self_mask""": """attn_mask""", """mlp.fc1""": """intermediate.dense""", """mlp.fc2""": """output.dense""", """norm1""": """layernorm_before""", """norm2""": """layernorm_after""", """bn0""": """batch_norm""", } lowerCAmelCase : int = AutoFeatureExtractor.from_pretrained("""laion/clap-htsat-unfused""", truncation="""rand_trunc""") def A_ ( _UpperCAmelCase , _UpperCAmelCase=False ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = create_model( "HTSAT-tiny" , "roberta" , _UpperCAmelCase , precision="fp32" , device="cuda:0" if torch.cuda.is_available() else "cpu" , enable_fusion=_UpperCAmelCase , fusion_type="aff_2d" if enable_fusion else None , ) return model, model_cfg def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = {} SCREAMING_SNAKE_CASE_: Tuple = R".*sequential.(\d+).*" SCREAMING_SNAKE_CASE_: Dict = R".*_projection.(\d+).*" for key, value in state_dict.items(): # check if any key needs to be modified for key_to_modify, new_key in KEYS_TO_MODIFY_MAPPING.items(): if key_to_modify in key: SCREAMING_SNAKE_CASE_: Any = key.replace(_UpperCAmelCase , _UpperCAmelCase ) if re.match(_UpperCAmelCase , _UpperCAmelCase ): # replace sequential layers with list SCREAMING_SNAKE_CASE_: Optional[int] = re.match(_UpperCAmelCase , _UpperCAmelCase ).group(1 ) SCREAMING_SNAKE_CASE_: Dict = key.replace(f"sequential.{sequential_layer}." , f"layers.{int(_UpperCAmelCase )//3}.linear." ) elif re.match(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = int(re.match(_UpperCAmelCase , _UpperCAmelCase ).group(1 ) ) # Because in CLAP they use `nn.Sequential`... SCREAMING_SNAKE_CASE_: Optional[int] = 1 if projecton_layer == 0 else 2 SCREAMING_SNAKE_CASE_: Dict = key.replace(f"_projection.{projecton_layer}." , f"_projection.linear{transformers_projection_layer}." ) if "audio" and "qkv" in key: # split qkv into query key and value SCREAMING_SNAKE_CASE_: Tuple = value SCREAMING_SNAKE_CASE_: List[str] = mixed_qkv.size(0 ) // 3 SCREAMING_SNAKE_CASE_: Any = mixed_qkv[:qkv_dim] SCREAMING_SNAKE_CASE_: Optional[int] = mixed_qkv[qkv_dim : qkv_dim * 2] SCREAMING_SNAKE_CASE_: Optional[Any] = mixed_qkv[qkv_dim * 2 :] SCREAMING_SNAKE_CASE_: str = query_layer SCREAMING_SNAKE_CASE_: int = key_layer SCREAMING_SNAKE_CASE_: List[Any] = value_layer else: SCREAMING_SNAKE_CASE_: int = value return model_state_dict def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=False ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = init_clap(_UpperCAmelCase , enable_fusion=_UpperCAmelCase ) clap_model.eval() SCREAMING_SNAKE_CASE_: Union[str, Any] = clap_model.state_dict() SCREAMING_SNAKE_CASE_: Optional[int] = rename_state_dict(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[str] = ClapConfig() SCREAMING_SNAKE_CASE_: Tuple = enable_fusion SCREAMING_SNAKE_CASE_: Tuple = ClapModel(_UpperCAmelCase ) # ignore the spectrogram embedding layer model.load_state_dict(_UpperCAmelCase , strict=_UpperCAmelCase ) model.save_pretrained(_UpperCAmelCase ) transformers_config.save_pretrained(_UpperCAmelCase ) if __name__ == "__main__": lowerCAmelCase : Tuple = 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("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""") parser.add_argument("""--enable_fusion""", action="""store_true""", help="""Whether to enable fusion or not""") lowerCAmelCase : int = parser.parse_args() convert_clap_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.enable_fusion)
13
1
def A_ ( _UpperCAmelCase ): if length <= 0 or not isinstance(_UpperCAmelCase , _UpperCAmelCase ): raise ValueError("Length must be a positive integer." ) return [n * (2 * n - 1) for n in range(_UpperCAmelCase )] if __name__ == "__main__": print(hexagonal_numbers(length=5)) print(hexagonal_numbers(length=10))
13
import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_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, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __lowercase : """simple docstring""" def __init__( self : Any , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Any=13 , lowerCAmelCase__ : Tuple=30 , lowerCAmelCase__ : List[str]=2 , lowerCAmelCase__ : int=3 , lowerCAmelCase__ : Optional[int]=True , lowerCAmelCase__ : List[str]=True , lowerCAmelCase__ : str=32 , lowerCAmelCase__ : Any=5 , lowerCAmelCase__ : str=4 , lowerCAmelCase__ : int=37 , lowerCAmelCase__ : Optional[Any]="gelu" , lowerCAmelCase__ : Optional[int]=0.1 , lowerCAmelCase__ : Dict=0.1 , lowerCAmelCase__ : Tuple=10 , lowerCAmelCase__ : Optional[Any]=0.02 , lowerCAmelCase__ : List[str]=None , lowerCAmelCase__ : Union[str, Any]=2 , ): SCREAMING_SNAKE_CASE_: str = parent SCREAMING_SNAKE_CASE_: Optional[Any] = batch_size SCREAMING_SNAKE_CASE_: str = image_size SCREAMING_SNAKE_CASE_: Tuple = patch_size SCREAMING_SNAKE_CASE_: int = num_channels SCREAMING_SNAKE_CASE_: List[str] = is_training SCREAMING_SNAKE_CASE_: str = use_labels SCREAMING_SNAKE_CASE_: int = hidden_size SCREAMING_SNAKE_CASE_: List[Any] = num_hidden_layers SCREAMING_SNAKE_CASE_: Union[str, Any] = num_attention_heads SCREAMING_SNAKE_CASE_: Any = intermediate_size SCREAMING_SNAKE_CASE_: str = hidden_act SCREAMING_SNAKE_CASE_: str = hidden_dropout_prob SCREAMING_SNAKE_CASE_: List[str] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_: int = type_sequence_label_size SCREAMING_SNAKE_CASE_: Dict = initializer_range SCREAMING_SNAKE_CASE_: Dict = scope SCREAMING_SNAKE_CASE_: Dict = encoder_stride # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) SCREAMING_SNAKE_CASE_: List[Any] = (image_size // patch_size) ** 2 SCREAMING_SNAKE_CASE_: Dict = num_patches + 1 def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]) SCREAMING_SNAKE_CASE_: str = None if self.use_labels: SCREAMING_SNAKE_CASE_: Union[str, Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size) SCREAMING_SNAKE_CASE_: Optional[Any] = self.get_config() return config, pixel_values, labels def _SCREAMING_SNAKE_CASE ( self : Optional[int]): return ViTConfig( 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 , encoder_stride=self.encoder_stride , ) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : Tuple): SCREAMING_SNAKE_CASE_: Union[str, Any] = ViTModel(config=lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: Optional[int] = model(lowerCAmelCase__) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Dict): SCREAMING_SNAKE_CASE_: Optional[int] = ViTForMaskedImageModeling(config=lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: str = model(lowerCAmelCase__) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size)) # test greyscale images SCREAMING_SNAKE_CASE_: Dict = 1 SCREAMING_SNAKE_CASE_: List[str] = ViTForMaskedImageModeling(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: List[str] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]) SCREAMING_SNAKE_CASE_: str = model(lowerCAmelCase__) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size)) def _SCREAMING_SNAKE_CASE ( self : Any , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Union[str, Any]): SCREAMING_SNAKE_CASE_: Tuple = self.type_sequence_label_size SCREAMING_SNAKE_CASE_: List[str] = ViTForImageClassification(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: Any = model(lowerCAmelCase__ , labels=lowerCAmelCase__) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) # test greyscale images SCREAMING_SNAKE_CASE_: Union[str, Any] = 1 SCREAMING_SNAKE_CASE_: List[str] = ViTForImageClassification(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: Union[str, Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]) SCREAMING_SNAKE_CASE_: Dict = model(lowerCAmelCase__) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ): List[str] = config_and_inputs SCREAMING_SNAKE_CASE_: Optional[Any] = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class __lowercase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : List[Any] = ( ( ViTModel, ViTForImageClassification, ViTForMaskedImageModeling, ) if is_torch_available() else () ) _UpperCAmelCase : Tuple = ( {'''feature-extraction''': ViTModel, '''image-classification''': ViTForImageClassification} if is_torch_available() else {} ) _UpperCAmelCase : List[str] = True _UpperCAmelCase : List[Any] = False _UpperCAmelCase : Optional[Any] = False _UpperCAmelCase : Tuple = False def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: List[str] = ViTModelTester(self) SCREAMING_SNAKE_CASE_: Union[str, Any] = ConfigTester(self , config_class=lowerCAmelCase__ , has_text_modality=lowerCAmelCase__ , hidden_size=37) def _SCREAMING_SNAKE_CASE ( self : Any): self.config_tester.run_common_tests() @unittest.skip(reason="ViT does not use inputs_embeds") def _SCREAMING_SNAKE_CASE ( self : str): pass def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_: Dict = model_class(lowerCAmelCase__) self.assertIsInstance(model.get_input_embeddings() , (nn.Module)) SCREAMING_SNAKE_CASE_: List[Any] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCAmelCase__ , nn.Linear)) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_: List[Any] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = inspect.signature(model.forward) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE_: Optional[Any] = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE_: Optional[int] = ["pixel_values"] self.assertListEqual(arg_names[:1] , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__) @slow def _SCREAMING_SNAKE_CASE ( self : int): for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_: Union[str, Any] = ViTModel.from_pretrained(lowerCAmelCase__) self.assertIsNotNone(lowerCAmelCase__) def A_ ( ): SCREAMING_SNAKE_CASE_: List[Any] = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class __lowercase ( unittest.TestCase ): """simple docstring""" @cached_property def _SCREAMING_SNAKE_CASE ( self : int): return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224") if is_vision_available() else None @slow def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: int = ViTForImageClassification.from_pretrained("google/vit-base-patch16-224").to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = self.default_image_processor SCREAMING_SNAKE_CASE_: str = prepare_img() SCREAMING_SNAKE_CASE_: Optional[Any] = image_processor(images=lowerCAmelCase__ , return_tensors="pt").to(lowerCAmelCase__) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_: Optional[int] = model(**lowerCAmelCase__) # verify the logits SCREAMING_SNAKE_CASE_: Any = torch.Size((1, 1000)) self.assertEqual(outputs.logits.shape , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = torch.tensor([-0.2744, 0.8215, -0.0836]).to(lowerCAmelCase__) self.assertTrue(torch.allclose(outputs.logits[0, :3] , lowerCAmelCase__ , atol=1E-4)) @slow def _SCREAMING_SNAKE_CASE ( self : List[Any]): # ViT models have an `interpolate_pos_encoding` argument in their forward method, # allowing to interpolate the pre-trained position embeddings in order to use # the model on higher resolutions. The DINO model by Facebook AI leverages this # to visualize self-attention on higher resolution images. SCREAMING_SNAKE_CASE_: str = ViTModel.from_pretrained("facebook/dino-vits8").to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = ViTImageProcessor.from_pretrained("facebook/dino-vits8" , size=480) SCREAMING_SNAKE_CASE_: List[Any] = prepare_img() SCREAMING_SNAKE_CASE_: List[Any] = image_processor(images=lowerCAmelCase__ , return_tensors="pt") SCREAMING_SNAKE_CASE_: int = inputs.pixel_values.to(lowerCAmelCase__) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_: Optional[int] = model(lowerCAmelCase__ , interpolate_pos_encoding=lowerCAmelCase__) # verify the logits SCREAMING_SNAKE_CASE_: Tuple = torch.Size((1, 3601, 384)) self.assertEqual(outputs.last_hidden_state.shape , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.tensor( [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]]).to(lowerCAmelCase__) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , lowerCAmelCase__ , atol=1E-4)) @slow @require_accelerate @require_torch_gpu def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Dict = ViTModel.from_pretrained("facebook/dino-vits8" , torch_dtype=torch.floataa , device_map="auto") SCREAMING_SNAKE_CASE_: int = self.default_image_processor SCREAMING_SNAKE_CASE_: Union[str, Any] = prepare_img() SCREAMING_SNAKE_CASE_: Dict = image_processor(images=lowerCAmelCase__ , return_tensors="pt") SCREAMING_SNAKE_CASE_: str = inputs.pixel_values.to(lowerCAmelCase__) # forward pass to make sure inference works in fp16 with torch.no_grad(): SCREAMING_SNAKE_CASE_: str = model(lowerCAmelCase__)
13
1
import logging from transformers import PretrainedConfig lowerCAmelCase : Optional[Any] = logging.getLogger(__name__) lowerCAmelCase : Optional[int] = { """bertabs-finetuned-cnndm""": """https://huggingface.co/remi/bertabs-finetuned-cnndm-extractive-abstractive-summarization/resolve/main/config.json""", } class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : Dict = '''bertabs''' def __init__( self : Any , lowerCAmelCase__ : Any=3_0522 , lowerCAmelCase__ : List[Any]=512 , lowerCAmelCase__ : Union[str, Any]=6 , lowerCAmelCase__ : Union[str, Any]=512 , lowerCAmelCase__ : Optional[int]=8 , lowerCAmelCase__ : int=512 , lowerCAmelCase__ : Dict=0.2 , lowerCAmelCase__ : str=6 , lowerCAmelCase__ : str=768 , lowerCAmelCase__ : Any=8 , lowerCAmelCase__ : Union[str, Any]=2048 , lowerCAmelCase__ : Union[str, Any]=0.2 , **lowerCAmelCase__ : List[Any] , ): super().__init__(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = vocab_size SCREAMING_SNAKE_CASE_: List[str] = max_pos SCREAMING_SNAKE_CASE_: List[str] = enc_layers SCREAMING_SNAKE_CASE_: Union[str, Any] = enc_hidden_size SCREAMING_SNAKE_CASE_: Union[str, Any] = enc_heads SCREAMING_SNAKE_CASE_: List[Any] = enc_ff_size SCREAMING_SNAKE_CASE_: Dict = enc_dropout SCREAMING_SNAKE_CASE_: List[str] = dec_layers SCREAMING_SNAKE_CASE_: Any = dec_hidden_size SCREAMING_SNAKE_CASE_: Union[str, Any] = dec_heads SCREAMING_SNAKE_CASE_: Union[str, Any] = dec_ff_size SCREAMING_SNAKE_CASE_: Union[str, Any] = dec_dropout
13
import json import os from functools import lru_cache from typing import List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging lowerCAmelCase : Any = logging.get_logger(__name__) lowerCAmelCase : Tuple = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt"""} lowerCAmelCase : Optional[int] = { """vocab_file""": { """allenai/longformer-base-4096""": """https://huggingface.co/allenai/longformer-base-4096/resolve/main/vocab.json""", """allenai/longformer-large-4096""": ( """https://huggingface.co/allenai/longformer-large-4096/resolve/main/vocab.json""" ), """allenai/longformer-large-4096-finetuned-triviaqa""": ( """https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/vocab.json""" ), """allenai/longformer-base-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/vocab.json""" ), """allenai/longformer-large-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/vocab.json""" ), }, """merges_file""": { """allenai/longformer-base-4096""": """https://huggingface.co/allenai/longformer-base-4096/resolve/main/merges.txt""", """allenai/longformer-large-4096""": ( """https://huggingface.co/allenai/longformer-large-4096/resolve/main/merges.txt""" ), """allenai/longformer-large-4096-finetuned-triviaqa""": ( """https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/merges.txt""" ), """allenai/longformer-base-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/merges.txt""" ), """allenai/longformer-large-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/merges.txt""" ), }, } lowerCAmelCase : Optional[Any] = { """allenai/longformer-base-4096""": 4096, """allenai/longformer-large-4096""": 4096, """allenai/longformer-large-4096-finetuned-triviaqa""": 4096, """allenai/longformer-base-4096-extra.pos.embd.only""": 4096, """allenai/longformer-large-4096-extra.pos.embd.only""": 4096, } @lru_cache() # Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode def A_ ( ): SCREAMING_SNAKE_CASE_: Any = ( list(range(ord("!" ) , ord("~" ) + 1 ) ) + list(range(ord("¡" ) , ord("¬" ) + 1 ) ) + list(range(ord("®" ) , ord("ÿ" ) + 1 ) ) ) SCREAMING_SNAKE_CASE_: Tuple = bs[:] SCREAMING_SNAKE_CASE_: str = 0 for b in range(2**8 ): if b not in bs: bs.append(_UpperCAmelCase ) cs.append(2**8 + n ) n += 1 SCREAMING_SNAKE_CASE_: Optional[int] = [chr(_UpperCAmelCase ) for n in cs] return dict(zip(_UpperCAmelCase , _UpperCAmelCase ) ) def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: str = set() SCREAMING_SNAKE_CASE_: Union[str, Any] = word[0] for char in word[1:]: pairs.add((prev_char, char) ) SCREAMING_SNAKE_CASE_: Tuple = char return pairs class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : Any = VOCAB_FILES_NAMES _UpperCAmelCase : Dict = PRETRAINED_VOCAB_FILES_MAP _UpperCAmelCase : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _UpperCAmelCase : List[str] = ['''input_ids''', '''attention_mask'''] def __init__( self : str , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[Any]="replace" , lowerCAmelCase__ : Optional[Any]="<s>" , lowerCAmelCase__ : int="</s>" , lowerCAmelCase__ : Optional[Any]="</s>" , lowerCAmelCase__ : int="<s>" , lowerCAmelCase__ : Optional[Any]="<unk>" , lowerCAmelCase__ : List[Any]="<pad>" , lowerCAmelCase__ : Any="<mask>" , lowerCAmelCase__ : Union[str, Any]=False , **lowerCAmelCase__ : Tuple , ): SCREAMING_SNAKE_CASE_: int = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else bos_token SCREAMING_SNAKE_CASE_: str = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else eos_token SCREAMING_SNAKE_CASE_: Optional[int] = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else sep_token SCREAMING_SNAKE_CASE_: Union[str, Any] = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else cls_token SCREAMING_SNAKE_CASE_: int = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else unk_token SCREAMING_SNAKE_CASE_: Any = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else pad_token # Mask token behave like a normal word, i.e. include the space before it SCREAMING_SNAKE_CASE_: Optional[int] = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else mask_token super().__init__( errors=lowerCAmelCase__ , bos_token=lowerCAmelCase__ , eos_token=lowerCAmelCase__ , unk_token=lowerCAmelCase__ , sep_token=lowerCAmelCase__ , cls_token=lowerCAmelCase__ , pad_token=lowerCAmelCase__ , mask_token=lowerCAmelCase__ , add_prefix_space=lowerCAmelCase__ , **lowerCAmelCase__ , ) with open(lowerCAmelCase__ , encoding="utf-8") as vocab_handle: SCREAMING_SNAKE_CASE_: Tuple = json.load(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = {v: k for k, v in self.encoder.items()} SCREAMING_SNAKE_CASE_: Optional[Any] = errors # how to handle errors in decoding SCREAMING_SNAKE_CASE_: List[Any] = bytes_to_unicode() SCREAMING_SNAKE_CASE_: Optional[Any] = {v: k for k, v in self.byte_encoder.items()} with open(lowerCAmelCase__ , encoding="utf-8") as merges_handle: SCREAMING_SNAKE_CASE_: List[Any] = merges_handle.read().split("\n")[1:-1] SCREAMING_SNAKE_CASE_: str = [tuple(merge.split()) for merge in bpe_merges] SCREAMING_SNAKE_CASE_: List[Any] = dict(zip(lowerCAmelCase__ , range(len(lowerCAmelCase__)))) SCREAMING_SNAKE_CASE_: str = {} SCREAMING_SNAKE_CASE_: Optional[Any] = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions SCREAMING_SNAKE_CASE_: List[Any] = re.compile(R"'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+") @property def _SCREAMING_SNAKE_CASE ( self : int): return len(self.encoder) def _SCREAMING_SNAKE_CASE ( self : int): return dict(self.encoder , **self.added_tokens_encoder) def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : List[str]): if token in self.cache: return self.cache[token] SCREAMING_SNAKE_CASE_: Optional[int] = tuple(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = get_pairs(lowerCAmelCase__) if not pairs: return token while True: SCREAMING_SNAKE_CASE_: int = min(lowerCAmelCase__ , key=lambda lowerCAmelCase__: self.bpe_ranks.get(lowerCAmelCase__ , float("inf"))) if bigram not in self.bpe_ranks: break SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = bigram SCREAMING_SNAKE_CASE_: Optional[int] = [] SCREAMING_SNAKE_CASE_: List[Any] = 0 while i < len(lowerCAmelCase__): try: SCREAMING_SNAKE_CASE_: List[Any] = word.index(lowerCAmelCase__ , lowerCAmelCase__) except ValueError: new_word.extend(word[i:]) break else: new_word.extend(word[i:j]) SCREAMING_SNAKE_CASE_: Tuple = j if word[i] == first and i < len(lowerCAmelCase__) - 1 and word[i + 1] == second: new_word.append(first + second) i += 2 else: new_word.append(word[i]) i += 1 SCREAMING_SNAKE_CASE_: str = tuple(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = new_word if len(lowerCAmelCase__) == 1: break else: SCREAMING_SNAKE_CASE_: Dict = get_pairs(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = " ".join(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = word return word def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Tuple): SCREAMING_SNAKE_CASE_: Optional[Any] = [] for token in re.findall(self.pat , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: str = "".join( self.byte_encoder[b] for b in token.encode("utf-8")) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(lowerCAmelCase__).split(" ")) return bpe_tokens def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Tuple): return self.encoder.get(lowerCAmelCase__ , self.encoder.get(self.unk_token)) def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : Union[str, Any]): return self.decoder.get(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Optional[int]): SCREAMING_SNAKE_CASE_: Any = "".join(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = bytearray([self.byte_decoder[c] for c in text]).decode("utf-8" , errors=self.errors) return text def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[str] = None): if not os.path.isdir(lowerCAmelCase__): logger.error(F"Vocabulary path ({save_directory}) should be a directory") return SCREAMING_SNAKE_CASE_: Any = os.path.join( lowerCAmelCase__ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]) SCREAMING_SNAKE_CASE_: Any = os.path.join( lowerCAmelCase__ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"]) with open(lowerCAmelCase__ , "w" , encoding="utf-8") as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=lowerCAmelCase__ , ensure_ascii=lowerCAmelCase__) + "\n") SCREAMING_SNAKE_CASE_: List[Any] = 0 with open(lowerCAmelCase__ , "w" , encoding="utf-8") as writer: writer.write("#version: 0.2\n") for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda lowerCAmelCase__: kv[1]): if index != token_index: logger.warning( F"Saving vocabulary to {merge_file}: BPE merge indices are not consecutive." " Please check that the tokenizer is not corrupted!") SCREAMING_SNAKE_CASE_: List[Any] = token_index writer.write(" ".join(lowerCAmelCase__) + "\n") index += 1 return vocab_file, merge_file def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] SCREAMING_SNAKE_CASE_: Optional[int] = [self.cls_token_id] SCREAMING_SNAKE_CASE_: Dict = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None , lowerCAmelCase__ : bool = False): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=lowerCAmelCase__ , token_ids_a=lowerCAmelCase__ , already_has_special_tokens=lowerCAmelCase__) if token_ids_a is None: return [1] + ([0] * len(lowerCAmelCase__)) + [1] return [1] + ([0] * len(lowerCAmelCase__)) + [1, 1] + ([0] * len(lowerCAmelCase__)) + [1] def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None): SCREAMING_SNAKE_CASE_: Optional[int] = [self.sep_token_id] SCREAMING_SNAKE_CASE_: int = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep) * [0] def _SCREAMING_SNAKE_CASE ( self : int , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : List[str]=False , **lowerCAmelCase__ : str): SCREAMING_SNAKE_CASE_: List[Any] = kwargs.pop("add_prefix_space" , self.add_prefix_space) if (is_split_into_words or add_prefix_space) and (len(lowerCAmelCase__) > 0 and not text[0].isspace()): SCREAMING_SNAKE_CASE_: Optional[Any] = " " + text return (text, kwargs)
13
1
from __future__ import annotations # This is the precision for this function which can be altered. # It is recommended for users to keep this number greater than or equal to 10. lowerCAmelCase : Optional[int] = 10 def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): for i in range(_UpperCAmelCase , _UpperCAmelCase ): if array[i] == target: return i return -1 def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Optional[int] = 0 SCREAMING_SNAKE_CASE_: str = len(_UpperCAmelCase ) while left <= right: if right - left < precision: return lin_search(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[str] = (left + right) // 3 + 1 SCREAMING_SNAKE_CASE_: Optional[Any] = 2 * (left + right) // 3 + 1 if array[one_third] == target: return one_third elif array[two_third] == target: return two_third elif target < array[one_third]: SCREAMING_SNAKE_CASE_: Optional[Any] = one_third - 1 elif array[two_third] < target: SCREAMING_SNAKE_CASE_: Dict = two_third + 1 else: SCREAMING_SNAKE_CASE_: List[Any] = one_third + 1 SCREAMING_SNAKE_CASE_: Any = two_third - 1 else: return -1 def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): if left < right: if right - left < precision: return lin_search(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: str = (left + right) // 3 + 1 SCREAMING_SNAKE_CASE_: int = 2 * (left + right) // 3 + 1 if array[one_third] == target: return one_third elif array[two_third] == target: return two_third elif target < array[one_third]: return rec_ternary_search(_UpperCAmelCase , one_third - 1 , _UpperCAmelCase , _UpperCAmelCase ) elif array[two_third] < target: return rec_ternary_search(two_third + 1 , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) else: return rec_ternary_search(one_third + 1 , two_third - 1 , _UpperCAmelCase , _UpperCAmelCase ) else: return -1 if __name__ == "__main__": import doctest doctest.testmod() lowerCAmelCase : int = input("""Enter numbers separated by comma:\n""").strip() lowerCAmelCase : Any = [int(item.strip()) for item in user_input.split(""",""")] assert collection == sorted(collection), f"List must be ordered.\n{collection}." lowerCAmelCase : Optional[int] = int(input("""Enter the number to be found in the list:\n""").strip()) lowerCAmelCase : List[Any] = ite_ternary_search(collection, target) lowerCAmelCase : Dict = rec_ternary_search(0, len(collection) - 1, collection, target) if resulta != -1: print(f'''Iterative search: {target} found at positions: {resulta}''') print(f'''Recursive search: {target} found at positions: {resulta}''') else: print("""Not found""")
13
import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from transformers.generation import DisjunctiveConstraint @require_torch class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : List[str]): # For consistency across different places the DisjunctiveConstraint is called, # dc.token_ids is a list of integers. It is also initialized only by integers. SCREAMING_SNAKE_CASE_: Optional[Any] = [[1, 2, 4], [1, 2, 3, 4]] SCREAMING_SNAKE_CASE_: Any = DisjunctiveConstraint(lowerCAmelCase__) self.assertTrue(isinstance(dc.token_ids , lowerCAmelCase__)) with self.assertRaises(lowerCAmelCase__): DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]])) with self.assertRaises(lowerCAmelCase__): DisjunctiveConstraint([torch.LongTensor([1, 2, 4]), torch.LongTensor([1, 2, 3, 4, 5])]) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): # We can't have constraints that are complete subsets of another. This leads to a preverse # interpretation of "constraint fulfillment": does generating [1,2,3] fulfill the constraint? # It would mean that it generated [1,2] which fulfills it, but it's in the middle of potentially # fulfilling [1,2,3,4]. If we believe that [1,2,3] does fulfill the constraint, then the algorithm # will necessarily never reach [1,2,3,4], giving users a false sense of control (better to just not allow it). SCREAMING_SNAKE_CASE_: Union[str, Any] = [[1, 2], [1, 2, 3, 4]] with self.assertRaises(lowerCAmelCase__): DisjunctiveConstraint(lowerCAmelCase__) # fails here def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: List[str] = [[1, 2, 3], [1, 2, 4]] SCREAMING_SNAKE_CASE_: Tuple = DisjunctiveConstraint(lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = dc.update(1) SCREAMING_SNAKE_CASE_: Dict = stepped is True and completed is False and reset is False self.assertTrue(lowerCAmelCase__) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = dc.update(2) SCREAMING_SNAKE_CASE_: Optional[Any] = stepped is True and completed is False and reset is False self.assertTrue(lowerCAmelCase__) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1, 2]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = dc.update(3) SCREAMING_SNAKE_CASE_: Tuple = stepped is True and completed is True and reset is False self.assertTrue(lowerCAmelCase__) self.assertTrue(dc.completed) # Completed! self.assertTrue(dc.current_seq == [1, 2, 3]) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]] SCREAMING_SNAKE_CASE_: List[Any] = DisjunctiveConstraint(lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = dc.update(1) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = dc.update(2) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1, 2]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = dc.update(4) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1, 2, 4]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = dc.update(5) self.assertTrue(dc.completed) # Completed! self.assertTrue(dc.current_seq == [1, 2, 4, 5]) dc.reset() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = dc.update(1) self.assertTrue(not dc.completed) self.assertTrue(dc.remaining() == 3) self.assertTrue(dc.current_seq == [1]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = dc.update(2) self.assertTrue(not dc.completed) self.assertTrue(dc.remaining() == 2) self.assertTrue(dc.current_seq == [1, 2]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = dc.update(5) self.assertTrue(dc.completed) # Completed! self.assertTrue(dc.remaining() == 0) self.assertTrue(dc.current_seq == [1, 2, 5])
13
1
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow if is_tf_available(): import numpy as np import tensorflow as tf from transformers import TFCamembertModel @require_tf @require_sentencepiece @require_tokenizers class __lowercase ( unittest.TestCase ): """simple docstring""" @slow def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Optional[int] = TFCamembertModel.from_pretrained("jplu/tf-camembert-base") SCREAMING_SNAKE_CASE_: Tuple = tf.convert_to_tensor( [[5, 121, 11, 660, 16, 730, 2_5543, 110, 83, 6]] , dtype=tf.intaa , ) # J'aime le camembert !" SCREAMING_SNAKE_CASE_: Optional[int] = model(lowerCAmelCase__)["last_hidden_state"] SCREAMING_SNAKE_CASE_: Optional[Any] = tf.TensorShape((1, 10, 768)) self.assertEqual(output.shape , lowerCAmelCase__) # compare the actual values for a slice. SCREAMING_SNAKE_CASE_: str = tf.convert_to_tensor( [[[-0.0254, 0.0235, 0.1027], [0.0606, -0.1811, -0.0418], [-0.1561, -0.1127, 0.2687]]] , dtype=tf.floataa , ) # camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0') # camembert.eval() # expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach() self.assertTrue(np.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1E-4))
13
import pickle import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, XGLMTokenizer, XGLMTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin lowerCAmelCase : Optional[Any] = get_tests_dir("""fixtures/test_sentencepiece.model""") @require_sentencepiece @require_tokenizers class __lowercase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : Optional[Any] = XGLMTokenizer _UpperCAmelCase : List[Any] = XGLMTokenizerFast _UpperCAmelCase : Optional[int] = True _UpperCAmelCase : Tuple = True def _SCREAMING_SNAKE_CASE ( self : Tuple): super().setUp() # We have a SentencePiece fixture for testing SCREAMING_SNAKE_CASE_: List[Any] = XGLMTokenizer(lowerCAmelCase__ , keep_accents=lowerCAmelCase__) tokenizer.save_pretrained(self.tmpdirname) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_: Optional[Any] = "<pad>" SCREAMING_SNAKE_CASE_: int = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCAmelCase__) , lowerCAmelCase__) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCAmelCase__) , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Optional[int] = list(self.get_tokenizer().get_vocab().keys()) self.assertEqual(vocab_keys[0] , "<s>") self.assertEqual(vocab_keys[1] , "<pad>") self.assertEqual(len(lowerCAmelCase__) , 1008) def _SCREAMING_SNAKE_CASE ( self : Any): self.assertEqual(self.get_tokenizer().vocab_size , 1008) def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Optional[int] = XGLMTokenizer(lowerCAmelCase__ , keep_accents=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = tokenizer.tokenize("This is a test") self.assertListEqual(lowerCAmelCase__ , ["▁This", "▁is", "▁a", "▁t", "est"]) self.assertListEqual( tokenizer.convert_tokens_to_ids(lowerCAmelCase__) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) SCREAMING_SNAKE_CASE_: List[str] = tokenizer.tokenize("I was born in 92000, and this is falsé.") self.assertListEqual( lowerCAmelCase__ , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "9", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "é", ".", ] , ) SCREAMING_SNAKE_CASE_: Optional[Any] = tokenizer.convert_tokens_to_ids(lowerCAmelCase__) self.assertListEqual( lowerCAmelCase__ , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) SCREAMING_SNAKE_CASE_: List[Any] = tokenizer.convert_ids_to_tokens(lowerCAmelCase__) self.assertListEqual( lowerCAmelCase__ , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "<unk>", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "<unk>", ".", ] , ) @cached_property def _SCREAMING_SNAKE_CASE ( self : Any): return XGLMTokenizer.from_pretrained("facebook/xglm-564M") def _SCREAMING_SNAKE_CASE ( self : str): with tempfile.NamedTemporaryFile() as f: shutil.copyfile(lowerCAmelCase__ , f.name) SCREAMING_SNAKE_CASE_: Tuple = XGLMTokenizer(f.name , keep_accents=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = pickle.dumps(lowerCAmelCase__) pickle.loads(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : str): if not self.test_rust_tokenizer: return SCREAMING_SNAKE_CASE_: Dict = self.get_tokenizer() SCREAMING_SNAKE_CASE_: List[str] = self.get_rust_tokenizer() SCREAMING_SNAKE_CASE_: Any = "I was born in 92000, and this is falsé." SCREAMING_SNAKE_CASE_: Union[str, Any] = tokenizer.tokenize(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = rust_tokenizer.tokenize(lowerCAmelCase__) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = rust_tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = self.get_rust_tokenizer() SCREAMING_SNAKE_CASE_: str = tokenizer.encode(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = rust_tokenizer.encode(lowerCAmelCase__) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__) @slow def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: Dict = "Hello World!" SCREAMING_SNAKE_CASE_: Union[str, Any] = [2, 3_1227, 4447, 35] self.assertListEqual(lowerCAmelCase__ , self.big_tokenizer.encode(lowerCAmelCase__)) @slow def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Union[str, Any] = ( "This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will" " add words that should not exsist and be tokenized to unk, such as saoneuhaoesuth" ) # fmt: off SCREAMING_SNAKE_CASE_: Optional[Any] = [2, 1018, 67, 11, 1988, 2617, 5631, 278, 11, 3407, 48, 7_1630, 2_8085, 4, 3234, 157, 13, 6, 5, 6, 4, 3526, 768, 15, 659, 57, 298, 3983, 864, 129, 21, 6, 5, 1_3675, 377, 652, 7580, 1_0341, 155, 2817, 422, 1666, 7, 1674, 53, 113, 20_2277, 1_7892, 33, 60, 87, 4, 3234, 157, 61, 2667, 5_2376, 19, 88, 23, 735] # fmt: on self.assertListEqual(lowerCAmelCase__ , self.big_tokenizer.encode(lowerCAmelCase__)) @slow def _SCREAMING_SNAKE_CASE ( self : int): # fmt: off SCREAMING_SNAKE_CASE_: str = { "input_ids": [[2, 10_8825, 1163, 15, 8_8010, 473, 1_5898, 157, 1_3672, 1857, 312, 8, 23_8021, 1163, 53, 1_3672, 1857, 312, 8, 5_3283, 18_2396, 8, 1_8566, 16, 3_6733, 4101, 8, 230, 24_4017, 12_2553, 7, 15, 13_2597, 4, 293, 1_2511, 7610, 4, 3414, 13_2597, 9, 4, 3_2361, 362, 4, 734, 2_8512, 3_2569, 18, 4, 3_2361, 2_6096, 1_4982, 73, 1_8715, 2_1433, 23_5261, 15, 492, 1_2427, 16, 53, 1_8715, 2_1433, 6_5454, 15, 2_3659, 563, 16, 278, 597, 2843, 595, 7931, 18_2396, 6_4186, 22, 886, 595, 13_2981, 53, 2_5540, 3449, 4_3982, 3_9901, 5951, 878, 330, 4, 2_7694, 8_0269, 312, 53, 6517, 1_1780, 611, 2_0408, 5], [2, 6, 13_2597, 67, 4_2897, 33, 592, 8, 16_3729, 2_5540, 361, 13_6997, 10_9514, 17_3230, 7, 501, 60, 10_2913, 196, 5631, 235, 6_3243, 473, 6, 23_1757, 74, 5277, 7905, 53, 3095, 3_7317, 22, 454, 18_3874, 5], [2, 268, 3_1298, 4_6530, 6, 13_2935, 4_3831, 7, 597, 32, 24, 3688, 9865, 5]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] } # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=lowerCAmelCase__ , model_name="facebook/xglm-564M" , padding=lowerCAmelCase__ , )
13
1
from datetime import datetime import requests from bsa import BeautifulSoup if __name__ == "__main__": lowerCAmelCase : Tuple = input("""Enter image url: """).strip() print(f'''Downloading image from {url} ...''') lowerCAmelCase : Dict = BeautifulSoup(requests.get(url).content, """html.parser""") # The image URL is in the content field of the first meta tag with property og:image lowerCAmelCase : List[str] = soup.find("""meta""", {"""property""": """og:image"""})["""content"""] lowerCAmelCase : List[Any] = requests.get(image_url).content lowerCAmelCase : List[str] = f'''{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg''' with open(file_name, """wb""") as fp: fp.write(image_data) print(f'''Done. Image saved to disk as {file_name}.''')
13
def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , ): SCREAMING_SNAKE_CASE_: Optional[int] = [redshift, radiation_density, matter_density, dark_energy] if any(p < 0 for p in parameters ): raise ValueError("All input parameters must be positive" ) if any(p > 1 for p in parameters[1:4] ): raise ValueError("Relative densities cannot be greater than one" ) else: SCREAMING_SNAKE_CASE_: int = 1 - (matter_density + radiation_density + dark_energy) SCREAMING_SNAKE_CASE_: Dict = ( radiation_density * (redshift + 1) ** 4 + matter_density * (redshift + 1) ** 3 + curvature * (redshift + 1) ** 2 + dark_energy ) SCREAMING_SNAKE_CASE_: Any = hubble_constant * e_a ** (1 / 2) return hubble if __name__ == "__main__": import doctest # run doctest doctest.testmod() # demo LCDM approximation lowerCAmelCase : List[Any] = 0.3 print( hubble_parameter( hubble_constant=68.3, radiation_density=1E-4, matter_density=matter_density, dark_energy=1 - matter_density, redshift=0, ) )
13
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available lowerCAmelCase : List[Any] = { """configuration_mvp""": ["""MVP_PRETRAINED_CONFIG_ARCHIVE_MAP""", """MvpConfig""", """MvpOnnxConfig"""], """tokenization_mvp""": ["""MvpTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Optional[Any] = ["""MvpTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : int = [ """MVP_PRETRAINED_MODEL_ARCHIVE_LIST""", """MvpForCausalLM""", """MvpForConditionalGeneration""", """MvpForQuestionAnswering""", """MvpForSequenceClassification""", """MvpModel""", """MvpPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_mvp import MVP_PRETRAINED_CONFIG_ARCHIVE_MAP, MvpConfig, MvpOnnxConfig from .tokenization_mvp import MvpTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_mvp_fast import MvpTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mvp import ( MVP_PRETRAINED_MODEL_ARCHIVE_LIST, MvpForCausalLM, MvpForConditionalGeneration, MvpForQuestionAnswering, MvpForSequenceClassification, MvpModel, MvpPreTrainedModel, ) else: import sys lowerCAmelCase : List[str] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
13
from typing import Optional, Union import torch from torch import nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention from ...modeling_utils import PreTrainedModel from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging from .configuration_mobilenet_va import MobileNetVaConfig lowerCAmelCase : int = logging.get_logger(__name__) # General docstring lowerCAmelCase : int = """MobileNetV1Config""" # Base docstring lowerCAmelCase : List[Any] = """google/mobilenet_v1_1.0_224""" lowerCAmelCase : Dict = [1, 1024, 7, 7] # Image classification docstring lowerCAmelCase : Union[str, Any] = """google/mobilenet_v1_1.0_224""" lowerCAmelCase : Any = """tabby, tabby cat""" lowerCAmelCase : List[Any] = [ """google/mobilenet_v1_1.0_224""", """google/mobilenet_v1_0.75_192""", # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 ] def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=None ): SCREAMING_SNAKE_CASE_: List[str] = {} if isinstance(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = model.mobilenet_va else: SCREAMING_SNAKE_CASE_: int = model SCREAMING_SNAKE_CASE_: Dict = "MobilenetV1/Conv2d_0/" SCREAMING_SNAKE_CASE_: str = backbone.conv_stem.convolution.weight SCREAMING_SNAKE_CASE_: List[str] = backbone.conv_stem.normalization.bias SCREAMING_SNAKE_CASE_: int = backbone.conv_stem.normalization.weight SCREAMING_SNAKE_CASE_: List[str] = backbone.conv_stem.normalization.running_mean SCREAMING_SNAKE_CASE_: Optional[int] = backbone.conv_stem.normalization.running_var for i in range(13 ): SCREAMING_SNAKE_CASE_: List[str] = i + 1 SCREAMING_SNAKE_CASE_: Optional[int] = i * 2 SCREAMING_SNAKE_CASE_: Any = backbone.layer[pt_index] SCREAMING_SNAKE_CASE_: Any = f"MobilenetV1/Conv2d_{tf_index}_depthwise/" SCREAMING_SNAKE_CASE_: Any = pointer.convolution.weight SCREAMING_SNAKE_CASE_: Any = pointer.normalization.bias SCREAMING_SNAKE_CASE_: str = pointer.normalization.weight SCREAMING_SNAKE_CASE_: Dict = pointer.normalization.running_mean SCREAMING_SNAKE_CASE_: Optional[Any] = pointer.normalization.running_var SCREAMING_SNAKE_CASE_: Tuple = backbone.layer[pt_index + 1] SCREAMING_SNAKE_CASE_: List[str] = f"MobilenetV1/Conv2d_{tf_index}_pointwise/" SCREAMING_SNAKE_CASE_: int = pointer.convolution.weight SCREAMING_SNAKE_CASE_: Any = pointer.normalization.bias SCREAMING_SNAKE_CASE_: Optional[int] = pointer.normalization.weight SCREAMING_SNAKE_CASE_: Optional[Any] = pointer.normalization.running_mean SCREAMING_SNAKE_CASE_: Dict = pointer.normalization.running_var if isinstance(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: List[str] = "MobilenetV1/Logits/Conv2d_1c_1x1/" SCREAMING_SNAKE_CASE_: Optional[Any] = model.classifier.weight SCREAMING_SNAKE_CASE_: Tuple = model.classifier.bias return tf_to_pt_map def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): try: import numpy as np import tensorflow as tf except ImportError: logger.error( "Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see " "https://www.tensorflow.org/install/ for installation instructions." ) raise # Load weights from TF model SCREAMING_SNAKE_CASE_: int = tf.train.list_variables(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: int = {} for name, shape in init_vars: logger.info(f"Loading TF weight {name} with shape {shape}" ) SCREAMING_SNAKE_CASE_: Any = tf.train.load_variable(_UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Union[str, Any] = array # Build TF to PyTorch weights loading map SCREAMING_SNAKE_CASE_: Optional[Any] = _build_tf_to_pytorch_map(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) for name, pointer in tf_to_pt_map.items(): logger.info(f"Importing {name}" ) if name not in tf_weights: logger.info(f"{name} not in tf pre-trained weights, skipping" ) continue SCREAMING_SNAKE_CASE_: int = tf_weights[name] if "depthwise_weights" in name: logger.info("Transposing depthwise" ) SCREAMING_SNAKE_CASE_: int = np.transpose(_UpperCAmelCase , (2, 3, 0, 1) ) elif "weights" in name: logger.info("Transposing" ) if len(pointer.shape ) == 2: # copying into linear layer SCREAMING_SNAKE_CASE_: List[str] = array.squeeze().transpose() else: SCREAMING_SNAKE_CASE_: Any = np.transpose(_UpperCAmelCase , (3, 2, 0, 1) ) if pointer.shape != array.shape: raise ValueError(f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched" ) logger.info(f"Initialize PyTorch weight {name} {array.shape}" ) SCREAMING_SNAKE_CASE_: int = torch.from_numpy(_UpperCAmelCase ) tf_weights.pop(_UpperCAmelCase , _UpperCAmelCase ) tf_weights.pop(name + "/RMSProp" , _UpperCAmelCase ) tf_weights.pop(name + "/RMSProp_1" , _UpperCAmelCase ) tf_weights.pop(name + "/ExponentialMovingAverage" , _UpperCAmelCase ) logger.info(f"Weights not copied to PyTorch model: {', '.join(tf_weights.keys() )}" ) return model def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = features.shape[-2:] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = conv_layer.stride SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = conv_layer.kernel_size if in_height % stride_height == 0: SCREAMING_SNAKE_CASE_: int = max(kernel_height - stride_height , 0 ) else: SCREAMING_SNAKE_CASE_: Tuple = max(kernel_height - (in_height % stride_height) , 0 ) if in_width % stride_width == 0: SCREAMING_SNAKE_CASE_: str = max(kernel_width - stride_width , 0 ) else: SCREAMING_SNAKE_CASE_: Dict = max(kernel_width - (in_width % stride_width) , 0 ) SCREAMING_SNAKE_CASE_: str = pad_along_width // 2 SCREAMING_SNAKE_CASE_: Union[str, Any] = pad_along_width - pad_left SCREAMING_SNAKE_CASE_: int = pad_along_height // 2 SCREAMING_SNAKE_CASE_: Tuple = pad_along_height - pad_top SCREAMING_SNAKE_CASE_: Union[str, Any] = (pad_left, pad_right, pad_top, pad_bottom) return nn.functional.pad(_UpperCAmelCase , _UpperCAmelCase , "constant" , 0.0 ) class __lowercase ( nn.Module ): """simple docstring""" def __init__( self : Optional[int] , lowerCAmelCase__ : MobileNetVaConfig , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : Optional[int] = 1 , lowerCAmelCase__ : Optional[int] = 1 , lowerCAmelCase__ : bool = False , lowerCAmelCase__ : Optional[bool] = True , lowerCAmelCase__ : Optional[bool or str] = True , ): super().__init__() SCREAMING_SNAKE_CASE_: Optional[int] = config if in_channels % groups != 0: raise ValueError(F"Input channels ({in_channels}) are not divisible by {groups} groups.") if out_channels % groups != 0: raise ValueError(F"Output channels ({out_channels}) are not divisible by {groups} groups.") SCREAMING_SNAKE_CASE_: int = 0 if config.tf_padding else int((kernel_size - 1) / 2) SCREAMING_SNAKE_CASE_: Union[str, Any] = nn.Convad( in_channels=lowerCAmelCase__ , out_channels=lowerCAmelCase__ , kernel_size=lowerCAmelCase__ , stride=lowerCAmelCase__ , padding=lowerCAmelCase__ , groups=lowerCAmelCase__ , bias=lowerCAmelCase__ , padding_mode="zeros" , ) if use_normalization: SCREAMING_SNAKE_CASE_: str = nn.BatchNormad( num_features=lowerCAmelCase__ , eps=config.layer_norm_eps , momentum=0.9997 , affine=lowerCAmelCase__ , track_running_stats=lowerCAmelCase__ , ) else: SCREAMING_SNAKE_CASE_: str = None if use_activation: if isinstance(lowerCAmelCase__ , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Dict = ACTaFN[use_activation] elif isinstance(config.hidden_act , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Dict = ACTaFN[config.hidden_act] else: SCREAMING_SNAKE_CASE_: Any = config.hidden_act else: SCREAMING_SNAKE_CASE_: int = None def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : torch.Tensor): if self.config.tf_padding: SCREAMING_SNAKE_CASE_: Union[str, Any] = apply_tf_padding(lowerCAmelCase__ , self.convolution) SCREAMING_SNAKE_CASE_: Optional[int] = self.convolution(lowerCAmelCase__) if self.normalization is not None: SCREAMING_SNAKE_CASE_: int = self.normalization(lowerCAmelCase__) if self.activation is not None: SCREAMING_SNAKE_CASE_: List[Any] = self.activation(lowerCAmelCase__) return features class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : List[str] = MobileNetVaConfig _UpperCAmelCase : List[Any] = load_tf_weights_in_mobilenet_va _UpperCAmelCase : List[Any] = '''mobilenet_v1''' _UpperCAmelCase : int = '''pixel_values''' _UpperCAmelCase : List[Any] = False def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : Union[nn.Linear, nn.Convad]): if isinstance(lowerCAmelCase__ , (nn.Linear, nn.Convad)): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range) if module.bias is not None: module.bias.data.zero_() elif isinstance(lowerCAmelCase__ , nn.BatchNormad): module.bias.data.zero_() module.weight.data.fill_(1.0) lowerCAmelCase : Any = R""" This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior. Parameters: config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights. """ lowerCAmelCase : List[str] = R""" Args: pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`MobileNetV1ImageProcessor.__call__`] for details. output_hidden_states (`bool`, *optional*): Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for more detail. return_dict (`bool`, *optional*): Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. """ @add_start_docstrings( '''The bare MobileNetV1 model outputting raw hidden-states without any specific head on top.''' , UpperCAmelCase_ , ) class __lowercase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Dict , lowerCAmelCase__ : MobileNetVaConfig , lowerCAmelCase__ : bool = True): super().__init__(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = config SCREAMING_SNAKE_CASE_: Union[str, Any] = 32 SCREAMING_SNAKE_CASE_: Dict = max(int(depth * config.depth_multiplier) , config.min_depth) SCREAMING_SNAKE_CASE_: Tuple = MobileNetVaConvLayer( lowerCAmelCase__ , in_channels=config.num_channels , out_channels=lowerCAmelCase__ , kernel_size=3 , stride=2 , ) SCREAMING_SNAKE_CASE_: Optional[int] = [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1] SCREAMING_SNAKE_CASE_: str = nn.ModuleList() for i in range(13): SCREAMING_SNAKE_CASE_: List[Any] = out_channels if strides[i] == 2 or i == 0: depth *= 2 SCREAMING_SNAKE_CASE_: str = max(int(depth * config.depth_multiplier) , config.min_depth) self.layer.append( MobileNetVaConvLayer( lowerCAmelCase__ , in_channels=lowerCAmelCase__ , out_channels=lowerCAmelCase__ , kernel_size=3 , stride=strides[i] , groups=lowerCAmelCase__ , )) self.layer.append( MobileNetVaConvLayer( lowerCAmelCase__ , in_channels=lowerCAmelCase__ , out_channels=lowerCAmelCase__ , kernel_size=1 , )) SCREAMING_SNAKE_CASE_: List[str] = nn.AdaptiveAvgPoolad((1, 1)) if add_pooling_layer else None # Initialize weights and apply final processing self.post_init() def _SCREAMING_SNAKE_CASE ( self : int , lowerCAmelCase__ : str): raise NotImplementedError @add_start_docstrings_to_model_forward(lowerCAmelCase__) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=lowerCAmelCase__ , config_class=_CONFIG_FOR_DOC , modality="vision" , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : Optional[torch.Tensor] = None , lowerCAmelCase__ : Optional[bool] = None , lowerCAmelCase__ : Optional[bool] = None , ): SCREAMING_SNAKE_CASE_: Optional[int] = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) SCREAMING_SNAKE_CASE_: Any = return_dict if return_dict is not None else self.config.use_return_dict if pixel_values is None: raise ValueError("You have to specify pixel_values") SCREAMING_SNAKE_CASE_: Optional[Any] = self.conv_stem(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = () if output_hidden_states else None for i, layer_module in enumerate(self.layer): SCREAMING_SNAKE_CASE_: Tuple = layer_module(lowerCAmelCase__) if output_hidden_states: SCREAMING_SNAKE_CASE_: Optional[int] = all_hidden_states + (hidden_states,) SCREAMING_SNAKE_CASE_: Optional[Any] = hidden_states if self.pooler is not None: SCREAMING_SNAKE_CASE_: int = torch.flatten(self.pooler(lowerCAmelCase__) , start_dim=1) else: SCREAMING_SNAKE_CASE_: List[str] = None if not return_dict: return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None) return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=lowerCAmelCase__ , pooler_output=lowerCAmelCase__ , hidden_states=lowerCAmelCase__ , ) @add_start_docstrings( ''' MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. ''' , UpperCAmelCase_ , ) class __lowercase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Any , lowerCAmelCase__ : MobileNetVaConfig): super().__init__(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = config.num_labels SCREAMING_SNAKE_CASE_: Dict = MobileNetVaModel(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = self.mobilenet_va.layer[-1].convolution.out_channels # Classifier head SCREAMING_SNAKE_CASE_: str = nn.Dropout(config.classifier_dropout_prob , inplace=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = nn.Linear(lowerCAmelCase__ , config.num_labels) if config.num_labels > 0 else nn.Identity() # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(lowerCAmelCase__) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=lowerCAmelCase__ , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : Optional[torch.Tensor] = None , lowerCAmelCase__ : Optional[bool] = None , lowerCAmelCase__ : Optional[torch.Tensor] = None , lowerCAmelCase__ : Optional[bool] = None , ): SCREAMING_SNAKE_CASE_: List[str] = return_dict if return_dict is not None else self.config.use_return_dict SCREAMING_SNAKE_CASE_: List[str] = self.mobilenet_va(lowerCAmelCase__ , output_hidden_states=lowerCAmelCase__ , return_dict=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = outputs.pooler_output if return_dict else outputs[1] SCREAMING_SNAKE_CASE_: Tuple = self.classifier(self.dropout(lowerCAmelCase__)) SCREAMING_SNAKE_CASE_: Optional[int] = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: SCREAMING_SNAKE_CASE_: List[Any] = "regression" elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): SCREAMING_SNAKE_CASE_: int = "single_label_classification" else: SCREAMING_SNAKE_CASE_: str = "multi_label_classification" if self.config.problem_type == "regression": SCREAMING_SNAKE_CASE_: Dict = MSELoss() if self.num_labels == 1: SCREAMING_SNAKE_CASE_: Any = loss_fct(logits.squeeze() , labels.squeeze()) else: SCREAMING_SNAKE_CASE_: int = loss_fct(lowerCAmelCase__ , lowerCAmelCase__) elif self.config.problem_type == "single_label_classification": SCREAMING_SNAKE_CASE_: Any = CrossEntropyLoss() SCREAMING_SNAKE_CASE_: Dict = loss_fct(logits.view(-1 , self.num_labels) , labels.view(-1)) elif self.config.problem_type == "multi_label_classification": SCREAMING_SNAKE_CASE_: Dict = BCEWithLogitsLoss() SCREAMING_SNAKE_CASE_: Dict = loss_fct(lowerCAmelCase__ , lowerCAmelCase__) if not return_dict: SCREAMING_SNAKE_CASE_: int = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return ImageClassifierOutputWithNoAttention( loss=lowerCAmelCase__ , logits=lowerCAmelCase__ , hidden_states=outputs.hidden_states , )
13
1
import os import unicodedata 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 SPIECE_UNDERLINE, logging lowerCAmelCase : List[str] = logging.get_logger(__name__) lowerCAmelCase : List[str] = {"""vocab_file""": """spiece.model"""} lowerCAmelCase : Union[str, Any] = { """vocab_file""": { """TsinghuaAI/CPM-Generate""": """https://huggingface.co/TsinghuaAI/CPM-Generate/resolve/main/spiece.model""", } } class __lowercase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Tuple , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Union[str, Any]=False , lowerCAmelCase__ : Any=True , lowerCAmelCase__ : List[Any]=False , lowerCAmelCase__ : int="<s>" , lowerCAmelCase__ : str="</s>" , lowerCAmelCase__ : Tuple="<unk>" , lowerCAmelCase__ : Tuple="<sep>" , lowerCAmelCase__ : List[Any]="<pad>" , lowerCAmelCase__ : Dict="<cls>" , lowerCAmelCase__ : str="<mask>" , lowerCAmelCase__ : str=["<eop>", "<eod>"] , lowerCAmelCase__ : Optional[Dict[str, Any]] = None , **lowerCAmelCase__ : List[Any] , ): SCREAMING_SNAKE_CASE_: Union[str, Any] = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else mask_token SCREAMING_SNAKE_CASE_: Optional[int] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=lowerCAmelCase__ , remove_space=lowerCAmelCase__ , keep_accents=lowerCAmelCase__ , bos_token=lowerCAmelCase__ , eos_token=lowerCAmelCase__ , unk_token=lowerCAmelCase__ , sep_token=lowerCAmelCase__ , pad_token=lowerCAmelCase__ , cls_token=lowerCAmelCase__ , mask_token=lowerCAmelCase__ , additional_special_tokens=lowerCAmelCase__ , sp_model_kwargs=self.sp_model_kwargs , **lowerCAmelCase__ , ) SCREAMING_SNAKE_CASE_: str = 3 SCREAMING_SNAKE_CASE_: List[Any] = do_lower_case SCREAMING_SNAKE_CASE_: Any = remove_space SCREAMING_SNAKE_CASE_: Dict = keep_accents SCREAMING_SNAKE_CASE_: List[str] = vocab_file SCREAMING_SNAKE_CASE_: Union[str, Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(lowerCAmelCase__) try: import jieba except ModuleNotFoundError as error: raise error.__class__( "You need to install jieba to use CpmTokenizer or CpmTokenizerFast. " "See https://pypi.org/project/jieba/ for installation.") SCREAMING_SNAKE_CASE_: Union[str, Any] = jieba SCREAMING_SNAKE_CASE_: List[Any] = str.maketrans(" \n" , "\u2582\u2583") @property # Copied from transformers.models.xlnet.tokenization_xlnet.XLNetTokenizer.vocab_size def _SCREAMING_SNAKE_CASE ( self : List[Any]): return len(self.sp_model) def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_: List[Any] = {self.convert_ids_to_tokens(lowerCAmelCase__): i for i in range(self.vocab_size)} vocab.update(self.added_tokens_encoder) return vocab def __getstate__( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: str = self.__dict__.copy() SCREAMING_SNAKE_CASE_: List[str] = None return state def __setstate__( self : Optional[Any] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Any = d # for backward compatibility if not hasattr(self , "sp_model_kwargs"): SCREAMING_SNAKE_CASE_: List[str] = {} SCREAMING_SNAKE_CASE_: int = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(self.vocab_file) def _SCREAMING_SNAKE_CASE ( self : int , lowerCAmelCase__ : List[Any]): if self.remove_space: SCREAMING_SNAKE_CASE_: Dict = " ".join(inputs.strip().split()) else: SCREAMING_SNAKE_CASE_: Optional[Any] = inputs SCREAMING_SNAKE_CASE_: str = outputs.replace("``" , "\"").replace("''" , "\"") if not self.keep_accents: SCREAMING_SNAKE_CASE_: Dict = unicodedata.normalize("NFKD" , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = "".join([c for c in outputs if not unicodedata.combining(lowerCAmelCase__)]) if self.do_lower_case: SCREAMING_SNAKE_CASE_: List[str] = outputs.lower() return outputs def _SCREAMING_SNAKE_CASE ( self : List[Any] , lowerCAmelCase__ : str): SCREAMING_SNAKE_CASE_: List[str] = self.preprocess_text(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = self.sp_model.encode(lowerCAmelCase__ , out_type=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = [] for piece in pieces: if len(lowerCAmelCase__) > 1 and piece[-1] == str(",") and piece[-2].isdigit(): SCREAMING_SNAKE_CASE_: str = self.sp_model.EncodeAsPieces(piece[:-1].replace(lowerCAmelCase__ , "")) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0]) == 1: SCREAMING_SNAKE_CASE_: Union[str, Any] = cur_pieces[1:] else: SCREAMING_SNAKE_CASE_: Union[str, Any] = cur_pieces[0][1:] cur_pieces.append(piece[-1]) new_pieces.extend(lowerCAmelCase__) else: new_pieces.append(lowerCAmelCase__) return new_pieces def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : Union[str, Any]): return self.sp_model.PieceToId(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : List[str]): return self.sp_model.IdToPiece(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : Optional[Any]): SCREAMING_SNAKE_CASE_: Dict = "".join(lowerCAmelCase__).replace(lowerCAmelCase__ , " ").strip() return out_string def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None): SCREAMING_SNAKE_CASE_: List[str] = [self.sep_token_id] SCREAMING_SNAKE_CASE_: Tuple = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def _SCREAMING_SNAKE_CASE ( self : Tuple , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None , lowerCAmelCase__ : bool = False): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=lowerCAmelCase__ , token_ids_a=lowerCAmelCase__ , already_has_special_tokens=lowerCAmelCase__) if token_ids_a is not None: return ([0] * len(lowerCAmelCase__)) + [1] + ([0] * len(lowerCAmelCase__)) + [1, 1] return ([0] * len(lowerCAmelCase__)) + [1, 1] def _SCREAMING_SNAKE_CASE ( self : int , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None): SCREAMING_SNAKE_CASE_: Optional[int] = [self.sep_token_id] SCREAMING_SNAKE_CASE_: Optional[Any] = [2] if token_ids_a is None: return len(token_ids_a + sep) * [0] + cls_segment_id return len(token_ids_a + sep) * [0] + len(token_ids_a + sep) * [1] + cls_segment_id def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[str] = None): if not os.path.isdir(lowerCAmelCase__): logger.error(F"Vocabulary path ({save_directory}) should be a directory") return SCREAMING_SNAKE_CASE_: Optional[Any] = os.path.join( lowerCAmelCase__ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]) if os.path.abspath(self.vocab_file) != os.path.abspath(lowerCAmelCase__) and os.path.isfile(self.vocab_file): copyfile(self.vocab_file , lowerCAmelCase__) elif not os.path.isfile(self.vocab_file): with open(lowerCAmelCase__ , "wb") as fi: SCREAMING_SNAKE_CASE_: Optional[int] = self.sp_model.serialized_model_proto() fi.write(lowerCAmelCase__) return (out_vocab_file,) def _SCREAMING_SNAKE_CASE ( self : str , *lowerCAmelCase__ : Optional[int] , **lowerCAmelCase__ : Union[str, Any]): SCREAMING_SNAKE_CASE_: List[str] = super()._decode(*lowerCAmelCase__ , **lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = text.replace(" " , "").replace("\u2582" , " ").replace("\u2583" , "\n") return text
13
def A_ ( _UpperCAmelCase , _UpperCAmelCase = False ): if not isinstance(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: str = f"Expected string as input, found {type(_UpperCAmelCase )}" raise ValueError(_UpperCAmelCase ) if not isinstance(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Optional[Any] = f"Expected boolean as use_pascal parameter, found {type(_UpperCAmelCase )}" raise ValueError(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Tuple = input_str.split("_" ) SCREAMING_SNAKE_CASE_: str = 0 if use_pascal else 1 SCREAMING_SNAKE_CASE_: int = words[start_index:] SCREAMING_SNAKE_CASE_: List[str] = [word[0].upper() + word[1:] for word in words_to_capitalize] SCREAMING_SNAKE_CASE_: List[Any] = "" if use_pascal else words[0] return "".join([initial_word, *capitalized_words] ) if __name__ == "__main__": from doctest import testmod testmod()
13
1
import unittest from transformers.utils.backbone_utils import ( BackboneMixin, get_aligned_output_features_output_indices, verify_out_features_out_indices, ) class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: Tuple = ["a", "b", "c"] # Defaults to last layer if both are None SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = get_aligned_output_features_output_indices(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) self.assertEqual(lowerCAmelCase__ , ["c"]) self.assertEqual(lowerCAmelCase__ , [2]) # Out indices set to match out features SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = get_aligned_output_features_output_indices(["a", "c"] , lowerCAmelCase__ , lowerCAmelCase__) self.assertEqual(lowerCAmelCase__ , ["a", "c"]) self.assertEqual(lowerCAmelCase__ , [0, 2]) # Out features set to match out indices SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = get_aligned_output_features_output_indices(lowerCAmelCase__ , [0, 2] , lowerCAmelCase__) self.assertEqual(lowerCAmelCase__ , ["a", "c"]) self.assertEqual(lowerCAmelCase__ , [0, 2]) # Out features selected from negative indices SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = get_aligned_output_features_output_indices(lowerCAmelCase__ , [-3, -1] , lowerCAmelCase__) self.assertEqual(lowerCAmelCase__ , ["a", "c"]) self.assertEqual(lowerCAmelCase__ , [-3, -1]) def _SCREAMING_SNAKE_CASE ( self : str): # Stage names must be set with self.assertRaises(lowerCAmelCase__): verify_out_features_out_indices(["a", "b"] , (0, 1) , lowerCAmelCase__) # Out features must be a list with self.assertRaises(lowerCAmelCase__): verify_out_features_out_indices(("a", "b") , (0, 1) , ["a", "b"]) # Out features must be a subset of stage names with self.assertRaises(lowerCAmelCase__): verify_out_features_out_indices(["a", "b"] , (0, 1) , ["a"]) # Out indices must be a list or tuple with self.assertRaises(lowerCAmelCase__): verify_out_features_out_indices(lowerCAmelCase__ , 0 , ["a", "b"]) # Out indices must be a subset of stage names with self.assertRaises(lowerCAmelCase__): verify_out_features_out_indices(lowerCAmelCase__ , (0, 1) , ["a"]) # Out features and out indices must be the same length with self.assertRaises(lowerCAmelCase__): verify_out_features_out_indices(["a", "b"] , (0,) , ["a", "b", "c"]) # Out features should match out indices with self.assertRaises(lowerCAmelCase__): verify_out_features_out_indices(["a", "b"] , (0, 2) , ["a", "b", "c"]) # Out features and out indices should be in order with self.assertRaises(lowerCAmelCase__): verify_out_features_out_indices(["b", "a"] , (0, 1) , ["a", "b"]) # Check passes with valid inputs verify_out_features_out_indices(["a", "b", "d"] , (0, 1, -1) , ["a", "b", "c", "d"]) def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: List[str] = BackboneMixin() SCREAMING_SNAKE_CASE_: List[str] = ["a", "b", "c"] SCREAMING_SNAKE_CASE_: Optional[Any] = ["a", "c"] SCREAMING_SNAKE_CASE_: Optional[int] = [0, 2] # Check that the output features and indices are set correctly self.assertEqual(backbone.out_features , ["a", "c"]) self.assertEqual(backbone.out_indices , [0, 2]) # Check out features and indices are updated correctly SCREAMING_SNAKE_CASE_: Optional[Any] = ["a", "b"] self.assertEqual(backbone.out_features , ["a", "b"]) self.assertEqual(backbone.out_indices , [0, 1]) SCREAMING_SNAKE_CASE_: Optional[Any] = [-3, -1] self.assertEqual(backbone.out_features , ["a", "c"]) self.assertEqual(backbone.out_indices , [-3, -1])
13
import os import tempfile import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from torch import nn from transformers import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_inverse_sqrt_schedule, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) def A_ ( _UpperCAmelCase , _UpperCAmelCase=10 ): SCREAMING_SNAKE_CASE_: Union[str, Any] = [] for _ in range(_UpperCAmelCase ): lrs.append(scheduler.get_lr()[0] ) scheduler.step() return lrs def A_ ( _UpperCAmelCase , _UpperCAmelCase=10 ): SCREAMING_SNAKE_CASE_: List[str] = [] for step in range(_UpperCAmelCase ): lrs.append(scheduler.get_lr()[0] ) scheduler.step() if step == num_steps // 2: with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE_: Optional[int] = os.path.join(_UpperCAmelCase , "schedule.bin" ) torch.save(scheduler.state_dict() , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Optional[Any] = torch.load(_UpperCAmelCase ) scheduler.load_state_dict(_UpperCAmelCase ) return lrs @require_torch class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : int , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Tuple): self.assertEqual(len(lowerCAmelCase__) , len(lowerCAmelCase__)) for a, b in zip(lowerCAmelCase__ , lowerCAmelCase__): self.assertAlmostEqual(lowerCAmelCase__ , lowerCAmelCase__ , delta=lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.tensor([0.1, -0.2, -0.1] , requires_grad=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = torch.tensor([0.4, 0.2, -0.5]) SCREAMING_SNAKE_CASE_: Optional[Any] = nn.MSELoss() # No warmup, constant schedule, no gradient clipping SCREAMING_SNAKE_CASE_: int = AdamW(params=[w] , lr=2E-1 , weight_decay=0.0) for _ in range(100): SCREAMING_SNAKE_CASE_: Dict = criterion(lowerCAmelCase__ , lowerCAmelCase__) loss.backward() optimizer.step() w.grad.detach_() # No zero_grad() function on simple tensors. we do it ourselves. w.grad.zero_() self.assertListAlmostEqual(w.tolist() , [0.4, 0.2, -0.5] , tol=1E-2) def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.tensor([0.1, -0.2, -0.1] , requires_grad=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = torch.tensor([0.4, 0.2, -0.5]) SCREAMING_SNAKE_CASE_: Any = nn.MSELoss() # No warmup, constant schedule, no gradient clipping SCREAMING_SNAKE_CASE_: int = Adafactor( params=[w] , lr=1E-2 , eps=(1E-30, 1E-3) , clip_threshold=1.0 , decay_rate=-0.8 , betaa=lowerCAmelCase__ , weight_decay=0.0 , relative_step=lowerCAmelCase__ , scale_parameter=lowerCAmelCase__ , warmup_init=lowerCAmelCase__ , ) for _ in range(1000): SCREAMING_SNAKE_CASE_: List[Any] = criterion(lowerCAmelCase__ , lowerCAmelCase__) loss.backward() optimizer.step() w.grad.detach_() # No zero_grad() function on simple tensors. we do it ourselves. w.grad.zero_() self.assertListAlmostEqual(w.tolist() , [0.4, 0.2, -0.5] , tol=1E-2) @require_torch class __lowercase ( unittest.TestCase ): """simple docstring""" _UpperCAmelCase : Union[str, Any] = nn.Linear(50 , 50 ) if is_torch_available() else None _UpperCAmelCase : List[Any] = AdamW(m.parameters() , lr=10.0 ) if is_torch_available() else None _UpperCAmelCase : Optional[Any] = 10 def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : Optional[Any]=None): self.assertEqual(len(lowerCAmelCase__) , len(lowerCAmelCase__)) for a, b in zip(lowerCAmelCase__ , lowerCAmelCase__): self.assertAlmostEqual(lowerCAmelCase__ , lowerCAmelCase__ , delta=lowerCAmelCase__ , msg=lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Dict = {"num_warmup_steps": 2, "num_training_steps": 10} # schedulers doct format # function: (sched_args_dict, expected_learning_rates) SCREAMING_SNAKE_CASE_: Dict = { get_constant_schedule: ({}, [10.0] * self.num_steps), get_constant_schedule_with_warmup: ( {"num_warmup_steps": 4}, [0.0, 2.5, 5.0, 7.5, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0], ), get_linear_schedule_with_warmup: ( {**common_kwargs}, [0.0, 5.0, 10.0, 8.75, 7.5, 6.25, 5.0, 3.75, 2.5, 1.25], ), get_cosine_schedule_with_warmup: ( {**common_kwargs}, [0.0, 5.0, 10.0, 9.61, 8.53, 6.91, 5.0, 3.08, 1.46, 0.38], ), get_cosine_with_hard_restarts_schedule_with_warmup: ( {**common_kwargs, "num_cycles": 2}, [0.0, 5.0, 10.0, 8.53, 5.0, 1.46, 10.0, 8.53, 5.0, 1.46], ), get_polynomial_decay_schedule_with_warmup: ( {**common_kwargs, "power": 2.0, "lr_end": 1E-7}, [0.0, 5.0, 10.0, 7.656, 5.625, 3.906, 2.5, 1.406, 0.625, 0.156], ), get_inverse_sqrt_schedule: ( {"num_warmup_steps": 2}, [0.0, 5.0, 10.0, 8.165, 7.071, 6.325, 5.774, 5.345, 5.0, 4.714], ), } for scheduler_func, data in scheds.items(): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = data SCREAMING_SNAKE_CASE_: List[Any] = scheduler_func(self.optimizer , **lowerCAmelCase__) self.assertEqual(len([scheduler.get_lr()[0]]) , 1) SCREAMING_SNAKE_CASE_: int = unwrap_schedule(lowerCAmelCase__ , self.num_steps) self.assertListAlmostEqual( lowerCAmelCase__ , lowerCAmelCase__ , tol=1E-2 , msg=F"failed for {scheduler_func} in normal scheduler" , ) SCREAMING_SNAKE_CASE_: List[str] = scheduler_func(self.optimizer , **lowerCAmelCase__) if scheduler_func.__name__ != "get_constant_schedule": LambdaScheduleWrapper.wrap_scheduler(lowerCAmelCase__) # wrap to test picklability of the schedule SCREAMING_SNAKE_CASE_: Tuple = unwrap_and_save_reload_schedule(lowerCAmelCase__ , self.num_steps) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ , msg=F"failed for {scheduler_func} in save and reload") class __lowercase : """simple docstring""" def __init__( self : str , lowerCAmelCase__ : List[str]): SCREAMING_SNAKE_CASE_: List[Any] = fn def __call__( self : Optional[int] , *lowerCAmelCase__ : List[Any] , **lowerCAmelCase__ : Tuple): return self.fn(*lowerCAmelCase__ , **lowerCAmelCase__) @classmethod def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : str): SCREAMING_SNAKE_CASE_: str = list(map(self , scheduler.lr_lambdas))
13
1
import collections import inspect import unittest from transformers import FocalNetConfig 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_backbone_common import BackboneTesterMixin 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 ( FocalNetBackbone, FocalNetForImageClassification, FocalNetForMaskedImageModeling, FocalNetModel, ) from transformers.models.focalnet.modeling_focalnet import FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class __lowercase : """simple docstring""" def __init__( self : Optional[Any] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[str]=13 , lowerCAmelCase__ : str=32 , lowerCAmelCase__ : Tuple=2 , lowerCAmelCase__ : List[str]=3 , lowerCAmelCase__ : Optional[int]=16 , lowerCAmelCase__ : Optional[int]=[32, 64, 128] , lowerCAmelCase__ : Union[str, Any]=[1, 2, 1] , lowerCAmelCase__ : Optional[int]=[2, 2, 4] , lowerCAmelCase__ : Optional[int]=2 , lowerCAmelCase__ : Dict=2.0 , lowerCAmelCase__ : Dict=True , lowerCAmelCase__ : List[str]=0.0 , lowerCAmelCase__ : Union[str, Any]=0.0 , lowerCAmelCase__ : Optional[int]=0.1 , lowerCAmelCase__ : Optional[Any]="gelu" , lowerCAmelCase__ : Union[str, Any]=False , lowerCAmelCase__ : str=True , lowerCAmelCase__ : Any=0.02 , lowerCAmelCase__ : List[str]=1E-5 , lowerCAmelCase__ : Optional[int]=True , lowerCAmelCase__ : Union[str, Any]=None , lowerCAmelCase__ : Union[str, Any]=True , lowerCAmelCase__ : Any=10 , lowerCAmelCase__ : Optional[int]=8 , lowerCAmelCase__ : List[Any]=["stage1", "stage2"] , lowerCAmelCase__ : str=[1, 2] , ): SCREAMING_SNAKE_CASE_: Tuple = parent SCREAMING_SNAKE_CASE_: str = batch_size SCREAMING_SNAKE_CASE_: int = image_size SCREAMING_SNAKE_CASE_: str = patch_size SCREAMING_SNAKE_CASE_: Optional[Any] = num_channels SCREAMING_SNAKE_CASE_: str = embed_dim SCREAMING_SNAKE_CASE_: Optional[int] = hidden_sizes SCREAMING_SNAKE_CASE_: str = depths SCREAMING_SNAKE_CASE_: Optional[int] = num_heads SCREAMING_SNAKE_CASE_: Optional[Any] = window_size SCREAMING_SNAKE_CASE_: int = mlp_ratio SCREAMING_SNAKE_CASE_: Any = qkv_bias SCREAMING_SNAKE_CASE_: List[str] = hidden_dropout_prob SCREAMING_SNAKE_CASE_: List[str] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_: Optional[Any] = drop_path_rate SCREAMING_SNAKE_CASE_: Optional[int] = hidden_act SCREAMING_SNAKE_CASE_: Dict = use_absolute_embeddings SCREAMING_SNAKE_CASE_: Any = patch_norm SCREAMING_SNAKE_CASE_: str = layer_norm_eps SCREAMING_SNAKE_CASE_: Any = initializer_range SCREAMING_SNAKE_CASE_: List[str] = is_training SCREAMING_SNAKE_CASE_: Dict = scope SCREAMING_SNAKE_CASE_: Any = use_labels SCREAMING_SNAKE_CASE_: List[str] = type_sequence_label_size SCREAMING_SNAKE_CASE_: Optional[int] = encoder_stride SCREAMING_SNAKE_CASE_: int = out_features SCREAMING_SNAKE_CASE_: str = out_indices def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]) SCREAMING_SNAKE_CASE_: List[Any] = None if self.use_labels: SCREAMING_SNAKE_CASE_: int = ids_tensor([self.batch_size] , self.type_sequence_label_size) SCREAMING_SNAKE_CASE_: Optional[Any] = self.get_config() return config, pixel_values, labels def _SCREAMING_SNAKE_CASE ( self : Tuple): return FocalNetConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , embed_dim=self.embed_dim , hidden_sizes=self.hidden_sizes , depths=self.depths , num_heads=self.num_heads , window_size=self.window_size , mlp_ratio=self.mlp_ratio , qkv_bias=self.qkv_bias , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , drop_path_rate=self.drop_path_rate , hidden_act=self.hidden_act , use_absolute_embeddings=self.use_absolute_embeddings , path_norm=self.patch_norm , layer_norm_eps=self.layer_norm_eps , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , out_features=self.out_features , out_indices=self.out_indices , ) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Optional[Any]): SCREAMING_SNAKE_CASE_: Optional[Any] = FocalNetModel(config=lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: Tuple = model(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths) - 1)) SCREAMING_SNAKE_CASE_: Optional[int] = int(config.embed_dim * 2 ** (len(config.depths) - 1)) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, expected_seq_len, expected_dim)) def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : List[Any]): SCREAMING_SNAKE_CASE_: str = FocalNetBackbone(config=lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: Tuple = model(lowerCAmelCase__) # verify feature maps self.parent.assertEqual(len(result.feature_maps) , len(config.out_features)) self.parent.assertListEqual(list(result.feature_maps[0].shape) , [self.batch_size, self.image_size, 8, 8]) # verify channels self.parent.assertEqual(len(model.channels) , len(config.out_features)) self.parent.assertListEqual(model.channels , config.hidden_sizes[:-1]) # verify backbone works with out_features=None SCREAMING_SNAKE_CASE_: Optional[Any] = None SCREAMING_SNAKE_CASE_: Optional[int] = FocalNetBackbone(config=lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: List[str] = model(lowerCAmelCase__) # verify feature maps self.parent.assertEqual(len(result.feature_maps) , 1) self.parent.assertListEqual(list(result.feature_maps[0].shape) , [self.batch_size, self.image_size * 2, 4, 4]) # verify channels self.parent.assertEqual(len(model.channels) , 1) self.parent.assertListEqual(model.channels , [config.hidden_sizes[-1]]) def _SCREAMING_SNAKE_CASE ( self : Tuple , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Optional[int] = FocalNetForMaskedImageModeling(config=lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: List[Any] = model(lowerCAmelCase__) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size)) # test greyscale images SCREAMING_SNAKE_CASE_: Optional[Any] = 1 SCREAMING_SNAKE_CASE_: Any = FocalNetForMaskedImageModeling(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: int = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]) SCREAMING_SNAKE_CASE_: str = model(lowerCAmelCase__) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size)) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Union[str, Any] = self.type_sequence_label_size SCREAMING_SNAKE_CASE_: str = FocalNetForImageClassification(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: int = model(lowerCAmelCase__ , labels=lowerCAmelCase__) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) # test greyscale images SCREAMING_SNAKE_CASE_: Optional[Any] = 1 SCREAMING_SNAKE_CASE_: Optional[Any] = FocalNetForImageClassification(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: Optional[int] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]) SCREAMING_SNAKE_CASE_: str = model(lowerCAmelCase__) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: List[Any] = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = config_and_inputs SCREAMING_SNAKE_CASE_: Dict = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class __lowercase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : Optional[int] = ( ( FocalNetModel, FocalNetForImageClassification, FocalNetForMaskedImageModeling, FocalNetBackbone, ) if is_torch_available() else () ) _UpperCAmelCase : Dict = ( {'''feature-extraction''': FocalNetModel, '''image-classification''': FocalNetForImageClassification} if is_torch_available() else {} ) _UpperCAmelCase : int = False _UpperCAmelCase : List[str] = False _UpperCAmelCase : List[Any] = False _UpperCAmelCase : Optional[Any] = False _UpperCAmelCase : Optional[int] = False def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Optional[Any] = FocalNetModelTester(self) SCREAMING_SNAKE_CASE_: str = ConfigTester(self , config_class=lowerCAmelCase__ , embed_dim=37 , has_text_modality=lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Any): self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def _SCREAMING_SNAKE_CASE ( self : int): return def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_: Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__) @unittest.skip(reason="FocalNet does not use inputs_embeds") def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): pass @unittest.skip(reason="FocalNet does not use feedforward chunking") def _SCREAMING_SNAKE_CASE ( self : Optional[int]): pass def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes[:-1]: SCREAMING_SNAKE_CASE_: str = model_class(lowerCAmelCase__) self.assertIsInstance(model.get_input_embeddings() , (nn.Module)) SCREAMING_SNAKE_CASE_: Union[str, Any] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCAmelCase__ , nn.Linear)) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes[:-1]: SCREAMING_SNAKE_CASE_: Optional[int] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = inspect.signature(model.forward) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE_: List[Any] = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE_: List[Any] = ["pixel_values"] self.assertListEqual(arg_names[:1] , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : int , lowerCAmelCase__ : str , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[int]): SCREAMING_SNAKE_CASE_: Tuple = model_class(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE_: Tuple = model(**self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__)) SCREAMING_SNAKE_CASE_: List[str] = outputs.hidden_states SCREAMING_SNAKE_CASE_: Any = getattr( self.model_tester , "expected_num_hidden_layers" , len(self.model_tester.depths) + 1) self.assertEqual(len(lowerCAmelCase__) , lowerCAmelCase__) # FocalNet has a different seq_length SCREAMING_SNAKE_CASE_: Optional[Any] = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable) else (config.patch_size, config.patch_size) ) SCREAMING_SNAKE_CASE_: List[str] = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.assertListEqual( list(hidden_states[0].shape[-2:]) , [num_patches, self.model_tester.embed_dim] , ) SCREAMING_SNAKE_CASE_: Dict = outputs.reshaped_hidden_states self.assertEqual(len(lowerCAmelCase__) , lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = reshaped_hidden_states[0].shape SCREAMING_SNAKE_CASE_: List[Any] = ( reshaped_hidden_states[0].view(lowerCAmelCase__ , lowerCAmelCase__ , height * width).permute(0 , 2 , 1) ) self.assertListEqual( list(reshaped_hidden_states.shape[-2:]) , [num_patches, self.model_tester.embed_dim] , ) def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_: Dict = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable) else (self.model_tester.image_size, self.model_tester.image_size) ) for model_class in self.all_model_classes[:-1]: SCREAMING_SNAKE_CASE_: List[str] = True self.check_hidden_states_output(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE_: List[Any] = True self.check_hidden_states_output(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_: List[str] = 3 SCREAMING_SNAKE_CASE_: int = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable) else (self.model_tester.image_size, self.model_tester.image_size) ) SCREAMING_SNAKE_CASE_: Optional[Any] = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable) else (config.patch_size, config.patch_size) ) SCREAMING_SNAKE_CASE_: List[Any] = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0]) SCREAMING_SNAKE_CASE_: List[Any] = image_size[1] + patch_size[1] - (image_size[1] % patch_size[1]) for model_class in self.all_model_classes[:-1]: SCREAMING_SNAKE_CASE_: Optional[int] = True self.check_hidden_states_output(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , (padded_height, padded_width)) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE_: Any = True self.check_hidden_states_output(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , (padded_height, padded_width)) @slow def _SCREAMING_SNAKE_CASE ( self : Optional[int]): for model_name in FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_: str = FocalNetModel.from_pretrained(lowerCAmelCase__) self.assertIsNotNone(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_: int = _config_zero_init(lowerCAmelCase__) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_: str = model_class(config=lowerCAmelCase__) for name, param in model.named_parameters(): if "embeddings" not in name and 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" , ) @require_vision @require_torch class __lowercase ( unittest.TestCase ): """simple docstring""" @cached_property def _SCREAMING_SNAKE_CASE ( self : List[str]): # TODO update organization return AutoImageProcessor.from_pretrained("microsoft/focalnet-tiny") if is_vision_available() else None @slow def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: Dict = FocalNetForImageClassification.from_pretrained("microsoft/focalnet-tiny").to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = self.default_image_processor SCREAMING_SNAKE_CASE_: Optional[Any] = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png") SCREAMING_SNAKE_CASE_: Tuple = image_processor(images=lowerCAmelCase__ , return_tensors="pt").to(lowerCAmelCase__) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_: Optional[int] = model(**lowerCAmelCase__) # verify the logits SCREAMING_SNAKE_CASE_: Any = torch.Size((1, 1000)) self.assertEqual(outputs.logits.shape , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = torch.tensor([0.2166, -0.4368, 0.2191]).to(lowerCAmelCase__) self.assertTrue(torch.allclose(outputs.logits[0, :3] , lowerCAmelCase__ , atol=1E-4)) self.assertTrue(outputs.logits.argmax(dim=-1).item() , 281) @require_torch class __lowercase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : int = (FocalNetBackbone,) if is_torch_available() else () _UpperCAmelCase : int = FocalNetConfig _UpperCAmelCase : Union[str, Any] = False def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_: Optional[Any] = FocalNetModelTester(self)
13
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, Features, Value from .base import TaskTemplate @dataclass(frozen=UpperCAmelCase_ ) class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : str = field(default='''automatic-speech-recognition''' , metadata={'''include_in_asdict_even_if_is_default''': True} ) _UpperCAmelCase : ClassVar[Features] = Features({'''audio''': Audio()} ) _UpperCAmelCase : ClassVar[Features] = Features({'''transcription''': Value('''string''' )} ) _UpperCAmelCase : str = "audio" _UpperCAmelCase : str = "transcription" def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int): if self.audio_column not in features: raise ValueError(F"Column {self.audio_column} is not present in features.") if not isinstance(features[self.audio_column] , lowerCAmelCase__): raise ValueError(F"Column {self.audio_column} is not an Audio type.") SCREAMING_SNAKE_CASE_: Tuple = copy.deepcopy(self) SCREAMING_SNAKE_CASE_: Optional[int] = self.input_schema.copy() SCREAMING_SNAKE_CASE_: Dict = features[self.audio_column] SCREAMING_SNAKE_CASE_: int = input_schema return task_template @property def _SCREAMING_SNAKE_CASE ( self : int): return {self.audio_column: "audio", self.transcription_column: "transcription"}
13
1
from ..utils import DummyObject, requires_backends class __lowercase ( metaclass=UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : Dict = ['''transformers''', '''torch''', '''note_seq'''] def __init__( self : Any , *lowerCAmelCase__ : int , **lowerCAmelCase__ : Any): requires_backends(self , ["transformers", "torch", "note_seq"]) @classmethod def _SCREAMING_SNAKE_CASE ( cls : int , *lowerCAmelCase__ : Tuple , **lowerCAmelCase__ : Optional[int]): requires_backends(cls , ["transformers", "torch", "note_seq"]) @classmethod def _SCREAMING_SNAKE_CASE ( cls : List[Any] , *lowerCAmelCase__ : List[str] , **lowerCAmelCase__ : Tuple): requires_backends(cls , ["transformers", "torch", "note_seq"])
13
import unittest import numpy as np from transformers import is_flax_available from transformers.testing_utils import require_flax from ..test_modeling_flax_common import ids_tensor if is_flax_available(): import jax import jax.numpy as jnp from transformers.generation import ( FlaxForcedBOSTokenLogitsProcessor, FlaxForcedEOSTokenLogitsProcessor, FlaxLogitsProcessorList, FlaxMinLengthLogitsProcessor, FlaxTemperatureLogitsWarper, FlaxTopKLogitsWarper, FlaxTopPLogitsWarper, ) @require_flax class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : int , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: str = jnp.ones((batch_size, length)) / length return scores def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Dict = None SCREAMING_SNAKE_CASE_: str = 20 SCREAMING_SNAKE_CASE_: List[Any] = self._get_uniform_logits(batch_size=2 , length=lowerCAmelCase__) # tweak scores to not be uniform anymore SCREAMING_SNAKE_CASE_: List[str] = scores.at[1, 5].set((1 / length) + 0.1) # peak, 1st batch SCREAMING_SNAKE_CASE_: Any = scores.at[1, 10].set((1 / length) - 0.4) # valley, 1st batch # compute softmax SCREAMING_SNAKE_CASE_: Dict = jax.nn.softmax(lowerCAmelCase__ , axis=-1) SCREAMING_SNAKE_CASE_: Optional[Any] = FlaxTemperatureLogitsWarper(temperature=0.5) SCREAMING_SNAKE_CASE_: List[str] = FlaxTemperatureLogitsWarper(temperature=1.3) SCREAMING_SNAKE_CASE_: str = jax.nn.softmax(temp_dist_warper_sharper(lowerCAmelCase__ , scores.copy() , cur_len=lowerCAmelCase__) , axis=-1) SCREAMING_SNAKE_CASE_: int = jax.nn.softmax(temp_dist_warper_smoother(lowerCAmelCase__ , scores.copy() , cur_len=lowerCAmelCase__) , axis=-1) # uniform distribution stays uniform self.assertTrue(jnp.allclose(probs[0, :] , warped_prob_sharp[0, :] , atol=1E-3)) self.assertTrue(jnp.allclose(probs[0, :] , warped_prob_smooth[0, :] , atol=1E-3)) # sharp peaks get higher, valleys get lower self.assertLess(probs[1, :].max() , warped_prob_sharp[1, :].max()) self.assertGreater(probs[1, :].min() , warped_prob_sharp[1, :].min()) # smooth peaks get lower, valleys get higher self.assertGreater(probs[1, :].max() , warped_prob_smooth[1, :].max()) self.assertLess(probs[1, :].min() , warped_prob_smooth[1, :].min()) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: List[str] = None SCREAMING_SNAKE_CASE_: str = 10 SCREAMING_SNAKE_CASE_: Tuple = 2 # create ramp distribution SCREAMING_SNAKE_CASE_: Optional[Any] = np.broadcast_to(np.arange(lowerCAmelCase__)[None, :] , (batch_size, vocab_size)).copy() SCREAMING_SNAKE_CASE_: Dict = ramp_logits[1:, : vocab_size // 2] + vocab_size SCREAMING_SNAKE_CASE_: Union[str, Any] = FlaxTopKLogitsWarper(3) SCREAMING_SNAKE_CASE_: Dict = top_k_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) # check that correct tokens are filtered self.assertListEqual(jnp.isinf(scores[0]).tolist() , 7 * [True] + 3 * [False]) self.assertListEqual(jnp.isinf(scores[1]).tolist() , 2 * [True] + 3 * [False] + 5 * [True]) # check special case SCREAMING_SNAKE_CASE_: Any = 5 SCREAMING_SNAKE_CASE_: str = FlaxTopKLogitsWarper(top_k=1 , filter_value=0.0 , min_tokens_to_keep=3) SCREAMING_SNAKE_CASE_: Any = np.broadcast_to(np.arange(lowerCAmelCase__)[None, :] , (batch_size, length)).copy() SCREAMING_SNAKE_CASE_: Any = top_k_warp_safety_check(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) # min_tokens overwrites k: 3 tokens are kept => 2 tokens are nullified self.assertListEqual((scores == 0.0).sum(axis=-1).tolist() , [2, 2]) def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Tuple = None SCREAMING_SNAKE_CASE_: Dict = 10 SCREAMING_SNAKE_CASE_: Dict = 2 # create distribution and take log (inverse to Softmax as taken in TopPLogitsWarper) SCREAMING_SNAKE_CASE_: Tuple = np.log(np.array([[0.3, 0.1, 0.1, 0.5], [0.15, 0.3, 0.3, 0.25]])) SCREAMING_SNAKE_CASE_: int = FlaxTopPLogitsWarper(0.8) SCREAMING_SNAKE_CASE_: Optional[Any] = np.exp(top_p_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__)) # dist should be filtered to keep min num values so that sum is >= top_p # exp (-inf) => 0 SCREAMING_SNAKE_CASE_: Dict = np.array([[0.3, 0.0, 0.0, 0.5], [0.0, 0.3, 0.3, 0.25]]) self.assertTrue(np.allclose(lowerCAmelCase__ , lowerCAmelCase__ , atol=1E-3)) # check edge cases with negative and extreme logits SCREAMING_SNAKE_CASE_: Union[str, Any] = np.broadcast_to(np.arange(lowerCAmelCase__)[None, :] , (batch_size, vocab_size)).copy() - ( vocab_size // 2 ) # make ramp_logits more extreme SCREAMING_SNAKE_CASE_: Dict = ramp_logits[1] * 100.0 # make sure at least 2 tokens are kept SCREAMING_SNAKE_CASE_: str = FlaxTopPLogitsWarper(0.9 , min_tokens_to_keep=2 , filter_value=0.0) SCREAMING_SNAKE_CASE_: Any = top_p_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) # first batch should keep three tokens, second batch would keep only 1, but due to `min_tokens_to_keep=2` keeps 2. self.assertListEqual((filtered_dist != 0.0).sum(axis=-1).tolist() , [3, 2]) def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Tuple = 20 SCREAMING_SNAKE_CASE_: List[str] = 4 SCREAMING_SNAKE_CASE_: Optional[int] = 0 SCREAMING_SNAKE_CASE_: str = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=lowerCAmelCase__) # check that min length is applied at length 5 SCREAMING_SNAKE_CASE_: str = ids_tensor((batch_size, 20) , vocab_size=20) SCREAMING_SNAKE_CASE_: int = 5 SCREAMING_SNAKE_CASE_: List[Any] = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = min_dist_processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) self.assertListEqual(scores_before_min_length[:, eos_token_id].tolist() , 4 * [-float("inf")]) # check that min length is not applied anymore at length 15 SCREAMING_SNAKE_CASE_: List[str] = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = 15 SCREAMING_SNAKE_CASE_: Any = min_dist_processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) self.assertFalse(jnp.isinf(lowerCAmelCase__).any()) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: int = 20 SCREAMING_SNAKE_CASE_: str = 4 SCREAMING_SNAKE_CASE_: List[Any] = 0 SCREAMING_SNAKE_CASE_: Optional[Any] = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=lowerCAmelCase__) # check that all scores are -inf except the bos_token_id score SCREAMING_SNAKE_CASE_: int = ids_tensor((batch_size, 1) , vocab_size=20) SCREAMING_SNAKE_CASE_: List[str] = 1 SCREAMING_SNAKE_CASE_: Union[str, Any] = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = logits_processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) self.assertTrue(jnp.isneginf(scores[:, bos_token_id + 1 :]).all()) self.assertListEqual(scores[:, bos_token_id].tolist() , 4 * [0]) # score for bos_token_id shold be zero # check that bos_token_id is not forced if current length is greater than 1 SCREAMING_SNAKE_CASE_: List[Any] = 3 SCREAMING_SNAKE_CASE_: Optional[Any] = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = logits_processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) self.assertFalse(jnp.isinf(lowerCAmelCase__).any()) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Any = 20 SCREAMING_SNAKE_CASE_: Optional[Any] = 4 SCREAMING_SNAKE_CASE_: Dict = 0 SCREAMING_SNAKE_CASE_: List[Any] = 5 SCREAMING_SNAKE_CASE_: Union[str, Any] = FlaxForcedEOSTokenLogitsProcessor(max_length=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__) # check that all scores are -inf except the eos_token_id when max_length is reached SCREAMING_SNAKE_CASE_: List[Any] = ids_tensor((batch_size, 4) , vocab_size=20) SCREAMING_SNAKE_CASE_: Optional[int] = 4 SCREAMING_SNAKE_CASE_: Dict = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = logits_processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) self.assertTrue(jnp.isneginf(scores[:, eos_token_id + 1 :]).all()) self.assertListEqual(scores[:, eos_token_id].tolist() , 4 * [0]) # score for eos_token_id should be zero # check that eos_token_id is not forced if max_length is not reached SCREAMING_SNAKE_CASE_: List[str] = 3 SCREAMING_SNAKE_CASE_: str = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = logits_processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) self.assertFalse(jnp.isinf(lowerCAmelCase__).any()) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: int = 4 SCREAMING_SNAKE_CASE_: List[Any] = 10 SCREAMING_SNAKE_CASE_: int = 15 SCREAMING_SNAKE_CASE_: Dict = 2 SCREAMING_SNAKE_CASE_: int = 1 SCREAMING_SNAKE_CASE_: List[Any] = 15 # dummy input_ids and scores SCREAMING_SNAKE_CASE_: int = ids_tensor((batch_size, sequence_length) , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = input_ids.copy() SCREAMING_SNAKE_CASE_: List[Any] = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = scores.copy() # instantiate all dist processors SCREAMING_SNAKE_CASE_: Optional[int] = FlaxTemperatureLogitsWarper(temperature=0.5) SCREAMING_SNAKE_CASE_: Tuple = FlaxTopKLogitsWarper(3) SCREAMING_SNAKE_CASE_: Optional[int] = FlaxTopPLogitsWarper(0.8) # instantiate all logits processors SCREAMING_SNAKE_CASE_: Optional[int] = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = FlaxForcedEOSTokenLogitsProcessor(max_length=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = 10 # no processor list SCREAMING_SNAKE_CASE_: Dict = temp_dist_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = top_k_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = top_p_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = min_dist_proc(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = bos_dist_proc(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = eos_dist_proc(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) # with processor list SCREAMING_SNAKE_CASE_: str = FlaxLogitsProcessorList( [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc]) SCREAMING_SNAKE_CASE_: Tuple = processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) # scores should be equal self.assertTrue(jnp.allclose(lowerCAmelCase__ , lowerCAmelCase__ , atol=1E-3)) # input_ids should never be changed self.assertListEqual(input_ids.tolist() , input_ids_comp.tolist()) def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_: Optional[int] = 4 SCREAMING_SNAKE_CASE_: int = 10 SCREAMING_SNAKE_CASE_: List[str] = 15 SCREAMING_SNAKE_CASE_: List[Any] = 2 SCREAMING_SNAKE_CASE_: Union[str, Any] = 1 SCREAMING_SNAKE_CASE_: str = 15 # dummy input_ids and scores SCREAMING_SNAKE_CASE_: Tuple = ids_tensor((batch_size, sequence_length) , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = input_ids.copy() SCREAMING_SNAKE_CASE_: List[Any] = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = scores.copy() # instantiate all dist processors SCREAMING_SNAKE_CASE_: Dict = FlaxTemperatureLogitsWarper(temperature=0.5) SCREAMING_SNAKE_CASE_: Union[str, Any] = FlaxTopKLogitsWarper(3) SCREAMING_SNAKE_CASE_: Dict = FlaxTopPLogitsWarper(0.8) # instantiate all logits processors SCREAMING_SNAKE_CASE_: int = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = FlaxForcedEOSTokenLogitsProcessor(max_length=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = 10 # no processor list def run_no_processor_list(lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Dict): SCREAMING_SNAKE_CASE_: Any = temp_dist_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = top_k_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = top_p_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = min_dist_proc(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = bos_dist_proc(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = eos_dist_proc(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) return scores # with processor list def run_processor_list(lowerCAmelCase__ : int , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Union[str, Any]): SCREAMING_SNAKE_CASE_: List[str] = FlaxLogitsProcessorList( [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc]) SCREAMING_SNAKE_CASE_: Dict = processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) return scores SCREAMING_SNAKE_CASE_: str = jax.jit(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = jax.jit(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = jitted_run_no_processor_list(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = jitted_run_processor_list(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) # scores should be equal self.assertTrue(jnp.allclose(lowerCAmelCase__ , lowerCAmelCase__ , atol=1E-3)) # input_ids should never be changed self.assertListEqual(input_ids.tolist() , input_ids_comp.tolist())
13
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_torch_available, ) lowerCAmelCase : str = { """configuration_trocr""": ["""TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP""", """TrOCRConfig"""], """processing_trocr""": ["""TrOCRProcessor"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : List[str] = [ """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 lowerCAmelCase : Union[str, Any] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
13
import math import sys def A_ ( _UpperCAmelCase ): if number != int(_UpperCAmelCase ): raise ValueError("the value of input must be a natural number" ) if number < 0: raise ValueError("the value of input must not be a negative number" ) if number == 0: return 1 SCREAMING_SNAKE_CASE_: List[str] = [-1] * (number + 1) SCREAMING_SNAKE_CASE_: str = 0 for i in range(1 , number + 1 ): SCREAMING_SNAKE_CASE_: str = sys.maxsize SCREAMING_SNAKE_CASE_: List[Any] = int(math.sqrt(_UpperCAmelCase ) ) for j in range(1 , root + 1 ): SCREAMING_SNAKE_CASE_: List[str] = 1 + answers[i - (j**2)] SCREAMING_SNAKE_CASE_: Optional[Any] = min(_UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Dict = answer return answers[number] if __name__ == "__main__": import doctest doctest.testmod()
13
1
from copy import deepcopy import torch import torch.nn.functional as F from torch.optim import AdamW from torch.optim.lr_scheduler import LambdaLR from torch.utils.data import DataLoader from accelerate.accelerator import Accelerator from accelerate.state import GradientState from accelerate.test_utils import RegressionDataset, RegressionModel from accelerate.utils import DistributedType, is_torch_version, set_seed def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): for param, grad_param in zip(model_a.parameters() , model_b.parameters() ): if not param.requires_grad: continue if not did_step: # Grads should not be in sync assert ( torch.allclose(param.grad , grad_param.grad ) is False ), f"Gradients in sync when they should not be at iteration {iteration}:\nmodel_a grad ({param.grad}) == model_b grad ({grad_param.grad})" else: # Grads should be in sync assert ( torch.allclose(param.grad , grad_param.grad ) is True ), f"Gradients not in sync when they should be at iteration {iteration}:\nmodel_a grad ({param.grad}) != model_b grad ({grad_param.grad})" def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=True ): model.train() SCREAMING_SNAKE_CASE_: Any = model(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[Any] = F.mse_loss(_UpperCAmelCase , target.to(output.device ) ) if not do_backward: loss /= accelerator.gradient_accumulation_steps loss.backward() else: accelerator.backward(_UpperCAmelCase ) def A_ ( _UpperCAmelCase , _UpperCAmelCase=False ): set_seed(42 ) SCREAMING_SNAKE_CASE_: Tuple = RegressionModel() SCREAMING_SNAKE_CASE_: Optional[int] = deepcopy(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Union[str, Any] = RegressionDataset(length=80 ) SCREAMING_SNAKE_CASE_: Union[str, Any] = DataLoader(_UpperCAmelCase , batch_size=16 ) model.to(accelerator.device ) if sched: SCREAMING_SNAKE_CASE_: List[Any] = AdamW(params=model.parameters() , lr=1e-3 ) SCREAMING_SNAKE_CASE_: str = AdamW(params=ddp_model.parameters() , lr=1e-3 ) SCREAMING_SNAKE_CASE_: Union[str, Any] = LambdaLR(_UpperCAmelCase , lr_lambda=lambda _UpperCAmelCase : epoch**0.6_5 ) SCREAMING_SNAKE_CASE_: List[Any] = LambdaLR(_UpperCAmelCase , lr_lambda=lambda _UpperCAmelCase : epoch**0.6_5 ) # Make a copy of `model` if sched: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = accelerator.prepare(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) else: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = accelerator.prepare(_UpperCAmelCase , _UpperCAmelCase ) if sched: return (model, opt, sched, dataloader, ddp_model, ddp_opt, ddp_sched) return model, ddp_model, dataloader def A_ ( _UpperCAmelCase ): # Test when on a single CPU or GPU that the context manager does nothing SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = get_training_setup(_UpperCAmelCase ) # Use a single batch SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = next(iter(_UpperCAmelCase ) ).values() for iteration in range(3 ): # Gather the distributed inputs and targs for the base model SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = accelerator.gather((ddp_input, ddp_target) ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" step_model(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) # Do "gradient accumulation" (noop) if iteration % 2 == 0: # Accumulate grads locally with accelerator.no_sync(_UpperCAmelCase ): step_model(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) else: # Sync grads step_model(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) # Since `no_sync` is a noop, `ddp_model` and `model` grads should always be in sync check_model_parameters(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) for param, ddp_param in zip(model.parameters() , ddp_model.parameters() ): if not param.requires_grad: continue assert torch.allclose( param.grad , ddp_param.grad ), f"Gradients not in sync when they should be:\nModel grad ({param.grad}) != DDP grad ({ddp_param.grad})" # Shuffle ddp_input on each iteration torch.manual_seed(13_37 + iteration ) SCREAMING_SNAKE_CASE_: Optional[Any] = ddp_input[torch.randperm(len(_UpperCAmelCase ) )] def A_ ( _UpperCAmelCase ): # Test on distributed setup that context manager behaves properly SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = get_training_setup(_UpperCAmelCase ) # Use a single batch SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = next(iter(_UpperCAmelCase ) ).values() for iteration in range(3 ): # Gather the distributed inputs and targs for the base model SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = accelerator.gather((ddp_input, ddp_target) ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" step_model(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) # Do "gradient accumulation" (noop) if iteration % 2 == 0: # Accumulate grads locally with accelerator.no_sync(_UpperCAmelCase ): step_model(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) else: # Sync grads step_model(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) # DDP model and model should only be in sync when not (iteration % 2 == 0) for param, ddp_param in zip(model.parameters() , ddp_model.parameters() ): if not param.requires_grad: continue if iteration % 2 == 0: # Grads should not be in sync assert ( torch.allclose(param.grad , ddp_param.grad ) is False ), f"Gradients in sync when they should not be:\nModel grad ({param.grad}) == DDP grad ({ddp_param.grad})" else: # Grads should be in sync assert ( torch.allclose(param.grad , ddp_param.grad ) is True ), f"Gradients not in sync when they should be:\nModel grad ({param.grad}) != DDP grad ({ddp_param.grad})" # Shuffle ddp_input on each iteration torch.manual_seed(13_37 + iteration ) SCREAMING_SNAKE_CASE_: Any = ddp_input[torch.randperm(len(_UpperCAmelCase ) )] def A_ ( _UpperCAmelCase=False , _UpperCAmelCase=False ): SCREAMING_SNAKE_CASE_: List[str] = Accelerator( split_batches=_UpperCAmelCase , dispatch_batches=_UpperCAmelCase , gradient_accumulation_steps=2 ) # Test that context manager behaves properly SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = get_training_setup(_UpperCAmelCase ) for iteration, batch in enumerate(_UpperCAmelCase ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = batch.values() # Gather the distributed inputs and targs for the base model SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = accelerator.gather((ddp_input, ddp_target) ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" step_model(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) # Do "gradient accumulation" (noop) with accelerator.accumulate(_UpperCAmelCase ): step_model(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) # DDP model and model should only be in sync when not (iteration % 2 == 0) for param, ddp_param in zip(model.parameters() , ddp_model.parameters() ): if not param.requires_grad: continue if ((iteration + 1) % 2 == 0) or (iteration == len(_UpperCAmelCase ) - 1): # Grads should be in sync assert ( torch.allclose(param.grad , ddp_param.grad ) is True ), f"Gradients not in sync when they should be at iteration {iteration}:\nModel grad ({param.grad}) != DDP grad ({ddp_param.grad})" else: # Grads should not be in sync assert ( torch.allclose(param.grad , ddp_param.grad ) is False ), f"Gradients in sync when they should not be at iteration {iteration}:\nModel grad ({param.grad}) == DDP grad ({ddp_param.grad})" # Shuffle ddp_input on each iteration torch.manual_seed(13_37 + iteration ) SCREAMING_SNAKE_CASE_: Dict = ddp_input[torch.randperm(len(_UpperCAmelCase ) )] GradientState._reset_state() def A_ ( _UpperCAmelCase=False , _UpperCAmelCase=False ): SCREAMING_SNAKE_CASE_: List[Any] = Accelerator( split_batches=_UpperCAmelCase , dispatch_batches=_UpperCAmelCase , gradient_accumulation_steps=2 ) # Test that context manager behaves properly SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = get_training_setup(_UpperCAmelCase , _UpperCAmelCase ) for iteration, batch in enumerate(_UpperCAmelCase ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = batch.values() # Gather the distributed inputs and targs for the base model SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = accelerator.gather((ddp_input, ddp_target) ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" model.train() ddp_model.train() step_model(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) opt.step() if ((iteration + 1) % 2 == 0) or ((iteration + 1) == len(_UpperCAmelCase )): if split_batches: sched.step() else: for _ in range(accelerator.num_processes ): sched.step() opt.zero_grad() # Perform gradient accumulation under wrapper with accelerator.accumulate(_UpperCAmelCase ): step_model(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) ddp_opt.step() ddp_sched.step() ddp_opt.zero_grad() # Learning rates should be the same assert ( opt.param_groups[0]["lr"] == ddp_opt.param_groups[0]["lr"] ), f"Learning rates found in each optimizer did not align\nopt: {opt.param_groups[0]['lr']}\nDDP opt: {ddp_opt.param_groups[0]['lr']}\n" SCREAMING_SNAKE_CASE_: Optional[Any] = (((iteration + 1) % 2) == 0) or ((iteration + 1) == len(_UpperCAmelCase )) if accelerator.num_processes > 1: check_model_parameters(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) # Shuffle ddp_input on each iteration torch.manual_seed(13_37 + iteration ) GradientState._reset_state() def A_ ( ): SCREAMING_SNAKE_CASE_: List[Any] = Accelerator() SCREAMING_SNAKE_CASE_: Dict = RegressionDataset(length=80 ) SCREAMING_SNAKE_CASE_: Optional[Any] = DataLoader(_UpperCAmelCase , batch_size=16 ) SCREAMING_SNAKE_CASE_: Any = RegressionDataset(length=96 ) SCREAMING_SNAKE_CASE_: Optional[Any] = DataLoader(_UpperCAmelCase , batch_size=16 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = accelerator.prepare(_UpperCAmelCase , _UpperCAmelCase ) assert accelerator.gradient_state.active_dataloader is None for iteration, _ in enumerate(_UpperCAmelCase ): assert id(accelerator.gradient_state.active_dataloader ) == id(_UpperCAmelCase ) if iteration < len(_UpperCAmelCase ) - 1: assert not accelerator.gradient_state.end_of_dataloader if iteration == 1: for batch_num, _ in enumerate(_UpperCAmelCase ): assert id(accelerator.gradient_state.active_dataloader ) == id(_UpperCAmelCase ) if batch_num < len(_UpperCAmelCase ) - 1: assert not accelerator.gradient_state.end_of_dataloader else: assert accelerator.gradient_state.end_of_dataloader else: assert accelerator.gradient_state.end_of_dataloader assert accelerator.gradient_state.active_dataloader is None def A_ ( ): SCREAMING_SNAKE_CASE_: Union[str, Any] = Accelerator() SCREAMING_SNAKE_CASE_: int = accelerator.state if state.local_process_index == 0: print("**Test `accumulate` gradient accumulation with dataloader break**" ) test_dataloader_break() if state.distributed_type == DistributedType.NO: if state.local_process_index == 0: print("**Test NOOP `no_sync` context manager**" ) test_noop_sync(_UpperCAmelCase ) if state.distributed_type in (DistributedType.MULTI_GPU, DistributedType.MULTI_CPU): if state.local_process_index == 0: print("**Test Distributed `no_sync` context manager**" ) test_distributed_sync(_UpperCAmelCase ) if state.distributed_type == DistributedType.MULTI_GPU: for split_batch in [True, False]: for dispatch_batches in [True, False]: if state.local_process_index == 0: print( "**Test `accumulate` gradient accumulation, " , f"`split_batches={split_batch}` and `dispatch_batches={dispatch_batches}`**" , ) test_gradient_accumulation(_UpperCAmelCase , _UpperCAmelCase ) # Currently will break on torch 2.0 +, need to investigate why if is_torch_version("<" , "2.0" ) or state.distributed_type == DistributedType.NO: if state.local_process_index == 0: print( "**Test `accumulate` gradient accumulation with optimizer and scheduler, " , "`split_batches=False`, `dispatch_batches=False`**" , ) test_gradient_accumulation_with_opt_and_scheduler() if state.distributed_type == DistributedType.MULTI_GPU: for split_batch in [True, False]: for dispatch_batches in [True, False]: if not split_batch and not dispatch_batches: continue if state.local_process_index == 0: print( "**Test `accumulate` gradient accumulation with optimizer and scheduler, " , f"`split_batches={split_batch}` and `dispatch_batches={dispatch_batches}`**" , ) test_gradient_accumulation_with_opt_and_scheduler(_UpperCAmelCase , _UpperCAmelCase ) def A_ ( _UpperCAmelCase ): # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
13
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase : Optional[int] = {"""configuration_wavlm""": ["""WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """WavLMConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Any = [ """WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST""", """WavLMForAudioFrameClassification""", """WavLMForCTC""", """WavLMForSequenceClassification""", """WavLMForXVector""", """WavLMModel""", """WavLMPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_wavlm import WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP, WavLMConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_wavlm import ( WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST, WavLMForAudioFrameClassification, WavLMForCTC, WavLMForSequenceClassification, WavLMForXVector, WavLMModel, WavLMPreTrainedModel, ) else: import sys lowerCAmelCase : Union[str, Any] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
13
1
lowerCAmelCase : str = [ 999, 800, 799, 600, 599, 500, 400, 399, 377, 355, 333, 311, 288, 266, 244, 222, 200, 199, 177, 155, 133, 111, 88, 66, 44, 22, 0, ] lowerCAmelCase : Tuple = [ 999, 976, 952, 928, 905, 882, 858, 857, 810, 762, 715, 714, 572, 429, 428, 286, 285, 238, 190, 143, 142, 118, 95, 71, 47, 24, 0, ] lowerCAmelCase : Any = [ 999, 988, 977, 966, 955, 944, 933, 922, 911, 900, 899, 879, 859, 840, 820, 800, 799, 766, 733, 700, 699, 650, 600, 599, 500, 499, 400, 399, 350, 300, 299, 266, 233, 200, 199, 179, 159, 140, 120, 100, 99, 88, 77, 66, 55, 44, 33, 22, 11, 0, ] lowerCAmelCase : Optional[Any] = [ 999, 995, 992, 989, 985, 981, 978, 975, 971, 967, 964, 961, 957, 956, 951, 947, 942, 937, 933, 928, 923, 919, 914, 913, 908, 903, 897, 892, 887, 881, 876, 871, 870, 864, 858, 852, 846, 840, 834, 828, 827, 820, 813, 806, 799, 792, 785, 784, 777, 770, 763, 756, 749, 742, 741, 733, 724, 716, 707, 699, 698, 688, 677, 666, 656, 655, 645, 634, 623, 613, 612, 598, 584, 570, 569, 555, 541, 527, 526, 505, 484, 483, 462, 440, 439, 396, 395, 352, 351, 308, 307, 264, 263, 220, 219, 176, 132, 88, 44, 0, ] lowerCAmelCase : int = [ 999, 997, 995, 992, 990, 988, 986, 984, 981, 979, 977, 975, 972, 970, 968, 966, 964, 961, 959, 957, 956, 954, 951, 949, 946, 944, 941, 939, 936, 934, 931, 929, 926, 924, 921, 919, 916, 914, 913, 910, 907, 905, 902, 899, 896, 893, 891, 888, 885, 882, 879, 877, 874, 871, 870, 867, 864, 861, 858, 855, 852, 849, 846, 843, 840, 837, 834, 831, 828, 827, 824, 821, 817, 814, 811, 808, 804, 801, 798, 795, 791, 788, 785, 784, 780, 777, 774, 770, 766, 763, 760, 756, 752, 749, 746, 742, 741, 737, 733, 730, 726, 722, 718, 714, 710, 707, 703, 699, 698, 694, 690, 685, 681, 677, 673, 669, 664, 660, 656, 655, 650, 646, 641, 636, 632, 627, 622, 618, 613, 612, 607, 602, 596, 591, 586, 580, 575, 570, 569, 563, 557, 551, 545, 539, 533, 527, 526, 519, 512, 505, 498, 491, 484, 483, 474, 466, 457, 449, 440, 439, 428, 418, 407, 396, 395, 381, 366, 352, 351, 330, 308, 307, 286, 264, 263, 242, 220, 219, 176, 175, 132, 131, 88, 44, 0, ] lowerCAmelCase : List[str] = [ 999, 991, 982, 974, 966, 958, 950, 941, 933, 925, 916, 908, 900, 899, 874, 850, 825, 800, 799, 700, 600, 500, 400, 300, 200, 100, 0, ] lowerCAmelCase : List[Any] = [ 999, 992, 985, 978, 971, 964, 957, 949, 942, 935, 928, 921, 914, 907, 900, 899, 879, 859, 840, 820, 800, 799, 766, 733, 700, 699, 650, 600, 599, 500, 499, 400, 399, 300, 299, 200, 199, 100, 99, 0, ] lowerCAmelCase : Optional[int] = [ 999, 996, 992, 989, 985, 982, 979, 975, 972, 968, 965, 961, 958, 955, 951, 948, 944, 941, 938, 934, 931, 927, 924, 920, 917, 914, 910, 907, 903, 900, 899, 891, 884, 876, 869, 861, 853, 846, 838, 830, 823, 815, 808, 800, 799, 788, 777, 766, 755, 744, 733, 722, 711, 700, 699, 688, 677, 666, 655, 644, 633, 622, 611, 600, 599, 585, 571, 557, 542, 528, 514, 500, 499, 485, 471, 457, 442, 428, 414, 400, 399, 379, 359, 340, 320, 300, 299, 279, 259, 240, 220, 200, 199, 166, 133, 100, 99, 66, 33, 0, ]
13
import unittest from transformers import ( MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TextaTextGenerationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, require_tf, require_torch from transformers.utils import is_torch_available from .test_pipelines_common import ANY if is_torch_available(): import torch @is_pipeline_test class __lowercase ( unittest.TestCase ): """simple docstring""" _UpperCAmelCase : List[Any] = MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING _UpperCAmelCase : str = TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING def _SCREAMING_SNAKE_CASE ( self : Any , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Dict): SCREAMING_SNAKE_CASE_: Any = TextaTextGenerationPipeline(model=lowerCAmelCase__ , tokenizer=lowerCAmelCase__) return generator, ["Something to write", "Something else"] def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Any): SCREAMING_SNAKE_CASE_: List[Any] = generator("Something there") self.assertEqual(lowerCAmelCase__ , [{"generated_text": ANY(lowerCAmelCase__)}]) # These are encoder decoder, they don't just append to incoming string self.assertFalse(outputs[0]["generated_text"].startswith("Something there")) SCREAMING_SNAKE_CASE_: List[Any] = generator(["This is great !", "Something else"] , num_return_sequences=2 , do_sample=lowerCAmelCase__) self.assertEqual( lowerCAmelCase__ , [ [{"generated_text": ANY(lowerCAmelCase__)}, {"generated_text": ANY(lowerCAmelCase__)}], [{"generated_text": ANY(lowerCAmelCase__)}, {"generated_text": ANY(lowerCAmelCase__)}], ] , ) SCREAMING_SNAKE_CASE_: Dict = generator( ["This is great !", "Something else"] , num_return_sequences=2 , batch_size=2 , do_sample=lowerCAmelCase__) self.assertEqual( lowerCAmelCase__ , [ [{"generated_text": ANY(lowerCAmelCase__)}, {"generated_text": ANY(lowerCAmelCase__)}], [{"generated_text": ANY(lowerCAmelCase__)}, {"generated_text": ANY(lowerCAmelCase__)}], ] , ) with self.assertRaises(lowerCAmelCase__): generator(4) @require_torch def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Optional[int] = pipeline("text2text-generation" , model="patrickvonplaten/t5-tiny-random" , framework="pt") # do_sample=False necessary for reproducibility SCREAMING_SNAKE_CASE_: Union[str, Any] = generator("Something there" , do_sample=lowerCAmelCase__) self.assertEqual(lowerCAmelCase__ , [{"generated_text": ""}]) SCREAMING_SNAKE_CASE_: Union[str, Any] = 3 SCREAMING_SNAKE_CASE_: Any = generator( "Something there" , num_return_sequences=lowerCAmelCase__ , num_beams=lowerCAmelCase__ , ) SCREAMING_SNAKE_CASE_: Any = [ {"generated_text": "Beide Beide Beide Beide Beide Beide Beide Beide Beide"}, {"generated_text": "Beide Beide Beide Beide Beide Beide Beide Beide"}, {"generated_text": ""}, ] self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = generator("This is a test" , do_sample=lowerCAmelCase__ , num_return_sequences=2 , return_tensors=lowerCAmelCase__) self.assertEqual( lowerCAmelCase__ , [ {"generated_token_ids": ANY(torch.Tensor)}, {"generated_token_ids": ANY(torch.Tensor)}, ] , ) SCREAMING_SNAKE_CASE_: str = generator.model.config.eos_token_id SCREAMING_SNAKE_CASE_: Union[str, Any] = "<pad>" SCREAMING_SNAKE_CASE_: Tuple = generator( ["This is a test", "This is a second test"] , do_sample=lowerCAmelCase__ , num_return_sequences=2 , batch_size=2 , return_tensors=lowerCAmelCase__ , ) self.assertEqual( lowerCAmelCase__ , [ [ {"generated_token_ids": ANY(torch.Tensor)}, {"generated_token_ids": ANY(torch.Tensor)}, ], [ {"generated_token_ids": ANY(torch.Tensor)}, {"generated_token_ids": ANY(torch.Tensor)}, ], ] , ) @require_tf def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = pipeline("text2text-generation" , model="patrickvonplaten/t5-tiny-random" , framework="tf") # do_sample=False necessary for reproducibility SCREAMING_SNAKE_CASE_: List[Any] = generator("Something there" , do_sample=lowerCAmelCase__) self.assertEqual(lowerCAmelCase__ , [{"generated_text": ""}])
13
1
import argparse from collections import OrderedDict from pathlib import Path import torch from transformers import ( VisualBertConfig, VisualBertForMultipleChoice, VisualBertForPreTraining, VisualBertForQuestionAnswering, VisualBertForVisualReasoning, ) from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase : List[str] = logging.get_logger(__name__) lowerCAmelCase : List[Any] = [ ("""bert.bert""", """visual_bert"""), ("""bert.cls""", """cls"""), ("""bert.classifier""", """cls"""), ("""token_type_embeddings_visual""", """visual_token_type_embeddings"""), ("""position_embeddings_visual""", """visual_position_embeddings"""), ("""projection""", """visual_projection"""), ] lowerCAmelCase : List[Any] = [ """nlvr2_coco_pre_trained.th""", """nlvr2_fine_tuned.th""", """nlvr2_pre_trained.th""", """vcr_coco_pre_train.th""", """vcr_fine_tune.th""", """vcr_pre_train.th""", """vqa_coco_pre_trained.th""", """vqa_fine_tuned.th""", """vqa_pre_trained.th""", ] def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: str = torch.load(_UpperCAmelCase , map_location="cpu" ) return sd def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=rename_keys_prefix ): SCREAMING_SNAKE_CASE_: List[Any] = OrderedDict() SCREAMING_SNAKE_CASE_: List[Any] = torch.arange(config.max_position_embeddings ).expand((1, -1) ) # detector_d = OrderedDict() for key in d: if "detector" in key: # detector_d[key.replace('detector.','')] = d[key] continue SCREAMING_SNAKE_CASE_: Tuple = key for name_pair in rename_keys_prefix: SCREAMING_SNAKE_CASE_: List[str] = new_key.replace(name_pair[0] , name_pair[1] ) SCREAMING_SNAKE_CASE_: Optional[int] = d[key] if key == "bert.cls.predictions.decoder.weight": # Old bert code didn't have `decoder.bias`, but was added separately SCREAMING_SNAKE_CASE_: Union[str, Any] = new_d["cls.predictions.bias"] return new_d @torch.no_grad() def A_ ( _UpperCAmelCase , _UpperCAmelCase ): assert ( checkpoint_path.split("/" )[-1] in ACCEPTABLE_CHECKPOINTS ), f"The checkpoint provided must be in {ACCEPTABLE_CHECKPOINTS}." # Get Config if "pre" in checkpoint_path: SCREAMING_SNAKE_CASE_: Any = "pretraining" if "vcr" in checkpoint_path: SCREAMING_SNAKE_CASE_: Optional[Any] = {"visual_embedding_dim": 5_12} elif "vqa_advanced" in checkpoint_path: SCREAMING_SNAKE_CASE_: int = {"visual_embedding_dim": 20_48} elif "vqa" in checkpoint_path: SCREAMING_SNAKE_CASE_: Optional[int] = {"visual_embedding_dim": 20_48} elif "nlvr" in checkpoint_path: SCREAMING_SNAKE_CASE_: List[str] = {"visual_embedding_dim": 10_24} else: raise NotImplementedError(f"No implementation found for `{checkpoint_path}`." ) else: if "vcr" in checkpoint_path: SCREAMING_SNAKE_CASE_: List[str] = {"visual_embedding_dim": 5_12} SCREAMING_SNAKE_CASE_: str = "multichoice" elif "vqa_advanced" in checkpoint_path: SCREAMING_SNAKE_CASE_: Union[str, Any] = {"visual_embedding_dim": 20_48} SCREAMING_SNAKE_CASE_: List[str] = "vqa_advanced" elif "vqa" in checkpoint_path: SCREAMING_SNAKE_CASE_: Union[str, Any] = {"visual_embedding_dim": 20_48, "num_labels": 31_29} SCREAMING_SNAKE_CASE_: Optional[int] = "vqa" elif "nlvr" in checkpoint_path: SCREAMING_SNAKE_CASE_: str = { "visual_embedding_dim": 10_24, "num_labels": 2, } SCREAMING_SNAKE_CASE_: Dict = "nlvr" SCREAMING_SNAKE_CASE_: Dict = VisualBertConfig(**_UpperCAmelCase ) # Load State Dict SCREAMING_SNAKE_CASE_: Union[str, Any] = load_state_dict(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Optional[Any] = get_new_dict(_UpperCAmelCase , _UpperCAmelCase ) if model_type == "pretraining": SCREAMING_SNAKE_CASE_: Dict = VisualBertForPreTraining(_UpperCAmelCase ) elif model_type == "vqa": SCREAMING_SNAKE_CASE_: str = VisualBertForQuestionAnswering(_UpperCAmelCase ) elif model_type == "nlvr": SCREAMING_SNAKE_CASE_: Tuple = VisualBertForVisualReasoning(_UpperCAmelCase ) elif model_type == "multichoice": SCREAMING_SNAKE_CASE_: Optional[Any] = VisualBertForMultipleChoice(_UpperCAmelCase ) model.load_state_dict(_UpperCAmelCase ) # Save Checkpoints Path(_UpperCAmelCase ).mkdir(exist_ok=_UpperCAmelCase ) model.save_pretrained(_UpperCAmelCase ) if __name__ == "__main__": lowerCAmelCase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument("""orig_checkpoint_path""", type=str, help="""A path to .th on local filesystem.""") parser.add_argument("""pytorch_dump_folder_path""", type=str, help="""Path to the output PyTorch model.""") lowerCAmelCase : Optional[int] = parser.parse_args() convert_visual_bert_checkpoint(args.orig_checkpoint_path, args.pytorch_dump_folder_path)
13
def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: List[str] = [0] * len(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[Any] = [] SCREAMING_SNAKE_CASE_: str = [] SCREAMING_SNAKE_CASE_: List[str] = 0 for values in graph.values(): for i in values: indegree[i] += 1 for i in range(len(_UpperCAmelCase ) ): if indegree[i] == 0: queue.append(_UpperCAmelCase ) while queue: SCREAMING_SNAKE_CASE_: Optional[int] = queue.pop(0 ) cnt += 1 topo.append(_UpperCAmelCase ) for x in graph[vertex]: indegree[x] -= 1 if indegree[x] == 0: queue.append(_UpperCAmelCase ) if cnt != len(_UpperCAmelCase ): print("Cycle exists" ) else: print(_UpperCAmelCase ) # Adjacency List of Graph lowerCAmelCase : Any = {0: [1, 2], 1: [3], 2: [3], 3: [4, 5], 4: [], 5: []} topological_sort(graph)
13
1
def A_ ( ): SCREAMING_SNAKE_CASE_: List[Any] = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] SCREAMING_SNAKE_CASE_: Optional[int] = 6 SCREAMING_SNAKE_CASE_: List[str] = 1 SCREAMING_SNAKE_CASE_: str = 19_01 SCREAMING_SNAKE_CASE_: Optional[Any] = 0 while year < 20_01: day += 7 if (year % 4 == 0 and year % 1_00 != 0) or (year % 4_00 == 0): if day > days_per_month[month - 1] and month != 2: month += 1 SCREAMING_SNAKE_CASE_: str = day - days_per_month[month - 2] elif day > 29 and month == 2: month += 1 SCREAMING_SNAKE_CASE_: Optional[Any] = day - 29 else: if day > days_per_month[month - 1]: month += 1 SCREAMING_SNAKE_CASE_: Union[str, Any] = day - days_per_month[month - 2] if month > 12: year += 1 SCREAMING_SNAKE_CASE_: Optional[int] = 1 if year < 20_01 and day == 1: sundays += 1 return sundays if __name__ == "__main__": print(solution())
13
import argparse import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## lowerCAmelCase : Optional[Any] = 16 lowerCAmelCase : List[str] = 32 def A_ ( _UpperCAmelCase , _UpperCAmelCase = 16 ): SCREAMING_SNAKE_CASE_: Tuple = AutoTokenizer.from_pretrained("bert-base-cased" ) SCREAMING_SNAKE_CASE_: List[Any] = load_dataset("glue" , "mrpc" ) def tokenize_function(_UpperCAmelCase ): # max_length=None => use the model max length (it's actually the default) SCREAMING_SNAKE_CASE_: Any = tokenizer(examples["sentence1"] , examples["sentence2"] , truncation=_UpperCAmelCase , max_length=_UpperCAmelCase ) 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(): SCREAMING_SNAKE_CASE_: Tuple = datasets.map( _UpperCAmelCase , batched=_UpperCAmelCase , 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 SCREAMING_SNAKE_CASE_: Union[str, Any] = tokenized_datasets.rename_column("label" , "labels" ) def collate_fn(_UpperCAmelCase ): # On TPU it's best to pad everything to the same length or training will be very slow. SCREAMING_SNAKE_CASE_: List[str] = 1_28 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": SCREAMING_SNAKE_CASE_: List[Any] = 16 elif accelerator.mixed_precision != "no": SCREAMING_SNAKE_CASE_: Optional[Any] = 8 else: SCREAMING_SNAKE_CASE_: List[str] = None return tokenizer.pad( _UpperCAmelCase , padding="longest" , max_length=_UpperCAmelCase , pad_to_multiple_of=_UpperCAmelCase , return_tensors="pt" , ) # Instantiate dataloaders. SCREAMING_SNAKE_CASE_: Dict = DataLoader( tokenized_datasets["train"] , shuffle=_UpperCAmelCase , collate_fn=_UpperCAmelCase , batch_size=_UpperCAmelCase , drop_last=_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Union[str, Any] = DataLoader( tokenized_datasets["validation"] , shuffle=_UpperCAmelCase , collate_fn=_UpperCAmelCase , batch_size=_UpperCAmelCase , drop_last=(accelerator.mixed_precision == "fp8") , ) return train_dataloader, eval_dataloader def A_ ( _UpperCAmelCase , _UpperCAmelCase ): # Initialize accelerator SCREAMING_SNAKE_CASE_: str = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs SCREAMING_SNAKE_CASE_: int = config["lr"] SCREAMING_SNAKE_CASE_: Any = int(config["num_epochs"] ) SCREAMING_SNAKE_CASE_: Optional[int] = int(config["seed"] ) SCREAMING_SNAKE_CASE_: List[Any] = int(config["batch_size"] ) SCREAMING_SNAKE_CASE_: List[str] = evaluate.load("glue" , "mrpc" ) # If the batch size is too big we use gradient accumulation SCREAMING_SNAKE_CASE_: Optional[int] = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: SCREAMING_SNAKE_CASE_: Tuple = batch_size // MAX_GPU_BATCH_SIZE SCREAMING_SNAKE_CASE_: Dict = MAX_GPU_BATCH_SIZE set_seed(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = get_dataloaders(_UpperCAmelCase , _UpperCAmelCase ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) SCREAMING_SNAKE_CASE_: List[Any] = AutoModelForSequenceClassification.from_pretrained("bert-base-cased" , return_dict=_UpperCAmelCase ) # 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). SCREAMING_SNAKE_CASE_: Tuple = model.to(accelerator.device ) # Instantiate optimizer SCREAMING_SNAKE_CASE_: Optional[int] = AdamW(params=model.parameters() , lr=_UpperCAmelCase ) # Instantiate scheduler SCREAMING_SNAKE_CASE_: Optional[int] = get_linear_schedule_with_warmup( optimizer=_UpperCAmelCase , num_warmup_steps=1_00 , num_training_steps=(len(_UpperCAmelCase ) * num_epochs) // gradient_accumulation_steps , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = accelerator.prepare( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) # Now we train the model for epoch in range(_UpperCAmelCase ): model.train() for step, batch in enumerate(_UpperCAmelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) SCREAMING_SNAKE_CASE_: Tuple = model(**_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Optional[int] = outputs.loss SCREAMING_SNAKE_CASE_: Tuple = loss / gradient_accumulation_steps accelerator.backward(_UpperCAmelCase ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(_UpperCAmelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): SCREAMING_SNAKE_CASE_: Optional[int] = model(**_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: int = outputs.logits.argmax(dim=-1 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = accelerator.gather_for_metrics((predictions, batch["labels"]) ) metric.add_batch( predictions=_UpperCAmelCase , references=_UpperCAmelCase , ) SCREAMING_SNAKE_CASE_: List[Any] = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f"epoch {epoch}:" , _UpperCAmelCase ) def A_ ( ): SCREAMING_SNAKE_CASE_: Any = argparse.ArgumentParser(description="Simple example of training script." ) parser.add_argument( "--mixed_precision" , type=_UpperCAmelCase , default=_UpperCAmelCase , 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." ) SCREAMING_SNAKE_CASE_: Optional[Any] = parser.parse_args() SCREAMING_SNAKE_CASE_: Optional[int] = {"lr": 2e-5, "num_epochs": 3, "seed": 42, "batch_size": 16} training_function(_UpperCAmelCase , _UpperCAmelCase ) if __name__ == "__main__": main()
13
1
# We ignore warnings about stepping the scheduler since we step it ourselves during gradient accumulation import warnings from .state import AcceleratorState, GradientState warnings.filterwarnings("""ignore""", category=UserWarning, module="""torch.optim.lr_scheduler""") class __lowercase : """simple docstring""" def __init__( self : Tuple , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : bool = False): SCREAMING_SNAKE_CASE_: Any = scheduler SCREAMING_SNAKE_CASE_: List[str] = optimizers if isinstance(lowerCAmelCase__ , (list, tuple)) else [optimizers] SCREAMING_SNAKE_CASE_: str = split_batches SCREAMING_SNAKE_CASE_: Optional[Any] = step_with_optimizer SCREAMING_SNAKE_CASE_: Dict = GradientState() def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , *lowerCAmelCase__ : Tuple , **lowerCAmelCase__ : List[Any]): if not self.step_with_optimizer: # No link between scheduler and optimizer -> just step self.scheduler.step(*lowerCAmelCase__ , **lowerCAmelCase__) return # Otherwise, first make sure the optimizer was stepped. if not self.gradient_state.sync_gradients: if self.gradient_state.adjust_scheduler: self.scheduler._step_count += 1 return for opt in self.optimizers: if opt.step_was_skipped: return if self.split_batches: # Split batches -> the training dataloader batch size is not changed so one step per training step self.scheduler.step(*lowerCAmelCase__ , **lowerCAmelCase__) else: # Otherwise the training dataloader batch size was multiplied by `num_processes`, so we need to do # num_processes steps per training step SCREAMING_SNAKE_CASE_: Union[str, Any] = AcceleratorState().num_processes for _ in range(lowerCAmelCase__): # Special case when using OneCycle and `drop_last` was not used if hasattr(self.scheduler , "total_steps"): if self.scheduler._step_count <= self.scheduler.total_steps: self.scheduler.step(*lowerCAmelCase__ , **lowerCAmelCase__) else: self.scheduler.step(*lowerCAmelCase__ , **lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : str): return self.scheduler.get_last_lr() def _SCREAMING_SNAKE_CASE ( self : Dict): return self.scheduler.state_dict() def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : Optional[Any]): self.scheduler.load_state_dict(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[Any]): return self.scheduler.get_lr() def _SCREAMING_SNAKE_CASE ( self : Optional[int] , *lowerCAmelCase__ : List[str] , **lowerCAmelCase__ : Optional[int]): return self.scheduler.print_lr(*lowerCAmelCase__ , **lowerCAmelCase__)
13
from collections.abc import Callable class __lowercase : """simple docstring""" def __init__( self : Tuple , lowerCAmelCase__ : Callable | None = None): # Stores actual heap items. SCREAMING_SNAKE_CASE_: list = [] # Stores indexes of each item for supporting updates and deletion. SCREAMING_SNAKE_CASE_: dict = {} # Stores current size of heap. SCREAMING_SNAKE_CASE_: Optional[Any] = 0 # Stores function used to evaluate the score of an item on which basis ordering # will be done. SCREAMING_SNAKE_CASE_: Any = key or (lambda lowerCAmelCase__: x) def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : int): return int((i - 1) / 2) if i > 0 else None def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Union[str, Any] = int(2 * i + 1) return left if 0 < left < self.size else None def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Union[str, Any] = int(2 * i + 2) return right if 0 < right < self.size else None def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : int , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = ( self.pos_map[self.arr[j][0]], self.pos_map[self.arr[i][0]], ) # Then swap the items in the list. SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = self.arr[j], self.arr[i] def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : int , lowerCAmelCase__ : int): return self.arr[i][1] < self.arr[j][1] def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Any = self._left(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = self._right(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = i if left is not None and not self._cmp(lowerCAmelCase__ , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Optional[int] = left if right is not None and not self._cmp(lowerCAmelCase__ , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Tuple = right return valid_parent def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: List[Any] = self._parent(lowerCAmelCase__) while parent is not None and not self._cmp(lowerCAmelCase__ , lowerCAmelCase__): self._swap(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = parent, self._parent(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Optional[int] = self._get_valid_parent(lowerCAmelCase__) while valid_parent != index: self._swap(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = valid_parent, self._get_valid_parent(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : int , lowerCAmelCase__ : int): if item not in self.pos_map: return SCREAMING_SNAKE_CASE_: Any = self.pos_map[item] SCREAMING_SNAKE_CASE_: int = [item, self.key(lowerCAmelCase__)] # Make sure heap is right in both up and down direction. # Ideally only one of them will make any change. self._heapify_up(lowerCAmelCase__) self._heapify_down(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : int): if item not in self.pos_map: return SCREAMING_SNAKE_CASE_: Optional[Any] = self.pos_map[item] del self.pos_map[item] SCREAMING_SNAKE_CASE_: List[str] = self.arr[self.size - 1] SCREAMING_SNAKE_CASE_: Tuple = index self.size -= 1 # Make sure heap is right in both up and down direction. Ideally only one # of them will make any change- so no performance loss in calling both. if self.size > index: self._heapify_up(lowerCAmelCase__) self._heapify_down(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Optional[int] = len(self.arr) if arr_len == self.size: self.arr.append([item, self.key(lowerCAmelCase__)]) else: SCREAMING_SNAKE_CASE_: str = [item, self.key(lowerCAmelCase__)] SCREAMING_SNAKE_CASE_: List[Any] = self.size self.size += 1 self._heapify_up(self.size - 1) def _SCREAMING_SNAKE_CASE ( self : List[Any]): return self.arr[0] if self.size else None def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Dict = self.get_top() if top_item_tuple: self.delete_item(top_item_tuple[0]) return top_item_tuple def A_ ( ): pass if __name__ == "__main__": import doctest doctest.testmod()
13
1
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase : List[Any] = logging.get_logger(__name__) lowerCAmelCase : Optional[int] = {"""ctrl""": """https://huggingface.co/ctrl/resolve/main/config.json"""} class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : Dict = '''ctrl''' _UpperCAmelCase : Dict = ['''past_key_values'''] _UpperCAmelCase : Optional[int] = { '''max_position_embeddings''': '''n_positions''', '''hidden_size''': '''n_embd''', '''num_attention_heads''': '''n_head''', '''num_hidden_layers''': '''n_layer''', } def __init__( self : str , lowerCAmelCase__ : Tuple=24_6534 , lowerCAmelCase__ : Tuple=256 , lowerCAmelCase__ : List[str]=1280 , lowerCAmelCase__ : List[Any]=8192 , lowerCAmelCase__ : Union[str, Any]=48 , lowerCAmelCase__ : Any=16 , lowerCAmelCase__ : Union[str, Any]=0.1 , lowerCAmelCase__ : Union[str, Any]=0.1 , lowerCAmelCase__ : Any=1E-6 , lowerCAmelCase__ : int=0.02 , lowerCAmelCase__ : Any=True , **lowerCAmelCase__ : Union[str, Any] , ): SCREAMING_SNAKE_CASE_: Union[str, Any] = vocab_size SCREAMING_SNAKE_CASE_: Optional[int] = n_positions SCREAMING_SNAKE_CASE_: List[Any] = n_embd SCREAMING_SNAKE_CASE_: Optional[int] = n_layer SCREAMING_SNAKE_CASE_: Tuple = n_head SCREAMING_SNAKE_CASE_: Optional[int] = dff SCREAMING_SNAKE_CASE_: str = resid_pdrop SCREAMING_SNAKE_CASE_: Optional[int] = embd_pdrop SCREAMING_SNAKE_CASE_: str = layer_norm_epsilon SCREAMING_SNAKE_CASE_: str = initializer_range SCREAMING_SNAKE_CASE_: List[Any] = use_cache super().__init__(**lowerCAmelCase__)
13
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) if is_sentencepiece_available(): from ..ta.tokenization_ta import TaTokenizer else: from ...utils.dummy_sentencepiece_objects import TaTokenizer lowerCAmelCase : Any = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast lowerCAmelCase : Dict = TaTokenizerFast lowerCAmelCase : Optional[int] = {"""configuration_mt5""": ["""MT5Config""", """MT5OnnxConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : int = [ """MT5EncoderModel""", """MT5ForConditionalGeneration""", """MT5ForQuestionAnswering""", """MT5Model""", """MT5PreTrainedModel""", """MT5Stack""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Tuple = ["""TFMT5EncoderModel""", """TFMT5ForConditionalGeneration""", """TFMT5Model"""] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : List[str] = ["""FlaxMT5EncoderModel""", """FlaxMT5ForConditionalGeneration""", """FlaxMT5Model"""] if TYPE_CHECKING: from .configuration_mta import MTaConfig, MTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mta import ( MTaEncoderModel, MTaForConditionalGeneration, MTaForQuestionAnswering, MTaModel, MTaPreTrainedModel, MTaStack, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel else: import sys lowerCAmelCase : Optional[Any] = _LazyModule( __name__, globals()["""__file__"""], _import_structure, extra_objects={"""MT5Tokenizer""": MTaTokenizer, """MT5TokenizerFast""": MTaTokenizerFast}, module_spec=__spec__, )
13
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available lowerCAmelCase : Tuple = { """configuration_conditional_detr""": [ """CONDITIONAL_DETR_PRETRAINED_CONFIG_ARCHIVE_MAP""", """ConditionalDetrConfig""", """ConditionalDetrOnnxConfig""", ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Optional[Any] = ["""ConditionalDetrFeatureExtractor"""] lowerCAmelCase : List[Any] = ["""ConditionalDetrImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : int = [ """CONDITIONAL_DETR_PRETRAINED_MODEL_ARCHIVE_LIST""", """ConditionalDetrForObjectDetection""", """ConditionalDetrForSegmentation""", """ConditionalDetrModel""", """ConditionalDetrPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_conditional_detr import ( CONDITIONAL_DETR_PRETRAINED_CONFIG_ARCHIVE_MAP, ConditionalDetrConfig, ConditionalDetrOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_conditional_detr import ConditionalDetrFeatureExtractor from .image_processing_conditional_detr import ConditionalDetrImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_conditional_detr import ( CONDITIONAL_DETR_PRETRAINED_MODEL_ARCHIVE_LIST, ConditionalDetrForObjectDetection, ConditionalDetrForSegmentation, ConditionalDetrModel, ConditionalDetrPreTrainedModel, ) else: import sys lowerCAmelCase : List[Any] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
13
import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEPipeline from diffusers.pipelines.shap_e import ShapERenderer from diffusers.utils import 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 __lowercase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : List[str] = ShapEPipeline _UpperCAmelCase : Tuple = ['''prompt'''] _UpperCAmelCase : Dict = ['''prompt'''] _UpperCAmelCase : Any = [ '''num_images_per_prompt''', '''num_inference_steps''', '''generator''', '''latents''', '''guidance_scale''', '''frame_size''', '''output_type''', '''return_dict''', ] _UpperCAmelCase : Optional[int] = False @property def _SCREAMING_SNAKE_CASE ( self : List[str]): return 32 @property def _SCREAMING_SNAKE_CASE ( self : List[str]): return 32 @property def _SCREAMING_SNAKE_CASE ( self : int): return self.time_input_dim * 4 @property def _SCREAMING_SNAKE_CASE ( self : Optional[int]): return 8 @property def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: str = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip") return tokenizer @property def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Optional[int] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) return CLIPTextModelWithProjection(lowerCAmelCase__) @property def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Tuple = { "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", "encoder_hid_proj_type": None, "added_emb_type": None, } SCREAMING_SNAKE_CASE_: Any = PriorTransformer(**lowerCAmelCase__) return model @property def _SCREAMING_SNAKE_CASE ( self : Dict): torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Union[str, Any] = { "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, ), } SCREAMING_SNAKE_CASE_: Optional[int] = ShapERenderer(**lowerCAmelCase__) return model def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Dict = self.dummy_prior SCREAMING_SNAKE_CASE_: Optional[Any] = self.dummy_text_encoder SCREAMING_SNAKE_CASE_: Union[str, Any] = self.dummy_tokenizer SCREAMING_SNAKE_CASE_: List[str] = self.dummy_renderer SCREAMING_SNAKE_CASE_: Any = HeunDiscreteScheduler( beta_schedule="exp" , num_train_timesteps=1024 , prediction_type="sample" , use_karras_sigmas=lowerCAmelCase__ , clip_sample=lowerCAmelCase__ , clip_sample_range=1.0 , ) SCREAMING_SNAKE_CASE_: Optional[int] = { "prior": prior, "text_encoder": text_encoder, "tokenizer": tokenizer, "renderer": renderer, "scheduler": scheduler, } return components def _SCREAMING_SNAKE_CASE ( self : Any , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[Any]=0): if str(lowerCAmelCase__).startswith("mps"): SCREAMING_SNAKE_CASE_: Optional[Any] = torch.manual_seed(lowerCAmelCase__) else: SCREAMING_SNAKE_CASE_: Any = torch.Generator(device=lowerCAmelCase__).manual_seed(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = { "prompt": "horse", "generator": generator, "num_inference_steps": 1, "frame_size": 32, "output_type": "np", } return inputs def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: str = "cpu" SCREAMING_SNAKE_CASE_: Tuple = self.get_dummy_components() SCREAMING_SNAKE_CASE_: Dict = self.pipeline_class(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = pipe(**self.get_dummy_inputs(lowerCAmelCase__)) SCREAMING_SNAKE_CASE_: Optional[Any] = output.images[0] SCREAMING_SNAKE_CASE_: Any = image[0, -3:, -3:, -1] assert image.shape == (20, 32, 32, 3) SCREAMING_SNAKE_CASE_: Union[str, Any] = np.array( [ 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, ]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-2 def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches self._test_inference_batch_consistent(batch_sizes=[1, 2]) def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_: Dict = torch_device == "cpu" SCREAMING_SNAKE_CASE_: List[Any] = True self._test_inference_batch_single_identical( batch_size=2 , test_max_difference=lowerCAmelCase__ , relax_max_difference=lowerCAmelCase__ , ) def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Dict = self.get_dummy_components() SCREAMING_SNAKE_CASE_: str = self.pipeline_class(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = 1 SCREAMING_SNAKE_CASE_: Any = 2 SCREAMING_SNAKE_CASE_: Dict = self.get_dummy_inputs(lowerCAmelCase__) for key in inputs.keys(): if key in self.batch_params: SCREAMING_SNAKE_CASE_: List[Any] = batch_size * [inputs[key]] SCREAMING_SNAKE_CASE_: Tuple = pipe(**lowerCAmelCase__ , num_images_per_prompt=lowerCAmelCase__)[0] assert images.shape[0] == batch_size * num_images_per_prompt @slow @require_torch_gpu class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : Any): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: List[str] = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/shap_e/test_shap_e_np_out.npy") SCREAMING_SNAKE_CASE_: List[str] = ShapEPipeline.from_pretrained("openai/shap-e") SCREAMING_SNAKE_CASE_: Optional[int] = pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = torch.Generator(device=lowerCAmelCase__).manual_seed(0) SCREAMING_SNAKE_CASE_: int = pipe( "a shark" , generator=lowerCAmelCase__ , guidance_scale=15.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__)
13
1
import unittest from diffusers import FlaxAutoencoderKL from diffusers.utils import is_flax_available from diffusers.utils.testing_utils import require_flax from .test_modeling_common_flax import FlaxModelTesterMixin if is_flax_available(): import jax @require_flax class __lowercase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : Optional[int] = FlaxAutoencoderKL @property def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Tuple = 4 SCREAMING_SNAKE_CASE_: List[str] = 3 SCREAMING_SNAKE_CASE_: str = (32, 32) SCREAMING_SNAKE_CASE_: Dict = jax.random.PRNGKey(0) SCREAMING_SNAKE_CASE_: List[str] = jax.random.uniform(lowerCAmelCase__ , ((batch_size, num_channels) + sizes)) return {"sample": image, "prng_key": prng_key} def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Dict = { "block_out_channels": [32, 64], "in_channels": 3, "out_channels": 3, "down_block_types": ["DownEncoderBlock2D", "DownEncoderBlock2D"], "up_block_types": ["UpDecoderBlock2D", "UpDecoderBlock2D"], "latent_channels": 4, } SCREAMING_SNAKE_CASE_: Any = self.dummy_input return init_dict, inputs_dict
13
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
13
1
import os import pickle import unittest from transformers import AutoTokenizer from transformers.models.bert.tokenization_bert import BertTokenizer from transformers.models.bert_japanese.tokenization_bert_japanese import ( VOCAB_FILES_NAMES, BertJapaneseTokenizer, CharacterTokenizer, JumanppTokenizer, MecabTokenizer, SudachiTokenizer, WordpieceTokenizer, ) from transformers.testing_utils import custom_tokenizers, require_jumanpp, require_sudachi from ...test_tokenization_common import TokenizerTesterMixin @custom_tokenizers class __lowercase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : Optional[int] = BertJapaneseTokenizer _UpperCAmelCase : Any = False _UpperCAmelCase : Dict = True def _SCREAMING_SNAKE_CASE ( self : Optional[int]): super().setUp() SCREAMING_SNAKE_CASE_: Dict = [ "[UNK]", "[CLS]", "[SEP]", "こんにちは", "こん", "にちは", "ばんは", "##こん", "##にちは", "##ばんは", "世界", "##世界", "、", "##、", "。", "##。", ] SCREAMING_SNAKE_CASE_: Tuple = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"]) with open(self.vocab_file , "w" , encoding="utf-8") as vocab_writer: vocab_writer.write("".join([x + "\n" for x in vocab_tokens])) def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Optional[int]): SCREAMING_SNAKE_CASE_: Optional[int] = "こんにちは、世界。 \nこんばんは、世界。" SCREAMING_SNAKE_CASE_: Dict = "こんにちは 、 世界 。 こんばんは 、 世界 。" return input_text, output_text def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : List[str]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = self.get_input_output_texts(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = tokenizer.decode(lowerCAmelCase__ , clean_up_tokenization_spaces=lowerCAmelCase__) return text, ids def _SCREAMING_SNAKE_CASE ( self : List[Any]): pass # TODO add if relevant def _SCREAMING_SNAKE_CASE ( self : Dict): pass # TODO add if relevant def _SCREAMING_SNAKE_CASE ( self : Optional[int]): pass # TODO add if relevant def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: int = self.tokenizer_class(self.vocab_file) SCREAMING_SNAKE_CASE_: List[Any] = tokenizer.tokenize("こんにちは、世界。\nこんばんは、世界。") self.assertListEqual(lowerCAmelCase__ , ["こんにちは", "、", "世界", "。", "こん", "##ばんは", "、", "世界", "。"]) self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase__) , [3, 12, 10, 14, 4, 9, 12, 10, 14]) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: int = self.tokenizer_class(self.vocab_file , word_tokenizer_type="mecab") self.assertIsNotNone(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = "こんにちは、世界。\nこんばんは、世界。" SCREAMING_SNAKE_CASE_: Optional[int] = tokenizer.tokenize(lowerCAmelCase__) self.assertListEqual(lowerCAmelCase__ , ["こんにちは", "、", "世界", "。", "こん", "##ばんは", "、", "世界", "。"]) self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase__) , [3, 12, 10, 14, 4, 9, 12, 10, 14]) SCREAMING_SNAKE_CASE_: int = os.path.join(self.tmpdirname , "tokenizer.bin") with open(lowerCAmelCase__ , "wb") as handle: pickle.dump(lowerCAmelCase__ , lowerCAmelCase__) with open(lowerCAmelCase__ , "rb") as handle: SCREAMING_SNAKE_CASE_: List[Any] = pickle.load(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = tokenizer_new.tokenize(lowerCAmelCase__) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = MecabTokenizer(mecab_dic="ipadic") self.assertListEqual( tokenizer.tokenize(" \tアップルストアでiPhone8 が \n 発売された 。 ") , ["アップルストア", "で", "iPhone", "8", "が", "発売", "さ", "れ", "た", "。"] , ) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): try: SCREAMING_SNAKE_CASE_: List[str] = MecabTokenizer(mecab_dic="unidic_lite") except ModuleNotFoundError: return self.assertListEqual( tokenizer.tokenize(" \tアップルストアでiPhone8 が \n 発売された 。 ") , ["アップル", "ストア", "で", "iPhone", "8", "が", "発売", "さ", "れ", "た", "。"] , ) def _SCREAMING_SNAKE_CASE ( self : str): try: SCREAMING_SNAKE_CASE_: Tuple = MecabTokenizer(mecab_dic="unidic") except ModuleNotFoundError: return self.assertListEqual( tokenizer.tokenize(" \tアップルストアでiPhone8 が \n 発売された 。 ") , ["アップル", "ストア", "で", "iPhone", "8", "が", "発売", "さ", "れ", "た", "。"] , ) def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Tuple = MecabTokenizer(do_lower_case=lowerCAmelCase__ , mecab_dic="ipadic") self.assertListEqual( tokenizer.tokenize(" \tアップルストアでiPhone8 が \n 発売された 。 ") , ["アップルストア", "で", "iphone", "8", "が", "発売", "さ", "れ", "た", "。"] , ) def _SCREAMING_SNAKE_CASE ( self : int): try: SCREAMING_SNAKE_CASE_: int = MecabTokenizer( do_lower_case=lowerCAmelCase__ , normalize_text=lowerCAmelCase__ , mecab_option="-d /usr/local/lib/mecab/dic/jumandic") except RuntimeError: # if dict doesn't exist in the system, previous code raises this error. return self.assertListEqual( tokenizer.tokenize(" \tアップルストアでiPhone8 が \n 発売された 。 ") , ["アップルストア", "で", "iPhone", "8", "が", "発売", "さ", "れた", "\u3000", "。"] , ) def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: Dict = MecabTokenizer(normalize_text=lowerCAmelCase__ , mecab_dic="ipadic") self.assertListEqual( tokenizer.tokenize(" \tアップルストアでiPhone8 が \n 発売された 。 ") , ["アップルストア", "で", "iPhone", "8", "が", "発売", "さ", "れ", "た", " ", "。"] , ) @require_sudachi def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Any = self.tokenizer_class(self.vocab_file , word_tokenizer_type="sudachi") self.assertIsNotNone(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = "こんにちは、世界。\nこんばんは、世界。" SCREAMING_SNAKE_CASE_: Any = tokenizer.tokenize(lowerCAmelCase__) self.assertListEqual(lowerCAmelCase__ , ["こんにちは", "、", "世界", "。", "こん", "##ばんは", "、", "世界", "。"]) self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase__) , [3, 12, 10, 14, 4, 9, 12, 10, 14]) SCREAMING_SNAKE_CASE_: Tuple = os.path.join(self.tmpdirname , "tokenizer.bin") with open(lowerCAmelCase__ , "wb") as handle: pickle.dump(lowerCAmelCase__ , lowerCAmelCase__) with open(lowerCAmelCase__ , "rb") as handle: SCREAMING_SNAKE_CASE_: str = pickle.load(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = tokenizer_new.tokenize(lowerCAmelCase__) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__) @require_sudachi def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Optional[int] = SudachiTokenizer(sudachi_dict_type="core") self.assertListEqual( tokenizer.tokenize(" \tアップルストアでiPhone8 が \n 発売された 。 ") , [" ", "\t", "アップル", "ストア", "で", "iPhone", "8", " ", "が", " ", " ", "\n ", "発売", "さ", "れ", "た", " ", "。", " ", " "] , ) @require_sudachi def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_: Union[str, Any] = SudachiTokenizer(sudachi_dict_type="core" , sudachi_split_mode="A") self.assertListEqual(tokenizer.tokenize("外国人参政権") , ["外国", "人", "参政", "権"]) @require_sudachi def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Tuple = SudachiTokenizer(sudachi_dict_type="core" , sudachi_split_mode="B") self.assertListEqual(tokenizer.tokenize("外国人参政権") , ["外国人", "参政権"]) @require_sudachi def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: Union[str, Any] = SudachiTokenizer(sudachi_dict_type="core" , sudachi_split_mode="C") self.assertListEqual(tokenizer.tokenize("外国人参政権") , ["外国人参政権"]) @require_sudachi def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Optional[Any] = SudachiTokenizer(do_lower_case=lowerCAmelCase__ , sudachi_dict_type="core") self.assertListEqual( tokenizer.tokenize(" \tアップルストアでiPhone8 が \n 発売された 。 ") , [" ", "\t", "アップル", "ストア", "で", "iphone", "8", " ", "が", " ", " ", "\n ", "発売", "さ", "れ", "た", " ", "。", " ", " "] , ) @require_sudachi def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_: str = SudachiTokenizer(normalize_text=lowerCAmelCase__ , sudachi_dict_type="core") self.assertListEqual( tokenizer.tokenize(" \tアップルストアでiPhone8 が \n 発売された 。 ") , [" ", "\t", "アップル", "ストア", "で", "iPhone", "8", " ", "が", " ", " ", "\n ", "発売", "さ", "れ", "た", "\u3000", "。", " ", " "] , ) @require_sudachi def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Optional[Any] = SudachiTokenizer(trim_whitespace=lowerCAmelCase__ , sudachi_dict_type="core") self.assertListEqual( tokenizer.tokenize(" \tアップルストアでiPhone8 が \n 発売された 。 ") , ["アップル", "ストア", "で", "iPhone", "8", "が", "発売", "さ", "れ", "た", "。"] , ) @require_jumanpp def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Tuple = self.tokenizer_class(self.vocab_file , word_tokenizer_type="jumanpp") self.assertIsNotNone(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = "こんにちは、世界。\nこんばんは、世界。" SCREAMING_SNAKE_CASE_: Dict = tokenizer.tokenize(lowerCAmelCase__) self.assertListEqual(lowerCAmelCase__ , ["こんにちは", "、", "世界", "。", "こん", "##ばんは", "、", "世界", "。"]) self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase__) , [3, 12, 10, 14, 4, 9, 12, 10, 14]) SCREAMING_SNAKE_CASE_: Optional[int] = os.path.join(self.tmpdirname , "tokenizer.bin") with open(lowerCAmelCase__ , "wb") as handle: pickle.dump(lowerCAmelCase__ , lowerCAmelCase__) with open(lowerCAmelCase__ , "rb") as handle: SCREAMING_SNAKE_CASE_: Dict = pickle.load(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = tokenizer_new.tokenize(lowerCAmelCase__) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__) @require_jumanpp def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_: Union[str, Any] = JumanppTokenizer() self.assertListEqual( tokenizer.tokenize(" \tアップルストアでiPhone8 が \n 発売された 。 ") , ["アップル", "ストア", "で", "iPhone", "8", "\u3000", "が", "\u3000", "\u3000", "\u3000", "発売", "さ", "れた", "\u3000", "。"] , ) @require_jumanpp def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: List[str] = JumanppTokenizer(do_lower_case=lowerCAmelCase__) self.assertListEqual( tokenizer.tokenize(" \tアップルストアでiPhone8 が \n 発売された 。 ") , ["アップル", "ストア", "で", "iphone", "8", "\u3000", "が", "\u3000", "\u3000", "\u3000", "発売", "さ", "れた", "\u3000", "。"] , ) @require_jumanpp def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Dict = JumanppTokenizer(normalize_text=lowerCAmelCase__) self.assertListEqual( tokenizer.tokenize(" \tアップルストアでiPhone8 が \n 発売された 。 ") , ["ア", "ッ", "フ", "゚", "ル", "ストア", "で", "iPhone", "8", "\u3000", "が", "\u3000", "\u3000", "\u3000", "発売", "さ", "れた", "\u3000", "。"] , ) @require_jumanpp def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: str = JumanppTokenizer(trim_whitespace=lowerCAmelCase__) self.assertListEqual( tokenizer.tokenize(" \tアップルストアでiPhone8 が \n 発売された 。 ") , ["アップル", "ストア", "で", "iPhone", "8", "が", "発売", "さ", "れた", "。"] , ) @require_jumanpp def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = JumanppTokenizer() self.assertListEqual( tokenizer.tokenize("ありがとうございますm(_ _)m見つけるのが大変です。") , ["ありがとう", "ございます", "m(_ _)m", "見つける", "の", "が", "大変です", "。"] , ) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Optional[Any] = ["[UNK]", "[CLS]", "[SEP]", "こんにちは", "こん", "にちは", "ばんは", "##こん", "##にちは", "##ばんは"] SCREAMING_SNAKE_CASE_: Optional[int] = {} for i, token in enumerate(lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Optional[Any] = i SCREAMING_SNAKE_CASE_: Union[str, Any] = WordpieceTokenizer(vocab=lowerCAmelCase__ , unk_token="[UNK]") self.assertListEqual(tokenizer.tokenize("") , []) self.assertListEqual(tokenizer.tokenize("こんにちは") , ["こんにちは"]) self.assertListEqual(tokenizer.tokenize("こんばんは") , ["こん", "##ばんは"]) self.assertListEqual(tokenizer.tokenize("こんばんは こんばんにちは こんにちは") , ["こん", "##ばんは", "[UNK]", "こんにちは"]) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_: str = BertJapaneseTokenizer.from_pretrained("nlp-waseda/roberta-base-japanese-with-auto-jumanpp") SCREAMING_SNAKE_CASE_: List[Any] = tokenizer.subword_tokenizer SCREAMING_SNAKE_CASE_: List[Any] = subword_tokenizer.tokenize("国境 の 長い トンネル を 抜ける と 雪国 であった 。") self.assertListEqual(lowerCAmelCase__ , ["▁国境", "▁の", "▁長い", "▁トンネル", "▁を", "▁抜ける", "▁と", "▁雪", "国", "▁であった", "▁。"]) SCREAMING_SNAKE_CASE_: str = subword_tokenizer.tokenize("こんばんは こんばん にち は こんにちは") self.assertListEqual(lowerCAmelCase__ , ["▁こん", "ばん", "は", "▁こん", "ばん", "▁に", "ち", "▁は", "▁こんにちは"]) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Dict = self.tokenizer_class.from_pretrained("cl-tohoku/bert-base-japanese") SCREAMING_SNAKE_CASE_: Tuple = tokenizer.encode("ありがとう。" , add_special_tokens=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = tokenizer.encode("どういたしまして。" , add_special_tokens=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = tokenizer.build_inputs_with_special_tokens(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = tokenizer.build_inputs_with_special_tokens(lowerCAmelCase__ , lowerCAmelCase__) # 2 is for "[CLS]", 3 is for "[SEP]" assert encoded_sentence == [2] + text + [3] assert encoded_pair == [2] + text + [3] + text_a + [3] @custom_tokenizers class __lowercase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : Optional[Any] = BertJapaneseTokenizer _UpperCAmelCase : List[str] = False def _SCREAMING_SNAKE_CASE ( self : List[Any]): super().setUp() SCREAMING_SNAKE_CASE_: Any = ["[UNK]", "[CLS]", "[SEP]", "こ", "ん", "に", "ち", "は", "ば", "世", "界", "、", "。"] SCREAMING_SNAKE_CASE_: int = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"]) with open(self.vocab_file , "w" , encoding="utf-8") as vocab_writer: vocab_writer.write("".join([x + "\n" for x in vocab_tokens])) def _SCREAMING_SNAKE_CASE ( self : List[str] , **lowerCAmelCase__ : Union[str, Any]): return BertJapaneseTokenizer.from_pretrained(self.tmpdirname , subword_tokenizer_type="character" , **lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Union[str, Any]): SCREAMING_SNAKE_CASE_: List[Any] = "こんにちは、世界。 \nこんばんは、世界。" SCREAMING_SNAKE_CASE_: Union[str, Any] = "こ ん に ち は 、 世 界 。 こ ん ば ん は 、 世 界 。" return input_text, output_text def _SCREAMING_SNAKE_CASE ( self : Tuple): pass # TODO add if relevant def _SCREAMING_SNAKE_CASE ( self : int): pass # TODO add if relevant def _SCREAMING_SNAKE_CASE ( self : Any): pass # TODO add if relevant def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: Dict = self.tokenizer_class(self.vocab_file , subword_tokenizer_type="character") SCREAMING_SNAKE_CASE_: Optional[int] = tokenizer.tokenize("こんにちは、世界。 \nこんばんは、世界。") self.assertListEqual( lowerCAmelCase__ , ["こ", "ん", "に", "ち", "は", "、", "世", "界", "。", "こ", "ん", "ば", "ん", "は", "、", "世", "界", "。"]) self.assertListEqual( tokenizer.convert_tokens_to_ids(lowerCAmelCase__) , [3, 4, 5, 6, 7, 11, 9, 10, 12, 3, 4, 8, 4, 7, 11, 9, 10, 12]) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_: Optional[Any] = ["[UNK]", "[CLS]", "[SEP]", "こ", "ん", "に", "ち", "は", "ば", "世", "界", "、", "。"] SCREAMING_SNAKE_CASE_: List[Any] = {} for i, token in enumerate(lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Dict = i SCREAMING_SNAKE_CASE_: Optional[int] = CharacterTokenizer(vocab=lowerCAmelCase__ , unk_token="[UNK]") self.assertListEqual(tokenizer.tokenize("") , []) self.assertListEqual(tokenizer.tokenize("こんにちは") , ["こ", "ん", "に", "ち", "は"]) self.assertListEqual(tokenizer.tokenize("こんにちほ") , ["こ", "ん", "に", "ち", "[UNK]"]) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_: str = self.tokenizer_class.from_pretrained("cl-tohoku/bert-base-japanese-char") SCREAMING_SNAKE_CASE_: Optional[int] = tokenizer.encode("ありがとう。" , add_special_tokens=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = tokenizer.encode("どういたしまして。" , add_special_tokens=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = tokenizer.build_inputs_with_special_tokens(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = tokenizer.build_inputs_with_special_tokens(lowerCAmelCase__ , lowerCAmelCase__) # 2 is for "[CLS]", 3 is for "[SEP]" assert encoded_sentence == [2] + text + [3] assert encoded_pair == [2] + text + [3] + text_a + [3] @custom_tokenizers class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: List[str] = "cl-tohoku/bert-base-japanese" SCREAMING_SNAKE_CASE_: List[str] = AutoTokenizer.from_pretrained(lowerCAmelCase__) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__) class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = "cl-tohoku/bert-base-japanese" with self.assertLogs("transformers" , level="WARNING") as cm: BertTokenizer.from_pretrained(lowerCAmelCase__) self.assertTrue( cm.records[0].message.startswith( "The tokenizer class you load from this checkpoint is not the same type as the class this function" " is called from.")) SCREAMING_SNAKE_CASE_: Union[str, Any] = "bert-base-cased" with self.assertLogs("transformers" , level="WARNING") as cm: BertJapaneseTokenizer.from_pretrained(lowerCAmelCase__) self.assertTrue( cm.records[0].message.startswith( "The tokenizer class you load from this checkpoint is not the same type as the class this function" " is called from."))
13
class __lowercase : """simple docstring""" def __init__( self : List[Any] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[Any]): SCREAMING_SNAKE_CASE_: List[str] = name SCREAMING_SNAKE_CASE_: Union[str, Any] = val def __str__( self : Dict): return F"{self.__class__.__name__}({self.name}, {self.val})" def __lt__( self : List[str] , lowerCAmelCase__ : Any): return self.val < other.val class __lowercase : """simple docstring""" def __init__( self : Tuple , lowerCAmelCase__ : Dict): SCREAMING_SNAKE_CASE_: str = {} SCREAMING_SNAKE_CASE_: int = {} SCREAMING_SNAKE_CASE_: Any = self.build_heap(lowerCAmelCase__) def __getitem__( self : List[Any] , lowerCAmelCase__ : Dict): return self.get_value(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : Dict): return (idx - 1) // 2 def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Optional[Any]): return idx * 2 + 1 def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Tuple): return idx * 2 + 2 def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Optional[int]): return self.heap_dict[key] def _SCREAMING_SNAKE_CASE ( self : List[Any] , lowerCAmelCase__ : Union[str, Any]): SCREAMING_SNAKE_CASE_: Tuple = len(lowerCAmelCase__) - 1 SCREAMING_SNAKE_CASE_: List[str] = self.get_parent_idx(lowerCAmelCase__) for idx, i in enumerate(lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Union[str, Any] = idx SCREAMING_SNAKE_CASE_: str = i.val for i in range(lowerCAmelCase__ , -1 , -1): self.sift_down(lowerCAmelCase__ , lowerCAmelCase__) return array def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[str]): while True: SCREAMING_SNAKE_CASE_: Optional[Any] = self.get_left_child_idx(lowerCAmelCase__) # noqa: E741 SCREAMING_SNAKE_CASE_: Dict = self.get_right_child_idx(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = idx if l < len(lowerCAmelCase__) and array[l] < array[idx]: SCREAMING_SNAKE_CASE_: List[str] = l if r < len(lowerCAmelCase__) and array[r] < array[smallest]: SCREAMING_SNAKE_CASE_: str = r if smallest != idx: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = array[smallest], array[idx] ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ): Optional[Any] = ( self.idx_of_element[array[smallest]], self.idx_of_element[array[idx]], ) SCREAMING_SNAKE_CASE_: Optional[int] = smallest else: break def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : str): SCREAMING_SNAKE_CASE_: Any = self.get_parent_idx(lowerCAmelCase__) while p >= 0 and self.heap[p] > self.heap[idx]: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = self.heap[idx], self.heap[p] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = ( self.idx_of_element[self.heap[idx]], self.idx_of_element[self.heap[p]], ) SCREAMING_SNAKE_CASE_: Union[str, Any] = p SCREAMING_SNAKE_CASE_: Optional[int] = self.get_parent_idx(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[Any]): return self.heap[0] def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self.heap[-1], self.heap[0] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = ( self.idx_of_element[self.heap[-1]], self.idx_of_element[self.heap[0]], ) SCREAMING_SNAKE_CASE_: int = self.heap.pop() del self.idx_of_element[x] self.sift_down(0 , self.heap) return x def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Tuple): self.heap.append(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = len(self.heap) - 1 SCREAMING_SNAKE_CASE_: List[str] = node.val self.sift_up(len(self.heap) - 1) def _SCREAMING_SNAKE_CASE ( self : List[Any]): return len(self.heap) == 0 def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Optional[int]): assert ( self.heap[self.idx_of_element[node]].val > new_value ), "newValue must be less that current value" SCREAMING_SNAKE_CASE_: Any = new_value SCREAMING_SNAKE_CASE_: Tuple = new_value self.sift_up(self.idx_of_element[node]) lowerCAmelCase : int = Node("""R""", -1) lowerCAmelCase : str = Node("""B""", 6) lowerCAmelCase : str = Node("""A""", 3) lowerCAmelCase : List[str] = Node("""X""", 1) lowerCAmelCase : Union[str, Any] = Node("""E""", 4) # Use one of these two ways to generate Min-Heap # Generating Min-Heap from array lowerCAmelCase : Optional[Any] = MinHeap([r, b, a, x, e]) # Generating Min-Heap by Insert method # myMinHeap.insert(a) # myMinHeap.insert(b) # myMinHeap.insert(x) # myMinHeap.insert(r) # myMinHeap.insert(e) # Before print("""Min Heap - before decrease key""") for i in my_min_heap.heap: print(i) print("""Min Heap - After decrease key of node [B -> -17]""") my_min_heap.decrease_key(b, -17) # After for i in my_min_heap.heap: print(i) if __name__ == "__main__": import doctest doctest.testmod()
13
1
from __future__ import annotations def A_ ( _UpperCAmelCase , _UpperCAmelCase = None , _UpperCAmelCase = None ): if start is None: SCREAMING_SNAKE_CASE_: Tuple = 0 if end is None: SCREAMING_SNAKE_CASE_: Tuple = len(_UpperCAmelCase ) - 1 if start >= end: return SCREAMING_SNAKE_CASE_: List[Any] = (start + end) // 2 slowsort(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) slowsort(_UpperCAmelCase , mid + 1 , _UpperCAmelCase ) if sequence[end] < sequence[mid]: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = sequence[mid], sequence[end] slowsort(_UpperCAmelCase , _UpperCAmelCase , end - 1 ) if __name__ == "__main__": from doctest import testmod testmod()
13
import random import unittest import numpy as np import transformers from transformers import is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax if is_flax_available(): import os import jax.numpy as jnp from jax import jit from transformers import AutoTokenizer, FlaxAutoModelForCausalLM from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model lowerCAmelCase : Any = """0.12""" # assumed parallelism: 8 if is_torch_available(): import torch def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=None ): if rng is None: SCREAMING_SNAKE_CASE_: List[Any] = random.Random() SCREAMING_SNAKE_CASE_: Optional[Any] = 1 for dim in shape: total_dims *= dim SCREAMING_SNAKE_CASE_: Optional[Any] = [] for _ in range(_UpperCAmelCase ): values.append(rng.randint(0 , vocab_size - 1 ) ) SCREAMING_SNAKE_CASE_: List[Any] = np.array(_UpperCAmelCase , dtype=jnp.intaa ).reshape(_UpperCAmelCase ) return output def A_ ( _UpperCAmelCase , _UpperCAmelCase=None ): SCREAMING_SNAKE_CASE_: Optional[int] = ids_tensor(_UpperCAmelCase , vocab_size=2 , rng=_UpperCAmelCase ) # make sure that at least one token is attended to for each batch SCREAMING_SNAKE_CASE_: Optional[Any] = 1 return attn_mask @require_flax class __lowercase : """simple docstring""" _UpperCAmelCase : Any = None _UpperCAmelCase : List[Any] = () def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self.model_tester.prepare_config_and_inputs_for_common() # cut to half length & take max batch_size 3 SCREAMING_SNAKE_CASE_: str = 2 SCREAMING_SNAKE_CASE_: Optional[int] = inputs["input_ids"].shape[-1] // 2 SCREAMING_SNAKE_CASE_: List[str] = inputs["input_ids"][:max_batch_size, :sequence_length] SCREAMING_SNAKE_CASE_: Any = jnp.ones_like(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = attention_mask[:max_batch_size, :sequence_length] # generate max 5 tokens SCREAMING_SNAKE_CASE_: Optional[Any] = input_ids.shape[-1] + 5 if config.eos_token_id is not None and config.pad_token_id is None: # hack to allow generate for models such as GPT2 as is done in `generate()` SCREAMING_SNAKE_CASE_: Optional[Any] = config.eos_token_id return config, input_ids, attention_mask, max_length @is_pt_flax_cross_test def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Union[str, Any] = False SCREAMING_SNAKE_CASE_: Dict = max_length SCREAMING_SNAKE_CASE_: List[Any] = 0 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: int = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = model_class.__name__[4:] # Skip the "Flax" at the beginning SCREAMING_SNAKE_CASE_: List[Any] = getattr(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = pt_model_class(lowerCAmelCase__).eval() SCREAMING_SNAKE_CASE_: str = load_flax_weights_in_pytorch_model(lowerCAmelCase__ , flax_model.params) SCREAMING_SNAKE_CASE_: List[Any] = flax_model.generate(lowerCAmelCase__).sequences SCREAMING_SNAKE_CASE_: str = pt_model.generate(torch.tensor(lowerCAmelCase__ , dtype=torch.long)) if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]: SCREAMING_SNAKE_CASE_: List[Any] = flax_generation_outputs[:, : pt_generation_outputs.shape[-1]] self.assertListEqual(pt_generation_outputs.numpy().tolist() , flax_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Optional[int] = False SCREAMING_SNAKE_CASE_: Optional[int] = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Union[str, Any] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = jit(model.generate) SCREAMING_SNAKE_CASE_: Union[str, Any] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Optional[Any] = True SCREAMING_SNAKE_CASE_: Dict = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Tuple = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = jit(model.generate) SCREAMING_SNAKE_CASE_: Dict = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: int = False SCREAMING_SNAKE_CASE_: Optional[int] = max_length SCREAMING_SNAKE_CASE_: Optional[int] = 2 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: List[str] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = jit(model.generate) SCREAMING_SNAKE_CASE_: Optional[int] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: str = False SCREAMING_SNAKE_CASE_: int = max_length SCREAMING_SNAKE_CASE_: str = 2 SCREAMING_SNAKE_CASE_: Optional[Any] = 2 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: str = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[0] , input_ids.shape[0] * config.num_return_sequences) def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Tuple = True SCREAMING_SNAKE_CASE_: List[str] = max_length SCREAMING_SNAKE_CASE_: Any = 0.8 SCREAMING_SNAKE_CASE_: Any = 10 SCREAMING_SNAKE_CASE_: List[str] = 0.3 SCREAMING_SNAKE_CASE_: Tuple = 1 SCREAMING_SNAKE_CASE_: Union[str, Any] = 8 SCREAMING_SNAKE_CASE_: int = 9 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: List[str] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = jit(model.generate) SCREAMING_SNAKE_CASE_: List[Any] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Any = max_length SCREAMING_SNAKE_CASE_: int = 1 SCREAMING_SNAKE_CASE_: Union[str, Any] = 8 SCREAMING_SNAKE_CASE_: List[Any] = 9 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: int = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = jit(model.generate) SCREAMING_SNAKE_CASE_: List[str] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Any = max_length SCREAMING_SNAKE_CASE_: List[str] = 2 SCREAMING_SNAKE_CASE_: str = 1 SCREAMING_SNAKE_CASE_: Tuple = 8 SCREAMING_SNAKE_CASE_: List[Any] = 9 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Optional[int] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = jit(model.generate) SCREAMING_SNAKE_CASE_: List[str] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self._get_input_ids_and_config() # pad attention mask on the left SCREAMING_SNAKE_CASE_: Dict = attention_mask.at[(0, 0)].set(0) SCREAMING_SNAKE_CASE_: Dict = False SCREAMING_SNAKE_CASE_: Optional[int] = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Any = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = model.generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = jit(model.generate) SCREAMING_SNAKE_CASE_: List[Any] = jit_generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = self._get_input_ids_and_config() # pad attention mask on the left SCREAMING_SNAKE_CASE_: List[Any] = attention_mask.at[(0, 0)].set(0) SCREAMING_SNAKE_CASE_: Optional[int] = True SCREAMING_SNAKE_CASE_: Union[str, Any] = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: str = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = model.generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = jit(model.generate) SCREAMING_SNAKE_CASE_: Optional[Any] = jit_generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self._get_input_ids_and_config() # pad attention mask on the left SCREAMING_SNAKE_CASE_: Dict = attention_mask.at[(0, 0)].set(0) SCREAMING_SNAKE_CASE_: Optional[Any] = 2 SCREAMING_SNAKE_CASE_: Any = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Tuple = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = model.generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = jit(model.generate) SCREAMING_SNAKE_CASE_: Union[str, Any] = jit_generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) @require_flax class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: Tuple = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-bert") SCREAMING_SNAKE_CASE_: List[Any] = FlaxAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-bert-flax-only") SCREAMING_SNAKE_CASE_: Optional[int] = "Hello world" SCREAMING_SNAKE_CASE_: List[Any] = tokenizer(lowerCAmelCase__ , return_tensors="np").input_ids # typos are quickly detected (the correct argument is `do_sample`) with self.assertRaisesRegex(lowerCAmelCase__ , "do_samples"): model.generate(lowerCAmelCase__ , do_samples=lowerCAmelCase__) # arbitrary arguments that will not be used anywhere are also not accepted with self.assertRaisesRegex(lowerCAmelCase__ , "foo"): SCREAMING_SNAKE_CASE_: str = {"foo": "bar"} model.generate(lowerCAmelCase__ , **lowerCAmelCase__)
13
1
import warnings from ...utils import logging from .image_processing_clip import CLIPImageProcessor lowerCAmelCase : int = logging.get_logger(__name__) class __lowercase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Dict , *lowerCAmelCase__ : Union[str, Any] , **lowerCAmelCase__ : Any): warnings.warn( "The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use CLIPImageProcessor instead." , lowerCAmelCase__ , ) super().__init__(*lowerCAmelCase__ , **lowerCAmelCase__)
13
import argparse import re import torch from CLAP import create_model from transformers import AutoFeatureExtractor, ClapConfig, ClapModel lowerCAmelCase : Union[str, Any] = { """text_branch""": """text_model""", """audio_branch""": """audio_model.audio_encoder""", """attn""": """attention.self""", """self.proj""": """output.dense""", """attention.self_mask""": """attn_mask""", """mlp.fc1""": """intermediate.dense""", """mlp.fc2""": """output.dense""", """norm1""": """layernorm_before""", """norm2""": """layernorm_after""", """bn0""": """batch_norm""", } lowerCAmelCase : int = AutoFeatureExtractor.from_pretrained("""laion/clap-htsat-unfused""", truncation="""rand_trunc""") def A_ ( _UpperCAmelCase , _UpperCAmelCase=False ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = create_model( "HTSAT-tiny" , "roberta" , _UpperCAmelCase , precision="fp32" , device="cuda:0" if torch.cuda.is_available() else "cpu" , enable_fusion=_UpperCAmelCase , fusion_type="aff_2d" if enable_fusion else None , ) return model, model_cfg def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = {} SCREAMING_SNAKE_CASE_: Tuple = R".*sequential.(\d+).*" SCREAMING_SNAKE_CASE_: Dict = R".*_projection.(\d+).*" for key, value in state_dict.items(): # check if any key needs to be modified for key_to_modify, new_key in KEYS_TO_MODIFY_MAPPING.items(): if key_to_modify in key: SCREAMING_SNAKE_CASE_: Any = key.replace(_UpperCAmelCase , _UpperCAmelCase ) if re.match(_UpperCAmelCase , _UpperCAmelCase ): # replace sequential layers with list SCREAMING_SNAKE_CASE_: Optional[int] = re.match(_UpperCAmelCase , _UpperCAmelCase ).group(1 ) SCREAMING_SNAKE_CASE_: Dict = key.replace(f"sequential.{sequential_layer}." , f"layers.{int(_UpperCAmelCase )//3}.linear." ) elif re.match(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = int(re.match(_UpperCAmelCase , _UpperCAmelCase ).group(1 ) ) # Because in CLAP they use `nn.Sequential`... SCREAMING_SNAKE_CASE_: Optional[int] = 1 if projecton_layer == 0 else 2 SCREAMING_SNAKE_CASE_: Dict = key.replace(f"_projection.{projecton_layer}." , f"_projection.linear{transformers_projection_layer}." ) if "audio" and "qkv" in key: # split qkv into query key and value SCREAMING_SNAKE_CASE_: Tuple = value SCREAMING_SNAKE_CASE_: List[str] = mixed_qkv.size(0 ) // 3 SCREAMING_SNAKE_CASE_: Any = mixed_qkv[:qkv_dim] SCREAMING_SNAKE_CASE_: Optional[int] = mixed_qkv[qkv_dim : qkv_dim * 2] SCREAMING_SNAKE_CASE_: Optional[Any] = mixed_qkv[qkv_dim * 2 :] SCREAMING_SNAKE_CASE_: str = query_layer SCREAMING_SNAKE_CASE_: int = key_layer SCREAMING_SNAKE_CASE_: List[Any] = value_layer else: SCREAMING_SNAKE_CASE_: int = value return model_state_dict def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=False ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = init_clap(_UpperCAmelCase , enable_fusion=_UpperCAmelCase ) clap_model.eval() SCREAMING_SNAKE_CASE_: Union[str, Any] = clap_model.state_dict() SCREAMING_SNAKE_CASE_: Optional[int] = rename_state_dict(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[str] = ClapConfig() SCREAMING_SNAKE_CASE_: Tuple = enable_fusion SCREAMING_SNAKE_CASE_: Tuple = ClapModel(_UpperCAmelCase ) # ignore the spectrogram embedding layer model.load_state_dict(_UpperCAmelCase , strict=_UpperCAmelCase ) model.save_pretrained(_UpperCAmelCase ) transformers_config.save_pretrained(_UpperCAmelCase ) if __name__ == "__main__": lowerCAmelCase : Tuple = 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("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""") parser.add_argument("""--enable_fusion""", action="""store_true""", help="""Whether to enable fusion or not""") lowerCAmelCase : int = parser.parse_args() convert_clap_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.enable_fusion)
13
1
import copy import inspect import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers import TimesformerConfig from transformers.models.auto import get_values 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 ( MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, TimesformerForVideoClassification, TimesformerModel, ) from transformers.models.timesformer.modeling_timesformer import TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from transformers import VideoMAEImageProcessor class __lowercase : """simple docstring""" def __init__( self : Union[str, Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : Optional[int]=13 , lowerCAmelCase__ : Tuple=10 , lowerCAmelCase__ : int=3 , lowerCAmelCase__ : List[str]=2 , lowerCAmelCase__ : Tuple=2 , lowerCAmelCase__ : List[str]=True , lowerCAmelCase__ : Union[str, Any]=True , lowerCAmelCase__ : int=32 , lowerCAmelCase__ : Dict=5 , lowerCAmelCase__ : Dict=4 , lowerCAmelCase__ : int=37 , lowerCAmelCase__ : List[str]="gelu" , lowerCAmelCase__ : List[str]=0.1 , lowerCAmelCase__ : Any=0.1 , lowerCAmelCase__ : List[str]=10 , lowerCAmelCase__ : int=0.02 , lowerCAmelCase__ : int="divided_space_time" , lowerCAmelCase__ : Tuple=None , ): SCREAMING_SNAKE_CASE_: Optional[Any] = parent SCREAMING_SNAKE_CASE_: Any = batch_size SCREAMING_SNAKE_CASE_: Any = image_size SCREAMING_SNAKE_CASE_: Optional[Any] = num_channels SCREAMING_SNAKE_CASE_: Optional[int] = patch_size SCREAMING_SNAKE_CASE_: Any = num_frames SCREAMING_SNAKE_CASE_: Optional[Any] = is_training SCREAMING_SNAKE_CASE_: int = use_labels SCREAMING_SNAKE_CASE_: Dict = hidden_size SCREAMING_SNAKE_CASE_: int = num_hidden_layers SCREAMING_SNAKE_CASE_: List[str] = num_attention_heads SCREAMING_SNAKE_CASE_: int = intermediate_size SCREAMING_SNAKE_CASE_: Dict = hidden_act SCREAMING_SNAKE_CASE_: Union[str, Any] = hidden_dropout_prob SCREAMING_SNAKE_CASE_: int = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_: Union[str, Any] = attention_type SCREAMING_SNAKE_CASE_: Dict = initializer_range SCREAMING_SNAKE_CASE_: int = scope SCREAMING_SNAKE_CASE_: List[Any] = num_labels # in TimeSformer, the number of spatial tokens equals num_frames * num_patches per frame + 1 CLS token SCREAMING_SNAKE_CASE_: Optional[int] = (image_size // patch_size) ** 2 SCREAMING_SNAKE_CASE_: List[str] = (num_frames) * self.num_patches_per_frame + 1 def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_: List[str] = floats_tensor( [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]) SCREAMING_SNAKE_CASE_: int = None if self.use_labels: SCREAMING_SNAKE_CASE_: List[Any] = ids_tensor([self.batch_size] , self.num_labels) SCREAMING_SNAKE_CASE_: Union[str, Any] = self.get_config() return config, pixel_values, labels def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: List[Any] = TimesformerConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , num_frames=self.num_frames , 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 , attention_type=self.attention_type , ) SCREAMING_SNAKE_CASE_: str = self.num_labels return config def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : str , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[int]): SCREAMING_SNAKE_CASE_: List[Any] = TimesformerModel(config=lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: List[str] = model(lowerCAmelCase__) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def _SCREAMING_SNAKE_CASE ( self : int , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : str): SCREAMING_SNAKE_CASE_: int = TimesformerForVideoClassification(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: str = model(lowerCAmelCase__) # verify the logits shape SCREAMING_SNAKE_CASE_: Optional[int] = torch.Size((self.batch_size, self.num_labels)) self.parent.assertEqual(result.logits.shape , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Dict = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = config_and_inputs SCREAMING_SNAKE_CASE_: Tuple = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class __lowercase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : Optional[Any] = (TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else () _UpperCAmelCase : List[str] = ( {'''feature-extraction''': TimesformerModel, '''video-classification''': TimesformerForVideoClassification} if is_torch_available() else {} ) _UpperCAmelCase : Dict = False _UpperCAmelCase : int = False _UpperCAmelCase : Dict = False _UpperCAmelCase : List[Any] = False def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Dict = TimesformerModelTester(self) SCREAMING_SNAKE_CASE_: Dict = ConfigTester( self , config_class=lowerCAmelCase__ , has_text_modality=lowerCAmelCase__ , hidden_size=37) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : str , lowerCAmelCase__ : Any=False): SCREAMING_SNAKE_CASE_: str = copy.deepcopy(lowerCAmelCase__) if return_labels: if model_class in get_values(lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Tuple = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=lowerCAmelCase__) return inputs_dict def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): self.config_tester.run_common_tests() @unittest.skip(reason="TimeSformer does not use inputs_embeds") def _SCREAMING_SNAKE_CASE ( self : Any): pass def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_: str = model_class(lowerCAmelCase__) self.assertIsInstance(model.get_input_embeddings() , (nn.Module)) SCREAMING_SNAKE_CASE_: Union[str, Any] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCAmelCase__ , nn.Linear)) def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_: Dict = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = inspect.signature(model.forward) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE_: Optional[Any] = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE_: str = ["pixel_values"] self.assertListEqual(arg_names[:1] , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_video_classification(*lowerCAmelCase__) @slow def _SCREAMING_SNAKE_CASE ( self : Any): for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_: int = TimesformerModel.from_pretrained(lowerCAmelCase__) self.assertIsNotNone(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): if not self.has_attentions: pass else: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_: List[str] = True for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_: Dict = self.model_tester.seq_length SCREAMING_SNAKE_CASE_: Union[str, Any] = self.model_tester.num_frames SCREAMING_SNAKE_CASE_: Union[str, Any] = True SCREAMING_SNAKE_CASE_: List[Any] = False SCREAMING_SNAKE_CASE_: Optional[int] = True SCREAMING_SNAKE_CASE_: Optional[Any] = model_class(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE_: Dict = model(**self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__)) SCREAMING_SNAKE_CASE_: Optional[int] = outputs.attentions self.assertEqual(len(lowerCAmelCase__) , self.model_tester.num_hidden_layers) # check that output_attentions also work using config del inputs_dict["output_attentions"] SCREAMING_SNAKE_CASE_: Any = True SCREAMING_SNAKE_CASE_: Tuple = model_class(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE_: Any = model(**self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__)) SCREAMING_SNAKE_CASE_: Dict = outputs.attentions self.assertEqual(len(lowerCAmelCase__) , self.model_tester.num_hidden_layers) # attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1) self.assertListEqual( list(attentions[0].shape[-3:]) , [self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1] , ) SCREAMING_SNAKE_CASE_: int = len(lowerCAmelCase__) # Check attention is always last and order is fine SCREAMING_SNAKE_CASE_: List[str] = True SCREAMING_SNAKE_CASE_: Tuple = True SCREAMING_SNAKE_CASE_: Any = model_class(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE_: int = model(**self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__)) self.assertEqual(out_len + 1 , len(lowerCAmelCase__)) SCREAMING_SNAKE_CASE_: Any = outputs.attentions self.assertEqual(len(lowerCAmelCase__) , self.model_tester.num_hidden_layers) # attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1) self.assertListEqual( list(self_attentions[0].shape[-3:]) , [self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1] , ) def _SCREAMING_SNAKE_CASE ( self : List[str]): def check_hidden_states_output(lowerCAmelCase__ : Any , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Any): SCREAMING_SNAKE_CASE_: List[Any] = model_class(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE_: Optional[Any] = model(**self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__)) SCREAMING_SNAKE_CASE_: List[Any] = outputs.hidden_states SCREAMING_SNAKE_CASE_: Union[str, Any] = self.model_tester.num_hidden_layers + 1 self.assertEqual(len(lowerCAmelCase__) , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = self.model_tester.seq_length self.assertListEqual( list(hidden_states[0].shape[-2:]) , [seq_length, self.model_tester.hidden_size] , ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_: Optional[int] = True check_hidden_states_output(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE_: Optional[Any] = True check_hidden_states_output(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) def A_ ( ): SCREAMING_SNAKE_CASE_: Any = hf_hub_download( repo_id="hf-internal-testing/spaghetti-video" , filename="eating_spaghetti.npy" , repo_type="dataset" ) SCREAMING_SNAKE_CASE_: str = np.load(_UpperCAmelCase ) return list(_UpperCAmelCase ) @require_torch @require_vision class __lowercase ( unittest.TestCase ): """simple docstring""" @cached_property def _SCREAMING_SNAKE_CASE ( self : List[Any]): # logits were tested with a different mean and std, so we use the same here return ( VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5] , image_std=[0.5, 0.5, 0.5]) if is_vision_available() else None ) @slow def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: Dict = TimesformerForVideoClassification.from_pretrained("facebook/timesformer-base-finetuned-k400").to( lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = self.default_image_processor SCREAMING_SNAKE_CASE_: Tuple = prepare_video() SCREAMING_SNAKE_CASE_: int = image_processor(video[:8] , return_tensors="pt").to(lowerCAmelCase__) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_: Optional[int] = model(**lowerCAmelCase__) # verify the logits SCREAMING_SNAKE_CASE_: str = torch.Size((1, 400)) self.assertEqual(outputs.logits.shape , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = torch.tensor([-0.3016, -0.7713, -0.4205]).to(lowerCAmelCase__) self.assertTrue(torch.allclose(outputs.logits[0, :3] , lowerCAmelCase__ , atol=1E-4))
13
import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_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, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __lowercase : """simple docstring""" def __init__( self : Any , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Any=13 , lowerCAmelCase__ : Tuple=30 , lowerCAmelCase__ : List[str]=2 , lowerCAmelCase__ : int=3 , lowerCAmelCase__ : Optional[int]=True , lowerCAmelCase__ : List[str]=True , lowerCAmelCase__ : str=32 , lowerCAmelCase__ : Any=5 , lowerCAmelCase__ : str=4 , lowerCAmelCase__ : int=37 , lowerCAmelCase__ : Optional[Any]="gelu" , lowerCAmelCase__ : Optional[int]=0.1 , lowerCAmelCase__ : Dict=0.1 , lowerCAmelCase__ : Tuple=10 , lowerCAmelCase__ : Optional[Any]=0.02 , lowerCAmelCase__ : List[str]=None , lowerCAmelCase__ : Union[str, Any]=2 , ): SCREAMING_SNAKE_CASE_: str = parent SCREAMING_SNAKE_CASE_: Optional[Any] = batch_size SCREAMING_SNAKE_CASE_: str = image_size SCREAMING_SNAKE_CASE_: Tuple = patch_size SCREAMING_SNAKE_CASE_: int = num_channels SCREAMING_SNAKE_CASE_: List[str] = is_training SCREAMING_SNAKE_CASE_: str = use_labels SCREAMING_SNAKE_CASE_: int = hidden_size SCREAMING_SNAKE_CASE_: List[Any] = num_hidden_layers SCREAMING_SNAKE_CASE_: Union[str, Any] = num_attention_heads SCREAMING_SNAKE_CASE_: Any = intermediate_size SCREAMING_SNAKE_CASE_: str = hidden_act SCREAMING_SNAKE_CASE_: str = hidden_dropout_prob SCREAMING_SNAKE_CASE_: List[str] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_: int = type_sequence_label_size SCREAMING_SNAKE_CASE_: Dict = initializer_range SCREAMING_SNAKE_CASE_: Dict = scope SCREAMING_SNAKE_CASE_: Dict = encoder_stride # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) SCREAMING_SNAKE_CASE_: List[Any] = (image_size // patch_size) ** 2 SCREAMING_SNAKE_CASE_: Dict = num_patches + 1 def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]) SCREAMING_SNAKE_CASE_: str = None if self.use_labels: SCREAMING_SNAKE_CASE_: Union[str, Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size) SCREAMING_SNAKE_CASE_: Optional[Any] = self.get_config() return config, pixel_values, labels def _SCREAMING_SNAKE_CASE ( self : Optional[int]): return ViTConfig( 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 , encoder_stride=self.encoder_stride , ) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : Tuple): SCREAMING_SNAKE_CASE_: Union[str, Any] = ViTModel(config=lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: Optional[int] = model(lowerCAmelCase__) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Dict): SCREAMING_SNAKE_CASE_: Optional[int] = ViTForMaskedImageModeling(config=lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: str = model(lowerCAmelCase__) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size)) # test greyscale images SCREAMING_SNAKE_CASE_: Dict = 1 SCREAMING_SNAKE_CASE_: List[str] = ViTForMaskedImageModeling(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: List[str] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]) SCREAMING_SNAKE_CASE_: str = model(lowerCAmelCase__) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size)) def _SCREAMING_SNAKE_CASE ( self : Any , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Union[str, Any]): SCREAMING_SNAKE_CASE_: Tuple = self.type_sequence_label_size SCREAMING_SNAKE_CASE_: List[str] = ViTForImageClassification(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: Any = model(lowerCAmelCase__ , labels=lowerCAmelCase__) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) # test greyscale images SCREAMING_SNAKE_CASE_: Union[str, Any] = 1 SCREAMING_SNAKE_CASE_: List[str] = ViTForImageClassification(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: Union[str, Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]) SCREAMING_SNAKE_CASE_: Dict = model(lowerCAmelCase__) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ): List[str] = config_and_inputs SCREAMING_SNAKE_CASE_: Optional[Any] = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class __lowercase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : List[Any] = ( ( ViTModel, ViTForImageClassification, ViTForMaskedImageModeling, ) if is_torch_available() else () ) _UpperCAmelCase : Tuple = ( {'''feature-extraction''': ViTModel, '''image-classification''': ViTForImageClassification} if is_torch_available() else {} ) _UpperCAmelCase : List[str] = True _UpperCAmelCase : List[Any] = False _UpperCAmelCase : Optional[Any] = False _UpperCAmelCase : Tuple = False def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: List[str] = ViTModelTester(self) SCREAMING_SNAKE_CASE_: Union[str, Any] = ConfigTester(self , config_class=lowerCAmelCase__ , has_text_modality=lowerCAmelCase__ , hidden_size=37) def _SCREAMING_SNAKE_CASE ( self : Any): self.config_tester.run_common_tests() @unittest.skip(reason="ViT does not use inputs_embeds") def _SCREAMING_SNAKE_CASE ( self : str): pass def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_: Dict = model_class(lowerCAmelCase__) self.assertIsInstance(model.get_input_embeddings() , (nn.Module)) SCREAMING_SNAKE_CASE_: List[Any] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCAmelCase__ , nn.Linear)) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_: List[Any] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = inspect.signature(model.forward) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE_: Optional[Any] = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE_: Optional[int] = ["pixel_values"] self.assertListEqual(arg_names[:1] , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__) @slow def _SCREAMING_SNAKE_CASE ( self : int): for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_: Union[str, Any] = ViTModel.from_pretrained(lowerCAmelCase__) self.assertIsNotNone(lowerCAmelCase__) def A_ ( ): SCREAMING_SNAKE_CASE_: List[Any] = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class __lowercase ( unittest.TestCase ): """simple docstring""" @cached_property def _SCREAMING_SNAKE_CASE ( self : int): return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224") if is_vision_available() else None @slow def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: int = ViTForImageClassification.from_pretrained("google/vit-base-patch16-224").to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = self.default_image_processor SCREAMING_SNAKE_CASE_: str = prepare_img() SCREAMING_SNAKE_CASE_: Optional[Any] = image_processor(images=lowerCAmelCase__ , return_tensors="pt").to(lowerCAmelCase__) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_: Optional[int] = model(**lowerCAmelCase__) # verify the logits SCREAMING_SNAKE_CASE_: Any = torch.Size((1, 1000)) self.assertEqual(outputs.logits.shape , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = torch.tensor([-0.2744, 0.8215, -0.0836]).to(lowerCAmelCase__) self.assertTrue(torch.allclose(outputs.logits[0, :3] , lowerCAmelCase__ , atol=1E-4)) @slow def _SCREAMING_SNAKE_CASE ( self : List[Any]): # ViT models have an `interpolate_pos_encoding` argument in their forward method, # allowing to interpolate the pre-trained position embeddings in order to use # the model on higher resolutions. The DINO model by Facebook AI leverages this # to visualize self-attention on higher resolution images. SCREAMING_SNAKE_CASE_: str = ViTModel.from_pretrained("facebook/dino-vits8").to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = ViTImageProcessor.from_pretrained("facebook/dino-vits8" , size=480) SCREAMING_SNAKE_CASE_: List[Any] = prepare_img() SCREAMING_SNAKE_CASE_: List[Any] = image_processor(images=lowerCAmelCase__ , return_tensors="pt") SCREAMING_SNAKE_CASE_: int = inputs.pixel_values.to(lowerCAmelCase__) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_: Optional[int] = model(lowerCAmelCase__ , interpolate_pos_encoding=lowerCAmelCase__) # verify the logits SCREAMING_SNAKE_CASE_: Tuple = torch.Size((1, 3601, 384)) self.assertEqual(outputs.last_hidden_state.shape , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.tensor( [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]]).to(lowerCAmelCase__) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , lowerCAmelCase__ , atol=1E-4)) @slow @require_accelerate @require_torch_gpu def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Dict = ViTModel.from_pretrained("facebook/dino-vits8" , torch_dtype=torch.floataa , device_map="auto") SCREAMING_SNAKE_CASE_: int = self.default_image_processor SCREAMING_SNAKE_CASE_: Union[str, Any] = prepare_img() SCREAMING_SNAKE_CASE_: Dict = image_processor(images=lowerCAmelCase__ , return_tensors="pt") SCREAMING_SNAKE_CASE_: str = inputs.pixel_values.to(lowerCAmelCase__) # forward pass to make sure inference works in fp16 with torch.no_grad(): SCREAMING_SNAKE_CASE_: str = model(lowerCAmelCase__)
13
1
from __future__ import annotations class __lowercase : """simple docstring""" def __init__( self : Dict , lowerCAmelCase__ : List[Any]=None): SCREAMING_SNAKE_CASE_: str = data SCREAMING_SNAKE_CASE_: Optional[int] = None def __repr__( self : Optional[Any]): SCREAMING_SNAKE_CASE_: Any = [] SCREAMING_SNAKE_CASE_: int = self while temp: string_rep.append(F"{temp.data}") SCREAMING_SNAKE_CASE_: Union[str, Any] = temp.next return "->".join(lowerCAmelCase__) def A_ ( _UpperCAmelCase ): if not elements_list: raise Exception("The Elements List is empty" ) SCREAMING_SNAKE_CASE_: Tuple = Node(elements_list[0] ) for i in range(1 , len(_UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_: Tuple = Node(elements_list[i] ) SCREAMING_SNAKE_CASE_: Any = current.next return head def A_ ( _UpperCAmelCase ): if head_node is not None and isinstance(_UpperCAmelCase , _UpperCAmelCase ): print_reverse(head_node.next ) print(head_node.data ) def A_ ( ): from doctest import testmod testmod() SCREAMING_SNAKE_CASE_: Optional[Any] = make_linked_list([14, 52, 14, 12, 43] ) print("Linked List:" ) print(_UpperCAmelCase ) print("Elements in Reverse:" ) print_reverse(_UpperCAmelCase ) if __name__ == "__main__": main()
13
import json import os from functools import lru_cache from typing import List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging lowerCAmelCase : Any = logging.get_logger(__name__) lowerCAmelCase : Tuple = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt"""} lowerCAmelCase : Optional[int] = { """vocab_file""": { """allenai/longformer-base-4096""": """https://huggingface.co/allenai/longformer-base-4096/resolve/main/vocab.json""", """allenai/longformer-large-4096""": ( """https://huggingface.co/allenai/longformer-large-4096/resolve/main/vocab.json""" ), """allenai/longformer-large-4096-finetuned-triviaqa""": ( """https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/vocab.json""" ), """allenai/longformer-base-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/vocab.json""" ), """allenai/longformer-large-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/vocab.json""" ), }, """merges_file""": { """allenai/longformer-base-4096""": """https://huggingface.co/allenai/longformer-base-4096/resolve/main/merges.txt""", """allenai/longformer-large-4096""": ( """https://huggingface.co/allenai/longformer-large-4096/resolve/main/merges.txt""" ), """allenai/longformer-large-4096-finetuned-triviaqa""": ( """https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/merges.txt""" ), """allenai/longformer-base-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/merges.txt""" ), """allenai/longformer-large-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/merges.txt""" ), }, } lowerCAmelCase : Optional[Any] = { """allenai/longformer-base-4096""": 4096, """allenai/longformer-large-4096""": 4096, """allenai/longformer-large-4096-finetuned-triviaqa""": 4096, """allenai/longformer-base-4096-extra.pos.embd.only""": 4096, """allenai/longformer-large-4096-extra.pos.embd.only""": 4096, } @lru_cache() # Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode def A_ ( ): SCREAMING_SNAKE_CASE_: Any = ( list(range(ord("!" ) , ord("~" ) + 1 ) ) + list(range(ord("¡" ) , ord("¬" ) + 1 ) ) + list(range(ord("®" ) , ord("ÿ" ) + 1 ) ) ) SCREAMING_SNAKE_CASE_: Tuple = bs[:] SCREAMING_SNAKE_CASE_: str = 0 for b in range(2**8 ): if b not in bs: bs.append(_UpperCAmelCase ) cs.append(2**8 + n ) n += 1 SCREAMING_SNAKE_CASE_: Optional[int] = [chr(_UpperCAmelCase ) for n in cs] return dict(zip(_UpperCAmelCase , _UpperCAmelCase ) ) def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: str = set() SCREAMING_SNAKE_CASE_: Union[str, Any] = word[0] for char in word[1:]: pairs.add((prev_char, char) ) SCREAMING_SNAKE_CASE_: Tuple = char return pairs class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : Any = VOCAB_FILES_NAMES _UpperCAmelCase : Dict = PRETRAINED_VOCAB_FILES_MAP _UpperCAmelCase : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _UpperCAmelCase : List[str] = ['''input_ids''', '''attention_mask'''] def __init__( self : str , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[Any]="replace" , lowerCAmelCase__ : Optional[Any]="<s>" , lowerCAmelCase__ : int="</s>" , lowerCAmelCase__ : Optional[Any]="</s>" , lowerCAmelCase__ : int="<s>" , lowerCAmelCase__ : Optional[Any]="<unk>" , lowerCAmelCase__ : List[Any]="<pad>" , lowerCAmelCase__ : Any="<mask>" , lowerCAmelCase__ : Union[str, Any]=False , **lowerCAmelCase__ : Tuple , ): SCREAMING_SNAKE_CASE_: int = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else bos_token SCREAMING_SNAKE_CASE_: str = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else eos_token SCREAMING_SNAKE_CASE_: Optional[int] = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else sep_token SCREAMING_SNAKE_CASE_: Union[str, Any] = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else cls_token SCREAMING_SNAKE_CASE_: int = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else unk_token SCREAMING_SNAKE_CASE_: Any = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else pad_token # Mask token behave like a normal word, i.e. include the space before it SCREAMING_SNAKE_CASE_: Optional[int] = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else mask_token super().__init__( errors=lowerCAmelCase__ , bos_token=lowerCAmelCase__ , eos_token=lowerCAmelCase__ , unk_token=lowerCAmelCase__ , sep_token=lowerCAmelCase__ , cls_token=lowerCAmelCase__ , pad_token=lowerCAmelCase__ , mask_token=lowerCAmelCase__ , add_prefix_space=lowerCAmelCase__ , **lowerCAmelCase__ , ) with open(lowerCAmelCase__ , encoding="utf-8") as vocab_handle: SCREAMING_SNAKE_CASE_: Tuple = json.load(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = {v: k for k, v in self.encoder.items()} SCREAMING_SNAKE_CASE_: Optional[Any] = errors # how to handle errors in decoding SCREAMING_SNAKE_CASE_: List[Any] = bytes_to_unicode() SCREAMING_SNAKE_CASE_: Optional[Any] = {v: k for k, v in self.byte_encoder.items()} with open(lowerCAmelCase__ , encoding="utf-8") as merges_handle: SCREAMING_SNAKE_CASE_: List[Any] = merges_handle.read().split("\n")[1:-1] SCREAMING_SNAKE_CASE_: str = [tuple(merge.split()) for merge in bpe_merges] SCREAMING_SNAKE_CASE_: List[Any] = dict(zip(lowerCAmelCase__ , range(len(lowerCAmelCase__)))) SCREAMING_SNAKE_CASE_: str = {} SCREAMING_SNAKE_CASE_: Optional[Any] = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions SCREAMING_SNAKE_CASE_: List[Any] = re.compile(R"'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+") @property def _SCREAMING_SNAKE_CASE ( self : int): return len(self.encoder) def _SCREAMING_SNAKE_CASE ( self : int): return dict(self.encoder , **self.added_tokens_encoder) def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : List[str]): if token in self.cache: return self.cache[token] SCREAMING_SNAKE_CASE_: Optional[int] = tuple(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = get_pairs(lowerCAmelCase__) if not pairs: return token while True: SCREAMING_SNAKE_CASE_: int = min(lowerCAmelCase__ , key=lambda lowerCAmelCase__: self.bpe_ranks.get(lowerCAmelCase__ , float("inf"))) if bigram not in self.bpe_ranks: break SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = bigram SCREAMING_SNAKE_CASE_: Optional[int] = [] SCREAMING_SNAKE_CASE_: List[Any] = 0 while i < len(lowerCAmelCase__): try: SCREAMING_SNAKE_CASE_: List[Any] = word.index(lowerCAmelCase__ , lowerCAmelCase__) except ValueError: new_word.extend(word[i:]) break else: new_word.extend(word[i:j]) SCREAMING_SNAKE_CASE_: Tuple = j if word[i] == first and i < len(lowerCAmelCase__) - 1 and word[i + 1] == second: new_word.append(first + second) i += 2 else: new_word.append(word[i]) i += 1 SCREAMING_SNAKE_CASE_: str = tuple(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = new_word if len(lowerCAmelCase__) == 1: break else: SCREAMING_SNAKE_CASE_: Dict = get_pairs(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = " ".join(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = word return word def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Tuple): SCREAMING_SNAKE_CASE_: Optional[Any] = [] for token in re.findall(self.pat , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: str = "".join( self.byte_encoder[b] for b in token.encode("utf-8")) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(lowerCAmelCase__).split(" ")) return bpe_tokens def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Tuple): return self.encoder.get(lowerCAmelCase__ , self.encoder.get(self.unk_token)) def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : Union[str, Any]): return self.decoder.get(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Optional[int]): SCREAMING_SNAKE_CASE_: Any = "".join(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = bytearray([self.byte_decoder[c] for c in text]).decode("utf-8" , errors=self.errors) return text def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[str] = None): if not os.path.isdir(lowerCAmelCase__): logger.error(F"Vocabulary path ({save_directory}) should be a directory") return SCREAMING_SNAKE_CASE_: Any = os.path.join( lowerCAmelCase__ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]) SCREAMING_SNAKE_CASE_: Any = os.path.join( lowerCAmelCase__ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"]) with open(lowerCAmelCase__ , "w" , encoding="utf-8") as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=lowerCAmelCase__ , ensure_ascii=lowerCAmelCase__) + "\n") SCREAMING_SNAKE_CASE_: List[Any] = 0 with open(lowerCAmelCase__ , "w" , encoding="utf-8") as writer: writer.write("#version: 0.2\n") for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda lowerCAmelCase__: kv[1]): if index != token_index: logger.warning( F"Saving vocabulary to {merge_file}: BPE merge indices are not consecutive." " Please check that the tokenizer is not corrupted!") SCREAMING_SNAKE_CASE_: List[Any] = token_index writer.write(" ".join(lowerCAmelCase__) + "\n") index += 1 return vocab_file, merge_file def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] SCREAMING_SNAKE_CASE_: Optional[int] = [self.cls_token_id] SCREAMING_SNAKE_CASE_: Dict = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None , lowerCAmelCase__ : bool = False): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=lowerCAmelCase__ , token_ids_a=lowerCAmelCase__ , already_has_special_tokens=lowerCAmelCase__) if token_ids_a is None: return [1] + ([0] * len(lowerCAmelCase__)) + [1] return [1] + ([0] * len(lowerCAmelCase__)) + [1, 1] + ([0] * len(lowerCAmelCase__)) + [1] def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None): SCREAMING_SNAKE_CASE_: Optional[int] = [self.sep_token_id] SCREAMING_SNAKE_CASE_: int = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep) * [0] def _SCREAMING_SNAKE_CASE ( self : int , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : List[str]=False , **lowerCAmelCase__ : str): SCREAMING_SNAKE_CASE_: List[Any] = kwargs.pop("add_prefix_space" , self.add_prefix_space) if (is_split_into_words or add_prefix_space) and (len(lowerCAmelCase__) > 0 and not text[0].isspace()): SCREAMING_SNAKE_CASE_: Optional[Any] = " " + text return (text, kwargs)
13
1
import unittest from queue import Empty from threading import Thread from transformers import AutoTokenizer, TextIteratorStreamer, TextStreamer, is_torch_available from transformers.testing_utils import CaptureStdout, require_torch, torch_device from ..test_modeling_common import ids_tensor if is_torch_available(): import torch from transformers import AutoModelForCausalLM @require_torch class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: Tuple = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2") SCREAMING_SNAKE_CASE_: Optional[Any] = AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2").to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = -1 SCREAMING_SNAKE_CASE_: Optional[Any] = ids_tensor((1, 5) , vocab_size=model.config.vocab_size).to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = model.generate(lowerCAmelCase__ , max_new_tokens=10 , do_sample=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = tokenizer.decode(greedy_ids[0]) with CaptureStdout() as cs: SCREAMING_SNAKE_CASE_: List[Any] = TextStreamer(lowerCAmelCase__) model.generate(lowerCAmelCase__ , max_new_tokens=10 , do_sample=lowerCAmelCase__ , streamer=lowerCAmelCase__) # The greedy text should be printed to stdout, except for the final "\n" in the streamer SCREAMING_SNAKE_CASE_: int = cs.out[:-1] self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: Any = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2") SCREAMING_SNAKE_CASE_: List[str] = AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2").to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = -1 SCREAMING_SNAKE_CASE_: Dict = ids_tensor((1, 5) , vocab_size=model.config.vocab_size).to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = model.generate(lowerCAmelCase__ , max_new_tokens=10 , do_sample=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = tokenizer.decode(greedy_ids[0]) SCREAMING_SNAKE_CASE_: int = TextIteratorStreamer(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = {"input_ids": input_ids, "max_new_tokens": 10, "do_sample": False, "streamer": streamer} SCREAMING_SNAKE_CASE_: Optional[int] = Thread(target=model.generate , kwargs=lowerCAmelCase__) thread.start() SCREAMING_SNAKE_CASE_: int = "" for new_text in streamer: streamer_text += new_text self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: List[str] = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2") SCREAMING_SNAKE_CASE_: Dict = AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2").to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = -1 SCREAMING_SNAKE_CASE_: Dict = ids_tensor((1, 5) , vocab_size=model.config.vocab_size).to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = model.generate(lowerCAmelCase__ , max_new_tokens=10 , do_sample=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = greedy_ids[:, input_ids.shape[1] :] SCREAMING_SNAKE_CASE_: str = tokenizer.decode(new_greedy_ids[0]) with CaptureStdout() as cs: SCREAMING_SNAKE_CASE_: Dict = TextStreamer(lowerCAmelCase__ , skip_prompt=lowerCAmelCase__) model.generate(lowerCAmelCase__ , max_new_tokens=10 , do_sample=lowerCAmelCase__ , streamer=lowerCAmelCase__) # The greedy text should be printed to stdout, except for the final "\n" in the streamer SCREAMING_SNAKE_CASE_: Union[str, Any] = cs.out[:-1] self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Any): # Tests that we can pass `decode_kwargs` to the streamer to control how the tokens are decoded. Must be tested # with actual models -- the dummy models' tokenizers are not aligned with their models, and # `skip_special_tokens=True` has no effect on them SCREAMING_SNAKE_CASE_: List[str] = AutoTokenizer.from_pretrained("distilgpt2") SCREAMING_SNAKE_CASE_: str = AutoModelForCausalLM.from_pretrained("distilgpt2").to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = -1 SCREAMING_SNAKE_CASE_: Tuple = torch.ones((1, 5) , device=lowerCAmelCase__).long() * model.config.bos_token_id with CaptureStdout() as cs: SCREAMING_SNAKE_CASE_: Optional[Any] = TextStreamer(lowerCAmelCase__ , skip_special_tokens=lowerCAmelCase__) model.generate(lowerCAmelCase__ , max_new_tokens=1 , do_sample=lowerCAmelCase__ , streamer=lowerCAmelCase__) # The prompt contains a special token, so the streamer should not print it. As such, the output text, when # re-tokenized, must only contain one token SCREAMING_SNAKE_CASE_: List[str] = cs.out[:-1] # Remove the final "\n" SCREAMING_SNAKE_CASE_: Any = tokenizer(lowerCAmelCase__ , return_tensors="pt") self.assertEqual(streamer_text_tokenized.input_ids.shape , (1, 1)) def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: List[Any] = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2") SCREAMING_SNAKE_CASE_: Union[str, Any] = AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2").to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = -1 SCREAMING_SNAKE_CASE_: List[Any] = ids_tensor((1, 5) , vocab_size=model.config.vocab_size).to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = TextIteratorStreamer(lowerCAmelCase__ , timeout=0.001) SCREAMING_SNAKE_CASE_: Optional[int] = {"input_ids": input_ids, "max_new_tokens": 10, "do_sample": False, "streamer": streamer} SCREAMING_SNAKE_CASE_: List[Any] = Thread(target=model.generate , kwargs=lowerCAmelCase__) thread.start() # The streamer will timeout after 0.001 seconds, so an exception will be raised with self.assertRaises(lowerCAmelCase__): SCREAMING_SNAKE_CASE_: List[Any] = "" for new_text in streamer: streamer_text += new_text
13
import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from transformers.generation import DisjunctiveConstraint @require_torch class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : List[str]): # For consistency across different places the DisjunctiveConstraint is called, # dc.token_ids is a list of integers. It is also initialized only by integers. SCREAMING_SNAKE_CASE_: Optional[Any] = [[1, 2, 4], [1, 2, 3, 4]] SCREAMING_SNAKE_CASE_: Any = DisjunctiveConstraint(lowerCAmelCase__) self.assertTrue(isinstance(dc.token_ids , lowerCAmelCase__)) with self.assertRaises(lowerCAmelCase__): DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]])) with self.assertRaises(lowerCAmelCase__): DisjunctiveConstraint([torch.LongTensor([1, 2, 4]), torch.LongTensor([1, 2, 3, 4, 5])]) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): # We can't have constraints that are complete subsets of another. This leads to a preverse # interpretation of "constraint fulfillment": does generating [1,2,3] fulfill the constraint? # It would mean that it generated [1,2] which fulfills it, but it's in the middle of potentially # fulfilling [1,2,3,4]. If we believe that [1,2,3] does fulfill the constraint, then the algorithm # will necessarily never reach [1,2,3,4], giving users a false sense of control (better to just not allow it). SCREAMING_SNAKE_CASE_: Union[str, Any] = [[1, 2], [1, 2, 3, 4]] with self.assertRaises(lowerCAmelCase__): DisjunctiveConstraint(lowerCAmelCase__) # fails here def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: List[str] = [[1, 2, 3], [1, 2, 4]] SCREAMING_SNAKE_CASE_: Tuple = DisjunctiveConstraint(lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = dc.update(1) SCREAMING_SNAKE_CASE_: Dict = stepped is True and completed is False and reset is False self.assertTrue(lowerCAmelCase__) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = dc.update(2) SCREAMING_SNAKE_CASE_: Optional[Any] = stepped is True and completed is False and reset is False self.assertTrue(lowerCAmelCase__) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1, 2]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = dc.update(3) SCREAMING_SNAKE_CASE_: Tuple = stepped is True and completed is True and reset is False self.assertTrue(lowerCAmelCase__) self.assertTrue(dc.completed) # Completed! self.assertTrue(dc.current_seq == [1, 2, 3]) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]] SCREAMING_SNAKE_CASE_: List[Any] = DisjunctiveConstraint(lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = dc.update(1) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = dc.update(2) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1, 2]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = dc.update(4) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1, 2, 4]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = dc.update(5) self.assertTrue(dc.completed) # Completed! self.assertTrue(dc.current_seq == [1, 2, 4, 5]) dc.reset() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = dc.update(1) self.assertTrue(not dc.completed) self.assertTrue(dc.remaining() == 3) self.assertTrue(dc.current_seq == [1]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = dc.update(2) self.assertTrue(not dc.completed) self.assertTrue(dc.remaining() == 2) self.assertTrue(dc.current_seq == [1, 2]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = dc.update(5) self.assertTrue(dc.completed) # Completed! self.assertTrue(dc.remaining() == 0) self.assertTrue(dc.current_seq == [1, 2, 5])
13
1
def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: List[Any] = [] if len(_UpperCAmelCase ) == 1: return [nums.copy()] for _ in range(len(_UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_: Any = nums.pop(0 ) SCREAMING_SNAKE_CASE_: List[str] = permute(_UpperCAmelCase ) for perm in permutations: perm.append(_UpperCAmelCase ) result.extend(_UpperCAmelCase ) nums.append(_UpperCAmelCase ) return result def A_ ( _UpperCAmelCase ): def backtrack(_UpperCAmelCase ): if start == len(_UpperCAmelCase ) - 1: output.append(nums[:] ) else: for i in range(_UpperCAmelCase , len(_UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = nums[i], nums[start] backtrack(start + 1 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = nums[i], nums[start] # backtrack SCREAMING_SNAKE_CASE_: Optional[Any] = [] backtrack(0 ) return output if __name__ == "__main__": import doctest # use res to print the data in permute2 function lowerCAmelCase : List[Any] = permutea([1, 2, 3]) print(res) doctest.testmod()
13
import pickle import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, XGLMTokenizer, XGLMTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin lowerCAmelCase : Optional[Any] = get_tests_dir("""fixtures/test_sentencepiece.model""") @require_sentencepiece @require_tokenizers class __lowercase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : Optional[Any] = XGLMTokenizer _UpperCAmelCase : List[Any] = XGLMTokenizerFast _UpperCAmelCase : Optional[int] = True _UpperCAmelCase : Tuple = True def _SCREAMING_SNAKE_CASE ( self : Tuple): super().setUp() # We have a SentencePiece fixture for testing SCREAMING_SNAKE_CASE_: List[Any] = XGLMTokenizer(lowerCAmelCase__ , keep_accents=lowerCAmelCase__) tokenizer.save_pretrained(self.tmpdirname) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_: Optional[Any] = "<pad>" SCREAMING_SNAKE_CASE_: int = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCAmelCase__) , lowerCAmelCase__) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCAmelCase__) , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Optional[int] = list(self.get_tokenizer().get_vocab().keys()) self.assertEqual(vocab_keys[0] , "<s>") self.assertEqual(vocab_keys[1] , "<pad>") self.assertEqual(len(lowerCAmelCase__) , 1008) def _SCREAMING_SNAKE_CASE ( self : Any): self.assertEqual(self.get_tokenizer().vocab_size , 1008) def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Optional[int] = XGLMTokenizer(lowerCAmelCase__ , keep_accents=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = tokenizer.tokenize("This is a test") self.assertListEqual(lowerCAmelCase__ , ["▁This", "▁is", "▁a", "▁t", "est"]) self.assertListEqual( tokenizer.convert_tokens_to_ids(lowerCAmelCase__) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) SCREAMING_SNAKE_CASE_: List[str] = tokenizer.tokenize("I was born in 92000, and this is falsé.") self.assertListEqual( lowerCAmelCase__ , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "9", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "é", ".", ] , ) SCREAMING_SNAKE_CASE_: Optional[Any] = tokenizer.convert_tokens_to_ids(lowerCAmelCase__) self.assertListEqual( lowerCAmelCase__ , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) SCREAMING_SNAKE_CASE_: List[Any] = tokenizer.convert_ids_to_tokens(lowerCAmelCase__) self.assertListEqual( lowerCAmelCase__ , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "<unk>", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "<unk>", ".", ] , ) @cached_property def _SCREAMING_SNAKE_CASE ( self : Any): return XGLMTokenizer.from_pretrained("facebook/xglm-564M") def _SCREAMING_SNAKE_CASE ( self : str): with tempfile.NamedTemporaryFile() as f: shutil.copyfile(lowerCAmelCase__ , f.name) SCREAMING_SNAKE_CASE_: Tuple = XGLMTokenizer(f.name , keep_accents=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = pickle.dumps(lowerCAmelCase__) pickle.loads(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : str): if not self.test_rust_tokenizer: return SCREAMING_SNAKE_CASE_: Dict = self.get_tokenizer() SCREAMING_SNAKE_CASE_: List[str] = self.get_rust_tokenizer() SCREAMING_SNAKE_CASE_: Any = "I was born in 92000, and this is falsé." SCREAMING_SNAKE_CASE_: Union[str, Any] = tokenizer.tokenize(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = rust_tokenizer.tokenize(lowerCAmelCase__) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = rust_tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = self.get_rust_tokenizer() SCREAMING_SNAKE_CASE_: str = tokenizer.encode(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = rust_tokenizer.encode(lowerCAmelCase__) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__) @slow def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: Dict = "Hello World!" SCREAMING_SNAKE_CASE_: Union[str, Any] = [2, 3_1227, 4447, 35] self.assertListEqual(lowerCAmelCase__ , self.big_tokenizer.encode(lowerCAmelCase__)) @slow def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Union[str, Any] = ( "This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will" " add words that should not exsist and be tokenized to unk, such as saoneuhaoesuth" ) # fmt: off SCREAMING_SNAKE_CASE_: Optional[Any] = [2, 1018, 67, 11, 1988, 2617, 5631, 278, 11, 3407, 48, 7_1630, 2_8085, 4, 3234, 157, 13, 6, 5, 6, 4, 3526, 768, 15, 659, 57, 298, 3983, 864, 129, 21, 6, 5, 1_3675, 377, 652, 7580, 1_0341, 155, 2817, 422, 1666, 7, 1674, 53, 113, 20_2277, 1_7892, 33, 60, 87, 4, 3234, 157, 61, 2667, 5_2376, 19, 88, 23, 735] # fmt: on self.assertListEqual(lowerCAmelCase__ , self.big_tokenizer.encode(lowerCAmelCase__)) @slow def _SCREAMING_SNAKE_CASE ( self : int): # fmt: off SCREAMING_SNAKE_CASE_: str = { "input_ids": [[2, 10_8825, 1163, 15, 8_8010, 473, 1_5898, 157, 1_3672, 1857, 312, 8, 23_8021, 1163, 53, 1_3672, 1857, 312, 8, 5_3283, 18_2396, 8, 1_8566, 16, 3_6733, 4101, 8, 230, 24_4017, 12_2553, 7, 15, 13_2597, 4, 293, 1_2511, 7610, 4, 3414, 13_2597, 9, 4, 3_2361, 362, 4, 734, 2_8512, 3_2569, 18, 4, 3_2361, 2_6096, 1_4982, 73, 1_8715, 2_1433, 23_5261, 15, 492, 1_2427, 16, 53, 1_8715, 2_1433, 6_5454, 15, 2_3659, 563, 16, 278, 597, 2843, 595, 7931, 18_2396, 6_4186, 22, 886, 595, 13_2981, 53, 2_5540, 3449, 4_3982, 3_9901, 5951, 878, 330, 4, 2_7694, 8_0269, 312, 53, 6517, 1_1780, 611, 2_0408, 5], [2, 6, 13_2597, 67, 4_2897, 33, 592, 8, 16_3729, 2_5540, 361, 13_6997, 10_9514, 17_3230, 7, 501, 60, 10_2913, 196, 5631, 235, 6_3243, 473, 6, 23_1757, 74, 5277, 7905, 53, 3095, 3_7317, 22, 454, 18_3874, 5], [2, 268, 3_1298, 4_6530, 6, 13_2935, 4_3831, 7, 597, 32, 24, 3688, 9865, 5]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] } # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=lowerCAmelCase__ , model_name="facebook/xglm-564M" , padding=lowerCAmelCase__ , )
13
1
# coding=utf-8 # Copyright 2020 The HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # this script dumps information about the environment import os import sys import transformers lowerCAmelCase : Optional[int] = """3""" print("""Python version:""", sys.version) print("""transformers version:""", transformers.__version__) try: import torch print("""Torch version:""", torch.__version__) print("""Cuda available:""", torch.cuda.is_available()) print("""Cuda version:""", torch.version.cuda) print("""CuDNN version:""", torch.backends.cudnn.version()) print("""Number of GPUs available:""", torch.cuda.device_count()) print("""NCCL version:""", torch.cuda.nccl.version()) except ImportError: print("""Torch version:""", None) try: import deepspeed print("""DeepSpeed version:""", deepspeed.__version__) except ImportError: print("""DeepSpeed version:""", None) try: import tensorflow as tf print("""TensorFlow version:""", tf.__version__) print("""TF GPUs available:""", bool(tf.config.list_physical_devices("""GPU"""))) print("""Number of TF GPUs available:""", len(tf.config.list_physical_devices("""GPU"""))) except ImportError: print("""TensorFlow version:""", None)
13
def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , ): SCREAMING_SNAKE_CASE_: Optional[int] = [redshift, radiation_density, matter_density, dark_energy] if any(p < 0 for p in parameters ): raise ValueError("All input parameters must be positive" ) if any(p > 1 for p in parameters[1:4] ): raise ValueError("Relative densities cannot be greater than one" ) else: SCREAMING_SNAKE_CASE_: int = 1 - (matter_density + radiation_density + dark_energy) SCREAMING_SNAKE_CASE_: Dict = ( radiation_density * (redshift + 1) ** 4 + matter_density * (redshift + 1) ** 3 + curvature * (redshift + 1) ** 2 + dark_energy ) SCREAMING_SNAKE_CASE_: Any = hubble_constant * e_a ** (1 / 2) return hubble if __name__ == "__main__": import doctest # run doctest doctest.testmod() # demo LCDM approximation lowerCAmelCase : List[Any] = 0.3 print( hubble_parameter( hubble_constant=68.3, radiation_density=1E-4, matter_density=matter_density, dark_energy=1 - matter_density, redshift=0, ) )
13
1
import colorsys from PIL import Image # type: ignore def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Dict = x SCREAMING_SNAKE_CASE_: Dict = y for step in range(_UpperCAmelCase ): # noqa: B007 SCREAMING_SNAKE_CASE_: Union[str, Any] = a * a - b * b + x SCREAMING_SNAKE_CASE_: Union[str, Any] = 2 * a * b + y SCREAMING_SNAKE_CASE_: List[str] = a_new # divergence happens for all complex number with an absolute value # greater than 4 if a * a + b * b > 4: break return step / (max_step - 1) def A_ ( _UpperCAmelCase ): if distance == 1: return (0, 0, 0) else: return (2_55, 2_55, 2_55) def A_ ( _UpperCAmelCase ): if distance == 1: return (0, 0, 0) else: return tuple(round(i * 2_55 ) for i in colorsys.hsv_to_rgb(_UpperCAmelCase , 1 , 1 ) ) def A_ ( _UpperCAmelCase = 8_00 , _UpperCAmelCase = 6_00 , _UpperCAmelCase = -0.6 , _UpperCAmelCase = 0 , _UpperCAmelCase = 3.2 , _UpperCAmelCase = 50 , _UpperCAmelCase = True , ): SCREAMING_SNAKE_CASE_: str = Image.new("RGB" , (image_width, image_height) ) SCREAMING_SNAKE_CASE_: List[Any] = img.load() # loop through the image-coordinates for image_x in range(_UpperCAmelCase ): for image_y in range(_UpperCAmelCase ): # determine the figure-coordinates based on the image-coordinates SCREAMING_SNAKE_CASE_: Dict = figure_width / image_width * image_height SCREAMING_SNAKE_CASE_: Tuple = figure_center_x + (image_x / image_width - 0.5) * figure_width SCREAMING_SNAKE_CASE_: Optional[Any] = figure_center_y + (image_y / image_height - 0.5) * figure_height SCREAMING_SNAKE_CASE_: Union[str, Any] = get_distance(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) # color the corresponding pixel based on the selected coloring-function if use_distance_color_coding: SCREAMING_SNAKE_CASE_: int = get_color_coded_rgb(_UpperCAmelCase ) else: SCREAMING_SNAKE_CASE_: Optional[int] = get_black_and_white_rgb(_UpperCAmelCase ) return img if __name__ == "__main__": import doctest doctest.testmod() # colored version, full figure lowerCAmelCase : str = get_image() # uncomment for colored version, different section, zoomed in # img = get_image(figure_center_x = -0.6, figure_center_y = -0.4, # figure_width = 0.8) # uncomment for black and white version, full figure # img = get_image(use_distance_color_coding = False) # uncomment to save the image # img.save("mandelbrot.png") img.show()
13
from typing import Optional, Union import torch from torch import nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention from ...modeling_utils import PreTrainedModel from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging from .configuration_mobilenet_va import MobileNetVaConfig lowerCAmelCase : int = logging.get_logger(__name__) # General docstring lowerCAmelCase : int = """MobileNetV1Config""" # Base docstring lowerCAmelCase : List[Any] = """google/mobilenet_v1_1.0_224""" lowerCAmelCase : Dict = [1, 1024, 7, 7] # Image classification docstring lowerCAmelCase : Union[str, Any] = """google/mobilenet_v1_1.0_224""" lowerCAmelCase : Any = """tabby, tabby cat""" lowerCAmelCase : List[Any] = [ """google/mobilenet_v1_1.0_224""", """google/mobilenet_v1_0.75_192""", # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 ] def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=None ): SCREAMING_SNAKE_CASE_: List[str] = {} if isinstance(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = model.mobilenet_va else: SCREAMING_SNAKE_CASE_: int = model SCREAMING_SNAKE_CASE_: Dict = "MobilenetV1/Conv2d_0/" SCREAMING_SNAKE_CASE_: str = backbone.conv_stem.convolution.weight SCREAMING_SNAKE_CASE_: List[str] = backbone.conv_stem.normalization.bias SCREAMING_SNAKE_CASE_: int = backbone.conv_stem.normalization.weight SCREAMING_SNAKE_CASE_: List[str] = backbone.conv_stem.normalization.running_mean SCREAMING_SNAKE_CASE_: Optional[int] = backbone.conv_stem.normalization.running_var for i in range(13 ): SCREAMING_SNAKE_CASE_: List[str] = i + 1 SCREAMING_SNAKE_CASE_: Optional[int] = i * 2 SCREAMING_SNAKE_CASE_: Any = backbone.layer[pt_index] SCREAMING_SNAKE_CASE_: Any = f"MobilenetV1/Conv2d_{tf_index}_depthwise/" SCREAMING_SNAKE_CASE_: Any = pointer.convolution.weight SCREAMING_SNAKE_CASE_: Any = pointer.normalization.bias SCREAMING_SNAKE_CASE_: str = pointer.normalization.weight SCREAMING_SNAKE_CASE_: Dict = pointer.normalization.running_mean SCREAMING_SNAKE_CASE_: Optional[Any] = pointer.normalization.running_var SCREAMING_SNAKE_CASE_: Tuple = backbone.layer[pt_index + 1] SCREAMING_SNAKE_CASE_: List[str] = f"MobilenetV1/Conv2d_{tf_index}_pointwise/" SCREAMING_SNAKE_CASE_: int = pointer.convolution.weight SCREAMING_SNAKE_CASE_: Any = pointer.normalization.bias SCREAMING_SNAKE_CASE_: Optional[int] = pointer.normalization.weight SCREAMING_SNAKE_CASE_: Optional[Any] = pointer.normalization.running_mean SCREAMING_SNAKE_CASE_: Dict = pointer.normalization.running_var if isinstance(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: List[str] = "MobilenetV1/Logits/Conv2d_1c_1x1/" SCREAMING_SNAKE_CASE_: Optional[Any] = model.classifier.weight SCREAMING_SNAKE_CASE_: Tuple = model.classifier.bias return tf_to_pt_map def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): try: import numpy as np import tensorflow as tf except ImportError: logger.error( "Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see " "https://www.tensorflow.org/install/ for installation instructions." ) raise # Load weights from TF model SCREAMING_SNAKE_CASE_: int = tf.train.list_variables(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: int = {} for name, shape in init_vars: logger.info(f"Loading TF weight {name} with shape {shape}" ) SCREAMING_SNAKE_CASE_: Any = tf.train.load_variable(_UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Union[str, Any] = array # Build TF to PyTorch weights loading map SCREAMING_SNAKE_CASE_: Optional[Any] = _build_tf_to_pytorch_map(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) for name, pointer in tf_to_pt_map.items(): logger.info(f"Importing {name}" ) if name not in tf_weights: logger.info(f"{name} not in tf pre-trained weights, skipping" ) continue SCREAMING_SNAKE_CASE_: int = tf_weights[name] if "depthwise_weights" in name: logger.info("Transposing depthwise" ) SCREAMING_SNAKE_CASE_: int = np.transpose(_UpperCAmelCase , (2, 3, 0, 1) ) elif "weights" in name: logger.info("Transposing" ) if len(pointer.shape ) == 2: # copying into linear layer SCREAMING_SNAKE_CASE_: List[str] = array.squeeze().transpose() else: SCREAMING_SNAKE_CASE_: Any = np.transpose(_UpperCAmelCase , (3, 2, 0, 1) ) if pointer.shape != array.shape: raise ValueError(f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched" ) logger.info(f"Initialize PyTorch weight {name} {array.shape}" ) SCREAMING_SNAKE_CASE_: int = torch.from_numpy(_UpperCAmelCase ) tf_weights.pop(_UpperCAmelCase , _UpperCAmelCase ) tf_weights.pop(name + "/RMSProp" , _UpperCAmelCase ) tf_weights.pop(name + "/RMSProp_1" , _UpperCAmelCase ) tf_weights.pop(name + "/ExponentialMovingAverage" , _UpperCAmelCase ) logger.info(f"Weights not copied to PyTorch model: {', '.join(tf_weights.keys() )}" ) return model def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = features.shape[-2:] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = conv_layer.stride SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = conv_layer.kernel_size if in_height % stride_height == 0: SCREAMING_SNAKE_CASE_: int = max(kernel_height - stride_height , 0 ) else: SCREAMING_SNAKE_CASE_: Tuple = max(kernel_height - (in_height % stride_height) , 0 ) if in_width % stride_width == 0: SCREAMING_SNAKE_CASE_: str = max(kernel_width - stride_width , 0 ) else: SCREAMING_SNAKE_CASE_: Dict = max(kernel_width - (in_width % stride_width) , 0 ) SCREAMING_SNAKE_CASE_: str = pad_along_width // 2 SCREAMING_SNAKE_CASE_: Union[str, Any] = pad_along_width - pad_left SCREAMING_SNAKE_CASE_: int = pad_along_height // 2 SCREAMING_SNAKE_CASE_: Tuple = pad_along_height - pad_top SCREAMING_SNAKE_CASE_: Union[str, Any] = (pad_left, pad_right, pad_top, pad_bottom) return nn.functional.pad(_UpperCAmelCase , _UpperCAmelCase , "constant" , 0.0 ) class __lowercase ( nn.Module ): """simple docstring""" def __init__( self : Optional[int] , lowerCAmelCase__ : MobileNetVaConfig , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : Optional[int] = 1 , lowerCAmelCase__ : Optional[int] = 1 , lowerCAmelCase__ : bool = False , lowerCAmelCase__ : Optional[bool] = True , lowerCAmelCase__ : Optional[bool or str] = True , ): super().__init__() SCREAMING_SNAKE_CASE_: Optional[int] = config if in_channels % groups != 0: raise ValueError(F"Input channels ({in_channels}) are not divisible by {groups} groups.") if out_channels % groups != 0: raise ValueError(F"Output channels ({out_channels}) are not divisible by {groups} groups.") SCREAMING_SNAKE_CASE_: int = 0 if config.tf_padding else int((kernel_size - 1) / 2) SCREAMING_SNAKE_CASE_: Union[str, Any] = nn.Convad( in_channels=lowerCAmelCase__ , out_channels=lowerCAmelCase__ , kernel_size=lowerCAmelCase__ , stride=lowerCAmelCase__ , padding=lowerCAmelCase__ , groups=lowerCAmelCase__ , bias=lowerCAmelCase__ , padding_mode="zeros" , ) if use_normalization: SCREAMING_SNAKE_CASE_: str = nn.BatchNormad( num_features=lowerCAmelCase__ , eps=config.layer_norm_eps , momentum=0.9997 , affine=lowerCAmelCase__ , track_running_stats=lowerCAmelCase__ , ) else: SCREAMING_SNAKE_CASE_: str = None if use_activation: if isinstance(lowerCAmelCase__ , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Dict = ACTaFN[use_activation] elif isinstance(config.hidden_act , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Dict = ACTaFN[config.hidden_act] else: SCREAMING_SNAKE_CASE_: Any = config.hidden_act else: SCREAMING_SNAKE_CASE_: int = None def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : torch.Tensor): if self.config.tf_padding: SCREAMING_SNAKE_CASE_: Union[str, Any] = apply_tf_padding(lowerCAmelCase__ , self.convolution) SCREAMING_SNAKE_CASE_: Optional[int] = self.convolution(lowerCAmelCase__) if self.normalization is not None: SCREAMING_SNAKE_CASE_: int = self.normalization(lowerCAmelCase__) if self.activation is not None: SCREAMING_SNAKE_CASE_: List[Any] = self.activation(lowerCAmelCase__) return features class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : List[str] = MobileNetVaConfig _UpperCAmelCase : List[Any] = load_tf_weights_in_mobilenet_va _UpperCAmelCase : List[Any] = '''mobilenet_v1''' _UpperCAmelCase : int = '''pixel_values''' _UpperCAmelCase : List[Any] = False def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : Union[nn.Linear, nn.Convad]): if isinstance(lowerCAmelCase__ , (nn.Linear, nn.Convad)): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range) if module.bias is not None: module.bias.data.zero_() elif isinstance(lowerCAmelCase__ , nn.BatchNormad): module.bias.data.zero_() module.weight.data.fill_(1.0) lowerCAmelCase : Any = R""" This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior. Parameters: config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights. """ lowerCAmelCase : List[str] = R""" Args: pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`MobileNetV1ImageProcessor.__call__`] for details. output_hidden_states (`bool`, *optional*): Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for more detail. return_dict (`bool`, *optional*): Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. """ @add_start_docstrings( '''The bare MobileNetV1 model outputting raw hidden-states without any specific head on top.''' , UpperCAmelCase_ , ) class __lowercase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Dict , lowerCAmelCase__ : MobileNetVaConfig , lowerCAmelCase__ : bool = True): super().__init__(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = config SCREAMING_SNAKE_CASE_: Union[str, Any] = 32 SCREAMING_SNAKE_CASE_: Dict = max(int(depth * config.depth_multiplier) , config.min_depth) SCREAMING_SNAKE_CASE_: Tuple = MobileNetVaConvLayer( lowerCAmelCase__ , in_channels=config.num_channels , out_channels=lowerCAmelCase__ , kernel_size=3 , stride=2 , ) SCREAMING_SNAKE_CASE_: Optional[int] = [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1] SCREAMING_SNAKE_CASE_: str = nn.ModuleList() for i in range(13): SCREAMING_SNAKE_CASE_: List[Any] = out_channels if strides[i] == 2 or i == 0: depth *= 2 SCREAMING_SNAKE_CASE_: str = max(int(depth * config.depth_multiplier) , config.min_depth) self.layer.append( MobileNetVaConvLayer( lowerCAmelCase__ , in_channels=lowerCAmelCase__ , out_channels=lowerCAmelCase__ , kernel_size=3 , stride=strides[i] , groups=lowerCAmelCase__ , )) self.layer.append( MobileNetVaConvLayer( lowerCAmelCase__ , in_channels=lowerCAmelCase__ , out_channels=lowerCAmelCase__ , kernel_size=1 , )) SCREAMING_SNAKE_CASE_: List[str] = nn.AdaptiveAvgPoolad((1, 1)) if add_pooling_layer else None # Initialize weights and apply final processing self.post_init() def _SCREAMING_SNAKE_CASE ( self : int , lowerCAmelCase__ : str): raise NotImplementedError @add_start_docstrings_to_model_forward(lowerCAmelCase__) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=lowerCAmelCase__ , config_class=_CONFIG_FOR_DOC , modality="vision" , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : Optional[torch.Tensor] = None , lowerCAmelCase__ : Optional[bool] = None , lowerCAmelCase__ : Optional[bool] = None , ): SCREAMING_SNAKE_CASE_: Optional[int] = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) SCREAMING_SNAKE_CASE_: Any = return_dict if return_dict is not None else self.config.use_return_dict if pixel_values is None: raise ValueError("You have to specify pixel_values") SCREAMING_SNAKE_CASE_: Optional[Any] = self.conv_stem(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = () if output_hidden_states else None for i, layer_module in enumerate(self.layer): SCREAMING_SNAKE_CASE_: Tuple = layer_module(lowerCAmelCase__) if output_hidden_states: SCREAMING_SNAKE_CASE_: Optional[int] = all_hidden_states + (hidden_states,) SCREAMING_SNAKE_CASE_: Optional[Any] = hidden_states if self.pooler is not None: SCREAMING_SNAKE_CASE_: int = torch.flatten(self.pooler(lowerCAmelCase__) , start_dim=1) else: SCREAMING_SNAKE_CASE_: List[str] = None if not return_dict: return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None) return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=lowerCAmelCase__ , pooler_output=lowerCAmelCase__ , hidden_states=lowerCAmelCase__ , ) @add_start_docstrings( ''' MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. ''' , UpperCAmelCase_ , ) class __lowercase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Any , lowerCAmelCase__ : MobileNetVaConfig): super().__init__(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = config.num_labels SCREAMING_SNAKE_CASE_: Dict = MobileNetVaModel(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = self.mobilenet_va.layer[-1].convolution.out_channels # Classifier head SCREAMING_SNAKE_CASE_: str = nn.Dropout(config.classifier_dropout_prob , inplace=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = nn.Linear(lowerCAmelCase__ , config.num_labels) if config.num_labels > 0 else nn.Identity() # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(lowerCAmelCase__) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=lowerCAmelCase__ , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : Optional[torch.Tensor] = None , lowerCAmelCase__ : Optional[bool] = None , lowerCAmelCase__ : Optional[torch.Tensor] = None , lowerCAmelCase__ : Optional[bool] = None , ): SCREAMING_SNAKE_CASE_: List[str] = return_dict if return_dict is not None else self.config.use_return_dict SCREAMING_SNAKE_CASE_: List[str] = self.mobilenet_va(lowerCAmelCase__ , output_hidden_states=lowerCAmelCase__ , return_dict=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = outputs.pooler_output if return_dict else outputs[1] SCREAMING_SNAKE_CASE_: Tuple = self.classifier(self.dropout(lowerCAmelCase__)) SCREAMING_SNAKE_CASE_: Optional[int] = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: SCREAMING_SNAKE_CASE_: List[Any] = "regression" elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): SCREAMING_SNAKE_CASE_: int = "single_label_classification" else: SCREAMING_SNAKE_CASE_: str = "multi_label_classification" if self.config.problem_type == "regression": SCREAMING_SNAKE_CASE_: Dict = MSELoss() if self.num_labels == 1: SCREAMING_SNAKE_CASE_: Any = loss_fct(logits.squeeze() , labels.squeeze()) else: SCREAMING_SNAKE_CASE_: int = loss_fct(lowerCAmelCase__ , lowerCAmelCase__) elif self.config.problem_type == "single_label_classification": SCREAMING_SNAKE_CASE_: Any = CrossEntropyLoss() SCREAMING_SNAKE_CASE_: Dict = loss_fct(logits.view(-1 , self.num_labels) , labels.view(-1)) elif self.config.problem_type == "multi_label_classification": SCREAMING_SNAKE_CASE_: Dict = BCEWithLogitsLoss() SCREAMING_SNAKE_CASE_: Dict = loss_fct(lowerCAmelCase__ , lowerCAmelCase__) if not return_dict: SCREAMING_SNAKE_CASE_: int = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return ImageClassifierOutputWithNoAttention( loss=lowerCAmelCase__ , logits=lowerCAmelCase__ , hidden_states=outputs.hidden_states , )
13
1
# tests directory-specific settings - this file is run automatically # by pytest before any tests are run import doctest import sys import warnings from os.path import abspath, dirname, join import _pytest from transformers.testing_utils import HfDoctestModule, HfDocTestParser # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. lowerCAmelCase : List[Any] = abspath(join(dirname(__file__), """src""")) sys.path.insert(1, git_repo_path) # silence FutureWarning warnings in tests since often we can't act on them until # they become normal warnings - i.e. the tests still need to test the current functionality warnings.simplefilter(action="""ignore""", category=FutureWarning) def A_ ( _UpperCAmelCase ): config.addinivalue_line( "markers" , "is_pt_tf_cross_test: mark test to run only when PT and TF interactions are tested" ) config.addinivalue_line( "markers" , "is_pt_flax_cross_test: mark test to run only when PT and FLAX interactions are tested" ) config.addinivalue_line("markers" , "is_pipeline_test: mark test to run only when pipelines are tested" ) config.addinivalue_line("markers" , "is_staging_test: mark test to run only in the staging environment" ) config.addinivalue_line("markers" , "accelerate_tests: mark test that require accelerate" ) config.addinivalue_line("markers" , "tool_tests: mark the tool tests that are run on their specific schedule" ) def A_ ( _UpperCAmelCase ): from transformers.testing_utils import pytest_addoption_shared pytest_addoption_shared(_UpperCAmelCase ) def A_ ( _UpperCAmelCase ): from transformers.testing_utils import pytest_terminal_summary_main SCREAMING_SNAKE_CASE_: Union[str, Any] = terminalreporter.config.getoption("--make-reports" ) if make_reports: pytest_terminal_summary_main(_UpperCAmelCase , id=_UpperCAmelCase ) def A_ ( _UpperCAmelCase , _UpperCAmelCase ): # If no tests are collected, pytest exists with code 5, which makes the CI fail. if exitstatus == 5: SCREAMING_SNAKE_CASE_: Tuple = 0 # Doctest custom flag to ignore output. lowerCAmelCase : Any = doctest.register_optionflag("""IGNORE_RESULT""") lowerCAmelCase : str = doctest.OutputChecker class __lowercase ( UpperCAmelCase_ ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : int): if IGNORE_RESULT & optionflags: return True return OutputChecker.check_output(self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) lowerCAmelCase : Union[str, Any] = CustomOutputChecker lowerCAmelCase : Dict = HfDoctestModule lowerCAmelCase : Union[str, Any] = HfDocTestParser
13
def A_ ( _UpperCAmelCase , _UpperCAmelCase = False ): if not isinstance(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: str = f"Expected string as input, found {type(_UpperCAmelCase )}" raise ValueError(_UpperCAmelCase ) if not isinstance(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Optional[Any] = f"Expected boolean as use_pascal parameter, found {type(_UpperCAmelCase )}" raise ValueError(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Tuple = input_str.split("_" ) SCREAMING_SNAKE_CASE_: str = 0 if use_pascal else 1 SCREAMING_SNAKE_CASE_: int = words[start_index:] SCREAMING_SNAKE_CASE_: List[str] = [word[0].upper() + word[1:] for word in words_to_capitalize] SCREAMING_SNAKE_CASE_: List[Any] = "" if use_pascal else words[0] return "".join([initial_word, *capitalized_words] ) if __name__ == "__main__": from doctest import testmod testmod()
13
1
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCAmelCase : List[Any] = logging.get_logger(__name__) lowerCAmelCase : int = { """facebook/deit-base-distilled-patch16-224""": ( """https://huggingface.co/facebook/deit-base-patch16-224/resolve/main/config.json""" ), # See all DeiT models at https://huggingface.co/models?filter=deit } class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : List[str] = '''deit''' def __init__( self : List[Any] , lowerCAmelCase__ : str=768 , lowerCAmelCase__ : Optional[int]=12 , lowerCAmelCase__ : Optional[Any]=12 , lowerCAmelCase__ : Dict=3072 , lowerCAmelCase__ : Dict="gelu" , lowerCAmelCase__ : Optional[Any]=0.0 , lowerCAmelCase__ : str=0.0 , lowerCAmelCase__ : Dict=0.02 , lowerCAmelCase__ : Any=1E-12 , lowerCAmelCase__ : int=224 , lowerCAmelCase__ : Dict=16 , lowerCAmelCase__ : Tuple=3 , lowerCAmelCase__ : Dict=True , lowerCAmelCase__ : List[Any]=16 , **lowerCAmelCase__ : List[str] , ): super().__init__(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = hidden_size SCREAMING_SNAKE_CASE_: List[str] = num_hidden_layers SCREAMING_SNAKE_CASE_: int = num_attention_heads SCREAMING_SNAKE_CASE_: Any = intermediate_size SCREAMING_SNAKE_CASE_: Any = hidden_act SCREAMING_SNAKE_CASE_: int = hidden_dropout_prob SCREAMING_SNAKE_CASE_: Any = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_: int = initializer_range SCREAMING_SNAKE_CASE_: str = layer_norm_eps SCREAMING_SNAKE_CASE_: List[Any] = image_size SCREAMING_SNAKE_CASE_: Optional[int] = patch_size SCREAMING_SNAKE_CASE_: Optional[Any] = num_channels SCREAMING_SNAKE_CASE_: Optional[int] = qkv_bias SCREAMING_SNAKE_CASE_: Optional[Any] = encoder_stride class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : List[str] = version.parse('''1.11''' ) @property def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ]) @property def _SCREAMING_SNAKE_CASE ( self : Tuple): return 1E-4
13
import os import tempfile import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from torch import nn from transformers import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_inverse_sqrt_schedule, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) def A_ ( _UpperCAmelCase , _UpperCAmelCase=10 ): SCREAMING_SNAKE_CASE_: Union[str, Any] = [] for _ in range(_UpperCAmelCase ): lrs.append(scheduler.get_lr()[0] ) scheduler.step() return lrs def A_ ( _UpperCAmelCase , _UpperCAmelCase=10 ): SCREAMING_SNAKE_CASE_: List[str] = [] for step in range(_UpperCAmelCase ): lrs.append(scheduler.get_lr()[0] ) scheduler.step() if step == num_steps // 2: with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE_: Optional[int] = os.path.join(_UpperCAmelCase , "schedule.bin" ) torch.save(scheduler.state_dict() , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Optional[Any] = torch.load(_UpperCAmelCase ) scheduler.load_state_dict(_UpperCAmelCase ) return lrs @require_torch class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : int , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Tuple): self.assertEqual(len(lowerCAmelCase__) , len(lowerCAmelCase__)) for a, b in zip(lowerCAmelCase__ , lowerCAmelCase__): self.assertAlmostEqual(lowerCAmelCase__ , lowerCAmelCase__ , delta=lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.tensor([0.1, -0.2, -0.1] , requires_grad=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = torch.tensor([0.4, 0.2, -0.5]) SCREAMING_SNAKE_CASE_: Optional[Any] = nn.MSELoss() # No warmup, constant schedule, no gradient clipping SCREAMING_SNAKE_CASE_: int = AdamW(params=[w] , lr=2E-1 , weight_decay=0.0) for _ in range(100): SCREAMING_SNAKE_CASE_: Dict = criterion(lowerCAmelCase__ , lowerCAmelCase__) loss.backward() optimizer.step() w.grad.detach_() # No zero_grad() function on simple tensors. we do it ourselves. w.grad.zero_() self.assertListAlmostEqual(w.tolist() , [0.4, 0.2, -0.5] , tol=1E-2) def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.tensor([0.1, -0.2, -0.1] , requires_grad=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = torch.tensor([0.4, 0.2, -0.5]) SCREAMING_SNAKE_CASE_: Any = nn.MSELoss() # No warmup, constant schedule, no gradient clipping SCREAMING_SNAKE_CASE_: int = Adafactor( params=[w] , lr=1E-2 , eps=(1E-30, 1E-3) , clip_threshold=1.0 , decay_rate=-0.8 , betaa=lowerCAmelCase__ , weight_decay=0.0 , relative_step=lowerCAmelCase__ , scale_parameter=lowerCAmelCase__ , warmup_init=lowerCAmelCase__ , ) for _ in range(1000): SCREAMING_SNAKE_CASE_: List[Any] = criterion(lowerCAmelCase__ , lowerCAmelCase__) loss.backward() optimizer.step() w.grad.detach_() # No zero_grad() function on simple tensors. we do it ourselves. w.grad.zero_() self.assertListAlmostEqual(w.tolist() , [0.4, 0.2, -0.5] , tol=1E-2) @require_torch class __lowercase ( unittest.TestCase ): """simple docstring""" _UpperCAmelCase : Union[str, Any] = nn.Linear(50 , 50 ) if is_torch_available() else None _UpperCAmelCase : List[Any] = AdamW(m.parameters() , lr=10.0 ) if is_torch_available() else None _UpperCAmelCase : Optional[Any] = 10 def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : Optional[Any]=None): self.assertEqual(len(lowerCAmelCase__) , len(lowerCAmelCase__)) for a, b in zip(lowerCAmelCase__ , lowerCAmelCase__): self.assertAlmostEqual(lowerCAmelCase__ , lowerCAmelCase__ , delta=lowerCAmelCase__ , msg=lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Dict = {"num_warmup_steps": 2, "num_training_steps": 10} # schedulers doct format # function: (sched_args_dict, expected_learning_rates) SCREAMING_SNAKE_CASE_: Dict = { get_constant_schedule: ({}, [10.0] * self.num_steps), get_constant_schedule_with_warmup: ( {"num_warmup_steps": 4}, [0.0, 2.5, 5.0, 7.5, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0], ), get_linear_schedule_with_warmup: ( {**common_kwargs}, [0.0, 5.0, 10.0, 8.75, 7.5, 6.25, 5.0, 3.75, 2.5, 1.25], ), get_cosine_schedule_with_warmup: ( {**common_kwargs}, [0.0, 5.0, 10.0, 9.61, 8.53, 6.91, 5.0, 3.08, 1.46, 0.38], ), get_cosine_with_hard_restarts_schedule_with_warmup: ( {**common_kwargs, "num_cycles": 2}, [0.0, 5.0, 10.0, 8.53, 5.0, 1.46, 10.0, 8.53, 5.0, 1.46], ), get_polynomial_decay_schedule_with_warmup: ( {**common_kwargs, "power": 2.0, "lr_end": 1E-7}, [0.0, 5.0, 10.0, 7.656, 5.625, 3.906, 2.5, 1.406, 0.625, 0.156], ), get_inverse_sqrt_schedule: ( {"num_warmup_steps": 2}, [0.0, 5.0, 10.0, 8.165, 7.071, 6.325, 5.774, 5.345, 5.0, 4.714], ), } for scheduler_func, data in scheds.items(): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = data SCREAMING_SNAKE_CASE_: List[Any] = scheduler_func(self.optimizer , **lowerCAmelCase__) self.assertEqual(len([scheduler.get_lr()[0]]) , 1) SCREAMING_SNAKE_CASE_: int = unwrap_schedule(lowerCAmelCase__ , self.num_steps) self.assertListAlmostEqual( lowerCAmelCase__ , lowerCAmelCase__ , tol=1E-2 , msg=F"failed for {scheduler_func} in normal scheduler" , ) SCREAMING_SNAKE_CASE_: List[str] = scheduler_func(self.optimizer , **lowerCAmelCase__) if scheduler_func.__name__ != "get_constant_schedule": LambdaScheduleWrapper.wrap_scheduler(lowerCAmelCase__) # wrap to test picklability of the schedule SCREAMING_SNAKE_CASE_: Tuple = unwrap_and_save_reload_schedule(lowerCAmelCase__ , self.num_steps) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ , msg=F"failed for {scheduler_func} in save and reload") class __lowercase : """simple docstring""" def __init__( self : str , lowerCAmelCase__ : List[str]): SCREAMING_SNAKE_CASE_: List[Any] = fn def __call__( self : Optional[int] , *lowerCAmelCase__ : List[Any] , **lowerCAmelCase__ : Tuple): return self.fn(*lowerCAmelCase__ , **lowerCAmelCase__) @classmethod def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : str): SCREAMING_SNAKE_CASE_: str = list(map(self , scheduler.lr_lambdas))
13
1
def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Union[str, Any] = (1 + 24 * n) ** 0.5 return ((1 + root) / 6) % 1 == 0 def A_ ( _UpperCAmelCase = 50_00 ): SCREAMING_SNAKE_CASE_: str = [(i * (3 * i - 1)) // 2 for i in range(1 , _UpperCAmelCase )] for i, pentagonal_i in enumerate(_UpperCAmelCase ): for j in range(_UpperCAmelCase , len(_UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_: Dict = pentagonal_nums[j] SCREAMING_SNAKE_CASE_: List[Any] = pentagonal_i + pentagonal_j SCREAMING_SNAKE_CASE_: str = pentagonal_j - pentagonal_i if is_pentagonal(_UpperCAmelCase ) and is_pentagonal(_UpperCAmelCase ): return b return -1 if __name__ == "__main__": print(f'''{solution() = }''')
13
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, Features, Value from .base import TaskTemplate @dataclass(frozen=UpperCAmelCase_ ) class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : str = field(default='''automatic-speech-recognition''' , metadata={'''include_in_asdict_even_if_is_default''': True} ) _UpperCAmelCase : ClassVar[Features] = Features({'''audio''': Audio()} ) _UpperCAmelCase : ClassVar[Features] = Features({'''transcription''': Value('''string''' )} ) _UpperCAmelCase : str = "audio" _UpperCAmelCase : str = "transcription" def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int): if self.audio_column not in features: raise ValueError(F"Column {self.audio_column} is not present in features.") if not isinstance(features[self.audio_column] , lowerCAmelCase__): raise ValueError(F"Column {self.audio_column} is not an Audio type.") SCREAMING_SNAKE_CASE_: Tuple = copy.deepcopy(self) SCREAMING_SNAKE_CASE_: Optional[int] = self.input_schema.copy() SCREAMING_SNAKE_CASE_: Dict = features[self.audio_column] SCREAMING_SNAKE_CASE_: int = input_schema return task_template @property def _SCREAMING_SNAKE_CASE ( self : int): return {self.audio_column: "audio", self.transcription_column: "transcription"}
13
1
def A_ ( _UpperCAmelCase = 10_00 ): return sum(e for e in range(3 , _UpperCAmelCase ) if e % 3 == 0 or e % 5 == 0 ) if __name__ == "__main__": print(f'''{solution() = }''')
13
import unittest import numpy as np from transformers import is_flax_available from transformers.testing_utils import require_flax from ..test_modeling_flax_common import ids_tensor if is_flax_available(): import jax import jax.numpy as jnp from transformers.generation import ( FlaxForcedBOSTokenLogitsProcessor, FlaxForcedEOSTokenLogitsProcessor, FlaxLogitsProcessorList, FlaxMinLengthLogitsProcessor, FlaxTemperatureLogitsWarper, FlaxTopKLogitsWarper, FlaxTopPLogitsWarper, ) @require_flax class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : int , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: str = jnp.ones((batch_size, length)) / length return scores def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Dict = None SCREAMING_SNAKE_CASE_: str = 20 SCREAMING_SNAKE_CASE_: List[Any] = self._get_uniform_logits(batch_size=2 , length=lowerCAmelCase__) # tweak scores to not be uniform anymore SCREAMING_SNAKE_CASE_: List[str] = scores.at[1, 5].set((1 / length) + 0.1) # peak, 1st batch SCREAMING_SNAKE_CASE_: Any = scores.at[1, 10].set((1 / length) - 0.4) # valley, 1st batch # compute softmax SCREAMING_SNAKE_CASE_: Dict = jax.nn.softmax(lowerCAmelCase__ , axis=-1) SCREAMING_SNAKE_CASE_: Optional[Any] = FlaxTemperatureLogitsWarper(temperature=0.5) SCREAMING_SNAKE_CASE_: List[str] = FlaxTemperatureLogitsWarper(temperature=1.3) SCREAMING_SNAKE_CASE_: str = jax.nn.softmax(temp_dist_warper_sharper(lowerCAmelCase__ , scores.copy() , cur_len=lowerCAmelCase__) , axis=-1) SCREAMING_SNAKE_CASE_: int = jax.nn.softmax(temp_dist_warper_smoother(lowerCAmelCase__ , scores.copy() , cur_len=lowerCAmelCase__) , axis=-1) # uniform distribution stays uniform self.assertTrue(jnp.allclose(probs[0, :] , warped_prob_sharp[0, :] , atol=1E-3)) self.assertTrue(jnp.allclose(probs[0, :] , warped_prob_smooth[0, :] , atol=1E-3)) # sharp peaks get higher, valleys get lower self.assertLess(probs[1, :].max() , warped_prob_sharp[1, :].max()) self.assertGreater(probs[1, :].min() , warped_prob_sharp[1, :].min()) # smooth peaks get lower, valleys get higher self.assertGreater(probs[1, :].max() , warped_prob_smooth[1, :].max()) self.assertLess(probs[1, :].min() , warped_prob_smooth[1, :].min()) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: List[str] = None SCREAMING_SNAKE_CASE_: str = 10 SCREAMING_SNAKE_CASE_: Tuple = 2 # create ramp distribution SCREAMING_SNAKE_CASE_: Optional[Any] = np.broadcast_to(np.arange(lowerCAmelCase__)[None, :] , (batch_size, vocab_size)).copy() SCREAMING_SNAKE_CASE_: Dict = ramp_logits[1:, : vocab_size // 2] + vocab_size SCREAMING_SNAKE_CASE_: Union[str, Any] = FlaxTopKLogitsWarper(3) SCREAMING_SNAKE_CASE_: Dict = top_k_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) # check that correct tokens are filtered self.assertListEqual(jnp.isinf(scores[0]).tolist() , 7 * [True] + 3 * [False]) self.assertListEqual(jnp.isinf(scores[1]).tolist() , 2 * [True] + 3 * [False] + 5 * [True]) # check special case SCREAMING_SNAKE_CASE_: Any = 5 SCREAMING_SNAKE_CASE_: str = FlaxTopKLogitsWarper(top_k=1 , filter_value=0.0 , min_tokens_to_keep=3) SCREAMING_SNAKE_CASE_: Any = np.broadcast_to(np.arange(lowerCAmelCase__)[None, :] , (batch_size, length)).copy() SCREAMING_SNAKE_CASE_: Any = top_k_warp_safety_check(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) # min_tokens overwrites k: 3 tokens are kept => 2 tokens are nullified self.assertListEqual((scores == 0.0).sum(axis=-1).tolist() , [2, 2]) def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Tuple = None SCREAMING_SNAKE_CASE_: Dict = 10 SCREAMING_SNAKE_CASE_: Dict = 2 # create distribution and take log (inverse to Softmax as taken in TopPLogitsWarper) SCREAMING_SNAKE_CASE_: Tuple = np.log(np.array([[0.3, 0.1, 0.1, 0.5], [0.15, 0.3, 0.3, 0.25]])) SCREAMING_SNAKE_CASE_: int = FlaxTopPLogitsWarper(0.8) SCREAMING_SNAKE_CASE_: Optional[Any] = np.exp(top_p_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__)) # dist should be filtered to keep min num values so that sum is >= top_p # exp (-inf) => 0 SCREAMING_SNAKE_CASE_: Dict = np.array([[0.3, 0.0, 0.0, 0.5], [0.0, 0.3, 0.3, 0.25]]) self.assertTrue(np.allclose(lowerCAmelCase__ , lowerCAmelCase__ , atol=1E-3)) # check edge cases with negative and extreme logits SCREAMING_SNAKE_CASE_: Union[str, Any] = np.broadcast_to(np.arange(lowerCAmelCase__)[None, :] , (batch_size, vocab_size)).copy() - ( vocab_size // 2 ) # make ramp_logits more extreme SCREAMING_SNAKE_CASE_: Dict = ramp_logits[1] * 100.0 # make sure at least 2 tokens are kept SCREAMING_SNAKE_CASE_: str = FlaxTopPLogitsWarper(0.9 , min_tokens_to_keep=2 , filter_value=0.0) SCREAMING_SNAKE_CASE_: Any = top_p_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) # first batch should keep three tokens, second batch would keep only 1, but due to `min_tokens_to_keep=2` keeps 2. self.assertListEqual((filtered_dist != 0.0).sum(axis=-1).tolist() , [3, 2]) def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Tuple = 20 SCREAMING_SNAKE_CASE_: List[str] = 4 SCREAMING_SNAKE_CASE_: Optional[int] = 0 SCREAMING_SNAKE_CASE_: str = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=lowerCAmelCase__) # check that min length is applied at length 5 SCREAMING_SNAKE_CASE_: str = ids_tensor((batch_size, 20) , vocab_size=20) SCREAMING_SNAKE_CASE_: int = 5 SCREAMING_SNAKE_CASE_: List[Any] = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = min_dist_processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) self.assertListEqual(scores_before_min_length[:, eos_token_id].tolist() , 4 * [-float("inf")]) # check that min length is not applied anymore at length 15 SCREAMING_SNAKE_CASE_: List[str] = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = 15 SCREAMING_SNAKE_CASE_: Any = min_dist_processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) self.assertFalse(jnp.isinf(lowerCAmelCase__).any()) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: int = 20 SCREAMING_SNAKE_CASE_: str = 4 SCREAMING_SNAKE_CASE_: List[Any] = 0 SCREAMING_SNAKE_CASE_: Optional[Any] = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=lowerCAmelCase__) # check that all scores are -inf except the bos_token_id score SCREAMING_SNAKE_CASE_: int = ids_tensor((batch_size, 1) , vocab_size=20) SCREAMING_SNAKE_CASE_: List[str] = 1 SCREAMING_SNAKE_CASE_: Union[str, Any] = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = logits_processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) self.assertTrue(jnp.isneginf(scores[:, bos_token_id + 1 :]).all()) self.assertListEqual(scores[:, bos_token_id].tolist() , 4 * [0]) # score for bos_token_id shold be zero # check that bos_token_id is not forced if current length is greater than 1 SCREAMING_SNAKE_CASE_: List[Any] = 3 SCREAMING_SNAKE_CASE_: Optional[Any] = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = logits_processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) self.assertFalse(jnp.isinf(lowerCAmelCase__).any()) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Any = 20 SCREAMING_SNAKE_CASE_: Optional[Any] = 4 SCREAMING_SNAKE_CASE_: Dict = 0 SCREAMING_SNAKE_CASE_: List[Any] = 5 SCREAMING_SNAKE_CASE_: Union[str, Any] = FlaxForcedEOSTokenLogitsProcessor(max_length=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__) # check that all scores are -inf except the eos_token_id when max_length is reached SCREAMING_SNAKE_CASE_: List[Any] = ids_tensor((batch_size, 4) , vocab_size=20) SCREAMING_SNAKE_CASE_: Optional[int] = 4 SCREAMING_SNAKE_CASE_: Dict = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = logits_processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) self.assertTrue(jnp.isneginf(scores[:, eos_token_id + 1 :]).all()) self.assertListEqual(scores[:, eos_token_id].tolist() , 4 * [0]) # score for eos_token_id should be zero # check that eos_token_id is not forced if max_length is not reached SCREAMING_SNAKE_CASE_: List[str] = 3 SCREAMING_SNAKE_CASE_: str = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = logits_processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) self.assertFalse(jnp.isinf(lowerCAmelCase__).any()) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: int = 4 SCREAMING_SNAKE_CASE_: List[Any] = 10 SCREAMING_SNAKE_CASE_: int = 15 SCREAMING_SNAKE_CASE_: Dict = 2 SCREAMING_SNAKE_CASE_: int = 1 SCREAMING_SNAKE_CASE_: List[Any] = 15 # dummy input_ids and scores SCREAMING_SNAKE_CASE_: int = ids_tensor((batch_size, sequence_length) , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = input_ids.copy() SCREAMING_SNAKE_CASE_: List[Any] = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = scores.copy() # instantiate all dist processors SCREAMING_SNAKE_CASE_: Optional[int] = FlaxTemperatureLogitsWarper(temperature=0.5) SCREAMING_SNAKE_CASE_: Tuple = FlaxTopKLogitsWarper(3) SCREAMING_SNAKE_CASE_: Optional[int] = FlaxTopPLogitsWarper(0.8) # instantiate all logits processors SCREAMING_SNAKE_CASE_: Optional[int] = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = FlaxForcedEOSTokenLogitsProcessor(max_length=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = 10 # no processor list SCREAMING_SNAKE_CASE_: Dict = temp_dist_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = top_k_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = top_p_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = min_dist_proc(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = bos_dist_proc(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = eos_dist_proc(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) # with processor list SCREAMING_SNAKE_CASE_: str = FlaxLogitsProcessorList( [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc]) SCREAMING_SNAKE_CASE_: Tuple = processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) # scores should be equal self.assertTrue(jnp.allclose(lowerCAmelCase__ , lowerCAmelCase__ , atol=1E-3)) # input_ids should never be changed self.assertListEqual(input_ids.tolist() , input_ids_comp.tolist()) def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_: Optional[int] = 4 SCREAMING_SNAKE_CASE_: int = 10 SCREAMING_SNAKE_CASE_: List[str] = 15 SCREAMING_SNAKE_CASE_: List[Any] = 2 SCREAMING_SNAKE_CASE_: Union[str, Any] = 1 SCREAMING_SNAKE_CASE_: str = 15 # dummy input_ids and scores SCREAMING_SNAKE_CASE_: Tuple = ids_tensor((batch_size, sequence_length) , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = input_ids.copy() SCREAMING_SNAKE_CASE_: List[Any] = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = scores.copy() # instantiate all dist processors SCREAMING_SNAKE_CASE_: Dict = FlaxTemperatureLogitsWarper(temperature=0.5) SCREAMING_SNAKE_CASE_: Union[str, Any] = FlaxTopKLogitsWarper(3) SCREAMING_SNAKE_CASE_: Dict = FlaxTopPLogitsWarper(0.8) # instantiate all logits processors SCREAMING_SNAKE_CASE_: int = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = FlaxForcedEOSTokenLogitsProcessor(max_length=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = 10 # no processor list def run_no_processor_list(lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Dict): SCREAMING_SNAKE_CASE_: Any = temp_dist_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = top_k_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = top_p_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = min_dist_proc(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = bos_dist_proc(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = eos_dist_proc(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) return scores # with processor list def run_processor_list(lowerCAmelCase__ : int , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Union[str, Any]): SCREAMING_SNAKE_CASE_: List[str] = FlaxLogitsProcessorList( [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc]) SCREAMING_SNAKE_CASE_: Dict = processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) return scores SCREAMING_SNAKE_CASE_: str = jax.jit(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = jax.jit(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = jitted_run_no_processor_list(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = jitted_run_processor_list(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) # scores should be equal self.assertTrue(jnp.allclose(lowerCAmelCase__ , lowerCAmelCase__ , atol=1E-3)) # input_ids should never be changed self.assertListEqual(input_ids.tolist() , input_ids_comp.tolist())
13
1
def A_ ( _UpperCAmelCase , _UpperCAmelCase = False ): if not isinstance(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: str = f"Expected string as input, found {type(_UpperCAmelCase )}" raise ValueError(_UpperCAmelCase ) if not isinstance(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Optional[Any] = f"Expected boolean as use_pascal parameter, found {type(_UpperCAmelCase )}" raise ValueError(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Tuple = input_str.split("_" ) SCREAMING_SNAKE_CASE_: str = 0 if use_pascal else 1 SCREAMING_SNAKE_CASE_: int = words[start_index:] SCREAMING_SNAKE_CASE_: List[str] = [word[0].upper() + word[1:] for word in words_to_capitalize] SCREAMING_SNAKE_CASE_: List[Any] = "" if use_pascal else words[0] return "".join([initial_word, *capitalized_words] ) if __name__ == "__main__": from doctest import testmod testmod()
13
import math import sys def A_ ( _UpperCAmelCase ): if number != int(_UpperCAmelCase ): raise ValueError("the value of input must be a natural number" ) if number < 0: raise ValueError("the value of input must not be a negative number" ) if number == 0: return 1 SCREAMING_SNAKE_CASE_: List[str] = [-1] * (number + 1) SCREAMING_SNAKE_CASE_: str = 0 for i in range(1 , number + 1 ): SCREAMING_SNAKE_CASE_: str = sys.maxsize SCREAMING_SNAKE_CASE_: List[Any] = int(math.sqrt(_UpperCAmelCase ) ) for j in range(1 , root + 1 ): SCREAMING_SNAKE_CASE_: List[str] = 1 + answers[i - (j**2)] SCREAMING_SNAKE_CASE_: Optional[Any] = min(_UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Dict = answer return answers[number] if __name__ == "__main__": import doctest doctest.testmod()
13
1
import shutil import tempfile import unittest import numpy as np import pytest from transformers.testing_utils import require_vision from transformers.utils import is_vision_available if is_vision_available(): from PIL import Image from transformers import ( AutoProcessor, BertTokenizerFast, BlipImageProcessor, GPTaTokenizer, InstructBlipProcessor, PreTrainedTokenizerFast, ) @require_vision class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: Dict = tempfile.mkdtemp() SCREAMING_SNAKE_CASE_: Optional[Any] = BlipImageProcessor() SCREAMING_SNAKE_CASE_: Tuple = GPTaTokenizer.from_pretrained("hf-internal-testing/tiny-random-GPT2Model") SCREAMING_SNAKE_CASE_: Union[str, Any] = BertTokenizerFast.from_pretrained("hf-internal-testing/tiny-random-bert") SCREAMING_SNAKE_CASE_: Tuple = InstructBlipProcessor(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) processor.save_pretrained(self.tmpdirname) def _SCREAMING_SNAKE_CASE ( self : Dict , **lowerCAmelCase__ : List[str]): return AutoProcessor.from_pretrained(self.tmpdirname , **lowerCAmelCase__).tokenizer def _SCREAMING_SNAKE_CASE ( self : Any , **lowerCAmelCase__ : Tuple): return AutoProcessor.from_pretrained(self.tmpdirname , **lowerCAmelCase__).image_processor def _SCREAMING_SNAKE_CASE ( self : Tuple , **lowerCAmelCase__ : Union[str, Any]): return AutoProcessor.from_pretrained(self.tmpdirname , **lowerCAmelCase__).qformer_tokenizer def _SCREAMING_SNAKE_CASE ( self : List[str]): shutil.rmtree(self.tmpdirname) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Dict = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta)] SCREAMING_SNAKE_CASE_: Any = [Image.fromarray(np.moveaxis(lowerCAmelCase__ , 0 , -1)) for x in image_inputs] return image_inputs def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Union[str, Any] = InstructBlipProcessor( tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() , qformer_tokenizer=self.get_qformer_tokenizer() , ) processor.save_pretrained(self.tmpdirname) SCREAMING_SNAKE_CASE_: Dict = self.get_tokenizer(bos_token="(BOS)" , eos_token="(EOS)") SCREAMING_SNAKE_CASE_: Tuple = self.get_image_processor(do_normalize=lowerCAmelCase__ , padding_value=1.0) SCREAMING_SNAKE_CASE_: List[Any] = InstructBlipProcessor.from_pretrained( self.tmpdirname , bos_token="(BOS)" , eos_token="(EOS)" , do_normalize=lowerCAmelCase__ , padding_value=1.0) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab()) self.assertIsInstance(processor.tokenizer , lowerCAmelCase__) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string()) self.assertIsInstance(processor.image_processor , lowerCAmelCase__) self.assertIsInstance(processor.qformer_tokenizer , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: Dict = self.get_image_processor() SCREAMING_SNAKE_CASE_: str = self.get_tokenizer() SCREAMING_SNAKE_CASE_: Union[str, Any] = self.get_qformer_tokenizer() SCREAMING_SNAKE_CASE_: Union[str, Any] = InstructBlipProcessor( tokenizer=lowerCAmelCase__ , image_processor=lowerCAmelCase__ , qformer_tokenizer=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = self.prepare_image_inputs() SCREAMING_SNAKE_CASE_: Tuple = image_processor(lowerCAmelCase__ , return_tensors="np") SCREAMING_SNAKE_CASE_: List[Any] = processor(images=lowerCAmelCase__ , return_tensors="np") for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1E-2) def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Any = self.get_image_processor() SCREAMING_SNAKE_CASE_: Dict = self.get_tokenizer() SCREAMING_SNAKE_CASE_: Union[str, Any] = self.get_qformer_tokenizer() SCREAMING_SNAKE_CASE_: int = InstructBlipProcessor( tokenizer=lowerCAmelCase__ , image_processor=lowerCAmelCase__ , qformer_tokenizer=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = "lower newer" SCREAMING_SNAKE_CASE_: Optional[int] = processor(text=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = tokenizer(lowerCAmelCase__ , return_token_type_ids=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = qformer_tokenizer(lowerCAmelCase__ , return_token_type_ids=lowerCAmelCase__) for key in encoded_tokens.keys(): self.assertListEqual(encoded_tokens[key] , encoded_processor[key]) for key in encoded_tokens_qformer.keys(): self.assertListEqual(encoded_tokens_qformer[key] , encoded_processor["qformer_" + key]) def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: Union[str, Any] = self.get_image_processor() SCREAMING_SNAKE_CASE_: Tuple = self.get_tokenizer() SCREAMING_SNAKE_CASE_: Dict = self.get_qformer_tokenizer() SCREAMING_SNAKE_CASE_: Tuple = InstructBlipProcessor( tokenizer=lowerCAmelCase__ , image_processor=lowerCAmelCase__ , qformer_tokenizer=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = "lower newer" SCREAMING_SNAKE_CASE_: Tuple = self.prepare_image_inputs() SCREAMING_SNAKE_CASE_: Dict = processor(text=lowerCAmelCase__ , images=lowerCAmelCase__) self.assertListEqual( list(inputs.keys()) , ["input_ids", "attention_mask", "qformer_input_ids", "qformer_attention_mask", "pixel_values"] , ) # test if it raises when no input is passed with pytest.raises(lowerCAmelCase__): processor() def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: Tuple = self.get_image_processor() SCREAMING_SNAKE_CASE_: List[str] = self.get_tokenizer() SCREAMING_SNAKE_CASE_: Optional[Any] = self.get_qformer_tokenizer() SCREAMING_SNAKE_CASE_: Any = InstructBlipProcessor( tokenizer=lowerCAmelCase__ , image_processor=lowerCAmelCase__ , qformer_tokenizer=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] SCREAMING_SNAKE_CASE_: str = processor.batch_decode(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = tokenizer.batch_decode(lowerCAmelCase__) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: List[Any] = self.get_image_processor() SCREAMING_SNAKE_CASE_: Tuple = self.get_tokenizer() SCREAMING_SNAKE_CASE_: Any = self.get_qformer_tokenizer() SCREAMING_SNAKE_CASE_: Tuple = InstructBlipProcessor( tokenizer=lowerCAmelCase__ , image_processor=lowerCAmelCase__ , qformer_tokenizer=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = "lower newer" SCREAMING_SNAKE_CASE_: Dict = self.prepare_image_inputs() SCREAMING_SNAKE_CASE_: int = processor(text=lowerCAmelCase__ , images=lowerCAmelCase__) self.assertListEqual( list(inputs.keys()) , ["input_ids", "attention_mask", "qformer_input_ids", "qformer_attention_mask", "pixel_values"] , )
13
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase : Optional[int] = {"""configuration_wavlm""": ["""WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """WavLMConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Any = [ """WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST""", """WavLMForAudioFrameClassification""", """WavLMForCTC""", """WavLMForSequenceClassification""", """WavLMForXVector""", """WavLMModel""", """WavLMPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_wavlm import WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP, WavLMConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_wavlm import ( WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST, WavLMForAudioFrameClassification, WavLMForCTC, WavLMForSequenceClassification, WavLMForXVector, WavLMModel, WavLMPreTrainedModel, ) else: import sys lowerCAmelCase : Union[str, Any] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
13
1
def A_ ( _UpperCAmelCase ): if any(not isinstance(_UpperCAmelCase , _UpperCAmelCase ) or x < 0 for x in sequence ): raise TypeError("Sequence must be list of non-negative integers" ) for _ in range(len(_UpperCAmelCase ) ): for i, (rod_upper, rod_lower) in enumerate(zip(_UpperCAmelCase , sequence[1:] ) ): if rod_upper > rod_lower: sequence[i] -= rod_upper - rod_lower sequence[i + 1] += rod_upper - rod_lower return sequence if __name__ == "__main__": assert bead_sort([5, 4, 3, 2, 1]) == [1, 2, 3, 4, 5] assert bead_sort([7, 9, 4, 3, 5]) == [3, 4, 5, 7, 9]
13
import unittest from transformers import ( MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TextaTextGenerationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, require_tf, require_torch from transformers.utils import is_torch_available from .test_pipelines_common import ANY if is_torch_available(): import torch @is_pipeline_test class __lowercase ( unittest.TestCase ): """simple docstring""" _UpperCAmelCase : List[Any] = MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING _UpperCAmelCase : str = TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING def _SCREAMING_SNAKE_CASE ( self : Any , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Dict): SCREAMING_SNAKE_CASE_: Any = TextaTextGenerationPipeline(model=lowerCAmelCase__ , tokenizer=lowerCAmelCase__) return generator, ["Something to write", "Something else"] def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Any): SCREAMING_SNAKE_CASE_: List[Any] = generator("Something there") self.assertEqual(lowerCAmelCase__ , [{"generated_text": ANY(lowerCAmelCase__)}]) # These are encoder decoder, they don't just append to incoming string self.assertFalse(outputs[0]["generated_text"].startswith("Something there")) SCREAMING_SNAKE_CASE_: List[Any] = generator(["This is great !", "Something else"] , num_return_sequences=2 , do_sample=lowerCAmelCase__) self.assertEqual( lowerCAmelCase__ , [ [{"generated_text": ANY(lowerCAmelCase__)}, {"generated_text": ANY(lowerCAmelCase__)}], [{"generated_text": ANY(lowerCAmelCase__)}, {"generated_text": ANY(lowerCAmelCase__)}], ] , ) SCREAMING_SNAKE_CASE_: Dict = generator( ["This is great !", "Something else"] , num_return_sequences=2 , batch_size=2 , do_sample=lowerCAmelCase__) self.assertEqual( lowerCAmelCase__ , [ [{"generated_text": ANY(lowerCAmelCase__)}, {"generated_text": ANY(lowerCAmelCase__)}], [{"generated_text": ANY(lowerCAmelCase__)}, {"generated_text": ANY(lowerCAmelCase__)}], ] , ) with self.assertRaises(lowerCAmelCase__): generator(4) @require_torch def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Optional[int] = pipeline("text2text-generation" , model="patrickvonplaten/t5-tiny-random" , framework="pt") # do_sample=False necessary for reproducibility SCREAMING_SNAKE_CASE_: Union[str, Any] = generator("Something there" , do_sample=lowerCAmelCase__) self.assertEqual(lowerCAmelCase__ , [{"generated_text": ""}]) SCREAMING_SNAKE_CASE_: Union[str, Any] = 3 SCREAMING_SNAKE_CASE_: Any = generator( "Something there" , num_return_sequences=lowerCAmelCase__ , num_beams=lowerCAmelCase__ , ) SCREAMING_SNAKE_CASE_: Any = [ {"generated_text": "Beide Beide Beide Beide Beide Beide Beide Beide Beide"}, {"generated_text": "Beide Beide Beide Beide Beide Beide Beide Beide"}, {"generated_text": ""}, ] self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = generator("This is a test" , do_sample=lowerCAmelCase__ , num_return_sequences=2 , return_tensors=lowerCAmelCase__) self.assertEqual( lowerCAmelCase__ , [ {"generated_token_ids": ANY(torch.Tensor)}, {"generated_token_ids": ANY(torch.Tensor)}, ] , ) SCREAMING_SNAKE_CASE_: str = generator.model.config.eos_token_id SCREAMING_SNAKE_CASE_: Union[str, Any] = "<pad>" SCREAMING_SNAKE_CASE_: Tuple = generator( ["This is a test", "This is a second test"] , do_sample=lowerCAmelCase__ , num_return_sequences=2 , batch_size=2 , return_tensors=lowerCAmelCase__ , ) self.assertEqual( lowerCAmelCase__ , [ [ {"generated_token_ids": ANY(torch.Tensor)}, {"generated_token_ids": ANY(torch.Tensor)}, ], [ {"generated_token_ids": ANY(torch.Tensor)}, {"generated_token_ids": ANY(torch.Tensor)}, ], ] , ) @require_tf def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = pipeline("text2text-generation" , model="patrickvonplaten/t5-tiny-random" , framework="tf") # do_sample=False necessary for reproducibility SCREAMING_SNAKE_CASE_: List[Any] = generator("Something there" , do_sample=lowerCAmelCase__) self.assertEqual(lowerCAmelCase__ , [{"generated_text": ""}])
13
1
from typing import Optional, Union import torch from torch import nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention from ...modeling_utils import PreTrainedModel from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging from .configuration_mobilenet_va import MobileNetVaConfig lowerCAmelCase : int = logging.get_logger(__name__) # General docstring lowerCAmelCase : int = """MobileNetV1Config""" # Base docstring lowerCAmelCase : List[Any] = """google/mobilenet_v1_1.0_224""" lowerCAmelCase : Dict = [1, 1024, 7, 7] # Image classification docstring lowerCAmelCase : Union[str, Any] = """google/mobilenet_v1_1.0_224""" lowerCAmelCase : Any = """tabby, tabby cat""" lowerCAmelCase : List[Any] = [ """google/mobilenet_v1_1.0_224""", """google/mobilenet_v1_0.75_192""", # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 ] def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=None ): SCREAMING_SNAKE_CASE_: List[str] = {} if isinstance(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = model.mobilenet_va else: SCREAMING_SNAKE_CASE_: int = model SCREAMING_SNAKE_CASE_: Dict = "MobilenetV1/Conv2d_0/" SCREAMING_SNAKE_CASE_: str = backbone.conv_stem.convolution.weight SCREAMING_SNAKE_CASE_: List[str] = backbone.conv_stem.normalization.bias SCREAMING_SNAKE_CASE_: int = backbone.conv_stem.normalization.weight SCREAMING_SNAKE_CASE_: List[str] = backbone.conv_stem.normalization.running_mean SCREAMING_SNAKE_CASE_: Optional[int] = backbone.conv_stem.normalization.running_var for i in range(13 ): SCREAMING_SNAKE_CASE_: List[str] = i + 1 SCREAMING_SNAKE_CASE_: Optional[int] = i * 2 SCREAMING_SNAKE_CASE_: Any = backbone.layer[pt_index] SCREAMING_SNAKE_CASE_: Any = f"MobilenetV1/Conv2d_{tf_index}_depthwise/" SCREAMING_SNAKE_CASE_: Any = pointer.convolution.weight SCREAMING_SNAKE_CASE_: Any = pointer.normalization.bias SCREAMING_SNAKE_CASE_: str = pointer.normalization.weight SCREAMING_SNAKE_CASE_: Dict = pointer.normalization.running_mean SCREAMING_SNAKE_CASE_: Optional[Any] = pointer.normalization.running_var SCREAMING_SNAKE_CASE_: Tuple = backbone.layer[pt_index + 1] SCREAMING_SNAKE_CASE_: List[str] = f"MobilenetV1/Conv2d_{tf_index}_pointwise/" SCREAMING_SNAKE_CASE_: int = pointer.convolution.weight SCREAMING_SNAKE_CASE_: Any = pointer.normalization.bias SCREAMING_SNAKE_CASE_: Optional[int] = pointer.normalization.weight SCREAMING_SNAKE_CASE_: Optional[Any] = pointer.normalization.running_mean SCREAMING_SNAKE_CASE_: Dict = pointer.normalization.running_var if isinstance(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: List[str] = "MobilenetV1/Logits/Conv2d_1c_1x1/" SCREAMING_SNAKE_CASE_: Optional[Any] = model.classifier.weight SCREAMING_SNAKE_CASE_: Tuple = model.classifier.bias return tf_to_pt_map def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): try: import numpy as np import tensorflow as tf except ImportError: logger.error( "Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see " "https://www.tensorflow.org/install/ for installation instructions." ) raise # Load weights from TF model SCREAMING_SNAKE_CASE_: int = tf.train.list_variables(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: int = {} for name, shape in init_vars: logger.info(f"Loading TF weight {name} with shape {shape}" ) SCREAMING_SNAKE_CASE_: Any = tf.train.load_variable(_UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Union[str, Any] = array # Build TF to PyTorch weights loading map SCREAMING_SNAKE_CASE_: Optional[Any] = _build_tf_to_pytorch_map(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) for name, pointer in tf_to_pt_map.items(): logger.info(f"Importing {name}" ) if name not in tf_weights: logger.info(f"{name} not in tf pre-trained weights, skipping" ) continue SCREAMING_SNAKE_CASE_: int = tf_weights[name] if "depthwise_weights" in name: logger.info("Transposing depthwise" ) SCREAMING_SNAKE_CASE_: int = np.transpose(_UpperCAmelCase , (2, 3, 0, 1) ) elif "weights" in name: logger.info("Transposing" ) if len(pointer.shape ) == 2: # copying into linear layer SCREAMING_SNAKE_CASE_: List[str] = array.squeeze().transpose() else: SCREAMING_SNAKE_CASE_: Any = np.transpose(_UpperCAmelCase , (3, 2, 0, 1) ) if pointer.shape != array.shape: raise ValueError(f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched" ) logger.info(f"Initialize PyTorch weight {name} {array.shape}" ) SCREAMING_SNAKE_CASE_: int = torch.from_numpy(_UpperCAmelCase ) tf_weights.pop(_UpperCAmelCase , _UpperCAmelCase ) tf_weights.pop(name + "/RMSProp" , _UpperCAmelCase ) tf_weights.pop(name + "/RMSProp_1" , _UpperCAmelCase ) tf_weights.pop(name + "/ExponentialMovingAverage" , _UpperCAmelCase ) logger.info(f"Weights not copied to PyTorch model: {', '.join(tf_weights.keys() )}" ) return model def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = features.shape[-2:] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = conv_layer.stride SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = conv_layer.kernel_size if in_height % stride_height == 0: SCREAMING_SNAKE_CASE_: int = max(kernel_height - stride_height , 0 ) else: SCREAMING_SNAKE_CASE_: Tuple = max(kernel_height - (in_height % stride_height) , 0 ) if in_width % stride_width == 0: SCREAMING_SNAKE_CASE_: str = max(kernel_width - stride_width , 0 ) else: SCREAMING_SNAKE_CASE_: Dict = max(kernel_width - (in_width % stride_width) , 0 ) SCREAMING_SNAKE_CASE_: str = pad_along_width // 2 SCREAMING_SNAKE_CASE_: Union[str, Any] = pad_along_width - pad_left SCREAMING_SNAKE_CASE_: int = pad_along_height // 2 SCREAMING_SNAKE_CASE_: Tuple = pad_along_height - pad_top SCREAMING_SNAKE_CASE_: Union[str, Any] = (pad_left, pad_right, pad_top, pad_bottom) return nn.functional.pad(_UpperCAmelCase , _UpperCAmelCase , "constant" , 0.0 ) class __lowercase ( nn.Module ): """simple docstring""" def __init__( self : Optional[int] , lowerCAmelCase__ : MobileNetVaConfig , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : Optional[int] = 1 , lowerCAmelCase__ : Optional[int] = 1 , lowerCAmelCase__ : bool = False , lowerCAmelCase__ : Optional[bool] = True , lowerCAmelCase__ : Optional[bool or str] = True , ): super().__init__() SCREAMING_SNAKE_CASE_: Optional[int] = config if in_channels % groups != 0: raise ValueError(F"Input channels ({in_channels}) are not divisible by {groups} groups.") if out_channels % groups != 0: raise ValueError(F"Output channels ({out_channels}) are not divisible by {groups} groups.") SCREAMING_SNAKE_CASE_: int = 0 if config.tf_padding else int((kernel_size - 1) / 2) SCREAMING_SNAKE_CASE_: Union[str, Any] = nn.Convad( in_channels=lowerCAmelCase__ , out_channels=lowerCAmelCase__ , kernel_size=lowerCAmelCase__ , stride=lowerCAmelCase__ , padding=lowerCAmelCase__ , groups=lowerCAmelCase__ , bias=lowerCAmelCase__ , padding_mode="zeros" , ) if use_normalization: SCREAMING_SNAKE_CASE_: str = nn.BatchNormad( num_features=lowerCAmelCase__ , eps=config.layer_norm_eps , momentum=0.9997 , affine=lowerCAmelCase__ , track_running_stats=lowerCAmelCase__ , ) else: SCREAMING_SNAKE_CASE_: str = None if use_activation: if isinstance(lowerCAmelCase__ , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Dict = ACTaFN[use_activation] elif isinstance(config.hidden_act , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Dict = ACTaFN[config.hidden_act] else: SCREAMING_SNAKE_CASE_: Any = config.hidden_act else: SCREAMING_SNAKE_CASE_: int = None def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : torch.Tensor): if self.config.tf_padding: SCREAMING_SNAKE_CASE_: Union[str, Any] = apply_tf_padding(lowerCAmelCase__ , self.convolution) SCREAMING_SNAKE_CASE_: Optional[int] = self.convolution(lowerCAmelCase__) if self.normalization is not None: SCREAMING_SNAKE_CASE_: int = self.normalization(lowerCAmelCase__) if self.activation is not None: SCREAMING_SNAKE_CASE_: List[Any] = self.activation(lowerCAmelCase__) return features class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : List[str] = MobileNetVaConfig _UpperCAmelCase : List[Any] = load_tf_weights_in_mobilenet_va _UpperCAmelCase : List[Any] = '''mobilenet_v1''' _UpperCAmelCase : int = '''pixel_values''' _UpperCAmelCase : List[Any] = False def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : Union[nn.Linear, nn.Convad]): if isinstance(lowerCAmelCase__ , (nn.Linear, nn.Convad)): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range) if module.bias is not None: module.bias.data.zero_() elif isinstance(lowerCAmelCase__ , nn.BatchNormad): module.bias.data.zero_() module.weight.data.fill_(1.0) lowerCAmelCase : Any = R""" This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior. Parameters: config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights. """ lowerCAmelCase : List[str] = R""" Args: pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`MobileNetV1ImageProcessor.__call__`] for details. output_hidden_states (`bool`, *optional*): Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for more detail. return_dict (`bool`, *optional*): Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. """ @add_start_docstrings( '''The bare MobileNetV1 model outputting raw hidden-states without any specific head on top.''' , UpperCAmelCase_ , ) class __lowercase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Dict , lowerCAmelCase__ : MobileNetVaConfig , lowerCAmelCase__ : bool = True): super().__init__(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = config SCREAMING_SNAKE_CASE_: Union[str, Any] = 32 SCREAMING_SNAKE_CASE_: Dict = max(int(depth * config.depth_multiplier) , config.min_depth) SCREAMING_SNAKE_CASE_: Tuple = MobileNetVaConvLayer( lowerCAmelCase__ , in_channels=config.num_channels , out_channels=lowerCAmelCase__ , kernel_size=3 , stride=2 , ) SCREAMING_SNAKE_CASE_: Optional[int] = [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1] SCREAMING_SNAKE_CASE_: str = nn.ModuleList() for i in range(13): SCREAMING_SNAKE_CASE_: List[Any] = out_channels if strides[i] == 2 or i == 0: depth *= 2 SCREAMING_SNAKE_CASE_: str = max(int(depth * config.depth_multiplier) , config.min_depth) self.layer.append( MobileNetVaConvLayer( lowerCAmelCase__ , in_channels=lowerCAmelCase__ , out_channels=lowerCAmelCase__ , kernel_size=3 , stride=strides[i] , groups=lowerCAmelCase__ , )) self.layer.append( MobileNetVaConvLayer( lowerCAmelCase__ , in_channels=lowerCAmelCase__ , out_channels=lowerCAmelCase__ , kernel_size=1 , )) SCREAMING_SNAKE_CASE_: List[str] = nn.AdaptiveAvgPoolad((1, 1)) if add_pooling_layer else None # Initialize weights and apply final processing self.post_init() def _SCREAMING_SNAKE_CASE ( self : int , lowerCAmelCase__ : str): raise NotImplementedError @add_start_docstrings_to_model_forward(lowerCAmelCase__) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=lowerCAmelCase__ , config_class=_CONFIG_FOR_DOC , modality="vision" , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : Optional[torch.Tensor] = None , lowerCAmelCase__ : Optional[bool] = None , lowerCAmelCase__ : Optional[bool] = None , ): SCREAMING_SNAKE_CASE_: Optional[int] = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) SCREAMING_SNAKE_CASE_: Any = return_dict if return_dict is not None else self.config.use_return_dict if pixel_values is None: raise ValueError("You have to specify pixel_values") SCREAMING_SNAKE_CASE_: Optional[Any] = self.conv_stem(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = () if output_hidden_states else None for i, layer_module in enumerate(self.layer): SCREAMING_SNAKE_CASE_: Tuple = layer_module(lowerCAmelCase__) if output_hidden_states: SCREAMING_SNAKE_CASE_: Optional[int] = all_hidden_states + (hidden_states,) SCREAMING_SNAKE_CASE_: Optional[Any] = hidden_states if self.pooler is not None: SCREAMING_SNAKE_CASE_: int = torch.flatten(self.pooler(lowerCAmelCase__) , start_dim=1) else: SCREAMING_SNAKE_CASE_: List[str] = None if not return_dict: return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None) return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=lowerCAmelCase__ , pooler_output=lowerCAmelCase__ , hidden_states=lowerCAmelCase__ , ) @add_start_docstrings( ''' MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. ''' , UpperCAmelCase_ , ) class __lowercase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Any , lowerCAmelCase__ : MobileNetVaConfig): super().__init__(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = config.num_labels SCREAMING_SNAKE_CASE_: Dict = MobileNetVaModel(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = self.mobilenet_va.layer[-1].convolution.out_channels # Classifier head SCREAMING_SNAKE_CASE_: str = nn.Dropout(config.classifier_dropout_prob , inplace=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = nn.Linear(lowerCAmelCase__ , config.num_labels) if config.num_labels > 0 else nn.Identity() # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(lowerCAmelCase__) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=lowerCAmelCase__ , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : Optional[torch.Tensor] = None , lowerCAmelCase__ : Optional[bool] = None , lowerCAmelCase__ : Optional[torch.Tensor] = None , lowerCAmelCase__ : Optional[bool] = None , ): SCREAMING_SNAKE_CASE_: List[str] = return_dict if return_dict is not None else self.config.use_return_dict SCREAMING_SNAKE_CASE_: List[str] = self.mobilenet_va(lowerCAmelCase__ , output_hidden_states=lowerCAmelCase__ , return_dict=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = outputs.pooler_output if return_dict else outputs[1] SCREAMING_SNAKE_CASE_: Tuple = self.classifier(self.dropout(lowerCAmelCase__)) SCREAMING_SNAKE_CASE_: Optional[int] = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: SCREAMING_SNAKE_CASE_: List[Any] = "regression" elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): SCREAMING_SNAKE_CASE_: int = "single_label_classification" else: SCREAMING_SNAKE_CASE_: str = "multi_label_classification" if self.config.problem_type == "regression": SCREAMING_SNAKE_CASE_: Dict = MSELoss() if self.num_labels == 1: SCREAMING_SNAKE_CASE_: Any = loss_fct(logits.squeeze() , labels.squeeze()) else: SCREAMING_SNAKE_CASE_: int = loss_fct(lowerCAmelCase__ , lowerCAmelCase__) elif self.config.problem_type == "single_label_classification": SCREAMING_SNAKE_CASE_: Any = CrossEntropyLoss() SCREAMING_SNAKE_CASE_: Dict = loss_fct(logits.view(-1 , self.num_labels) , labels.view(-1)) elif self.config.problem_type == "multi_label_classification": SCREAMING_SNAKE_CASE_: Dict = BCEWithLogitsLoss() SCREAMING_SNAKE_CASE_: Dict = loss_fct(lowerCAmelCase__ , lowerCAmelCase__) if not return_dict: SCREAMING_SNAKE_CASE_: int = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return ImageClassifierOutputWithNoAttention( loss=lowerCAmelCase__ , logits=lowerCAmelCase__ , hidden_states=outputs.hidden_states , )
13
def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: List[str] = [0] * len(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[Any] = [] SCREAMING_SNAKE_CASE_: str = [] SCREAMING_SNAKE_CASE_: List[str] = 0 for values in graph.values(): for i in values: indegree[i] += 1 for i in range(len(_UpperCAmelCase ) ): if indegree[i] == 0: queue.append(_UpperCAmelCase ) while queue: SCREAMING_SNAKE_CASE_: Optional[int] = queue.pop(0 ) cnt += 1 topo.append(_UpperCAmelCase ) for x in graph[vertex]: indegree[x] -= 1 if indegree[x] == 0: queue.append(_UpperCAmelCase ) if cnt != len(_UpperCAmelCase ): print("Cycle exists" ) else: print(_UpperCAmelCase ) # Adjacency List of Graph lowerCAmelCase : Any = {0: [1, 2], 1: [3], 2: [3], 3: [4, 5], 4: [], 5: []} topological_sort(graph)
13
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) if is_sentencepiece_available(): from ..ta.tokenization_ta import TaTokenizer else: from ...utils.dummy_sentencepiece_objects import TaTokenizer lowerCAmelCase : Any = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast lowerCAmelCase : Dict = TaTokenizerFast lowerCAmelCase : Optional[int] = {"""configuration_mt5""": ["""MT5Config""", """MT5OnnxConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : int = [ """MT5EncoderModel""", """MT5ForConditionalGeneration""", """MT5ForQuestionAnswering""", """MT5Model""", """MT5PreTrainedModel""", """MT5Stack""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Tuple = ["""TFMT5EncoderModel""", """TFMT5ForConditionalGeneration""", """TFMT5Model"""] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : List[str] = ["""FlaxMT5EncoderModel""", """FlaxMT5ForConditionalGeneration""", """FlaxMT5Model"""] if TYPE_CHECKING: from .configuration_mta import MTaConfig, MTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mta import ( MTaEncoderModel, MTaForConditionalGeneration, MTaForQuestionAnswering, MTaModel, MTaPreTrainedModel, MTaStack, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel else: import sys lowerCAmelCase : Optional[Any] = _LazyModule( __name__, globals()["""__file__"""], _import_structure, extra_objects={"""MT5Tokenizer""": MTaTokenizer, """MT5TokenizerFast""": MTaTokenizerFast}, module_spec=__spec__, )
13
import argparse import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## lowerCAmelCase : Optional[Any] = 16 lowerCAmelCase : List[str] = 32 def A_ ( _UpperCAmelCase , _UpperCAmelCase = 16 ): SCREAMING_SNAKE_CASE_: Tuple = AutoTokenizer.from_pretrained("bert-base-cased" ) SCREAMING_SNAKE_CASE_: List[Any] = load_dataset("glue" , "mrpc" ) def tokenize_function(_UpperCAmelCase ): # max_length=None => use the model max length (it's actually the default) SCREAMING_SNAKE_CASE_: Any = tokenizer(examples["sentence1"] , examples["sentence2"] , truncation=_UpperCAmelCase , max_length=_UpperCAmelCase ) 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(): SCREAMING_SNAKE_CASE_: Tuple = datasets.map( _UpperCAmelCase , batched=_UpperCAmelCase , 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 SCREAMING_SNAKE_CASE_: Union[str, Any] = tokenized_datasets.rename_column("label" , "labels" ) def collate_fn(_UpperCAmelCase ): # On TPU it's best to pad everything to the same length or training will be very slow. SCREAMING_SNAKE_CASE_: List[str] = 1_28 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": SCREAMING_SNAKE_CASE_: List[Any] = 16 elif accelerator.mixed_precision != "no": SCREAMING_SNAKE_CASE_: Optional[Any] = 8 else: SCREAMING_SNAKE_CASE_: List[str] = None return tokenizer.pad( _UpperCAmelCase , padding="longest" , max_length=_UpperCAmelCase , pad_to_multiple_of=_UpperCAmelCase , return_tensors="pt" , ) # Instantiate dataloaders. SCREAMING_SNAKE_CASE_: Dict = DataLoader( tokenized_datasets["train"] , shuffle=_UpperCAmelCase , collate_fn=_UpperCAmelCase , batch_size=_UpperCAmelCase , drop_last=_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Union[str, Any] = DataLoader( tokenized_datasets["validation"] , shuffle=_UpperCAmelCase , collate_fn=_UpperCAmelCase , batch_size=_UpperCAmelCase , drop_last=(accelerator.mixed_precision == "fp8") , ) return train_dataloader, eval_dataloader def A_ ( _UpperCAmelCase , _UpperCAmelCase ): # Initialize accelerator SCREAMING_SNAKE_CASE_: str = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs SCREAMING_SNAKE_CASE_: int = config["lr"] SCREAMING_SNAKE_CASE_: Any = int(config["num_epochs"] ) SCREAMING_SNAKE_CASE_: Optional[int] = int(config["seed"] ) SCREAMING_SNAKE_CASE_: List[Any] = int(config["batch_size"] ) SCREAMING_SNAKE_CASE_: List[str] = evaluate.load("glue" , "mrpc" ) # If the batch size is too big we use gradient accumulation SCREAMING_SNAKE_CASE_: Optional[int] = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: SCREAMING_SNAKE_CASE_: Tuple = batch_size // MAX_GPU_BATCH_SIZE SCREAMING_SNAKE_CASE_: Dict = MAX_GPU_BATCH_SIZE set_seed(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = get_dataloaders(_UpperCAmelCase , _UpperCAmelCase ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) SCREAMING_SNAKE_CASE_: List[Any] = AutoModelForSequenceClassification.from_pretrained("bert-base-cased" , return_dict=_UpperCAmelCase ) # 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). SCREAMING_SNAKE_CASE_: Tuple = model.to(accelerator.device ) # Instantiate optimizer SCREAMING_SNAKE_CASE_: Optional[int] = AdamW(params=model.parameters() , lr=_UpperCAmelCase ) # Instantiate scheduler SCREAMING_SNAKE_CASE_: Optional[int] = get_linear_schedule_with_warmup( optimizer=_UpperCAmelCase , num_warmup_steps=1_00 , num_training_steps=(len(_UpperCAmelCase ) * num_epochs) // gradient_accumulation_steps , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = accelerator.prepare( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) # Now we train the model for epoch in range(_UpperCAmelCase ): model.train() for step, batch in enumerate(_UpperCAmelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) SCREAMING_SNAKE_CASE_: Tuple = model(**_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Optional[int] = outputs.loss SCREAMING_SNAKE_CASE_: Tuple = loss / gradient_accumulation_steps accelerator.backward(_UpperCAmelCase ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(_UpperCAmelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): SCREAMING_SNAKE_CASE_: Optional[int] = model(**_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: int = outputs.logits.argmax(dim=-1 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = accelerator.gather_for_metrics((predictions, batch["labels"]) ) metric.add_batch( predictions=_UpperCAmelCase , references=_UpperCAmelCase , ) SCREAMING_SNAKE_CASE_: List[Any] = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f"epoch {epoch}:" , _UpperCAmelCase ) def A_ ( ): SCREAMING_SNAKE_CASE_: Any = argparse.ArgumentParser(description="Simple example of training script." ) parser.add_argument( "--mixed_precision" , type=_UpperCAmelCase , default=_UpperCAmelCase , 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." ) SCREAMING_SNAKE_CASE_: Optional[Any] = parser.parse_args() SCREAMING_SNAKE_CASE_: Optional[int] = {"lr": 2e-5, "num_epochs": 3, "seed": 42, "batch_size": 16} training_function(_UpperCAmelCase , _UpperCAmelCase ) if __name__ == "__main__": main()
13
1
import pytest import datasets # Import fixture modules as plugins lowerCAmelCase : str = ["""tests.fixtures.files""", """tests.fixtures.hub""", """tests.fixtures.fsspec"""] def A_ ( _UpperCAmelCase , _UpperCAmelCase ): # Mark tests as "unit" by default if not marked as "integration" (or already marked as "unit") for item in items: if any(marker in item.keywords for marker in ["integration", "unit"] ): continue item.add_marker(pytest.mark.unit ) def A_ ( _UpperCAmelCase ): config.addinivalue_line("markers" , "torchaudio_latest: mark test to run with torchaudio>=0.12" ) @pytest.fixture(autouse=_UpperCAmelCase ) def A_ ( _UpperCAmelCase , _UpperCAmelCase ): # test_hf_cache_home = tmp_path_factory.mktemp("cache") # TODO: why a cache dir per test function does not work? SCREAMING_SNAKE_CASE_: List[Any] = tmp_path_factory.getbasetemp() / "cache" SCREAMING_SNAKE_CASE_: Union[str, Any] = test_hf_cache_home / "datasets" SCREAMING_SNAKE_CASE_: Tuple = test_hf_cache_home / "metrics" SCREAMING_SNAKE_CASE_: Union[str, Any] = test_hf_cache_home / "modules" monkeypatch.setattr("datasets.config.HF_DATASETS_CACHE" , str(_UpperCAmelCase ) ) monkeypatch.setattr("datasets.config.HF_METRICS_CACHE" , str(_UpperCAmelCase ) ) monkeypatch.setattr("datasets.config.HF_MODULES_CACHE" , str(_UpperCAmelCase ) ) SCREAMING_SNAKE_CASE_: int = test_hf_datasets_cache / "downloads" monkeypatch.setattr("datasets.config.DOWNLOADED_DATASETS_PATH" , str(_UpperCAmelCase ) ) SCREAMING_SNAKE_CASE_: Optional[int] = test_hf_datasets_cache / "downloads" / "extracted" monkeypatch.setattr("datasets.config.EXTRACTED_DATASETS_PATH" , str(_UpperCAmelCase ) ) @pytest.fixture(autouse=_UpperCAmelCase , scope="session" ) def A_ ( ): datasets.disable_progress_bar() @pytest.fixture(autouse=_UpperCAmelCase ) def A_ ( _UpperCAmelCase ): # don't take tests into account when counting downloads monkeypatch.setattr("datasets.config.HF_UPDATE_DOWNLOAD_COUNTS" , _UpperCAmelCase ) @pytest.fixture def A_ ( _UpperCAmelCase ): # Required to suppress RemovedIn20Warning when feature(s) are not compatible with SQLAlchemy 2.0 # To be removed once SQLAlchemy 2.0 supported monkeypatch.setattr("sqlalchemy.util.deprecations.SILENCE_UBER_WARNING" , _UpperCAmelCase )
13
from collections.abc import Callable class __lowercase : """simple docstring""" def __init__( self : Tuple , lowerCAmelCase__ : Callable | None = None): # Stores actual heap items. SCREAMING_SNAKE_CASE_: list = [] # Stores indexes of each item for supporting updates and deletion. SCREAMING_SNAKE_CASE_: dict = {} # Stores current size of heap. SCREAMING_SNAKE_CASE_: Optional[Any] = 0 # Stores function used to evaluate the score of an item on which basis ordering # will be done. SCREAMING_SNAKE_CASE_: Any = key or (lambda lowerCAmelCase__: x) def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : int): return int((i - 1) / 2) if i > 0 else None def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Union[str, Any] = int(2 * i + 1) return left if 0 < left < self.size else None def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Union[str, Any] = int(2 * i + 2) return right if 0 < right < self.size else None def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : int , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = ( self.pos_map[self.arr[j][0]], self.pos_map[self.arr[i][0]], ) # Then swap the items in the list. SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = self.arr[j], self.arr[i] def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : int , lowerCAmelCase__ : int): return self.arr[i][1] < self.arr[j][1] def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Any = self._left(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = self._right(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = i if left is not None and not self._cmp(lowerCAmelCase__ , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Optional[int] = left if right is not None and not self._cmp(lowerCAmelCase__ , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Tuple = right return valid_parent def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: List[Any] = self._parent(lowerCAmelCase__) while parent is not None and not self._cmp(lowerCAmelCase__ , lowerCAmelCase__): self._swap(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = parent, self._parent(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Optional[int] = self._get_valid_parent(lowerCAmelCase__) while valid_parent != index: self._swap(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = valid_parent, self._get_valid_parent(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : int , lowerCAmelCase__ : int): if item not in self.pos_map: return SCREAMING_SNAKE_CASE_: Any = self.pos_map[item] SCREAMING_SNAKE_CASE_: int = [item, self.key(lowerCAmelCase__)] # Make sure heap is right in both up and down direction. # Ideally only one of them will make any change. self._heapify_up(lowerCAmelCase__) self._heapify_down(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : int): if item not in self.pos_map: return SCREAMING_SNAKE_CASE_: Optional[Any] = self.pos_map[item] del self.pos_map[item] SCREAMING_SNAKE_CASE_: List[str] = self.arr[self.size - 1] SCREAMING_SNAKE_CASE_: Tuple = index self.size -= 1 # Make sure heap is right in both up and down direction. Ideally only one # of them will make any change- so no performance loss in calling both. if self.size > index: self._heapify_up(lowerCAmelCase__) self._heapify_down(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Optional[int] = len(self.arr) if arr_len == self.size: self.arr.append([item, self.key(lowerCAmelCase__)]) else: SCREAMING_SNAKE_CASE_: str = [item, self.key(lowerCAmelCase__)] SCREAMING_SNAKE_CASE_: List[Any] = self.size self.size += 1 self._heapify_up(self.size - 1) def _SCREAMING_SNAKE_CASE ( self : List[Any]): return self.arr[0] if self.size else None def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Dict = self.get_top() if top_item_tuple: self.delete_item(top_item_tuple[0]) return top_item_tuple def A_ ( ): pass if __name__ == "__main__": import doctest doctest.testmod()
13
1
from .testing import ( are_the_same_tensors, execute_subprocess_async, require_bnb, require_cpu, require_cuda, require_huggingface_suite, require_mps, require_multi_gpu, require_multi_xpu, require_safetensors, require_single_gpu, require_single_xpu, require_torch_min_version, require_tpu, require_xpu, skip, slow, ) from .training import RegressionDataset, RegressionModel, RegressionModelaXPU from .scripts import test_script, test_sync, test_ops # isort: skip
13
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) if is_sentencepiece_available(): from ..ta.tokenization_ta import TaTokenizer else: from ...utils.dummy_sentencepiece_objects import TaTokenizer lowerCAmelCase : Any = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast lowerCAmelCase : Dict = TaTokenizerFast lowerCAmelCase : Optional[int] = {"""configuration_mt5""": ["""MT5Config""", """MT5OnnxConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : int = [ """MT5EncoderModel""", """MT5ForConditionalGeneration""", """MT5ForQuestionAnswering""", """MT5Model""", """MT5PreTrainedModel""", """MT5Stack""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Tuple = ["""TFMT5EncoderModel""", """TFMT5ForConditionalGeneration""", """TFMT5Model"""] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : List[str] = ["""FlaxMT5EncoderModel""", """FlaxMT5ForConditionalGeneration""", """FlaxMT5Model"""] if TYPE_CHECKING: from .configuration_mta import MTaConfig, MTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mta import ( MTaEncoderModel, MTaForConditionalGeneration, MTaForQuestionAnswering, MTaModel, MTaPreTrainedModel, MTaStack, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel else: import sys lowerCAmelCase : Optional[Any] = _LazyModule( __name__, globals()["""__file__"""], _import_structure, extra_objects={"""MT5Tokenizer""": MTaTokenizer, """MT5TokenizerFast""": MTaTokenizerFast}, module_spec=__spec__, )
13
1
from __future__ import annotations import os from typing import Any import requests lowerCAmelCase : Tuple = """https://api.github.com""" # https://docs.github.com/en/free-pro-team@latest/rest/reference/users#get-the-authenticated-user lowerCAmelCase : Union[str, Any] = BASE_URL + """/user""" # https://github.com/settings/tokens lowerCAmelCase : Dict = os.environ.get("""USER_TOKEN""", """""") def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Tuple = { "Authorization": f"token {auth_token}", "Accept": "application/vnd.github.v3+json", } return requests.get(_UpperCAmelCase , headers=_UpperCAmelCase ).json() if __name__ == "__main__": # pragma: no cover if USER_TOKEN: for key, value in fetch_github_info(USER_TOKEN).items(): print(f'''{key}: {value}''') else: raise ValueError("""'USER_TOKEN' field cannot be empty.""")
13
import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEPipeline from diffusers.pipelines.shap_e import ShapERenderer from diffusers.utils import 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 __lowercase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : List[str] = ShapEPipeline _UpperCAmelCase : Tuple = ['''prompt'''] _UpperCAmelCase : Dict = ['''prompt'''] _UpperCAmelCase : Any = [ '''num_images_per_prompt''', '''num_inference_steps''', '''generator''', '''latents''', '''guidance_scale''', '''frame_size''', '''output_type''', '''return_dict''', ] _UpperCAmelCase : Optional[int] = False @property def _SCREAMING_SNAKE_CASE ( self : List[str]): return 32 @property def _SCREAMING_SNAKE_CASE ( self : List[str]): return 32 @property def _SCREAMING_SNAKE_CASE ( self : int): return self.time_input_dim * 4 @property def _SCREAMING_SNAKE_CASE ( self : Optional[int]): return 8 @property def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: str = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip") return tokenizer @property def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Optional[int] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) return CLIPTextModelWithProjection(lowerCAmelCase__) @property def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Tuple = { "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", "encoder_hid_proj_type": None, "added_emb_type": None, } SCREAMING_SNAKE_CASE_: Any = PriorTransformer(**lowerCAmelCase__) return model @property def _SCREAMING_SNAKE_CASE ( self : Dict): torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Union[str, Any] = { "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, ), } SCREAMING_SNAKE_CASE_: Optional[int] = ShapERenderer(**lowerCAmelCase__) return model def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Dict = self.dummy_prior SCREAMING_SNAKE_CASE_: Optional[Any] = self.dummy_text_encoder SCREAMING_SNAKE_CASE_: Union[str, Any] = self.dummy_tokenizer SCREAMING_SNAKE_CASE_: List[str] = self.dummy_renderer SCREAMING_SNAKE_CASE_: Any = HeunDiscreteScheduler( beta_schedule="exp" , num_train_timesteps=1024 , prediction_type="sample" , use_karras_sigmas=lowerCAmelCase__ , clip_sample=lowerCAmelCase__ , clip_sample_range=1.0 , ) SCREAMING_SNAKE_CASE_: Optional[int] = { "prior": prior, "text_encoder": text_encoder, "tokenizer": tokenizer, "renderer": renderer, "scheduler": scheduler, } return components def _SCREAMING_SNAKE_CASE ( self : Any , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[Any]=0): if str(lowerCAmelCase__).startswith("mps"): SCREAMING_SNAKE_CASE_: Optional[Any] = torch.manual_seed(lowerCAmelCase__) else: SCREAMING_SNAKE_CASE_: Any = torch.Generator(device=lowerCAmelCase__).manual_seed(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = { "prompt": "horse", "generator": generator, "num_inference_steps": 1, "frame_size": 32, "output_type": "np", } return inputs def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: str = "cpu" SCREAMING_SNAKE_CASE_: Tuple = self.get_dummy_components() SCREAMING_SNAKE_CASE_: Dict = self.pipeline_class(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = pipe(**self.get_dummy_inputs(lowerCAmelCase__)) SCREAMING_SNAKE_CASE_: Optional[Any] = output.images[0] SCREAMING_SNAKE_CASE_: Any = image[0, -3:, -3:, -1] assert image.shape == (20, 32, 32, 3) SCREAMING_SNAKE_CASE_: Union[str, Any] = np.array( [ 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, ]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-2 def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches self._test_inference_batch_consistent(batch_sizes=[1, 2]) def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_: Dict = torch_device == "cpu" SCREAMING_SNAKE_CASE_: List[Any] = True self._test_inference_batch_single_identical( batch_size=2 , test_max_difference=lowerCAmelCase__ , relax_max_difference=lowerCAmelCase__ , ) def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Dict = self.get_dummy_components() SCREAMING_SNAKE_CASE_: str = self.pipeline_class(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = 1 SCREAMING_SNAKE_CASE_: Any = 2 SCREAMING_SNAKE_CASE_: Dict = self.get_dummy_inputs(lowerCAmelCase__) for key in inputs.keys(): if key in self.batch_params: SCREAMING_SNAKE_CASE_: List[Any] = batch_size * [inputs[key]] SCREAMING_SNAKE_CASE_: Tuple = pipe(**lowerCAmelCase__ , num_images_per_prompt=lowerCAmelCase__)[0] assert images.shape[0] == batch_size * num_images_per_prompt @slow @require_torch_gpu class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : Any): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: List[str] = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/shap_e/test_shap_e_np_out.npy") SCREAMING_SNAKE_CASE_: List[str] = ShapEPipeline.from_pretrained("openai/shap-e") SCREAMING_SNAKE_CASE_: Optional[int] = pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = torch.Generator(device=lowerCAmelCase__).manual_seed(0) SCREAMING_SNAKE_CASE_: int = pipe( "a shark" , generator=lowerCAmelCase__ , guidance_scale=15.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__)
13
1
def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Union[str, Any] = 0 SCREAMING_SNAKE_CASE_: Any = len(_UpperCAmelCase ) for i in range(n - 1 ): for j in range(i + 1 , _UpperCAmelCase ): if arr[i] > arr[j]: num_inversions += 1 return num_inversions def A_ ( _UpperCAmelCase ): if len(_UpperCAmelCase ) <= 1: return arr, 0 SCREAMING_SNAKE_CASE_: str = len(_UpperCAmelCase ) // 2 SCREAMING_SNAKE_CASE_: List[str] = arr[0:mid] SCREAMING_SNAKE_CASE_: Dict = arr[mid:] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = count_inversions_recursive(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = count_inversions_recursive(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = _count_cross_inversions(_UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[str] = inversion_p + inversions_q + cross_inversions return c, num_inversions def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Dict = [] SCREAMING_SNAKE_CASE_: List[Any] = 0 while i < len(_UpperCAmelCase ) and j < len(_UpperCAmelCase ): if p[i] > q[j]: # if P[1] > Q[j], then P[k] > Q[k] for all i < k <= len(P) # These are all inversions. The claim emerges from the # property that P is sorted. num_inversion += len(_UpperCAmelCase ) - i r.append(q[j] ) j += 1 else: r.append(p[i] ) i += 1 if i < len(_UpperCAmelCase ): r.extend(p[i:] ) else: r.extend(q[j:] ) return r, num_inversion def A_ ( ): SCREAMING_SNAKE_CASE_: Union[str, Any] = [10, 2, 1, 5, 5, 2, 11] # this arr has 8 inversions: # (10, 2), (10, 1), (10, 5), (10, 5), (10, 2), (2, 1), (5, 2), (5, 2) SCREAMING_SNAKE_CASE_: Optional[int] = count_inversions_bf(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = count_inversions_recursive(_UpperCAmelCase ) assert num_inversions_bf == num_inversions_recursive == 8 print("number of inversions = " , _UpperCAmelCase ) # testing an array with zero inversion (a sorted arr_1) arr_a.sort() SCREAMING_SNAKE_CASE_: str = count_inversions_bf(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = count_inversions_recursive(_UpperCAmelCase ) assert num_inversions_bf == num_inversions_recursive == 0 print("number of inversions = " , _UpperCAmelCase ) # an empty list should also have zero inversions SCREAMING_SNAKE_CASE_: List[Any] = [] SCREAMING_SNAKE_CASE_: Union[str, Any] = count_inversions_bf(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = count_inversions_recursive(_UpperCAmelCase ) assert num_inversions_bf == num_inversions_recursive == 0 print("number of inversions = " , _UpperCAmelCase ) if __name__ == "__main__": main()
13
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
13
1
def A_ ( _UpperCAmelCase , _UpperCAmelCase ): return [sentence[i : i + ngram_size] for i in range(len(_UpperCAmelCase ) - ngram_size + 1 )] if __name__ == "__main__": from doctest import testmod testmod()
13
class __lowercase : """simple docstring""" def __init__( self : List[Any] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[Any]): SCREAMING_SNAKE_CASE_: List[str] = name SCREAMING_SNAKE_CASE_: Union[str, Any] = val def __str__( self : Dict): return F"{self.__class__.__name__}({self.name}, {self.val})" def __lt__( self : List[str] , lowerCAmelCase__ : Any): return self.val < other.val class __lowercase : """simple docstring""" def __init__( self : Tuple , lowerCAmelCase__ : Dict): SCREAMING_SNAKE_CASE_: str = {} SCREAMING_SNAKE_CASE_: int = {} SCREAMING_SNAKE_CASE_: Any = self.build_heap(lowerCAmelCase__) def __getitem__( self : List[Any] , lowerCAmelCase__ : Dict): return self.get_value(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : Dict): return (idx - 1) // 2 def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Optional[Any]): return idx * 2 + 1 def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Tuple): return idx * 2 + 2 def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Optional[int]): return self.heap_dict[key] def _SCREAMING_SNAKE_CASE ( self : List[Any] , lowerCAmelCase__ : Union[str, Any]): SCREAMING_SNAKE_CASE_: Tuple = len(lowerCAmelCase__) - 1 SCREAMING_SNAKE_CASE_: List[str] = self.get_parent_idx(lowerCAmelCase__) for idx, i in enumerate(lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Union[str, Any] = idx SCREAMING_SNAKE_CASE_: str = i.val for i in range(lowerCAmelCase__ , -1 , -1): self.sift_down(lowerCAmelCase__ , lowerCAmelCase__) return array def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[str]): while True: SCREAMING_SNAKE_CASE_: Optional[Any] = self.get_left_child_idx(lowerCAmelCase__) # noqa: E741 SCREAMING_SNAKE_CASE_: Dict = self.get_right_child_idx(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = idx if l < len(lowerCAmelCase__) and array[l] < array[idx]: SCREAMING_SNAKE_CASE_: List[str] = l if r < len(lowerCAmelCase__) and array[r] < array[smallest]: SCREAMING_SNAKE_CASE_: str = r if smallest != idx: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = array[smallest], array[idx] ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ): Optional[Any] = ( self.idx_of_element[array[smallest]], self.idx_of_element[array[idx]], ) SCREAMING_SNAKE_CASE_: Optional[int] = smallest else: break def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : str): SCREAMING_SNAKE_CASE_: Any = self.get_parent_idx(lowerCAmelCase__) while p >= 0 and self.heap[p] > self.heap[idx]: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = self.heap[idx], self.heap[p] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = ( self.idx_of_element[self.heap[idx]], self.idx_of_element[self.heap[p]], ) SCREAMING_SNAKE_CASE_: Union[str, Any] = p SCREAMING_SNAKE_CASE_: Optional[int] = self.get_parent_idx(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[Any]): return self.heap[0] def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self.heap[-1], self.heap[0] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = ( self.idx_of_element[self.heap[-1]], self.idx_of_element[self.heap[0]], ) SCREAMING_SNAKE_CASE_: int = self.heap.pop() del self.idx_of_element[x] self.sift_down(0 , self.heap) return x def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Tuple): self.heap.append(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = len(self.heap) - 1 SCREAMING_SNAKE_CASE_: List[str] = node.val self.sift_up(len(self.heap) - 1) def _SCREAMING_SNAKE_CASE ( self : List[Any]): return len(self.heap) == 0 def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Optional[int]): assert ( self.heap[self.idx_of_element[node]].val > new_value ), "newValue must be less that current value" SCREAMING_SNAKE_CASE_: Any = new_value SCREAMING_SNAKE_CASE_: Tuple = new_value self.sift_up(self.idx_of_element[node]) lowerCAmelCase : int = Node("""R""", -1) lowerCAmelCase : str = Node("""B""", 6) lowerCAmelCase : str = Node("""A""", 3) lowerCAmelCase : List[str] = Node("""X""", 1) lowerCAmelCase : Union[str, Any] = Node("""E""", 4) # Use one of these two ways to generate Min-Heap # Generating Min-Heap from array lowerCAmelCase : Optional[Any] = MinHeap([r, b, a, x, e]) # Generating Min-Heap by Insert method # myMinHeap.insert(a) # myMinHeap.insert(b) # myMinHeap.insert(x) # myMinHeap.insert(r) # myMinHeap.insert(e) # Before print("""Min Heap - before decrease key""") for i in my_min_heap.heap: print(i) print("""Min Heap - After decrease key of node [B -> -17]""") my_min_heap.decrease_key(b, -17) # After for i in my_min_heap.heap: print(i) if __name__ == "__main__": import doctest doctest.testmod()
13
1
import copy import os import tempfile from unittest import TestCase from unittest.mock import patch import numpy as np import pyarrow as pa import pyarrow.parquet as pq import pytest from datasets.arrow_writer import ArrowWriter, OptimizedTypedSequence, ParquetWriter, TypedSequence from datasets.features import ArrayaD, ClassLabel, Features, Image, Value from datasets.features.features import ArrayaDExtensionType, cast_to_python_objects from datasets.keyhash import DuplicatedKeysError, InvalidKeyError from .utils import require_pil class __lowercase ( UpperCAmelCase_ ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Optional[Any] = pa.array(TypedSequence([1, 2, 3])) self.assertEqual(arr.type , pa.intaa()) def _SCREAMING_SNAKE_CASE ( self : str): with self.assertRaises(lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Optional[int] = pa.array(TypedSequence([1, 2, 3]) , type=pa.intaa()) def _SCREAMING_SNAKE_CASE ( self : Dict): with self.assertRaises(lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Dict = pa.array(TypedSequence([1, 2, 3] , try_type=Value("bool") , type=Value("int64"))) def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: Dict = pa.array(TypedSequence([1, 2, 3] , type=Value("int32"))) self.assertEqual(arr.type , pa.intaa()) def _SCREAMING_SNAKE_CASE ( self : str): with self.assertRaises((TypeError, pa.lib.ArrowInvalid)): SCREAMING_SNAKE_CASE_: List[str] = pa.array(TypedSequence(["foo", "bar"] , type=Value("int64"))) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Optional[int] = pa.array(TypedSequence([1, 2, 3] , try_type=Value("int32"))) self.assertEqual(arr.type , pa.intaa()) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: Dict = pa.array(TypedSequence(["foo", "bar"] , try_type=Value("int64"))) self.assertEqual(arr.type , pa.string()) def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: int = pa.array(TypedSequence([[[1, 2, 3]]] , type=ArrayaD((1, 3) , "int64"))) self.assertEqual(arr.type , ArrayaDExtensionType((1, 3) , "int64")) def _SCREAMING_SNAKE_CASE ( self : str): with self.assertRaises((TypeError, pa.lib.ArrowInvalid)): SCREAMING_SNAKE_CASE_: List[str] = pa.array(TypedSequence(["foo", "bar"] , type=ArrayaD((1, 3) , "int64"))) def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_: Optional[int] = pa.array(TypedSequence([[[1, 2, 3]]] , try_type=ArrayaD((1, 3) , "int64"))) self.assertEqual(arr.type , ArrayaDExtensionType((1, 3) , "int64")) def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_: List[Any] = pa.array(TypedSequence(["foo", "bar"] , try_type=ArrayaD((1, 3) , "int64"))) self.assertEqual(arr.type , pa.string()) @require_pil def _SCREAMING_SNAKE_CASE ( self : str): import PIL.Image SCREAMING_SNAKE_CASE_: Tuple = PIL.Image.fromarray(np.arange(10 , dtype=np.uinta).reshape(2 , 5)) with patch( "datasets.arrow_writer.cast_to_python_objects" , side_effect=lowerCAmelCase__) as mock_cast_to_python_objects: SCREAMING_SNAKE_CASE_: Tuple = pa.array(TypedSequence([{"path": None, "bytes": b"image_bytes"}, pil_image] , type=Image())) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = mock_cast_to_python_objects.call_args_list[-1] self.assertIn("optimize_list_casting" , lowerCAmelCase__) self.assertFalse(kwargs["optimize_list_casting"]) def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Dict = pa.BufferReader(_UpperCAmelCase ) if isinstance(_UpperCAmelCase , pa.Buffer ) else pa.memory_map(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Optional[int] = pa.ipc.open_stream(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: pa.Table = f.read_all() assert len(pa_table.to_batches() ) == expected_num_chunks assert pa_table.to_pydict() == {"col_1": ["foo", "bar"], "col_2": [1, 2]} del pa_table @pytest.mark.parametrize("writer_batch_size" , [None, 1, 10] ) @pytest.mark.parametrize( "fields" , [None, {"col_1": pa.string(), "col_2": pa.intaa()}, {"col_1": pa.string(), "col_2": pa.intaa()}] ) def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Optional[int] = pa.BufferOutputStream() SCREAMING_SNAKE_CASE_: Optional[Any] = pa.schema(_UpperCAmelCase ) if fields else None with ArrowWriter(stream=_UpperCAmelCase , schema=_UpperCAmelCase , writer_batch_size=_UpperCAmelCase ) as writer: writer.write({"col_1": "foo", "col_2": 1} ) writer.write({"col_1": "bar", "col_2": 2} ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = writer.finalize() assert num_examples == 2 assert num_bytes > 0 if not fields: SCREAMING_SNAKE_CASE_: str = {"col_1": pa.string(), "col_2": pa.intaa()} assert writer._schema == pa.schema(_UpperCAmelCase , metadata=writer._schema.metadata ) _check_output(output.getvalue() , expected_num_chunks=num_examples if writer_batch_size == 1 else 1 ) def A_ ( ): SCREAMING_SNAKE_CASE_: Optional[Any] = pa.BufferOutputStream() SCREAMING_SNAKE_CASE_: int = Features({"labels": ClassLabel(names=["neg", "pos"] )} ) with ArrowWriter(stream=_UpperCAmelCase , features=_UpperCAmelCase ) as writer: writer.write({"labels": 0} ) writer.write({"labels": 1} ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = writer.finalize() assert num_examples == 2 assert num_bytes > 0 assert writer._schema == features.arrow_schema assert writer._schema.metadata == features.arrow_schema.metadata SCREAMING_SNAKE_CASE_: Any = pa.BufferReader(output.getvalue() ) SCREAMING_SNAKE_CASE_: Optional[int] = pa.ipc.open_stream(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: pa.Table = f.read_all() SCREAMING_SNAKE_CASE_: str = pa_table.schema assert pa_table.num_rows == 2 assert schema == features.arrow_schema assert schema.metadata == features.arrow_schema.metadata assert features == Features.from_arrow_schema(_UpperCAmelCase ) @pytest.mark.parametrize("writer_batch_size" , [None, 1, 10] ) def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: str = pa.BufferOutputStream() with ArrowWriter( stream=_UpperCAmelCase , writer_batch_size=_UpperCAmelCase , hash_salt="split_name" , check_duplicates=_UpperCAmelCase , ) as writer: with pytest.raises(_UpperCAmelCase ): writer.write({"col_1": "foo", "col_2": 1} , key=[1, 2] ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = writer.finalize() @pytest.mark.parametrize("writer_batch_size" , [None, 2, 10] ) def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Tuple = pa.BufferOutputStream() with ArrowWriter( stream=_UpperCAmelCase , writer_batch_size=_UpperCAmelCase , hash_salt="split_name" , check_duplicates=_UpperCAmelCase , ) as writer: with pytest.raises(_UpperCAmelCase ): writer.write({"col_1": "foo", "col_2": 1} , key=10 ) writer.write({"col_1": "bar", "col_2": 2} , key=10 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = writer.finalize() @pytest.mark.parametrize("writer_batch_size" , [None, 2, 10] ) def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: int = pa.BufferOutputStream() with ArrowWriter( stream=_UpperCAmelCase , writer_batch_size=_UpperCAmelCase , hash_salt="split_name" , check_duplicates=_UpperCAmelCase , ) as writer: writer.write({"col_1": "foo", "col_2": 1} , key=1 ) writer.write({"col_1": "bar", "col_2": 2} , key=2 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = writer.finalize() assert num_examples == 2 assert num_bytes > 0 _check_output(output.getvalue() , expected_num_chunks=num_examples if writer_batch_size == 1 else 1 ) @pytest.mark.parametrize("writer_batch_size" , [None, 1, 10] ) @pytest.mark.parametrize( "fields" , [None, {"col_1": pa.string(), "col_2": pa.intaa()}, {"col_1": pa.string(), "col_2": pa.intaa()}] ) def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Tuple = pa.BufferOutputStream() SCREAMING_SNAKE_CASE_: List[Any] = pa.schema(_UpperCAmelCase ) if fields else None with ArrowWriter(stream=_UpperCAmelCase , schema=_UpperCAmelCase , writer_batch_size=_UpperCAmelCase ) as writer: writer.write_batch({"col_1": ["foo", "bar"], "col_2": [1, 2]} ) writer.write_batch({"col_1": [], "col_2": []} ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = writer.finalize() assert num_examples == 2 assert num_bytes > 0 if not fields: SCREAMING_SNAKE_CASE_: List[str] = {"col_1": pa.string(), "col_2": pa.intaa()} assert writer._schema == pa.schema(_UpperCAmelCase , metadata=writer._schema.metadata ) _check_output(output.getvalue() , expected_num_chunks=num_examples if writer_batch_size == 1 else 1 ) @pytest.mark.parametrize("writer_batch_size" , [None, 1, 10] ) @pytest.mark.parametrize( "fields" , [None, {"col_1": pa.string(), "col_2": pa.intaa()}, {"col_1": pa.string(), "col_2": pa.intaa()}] ) def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = pa.BufferOutputStream() SCREAMING_SNAKE_CASE_: str = pa.schema(_UpperCAmelCase ) if fields else None with ArrowWriter(stream=_UpperCAmelCase , schema=_UpperCAmelCase , writer_batch_size=_UpperCAmelCase ) as writer: writer.write_table(pa.Table.from_pydict({"col_1": ["foo", "bar"], "col_2": [1, 2]} ) ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = writer.finalize() assert num_examples == 2 assert num_bytes > 0 if not fields: SCREAMING_SNAKE_CASE_: Dict = {"col_1": pa.string(), "col_2": pa.intaa()} assert writer._schema == pa.schema(_UpperCAmelCase , metadata=writer._schema.metadata ) _check_output(output.getvalue() , expected_num_chunks=num_examples if writer_batch_size == 1 else 1 ) @pytest.mark.parametrize("writer_batch_size" , [None, 1, 10] ) @pytest.mark.parametrize( "fields" , [None, {"col_1": pa.string(), "col_2": pa.intaa()}, {"col_1": pa.string(), "col_2": pa.intaa()}] ) def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Tuple = pa.BufferOutputStream() SCREAMING_SNAKE_CASE_: Any = pa.schema(_UpperCAmelCase ) if fields else None with ArrowWriter(stream=_UpperCAmelCase , schema=_UpperCAmelCase , writer_batch_size=_UpperCAmelCase ) as writer: writer.write_row(pa.Table.from_pydict({"col_1": ["foo"], "col_2": [1]} ) ) writer.write_row(pa.Table.from_pydict({"col_1": ["bar"], "col_2": [2]} ) ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = writer.finalize() assert num_examples == 2 assert num_bytes > 0 if not fields: SCREAMING_SNAKE_CASE_: List[str] = {"col_1": pa.string(), "col_2": pa.intaa()} assert writer._schema == pa.schema(_UpperCAmelCase , metadata=writer._schema.metadata ) _check_output(output.getvalue() , expected_num_chunks=num_examples if writer_batch_size == 1 else 1 ) def A_ ( ): with tempfile.TemporaryDirectory() as tmp_dir: SCREAMING_SNAKE_CASE_: Optional[int] = {"col_1": pa.string(), "col_2": pa.intaa()} SCREAMING_SNAKE_CASE_: Any = os.path.join(_UpperCAmelCase , "test.arrow" ) with ArrowWriter(path=_UpperCAmelCase , schema=pa.schema(_UpperCAmelCase ) ) as writer: writer.write_batch({"col_1": ["foo", "bar"], "col_2": [1, 2]} ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = writer.finalize() assert num_examples == 2 assert num_bytes > 0 assert writer._schema == pa.schema(_UpperCAmelCase , metadata=writer._schema.metadata ) _check_output(_UpperCAmelCase , 1 ) def A_ ( _UpperCAmelCase ): if pa.types.is_list(_UpperCAmelCase ): return get_base_dtype(arr_type.value_type ) else: return arr_type def A_ ( _UpperCAmelCase , _UpperCAmelCase ): if isinstance(lst[0] , _UpperCAmelCase ): change_first_primitive_element_in_list(lst[0] , _UpperCAmelCase ) else: SCREAMING_SNAKE_CASE_: Optional[int] = value @pytest.mark.parametrize("optimized_int_type, expected_dtype" , [(None, pa.intaa()), (Value("int32" ), pa.intaa())] ) @pytest.mark.parametrize("sequence" , [[1, 2, 3], [[1, 2, 3]], [[[1, 2, 3]]]] ) def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Tuple = pa.array(TypedSequence(_UpperCAmelCase , optimized_int_type=_UpperCAmelCase ) ) assert get_base_dtype(arr.type ) == expected_dtype @pytest.mark.parametrize( "col, expected_dtype" , [ ("attention_mask", pa.inta()), ("special_tokens_mask", pa.inta()), ("token_type_ids", pa.inta()), ("input_ids", pa.intaa()), ("other", pa.intaa()), ] , ) @pytest.mark.parametrize("sequence" , [[1, 2, 3], [[1, 2, 3]], [[[1, 2, 3]]]] ) def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): # in range SCREAMING_SNAKE_CASE_: List[str] = pa.array(OptimizedTypedSequence(_UpperCAmelCase , col=_UpperCAmelCase ) ) assert get_base_dtype(arr.type ) == expected_dtype # not in range if col != "other": # avoids errors due to in-place modifications SCREAMING_SNAKE_CASE_: Optional[int] = copy.deepcopy(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: str = np.iinfo(expected_dtype.to_pandas_dtype() ).max + 1 change_first_primitive_element_in_list(_UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: int = pa.array(OptimizedTypedSequence(_UpperCAmelCase , col=_UpperCAmelCase ) ) assert get_base_dtype(arr.type ) == pa.intaa() @pytest.mark.parametrize("raise_exception" , [False, True] ) def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = str(tmp_path / "dataset-train.arrow" ) try: with ArrowWriter(path=_UpperCAmelCase ) as writer: if raise_exception: raise pa.lib.ArrowInvalid() else: writer.stream.close() except pa.lib.ArrowInvalid: pass finally: assert writer.stream.closed def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: str = "mock://dataset-train.arrow" with ArrowWriter(path=_UpperCAmelCase , storage_options=mockfs.storage_options ) as writer: assert isinstance(writer._fs , type(_UpperCAmelCase ) ) assert writer._fs.storage_options == mockfs.storage_options writer.write({"col_1": "foo", "col_2": 1} ) writer.write({"col_1": "bar", "col_2": 2} ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = writer.finalize() assert num_examples == 2 assert num_bytes > 0 assert mockfs.exists(_UpperCAmelCase ) def A_ ( ): SCREAMING_SNAKE_CASE_: List[str] = pa.BufferOutputStream() with ParquetWriter(stream=_UpperCAmelCase ) as writer: writer.write({"col_1": "foo", "col_2": 1} ) writer.write({"col_1": "bar", "col_2": 2} ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = writer.finalize() assert num_examples == 2 assert num_bytes > 0 SCREAMING_SNAKE_CASE_: int = pa.BufferReader(output.getvalue() ) SCREAMING_SNAKE_CASE_: pa.Table = pq.read_table(_UpperCAmelCase ) assert pa_table.to_pydict() == {"col_1": ["foo", "bar"], "col_2": [1, 2]} @require_pil @pytest.mark.parametrize("embed_local_files" , [False, True] ) def A_ ( _UpperCAmelCase , _UpperCAmelCase ): import PIL.Image SCREAMING_SNAKE_CASE_: Any = str(tmp_path / "test_image_rgb.jpg" ) PIL.Image.fromarray(np.zeros((5, 5) , dtype=np.uinta ) ).save(_UpperCAmelCase , format="png" ) SCREAMING_SNAKE_CASE_: Any = pa.BufferOutputStream() with ParquetWriter( stream=_UpperCAmelCase , features=Features({"image": Image()} ) , embed_local_files=_UpperCAmelCase ) as writer: writer.write({"image": image_path} ) writer.finalize() SCREAMING_SNAKE_CASE_: Union[str, Any] = pa.BufferReader(output.getvalue() ) SCREAMING_SNAKE_CASE_: pa.Table = pq.read_table(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: int = pa_table.to_pydict() if embed_local_files: assert isinstance(out["image"][0]["path"] , _UpperCAmelCase ) with open(_UpperCAmelCase , "rb" ) as f: assert out["image"][0]["bytes"] == f.read() else: assert out["image"][0]["path"] == image_path assert out["image"][0]["bytes"] is None def A_ ( ): SCREAMING_SNAKE_CASE_: Dict = pa.schema([pa.field("col_1" , pa.string() , nullable=_UpperCAmelCase )] ) SCREAMING_SNAKE_CASE_: int = pa.BufferOutputStream() with ArrowWriter(stream=_UpperCAmelCase ) as writer: writer._build_writer(inferred_schema=_UpperCAmelCase ) assert writer._schema == pa.schema([pa.field("col_1" , pa.string() )] )
13
import random import unittest import numpy as np import transformers from transformers import is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax if is_flax_available(): import os import jax.numpy as jnp from jax import jit from transformers import AutoTokenizer, FlaxAutoModelForCausalLM from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model lowerCAmelCase : Any = """0.12""" # assumed parallelism: 8 if is_torch_available(): import torch def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=None ): if rng is None: SCREAMING_SNAKE_CASE_: List[Any] = random.Random() SCREAMING_SNAKE_CASE_: Optional[Any] = 1 for dim in shape: total_dims *= dim SCREAMING_SNAKE_CASE_: Optional[Any] = [] for _ in range(_UpperCAmelCase ): values.append(rng.randint(0 , vocab_size - 1 ) ) SCREAMING_SNAKE_CASE_: List[Any] = np.array(_UpperCAmelCase , dtype=jnp.intaa ).reshape(_UpperCAmelCase ) return output def A_ ( _UpperCAmelCase , _UpperCAmelCase=None ): SCREAMING_SNAKE_CASE_: Optional[int] = ids_tensor(_UpperCAmelCase , vocab_size=2 , rng=_UpperCAmelCase ) # make sure that at least one token is attended to for each batch SCREAMING_SNAKE_CASE_: Optional[Any] = 1 return attn_mask @require_flax class __lowercase : """simple docstring""" _UpperCAmelCase : Any = None _UpperCAmelCase : List[Any] = () def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self.model_tester.prepare_config_and_inputs_for_common() # cut to half length & take max batch_size 3 SCREAMING_SNAKE_CASE_: str = 2 SCREAMING_SNAKE_CASE_: Optional[int] = inputs["input_ids"].shape[-1] // 2 SCREAMING_SNAKE_CASE_: List[str] = inputs["input_ids"][:max_batch_size, :sequence_length] SCREAMING_SNAKE_CASE_: Any = jnp.ones_like(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = attention_mask[:max_batch_size, :sequence_length] # generate max 5 tokens SCREAMING_SNAKE_CASE_: Optional[Any] = input_ids.shape[-1] + 5 if config.eos_token_id is not None and config.pad_token_id is None: # hack to allow generate for models such as GPT2 as is done in `generate()` SCREAMING_SNAKE_CASE_: Optional[Any] = config.eos_token_id return config, input_ids, attention_mask, max_length @is_pt_flax_cross_test def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Union[str, Any] = False SCREAMING_SNAKE_CASE_: Dict = max_length SCREAMING_SNAKE_CASE_: List[Any] = 0 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: int = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = model_class.__name__[4:] # Skip the "Flax" at the beginning SCREAMING_SNAKE_CASE_: List[Any] = getattr(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = pt_model_class(lowerCAmelCase__).eval() SCREAMING_SNAKE_CASE_: str = load_flax_weights_in_pytorch_model(lowerCAmelCase__ , flax_model.params) SCREAMING_SNAKE_CASE_: List[Any] = flax_model.generate(lowerCAmelCase__).sequences SCREAMING_SNAKE_CASE_: str = pt_model.generate(torch.tensor(lowerCAmelCase__ , dtype=torch.long)) if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]: SCREAMING_SNAKE_CASE_: List[Any] = flax_generation_outputs[:, : pt_generation_outputs.shape[-1]] self.assertListEqual(pt_generation_outputs.numpy().tolist() , flax_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Optional[int] = False SCREAMING_SNAKE_CASE_: Optional[int] = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Union[str, Any] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = jit(model.generate) SCREAMING_SNAKE_CASE_: Union[str, Any] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Optional[Any] = True SCREAMING_SNAKE_CASE_: Dict = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Tuple = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = jit(model.generate) SCREAMING_SNAKE_CASE_: Dict = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: int = False SCREAMING_SNAKE_CASE_: Optional[int] = max_length SCREAMING_SNAKE_CASE_: Optional[int] = 2 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: List[str] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = jit(model.generate) SCREAMING_SNAKE_CASE_: Optional[int] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: str = False SCREAMING_SNAKE_CASE_: int = max_length SCREAMING_SNAKE_CASE_: str = 2 SCREAMING_SNAKE_CASE_: Optional[Any] = 2 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: str = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[0] , input_ids.shape[0] * config.num_return_sequences) def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Tuple = True SCREAMING_SNAKE_CASE_: List[str] = max_length SCREAMING_SNAKE_CASE_: Any = 0.8 SCREAMING_SNAKE_CASE_: Any = 10 SCREAMING_SNAKE_CASE_: List[str] = 0.3 SCREAMING_SNAKE_CASE_: Tuple = 1 SCREAMING_SNAKE_CASE_: Union[str, Any] = 8 SCREAMING_SNAKE_CASE_: int = 9 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: List[str] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = jit(model.generate) SCREAMING_SNAKE_CASE_: List[Any] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Any = max_length SCREAMING_SNAKE_CASE_: int = 1 SCREAMING_SNAKE_CASE_: Union[str, Any] = 8 SCREAMING_SNAKE_CASE_: List[Any] = 9 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: int = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = jit(model.generate) SCREAMING_SNAKE_CASE_: List[str] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Any = max_length SCREAMING_SNAKE_CASE_: List[str] = 2 SCREAMING_SNAKE_CASE_: str = 1 SCREAMING_SNAKE_CASE_: Tuple = 8 SCREAMING_SNAKE_CASE_: List[Any] = 9 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Optional[int] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = jit(model.generate) SCREAMING_SNAKE_CASE_: List[str] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self._get_input_ids_and_config() # pad attention mask on the left SCREAMING_SNAKE_CASE_: Dict = attention_mask.at[(0, 0)].set(0) SCREAMING_SNAKE_CASE_: Dict = False SCREAMING_SNAKE_CASE_: Optional[int] = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Any = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = model.generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = jit(model.generate) SCREAMING_SNAKE_CASE_: List[Any] = jit_generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = self._get_input_ids_and_config() # pad attention mask on the left SCREAMING_SNAKE_CASE_: List[Any] = attention_mask.at[(0, 0)].set(0) SCREAMING_SNAKE_CASE_: Optional[int] = True SCREAMING_SNAKE_CASE_: Union[str, Any] = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: str = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = model.generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = jit(model.generate) SCREAMING_SNAKE_CASE_: Optional[Any] = jit_generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self._get_input_ids_and_config() # pad attention mask on the left SCREAMING_SNAKE_CASE_: Dict = attention_mask.at[(0, 0)].set(0) SCREAMING_SNAKE_CASE_: Optional[Any] = 2 SCREAMING_SNAKE_CASE_: Any = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Tuple = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = model.generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = jit(model.generate) SCREAMING_SNAKE_CASE_: Union[str, Any] = jit_generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) @require_flax class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: Tuple = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-bert") SCREAMING_SNAKE_CASE_: List[Any] = FlaxAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-bert-flax-only") SCREAMING_SNAKE_CASE_: Optional[int] = "Hello world" SCREAMING_SNAKE_CASE_: List[Any] = tokenizer(lowerCAmelCase__ , return_tensors="np").input_ids # typos are quickly detected (the correct argument is `do_sample`) with self.assertRaisesRegex(lowerCAmelCase__ , "do_samples"): model.generate(lowerCAmelCase__ , do_samples=lowerCAmelCase__) # arbitrary arguments that will not be used anywhere are also not accepted with self.assertRaisesRegex(lowerCAmelCase__ , "foo"): SCREAMING_SNAKE_CASE_: str = {"foo": "bar"} model.generate(lowerCAmelCase__ , **lowerCAmelCase__)
13
1
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCAmelCase : Union[str, Any] = logging.get_logger(__name__) lowerCAmelCase : List[Any] = { """shi-labs/dinat-mini-in1k-224""": """https://huggingface.co/shi-labs/dinat-mini-in1k-224/resolve/main/config.json""", # See all Dinat models at https://huggingface.co/models?filter=dinat } class __lowercase ( UpperCAmelCase_ , UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : Tuple = '''dinat''' _UpperCAmelCase : Union[str, Any] = { '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers''', } def __init__( self : Dict , lowerCAmelCase__ : Tuple=4 , lowerCAmelCase__ : Optional[int]=3 , lowerCAmelCase__ : int=64 , lowerCAmelCase__ : Union[str, Any]=[3, 4, 6, 5] , lowerCAmelCase__ : int=[2, 4, 8, 16] , lowerCAmelCase__ : Optional[Any]=7 , lowerCAmelCase__ : Tuple=[[1, 8, 1], [1, 4, 1, 4], [1, 2, 1, 2, 1, 2], [1, 1, 1, 1, 1]] , lowerCAmelCase__ : Tuple=3.0 , lowerCAmelCase__ : List[Any]=True , lowerCAmelCase__ : Dict=0.0 , lowerCAmelCase__ : str=0.0 , lowerCAmelCase__ : List[str]=0.1 , lowerCAmelCase__ : List[Any]="gelu" , lowerCAmelCase__ : Tuple=0.02 , lowerCAmelCase__ : Any=1E-5 , lowerCAmelCase__ : int=0.0 , lowerCAmelCase__ : Optional[Any]=None , lowerCAmelCase__ : str=None , **lowerCAmelCase__ : Tuple , ): super().__init__(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = patch_size SCREAMING_SNAKE_CASE_: List[str] = num_channels SCREAMING_SNAKE_CASE_: Optional[int] = embed_dim SCREAMING_SNAKE_CASE_: List[Any] = depths SCREAMING_SNAKE_CASE_: str = len(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = num_heads SCREAMING_SNAKE_CASE_: List[Any] = kernel_size SCREAMING_SNAKE_CASE_: Any = dilations SCREAMING_SNAKE_CASE_: Tuple = mlp_ratio SCREAMING_SNAKE_CASE_: Any = qkv_bias SCREAMING_SNAKE_CASE_: Union[str, Any] = hidden_dropout_prob SCREAMING_SNAKE_CASE_: List[Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_: List[str] = drop_path_rate SCREAMING_SNAKE_CASE_: List[str] = hidden_act SCREAMING_SNAKE_CASE_: Optional[Any] = layer_norm_eps SCREAMING_SNAKE_CASE_: Any = initializer_range # we set the hidden_size attribute in order to make Dinat work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model SCREAMING_SNAKE_CASE_: Union[str, Any] = int(embed_dim * 2 ** (len(lowerCAmelCase__) - 1)) SCREAMING_SNAKE_CASE_: Optional[Any] = layer_scale_init_value SCREAMING_SNAKE_CASE_: str = ["stem"] + [F"stage{idx}" for idx in range(1 , len(lowerCAmelCase__) + 1)] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = get_aligned_output_features_output_indices( out_features=lowerCAmelCase__ , out_indices=lowerCAmelCase__ , stage_names=self.stage_names)
13
import argparse import re import torch from CLAP import create_model from transformers import AutoFeatureExtractor, ClapConfig, ClapModel lowerCAmelCase : Union[str, Any] = { """text_branch""": """text_model""", """audio_branch""": """audio_model.audio_encoder""", """attn""": """attention.self""", """self.proj""": """output.dense""", """attention.self_mask""": """attn_mask""", """mlp.fc1""": """intermediate.dense""", """mlp.fc2""": """output.dense""", """norm1""": """layernorm_before""", """norm2""": """layernorm_after""", """bn0""": """batch_norm""", } lowerCAmelCase : int = AutoFeatureExtractor.from_pretrained("""laion/clap-htsat-unfused""", truncation="""rand_trunc""") def A_ ( _UpperCAmelCase , _UpperCAmelCase=False ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = create_model( "HTSAT-tiny" , "roberta" , _UpperCAmelCase , precision="fp32" , device="cuda:0" if torch.cuda.is_available() else "cpu" , enable_fusion=_UpperCAmelCase , fusion_type="aff_2d" if enable_fusion else None , ) return model, model_cfg def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = {} SCREAMING_SNAKE_CASE_: Tuple = R".*sequential.(\d+).*" SCREAMING_SNAKE_CASE_: Dict = R".*_projection.(\d+).*" for key, value in state_dict.items(): # check if any key needs to be modified for key_to_modify, new_key in KEYS_TO_MODIFY_MAPPING.items(): if key_to_modify in key: SCREAMING_SNAKE_CASE_: Any = key.replace(_UpperCAmelCase , _UpperCAmelCase ) if re.match(_UpperCAmelCase , _UpperCAmelCase ): # replace sequential layers with list SCREAMING_SNAKE_CASE_: Optional[int] = re.match(_UpperCAmelCase , _UpperCAmelCase ).group(1 ) SCREAMING_SNAKE_CASE_: Dict = key.replace(f"sequential.{sequential_layer}." , f"layers.{int(_UpperCAmelCase )//3}.linear." ) elif re.match(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = int(re.match(_UpperCAmelCase , _UpperCAmelCase ).group(1 ) ) # Because in CLAP they use `nn.Sequential`... SCREAMING_SNAKE_CASE_: Optional[int] = 1 if projecton_layer == 0 else 2 SCREAMING_SNAKE_CASE_: Dict = key.replace(f"_projection.{projecton_layer}." , f"_projection.linear{transformers_projection_layer}." ) if "audio" and "qkv" in key: # split qkv into query key and value SCREAMING_SNAKE_CASE_: Tuple = value SCREAMING_SNAKE_CASE_: List[str] = mixed_qkv.size(0 ) // 3 SCREAMING_SNAKE_CASE_: Any = mixed_qkv[:qkv_dim] SCREAMING_SNAKE_CASE_: Optional[int] = mixed_qkv[qkv_dim : qkv_dim * 2] SCREAMING_SNAKE_CASE_: Optional[Any] = mixed_qkv[qkv_dim * 2 :] SCREAMING_SNAKE_CASE_: str = query_layer SCREAMING_SNAKE_CASE_: int = key_layer SCREAMING_SNAKE_CASE_: List[Any] = value_layer else: SCREAMING_SNAKE_CASE_: int = value return model_state_dict def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=False ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = init_clap(_UpperCAmelCase , enable_fusion=_UpperCAmelCase ) clap_model.eval() SCREAMING_SNAKE_CASE_: Union[str, Any] = clap_model.state_dict() SCREAMING_SNAKE_CASE_: Optional[int] = rename_state_dict(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[str] = ClapConfig() SCREAMING_SNAKE_CASE_: Tuple = enable_fusion SCREAMING_SNAKE_CASE_: Tuple = ClapModel(_UpperCAmelCase ) # ignore the spectrogram embedding layer model.load_state_dict(_UpperCAmelCase , strict=_UpperCAmelCase ) model.save_pretrained(_UpperCAmelCase ) transformers_config.save_pretrained(_UpperCAmelCase ) if __name__ == "__main__": lowerCAmelCase : Tuple = 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("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""") parser.add_argument("""--enable_fusion""", action="""store_true""", help="""Whether to enable fusion or not""") lowerCAmelCase : int = parser.parse_args() convert_clap_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.enable_fusion)
13
1
def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Optional[int] = [0 for i in range(r + 1 )] # nc0 = 1 SCREAMING_SNAKE_CASE_: Tuple = 1 for i in range(1 , n + 1 ): # to compute current row from previous row. SCREAMING_SNAKE_CASE_: int = min(_UpperCAmelCase , _UpperCAmelCase ) while j > 0: c[j] += c[j - 1] j -= 1 return c[r] print(binomial_coefficient(n=10, r=5))
13
import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_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, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __lowercase : """simple docstring""" def __init__( self : Any , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Any=13 , lowerCAmelCase__ : Tuple=30 , lowerCAmelCase__ : List[str]=2 , lowerCAmelCase__ : int=3 , lowerCAmelCase__ : Optional[int]=True , lowerCAmelCase__ : List[str]=True , lowerCAmelCase__ : str=32 , lowerCAmelCase__ : Any=5 , lowerCAmelCase__ : str=4 , lowerCAmelCase__ : int=37 , lowerCAmelCase__ : Optional[Any]="gelu" , lowerCAmelCase__ : Optional[int]=0.1 , lowerCAmelCase__ : Dict=0.1 , lowerCAmelCase__ : Tuple=10 , lowerCAmelCase__ : Optional[Any]=0.02 , lowerCAmelCase__ : List[str]=None , lowerCAmelCase__ : Union[str, Any]=2 , ): SCREAMING_SNAKE_CASE_: str = parent SCREAMING_SNAKE_CASE_: Optional[Any] = batch_size SCREAMING_SNAKE_CASE_: str = image_size SCREAMING_SNAKE_CASE_: Tuple = patch_size SCREAMING_SNAKE_CASE_: int = num_channels SCREAMING_SNAKE_CASE_: List[str] = is_training SCREAMING_SNAKE_CASE_: str = use_labels SCREAMING_SNAKE_CASE_: int = hidden_size SCREAMING_SNAKE_CASE_: List[Any] = num_hidden_layers SCREAMING_SNAKE_CASE_: Union[str, Any] = num_attention_heads SCREAMING_SNAKE_CASE_: Any = intermediate_size SCREAMING_SNAKE_CASE_: str = hidden_act SCREAMING_SNAKE_CASE_: str = hidden_dropout_prob SCREAMING_SNAKE_CASE_: List[str] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_: int = type_sequence_label_size SCREAMING_SNAKE_CASE_: Dict = initializer_range SCREAMING_SNAKE_CASE_: Dict = scope SCREAMING_SNAKE_CASE_: Dict = encoder_stride # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) SCREAMING_SNAKE_CASE_: List[Any] = (image_size // patch_size) ** 2 SCREAMING_SNAKE_CASE_: Dict = num_patches + 1 def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]) SCREAMING_SNAKE_CASE_: str = None if self.use_labels: SCREAMING_SNAKE_CASE_: Union[str, Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size) SCREAMING_SNAKE_CASE_: Optional[Any] = self.get_config() return config, pixel_values, labels def _SCREAMING_SNAKE_CASE ( self : Optional[int]): return ViTConfig( 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 , encoder_stride=self.encoder_stride , ) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : Tuple): SCREAMING_SNAKE_CASE_: Union[str, Any] = ViTModel(config=lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: Optional[int] = model(lowerCAmelCase__) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Dict): SCREAMING_SNAKE_CASE_: Optional[int] = ViTForMaskedImageModeling(config=lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: str = model(lowerCAmelCase__) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size)) # test greyscale images SCREAMING_SNAKE_CASE_: Dict = 1 SCREAMING_SNAKE_CASE_: List[str] = ViTForMaskedImageModeling(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: List[str] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]) SCREAMING_SNAKE_CASE_: str = model(lowerCAmelCase__) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size)) def _SCREAMING_SNAKE_CASE ( self : Any , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Union[str, Any]): SCREAMING_SNAKE_CASE_: Tuple = self.type_sequence_label_size SCREAMING_SNAKE_CASE_: List[str] = ViTForImageClassification(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: Any = model(lowerCAmelCase__ , labels=lowerCAmelCase__) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) # test greyscale images SCREAMING_SNAKE_CASE_: Union[str, Any] = 1 SCREAMING_SNAKE_CASE_: List[str] = ViTForImageClassification(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() SCREAMING_SNAKE_CASE_: Union[str, Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]) SCREAMING_SNAKE_CASE_: Dict = model(lowerCAmelCase__) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ): List[str] = config_and_inputs SCREAMING_SNAKE_CASE_: Optional[Any] = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class __lowercase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : List[Any] = ( ( ViTModel, ViTForImageClassification, ViTForMaskedImageModeling, ) if is_torch_available() else () ) _UpperCAmelCase : Tuple = ( {'''feature-extraction''': ViTModel, '''image-classification''': ViTForImageClassification} if is_torch_available() else {} ) _UpperCAmelCase : List[str] = True _UpperCAmelCase : List[Any] = False _UpperCAmelCase : Optional[Any] = False _UpperCAmelCase : Tuple = False def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: List[str] = ViTModelTester(self) SCREAMING_SNAKE_CASE_: Union[str, Any] = ConfigTester(self , config_class=lowerCAmelCase__ , has_text_modality=lowerCAmelCase__ , hidden_size=37) def _SCREAMING_SNAKE_CASE ( self : Any): self.config_tester.run_common_tests() @unittest.skip(reason="ViT does not use inputs_embeds") def _SCREAMING_SNAKE_CASE ( self : str): pass def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_: Dict = model_class(lowerCAmelCase__) self.assertIsInstance(model.get_input_embeddings() , (nn.Module)) SCREAMING_SNAKE_CASE_: List[Any] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCAmelCase__ , nn.Linear)) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_: List[Any] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = inspect.signature(model.forward) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE_: Optional[Any] = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE_: Optional[int] = ["pixel_values"] self.assertListEqual(arg_names[:1] , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__) @slow def _SCREAMING_SNAKE_CASE ( self : int): for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_: Union[str, Any] = ViTModel.from_pretrained(lowerCAmelCase__) self.assertIsNotNone(lowerCAmelCase__) def A_ ( ): SCREAMING_SNAKE_CASE_: List[Any] = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class __lowercase ( unittest.TestCase ): """simple docstring""" @cached_property def _SCREAMING_SNAKE_CASE ( self : int): return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224") if is_vision_available() else None @slow def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: int = ViTForImageClassification.from_pretrained("google/vit-base-patch16-224").to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = self.default_image_processor SCREAMING_SNAKE_CASE_: str = prepare_img() SCREAMING_SNAKE_CASE_: Optional[Any] = image_processor(images=lowerCAmelCase__ , return_tensors="pt").to(lowerCAmelCase__) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_: Optional[int] = model(**lowerCAmelCase__) # verify the logits SCREAMING_SNAKE_CASE_: Any = torch.Size((1, 1000)) self.assertEqual(outputs.logits.shape , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = torch.tensor([-0.2744, 0.8215, -0.0836]).to(lowerCAmelCase__) self.assertTrue(torch.allclose(outputs.logits[0, :3] , lowerCAmelCase__ , atol=1E-4)) @slow def _SCREAMING_SNAKE_CASE ( self : List[Any]): # ViT models have an `interpolate_pos_encoding` argument in their forward method, # allowing to interpolate the pre-trained position embeddings in order to use # the model on higher resolutions. The DINO model by Facebook AI leverages this # to visualize self-attention on higher resolution images. SCREAMING_SNAKE_CASE_: str = ViTModel.from_pretrained("facebook/dino-vits8").to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = ViTImageProcessor.from_pretrained("facebook/dino-vits8" , size=480) SCREAMING_SNAKE_CASE_: List[Any] = prepare_img() SCREAMING_SNAKE_CASE_: List[Any] = image_processor(images=lowerCAmelCase__ , return_tensors="pt") SCREAMING_SNAKE_CASE_: int = inputs.pixel_values.to(lowerCAmelCase__) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_: Optional[int] = model(lowerCAmelCase__ , interpolate_pos_encoding=lowerCAmelCase__) # verify the logits SCREAMING_SNAKE_CASE_: Tuple = torch.Size((1, 3601, 384)) self.assertEqual(outputs.last_hidden_state.shape , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.tensor( [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]]).to(lowerCAmelCase__) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , lowerCAmelCase__ , atol=1E-4)) @slow @require_accelerate @require_torch_gpu def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Dict = ViTModel.from_pretrained("facebook/dino-vits8" , torch_dtype=torch.floataa , device_map="auto") SCREAMING_SNAKE_CASE_: int = self.default_image_processor SCREAMING_SNAKE_CASE_: Union[str, Any] = prepare_img() SCREAMING_SNAKE_CASE_: Dict = image_processor(images=lowerCAmelCase__ , return_tensors="pt") SCREAMING_SNAKE_CASE_: str = inputs.pixel_values.to(lowerCAmelCase__) # forward pass to make sure inference works in fp16 with torch.no_grad(): SCREAMING_SNAKE_CASE_: str = model(lowerCAmelCase__)
13
1
from collections.abc import Callable def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: float = a SCREAMING_SNAKE_CASE_: float = b if function(_UpperCAmelCase ) == 0: # one of the a or b is a root for the function return a elif function(_UpperCAmelCase ) == 0: return b elif ( function(_UpperCAmelCase ) * function(_UpperCAmelCase ) > 0 ): # if none of these are root and they are both positive or negative, # then this algorithm can't find the root raise ValueError("could not find root in given interval." ) else: SCREAMING_SNAKE_CASE_: float = start + (end - start) / 2.0 while abs(start - mid ) > 10**-7: # until precisely equals to 10^-7 if function(_UpperCAmelCase ) == 0: return mid elif function(_UpperCAmelCase ) * function(_UpperCAmelCase ) < 0: SCREAMING_SNAKE_CASE_: Dict = mid else: SCREAMING_SNAKE_CASE_: Union[str, Any] = mid SCREAMING_SNAKE_CASE_: Dict = start + (end - start) / 2.0 return mid def A_ ( _UpperCAmelCase ): return x**3 - 2 * x - 5 if __name__ == "__main__": print(bisection(f, 1, 1000)) import doctest doctest.testmod()
13
import json import os from functools import lru_cache from typing import List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging lowerCAmelCase : Any = logging.get_logger(__name__) lowerCAmelCase : Tuple = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt"""} lowerCAmelCase : Optional[int] = { """vocab_file""": { """allenai/longformer-base-4096""": """https://huggingface.co/allenai/longformer-base-4096/resolve/main/vocab.json""", """allenai/longformer-large-4096""": ( """https://huggingface.co/allenai/longformer-large-4096/resolve/main/vocab.json""" ), """allenai/longformer-large-4096-finetuned-triviaqa""": ( """https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/vocab.json""" ), """allenai/longformer-base-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/vocab.json""" ), """allenai/longformer-large-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/vocab.json""" ), }, """merges_file""": { """allenai/longformer-base-4096""": """https://huggingface.co/allenai/longformer-base-4096/resolve/main/merges.txt""", """allenai/longformer-large-4096""": ( """https://huggingface.co/allenai/longformer-large-4096/resolve/main/merges.txt""" ), """allenai/longformer-large-4096-finetuned-triviaqa""": ( """https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/merges.txt""" ), """allenai/longformer-base-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/merges.txt""" ), """allenai/longformer-large-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/merges.txt""" ), }, } lowerCAmelCase : Optional[Any] = { """allenai/longformer-base-4096""": 4096, """allenai/longformer-large-4096""": 4096, """allenai/longformer-large-4096-finetuned-triviaqa""": 4096, """allenai/longformer-base-4096-extra.pos.embd.only""": 4096, """allenai/longformer-large-4096-extra.pos.embd.only""": 4096, } @lru_cache() # Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode def A_ ( ): SCREAMING_SNAKE_CASE_: Any = ( list(range(ord("!" ) , ord("~" ) + 1 ) ) + list(range(ord("¡" ) , ord("¬" ) + 1 ) ) + list(range(ord("®" ) , ord("ÿ" ) + 1 ) ) ) SCREAMING_SNAKE_CASE_: Tuple = bs[:] SCREAMING_SNAKE_CASE_: str = 0 for b in range(2**8 ): if b not in bs: bs.append(_UpperCAmelCase ) cs.append(2**8 + n ) n += 1 SCREAMING_SNAKE_CASE_: Optional[int] = [chr(_UpperCAmelCase ) for n in cs] return dict(zip(_UpperCAmelCase , _UpperCAmelCase ) ) def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: str = set() SCREAMING_SNAKE_CASE_: Union[str, Any] = word[0] for char in word[1:]: pairs.add((prev_char, char) ) SCREAMING_SNAKE_CASE_: Tuple = char return pairs class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : Any = VOCAB_FILES_NAMES _UpperCAmelCase : Dict = PRETRAINED_VOCAB_FILES_MAP _UpperCAmelCase : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _UpperCAmelCase : List[str] = ['''input_ids''', '''attention_mask'''] def __init__( self : str , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[Any]="replace" , lowerCAmelCase__ : Optional[Any]="<s>" , lowerCAmelCase__ : int="</s>" , lowerCAmelCase__ : Optional[Any]="</s>" , lowerCAmelCase__ : int="<s>" , lowerCAmelCase__ : Optional[Any]="<unk>" , lowerCAmelCase__ : List[Any]="<pad>" , lowerCAmelCase__ : Any="<mask>" , lowerCAmelCase__ : Union[str, Any]=False , **lowerCAmelCase__ : Tuple , ): SCREAMING_SNAKE_CASE_: int = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else bos_token SCREAMING_SNAKE_CASE_: str = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else eos_token SCREAMING_SNAKE_CASE_: Optional[int] = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else sep_token SCREAMING_SNAKE_CASE_: Union[str, Any] = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else cls_token SCREAMING_SNAKE_CASE_: int = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else unk_token SCREAMING_SNAKE_CASE_: Any = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else pad_token # Mask token behave like a normal word, i.e. include the space before it SCREAMING_SNAKE_CASE_: Optional[int] = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__) if isinstance(lowerCAmelCase__ , lowerCAmelCase__) else mask_token super().__init__( errors=lowerCAmelCase__ , bos_token=lowerCAmelCase__ , eos_token=lowerCAmelCase__ , unk_token=lowerCAmelCase__ , sep_token=lowerCAmelCase__ , cls_token=lowerCAmelCase__ , pad_token=lowerCAmelCase__ , mask_token=lowerCAmelCase__ , add_prefix_space=lowerCAmelCase__ , **lowerCAmelCase__ , ) with open(lowerCAmelCase__ , encoding="utf-8") as vocab_handle: SCREAMING_SNAKE_CASE_: Tuple = json.load(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = {v: k for k, v in self.encoder.items()} SCREAMING_SNAKE_CASE_: Optional[Any] = errors # how to handle errors in decoding SCREAMING_SNAKE_CASE_: List[Any] = bytes_to_unicode() SCREAMING_SNAKE_CASE_: Optional[Any] = {v: k for k, v in self.byte_encoder.items()} with open(lowerCAmelCase__ , encoding="utf-8") as merges_handle: SCREAMING_SNAKE_CASE_: List[Any] = merges_handle.read().split("\n")[1:-1] SCREAMING_SNAKE_CASE_: str = [tuple(merge.split()) for merge in bpe_merges] SCREAMING_SNAKE_CASE_: List[Any] = dict(zip(lowerCAmelCase__ , range(len(lowerCAmelCase__)))) SCREAMING_SNAKE_CASE_: str = {} SCREAMING_SNAKE_CASE_: Optional[Any] = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions SCREAMING_SNAKE_CASE_: List[Any] = re.compile(R"'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+") @property def _SCREAMING_SNAKE_CASE ( self : int): return len(self.encoder) def _SCREAMING_SNAKE_CASE ( self : int): return dict(self.encoder , **self.added_tokens_encoder) def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : List[str]): if token in self.cache: return self.cache[token] SCREAMING_SNAKE_CASE_: Optional[int] = tuple(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = get_pairs(lowerCAmelCase__) if not pairs: return token while True: SCREAMING_SNAKE_CASE_: int = min(lowerCAmelCase__ , key=lambda lowerCAmelCase__: self.bpe_ranks.get(lowerCAmelCase__ , float("inf"))) if bigram not in self.bpe_ranks: break SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = bigram SCREAMING_SNAKE_CASE_: Optional[int] = [] SCREAMING_SNAKE_CASE_: List[Any] = 0 while i < len(lowerCAmelCase__): try: SCREAMING_SNAKE_CASE_: List[Any] = word.index(lowerCAmelCase__ , lowerCAmelCase__) except ValueError: new_word.extend(word[i:]) break else: new_word.extend(word[i:j]) SCREAMING_SNAKE_CASE_: Tuple = j if word[i] == first and i < len(lowerCAmelCase__) - 1 and word[i + 1] == second: new_word.append(first + second) i += 2 else: new_word.append(word[i]) i += 1 SCREAMING_SNAKE_CASE_: str = tuple(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = new_word if len(lowerCAmelCase__) == 1: break else: SCREAMING_SNAKE_CASE_: Dict = get_pairs(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = " ".join(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = word return word def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Tuple): SCREAMING_SNAKE_CASE_: Optional[Any] = [] for token in re.findall(self.pat , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: str = "".join( self.byte_encoder[b] for b in token.encode("utf-8")) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(lowerCAmelCase__).split(" ")) return bpe_tokens def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Tuple): return self.encoder.get(lowerCAmelCase__ , self.encoder.get(self.unk_token)) def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : Union[str, Any]): return self.decoder.get(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Optional[int]): SCREAMING_SNAKE_CASE_: Any = "".join(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = bytearray([self.byte_decoder[c] for c in text]).decode("utf-8" , errors=self.errors) return text def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[str] = None): if not os.path.isdir(lowerCAmelCase__): logger.error(F"Vocabulary path ({save_directory}) should be a directory") return SCREAMING_SNAKE_CASE_: Any = os.path.join( lowerCAmelCase__ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]) SCREAMING_SNAKE_CASE_: Any = os.path.join( lowerCAmelCase__ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"]) with open(lowerCAmelCase__ , "w" , encoding="utf-8") as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=lowerCAmelCase__ , ensure_ascii=lowerCAmelCase__) + "\n") SCREAMING_SNAKE_CASE_: List[Any] = 0 with open(lowerCAmelCase__ , "w" , encoding="utf-8") as writer: writer.write("#version: 0.2\n") for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda lowerCAmelCase__: kv[1]): if index != token_index: logger.warning( F"Saving vocabulary to {merge_file}: BPE merge indices are not consecutive." " Please check that the tokenizer is not corrupted!") SCREAMING_SNAKE_CASE_: List[Any] = token_index writer.write(" ".join(lowerCAmelCase__) + "\n") index += 1 return vocab_file, merge_file def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] SCREAMING_SNAKE_CASE_: Optional[int] = [self.cls_token_id] SCREAMING_SNAKE_CASE_: Dict = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None , lowerCAmelCase__ : bool = False): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=lowerCAmelCase__ , token_ids_a=lowerCAmelCase__ , already_has_special_tokens=lowerCAmelCase__) if token_ids_a is None: return [1] + ([0] * len(lowerCAmelCase__)) + [1] return [1] + ([0] * len(lowerCAmelCase__)) + [1, 1] + ([0] * len(lowerCAmelCase__)) + [1] def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None): SCREAMING_SNAKE_CASE_: Optional[int] = [self.sep_token_id] SCREAMING_SNAKE_CASE_: int = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep) * [0] def _SCREAMING_SNAKE_CASE ( self : int , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : List[str]=False , **lowerCAmelCase__ : str): SCREAMING_SNAKE_CASE_: List[Any] = kwargs.pop("add_prefix_space" , self.add_prefix_space) if (is_split_into_words or add_prefix_space) and (len(lowerCAmelCase__) > 0 and not text[0].isspace()): SCREAMING_SNAKE_CASE_: Optional[Any] = " " + text return (text, kwargs)
13
1
from collections.abc import Callable class __lowercase : """simple docstring""" def __init__( self : Tuple , lowerCAmelCase__ : Callable | None = None): # Stores actual heap items. SCREAMING_SNAKE_CASE_: list = [] # Stores indexes of each item for supporting updates and deletion. SCREAMING_SNAKE_CASE_: dict = {} # Stores current size of heap. SCREAMING_SNAKE_CASE_: Optional[Any] = 0 # Stores function used to evaluate the score of an item on which basis ordering # will be done. SCREAMING_SNAKE_CASE_: Any = key or (lambda lowerCAmelCase__: x) def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : int): return int((i - 1) / 2) if i > 0 else None def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Union[str, Any] = int(2 * i + 1) return left if 0 < left < self.size else None def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Union[str, Any] = int(2 * i + 2) return right if 0 < right < self.size else None def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : int , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = ( self.pos_map[self.arr[j][0]], self.pos_map[self.arr[i][0]], ) # Then swap the items in the list. SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = self.arr[j], self.arr[i] def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : int , lowerCAmelCase__ : int): return self.arr[i][1] < self.arr[j][1] def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Any = self._left(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = self._right(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = i if left is not None and not self._cmp(lowerCAmelCase__ , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Optional[int] = left if right is not None and not self._cmp(lowerCAmelCase__ , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Tuple = right return valid_parent def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: List[Any] = self._parent(lowerCAmelCase__) while parent is not None and not self._cmp(lowerCAmelCase__ , lowerCAmelCase__): self._swap(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = parent, self._parent(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Optional[int] = self._get_valid_parent(lowerCAmelCase__) while valid_parent != index: self._swap(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = valid_parent, self._get_valid_parent(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : int , lowerCAmelCase__ : int): if item not in self.pos_map: return SCREAMING_SNAKE_CASE_: Any = self.pos_map[item] SCREAMING_SNAKE_CASE_: int = [item, self.key(lowerCAmelCase__)] # Make sure heap is right in both up and down direction. # Ideally only one of them will make any change. self._heapify_up(lowerCAmelCase__) self._heapify_down(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : int): if item not in self.pos_map: return SCREAMING_SNAKE_CASE_: Optional[Any] = self.pos_map[item] del self.pos_map[item] SCREAMING_SNAKE_CASE_: List[str] = self.arr[self.size - 1] SCREAMING_SNAKE_CASE_: Tuple = index self.size -= 1 # Make sure heap is right in both up and down direction. Ideally only one # of them will make any change- so no performance loss in calling both. if self.size > index: self._heapify_up(lowerCAmelCase__) self._heapify_down(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Optional[int] = len(self.arr) if arr_len == self.size: self.arr.append([item, self.key(lowerCAmelCase__)]) else: SCREAMING_SNAKE_CASE_: str = [item, self.key(lowerCAmelCase__)] SCREAMING_SNAKE_CASE_: List[Any] = self.size self.size += 1 self._heapify_up(self.size - 1) def _SCREAMING_SNAKE_CASE ( self : List[Any]): return self.arr[0] if self.size else None def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Dict = self.get_top() if top_item_tuple: self.delete_item(top_item_tuple[0]) return top_item_tuple def A_ ( ): pass if __name__ == "__main__": import doctest doctest.testmod()
13
import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from transformers.generation import DisjunctiveConstraint @require_torch class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : List[str]): # For consistency across different places the DisjunctiveConstraint is called, # dc.token_ids is a list of integers. It is also initialized only by integers. SCREAMING_SNAKE_CASE_: Optional[Any] = [[1, 2, 4], [1, 2, 3, 4]] SCREAMING_SNAKE_CASE_: Any = DisjunctiveConstraint(lowerCAmelCase__) self.assertTrue(isinstance(dc.token_ids , lowerCAmelCase__)) with self.assertRaises(lowerCAmelCase__): DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]])) with self.assertRaises(lowerCAmelCase__): DisjunctiveConstraint([torch.LongTensor([1, 2, 4]), torch.LongTensor([1, 2, 3, 4, 5])]) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): # We can't have constraints that are complete subsets of another. This leads to a preverse # interpretation of "constraint fulfillment": does generating [1,2,3] fulfill the constraint? # It would mean that it generated [1,2] which fulfills it, but it's in the middle of potentially # fulfilling [1,2,3,4]. If we believe that [1,2,3] does fulfill the constraint, then the algorithm # will necessarily never reach [1,2,3,4], giving users a false sense of control (better to just not allow it). SCREAMING_SNAKE_CASE_: Union[str, Any] = [[1, 2], [1, 2, 3, 4]] with self.assertRaises(lowerCAmelCase__): DisjunctiveConstraint(lowerCAmelCase__) # fails here def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: List[str] = [[1, 2, 3], [1, 2, 4]] SCREAMING_SNAKE_CASE_: Tuple = DisjunctiveConstraint(lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = dc.update(1) SCREAMING_SNAKE_CASE_: Dict = stepped is True and completed is False and reset is False self.assertTrue(lowerCAmelCase__) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = dc.update(2) SCREAMING_SNAKE_CASE_: Optional[Any] = stepped is True and completed is False and reset is False self.assertTrue(lowerCAmelCase__) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1, 2]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = dc.update(3) SCREAMING_SNAKE_CASE_: Tuple = stepped is True and completed is True and reset is False self.assertTrue(lowerCAmelCase__) self.assertTrue(dc.completed) # Completed! self.assertTrue(dc.current_seq == [1, 2, 3]) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]] SCREAMING_SNAKE_CASE_: List[Any] = DisjunctiveConstraint(lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = dc.update(1) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = dc.update(2) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1, 2]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = dc.update(4) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1, 2, 4]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = dc.update(5) self.assertTrue(dc.completed) # Completed! self.assertTrue(dc.current_seq == [1, 2, 4, 5]) dc.reset() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = dc.update(1) self.assertTrue(not dc.completed) self.assertTrue(dc.remaining() == 3) self.assertTrue(dc.current_seq == [1]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = dc.update(2) self.assertTrue(not dc.completed) self.assertTrue(dc.remaining() == 2) self.assertTrue(dc.current_seq == [1, 2]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = dc.update(5) self.assertTrue(dc.completed) # Completed! self.assertTrue(dc.remaining() == 0) self.assertTrue(dc.current_seq == [1, 2, 5])
13
1
from __future__ import annotations def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , ): SCREAMING_SNAKE_CASE_: Optional[int] = len(_UpperCAmelCase ) # If row is equal to the size of the board it means there are a queen in each row in # the current board (possible_board) if row == n: # We convert the variable possible_board that looks like this: [1, 3, 0, 2] to # this: ['. Q . . ', '. . . Q ', 'Q . . . ', '. . Q . '] boards.append([". " * i + "Q " + ". " * (n - 1 - i) for i in possible_board] ) return # We iterate each column in the row to find all possible results in each row for col in range(_UpperCAmelCase ): # We apply that we learned previously. First we check that in the current board # (possible_board) there are not other same value because if there is it means # that there are a collision in vertical. Then we apply the two formulas we # learned before: # # 45º: y - x = b or 45: row - col = b # 135º: y + x = b or row + col = b. # # And we verify if the results of this two formulas not exist in their variables # respectively. (diagonal_right_collisions, diagonal_left_collisions) # # If any or these are True it means there is a collision so we continue to the # next value in the for loop. if ( col in possible_board or row - col in diagonal_right_collisions or row + col in diagonal_left_collisions ): continue # If it is False we call dfs function again and we update the inputs depth_first_search( [*possible_board, col] , [*diagonal_right_collisions, row - col] , [*diagonal_left_collisions, row + col] , _UpperCAmelCase , _UpperCAmelCase , ) def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: list[list[str]] = [] depth_first_search([] , [] , [] , _UpperCAmelCase , _UpperCAmelCase ) # Print all the boards for board in boards: for column in board: print(_UpperCAmelCase ) print("" ) print(len(_UpperCAmelCase ) , "solutions were found." ) if __name__ == "__main__": import doctest doctest.testmod() n_queens_solution(4)
13
import pickle import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, XGLMTokenizer, XGLMTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin lowerCAmelCase : Optional[Any] = get_tests_dir("""fixtures/test_sentencepiece.model""") @require_sentencepiece @require_tokenizers class __lowercase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : Optional[Any] = XGLMTokenizer _UpperCAmelCase : List[Any] = XGLMTokenizerFast _UpperCAmelCase : Optional[int] = True _UpperCAmelCase : Tuple = True def _SCREAMING_SNAKE_CASE ( self : Tuple): super().setUp() # We have a SentencePiece fixture for testing SCREAMING_SNAKE_CASE_: List[Any] = XGLMTokenizer(lowerCAmelCase__ , keep_accents=lowerCAmelCase__) tokenizer.save_pretrained(self.tmpdirname) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_: Optional[Any] = "<pad>" SCREAMING_SNAKE_CASE_: int = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCAmelCase__) , lowerCAmelCase__) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCAmelCase__) , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Optional[int] = list(self.get_tokenizer().get_vocab().keys()) self.assertEqual(vocab_keys[0] , "<s>") self.assertEqual(vocab_keys[1] , "<pad>") self.assertEqual(len(lowerCAmelCase__) , 1008) def _SCREAMING_SNAKE_CASE ( self : Any): self.assertEqual(self.get_tokenizer().vocab_size , 1008) def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Optional[int] = XGLMTokenizer(lowerCAmelCase__ , keep_accents=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = tokenizer.tokenize("This is a test") self.assertListEqual(lowerCAmelCase__ , ["▁This", "▁is", "▁a", "▁t", "est"]) self.assertListEqual( tokenizer.convert_tokens_to_ids(lowerCAmelCase__) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) SCREAMING_SNAKE_CASE_: List[str] = tokenizer.tokenize("I was born in 92000, and this is falsé.") self.assertListEqual( lowerCAmelCase__ , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "9", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "é", ".", ] , ) SCREAMING_SNAKE_CASE_: Optional[Any] = tokenizer.convert_tokens_to_ids(lowerCAmelCase__) self.assertListEqual( lowerCAmelCase__ , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) SCREAMING_SNAKE_CASE_: List[Any] = tokenizer.convert_ids_to_tokens(lowerCAmelCase__) self.assertListEqual( lowerCAmelCase__ , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "<unk>", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "<unk>", ".", ] , ) @cached_property def _SCREAMING_SNAKE_CASE ( self : Any): return XGLMTokenizer.from_pretrained("facebook/xglm-564M") def _SCREAMING_SNAKE_CASE ( self : str): with tempfile.NamedTemporaryFile() as f: shutil.copyfile(lowerCAmelCase__ , f.name) SCREAMING_SNAKE_CASE_: Tuple = XGLMTokenizer(f.name , keep_accents=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = pickle.dumps(lowerCAmelCase__) pickle.loads(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : str): if not self.test_rust_tokenizer: return SCREAMING_SNAKE_CASE_: Dict = self.get_tokenizer() SCREAMING_SNAKE_CASE_: List[str] = self.get_rust_tokenizer() SCREAMING_SNAKE_CASE_: Any = "I was born in 92000, and this is falsé." SCREAMING_SNAKE_CASE_: Union[str, Any] = tokenizer.tokenize(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = rust_tokenizer.tokenize(lowerCAmelCase__) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = rust_tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = self.get_rust_tokenizer() SCREAMING_SNAKE_CASE_: str = tokenizer.encode(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = rust_tokenizer.encode(lowerCAmelCase__) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__) @slow def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: Dict = "Hello World!" SCREAMING_SNAKE_CASE_: Union[str, Any] = [2, 3_1227, 4447, 35] self.assertListEqual(lowerCAmelCase__ , self.big_tokenizer.encode(lowerCAmelCase__)) @slow def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Union[str, Any] = ( "This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will" " add words that should not exsist and be tokenized to unk, such as saoneuhaoesuth" ) # fmt: off SCREAMING_SNAKE_CASE_: Optional[Any] = [2, 1018, 67, 11, 1988, 2617, 5631, 278, 11, 3407, 48, 7_1630, 2_8085, 4, 3234, 157, 13, 6, 5, 6, 4, 3526, 768, 15, 659, 57, 298, 3983, 864, 129, 21, 6, 5, 1_3675, 377, 652, 7580, 1_0341, 155, 2817, 422, 1666, 7, 1674, 53, 113, 20_2277, 1_7892, 33, 60, 87, 4, 3234, 157, 61, 2667, 5_2376, 19, 88, 23, 735] # fmt: on self.assertListEqual(lowerCAmelCase__ , self.big_tokenizer.encode(lowerCAmelCase__)) @slow def _SCREAMING_SNAKE_CASE ( self : int): # fmt: off SCREAMING_SNAKE_CASE_: str = { "input_ids": [[2, 10_8825, 1163, 15, 8_8010, 473, 1_5898, 157, 1_3672, 1857, 312, 8, 23_8021, 1163, 53, 1_3672, 1857, 312, 8, 5_3283, 18_2396, 8, 1_8566, 16, 3_6733, 4101, 8, 230, 24_4017, 12_2553, 7, 15, 13_2597, 4, 293, 1_2511, 7610, 4, 3414, 13_2597, 9, 4, 3_2361, 362, 4, 734, 2_8512, 3_2569, 18, 4, 3_2361, 2_6096, 1_4982, 73, 1_8715, 2_1433, 23_5261, 15, 492, 1_2427, 16, 53, 1_8715, 2_1433, 6_5454, 15, 2_3659, 563, 16, 278, 597, 2843, 595, 7931, 18_2396, 6_4186, 22, 886, 595, 13_2981, 53, 2_5540, 3449, 4_3982, 3_9901, 5951, 878, 330, 4, 2_7694, 8_0269, 312, 53, 6517, 1_1780, 611, 2_0408, 5], [2, 6, 13_2597, 67, 4_2897, 33, 592, 8, 16_3729, 2_5540, 361, 13_6997, 10_9514, 17_3230, 7, 501, 60, 10_2913, 196, 5631, 235, 6_3243, 473, 6, 23_1757, 74, 5277, 7905, 53, 3095, 3_7317, 22, 454, 18_3874, 5], [2, 268, 3_1298, 4_6530, 6, 13_2935, 4_3831, 7, 597, 32, 24, 3688, 9865, 5]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] } # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=lowerCAmelCase__ , model_name="facebook/xglm-564M" , padding=lowerCAmelCase__ , )
13
1
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCAmelCase : int = logging.get_logger(__name__) lowerCAmelCase : List[str] = { """microsoft/resnet-50""": """https://huggingface.co/microsoft/resnet-50/blob/main/config.json""", } class __lowercase ( UpperCAmelCase_ , UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : Optional[int] = '''resnet''' _UpperCAmelCase : Optional[Any] = ['''basic''', '''bottleneck'''] def __init__( self : str , lowerCAmelCase__ : Any=3 , lowerCAmelCase__ : Optional[int]=64 , lowerCAmelCase__ : Dict=[256, 512, 1024, 2048] , lowerCAmelCase__ : List[str]=[3, 4, 6, 3] , lowerCAmelCase__ : Optional[Any]="bottleneck" , lowerCAmelCase__ : int="relu" , lowerCAmelCase__ : Optional[Any]=False , lowerCAmelCase__ : Dict=None , lowerCAmelCase__ : Optional[int]=None , **lowerCAmelCase__ : List[str] , ): super().__init__(**lowerCAmelCase__) if layer_type not in self.layer_types: raise ValueError(F"layer_type={layer_type} is not one of {','.join(self.layer_types)}") SCREAMING_SNAKE_CASE_: Union[str, Any] = num_channels SCREAMING_SNAKE_CASE_: Optional[int] = embedding_size SCREAMING_SNAKE_CASE_: Dict = hidden_sizes SCREAMING_SNAKE_CASE_: List[Any] = depths SCREAMING_SNAKE_CASE_: List[Any] = layer_type SCREAMING_SNAKE_CASE_: Any = hidden_act SCREAMING_SNAKE_CASE_: Any = downsample_in_first_stage SCREAMING_SNAKE_CASE_: Tuple = ["stem"] + [F"stage{idx}" for idx in range(1 , len(lowerCAmelCase__) + 1)] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = get_aligned_output_features_output_indices( out_features=lowerCAmelCase__ , out_indices=lowerCAmelCase__ , stage_names=self.stage_names) class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : Dict = version.parse('''1.11''' ) @property def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ]) @property def _SCREAMING_SNAKE_CASE ( self : Optional[int]): return 1E-3
13
def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , ): SCREAMING_SNAKE_CASE_: Optional[int] = [redshift, radiation_density, matter_density, dark_energy] if any(p < 0 for p in parameters ): raise ValueError("All input parameters must be positive" ) if any(p > 1 for p in parameters[1:4] ): raise ValueError("Relative densities cannot be greater than one" ) else: SCREAMING_SNAKE_CASE_: int = 1 - (matter_density + radiation_density + dark_energy) SCREAMING_SNAKE_CASE_: Dict = ( radiation_density * (redshift + 1) ** 4 + matter_density * (redshift + 1) ** 3 + curvature * (redshift + 1) ** 2 + dark_energy ) SCREAMING_SNAKE_CASE_: Any = hubble_constant * e_a ** (1 / 2) return hubble if __name__ == "__main__": import doctest # run doctest doctest.testmod() # demo LCDM approximation lowerCAmelCase : List[Any] = 0.3 print( hubble_parameter( hubble_constant=68.3, radiation_density=1E-4, matter_density=matter_density, dark_energy=1 - matter_density, redshift=0, ) )
13
1
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 lowerCAmelCase : Any = True except (ImportError, AttributeError): lowerCAmelCase : Any = object def A_ ( *_UpperCAmelCase , **_UpperCAmelCase ): pass lowerCAmelCase : Tuple = False lowerCAmelCase : List[Any] = logging.get_logger("""transformers-cli/serving""") def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Union[str, Any] = pipeline( task=args.task , model=args.model if args.model else None , config=args.config , tokenizer=args.tokenizer , device=args.device , ) return ServeCommand(_UpperCAmelCase , args.host , args.port , args.workers ) class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : dict class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : List[str] _UpperCAmelCase : Optional[List[int]] class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : str class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : Any class __lowercase ( UpperCAmelCase_ ): """simple docstring""" @staticmethod def _SCREAMING_SNAKE_CASE ( lowerCAmelCase__ : ArgumentParser): SCREAMING_SNAKE_CASE_: Optional[Any] = 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=8888 , 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 : Optional[int] , lowerCAmelCase__ : Pipeline , lowerCAmelCase__ : str , lowerCAmelCase__ : int , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: int = pipeline SCREAMING_SNAKE_CASE_: Dict = host SCREAMING_SNAKE_CASE_: str = port SCREAMING_SNAKE_CASE_: List[Any] = 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}") SCREAMING_SNAKE_CASE_: Tuple = 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 _SCREAMING_SNAKE_CASE ( self : Tuple): run(self._app , host=self.host , port=self.port , workers=self.workers) def _SCREAMING_SNAKE_CASE ( self : List[str]): return ServeModelInfoResult(infos=vars(self._pipeline.model.config)) def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : str = Body(lowerCAmelCase__ , embed=lowerCAmelCase__) , lowerCAmelCase__ : bool = Body(lowerCAmelCase__ , embed=lowerCAmelCase__)): try: SCREAMING_SNAKE_CASE_: Optional[Any] = self._pipeline.tokenizer.tokenize(lowerCAmelCase__) if return_ids: SCREAMING_SNAKE_CASE_: int = 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 _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : List[int] = Body(lowerCAmelCase__ , embed=lowerCAmelCase__) , lowerCAmelCase__ : bool = Body(lowerCAmelCase__ , embed=lowerCAmelCase__) , lowerCAmelCase__ : bool = Body(lowerCAmelCase__ , embed=lowerCAmelCase__) , ): try: SCREAMING_SNAKE_CASE_: Optional[Any] = 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 _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : Optional[int]=Body(lowerCAmelCase__ , embed=lowerCAmelCase__)): # Check we don't have empty string if len(lowerCAmelCase__) == 0: return ServeForwardResult(output=[] , attention=[]) try: # Forward through the model SCREAMING_SNAKE_CASE_: Optional[int] = self._pipeline(lowerCAmelCase__) return ServeForwardResult(output=lowerCAmelCase__) except Exception as e: raise HTTPException(500 , {"error": str(lowerCAmelCase__)})
13
from typing import Optional, Union import torch from torch import nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention from ...modeling_utils import PreTrainedModel from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging from .configuration_mobilenet_va import MobileNetVaConfig lowerCAmelCase : int = logging.get_logger(__name__) # General docstring lowerCAmelCase : int = """MobileNetV1Config""" # Base docstring lowerCAmelCase : List[Any] = """google/mobilenet_v1_1.0_224""" lowerCAmelCase : Dict = [1, 1024, 7, 7] # Image classification docstring lowerCAmelCase : Union[str, Any] = """google/mobilenet_v1_1.0_224""" lowerCAmelCase : Any = """tabby, tabby cat""" lowerCAmelCase : List[Any] = [ """google/mobilenet_v1_1.0_224""", """google/mobilenet_v1_0.75_192""", # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 ] def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=None ): SCREAMING_SNAKE_CASE_: List[str] = {} if isinstance(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = model.mobilenet_va else: SCREAMING_SNAKE_CASE_: int = model SCREAMING_SNAKE_CASE_: Dict = "MobilenetV1/Conv2d_0/" SCREAMING_SNAKE_CASE_: str = backbone.conv_stem.convolution.weight SCREAMING_SNAKE_CASE_: List[str] = backbone.conv_stem.normalization.bias SCREAMING_SNAKE_CASE_: int = backbone.conv_stem.normalization.weight SCREAMING_SNAKE_CASE_: List[str] = backbone.conv_stem.normalization.running_mean SCREAMING_SNAKE_CASE_: Optional[int] = backbone.conv_stem.normalization.running_var for i in range(13 ): SCREAMING_SNAKE_CASE_: List[str] = i + 1 SCREAMING_SNAKE_CASE_: Optional[int] = i * 2 SCREAMING_SNAKE_CASE_: Any = backbone.layer[pt_index] SCREAMING_SNAKE_CASE_: Any = f"MobilenetV1/Conv2d_{tf_index}_depthwise/" SCREAMING_SNAKE_CASE_: Any = pointer.convolution.weight SCREAMING_SNAKE_CASE_: Any = pointer.normalization.bias SCREAMING_SNAKE_CASE_: str = pointer.normalization.weight SCREAMING_SNAKE_CASE_: Dict = pointer.normalization.running_mean SCREAMING_SNAKE_CASE_: Optional[Any] = pointer.normalization.running_var SCREAMING_SNAKE_CASE_: Tuple = backbone.layer[pt_index + 1] SCREAMING_SNAKE_CASE_: List[str] = f"MobilenetV1/Conv2d_{tf_index}_pointwise/" SCREAMING_SNAKE_CASE_: int = pointer.convolution.weight SCREAMING_SNAKE_CASE_: Any = pointer.normalization.bias SCREAMING_SNAKE_CASE_: Optional[int] = pointer.normalization.weight SCREAMING_SNAKE_CASE_: Optional[Any] = pointer.normalization.running_mean SCREAMING_SNAKE_CASE_: Dict = pointer.normalization.running_var if isinstance(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: List[str] = "MobilenetV1/Logits/Conv2d_1c_1x1/" SCREAMING_SNAKE_CASE_: Optional[Any] = model.classifier.weight SCREAMING_SNAKE_CASE_: Tuple = model.classifier.bias return tf_to_pt_map def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): try: import numpy as np import tensorflow as tf except ImportError: logger.error( "Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see " "https://www.tensorflow.org/install/ for installation instructions." ) raise # Load weights from TF model SCREAMING_SNAKE_CASE_: int = tf.train.list_variables(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: int = {} for name, shape in init_vars: logger.info(f"Loading TF weight {name} with shape {shape}" ) SCREAMING_SNAKE_CASE_: Any = tf.train.load_variable(_UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Union[str, Any] = array # Build TF to PyTorch weights loading map SCREAMING_SNAKE_CASE_: Optional[Any] = _build_tf_to_pytorch_map(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) for name, pointer in tf_to_pt_map.items(): logger.info(f"Importing {name}" ) if name not in tf_weights: logger.info(f"{name} not in tf pre-trained weights, skipping" ) continue SCREAMING_SNAKE_CASE_: int = tf_weights[name] if "depthwise_weights" in name: logger.info("Transposing depthwise" ) SCREAMING_SNAKE_CASE_: int = np.transpose(_UpperCAmelCase , (2, 3, 0, 1) ) elif "weights" in name: logger.info("Transposing" ) if len(pointer.shape ) == 2: # copying into linear layer SCREAMING_SNAKE_CASE_: List[str] = array.squeeze().transpose() else: SCREAMING_SNAKE_CASE_: Any = np.transpose(_UpperCAmelCase , (3, 2, 0, 1) ) if pointer.shape != array.shape: raise ValueError(f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched" ) logger.info(f"Initialize PyTorch weight {name} {array.shape}" ) SCREAMING_SNAKE_CASE_: int = torch.from_numpy(_UpperCAmelCase ) tf_weights.pop(_UpperCAmelCase , _UpperCAmelCase ) tf_weights.pop(name + "/RMSProp" , _UpperCAmelCase ) tf_weights.pop(name + "/RMSProp_1" , _UpperCAmelCase ) tf_weights.pop(name + "/ExponentialMovingAverage" , _UpperCAmelCase ) logger.info(f"Weights not copied to PyTorch model: {', '.join(tf_weights.keys() )}" ) return model def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = features.shape[-2:] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = conv_layer.stride SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = conv_layer.kernel_size if in_height % stride_height == 0: SCREAMING_SNAKE_CASE_: int = max(kernel_height - stride_height , 0 ) else: SCREAMING_SNAKE_CASE_: Tuple = max(kernel_height - (in_height % stride_height) , 0 ) if in_width % stride_width == 0: SCREAMING_SNAKE_CASE_: str = max(kernel_width - stride_width , 0 ) else: SCREAMING_SNAKE_CASE_: Dict = max(kernel_width - (in_width % stride_width) , 0 ) SCREAMING_SNAKE_CASE_: str = pad_along_width // 2 SCREAMING_SNAKE_CASE_: Union[str, Any] = pad_along_width - pad_left SCREAMING_SNAKE_CASE_: int = pad_along_height // 2 SCREAMING_SNAKE_CASE_: Tuple = pad_along_height - pad_top SCREAMING_SNAKE_CASE_: Union[str, Any] = (pad_left, pad_right, pad_top, pad_bottom) return nn.functional.pad(_UpperCAmelCase , _UpperCAmelCase , "constant" , 0.0 ) class __lowercase ( nn.Module ): """simple docstring""" def __init__( self : Optional[int] , lowerCAmelCase__ : MobileNetVaConfig , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : Optional[int] = 1 , lowerCAmelCase__ : Optional[int] = 1 , lowerCAmelCase__ : bool = False , lowerCAmelCase__ : Optional[bool] = True , lowerCAmelCase__ : Optional[bool or str] = True , ): super().__init__() SCREAMING_SNAKE_CASE_: Optional[int] = config if in_channels % groups != 0: raise ValueError(F"Input channels ({in_channels}) are not divisible by {groups} groups.") if out_channels % groups != 0: raise ValueError(F"Output channels ({out_channels}) are not divisible by {groups} groups.") SCREAMING_SNAKE_CASE_: int = 0 if config.tf_padding else int((kernel_size - 1) / 2) SCREAMING_SNAKE_CASE_: Union[str, Any] = nn.Convad( in_channels=lowerCAmelCase__ , out_channels=lowerCAmelCase__ , kernel_size=lowerCAmelCase__ , stride=lowerCAmelCase__ , padding=lowerCAmelCase__ , groups=lowerCAmelCase__ , bias=lowerCAmelCase__ , padding_mode="zeros" , ) if use_normalization: SCREAMING_SNAKE_CASE_: str = nn.BatchNormad( num_features=lowerCAmelCase__ , eps=config.layer_norm_eps , momentum=0.9997 , affine=lowerCAmelCase__ , track_running_stats=lowerCAmelCase__ , ) else: SCREAMING_SNAKE_CASE_: str = None if use_activation: if isinstance(lowerCAmelCase__ , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Dict = ACTaFN[use_activation] elif isinstance(config.hidden_act , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Dict = ACTaFN[config.hidden_act] else: SCREAMING_SNAKE_CASE_: Any = config.hidden_act else: SCREAMING_SNAKE_CASE_: int = None def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : torch.Tensor): if self.config.tf_padding: SCREAMING_SNAKE_CASE_: Union[str, Any] = apply_tf_padding(lowerCAmelCase__ , self.convolution) SCREAMING_SNAKE_CASE_: Optional[int] = self.convolution(lowerCAmelCase__) if self.normalization is not None: SCREAMING_SNAKE_CASE_: int = self.normalization(lowerCAmelCase__) if self.activation is not None: SCREAMING_SNAKE_CASE_: List[Any] = self.activation(lowerCAmelCase__) return features class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : List[str] = MobileNetVaConfig _UpperCAmelCase : List[Any] = load_tf_weights_in_mobilenet_va _UpperCAmelCase : List[Any] = '''mobilenet_v1''' _UpperCAmelCase : int = '''pixel_values''' _UpperCAmelCase : List[Any] = False def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : Union[nn.Linear, nn.Convad]): if isinstance(lowerCAmelCase__ , (nn.Linear, nn.Convad)): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range) if module.bias is not None: module.bias.data.zero_() elif isinstance(lowerCAmelCase__ , nn.BatchNormad): module.bias.data.zero_() module.weight.data.fill_(1.0) lowerCAmelCase : Any = R""" This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior. Parameters: config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights. """ lowerCAmelCase : List[str] = R""" Args: pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`MobileNetV1ImageProcessor.__call__`] for details. output_hidden_states (`bool`, *optional*): Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for more detail. return_dict (`bool`, *optional*): Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. """ @add_start_docstrings( '''The bare MobileNetV1 model outputting raw hidden-states without any specific head on top.''' , UpperCAmelCase_ , ) class __lowercase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Dict , lowerCAmelCase__ : MobileNetVaConfig , lowerCAmelCase__ : bool = True): super().__init__(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = config SCREAMING_SNAKE_CASE_: Union[str, Any] = 32 SCREAMING_SNAKE_CASE_: Dict = max(int(depth * config.depth_multiplier) , config.min_depth) SCREAMING_SNAKE_CASE_: Tuple = MobileNetVaConvLayer( lowerCAmelCase__ , in_channels=config.num_channels , out_channels=lowerCAmelCase__ , kernel_size=3 , stride=2 , ) SCREAMING_SNAKE_CASE_: Optional[int] = [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1] SCREAMING_SNAKE_CASE_: str = nn.ModuleList() for i in range(13): SCREAMING_SNAKE_CASE_: List[Any] = out_channels if strides[i] == 2 or i == 0: depth *= 2 SCREAMING_SNAKE_CASE_: str = max(int(depth * config.depth_multiplier) , config.min_depth) self.layer.append( MobileNetVaConvLayer( lowerCAmelCase__ , in_channels=lowerCAmelCase__ , out_channels=lowerCAmelCase__ , kernel_size=3 , stride=strides[i] , groups=lowerCAmelCase__ , )) self.layer.append( MobileNetVaConvLayer( lowerCAmelCase__ , in_channels=lowerCAmelCase__ , out_channels=lowerCAmelCase__ , kernel_size=1 , )) SCREAMING_SNAKE_CASE_: List[str] = nn.AdaptiveAvgPoolad((1, 1)) if add_pooling_layer else None # Initialize weights and apply final processing self.post_init() def _SCREAMING_SNAKE_CASE ( self : int , lowerCAmelCase__ : str): raise NotImplementedError @add_start_docstrings_to_model_forward(lowerCAmelCase__) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=lowerCAmelCase__ , config_class=_CONFIG_FOR_DOC , modality="vision" , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : Optional[torch.Tensor] = None , lowerCAmelCase__ : Optional[bool] = None , lowerCAmelCase__ : Optional[bool] = None , ): SCREAMING_SNAKE_CASE_: Optional[int] = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) SCREAMING_SNAKE_CASE_: Any = return_dict if return_dict is not None else self.config.use_return_dict if pixel_values is None: raise ValueError("You have to specify pixel_values") SCREAMING_SNAKE_CASE_: Optional[Any] = self.conv_stem(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = () if output_hidden_states else None for i, layer_module in enumerate(self.layer): SCREAMING_SNAKE_CASE_: Tuple = layer_module(lowerCAmelCase__) if output_hidden_states: SCREAMING_SNAKE_CASE_: Optional[int] = all_hidden_states + (hidden_states,) SCREAMING_SNAKE_CASE_: Optional[Any] = hidden_states if self.pooler is not None: SCREAMING_SNAKE_CASE_: int = torch.flatten(self.pooler(lowerCAmelCase__) , start_dim=1) else: SCREAMING_SNAKE_CASE_: List[str] = None if not return_dict: return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None) return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=lowerCAmelCase__ , pooler_output=lowerCAmelCase__ , hidden_states=lowerCAmelCase__ , ) @add_start_docstrings( ''' MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. ''' , UpperCAmelCase_ , ) class __lowercase ( UpperCAmelCase_ ): """simple docstring""" def __init__( self : Any , lowerCAmelCase__ : MobileNetVaConfig): super().__init__(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = config.num_labels SCREAMING_SNAKE_CASE_: Dict = MobileNetVaModel(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = self.mobilenet_va.layer[-1].convolution.out_channels # Classifier head SCREAMING_SNAKE_CASE_: str = nn.Dropout(config.classifier_dropout_prob , inplace=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = nn.Linear(lowerCAmelCase__ , config.num_labels) if config.num_labels > 0 else nn.Identity() # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(lowerCAmelCase__) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=lowerCAmelCase__ , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : Optional[torch.Tensor] = None , lowerCAmelCase__ : Optional[bool] = None , lowerCAmelCase__ : Optional[torch.Tensor] = None , lowerCAmelCase__ : Optional[bool] = None , ): SCREAMING_SNAKE_CASE_: List[str] = return_dict if return_dict is not None else self.config.use_return_dict SCREAMING_SNAKE_CASE_: List[str] = self.mobilenet_va(lowerCAmelCase__ , output_hidden_states=lowerCAmelCase__ , return_dict=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = outputs.pooler_output if return_dict else outputs[1] SCREAMING_SNAKE_CASE_: Tuple = self.classifier(self.dropout(lowerCAmelCase__)) SCREAMING_SNAKE_CASE_: Optional[int] = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: SCREAMING_SNAKE_CASE_: List[Any] = "regression" elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): SCREAMING_SNAKE_CASE_: int = "single_label_classification" else: SCREAMING_SNAKE_CASE_: str = "multi_label_classification" if self.config.problem_type == "regression": SCREAMING_SNAKE_CASE_: Dict = MSELoss() if self.num_labels == 1: SCREAMING_SNAKE_CASE_: Any = loss_fct(logits.squeeze() , labels.squeeze()) else: SCREAMING_SNAKE_CASE_: int = loss_fct(lowerCAmelCase__ , lowerCAmelCase__) elif self.config.problem_type == "single_label_classification": SCREAMING_SNAKE_CASE_: Any = CrossEntropyLoss() SCREAMING_SNAKE_CASE_: Dict = loss_fct(logits.view(-1 , self.num_labels) , labels.view(-1)) elif self.config.problem_type == "multi_label_classification": SCREAMING_SNAKE_CASE_: Dict = BCEWithLogitsLoss() SCREAMING_SNAKE_CASE_: Dict = loss_fct(lowerCAmelCase__ , lowerCAmelCase__) if not return_dict: SCREAMING_SNAKE_CASE_: int = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return ImageClassifierOutputWithNoAttention( loss=lowerCAmelCase__ , logits=lowerCAmelCase__ , hidden_states=outputs.hidden_states , )
13
1
import os import posixpath import uuid from dataclasses import dataclass from typing import TYPE_CHECKING, Iterable, List, Optional, Tuple, Union import numpy as np import pyarrow as pa import datasets from datasets.arrow_writer import ArrowWriter, ParquetWriter from datasets.config import MAX_SHARD_SIZE from datasets.filesystems import ( is_remote_filesystem, rename, ) from datasets.iterable_dataset import _BaseExamplesIterable from datasets.utils.py_utils import convert_file_size_to_int lowerCAmelCase : Any = datasets.utils.logging.get_logger(__name__) if TYPE_CHECKING: import pyspark @dataclass class __lowercase ( datasets.BuilderConfig ): """simple docstring""" _UpperCAmelCase : Optional[datasets.Features] = None def A_ ( _UpperCAmelCase , _UpperCAmelCase , ): import pyspark def generate_fn(): SCREAMING_SNAKE_CASE_: List[Any] = df.select("*" , pyspark.sql.functions.spark_partition_id().alias("part_id" ) ) for partition_id in partition_order: SCREAMING_SNAKE_CASE_: Optional[Any] = df_with_partition_id.select("*" ).where(f"part_id = {partition_id}" ).drop("part_id" ) SCREAMING_SNAKE_CASE_: int = partition_df.collect() SCREAMING_SNAKE_CASE_: Optional[int] = 0 for row in rows: yield f"{partition_id}_{row_id}", row.asDict() row_id += 1 return generate_fn class __lowercase ( _BaseExamplesIterable ): """simple docstring""" def __init__( self : Optional[Any] , lowerCAmelCase__ : "pyspark.sql.DataFrame" , lowerCAmelCase__ : Optional[Any]=None , ): SCREAMING_SNAKE_CASE_: Optional[Any] = df SCREAMING_SNAKE_CASE_: Tuple = partition_order or range(self.df.rdd.getNumPartitions()) SCREAMING_SNAKE_CASE_: Tuple = _generate_iterable_examples(self.df , self.partition_order) def __iter__( self : Optional[Any]): yield from self.generate_examples_fn() def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : np.random.Generator): SCREAMING_SNAKE_CASE_: List[str] = list(range(self.df.rdd.getNumPartitions())) generator.shuffle(lowerCAmelCase__) return SparkExamplesIterable(self.df , partition_order=lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : int , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: str = self.split_shard_indices_by_worker(lowerCAmelCase__ , lowerCAmelCase__) return SparkExamplesIterable(self.df , partition_order=lowerCAmelCase__) @property def _SCREAMING_SNAKE_CASE ( self : Tuple): return len(self.partition_order) class __lowercase ( datasets.DatasetBuilder ): """simple docstring""" _UpperCAmelCase : Union[str, Any] = SparkConfig def __init__( self : Union[str, Any] , lowerCAmelCase__ : "pyspark.sql.DataFrame" , lowerCAmelCase__ : str = None , lowerCAmelCase__ : str = None , **lowerCAmelCase__ : Any , ): import pyspark SCREAMING_SNAKE_CASE_: str = pyspark.sql.SparkSession.builder.getOrCreate() SCREAMING_SNAKE_CASE_: List[str] = df SCREAMING_SNAKE_CASE_: Optional[Any] = working_dir super().__init__( cache_dir=lowerCAmelCase__ , config_name=str(self.df.semanticHash()) , **lowerCAmelCase__ , ) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): # Returns the path of the created file. def create_cache_and_write_probe(lowerCAmelCase__ : int): # makedirs with exist_ok will recursively create the directory. It will not throw an error if directories # already exist. os.makedirs(self._cache_dir , exist_ok=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = os.path.join(self._cache_dir , "fs_test" + uuid.uuida().hex) # Opening the file in append mode will create a new file unless it already exists, in which case it will not # change the file contents. open(lowerCAmelCase__ , "a") return [probe_file] if self._spark.conf.get("spark.master" , "").startswith("local"): return # If the cluster is multi-node, make sure that the user provided a cache_dir and that it is on an NFS # accessible to the driver. # TODO: Stream batches to the driver using ArrowCollectSerializer instead of throwing an error. if self._cache_dir: SCREAMING_SNAKE_CASE_: Dict = ( self._spark.sparkContext.parallelize(range(1) , 1).mapPartitions(lowerCAmelCase__).collect() ) if os.path.isfile(probe[0]): return raise ValueError( "When using Dataset.from_spark on a multi-node cluster, the driver and all workers should be able to access cache_dir") def _SCREAMING_SNAKE_CASE ( self : Any): return datasets.DatasetInfo(features=self.config.features) def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : datasets.download.download_manager.DownloadManager): return [datasets.SplitGenerator(name=datasets.Split.TRAIN)] def _SCREAMING_SNAKE_CASE ( self : Any , lowerCAmelCase__ : Optional[Any]): import pyspark def get_arrow_batch_size(lowerCAmelCase__ : Optional[int]): for batch in it: yield pa.RecordBatch.from_pydict({"batch_bytes": [batch.nbytes]}) SCREAMING_SNAKE_CASE_: Optional[Any] = self.df.count() SCREAMING_SNAKE_CASE_: Tuple = df_num_rows if df_num_rows <= 100 else 100 # Approximate the size of each row (in Arrow format) by averaging over a max-100-row sample. SCREAMING_SNAKE_CASE_: Optional[int] = ( self.df.limit(lowerCAmelCase__) .repartition(1) .mapInArrow(lowerCAmelCase__ , "batch_bytes: long") .agg(pyspark.sql.functions.sum("batch_bytes").alias("sample_bytes")) .collect()[0] .sample_bytes / sample_num_rows ) SCREAMING_SNAKE_CASE_: Union[str, Any] = approx_bytes_per_row * df_num_rows if approx_total_size > max_shard_size: # Make sure there is at least one row per partition. SCREAMING_SNAKE_CASE_: Any = min(lowerCAmelCase__ , int(approx_total_size / max_shard_size)) SCREAMING_SNAKE_CASE_: Any = self.df.repartition(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : str , lowerCAmelCase__ : int , ): import pyspark SCREAMING_SNAKE_CASE_: Dict = ParquetWriter if file_format == "parquet" else ArrowWriter SCREAMING_SNAKE_CASE_: Tuple = os.path.join(self._working_dir , os.path.basename(lowerCAmelCase__)) if self._working_dir else fpath SCREAMING_SNAKE_CASE_: str = file_format == "parquet" # Define these so that we don't reference self in write_arrow, which will result in a pickling error due to # pickling the SparkContext. SCREAMING_SNAKE_CASE_: List[Any] = self.config.features SCREAMING_SNAKE_CASE_: List[Any] = self._writer_batch_size SCREAMING_SNAKE_CASE_: Optional[Any] = self._fs.storage_options def write_arrow(lowerCAmelCase__ : Optional[Any]): # Within the same SparkContext, no two task attempts will share the same attempt ID. SCREAMING_SNAKE_CASE_: Tuple = pyspark.TaskContext().taskAttemptId() SCREAMING_SNAKE_CASE_: Optional[Any] = next(lowerCAmelCase__ , lowerCAmelCase__) if first_batch is None: # Some partitions might not receive any data. return pa.RecordBatch.from_arrays( [[task_id], [0], [0]] , names=["task_id", "num_examples", "num_bytes"] , ) SCREAMING_SNAKE_CASE_: Dict = 0 SCREAMING_SNAKE_CASE_: Dict = writer_class( features=lowerCAmelCase__ , path=working_fpath.replace("SSSSS" , F"{shard_id:05d}").replace("TTTTT" , F"{task_id:05d}") , writer_batch_size=lowerCAmelCase__ , storage_options=lowerCAmelCase__ , embed_local_files=lowerCAmelCase__ , ) SCREAMING_SNAKE_CASE_: List[str] = pa.Table.from_batches([first_batch]) writer.write_table(lowerCAmelCase__) for batch in it: if max_shard_size is not None and writer._num_bytes >= max_shard_size: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = writer.finalize() writer.close() yield pa.RecordBatch.from_arrays( [[task_id], [num_examples], [num_bytes]] , names=["task_id", "num_examples", "num_bytes"] , ) shard_id += 1 SCREAMING_SNAKE_CASE_: Optional[int] = writer_class( features=writer._features , path=working_fpath.replace("SSSSS" , F"{shard_id:05d}").replace("TTTTT" , F"{task_id:05d}") , writer_batch_size=lowerCAmelCase__ , storage_options=lowerCAmelCase__ , embed_local_files=lowerCAmelCase__ , ) SCREAMING_SNAKE_CASE_: Tuple = pa.Table.from_batches([batch]) writer.write_table(lowerCAmelCase__) if writer._num_bytes > 0: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = writer.finalize() writer.close() yield pa.RecordBatch.from_arrays( [[task_id], [num_examples], [num_bytes]] , names=["task_id", "num_examples", "num_bytes"] , ) if working_fpath != fpath: for file in os.listdir(os.path.dirname(lowerCAmelCase__)): SCREAMING_SNAKE_CASE_: List[Any] = os.path.join(os.path.dirname(lowerCAmelCase__) , os.path.basename(lowerCAmelCase__)) shutil.move(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = ( self.df.mapInArrow(lowerCAmelCase__ , "task_id: long, num_examples: long, num_bytes: long") .groupBy("task_id") .agg( pyspark.sql.functions.sum("num_examples").alias("total_num_examples") , pyspark.sql.functions.sum("num_bytes").alias("total_num_bytes") , pyspark.sql.functions.count("num_bytes").alias("num_shards") , pyspark.sql.functions.collect_list("num_examples").alias("shard_lengths") , ) .collect() ) for row in stats: yield row.task_id, (row.total_num_examples, row.total_num_bytes, row.num_shards, row.shard_lengths) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : "datasets.SplitGenerator" , lowerCAmelCase__ : str = "arrow" , lowerCAmelCase__ : Optional[Union[str, int]] = None , lowerCAmelCase__ : Optional[int] = None , **lowerCAmelCase__ : Union[str, Any] , ): self._validate_cache_dir() SCREAMING_SNAKE_CASE_: Dict = convert_file_size_to_int(max_shard_size or MAX_SHARD_SIZE) self._repartition_df_if_needed(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = not is_remote_filesystem(self._fs) SCREAMING_SNAKE_CASE_: List[str] = os.path.join if is_local else posixpath.join SCREAMING_SNAKE_CASE_: List[Any] = "-TTTTT-SSSSS-of-NNNNN" SCREAMING_SNAKE_CASE_: int = F"{self.name}-{split_generator.name}{SUFFIX}.{file_format}" SCREAMING_SNAKE_CASE_: Any = path_join(self._output_dir , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = 0 SCREAMING_SNAKE_CASE_: Any = 0 SCREAMING_SNAKE_CASE_: Union[str, Any] = 0 SCREAMING_SNAKE_CASE_: Optional[Any] = [] SCREAMING_SNAKE_CASE_: Tuple = [] for task_id, content in self._prepare_split_single(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__): ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ): List[str] = content if num_bytes > 0: total_num_examples += num_examples total_num_bytes += num_bytes total_shards += num_shards task_id_and_num_shards.append((task_id, num_shards)) all_shard_lengths.extend(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = total_num_examples SCREAMING_SNAKE_CASE_: Any = total_num_bytes # should rename everything at the end logger.debug(F"Renaming {total_shards} shards.") if total_shards > 1: SCREAMING_SNAKE_CASE_: int = all_shard_lengths # Define fs outside of _rename_shard so that we don't reference self in the function, which will result in a # pickling error due to pickling the SparkContext. SCREAMING_SNAKE_CASE_: Tuple = self._fs # use the -SSSSS-of-NNNNN pattern def _rename_shard( lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : int , ): rename( lowerCAmelCase__ , fpath.replace("SSSSS" , F"{shard_id:05d}").replace("TTTTT" , F"{task_id:05d}") , fpath.replace("TTTTT-SSSSS" , F"{global_shard_id:05d}").replace("NNNNN" , F"{total_shards:05d}") , ) SCREAMING_SNAKE_CASE_: Tuple = [] SCREAMING_SNAKE_CASE_: Tuple = 0 for i in range(len(lowerCAmelCase__)): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = task_id_and_num_shards[i] for shard_id in range(lowerCAmelCase__): args.append([task_id, shard_id, global_shard_id]) global_shard_id += 1 self._spark.sparkContext.parallelize(lowerCAmelCase__ , len(lowerCAmelCase__)).map(lambda lowerCAmelCase__: _rename_shard(*lowerCAmelCase__)).collect() else: # don't use any pattern SCREAMING_SNAKE_CASE_: Optional[int] = 0 SCREAMING_SNAKE_CASE_: Optional[Any] = task_id_and_num_shards[0][0] self._rename( fpath.replace("SSSSS" , F"{shard_id:05d}").replace("TTTTT" , F"{task_id:05d}") , fpath.replace(lowerCAmelCase__ , "") , ) def _SCREAMING_SNAKE_CASE ( self : List[Any] , lowerCAmelCase__ : "datasets.SplitGenerator" , ): return SparkExamplesIterable(self.df)
13
def A_ ( _UpperCAmelCase , _UpperCAmelCase = False ): if not isinstance(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: str = f"Expected string as input, found {type(_UpperCAmelCase )}" raise ValueError(_UpperCAmelCase ) if not isinstance(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Optional[Any] = f"Expected boolean as use_pascal parameter, found {type(_UpperCAmelCase )}" raise ValueError(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Tuple = input_str.split("_" ) SCREAMING_SNAKE_CASE_: str = 0 if use_pascal else 1 SCREAMING_SNAKE_CASE_: int = words[start_index:] SCREAMING_SNAKE_CASE_: List[str] = [word[0].upper() + word[1:] for word in words_to_capitalize] SCREAMING_SNAKE_CASE_: List[Any] = "" if use_pascal else words[0] return "".join([initial_word, *capitalized_words] ) if __name__ == "__main__": from doctest import testmod testmod()
13
1
def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: list[list[int]] = [[0 for _ in range(_UpperCAmelCase )] for _ in range(m + 1 )] for i in range(m + 1 ): SCREAMING_SNAKE_CASE_: List[str] = 1 for n in range(m + 1 ): for k in range(1 , _UpperCAmelCase ): memo[n][k] += memo[n][k - 1] if n - k > 0: memo[n][k] += memo[n - k - 1][k] return memo[m][m - 1] if __name__ == "__main__": import sys if len(sys.argv) == 1: try: lowerCAmelCase : Union[str, Any] = int(input("""Enter a number: """).strip()) print(partition(n)) except ValueError: print("""Please enter a number.""") else: try: lowerCAmelCase : Optional[Any] = int(sys.argv[1]) print(partition(n)) except ValueError: print("""Please pass a number.""")
13
import os import tempfile import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from torch import nn from transformers import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_inverse_sqrt_schedule, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) def A_ ( _UpperCAmelCase , _UpperCAmelCase=10 ): SCREAMING_SNAKE_CASE_: Union[str, Any] = [] for _ in range(_UpperCAmelCase ): lrs.append(scheduler.get_lr()[0] ) scheduler.step() return lrs def A_ ( _UpperCAmelCase , _UpperCAmelCase=10 ): SCREAMING_SNAKE_CASE_: List[str] = [] for step in range(_UpperCAmelCase ): lrs.append(scheduler.get_lr()[0] ) scheduler.step() if step == num_steps // 2: with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE_: Optional[int] = os.path.join(_UpperCAmelCase , "schedule.bin" ) torch.save(scheduler.state_dict() , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Optional[Any] = torch.load(_UpperCAmelCase ) scheduler.load_state_dict(_UpperCAmelCase ) return lrs @require_torch class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : int , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Tuple): self.assertEqual(len(lowerCAmelCase__) , len(lowerCAmelCase__)) for a, b in zip(lowerCAmelCase__ , lowerCAmelCase__): self.assertAlmostEqual(lowerCAmelCase__ , lowerCAmelCase__ , delta=lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.tensor([0.1, -0.2, -0.1] , requires_grad=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = torch.tensor([0.4, 0.2, -0.5]) SCREAMING_SNAKE_CASE_: Optional[Any] = nn.MSELoss() # No warmup, constant schedule, no gradient clipping SCREAMING_SNAKE_CASE_: int = AdamW(params=[w] , lr=2E-1 , weight_decay=0.0) for _ in range(100): SCREAMING_SNAKE_CASE_: Dict = criterion(lowerCAmelCase__ , lowerCAmelCase__) loss.backward() optimizer.step() w.grad.detach_() # No zero_grad() function on simple tensors. we do it ourselves. w.grad.zero_() self.assertListAlmostEqual(w.tolist() , [0.4, 0.2, -0.5] , tol=1E-2) def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.tensor([0.1, -0.2, -0.1] , requires_grad=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = torch.tensor([0.4, 0.2, -0.5]) SCREAMING_SNAKE_CASE_: Any = nn.MSELoss() # No warmup, constant schedule, no gradient clipping SCREAMING_SNAKE_CASE_: int = Adafactor( params=[w] , lr=1E-2 , eps=(1E-30, 1E-3) , clip_threshold=1.0 , decay_rate=-0.8 , betaa=lowerCAmelCase__ , weight_decay=0.0 , relative_step=lowerCAmelCase__ , scale_parameter=lowerCAmelCase__ , warmup_init=lowerCAmelCase__ , ) for _ in range(1000): SCREAMING_SNAKE_CASE_: List[Any] = criterion(lowerCAmelCase__ , lowerCAmelCase__) loss.backward() optimizer.step() w.grad.detach_() # No zero_grad() function on simple tensors. we do it ourselves. w.grad.zero_() self.assertListAlmostEqual(w.tolist() , [0.4, 0.2, -0.5] , tol=1E-2) @require_torch class __lowercase ( unittest.TestCase ): """simple docstring""" _UpperCAmelCase : Union[str, Any] = nn.Linear(50 , 50 ) if is_torch_available() else None _UpperCAmelCase : List[Any] = AdamW(m.parameters() , lr=10.0 ) if is_torch_available() else None _UpperCAmelCase : Optional[Any] = 10 def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : Optional[Any]=None): self.assertEqual(len(lowerCAmelCase__) , len(lowerCAmelCase__)) for a, b in zip(lowerCAmelCase__ , lowerCAmelCase__): self.assertAlmostEqual(lowerCAmelCase__ , lowerCAmelCase__ , delta=lowerCAmelCase__ , msg=lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Dict = {"num_warmup_steps": 2, "num_training_steps": 10} # schedulers doct format # function: (sched_args_dict, expected_learning_rates) SCREAMING_SNAKE_CASE_: Dict = { get_constant_schedule: ({}, [10.0] * self.num_steps), get_constant_schedule_with_warmup: ( {"num_warmup_steps": 4}, [0.0, 2.5, 5.0, 7.5, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0], ), get_linear_schedule_with_warmup: ( {**common_kwargs}, [0.0, 5.0, 10.0, 8.75, 7.5, 6.25, 5.0, 3.75, 2.5, 1.25], ), get_cosine_schedule_with_warmup: ( {**common_kwargs}, [0.0, 5.0, 10.0, 9.61, 8.53, 6.91, 5.0, 3.08, 1.46, 0.38], ), get_cosine_with_hard_restarts_schedule_with_warmup: ( {**common_kwargs, "num_cycles": 2}, [0.0, 5.0, 10.0, 8.53, 5.0, 1.46, 10.0, 8.53, 5.0, 1.46], ), get_polynomial_decay_schedule_with_warmup: ( {**common_kwargs, "power": 2.0, "lr_end": 1E-7}, [0.0, 5.0, 10.0, 7.656, 5.625, 3.906, 2.5, 1.406, 0.625, 0.156], ), get_inverse_sqrt_schedule: ( {"num_warmup_steps": 2}, [0.0, 5.0, 10.0, 8.165, 7.071, 6.325, 5.774, 5.345, 5.0, 4.714], ), } for scheduler_func, data in scheds.items(): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = data SCREAMING_SNAKE_CASE_: List[Any] = scheduler_func(self.optimizer , **lowerCAmelCase__) self.assertEqual(len([scheduler.get_lr()[0]]) , 1) SCREAMING_SNAKE_CASE_: int = unwrap_schedule(lowerCAmelCase__ , self.num_steps) self.assertListAlmostEqual( lowerCAmelCase__ , lowerCAmelCase__ , tol=1E-2 , msg=F"failed for {scheduler_func} in normal scheduler" , ) SCREAMING_SNAKE_CASE_: List[str] = scheduler_func(self.optimizer , **lowerCAmelCase__) if scheduler_func.__name__ != "get_constant_schedule": LambdaScheduleWrapper.wrap_scheduler(lowerCAmelCase__) # wrap to test picklability of the schedule SCREAMING_SNAKE_CASE_: Tuple = unwrap_and_save_reload_schedule(lowerCAmelCase__ , self.num_steps) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ , msg=F"failed for {scheduler_func} in save and reload") class __lowercase : """simple docstring""" def __init__( self : str , lowerCAmelCase__ : List[str]): SCREAMING_SNAKE_CASE_: List[Any] = fn def __call__( self : Optional[int] , *lowerCAmelCase__ : List[Any] , **lowerCAmelCase__ : Tuple): return self.fn(*lowerCAmelCase__ , **lowerCAmelCase__) @classmethod def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : str): SCREAMING_SNAKE_CASE_: str = list(map(self , scheduler.lr_lambdas))
13
1
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase : Any = logging.get_logger(__name__) lowerCAmelCase : List[Any] = { """google/vivit-b-16x2-kinetics400""": ( """https://huggingface.co/google/vivit-b-16x2-kinetics400/resolve/main/config.json""" ), # See all Vivit models at https://huggingface.co/models?filter=vivit } class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : List[str] = '''vivit''' def __init__( self : Optional[Any] , lowerCAmelCase__ : Dict=224 , lowerCAmelCase__ : str=32 , lowerCAmelCase__ : int=[2, 16, 16] , lowerCAmelCase__ : int=3 , lowerCAmelCase__ : Any=768 , lowerCAmelCase__ : Union[str, Any]=12 , lowerCAmelCase__ : str=12 , lowerCAmelCase__ : Tuple=3072 , lowerCAmelCase__ : Optional[int]="gelu_fast" , lowerCAmelCase__ : Tuple=0.0 , lowerCAmelCase__ : Union[str, Any]=0.0 , lowerCAmelCase__ : Dict=0.02 , lowerCAmelCase__ : List[str]=1E-06 , lowerCAmelCase__ : Dict=True , **lowerCAmelCase__ : str , ): SCREAMING_SNAKE_CASE_: Any = hidden_size SCREAMING_SNAKE_CASE_: str = num_hidden_layers SCREAMING_SNAKE_CASE_: Optional[Any] = num_attention_heads SCREAMING_SNAKE_CASE_: Union[str, Any] = intermediate_size SCREAMING_SNAKE_CASE_: Any = hidden_act SCREAMING_SNAKE_CASE_: str = hidden_dropout_prob SCREAMING_SNAKE_CASE_: int = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_: Dict = initializer_range SCREAMING_SNAKE_CASE_: int = layer_norm_eps SCREAMING_SNAKE_CASE_: Union[str, Any] = image_size SCREAMING_SNAKE_CASE_: Any = num_frames SCREAMING_SNAKE_CASE_: Optional[Any] = tubelet_size SCREAMING_SNAKE_CASE_: int = num_channels SCREAMING_SNAKE_CASE_: Optional[Any] = qkv_bias super().__init__(**lowerCAmelCase__)
13
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, Features, Value from .base import TaskTemplate @dataclass(frozen=UpperCAmelCase_ ) class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : str = field(default='''automatic-speech-recognition''' , metadata={'''include_in_asdict_even_if_is_default''': True} ) _UpperCAmelCase : ClassVar[Features] = Features({'''audio''': Audio()} ) _UpperCAmelCase : ClassVar[Features] = Features({'''transcription''': Value('''string''' )} ) _UpperCAmelCase : str = "audio" _UpperCAmelCase : str = "transcription" def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int): if self.audio_column not in features: raise ValueError(F"Column {self.audio_column} is not present in features.") if not isinstance(features[self.audio_column] , lowerCAmelCase__): raise ValueError(F"Column {self.audio_column} is not an Audio type.") SCREAMING_SNAKE_CASE_: Tuple = copy.deepcopy(self) SCREAMING_SNAKE_CASE_: Optional[int] = self.input_schema.copy() SCREAMING_SNAKE_CASE_: Dict = features[self.audio_column] SCREAMING_SNAKE_CASE_: int = input_schema return task_template @property def _SCREAMING_SNAKE_CASE ( self : int): return {self.audio_column: "audio", self.transcription_column: "transcription"}
13
1
import functools def A_ ( _UpperCAmelCase , _UpperCAmelCase ): # Validation if not isinstance(_UpperCAmelCase , _UpperCAmelCase ) or not all(isinstance(_UpperCAmelCase , _UpperCAmelCase ) for day in days ): raise ValueError("The parameter days should be a list of integers" ) if len(_UpperCAmelCase ) != 3 or not all(isinstance(_UpperCAmelCase , _UpperCAmelCase ) for cost in costs ): raise ValueError("The parameter costs should be a list of three integers" ) if len(_UpperCAmelCase ) == 0: return 0 if min(_UpperCAmelCase ) <= 0: raise ValueError("All days elements should be greater than 0" ) if max(_UpperCAmelCase ) >= 3_66: raise ValueError("All days elements should be less than 366" ) SCREAMING_SNAKE_CASE_: Any = set(_UpperCAmelCase ) @functools.cache def dynamic_programming(_UpperCAmelCase ) -> int: if index > 3_65: return 0 if index not in days_set: return dynamic_programming(index + 1 ) return min( costs[0] + dynamic_programming(index + 1 ) , costs[1] + dynamic_programming(index + 7 ) , costs[2] + dynamic_programming(index + 30 ) , ) return dynamic_programming(1 ) if __name__ == "__main__": import doctest doctest.testmod()
13
import unittest import numpy as np from transformers import is_flax_available from transformers.testing_utils import require_flax from ..test_modeling_flax_common import ids_tensor if is_flax_available(): import jax import jax.numpy as jnp from transformers.generation import ( FlaxForcedBOSTokenLogitsProcessor, FlaxForcedEOSTokenLogitsProcessor, FlaxLogitsProcessorList, FlaxMinLengthLogitsProcessor, FlaxTemperatureLogitsWarper, FlaxTopKLogitsWarper, FlaxTopPLogitsWarper, ) @require_flax class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : int , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: str = jnp.ones((batch_size, length)) / length return scores def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Dict = None SCREAMING_SNAKE_CASE_: str = 20 SCREAMING_SNAKE_CASE_: List[Any] = self._get_uniform_logits(batch_size=2 , length=lowerCAmelCase__) # tweak scores to not be uniform anymore SCREAMING_SNAKE_CASE_: List[str] = scores.at[1, 5].set((1 / length) + 0.1) # peak, 1st batch SCREAMING_SNAKE_CASE_: Any = scores.at[1, 10].set((1 / length) - 0.4) # valley, 1st batch # compute softmax SCREAMING_SNAKE_CASE_: Dict = jax.nn.softmax(lowerCAmelCase__ , axis=-1) SCREAMING_SNAKE_CASE_: Optional[Any] = FlaxTemperatureLogitsWarper(temperature=0.5) SCREAMING_SNAKE_CASE_: List[str] = FlaxTemperatureLogitsWarper(temperature=1.3) SCREAMING_SNAKE_CASE_: str = jax.nn.softmax(temp_dist_warper_sharper(lowerCAmelCase__ , scores.copy() , cur_len=lowerCAmelCase__) , axis=-1) SCREAMING_SNAKE_CASE_: int = jax.nn.softmax(temp_dist_warper_smoother(lowerCAmelCase__ , scores.copy() , cur_len=lowerCAmelCase__) , axis=-1) # uniform distribution stays uniform self.assertTrue(jnp.allclose(probs[0, :] , warped_prob_sharp[0, :] , atol=1E-3)) self.assertTrue(jnp.allclose(probs[0, :] , warped_prob_smooth[0, :] , atol=1E-3)) # sharp peaks get higher, valleys get lower self.assertLess(probs[1, :].max() , warped_prob_sharp[1, :].max()) self.assertGreater(probs[1, :].min() , warped_prob_sharp[1, :].min()) # smooth peaks get lower, valleys get higher self.assertGreater(probs[1, :].max() , warped_prob_smooth[1, :].max()) self.assertLess(probs[1, :].min() , warped_prob_smooth[1, :].min()) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: List[str] = None SCREAMING_SNAKE_CASE_: str = 10 SCREAMING_SNAKE_CASE_: Tuple = 2 # create ramp distribution SCREAMING_SNAKE_CASE_: Optional[Any] = np.broadcast_to(np.arange(lowerCAmelCase__)[None, :] , (batch_size, vocab_size)).copy() SCREAMING_SNAKE_CASE_: Dict = ramp_logits[1:, : vocab_size // 2] + vocab_size SCREAMING_SNAKE_CASE_: Union[str, Any] = FlaxTopKLogitsWarper(3) SCREAMING_SNAKE_CASE_: Dict = top_k_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) # check that correct tokens are filtered self.assertListEqual(jnp.isinf(scores[0]).tolist() , 7 * [True] + 3 * [False]) self.assertListEqual(jnp.isinf(scores[1]).tolist() , 2 * [True] + 3 * [False] + 5 * [True]) # check special case SCREAMING_SNAKE_CASE_: Any = 5 SCREAMING_SNAKE_CASE_: str = FlaxTopKLogitsWarper(top_k=1 , filter_value=0.0 , min_tokens_to_keep=3) SCREAMING_SNAKE_CASE_: Any = np.broadcast_to(np.arange(lowerCAmelCase__)[None, :] , (batch_size, length)).copy() SCREAMING_SNAKE_CASE_: Any = top_k_warp_safety_check(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) # min_tokens overwrites k: 3 tokens are kept => 2 tokens are nullified self.assertListEqual((scores == 0.0).sum(axis=-1).tolist() , [2, 2]) def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Tuple = None SCREAMING_SNAKE_CASE_: Dict = 10 SCREAMING_SNAKE_CASE_: Dict = 2 # create distribution and take log (inverse to Softmax as taken in TopPLogitsWarper) SCREAMING_SNAKE_CASE_: Tuple = np.log(np.array([[0.3, 0.1, 0.1, 0.5], [0.15, 0.3, 0.3, 0.25]])) SCREAMING_SNAKE_CASE_: int = FlaxTopPLogitsWarper(0.8) SCREAMING_SNAKE_CASE_: Optional[Any] = np.exp(top_p_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__)) # dist should be filtered to keep min num values so that sum is >= top_p # exp (-inf) => 0 SCREAMING_SNAKE_CASE_: Dict = np.array([[0.3, 0.0, 0.0, 0.5], [0.0, 0.3, 0.3, 0.25]]) self.assertTrue(np.allclose(lowerCAmelCase__ , lowerCAmelCase__ , atol=1E-3)) # check edge cases with negative and extreme logits SCREAMING_SNAKE_CASE_: Union[str, Any] = np.broadcast_to(np.arange(lowerCAmelCase__)[None, :] , (batch_size, vocab_size)).copy() - ( vocab_size // 2 ) # make ramp_logits more extreme SCREAMING_SNAKE_CASE_: Dict = ramp_logits[1] * 100.0 # make sure at least 2 tokens are kept SCREAMING_SNAKE_CASE_: str = FlaxTopPLogitsWarper(0.9 , min_tokens_to_keep=2 , filter_value=0.0) SCREAMING_SNAKE_CASE_: Any = top_p_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) # first batch should keep three tokens, second batch would keep only 1, but due to `min_tokens_to_keep=2` keeps 2. self.assertListEqual((filtered_dist != 0.0).sum(axis=-1).tolist() , [3, 2]) def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Tuple = 20 SCREAMING_SNAKE_CASE_: List[str] = 4 SCREAMING_SNAKE_CASE_: Optional[int] = 0 SCREAMING_SNAKE_CASE_: str = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=lowerCAmelCase__) # check that min length is applied at length 5 SCREAMING_SNAKE_CASE_: str = ids_tensor((batch_size, 20) , vocab_size=20) SCREAMING_SNAKE_CASE_: int = 5 SCREAMING_SNAKE_CASE_: List[Any] = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = min_dist_processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) self.assertListEqual(scores_before_min_length[:, eos_token_id].tolist() , 4 * [-float("inf")]) # check that min length is not applied anymore at length 15 SCREAMING_SNAKE_CASE_: List[str] = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = 15 SCREAMING_SNAKE_CASE_: Any = min_dist_processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) self.assertFalse(jnp.isinf(lowerCAmelCase__).any()) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: int = 20 SCREAMING_SNAKE_CASE_: str = 4 SCREAMING_SNAKE_CASE_: List[Any] = 0 SCREAMING_SNAKE_CASE_: Optional[Any] = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=lowerCAmelCase__) # check that all scores are -inf except the bos_token_id score SCREAMING_SNAKE_CASE_: int = ids_tensor((batch_size, 1) , vocab_size=20) SCREAMING_SNAKE_CASE_: List[str] = 1 SCREAMING_SNAKE_CASE_: Union[str, Any] = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = logits_processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) self.assertTrue(jnp.isneginf(scores[:, bos_token_id + 1 :]).all()) self.assertListEqual(scores[:, bos_token_id].tolist() , 4 * [0]) # score for bos_token_id shold be zero # check that bos_token_id is not forced if current length is greater than 1 SCREAMING_SNAKE_CASE_: List[Any] = 3 SCREAMING_SNAKE_CASE_: Optional[Any] = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = logits_processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) self.assertFalse(jnp.isinf(lowerCAmelCase__).any()) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Any = 20 SCREAMING_SNAKE_CASE_: Optional[Any] = 4 SCREAMING_SNAKE_CASE_: Dict = 0 SCREAMING_SNAKE_CASE_: List[Any] = 5 SCREAMING_SNAKE_CASE_: Union[str, Any] = FlaxForcedEOSTokenLogitsProcessor(max_length=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__) # check that all scores are -inf except the eos_token_id when max_length is reached SCREAMING_SNAKE_CASE_: List[Any] = ids_tensor((batch_size, 4) , vocab_size=20) SCREAMING_SNAKE_CASE_: Optional[int] = 4 SCREAMING_SNAKE_CASE_: Dict = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = logits_processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) self.assertTrue(jnp.isneginf(scores[:, eos_token_id + 1 :]).all()) self.assertListEqual(scores[:, eos_token_id].tolist() , 4 * [0]) # score for eos_token_id should be zero # check that eos_token_id is not forced if max_length is not reached SCREAMING_SNAKE_CASE_: List[str] = 3 SCREAMING_SNAKE_CASE_: str = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = logits_processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) self.assertFalse(jnp.isinf(lowerCAmelCase__).any()) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: int = 4 SCREAMING_SNAKE_CASE_: List[Any] = 10 SCREAMING_SNAKE_CASE_: int = 15 SCREAMING_SNAKE_CASE_: Dict = 2 SCREAMING_SNAKE_CASE_: int = 1 SCREAMING_SNAKE_CASE_: List[Any] = 15 # dummy input_ids and scores SCREAMING_SNAKE_CASE_: int = ids_tensor((batch_size, sequence_length) , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = input_ids.copy() SCREAMING_SNAKE_CASE_: List[Any] = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = scores.copy() # instantiate all dist processors SCREAMING_SNAKE_CASE_: Optional[int] = FlaxTemperatureLogitsWarper(temperature=0.5) SCREAMING_SNAKE_CASE_: Tuple = FlaxTopKLogitsWarper(3) SCREAMING_SNAKE_CASE_: Optional[int] = FlaxTopPLogitsWarper(0.8) # instantiate all logits processors SCREAMING_SNAKE_CASE_: Optional[int] = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = FlaxForcedEOSTokenLogitsProcessor(max_length=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = 10 # no processor list SCREAMING_SNAKE_CASE_: Dict = temp_dist_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = top_k_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = top_p_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = min_dist_proc(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = bos_dist_proc(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = eos_dist_proc(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) # with processor list SCREAMING_SNAKE_CASE_: str = FlaxLogitsProcessorList( [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc]) SCREAMING_SNAKE_CASE_: Tuple = processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) # scores should be equal self.assertTrue(jnp.allclose(lowerCAmelCase__ , lowerCAmelCase__ , atol=1E-3)) # input_ids should never be changed self.assertListEqual(input_ids.tolist() , input_ids_comp.tolist()) def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_: Optional[int] = 4 SCREAMING_SNAKE_CASE_: int = 10 SCREAMING_SNAKE_CASE_: List[str] = 15 SCREAMING_SNAKE_CASE_: List[Any] = 2 SCREAMING_SNAKE_CASE_: Union[str, Any] = 1 SCREAMING_SNAKE_CASE_: str = 15 # dummy input_ids and scores SCREAMING_SNAKE_CASE_: Tuple = ids_tensor((batch_size, sequence_length) , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = input_ids.copy() SCREAMING_SNAKE_CASE_: List[Any] = self._get_uniform_logits(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = scores.copy() # instantiate all dist processors SCREAMING_SNAKE_CASE_: Dict = FlaxTemperatureLogitsWarper(temperature=0.5) SCREAMING_SNAKE_CASE_: Union[str, Any] = FlaxTopKLogitsWarper(3) SCREAMING_SNAKE_CASE_: Dict = FlaxTopPLogitsWarper(0.8) # instantiate all logits processors SCREAMING_SNAKE_CASE_: int = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = FlaxForcedEOSTokenLogitsProcessor(max_length=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = 10 # no processor list def run_no_processor_list(lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Dict): SCREAMING_SNAKE_CASE_: Any = temp_dist_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = top_k_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = top_p_warp(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = min_dist_proc(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = bos_dist_proc(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = eos_dist_proc(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) return scores # with processor list def run_processor_list(lowerCAmelCase__ : int , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Union[str, Any]): SCREAMING_SNAKE_CASE_: List[str] = FlaxLogitsProcessorList( [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc]) SCREAMING_SNAKE_CASE_: Dict = processor(lowerCAmelCase__ , lowerCAmelCase__ , cur_len=lowerCAmelCase__) return scores SCREAMING_SNAKE_CASE_: str = jax.jit(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = jax.jit(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = jitted_run_no_processor_list(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = jitted_run_processor_list(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) # scores should be equal self.assertTrue(jnp.allclose(lowerCAmelCase__ , lowerCAmelCase__ , atol=1E-3)) # input_ids should never be changed self.assertListEqual(input_ids.tolist() , input_ids_comp.tolist())
13
1
from ....configuration_utils import PretrainedConfig from ....utils import logging lowerCAmelCase : Union[str, Any] = logging.get_logger(__name__) lowerCAmelCase : List[str] = { """speechbrain/m-ctc-t-large""": """https://huggingface.co/speechbrain/m-ctc-t-large/resolve/main/config.json""", # See all M-CTC-T models at https://huggingface.co/models?filter=mctct } class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : int = '''mctct''' def __init__( self : Optional[Any] , lowerCAmelCase__ : List[Any]=8065 , lowerCAmelCase__ : int=1536 , lowerCAmelCase__ : Tuple=36 , lowerCAmelCase__ : str=6144 , lowerCAmelCase__ : Optional[Any]=4 , lowerCAmelCase__ : List[Any]=384 , lowerCAmelCase__ : Dict=920 , lowerCAmelCase__ : Any=1E-5 , lowerCAmelCase__ : Dict=0.3 , lowerCAmelCase__ : Tuple="relu" , lowerCAmelCase__ : int=0.02 , lowerCAmelCase__ : List[str]=0.3 , lowerCAmelCase__ : Any=0.3 , lowerCAmelCase__ : Tuple=1 , lowerCAmelCase__ : List[Any]=0 , lowerCAmelCase__ : Union[str, Any]=2 , lowerCAmelCase__ : Optional[Any]=1 , lowerCAmelCase__ : Optional[Any]=0.3 , lowerCAmelCase__ : str=1 , lowerCAmelCase__ : Union[str, Any]=(7,) , lowerCAmelCase__ : Dict=(3,) , lowerCAmelCase__ : Optional[int]=80 , lowerCAmelCase__ : Any=1 , lowerCAmelCase__ : Dict=None , lowerCAmelCase__ : Dict="sum" , lowerCAmelCase__ : Any=False , **lowerCAmelCase__ : Any , ): super().__init__(**lowerCAmelCase__ , pad_token_id=lowerCAmelCase__ , bos_token_id=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = vocab_size SCREAMING_SNAKE_CASE_: Optional[int] = hidden_size SCREAMING_SNAKE_CASE_: List[str] = num_hidden_layers SCREAMING_SNAKE_CASE_: Dict = intermediate_size SCREAMING_SNAKE_CASE_: int = num_attention_heads SCREAMING_SNAKE_CASE_: Optional[Any] = attention_head_dim SCREAMING_SNAKE_CASE_: str = max_position_embeddings SCREAMING_SNAKE_CASE_: Optional[int] = layer_norm_eps SCREAMING_SNAKE_CASE_: Optional[Any] = layerdrop SCREAMING_SNAKE_CASE_: int = hidden_act SCREAMING_SNAKE_CASE_: Tuple = initializer_range SCREAMING_SNAKE_CASE_: List[Any] = hidden_dropout_prob SCREAMING_SNAKE_CASE_: Optional[int] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_: str = pad_token_id SCREAMING_SNAKE_CASE_: str = bos_token_id SCREAMING_SNAKE_CASE_: Optional[int] = eos_token_id SCREAMING_SNAKE_CASE_: Tuple = conv_glu_dim SCREAMING_SNAKE_CASE_: List[Any] = conv_dropout SCREAMING_SNAKE_CASE_: List[str] = num_conv_layers SCREAMING_SNAKE_CASE_: List[Any] = input_feat_per_channel SCREAMING_SNAKE_CASE_: Dict = input_channels SCREAMING_SNAKE_CASE_: str = conv_channels SCREAMING_SNAKE_CASE_: Union[str, Any] = ctc_loss_reduction SCREAMING_SNAKE_CASE_: Union[str, Any] = ctc_zero_infinity # prevents config testing fail with exporting to json SCREAMING_SNAKE_CASE_: Union[str, Any] = list(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = list(lowerCAmelCase__) if len(self.conv_kernel) != self.num_conv_layers: raise ValueError( "Configuration for convolutional module is incorrect. " "It is required that `len(config.conv_kernel)` == `config.num_conv_layers` " F"but is `len(config.conv_kernel) = {len(self.conv_kernel)}`, " F"`config.num_conv_layers = {self.num_conv_layers}`.")
13
import math import sys def A_ ( _UpperCAmelCase ): if number != int(_UpperCAmelCase ): raise ValueError("the value of input must be a natural number" ) if number < 0: raise ValueError("the value of input must not be a negative number" ) if number == 0: return 1 SCREAMING_SNAKE_CASE_: List[str] = [-1] * (number + 1) SCREAMING_SNAKE_CASE_: str = 0 for i in range(1 , number + 1 ): SCREAMING_SNAKE_CASE_: str = sys.maxsize SCREAMING_SNAKE_CASE_: List[Any] = int(math.sqrt(_UpperCAmelCase ) ) for j in range(1 , root + 1 ): SCREAMING_SNAKE_CASE_: List[str] = 1 + answers[i - (j**2)] SCREAMING_SNAKE_CASE_: Optional[Any] = min(_UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Dict = answer return answers[number] if __name__ == "__main__": import doctest doctest.testmod()
13
1
import unittest import numpy as np from transformers import RobertaPreLayerNormConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.roberta_prelayernorm.modeling_flax_roberta_prelayernorm import ( FlaxRobertaPreLayerNormForCausalLM, FlaxRobertaPreLayerNormForMaskedLM, FlaxRobertaPreLayerNormForMultipleChoice, FlaxRobertaPreLayerNormForQuestionAnswering, FlaxRobertaPreLayerNormForSequenceClassification, FlaxRobertaPreLayerNormForTokenClassification, FlaxRobertaPreLayerNormModel, ) class __lowercase ( unittest.TestCase ): """simple docstring""" def __init__( self : Any , lowerCAmelCase__ : str , lowerCAmelCase__ : Union[str, Any]=13 , lowerCAmelCase__ : List[str]=7 , lowerCAmelCase__ : Tuple=True , lowerCAmelCase__ : str=True , lowerCAmelCase__ : Tuple=True , lowerCAmelCase__ : Optional[Any]=True , lowerCAmelCase__ : Optional[int]=99 , lowerCAmelCase__ : int=32 , lowerCAmelCase__ : str=5 , lowerCAmelCase__ : List[str]=4 , lowerCAmelCase__ : Union[str, Any]=37 , lowerCAmelCase__ : Union[str, Any]="gelu" , lowerCAmelCase__ : List[str]=0.1 , lowerCAmelCase__ : Optional[int]=0.1 , lowerCAmelCase__ : Optional[int]=512 , lowerCAmelCase__ : Dict=16 , lowerCAmelCase__ : Optional[Any]=2 , lowerCAmelCase__ : Optional[Any]=0.02 , lowerCAmelCase__ : Dict=4 , ): SCREAMING_SNAKE_CASE_: Any = parent SCREAMING_SNAKE_CASE_: str = batch_size SCREAMING_SNAKE_CASE_: Any = seq_length SCREAMING_SNAKE_CASE_: int = is_training SCREAMING_SNAKE_CASE_: Optional[int] = use_attention_mask SCREAMING_SNAKE_CASE_: List[Any] = use_token_type_ids SCREAMING_SNAKE_CASE_: Union[str, Any] = use_labels SCREAMING_SNAKE_CASE_: Optional[Any] = vocab_size SCREAMING_SNAKE_CASE_: str = hidden_size SCREAMING_SNAKE_CASE_: Tuple = num_hidden_layers SCREAMING_SNAKE_CASE_: List[Any] = num_attention_heads SCREAMING_SNAKE_CASE_: int = intermediate_size SCREAMING_SNAKE_CASE_: Any = hidden_act SCREAMING_SNAKE_CASE_: Optional[int] = hidden_dropout_prob SCREAMING_SNAKE_CASE_: List[Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_: int = max_position_embeddings SCREAMING_SNAKE_CASE_: List[Any] = type_vocab_size SCREAMING_SNAKE_CASE_: Dict = type_sequence_label_size SCREAMING_SNAKE_CASE_: Any = initializer_range SCREAMING_SNAKE_CASE_: Optional[int] = num_choices def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size) SCREAMING_SNAKE_CASE_: Tuple = None if self.use_attention_mask: SCREAMING_SNAKE_CASE_: Any = random_attention_mask([self.batch_size, self.seq_length]) SCREAMING_SNAKE_CASE_: List[Any] = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE_: Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size) SCREAMING_SNAKE_CASE_: Dict = RobertaPreLayerNormConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=lowerCAmelCase__ , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: List[str] = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = config_and_inputs SCREAMING_SNAKE_CASE_: List[str] = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask} return config, inputs_dict def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Optional[int] = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = config_and_inputs SCREAMING_SNAKE_CASE_: Union[str, Any] = True SCREAMING_SNAKE_CASE_: Union[str, Any] = floats_tensor([self.batch_size, self.seq_length, self.hidden_size]) SCREAMING_SNAKE_CASE_: List[Any] = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2) return ( config, input_ids, token_type_ids, encoder_hidden_states, encoder_attention_mask, ) @require_flax # Copied from tests.models.roberta.test_modelling_flax_roberta.FlaxRobertaPreLayerNormModelTest with ROBERTA->ROBERTA_PRELAYERNORM,Roberta->RobertaPreLayerNorm,roberta-base->andreasmadsen/efficient_mlm_m0.40 class __lowercase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : int = True _UpperCAmelCase : Optional[Any] = ( ( FlaxRobertaPreLayerNormModel, FlaxRobertaPreLayerNormForCausalLM, FlaxRobertaPreLayerNormForMaskedLM, FlaxRobertaPreLayerNormForSequenceClassification, FlaxRobertaPreLayerNormForTokenClassification, FlaxRobertaPreLayerNormForMultipleChoice, FlaxRobertaPreLayerNormForQuestionAnswering, ) if is_flax_available() else () ) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: List[Any] = FlaxRobertaPreLayerNormModelTester(self) @slow def _SCREAMING_SNAKE_CASE ( self : int): for model_class_name in self.all_model_classes: SCREAMING_SNAKE_CASE_: str = model_class_name.from_pretrained("andreasmadsen/efficient_mlm_m0.40" , from_pt=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = model(np.ones((1, 1))) self.assertIsNotNone(lowerCAmelCase__) @require_flax class __lowercase ( unittest.TestCase ): """simple docstring""" @slow def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Optional[int] = FlaxRobertaPreLayerNormForMaskedLM.from_pretrained("andreasmadsen/efficient_mlm_m0.40" , from_pt=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = np.array([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]] , dtype=jnp.intaa) SCREAMING_SNAKE_CASE_: Dict = model(lowerCAmelCase__)[0] SCREAMING_SNAKE_CASE_: List[Any] = [1, 11, 5_0265] self.assertEqual(list(output.shape) , lowerCAmelCase__) # compare the actual values for a slice. SCREAMING_SNAKE_CASE_: Any = np.array( [[[40.4880, 18.0199, -5.2367], [-1.8877, -4.0885, 10.7085], [-2.2613, -5.6110, 7.2665]]] , dtype=np.floataa) self.assertTrue(np.allclose(output[:, :3, :3] , lowerCAmelCase__ , atol=1E-4)) @slow def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Optional[int] = FlaxRobertaPreLayerNormModel.from_pretrained("andreasmadsen/efficient_mlm_m0.40" , from_pt=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = np.array([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]] , dtype=jnp.intaa) SCREAMING_SNAKE_CASE_: Dict = model(lowerCAmelCase__)[0] # compare the actual values for a slice. SCREAMING_SNAKE_CASE_: List[str] = np.array( [[[0.0208, -0.0356, 0.0237], [-0.1569, -0.0411, -0.2626], [0.1879, 0.0125, -0.0089]]] , dtype=np.floataa) self.assertTrue(np.allclose(output[:, :3, :3] , lowerCAmelCase__ , atol=1E-4))
13
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase : Optional[int] = {"""configuration_wavlm""": ["""WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """WavLMConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Any = [ """WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST""", """WavLMForAudioFrameClassification""", """WavLMForCTC""", """WavLMForSequenceClassification""", """WavLMForXVector""", """WavLMModel""", """WavLMPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_wavlm import WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP, WavLMConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_wavlm import ( WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST, WavLMForAudioFrameClassification, WavLMForCTC, WavLMForSequenceClassification, WavLMForXVector, WavLMModel, WavLMPreTrainedModel, ) else: import sys lowerCAmelCase : Union[str, Any] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
13
1
import os import numpy import onnx def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Union[str, Any] = a.name SCREAMING_SNAKE_CASE_: int = b.name SCREAMING_SNAKE_CASE_: Any = "" SCREAMING_SNAKE_CASE_: Tuple = "" SCREAMING_SNAKE_CASE_: List[Any] = a == b SCREAMING_SNAKE_CASE_: Any = name_a SCREAMING_SNAKE_CASE_: Optional[int] = name_b return res def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): for i, input_name in enumerate(node_proto.input ): if input_name == name: node_proto.input.insert(_UpperCAmelCase , _UpperCAmelCase ) node_proto.input.pop(i + 1 ) if node_proto.op_type == "If": _graph_replace_input_with(node_proto.attribute[0].g , _UpperCAmelCase , _UpperCAmelCase ) _graph_replace_input_with(node_proto.attribute[1].g , _UpperCAmelCase , _UpperCAmelCase ) if node_proto.op_type == "Loop": _graph_replace_input_with(node_proto.attribute[0].g , _UpperCAmelCase , _UpperCAmelCase ) def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): for n in graph_proto.node: _node_replace_input_with(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Optional[int] = list(model.graph.initializer ) SCREAMING_SNAKE_CASE_: Optional[Any] = list(model_without_ext.graph.initializer ) for i, ref_i in ind_to_replace: assert inits_with_data[i].name == inits[i].name assert inits_with_data[ref_i].name == inits[ref_i].name assert i > ref_i SCREAMING_SNAKE_CASE_: Optional[Any] = inits[i].name SCREAMING_SNAKE_CASE_: Union[str, Any] = inits[ref_i].name model_without_ext.graph.initializer.remove(inits[i] ) # for n in model.graph.node: _graph_replace_input_with(model_without_ext.graph , _UpperCAmelCase , _UpperCAmelCase ) def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Union[str, Any] = os.path.dirname(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Any = os.path.basename(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: int = onnx.load(os.path.join(_UpperCAmelCase , _UpperCAmelCase ) ) SCREAMING_SNAKE_CASE_: Any = list(model.graph.initializer ) SCREAMING_SNAKE_CASE_: Optional[Any] = set() SCREAMING_SNAKE_CASE_: Any = {} SCREAMING_SNAKE_CASE_: Any = [] SCREAMING_SNAKE_CASE_: int = 0 for i in range(len(_UpperCAmelCase ) ): if i in dup_set: continue for j in range(i + 1 , len(_UpperCAmelCase ) ): if j in dup_set: continue if _is_equal_tensor_proto(inits[i] , inits[j] ): dup_set.add(_UpperCAmelCase ) dup_set.add(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[str] = inits[j].data_type SCREAMING_SNAKE_CASE_: Any = numpy.prod(inits[j].dims ) if dtype == 1: mem_size *= 4 elif dtype == 6: mem_size *= 4 elif dtype == 7 or dtype == 11: mem_size *= 8 else: print("unexpected data type: " , _UpperCAmelCase ) total_reduced_size += mem_size SCREAMING_SNAKE_CASE_: Optional[int] = inits[i].name SCREAMING_SNAKE_CASE_: Optional[int] = inits[j].name if name_i in dup_map: dup_map[name_i].append(_UpperCAmelCase ) else: SCREAMING_SNAKE_CASE_: List[Any] = [name_j] ind_to_replace.append((j, i) ) print("total reduced size: " , total_reduced_size / 10_24 / 10_24 / 10_24 , "GB" ) SCREAMING_SNAKE_CASE_: List[Any] = sorted(_UpperCAmelCase ) _remove_dup_initializers_from_model(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Union[str, Any] = "optimized_" + model_file_name SCREAMING_SNAKE_CASE_: Union[str, Any] = os.path.join(_UpperCAmelCase , _UpperCAmelCase ) onnx.save(_UpperCAmelCase , _UpperCAmelCase ) return new_model
13
import unittest from transformers import ( MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TextaTextGenerationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, require_tf, require_torch from transformers.utils import is_torch_available from .test_pipelines_common import ANY if is_torch_available(): import torch @is_pipeline_test class __lowercase ( unittest.TestCase ): """simple docstring""" _UpperCAmelCase : List[Any] = MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING _UpperCAmelCase : str = TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING def _SCREAMING_SNAKE_CASE ( self : Any , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Dict): SCREAMING_SNAKE_CASE_: Any = TextaTextGenerationPipeline(model=lowerCAmelCase__ , tokenizer=lowerCAmelCase__) return generator, ["Something to write", "Something else"] def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Any): SCREAMING_SNAKE_CASE_: List[Any] = generator("Something there") self.assertEqual(lowerCAmelCase__ , [{"generated_text": ANY(lowerCAmelCase__)}]) # These are encoder decoder, they don't just append to incoming string self.assertFalse(outputs[0]["generated_text"].startswith("Something there")) SCREAMING_SNAKE_CASE_: List[Any] = generator(["This is great !", "Something else"] , num_return_sequences=2 , do_sample=lowerCAmelCase__) self.assertEqual( lowerCAmelCase__ , [ [{"generated_text": ANY(lowerCAmelCase__)}, {"generated_text": ANY(lowerCAmelCase__)}], [{"generated_text": ANY(lowerCAmelCase__)}, {"generated_text": ANY(lowerCAmelCase__)}], ] , ) SCREAMING_SNAKE_CASE_: Dict = generator( ["This is great !", "Something else"] , num_return_sequences=2 , batch_size=2 , do_sample=lowerCAmelCase__) self.assertEqual( lowerCAmelCase__ , [ [{"generated_text": ANY(lowerCAmelCase__)}, {"generated_text": ANY(lowerCAmelCase__)}], [{"generated_text": ANY(lowerCAmelCase__)}, {"generated_text": ANY(lowerCAmelCase__)}], ] , ) with self.assertRaises(lowerCAmelCase__): generator(4) @require_torch def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: Optional[int] = pipeline("text2text-generation" , model="patrickvonplaten/t5-tiny-random" , framework="pt") # do_sample=False necessary for reproducibility SCREAMING_SNAKE_CASE_: Union[str, Any] = generator("Something there" , do_sample=lowerCAmelCase__) self.assertEqual(lowerCAmelCase__ , [{"generated_text": ""}]) SCREAMING_SNAKE_CASE_: Union[str, Any] = 3 SCREAMING_SNAKE_CASE_: Any = generator( "Something there" , num_return_sequences=lowerCAmelCase__ , num_beams=lowerCAmelCase__ , ) SCREAMING_SNAKE_CASE_: Any = [ {"generated_text": "Beide Beide Beide Beide Beide Beide Beide Beide Beide"}, {"generated_text": "Beide Beide Beide Beide Beide Beide Beide Beide"}, {"generated_text": ""}, ] self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = generator("This is a test" , do_sample=lowerCAmelCase__ , num_return_sequences=2 , return_tensors=lowerCAmelCase__) self.assertEqual( lowerCAmelCase__ , [ {"generated_token_ids": ANY(torch.Tensor)}, {"generated_token_ids": ANY(torch.Tensor)}, ] , ) SCREAMING_SNAKE_CASE_: str = generator.model.config.eos_token_id SCREAMING_SNAKE_CASE_: Union[str, Any] = "<pad>" SCREAMING_SNAKE_CASE_: Tuple = generator( ["This is a test", "This is a second test"] , do_sample=lowerCAmelCase__ , num_return_sequences=2 , batch_size=2 , return_tensors=lowerCAmelCase__ , ) self.assertEqual( lowerCAmelCase__ , [ [ {"generated_token_ids": ANY(torch.Tensor)}, {"generated_token_ids": ANY(torch.Tensor)}, ], [ {"generated_token_ids": ANY(torch.Tensor)}, {"generated_token_ids": ANY(torch.Tensor)}, ], ] , ) @require_tf def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = pipeline("text2text-generation" , model="patrickvonplaten/t5-tiny-random" , framework="tf") # do_sample=False necessary for reproducibility SCREAMING_SNAKE_CASE_: List[Any] = generator("Something there" , do_sample=lowerCAmelCase__) self.assertEqual(lowerCAmelCase__ , [{"generated_text": ""}])
13
1
class __lowercase : """simple docstring""" def __init__( self : List[Any] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[Any]): SCREAMING_SNAKE_CASE_: List[str] = name SCREAMING_SNAKE_CASE_: Union[str, Any] = val def __str__( self : Dict): return F"{self.__class__.__name__}({self.name}, {self.val})" def __lt__( self : List[str] , lowerCAmelCase__ : Any): return self.val < other.val class __lowercase : """simple docstring""" def __init__( self : Tuple , lowerCAmelCase__ : Dict): SCREAMING_SNAKE_CASE_: str = {} SCREAMING_SNAKE_CASE_: int = {} SCREAMING_SNAKE_CASE_: Any = self.build_heap(lowerCAmelCase__) def __getitem__( self : List[Any] , lowerCAmelCase__ : Dict): return self.get_value(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : Dict): return (idx - 1) // 2 def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Optional[Any]): return idx * 2 + 1 def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Tuple): return idx * 2 + 2 def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Optional[int]): return self.heap_dict[key] def _SCREAMING_SNAKE_CASE ( self : List[Any] , lowerCAmelCase__ : Union[str, Any]): SCREAMING_SNAKE_CASE_: Tuple = len(lowerCAmelCase__) - 1 SCREAMING_SNAKE_CASE_: List[str] = self.get_parent_idx(lowerCAmelCase__) for idx, i in enumerate(lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Union[str, Any] = idx SCREAMING_SNAKE_CASE_: str = i.val for i in range(lowerCAmelCase__ , -1 , -1): self.sift_down(lowerCAmelCase__ , lowerCAmelCase__) return array def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[str]): while True: SCREAMING_SNAKE_CASE_: Optional[Any] = self.get_left_child_idx(lowerCAmelCase__) # noqa: E741 SCREAMING_SNAKE_CASE_: Dict = self.get_right_child_idx(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = idx if l < len(lowerCAmelCase__) and array[l] < array[idx]: SCREAMING_SNAKE_CASE_: List[str] = l if r < len(lowerCAmelCase__) and array[r] < array[smallest]: SCREAMING_SNAKE_CASE_: str = r if smallest != idx: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = array[smallest], array[idx] ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ): Optional[Any] = ( self.idx_of_element[array[smallest]], self.idx_of_element[array[idx]], ) SCREAMING_SNAKE_CASE_: Optional[int] = smallest else: break def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : str): SCREAMING_SNAKE_CASE_: Any = self.get_parent_idx(lowerCAmelCase__) while p >= 0 and self.heap[p] > self.heap[idx]: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = self.heap[idx], self.heap[p] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = ( self.idx_of_element[self.heap[idx]], self.idx_of_element[self.heap[p]], ) SCREAMING_SNAKE_CASE_: Union[str, Any] = p SCREAMING_SNAKE_CASE_: Optional[int] = self.get_parent_idx(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[Any]): return self.heap[0] def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self.heap[-1], self.heap[0] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = ( self.idx_of_element[self.heap[-1]], self.idx_of_element[self.heap[0]], ) SCREAMING_SNAKE_CASE_: int = self.heap.pop() del self.idx_of_element[x] self.sift_down(0 , self.heap) return x def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Tuple): self.heap.append(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = len(self.heap) - 1 SCREAMING_SNAKE_CASE_: List[str] = node.val self.sift_up(len(self.heap) - 1) def _SCREAMING_SNAKE_CASE ( self : List[Any]): return len(self.heap) == 0 def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Optional[int]): assert ( self.heap[self.idx_of_element[node]].val > new_value ), "newValue must be less that current value" SCREAMING_SNAKE_CASE_: Any = new_value SCREAMING_SNAKE_CASE_: Tuple = new_value self.sift_up(self.idx_of_element[node]) lowerCAmelCase : int = Node("""R""", -1) lowerCAmelCase : str = Node("""B""", 6) lowerCAmelCase : str = Node("""A""", 3) lowerCAmelCase : List[str] = Node("""X""", 1) lowerCAmelCase : Union[str, Any] = Node("""E""", 4) # Use one of these two ways to generate Min-Heap # Generating Min-Heap from array lowerCAmelCase : Optional[Any] = MinHeap([r, b, a, x, e]) # Generating Min-Heap by Insert method # myMinHeap.insert(a) # myMinHeap.insert(b) # myMinHeap.insert(x) # myMinHeap.insert(r) # myMinHeap.insert(e) # Before print("""Min Heap - before decrease key""") for i in my_min_heap.heap: print(i) print("""Min Heap - After decrease key of node [B -> -17]""") my_min_heap.decrease_key(b, -17) # After for i in my_min_heap.heap: print(i) if __name__ == "__main__": import doctest doctest.testmod()
13
def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: List[str] = [0] * len(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[Any] = [] SCREAMING_SNAKE_CASE_: str = [] SCREAMING_SNAKE_CASE_: List[str] = 0 for values in graph.values(): for i in values: indegree[i] += 1 for i in range(len(_UpperCAmelCase ) ): if indegree[i] == 0: queue.append(_UpperCAmelCase ) while queue: SCREAMING_SNAKE_CASE_: Optional[int] = queue.pop(0 ) cnt += 1 topo.append(_UpperCAmelCase ) for x in graph[vertex]: indegree[x] -= 1 if indegree[x] == 0: queue.append(_UpperCAmelCase ) if cnt != len(_UpperCAmelCase ): print("Cycle exists" ) else: print(_UpperCAmelCase ) # Adjacency List of Graph lowerCAmelCase : Any = {0: [1, 2], 1: [3], 2: [3], 3: [4, 5], 4: [], 5: []} topological_sort(graph)
13
1
import os import jsonlines import numpy as np from tqdm import tqdm lowerCAmelCase : Optional[int] = 2048 lowerCAmelCase : Dict = 4096 lowerCAmelCase : Any = 42 lowerCAmelCase : str = os.environ.pop("""PROCESS_TRAIN""", """false""") lowerCAmelCase : Optional[Any] = {"""null""": 0, """short""": 1, """long""": 2, """yes""": 3, """no""": 4} def A_ ( _UpperCAmelCase ): def choose_first(_UpperCAmelCase , _UpperCAmelCase=False ): assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) if len(_UpperCAmelCase ) == 1: SCREAMING_SNAKE_CASE_: Dict = answer[0] return {k: [answer[k]] for k in answer} if is_long_answer else answer for a in answer: if is_long_answer: SCREAMING_SNAKE_CASE_: Optional[Any] = {k: [a[k]] for k in a} if len(a["start_token"] ) > 0: break return a SCREAMING_SNAKE_CASE_: Optional[int] = {"id": example["id"]} SCREAMING_SNAKE_CASE_: Dict = example["annotations"] SCREAMING_SNAKE_CASE_: List[Any] = annotation["yes_no_answer"] if 0 in yes_no_answer or 1 in yes_no_answer: SCREAMING_SNAKE_CASE_: List[str] = ["yes"] if 1 in yes_no_answer else ["no"] SCREAMING_SNAKE_CASE_: Tuple = [] SCREAMING_SNAKE_CASE_: Optional[Any] = [] SCREAMING_SNAKE_CASE_: Union[str, Any] = ["<cls>"] else: SCREAMING_SNAKE_CASE_: List[str] = ["short"] SCREAMING_SNAKE_CASE_: Optional[Any] = choose_first(annotation["short_answers"] ) if len(out["start_token"] ) == 0: # answer will be long if short is not available SCREAMING_SNAKE_CASE_: List[str] = ["long"] SCREAMING_SNAKE_CASE_: str = choose_first(annotation["long_answer"] , is_long_answer=_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: str = [] answer.update(_UpperCAmelCase ) # disregard some samples if len(answer["start_token"] ) > 1 or answer["start_token"] == answer["end_token"]: SCREAMING_SNAKE_CASE_: str = True else: SCREAMING_SNAKE_CASE_: Dict = False SCREAMING_SNAKE_CASE_: Tuple = ["start_token", "end_token", "start_byte", "end_byte", "text"] if not all(isinstance(answer[k] , _UpperCAmelCase ) for k in cols ): raise ValueError("Issue in ID" , example["id"] ) return answer def A_ ( _UpperCAmelCase , _UpperCAmelCase=False ): SCREAMING_SNAKE_CASE_: Optional[Any] = _get_single_answer(_UpperCAmelCase ) # bytes are of no use del answer["start_byte"] del answer["end_byte"] # handle yes_no answers explicitly if answer["category"][0] in ["yes", "no"]: # category is list with one element SCREAMING_SNAKE_CASE_: Tuple = example["document"]["tokens"] SCREAMING_SNAKE_CASE_: Any = [] for i in range(len(doc["token"] ) ): if not doc["is_html"][i]: context.append(doc["token"][i] ) return { "context": " ".join(_UpperCAmelCase ), "answer": { "start_token": -1_00, # ignore index in cross-entropy "end_token": -1_00, # ignore index in cross-entropy "category": answer["category"], "span": answer["category"], # extra }, } # later, help in removing all no answers if answer["start_token"] == [-1]: return { "context": "None", "answer": { "start_token": -1, "end_token": -1, "category": "null", "span": "None", # extra }, } # handling normal samples SCREAMING_SNAKE_CASE_: Optional[int] = ["start_token", "end_token"] answer.update({k: answer[k][0] if len(answer[k] ) > 0 else answer[k] for k in cols} ) # e.g. [10] == 10 SCREAMING_SNAKE_CASE_: List[str] = example["document"]["tokens"] SCREAMING_SNAKE_CASE_: Optional[int] = answer["start_token"] SCREAMING_SNAKE_CASE_: Union[str, Any] = answer["end_token"] SCREAMING_SNAKE_CASE_: Any = [] for i in range(len(doc["token"] ) ): if not doc["is_html"][i]: context.append(doc["token"][i] ) else: if answer["start_token"] > i: start_token -= 1 if answer["end_token"] > i: end_token -= 1 SCREAMING_SNAKE_CASE_: Optional[Any] = " ".join(context[start_token:end_token] ) # checking above code if assertion: SCREAMING_SNAKE_CASE_: Optional[Any] = doc["is_html"][answer["start_token"] : answer["end_token"]] SCREAMING_SNAKE_CASE_: Tuple = doc["token"][answer["start_token"] : answer["end_token"]] SCREAMING_SNAKE_CASE_: str = " ".join([old[i] for i in range(len(_UpperCAmelCase ) ) if not is_html[i]] ) if new != old: print("ID:" , example["id"] ) print("New:" , _UpperCAmelCase , end="\n" ) print("Old:" , _UpperCAmelCase , end="\n\n" ) return { "context": " ".join(_UpperCAmelCase ), "answer": { "start_token": start_token, "end_token": end_token - 1, # this makes it inclusive "category": answer["category"], # either long or short "span": new, # extra }, } def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=20_48 , _UpperCAmelCase=40_96 , _UpperCAmelCase=True ): # overlap will be of doc_stride - q_len SCREAMING_SNAKE_CASE_: Dict = get_context_and_ans(_UpperCAmelCase , assertion=_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: int = out["answer"] # later, removing these samples if answer["start_token"] == -1: return { "example_id": example["id"], "input_ids": [[-1]], "labels": { "start_token": [-1], "end_token": [-1], "category": ["null"], }, } SCREAMING_SNAKE_CASE_: Union[str, Any] = tokenizer(example["question"]["text"] , out["context"] ).input_ids SCREAMING_SNAKE_CASE_: List[Any] = input_ids.index(tokenizer.sep_token_id ) + 1 # return yes/no if answer["category"][0] in ["yes", "no"]: # category is list with one element SCREAMING_SNAKE_CASE_: Union[str, Any] = [] SCREAMING_SNAKE_CASE_: Union[str, Any] = [] SCREAMING_SNAKE_CASE_: Tuple = input_ids[:q_len] SCREAMING_SNAKE_CASE_: int = range(_UpperCAmelCase , len(_UpperCAmelCase ) , max_length - doc_stride ) for i in doc_start_indices: SCREAMING_SNAKE_CASE_: Optional[int] = i + max_length - q_len SCREAMING_SNAKE_CASE_: Dict = input_ids[i:end_index] inputs.append(q_indices + slice ) category.append(answer["category"][0] ) if slice[-1] == tokenizer.sep_token_id: break return { "example_id": example["id"], "input_ids": inputs, "labels": { "start_token": [-1_00] * len(_UpperCAmelCase ), "end_token": [-1_00] * len(_UpperCAmelCase ), "category": category, }, } SCREAMING_SNAKE_CASE_: Dict = out["context"].split() SCREAMING_SNAKE_CASE_: Optional[int] = splitted_context[answer["end_token"]] SCREAMING_SNAKE_CASE_: Union[str, Any] = len( tokenizer( " ".join(splitted_context[: answer["start_token"]] ) , add_special_tokens=_UpperCAmelCase , ).input_ids ) SCREAMING_SNAKE_CASE_: int = len( tokenizer(" ".join(splitted_context[: answer["end_token"]] ) , add_special_tokens=_UpperCAmelCase ).input_ids ) answer["start_token"] += q_len answer["end_token"] += q_len # fixing end token SCREAMING_SNAKE_CASE_: List[str] = len(tokenizer(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ).input_ids ) if num_sub_tokens > 1: answer["end_token"] += num_sub_tokens - 1 SCREAMING_SNAKE_CASE_: List[str] = input_ids[answer["start_token"] : answer["end_token"] + 1] # right & left are inclusive SCREAMING_SNAKE_CASE_: List[Any] = answer["start_token"] SCREAMING_SNAKE_CASE_: List[Any] = answer["end_token"] if assertion: SCREAMING_SNAKE_CASE_: Optional[Any] = tokenizer.decode(_UpperCAmelCase ) if answer["span"] != new: print("ISSUE IN TOKENIZATION" ) print("OLD:" , answer["span"] ) print("NEW:" , _UpperCAmelCase , end="\n\n" ) if len(_UpperCAmelCase ) <= max_length: return { "example_id": example["id"], "input_ids": [input_ids], "labels": { "start_token": [answer["start_token"]], "end_token": [answer["end_token"]], "category": answer["category"], }, } SCREAMING_SNAKE_CASE_: Union[str, Any] = input_ids[:q_len] SCREAMING_SNAKE_CASE_: List[str] = range(_UpperCAmelCase , len(_UpperCAmelCase ) , max_length - doc_stride ) SCREAMING_SNAKE_CASE_: Tuple = [] SCREAMING_SNAKE_CASE_: Optional[Any] = [] SCREAMING_SNAKE_CASE_: List[Any] = [] SCREAMING_SNAKE_CASE_: str = [] # null, yes, no, long, short for i in doc_start_indices: SCREAMING_SNAKE_CASE_: List[Any] = i + max_length - q_len SCREAMING_SNAKE_CASE_: Tuple = input_ids[i:end_index] inputs.append(q_indices + slice ) assert len(inputs[-1] ) <= max_length, "Issue in truncating length" if start_token >= i and end_token <= end_index - 1: SCREAMING_SNAKE_CASE_: int = start_token - i + q_len SCREAMING_SNAKE_CASE_: Optional[Any] = end_token - i + q_len answers_category.append(answer["category"][0] ) # ["short"] -> "short" else: SCREAMING_SNAKE_CASE_: Union[str, Any] = -1_00 SCREAMING_SNAKE_CASE_: Union[str, Any] = -1_00 answers_category.append("null" ) SCREAMING_SNAKE_CASE_: List[Any] = inputs[-1][start_token : end_token + 1] answers_start_token.append(_UpperCAmelCase ) answers_end_token.append(_UpperCAmelCase ) if assertion: if new != old and new != [tokenizer.cls_token_id]: print("ISSUE in strided for ID:" , example["id"] ) print("New:" , tokenizer.decode(_UpperCAmelCase ) ) print("Old:" , tokenizer.decode(_UpperCAmelCase ) , end="\n\n" ) if slice[-1] == tokenizer.sep_token_id: break return { "example_id": example["id"], "input_ids": inputs, "labels": { "start_token": answers_start_token, "end_token": answers_end_token, "category": answers_category, }, } def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=20_48 , _UpperCAmelCase=40_96 , _UpperCAmelCase=False ): SCREAMING_SNAKE_CASE_: str = get_strided_contexts_and_ans( _UpperCAmelCase , _UpperCAmelCase , doc_stride=_UpperCAmelCase , max_length=_UpperCAmelCase , assertion=_UpperCAmelCase , ) return example def A_ ( _UpperCAmelCase , _UpperCAmelCase ): with jsonlines.open(_UpperCAmelCase , "a" ) as writer: for example in tqdm(_UpperCAmelCase , total=len(_UpperCAmelCase ) , desc="Saving samples ... " ): SCREAMING_SNAKE_CASE_: Tuple = example["labels"] for ids, start, end, cat in zip( example["input_ids"] , labels["start_token"] , labels["end_token"] , labels["category"] , ): if start == -1 and end == -1: continue # leave waste samples with no answer if cat == "null" and np.random.rand() < 0.6: continue # removing 50 % samples writer.write( { "input_ids": ids, "start_token": start, "end_token": end, "category": CATEGORY_MAPPING[cat], } ) if __name__ == "__main__": from datasets import load_dataset from transformers import BigBirdTokenizer lowerCAmelCase : str = load_dataset("""natural_questions""") lowerCAmelCase : Dict = BigBirdTokenizer.from_pretrained("""google/bigbird-roberta-base""") lowerCAmelCase : str = data["""train""" if PROCESS_TRAIN == """true""" else """validation"""] lowerCAmelCase : str = { """tokenizer""": tokenizer, """doc_stride""": DOC_STRIDE, """max_length""": MAX_LENGTH, """assertion""": False, } lowerCAmelCase : Optional[Any] = data.map(prepare_inputs, fn_kwargs=fn_kwargs) lowerCAmelCase : Dict = data.remove_columns(["""annotations""", """document""", """id""", """question"""]) print(data) np.random.seed(SEED) lowerCAmelCase : Optional[int] = """nq-training.jsonl""" if PROCESS_TRAIN == """true""" else """nq-validation.jsonl""" save_to_disk(data, file_name=cache_file_name)
13
import argparse import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## lowerCAmelCase : Optional[Any] = 16 lowerCAmelCase : List[str] = 32 def A_ ( _UpperCAmelCase , _UpperCAmelCase = 16 ): SCREAMING_SNAKE_CASE_: Tuple = AutoTokenizer.from_pretrained("bert-base-cased" ) SCREAMING_SNAKE_CASE_: List[Any] = load_dataset("glue" , "mrpc" ) def tokenize_function(_UpperCAmelCase ): # max_length=None => use the model max length (it's actually the default) SCREAMING_SNAKE_CASE_: Any = tokenizer(examples["sentence1"] , examples["sentence2"] , truncation=_UpperCAmelCase , max_length=_UpperCAmelCase ) 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(): SCREAMING_SNAKE_CASE_: Tuple = datasets.map( _UpperCAmelCase , batched=_UpperCAmelCase , 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 SCREAMING_SNAKE_CASE_: Union[str, Any] = tokenized_datasets.rename_column("label" , "labels" ) def collate_fn(_UpperCAmelCase ): # On TPU it's best to pad everything to the same length or training will be very slow. SCREAMING_SNAKE_CASE_: List[str] = 1_28 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": SCREAMING_SNAKE_CASE_: List[Any] = 16 elif accelerator.mixed_precision != "no": SCREAMING_SNAKE_CASE_: Optional[Any] = 8 else: SCREAMING_SNAKE_CASE_: List[str] = None return tokenizer.pad( _UpperCAmelCase , padding="longest" , max_length=_UpperCAmelCase , pad_to_multiple_of=_UpperCAmelCase , return_tensors="pt" , ) # Instantiate dataloaders. SCREAMING_SNAKE_CASE_: Dict = DataLoader( tokenized_datasets["train"] , shuffle=_UpperCAmelCase , collate_fn=_UpperCAmelCase , batch_size=_UpperCAmelCase , drop_last=_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Union[str, Any] = DataLoader( tokenized_datasets["validation"] , shuffle=_UpperCAmelCase , collate_fn=_UpperCAmelCase , batch_size=_UpperCAmelCase , drop_last=(accelerator.mixed_precision == "fp8") , ) return train_dataloader, eval_dataloader def A_ ( _UpperCAmelCase , _UpperCAmelCase ): # Initialize accelerator SCREAMING_SNAKE_CASE_: str = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs SCREAMING_SNAKE_CASE_: int = config["lr"] SCREAMING_SNAKE_CASE_: Any = int(config["num_epochs"] ) SCREAMING_SNAKE_CASE_: Optional[int] = int(config["seed"] ) SCREAMING_SNAKE_CASE_: List[Any] = int(config["batch_size"] ) SCREAMING_SNAKE_CASE_: List[str] = evaluate.load("glue" , "mrpc" ) # If the batch size is too big we use gradient accumulation SCREAMING_SNAKE_CASE_: Optional[int] = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: SCREAMING_SNAKE_CASE_: Tuple = batch_size // MAX_GPU_BATCH_SIZE SCREAMING_SNAKE_CASE_: Dict = MAX_GPU_BATCH_SIZE set_seed(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = get_dataloaders(_UpperCAmelCase , _UpperCAmelCase ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) SCREAMING_SNAKE_CASE_: List[Any] = AutoModelForSequenceClassification.from_pretrained("bert-base-cased" , return_dict=_UpperCAmelCase ) # 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). SCREAMING_SNAKE_CASE_: Tuple = model.to(accelerator.device ) # Instantiate optimizer SCREAMING_SNAKE_CASE_: Optional[int] = AdamW(params=model.parameters() , lr=_UpperCAmelCase ) # Instantiate scheduler SCREAMING_SNAKE_CASE_: Optional[int] = get_linear_schedule_with_warmup( optimizer=_UpperCAmelCase , num_warmup_steps=1_00 , num_training_steps=(len(_UpperCAmelCase ) * num_epochs) // gradient_accumulation_steps , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = accelerator.prepare( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) # Now we train the model for epoch in range(_UpperCAmelCase ): model.train() for step, batch in enumerate(_UpperCAmelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) SCREAMING_SNAKE_CASE_: Tuple = model(**_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Optional[int] = outputs.loss SCREAMING_SNAKE_CASE_: Tuple = loss / gradient_accumulation_steps accelerator.backward(_UpperCAmelCase ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(_UpperCAmelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): SCREAMING_SNAKE_CASE_: Optional[int] = model(**_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: int = outputs.logits.argmax(dim=-1 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = accelerator.gather_for_metrics((predictions, batch["labels"]) ) metric.add_batch( predictions=_UpperCAmelCase , references=_UpperCAmelCase , ) SCREAMING_SNAKE_CASE_: List[Any] = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f"epoch {epoch}:" , _UpperCAmelCase ) def A_ ( ): SCREAMING_SNAKE_CASE_: Any = argparse.ArgumentParser(description="Simple example of training script." ) parser.add_argument( "--mixed_precision" , type=_UpperCAmelCase , default=_UpperCAmelCase , 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." ) SCREAMING_SNAKE_CASE_: Optional[Any] = parser.parse_args() SCREAMING_SNAKE_CASE_: Optional[int] = {"lr": 2e-5, "num_epochs": 3, "seed": 42, "batch_size": 16} training_function(_UpperCAmelCase , _UpperCAmelCase ) if __name__ == "__main__": main()
13
1
from __future__ import annotations import collections import tempfile import unittest import numpy as np from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import is_tf_available, is_vision_available from ...test_modeling_tf_common import floats_tensor, ids_tensor, random_attention_mask from ..bert.test_modeling_tf_bert import TFBertModelTester from ..clip.test_modeling_tf_clip import TFCLIPVisionModelTester from ..deit.test_modeling_tf_deit import TFDeiTModelTester from ..roberta.test_modeling_tf_roberta import TFRobertaModelTester from ..vit.test_modeling_tf_vit import TFViTModelTester if is_tf_available(): from transformers import ( TFBertModel, TFCLIPVisionModel, TFDeiTModel, TFRobertaModel, TFVisionTextDualEncoderModel, TFViTModel, VisionTextDualEncoderConfig, ) if is_vision_available(): from PIL import Image from transformers import VisionTextDualEncoderProcessor def A_ ( _UpperCAmelCase ): if isinstance(_UpperCAmelCase , collections.abc.Iterable ): return x return (x, x) @require_tf class __lowercase : """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : Any , lowerCAmelCase__ : str , lowerCAmelCase__ : int): pass def _SCREAMING_SNAKE_CASE ( self : str): pass def _SCREAMING_SNAKE_CASE ( self : Dict): pass def _SCREAMING_SNAKE_CASE ( self : int , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Dict=None , **lowerCAmelCase__ : Tuple): SCREAMING_SNAKE_CASE_: int = VisionTextDualEncoderConfig.from_vision_text_configs(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = TFVisionTextDualEncoderModel(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = model(input_ids=lowerCAmelCase__ , pixel_values=lowerCAmelCase__ , attention_mask=lowerCAmelCase__) self.assertEqual(output["text_embeds"].shape , (input_ids.shape[0], config.projection_dim)) self.assertEqual(output["image_embeds"].shape , (pixel_values.shape[0], config.projection_dim)) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : int , lowerCAmelCase__ : str , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Any=None , **lowerCAmelCase__ : List[str]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = self.get_vision_text_model(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase__ , text_model=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model(input_ids=lowerCAmelCase__ , pixel_values=lowerCAmelCase__ , attention_mask=lowerCAmelCase__) self.assertEqual(output["text_embeds"].shape , (input_ids.shape[0], model.config.projection_dim)) self.assertEqual(output["image_embeds"].shape , (pixel_values.shape[0], model.config.projection_dim)) def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any , lowerCAmelCase__ : Optional[int]=None , **lowerCAmelCase__ : str): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = self.get_vision_text_model(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = {"vision_model": vision_model, "text_model": text_model} SCREAMING_SNAKE_CASE_: Dict = TFVisionTextDualEncoderModel.from_vision_text_pretrained(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = model(input_ids=lowerCAmelCase__ , pixel_values=lowerCAmelCase__ , attention_mask=lowerCAmelCase__) self.assertEqual(output["text_embeds"].shape , (input_ids.shape[0], model.config.projection_dim)) self.assertEqual(output["image_embeds"].shape , (pixel_values.shape[0], model.config.projection_dim)) def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : Any , lowerCAmelCase__ : int , lowerCAmelCase__ : Any , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Union[str, Any]=None , **lowerCAmelCase__ : Dict): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = self.get_vision_text_model(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase__ , text_model=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = model(input_ids=lowerCAmelCase__ , pixel_values=lowerCAmelCase__ , attention_mask=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = output[0].numpy() with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = TFVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model(input_ids=lowerCAmelCase__ , pixel_values=lowerCAmelCase__ , attention_mask=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = after_output[0].numpy() SCREAMING_SNAKE_CASE_: Union[str, Any] = np.amax(np.abs(out_a - out_a)) self.assertLessEqual(lowerCAmelCase__ , 1E-5) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : int , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : List[Any]=None , **lowerCAmelCase__ : List[Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self.get_vision_text_model(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase__ , text_model=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = model( input_ids=lowerCAmelCase__ , pixel_values=lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , output_attentions=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = output.vision_model_output.attentions self.assertEqual(len(lowerCAmelCase__) , vision_config.num_hidden_layers) # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token) SCREAMING_SNAKE_CASE_: List[str] = to_atuple(vision_model.config.image_size) SCREAMING_SNAKE_CASE_: Dict = to_atuple(vision_model.config.patch_size) SCREAMING_SNAKE_CASE_: int = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) SCREAMING_SNAKE_CASE_: str = num_patches + 1 self.assertEqual(vision_attentions[0].shape[-3:] , (vision_config.num_attention_heads, seq_len, seq_len)) SCREAMING_SNAKE_CASE_: Dict = output.text_model_output.attentions self.assertEqual(len(lowerCAmelCase__) , text_config.num_hidden_layers) self.assertEqual( text_attentions[0].shape[-3:] , (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1]) , ) def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : float): SCREAMING_SNAKE_CASE_: int = np.abs((a - b)).max() self.assertLessEqual(lowerCAmelCase__ , lowerCAmelCase__ , F"Difference between torch and flax is {diff} (>= {tol}).") def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: List[Any] = self.prepare_config_and_inputs() self.check_vision_text_dual_encoder_model(**lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: Tuple = self.prepare_config_and_inputs() self.check_model_from_pretrained_configs(**lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: List[Any] = self.prepare_config_and_inputs() self.check_vision_text_dual_encoder_from_pretrained(**lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: Any = self.prepare_config_and_inputs() self.check_save_load(**lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_: Any = self.prepare_config_and_inputs() self.check_vision_text_output_attention(**lowerCAmelCase__) @slow def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = self.get_pretrained_model_and_inputs() SCREAMING_SNAKE_CASE_: Dict = model_a(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = outputs[0].numpy() with tempfile.TemporaryDirectory() as tmp_dirname: model_a.save_pretrained(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = TFVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = model_a(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = after_outputs[0].numpy() SCREAMING_SNAKE_CASE_: Optional[Any] = np.amax(np.abs(out_a - out_a)) self.assertLessEqual(lowerCAmelCase__ , 1E-5) @require_tf class __lowercase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Tuple = TFVisionTextDualEncoderModel.from_vision_text_pretrained( "hf-internal-testing/tiny-random-vit" , "hf-internal-testing/tiny-random-bert") SCREAMING_SNAKE_CASE_: List[str] = 13 SCREAMING_SNAKE_CASE_: int = floats_tensor( [ batch_size, model.vision_model.config.num_channels, model.vision_model.config.image_size, model.vision_model.config.image_size, ]) SCREAMING_SNAKE_CASE_: List[str] = ids_tensor([batch_size, 4] , model.text_model.config.vocab_size) SCREAMING_SNAKE_CASE_: Union[str, Any] = random_attention_mask([batch_size, 4]) SCREAMING_SNAKE_CASE_: Dict = {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask} return model, inputs def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Union[str, Any]): SCREAMING_SNAKE_CASE_: Any = TFViTModel(lowerCAmelCase__ , name="vision_model") SCREAMING_SNAKE_CASE_: Optional[Any] = TFBertModel(lowerCAmelCase__ , name="text_model") return vision_model, text_model def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Union[str, Any] = TFViTModelTester(self) SCREAMING_SNAKE_CASE_: Union[str, Any] = TFBertModelTester(self) SCREAMING_SNAKE_CASE_: str = vit_model_tester.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_: List[Any] = bert_model_tester.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = vision_config_and_inputs ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ): List[Any] = text_config_and_inputs return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": input_mask, "input_ids": input_ids, "text_token_type_ids": token_type_ids, "text_sequence_labels": sequence_labels, "text_token_labels": token_labels, "text_choice_labels": choice_labels, } @require_tf class __lowercase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : Tuple): # DeiT repo doesn't have TF weights, but we don't actually use the weights at all so let's # just reinitialize it. SCREAMING_SNAKE_CASE_: Any = TFVisionTextDualEncoderModel.from_vision_text_pretrained( "Rocketknight1/tiny-random-deit-tf" , "hf-internal-testing/tiny-random-roberta") SCREAMING_SNAKE_CASE_: Optional[int] = 13 SCREAMING_SNAKE_CASE_: Optional[int] = floats_tensor( [ batch_size, model.vision_model.config.num_channels, model.vision_model.config.image_size, model.vision_model.config.image_size, ]) SCREAMING_SNAKE_CASE_: Union[str, Any] = ids_tensor([batch_size, 4] , model.text_model.config.vocab_size) SCREAMING_SNAKE_CASE_: Dict = random_attention_mask([batch_size, 4]) SCREAMING_SNAKE_CASE_: Optional[int] = {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask} return model, inputs def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Dict , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : Optional[Any]=None , **lowerCAmelCase__ : str): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = self.get_vision_text_model(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase__ , text_model=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = model( input_ids=lowerCAmelCase__ , pixel_values=lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , output_attentions=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = output.vision_model_output.attentions self.assertEqual(len(lowerCAmelCase__) , vision_config.num_hidden_layers) # in DEiT, the seq_len equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens) SCREAMING_SNAKE_CASE_: Dict = to_atuple(vision_model.config.image_size) SCREAMING_SNAKE_CASE_: int = to_atuple(vision_model.config.patch_size) SCREAMING_SNAKE_CASE_: List[str] = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) SCREAMING_SNAKE_CASE_: Union[str, Any] = num_patches + 2 self.assertEqual(vision_attentions[0].shape[-3:] , (vision_config.num_attention_heads, seq_len, seq_len)) SCREAMING_SNAKE_CASE_: List[Any] = output.text_model_output.attentions self.assertEqual(len(lowerCAmelCase__) , text_config.num_hidden_layers) self.assertEqual( text_attentions[0].shape[-3:] , (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1]) , ) def _SCREAMING_SNAKE_CASE ( self : int , lowerCAmelCase__ : int , lowerCAmelCase__ : str): SCREAMING_SNAKE_CASE_: Optional[int] = TFDeiTModel(lowerCAmelCase__ , name="vision_model") SCREAMING_SNAKE_CASE_: Optional[int] = TFRobertaModel(lowerCAmelCase__ , name="text_model") return vision_model, text_model def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_: Dict = TFDeiTModelTester(self) SCREAMING_SNAKE_CASE_: List[str] = TFRobertaModelTester(self) SCREAMING_SNAKE_CASE_: Optional[Any] = vit_model_tester.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_: Union[str, Any] = bert_model_tester.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = vision_config_and_inputs ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ): Tuple = text_config_and_inputs return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": input_mask, "input_ids": input_ids, "text_token_type_ids": token_type_ids, "text_sequence_labels": sequence_labels, "text_token_labels": token_labels, "text_choice_labels": choice_labels, } @require_tf class __lowercase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_: str = TFVisionTextDualEncoderModel.from_vision_text_pretrained( "Rocketknight1/tiny-random-clip-tf" , "hf-internal-testing/tiny-random-bert") SCREAMING_SNAKE_CASE_: List[str] = 13 SCREAMING_SNAKE_CASE_: Tuple = floats_tensor( [ batch_size, model.vision_model.config.num_channels, model.vision_model.config.image_size, model.vision_model.config.image_size, ]) SCREAMING_SNAKE_CASE_: Tuple = ids_tensor([batch_size, 4] , model.text_model.config.vocab_size) SCREAMING_SNAKE_CASE_: Dict = random_attention_mask([batch_size, 4]) SCREAMING_SNAKE_CASE_: Any = {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask} return model, inputs def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any): SCREAMING_SNAKE_CASE_: List[str] = TFCLIPVisionModel(lowerCAmelCase__ , name="vision_model") SCREAMING_SNAKE_CASE_: List[str] = TFBertModel(lowerCAmelCase__ , name="text_model") return vision_model, text_model def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Optional[int] = TFCLIPVisionModelTester(self) SCREAMING_SNAKE_CASE_: Any = TFBertModelTester(self) SCREAMING_SNAKE_CASE_: Optional[int] = clip_model_tester.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_: str = bert_model_tester.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = vision_config_and_inputs ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ): List[Any] = text_config_and_inputs return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": input_mask, "input_ids": input_ids, "text_token_type_ids": token_type_ids, "text_sequence_labels": sequence_labels, "text_token_labels": token_labels, "text_choice_labels": choice_labels, } @require_vision @require_tf class __lowercase ( unittest.TestCase ): """simple docstring""" @slow def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: Optional[int] = TFVisionTextDualEncoderModel.from_pretrained( "clip-italian/clip-italian" , logit_scale_init_value=1.0 , from_pt=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = VisionTextDualEncoderProcessor.from_pretrained("clip-italian/clip-italian") SCREAMING_SNAKE_CASE_: Optional[int] = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png") SCREAMING_SNAKE_CASE_: int = processor( text=["una foto di un gatto", "una foto di un cane"] , images=lowerCAmelCase__ , padding=lowerCAmelCase__ , return_tensors="np") SCREAMING_SNAKE_CASE_: List[str] = model(**lowerCAmelCase__) # verify the logits self.assertEqual(outputs.logits_per_image.shape , (inputs.pixel_values.shape[0], inputs.input_ids.shape[0])) self.assertEqual( outputs.logits_per_text.shape , (inputs.input_ids.shape[0], inputs.pixel_values.shape[0]) , ) SCREAMING_SNAKE_CASE_: Optional[Any] = np.array([[1.228_4727, 0.310_4122]]) self.assertTrue(np.allclose(outputs.logits_per_image.numpy() , lowerCAmelCase__ , atol=1E-3))
13
from collections.abc import Callable class __lowercase : """simple docstring""" def __init__( self : Tuple , lowerCAmelCase__ : Callable | None = None): # Stores actual heap items. SCREAMING_SNAKE_CASE_: list = [] # Stores indexes of each item for supporting updates and deletion. SCREAMING_SNAKE_CASE_: dict = {} # Stores current size of heap. SCREAMING_SNAKE_CASE_: Optional[Any] = 0 # Stores function used to evaluate the score of an item on which basis ordering # will be done. SCREAMING_SNAKE_CASE_: Any = key or (lambda lowerCAmelCase__: x) def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : int): return int((i - 1) / 2) if i > 0 else None def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Union[str, Any] = int(2 * i + 1) return left if 0 < left < self.size else None def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Union[str, Any] = int(2 * i + 2) return right if 0 < right < self.size else None def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : int , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = ( self.pos_map[self.arr[j][0]], self.pos_map[self.arr[i][0]], ) # Then swap the items in the list. SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = self.arr[j], self.arr[i] def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : int , lowerCAmelCase__ : int): return self.arr[i][1] < self.arr[j][1] def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Any = self._left(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = self._right(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = i if left is not None and not self._cmp(lowerCAmelCase__ , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Optional[int] = left if right is not None and not self._cmp(lowerCAmelCase__ , lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Tuple = right return valid_parent def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: List[Any] = self._parent(lowerCAmelCase__) while parent is not None and not self._cmp(lowerCAmelCase__ , lowerCAmelCase__): self._swap(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = parent, self._parent(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Optional[int] = self._get_valid_parent(lowerCAmelCase__) while valid_parent != index: self._swap(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = valid_parent, self._get_valid_parent(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : int , lowerCAmelCase__ : int): if item not in self.pos_map: return SCREAMING_SNAKE_CASE_: Any = self.pos_map[item] SCREAMING_SNAKE_CASE_: int = [item, self.key(lowerCAmelCase__)] # Make sure heap is right in both up and down direction. # Ideally only one of them will make any change. self._heapify_up(lowerCAmelCase__) self._heapify_down(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[str] , lowerCAmelCase__ : int): if item not in self.pos_map: return SCREAMING_SNAKE_CASE_: Optional[Any] = self.pos_map[item] del self.pos_map[item] SCREAMING_SNAKE_CASE_: List[str] = self.arr[self.size - 1] SCREAMING_SNAKE_CASE_: Tuple = index self.size -= 1 # Make sure heap is right in both up and down direction. Ideally only one # of them will make any change- so no performance loss in calling both. if self.size > index: self._heapify_up(lowerCAmelCase__) self._heapify_down(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : int): SCREAMING_SNAKE_CASE_: Optional[int] = len(self.arr) if arr_len == self.size: self.arr.append([item, self.key(lowerCAmelCase__)]) else: SCREAMING_SNAKE_CASE_: str = [item, self.key(lowerCAmelCase__)] SCREAMING_SNAKE_CASE_: List[Any] = self.size self.size += 1 self._heapify_up(self.size - 1) def _SCREAMING_SNAKE_CASE ( self : List[Any]): return self.arr[0] if self.size else None def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: Dict = self.get_top() if top_item_tuple: self.delete_item(top_item_tuple[0]) return top_item_tuple def A_ ( ): pass if __name__ == "__main__": import doctest doctest.testmod()
13
1
def A_ ( _UpperCAmelCase=2_81_23 ): SCREAMING_SNAKE_CASE_: Any = [1] * (limit + 1) for i in range(2 , int(limit**0.5 ) + 1 ): sum_divs[i * i] += i for k in range(i + 1 , limit // i + 1 ): sum_divs[k * i] += k + i SCREAMING_SNAKE_CASE_: int = set() SCREAMING_SNAKE_CASE_: Tuple = 0 for n in range(1 , limit + 1 ): if sum_divs[n] > n: abundants.add(_UpperCAmelCase ) if not any((n - a in abundants) for a in abundants ): res += n return res if __name__ == "__main__": print(solution())
13
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) if is_sentencepiece_available(): from ..ta.tokenization_ta import TaTokenizer else: from ...utils.dummy_sentencepiece_objects import TaTokenizer lowerCAmelCase : Any = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast lowerCAmelCase : Dict = TaTokenizerFast lowerCAmelCase : Optional[int] = {"""configuration_mt5""": ["""MT5Config""", """MT5OnnxConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : int = [ """MT5EncoderModel""", """MT5ForConditionalGeneration""", """MT5ForQuestionAnswering""", """MT5Model""", """MT5PreTrainedModel""", """MT5Stack""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Tuple = ["""TFMT5EncoderModel""", """TFMT5ForConditionalGeneration""", """TFMT5Model"""] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : List[str] = ["""FlaxMT5EncoderModel""", """FlaxMT5ForConditionalGeneration""", """FlaxMT5Model"""] if TYPE_CHECKING: from .configuration_mta import MTaConfig, MTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mta import ( MTaEncoderModel, MTaForConditionalGeneration, MTaForQuestionAnswering, MTaModel, MTaPreTrainedModel, MTaStack, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel else: import sys lowerCAmelCase : Optional[Any] = _LazyModule( __name__, globals()["""__file__"""], _import_structure, extra_objects={"""MT5Tokenizer""": MTaTokenizer, """MT5TokenizerFast""": MTaTokenizerFast}, module_spec=__spec__, )
13
1
from __future__ import annotations def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: int = [True] * limit SCREAMING_SNAKE_CASE_: int = False SCREAMING_SNAKE_CASE_: Dict = False SCREAMING_SNAKE_CASE_: Optional[Any] = True for i in range(3 , int(limit**0.5 + 1 ) , 2 ): SCREAMING_SNAKE_CASE_: List[str] = i * 2 while index < limit: SCREAMING_SNAKE_CASE_: Dict = False SCREAMING_SNAKE_CASE_: str = index + i SCREAMING_SNAKE_CASE_: str = [2] for i in range(3 , _UpperCAmelCase , 2 ): if is_prime[i]: primes.append(_UpperCAmelCase ) return primes def A_ ( _UpperCAmelCase = 1_00_00_00 ): SCREAMING_SNAKE_CASE_: Any = prime_sieve(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Optional[int] = 0 SCREAMING_SNAKE_CASE_: List[str] = 0 for i in range(len(_UpperCAmelCase ) ): for j in range(i + length , len(_UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_: Tuple = sum(primes[i:j] ) if sol >= ceiling: break if sol in primes: SCREAMING_SNAKE_CASE_: Dict = j - i SCREAMING_SNAKE_CASE_: Tuple = sol return largest if __name__ == "__main__": print(f'''{solution() = }''')
13
import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEPipeline from diffusers.pipelines.shap_e import ShapERenderer from diffusers.utils import 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 __lowercase ( UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : List[str] = ShapEPipeline _UpperCAmelCase : Tuple = ['''prompt'''] _UpperCAmelCase : Dict = ['''prompt'''] _UpperCAmelCase : Any = [ '''num_images_per_prompt''', '''num_inference_steps''', '''generator''', '''latents''', '''guidance_scale''', '''frame_size''', '''output_type''', '''return_dict''', ] _UpperCAmelCase : Optional[int] = False @property def _SCREAMING_SNAKE_CASE ( self : List[str]): return 32 @property def _SCREAMING_SNAKE_CASE ( self : List[str]): return 32 @property def _SCREAMING_SNAKE_CASE ( self : int): return self.time_input_dim * 4 @property def _SCREAMING_SNAKE_CASE ( self : Optional[int]): return 8 @property def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: str = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip") return tokenizer @property def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Optional[int] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) return CLIPTextModelWithProjection(lowerCAmelCase__) @property def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Tuple = { "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", "encoder_hid_proj_type": None, "added_emb_type": None, } SCREAMING_SNAKE_CASE_: Any = PriorTransformer(**lowerCAmelCase__) return model @property def _SCREAMING_SNAKE_CASE ( self : Dict): torch.manual_seed(0) SCREAMING_SNAKE_CASE_: Union[str, Any] = { "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, ), } SCREAMING_SNAKE_CASE_: Optional[int] = ShapERenderer(**lowerCAmelCase__) return model def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_: Dict = self.dummy_prior SCREAMING_SNAKE_CASE_: Optional[Any] = self.dummy_text_encoder SCREAMING_SNAKE_CASE_: Union[str, Any] = self.dummy_tokenizer SCREAMING_SNAKE_CASE_: List[str] = self.dummy_renderer SCREAMING_SNAKE_CASE_: Any = HeunDiscreteScheduler( beta_schedule="exp" , num_train_timesteps=1024 , prediction_type="sample" , use_karras_sigmas=lowerCAmelCase__ , clip_sample=lowerCAmelCase__ , clip_sample_range=1.0 , ) SCREAMING_SNAKE_CASE_: Optional[int] = { "prior": prior, "text_encoder": text_encoder, "tokenizer": tokenizer, "renderer": renderer, "scheduler": scheduler, } return components def _SCREAMING_SNAKE_CASE ( self : Any , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[Any]=0): if str(lowerCAmelCase__).startswith("mps"): SCREAMING_SNAKE_CASE_: Optional[Any] = torch.manual_seed(lowerCAmelCase__) else: SCREAMING_SNAKE_CASE_: Any = torch.Generator(device=lowerCAmelCase__).manual_seed(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = { "prompt": "horse", "generator": generator, "num_inference_steps": 1, "frame_size": 32, "output_type": "np", } return inputs def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_: str = "cpu" SCREAMING_SNAKE_CASE_: Tuple = self.get_dummy_components() SCREAMING_SNAKE_CASE_: Dict = self.pipeline_class(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = pipe(**self.get_dummy_inputs(lowerCAmelCase__)) SCREAMING_SNAKE_CASE_: Optional[Any] = output.images[0] SCREAMING_SNAKE_CASE_: Any = image[0, -3:, -3:, -1] assert image.shape == (20, 32, 32, 3) SCREAMING_SNAKE_CASE_: Union[str, Any] = np.array( [ 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, ]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-2 def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches self._test_inference_batch_consistent(batch_sizes=[1, 2]) def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_: Dict = torch_device == "cpu" SCREAMING_SNAKE_CASE_: List[Any] = True self._test_inference_batch_single_identical( batch_size=2 , test_max_difference=lowerCAmelCase__ , relax_max_difference=lowerCAmelCase__ , ) def _SCREAMING_SNAKE_CASE ( self : int): SCREAMING_SNAKE_CASE_: Dict = self.get_dummy_components() SCREAMING_SNAKE_CASE_: str = self.pipeline_class(**lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = 1 SCREAMING_SNAKE_CASE_: Any = 2 SCREAMING_SNAKE_CASE_: Dict = self.get_dummy_inputs(lowerCAmelCase__) for key in inputs.keys(): if key in self.batch_params: SCREAMING_SNAKE_CASE_: List[Any] = batch_size * [inputs[key]] SCREAMING_SNAKE_CASE_: Tuple = pipe(**lowerCAmelCase__ , num_images_per_prompt=lowerCAmelCase__)[0] assert images.shape[0] == batch_size * num_images_per_prompt @slow @require_torch_gpu class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : Any): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_: List[str] = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/shap_e/test_shap_e_np_out.npy") SCREAMING_SNAKE_CASE_: List[str] = ShapEPipeline.from_pretrained("openai/shap-e") SCREAMING_SNAKE_CASE_: Optional[int] = pipe.to(lowerCAmelCase__) pipe.set_progress_bar_config(disable=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = torch.Generator(device=lowerCAmelCase__).manual_seed(0) SCREAMING_SNAKE_CASE_: int = pipe( "a shark" , generator=lowerCAmelCase__ , guidance_scale=15.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__)
13
1
from sklearn.metrics import recall_score import datasets lowerCAmelCase : Optional[int] = """ Recall is the fraction of the positive examples that were correctly labeled by the model as positive. It can be computed with the equation: Recall = TP / (TP + FN) Where TP is the true positives and FN is the false negatives. """ lowerCAmelCase : int = """ Args: - **predictions** (`list` of `int`): The predicted labels. - **references** (`list` of `int`): The ground truth labels. - **labels** (`list` of `int`): The set of labels to include when `average` is not set to `binary`, and their order when average is `None`. Labels present in the data can be excluded in this input, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order. Defaults to None. - **pos_label** (`int`): The class label to use as the 'positive class' when calculating the recall. Defaults to `1`. - **average** (`string`): This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `'binary'`. - `'binary'`: Only report results for the class specified by `pos_label`. This is applicable only if the target labels and predictions are binary. - `'micro'`: Calculate metrics globally by counting the total true positives, false negatives, and false positives. - `'macro'`: Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. - `'weighted'`: Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `'macro'` to account for label imbalance. Note that it can result in an F-score that is not between precision and recall. - `'samples'`: Calculate metrics for each instance, and find their average (only meaningful for multilabel classification). - **sample_weight** (`list` of `float`): Sample weights Defaults to `None`. - **zero_division** (): Sets the value to return when there is a zero division. Defaults to . - `'warn'`: If there is a zero division, the return value is `0`, but warnings are also raised. - `0`: If there is a zero division, the return value is `0`. - `1`: If there is a zero division, the return value is `1`. Returns: - **recall** (`float`, or `array` of `float`): Either the general recall score, or the recall scores for individual classes, depending on the values input to `labels` and `average`. Minimum possible value is 0. Maximum possible value is 1. A higher recall means that more of the positive examples have been labeled correctly. Therefore, a higher recall is generally considered better. Examples: Example 1-A simple example with some errors >>> recall_metric = datasets.load_metric('recall') >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1]) >>> print(results) {'recall': 0.6666666666666666} Example 2-The same example as Example 1, but with `pos_label=0` instead of the default `pos_label=1`. >>> recall_metric = datasets.load_metric('recall') >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], pos_label=0) >>> print(results) {'recall': 0.5} Example 3-The same example as Example 1, but with `sample_weight` included. >>> recall_metric = datasets.load_metric('recall') >>> sample_weight = [0.9, 0.2, 0.9, 0.3, 0.8] >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], sample_weight=sample_weight) >>> print(results) {'recall': 0.55} Example 4-A multiclass example, using different averages. >>> recall_metric = datasets.load_metric('recall') >>> predictions = [0, 2, 1, 0, 0, 1] >>> references = [0, 1, 2, 0, 1, 2] >>> results = recall_metric.compute(predictions=predictions, references=references, average='macro') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average='micro') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average='weighted') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average=None) >>> print(results) {'recall': array([1., 0., 0.])} """ lowerCAmelCase : Tuple = """ @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 __lowercase ( datasets.Metric ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : List[str]): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Sequence(datasets.Value("int32")), "references": datasets.Sequence(datasets.Value("int32")), } if self.config_name == "multilabel" else { "predictions": datasets.Value("int32"), "references": datasets.Value("int32"), }) , reference_urls=["https://scikit-learn.org/stable/modules/generated/sklearn.metrics.recall_score.html"] , ) def _SCREAMING_SNAKE_CASE ( self : List[Any] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : Tuple=None , lowerCAmelCase__ : str=1 , lowerCAmelCase__ : List[str]="binary" , lowerCAmelCase__ : Tuple=None , lowerCAmelCase__ : str="warn" , ): SCREAMING_SNAKE_CASE_: List[str] = recall_score( lowerCAmelCase__ , lowerCAmelCase__ , labels=lowerCAmelCase__ , pos_label=lowerCAmelCase__ , average=lowerCAmelCase__ , sample_weight=lowerCAmelCase__ , zero_division=lowerCAmelCase__ , ) return {"recall": float(lowerCAmelCase__) if score.size == 1 else score}
13
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
13
1
import argparse from tax import checkpoints from transformers import AutoConfig, FlaxAutoModelForSeqaSeqLM def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: int = AutoConfig.from_pretrained(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[str] = FlaxAutoModelForSeqaSeqLM.from_config(config=_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Optional[Any] = checkpoints.load_tax_checkpoint(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Any = "wi_0" in tax_model["target"]["encoder"]["layers_0"]["mlp"] if config.model_type == "t5": SCREAMING_SNAKE_CASE_: str = "SelfAttention" if config.model_type == "longt5" and config.encoder_attention_type == "local": SCREAMING_SNAKE_CASE_: Any = "LocalSelfAttention" elif config.model_type == "longt5" and config.encoder_attention_type == "transient-global": SCREAMING_SNAKE_CASE_: Any = "TransientGlobalSelfAttention" else: raise ValueError( "Given config is expected to have `model_type='t5'`, or `model_type='longt5` with `encoder_attention_type`" " attribute with a value from ['local', 'transient-global]." ) # Encoder for layer_index in range(config.num_layers ): SCREAMING_SNAKE_CASE_: int = f"layers_{str(_UpperCAmelCase )}" # Self-Attention SCREAMING_SNAKE_CASE_: Optional[int] = tax_model["target"]["encoder"][layer_name]["attention"]["key"]["kernel"] SCREAMING_SNAKE_CASE_: int = tax_model["target"]["encoder"][layer_name]["attention"]["out"]["kernel"] SCREAMING_SNAKE_CASE_: Dict = tax_model["target"]["encoder"][layer_name]["attention"]["query"]["kernel"] SCREAMING_SNAKE_CASE_: str = tax_model["target"]["encoder"][layer_name]["attention"]["value"]["kernel"] # Global input layer norm if config.model_type == "longt5" and config.encoder_attention_type == "transient-global": SCREAMING_SNAKE_CASE_: int = tax_model["target"]["encoder"][layer_name]["attention"]["T5LayerNorm_0"]["scale"] # Layer Normalization SCREAMING_SNAKE_CASE_: Any = tax_model["target"]["encoder"][layer_name]["pre_attention_layer_norm"]["scale"] if split_mlp_wi: SCREAMING_SNAKE_CASE_: int = tax_model["target"]["encoder"][layer_name]["mlp"]["wi_0"]["kernel"] SCREAMING_SNAKE_CASE_: Optional[Any] = tax_model["target"]["encoder"][layer_name]["mlp"]["wi_1"]["kernel"] else: SCREAMING_SNAKE_CASE_: Optional[int] = tax_model["target"]["encoder"][layer_name]["mlp"]["wi"]["kernel"] SCREAMING_SNAKE_CASE_: Dict = tax_model["target"]["encoder"][layer_name]["mlp"]["wo"]["kernel"] # Layer Normalization SCREAMING_SNAKE_CASE_: Dict = tax_model["target"]["encoder"][layer_name]["pre_mlp_layer_norm"]["scale"] # Assigning SCREAMING_SNAKE_CASE_: List[str] = flax_model.params["encoder"]["block"][str(_UpperCAmelCase )]["layer"] SCREAMING_SNAKE_CASE_: Any = tax_attention_key SCREAMING_SNAKE_CASE_: str = tax_attention_out SCREAMING_SNAKE_CASE_: Union[str, Any] = tax_attention_query SCREAMING_SNAKE_CASE_: Optional[int] = tax_attention_value SCREAMING_SNAKE_CASE_: Any = tax_attention_layer_norm # Global input layer norm if config.model_type == "longt5" and config.encoder_attention_type == "transient-global": SCREAMING_SNAKE_CASE_: Optional[Any] = tax_global_layer_norm if split_mlp_wi: SCREAMING_SNAKE_CASE_: Any = tax_mlp_wi_a SCREAMING_SNAKE_CASE_: Any = tax_mlp_wi_a else: SCREAMING_SNAKE_CASE_: Optional[int] = tax_mlp_wi SCREAMING_SNAKE_CASE_: str = tax_mlp_wo SCREAMING_SNAKE_CASE_: int = tax_mlp_layer_norm SCREAMING_SNAKE_CASE_: int = flax_model_encoder_layer_block # Only for layer 0: SCREAMING_SNAKE_CASE_: Tuple = tax_model["target"]["encoder"]["relpos_bias"]["rel_embedding"].T SCREAMING_SNAKE_CASE_: List[str] = tax_encoder_rel_embedding # Side/global relative position_bias + layer norm if config.model_type == "longt5" and config.encoder_attention_type == "transient-global": SCREAMING_SNAKE_CASE_: List[Any] = tax_model["target"]["encoder"]["side_relpos_bias"]["rel_embedding"].T SCREAMING_SNAKE_CASE_: Any = tax_encoder_global_rel_embedding # Assigning SCREAMING_SNAKE_CASE_: List[str] = tax_model["target"]["encoder"]["encoder_norm"]["scale"] SCREAMING_SNAKE_CASE_: Any = tax_encoder_norm # Decoder for layer_index in range(config.num_layers ): SCREAMING_SNAKE_CASE_: int = f"layers_{str(_UpperCAmelCase )}" # Self-Attention SCREAMING_SNAKE_CASE_: str = tax_model["target"]["decoder"][layer_name]["self_attention"]["key"]["kernel"] SCREAMING_SNAKE_CASE_: List[str] = tax_model["target"]["decoder"][layer_name]["self_attention"]["out"]["kernel"] SCREAMING_SNAKE_CASE_: List[str] = tax_model["target"]["decoder"][layer_name]["self_attention"]["query"]["kernel"] SCREAMING_SNAKE_CASE_: Optional[Any] = tax_model["target"]["decoder"][layer_name]["self_attention"]["value"]["kernel"] # Layer Normalization SCREAMING_SNAKE_CASE_: int = tax_model["target"]["decoder"][layer_name]["pre_self_attention_layer_norm"][ "scale" ] # Encoder-Decoder-Attention SCREAMING_SNAKE_CASE_: List[Any] = tax_model["target"]["decoder"][layer_name]["encoder_decoder_attention"] SCREAMING_SNAKE_CASE_: Any = tax_enc_dec_attention_module["key"]["kernel"] SCREAMING_SNAKE_CASE_: int = tax_enc_dec_attention_module["out"]["kernel"] SCREAMING_SNAKE_CASE_: Tuple = tax_enc_dec_attention_module["query"]["kernel"] SCREAMING_SNAKE_CASE_: List[str] = tax_enc_dec_attention_module["value"]["kernel"] # Layer Normalization SCREAMING_SNAKE_CASE_: Union[str, Any] = tax_model["target"]["decoder"][layer_name]["pre_cross_attention_layer_norm"]["scale"] # MLP if split_mlp_wi: SCREAMING_SNAKE_CASE_: Any = tax_model["target"]["decoder"][layer_name]["mlp"]["wi_0"]["kernel"] SCREAMING_SNAKE_CASE_: Optional[Any] = tax_model["target"]["decoder"][layer_name]["mlp"]["wi_1"]["kernel"] else: SCREAMING_SNAKE_CASE_: int = tax_model["target"]["decoder"][layer_name]["mlp"]["wi"]["kernel"] SCREAMING_SNAKE_CASE_: int = tax_model["target"]["decoder"][layer_name]["mlp"]["wo"]["kernel"] # Layer Normalization SCREAMING_SNAKE_CASE_: Union[str, Any] = tax_model["target"]["decoder"][layer_name]["pre_mlp_layer_norm"]["scale"] # Assigning SCREAMING_SNAKE_CASE_: List[str] = flax_model.params["decoder"]["block"][str(_UpperCAmelCase )]["layer"] SCREAMING_SNAKE_CASE_: Optional[int] = tax_attention_key SCREAMING_SNAKE_CASE_: Optional[Any] = tax_attention_out SCREAMING_SNAKE_CASE_: List[Any] = tax_attention_query SCREAMING_SNAKE_CASE_: Optional[int] = tax_attention_value SCREAMING_SNAKE_CASE_: List[Any] = tax_pre_attention_layer_norm SCREAMING_SNAKE_CASE_: Optional[int] = tax_enc_dec_attention_key SCREAMING_SNAKE_CASE_: List[Any] = tax_enc_dec_attention_out SCREAMING_SNAKE_CASE_: List[Any] = tax_enc_dec_attention_query SCREAMING_SNAKE_CASE_: Optional[Any] = tax_enc_dec_attention_value SCREAMING_SNAKE_CASE_: int = tax_cross_layer_norm if split_mlp_wi: SCREAMING_SNAKE_CASE_: Optional[int] = tax_mlp_wi_a SCREAMING_SNAKE_CASE_: Any = tax_mlp_wi_a else: SCREAMING_SNAKE_CASE_: Any = tax_mlp_wi SCREAMING_SNAKE_CASE_: Optional[Any] = tax_mlp_wo SCREAMING_SNAKE_CASE_: Optional[int] = txa_mlp_layer_norm SCREAMING_SNAKE_CASE_: Optional[Any] = flax_model_decoder_layer_block # Decoder Normalization SCREAMING_SNAKE_CASE_: Optional[int] = tax_model["target"]["decoder"]["decoder_norm"]["scale"] SCREAMING_SNAKE_CASE_: Any = txa_decoder_norm # Only for layer 0: SCREAMING_SNAKE_CASE_: Tuple = tax_model["target"]["decoder"]["relpos_bias"]["rel_embedding"].T SCREAMING_SNAKE_CASE_: Any = tax_decoder_rel_embedding # Token Embeddings SCREAMING_SNAKE_CASE_: Dict = tax_model["target"]["token_embedder"]["embedding"] SCREAMING_SNAKE_CASE_: Optional[Any] = txa_token_embeddings # LM Head (only in v1.1 and LongT5 checkpoints) if "logits_dense" in tax_model["target"]["decoder"]: SCREAMING_SNAKE_CASE_: Optional[int] = tax_model["target"]["decoder"]["logits_dense"]["kernel"] flax_model.save_pretrained(_UpperCAmelCase ) print("T5X Model was sucessfully converted!" ) if __name__ == "__main__": lowerCAmelCase : List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--t5x_checkpoint_path""", default=None, type=str, required=True, help="""Path the T5X checkpoint.""" ) parser.add_argument("""--config_name""", default=None, type=str, required=True, help="""Config name of LongT5/T5 model.""") parser.add_argument( """--flax_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output FLAX model.""" ) lowerCAmelCase : List[str] = parser.parse_args() convert_tax_checkpoint_to_flax(args.tax_checkpoint_path, args.config_name, args.flax_dump_folder_path)
13
class __lowercase : """simple docstring""" def __init__( self : List[Any] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[Any]): SCREAMING_SNAKE_CASE_: List[str] = name SCREAMING_SNAKE_CASE_: Union[str, Any] = val def __str__( self : Dict): return F"{self.__class__.__name__}({self.name}, {self.val})" def __lt__( self : List[str] , lowerCAmelCase__ : Any): return self.val < other.val class __lowercase : """simple docstring""" def __init__( self : Tuple , lowerCAmelCase__ : Dict): SCREAMING_SNAKE_CASE_: str = {} SCREAMING_SNAKE_CASE_: int = {} SCREAMING_SNAKE_CASE_: Any = self.build_heap(lowerCAmelCase__) def __getitem__( self : List[Any] , lowerCAmelCase__ : Dict): return self.get_value(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : Dict): return (idx - 1) // 2 def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Optional[Any]): return idx * 2 + 1 def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Tuple): return idx * 2 + 2 def _SCREAMING_SNAKE_CASE ( self : Optional[int] , lowerCAmelCase__ : Optional[int]): return self.heap_dict[key] def _SCREAMING_SNAKE_CASE ( self : List[Any] , lowerCAmelCase__ : Union[str, Any]): SCREAMING_SNAKE_CASE_: Tuple = len(lowerCAmelCase__) - 1 SCREAMING_SNAKE_CASE_: List[str] = self.get_parent_idx(lowerCAmelCase__) for idx, i in enumerate(lowerCAmelCase__): SCREAMING_SNAKE_CASE_: Union[str, Any] = idx SCREAMING_SNAKE_CASE_: str = i.val for i in range(lowerCAmelCase__ , -1 , -1): self.sift_down(lowerCAmelCase__ , lowerCAmelCase__) return array def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[str]): while True: SCREAMING_SNAKE_CASE_: Optional[Any] = self.get_left_child_idx(lowerCAmelCase__) # noqa: E741 SCREAMING_SNAKE_CASE_: Dict = self.get_right_child_idx(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = idx if l < len(lowerCAmelCase__) and array[l] < array[idx]: SCREAMING_SNAKE_CASE_: List[str] = l if r < len(lowerCAmelCase__) and array[r] < array[smallest]: SCREAMING_SNAKE_CASE_: str = r if smallest != idx: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = array[smallest], array[idx] ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ): Optional[Any] = ( self.idx_of_element[array[smallest]], self.idx_of_element[array[idx]], ) SCREAMING_SNAKE_CASE_: Optional[int] = smallest else: break def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : str): SCREAMING_SNAKE_CASE_: Any = self.get_parent_idx(lowerCAmelCase__) while p >= 0 and self.heap[p] > self.heap[idx]: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = self.heap[idx], self.heap[p] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = ( self.idx_of_element[self.heap[idx]], self.idx_of_element[self.heap[p]], ) SCREAMING_SNAKE_CASE_: Union[str, Any] = p SCREAMING_SNAKE_CASE_: Optional[int] = self.get_parent_idx(lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[Any]): return self.heap[0] def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self.heap[-1], self.heap[0] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[str] = ( self.idx_of_element[self.heap[-1]], self.idx_of_element[self.heap[0]], ) SCREAMING_SNAKE_CASE_: int = self.heap.pop() del self.idx_of_element[x] self.sift_down(0 , self.heap) return x def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] , lowerCAmelCase__ : Tuple): self.heap.append(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = len(self.heap) - 1 SCREAMING_SNAKE_CASE_: List[str] = node.val self.sift_up(len(self.heap) - 1) def _SCREAMING_SNAKE_CASE ( self : List[Any]): return len(self.heap) == 0 def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Optional[int]): assert ( self.heap[self.idx_of_element[node]].val > new_value ), "newValue must be less that current value" SCREAMING_SNAKE_CASE_: Any = new_value SCREAMING_SNAKE_CASE_: Tuple = new_value self.sift_up(self.idx_of_element[node]) lowerCAmelCase : int = Node("""R""", -1) lowerCAmelCase : str = Node("""B""", 6) lowerCAmelCase : str = Node("""A""", 3) lowerCAmelCase : List[str] = Node("""X""", 1) lowerCAmelCase : Union[str, Any] = Node("""E""", 4) # Use one of these two ways to generate Min-Heap # Generating Min-Heap from array lowerCAmelCase : Optional[Any] = MinHeap([r, b, a, x, e]) # Generating Min-Heap by Insert method # myMinHeap.insert(a) # myMinHeap.insert(b) # myMinHeap.insert(x) # myMinHeap.insert(r) # myMinHeap.insert(e) # Before print("""Min Heap - before decrease key""") for i in my_min_heap.heap: print(i) print("""Min Heap - After decrease key of node [B -> -17]""") my_min_heap.decrease_key(b, -17) # After for i in my_min_heap.heap: print(i) if __name__ == "__main__": import doctest doctest.testmod()
13
1
import gc import random import unittest import torch from diffusers import ( IFImgaImgPipeline, IFImgaImgSuperResolutionPipeline, IFInpaintingPipeline, IFInpaintingSuperResolutionPipeline, IFPipeline, IFSuperResolutionPipeline, ) from diffusers.models.attention_processor import AttnAddedKVProcessor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import floats_tensor, load_numpy, require_torch_gpu, skip_mps, slow, torch_device from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference from . import IFPipelineTesterMixin @skip_mps class __lowercase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : str = IFPipeline _UpperCAmelCase : Tuple = TEXT_TO_IMAGE_PARAMS - {'''width''', '''height''', '''latents'''} _UpperCAmelCase : Any = TEXT_TO_IMAGE_BATCH_PARAMS _UpperCAmelCase : Union[str, Any] = PipelineTesterMixin.required_optional_params - {'''latents'''} def _SCREAMING_SNAKE_CASE ( self : str): return self._get_dummy_components() def _SCREAMING_SNAKE_CASE ( self : Tuple , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[str]=0): if str(lowerCAmelCase__).startswith("mps"): SCREAMING_SNAKE_CASE_: int = torch.manual_seed(lowerCAmelCase__) else: SCREAMING_SNAKE_CASE_: Optional[int] = torch.Generator(device=lowerCAmelCase__).manual_seed(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = { "prompt": "A painting of a squirrel eating a burger", "generator": generator, "num_inference_steps": 2, "output_type": "numpy", } return inputs def _SCREAMING_SNAKE_CASE ( self : Dict): self._test_save_load_optional_components() @unittest.skipIf(torch_device != "cuda" , reason="float16 requires CUDA") def _SCREAMING_SNAKE_CASE ( self : Dict): # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder super().test_save_load_floataa(expected_max_diff=1E-1) def _SCREAMING_SNAKE_CASE ( self : Dict): self._test_attention_slicing_forward_pass(expected_max_diff=1E-2) def _SCREAMING_SNAKE_CASE ( self : int): self._test_save_load_local() def _SCREAMING_SNAKE_CASE ( self : List[Any]): self._test_inference_batch_single_identical( expected_max_diff=1E-2 , ) @unittest.skipIf( torch_device != "cuda" or not is_xformers_available() , reason="XFormers attention is only available with CUDA and `xformers` installed" , ) def _SCREAMING_SNAKE_CASE ( self : Dict): self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3) @slow @require_torch_gpu class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : List[Any]): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): # if SCREAMING_SNAKE_CASE_: str = IFPipeline.from_pretrained("DeepFloyd/IF-I-XL-v1.0" , variant="fp16" , torch_dtype=torch.floataa) SCREAMING_SNAKE_CASE_: Optional[int] = IFSuperResolutionPipeline.from_pretrained( "DeepFloyd/IF-II-L-v1.0" , variant="fp16" , torch_dtype=torch.floataa , text_encoder=lowerCAmelCase__ , tokenizer=lowerCAmelCase__) # pre compute text embeddings and remove T5 to save memory pipe_a.text_encoder.to("cuda") SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = pipe_a.encode_prompt("anime turtle" , device="cuda") del pipe_a.tokenizer del pipe_a.text_encoder gc.collect() SCREAMING_SNAKE_CASE_: Any = None SCREAMING_SNAKE_CASE_: int = None pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()) self._test_if(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) pipe_a.remove_all_hooks() pipe_a.remove_all_hooks() # img2img SCREAMING_SNAKE_CASE_: Any = IFImgaImgPipeline(**pipe_a.components) SCREAMING_SNAKE_CASE_: Optional[int] = IFImgaImgSuperResolutionPipeline(**pipe_a.components) pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()) self._test_if_imgaimg(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) pipe_a.remove_all_hooks() pipe_a.remove_all_hooks() # inpainting SCREAMING_SNAKE_CASE_: Optional[int] = IFInpaintingPipeline(**pipe_a.components) SCREAMING_SNAKE_CASE_: Optional[Any] = IFInpaintingSuperResolutionPipeline(**pipe_a.components) pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()) self._test_if_inpainting(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : int , lowerCAmelCase__ : str , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Tuple): # pipeline 1 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.Generator(device="cpu").manual_seed(0) SCREAMING_SNAKE_CASE_: Optional[int] = pipe_a( prompt_embeds=lowerCAmelCase__ , negative_prompt_embeds=lowerCAmelCase__ , num_inference_steps=2 , generator=lowerCAmelCase__ , output_type="np" , ) SCREAMING_SNAKE_CASE_: Optional[int] = output.images[0] assert image.shape == (64, 64, 3) SCREAMING_SNAKE_CASE_: Any = torch.cuda.max_memory_allocated() assert mem_bytes < 13 * 10**9 SCREAMING_SNAKE_CASE_: Any = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if.npy") assert_mean_pixel_difference(lowerCAmelCase__ , lowerCAmelCase__) # pipeline 2 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE_: List[Any] = torch.Generator(device="cpu").manual_seed(0) SCREAMING_SNAKE_CASE_: Any = floats_tensor((1, 3, 64, 64) , rng=random.Random(0)).to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = pipe_a( prompt_embeds=lowerCAmelCase__ , negative_prompt_embeds=lowerCAmelCase__ , image=lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=2 , output_type="np" , ) SCREAMING_SNAKE_CASE_: str = output.images[0] assert image.shape == (256, 256, 3) SCREAMING_SNAKE_CASE_: Optional[Any] = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 SCREAMING_SNAKE_CASE_: Dict = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_superresolution_stage_II.npy") assert_mean_pixel_difference(lowerCAmelCase__ , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : int , lowerCAmelCase__ : Optional[int]): # pipeline 1 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE_: Optional[int] = floats_tensor((1, 3, 64, 64) , rng=random.Random(0)).to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = torch.Generator(device="cpu").manual_seed(0) SCREAMING_SNAKE_CASE_: List[Any] = pipe_a( prompt_embeds=lowerCAmelCase__ , negative_prompt_embeds=lowerCAmelCase__ , image=lowerCAmelCase__ , num_inference_steps=2 , generator=lowerCAmelCase__ , output_type="np" , ) SCREAMING_SNAKE_CASE_: str = output.images[0] assert image.shape == (64, 64, 3) SCREAMING_SNAKE_CASE_: List[str] = torch.cuda.max_memory_allocated() assert mem_bytes < 10 * 10**9 SCREAMING_SNAKE_CASE_: int = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img.npy") assert_mean_pixel_difference(lowerCAmelCase__ , lowerCAmelCase__) # pipeline 2 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.Generator(device="cpu").manual_seed(0) SCREAMING_SNAKE_CASE_: Optional[Any] = floats_tensor((1, 3, 256, 256) , rng=random.Random(0)).to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = floats_tensor((1, 3, 64, 64) , rng=random.Random(0)).to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = pipe_a( prompt_embeds=lowerCAmelCase__ , negative_prompt_embeds=lowerCAmelCase__ , image=lowerCAmelCase__ , original_image=lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=2 , output_type="np" , ) SCREAMING_SNAKE_CASE_: Any = output.images[0] assert image.shape == (256, 256, 3) SCREAMING_SNAKE_CASE_: Dict = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 SCREAMING_SNAKE_CASE_: Any = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img_superresolution_stage_II.npy") assert_mean_pixel_difference(lowerCAmelCase__ , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : int): # pipeline 1 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE_: List[Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(0)).to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = floats_tensor((1, 3, 64, 64) , rng=random.Random(1)).to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = torch.Generator(device="cpu").manual_seed(0) SCREAMING_SNAKE_CASE_: Tuple = pipe_a( prompt_embeds=lowerCAmelCase__ , negative_prompt_embeds=lowerCAmelCase__ , image=lowerCAmelCase__ , mask_image=lowerCAmelCase__ , num_inference_steps=2 , generator=lowerCAmelCase__ , output_type="np" , ) SCREAMING_SNAKE_CASE_: int = output.images[0] assert image.shape == (64, 64, 3) SCREAMING_SNAKE_CASE_: Tuple = torch.cuda.max_memory_allocated() assert mem_bytes < 10 * 10**9 SCREAMING_SNAKE_CASE_: Union[str, Any] = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting.npy") assert_mean_pixel_difference(lowerCAmelCase__ , lowerCAmelCase__) # pipeline 2 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE_: Optional[Any] = torch.Generator(device="cpu").manual_seed(0) SCREAMING_SNAKE_CASE_: List[Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(0)).to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = floats_tensor((1, 3, 256, 256) , rng=random.Random(0)).to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = floats_tensor((1, 3, 256, 256) , rng=random.Random(1)).to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = pipe_a( prompt_embeds=lowerCAmelCase__ , negative_prompt_embeds=lowerCAmelCase__ , image=lowerCAmelCase__ , mask_image=lowerCAmelCase__ , original_image=lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=2 , output_type="np" , ) SCREAMING_SNAKE_CASE_: Union[str, Any] = output.images[0] assert image.shape == (256, 256, 3) SCREAMING_SNAKE_CASE_: List[str] = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 SCREAMING_SNAKE_CASE_: List[Any] = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting_superresolution_stage_II.npy") assert_mean_pixel_difference(lowerCAmelCase__ , lowerCAmelCase__) def A_ ( ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats()
13
import random import unittest import numpy as np import transformers from transformers import is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax if is_flax_available(): import os import jax.numpy as jnp from jax import jit from transformers import AutoTokenizer, FlaxAutoModelForCausalLM from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model lowerCAmelCase : Any = """0.12""" # assumed parallelism: 8 if is_torch_available(): import torch def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=None ): if rng is None: SCREAMING_SNAKE_CASE_: List[Any] = random.Random() SCREAMING_SNAKE_CASE_: Optional[Any] = 1 for dim in shape: total_dims *= dim SCREAMING_SNAKE_CASE_: Optional[Any] = [] for _ in range(_UpperCAmelCase ): values.append(rng.randint(0 , vocab_size - 1 ) ) SCREAMING_SNAKE_CASE_: List[Any] = np.array(_UpperCAmelCase , dtype=jnp.intaa ).reshape(_UpperCAmelCase ) return output def A_ ( _UpperCAmelCase , _UpperCAmelCase=None ): SCREAMING_SNAKE_CASE_: Optional[int] = ids_tensor(_UpperCAmelCase , vocab_size=2 , rng=_UpperCAmelCase ) # make sure that at least one token is attended to for each batch SCREAMING_SNAKE_CASE_: Optional[Any] = 1 return attn_mask @require_flax class __lowercase : """simple docstring""" _UpperCAmelCase : Any = None _UpperCAmelCase : List[Any] = () def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self.model_tester.prepare_config_and_inputs_for_common() # cut to half length & take max batch_size 3 SCREAMING_SNAKE_CASE_: str = 2 SCREAMING_SNAKE_CASE_: Optional[int] = inputs["input_ids"].shape[-1] // 2 SCREAMING_SNAKE_CASE_: List[str] = inputs["input_ids"][:max_batch_size, :sequence_length] SCREAMING_SNAKE_CASE_: Any = jnp.ones_like(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = attention_mask[:max_batch_size, :sequence_length] # generate max 5 tokens SCREAMING_SNAKE_CASE_: Optional[Any] = input_ids.shape[-1] + 5 if config.eos_token_id is not None and config.pad_token_id is None: # hack to allow generate for models such as GPT2 as is done in `generate()` SCREAMING_SNAKE_CASE_: Optional[Any] = config.eos_token_id return config, input_ids, attention_mask, max_length @is_pt_flax_cross_test def _SCREAMING_SNAKE_CASE ( self : Tuple): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[int] = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Union[str, Any] = False SCREAMING_SNAKE_CASE_: Dict = max_length SCREAMING_SNAKE_CASE_: List[Any] = 0 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: int = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = model_class.__name__[4:] # Skip the "Flax" at the beginning SCREAMING_SNAKE_CASE_: List[Any] = getattr(lowerCAmelCase__ , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = pt_model_class(lowerCAmelCase__).eval() SCREAMING_SNAKE_CASE_: str = load_flax_weights_in_pytorch_model(lowerCAmelCase__ , flax_model.params) SCREAMING_SNAKE_CASE_: List[Any] = flax_model.generate(lowerCAmelCase__).sequences SCREAMING_SNAKE_CASE_: str = pt_model.generate(torch.tensor(lowerCAmelCase__ , dtype=torch.long)) if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]: SCREAMING_SNAKE_CASE_: List[Any] = flax_generation_outputs[:, : pt_generation_outputs.shape[-1]] self.assertListEqual(pt_generation_outputs.numpy().tolist() , flax_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Dict): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Optional[int] = False SCREAMING_SNAKE_CASE_: Optional[int] = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Union[str, Any] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = jit(model.generate) SCREAMING_SNAKE_CASE_: Union[str, Any] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : List[str]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Optional[Any] = True SCREAMING_SNAKE_CASE_: Dict = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Tuple = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = jit(model.generate) SCREAMING_SNAKE_CASE_: Dict = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Any = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: int = False SCREAMING_SNAKE_CASE_: Optional[int] = max_length SCREAMING_SNAKE_CASE_: Optional[int] = 2 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: List[str] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = jit(model.generate) SCREAMING_SNAKE_CASE_: Optional[int] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: str = False SCREAMING_SNAKE_CASE_: int = max_length SCREAMING_SNAKE_CASE_: str = 2 SCREAMING_SNAKE_CASE_: Optional[Any] = 2 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: str = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[0] , input_ids.shape[0] * config.num_return_sequences) def _SCREAMING_SNAKE_CASE ( self : Any): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Tuple = True SCREAMING_SNAKE_CASE_: List[str] = max_length SCREAMING_SNAKE_CASE_: Any = 0.8 SCREAMING_SNAKE_CASE_: Any = 10 SCREAMING_SNAKE_CASE_: List[str] = 0.3 SCREAMING_SNAKE_CASE_: Tuple = 1 SCREAMING_SNAKE_CASE_: Union[str, Any] = 8 SCREAMING_SNAKE_CASE_: int = 9 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: List[str] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = jit(model.generate) SCREAMING_SNAKE_CASE_: List[Any] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Any = max_length SCREAMING_SNAKE_CASE_: int = 1 SCREAMING_SNAKE_CASE_: Union[str, Any] = 8 SCREAMING_SNAKE_CASE_: List[Any] = 9 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: int = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Union[str, Any] = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = jit(model.generate) SCREAMING_SNAKE_CASE_: List[str] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = self._get_input_ids_and_config() SCREAMING_SNAKE_CASE_: Any = max_length SCREAMING_SNAKE_CASE_: List[str] = 2 SCREAMING_SNAKE_CASE_: str = 1 SCREAMING_SNAKE_CASE_: Tuple = 8 SCREAMING_SNAKE_CASE_: List[Any] = 9 for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Optional[int] = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = model.generate(lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: int = jit(model.generate) SCREAMING_SNAKE_CASE_: List[str] = jit_generate(lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : str): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self._get_input_ids_and_config() # pad attention mask on the left SCREAMING_SNAKE_CASE_: Dict = attention_mask.at[(0, 0)].set(0) SCREAMING_SNAKE_CASE_: Dict = False SCREAMING_SNAKE_CASE_: Optional[int] = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Any = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = model.generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = jit(model.generate) SCREAMING_SNAKE_CASE_: List[Any] = jit_generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = self._get_input_ids_and_config() # pad attention mask on the left SCREAMING_SNAKE_CASE_: List[Any] = attention_mask.at[(0, 0)].set(0) SCREAMING_SNAKE_CASE_: Optional[int] = True SCREAMING_SNAKE_CASE_: Union[str, Any] = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: str = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = model.generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = jit(model.generate) SCREAMING_SNAKE_CASE_: Optional[Any] = jit_generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = self._get_input_ids_and_config() # pad attention mask on the left SCREAMING_SNAKE_CASE_: Dict = attention_mask.at[(0, 0)].set(0) SCREAMING_SNAKE_CASE_: Optional[Any] = 2 SCREAMING_SNAKE_CASE_: Any = max_length for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE_: Tuple = model_class(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = model.generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertEqual(generation_outputs.shape[-1] , lowerCAmelCase__) SCREAMING_SNAKE_CASE_: str = jit(model.generate) SCREAMING_SNAKE_CASE_: Union[str, Any] = jit_generate(lowerCAmelCase__ , attention_mask=lowerCAmelCase__).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) @require_flax class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : List[Any]): SCREAMING_SNAKE_CASE_: Tuple = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-bert") SCREAMING_SNAKE_CASE_: List[Any] = FlaxAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-bert-flax-only") SCREAMING_SNAKE_CASE_: Optional[int] = "Hello world" SCREAMING_SNAKE_CASE_: List[Any] = tokenizer(lowerCAmelCase__ , return_tensors="np").input_ids # typos are quickly detected (the correct argument is `do_sample`) with self.assertRaisesRegex(lowerCAmelCase__ , "do_samples"): model.generate(lowerCAmelCase__ , do_samples=lowerCAmelCase__) # arbitrary arguments that will not be used anywhere are also not accepted with self.assertRaisesRegex(lowerCAmelCase__ , "foo"): SCREAMING_SNAKE_CASE_: str = {"foo": "bar"} model.generate(lowerCAmelCase__ , **lowerCAmelCase__)
13
1
import os import pytest from transformers.dynamic_module_utils import get_imports lowerCAmelCase : str = """ import os """ lowerCAmelCase : Optional[Any] = """ def foo(): import os return False """ lowerCAmelCase : str = """ def foo(): def bar(): if True: import os return False return bar() """ lowerCAmelCase : Any = """ import os try: import bar except ImportError: raise ValueError() """ lowerCAmelCase : int = """ import os def foo(): try: import bar except ImportError: raise ValueError() """ lowerCAmelCase : Tuple = """ import os try: import bar except (ImportError, AttributeError): raise ValueError() """ lowerCAmelCase : Optional[Any] = """ import os try: import bar except ImportError as e: raise ValueError() """ lowerCAmelCase : List[str] = """ import os try: import bar except: raise ValueError() """ lowerCAmelCase : List[Any] = """ import os try: import bar import baz except ImportError: raise ValueError() """ lowerCAmelCase : Optional[Any] = """ import os try: import bar import baz except ImportError: x = 1 raise ValueError() """ lowerCAmelCase : Any = [ TOP_LEVEL_IMPORT, IMPORT_IN_FUNCTION, DEEPLY_NESTED_IMPORT, TOP_LEVEL_TRY_IMPORT, GENERIC_EXCEPT_IMPORT, MULTILINE_TRY_IMPORT, MULTILINE_BOTH_IMPORT, MULTIPLE_EXCEPTS_IMPORT, EXCEPT_AS_IMPORT, TRY_IMPORT_IN_FUNCTION, ] @pytest.mark.parametrize("case" , _UpperCAmelCase ) def A_ ( _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Optional[int] = os.path.join(_UpperCAmelCase , "test_file.py" ) with open(_UpperCAmelCase , "w" ) as _tmp_file: _tmp_file.write(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Any = get_imports(_UpperCAmelCase ) assert parsed_imports == ["os"]
13
import argparse import re import torch from CLAP import create_model from transformers import AutoFeatureExtractor, ClapConfig, ClapModel lowerCAmelCase : Union[str, Any] = { """text_branch""": """text_model""", """audio_branch""": """audio_model.audio_encoder""", """attn""": """attention.self""", """self.proj""": """output.dense""", """attention.self_mask""": """attn_mask""", """mlp.fc1""": """intermediate.dense""", """mlp.fc2""": """output.dense""", """norm1""": """layernorm_before""", """norm2""": """layernorm_after""", """bn0""": """batch_norm""", } lowerCAmelCase : int = AutoFeatureExtractor.from_pretrained("""laion/clap-htsat-unfused""", truncation="""rand_trunc""") def A_ ( _UpperCAmelCase , _UpperCAmelCase=False ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = create_model( "HTSAT-tiny" , "roberta" , _UpperCAmelCase , precision="fp32" , device="cuda:0" if torch.cuda.is_available() else "cpu" , enable_fusion=_UpperCAmelCase , fusion_type="aff_2d" if enable_fusion else None , ) return model, model_cfg def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = {} SCREAMING_SNAKE_CASE_: Tuple = R".*sequential.(\d+).*" SCREAMING_SNAKE_CASE_: Dict = R".*_projection.(\d+).*" for key, value in state_dict.items(): # check if any key needs to be modified for key_to_modify, new_key in KEYS_TO_MODIFY_MAPPING.items(): if key_to_modify in key: SCREAMING_SNAKE_CASE_: Any = key.replace(_UpperCAmelCase , _UpperCAmelCase ) if re.match(_UpperCAmelCase , _UpperCAmelCase ): # replace sequential layers with list SCREAMING_SNAKE_CASE_: Optional[int] = re.match(_UpperCAmelCase , _UpperCAmelCase ).group(1 ) SCREAMING_SNAKE_CASE_: Dict = key.replace(f"sequential.{sequential_layer}." , f"layers.{int(_UpperCAmelCase )//3}.linear." ) elif re.match(_UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = int(re.match(_UpperCAmelCase , _UpperCAmelCase ).group(1 ) ) # Because in CLAP they use `nn.Sequential`... SCREAMING_SNAKE_CASE_: Optional[int] = 1 if projecton_layer == 0 else 2 SCREAMING_SNAKE_CASE_: Dict = key.replace(f"_projection.{projecton_layer}." , f"_projection.linear{transformers_projection_layer}." ) if "audio" and "qkv" in key: # split qkv into query key and value SCREAMING_SNAKE_CASE_: Tuple = value SCREAMING_SNAKE_CASE_: List[str] = mixed_qkv.size(0 ) // 3 SCREAMING_SNAKE_CASE_: Any = mixed_qkv[:qkv_dim] SCREAMING_SNAKE_CASE_: Optional[int] = mixed_qkv[qkv_dim : qkv_dim * 2] SCREAMING_SNAKE_CASE_: Optional[Any] = mixed_qkv[qkv_dim * 2 :] SCREAMING_SNAKE_CASE_: str = query_layer SCREAMING_SNAKE_CASE_: int = key_layer SCREAMING_SNAKE_CASE_: List[Any] = value_layer else: SCREAMING_SNAKE_CASE_: int = value return model_state_dict def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=False ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = init_clap(_UpperCAmelCase , enable_fusion=_UpperCAmelCase ) clap_model.eval() SCREAMING_SNAKE_CASE_: Union[str, Any] = clap_model.state_dict() SCREAMING_SNAKE_CASE_: Optional[int] = rename_state_dict(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[str] = ClapConfig() SCREAMING_SNAKE_CASE_: Tuple = enable_fusion SCREAMING_SNAKE_CASE_: Tuple = ClapModel(_UpperCAmelCase ) # ignore the spectrogram embedding layer model.load_state_dict(_UpperCAmelCase , strict=_UpperCAmelCase ) model.save_pretrained(_UpperCAmelCase ) transformers_config.save_pretrained(_UpperCAmelCase ) if __name__ == "__main__": lowerCAmelCase : Tuple = 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("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""") parser.add_argument("""--enable_fusion""", action="""store_true""", help="""Whether to enable fusion or not""") lowerCAmelCase : int = parser.parse_args() convert_clap_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.enable_fusion)
13
1