code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
'''simple docstring''' from __future__ import annotations from typing import Any def snake_case_ ( lowerCAmelCase_ )-> None: '''simple docstring''' create_state_space_tree(lowerCAmelCase_ , [] , 0 ) def snake_case_ ( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ )-> None: '''simple docstring''' if index == len(lowerCAmelCase_ ): print(lowerCAmelCase_ ) return create_state_space_tree(lowerCAmelCase_ , lowerCAmelCase_ , index + 1 ) current_subsequence.append(sequence[index] ) create_state_space_tree(lowerCAmelCase_ , lowerCAmelCase_ , index + 1 ) current_subsequence.pop() if __name__ == "__main__": A_ : list[Any] = [3, 1, 2, 4] generate_all_subsequences(seq) seq.clear() seq.extend(["""A""", """B""", """C"""]) generate_all_subsequences(seq)
215
'''simple docstring''' from numpy import exp, pi, sqrt def snake_case_ ( lowerCAmelCase_ , lowerCAmelCase_ = 0.0 , lowerCAmelCase_ = 1.0 )-> int: '''simple docstring''' return 1 / sqrt(2 * pi * sigma**2 ) * exp(-((x - mu) ** 2) / (2 * sigma**2) ) if __name__ == "__main__": import doctest doctest.testmod()
215
1
from unittest.mock import Mock, patch from file_transfer.send_file import send_file @patch("socket.socket" ) @patch("builtins.open" ) def __lowerCamelCase ( lowerCamelCase__ , lowerCamelCase__ ): """simple docstring""" lowercase__ : List[Any] = Mock() lowercase__ : Union[str, Any] = conn, Mock() lowercase__ : List[str] = iter([1, None] ) lowercase__ : int = lambda lowerCamelCase__ : next(lowerCamelCase__ ) # ===== invoke ===== send_file(filename="mytext.txt" , testing=lowerCamelCase__ ) # ===== ensurance ===== sock.assert_called_once() sock.return_value.bind.assert_called_once() sock.return_value.listen.assert_called_once() sock.return_value.accept.assert_called_once() conn.recv.assert_called_once() file.return_value.__enter__.assert_called_once() file.return_value.__enter__.return_value.read.assert_called() conn.send.assert_called_once() conn.close.assert_called_once() sock.return_value.shutdown.assert_called_once() sock.return_value.close.assert_called_once()
121
def __lowerCamelCase ( lowerCamelCase__ ): """simple docstring""" lowercase__ : Union[str, Any] = [] lowercase__ : Tuple = [] lowercase__ : Any = { "^": 3, "*": 2, "/": 2, "%": 2, "+": 1, "-": 1, } # Priority of each operator lowercase__ : Any = len(lowerCamelCase__ ) if (len(lowerCamelCase__ ) > 7) else 7 # Print table header for output print( "Symbol".center(8 ) , "Stack".center(lowerCamelCase__ ) , "Postfix".center(lowerCamelCase__ ) , sep=" | " , ) print("-" * (print_width * 3 + 7) ) for x in infix: if x.isalpha() or x.isdigit(): post_fix.append(lowerCamelCase__ ) # if x is Alphabet / Digit, add it to Postfix elif x == "(": stack.append(lowerCamelCase__ ) # if x is "(" push to Stack elif x == ")": # if x is ")" pop stack until "(" is encountered while stack[-1] != "(": post_fix.append(stack.pop() ) # Pop stack & add the content to Postfix stack.pop() else: if len(lowerCamelCase__ ) == 0: stack.append(lowerCamelCase__ ) # If stack is empty, push x to stack else: # while priority of x is not > priority of element in the stack while len(lowerCamelCase__ ) > 0 and priority[x] <= priority[stack[-1]]: post_fix.append(stack.pop() ) # pop stack & add to Postfix stack.append(lowerCamelCase__ ) # push x to stack print( x.center(8 ) , ("".join(lowerCamelCase__ )).ljust(lowerCamelCase__ ) , ("".join(lowerCamelCase__ )).ljust(lowerCamelCase__ ) , sep=" | " , ) # Output in tabular format while len(lowerCamelCase__ ) > 0: # while stack is not empty post_fix.append(stack.pop() ) # pop stack & add to Postfix print( " ".center(8 ) , ("".join(lowerCamelCase__ )).ljust(lowerCamelCase__ ) , ("".join(lowerCamelCase__ )).ljust(lowerCamelCase__ ) , sep=" | " , ) # Output in tabular format return "".join(lowerCamelCase__ ) # return Postfix as str def __lowerCamelCase ( lowerCamelCase__ ): """simple docstring""" lowercase__ : Optional[int] = list(infix[::-1] ) # reverse the infix equation for i in range(len(lowerCamelCase__ ) ): if infix[i] == "(": lowercase__ : Tuple = ")" # change "(" to ")" elif infix[i] == ")": lowercase__ : Optional[Any] = "(" # change ")" to "(" return (infix_2_postfix("".join(lowerCamelCase__ ) ))[ ::-1 ] # call infix_2_postfix on Infix, return reverse of Postfix if __name__ == "__main__": lowerCAmelCase__ = input('''\nEnter an Infix Equation = ''') # Input an Infix equation lowerCAmelCase__ = ''''''.join(Infix.split()) # Remove spaces from the input print('''\n\t''', Infix, '''(Infix) -> ''', infix_2_prefix(Infix), '''(Prefix)''')
121
1
"""simple docstring""" def lowerCAmelCase__ ( UpperCamelCase__ = 1_0_0_0_0_0_0 ): '''simple docstring''' _a : Tuple = limit + 1 _a : List[str] = [0] * limit for first_term in range(1 , __a ): for n in range(__a , __a , __a ): _a : Optional[int] = first_term + n / first_term if common_difference % 4: # d must be divisble by 4 continue else: common_difference /= 4 if ( first_term > common_difference and first_term < 4 * common_difference ): # since x,y,z are positive integers frequency[n] += 1 # so z>0 and a>d ,also 4d<a _a : Dict = sum(1 for x in frequency[1:limit] if x == 1_0 ) return count if __name__ == "__main__": print(F'''{solution() = }''')
294
from datetime import datetime import requests from bsa import BeautifulSoup if __name__ == "__main__": A : List[str] = input('''Enter image url: ''').strip() print(F'''Downloading image from {url} ...''') A : Any = BeautifulSoup(requests.get(url).content, '''html.parser''') # The image URL is in the content field of the first meta tag with property og:image A : List[Any] = soup.find('''meta''', {'''property''': '''og:image'''})['''content'''] A : Dict = requests.get(image_url).content A : Tuple = 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}.''')
274
0
"""simple docstring""" import warnings from ...utils import logging from .image_processing_flava import FlavaImageProcessor lowercase : List[Any] = logging.get_logger(__name__) class __snake_case ( lowerCAmelCase ): def __init__( self ,*snake_case ,**snake_case ): '''simple docstring''' warnings.warn( """The class FlavaFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please""" """ use FlavaImageProcessor instead.""" ,snake_case ,) super().__init__(*snake_case ,**snake_case )
367
from __future__ import annotations import numpy as np def _snake_case( SCREAMING_SNAKE_CASE__ ) -> tuple[np.ndarray, np.ndarray]: lowercase , lowercase : Dict = np.shape(SCREAMING_SNAKE_CASE__ ) if rows != columns: lowercase : str = ( """'table' has to be of square shaped array but got a """ f"{rows}x{columns} array:\n{table}" ) raise ValueError(SCREAMING_SNAKE_CASE__ ) lowercase : Any = np.zeros((rows, columns) ) lowercase : int = np.zeros((rows, columns) ) for i in range(SCREAMING_SNAKE_CASE__ ): for j in range(SCREAMING_SNAKE_CASE__ ): lowercase : Optional[int] = sum(lower[i][k] * upper[k][j] for k in range(SCREAMING_SNAKE_CASE__ ) ) if upper[j][j] == 0: raise ArithmeticError("""No LU decomposition exists""" ) lowercase : str = (table[i][j] - total) / upper[j][j] lowercase : Optional[Any] = 1 for j in range(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): lowercase : Any = sum(lower[i][k] * upper[k][j] for k in range(SCREAMING_SNAKE_CASE__ ) ) lowercase : Tuple = table[i][j] - total return lower, upper if __name__ == "__main__": import doctest doctest.testmod()
285
0
from __future__ import annotations def lowerCamelCase__ ( _a , _a , _a): if days_between_payments <= 0: raise ValueError("days_between_payments must be > 0") if daily_interest_rate < 0: raise ValueError("daily_interest_rate must be >= 0") if principal <= 0: raise ValueError("principal must be > 0") return principal * daily_interest_rate * days_between_payments def lowerCamelCase__ ( _a , _a , _a , ): if number_of_compounding_periods <= 0: raise ValueError("number_of_compounding_periods must be > 0") if nominal_annual_interest_rate_percentage < 0: raise ValueError("nominal_annual_interest_rate_percentage must be >= 0") if principal <= 0: raise ValueError("principal must be > 0") return principal * ( (1 + nominal_annual_interest_rate_percentage) ** number_of_compounding_periods - 1 ) def lowerCamelCase__ ( _a , _a , _a , ): if number_of_years <= 0: raise ValueError("number_of_years must be > 0") if nominal_annual_percentage_rate < 0: raise ValueError("nominal_annual_percentage_rate must be >= 0") if principal <= 0: raise ValueError("principal must be > 0") return compound_interest( _a , nominal_annual_percentage_rate / 365 , number_of_years * 365) if __name__ == "__main__": import doctest doctest.testmod()
76
import json import os import shutil import tempfile import unittest import numpy as np from transformers import BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer from transformers.testing_utils import require_tokenizers, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor @require_tokenizers @require_vision class UpperCAmelCase ( unittest.TestCase ): def _SCREAMING_SNAKE_CASE (self : Any ) -> List[str]: '''simple docstring''' snake_case : int = tempfile.mkdtemp() # fmt: off snake_case : Optional[int] = ["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "want", "##want", "##ed", "wa", "un", "runn", "##ing", ",", "low", "lowest"] # fmt: on snake_case : List[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] ) ) snake_case : int = { "do_resize": True, "size": {"height": 18, "width": 18}, "do_normalize": True, "image_mean": [0.5, 0.5, 0.5], "image_std": [0.5, 0.5, 0.5], } snake_case : Optional[Any] = os.path.join(self.tmpdirname , snake_case__ ) with open(self.image_processor_file , "w" , encoding="utf-8" ) as fp: json.dump(snake_case__ , snake_case__ ) def _SCREAMING_SNAKE_CASE (self : Optional[Any] , **snake_case__ : str ) -> Optional[int]: '''simple docstring''' return BertTokenizer.from_pretrained(self.tmpdirname , **snake_case__ ) def _SCREAMING_SNAKE_CASE (self : Optional[Any] , **snake_case__ : List[str] ) -> int: '''simple docstring''' return ViTImageProcessor.from_pretrained(self.tmpdirname , **snake_case__ ) def _SCREAMING_SNAKE_CASE (self : Optional[int] ) -> Dict: '''simple docstring''' shutil.rmtree(self.tmpdirname ) def _SCREAMING_SNAKE_CASE (self : Dict ) -> str: '''simple docstring''' snake_case : List[Any] = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] snake_case : Optional[int] = [Image.fromarray(np.moveaxis(snake_case__ , 0 , -1 ) ) for x in image_inputs] return image_inputs def _SCREAMING_SNAKE_CASE (self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' snake_case : Dict = self.get_tokenizer() snake_case : Optional[Any] = self.get_image_processor() snake_case : Optional[Any] = VisionTextDualEncoderProcessor(tokenizer=snake_case__ , image_processor=snake_case__ ) processor.save_pretrained(self.tmpdirname ) snake_case : Any = VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.tokenizer , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor , snake_case__ ) def _SCREAMING_SNAKE_CASE (self : Any ) -> Optional[Any]: '''simple docstring''' snake_case : str = VisionTextDualEncoderProcessor( tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) snake_case : Optional[int] = self.get_tokenizer(bos_token="(BOS)" , eos_token="(EOS)" ) snake_case : Tuple = self.get_image_processor(do_normalize=snake_case__ , padding_value=1.0 ) snake_case : List[str] = VisionTextDualEncoderProcessor.from_pretrained( self.tmpdirname , bos_token="(BOS)" , eos_token="(EOS)" , do_normalize=snake_case__ , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , snake_case__ ) def _SCREAMING_SNAKE_CASE (self : Union[str, Any] ) -> int: '''simple docstring''' snake_case : str = self.get_image_processor() snake_case : Optional[int] = self.get_tokenizer() snake_case : List[Any] = VisionTextDualEncoderProcessor(tokenizer=snake_case__ , image_processor=snake_case__ ) snake_case : Optional[Any] = self.prepare_image_inputs() snake_case : str = image_processor(snake_case__ , return_tensors="np" ) snake_case : Any = processor(images=snake_case__ , 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 ) -> Optional[Any]: '''simple docstring''' snake_case : Dict = self.get_image_processor() snake_case : int = self.get_tokenizer() snake_case : Any = VisionTextDualEncoderProcessor(tokenizer=snake_case__ , image_processor=snake_case__ ) snake_case : Tuple = "lower newer" snake_case : Tuple = processor(text=snake_case__ ) snake_case : Union[str, Any] = tokenizer(snake_case__ ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def _SCREAMING_SNAKE_CASE (self : Dict ) -> Optional[int]: '''simple docstring''' snake_case : List[Any] = self.get_image_processor() snake_case : Dict = self.get_tokenizer() snake_case : Dict = VisionTextDualEncoderProcessor(tokenizer=snake_case__ , image_processor=snake_case__ ) snake_case : int = "lower newer" snake_case : Dict = self.prepare_image_inputs() snake_case : Union[str, Any] = processor(text=snake_case__ , images=snake_case__ ) self.assertListEqual(list(inputs.keys() ) , ["input_ids", "token_type_ids", "attention_mask", "pixel_values"] ) # test if it raises when no input is passed with self.assertRaises(snake_case__ ): processor() def _SCREAMING_SNAKE_CASE (self : str ) -> Tuple: '''simple docstring''' snake_case : Tuple = self.get_image_processor() snake_case : Optional[Any] = self.get_tokenizer() snake_case : Tuple = VisionTextDualEncoderProcessor(tokenizer=snake_case__ , image_processor=snake_case__ ) snake_case : List[str] = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] snake_case : List[Any] = processor.batch_decode(snake_case__ ) snake_case : Union[str, Any] = tokenizer.batch_decode(snake_case__ ) self.assertListEqual(snake_case__ , snake_case__ ) def _SCREAMING_SNAKE_CASE (self : Optional[int] ) -> List[str]: '''simple docstring''' snake_case : str = self.get_image_processor() snake_case : Union[str, Any] = self.get_tokenizer() snake_case : Any = VisionTextDualEncoderProcessor(tokenizer=snake_case__ , image_processor=snake_case__ ) snake_case : Optional[Any] = "lower newer" snake_case : List[Any] = self.prepare_image_inputs() snake_case : Tuple = processor(text=snake_case__ , images=snake_case__ ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
59
0
"""simple docstring""" 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_ = Mapping[str, np.ndarray] lowerCAmelCase_ = Mapping[str, Any] # Is a nested dict. lowerCAmelCase_ = 0.0_1 @dataclasses.dataclass(frozen=A_ ) class __A : '''simple docstring''' lowerCAmelCase : 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'. lowerCAmelCase : 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. lowerCAmelCase : np.ndarray # [num_res, num_atom_type] # Residue index as used in PDB. It is not necessarily continuous or 0-indexed. lowerCAmelCase : 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. lowerCAmelCase : np.ndarray # [num_res, num_atom_type] # Chain indices for multi-chain predictions lowerCAmelCase : Optional[np.ndarray] = None # Optional remark about the protein. Included as a comment in output PDB # files lowerCAmelCase : Optional[str] = None # Templates used to generate this protein (prediction-only) lowerCAmelCase : Optional[Sequence[str]] = None # Chain corresponding to each parent lowerCAmelCase : Optional[Sequence[int]] = None def __UpperCAmelCase ( __lowerCamelCase ) -> Protein: lowercase__ : Tuple = r'''(\[[A-Z]+\]\n)''' lowercase__ : List[str] = [tag.strip() for tag in re.split(__lowerCamelCase , __lowerCamelCase ) if len(__lowerCamelCase ) > 0] lowercase__ : Iterator[Tuple[str, List[str]]] = zip(tags[0::2] , [l.split('''\n''' ) for l in tags[1::2]] ) lowercase__ : List[str] = ["N", "CA", "C"] lowercase__ : Dict = None lowercase__ : Tuple = None lowercase__ : Optional[int] = None for g in groups: if "[PRIMARY]" == g[0]: lowercase__ : Union[str, Any] = g[1][0].strip() for i in range(len(__lowerCamelCase ) ): if seq[i] not in residue_constants.restypes: lowercase__ : str = '''X''' # FIXME: strings are immutable lowercase__ : List[str] = np.array( [residue_constants.restype_order.get(__lowerCamelCase , residue_constants.restype_num ) for res_symbol in seq] ) elif "[TERTIARY]" == g[0]: lowercase__ : List[List[float]] = [] for axis in range(3 ): tertiary.append(list(map(__lowerCamelCase , g[1][axis].split() ) ) ) lowercase__ : Dict = np.array(__lowerCamelCase ) lowercase__ : Dict = np.zeros((len(tertiary[0] ) // 3, residue_constants.atom_type_num, 3) ).astype(np.floataa ) for i, atom in enumerate(__lowerCamelCase ): lowercase__ : List[Any] = np.transpose(tertiary_np[:, i::3] ) atom_positions *= PICO_TO_ANGSTROM elif "[MASK]" == g[0]: lowercase__ : Optional[int] = np.array(list(map({'''-''': 0, '''+''': 1}.get , g[1][0].strip() ) ) ) lowercase__ : Optional[int] = np.zeros( ( len(__lowerCamelCase ), residue_constants.atom_type_num, ) ).astype(np.floataa ) for i, atom in enumerate(__lowerCamelCase ): lowercase__ : List[str] = 1 atom_mask *= mask[..., None] assert aatype is not None return Protein( atom_positions=__lowerCamelCase , atom_mask=__lowerCamelCase , aatype=__lowerCamelCase , residue_index=np.arange(len(__lowerCamelCase ) ) , b_factors=__lowerCamelCase , ) def __UpperCAmelCase ( __lowerCamelCase , __lowerCamelCase = 0 ) -> List[str]: lowercase__ : List[str] = [] lowercase__ : str = prot.remark if remark is not None: pdb_headers.append(f"""REMARK {remark}""" ) lowercase__ : Optional[int] = prot.parents lowercase__ : int = prot.parents_chain_index if parents is not None and parents_chain_index is not None: lowercase__ : List[Any] = [p for i, p in zip(__lowerCamelCase , __lowerCamelCase ) if i == chain_id] if parents is None or len(__lowerCamelCase ) == 0: lowercase__ : List[str] = ['''N/A'''] pdb_headers.append(f"""PARENT {" ".join(__lowerCamelCase )}""" ) return pdb_headers def __UpperCAmelCase ( __lowerCamelCase , __lowerCamelCase ) -> str: lowercase__ : List[str] = [] lowercase__ : str = pdb_str.split('''\n''' ) lowercase__ : List[str] = prot.remark if remark is not None: out_pdb_lines.append(f"""REMARK {remark}""" ) lowercase__ : List[List[str]] if prot.parents is not None and len(prot.parents ) > 0: lowercase__ : List[Any] = [] if prot.parents_chain_index is not None: lowercase__ : Dict[str, List[str]] = {} for p, i in zip(prot.parents , prot.parents_chain_index ): parent_dict.setdefault(str(__lowerCamelCase ) , [] ) parent_dict[str(__lowerCamelCase )].append(__lowerCamelCase ) lowercase__ : Optional[int] = max([int(__lowerCamelCase ) for chain_idx in parent_dict] ) for i in range(max_idx + 1 ): lowercase__ : int = parent_dict.get(str(__lowerCamelCase ) , ['''N/A'''] ) parents_per_chain.append(__lowerCamelCase ) else: parents_per_chain.append(list(prot.parents ) ) else: lowercase__ : str = [['''N/A''']] def make_parent_line(__lowerCamelCase ) -> str: return f"""PARENT {" ".join(__lowerCamelCase )}""" out_pdb_lines.append(make_parent_line(parents_per_chain[0] ) ) lowercase__ : Union[str, Any] = 0 for i, l in enumerate(__lowerCamelCase ): if "PARENT" not in l and "REMARK" not in l: out_pdb_lines.append(__lowerCamelCase ) if "TER" in l and "END" not in lines[i + 1]: chain_counter += 1 if not chain_counter >= len(__lowerCamelCase ): lowercase__ : List[str] = parents_per_chain[chain_counter] else: lowercase__ : List[str] = ['''N/A'''] out_pdb_lines.append(make_parent_line(__lowerCamelCase ) ) return "\n".join(__lowerCamelCase ) def __UpperCAmelCase ( __lowerCamelCase ) -> str: lowercase__ : List[str] = residue_constants.restypes + ['''X'''] def res_atoa(__lowerCamelCase ) -> str: return residue_constants.restype_atoa.get(restypes[r] , '''UNK''' ) lowercase__ : Tuple = residue_constants.atom_types lowercase__ : List[str] = [] lowercase__ : Dict = prot.atom_mask lowercase__ : str = prot.aatype lowercase__ : Tuple = prot.atom_positions lowercase__ : int = prot.residue_index.astype(np.intaa ) lowercase__ : Dict = prot.b_factors lowercase__ : Optional[int] = prot.chain_index if np.any(aatype > residue_constants.restype_num ): raise ValueError('''Invalid aatypes.''' ) lowercase__ : str = get_pdb_headers(__lowerCamelCase ) if len(__lowerCamelCase ) > 0: pdb_lines.extend(__lowerCamelCase ) lowercase__ : Union[str, Any] = aatype.shape[0] lowercase__ : Dict = 1 lowercase__ : Optional[int] = 0 lowercase__ : str = string.ascii_uppercase lowercase__ : int = None # Add all atom sites. for i in range(__lowerCamelCase ): lowercase__ : List[str] = res_atoa(aatype[i] ) for atom_name, pos, mask, b_factor in zip(__lowerCamelCase , atom_positions[i] , atom_mask[i] , b_factors[i] ): if mask < 0.5: continue lowercase__ : List[str] = '''ATOM''' lowercase__ : List[Any] = atom_name if len(__lowerCamelCase ) == 4 else f""" {atom_name}""" lowercase__ : List[Any] = '''''' lowercase__ : Dict = '''''' lowercase__ : str = 1.0_0 lowercase__ : Optional[int] = atom_name[0] # Protein supports only C, N, O, S, this works. lowercase__ : Any = '''''' lowercase__ : str = '''A''' if chain_index is not None: lowercase__ : List[str] = chain_tags[chain_index[i]] # PDB is a columnar format, every space matters here! lowercase__ : 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(__lowerCamelCase ) atom_index += 1 lowercase__ : Optional[Any] = i == n - 1 if chain_index is not None: if i != n - 1 and chain_index[i + 1] != prev_chain_index: lowercase__ : int = True lowercase__ : str = chain_index[i + 1] if should_terminate: # Close the chain. lowercase__ : Optional[Any] = '''TER''' lowercase__ : str = ( f"""{chain_end:<6}{atom_index:>5} {res_atoa(aatype[i] ):>3} {chain_tag:>1}{residue_index[i]:>4}""" ) pdb_lines.append(__lowerCamelCase ) 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(__lowerCamelCase , __lowerCamelCase ) ) pdb_lines.append('''END''' ) pdb_lines.append('''''' ) return "\n".join(__lowerCamelCase ) def __UpperCAmelCase ( __lowerCamelCase ) -> np.ndarray: return residue_constants.STANDARD_ATOM_MASK[prot.aatype] def __UpperCAmelCase ( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = None , __lowerCamelCase = None , __lowerCamelCase = None , __lowerCamelCase = None , __lowerCamelCase = None , ) -> Protein: 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=__lowerCamelCase , remark=__lowerCamelCase , parents=__lowerCamelCase , parents_chain_index=__lowerCamelCase , )
302
"""simple docstring""" import unittest from transformers import AutoTokenizer, is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, slow if is_flax_available(): import jax.numpy as jnp from transformers import FlaxXLMRobertaModel @require_sentencepiece @require_tokenizers @require_flax class __A ( unittest.TestCase ): '''simple docstring''' @slow def UpperCAmelCase ( self : List[str] ) -> Any: """simple docstring""" lowercase__ : List[str] = FlaxXLMRobertaModel.from_pretrained('''xlm-roberta-base''' ) lowercase__ : List[str] = AutoTokenizer.from_pretrained('''xlm-roberta-base''' ) lowercase__ : List[str] = '''The dog is cute and lives in the garden house''' lowercase__ : int = jnp.array([tokenizer.encode(_snake_case )] ) lowercase__ : Any = (1, 12, 768) # batch_size, sequence_length, embedding_vector_dim lowercase__ : Tuple = jnp.array( [[-0.0101, 0.1218, -0.0803, 0.0801, 0.1327, 0.0776, -0.1215, 0.2383, 0.3338, 0.3106, 0.0300, 0.0252]] ) lowercase__ : Optional[Any] = model(_snake_case )['''last_hidden_state'''] self.assertEqual(output.shape ,_snake_case ) # compare the actual values for a slice of last dim self.assertTrue(jnp.allclose(output[:, :, -1] ,_snake_case ,atol=1e-3 ) )
302
1
'''simple docstring''' import unittest import numpy as np from transformers import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING, TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING from transformers.pipelines import AudioClassificationPipeline, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_torchaudio, slow, ) from .test_pipelines_common import ANY @is_pipeline_test class lowerCAmelCase_( unittest.TestCase ): '''simple docstring''' __lowercase : List[Any] = MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING __lowercase : Tuple = TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING def UpperCAmelCase_ ( self ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ) -> Optional[Any]: lowerCAmelCase__ : str = AudioClassificationPipeline(model=__UpperCAmelCase ,feature_extractor=__UpperCAmelCase ) # test with a raw waveform lowerCAmelCase__ : Union[str, Any] = np.zeros((3_4000,) ) lowerCAmelCase__ : Any = np.zeros((1_4000,) ) return audio_classifier, [audioa, audio] def UpperCAmelCase_ ( self ,__UpperCAmelCase ,__UpperCAmelCase ) -> Optional[int]: lowerCAmelCase__ : str = examples lowerCAmelCase__ : str = audio_classifier(__UpperCAmelCase ) # by default a model is initialized with num_labels=2 self.assertEqual( __UpperCAmelCase ,[ {"""score""": ANY(__UpperCAmelCase ), """label""": ANY(__UpperCAmelCase )}, {"""score""": ANY(__UpperCAmelCase ), """label""": ANY(__UpperCAmelCase )}, ] ,) lowerCAmelCase__ : Optional[int] = audio_classifier(__UpperCAmelCase ,top_k=1 ) self.assertEqual( __UpperCAmelCase ,[ {"""score""": ANY(__UpperCAmelCase ), """label""": ANY(__UpperCAmelCase )}, ] ,) self.run_torchaudio(__UpperCAmelCase ) @require_torchaudio def UpperCAmelCase_ ( self ,__UpperCAmelCase ) -> Any: import datasets # test with a local file lowerCAmelCase__ : List[Any] = datasets.load_dataset("""hf-internal-testing/librispeech_asr_dummy""" ,"""clean""" ,split="""validation""" ) lowerCAmelCase__ : Union[str, Any] = dataset[0]["audio"]["array"] lowerCAmelCase__ : List[Any] = audio_classifier(__UpperCAmelCase ) self.assertEqual( __UpperCAmelCase ,[ {"""score""": ANY(__UpperCAmelCase ), """label""": ANY(__UpperCAmelCase )}, {"""score""": ANY(__UpperCAmelCase ), """label""": ANY(__UpperCAmelCase )}, ] ,) @require_torch def UpperCAmelCase_ ( self ) -> int: lowerCAmelCase__ : Tuple = "anton-l/wav2vec2-random-tiny-classifier" lowerCAmelCase__ : Union[str, Any] = pipeline("""audio-classification""" ,model=__UpperCAmelCase ) lowerCAmelCase__ : Dict = np.ones((8000,) ) lowerCAmelCase__ : Any = audio_classifier(__UpperCAmelCase ,top_k=4 ) lowerCAmelCase__ : Optional[Any] = [ {"score": 0.0_8_4_2, "label": "no"}, {"score": 0.0_8_3_8, "label": "up"}, {"score": 0.0_8_3_7, "label": "go"}, {"score": 0.0_8_3_4, "label": "right"}, ] lowerCAmelCase__ : Union[str, Any] = [ {"score": 0.0_8_4_5, "label": "stop"}, {"score": 0.0_8_4_4, "label": "on"}, {"score": 0.0_8_4_1, "label": "right"}, {"score": 0.0_8_3_4, "label": "left"}, ] self.assertIn(nested_simplify(__UpperCAmelCase ,decimals=4 ) ,[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2] ) lowerCAmelCase__ : Optional[int] = {"array": np.ones((8000,) ), "sampling_rate": audio_classifier.feature_extractor.sampling_rate} lowerCAmelCase__ : str = audio_classifier(__UpperCAmelCase ,top_k=4 ) self.assertIn(nested_simplify(__UpperCAmelCase ,decimals=4 ) ,[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2] ) @require_torch @slow def UpperCAmelCase_ ( self ) -> Optional[int]: import datasets lowerCAmelCase__ : Union[str, Any] = "superb/wav2vec2-base-superb-ks" lowerCAmelCase__ : List[str] = pipeline("""audio-classification""" ,model=__UpperCAmelCase ) lowerCAmelCase__ : List[str] = datasets.load_dataset("""anton-l/superb_dummy""" ,"""ks""" ,split="""test""" ) lowerCAmelCase__ : Optional[Any] = np.array(dataset[3]["""speech"""] ,dtype=np.floataa ) lowerCAmelCase__ : int = audio_classifier(__UpperCAmelCase ,top_k=4 ) self.assertEqual( nested_simplify(__UpperCAmelCase ,decimals=3 ) ,[ {"""score""": 0.9_8_1, """label""": """go"""}, {"""score""": 0.0_0_7, """label""": """up"""}, {"""score""": 0.0_0_6, """label""": """_unknown_"""}, {"""score""": 0.0_0_1, """label""": """down"""}, ] ,) @require_tf @unittest.skip("""Audio classification is not implemented for TF""" ) def UpperCAmelCase_ ( self ) -> Union[str, Any]: pass
37
import numpy as np # Importing the Keras libraries and packages import tensorflow as tf from tensorflow.keras import layers, models if __name__ == "__main__": # Initialising the CNN # (Sequential- Building the model layer by layer) _snake_case : Any = models.Sequential() # Step 1 - Convolution # Here 64,64 is the length & breadth of dataset images and 3 is for the RGB channel # (3,3) is the kernel size (filter matrix) classifier.add( layers.ConvaD(32, (3, 3), input_shape=(64, 64, 3), activation="relu") ) # Step 2 - Pooling classifier.add(layers.MaxPoolingaD(pool_size=(2, 2))) # Adding a second convolutional layer classifier.add(layers.ConvaD(32, (3, 3), activation="relu")) classifier.add(layers.MaxPoolingaD(pool_size=(2, 2))) # Step 3 - Flattening classifier.add(layers.Flatten()) # Step 4 - Full connection classifier.add(layers.Dense(units=128, activation="relu")) classifier.add(layers.Dense(units=1, activation="sigmoid")) # Compiling the CNN classifier.compile( optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"] ) # Part 2 - Fitting the CNN to the images # Load Trained model weights # from keras.models import load_model # regressor=load_model('cnn.h5') _snake_case : int = tf.keras.preprocessing.image.ImageDataGenerator( rescale=1.0 / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True ) _snake_case : Optional[Any] = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1.0 / 255) _snake_case : List[str] = train_datagen.flow_from_directory( "dataset/training_set", target_size=(64, 64), batch_size=32, class_mode="binary" ) _snake_case : Any = test_datagen.flow_from_directory( "dataset/test_set", target_size=(64, 64), batch_size=32, class_mode="binary" ) classifier.fit_generator( training_set, steps_per_epoch=5, epochs=30, validation_data=test_set ) classifier.save("cnn.h5") # Part 3 - Making new predictions _snake_case : Optional[Any] = tf.keras.preprocessing.image.load_img( "dataset/single_prediction/image.png", target_size=(64, 64) ) _snake_case : int = tf.keras.preprocessing.image.img_to_array(test_image) _snake_case : Tuple = np.expand_dims(test_image, axis=0) _snake_case : Any = classifier.predict(test_image) # training_set.class_indices if result[0][0] == 0: _snake_case : Any = "Normal" if result[0][0] == 1: _snake_case : List[str] = "Abnormality detected"
123
0
"""simple docstring""" import logging from dataclasses import dataclass, field from typing import Optional from seqaseq_trainer import arg_to_scheduler from transformers import TrainingArguments A_ = logging.getLogger(__name__) @dataclass class __SCREAMING_SNAKE_CASE ( UpperCamelCase ): snake_case_ = field( default=0.0 , metadata={'help': 'The label smoothing epsilon to apply (if not zero).'} ) snake_case_ = field(default=UpperCamelCase , metadata={'help': 'Whether to SortishSamler or not.'} ) snake_case_ = field( default=UpperCamelCase , metadata={'help': 'Whether to use generate to calculate generative metrics (ROUGE, BLEU).'} ) snake_case_ = field(default=UpperCamelCase , metadata={'help': 'whether to use adafactor'} ) snake_case_ = field( default=UpperCamelCase , metadata={'help': 'Encoder layer dropout probability. Goes into model.config.'} ) snake_case_ = field( default=UpperCamelCase , metadata={'help': 'Decoder layer dropout probability. Goes into model.config.'} ) snake_case_ = field(default=UpperCamelCase , metadata={'help': 'Dropout probability. Goes into model.config.'} ) snake_case_ = field( default=UpperCamelCase , metadata={'help': 'Attention dropout probability. Goes into model.config.'} ) snake_case_ = field( default='linear' , metadata={'help': F"Which lr scheduler to use. Selected in {sorted(arg_to_scheduler.keys() )}"} , )
296
"""simple docstring""" import argparse import json from pathlib import Path import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import DeiTConfig, DeiTForImageClassificationWithTeacher, DeiTImageProcessor from transformers.utils import logging logging.set_verbosity_info() A_ = logging.get_logger(__name__) def _lowerCAmelCase ( UpperCAmelCase__ : Union[str, Any], UpperCAmelCase__ : Tuple=False ) ->str: A__ : Optional[int] = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((f'blocks.{i}.norm1.weight', f'deit.encoder.layer.{i}.layernorm_before.weight') ) rename_keys.append((f'blocks.{i}.norm1.bias', f'deit.encoder.layer.{i}.layernorm_before.bias') ) rename_keys.append((f'blocks.{i}.attn.proj.weight', f'deit.encoder.layer.{i}.attention.output.dense.weight') ) rename_keys.append((f'blocks.{i}.attn.proj.bias', f'deit.encoder.layer.{i}.attention.output.dense.bias') ) rename_keys.append((f'blocks.{i}.norm2.weight', f'deit.encoder.layer.{i}.layernorm_after.weight') ) rename_keys.append((f'blocks.{i}.norm2.bias', f'deit.encoder.layer.{i}.layernorm_after.bias') ) rename_keys.append((f'blocks.{i}.mlp.fc1.weight', f'deit.encoder.layer.{i}.intermediate.dense.weight') ) rename_keys.append((f'blocks.{i}.mlp.fc1.bias', f'deit.encoder.layer.{i}.intermediate.dense.bias') ) rename_keys.append((f'blocks.{i}.mlp.fc2.weight', f'deit.encoder.layer.{i}.output.dense.weight') ) rename_keys.append((f'blocks.{i}.mlp.fc2.bias', f'deit.encoder.layer.{i}.output.dense.bias') ) # projection layer + position embeddings rename_keys.extend( [ ("""cls_token""", """deit.embeddings.cls_token"""), ("""dist_token""", """deit.embeddings.distillation_token"""), ("""patch_embed.proj.weight""", """deit.embeddings.patch_embeddings.projection.weight"""), ("""patch_embed.proj.bias""", """deit.embeddings.patch_embeddings.projection.bias"""), ("""pos_embed""", """deit.embeddings.position_embeddings"""), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ("""norm.weight""", """layernorm.weight"""), ("""norm.bias""", """layernorm.bias"""), ("""pre_logits.fc.weight""", """pooler.dense.weight"""), ("""pre_logits.fc.bias""", """pooler.dense.bias"""), ] ) # if just the base model, we should remove "deit" from all keys that start with "deit" A__ : Optional[int] = [(pair[0], pair[1][4:]) if pair[1].startswith("""deit""" ) else pair for pair in rename_keys] else: # layernorm + classification heads rename_keys.extend( [ ("""norm.weight""", """deit.layernorm.weight"""), ("""norm.bias""", """deit.layernorm.bias"""), ("""head.weight""", """cls_classifier.weight"""), ("""head.bias""", """cls_classifier.bias"""), ("""head_dist.weight""", """distillation_classifier.weight"""), ("""head_dist.bias""", """distillation_classifier.bias"""), ] ) return rename_keys def _lowerCAmelCase ( UpperCAmelCase__ : Optional[int], UpperCAmelCase__ : Tuple, UpperCAmelCase__ : List[Any]=False ) ->str: for i in range(config.num_hidden_layers ): if base_model: A__ : Any = """""" else: A__ : Tuple = """deit.""" # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) A__ : Any = state_dict.pop(f'blocks.{i}.attn.qkv.weight' ) A__ : Tuple = state_dict.pop(f'blocks.{i}.attn.qkv.bias' ) # next, add query, keys and values (in that order) to the state dict A__ : List[Any] = in_proj_weight[ : config.hidden_size, : ] A__ : str = in_proj_bias[: config.hidden_size] A__ : Any = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] A__ : Dict = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] A__ : Optional[Any] = in_proj_weight[ -config.hidden_size :, : ] A__ : Any = in_proj_bias[-config.hidden_size :] def _lowerCAmelCase ( UpperCAmelCase__ : List[Any], UpperCAmelCase__ : List[Any], UpperCAmelCase__ : Union[str, Any] ) ->Any: A__ : int = dct.pop(UpperCAmelCase__ ) A__ : Tuple = val def _lowerCAmelCase ( ) ->List[Any]: A__ : Optional[int] = """http://images.cocodataset.org/val2017/000000039769.jpg""" A__ : int = Image.open(requests.get(UpperCAmelCase__, stream=UpperCAmelCase__ ).raw ) return im @torch.no_grad() def _lowerCAmelCase ( UpperCAmelCase__ : Dict, UpperCAmelCase__ : Any ) ->Tuple: A__ : List[Any] = DeiTConfig() # all deit models have fine-tuned heads A__ : Tuple = False # dataset (fine-tuned on ImageNet 2012), patch_size and image_size A__ : str = 1_0_0_0 A__ : List[str] = """huggingface/label-files""" A__ : Dict = """imagenet-1k-id2label.json""" A__ : List[str] = json.load(open(hf_hub_download(UpperCAmelCase__, UpperCAmelCase__, repo_type="""dataset""" ), """r""" ) ) A__ : Dict = {int(UpperCAmelCase__ ): v for k, v in idalabel.items()} A__ : Optional[int] = idalabel A__ : Dict = {v: k for k, v in idalabel.items()} A__ : List[str] = int(deit_name[-6:-4] ) A__ : str = int(deit_name[-3:] ) # size of the architecture if deit_name[9:].startswith("""tiny""" ): A__ : List[str] = 1_9_2 A__ : int = 7_6_8 A__ : List[Any] = 1_2 A__ : Dict = 3 elif deit_name[9:].startswith("""small""" ): A__ : List[Any] = 3_8_4 A__ : List[str] = 1_5_3_6 A__ : Any = 1_2 A__ : Union[str, Any] = 6 if deit_name[9:].startswith("""base""" ): pass elif deit_name[4:].startswith("""large""" ): A__ : int = 1_0_2_4 A__ : str = 4_0_9_6 A__ : Any = 2_4 A__ : int = 1_6 # load original model from timm A__ : Dict = timm.create_model(UpperCAmelCase__, pretrained=UpperCAmelCase__ ) timm_model.eval() # load state_dict of original model, remove and rename some keys A__ : Tuple = timm_model.state_dict() A__ : str = create_rename_keys(UpperCAmelCase__, UpperCAmelCase__ ) for src, dest in rename_keys: rename_key(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) read_in_q_k_v(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) # load HuggingFace model A__ : str = DeiTForImageClassificationWithTeacher(UpperCAmelCase__ ).eval() model.load_state_dict(UpperCAmelCase__ ) # Check outputs on an image, prepared by DeiTImageProcessor A__ : int = int( (2_5_6 / 2_2_4) * config.image_size ) # to maintain same ratio w.r.t. 224 images, see https://github.com/facebookresearch/deit/blob/ab5715372db8c6cad5740714b2216d55aeae052e/datasets.py#L103 A__ : Any = DeiTImageProcessor(size=UpperCAmelCase__, crop_size=config.image_size ) A__ : Union[str, Any] = image_processor(images=prepare_img(), return_tensors="""pt""" ) A__ : Optional[Any] = encoding["""pixel_values"""] A__ : Union[str, Any] = model(UpperCAmelCase__ ) A__ : Union[str, Any] = timm_model(UpperCAmelCase__ ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(UpperCAmelCase__, outputs.logits, atol=1e-3 ) Path(UpperCAmelCase__ ).mkdir(exist_ok=UpperCAmelCase__ ) print(f'Saving model {deit_name} to {pytorch_dump_folder_path}' ) model.save_pretrained(UpperCAmelCase__ ) print(f'Saving image processor to {pytorch_dump_folder_path}' ) image_processor.save_pretrained(UpperCAmelCase__ ) if __name__ == "__main__": A_ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--deit_name''', default='''vit_deit_base_distilled_patch16_224''', type=str, help='''Name of the DeiT timm model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) A_ = parser.parse_args() convert_deit_checkpoint(args.deit_name, args.pytorch_dump_folder_path)
296
1
"""simple docstring""" from math import factorial def lowercase ( a__ : int = 100 ) -> int: return sum(int(a__ ) for x in str(factorial(a__ ) ) ) if __name__ == "__main__": print(solution(int(input("""Enter the Number: """).strip())))
256
"""simple docstring""" from typing import List, Optional, Union import numpy as np from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging UpperCAmelCase = logging.get_logger(__name__) class UpperCAmelCase_ ( _lowercase): snake_case__ = ['''input_values''', '''padding_mask'''] def __init__( self : Optional[Any] , __UpperCamelCase : int = 1 , __UpperCamelCase : int = 2_4000 , __UpperCamelCase : float = 0.0 , __UpperCamelCase : float = None , __UpperCamelCase : float = None , **__UpperCamelCase : Optional[Any] , ) -> Optional[int]: super().__init__(feature_size=__UpperCamelCase , sampling_rate=__UpperCamelCase , padding_value=__UpperCamelCase , **__UpperCamelCase ) _UpperCamelCase = chunk_length_s _UpperCamelCase = overlap @property def _UpperCamelCase ( self : Optional[int] ) -> Optional[int]: if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def _UpperCamelCase ( self : Union[str, Any] ) -> Optional[int]: if self.chunk_length_s is None or self.overlap is None: return None else: return max(1 , int((1.0 - self.overlap) * self.chunk_length ) ) def __call__( self : Union[str, Any] , __UpperCamelCase : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , __UpperCamelCase : Optional[Union[bool, str, PaddingStrategy]] = None , __UpperCamelCase : Optional[bool] = False , __UpperCamelCase : Optional[int] = None , __UpperCamelCase : Optional[Union[str, TensorType]] = None , __UpperCamelCase : Optional[int] = None , ) -> BatchFeature: if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' F''' {self.sampling_rate}. Please make sure that the provided audio input was sampled with''' F''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( '''It is strongly recommended to pass the `sampling_rate` argument to this function. ''' '''Failing to do so can result in silent errors that might be hard to debug.''' ) if padding and truncation: raise ValueError('''Both padding and truncation were set. Make sure you only set one.''' ) elif padding is None: # by default let's pad the inputs _UpperCamelCase = True _UpperCamelCase = bool( isinstance(__UpperCamelCase , (list, tuple) ) and (isinstance(raw_audio[0] , (np.ndarray, tuple, list) )) ) if is_batched: _UpperCamelCase = [np.asarray(__UpperCamelCase , dtype=np.floataa ).T for audio in raw_audio] elif not is_batched and not isinstance(__UpperCamelCase , np.ndarray ): _UpperCamelCase = np.asarray(__UpperCamelCase , dtype=np.floataa ) elif isinstance(__UpperCamelCase , np.ndarray ) and raw_audio.dtype is np.dtype(np.floataa ): _UpperCamelCase = raw_audio.astype(np.floataa ) # always return batch if not is_batched: _UpperCamelCase = [np.asarray(__UpperCamelCase ).T] # verify inputs are valid for idx, example in enumerate(__UpperCamelCase ): if example.ndim > 2: raise ValueError(F'''Expected input shape (channels, length) but got shape {example.shape}''' ) if self.feature_size == 1 and example.ndim != 1: raise ValueError(F'''Expected mono audio but example has {example.shape[-1]} channels''' ) if self.feature_size == 2 and example.shape[-1] != 2: raise ValueError(F'''Expected stereo audio but example has {example.shape[-1]} channels''' ) _UpperCamelCase = None _UpperCamelCase = BatchFeature({'''input_values''': raw_audio} ) if self.chunk_stride is not None and self.chunk_length is not None and max_length is None: if truncation: _UpperCamelCase = min(array.shape[0] for array in raw_audio ) _UpperCamelCase = int(np.floor(max_length / self.chunk_stride ) ) _UpperCamelCase = (nb_step - 1) * self.chunk_stride + self.chunk_length elif padding: _UpperCamelCase = max(array.shape[0] for array in raw_audio ) _UpperCamelCase = int(np.ceil(max_length / self.chunk_stride ) ) _UpperCamelCase = (nb_step - 1) * self.chunk_stride + self.chunk_length _UpperCamelCase = '''max_length''' else: _UpperCamelCase = input_values # normal padding on batch if padded_inputs is None: _UpperCamelCase = self.pad( __UpperCamelCase , max_length=__UpperCamelCase , truncation=__UpperCamelCase , padding=__UpperCamelCase , return_attention_mask=__UpperCamelCase , ) if padding: _UpperCamelCase = padded_inputs.pop('''attention_mask''' ) _UpperCamelCase = [] for example in padded_inputs.pop('''input_values''' ): if self.feature_size == 1: _UpperCamelCase = example[..., None] input_values.append(example.T ) _UpperCamelCase = input_values if return_tensors is not None: _UpperCamelCase = padded_inputs.convert_to_tensors(__UpperCamelCase ) return padded_inputs
256
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __UpperCamelCase = { 'configuration_rembert': ['REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'RemBertConfig', 'RemBertOnnxConfig'] } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase = ['RemBertTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase = ['RemBertTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase = [ 'REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'RemBertForCausalLM', 'RemBertForMaskedLM', 'RemBertForMultipleChoice', 'RemBertForQuestionAnswering', 'RemBertForSequenceClassification', 'RemBertForTokenClassification', 'RemBertLayer', 'RemBertModel', 'RemBertPreTrainedModel', 'load_tf_weights_in_rembert', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase = [ 'TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFRemBertForCausalLM', 'TFRemBertForMaskedLM', 'TFRemBertForMultipleChoice', 'TFRemBertForQuestionAnswering', 'TFRemBertForSequenceClassification', 'TFRemBertForTokenClassification', 'TFRemBertLayer', 'TFRemBertModel', 'TFRemBertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_rembert import REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RemBertConfig, RemBertOnnxConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_rembert import RemBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_rembert_fast import RemBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_rembert import ( REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST, RemBertForCausalLM, RemBertForMaskedLM, RemBertForMultipleChoice, RemBertForQuestionAnswering, RemBertForSequenceClassification, RemBertForTokenClassification, RemBertLayer, RemBertModel, RemBertPreTrainedModel, load_tf_weights_in_rembert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_rembert import ( TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFRemBertForCausalLM, TFRemBertForMaskedLM, TFRemBertForMultipleChoice, TFRemBertForQuestionAnswering, TFRemBertForSequenceClassification, TFRemBertForTokenClassification, TFRemBertLayer, TFRemBertModel, TFRemBertPreTrainedModel, ) else: import sys __UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
355
"""simple docstring""" from collections.abc import Callable from math import pi, sqrt from random import uniform from statistics import mean def lowercase (SCREAMING_SNAKE_CASE_ : int ) -> List[str]: # A local function to see if a dot lands in the circle. def is_in_circle(SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : float ) -> bool: SCREAMING_SNAKE_CASE = sqrt((x**2) + (y**2) ) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle SCREAMING_SNAKE_CASE = mean( int(is_in_circle(uniform(-1.0 , 1.0 ) , uniform(-1.0 , 1.0 ) ) ) for _ in range(SCREAMING_SNAKE_CASE_ ) ) # The ratio of the area for circle to square is pi/4. SCREAMING_SNAKE_CASE = proportion * 4 print(F'The estimated value of pi is {pi_estimate}' ) print(F'The numpy value of pi is {pi}' ) print(F'The total error is {abs(pi - pi_estimate )}' ) def lowercase (SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Callable[[float], float] , SCREAMING_SNAKE_CASE_ : float = 0.0 , SCREAMING_SNAKE_CASE_ : float = 1.0 , ) -> float: return mean( function_to_integrate(uniform(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) for _ in range(SCREAMING_SNAKE_CASE_ ) ) * (max_value - min_value) def lowercase (SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : float = 0.0 , SCREAMING_SNAKE_CASE_ : float = 1.0 ) -> None: def identity_function(SCREAMING_SNAKE_CASE_ : float ) -> float: return x SCREAMING_SNAKE_CASE = area_under_curve_estimator( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) SCREAMING_SNAKE_CASE = (max_value * max_value - min_value * min_value) / 2 print('******************' ) print(F'Estimating area under y=x where x varies from {min_value} to {max_value}' ) print(F'Estimated value is {estimated_value}' ) print(F'Expected value is {expected_value}' ) print(F'Total error is {abs(estimated_value - expected_value )}' ) print('******************' ) def lowercase (SCREAMING_SNAKE_CASE_ : int ) -> None: def function_to_integrate(SCREAMING_SNAKE_CASE_ : float ) -> float: return sqrt(4.0 - x * x ) SCREAMING_SNAKE_CASE = area_under_curve_estimator( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , 0.0 , 2.0 ) print('******************' ) print('Estimating pi using area_under_curve_estimator' ) print(F'Estimated value is {estimated_value}' ) print(F'Expected value is {pi}' ) print(F'Total error is {abs(estimated_value - pi )}' ) print('******************' ) if __name__ == "__main__": import doctest doctest.testmod()
38
0
"""simple docstring""" from PIL import Image def _lowerCAmelCase ( UpperCamelCase_ , UpperCamelCase_ ): __SCREAMING_SNAKE_CASE = (259 * (level + 255)) / (255 * (259 - level)) def contrast(UpperCamelCase_ ) -> int: return int(128 + factor * (c - 128) ) return img.point(UpperCamelCase__ ) if __name__ == "__main__": # Load image with Image.open("image_data/lena.jpg") as img: # Change contrast to 170 __magic_name__ = change_contrast(img, 170) cont_img.save("image_data/lena_high_contrast.png", format="png")
100
"""simple docstring""" import argparse import torch from ...utils import logging from . import AlbertConfig, AlbertForPreTraining, load_tf_weights_in_albert logging.set_verbosity_info() def UpperCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): """simple docstring""" A__ = AlbertConfig.from_json_file(UpperCamelCase__ ) print(F'''Building PyTorch model from configuration: {config}''' ) A__ = AlbertForPreTraining(UpperCamelCase__ ) # Load weights from tf checkpoint load_tf_weights_in_albert(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) # Save pytorch-model print(F'''Save PyTorch model to {pytorch_dump_path}''' ) torch.save(model.state_dict() , UpperCamelCase__ ) if __name__ == "__main__": __lowerCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path." ) parser.add_argument( "--albert_config_file", default=None, type=str, required=True, help=( "The config json file corresponding to the pre-trained ALBERT model. \n" "This specifies the model architecture." ), ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) __lowerCamelCase = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.albert_config_file, args.pytorch_dump_path)
221
0
"""simple docstring""" def lowerCAmelCase_( lowercase_ : int = 10**9 ) -> int: _lowerCamelCase = 1 _lowerCamelCase = 2 _lowerCamelCase = 0 _lowerCamelCase = 0 _lowerCamelCase = 0 while perimeter <= max_perimeter: perimeters_sum += perimeter prev_value += 2 * value value += prev_value _lowerCamelCase = 2 * value + 2 if i % 2 == 0 else 2 * value - 2 i += 1 return perimeters_sum if __name__ == "__main__": print(F"""{solution() = }""")
73
"""simple docstring""" import os from collections.abc import Iterator def lowerCAmelCase_( lowercase_ : str = "." ) -> Iterator[str]: for dir_path, dir_names, filenames in os.walk(lowercase_ ): _lowerCamelCase = [d for d in dir_names if d != '''scripts''' and d[0] not in '''._'''] for filename in filenames: if filename == "__init__.py": continue if os.path.splitext(lowercase_ )[1] in (".py", ".ipynb"): yield os.path.join(lowercase_ , lowercase_ ).lstrip('''./''' ) def lowerCAmelCase_( lowercase_ : Dict ) -> List[Any]: return F"""{i * " "}*""" if i else "\n##" def lowerCAmelCase_( lowercase_ : str , lowercase_ : str ) -> str: _lowerCamelCase = old_path.split(os.sep ) for i, new_part in enumerate(new_path.split(os.sep ) ): if (i + 1 > len(lowercase_ ) or old_parts[i] != new_part) and new_part: print(F"""{md_prefix(lowercase_ )} {new_part.replace("_" , " " ).title()}""" ) return new_path def lowerCAmelCase_( lowercase_ : str = "." ) -> None: _lowerCamelCase = '''''' for filepath in sorted(good_file_paths(lowercase_ ) ): _lowerCamelCase , _lowerCamelCase = os.path.split(lowercase_ ) if filepath != old_path: _lowerCamelCase = print_path(lowercase_ , lowercase_ ) _lowerCamelCase = (filepath.count(os.sep ) + 1) if filepath else 0 _lowerCamelCase = F"""{filepath}/{filename}""".replace(''' ''' , '''%20''' ) _lowerCamelCase = os.path.splitext(filename.replace('''_''' , ''' ''' ).title() )[0] print(F"""{md_prefix(lowercase_ )} [{filename}]({url})""" ) if __name__ == "__main__": print_directory_md('''.''')
73
1
'''simple docstring''' from typing import List, Optional, Union import numpy as np import tensorflow as tf from .utils import logging UpperCAmelCase : Optional[Any] = logging.get_logger(__name__) def a__ ( a__ ): """simple docstring""" if isinstance(a__ , np.ndarray ): return list(tensor.shape ) __SCREAMING_SNAKE_CASE = tf.shape(a__ ) if tensor.shape == tf.TensorShape(a__ ): return dynamic __SCREAMING_SNAKE_CASE = tensor.shape.as_list() return [dynamic[i] if s is None else s for i, s in enumerate(a__ )] def a__ ( a__ , a__ = None , a__ = None ): """simple docstring""" return tf.nn.softmax(logits=logits + 1E-9 , axis=a__ , name=a__ ) def a__ ( a__ , a__ , a__ , a__=1E-5 , a__=-1 ): """simple docstring""" if weight.shape.rank != 1 or bias.shape.rank != 1 or not isinstance(a__ , a__ ): raise NotImplementedError("""Only 1D weight and bias tensors are supported for now, with only a single axis.""" ) # Get mean and variance on the axis to be normalized __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = tf.nn.moments(a__ , axes=[axis] , keepdims=a__ ) if axis != -1: # Reshape scale and weight to have the same rank as inputs, but with 1 dimensions # on every dimension except axis __SCREAMING_SNAKE_CASE = [1] * inputs.shape.rank __SCREAMING_SNAKE_CASE = shape_list(a__ )[axis] __SCREAMING_SNAKE_CASE = tf.reshape(a__ , a__ ) __SCREAMING_SNAKE_CASE = tf.reshape(a__ , a__ ) # Compute layer normalization using the batch_normalization # function. __SCREAMING_SNAKE_CASE = tf.nn.batch_normalization( a__ , a__ , a__ , offset=a__ , scale=a__ , variance_epsilon=a__ , ) return outputs def a__ ( a__ , a__=0 , a__=-1 ): """simple docstring""" if end_dim < 0: end_dim += input.shape.rank if start_dim < 0: start_dim += input.shape.rank if start_dim == end_dim: return input __SCREAMING_SNAKE_CASE = tf.shape(a__ ) __SCREAMING_SNAKE_CASE = tf.math.reduce_prod(in_shape[start_dim : end_dim + 1] ) __SCREAMING_SNAKE_CASE = tf.concat([in_shape[:start_dim], [flattened_dim], in_shape[end_dim + 1 :]] , axis=0 ) return tf.reshape(a__ , a__ ) def a__ ( a__ ): """simple docstring""" if not isinstance(a__ , tf.Tensor ): __SCREAMING_SNAKE_CASE = tf.convert_to_tensor(a__ ) # Catches stray NumPy inputs if encoder_attention_mask.shape.rank == 3: __SCREAMING_SNAKE_CASE = encoder_attention_mask[:, None, :, :] if encoder_attention_mask.shape.rank == 2: __SCREAMING_SNAKE_CASE = encoder_attention_mask[:, None, None, :] # T5 has a mask that can compare sequence ids, we can simulate this here with this transposition # Cf. https://github.com/tensorflow/mesh/blob/8d2465e9bc93129b913b5ccc6a59aa97abd96ec6/mesh_tensorflow # /transformer/transformer_layers.py#L270 # encoder_extended_attention_mask = (encoder_extended_attention_mask == # encoder_extended_attention_mask.transpose(-1, -2)) __SCREAMING_SNAKE_CASE = ( tf.cast(1 , encoder_attention_mask.dtype ) - encoder_extended_attention_mask ) * encoder_extended_attention_mask.dtype.min return encoder_extended_attention_mask def a__ ( a__ , a__ , a__ = "input_ids" ): """simple docstring""" tf.debugging.assert_less( a__ , tf.cast(a__ , dtype=tensor.dtype ) , message=( F'The maximum value of {tensor_name} ({tf.math.reduce_max(a__ )}) must be smaller than the embedding ' F'layer\'s input dimension ({embed_dim}). The likely cause is some problem at tokenization time.' ) , ) def a__ ( a__ , a__ , a__ ): """simple docstring""" __SCREAMING_SNAKE_CASE = 6_45_12 # Check that no item in `data` is larger than `HDF5_OBJECT_HEADER_LIMIT` # because in that case even chunking the array would not make the saving # possible. __SCREAMING_SNAKE_CASE = [x for x in data if len(a__ ) > HDF5_OBJECT_HEADER_LIMIT] # Expecting this to never be true. if bad_attributes: raise RuntimeError( """The following attributes cannot be saved to HDF5 file because """ F'they are larger than {HDF5_OBJECT_HEADER_LIMIT} ' F'bytes: {bad_attributes}' ) __SCREAMING_SNAKE_CASE = np.asarray(a__ ) __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = np.array_split(a__ , a__ ) # This will never loop forever thanks to the test above. while any(x.nbytes > HDF5_OBJECT_HEADER_LIMIT for x in chunked_data ): num_chunks += 1 __SCREAMING_SNAKE_CASE = np.array_split(a__ , a__ ) if num_chunks > 1: for chunk_id, chunk_data in enumerate(a__ ): __SCREAMING_SNAKE_CASE = chunk_data else: __SCREAMING_SNAKE_CASE = data def a__ ( a__ , a__ ): """simple docstring""" if name in group.attrs: __SCREAMING_SNAKE_CASE = [n.decode("""utf8""" ) if hasattr(a__ , """decode""" ) else n for n in group.attrs[name]] else: __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = 0 while "%s%d" % (name, chunk_id) in group.attrs: data.extend( [n.decode("""utf8""" ) if hasattr(a__ , """decode""" ) else n for n in group.attrs["""%s%d""" % (name, chunk_id)]] ) chunk_id += 1 return data def a__ ( a__ ): """simple docstring""" def _expand_single_ad_tensor(a__ ): if isinstance(a__ , tf.Tensor ) and t.shape.rank == 1: return tf.expand_dims(a__ , axis=-1 ) return t return tf.nest.map_structure(_expand_single_ad_tensor , a__ )
267
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCAmelCase : Union[str, Any] = {'configuration_sew': ['SEW_PRETRAINED_CONFIG_ARCHIVE_MAP', 'SEWConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase : Union[str, Any] = [ 'SEW_PRETRAINED_MODEL_ARCHIVE_LIST', 'SEWForCTC', 'SEWForSequenceClassification', 'SEWModel', 'SEWPreTrainedModel', ] if TYPE_CHECKING: from .configuration_sew import SEW_PRETRAINED_CONFIG_ARCHIVE_MAP, SEWConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_sew import ( SEW_PRETRAINED_MODEL_ARCHIVE_LIST, SEWForCTC, SEWForSequenceClassification, SEWModel, SEWPreTrainedModel, ) else: import sys UpperCAmelCase : int = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
267
1
import numpy as np from nltk.translate import meteor_score import datasets from datasets.config import importlib_metadata, version UpperCAmelCase_ : Dict = version.parse(importlib_metadata.version("nltk")) if NLTK_VERSION >= version.Version("3.6.4"): from nltk import word_tokenize UpperCAmelCase_ : List[Any] = "\\n@inproceedings{banarjee2005,\n title = {{METEOR}: An Automatic Metric for {MT} Evaluation with Improved Correlation with Human Judgments},\n author = {Banerjee, Satanjeev and Lavie, Alon},\n booktitle = {Proceedings of the {ACL} Workshop on Intrinsic and Extrinsic Evaluation Measures for Machine Translation and/or Summarization},\n month = jun,\n year = {2005},\n address = {Ann Arbor, Michigan},\n publisher = {Association for Computational Linguistics},\n url = {https://www.aclweb.org/anthology/W05-0909},\n pages = {65--72},\n}\n" UpperCAmelCase_ : Any = "\\nMETEOR, an automatic metric for machine translation evaluation\nthat is based on a generalized concept of unigram matching between the\nmachine-produced translation and human-produced reference translations.\nUnigrams can be matched based on their surface forms, stemmed forms,\nand meanings; furthermore, METEOR can be easily extended to include more\nadvanced matching strategies. Once all generalized unigram matches\nbetween the two strings have been found, METEOR computes a score for\nthis matching using a combination of unigram-precision, unigram-recall, and\na measure of fragmentation that is designed to directly capture how\nwell-ordered the matched words in the machine translation are in relation\nto the reference.\n\nMETEOR gets an R correlation value of 0.347 with human evaluation on the Arabic\ndata and 0.331 on the Chinese data. This is shown to be an improvement on\nusing simply unigram-precision, unigram-recall and their harmonic F1\ncombination.\n" UpperCAmelCase_ : int = "\nComputes METEOR score of translated segments against one or more references.\nArgs:\n predictions: list of predictions to score. Each prediction\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\n alpha: Parameter for controlling relative weights of precision and recall. default: 0.9\n beta: Parameter for controlling shape of penalty as a function of fragmentation. default: 3\n gamma: Relative weight assigned to fragmentation penalty. default: 0.5\nReturns:\n 'meteor': meteor score.\nExamples:\n\n >>> meteor = datasets.load_metric('meteor')\n >>> predictions = [\"It is a guide to action which ensures that the military always obeys the commands of the party\"]\n >>> references = [\"It is a guide to action that ensures that the military will forever heed Party commands\"]\n >>> results = meteor.compute(predictions=predictions, references=references)\n >>> print(round(results[\"meteor\"], 4))\n 0.6944\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCamelCase ( datasets.Metric ): def __A ( self ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Value("string" , id="sequence" ), } ) , codebase_urls=["https://github.com/nltk/nltk/blob/develop/nltk/translate/meteor_score.py"] , reference_urls=[ "https://www.nltk.org/api/nltk.translate.html#module-nltk.translate.meteor_score", "https://en.wikipedia.org/wiki/METEOR", ] , ) def __A ( self , UpperCAmelCase__ ): import nltk nltk.download("wordnet" ) if NLTK_VERSION >= version.Version("3.6.5" ): nltk.download("punkt" ) if NLTK_VERSION >= version.Version("3.6.6" ): nltk.download("omw-1.4" ) def __A ( self , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__=0.9 , UpperCAmelCase__=3 , UpperCAmelCase__=0.5 ): if NLTK_VERSION >= version.Version("3.6.5" ): A__ = [ meteor_score.single_meteor_score( word_tokenize(UpperCAmelCase__ ) , word_tokenize(UpperCAmelCase__ ) , alpha=UpperCAmelCase__ , beta=UpperCAmelCase__ , gamma=UpperCAmelCase__ ) for ref, pred in zip(UpperCAmelCase__ , UpperCAmelCase__ ) ] else: A__ = [ meteor_score.single_meteor_score(UpperCAmelCase__ , UpperCAmelCase__ , alpha=UpperCAmelCase__ , beta=UpperCAmelCase__ , gamma=UpperCAmelCase__ ) for ref, pred in zip(UpperCAmelCase__ , UpperCAmelCase__ ) ] return {"meteor": np.mean(UpperCAmelCase__ )}
366
def UpperCamelCase ( _A : str )-> str: """simple docstring""" A__ = "" for ch in key: if ch == " " or ch not in key_no_dups and ch.isalpha(): key_no_dups += ch return key_no_dups def UpperCamelCase ( _A : str )-> dict[str, str]: """simple docstring""" A__ = [chr(i + 65 ) for i in range(26 )] # Remove duplicate characters from key A__ = remove_duplicates(key.upper() ) A__ = len(_A ) # First fill cipher with key characters A__ = {alphabet[i]: char for i, char in enumerate(_A )} # Then map remaining characters in alphabet to # the alphabet from the beginning for i in range(len(_A ) , 26 ): A__ = alphabet[i - offset] # Ensure we are not mapping letters to letters previously mapped while char in key: offset -= 1 A__ = alphabet[i - offset] A__ = char return cipher_alphabet def UpperCamelCase ( _A : str , _A : dict[str, str] )-> str: """simple docstring""" return "".join(cipher_map.get(_A , _A ) for ch in message.upper() ) def UpperCamelCase ( _A : str , _A : dict[str, str] )-> str: """simple docstring""" A__ = {v: k for k, v in cipher_map.items()} return "".join(rev_cipher_map.get(_A , _A ) for ch in message.upper() ) def UpperCamelCase ( )-> None: """simple docstring""" A__ = input("Enter message to encode or decode: " ).strip() A__ = input("Enter keyword: " ).strip() A__ = input("Encipher or decipher? E/D:" ).strip()[0].lower() try: A__ = {"e": encipher, "d": decipher}[option] except KeyError: raise KeyError("invalid input option" ) A__ = create_cipher_map(_A ) print(func(_A , _A ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
198
0
"""simple docstring""" from typing import Optional, Union import torch from torch import nn from ...configuration_utils import ConfigMixin, register_to_config from ...models.modeling_utils import ModelMixin class UpperCamelCase__ ( lowercase_, lowercase_ ): """simple docstring""" @register_to_config def __init__( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : str = 7_6_8 , ): super().__init__() lowerCAmelCase_ : Any = nn.Parameter(torch.zeros(1 , __UpperCamelCase ) ) lowerCAmelCase_ : Tuple = nn.Parameter(torch.ones(1 , __UpperCamelCase ) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : Union[str, Any] = None , ): lowerCAmelCase_ : Optional[Any] = nn.Parameter(self.mean.to(__UpperCamelCase ).to(__UpperCamelCase ) ) lowerCAmelCase_ : Optional[Any] = nn.Parameter(self.std.to(__UpperCamelCase ).to(__UpperCamelCase ) ) return self def SCREAMING_SNAKE_CASE__ ( self : int , SCREAMING_SNAKE_CASE_ : Optional[Any] ): lowerCAmelCase_ : Optional[int] = (embeds - self.mean) * 1.0 / self.std return embeds def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : str ): lowerCAmelCase_ : Optional[int] = (embeds * self.std) + self.mean return embeds
224
"""simple docstring""" import argparse import os import torch from diffusers import ( CMStochasticIterativeScheduler, ConsistencyModelPipeline, UNetaDModel, ) __SCREAMING_SNAKE_CASE ={ "sample_size": 32, "in_channels": 3, "out_channels": 3, "layers_per_block": 2, "num_class_embeds": 1000, "block_out_channels": [32, 64], "attention_head_dim": 8, "down_block_types": [ "ResnetDownsampleBlock2D", "AttnDownBlock2D", ], "up_block_types": [ "AttnUpBlock2D", "ResnetUpsampleBlock2D", ], "resnet_time_scale_shift": "scale_shift", "upsample_type": "resnet", "downsample_type": "resnet", } __SCREAMING_SNAKE_CASE ={ "sample_size": 64, "in_channels": 3, "out_channels": 3, "layers_per_block": 3, "num_class_embeds": 1000, "block_out_channels": [192, 192 * 2, 192 * 3, 192 * 4], "attention_head_dim": 64, "down_block_types": [ "ResnetDownsampleBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", ], "up_block_types": [ "AttnUpBlock2D", "AttnUpBlock2D", "AttnUpBlock2D", "ResnetUpsampleBlock2D", ], "resnet_time_scale_shift": "scale_shift", "upsample_type": "resnet", "downsample_type": "resnet", } __SCREAMING_SNAKE_CASE ={ "sample_size": 256, "in_channels": 3, "out_channels": 3, "layers_per_block": 2, "num_class_embeds": None, "block_out_channels": [256, 256, 256 * 2, 256 * 2, 256 * 4, 256 * 4], "attention_head_dim": 64, "down_block_types": [ "ResnetDownsampleBlock2D", "ResnetDownsampleBlock2D", "ResnetDownsampleBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", ], "up_block_types": [ "AttnUpBlock2D", "AttnUpBlock2D", "AttnUpBlock2D", "ResnetUpsampleBlock2D", "ResnetUpsampleBlock2D", "ResnetUpsampleBlock2D", ], "resnet_time_scale_shift": "default", "upsample_type": "resnet", "downsample_type": "resnet", } __SCREAMING_SNAKE_CASE ={ "num_train_timesteps": 40, "sigma_min": 0.0_02, "sigma_max": 80.0, } __SCREAMING_SNAKE_CASE ={ "num_train_timesteps": 201, "sigma_min": 0.0_02, "sigma_max": 80.0, } __SCREAMING_SNAKE_CASE ={ "num_train_timesteps": 151, "sigma_min": 0.0_02, "sigma_max": 80.0, } def lowercase__( __SCREAMING_SNAKE_CASE : Optional[int] ): if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): return v if v.lower() in ("yes", "true", "t", "y", "1"): return True elif v.lower() in ("no", "false", "f", "n", "0"): return False else: raise argparse.ArgumentTypeError('boolean value expected' ) def lowercase__( __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : str=False ): lowercase_ : str = checkpoint[F'''{old_prefix}.in_layers.0.weight'''] lowercase_ : Any = checkpoint[F'''{old_prefix}.in_layers.0.bias'''] lowercase_ : str = checkpoint[F'''{old_prefix}.in_layers.2.weight'''] lowercase_ : str = checkpoint[F'''{old_prefix}.in_layers.2.bias'''] lowercase_ : Optional[Any] = checkpoint[F'''{old_prefix}.emb_layers.1.weight'''] lowercase_ : Any = checkpoint[F'''{old_prefix}.emb_layers.1.bias'''] lowercase_ : Tuple = checkpoint[F'''{old_prefix}.out_layers.0.weight'''] lowercase_ : Dict = checkpoint[F'''{old_prefix}.out_layers.0.bias'''] lowercase_ : Any = checkpoint[F'''{old_prefix}.out_layers.3.weight'''] lowercase_ : Dict = checkpoint[F'''{old_prefix}.out_layers.3.bias'''] if has_skip: lowercase_ : Optional[Any] = checkpoint[F'''{old_prefix}.skip_connection.weight'''] lowercase_ : Tuple = checkpoint[F'''{old_prefix}.skip_connection.bias'''] return new_checkpoint def lowercase__( __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : str=None ): lowercase_ , lowercase_ , lowercase_ : Optional[int] = checkpoint[F'''{old_prefix}.qkv.weight'''].chunk(3 , dim=0 ) lowercase_ , lowercase_ , lowercase_ : Dict = checkpoint[F'''{old_prefix}.qkv.bias'''].chunk(3 , dim=0 ) lowercase_ : Any = checkpoint[F'''{old_prefix}.norm.weight'''] lowercase_ : Tuple = checkpoint[F'''{old_prefix}.norm.bias'''] lowercase_ : List[str] = weight_q.squeeze(-1 ).squeeze(-1 ) lowercase_ : Tuple = bias_q.squeeze(-1 ).squeeze(-1 ) lowercase_ : Optional[Any] = weight_k.squeeze(-1 ).squeeze(-1 ) lowercase_ : int = bias_k.squeeze(-1 ).squeeze(-1 ) lowercase_ : Optional[int] = weight_v.squeeze(-1 ).squeeze(-1 ) lowercase_ : Tuple = bias_v.squeeze(-1 ).squeeze(-1 ) lowercase_ : List[str] = ( checkpoint[F'''{old_prefix}.proj_out.weight'''].squeeze(-1 ).squeeze(-1 ) ) lowercase_ : int = checkpoint[F'''{old_prefix}.proj_out.bias'''].squeeze(-1 ).squeeze(-1 ) return new_checkpoint def lowercase__( __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Tuple ): lowercase_ : Any = torch.load(__SCREAMING_SNAKE_CASE , map_location='cpu' ) lowercase_ : Any = {} lowercase_ : Dict = checkpoint['time_embed.0.weight'] lowercase_ : Optional[Any] = checkpoint['time_embed.0.bias'] lowercase_ : List[str] = checkpoint['time_embed.2.weight'] lowercase_ : Any = checkpoint['time_embed.2.bias'] if unet_config["num_class_embeds"] is not None: lowercase_ : str = checkpoint['label_emb.weight'] lowercase_ : Optional[Any] = checkpoint['input_blocks.0.0.weight'] lowercase_ : Optional[int] = checkpoint['input_blocks.0.0.bias'] lowercase_ : Dict = unet_config['down_block_types'] lowercase_ : int = unet_config['layers_per_block'] lowercase_ : int = unet_config['attention_head_dim'] lowercase_ : Any = unet_config['block_out_channels'] lowercase_ : Optional[Any] = 1 lowercase_ : Tuple = channels_list[0] for i, layer_type in enumerate(__SCREAMING_SNAKE_CASE ): lowercase_ : Tuple = channels_list[i] lowercase_ : Any = current_channels != prev_channels if layer_type == "ResnetDownsampleBlock2D": for j in range(__SCREAMING_SNAKE_CASE ): lowercase_ : List[str] = F'''down_blocks.{i}.resnets.{j}''' lowercase_ : int = F'''input_blocks.{current_layer}.0''' lowercase_ : List[str] = True if j == 0 and downsample_block_has_skip else False lowercase_ : str = convert_resnet(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , has_skip=__SCREAMING_SNAKE_CASE ) current_layer += 1 elif layer_type == "AttnDownBlock2D": for j in range(__SCREAMING_SNAKE_CASE ): lowercase_ : Optional[int] = F'''down_blocks.{i}.resnets.{j}''' lowercase_ : Dict = F'''input_blocks.{current_layer}.0''' lowercase_ : Optional[Any] = True if j == 0 and downsample_block_has_skip else False lowercase_ : Union[str, Any] = convert_resnet(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , has_skip=__SCREAMING_SNAKE_CASE ) lowercase_ : str = F'''down_blocks.{i}.attentions.{j}''' lowercase_ : Union[str, Any] = F'''input_blocks.{current_layer}.1''' lowercase_ : int = convert_attention( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) current_layer += 1 if i != len(__SCREAMING_SNAKE_CASE ) - 1: lowercase_ : Tuple = F'''down_blocks.{i}.downsamplers.0''' lowercase_ : str = F'''input_blocks.{current_layer}.0''' lowercase_ : Tuple = convert_resnet(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) current_layer += 1 lowercase_ : int = current_channels # hardcoded the mid-block for now lowercase_ : Any = 'mid_block.resnets.0' lowercase_ : Optional[Any] = 'middle_block.0' lowercase_ : Optional[int] = convert_resnet(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowercase_ : List[str] = 'mid_block.attentions.0' lowercase_ : Optional[Any] = 'middle_block.1' lowercase_ : Dict = convert_attention(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowercase_ : Any = 'mid_block.resnets.1' lowercase_ : str = 'middle_block.2' lowercase_ : Dict = convert_resnet(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowercase_ : Union[str, Any] = 0 lowercase_ : Union[str, Any] = unet_config['up_block_types'] for i, layer_type in enumerate(__SCREAMING_SNAKE_CASE ): if layer_type == "ResnetUpsampleBlock2D": for j in range(layers_per_block + 1 ): lowercase_ : Any = F'''up_blocks.{i}.resnets.{j}''' lowercase_ : Any = F'''output_blocks.{current_layer}.0''' lowercase_ : Union[str, Any] = convert_resnet(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , has_skip=__SCREAMING_SNAKE_CASE ) current_layer += 1 if i != len(__SCREAMING_SNAKE_CASE ) - 1: lowercase_ : Optional[Any] = F'''up_blocks.{i}.upsamplers.0''' lowercase_ : Optional[int] = F'''output_blocks.{current_layer-1}.1''' lowercase_ : Tuple = convert_resnet(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) elif layer_type == "AttnUpBlock2D": for j in range(layers_per_block + 1 ): lowercase_ : Tuple = F'''up_blocks.{i}.resnets.{j}''' lowercase_ : List[Any] = F'''output_blocks.{current_layer}.0''' lowercase_ : Union[str, Any] = convert_resnet(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , has_skip=__SCREAMING_SNAKE_CASE ) lowercase_ : Tuple = F'''up_blocks.{i}.attentions.{j}''' lowercase_ : Dict = F'''output_blocks.{current_layer}.1''' lowercase_ : int = convert_attention( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) current_layer += 1 if i != len(__SCREAMING_SNAKE_CASE ) - 1: lowercase_ : Any = F'''up_blocks.{i}.upsamplers.0''' lowercase_ : str = F'''output_blocks.{current_layer-1}.2''' lowercase_ : int = convert_resnet(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowercase_ : List[str] = checkpoint['out.0.weight'] lowercase_ : Dict = checkpoint['out.0.bias'] lowercase_ : Optional[int] = checkpoint['out.2.weight'] lowercase_ : Optional[int] = checkpoint['out.2.bias'] return new_checkpoint if __name__ == "__main__": __SCREAMING_SNAKE_CASE =argparse.ArgumentParser() parser.add_argument("--unet_path", default=None, type=str, required=True, help="Path to the unet.pt to convert.") parser.add_argument( "--dump_path", default=None, type=str, required=True, help="Path to output the converted UNet model." ) parser.add_argument("--class_cond", default=True, type=str, help="Whether the model is class-conditional.") __SCREAMING_SNAKE_CASE =parser.parse_args() __SCREAMING_SNAKE_CASE =strabool(args.class_cond) __SCREAMING_SNAKE_CASE =os.path.basename(args.unet_path) print(F"Checkpoint: {ckpt_name}") # Get U-Net config if "imagenet64" in ckpt_name: __SCREAMING_SNAKE_CASE =IMAGENET_64_UNET_CONFIG elif "256" in ckpt_name and (("bedroom" in ckpt_name) or ("cat" in ckpt_name)): __SCREAMING_SNAKE_CASE =LSUN_256_UNET_CONFIG elif "test" in ckpt_name: __SCREAMING_SNAKE_CASE =TEST_UNET_CONFIG else: raise ValueError(F"Checkpoint type {ckpt_name} is not currently supported.") if not args.class_cond: __SCREAMING_SNAKE_CASE =None __SCREAMING_SNAKE_CASE =con_pt_to_diffuser(args.unet_path, unet_config) __SCREAMING_SNAKE_CASE =UNetaDModel(**unet_config) image_unet.load_state_dict(converted_unet_ckpt) # Get scheduler config if "cd" in ckpt_name or "test" in ckpt_name: __SCREAMING_SNAKE_CASE =CD_SCHEDULER_CONFIG elif "ct" in ckpt_name and "imagenet64" in ckpt_name: __SCREAMING_SNAKE_CASE =CT_IMAGENET_64_SCHEDULER_CONFIG elif "ct" in ckpt_name and "256" in ckpt_name and (("bedroom" in ckpt_name) or ("cat" in ckpt_name)): __SCREAMING_SNAKE_CASE =CT_LSUN_256_SCHEDULER_CONFIG else: raise ValueError(F"Checkpoint type {ckpt_name} is not currently supported.") __SCREAMING_SNAKE_CASE =CMStochasticIterativeScheduler(**scheduler_config) __SCREAMING_SNAKE_CASE =ConsistencyModelPipeline(unet=image_unet, scheduler=cm_scheduler) consistency_model.save_pretrained(args.dump_path)
213
0
"""simple docstring""" import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_barthez import BarthezTokenizer else: __A = None __A = logging.get_logger(__name__) __A = {'''vocab_file''': '''sentencepiece.bpe.model''', '''tokenizer_file''': '''tokenizer.json'''} __A = { '''vocab_file''': { '''moussaKam/mbarthez''': '''https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model''', '''moussaKam/barthez''': '''https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model''', '''moussaKam/barthez-orangesum-title''': ( '''https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model''' ), }, '''tokenizer_file''': { '''moussaKam/mbarthez''': '''https://huggingface.co/moussaKam/mbarthez/resolve/main/tokenizer.json''', '''moussaKam/barthez''': '''https://huggingface.co/moussaKam/barthez/resolve/main/tokenizer.json''', '''moussaKam/barthez-orangesum-title''': ( '''https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/tokenizer.json''' ), }, } __A = { '''moussaKam/mbarthez''': 1024, '''moussaKam/barthez''': 1024, '''moussaKam/barthez-orangesum-title''': 1024, } __A = '''▁''' class _snake_case ( a__ ): snake_case__ = VOCAB_FILES_NAMES snake_case__ = PRETRAINED_VOCAB_FILES_MAP snake_case__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case__ = ["input_ids", "attention_mask"] snake_case__ = BarthezTokenizer def __init__( self : Dict , UpperCAmelCase : str=None , UpperCAmelCase : Optional[Any]=None , UpperCAmelCase : int="<s>" , UpperCAmelCase : Dict="</s>" , UpperCAmelCase : List[str]="</s>" , UpperCAmelCase : int="<s>" , UpperCAmelCase : Dict="<unk>" , UpperCAmelCase : List[Any]="<pad>" , UpperCAmelCase : List[Any]="<mask>" , **UpperCAmelCase : Dict , ): # Mask token behave like a normal word, i.e. include the space before it __lowerCamelCase : Union[str, Any] = AddedToken(UpperCAmelCase , lstrip=UpperCAmelCase , rstrip=UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ) else mask_token super().__init__( UpperCAmelCase , tokenizer_file=UpperCAmelCase , bos_token=UpperCAmelCase , eos_token=UpperCAmelCase , unk_token=UpperCAmelCase , sep_token=UpperCAmelCase , cls_token=UpperCAmelCase , pad_token=UpperCAmelCase , mask_token=UpperCAmelCase , **UpperCAmelCase , ) __lowerCamelCase : Any = vocab_file __lowerCamelCase : Dict = False if not self.vocab_file else True def lowerCamelCase__ ( self : Any , UpperCAmelCase : List[int] , UpperCAmelCase : Optional[List[int]] = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __lowerCamelCase : Optional[Any] = [self.cls_token_id] __lowerCamelCase : Any = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def lowerCamelCase__ ( self : List[Any] , UpperCAmelCase : List[int] , UpperCAmelCase : Optional[List[int]] = None ): __lowerCamelCase : int = [self.sep_token_id] __lowerCamelCase : 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 lowerCamelCase__ ( self : Optional[int] , UpperCAmelCase : str , UpperCAmelCase : 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(UpperCAmelCase ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return __lowerCamelCase : Optional[Any] = os.path.join( UpperCAmelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(UpperCAmelCase ): copyfile(self.vocab_file , UpperCAmelCase ) return (out_vocab_file,)
64
"""simple docstring""" import gc import unittest import numpy as np import torch from diffusers import AutoencoderKL, DDIMScheduler, DiTPipeline, DPMSolverMultistepScheduler, TransformeraDModel from diffusers.utils import is_xformers_available, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS, CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class _snake_case ( a__ , unittest.TestCase ): snake_case__ = DiTPipeline snake_case__ = CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS snake_case__ = PipelineTesterMixin.required_optional_params - { "latents", "num_images_per_prompt", "callback", "callback_steps", } snake_case__ = CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS snake_case__ = False def lowerCamelCase__ ( self : Tuple ): torch.manual_seed(0 ) __lowerCamelCase : Optional[Any] = TransformeraDModel( sample_size=16 , num_layers=2 , patch_size=4 , attention_head_dim=8 , num_attention_heads=2 , in_channels=4 , out_channels=8 , attention_bias=UpperCAmelCase , activation_fn="gelu-approximate" , num_embeds_ada_norm=1000 , norm_type="ada_norm_zero" , norm_elementwise_affine=UpperCAmelCase , ) __lowerCamelCase : List[str] = AutoencoderKL() __lowerCamelCase : List[Any] = DDIMScheduler() __lowerCamelCase : Optional[Any] = {"transformer": transformer.eval(), "vae": vae.eval(), "scheduler": scheduler} return components def lowerCamelCase__ ( self : List[str] , UpperCAmelCase : Any , UpperCAmelCase : Union[str, Any]=0 ): if str(UpperCAmelCase ).startswith("mps" ): __lowerCamelCase : List[str] = torch.manual_seed(UpperCAmelCase ) else: __lowerCamelCase : List[str] = torch.Generator(device=UpperCAmelCase ).manual_seed(UpperCAmelCase ) __lowerCamelCase : str = { "class_labels": [1], "generator": generator, "num_inference_steps": 2, "output_type": "numpy", } return inputs def lowerCamelCase__ ( self : Optional[Any] ): __lowerCamelCase : Dict = "cpu" __lowerCamelCase : int = self.get_dummy_components() __lowerCamelCase : Optional[Any] = self.pipeline_class(**UpperCAmelCase ) pipe.to(UpperCAmelCase ) pipe.set_progress_bar_config(disable=UpperCAmelCase ) __lowerCamelCase : Optional[int] = self.get_dummy_inputs(UpperCAmelCase ) __lowerCamelCase : List[Any] = pipe(**UpperCAmelCase ).images __lowerCamelCase : Optional[int] = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 16, 16, 3) ) __lowerCamelCase : Optional[int] = np.array([0.2_9_4_6, 0.6_6_0_1, 0.4_3_2_9, 0.3_2_9_6, 0.4_1_4_4, 0.5_3_1_9, 0.7_2_7_3, 0.5_0_1_3, 0.4_4_5_7] ) __lowerCamelCase : Optional[int] = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(UpperCAmelCase , 1E-3 ) def lowerCamelCase__ ( self : Any ): self._test_inference_batch_single_identical(relax_max_difference=UpperCAmelCase , expected_max_diff=1E-3 ) @unittest.skipIf( torch_device != "cuda" or not is_xformers_available() , reason="XFormers attention is only available with CUDA and `xformers` installed" , ) def lowerCamelCase__ ( self : List[str] ): self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) @require_torch_gpu @slow class _snake_case ( unittest.TestCase ): def lowerCamelCase__ ( self : Any ): super().tearDown() gc.collect() torch.cuda.empty_cache() def lowerCamelCase__ ( self : List[str] ): __lowerCamelCase : Optional[int] = torch.manual_seed(0 ) __lowerCamelCase : str = DiTPipeline.from_pretrained("facebook/DiT-XL-2-256" ) pipe.to("cuda" ) __lowerCamelCase : Tuple = ["vase", "umbrella", "white shark", "white wolf"] __lowerCamelCase : Optional[int] = pipe.get_label_ids(UpperCAmelCase ) __lowerCamelCase : Optional[Any] = pipe(UpperCAmelCase , generator=UpperCAmelCase , num_inference_steps=40 , output_type="np" ).images for word, image in zip(UpperCAmelCase , UpperCAmelCase ): __lowerCamelCase : Dict = load_numpy( F"""https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/dit/{word}.npy""" ) assert np.abs((expected_image - image).max() ) < 1E-2 def lowerCamelCase__ ( self : str ): __lowerCamelCase : Tuple = DiTPipeline.from_pretrained("facebook/DiT-XL-2-512" ) __lowerCamelCase : Any = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) pipe.to("cuda" ) __lowerCamelCase : Union[str, Any] = ["vase", "umbrella"] __lowerCamelCase : int = pipe.get_label_ids(UpperCAmelCase ) __lowerCamelCase : Dict = torch.manual_seed(0 ) __lowerCamelCase : Dict = pipe(UpperCAmelCase , generator=UpperCAmelCase , num_inference_steps=25 , output_type="np" ).images for word, image in zip(UpperCAmelCase , UpperCAmelCase ): __lowerCamelCase : Union[str, Any] = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" F"""/dit/{word}_512.npy""" ) assert np.abs((expected_image - image).max() ) < 1E-1
64
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, is_vision_available, ) __snake_case : List[str] = { 'configuration_owlvit': [ 'OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'OwlViTConfig', 'OwlViTOnnxConfig', 'OwlViTTextConfig', 'OwlViTVisionConfig', ], 'processing_owlvit': ['OwlViTProcessor'], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case : Union[str, Any] = ['OwlViTFeatureExtractor'] __snake_case : str = ['OwlViTImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case : Any = [ 'OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'OwlViTModel', 'OwlViTPreTrainedModel', 'OwlViTTextModel', 'OwlViTVisionModel', 'OwlViTForObjectDetection', ] if TYPE_CHECKING: from .configuration_owlvit import ( OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, OwlViTConfig, OwlViTOnnxConfig, OwlViTTextConfig, OwlViTVisionConfig, ) from .processing_owlvit import OwlViTProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_owlvit import OwlViTFeatureExtractor from .image_processing_owlvit import OwlViTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_owlvit import ( OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST, OwlViTForObjectDetection, OwlViTModel, OwlViTPreTrainedModel, OwlViTTextModel, OwlViTVisionModel, ) else: import sys __snake_case : Union[str, Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
269
'''simple docstring''' from __future__ import annotations from collections.abc import Sequence from typing import Literal def __lowerCamelCase ( lowerCAmelCase_ , lowerCAmelCase_ ) -> str | Literal[False]: _a : Optional[int] = list(lowerCAmelCase_ ) _a : Optional[Any] = list(lowerCAmelCase_ ) _a : Union[str, Any] = 0 for i in range(len(lowerCAmelCase_ ) ): if lista[i] != lista[i]: count += 1 _a : Optional[int] = '_' if count > 1: return False else: return "".join(lowerCAmelCase_ ) def __lowerCamelCase ( lowerCAmelCase_ ) -> list[str]: _a : Optional[int] = [] while True: _a : Any = ['$'] * len(lowerCAmelCase_ ) _a : List[str] = [] for i in range(len(lowerCAmelCase_ ) ): for j in range(i + 1 , len(lowerCAmelCase_ ) ): _a : Optional[int] = compare_string(binary[i] , binary[j] ) if k is False: _a : Optional[Any] = '*' _a : Optional[Any] = '*' temp.append('X' ) for i in range(len(lowerCAmelCase_ ) ): if checka[i] == "$": pi.append(binary[i] ) if len(lowerCAmelCase_ ) == 0: return pi _a : Any = list(set(lowerCAmelCase_ ) ) def __lowerCamelCase ( lowerCAmelCase_ , lowerCAmelCase_ ) -> list[str]: _a : int = [] for minterm in minterms: _a : Optional[int] = '' for _ in range(lowerCAmelCase_ ): _a : Union[str, Any] = str(minterm % 2 ) + string minterm //= 2 temp.append(lowerCAmelCase_ ) return temp def __lowerCamelCase ( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) -> bool: _a : int = list(lowerCAmelCase_ ) _a : Union[str, Any] = list(lowerCAmelCase_ ) _a : str = 0 for i in range(len(lowerCAmelCase_ ) ): if lista[i] != lista[i]: count_n += 1 return count_n == count def __lowerCamelCase ( lowerCAmelCase_ , lowerCAmelCase_ ) -> list[str]: _a : List[Any] = [] _a : Optional[Any] = [0] * len(lowerCAmelCase_ ) for i in range(len(chart[0] ) ): _a : Union[str, Any] = 0 _a : int = -1 for j in range(len(lowerCAmelCase_ ) ): if chart[j][i] == 1: count += 1 _a : int = j if count == 1: _a : List[Any] = 1 for i in range(len(lowerCAmelCase_ ) ): if select[i] == 1: for j in range(len(chart[0] ) ): if chart[i][j] == 1: for k in range(len(lowerCAmelCase_ ) ): _a : Any = 0 temp.append(prime_implicants[i] ) while True: _a : Union[str, Any] = 0 _a : List[Any] = -1 _a : str = 0 for i in range(len(lowerCAmelCase_ ) ): _a : Union[str, Any] = chart[i].count(1 ) if count_n > max_n: _a : Any = count_n _a : int = i if max_n == 0: return temp temp.append(prime_implicants[rem] ) for i in range(len(chart[0] ) ): if chart[rem][i] == 1: for j in range(len(lowerCAmelCase_ ) ): _a : List[str] = 0 def __lowerCamelCase ( lowerCAmelCase_ , lowerCAmelCase_ ) -> list[list[int]]: _a : int = [[0 for x in range(len(lowerCAmelCase_ ) )] for x in range(len(lowerCAmelCase_ ) )] for i in range(len(lowerCAmelCase_ ) ): _a : str = prime_implicants[i].count('_' ) for j in range(len(lowerCAmelCase_ ) ): if is_for_table(prime_implicants[i] , binary[j] , lowerCAmelCase_ ): _a : Optional[Any] = 1 return chart def __lowerCamelCase ( ) -> None: _a : Optional[int] = int(input('Enter the no. of variables\n' ) ) _a : List[Any] = [ float(lowerCAmelCase_ ) for x in input( 'Enter the decimal representation of Minterms \'Spaces Separated\'\n' ).split() ] _a : List[str] = decimal_to_binary(lowerCAmelCase_ , lowerCAmelCase_ ) _a : Dict = check(lowerCAmelCase_ ) print('Prime Implicants are:' ) print(lowerCAmelCase_ ) _a : List[Any] = prime_implicant_chart(lowerCAmelCase_ , lowerCAmelCase_ ) _a : int = selection(lowerCAmelCase_ , lowerCAmelCase_ ) print('Essential Prime Implicants are:' ) print(lowerCAmelCase_ ) if __name__ == "__main__": import doctest doctest.testmod() main()
89
0
import pprint import requests SCREAMING_SNAKE_CASE_ = "https://zenquotes.io/api" def __lowercase ( ) -> Optional[int]: '''simple docstring''' return requests.get(API_ENDPOINT_URL + """/today""" ).json() def __lowercase ( ) -> int: '''simple docstring''' return requests.get(API_ENDPOINT_URL + """/random""" ).json() if __name__ == "__main__": SCREAMING_SNAKE_CASE_ = random_quotes() pprint.pprint(response)
350
import inspect import unittest from transformers import RegNetConfig, is_flax_available from transformers.testing_utils import require_flax, slow from transformers.utils import cached_property, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor if is_flax_available(): import jax import jax.numpy as jnp from transformers.models.regnet.modeling_flax_regnet import FlaxRegNetForImageClassification, FlaxRegNetModel if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __init__( self : Optional[int] ,lowerCamelCase__ : List[str] ,lowerCamelCase__ : Optional[Any]=3 ,lowerCamelCase__ : List[str]=32 ,lowerCamelCase__ : List[Any]=3 ,lowerCamelCase__ : str=10 ,lowerCamelCase__ : Any=[10, 20, 30, 40] ,lowerCamelCase__ : Optional[Any]=[1, 1, 2, 1] ,lowerCamelCase__ : Union[str, Any]=True ,lowerCamelCase__ : int=True ,lowerCamelCase__ : Tuple="relu" ,lowerCamelCase__ : Dict=3 ,lowerCamelCase__ : Optional[int]=None ,) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = embeddings_size SCREAMING_SNAKE_CASE = hidden_sizes SCREAMING_SNAKE_CASE = depths SCREAMING_SNAKE_CASE = is_training SCREAMING_SNAKE_CASE = use_labels SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = num_labels SCREAMING_SNAKE_CASE = scope SCREAMING_SNAKE_CASE = len(lowerCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( self : Tuple ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values def SCREAMING_SNAKE_CASE__ ( self : str ) -> Optional[int]: '''simple docstring''' return RegNetConfig( num_channels=self.num_channels ,embeddings_size=self.embeddings_size ,hidden_sizes=self.hidden_sizes ,depths=self.depths ,hidden_act=self.hidden_act ,num_labels=self.num_labels ,image_size=self.image_size ,) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ,lowerCamelCase__ : int ,lowerCamelCase__ : Union[str, Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = FlaxRegNetModel(config=lowerCamelCase__ ) SCREAMING_SNAKE_CASE = model(lowerCamelCase__ ) # Output shape (b, c, h, w) self.parent.assertEqual( result.last_hidden_state.shape ,(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) ,) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ,lowerCamelCase__ : Optional[int] ,lowerCamelCase__ : Optional[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = self.num_labels SCREAMING_SNAKE_CASE = FlaxRegNetForImageClassification(config=lowerCamelCase__ ) SCREAMING_SNAKE_CASE = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_labels) ) def SCREAMING_SNAKE_CASE__ ( self : str ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE = config_and_inputs SCREAMING_SNAKE_CASE = {"""pixel_values""": pixel_values} return config, inputs_dict @require_flax class UpperCamelCase__ ( lowerCAmelCase_ , unittest.TestCase ): '''simple docstring''' __snake_case : Union[str, Any] = (FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else () __snake_case : Tuple = False __snake_case : int = False __snake_case : Tuple = False def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = FlaxRegNetModelTester(self ) SCREAMING_SNAKE_CASE = ConfigTester(self ,config_class=lowerCamelCase__ ,has_text_modality=lowerCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( self : Any ) -> Optional[int]: '''simple docstring''' 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 ) -> List[str]: '''simple docstring''' return def SCREAMING_SNAKE_CASE__ ( self : Tuple ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__ ) @unittest.skip(reason="""RegNet does not use inputs_embeds""" ) def SCREAMING_SNAKE_CASE__ ( self : str ) -> Any: '''simple docstring''' pass @unittest.skip(reason="""RegNet does not support input and output embeddings""" ) def SCREAMING_SNAKE_CASE__ ( self : str ) -> Any: '''simple docstring''' pass def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(lowerCamelCase__ ) SCREAMING_SNAKE_CASE = inspect.signature(model.__call__ ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE = ["""pixel_values"""] self.assertListEqual(arg_names[:1] ,lowerCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ) -> int: '''simple docstring''' def check_hidden_states_output(lowerCamelCase__ : Tuple ,lowerCamelCase__ : Union[str, Any] ,lowerCamelCase__ : Optional[int] ): SCREAMING_SNAKE_CASE = model_class(lowerCamelCase__ ) SCREAMING_SNAKE_CASE = model(**self._prepare_for_class(lowerCamelCase__ ,lowerCamelCase__ ) ) SCREAMING_SNAKE_CASE = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states SCREAMING_SNAKE_CASE = self.model_tester.num_stages self.assertEqual(len(lowerCamelCase__ ) ,expected_num_stages + 1 ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = True check_hidden_states_output(lowerCamelCase__ ,lowerCamelCase__ ,lowerCamelCase__ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE = True check_hidden_states_output(lowerCamelCase__ ,lowerCamelCase__ ,lowerCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( self : Any ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): SCREAMING_SNAKE_CASE = self._prepare_for_class(lowerCamelCase__ ,lowerCamelCase__ ) SCREAMING_SNAKE_CASE = model_class(lowerCamelCase__ ) @jax.jit def model_jitted(lowerCamelCase__ : Dict ,**lowerCamelCase__ : Optional[Any] ): return model(pixel_values=lowerCamelCase__ ,**lowerCamelCase__ ) with self.subTest("""JIT Enabled""" ): SCREAMING_SNAKE_CASE = model_jitted(**lowerCamelCase__ ).to_tuple() with self.subTest("""JIT Disabled""" ): with jax.disable_jit(): SCREAMING_SNAKE_CASE = model_jitted(**lowerCamelCase__ ).to_tuple() self.assertEqual(len(lowerCamelCase__ ) ,len(lowerCamelCase__ ) ) for jitted_output, output in zip(lowerCamelCase__ ,lowerCamelCase__ ): self.assertEqual(jitted_output.shape ,output.shape ) def __lowercase ( ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_flax class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' @cached_property def SCREAMING_SNAKE_CASE__ ( self : int ) -> str: '''simple docstring''' return AutoImageProcessor.from_pretrained("""facebook/regnet-y-040""" ) if is_vision_available() else None @slow def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = FlaxRegNetForImageClassification.from_pretrained("""facebook/regnet-y-040""" ) SCREAMING_SNAKE_CASE = self.default_image_processor SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=lowerCamelCase__ ,return_tensors="""np""" ) SCREAMING_SNAKE_CASE = model(**lowerCamelCase__ ) # verify the logits SCREAMING_SNAKE_CASE = (1, 1000) self.assertEqual(outputs.logits.shape ,lowerCamelCase__ ) SCREAMING_SNAKE_CASE = jnp.array([-0.4180, -1.5051, -3.4836] ) self.assertTrue(jnp.allclose(outputs.logits[0, :3] ,lowerCamelCase__ ,atol=1e-4 ) )
193
0
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device if is_torch_available(): from transformers import AutoModelForSeqaSeqLM, AutoTokenizer @require_torch @require_sentencepiece @require_tokenizers class __lowercase ( unittest.TestCase ): @slow def UpperCAmelCase__ (self ): lowerCamelCase_ : List[str] = AutoModelForSeqaSeqLM.from_pretrained('''google/mt5-small''' , return_dict=A ).to(A ) lowerCamelCase_ : Any = AutoTokenizer.from_pretrained('''google/mt5-small''' ) lowerCamelCase_ : Optional[Any] = tokenizer('''Hello there''' , return_tensors='''pt''' ).input_ids lowerCamelCase_ : Optional[Any] = tokenizer('''Hi I am''' , return_tensors='''pt''' ).input_ids lowerCamelCase_ : Tuple = model(input_ids.to(A ) , labels=labels.to(A ) ).loss lowerCamelCase_ : int = -(labels.shape[-1] * loss.item()) lowerCamelCase_ : Tuple = -84.91_27 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1E-4 )
318
'''simple docstring''' import copy import tempfile import unittest from huggingface_hub import HfFolder, delete_repo from parameterized import parameterized from requests.exceptions import HTTPError from transformers import AutoConfig, GenerationConfig from transformers.testing_utils import TOKEN, USER, is_staging_test class __lowercase ( unittest.TestCase ): @parameterized.expand([(None,), ('''foo.json''',)] ) def UpperCAmelCase__ (self , A ): lowerCamelCase_ : List[str] = GenerationConfig( do_sample=A , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(A , config_name=A ) lowerCamelCase_ : List[Any] = GenerationConfig.from_pretrained(A , config_name=A ) # Checks parameters that were specified self.assertEqual(loaded_config.do_sample , A ) self.assertEqual(loaded_config.temperature , 0.7 ) self.assertEqual(loaded_config.length_penalty , 1.0 ) self.assertEqual(loaded_config.bad_words_ids , [[1, 2, 3], [4, 5]] ) # Checks parameters that were not specified (defaults) self.assertEqual(loaded_config.top_k , 5_0 ) self.assertEqual(loaded_config.max_length , 2_0 ) self.assertEqual(loaded_config.max_time , A ) def UpperCAmelCase__ (self ): lowerCamelCase_ : Tuple = AutoConfig.from_pretrained('''gpt2''' ) lowerCamelCase_ : Dict = GenerationConfig.from_model_config(A ) lowerCamelCase_ : Optional[int] = GenerationConfig() # The generation config has loaded a few non-default parameters from the model config self.assertNotEqual(A , A ) # One of those parameters is eos_token_id -- check if it matches self.assertNotEqual(generation_config_from_model.eos_token_id , default_generation_config.eos_token_id ) self.assertEqual(generation_config_from_model.eos_token_id , model_config.eos_token_id ) def UpperCAmelCase__ (self ): lowerCamelCase_ : Optional[int] = GenerationConfig() lowerCamelCase_ : Dict = { '''max_new_tokens''': 1_0_2_4, '''foo''': '''bar''', } lowerCamelCase_ : int = copy.deepcopy(A ) lowerCamelCase_ : str = generation_config.update(**A ) # update_kwargs was not modified (no side effects) self.assertEqual(A , A ) # update_kwargs was used to update the config on valid attributes self.assertEqual(generation_config.max_new_tokens , 1_0_2_4 ) # `.update()` returns a dictionary of unused kwargs self.assertEqual(A , {'''foo''': '''bar'''} ) def UpperCAmelCase__ (self ): lowerCamelCase_ : str = GenerationConfig() lowerCamelCase_ : str = '''bar''' with tempfile.TemporaryDirectory('''test-generation-config''' ) as tmp_dir: generation_config.save_pretrained(A ) lowerCamelCase_ : Optional[int] = GenerationConfig.from_pretrained(A ) # update_kwargs was used to update the config on valid attributes self.assertEqual(new_config.foo , '''bar''' ) lowerCamelCase_ : Tuple = GenerationConfig.from_model_config(A ) assert not hasattr(A , '''foo''' ) # no new kwargs should be initialized if from config def UpperCAmelCase__ (self ): lowerCamelCase_ : Any = GenerationConfig() self.assertEqual(default_config.temperature , 1.0 ) self.assertEqual(default_config.do_sample , A ) self.assertEqual(default_config.num_beams , 1 ) lowerCamelCase_ : Tuple = GenerationConfig( do_sample=A , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) self.assertEqual(config.temperature , 0.7 ) self.assertEqual(config.do_sample , A ) self.assertEqual(config.num_beams , 1 ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(A ) lowerCamelCase_ : List[str] = GenerationConfig.from_pretrained(A , temperature=1.0 ) self.assertEqual(loaded_config.temperature , 1.0 ) self.assertEqual(loaded_config.do_sample , A ) self.assertEqual(loaded_config.num_beams , 1 ) # default value @is_staging_test class __lowercase ( unittest.TestCase ): @classmethod def UpperCAmelCase__ (cls ): lowerCamelCase_ : Dict = TOKEN HfFolder.save_token(A ) @classmethod def UpperCAmelCase__ (cls ): try: delete_repo(token=cls._token , repo_id='''test-generation-config''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-generation-config-org''' ) except HTTPError: pass def UpperCAmelCase__ (self ): lowerCamelCase_ : List[Any] = GenerationConfig( do_sample=A , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub('''test-generation-config''' , use_auth_token=self._token ) lowerCamelCase_ : Optional[Any] = GenerationConfig.from_pretrained(F"""{USER}/test-generation-config""" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(A , getattr(A , A ) ) # Reset repo delete_repo(token=self._token , repo_id='''test-generation-config''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( A , repo_id='''test-generation-config''' , push_to_hub=A , use_auth_token=self._token ) lowerCamelCase_ : List[Any] = GenerationConfig.from_pretrained(F"""{USER}/test-generation-config""" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(A , getattr(A , A ) ) def UpperCAmelCase__ (self ): lowerCamelCase_ : Dict = GenerationConfig( do_sample=A , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub('''valid_org/test-generation-config-org''' , use_auth_token=self._token ) lowerCamelCase_ : Optional[Any] = GenerationConfig.from_pretrained('''valid_org/test-generation-config-org''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(A , getattr(A , A ) ) # Reset repo delete_repo(token=self._token , repo_id='''valid_org/test-generation-config-org''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( A , repo_id='''valid_org/test-generation-config-org''' , push_to_hub=A , use_auth_token=self._token ) lowerCamelCase_ : Optional[int] = GenerationConfig.from_pretrained('''valid_org/test-generation-config-org''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(A , getattr(A , A ) )
318
1
from ..utils import DummyObject, requires_backends class __SCREAMING_SNAKE_CASE ( metaclass=_a ): snake_case : List[Any] = ["""speech"""] def __init__( self , *__lowerCAmelCase , **__lowerCAmelCase ): requires_backends(self , ["""speech"""] ) class __SCREAMING_SNAKE_CASE ( metaclass=_a ): snake_case : List[Any] = ["""speech"""] def __init__( self , *__lowerCAmelCase , **__lowerCAmelCase ): requires_backends(self , ["""speech"""] )
87
def _UpperCamelCase (a__ :str ): """simple docstring""" UpperCamelCase__ = 0 # if input_string is "aba" than new_input_string become "a|b|a" UpperCamelCase__ = """""" UpperCamelCase__ = """""" # append each character + "|" in new_string for range(0, length-1) for i in input_string[: len(a__ ) - 1]: new_input_string += i + "|" # append last character new_input_string += input_string[-1] # we will store the starting and ending of previous furthest ending palindromic # substring UpperCamelCase__ , UpperCamelCase__ = 0, 0 # length[i] shows the length of palindromic substring with center i UpperCamelCase__ = [1 for i in range(len(a__ ) )] # for each character in new_string find corresponding palindromic string UpperCamelCase__ = 0 for j in range(len(a__ ) ): UpperCamelCase__ = 1 if j > r else min(length[l + r - j] // 2 , r - j + 1 ) while ( j - k >= 0 and j + k < len(a__ ) and new_input_string[k + j] == new_input_string[j - k] ): k += 1 UpperCamelCase__ = 2 * k - 1 # does this string is ending after the previously explored end (that is r) ? # if yes the update the new r to the last index of this if j + k - 1 > r: UpperCamelCase__ = j - k + 1 # noqa: E741 UpperCamelCase__ = j + k - 1 # update max_length and start position if max_length < length[j]: UpperCamelCase__ = length[j] UpperCamelCase__ = j # create that string UpperCamelCase__ = new_input_string[start - max_length // 2 : start + max_length // 2 + 1] for i in s: if i != "|": output_string += i return output_string if __name__ == "__main__": import doctest doctest.testmod()
87
1
'''simple docstring''' from __future__ import absolute_import, division, print_function, unicode_literals from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers import RobertaConfig from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.roberta.modeling_roberta import ( ROBERTA_INPUTS_DOCSTRING, ROBERTA_START_DOCSTRING, RobertaEmbeddings, ) from .modeling_highway_bert import BertPreTrainedModel, DeeBertModel, HighwayException, entropy @add_start_docstrings( '''The RoBERTa Model transformer with early exiting (DeeRoBERTa). ''' , __snake_case , ) class _a ( __snake_case ): '''simple docstring''' A : str = RobertaConfig A : Any = 'roberta' def __init__( self, A ): '''simple docstring''' super().__init__(A_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = RobertaEmbeddings(A_ ) self.init_weights() @add_start_docstrings( '''RoBERTa Model (with early exiting - DeeRoBERTa) with a classifier on top,\n also takes care of multi-layer training. ''' , __snake_case , ) class _a ( __snake_case ): '''simple docstring''' A : Optional[int] = RobertaConfig A : List[Any] = 'roberta' def __init__( self, A ): '''simple docstring''' super().__init__(A_ ) SCREAMING_SNAKE_CASE : Any = config.num_labels SCREAMING_SNAKE_CASE : int = config.num_hidden_layers SCREAMING_SNAKE_CASE : Union[str, Any] = DeeRobertaModel(A_ ) SCREAMING_SNAKE_CASE : str = nn.Dropout(config.hidden_dropout_prob ) SCREAMING_SNAKE_CASE : List[str] = nn.Linear(config.hidden_size, self.config.num_labels ) @add_start_docstrings_to_model_forward(A_ ) def UpperCamelCase_ ( self, A=None, A=None, A=None, A=None, A=None, A=None, A=None, A=-1, A=False, ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.num_layers try: SCREAMING_SNAKE_CASE : Any = self.roberta( A_, attention_mask=A_, token_type_ids=A_, position_ids=A_, head_mask=A_, inputs_embeds=A_, ) SCREAMING_SNAKE_CASE : Optional[int] = outputs[1] SCREAMING_SNAKE_CASE : Tuple = self.dropout(A_ ) SCREAMING_SNAKE_CASE : str = self.classifier(A_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = (logits,) + outputs[2:] # add hidden states and attention if they are here except HighwayException as e: SCREAMING_SNAKE_CASE : Optional[Any] = e.message SCREAMING_SNAKE_CASE : Dict = e.exit_layer SCREAMING_SNAKE_CASE : Optional[Any] = outputs[0] if not self.training: SCREAMING_SNAKE_CASE : Optional[int] = entropy(A_ ) SCREAMING_SNAKE_CASE : str = [] SCREAMING_SNAKE_CASE : Dict = [] if labels is not None: if self.num_labels == 1: # We are doing regression SCREAMING_SNAKE_CASE : List[Any] = MSELoss() SCREAMING_SNAKE_CASE : Any = loss_fct(logits.view(-1 ), labels.view(-1 ) ) else: SCREAMING_SNAKE_CASE : List[str] = CrossEntropyLoss() SCREAMING_SNAKE_CASE : Tuple = loss_fct(logits.view(-1, self.num_labels ), labels.view(-1 ) ) # work with highway exits SCREAMING_SNAKE_CASE : Optional[int] = [] for highway_exit in outputs[-1]: SCREAMING_SNAKE_CASE : Optional[Any] = highway_exit[0] if not self.training: highway_logits_all.append(A_ ) highway_entropy.append(highway_exit[2] ) if self.num_labels == 1: # We are doing regression SCREAMING_SNAKE_CASE : Dict = MSELoss() SCREAMING_SNAKE_CASE : int = loss_fct(highway_logits.view(-1 ), labels.view(-1 ) ) else: SCREAMING_SNAKE_CASE : Union[str, Any] = CrossEntropyLoss() SCREAMING_SNAKE_CASE : str = loss_fct(highway_logits.view(-1, self.num_labels ), labels.view(-1 ) ) highway_losses.append(A_ ) if train_highway: SCREAMING_SNAKE_CASE : Union[str, Any] = (sum(highway_losses[:-1] ),) + outputs # exclude the final highway, of course else: SCREAMING_SNAKE_CASE : int = (loss,) + outputs if not self.training: SCREAMING_SNAKE_CASE : List[str] = outputs + ((original_entropy, highway_entropy), exit_layer) if output_layer >= 0: SCREAMING_SNAKE_CASE : Union[str, Any] = ( (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:] ) # use the highway of the last layer return outputs # (loss), logits, (hidden_states), (attentions), entropy
251
from typing import List, Optional, Tuple, Union import torch from ...utils import logging, randn_tensor from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline __lowerCamelCase : str = logging.get_logger(__name__) # pylint: disable=invalid-name class A__ ( __snake_case ): def __init__( self , A_ , A_ ): '''simple docstring''' super().__init__() self.register_modules(unet=A_ , scheduler=A_ ) @torch.no_grad() def __call__( self , A_ = 1 , A_ = 100 , A_ = None , A_ = None , A_ = True , ): '''simple docstring''' if audio_length_in_s is None: UpperCamelCase : str = self.unet.config.sample_size / self.unet.config.sample_rate UpperCamelCase : Optional[Any] = audio_length_in_s * self.unet.config.sample_rate UpperCamelCase : Any = 2 ** len(self.unet.up_blocks ) if sample_size < 3 * down_scale_factor: raise ValueError( F"""{audio_length_in_s} is too small. Make sure it's bigger or equal to""" F""" {3 * down_scale_factor / self.unet.config.sample_rate}.""" ) UpperCamelCase : Union[str, Any] = int(A_ ) if sample_size % down_scale_factor != 0: UpperCamelCase : List[str] = ( (audio_length_in_s * self.unet.config.sample_rate) // down_scale_factor + 1 ) * down_scale_factor logger.info( F"""{audio_length_in_s} is increased to {sample_size / self.unet.config.sample_rate} so that it can be handled""" F""" by the model. It will be cut to {original_sample_size / self.unet.config.sample_rate} after the denoising""" " process." ) UpperCamelCase : Any = int(A_ ) UpperCamelCase : Union[str, Any] = next(iter(self.unet.parameters() ) ).dtype UpperCamelCase : Optional[int] = (batch_size, self.unet.config.in_channels, sample_size) if isinstance(A_ , A_ ) and len(A_ ) != batch_size: raise ValueError( F"""You have passed a list of generators of length {len(A_ )}, but requested an effective batch""" F""" size of {batch_size}. Make sure the batch size matches the length of the generators.""" ) UpperCamelCase : Optional[Any] = randn_tensor(A_ , generator=A_ , device=self.device , dtype=A_ ) # set step values self.scheduler.set_timesteps(A_ , device=audio.device ) UpperCamelCase : Optional[int] = self.scheduler.timesteps.to(A_ ) for t in self.progress_bar(self.scheduler.timesteps ): # 1. predict noise model_output UpperCamelCase : Dict = self.unet(A_ , A_ ).sample # 2. compute previous image: x_t -> t_t-1 UpperCamelCase : int = self.scheduler.step(A_ , A_ , A_ ).prev_sample UpperCamelCase : Optional[Any] = audio.clamp(-1 , 1 ).float().cpu().numpy() UpperCamelCase : Dict = audio[:, :, :original_sample_size] if not return_dict: return (audio,) return AudioPipelineOutput(audios=A_ )
52
0
'''simple docstring''' import warnings from ...utils import logging from .image_processing_perceiver import PerceiverImageProcessor UpperCamelCase__ = logging.get_logger(__name__) class lowerCamelCase_ ( __a ): def __init__( self : Optional[int] , *_A : Optional[int] , **_A : Optional[Any] ): '''simple docstring''' warnings.warn( '''The class PerceiverFeatureExtractor is deprecated and will be removed in version 5 of Transformers.''' ''' Please use PerceiverImageProcessor instead.''' , _A , ) super().__init__(*_A , **_A )
299
'''simple docstring''' import gc import math import unittest import torch from diffusers import UNetaDModel from diffusers.utils import floats_tensor, logging, slow, torch_all_close, torch_device from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin, UNetTesterMixin UpperCamelCase__ = logging.get_logger(__name__) enable_full_determinism() class lowerCamelCase_ ( __a , __a , unittest.TestCase ): lowerCAmelCase__ = UNetaDModel lowerCAmelCase__ = 'sample' @property def lowercase_ ( self : Union[str, Any] ): '''simple docstring''' UpperCAmelCase__ : Optional[Any] = 4 UpperCAmelCase__ : str = 3 UpperCAmelCase__ : str = (32, 32) UpperCAmelCase__ : List[Any] = floats_tensor((batch_size, num_channels) + sizes ).to(_A ) UpperCAmelCase__ : Tuple = torch.tensor([10] ).to(_A ) return {"sample": noise, "timestep": time_step} @property def lowercase_ ( self : int ): '''simple docstring''' return (3, 32, 32) @property def lowercase_ ( self : Dict ): '''simple docstring''' return (3, 32, 32) def lowercase_ ( self : Union[str, Any] ): '''simple docstring''' UpperCAmelCase__ : Tuple = { '''block_out_channels''': (32, 64), '''down_block_types''': ('''DownBlock2D''', '''AttnDownBlock2D'''), '''up_block_types''': ('''AttnUpBlock2D''', '''UpBlock2D'''), '''attention_head_dim''': 3, '''out_channels''': 3, '''in_channels''': 3, '''layers_per_block''': 2, '''sample_size''': 32, } UpperCAmelCase__ : Tuple = self.dummy_input return init_dict, inputs_dict class lowerCamelCase_ ( __a , __a , unittest.TestCase ): lowerCAmelCase__ = UNetaDModel lowerCAmelCase__ = 'sample' @property def lowercase_ ( self : Any ): '''simple docstring''' UpperCAmelCase__ : List[str] = 4 UpperCAmelCase__ : Dict = 4 UpperCAmelCase__ : List[str] = (32, 32) UpperCAmelCase__ : List[str] = floats_tensor((batch_size, num_channels) + sizes ).to(_A ) UpperCAmelCase__ : List[Any] = torch.tensor([10] ).to(_A ) return {"sample": noise, "timestep": time_step} @property def lowercase_ ( self : Tuple ): '''simple docstring''' return (4, 32, 32) @property def lowercase_ ( self : List[str] ): '''simple docstring''' return (4, 32, 32) def lowercase_ ( self : Dict ): '''simple docstring''' UpperCAmelCase__ : List[str] = { '''sample_size''': 32, '''in_channels''': 4, '''out_channels''': 4, '''layers_per_block''': 2, '''block_out_channels''': (32, 64), '''attention_head_dim''': 32, '''down_block_types''': ('''DownBlock2D''', '''DownBlock2D'''), '''up_block_types''': ('''UpBlock2D''', '''UpBlock2D'''), } UpperCAmelCase__ : Optional[Any] = self.dummy_input return init_dict, inputs_dict def lowercase_ ( self : Any ): '''simple docstring''' UpperCAmelCase__ , UpperCAmelCase__ : int = UNetaDModel.from_pretrained('''fusing/unet-ldm-dummy-update''' , output_loading_info=_A ) self.assertIsNotNone(_A ) self.assertEqual(len(loading_info['''missing_keys'''] ) , 0 ) model.to(_A ) UpperCAmelCase__ : Dict = model(**self.dummy_input ).sample assert image is not None, "Make sure output is not None" @unittest.skipIf(torch_device != '''cuda''' , '''This test is supposed to run on GPU''' ) def lowercase_ ( self : Optional[Any] ): '''simple docstring''' UpperCAmelCase__ , UpperCAmelCase__ : Any = UNetaDModel.from_pretrained('''fusing/unet-ldm-dummy-update''' , output_loading_info=_A ) model.to(_A ) UpperCAmelCase__ : Dict = model(**self.dummy_input ).sample assert image is not None, "Make sure output is not None" @unittest.skipIf(torch_device != '''cuda''' , '''This test is supposed to run on GPU''' ) def lowercase_ ( self : Optional[Any] ): '''simple docstring''' UpperCAmelCase__ , UpperCAmelCase__ : Optional[int] = UNetaDModel.from_pretrained('''fusing/unet-ldm-dummy-update''' , output_loading_info=_A ) model_accelerate.to(_A ) model_accelerate.eval() UpperCAmelCase__ : Tuple = torch.randn( 1 , model_accelerate.config.in_channels , model_accelerate.config.sample_size , model_accelerate.config.sample_size , generator=torch.manual_seed(0 ) , ) UpperCAmelCase__ : Union[str, Any] = noise.to(_A ) UpperCAmelCase__ : Optional[Any] = torch.tensor([10] * noise.shape[0] ).to(_A ) UpperCAmelCase__ : Any = model_accelerate(_A , _A )['''sample'''] # two models don't need to stay in the device at the same time del model_accelerate torch.cuda.empty_cache() gc.collect() UpperCAmelCase__ , UpperCAmelCase__ : Dict = UNetaDModel.from_pretrained( '''fusing/unet-ldm-dummy-update''' , output_loading_info=_A , low_cpu_mem_usage=_A ) model_normal_load.to(_A ) model_normal_load.eval() UpperCAmelCase__ : Optional[int] = model_normal_load(_A , _A )['''sample'''] assert torch_all_close(_A , _A , rtol=1e-3 ) def lowercase_ ( self : Optional[Any] ): '''simple docstring''' UpperCAmelCase__ : Optional[int] = UNetaDModel.from_pretrained('''fusing/unet-ldm-dummy-update''' ) model.eval() model.to(_A ) UpperCAmelCase__ : Union[str, Any] = torch.randn( 1 , model.config.in_channels , model.config.sample_size , model.config.sample_size , generator=torch.manual_seed(0 ) , ) UpperCAmelCase__ : str = noise.to(_A ) UpperCAmelCase__ : str = torch.tensor([10] * noise.shape[0] ).to(_A ) with torch.no_grad(): UpperCAmelCase__ : Optional[int] = model(_A , _A ).sample UpperCAmelCase__ : List[Any] = output[0, -1, -3:, -3:].flatten().cpu() # fmt: off UpperCAmelCase__ : Tuple = torch.tensor([-1_3.3_2_5_8, -2_0.1_1_0_0, -1_5.9_8_7_3, -1_7.6_6_1_7, -2_3.0_5_9_6, -1_7.9_4_1_9, -1_3.3_6_7_5, -1_6.1_8_8_9, -1_2.3_8_0_0] ) # fmt: on self.assertTrue(torch_all_close(_A , _A , rtol=1e-3 ) ) class lowerCamelCase_ ( __a , __a , unittest.TestCase ): lowerCAmelCase__ = UNetaDModel lowerCAmelCase__ = 'sample' @property def lowercase_ ( self : Any , _A : str=(32, 32) ): '''simple docstring''' UpperCAmelCase__ : Tuple = 4 UpperCAmelCase__ : List[str] = 3 UpperCAmelCase__ : str = floats_tensor((batch_size, num_channels) + sizes ).to(_A ) UpperCAmelCase__ : Dict = torch.tensor(batch_size * [10] ).to(dtype=torch.intaa , device=_A ) return {"sample": noise, "timestep": time_step} @property def lowercase_ ( self : List[str] ): '''simple docstring''' return (3, 32, 32) @property def lowercase_ ( self : List[Any] ): '''simple docstring''' return (3, 32, 32) def lowercase_ ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase__ : List[str] = { '''block_out_channels''': [32, 64, 64, 64], '''in_channels''': 3, '''layers_per_block''': 1, '''out_channels''': 3, '''time_embedding_type''': '''fourier''', '''norm_eps''': 1e-6, '''mid_block_scale_factor''': math.sqrt(2.0 ), '''norm_num_groups''': None, '''down_block_types''': [ '''SkipDownBlock2D''', '''AttnSkipDownBlock2D''', '''SkipDownBlock2D''', '''SkipDownBlock2D''', ], '''up_block_types''': [ '''SkipUpBlock2D''', '''SkipUpBlock2D''', '''AttnSkipUpBlock2D''', '''SkipUpBlock2D''', ], } UpperCAmelCase__ : Tuple = self.dummy_input return init_dict, inputs_dict @slow def lowercase_ ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase__ , UpperCAmelCase__ : str = UNetaDModel.from_pretrained('''google/ncsnpp-celebahq-256''' , output_loading_info=_A ) self.assertIsNotNone(_A ) self.assertEqual(len(loading_info['''missing_keys'''] ) , 0 ) model.to(_A ) UpperCAmelCase__ : List[str] = self.dummy_input UpperCAmelCase__ : Dict = floats_tensor((4, 3) + (256, 256) ).to(_A ) UpperCAmelCase__ : Optional[Any] = noise UpperCAmelCase__ : Any = model(**_A ) assert image is not None, "Make sure output is not None" @slow def lowercase_ ( self : Dict ): '''simple docstring''' UpperCAmelCase__ : int = UNetaDModel.from_pretrained('''google/ncsnpp-celebahq-256''' ) model.to(_A ) UpperCAmelCase__ : Optional[Any] = 4 UpperCAmelCase__ : List[str] = 3 UpperCAmelCase__ : Dict = (256, 256) UpperCAmelCase__ : Optional[int] = torch.ones((batch_size, num_channels) + sizes ).to(_A ) UpperCAmelCase__ : Union[str, Any] = torch.tensor(batch_size * [1e-4] ).to(_A ) with torch.no_grad(): UpperCAmelCase__ : Optional[int] = model(_A , _A ).sample UpperCAmelCase__ : Any = output[0, -3:, -3:, -1].flatten().cpu() # fmt: off UpperCAmelCase__ : Tuple = torch.tensor([-4_8_4_2.8_6_9_1, -6_4_9_9.6_6_3_1, -3_8_0_0.1_9_5_3, -7_9_7_8.2_6_8_6, -1_0_9_8_0.7_1_2_9, -2_0_0_2_8.8_5_3_5, 8_1_4_8.2_8_2_2, 2_3_4_2.2_9_0_5, 5_6_7.7_6_0_8] ) # fmt: on self.assertTrue(torch_all_close(_A , _A , rtol=1e-2 ) ) def lowercase_ ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase__ : Dict = UNetaDModel.from_pretrained('''fusing/ncsnpp-ffhq-ve-dummy-update''' ) model.to(_A ) UpperCAmelCase__ : str = 4 UpperCAmelCase__ : Any = 3 UpperCAmelCase__ : int = (32, 32) UpperCAmelCase__ : Optional[Any] = torch.ones((batch_size, num_channels) + sizes ).to(_A ) UpperCAmelCase__ : Optional[Any] = torch.tensor(batch_size * [1e-4] ).to(_A ) with torch.no_grad(): UpperCAmelCase__ : int = model(_A , _A ).sample UpperCAmelCase__ : Dict = output[0, -3:, -3:, -1].flatten().cpu() # fmt: off UpperCAmelCase__ : Any = torch.tensor([-0.0_3_2_5, -0.0_9_0_0, -0.0_8_6_9, -0.0_3_3_2, -0.0_7_2_5, -0.0_2_7_0, -0.0_1_0_1, 0.0_2_2_7, 0.0_2_5_6] ) # fmt: on self.assertTrue(torch_all_close(_A , _A , rtol=1e-2 ) ) def lowercase_ ( self : Tuple ): '''simple docstring''' pass
299
1
'''simple docstring''' from typing import Any, Dict, Optional import torch import torch.nn.functional as F from torch import nn from ..utils import maybe_allow_in_graph from .activations import get_activation from .attention_processor import Attention from .embeddings import CombinedTimestepLabelEmbeddings @maybe_allow_in_graph class lowerCAmelCase_ ( nn.Module ): '''simple docstring''' def __init__( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : str=0.0 , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : str = "geglu" , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : bool = True , SCREAMING_SNAKE_CASE_ : str = "layer_norm" , SCREAMING_SNAKE_CASE_ : bool = False , ) -> Optional[int]: '''simple docstring''' super().__init__() A: str = only_cross_attention A: int = (num_embeds_ada_norm is not None) and norm_type == '''ada_norm_zero''' A: int = (num_embeds_ada_norm is not None) and norm_type == '''ada_norm''' if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None: raise ValueError( f"""`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to""" f""" define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}.""" ) # Define 3 blocks. Each block has its own normalization layer. # 1. Self-Attn if self.use_ada_layer_norm: A: Tuple = AdaLayerNorm(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) elif self.use_ada_layer_norm_zero: A: str = AdaLayerNormZero(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else: A: Tuple = nn.LayerNorm(SCREAMING_SNAKE_CASE_ , elementwise_affine=SCREAMING_SNAKE_CASE_ ) A: List[Any] = Attention( query_dim=SCREAMING_SNAKE_CASE_ , heads=SCREAMING_SNAKE_CASE_ , dim_head=SCREAMING_SNAKE_CASE_ , dropout=SCREAMING_SNAKE_CASE_ , bias=SCREAMING_SNAKE_CASE_ , cross_attention_dim=cross_attention_dim if only_cross_attention else None , upcast_attention=SCREAMING_SNAKE_CASE_ , ) # 2. Cross-Attn if cross_attention_dim is not None or double_self_attention: # We currently only use AdaLayerNormZero for self attention where there will only be one attention block. # I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during # the second cross attention block. A: Any = ( AdaLayerNorm(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if self.use_ada_layer_norm else nn.LayerNorm(SCREAMING_SNAKE_CASE_ , elementwise_affine=SCREAMING_SNAKE_CASE_ ) ) A: Optional[int] = Attention( query_dim=SCREAMING_SNAKE_CASE_ , cross_attention_dim=cross_attention_dim if not double_self_attention else None , heads=SCREAMING_SNAKE_CASE_ , dim_head=SCREAMING_SNAKE_CASE_ , dropout=SCREAMING_SNAKE_CASE_ , bias=SCREAMING_SNAKE_CASE_ , upcast_attention=SCREAMING_SNAKE_CASE_ , ) # is self-attn if encoder_hidden_states is none else: A: Any = None A: Optional[Any] = None # 3. Feed-forward A: str = nn.LayerNorm(SCREAMING_SNAKE_CASE_ , elementwise_affine=SCREAMING_SNAKE_CASE_ ) A: Dict = FeedForward(SCREAMING_SNAKE_CASE_ , dropout=SCREAMING_SNAKE_CASE_ , activation_fn=SCREAMING_SNAKE_CASE_ , final_dropout=SCREAMING_SNAKE_CASE_ ) # let chunk size default to None A: Tuple = None A: Tuple = 0 def _snake_case ( self : Dict , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : int ) -> Optional[Any]: '''simple docstring''' A: Tuple = chunk_size A: Tuple = dim def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : Optional[torch.FloatTensor] = None , SCREAMING_SNAKE_CASE_ : Optional[torch.FloatTensor] = None , SCREAMING_SNAKE_CASE_ : Optional[torch.FloatTensor] = None , SCREAMING_SNAKE_CASE_ : Optional[torch.LongTensor] = None , SCREAMING_SNAKE_CASE_ : Dict[str, Any] = None , SCREAMING_SNAKE_CASE_ : Optional[torch.LongTensor] = None , ) -> Union[str, Any]: '''simple docstring''' if self.use_ada_layer_norm: A: Optional[int] = self.norma(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) elif self.use_ada_layer_norm_zero: A , A , A , A , A: Union[str, Any] = self.norma( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , hidden_dtype=hidden_states.dtype ) else: A: Tuple = self.norma(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = cross_attention_kwargs if cross_attention_kwargs is not None else {} A: List[str] = self.attna( SCREAMING_SNAKE_CASE_ , encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None , attention_mask=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) if self.use_ada_layer_norm_zero: A: Optional[int] = gate_msa.unsqueeze(1 ) * attn_output A: Tuple = attn_output + hidden_states # 2. Cross-Attention if self.attna is not None: A: Optional[Any] = ( self.norma(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if self.use_ada_layer_norm else self.norma(SCREAMING_SNAKE_CASE_ ) ) A: int = self.attna( SCREAMING_SNAKE_CASE_ , encoder_hidden_states=SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) A: str = attn_output + hidden_states # 3. Feed-forward A: Dict = self.norma(SCREAMING_SNAKE_CASE_ ) if self.use_ada_layer_norm_zero: A: Optional[Any] = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None] if self._chunk_size is not None: # "feed_forward_chunk_size" can be used to save memory if norm_hidden_states.shape[self._chunk_dim] % self._chunk_size != 0: raise ValueError( f"""`hidden_states` dimension to be chunked: {norm_hidden_states.shape[self._chunk_dim]} has to be divisible by chunk size: {self._chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`.""" ) A: Tuple = norm_hidden_states.shape[self._chunk_dim] // self._chunk_size A: Dict = torch.cat( [self.ff(SCREAMING_SNAKE_CASE_ ) for hid_slice in norm_hidden_states.chunk(SCREAMING_SNAKE_CASE_ , dim=self._chunk_dim )] , dim=self._chunk_dim , ) else: A: Tuple = self.ff(SCREAMING_SNAKE_CASE_ ) if self.use_ada_layer_norm_zero: A: Dict = gate_mlp.unsqueeze(1 ) * ff_output A: Tuple = ff_output + hidden_states return hidden_states class lowerCAmelCase_ ( nn.Module ): '''simple docstring''' def __init__( self : Any , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : int = 4 , SCREAMING_SNAKE_CASE_ : float = 0.0 , SCREAMING_SNAKE_CASE_ : str = "geglu" , SCREAMING_SNAKE_CASE_ : bool = False , ) -> Tuple: '''simple docstring''' super().__init__() A: Optional[int] = int(dim * mult ) A: List[Any] = dim_out if dim_out is not None else dim if activation_fn == "gelu": A: List[Any] = GELU(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if activation_fn == "gelu-approximate": A: Optional[Any] = GELU(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , approximate='''tanh''' ) elif activation_fn == "geglu": A: Dict = GEGLU(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) elif activation_fn == "geglu-approximate": A: List[str] = ApproximateGELU(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: List[str] = nn.ModuleList([] ) # project in self.net.append(SCREAMING_SNAKE_CASE_ ) # project dropout self.net.append(nn.Dropout(SCREAMING_SNAKE_CASE_ ) ) # project out self.net.append(nn.Linear(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout if final_dropout: self.net.append(nn.Dropout(SCREAMING_SNAKE_CASE_ ) ) def _snake_case ( self : Any , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> List[str]: '''simple docstring''' for module in self.net: A: Tuple = module(SCREAMING_SNAKE_CASE_ ) return hidden_states class lowerCAmelCase_ ( nn.Module ): '''simple docstring''' def __init__( self : List[Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : str = "none" ) -> List[Any]: '''simple docstring''' super().__init__() A: int = nn.Linear(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Dict = approximate def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : List[Any] ) -> Tuple: '''simple docstring''' if gate.device.type != "mps": return F.gelu(SCREAMING_SNAKE_CASE_ , approximate=self.approximate ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) , approximate=self.approximate ).to(dtype=gate.dtype ) def _snake_case ( self : Dict , SCREAMING_SNAKE_CASE_ : str ) -> List[str]: '''simple docstring''' A: Dict = self.proj(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = self.gelu(SCREAMING_SNAKE_CASE_ ) return hidden_states class lowerCAmelCase_ ( nn.Module ): '''simple docstring''' def __init__( self : str , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ) -> Dict: '''simple docstring''' super().__init__() A: List[Any] = nn.Linear(SCREAMING_SNAKE_CASE_ , dim_out * 2 ) def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Optional[Any]: '''simple docstring''' if gate.device.type != "mps": return F.gelu(SCREAMING_SNAKE_CASE_ ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) ).to(dtype=gate.dtype ) def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : List[str] ) -> Optional[Any]: '''simple docstring''' A , A: Any = self.proj(SCREAMING_SNAKE_CASE_ ).chunk(2 , dim=-1 ) return hidden_states * self.gelu(SCREAMING_SNAKE_CASE_ ) class lowerCAmelCase_ ( nn.Module ): '''simple docstring''' def __init__( self : Any , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ) -> Union[str, Any]: '''simple docstring''' super().__init__() A: Dict = nn.Linear(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Tuple ) -> int: '''simple docstring''' A: str = self.proj(SCREAMING_SNAKE_CASE_ ) return x * torch.sigmoid(1.702 * x ) class lowerCAmelCase_ ( nn.Module ): '''simple docstring''' def __init__( self : List[str] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> List[str]: '''simple docstring''' super().__init__() A: List[str] = nn.Embedding(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = nn.SiLU() A: Tuple = nn.Linear(SCREAMING_SNAKE_CASE_ , embedding_dim * 2 ) A: Dict = nn.LayerNorm(SCREAMING_SNAKE_CASE_ , elementwise_affine=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Dict ) -> List[Any]: '''simple docstring''' A: str = self.linear(self.silu(self.emb(SCREAMING_SNAKE_CASE_ ) ) ) A , A: Dict = torch.chunk(SCREAMING_SNAKE_CASE_ , 2 ) A: List[str] = self.norm(SCREAMING_SNAKE_CASE_ ) * (1 + scale) + shift return x class lowerCAmelCase_ ( nn.Module ): '''simple docstring''' def __init__( self : Any , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' super().__init__() A: Dict = CombinedTimestepLabelEmbeddings(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Optional[int] = nn.SiLU() A: Any = nn.Linear(SCREAMING_SNAKE_CASE_ , 6 * embedding_dim , bias=SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = nn.LayerNorm(SCREAMING_SNAKE_CASE_ , elementwise_affine=SCREAMING_SNAKE_CASE_ , eps=1E-6 ) def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : List[str]=None ) -> Any: '''simple docstring''' A: int = self.linear(self.silu(self.emb(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , hidden_dtype=SCREAMING_SNAKE_CASE_ ) ) ) A , A , A , A , A , A: Dict = emb.chunk(6 , dim=1 ) A: int = self.norm(SCREAMING_SNAKE_CASE_ ) * (1 + scale_msa[:, None]) + shift_msa[:, None] return x, gate_msa, shift_mlp, scale_mlp, gate_mlp class lowerCAmelCase_ ( nn.Module ): '''simple docstring''' def __init__( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Optional[str] = None , SCREAMING_SNAKE_CASE_ : float = 1E-5 ) -> int: '''simple docstring''' super().__init__() A: Optional[int] = num_groups A: List[Any] = eps if act_fn is None: A: Dict = None else: A: str = get_activation(SCREAMING_SNAKE_CASE_ ) A: int = nn.Linear(SCREAMING_SNAKE_CASE_ , out_dim * 2 ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Any ) -> List[str]: '''simple docstring''' if self.act: A: Tuple = self.act(SCREAMING_SNAKE_CASE_ ) A: Dict = self.linear(SCREAMING_SNAKE_CASE_ ) A: Optional[int] = emb[:, :, None, None] A , A: Optional[Any] = emb.chunk(2 , dim=1 ) A: Any = F.group_norm(SCREAMING_SNAKE_CASE_ , self.num_groups , eps=self.eps ) A: Union[str, Any] = x * (1 + scale) + shift return x
319
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase , __lowercase = 0 ) -> list: A: Dict = length or len(__lowercase ) A: Dict = False for i in range(length - 1 ): if list_data[i] > list_data[i + 1]: A , A: Tuple = list_data[i + 1], list_data[i] A: Union[str, Any] = True return list_data if not swapped else bubble_sort(__lowercase , length - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
319
1
"""simple docstring""" import json import os import pickle import shutil import tempfile from unittest import TestCase from unittest.mock import patch import numpy as np from datasets import Dataset from transformers import is_faiss_available from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bart.tokenization_bart import BartTokenizer from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.dpr.tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.retrieval_rag import CustomHFIndex, RagRetriever from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_sentencepiece, require_tokenizers, require_torch if is_faiss_available(): import faiss @require_faiss class a__ ( UpperCamelCase_ ): def lowerCAmelCase_ ( self ) -> Optional[int]: '''simple docstring''' a = tempfile.mkdtemp() a = 8 # DPR tok a = [ """[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest""", ] a = os.path.join(self.tmpdirname , "dpr_tokenizer" ) os.makedirs(_a , exist_ok=_a ) a = os.path.join(_a , DPR_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] ) ) # BART tok a = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """\u0120""", """\u0120l""", """\u0120n""", """\u0120lo""", """\u0120low""", """er""", """\u0120lowest""", """\u0120newer""", """\u0120wider""", """<unk>""", ] a = dict(zip(_a , range(len(_a ) ) ) ) a = ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""] a = {"""unk_token""": """<unk>"""} a = os.path.join(self.tmpdirname , "bart_tokenizer" ) os.makedirs(_a , exist_ok=_a ) a = os.path.join(_a , BART_VOCAB_FILES_NAMES["vocab_file"] ) a = os.path.join(_a , BART_VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as fp: fp.write(json.dumps(_a ) + "\n" ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(_a ) ) def lowerCAmelCase_ ( self ) -> DPRQuestionEncoderTokenizer: '''simple docstring''' return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , "dpr_tokenizer" ) ) def lowerCAmelCase_ ( self ) -> DPRContextEncoderTokenizer: '''simple docstring''' return DPRContextEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , "dpr_tokenizer" ) ) def lowerCAmelCase_ ( self ) -> BartTokenizer: '''simple docstring''' return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , "bart_tokenizer" ) ) def lowerCAmelCase_ ( self ) -> Union[str, Any]: '''simple docstring''' shutil.rmtree(self.tmpdirname ) def lowerCAmelCase_ ( self ) -> Union[str, Any]: '''simple docstring''' a = Dataset.from_dict( { "id": ["0", "1"], "text": ["foo", "bar"], "title": ["Foo", "Bar"], "embeddings": [np.ones(self.retrieval_vector_size ), 2 * np.ones(self.retrieval_vector_size )], } ) dataset.add_faiss_index("embeddings" , string_factory="Flat" , metric_type=faiss.METRIC_INNER_PRODUCT ) return dataset def lowerCAmelCase_ ( self ) -> Tuple: '''simple docstring''' a = self.get_dummy_dataset() a = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , ) with patch("transformers.models.rag.retrieval_rag.load_dataset" ) as mock_load_dataset: a = dataset a = RagRetriever( _a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) return retriever def lowerCAmelCase_ ( self , A ) -> str: '''simple docstring''' a = self.get_dummy_dataset() a = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name="custom" , ) if from_disk: a = os.path.join(self.tmpdirname , "dataset" ) a = os.path.join(self.tmpdirname , "index.faiss" ) dataset.get_index("embeddings" ).save(os.path.join(self.tmpdirname , "index.faiss" ) ) dataset.drop_index("embeddings" ) dataset.save_to_disk(os.path.join(self.tmpdirname , "dataset" ) ) del dataset a = RagRetriever( _a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) else: a = RagRetriever( _a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , index=CustomHFIndex(config.retrieval_vector_size , _a ) , ) return retriever def lowerCAmelCase_ ( self ) -> Tuple: '''simple docstring''' a = Dataset.from_dict( { "id": ["0", "1"], "text": ["foo", "bar"], "title": ["Foo", "Bar"], "embeddings": [np.ones(self.retrieval_vector_size + 1 ), 2 * np.ones(self.retrieval_vector_size + 1 )], } ) dataset.add_faiss_index("embeddings" , string_factory="Flat" , metric_type=faiss.METRIC_INNER_PRODUCT ) a = os.path.join(self.tmpdirname , "hf_bert_base.hnswSQ8_correct_phi_128.c_index" ) dataset.save_faiss_index("embeddings" , index_file_name + ".index.dpr" ) pickle.dump(dataset["id"] , open(index_file_name + ".index_meta.dpr" , "wb" ) ) a = os.path.join(self.tmpdirname , "psgs_w100.tsv.pkl" ) a = {sample["""id"""]: [sample["""text"""], sample["""title"""]] for sample in dataset} pickle.dump(_a , open(_a , "wb" ) ) a = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name="legacy" , index_path=self.tmpdirname , ) a = RagRetriever( _a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() ) return retriever def lowerCAmelCase_ ( self ) -> List[str]: '''simple docstring''' a = 1 a = self.get_dummy_canonical_hf_index_retriever() a = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a = retriever.retrieve(_a , n_docs=_a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(_a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ["embeddings", "id", "text", "title"] ) self.assertEqual(len(doc_dicts[0]["id"] ) , _a ) self.assertEqual(doc_dicts[0]["id"][0] , "1" ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]["id"][0] , "0" ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def lowerCAmelCase_ ( self ) -> Any: '''simple docstring''' a = self.get_dummy_canonical_hf_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: with patch("transformers.models.rag.retrieval_rag.load_dataset" ) as mock_load_dataset: a = self.get_dummy_dataset() retriever.save_pretrained(_a ) a = RagRetriever.from_pretrained(_a ) self.assertIsInstance(_a , _a ) a = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a = retriever.retrieve(_a , n_docs=1 ) self.assertTrue(out is not None ) def lowerCAmelCase_ ( self ) -> Dict: '''simple docstring''' a = 1 a = self.get_dummy_custom_hf_index_retriever(from_disk=_a ) a = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a = retriever.retrieve(_a , n_docs=_a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(_a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ["embeddings", "id", "text", "title"] ) self.assertEqual(len(doc_dicts[0]["id"] ) , _a ) self.assertEqual(doc_dicts[0]["id"][0] , "1" ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]["id"][0] , "0" ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def lowerCAmelCase_ ( self ) -> str: '''simple docstring''' a = self.get_dummy_custom_hf_index_retriever(from_disk=_a ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(_a ) a = RagRetriever.from_pretrained(_a ) self.assertIsInstance(_a , _a ) a = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a = retriever.retrieve(_a , n_docs=1 ) self.assertTrue(out is not None ) def lowerCAmelCase_ ( self ) -> List[Any]: '''simple docstring''' a = 1 a = self.get_dummy_custom_hf_index_retriever(from_disk=_a ) a = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a = retriever.retrieve(_a , n_docs=_a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(_a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ["embeddings", "id", "text", "title"] ) self.assertEqual(len(doc_dicts[0]["id"] ) , _a ) self.assertEqual(doc_dicts[0]["id"][0] , "1" ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]["id"][0] , "0" ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def lowerCAmelCase_ ( self ) -> List[str]: '''simple docstring''' a = self.get_dummy_custom_hf_index_retriever(from_disk=_a ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(_a ) a = RagRetriever.from_pretrained(_a ) self.assertIsInstance(_a , _a ) a = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a = retriever.retrieve(_a , n_docs=1 ) self.assertTrue(out is not None ) def lowerCAmelCase_ ( self ) -> Dict: '''simple docstring''' a = 1 a = self.get_dummy_legacy_index_retriever() a = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a = retriever.retrieve(_a , n_docs=_a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(_a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ["text", "title"] ) self.assertEqual(len(doc_dicts[0]["text"] ) , _a ) self.assertEqual(doc_dicts[0]["text"][0] , "bar" ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]["text"][0] , "foo" ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def lowerCAmelCase_ ( self ) -> Dict: '''simple docstring''' a = self.get_dummy_legacy_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(_a ) a = RagRetriever.from_pretrained(_a ) self.assertIsInstance(_a , _a ) a = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a = retriever.retrieve(_a , n_docs=1 ) self.assertTrue(out is not None ) @require_torch @require_tokenizers @require_sentencepiece def lowerCAmelCase_ ( self ) -> int: '''simple docstring''' import torch a = 1 a = self.get_dummy_canonical_hf_index_retriever() a = [[5, 7], [10, 11]] a = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a = retriever(_a , _a , prefix=retriever.config.generator.prefix , n_docs=_a ) a = ( out["""context_input_ids"""], out["""context_attention_mask"""], out["""retrieved_doc_embeds"""], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(_a , _a ) self.assertIsInstance(_a , _a ) self.assertIsInstance(_a , np.ndarray ) a = retriever( _a , _a , prefix=retriever.config.generator.prefix , n_docs=_a , return_tensors="pt" , ) a = ( # noqa: F841 out["""context_input_ids"""], out["""context_attention_mask"""], out["""retrieved_doc_embeds"""], out["""doc_ids"""], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(_a , torch.Tensor ) self.assertIsInstance(_a , torch.Tensor ) self.assertIsInstance(_a , torch.Tensor ) @require_torch @require_tokenizers @require_sentencepiece def lowerCAmelCase_ ( self ) -> Optional[Any]: '''simple docstring''' a = self.get_dpr_ctx_encoder_tokenizer() a = 1 a = self.get_dummy_custom_hf_index_retriever(from_disk=_a ) retriever.set_ctx_encoder_tokenizer(_a ) a = [[5, 7], [10, 11]] a = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a = retriever(_a , _a , prefix=retriever.config.generator.prefix , n_docs=_a ) self.assertEqual( len(_a ) , 6 ) # check whether the retriever output consist of 6 attributes including tokenized docs self.assertEqual( all(k in out for k in ("tokenized_doc_ids", "tokenized_doc_attention_mask") ) , _a ) # check for doc token related keys in dictionary.
362
from manim import * class a__ ( UpperCamelCase__ ): def lowerCAmelCase_ ( self ) -> List[Any]: '''simple docstring''' a = Rectangle(height=0.5 , width=0.5 ) a = Rectangle(height=0.4_6 , width=0.4_6 ).set_stroke(width=0 ) a = Rectangle(height=0.2_5 , width=0.2_5 ) a = [mem.copy() for i in range(6 )] a = [mem.copy() for i in range(6 )] a = VGroup(*A ).arrange(A , buff=0 ) a = VGroup(*A ).arrange(A , buff=0 ) a = VGroup(A , A ).arrange(A , buff=0 ) a = Text("CPU" , font_size=24 ) a = Group(A , A ).arrange(A , buff=0.5 , aligned_edge=A ) cpu.move_to([-2.5, -0.5, 0] ) self.add(A ) a = [mem.copy() for i in range(4 )] a = VGroup(*A ).arrange(A , buff=0 ) a = Text("GPU" , font_size=24 ) a = Group(A , A ).arrange(A , buff=0.5 , aligned_edge=A ) gpu.move_to([-1, -1, 0] ) self.add(A ) a = [mem.copy() for i in range(6 )] a = VGroup(*A ).arrange(A , buff=0 ) a = Text("Model" , font_size=24 ) a = Group(A , A ).arrange(A , buff=0.5 , aligned_edge=A ) model.move_to([3, -1.0, 0] ) self.add(A ) a = [] a = [] for i, rect in enumerate(A ): a = fill.copy().set_fill(A , opacity=0.8 ) target.move_to(A ) model_arr.append(A ) a = Rectangle(height=0.4_6 , width=0.4_6 ).set_stroke(width=0.0 ).set_fill(A , opacity=0.8 ) cpu_target.move_to(cpu_left_col_base[i] ) model_cpu_arr.append(A ) self.add(*A , *A ) a = [meta_mem.copy() for i in range(6 )] a = [meta_mem.copy() for i in range(6 )] a = VGroup(*A ).arrange(A , buff=0 ) a = VGroup(*A ).arrange(A , buff=0 ) a = VGroup(A , A ).arrange(A , buff=0 ) a = Text("Disk" , font_size=24 ) a = Group(A , A ).arrange(A , buff=0.5 , aligned_edge=A ) disk.move_to([-4, -1.2_5, 0] ) self.add(A , A ) a = Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) a = MarkupText( F'''<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model''' , font_size=18 , ) key_text.move_to([-5, 2.4, 0] ) self.add(A , A ) a = MarkupText( F'''<span fgcolor=\'{BLUE}\'>●</span> Checkpoint''' , font_size=18 , ) blue_text.next_to(A , DOWN * 2.4 , aligned_edge=key_text.get_left() ) self.add(A ) a = MarkupText( F'''Now watch as an input is passed through the model\nand how the memory is utilized and handled.''' , font_size=24 , ) step_a.move_to([2, 2, 0] ) self.play(Write(A ) ) a = Square(0.3 ) input.set_fill(A , opacity=1.0 ) input.set_stroke(width=0.0 ) input.next_to(model_base[0] , A , buff=0.5 ) self.play(Write(A ) ) input.generate_target() input.target.next_to(model_arr[0] , direction=A , buff=0.0_2 ) self.play(MoveToTarget(A ) ) self.play(FadeOut(A ) ) a = Arrow(start=A , end=A , color=A , buff=0.5 ) a.next_to(model_arr[0].get_left() , A , buff=0.2 ) model_cpu_arr[0].generate_target() model_cpu_arr[0].target.move_to(gpu_rect[0] ) a = MarkupText( F'''As the input reaches a layer, the hook triggers\nand weights are moved from the CPU\nto the GPU and back.''' , font_size=24 , ) step_a.move_to([2, 2, 0] ) self.play(Write(A , run_time=3 ) ) a = {"run_time": 1, "fade_in": True, "fade_out": True, "buff": 0.0_2} self.play( Write(A ) , Circumscribe(model_arr[0] , color=A , **A ) , Circumscribe(model_cpu_arr[0] , color=A , **A ) , Circumscribe(gpu_rect[0] , color=A , **A ) , ) self.play(MoveToTarget(model_cpu_arr[0] ) ) a = a.copy() for i in range(6 ): a_c.next_to(model_arr[i].get_right() + 0.0_2 , A , buff=0.2 ) input.generate_target() input.target.move_to(model_arr[i].get_right() + 0.0_2 ) a = AnimationGroup( FadeOut(A , run_time=0.5 ) , MoveToTarget(A , run_time=0.5 ) , FadeIn(A , run_time=0.5 ) , lag_ratio=0.2 ) self.play(A ) model_cpu_arr[i].generate_target() model_cpu_arr[i].target.move_to(cpu_left_col_base[i] ) if i < 5: model_cpu_arr[i + 1].generate_target() model_cpu_arr[i + 1].target.move_to(gpu_rect[0] ) if i >= 1: a = 0.7 self.play( Circumscribe(model_arr[i] , **A ) , Circumscribe(cpu_left_col_base[i] , **A ) , Circumscribe(cpu_left_col_base[i + 1] , color=A , **A ) , Circumscribe(gpu_rect[0] , color=A , **A ) , Circumscribe(model_arr[i + 1] , color=A , **A ) , ) if i < 1: self.play( MoveToTarget(model_cpu_arr[i] ) , MoveToTarget(model_cpu_arr[i + 1] ) , ) else: self.play( MoveToTarget(model_cpu_arr[i] , run_time=0.7 ) , MoveToTarget(model_cpu_arr[i + 1] , run_time=0.7 ) , ) else: model_cpu_arr[i].generate_target() model_cpu_arr[i].target.move_to(cpu_left_col_base[-1] ) input.generate_target() input.target.next_to(model_arr[-1].get_right() , RIGHT + 0.0_2 , buff=0.2 ) self.play( Circumscribe(model_arr[-1] , color=A , **A ) , Circumscribe(cpu_left_col_base[-1] , color=A , **A ) , Circumscribe(gpu_rect[0] , color=A , **A ) , ) self.play(MoveToTarget(model_cpu_arr[i] ) ) a = a_c a = a_c.copy() input.generate_target() input.target.next_to(model_base[-1] , RIGHT + 0.0_2 , buff=0.5 ) self.play( FadeOut(A ) , FadeOut(A , run_time=0.5 ) , ) a = MarkupText(F'''Inference on a model too large for GPU memory\nis successfully completed.''' , font_size=24 ) step_a.move_to([2, 2, 0] ) self.play(Write(A , run_time=3 ) , MoveToTarget(A ) ) self.wait()
180
0
from datetime import datetime as dt import os from github import Github SCREAMING_SNAKE_CASE :int = [ 'good first issue', 'good second issue', 'good difficult issue', 'feature request', 'new model', 'wip', ] def UpperCAmelCase ( ) -> Dict: """simple docstring""" __A = Github(os.environ["GITHUB_TOKEN"] ) __A = g.get_repo("huggingface/transformers" ) __A = repo.get_issues(state="open" ) for issue in open_issues: __A = sorted([comment for comment in issue.get_comments()] , key=lambda a_ : i.created_at , reverse=a_ ) __A = comments[0] if len(a_ ) > 0 else None if ( last_comment is not None and last_comment.user.login == "github-actions[bot]" and (dt.utcnow() - issue.updated_at).days > 7 and (dt.utcnow() - issue.created_at).days >= 3_0 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # print(f"Would close issue {issue.number} since it has been 7 days of inactivity since bot mention.") issue.edit(state="closed" ) elif ( (dt.utcnow() - issue.updated_at).days > 2_3 and (dt.utcnow() - issue.created_at).days >= 3_0 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # print(f"Would add stale comment to {issue.number}") issue.create_comment( "This issue has been automatically marked as stale because it has not had " "recent activity. If you think this still needs to be addressed " "please comment on this thread.\n\nPlease note that issues that do not follow the " "[contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) " "are likely to be ignored." ) if __name__ == "__main__": main()
15
import numpy as np import pandas as pd from sklearn.preprocessing import Normalizer from sklearn.svm import SVR from statsmodels.tsa.statespace.sarimax import SARIMAX def UpperCamelCase ( __magic_name__ : list , __magic_name__ : list , __magic_name__ : list , __magic_name__ : list , __magic_name__ : list ) -> float: """simple docstring""" lowercase__ = np.array([[1, item, train_mtch[i]] for i, item in enumerate(__magic_name__ )] ) lowercase__ = np.array(__magic_name__ ) lowercase__ = np.dot(np.dot(np.linalg.inv(np.dot(x.transpose() , __magic_name__ ) ) , x.transpose() ) , __magic_name__ ) return abs(beta[0] + test_dt[0] * beta[1] + test_mtch[0] + beta[2] ) def UpperCamelCase ( __magic_name__ : list , __magic_name__ : list , __magic_name__ : list ) -> float: """simple docstring""" lowercase__ = (1, 2, 1) lowercase__ = (1, 1, 0, 7) lowercase__ = SARIMAX( __magic_name__ , exog=__magic_name__ , order=__magic_name__ , seasonal_order=__magic_name__ ) lowercase__ = model.fit(disp=__magic_name__ , maxiter=600 , method="""nm""" ) lowercase__ = model_fit.predict(1 , len(__magic_name__ ) , exog=[test_match] ) return result[0] def UpperCamelCase ( __magic_name__ : list , __magic_name__ : list , __magic_name__ : list ) -> float: """simple docstring""" lowercase__ = SVR(kernel="""rbf""" , C=1 , gamma=0.1 , epsilon=0.1 ) regressor.fit(__magic_name__ , __magic_name__ ) lowercase__ = regressor.predict(__magic_name__ ) return y_pred[0] def UpperCamelCase ( __magic_name__ : list ) -> float: """simple docstring""" train_user.sort() lowercase__ = np.percentile(__magic_name__ , 25 ) lowercase__ = np.percentile(__magic_name__ , 75 ) lowercase__ = qa - qa lowercase__ = qa - (iqr * 0.1) return low_lim def UpperCamelCase ( __magic_name__ : list , __magic_name__ : float ) -> bool: """simple docstring""" lowercase__ = 0 lowercase__ = 0 for i in list_vote: if i > actual_result: lowercase__ = not_safe + 1 else: if abs(abs(__magic_name__ ) - abs(__magic_name__ ) ) <= 0.1: safe += 1 else: not_safe += 1 return safe > not_safe if __name__ == "__main__": # data_input_df = pd.read_csv("ex_data.csv", header=None) A : Dict = [[1_8_2_3_1, 0.0, 1], [2_2_6_2_1, 1.0, 2], [1_5_6_7_5, 0.0, 3], [2_3_5_8_3, 1.0, 4]] A : str = pd.DataFrame( data_input, columns=['total_user', 'total_even', 'days'] ) A : Any = Normalizer().fit_transform(data_input_df.values) # split data A : Optional[int] = normalize_df[:, 2].tolist() A : Any = normalize_df[:, 0].tolist() A : str = normalize_df[:, 1].tolist() # for svr (input variable = total date and total match) A : int = normalize_df[:, [1, 2]].tolist() A : Any = x[: len(x) - 1] A : Tuple = x[len(x) - 1 :] # for linear regression & sarimax A : Optional[int] = total_date[: len(total_date) - 1] A : Optional[int] = total_user[: len(total_user) - 1] A : str = total_match[: len(total_match) - 1] A : Union[str, Any] = total_date[len(total_date) - 1 :] A : List[str] = total_user[len(total_user) - 1 :] A : str = total_match[len(total_match) - 1 :] # voting system with forecasting A : int = [ linear_regression_prediction( trn_date, trn_user, trn_match, tst_date, tst_match ), sarimax_predictor(trn_user, trn_match, tst_match), support_vector_regressor(x_train, x_test, trn_user), ] # check the safety of today's data A : int = '' if data_safety_checker(res_vote, tst_user) else 'not ' print('Today\'s data is {not_str}safe.')
305
0
import colorsys from PIL import Image # type: ignore def __SCREAMING_SNAKE_CASE ( __UpperCamelCase : float , __UpperCamelCase : float , __UpperCamelCase : int ) -> float: """simple docstring""" SCREAMING_SNAKE_CASE__ = x SCREAMING_SNAKE_CASE__ = y for step in range(__UpperCamelCase ): # noqa: B007 SCREAMING_SNAKE_CASE__ = a * a - b * b + x SCREAMING_SNAKE_CASE__ = 2 * a * b + y SCREAMING_SNAKE_CASE__ = 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 __SCREAMING_SNAKE_CASE ( __UpperCamelCase : float ) -> tuple: """simple docstring""" if distance == 1: return (0, 0, 0) else: return (2_55, 2_55, 2_55) def __SCREAMING_SNAKE_CASE ( __UpperCamelCase : float ) -> tuple: """simple docstring""" 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 __SCREAMING_SNAKE_CASE ( __UpperCamelCase : int = 8_00 , __UpperCamelCase : int = 6_00 , __UpperCamelCase : float = -0.6 , __UpperCamelCase : float = 0 , __UpperCamelCase : float = 3.2 , __UpperCamelCase : int = 50 , __UpperCamelCase : bool = True , ) -> Image.Image: """simple docstring""" SCREAMING_SNAKE_CASE__ = Image.new("""RGB""" , (image_width, image_height) ) SCREAMING_SNAKE_CASE__ = 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__ = figure_width / image_width * image_height SCREAMING_SNAKE_CASE__ = figure_center_x + (image_x / image_width - 0.5) * figure_width SCREAMING_SNAKE_CASE__ = figure_center_y + (image_y / image_height - 0.5) * figure_height SCREAMING_SNAKE_CASE__ = get_distance(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) # color the corresponding pixel based on the selected coloring-function if use_distance_color_coding: SCREAMING_SNAKE_CASE__ = get_color_coded_rgb(__UpperCamelCase ) else: SCREAMING_SNAKE_CASE__ = get_black_and_white_rgb(__UpperCamelCase ) return img if __name__ == "__main__": import doctest doctest.testmod() # colored version, full figure __lowerCamelCase : int = 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()
204
import contextlib import importlib import io import unittest import transformers # Try to import everything from transformers to ensure every object can be loaded. from transformers import * # noqa F406 from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, require_tf, require_torch from transformers.utils import ContextManagers, find_labels, is_flax_available, is_tf_available, is_torch_available if is_torch_available(): from transformers import BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification if is_tf_available(): from transformers import TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification if is_flax_available(): from transformers import FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification __lowerCamelCase : Tuple = DUMMY_UNKNOWN_IDENTIFIER # An actual model hosted on huggingface.co __lowerCamelCase : Optional[Any] = '''main''' # Default branch name __lowerCamelCase : Optional[int] = '''f2c752cfc5c0ab6f4bdec59acea69eefbee381c2''' # One particular commit (not the top of `main`) __lowerCamelCase : Any = '''aaaaaaa''' # This commit does not exist, so we should 404. __lowerCamelCase : List[str] = '''d9e9f15bc825e4b2c9249e9578f884bbcb5e3684''' # Sha-1 of config.json on the top of `main`, for checking purposes __lowerCamelCase : List[Any] = '''4b243c475af8d0a7754e87d7d096c92e5199ec2fe168a2ee7998e3b8e9bcb1d3''' @contextlib.contextmanager def __SCREAMING_SNAKE_CASE ( ) -> str: """simple docstring""" print("""Welcome!""" ) yield print("""Bye!""" ) @contextlib.contextmanager def __SCREAMING_SNAKE_CASE ( ) -> Any: """simple docstring""" print("""Bonjour!""" ) yield print("""Au revoir!""" ) class __snake_case ( unittest.TestCase ): def __a ( self : List[Any] ): """simple docstring""" assert transformers.__spec__ is not None assert importlib.util.find_spec("""transformers""" ) is not None class __snake_case ( unittest.TestCase ): @unittest.mock.patch("""sys.stdout""" , new_callable=io.StringIO ) def __a ( self : List[str] , _lowercase : str ): """simple docstring""" with ContextManagers([] ): print("""Transformers are awesome!""" ) # The print statement adds a new line at the end of the output self.assertEqual(mock_stdout.getvalue() , """Transformers are awesome!\n""" ) @unittest.mock.patch("""sys.stdout""" , new_callable=io.StringIO ) def __a ( self : Optional[Any] , _lowercase : str ): """simple docstring""" with ContextManagers([context_en()] ): print("""Transformers are awesome!""" ) # The output should be wrapped with an English welcome and goodbye self.assertEqual(mock_stdout.getvalue() , """Welcome!\nTransformers are awesome!\nBye!\n""" ) @unittest.mock.patch("""sys.stdout""" , new_callable=io.StringIO ) def __a ( self : Tuple , _lowercase : Dict ): """simple docstring""" with ContextManagers([context_fr(), context_en()] ): print("""Transformers are awesome!""" ) # The output should be wrapped with an English and French welcome and goodbye self.assertEqual(mock_stdout.getvalue() , """Bonjour!\nWelcome!\nTransformers are awesome!\nBye!\nAu revoir!\n""" ) @require_torch def __a ( self : Union[str, Any] ): """simple docstring""" self.assertEqual(find_labels(_lowercase ) , ["""labels"""] ) self.assertEqual(find_labels(_lowercase ) , ["""labels""", """next_sentence_label"""] ) self.assertEqual(find_labels(_lowercase ) , ["""start_positions""", """end_positions"""] ) class __snake_case ( lowerCamelCase_ ): pass self.assertEqual(find_labels(_lowercase ) , ["""labels"""] ) @require_tf def __a ( self : Any ): """simple docstring""" self.assertEqual(find_labels(_lowercase ) , ["""labels"""] ) self.assertEqual(find_labels(_lowercase ) , ["""labels""", """next_sentence_label"""] ) self.assertEqual(find_labels(_lowercase ) , ["""start_positions""", """end_positions"""] ) class __snake_case ( lowerCamelCase_ ): pass self.assertEqual(find_labels(_lowercase ) , ["""labels"""] ) @require_flax def __a ( self : Union[str, Any] ): """simple docstring""" self.assertEqual(find_labels(_lowercase ) , [] ) self.assertEqual(find_labels(_lowercase ) , [] ) self.assertEqual(find_labels(_lowercase ) , [] ) class __snake_case ( lowerCamelCase_ ): pass self.assertEqual(find_labels(_lowercase ) , [] )
204
1
def UpperCamelCase( __UpperCamelCase : int ): lowerCAmelCase_ : Union[str, Any] = int(__UpperCamelCase ) if decimal in (0, 1): # Exit cases for the recursion return str(__UpperCamelCase ) lowerCAmelCase_ , lowerCAmelCase_ : Tuple = divmod(__UpperCamelCase ,2 ) return binary_recursive(__UpperCamelCase ) + str(__UpperCamelCase ) def UpperCamelCase( __UpperCamelCase : str ): lowerCAmelCase_ : str = str(__UpperCamelCase ).strip() if not number: raise ValueError('''No input value was provided''' ) lowerCAmelCase_ : List[Any] = '''-''' if number.startswith('''-''' ) else '''''' lowerCAmelCase_ : List[str] = number.lstrip('''-''' ) if not number.isnumeric(): raise ValueError('''Input value is not an integer''' ) return f"""{negative}0b{binary_recursive(int(__UpperCamelCase ) )}""" if __name__ == "__main__": from doctest import testmod testmod()
103
def snake_case_ ( snake_case , snake_case ) -> str: if number < 0 or shift_amount < 0: raise ValueError('both inputs must be positive integers' ) lowercase__: str = str(bin(snake_case ) ) binary_number += "0" * shift_amount return binary_number def snake_case_ ( snake_case , snake_case ) -> str: if number < 0 or shift_amount < 0: raise ValueError('both inputs must be positive integers' ) lowercase__: Optional[Any] = str(bin(snake_case ) )[2:] if shift_amount >= len(snake_case ): return "0b0" lowercase__: Optional[int] = binary_number[: len(snake_case ) - shift_amount] return "0b" + shifted_binary_number def snake_case_ ( snake_case , snake_case ) -> str: if number >= 0: # Get binary representation of positive number lowercase__: Union[str, Any] = '0' + str(bin(snake_case ) ).strip('-' )[2:] else: # Get binary (2's complement) representation of negative number lowercase__: Dict = len(bin(snake_case )[3:] ) # Find 2's complement of number lowercase__: int = bin(abs(snake_case ) - (1 << binary_number_length) )[3:] lowercase__: Any = ( '1' + '0' * (binary_number_length - len(snake_case )) + binary_number ) if shift_amount >= len(snake_case ): return "0b" + binary_number[0] * len(snake_case ) return ( "0b" + binary_number[0] * shift_amount + binary_number[: len(snake_case ) - shift_amount] ) if __name__ == "__main__": import doctest doctest.testmod()
196
0
"""simple docstring""" from math import isqrt def __UpperCAmelCase ( snake_case_ : int ) -> list[int]: """simple docstring""" _lowerCAmelCase = [True] * max_number for i in range(2 , isqrt(max_number - 1 ) + 1 ): if is_prime[i]: for j in range(i**2 , snake_case_ , snake_case_ ): _lowerCAmelCase = False return [i for i in range(2 , snake_case_ ) if is_prime[i]] def __UpperCAmelCase ( snake_case_ : int = 10**8 ) -> int: """simple docstring""" _lowerCAmelCase = calculate_prime_numbers(max_number // 2 ) _lowerCAmelCase = 0 _lowerCAmelCase = 0 _lowerCAmelCase = len(snake_case_ ) - 1 while left <= right: while prime_numbers[left] * prime_numbers[right] >= max_number: right -= 1 semiprimes_count += right - left + 1 left += 1 return semiprimes_count if __name__ == "__main__": print(F'{solution() = }')
362
"""simple docstring""" import glob import os import random from string import ascii_lowercase, digits import cva import numpy as np # Parrameters SCREAMING_SNAKE_CASE : Any = (7_2_0, 1_2_8_0) # Height, Width SCREAMING_SNAKE_CASE : List[str] = (0.4, 0.6) # if height or width lower than this scale, drop it. SCREAMING_SNAKE_CASE : List[Any] = 1 / 1_0_0 SCREAMING_SNAKE_CASE : Optional[Any] = '''''' SCREAMING_SNAKE_CASE : Dict = '''''' SCREAMING_SNAKE_CASE : List[Any] = '''''' SCREAMING_SNAKE_CASE : Dict = 2_5_0 def __UpperCAmelCase ( ) -> None: """simple docstring""" _lowerCAmelCase , _lowerCAmelCase = get_dataset(snake_case_ , snake_case_ ) for index in range(snake_case_ ): _lowerCAmelCase = random.sample(range(len(snake_case_ ) ) , 4 ) _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase = update_image_and_anno( snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , filter_scale=snake_case_ , ) # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' _lowerCAmelCase = random_chars(32 ) _lowerCAmelCase = path.split(os.sep )[-1].rsplit(""".""" , 1 )[0] _lowerCAmelCase = F"""{OUTPUT_DIR}/{file_name}_MOSAIC_{letter_code}""" cva.imwrite(F"""{file_root}.jpg""" , snake_case_ , [cva.IMWRITE_JPEG_QUALITY, 85] ) print(F"""Succeeded {index+1}/{NUMBER_IMAGES} with {file_name}""" ) _lowerCAmelCase = [] for anno in new_annos: _lowerCAmelCase = anno[3] - anno[1] _lowerCAmelCase = anno[4] - anno[2] _lowerCAmelCase = anno[1] + width / 2 _lowerCAmelCase = anno[2] + height / 2 _lowerCAmelCase = F"""{anno[0]} {x_center} {y_center} {width} {height}""" annos_list.append(snake_case_ ) with open(F"""{file_root}.txt""" , """w""" ) as outfile: outfile.write("""\n""".join(line for line in annos_list ) ) def __UpperCAmelCase ( snake_case_ : str , snake_case_ : str ) -> tuple[list, list]: """simple docstring""" _lowerCAmelCase = [] _lowerCAmelCase = [] for label_file in glob.glob(os.path.join(snake_case_ , """*.txt""" ) ): _lowerCAmelCase = label_file.split(os.sep )[-1].rsplit(""".""" , 1 )[0] with open(snake_case_ ) as in_file: _lowerCAmelCase = in_file.readlines() _lowerCAmelCase = os.path.join(snake_case_ , F"""{label_name}.jpg""" ) _lowerCAmelCase = [] for obj_list in obj_lists: _lowerCAmelCase = obj_list.rstrip("""\n""" ).split(""" """ ) _lowerCAmelCase = float(obj[1] ) - float(obj[3] ) / 2 _lowerCAmelCase = float(obj[2] ) - float(obj[4] ) / 2 _lowerCAmelCase = float(obj[1] ) + float(obj[3] ) / 2 _lowerCAmelCase = float(obj[2] ) + float(obj[4] ) / 2 boxes.append([int(obj[0] ), xmin, ymin, xmax, ymax] ) if not boxes: continue img_paths.append(snake_case_ ) labels.append(snake_case_ ) return img_paths, labels def __UpperCAmelCase ( snake_case_ : list , snake_case_ : list , snake_case_ : list[int] , snake_case_ : tuple[int, int] , snake_case_ : tuple[float, float] , snake_case_ : float = 0.0 , ) -> tuple[list, list, str]: """simple docstring""" _lowerCAmelCase = np.zeros([output_size[0], output_size[1], 3] , dtype=np.uinta ) _lowerCAmelCase = scale_range[0] + random.random() * (scale_range[1] - scale_range[0]) _lowerCAmelCase = scale_range[0] + random.random() * (scale_range[1] - scale_range[0]) _lowerCAmelCase = int(scale_x * output_size[1] ) _lowerCAmelCase = int(scale_y * output_size[0] ) _lowerCAmelCase = [] _lowerCAmelCase = [] for i, index in enumerate(snake_case_ ): _lowerCAmelCase = all_img_list[index] path_list.append(snake_case_ ) _lowerCAmelCase = all_annos[index] _lowerCAmelCase = cva.imread(snake_case_ ) if i == 0: # top-left _lowerCAmelCase = cva.resize(snake_case_ , (divid_point_x, divid_point_y) ) _lowerCAmelCase = img for bbox in img_annos: _lowerCAmelCase = bbox[1] * scale_x _lowerCAmelCase = bbox[2] * scale_y _lowerCAmelCase = bbox[3] * scale_x _lowerCAmelCase = bbox[4] * scale_y new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) elif i == 1: # top-right _lowerCAmelCase = cva.resize(snake_case_ , (output_size[1] - divid_point_x, divid_point_y) ) _lowerCAmelCase = img for bbox in img_annos: _lowerCAmelCase = scale_x + bbox[1] * (1 - scale_x) _lowerCAmelCase = bbox[2] * scale_y _lowerCAmelCase = scale_x + bbox[3] * (1 - scale_x) _lowerCAmelCase = bbox[4] * scale_y new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) elif i == 2: # bottom-left _lowerCAmelCase = cva.resize(snake_case_ , (divid_point_x, output_size[0] - divid_point_y) ) _lowerCAmelCase = img for bbox in img_annos: _lowerCAmelCase = bbox[1] * scale_x _lowerCAmelCase = scale_y + bbox[2] * (1 - scale_y) _lowerCAmelCase = bbox[3] * scale_x _lowerCAmelCase = scale_y + bbox[4] * (1 - scale_y) new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) else: # bottom-right _lowerCAmelCase = cva.resize( snake_case_ , (output_size[1] - divid_point_x, output_size[0] - divid_point_y) ) _lowerCAmelCase = img for bbox in img_annos: _lowerCAmelCase = scale_x + bbox[1] * (1 - scale_x) _lowerCAmelCase = scale_y + bbox[2] * (1 - scale_y) _lowerCAmelCase = scale_x + bbox[3] * (1 - scale_x) _lowerCAmelCase = scale_y + bbox[4] * (1 - scale_y) new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) # Remove bounding box small than scale of filter if filter_scale > 0: _lowerCAmelCase = [ anno for anno in new_anno if filter_scale < (anno[3] - anno[1]) and filter_scale < (anno[4] - anno[2]) ] return output_img, new_anno, path_list[0] def __UpperCAmelCase ( snake_case_ : int ) -> str: """simple docstring""" assert number_char > 1, "The number of character should greater than 1" _lowerCAmelCase = ascii_lowercase + digits return "".join(random.choice(snake_case_ ) for _ in range(snake_case_ ) ) if __name__ == "__main__": main() print('''DONE ✅''')
317
0
"""simple docstring""" from queue import PriorityQueue from typing import Any import numpy as np def snake_case__ ( __lowerCamelCase : dict , __lowerCamelCase : str , __lowerCamelCase : set , __lowerCamelCase : set , __lowerCamelCase : dict , __lowerCamelCase : dict , __lowerCamelCase : PriorityQueue , __lowerCamelCase : dict , __lowerCamelCase : float | int , ): """simple docstring""" for nxt, d in graph[v]: if nxt in visited_forward: continue lowerCamelCase__ : Optional[Any] =cst_fwd.get(__lowerCamelCase , np.inf ) lowerCamelCase__ : List[str] =cst_fwd[v] + d if new_cost_f < old_cost_f: queue.put((new_cost_f, nxt) ) lowerCamelCase__ : Tuple =new_cost_f lowerCamelCase__ : Optional[Any] =v if nxt in visited_backward: if cst_fwd[v] + d + cst_bwd[nxt] < shortest_distance: lowerCamelCase__ : Optional[Any] =cst_fwd[v] + d + cst_bwd[nxt] return shortest_distance def snake_case__ ( __lowerCamelCase : str , __lowerCamelCase : str , __lowerCamelCase : dict , __lowerCamelCase : dict ): """simple docstring""" lowerCamelCase__ : Union[str, Any] =-1 lowerCamelCase__ : List[Any] =set() lowerCamelCase__ : Any =set() lowerCamelCase__ : Any ={source: 0} lowerCamelCase__ : Any ={destination: 0} lowerCamelCase__ : List[Any] ={source: None} lowerCamelCase__ : int ={destination: None} lowerCamelCase__ : PriorityQueue[Any] =PriorityQueue() lowerCamelCase__ : PriorityQueue[Any] =PriorityQueue() lowerCamelCase__ : List[Any] =np.inf queue_forward.put((0, source) ) queue_backward.put((0, destination) ) if source == destination: return 0 while not queue_forward.empty() and not queue_backward.empty(): lowerCamelCase__ , lowerCamelCase__ : Tuple =queue_forward.get() visited_forward.add(__lowerCamelCase ) lowerCamelCase__ , lowerCamelCase__ : Optional[Any] =queue_backward.get() visited_backward.add(__lowerCamelCase ) lowerCamelCase__ : Union[str, Any] =pass_and_relaxation( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , ) lowerCamelCase__ : Optional[int] =pass_and_relaxation( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , ) if cst_fwd[v_fwd] + cst_bwd[v_bwd] >= shortest_distance: break if shortest_distance != np.inf: lowerCamelCase__ : int =shortest_distance return shortest_path_distance _lowercase : Union[str, Any] = { "B": [["C", 1]], "C": [["D", 1]], "D": [["F", 1]], "E": [["B", 1], ["G", 2]], "F": [], "G": [["F", 1]], } _lowercase : Any = { "B": [["E", 1]], "C": [["B", 1]], "D": [["C", 1]], "F": [["D", 1], ["G", 1]], "E": [[None, np.inf]], "G": [["E", 2]], } if __name__ == "__main__": import doctest doctest.testmod()
238
"""simple docstring""" from typing import List, Optional, Tuple, Union import torch from ...utils import logging, randn_tensor from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline _lowercase : int = logging.get_logger(__name__) # pylint: disable=invalid-name class __SCREAMING_SNAKE_CASE ( lowerCAmelCase_ ): '''simple docstring''' def __init__( self : List[Any], lowerCamelCase : Union[str, Any], lowerCamelCase : Dict )-> Optional[int]: super().__init__() self.register_modules(unet=lowerCamelCase, scheduler=lowerCamelCase ) @torch.no_grad() def __call__( self : Optional[int], lowerCamelCase : int = 1, lowerCamelCase : int = 100, lowerCamelCase : Optional[Union[torch.Generator, List[torch.Generator]]] = None, lowerCamelCase : Optional[float] = None, lowerCamelCase : bool = True, )-> Union[AudioPipelineOutput, Tuple]: if audio_length_in_s is None: lowerCamelCase__ : int =self.unet.config.sample_size / self.unet.config.sample_rate lowerCamelCase__ : List[str] =audio_length_in_s * self.unet.config.sample_rate lowerCamelCase__ : Any =2 ** len(self.unet.up_blocks ) if sample_size < 3 * down_scale_factor: raise ValueError( F'''{audio_length_in_s} is too small. Make sure it\'s bigger or equal to''' F''' {3 * down_scale_factor / self.unet.config.sample_rate}.''' ) lowerCamelCase__ : Optional[int] =int(lowerCamelCase ) if sample_size % down_scale_factor != 0: lowerCamelCase__ : Tuple =( (audio_length_in_s * self.unet.config.sample_rate) // down_scale_factor + 1 ) * down_scale_factor logger.info( F'''{audio_length_in_s} is increased to {sample_size / self.unet.config.sample_rate} so that it can be handled''' F''' by the model. It will be cut to {original_sample_size / self.unet.config.sample_rate} after the denoising''' ''' process.''' ) lowerCamelCase__ : int =int(lowerCamelCase ) lowerCamelCase__ : str =next(iter(self.unet.parameters() ) ).dtype lowerCamelCase__ : Union[str, Any] =(batch_size, self.unet.config.in_channels, sample_size) if isinstance(lowerCamelCase, lowerCamelCase ) and len(lowerCamelCase ) != batch_size: raise ValueError( F'''You have passed a list of generators of length {len(lowerCamelCase )}, but requested an effective batch''' F''' size of {batch_size}. Make sure the batch size matches the length of the generators.''' ) lowerCamelCase__ : str =randn_tensor(lowerCamelCase, generator=lowerCamelCase, device=self.device, dtype=lowerCamelCase ) # set step values self.scheduler.set_timesteps(lowerCamelCase, device=audio.device ) lowerCamelCase__ : Any =self.scheduler.timesteps.to(lowerCamelCase ) for t in self.progress_bar(self.scheduler.timesteps ): # 1. predict noise model_output lowerCamelCase__ : str =self.unet(lowerCamelCase, lowerCamelCase ).sample # 2. compute previous image: x_t -> t_t-1 lowerCamelCase__ : Dict =self.scheduler.step(lowerCamelCase, lowerCamelCase, lowerCamelCase ).prev_sample lowerCamelCase__ : Optional[int] =audio.clamp(-1, 1 ).float().cpu().numpy() lowerCamelCase__ : List[Any] =audio[:, :, :original_sample_size] if not return_dict: return (audio,) return AudioPipelineOutput(audios=lowerCamelCase )
238
1
"""simple docstring""" import warnings from ...utils import logging from .image_processing_dpt import DPTImageProcessor __A : List[str] = logging.get_logger(__name__) class _a ( lowerCAmelCase): """simple docstring""" def __init__( self : Optional[int] , *__UpperCamelCase : Union[str, Any] , **__UpperCamelCase : Optional[Any] )->None: warnings.warn( '''The class DPTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use DPTImageProcessor instead.''' , __UpperCamelCase , ) super().__init__(*__UpperCamelCase , **__UpperCamelCase )
326
"""simple docstring""" import argparse import json import os from collections import OrderedDict import torch from transformers import LukeConfig, LukeForMaskedLM, MLukeTokenizer, XLMRobertaTokenizer from transformers.tokenization_utils_base import AddedToken @torch.no_grad() def lowercase ( _SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : Tuple , _SCREAMING_SNAKE_CASE : Any ): '''simple docstring''' with open(_SCREAMING_SNAKE_CASE ) as metadata_file: _UpperCAmelCase = json.load(_SCREAMING_SNAKE_CASE ) _UpperCAmelCase = LukeConfig(use_entity_aware_attention=_SCREAMING_SNAKE_CASE , **metadata['''model_config'''] ) # Load in the weights from the checkpoint_path _UpperCAmelCase = torch.load(_SCREAMING_SNAKE_CASE , map_location='''cpu''' )['''module'''] # Load the entity vocab file _UpperCAmelCase = load_original_entity_vocab(_SCREAMING_SNAKE_CASE ) # add an entry for [MASK2] _UpperCAmelCase = max(entity_vocab.values() ) + 1 config.entity_vocab_size += 1 _UpperCAmelCase = XLMRobertaTokenizer.from_pretrained(metadata['''model_config''']['''bert_model_name'''] ) # Add special tokens to the token vocabulary for downstream tasks _UpperCAmelCase = AddedToken('''<ent>''' , lstrip=_SCREAMING_SNAKE_CASE , rstrip=_SCREAMING_SNAKE_CASE ) _UpperCAmelCase = AddedToken('''<ent2>''' , lstrip=_SCREAMING_SNAKE_CASE , rstrip=_SCREAMING_SNAKE_CASE ) tokenizer.add_special_tokens({'''additional_special_tokens''': [entity_token_a, entity_token_a]} ) config.vocab_size += 2 print(f'Saving tokenizer to {pytorch_dump_folder_path}' ) tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) with open(os.path.join(_SCREAMING_SNAKE_CASE , '''tokenizer_config.json''' ) , '''r''' ) as f: _UpperCAmelCase = json.load(_SCREAMING_SNAKE_CASE ) _UpperCAmelCase = '''MLukeTokenizer''' with open(os.path.join(_SCREAMING_SNAKE_CASE , '''tokenizer_config.json''' ) , '''w''' ) as f: json.dump(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) with open(os.path.join(_SCREAMING_SNAKE_CASE , MLukeTokenizer.vocab_files_names['''entity_vocab_file'''] ) , '''w''' ) as f: json.dump(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) _UpperCAmelCase = MLukeTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) # Initialize the embeddings of the special tokens _UpperCAmelCase = tokenizer.convert_tokens_to_ids(['''@'''] )[0] _UpperCAmelCase = tokenizer.convert_tokens_to_ids(['''#'''] )[0] _UpperCAmelCase = state_dict['''embeddings.word_embeddings.weight'''] _UpperCAmelCase = word_emb[ent_init_index].unsqueeze(0 ) _UpperCAmelCase = word_emb[enta_init_index].unsqueeze(0 ) _UpperCAmelCase = torch.cat([word_emb, ent_emb, enta_emb] ) # add special tokens for 'entity_predictions.bias' for bias_name in ["lm_head.decoder.bias", "lm_head.bias"]: _UpperCAmelCase = state_dict[bias_name] _UpperCAmelCase = decoder_bias[ent_init_index].unsqueeze(0 ) _UpperCAmelCase = decoder_bias[enta_init_index].unsqueeze(0 ) _UpperCAmelCase = torch.cat([decoder_bias, ent_decoder_bias, enta_decoder_bias] ) # Initialize the query layers of the entity-aware self-attention mechanism for layer_index in range(config.num_hidden_layers ): for matrix_name in ["query.weight", "query.bias"]: _UpperCAmelCase = f'encoder.layer.{layer_index}.attention.self.' _UpperCAmelCase = state_dict[prefix + matrix_name] _UpperCAmelCase = state_dict[prefix + matrix_name] _UpperCAmelCase = state_dict[prefix + matrix_name] # Initialize the embedding of the [MASK2] entity using that of the [MASK] entity for downstream tasks _UpperCAmelCase = state_dict['''entity_embeddings.entity_embeddings.weight'''] _UpperCAmelCase = entity_emb[entity_vocab['''[MASK]''']].unsqueeze(0 ) _UpperCAmelCase = torch.cat([entity_emb, entity_mask_emb] ) # add [MASK2] for 'entity_predictions.bias' _UpperCAmelCase = state_dict['''entity_predictions.bias'''] _UpperCAmelCase = entity_prediction_bias[entity_vocab['''[MASK]''']].unsqueeze(0 ) _UpperCAmelCase = torch.cat([entity_prediction_bias, entity_mask_bias] ) _UpperCAmelCase = LukeForMaskedLM(config=_SCREAMING_SNAKE_CASE ).eval() state_dict.pop('''entity_predictions.decoder.weight''' ) state_dict.pop('''lm_head.decoder.weight''' ) state_dict.pop('''lm_head.decoder.bias''' ) _UpperCAmelCase = OrderedDict() for key, value in state_dict.items(): if not (key.startswith('''lm_head''' ) or key.startswith('''entity_predictions''' )): _UpperCAmelCase = state_dict[key] else: _UpperCAmelCase = state_dict[key] _UpperCAmelCase , _UpperCAmelCase = model.load_state_dict(_SCREAMING_SNAKE_CASE , strict=_SCREAMING_SNAKE_CASE ) if set(_SCREAMING_SNAKE_CASE ) != {"luke.embeddings.position_ids"}: raise ValueError(f'Unexpected unexpected_keys: {unexpected_keys}' ) if set(_SCREAMING_SNAKE_CASE ) != { "lm_head.decoder.weight", "lm_head.decoder.bias", "entity_predictions.decoder.weight", }: raise ValueError(f'Unexpected missing_keys: {missing_keys}' ) model.tie_weights() assert (model.luke.embeddings.word_embeddings.weight == model.lm_head.decoder.weight).all() assert (model.luke.entity_embeddings.entity_embeddings.weight == model.entity_predictions.decoder.weight).all() # Check outputs _UpperCAmelCase = MLukeTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE , task='''entity_classification''' ) _UpperCAmelCase = '''ISO 639-3 uses the code fas for the dialects spoken across Iran and アフガニスタン (Afghanistan).''' _UpperCAmelCase = (0, 9) _UpperCAmelCase = tokenizer(_SCREAMING_SNAKE_CASE , entity_spans=[span] , return_tensors='''pt''' ) _UpperCAmelCase = model(**_SCREAMING_SNAKE_CASE ) # Verify word hidden states if model_size == "large": raise NotImplementedError else: # base _UpperCAmelCase = torch.Size((1, 33, 768) ) _UpperCAmelCase = torch.tensor([[0.0892, 0.0596, -0.2819], [0.0134, 0.1199, 0.0573], [-0.0169, 0.0927, 0.0644]] ) if not (outputs.last_hidden_state.shape == expected_shape): raise ValueError( f'Outputs.last_hidden_state.shape is {outputs.last_hidden_state.shape}, Expected shape is {expected_shape}' ) if not torch.allclose(outputs.last_hidden_state[0, :3, :3] , _SCREAMING_SNAKE_CASE , atol=1E-4 ): raise ValueError # Verify entity hidden states if model_size == "large": raise NotImplementedError else: # base _UpperCAmelCase = torch.Size((1, 1, 768) ) _UpperCAmelCase = torch.tensor([[-0.1482, 0.0609, 0.0322]] ) if not (outputs.entity_last_hidden_state.shape == expected_shape): raise ValueError( f'Outputs.entity_last_hidden_state.shape is {outputs.entity_last_hidden_state.shape}, Expected shape is' f' {expected_shape}' ) if not torch.allclose(outputs.entity_last_hidden_state[0, :3, :3] , _SCREAMING_SNAKE_CASE , atol=1E-4 ): raise ValueError # Verify masked word/entity prediction _UpperCAmelCase = MLukeTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ) _UpperCAmelCase = '''Tokyo is the capital of <mask>.''' _UpperCAmelCase = (24, 30) _UpperCAmelCase = tokenizer(_SCREAMING_SNAKE_CASE , entity_spans=[span] , return_tensors='''pt''' ) _UpperCAmelCase = model(**_SCREAMING_SNAKE_CASE ) _UpperCAmelCase = encoding['''input_ids'''][0].tolist() _UpperCAmelCase = input_ids.index(tokenizer.convert_tokens_to_ids('''<mask>''' ) ) _UpperCAmelCase = outputs.logits[0][mask_position_id].argmax(dim=-1 ) assert "Japan" == tokenizer.decode(_SCREAMING_SNAKE_CASE ) _UpperCAmelCase = outputs.entity_logits[0][0].argmax().item() _UpperCAmelCase = [ entity for entity, entity_id in tokenizer.entity_vocab.items() if entity_id == predicted_entity_id ] assert [e for e in multilingual_predicted_entities if e.startswith('''en:''' )][0] == "en:Japan" # Finally, save our PyTorch model and tokenizer print('''Saving PyTorch model to {}'''.format(_SCREAMING_SNAKE_CASE ) ) model.save_pretrained(_SCREAMING_SNAKE_CASE ) def lowercase ( _SCREAMING_SNAKE_CASE : Tuple ): '''simple docstring''' _UpperCAmelCase = ['''[MASK]''', '''[PAD]''', '''[UNK]'''] _UpperCAmelCase = [json.loads(_SCREAMING_SNAKE_CASE ) for line in open(_SCREAMING_SNAKE_CASE )] _UpperCAmelCase = {} for entry in data: _UpperCAmelCase = entry['''id'''] for entity_name, language in entry["entities"]: if entity_name in SPECIAL_TOKENS: _UpperCAmelCase = entity_id break _UpperCAmelCase = f'{language}:{entity_name}' _UpperCAmelCase = entity_id return new_mapping if __name__ == "__main__": __A : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument("--checkpoint_path", type=str, help="Path to a pytorch_model.bin file.") parser.add_argument( "--metadata_path", default=None, type=str, help="Path to a metadata.json file, defining the configuration." ) parser.add_argument( "--entity_vocab_path", default=None, type=str, help="Path to an entity_vocab.tsv file, containing the entity vocabulary.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to where to dump the output PyTorch model." ) parser.add_argument( "--model_size", default="base", type=str, choices=["base", "large"], help="Size of the model to be converted." ) __A : List[str] = parser.parse_args() convert_luke_checkpoint( args.checkpoint_path, args.metadata_path, args.entity_vocab_path, args.pytorch_dump_folder_path, args.model_size, )
326
1
"""simple docstring""" from typing import Optional from .. import Features, NamedSplit from ..packaged_modules.text.text import Text from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader class __snake_case ( _lowercase): def __init__( self : Union[str, Any] , __lowerCAmelCase : NestedDataStructureLike[PathLike] , __lowerCAmelCase : Optional[NamedSplit] = None , __lowerCAmelCase : Optional[Features] = None , __lowerCAmelCase : str = None , __lowerCAmelCase : bool = False , __lowerCAmelCase : bool = False , __lowerCAmelCase : Optional[int] = None , **__lowerCAmelCase : Union[str, Any] , ): """simple docstring""" super().__init__( lowerCAmelCase__ , split=lowerCAmelCase__ , features=lowerCAmelCase__ , cache_dir=lowerCAmelCase__ , keep_in_memory=lowerCAmelCase__ , streaming=lowerCAmelCase__ , num_proc=lowerCAmelCase__ , **lowerCAmelCase__ , ) _lowerCamelCase : Tuple = path_or_paths if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) else {self.split: path_or_paths} _lowerCamelCase : Tuple = Text( cache_dir=lowerCAmelCase__ , data_files=lowerCAmelCase__ , features=lowerCAmelCase__ , **lowerCAmelCase__ , ) def SCREAMING_SNAKE_CASE ( self : int ): """simple docstring""" if self.streaming: _lowerCamelCase : List[Any] = self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: _lowerCamelCase : int = None _lowerCamelCase : Optional[Any] = None _lowerCamelCase : Optional[Any] = None _lowerCamelCase : Optional[Any] = None self.builder.download_and_prepare( download_config=lowerCAmelCase__ , download_mode=lowerCAmelCase__ , verification_mode=lowerCAmelCase__ , base_path=lowerCAmelCase__ , num_proc=self.num_proc , ) _lowerCamelCase : Dict = self.builder.as_dataset( split=self.split , verification_mode=lowerCAmelCase__ , in_memory=self.keep_in_memory ) return dataset
72
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __a = logging.get_logger(__name__) __a = { 'kssteven/ibert-roberta-base': 'https://huggingface.co/kssteven/ibert-roberta-base/resolve/main/config.json', 'kssteven/ibert-roberta-large': 'https://huggingface.co/kssteven/ibert-roberta-large/resolve/main/config.json', 'kssteven/ibert-roberta-large-mnli': ( 'https://huggingface.co/kssteven/ibert-roberta-large-mnli/resolve/main/config.json' ), } class A__ ( UpperCamelCase ): """simple docstring""" UpperCamelCase_ : Union[str, Any] = '''ibert''' def __init__( self : int , lowerCAmelCase__ : Any=3_0_5_2_2 , lowerCAmelCase__ : List[Any]=7_6_8 , lowerCAmelCase__ : int=1_2 , lowerCAmelCase__ : List[Any]=1_2 , lowerCAmelCase__ : Optional[int]=3_0_7_2 , lowerCAmelCase__ : List[str]="gelu" , lowerCAmelCase__ : str=0.1 , lowerCAmelCase__ : Optional[Any]=0.1 , lowerCAmelCase__ : Optional[Any]=5_1_2 , lowerCAmelCase__ : List[str]=2 , lowerCAmelCase__ : Any=0.02 , lowerCAmelCase__ : Union[str, Any]=1e-12 , lowerCAmelCase__ : Optional[Any]=1 , lowerCAmelCase__ : Optional[Any]=0 , lowerCAmelCase__ : Tuple=2 , lowerCAmelCase__ : Optional[Any]="absolute" , lowerCAmelCase__ : Optional[Any]=False , lowerCAmelCase__ : Tuple="none" , **lowerCAmelCase__ : Optional[Any] , ) -> Optional[int]: """simple docstring""" super().__init__(pad_token_id=lowerCAmelCase__ , bos_token_id=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__ , **lowerCAmelCase__ ) _UpperCAmelCase : Any = vocab_size _UpperCAmelCase : Tuple = hidden_size _UpperCAmelCase : Optional[int] = num_hidden_layers _UpperCAmelCase : Union[str, Any] = num_attention_heads _UpperCAmelCase : Any = hidden_act _UpperCAmelCase : Any = intermediate_size _UpperCAmelCase : Tuple = hidden_dropout_prob _UpperCAmelCase : str = attention_probs_dropout_prob _UpperCAmelCase : Optional[int] = max_position_embeddings _UpperCAmelCase : Union[str, Any] = type_vocab_size _UpperCAmelCase : Optional[int] = initializer_range _UpperCAmelCase : Tuple = layer_norm_eps _UpperCAmelCase : Optional[int] = position_embedding_type _UpperCAmelCase : Any = quant_mode _UpperCAmelCase : Optional[Any] = force_dequant class A__ ( UpperCamelCase ): """simple docstring""" @property def _lowerCAmelCase ( self : Tuple ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task == "multiple-choice": _UpperCAmelCase : Union[str, Any] = {0: "batch", 1: "choice", 2: "sequence"} else: _UpperCAmelCase : Dict = {0: "batch", 1: "sequence"} return OrderedDict( [ ("input_ids", dynamic_axis), ("attention_mask", dynamic_axis), ] )
145
0
'''simple docstring''' def lowercase_ ( lowerCAmelCase__ : list ): """simple docstring""" __UpperCAmelCase : Union[str, Any] = 0 while len(lowerCAmelCase__ ) > 1: __UpperCAmelCase : Dict = 0 # Consider two files with minimum cost to be merged for _ in range(2 ): __UpperCAmelCase : Optional[Any] = files.index(min(lowerCAmelCase__ ) ) temp += files[min_index] files.pop(lowerCAmelCase__ ) files.append(lowerCAmelCase__ ) optimal_merge_cost += temp return optimal_merge_cost if __name__ == "__main__": import doctest doctest.testmod()
16
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, is_vision_available, ) _UpperCamelCase = { '''configuration_owlvit''': [ '''OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''OwlViTConfig''', '''OwlViTOnnxConfig''', '''OwlViTTextConfig''', '''OwlViTVisionConfig''', ], '''processing_owlvit''': ['''OwlViTProcessor'''], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _UpperCamelCase = ['''OwlViTFeatureExtractor'''] _UpperCamelCase = ['''OwlViTImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _UpperCamelCase = [ '''OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''OwlViTModel''', '''OwlViTPreTrainedModel''', '''OwlViTTextModel''', '''OwlViTVisionModel''', '''OwlViTForObjectDetection''', ] if TYPE_CHECKING: from .configuration_owlvit import ( OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, OwlViTConfig, OwlViTOnnxConfig, OwlViTTextConfig, OwlViTVisionConfig, ) from .processing_owlvit import OwlViTProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_owlvit import OwlViTFeatureExtractor from .image_processing_owlvit import OwlViTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_owlvit import ( OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST, OwlViTForObjectDetection, OwlViTModel, OwlViTPreTrainedModel, OwlViTTextModel, OwlViTVisionModel, ) else: import sys _UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
16
1
'''simple docstring''' from __future__ import annotations class snake_case : """simple docstring""" def __init__( self : Optional[int] , __A : list[list[int]] ): __UpperCamelCase = TypeError( 'Matrices must be formed from a list of zero or more lists containing at ' 'least one and the same number of values, each of which must be of type ' 'int or float.' ) if len(__A ) != 0: __UpperCamelCase = len(rows[0] ) if cols == 0: raise error for row in rows: if len(__A ) != cols: raise error for value in row: if not isinstance(__A , (int, float) ): raise error __UpperCamelCase = rows else: __UpperCamelCase = [] def _lowerCamelCase ( self : int ): return [[row[i] for row in self.rows] for i in range(len(self.rows[0] ) )] @property def _lowerCamelCase ( self : str ): return len(self.rows ) @property def _lowerCamelCase ( self : Any ): return len(self.rows[0] ) @property def _lowerCamelCase ( self : Optional[Any] ): return (self.num_rows, self.num_columns) @property def _lowerCamelCase ( self : Dict ): return self.order[0] == self.order[1] def _lowerCamelCase ( self : Any ): __UpperCamelCase = [ [0 if column_num != row_num else 1 for column_num in range(self.num_rows )] for row_num in range(self.num_rows ) ] return Matrix(__A ) def _lowerCamelCase ( self : Any ): if not self.is_square: return 0 if self.order == (0, 0): return 1 if self.order == (1, 1): return int(self.rows[0][0] ) if self.order == (2, 2): return int( (self.rows[0][0] * self.rows[1][1]) - (self.rows[0][1] * self.rows[1][0]) ) else: return sum( self.rows[0][column] * self.cofactors().rows[0][column] for column in range(self.num_columns ) ) def _lowerCamelCase ( self : List[str] ): return bool(self.determinant() ) def _lowerCamelCase ( self : Dict , __A : int , __A : int ): __UpperCamelCase = [ [ self.rows[other_row][other_column] for other_column in range(self.num_columns ) if other_column != column ] for other_row in range(self.num_rows ) if other_row != row ] return Matrix(__A ).determinant() def _lowerCamelCase ( self : Dict , __A : int , __A : int ): if (row + column) % 2 == 0: return self.get_minor(__A , __A ) return -1 * self.get_minor(__A , __A ) def _lowerCamelCase ( self : List[str] ): return Matrix( [ [self.get_minor(__A , __A ) for column in range(self.num_columns )] for row in range(self.num_rows ) ] ) def _lowerCamelCase ( self : Union[str, Any] ): return Matrix( [ [ self.minors().rows[row][column] if (row + column) % 2 == 0 else self.minors().rows[row][column] * -1 for column in range(self.minors().num_columns ) ] for row in range(self.minors().num_rows ) ] ) def _lowerCamelCase ( self : List[str] ): __UpperCamelCase = [ [self.cofactors().rows[column][row] for column in range(self.num_columns )] for row in range(self.num_rows ) ] return Matrix(__A ) def _lowerCamelCase ( self : Dict ): __UpperCamelCase = self.determinant() if not determinant: raise TypeError('Only matrices with a non-zero determinant have an inverse' ) return self.adjugate() * (1 / determinant) def __repr__( self : Optional[Any] ): return str(self.rows ) def __str__( self : Union[str, Any] ): if self.num_rows == 0: return "[]" if self.num_rows == 1: return "[[" + ". ".join(str(self.rows[0] ) ) + "]]" return ( "[" + "\n ".join( [ '[' + '. '.join([str(__A ) for value in row] ) + '.]' for row in self.rows ] ) + "]" ) def _lowerCamelCase ( self : List[Any] , __A : list[int] , __A : int | None = None ): __UpperCamelCase = TypeError('Row must be a list containing all ints and/or floats' ) if not isinstance(__A , __A ): raise type_error for value in row: if not isinstance(__A , (int, float) ): raise type_error if len(__A ) != self.num_columns: raise ValueError( 'Row must be equal in length to the other rows in the matrix' ) if position is None: self.rows.append(__A ) else: __UpperCamelCase = self.rows[0:position] + [row] + self.rows[position:] def _lowerCamelCase ( self : Optional[Any] , __A : list[int] , __A : int | None = None ): __UpperCamelCase = TypeError( 'Column must be a list containing all ints and/or floats' ) if not isinstance(__A , __A ): raise type_error for value in column: if not isinstance(__A , (int, float) ): raise type_error if len(__A ) != self.num_rows: raise ValueError( 'Column must be equal in length to the other columns in the matrix' ) if position is None: __UpperCamelCase = [self.rows[i] + [column[i]] for i in range(self.num_rows )] else: __UpperCamelCase = [ self.rows[i][0:position] + [column[i]] + self.rows[i][position:] for i in range(self.num_rows ) ] def __eq__( self : Tuple , __A : object ): if not isinstance(__A , __A ): return NotImplemented return self.rows == other.rows def __ne__( self : Any , __A : object ): return not self == other def __neg__( self : List[Any] ): return self * -1 def __add__( self : List[str] , __A : Matrix ): if self.order != other.order: raise ValueError('Addition requires matrices of the same order' ) return Matrix( [ [self.rows[i][j] + other.rows[i][j] for j in range(self.num_columns )] for i in range(self.num_rows ) ] ) def __sub__( self : str , __A : Matrix ): if self.order != other.order: raise ValueError('Subtraction requires matrices of the same order' ) return Matrix( [ [self.rows[i][j] - other.rows[i][j] for j in range(self.num_columns )] for i in range(self.num_rows ) ] ) def __mul__( self : str , __A : Matrix | int | float ): if isinstance(__A , (int, float) ): return Matrix( [[int(element * other ) for element in row] for row in self.rows] ) elif isinstance(__A , __A ): if self.num_columns != other.num_rows: raise ValueError( 'The number of columns in the first matrix must ' 'be equal to the number of rows in the second' ) return Matrix( [ [Matrix.dot_product(__A , __A ) for column in other.columns()] for row in self.rows ] ) else: raise TypeError( 'A Matrix can only be multiplied by an int, float, or another matrix' ) def __pow__( self : Union[str, Any] , __A : int ): if not isinstance(__A , __A ): raise TypeError('A Matrix can only be raised to the power of an int' ) if not self.is_square: raise ValueError('Only square matrices can be raised to a power' ) if other == 0: return self.identity() if other < 0: if self.is_invertable(): return self.inverse() ** (-other) raise ValueError( 'Only invertable matrices can be raised to a negative power' ) __UpperCamelCase = self for _ in range(other - 1 ): result *= self return result @classmethod def _lowerCamelCase ( cls : Tuple , __A : list[int] , __A : list[int] ): return sum(row[i] * column[i] for i in range(len(__A ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
53
import sys snake_case : int = ( '''73167176531330624919225119674426574742355349194934''' '''96983520312774506326239578318016984801869478851843''' '''85861560789112949495459501737958331952853208805511''' '''12540698747158523863050715693290963295227443043557''' '''66896648950445244523161731856403098711121722383113''' '''62229893423380308135336276614282806444486645238749''' '''30358907296290491560440772390713810515859307960866''' '''70172427121883998797908792274921901699720888093776''' '''65727333001053367881220235421809751254540594752243''' '''52584907711670556013604839586446706324415722155397''' '''53697817977846174064955149290862569321978468622482''' '''83972241375657056057490261407972968652414535100474''' '''82166370484403199890008895243450658541227588666881''' '''16427171479924442928230863465674813919123162824586''' '''17866458359124566529476545682848912883142607690042''' '''24219022671055626321111109370544217506941658960408''' '''07198403850962455444362981230987879927244284909188''' '''84580156166097919133875499200524063689912560717606''' '''05886116467109405077541002256983155200055935729725''' '''71636269561882670428252483600823257530420752963450''' ) def __lowerCamelCase ( UpperCAmelCase_ : str = N ): """simple docstring""" a :Optional[Any] = -sys.maxsize - 1 for i in range(len(UpperCAmelCase_ ) - 12 ): a :Dict = 1 for j in range(13 ): product *= int(n[i + j] ) if product > largest_product: a :str = product return largest_product if __name__ == "__main__": print(F"""{solution() = }""")
94
0
"""simple docstring""" from math import factorial def lowercase__(A = 100 ) ->int: """simple docstring""" return sum(map(A , str(factorial(A ) ) ) ) if __name__ == "__main__": print(solution(int(input("""Enter the Number: """).strip())))
150
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available a : List[str] = { """configuration_xlm_roberta_xl""": [ """XLM_ROBERTA_XL_PRETRAINED_CONFIG_ARCHIVE_MAP""", """XLMRobertaXLConfig""", """XLMRobertaXLOnnxConfig""", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a : List[str] = [ """XLM_ROBERTA_XL_PRETRAINED_MODEL_ARCHIVE_LIST""", """XLMRobertaXLForCausalLM""", """XLMRobertaXLForMaskedLM""", """XLMRobertaXLForMultipleChoice""", """XLMRobertaXLForQuestionAnswering""", """XLMRobertaXLForSequenceClassification""", """XLMRobertaXLForTokenClassification""", """XLMRobertaXLModel""", """XLMRobertaXLPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_xlm_roberta_xl import ( XLM_ROBERTA_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMRobertaXLConfig, XLMRobertaXLOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm_roberta_xl import ( XLM_ROBERTA_XL_PRETRAINED_MODEL_ARCHIVE_LIST, XLMRobertaXLForCausalLM, XLMRobertaXLForMaskedLM, XLMRobertaXLForMultipleChoice, XLMRobertaXLForQuestionAnswering, XLMRobertaXLForSequenceClassification, XLMRobertaXLForTokenClassification, XLMRobertaXLModel, XLMRobertaXLPreTrainedModel, ) else: import sys a : List[str] = _LazyModule(__name__, globals()["""__file__"""], _import_structure)
150
1
import math def a ( _UpperCAmelCase : Optional[Any] = 1_00 ): '''simple docstring''' __UpperCAmelCase : Tuple = sum(i * i for i in range(1 , n + 1 ) ) __UpperCAmelCase : List[Any] = int(math.pow(sum(range(1 , n + 1 ) ) , 2 ) ) return square_of_sum - sum_of_squares if __name__ == "__main__": print(f'''{solution() = }''')
226
import os import re import shutil import sys import tempfile import unittest import black __a = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, '''utils''')) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If DDPMSchedulerOutput is changed in scheduling_ddpm.py, this code needs to be manually updated. __a = ''' \""" Output class for the scheduler\'s step function output. Args: prev_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images): Computed sample (x_{t-1}) of previous timestep. `prev_sample` should be used as next model input in the denoising loop. pred_original_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images): The predicted denoised sample (x_{0}) based on the model output from the current timestep. `pred_original_sample` can be used to preview progress or for guidance. \""" prev_sample: torch.FloatTensor pred_original_sample: Optional[torch.FloatTensor] = None ''' class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): def __lowerCamelCase ( self ): lowercase : str = tempfile.mkdtemp() os.makedirs(os.path.join(self.diffusers_dir , '''schedulers/''' ) ) lowercase : Any = self.diffusers_dir shutil.copy( os.path.join(SCREAMING_SNAKE_CASE__ , '''src/diffusers/schedulers/scheduling_ddpm.py''' ) , os.path.join(self.diffusers_dir , '''schedulers/scheduling_ddpm.py''' ) , ) def __lowerCamelCase ( self ): lowercase : List[Any] = '''src/diffusers''' shutil.rmtree(self.diffusers_dir ) def __lowerCamelCase ( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=None ): lowercase : Tuple = comment + f"""\nclass {class_name}(nn.Module):\n""" + class_code if overwrite_result is not None: lowercase : str = comment + f"""\nclass {class_name}(nn.Module):\n""" + overwrite_result lowercase : Any = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) lowercase : List[Any] = black.format_str(SCREAMING_SNAKE_CASE__ , mode=SCREAMING_SNAKE_CASE__ ) lowercase : Dict = os.path.join(self.diffusers_dir , '''new_code.py''' ) with open(SCREAMING_SNAKE_CASE__ , '''w''' , newline='''\n''' ) as f: f.write(SCREAMING_SNAKE_CASE__ ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(SCREAMING_SNAKE_CASE__ ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=SCREAMING_SNAKE_CASE__ ) with open(SCREAMING_SNAKE_CASE__ , '''r''' ) as f: self.assertTrue(f.read() , SCREAMING_SNAKE_CASE__ ) def __lowerCamelCase ( self ): lowercase : Tuple = check_copies.find_code_in_diffusers('''schedulers.scheduling_ddpm.DDPMSchedulerOutput''' ) self.assertEqual(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def __lowerCamelCase ( self ): # Base copy consistency self.check_copy_consistency( '''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput''' , '''DDPMSchedulerOutput''' , REFERENCE_CODE + '''\n''' , ) # With no empty line at the end self.check_copy_consistency( '''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput''' , '''DDPMSchedulerOutput''' , SCREAMING_SNAKE_CASE__ , ) # Copy consistency with rename self.check_copy_consistency( '''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test''' , '''TestSchedulerOutput''' , re.sub('''DDPM''' , '''Test''' , SCREAMING_SNAKE_CASE__ ) , ) # Copy consistency with a really long name lowercase : List[Any] = '''TestClassWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason''' self.check_copy_consistency( f"""# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->{long_class_name}""" , f"""{long_class_name}SchedulerOutput""" , re.sub('''Bert''' , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) , ) # Copy consistency with overwrite self.check_copy_consistency( '''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test''' , '''TestSchedulerOutput''' , SCREAMING_SNAKE_CASE__ , overwrite_result=re.sub('''DDPM''' , '''Test''' , SCREAMING_SNAKE_CASE__ ) , )
337
0
'''simple docstring''' import numpy # List of input, output pairs __A =( ((5, 2, 3), 15), ((6, 5, 9), 25), ((11, 12, 13), 41), ((1, 1, 1), 8), ((11, 12, 13), 41), ) __A =(((5_15, 22, 13), 5_55), ((61, 35, 49), 1_50)) __A =[2, 4, 1, 5] __A =len(train_data) __A =0.0_0_9 def _UpperCamelCase ( UpperCamelCase__ , UpperCamelCase__="train" ): return calculate_hypothesis_value(_lowerCAmelCase , _lowerCAmelCase ) - output( _lowerCAmelCase , _lowerCAmelCase ) def _UpperCamelCase ( UpperCamelCase__ ): UpperCAmelCase__ : Union[str, Any] = 0 for i in range(len(_lowerCAmelCase ) - 1 ): hyp_val += data_input_tuple[i] * parameter_vector[i + 1] hyp_val += parameter_vector[0] return hyp_val def _UpperCamelCase ( UpperCamelCase__ , UpperCamelCase__ ): if data_set == "train": return train_data[example_no][1] elif data_set == "test": return test_data[example_no][1] return None def _UpperCamelCase ( UpperCamelCase__ , UpperCamelCase__ ): if data_set == "train": return _hypothesis_value(train_data[example_no][0] ) elif data_set == "test": return _hypothesis_value(test_data[example_no][0] ) return None def _UpperCamelCase ( UpperCamelCase__ , UpperCamelCase__=m ): UpperCAmelCase__ : int = 0 for i in range(_lowerCAmelCase ): if index == -1: summation_value += _error(_lowerCAmelCase ) else: summation_value += _error(_lowerCAmelCase ) * train_data[i][0][index] return summation_value def _UpperCamelCase ( UpperCamelCase__ ): UpperCAmelCase__ : int = summation_of_cost_derivative(_lowerCAmelCase , _lowerCAmelCase ) / m return cost_derivative_value def _UpperCamelCase ( ): global parameter_vector # Tune these values to set a tolerance value for predicted output UpperCAmelCase__ : Dict = 0.00_00_02 UpperCAmelCase__ : int = 0 UpperCAmelCase__ : Dict = 0 while True: j += 1 UpperCAmelCase__ : int = [0, 0, 0, 0] for i in range(0 , len(_lowerCAmelCase ) ): UpperCAmelCase__ : Any = get_cost_derivative(i - 1 ) UpperCAmelCase__ : str = ( parameter_vector[i] - LEARNING_RATE * cost_derivative ) if numpy.allclose( _lowerCAmelCase , _lowerCAmelCase , atol=_lowerCAmelCase , rtol=_lowerCAmelCase , ): break UpperCAmelCase__ : Union[str, Any] = temp_parameter_vector print(("""Number of iterations:""", j) ) def _UpperCamelCase ( ): for i in range(len(_lowerCAmelCase ) ): print(("""Actual output value:""", output(_lowerCAmelCase , """test""" )) ) print(("""Hypothesis output:""", calculate_hypothesis_value(_lowerCAmelCase , """test""" )) ) if __name__ == "__main__": run_gradient_descent() print('\nTesting gradient descent for a linear hypothesis function.\n') test_gradient_descent()
354
'''simple docstring''' def _UpperCamelCase ( UpperCamelCase__ = 1_0 , UpperCamelCase__ = 2_2 ): UpperCAmelCase__ : List[str] = range(1 , UpperCamelCase__ ) UpperCAmelCase__ : int = range(1 , UpperCamelCase__ ) return sum( 1 for power in powers for base in bases if len(str(base**power ) ) == power ) if __name__ == "__main__": print(f"""{solution(10, 22) = }""")
283
0
from __future__ import annotations import requests __lowerCamelCase : int = set( '''approved_at_utc approved_by author_flair_background_color author_flair_css_class author_flair_richtext author_flair_template_id author_fullname author_premium can_mod_post category clicked content_categories created_utc downs edited gilded gildings hidden hide_score is_created_from_ads_ui is_meta is_original_content is_reddit_media_domain is_video link_flair_css_class link_flair_richtext link_flair_text link_flair_text_color media_embed mod_reason_title name permalink pwls quarantine saved score secure_media secure_media_embed selftext subreddit subreddit_name_prefixed subreddit_type thumbnail title top_awarded_type total_awards_received ups upvote_ratio url user_reports'''.split() ) def _snake_case ( lowerCAmelCase : str , lowerCAmelCase : int = 1 , lowerCAmelCase : str = "new" , lowerCAmelCase : list | None = None ): """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = wanted_data or [] if invalid_search_terms := ", ".join(sorted(set(lowerCAmelCase ) - valid_terms ) ): SCREAMING_SNAKE_CASE_ : List[Any] = f'Invalid search term: {invalid_search_terms}' raise ValueError(lowerCAmelCase ) SCREAMING_SNAKE_CASE_ : Optional[Any] = requests.get( f'https://reddit.com/r/{subreddit}/{age}.json?limit={limit}' , headers={"User-agent": "A random string"} , ) if response.status_code == 4_2_9: raise requests.HTTPError SCREAMING_SNAKE_CASE_ : Union[str, Any] = response.json() if not wanted_data: return {id_: data["data"]["children"][id_] for id_ in range(lowerCAmelCase )} SCREAMING_SNAKE_CASE_ : List[str] = {} for id_ in range(lowerCAmelCase ): SCREAMING_SNAKE_CASE_ : int = { item: data["data"]["children"][id_]["data"][item] for item in wanted_data } return data_dict if __name__ == "__main__": # If you get Error 429, that means you are rate limited.Try after some time print(get_subreddit_data('''learnpython''', wanted_data=['''title''', '''url''', '''selftext''']))
18
import json from typing import List, Optional, Tuple from tokenizers import normalizers from tokenizers.pre_tokenizers import BertPreTokenizer, PreTokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_roformer import RoFormerTokenizer from .tokenization_utils import JiebaPreTokenizer __lowerCamelCase : Optional[Any] = logging.get_logger(__name__) __lowerCamelCase : Dict = {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''} __lowerCamelCase : int = { '''vocab_file''': { '''junnyu/roformer_chinese_small''': '''https://huggingface.co/junnyu/roformer_chinese_small/resolve/main/vocab.txt''', '''junnyu/roformer_chinese_base''': '''https://huggingface.co/junnyu/roformer_chinese_base/resolve/main/vocab.txt''', '''junnyu/roformer_chinese_char_small''': ( '''https://huggingface.co/junnyu/roformer_chinese_char_small/resolve/main/vocab.txt''' ), '''junnyu/roformer_chinese_char_base''': ( '''https://huggingface.co/junnyu/roformer_chinese_char_base/resolve/main/vocab.txt''' ), '''junnyu/roformer_small_discriminator''': ( '''https://huggingface.co/junnyu/roformer_small_discriminator/resolve/main/vocab.txt''' ), '''junnyu/roformer_small_generator''': ( '''https://huggingface.co/junnyu/roformer_small_generator/resolve/main/vocab.txt''' ), } } __lowerCamelCase : Any = { '''junnyu/roformer_chinese_small''': 15_36, '''junnyu/roformer_chinese_base''': 15_36, '''junnyu/roformer_chinese_char_small''': 5_12, '''junnyu/roformer_chinese_char_base''': 5_12, '''junnyu/roformer_small_discriminator''': 1_28, '''junnyu/roformer_small_generator''': 1_28, } __lowerCamelCase : Union[str, Any] = { '''junnyu/roformer_chinese_small''': {'''do_lower_case''': True}, '''junnyu/roformer_chinese_base''': {'''do_lower_case''': True}, '''junnyu/roformer_chinese_char_small''': {'''do_lower_case''': True}, '''junnyu/roformer_chinese_char_base''': {'''do_lower_case''': True}, '''junnyu/roformer_small_discriminator''': {'''do_lower_case''': True}, '''junnyu/roformer_small_generator''': {'''do_lower_case''': True}, } class a__ ( A__ ): A = VOCAB_FILES_NAMES A = PRETRAINED_VOCAB_FILES_MAP A = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A = PRETRAINED_INIT_CONFIGURATION A = RoFormerTokenizer def __init__( self : List[str],_A : int=None,_A : int=None,_A : int=True,_A : List[Any]="[UNK]",_A : Tuple="[SEP]",_A : List[Any]="[PAD]",_A : Optional[int]="[CLS]",_A : Optional[Any]="[MASK]",_A : Optional[int]=True,_A : List[str]=None,**_A : List[Any],): """simple docstring""" super().__init__( _A,tokenizer_file=_A,do_lower_case=_A,unk_token=_A,sep_token=_A,pad_token=_A,cls_token=_A,mask_token=_A,tokenize_chinese_chars=_A,strip_accents=_A,**_A,) SCREAMING_SNAKE_CASE_ : Tuple = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( pre_tok_state.get("lowercase",_A ) != do_lower_case or pre_tok_state.get("strip_accents",_A ) != strip_accents ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = getattr(_A,pre_tok_state.pop("type" ) ) SCREAMING_SNAKE_CASE_ : Any = do_lower_case SCREAMING_SNAKE_CASE_ : List[str] = strip_accents SCREAMING_SNAKE_CASE_ : str = pre_tok_class(**_A ) SCREAMING_SNAKE_CASE_ : List[str] = do_lower_case def __getstate__( self : Optional[int] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = self.__dict__.copy() SCREAMING_SNAKE_CASE_ : Optional[Any] = BertPreTokenizer() return state def __setstate__( self : List[Any],_A : Union[str, Any] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : int = d SCREAMING_SNAKE_CASE_ : List[str] = self.__dict__["_tokenizer"].get_vocab() SCREAMING_SNAKE_CASE_ : Any = PreTokenizer.custom(JiebaPreTokenizer(_A ) ) def __UpperCamelCase ( self : Union[str, Any],_A : List[Any],_A : str=None ): """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def __UpperCamelCase ( self : str,_A : List[int],_A : Optional[List[int]] = None ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[Any] = [self.sep_token_id] SCREAMING_SNAKE_CASE_ : List[str] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __UpperCamelCase ( self : int,_A : str,_A : Optional[str] = None ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = self._tokenizer.model.save(_A,name=_A ) return tuple(_A ) def __UpperCamelCase ( self : int,_A : Optional[int],_A : List[Any]=None,_A : Tuple=None,_A : str=False,**_A : List[Any],): """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = BertPreTokenizer() return super().save_pretrained(_A,_A,_A,_A,**_A )
18
1
'''simple docstring''' def UpperCamelCase ( _lowerCamelCase : Dict ): A__, A__ = [], [] while len(SCREAMING_SNAKE_CASE__ ) > 1: A__, A__ = min(SCREAMING_SNAKE_CASE__ ), max(SCREAMING_SNAKE_CASE__ ) start.append(SCREAMING_SNAKE_CASE__ ) end.append(SCREAMING_SNAKE_CASE__ ) collection.remove(SCREAMING_SNAKE_CASE__ ) collection.remove(SCREAMING_SNAKE_CASE__ ) end.reverse() return start + collection + end if __name__ == "__main__": __lowerCAmelCase : int =input("Enter numbers separated by a comma:\n").strip() __lowerCAmelCase : Optional[Any] =[int(item) for item in user_input.split(",")] print(*merge_sort(unsorted), sep=",")
367
'''simple docstring''' import argparse import logging import os import time import timeit import datasets import numpy as np import pycuda.autoinit # noqa: F401 import pycuda.driver as cuda import tensorrt as trt import torch from absl import logging as absl_logging from accelerate import Accelerator from datasets import load_dataset, load_metric from torch.utils.data import DataLoader from utils_qa import postprocess_qa_predictions import transformers from transformers import AutoTokenizer, EvalPrediction, default_data_collator, set_seed from transformers.trainer_pt_utils import nested_concat, nested_truncate __lowerCAmelCase : Tuple =trt.Logger(trt.Logger.WARNING) __lowerCAmelCase : Optional[Any] =absl_logging.get_absl_logger() absl_logger.setLevel(logging.WARNING) __lowerCAmelCase : List[Any] =logging.getLogger(__name__) __lowerCAmelCase : Optional[Any] =argparse.ArgumentParser() # Required parameters parser.add_argument( "--onnx_model_path", default=None, type=str, required=True, help="Path to ONNX model: ", ) parser.add_argument( "--output_dir", default=None, type=str, required=True, help="The output directory where the model checkpoints and predictions will be written.", ) # Other parameters parser.add_argument( "--tokenizer_name", default="", type=str, required=True, help="Pretrained tokenizer name or path if not the same as model_name", ) parser.add_argument( "--version_2_with_negative", action="store_true", help="If true, the SQuAD examples contain some that do not have an answer.", ) parser.add_argument( "--null_score_diff_threshold", type=float, default=0.0, help="If null_score - best_non_null is greater than the threshold predict null.", ) parser.add_argument( "--max_seq_length", default=384, type=int, help=( "The maximum total input sequence length after WordPiece tokenization. Sequences " "longer than this will be truncated, and sequences shorter than this will be padded." ), ) parser.add_argument( "--doc_stride", default=128, type=int, help="When splitting up a long document into chunks, how much stride to take between chunks.", ) parser.add_argument("--per_device_eval_batch_size", default=8, type=int, help="Batch size per GPU/CPU for evaluation.") parser.add_argument( "--n_best_size", default=20, type=int, help="The total number of n-best predictions to generate in the nbest_predictions.json output file.", ) parser.add_argument( "--max_answer_length", default=30, type=int, help=( "The maximum length of an answer that can be generated. This is needed because the start " "and end predictions are not conditioned on one another." ), ) parser.add_argument("--seed", type=int, default=42, help="random seed for initialization") parser.add_argument( "--dataset_name", type=str, default=None, required=True, help="The name of the dataset to use (via the datasets library).", ) parser.add_argument( "--dataset_config_name", type=str, default=None, help="The configuration name of the dataset to use (via the datasets library).", ) parser.add_argument( "--preprocessing_num_workers", type=int, default=4, help="A csv or a json file containing the training data." ) parser.add_argument("--overwrite_cache", action="store_true", help="Overwrite the cached training and evaluation sets") parser.add_argument( "--fp16", action="store_true", help="Whether to use 16-bit (mixed) precision instead of 32-bit", ) parser.add_argument( "--int8", action="store_true", help="Whether to use INT8", ) __lowerCAmelCase : Tuple =parser.parse_args() if args.tokenizer_name: __lowerCAmelCase : int =AutoTokenizer.from_pretrained(args.tokenizer_name, use_fast=True) else: raise ValueError( "You are instantiating a new tokenizer from scratch. This is not supported by this script." "You can do it from another script, save it, and load it from here, using --tokenizer_name." ) logger.info("Training/evaluation parameters %s", args) __lowerCAmelCase : Union[str, Any] =args.per_device_eval_batch_size __lowerCAmelCase : List[Any] =(args.eval_batch_size, args.max_seq_length) # TRT Engine properties __lowerCAmelCase : Tuple =True __lowerCAmelCase : int ="temp_engine/bert-fp32.engine" if args.fpaa: __lowerCAmelCase : Tuple ="temp_engine/bert-fp16.engine" if args.inta: __lowerCAmelCase : Optional[int] ="temp_engine/bert-int8.engine" # import ONNX file if not os.path.exists("temp_engine"): os.makedirs("temp_engine") __lowerCAmelCase : Tuple =1 << (int)(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH) with trt.Builder(TRT_LOGGER) as builder, builder.create_network(EXPLICIT_BATCH) as network, trt.OnnxParser( network, TRT_LOGGER ) as parser: with open(args.onnx_model_path, "rb") as model: if not parser.parse(model.read()): for error in range(parser.num_errors): print(parser.get_error(error)) # Query input names and shapes from parsed TensorRT network __lowerCAmelCase : Optional[Any] =[network.get_input(i) for i in range(network.num_inputs)] __lowerCAmelCase : Any =[_input.name for _input in network_inputs] # ex: ["actual_input1"] with builder.create_builder_config() as config: __lowerCAmelCase : Optional[Any] =1 << 50 if STRICT_TYPES: config.set_flag(trt.BuilderFlag.STRICT_TYPES) if args.fpaa: config.set_flag(trt.BuilderFlag.FPaa) if args.inta: config.set_flag(trt.BuilderFlag.INTa) __lowerCAmelCase : int =builder.create_optimization_profile() config.add_optimization_profile(profile) for i in range(len(input_names)): profile.set_shape(input_names[i], INPUT_SHAPE, INPUT_SHAPE, INPUT_SHAPE) __lowerCAmelCase : Dict =builder.build_engine(network, config) # serialize_engine and store in file (can be directly loaded and deserialized): with open(engine_name, "wb") as f: f.write(engine.serialize()) def UpperCamelCase ( _lowerCamelCase : Any , _lowerCamelCase : Tuple , _lowerCamelCase : Optional[int] , _lowerCamelCase : Any , _lowerCamelCase : Optional[Any] , _lowerCamelCase : Tuple , _lowerCamelCase : List[Any] , _lowerCamelCase : Dict ): A__ = np.asarray(inputs["input_ids"] , dtype=np.intaa ) A__ = np.asarray(inputs["attention_mask"] , dtype=np.intaa ) A__ = np.asarray(inputs["token_type_ids"] , dtype=np.intaa ) # Copy inputs cuda.memcpy_htod_async(d_inputs[0] , input_ids.ravel() , _lowerCamelCase ) cuda.memcpy_htod_async(d_inputs[1] , attention_mask.ravel() , _lowerCamelCase ) cuda.memcpy_htod_async(d_inputs[2] , token_type_ids.ravel() , _lowerCamelCase ) # start time A__ = time.time() # Run inference context.execute_async( bindings=[int(_lowerCamelCase ) for d_inp in d_inputs] + [int(_lowerCamelCase ), int(_lowerCamelCase )] , stream_handle=stream.handle ) # Transfer predictions back from GPU cuda.memcpy_dtoh_async(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) cuda.memcpy_dtoh_async(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) # Synchronize the stream and take time stream.synchronize() # end time A__ = time.time() A__ = end_time - start_time A__ = (h_outputa, h_outputa) # print(outputs) return outputs, infer_time # Initialize the accelerator. We will let the accelerator handle device placement for us in this example. __lowerCAmelCase : str =Accelerator() # Make one log on every process with the configuration for debugging. logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO, ) # Setup logging, we only want one process per machine to log things on the screen. # accelerator.is_local_main_process is only True for one process per machine. logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() # If passed along, set the training seed now. if args.seed is not None: set_seed(args.seed) # Get the datasets: you can either provide your own CSV/JSON/TXT training and evaluation files (see below) # or just provide the name of one of the public datasets available on the hub at https://huggingface.co/datasets/ # (the dataset will be downloaded automatically from the datasets Hub). # # For CSV/JSON files, this script will use the column called 'text' or the first column if no column called # 'text' is found. You can easily tweak this behavior (see below). if args.dataset_name is not None: # Downloading and loading a dataset from the hub. __lowerCAmelCase : List[Any] =load_dataset(args.dataset_name, args.dataset_config_name) else: raise ValueError("Evaluation requires a dataset name") # See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at # https://huggingface.co/docs/datasets/loading_datasets.html. # Preprocessing the datasets. # Preprocessing is slighlty different for training and evaluation. __lowerCAmelCase : Optional[Any] =raw_datasets["validation"].column_names __lowerCAmelCase : Optional[Any] ="question" if "question" in column_names else column_names[0] __lowerCAmelCase : str ="context" if "context" in column_names else column_names[1] __lowerCAmelCase : Optional[Any] ="answers" if "answers" in column_names else column_names[2] # Padding side determines if we do (question|context) or (context|question). __lowerCAmelCase : Any =tokenizer.padding_side == "right" if args.max_seq_length > tokenizer.model_max_length: logger.warning( f"""The max_seq_length passed ({args.max_seq_length}) is larger than the maximum length for the""" f"""model ({tokenizer.model_max_length}). Using max_seq_length={tokenizer.model_max_length}.""" ) __lowerCAmelCase : Any =min(args.max_seq_length, tokenizer.model_max_length) def UpperCamelCase ( _lowerCamelCase : Optional[int] ): # Some of the questions have lots of whitespace on the left, which is not useful and will make the # truncation of the context fail (the tokenized question will take a lots of space). So we remove that # left whitespace A__ = [q.lstrip() for q in examples[question_column_name]] # Tokenize our examples with truncation and maybe padding, but keep the overflows using a stride. This results # in one example possible giving several features when a context is long, each of those features having a # context that overlaps a bit the context of the previous feature. A__ = tokenizer( examples[question_column_name if pad_on_right else context_column_name] , examples[context_column_name if pad_on_right else question_column_name] , truncation="only_second" if pad_on_right else "only_first" , max_length=_lowerCamelCase , stride=args.doc_stride , return_overflowing_tokens=_lowerCamelCase , return_offsets_mapping=_lowerCamelCase , padding="max_length" , ) # Since one example might give us several features if it has a long context, we need a map from a feature to # its corresponding example. This key gives us just that. A__ = tokenized_examples.pop("overflow_to_sample_mapping" ) # For evaluation, we will need to convert our predictions to substrings of the context, so we keep the # corresponding example_id and we will store the offset mappings. A__ = [] for i in range(len(tokenized_examples["input_ids"] ) ): # Grab the sequence corresponding to that example (to know what is the context and what is the question). A__ = tokenized_examples.sequence_ids(_lowerCamelCase ) A__ = 1 if pad_on_right else 0 # One example can give several spans, this is the index of the example containing this span of text. A__ = sample_mapping[i] tokenized_examples["example_id"].append(examples["id"][sample_index] ) # Set to None the offset_mapping that are not part of the context so it's easy to determine if a token # position is part of the context or not. A__ = [ (o if sequence_ids[k] == context_index else None) for k, o in enumerate(tokenized_examples["offset_mapping"][i] ) ] return tokenized_examples __lowerCAmelCase : str =raw_datasets["validation"] # Validation Feature Creation __lowerCAmelCase : Union[str, Any] =eval_examples.map( prepare_validation_features, batched=True, num_proc=args.preprocessing_num_workers, remove_columns=column_names, load_from_cache_file=not args.overwrite_cache, desc="Running tokenizer on validation dataset", ) __lowerCAmelCase : List[Any] =default_data_collator __lowerCAmelCase : List[Any] =eval_dataset.remove_columns(["example_id", "offset_mapping"]) __lowerCAmelCase : List[str] =DataLoader( eval_dataset_for_model, collate_fn=data_collator, batch_size=args.per_device_eval_batch_size ) def UpperCamelCase ( _lowerCamelCase : List[str] , _lowerCamelCase : List[Any] , _lowerCamelCase : Optional[int] , _lowerCamelCase : Union[str, Any]="eval" ): # Post-processing: we match the start logits and end logits to answers in the original context. A__ = postprocess_qa_predictions( examples=_lowerCamelCase , features=_lowerCamelCase , predictions=_lowerCamelCase , version_2_with_negative=args.version_2_with_negative , n_best_size=args.n_best_size , max_answer_length=args.max_answer_length , null_score_diff_threshold=args.null_score_diff_threshold , output_dir=args.output_dir , prefix=_lowerCamelCase , ) # Format the result to the format the metric expects. if args.version_2_with_negative: A__ = [ {"id": k, "prediction_text": v, "no_answer_probability": 0.0} for k, v in predictions.items() ] else: A__ = [{"id": k, "prediction_text": v} for k, v in predictions.items()] A__ = [{"id": ex["id"], "answers": ex[answer_column_name]} for ex in examples] return EvalPrediction(predictions=_lowerCamelCase , label_ids=_lowerCamelCase ) __lowerCAmelCase : Tuple =load_metric("squad_v2" if args.version_2_with_negative else "squad") # Evaluation! logger.info("Loading ONNX model %s for evaluation", args.onnx_model_path) with open(engine_name, "rb") as f, trt.Runtime(TRT_LOGGER) as runtime, runtime.deserialize_cuda_engine( f.read() ) as engine, engine.create_execution_context() as context: # setup for TRT inferrence for i in range(len(input_names)): context.set_binding_shape(i, INPUT_SHAPE) assert context.all_binding_shapes_specified def UpperCamelCase ( _lowerCamelCase : Union[str, Any] ): return trt.volume(engine.get_binding_shape(_lowerCamelCase ) ) * engine.get_binding_dtype(_lowerCamelCase ).itemsize # Allocate device memory for inputs and outputs. __lowerCAmelCase : Any =[cuda.mem_alloc(binding_nbytes(binding)) for binding in engine if engine.binding_is_input(binding)] # Allocate output buffer __lowerCAmelCase : List[Any] =cuda.pagelocked_empty(tuple(context.get_binding_shape(3)), dtype=np.floataa) __lowerCAmelCase : List[str] =cuda.pagelocked_empty(tuple(context.get_binding_shape(4)), dtype=np.floataa) __lowerCAmelCase : List[str] =cuda.mem_alloc(h_outputa.nbytes) __lowerCAmelCase : int =cuda.mem_alloc(h_outputa.nbytes) # Create a stream in which to copy inputs/outputs and run inference. __lowerCAmelCase : Optional[Any] =cuda.Stream() # Evaluation logger.info("***** Running Evaluation *****") logger.info(f""" Num examples = {len(eval_dataset)}""") logger.info(f""" Batch size = {args.per_device_eval_batch_size}""") __lowerCAmelCase : str =0.0 __lowerCAmelCase : Tuple =0 __lowerCAmelCase : List[str] =timeit.default_timer() __lowerCAmelCase : Union[str, Any] =None for step, batch in enumerate(eval_dataloader): __lowerCAmelCase , __lowerCAmelCase : Dict =model_infer(batch, context, d_inputs, h_outputa, h_outputa, d_outputa, d_outputa, stream) total_time += infer_time niter += 1 __lowerCAmelCase , __lowerCAmelCase : List[Any] =outputs __lowerCAmelCase : Tuple =torch.tensor(start_logits) __lowerCAmelCase : Tuple =torch.tensor(end_logits) # necessary to pad predictions and labels for being gathered __lowerCAmelCase : Tuple =accelerator.pad_across_processes(start_logits, dim=1, pad_index=-100) __lowerCAmelCase : Union[str, Any] =accelerator.pad_across_processes(end_logits, dim=1, pad_index=-100) __lowerCAmelCase : int =(accelerator.gather(start_logits).cpu().numpy(), accelerator.gather(end_logits).cpu().numpy()) __lowerCAmelCase : List[Any] =logits if all_preds is None else nested_concat(all_preds, logits, padding_index=-100) if all_preds is not None: __lowerCAmelCase : Dict =nested_truncate(all_preds, len(eval_dataset)) __lowerCAmelCase : Optional[int] =timeit.default_timer() - start_time logger.info(" Evaluation done in total %f secs (%f sec per example)", evalTime, evalTime / len(eval_dataset)) # Inference time from TRT logger.info("Average Inference Time = {:.3f} ms".format(total_time * 1000 / niter)) logger.info("Total Inference Time = {:.3f} ms".format(total_time * 1000)) logger.info("Total Number of Inference = %d", niter) __lowerCAmelCase : Optional[Any] =post_processing_function(eval_examples, eval_dataset, all_preds) __lowerCAmelCase : Optional[Any] =metric.compute(predictions=prediction.predictions, references=prediction.label_ids) logger.info(f"""Evaluation metrics: {eval_metric}""")
123
0
from functools import lru_cache def __UpperCamelCase ( _A : int ) ->set: """simple docstring""" lowerCamelCase_ =2 lowerCamelCase_ =set() while i * i <= n: if n % i: i += 1 else: n //= i factors.add(_A ) if n > 1: factors.add(_A ) return factors @lru_cache def __UpperCamelCase ( _A : int ) ->int: """simple docstring""" return len(unique_prime_factors(_A ) ) def __UpperCamelCase ( _A : list ) ->bool: """simple docstring""" return len(set(_A ) ) in (0, 1) def __UpperCamelCase ( _A : int ) ->list: """simple docstring""" lowerCamelCase_ =2 while True: # Increment each value of a generated range lowerCamelCase_ =[base + i for i in range(_A )] # Run elements through out unique_prime_factors function # Append our target number to the end. lowerCamelCase_ =[upf_len(_A ) for x in group] checker.append(_A ) # If all numbers in the list are equal, return the group variable. if equality(_A ): return group # Increment our base variable by 1 base += 1 def __UpperCamelCase ( _A : int = 4 ) ->int: """simple docstring""" lowerCamelCase_ =run(_A ) return results[0] if len(_A ) else None if __name__ == "__main__": print(solution())
154
import inspect import re from transformers.utils import direct_transformers_import # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_config_docstrings.py __A : str = 'src/transformers' # This is to make sure the transformers module imported is the one in the repo. __A : Dict = direct_transformers_import(PATH_TO_TRANSFORMERS) __A : Optional[Any] = transformers.models.auto.configuration_auto.CONFIG_MAPPING # Regex pattern used to find the checkpoint mentioned in the docstring of `config_class`. # For example, `[bert-base-uncased](https://huggingface.co/bert-base-uncased)` __A : Tuple = re.compile(R'\[(.+?)\]\((https://huggingface\.co/.+?)\)') __A : Dict = { 'DecisionTransformerConfig', 'EncoderDecoderConfig', 'MusicgenConfig', 'RagConfig', 'SpeechEncoderDecoderConfig', 'TimmBackboneConfig', 'VisionEncoderDecoderConfig', 'VisionTextDualEncoderConfig', 'LlamaConfig', } def __UpperCamelCase ( _A : Optional[Any] ) ->Union[str, Any]: """simple docstring""" lowerCamelCase_ =None # source code of `config_class` lowerCamelCase_ =inspect.getsource(_A ) lowerCamelCase_ =_re_checkpoint.findall(_A ) # Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link. # For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')` for ckpt_name, ckpt_link in checkpoints: # allow the link to end with `/` if ckpt_link.endswith("""/""" ): lowerCamelCase_ =ckpt_link[:-1] # verify the checkpoint name corresponds to the checkpoint link lowerCamelCase_ =f'https://huggingface.co/{ckpt_name}' if ckpt_link == ckpt_link_from_name: lowerCamelCase_ =ckpt_name break return checkpoint def __UpperCamelCase ( ) ->Tuple: """simple docstring""" lowerCamelCase_ =[] for config_class in list(CONFIG_MAPPING.values() ): # Skip deprecated models if "models.deprecated" in config_class.__module__: continue lowerCamelCase_ =get_checkpoint_from_config_class(_A ) lowerCamelCase_ =config_class.__name__ if checkpoint is None and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK: configs_without_checkpoint.append(_A ) if len(_A ) > 0: lowerCamelCase_ ="""\n""".join(sorted(_A ) ) raise ValueError(f'The following configurations don\'t contain any valid checkpoint:\n{message}' ) if __name__ == "__main__": check_config_docstrings_have_checkpoints()
154
1
import inspect import os import unittest import torch import accelerate from accelerate import debug_launcher from accelerate.test_utils import ( execute_subprocess_async, require_cpu, require_huggingface_suite, require_multi_gpu, require_single_gpu, ) from accelerate.utils import patch_environment @require_huggingface_suite class a_ ( unittest.TestCase ): '''simple docstring''' def _lowercase ( self ) -> Optional[Any]: '''simple docstring''' lowerCAmelCase_ = inspect.getfile(accelerate.test_utils ) lowerCAmelCase_ = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ['scripts', 'external_deps', 'test_metrics.py'] ) from accelerate.test_utils.scripts.external_deps import test_metrics # noqa: F401 lowerCAmelCase_ = test_metrics @require_cpu def _lowercase ( self ) -> Any: '''simple docstring''' debug_launcher(self.test_metrics.main , num_processes=1 ) @require_cpu def _lowercase ( self ) -> List[Any]: '''simple docstring''' debug_launcher(self.test_metrics.main ) @require_single_gpu def _lowercase ( self ) -> Union[str, Any]: '''simple docstring''' self.test_metrics.main() @require_multi_gpu def _lowercase ( self ) -> Tuple: '''simple docstring''' print(f'''Found {torch.cuda.device_count()} devices.''' ) lowerCAmelCase_ = ['torchrun', f'''--nproc_per_node={torch.cuda.device_count()}''', self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(lowercase_ , env=os.environ.copy() )
14
from __future__ import annotations lowerCamelCase_ = 1_0 def lowerCamelCase ( a_ ) -> list[int]: lowerCAmelCase_ = 1 lowerCAmelCase_ = max(a_ ) while placement <= max_digit: # declare and initialize empty buckets lowerCAmelCase_ = [[] for _ in range(a_ )] # split list_of_ints between the buckets for i in list_of_ints: lowerCAmelCase_ = int((i / placement) % RADIX ) buckets[tmp].append(a_ ) # put each buckets' contents into list_of_ints lowerCAmelCase_ = 0 for b in range(a_ ): for i in buckets[b]: lowerCAmelCase_ = i a += 1 # move to next placement *= RADIX return list_of_ints if __name__ == "__main__": import doctest doctest.testmod()
14
1
import argparse import json import logging import os import shutil import sys import tempfile import unittest from unittest import mock import torch from accelerate.utils import write_basic_config from transformers.testing_utils import TestCasePlus, get_gpu_count, run_command, slow, torch_device from transformers.utils import is_apex_available logging.basicConfig(level=logging.DEBUG) __A =logging.getLogger() def a ( ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = argparse.ArgumentParser() parser.add_argument('''-f''' ) __UpperCAmelCase : Optional[int] = parser.parse_args() return args.f def a ( _UpperCAmelCase : Dict ): '''simple docstring''' __UpperCAmelCase : Optional[Any] = {} __UpperCAmelCase : Optional[int] = os.path.join(_UpperCAmelCase , '''all_results.json''' ) if os.path.exists(_UpperCAmelCase ): with open(_UpperCAmelCase , '''r''' ) as f: __UpperCAmelCase : Union[str, Any] = json.load(_UpperCAmelCase ) else: raise ValueError(f'can\'t find {path}' ) return results def a ( ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = torch.cuda.is_available() and torch_device == '''cuda''' return is_using_cuda and is_apex_available() __A =logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class UpperCAmelCase__ ( __UpperCamelCase ): '''simple docstring''' @classmethod def snake_case__ ( cls : List[Any] ): '''simple docstring''' __UpperCAmelCase : Dict = tempfile.mkdtemp() __UpperCAmelCase : List[Any] = os.path.join(cls.tmpdir , '''default_config.yml''' ) write_basic_config(save_location=cls.configPath ) __UpperCAmelCase : str = ['''accelerate''', '''launch''', '''--config_file''', cls.configPath] @classmethod def snake_case__ ( cls : Optional[int] ): '''simple docstring''' shutil.rmtree(cls.tmpdir ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def snake_case__ ( self : Any ): '''simple docstring''' __UpperCAmelCase : Optional[int] = self.get_auto_remove_tmp_dir() __UpperCAmelCase : int = F'\n {self.examples_dir}/pytorch/text-classification/run_glue_no_trainer.py\n --model_name_or_path distilbert-base-uncased\n --output_dir {tmp_dir}\n --train_file ./tests/fixtures/tests_samples/MRPC/train.csv\n --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --learning_rate=1e-4\n --seed=42\n --checkpointing_steps epoch\n --with_tracking\n '.split() if is_cuda_and_apex_available(): testargs.append('''--fp16''' ) run_command(self._launch_args + testargs ) __UpperCAmelCase : Optional[Any] = get_results(a_ ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.7_5 ) self.assertTrue(os.path.exists(os.path.join(a_ , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(a_ , '''glue_no_trainer''' ) ) ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def snake_case__ ( self : Union[str, Any] ): '''simple docstring''' __UpperCAmelCase : Any = self.get_auto_remove_tmp_dir() __UpperCAmelCase : int = F'\n {self.examples_dir}/pytorch/language-modeling/run_clm_no_trainer.py\n --model_name_or_path distilgpt2\n --train_file ./tests/fixtures/sample_text.txt\n --validation_file ./tests/fixtures/sample_text.txt\n --block_size 128\n --per_device_train_batch_size 5\n --per_device_eval_batch_size 5\n --num_train_epochs 2\n --output_dir {tmp_dir}\n --checkpointing_steps epoch\n --with_tracking\n '.split() if torch.cuda.device_count() > 1: # Skipping because there are not enough batches to train the model + would need a drop_last to work. return run_command(self._launch_args + testargs ) __UpperCAmelCase : List[str] = get_results(a_ ) self.assertLess(result['''perplexity'''] , 1_00 ) self.assertTrue(os.path.exists(os.path.join(a_ , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(a_ , '''clm_no_trainer''' ) ) ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def snake_case__ ( self : List[Any] ): '''simple docstring''' __UpperCAmelCase : Any = self.get_auto_remove_tmp_dir() __UpperCAmelCase : Optional[int] = F'\n {self.examples_dir}/pytorch/language-modeling/run_mlm_no_trainer.py\n --model_name_or_path distilroberta-base\n --train_file ./tests/fixtures/sample_text.txt\n --validation_file ./tests/fixtures/sample_text.txt\n --output_dir {tmp_dir}\n --num_train_epochs=1\n --checkpointing_steps epoch\n --with_tracking\n '.split() run_command(self._launch_args + testargs ) __UpperCAmelCase : Optional[int] = get_results(a_ ) self.assertLess(result['''perplexity'''] , 42 ) self.assertTrue(os.path.exists(os.path.join(a_ , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(a_ , '''mlm_no_trainer''' ) ) ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def snake_case__ ( self : List[str] ): '''simple docstring''' __UpperCAmelCase : List[Any] = 7 if get_gpu_count() > 1 else 2 __UpperCAmelCase : Optional[int] = self.get_auto_remove_tmp_dir() __UpperCAmelCase : str = F'\n {self.examples_dir}/pytorch/token-classification/run_ner_no_trainer.py\n --model_name_or_path bert-base-uncased\n --train_file tests/fixtures/tests_samples/conll/sample.json\n --validation_file tests/fixtures/tests_samples/conll/sample.json\n --output_dir {tmp_dir}\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=2\n --num_train_epochs={epochs}\n --seed 7\n --checkpointing_steps epoch\n --with_tracking\n '.split() run_command(self._launch_args + testargs ) __UpperCAmelCase : int = get_results(a_ ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.7_5 ) self.assertLess(result['''train_loss'''] , 0.5 ) self.assertTrue(os.path.exists(os.path.join(a_ , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(a_ , '''ner_no_trainer''' ) ) ) @unittest.skip(reason='''Fix me @muellerzr''' ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def snake_case__ ( self : Dict ): '''simple docstring''' __UpperCAmelCase : List[Any] = self.get_auto_remove_tmp_dir() __UpperCAmelCase : int = F'\n {self.examples_dir}/pytorch/question-answering/run_qa_no_trainer.py\n --model_name_or_path bert-base-uncased\n --version_2_with_negative\n --train_file tests/fixtures/tests_samples/SQUAD/sample.json\n --validation_file tests/fixtures/tests_samples/SQUAD/sample.json\n --output_dir {tmp_dir}\n --seed=42\n --max_train_steps=10\n --num_warmup_steps=2\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --checkpointing_steps epoch\n --with_tracking\n '.split() run_command(self._launch_args + testargs ) __UpperCAmelCase : Any = get_results(a_ ) # Because we use --version_2_with_negative the testing script uses SQuAD v2 metrics. self.assertGreaterEqual(result['''eval_f1'''] , 28 ) self.assertGreaterEqual(result['''eval_exact'''] , 28 ) self.assertTrue(os.path.exists(os.path.join(a_ , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(a_ , '''qa_no_trainer''' ) ) ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def snake_case__ ( self : Optional[Any] ): '''simple docstring''' __UpperCAmelCase : Dict = self.get_auto_remove_tmp_dir() __UpperCAmelCase : int = F'\n {self.examples_dir}/pytorch/multiple-choice/run_swag_no_trainer.py\n --model_name_or_path bert-base-uncased\n --train_file tests/fixtures/tests_samples/swag/sample.json\n --validation_file tests/fixtures/tests_samples/swag/sample.json\n --output_dir {tmp_dir}\n --max_train_steps=20\n --num_warmup_steps=2\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --with_tracking\n '.split() run_command(self._launch_args + testargs ) __UpperCAmelCase : int = get_results(a_ ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.8 ) self.assertTrue(os.path.exists(os.path.join(a_ , '''swag_no_trainer''' ) ) ) @slow @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def snake_case__ ( self : Union[str, Any] ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = self.get_auto_remove_tmp_dir() __UpperCAmelCase : Optional[Any] = F'\n {self.examples_dir}/pytorch/summarization/run_summarization_no_trainer.py\n --model_name_or_path t5-small\n --train_file tests/fixtures/tests_samples/xsum/sample.json\n --validation_file tests/fixtures/tests_samples/xsum/sample.json\n --output_dir {tmp_dir}\n --max_train_steps=50\n --num_warmup_steps=8\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --checkpointing_steps epoch\n --with_tracking\n '.split() run_command(self._launch_args + testargs ) __UpperCAmelCase : Optional[int] = get_results(a_ ) self.assertGreaterEqual(result['''eval_rouge1'''] , 10 ) self.assertGreaterEqual(result['''eval_rouge2'''] , 2 ) self.assertGreaterEqual(result['''eval_rougeL'''] , 7 ) self.assertGreaterEqual(result['''eval_rougeLsum'''] , 7 ) self.assertTrue(os.path.exists(os.path.join(a_ , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(a_ , '''summarization_no_trainer''' ) ) ) @slow @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def snake_case__ ( self : List[str] ): '''simple docstring''' __UpperCAmelCase : Tuple = self.get_auto_remove_tmp_dir() __UpperCAmelCase : Optional[Any] = F'\n {self.examples_dir}/pytorch/translation/run_translation_no_trainer.py\n --model_name_or_path sshleifer/student_marian_en_ro_6_1\n --source_lang en\n --target_lang ro\n --train_file tests/fixtures/tests_samples/wmt16/sample.json\n --validation_file tests/fixtures/tests_samples/wmt16/sample.json\n --output_dir {tmp_dir}\n --max_train_steps=50\n --num_warmup_steps=8\n --num_beams=6\n --learning_rate=3e-3\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --source_lang en_XX\n --target_lang ro_RO\n --checkpointing_steps epoch\n --with_tracking\n '.split() run_command(self._launch_args + testargs ) __UpperCAmelCase : Optional[Any] = get_results(a_ ) self.assertGreaterEqual(result['''eval_bleu'''] , 30 ) self.assertTrue(os.path.exists(os.path.join(a_ , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(a_ , '''translation_no_trainer''' ) ) ) @slow def snake_case__ ( self : str ): '''simple docstring''' __UpperCAmelCase : Dict = logging.StreamHandler(sys.stdout ) logger.addHandler(a_ ) __UpperCAmelCase : Optional[Any] = self.get_auto_remove_tmp_dir() __UpperCAmelCase : str = F'\n {self.examples_dir}/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py\n --dataset_name huggingface/semantic-segmentation-test-sample\n --output_dir {tmp_dir}\n --max_train_steps=10\n --num_warmup_steps=2\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --checkpointing_steps epoch\n '.split() run_command(self._launch_args + testargs ) __UpperCAmelCase : Dict = get_results(a_ ) self.assertGreaterEqual(result['''eval_overall_accuracy'''] , 0.1_0 ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def snake_case__ ( self : List[Any] ): '''simple docstring''' __UpperCAmelCase : Optional[Any] = self.get_auto_remove_tmp_dir() __UpperCAmelCase : Optional[int] = F'\n {self.examples_dir}/pytorch/image-classification/run_image_classification_no_trainer.py\n --model_name_or_path google/vit-base-patch16-224-in21k\n --dataset_name hf-internal-testing/cats_vs_dogs_sample\n --learning_rate 1e-4\n --per_device_train_batch_size 2\n --per_device_eval_batch_size 1\n --max_train_steps 2\n --train_val_split 0.1\n --seed 42\n --output_dir {tmp_dir}\n --with_tracking\n --checkpointing_steps 1\n '.split() if is_cuda_and_apex_available(): testargs.append('''--fp16''' ) run_command(self._launch_args + testargs ) __UpperCAmelCase : int = get_results(a_ ) # The base model scores a 25% self.assertGreaterEqual(result['''eval_accuracy'''] , 0.6 ) self.assertTrue(os.path.exists(os.path.join(a_ , '''step_1''' ) ) ) self.assertTrue(os.path.exists(os.path.join(a_ , '''image_classification_no_trainer''' ) ) )
226
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType __A =logging.get_logger(__name__) __A ={ "microsoft/deberta-v2-xlarge": "https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json", "microsoft/deberta-v2-xxlarge": "https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json", "microsoft/deberta-v2-xlarge-mnli": ( "https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json" ), "microsoft/deberta-v2-xxlarge-mnli": ( "https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json" ), } class UpperCAmelCase__ ( __UpperCamelCase ): '''simple docstring''' UpperCamelCase = """deberta-v2""" def __init__( self : Optional[int] , a_ : List[str]=12_81_00 , a_ : Optional[Any]=15_36 , a_ : Optional[Any]=24 , a_ : List[Any]=24 , a_ : Optional[int]=61_44 , a_ : List[Any]="gelu" , a_ : Any=0.1 , a_ : Tuple=0.1 , a_ : Optional[Any]=5_12 , a_ : Tuple=0 , a_ : Dict=0.0_2 , a_ : Optional[Any]=1e-7 , a_ : List[str]=False , a_ : List[Any]=-1 , a_ : List[str]=0 , a_ : Optional[Any]=True , a_ : List[Any]=None , a_ : Optional[int]=0 , a_ : Tuple="gelu" , **a_ : List[str] , ): '''simple docstring''' super().__init__(**a_ ) __UpperCAmelCase : Any = hidden_size __UpperCAmelCase : Any = num_hidden_layers __UpperCAmelCase : List[Any] = num_attention_heads __UpperCAmelCase : Union[str, Any] = intermediate_size __UpperCAmelCase : List[str] = hidden_act __UpperCAmelCase : int = hidden_dropout_prob __UpperCAmelCase : List[str] = attention_probs_dropout_prob __UpperCAmelCase : str = max_position_embeddings __UpperCAmelCase : int = type_vocab_size __UpperCAmelCase : Tuple = initializer_range __UpperCAmelCase : Optional[int] = relative_attention __UpperCAmelCase : int = max_relative_positions __UpperCAmelCase : Any = pad_token_id __UpperCAmelCase : int = position_biased_input # Backwards compatibility if type(a_ ) == str: __UpperCAmelCase : Optional[Any] = [x.strip() for x in pos_att_type.lower().split('''|''' )] __UpperCAmelCase : Tuple = pos_att_type __UpperCAmelCase : int = vocab_size __UpperCAmelCase : Optional[Any] = layer_norm_eps __UpperCAmelCase : str = kwargs.get('''pooler_hidden_size''' , a_ ) __UpperCAmelCase : Union[str, Any] = pooler_dropout __UpperCAmelCase : Tuple = pooler_hidden_act class UpperCAmelCase__ ( __UpperCamelCase ): '''simple docstring''' @property def snake_case__ ( self : Optional[Any] ): '''simple docstring''' if self.task == "multiple-choice": __UpperCAmelCase : List[Any] = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: __UpperCAmelCase : int = {0: '''batch''', 1: '''sequence'''} if self._config.type_vocab_size > 0: return OrderedDict( [('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis)] ) else: return OrderedDict([('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis)] ) @property def snake_case__ ( self : Union[str, Any] ): '''simple docstring''' return 12 def snake_case__ ( self : Any , a_ : Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"] , a_ : int = -1 , a_ : int = -1 , a_ : int = -1 , a_ : bool = False , a_ : Optional["TensorType"] = None , a_ : int = 3 , a_ : int = 40 , a_ : int = 40 , a_ : "PreTrainedTokenizerBase" = None , ): '''simple docstring''' __UpperCAmelCase : List[Any] = super().generate_dummy_inputs(preprocessor=a_ , framework=a_ ) if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs: del dummy_inputs["token_type_ids"] return dummy_inputs
226
1
'''simple docstring''' import gc import unittest import torch from parameterized import parameterized from diffusers import AutoencoderKL from diffusers.utils import floats_tensor, load_hf_numpy, require_torch_gpu, slow, torch_all_close, torch_device from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin, UNetTesterMixin enable_full_determinism() class a__ ( lowerCamelCase_ , lowerCamelCase_ , unittest.TestCase ): _SCREAMING_SNAKE_CASE : List[Any] = AutoencoderKL _SCREAMING_SNAKE_CASE : List[Any] = 'sample' _SCREAMING_SNAKE_CASE : List[Any] = 1E-2 @property def _lowerCamelCase ( self ): """simple docstring""" _lowercase : Union[str, Any] = 4 _lowercase : Dict = 3 _lowercase : Dict = (32, 32) _lowercase : List[Any] = floats_tensor((batch_size, num_channels) + sizes ).to(_UpperCamelCase ) return {"sample": image} @property def _lowerCamelCase ( self ): """simple docstring""" return (3, 32, 32) @property def _lowerCamelCase ( self ): """simple docstring""" return (3, 32, 32) def _lowerCamelCase ( self ): """simple docstring""" _lowercase : Optional[int] = { "block_out_channels": [32, 64], "in_channels": 3, "out_channels": 3, "down_block_types": ["DownEncoderBlock2D", "DownEncoderBlock2D"], "up_block_types": ["UpDecoderBlock2D", "UpDecoderBlock2D"], "latent_channels": 4, } _lowercase : Optional[Any] = self.dummy_input return init_dict, inputs_dict def _lowerCamelCase ( self ): """simple docstring""" pass def _lowerCamelCase ( self ): """simple docstring""" pass @unittest.skipIf(torch_device == "mps" , "Gradient checkpointing skipped on MPS" ) def _lowerCamelCase ( self ): """simple docstring""" _lowercase , _lowercase : Union[str, Any] = self.prepare_init_args_and_inputs_for_common() _lowercase : Union[str, Any] = self.model_class(**_UpperCamelCase ) model.to(_UpperCamelCase ) assert not model.is_gradient_checkpointing and model.training _lowercase : List[Any] = model(**_UpperCamelCase ).sample # run the backwards pass on the model. For backwards pass, for simplicity purpose, # we won't calculate the loss and rather backprop on out.sum() model.zero_grad() _lowercase : Optional[int] = torch.randn_like(_UpperCamelCase ) _lowercase : Optional[int] = (out - labels).mean() loss.backward() # re-instantiate the model now enabling gradient checkpointing _lowercase : List[Any] = self.model_class(**_UpperCamelCase ) # clone model model_a.load_state_dict(model.state_dict() ) model_a.to(_UpperCamelCase ) model_a.enable_gradient_checkpointing() assert model_a.is_gradient_checkpointing and model_a.training _lowercase : Any = model_a(**_UpperCamelCase ).sample # run the backwards pass on the model. For backwards pass, for simplicity purpose, # we won't calculate the loss and rather backprop on out.sum() model_a.zero_grad() _lowercase : Tuple = (out_a - labels).mean() loss_a.backward() # compare the output and parameters gradients self.assertTrue((loss - loss_a).abs() < 1E-5 ) _lowercase : Tuple = dict(model.named_parameters() ) _lowercase : Union[str, Any] = dict(model_a.named_parameters() ) for name, param in named_params.items(): self.assertTrue(torch_all_close(param.grad.data , named_params_a[name].grad.data , atol=5E-5 ) ) def _lowerCamelCase ( self ): """simple docstring""" _lowercase , _lowercase : Tuple = AutoencoderKL.from_pretrained("fusing/autoencoder-kl-dummy" , output_loading_info=_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) self.assertEqual(len(loading_info["missing_keys"] ) , 0 ) model.to(_UpperCamelCase ) _lowercase : Optional[Any] = model(**self.dummy_input ) assert image is not None, "Make sure output is not None" def _lowerCamelCase ( self ): """simple docstring""" _lowercase : Tuple = AutoencoderKL.from_pretrained("fusing/autoencoder-kl-dummy" ) _lowercase : int = model.to(_UpperCamelCase ) model.eval() if torch_device == "mps": _lowercase : int = torch.manual_seed(0 ) else: _lowercase : List[str] = torch.Generator(device=_UpperCamelCase ).manual_seed(0 ) _lowercase : Optional[Any] = torch.randn( 1 , model.config.in_channels , model.config.sample_size , model.config.sample_size , generator=torch.manual_seed(0 ) , ) _lowercase : str = image.to(_UpperCamelCase ) with torch.no_grad(): _lowercase : Optional[int] = model(_UpperCamelCase , sample_posterior=_UpperCamelCase , generator=_UpperCamelCase ).sample _lowercase : Optional[Any] = output[0, -1, -3:, -3:].flatten().cpu() # Since the VAE Gaussian prior's generator is seeded on the appropriate device, # the expected output slices are not the same for CPU and GPU. if torch_device == "mps": _lowercase : List[Any] = torch.tensor( [ -4.0_0_7_8E-0_1, -3.8_3_2_3E-0_4, -1.2_6_8_1E-0_1, -1.1_4_6_2E-0_1, 2.0_0_9_5E-0_1, 1.0_8_9_3E-0_1, -8.8_2_4_7E-0_2, -3.0_3_6_1E-0_1, -9.8_6_4_4E-0_3, ] ) elif torch_device == "cpu": _lowercase : Tuple = torch.tensor( [-0.1_3_5_2, 0.0_8_7_8, 0.0_4_1_9, -0.0_8_1_8, -0.1_0_6_9, 0.0_6_8_8, -0.1_4_5_8, -0.4_4_4_6, -0.0_0_2_6] ) else: _lowercase : Union[str, Any] = torch.tensor( [-0.2_4_2_1, 0.4_6_4_2, 0.2_5_0_7, -0.0_4_3_8, 0.0_6_8_2, 0.3_1_6_0, -0.2_0_1_8, -0.0_7_2_7, 0.2_4_8_5] ) self.assertTrue(torch_all_close(_UpperCamelCase , _UpperCamelCase , rtol=1E-2 ) ) @slow class a__ ( unittest.TestCase ): def _lowerCamelCase ( self , _UpperCamelCase , _UpperCamelCase ): """simple docstring""" return f'''gaussian_noise_s={seed}_shape={'_'.join([str(_UpperCamelCase ) for s in shape] )}.npy''' def _lowerCamelCase ( self ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowerCamelCase ( self , _UpperCamelCase=0 , _UpperCamelCase=(4, 3, 512, 512) , _UpperCamelCase=False ): """simple docstring""" _lowercase : Optional[int] = torch.floataa if fpaa else torch.floataa _lowercase : Optional[int] = torch.from_numpy(load_hf_numpy(self.get_file_format(_UpperCamelCase , _UpperCamelCase ) ) ).to(_UpperCamelCase ).to(_UpperCamelCase ) return image def _lowerCamelCase ( self , _UpperCamelCase="CompVis/stable-diffusion-v1-4" , _UpperCamelCase=False ): """simple docstring""" _lowercase : List[Any] = "fp16" if fpaa else None _lowercase : str = torch.floataa if fpaa else torch.floataa _lowercase : List[str] = AutoencoderKL.from_pretrained( _UpperCamelCase , subfolder="vae" , torch_dtype=_UpperCamelCase , revision=_UpperCamelCase , ) model.to(_UpperCamelCase ).eval() return model def _lowerCamelCase ( self , _UpperCamelCase=0 ): """simple docstring""" if torch_device == "mps": return torch.manual_seed(_UpperCamelCase ) return torch.Generator(device=_UpperCamelCase ).manual_seed(_UpperCamelCase ) @parameterized.expand( [ # fmt: off [33, [-0.1_6_0_3, 0.9_8_7_8, -0.0_4_9_5, -0.0_7_9_0, -0.2_7_0_9, 0.8_3_7_5, -0.2_0_6_0, -0.0_8_2_4], [-0.2_3_9_5, 0.0_0_9_8, 0.0_1_0_2, -0.0_7_0_9, -0.2_8_4_0, -0.0_2_7_4, -0.0_7_1_8, -0.1_8_2_4]], [47, [-0.2_3_7_6, 0.1_1_6_8, 0.1_3_3_2, -0.4_8_4_0, -0.2_5_0_8, -0.0_7_9_1, -0.0_4_9_3, -0.4_0_8_9], [0.0_3_5_0, 0.0_8_4_7, 0.0_4_6_7, 0.0_3_4_4, -0.0_8_4_2, -0.0_5_4_7, -0.0_6_3_3, -0.1_1_3_1]], # fmt: on ] ) def _lowerCamelCase ( self , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): """simple docstring""" _lowercase : Dict = self.get_sd_vae_model() _lowercase : Dict = self.get_sd_image(_UpperCamelCase ) _lowercase : List[Any] = self.get_generator(_UpperCamelCase ) with torch.no_grad(): _lowercase : Any = model(_UpperCamelCase , generator=_UpperCamelCase , sample_posterior=_UpperCamelCase ).sample assert sample.shape == image.shape _lowercase : Dict = sample[-1, -2:, -2:, :2].flatten().float().cpu() _lowercase : Any = torch.tensor(expected_slice_mps if torch_device == "mps" else expected_slice ) assert torch_all_close(_UpperCamelCase , _UpperCamelCase , atol=3E-3 ) @parameterized.expand( [ # fmt: off [33, [-0.0_5_1_3, 0.0_2_8_9, 1.3_7_9_9, 0.2_1_6_6, -0.2_5_7_3, -0.0_8_7_1, 0.5_1_0_3, -0.0_9_9_9]], [47, [-0.4_1_2_8, -0.1_3_2_0, -0.3_7_0_4, 0.1_9_6_5, -0.4_1_1_6, -0.2_3_3_2, -0.3_3_4_0, 0.2_2_4_7]], # fmt: on ] ) @require_torch_gpu def _lowerCamelCase ( self , _UpperCamelCase , _UpperCamelCase ): """simple docstring""" _lowercase : Any = self.get_sd_vae_model(fpaa=_UpperCamelCase ) _lowercase : List[str] = self.get_sd_image(_UpperCamelCase , fpaa=_UpperCamelCase ) _lowercase : Any = self.get_generator(_UpperCamelCase ) with torch.no_grad(): _lowercase : Tuple = model(_UpperCamelCase , generator=_UpperCamelCase , sample_posterior=_UpperCamelCase ).sample assert sample.shape == image.shape _lowercase : List[Any] = sample[-1, -2:, :2, -2:].flatten().float().cpu() _lowercase : List[str] = torch.tensor(_UpperCamelCase ) assert torch_all_close(_UpperCamelCase , _UpperCamelCase , atol=1E-2 ) @parameterized.expand( [ # fmt: off [33, [-0.1_6_0_9, 0.9_8_6_6, -0.0_4_8_7, -0.0_7_7_7, -0.2_7_1_6, 0.8_3_6_8, -0.2_0_5_5, -0.0_8_1_4], [-0.2_3_9_5, 0.0_0_9_8, 0.0_1_0_2, -0.0_7_0_9, -0.2_8_4_0, -0.0_2_7_4, -0.0_7_1_8, -0.1_8_2_4]], [47, [-0.2_3_7_7, 0.1_1_4_7, 0.1_3_3_3, -0.4_8_4_1, -0.2_5_0_6, -0.0_8_0_5, -0.0_4_9_1, -0.4_0_8_5], [0.0_3_5_0, 0.0_8_4_7, 0.0_4_6_7, 0.0_3_4_4, -0.0_8_4_2, -0.0_5_4_7, -0.0_6_3_3, -0.1_1_3_1]], # fmt: on ] ) def _lowerCamelCase ( self , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): """simple docstring""" _lowercase : Optional[int] = self.get_sd_vae_model() _lowercase : Tuple = self.get_sd_image(_UpperCamelCase ) with torch.no_grad(): _lowercase : Dict = model(_UpperCamelCase ).sample assert sample.shape == image.shape _lowercase : Tuple = sample[-1, -2:, -2:, :2].flatten().float().cpu() _lowercase : Any = torch.tensor(expected_slice_mps if torch_device == "mps" else expected_slice ) assert torch_all_close(_UpperCamelCase , _UpperCamelCase , atol=3E-3 ) @parameterized.expand( [ # fmt: off [13, [-0.2_0_5_1, -0.1_8_0_3, -0.2_3_1_1, -0.2_1_1_4, -0.3_2_9_2, -0.3_5_7_4, -0.2_9_5_3, -0.3_3_2_3]], [37, [-0.2_6_3_2, -0.2_6_2_5, -0.2_1_9_9, -0.2_7_4_1, -0.4_5_3_9, -0.4_9_9_0, -0.3_7_2_0, -0.4_9_2_5]], # fmt: on ] ) @require_torch_gpu def _lowerCamelCase ( self , _UpperCamelCase , _UpperCamelCase ): """simple docstring""" _lowercase : List[str] = self.get_sd_vae_model() _lowercase : Union[str, Any] = self.get_sd_image(_UpperCamelCase , shape=(3, 4, 64, 64) ) with torch.no_grad(): _lowercase : Any = model.decode(_UpperCamelCase ).sample assert list(sample.shape ) == [3, 3, 512, 512] _lowercase : Union[str, Any] = sample[-1, -2:, :2, -2:].flatten().cpu() _lowercase : Any = torch.tensor(_UpperCamelCase ) assert torch_all_close(_UpperCamelCase , _UpperCamelCase , atol=1E-3 ) @parameterized.expand( [ # fmt: off [27, [-0.0_3_6_9, 0.0_2_0_7, -0.0_7_7_6, -0.0_6_8_2, -0.1_7_4_7, -0.1_9_3_0, -0.1_4_6_5, -0.2_0_3_9]], [16, [-0.1_6_2_8, -0.2_1_3_4, -0.2_7_4_7, -0.2_6_4_2, -0.3_7_7_4, -0.4_4_0_4, -0.3_6_8_7, -0.4_2_7_7]], # fmt: on ] ) @require_torch_gpu def _lowerCamelCase ( self , _UpperCamelCase , _UpperCamelCase ): """simple docstring""" _lowercase : Any = self.get_sd_vae_model(fpaa=_UpperCamelCase ) _lowercase : Any = self.get_sd_image(_UpperCamelCase , shape=(3, 4, 64, 64) , fpaa=_UpperCamelCase ) with torch.no_grad(): _lowercase : int = model.decode(_UpperCamelCase ).sample assert list(sample.shape ) == [3, 3, 512, 512] _lowercase : Dict = sample[-1, -2:, :2, -2:].flatten().float().cpu() _lowercase : List[str] = torch.tensor(_UpperCamelCase ) assert torch_all_close(_UpperCamelCase , _UpperCamelCase , atol=5E-3 ) @parameterized.expand([(13,), (16,), (27,)] ) @require_torch_gpu @unittest.skipIf(not is_xformers_available() , reason="xformers is not required when using PyTorch 2.0." ) def _lowerCamelCase ( self , _UpperCamelCase ): """simple docstring""" _lowercase : Dict = self.get_sd_vae_model(fpaa=_UpperCamelCase ) _lowercase : Tuple = self.get_sd_image(_UpperCamelCase , shape=(3, 4, 64, 64) , fpaa=_UpperCamelCase ) with torch.no_grad(): _lowercase : int = model.decode(_UpperCamelCase ).sample model.enable_xformers_memory_efficient_attention() with torch.no_grad(): _lowercase : List[Any] = model.decode(_UpperCamelCase ).sample assert list(sample.shape ) == [3, 3, 512, 512] assert torch_all_close(_UpperCamelCase , _UpperCamelCase , atol=1E-1 ) @parameterized.expand([(13,), (16,), (37,)] ) @require_torch_gpu @unittest.skipIf(not is_xformers_available() , reason="xformers is not required when using PyTorch 2.0." ) def _lowerCamelCase ( self , _UpperCamelCase ): """simple docstring""" _lowercase : Optional[Any] = self.get_sd_vae_model() _lowercase : List[Any] = self.get_sd_image(_UpperCamelCase , shape=(3, 4, 64, 64) ) with torch.no_grad(): _lowercase : Any = model.decode(_UpperCamelCase ).sample model.enable_xformers_memory_efficient_attention() with torch.no_grad(): _lowercase : List[Any] = model.decode(_UpperCamelCase ).sample assert list(sample.shape ) == [3, 3, 512, 512] assert torch_all_close(_UpperCamelCase , _UpperCamelCase , atol=1E-2 ) @parameterized.expand( [ # fmt: off [33, [-0.3_0_0_1, 0.0_9_1_8, -2.6_9_8_4, -3.9_7_2_0, -3.2_0_9_9, -5.0_3_5_3, 1.7_3_3_8, -0.2_0_6_5, 3.4_2_6_7]], [47, [-1.5_0_3_0, -4.3_8_7_1, -6.0_3_5_5, -9.1_1_5_7, -1.6_6_6_1, -2.7_8_5_3, 2.1_6_0_7, -5.0_8_2_3, 2.5_6_3_3]], # fmt: on ] ) def _lowerCamelCase ( self , _UpperCamelCase , _UpperCamelCase ): """simple docstring""" _lowercase : Optional[Any] = self.get_sd_vae_model() _lowercase : str = self.get_sd_image(_UpperCamelCase ) _lowercase : Union[str, Any] = self.get_generator(_UpperCamelCase ) with torch.no_grad(): _lowercase : Optional[int] = model.encode(_UpperCamelCase ).latent_dist _lowercase : Union[str, Any] = dist.sample(generator=_UpperCamelCase ) assert list(sample.shape ) == [image.shape[0], 4] + [i // 8 for i in image.shape[2:]] _lowercase : Dict = sample[0, -1, -3:, -3:].flatten().cpu() _lowercase : Union[str, Any] = torch.tensor(_UpperCamelCase ) _lowercase : int = 3E-3 if torch_device != "mps" else 1E-2 assert torch_all_close(_UpperCamelCase , _UpperCamelCase , atol=_UpperCamelCase )
199
'''simple docstring''' import json import logging import os import re import sys from dataclasses import dataclass, field from typing import Any, Dict, List, Optional, Union import datasets import numpy as np import torch import torchaudio from packaging import version from torch import nn import transformers from transformers import ( HfArgumentParser, Trainer, TrainingArguments, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaForCTC, WavaVecaProcessor, is_apex_available, set_seed, ) from transformers.trainer_utils import get_last_checkpoint, is_main_process if is_apex_available(): from apex import amp if version.parse(version.parse(torch.__version__).base_version) >= version.parse('1.6'): _snake_case = True from torch.cuda.amp import autocast _snake_case = logging.getLogger(__name__) def _A ( snake_case=None , snake_case=None ) -> Any: return field(default_factory=lambda: default , metadata=snake_case ) @dataclass class a__ : _SCREAMING_SNAKE_CASE : str = field( metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} ) _SCREAMING_SNAKE_CASE : Optional[str] = field( default=lowerCamelCase_ , metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'} , ) _SCREAMING_SNAKE_CASE : Optional[bool] = field( default=lowerCamelCase_ , metadata={'help': 'Whether to freeze the feature extractor layers of the model.'} ) _SCREAMING_SNAKE_CASE : Optional[float] = field( default=0.1 , metadata={'help': 'The dropout ratio for the attention probabilities.'} ) _SCREAMING_SNAKE_CASE : Optional[float] = field( default=0.1 , metadata={'help': 'The dropout ratio for activations inside the fully connected layer.'} ) _SCREAMING_SNAKE_CASE : Optional[float] = field( default=0.1 , metadata={ 'help': 'The dropout probabilitiy for all fully connected layers in the embeddings, encoder, and pooler.' } , ) _SCREAMING_SNAKE_CASE : Optional[float] = field( default=0.1 , metadata={'help': 'The dropout probabilitiy for all 1D convolutional layers in feature extractor.'} , ) _SCREAMING_SNAKE_CASE : Optional[float] = field( default=0.05 , metadata={ 'help': ( 'Propability of each feature vector along the time axis to be chosen as the start of the vector' 'span to be masked. Approximately ``mask_time_prob * sequence_length // mask_time_length`` feature' 'vectors will be masked along the time axis. This is only relevant if ``apply_spec_augment is True``.' ) } , ) _SCREAMING_SNAKE_CASE : Optional[float] = field(default=0.0 , metadata={'help': 'The LayerDrop probability.'} ) @dataclass class a__ : _SCREAMING_SNAKE_CASE : Optional[str] = field( default=lowerCamelCase_ , metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'} ) _SCREAMING_SNAKE_CASE : Optional[str] = field( default='train+validation' , metadata={ 'help': 'The name of the training data set split to use (via the datasets library). Defaults to \'train\'' } , ) _SCREAMING_SNAKE_CASE : bool = field( default=lowerCamelCase_ , metadata={'help': 'Overwrite the cached preprocessed datasets or not.'} ) _SCREAMING_SNAKE_CASE : Optional[int] = field( default=lowerCamelCase_ , metadata={'help': 'The number of processes to use for the preprocessing.'} , ) _SCREAMING_SNAKE_CASE : Optional[int] = field( default=lowerCamelCase_ , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of training examples to this ' 'value if set.' ) } , ) _SCREAMING_SNAKE_CASE : Optional[int] = field( default=lowerCamelCase_ , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of validation examples to this ' 'value if set.' ) } , ) _SCREAMING_SNAKE_CASE : List[str] = list_field( default=[',', '?', '.', '!', '-', ';', ':', '""', '%', '\'', '"', '�'] , metadata={'help': 'A list of characters to remove from the transcripts.'} , ) @dataclass class a__ : _SCREAMING_SNAKE_CASE : WavaVecaProcessor _SCREAMING_SNAKE_CASE : Union[bool, str] = True _SCREAMING_SNAKE_CASE : Optional[int] = None _SCREAMING_SNAKE_CASE : Optional[int] = None _SCREAMING_SNAKE_CASE : Optional[int] = None _SCREAMING_SNAKE_CASE : Optional[int] = None def __call__( self , _UpperCamelCase ): """simple docstring""" _lowercase : List[str] = [{"input_values": feature["input_values"]} for feature in features] _lowercase : Dict = [{"input_ids": feature["labels"]} for feature in features] _lowercase : Any = self.processor.pad( _UpperCamelCase , padding=self.padding , max_length=self.max_length , pad_to_multiple_of=self.pad_to_multiple_of , return_tensors="pt" , ) _lowercase : Union[str, Any] = self.processor.pad( labels=_UpperCamelCase , padding=self.padding , max_length=self.max_length_labels , pad_to_multiple_of=self.pad_to_multiple_of_labels , return_tensors="pt" , ) # replace padding with -100 to ignore loss correctly _lowercase : List[str] = labels_batch["input_ids"].masked_fill(labels_batch.attention_mask.ne(1 ) , -100 ) _lowercase : Optional[Any] = labels return batch class a__ ( lowerCamelCase_ ): def _lowerCamelCase ( self , _UpperCamelCase , _UpperCamelCase ): """simple docstring""" model.train() _lowercase : str = self._prepare_inputs(_UpperCamelCase ) if self.use_amp: with autocast(): _lowercase : Optional[int] = self.compute_loss(_UpperCamelCase , _UpperCamelCase ) else: _lowercase : Tuple = self.compute_loss(_UpperCamelCase , _UpperCamelCase ) if self.args.n_gpu > 1: if model.module.config.ctc_loss_reduction == "mean": _lowercase : int = loss.mean() elif model.module.config.ctc_loss_reduction == "sum": _lowercase : Any = loss.sum() / (inputs["labels"] >= 0).sum() else: raise ValueError(f'''{model.config.ctc_loss_reduction} is not valid. Choose one of [\'mean\', \'sum\']''' ) if self.args.gradient_accumulation_steps > 1: _lowercase : Optional[int] = loss / self.args.gradient_accumulation_steps if self.use_amp: self.scaler.scale(_UpperCamelCase ).backward() elif self.use_apex: with amp.scale_loss(_UpperCamelCase , self.optimizer ) as scaled_loss: scaled_loss.backward() elif self.deepspeed: self.deepspeed.backward(_UpperCamelCase ) else: loss.backward() return loss.detach() def _A ( ) -> Optional[Any]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. _lowercase : Optional[int] = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(".json" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. _lowercase , _lowercase , _lowercase : List[Any] = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: _lowercase , _lowercase , _lowercase : int = parser.parse_args_into_dataclasses() # Detecting last checkpoint. _lowercase : Union[str, Any] = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: _lowercase : Union[str, Any] = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F'''Output directory ({training_args.output_dir}) already exists and is not empty. ''' "Use --overwrite_output_dir to overcome." ) elif last_checkpoint is not None: logger.info( F'''Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change ''' "the `--output_dir` or add `--overwrite_output_dir` to train from scratch." ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , handlers=[logging.StreamHandler(sys.stdout )] , ) logger.setLevel(logging.INFO if is_main_process(training_args.local_rank ) else logging.WARN ) # Log on each process the small summary: logger.warning( F'''Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}''' + F'''distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}''' ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() logger.info("Training/evaluation parameters %s" , snake_case ) # Set seed before initializing model. set_seed(training_args.seed ) # Get the datasets: _lowercase : Tuple = datasets.load_dataset( "common_voice" , data_args.dataset_config_name , split=data_args.train_split_name ) _lowercase : Any = datasets.load_dataset("common_voice" , data_args.dataset_config_name , split="test" ) # Create and save tokenizer _lowercase : Dict = F'''[{''.join(data_args.chars_to_ignore )}]''' def remove_special_characters(snake_case ): _lowercase : List[str] = re.sub(snake_case , "" , batch["sentence"] ).lower() + " " return batch _lowercase : int = train_dataset.map(snake_case , remove_columns=["sentence"] ) _lowercase : int = eval_dataset.map(snake_case , remove_columns=["sentence"] ) def extract_all_chars(snake_case ): _lowercase : Optional[int] = " ".join(batch["text"] ) _lowercase : int = list(set(snake_case ) ) return {"vocab": [vocab], "all_text": [all_text]} _lowercase : Dict = train_dataset.map( snake_case , batched=snake_case , batch_size=-1 , keep_in_memory=snake_case , remove_columns=train_dataset.column_names , ) _lowercase : List[Any] = train_dataset.map( snake_case , batched=snake_case , batch_size=-1 , keep_in_memory=snake_case , remove_columns=eval_dataset.column_names , ) _lowercase : Dict = list(set(vocab_train["vocab"][0] ) | set(vocab_test["vocab"][0] ) ) _lowercase : List[str] = {v: k for k, v in enumerate(snake_case )} _lowercase : Union[str, Any] = vocab_dict[" "] del vocab_dict[" "] _lowercase : Dict = len(snake_case ) _lowercase : Dict = len(snake_case ) with open("vocab.json" , "w" ) as vocab_file: json.dump(snake_case , snake_case ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. _lowercase : Dict = WavaVecaCTCTokenizer( "vocab.json" , unk_token="[UNK]" , pad_token="[PAD]" , word_delimiter_token="|" , ) _lowercase : List[str] = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_60_00 , padding_value=0.0 , do_normalize=snake_case , return_attention_mask=snake_case ) _lowercase : Optional[int] = WavaVecaProcessor(feature_extractor=snake_case , tokenizer=snake_case ) _lowercase : int = WavaVecaForCTC.from_pretrained( model_args.model_name_or_path , cache_dir=model_args.cache_dir , activation_dropout=model_args.activation_dropout , attention_dropout=model_args.attention_dropout , hidden_dropout=model_args.hidden_dropout , feat_proj_dropout=model_args.feat_proj_dropout , mask_time_prob=model_args.mask_time_prob , gradient_checkpointing=training_args.gradient_checkpointing , layerdrop=model_args.layerdrop , ctc_loss_reduction="mean" , pad_token_id=processor.tokenizer.pad_token_id , vocab_size=len(processor.tokenizer ) , ) if data_args.max_train_samples is not None: _lowercase : Optional[Any] = min(len(snake_case ) , data_args.max_train_samples ) _lowercase : Any = train_dataset.select(range(snake_case ) ) if data_args.max_val_samples is not None: _lowercase : Any = eval_dataset.select(range(data_args.max_val_samples ) ) _lowercase : Dict = torchaudio.transforms.Resample(4_80_00 , 1_60_00 ) # Preprocessing the datasets. # We need to read the aduio files as arrays and tokenize the targets. def speech_file_to_array_fn(snake_case ): _lowercase , _lowercase : List[str] = torchaudio.load(batch["path"] ) _lowercase : List[Any] = resampler(snake_case ).squeeze().numpy() _lowercase : str = 1_60_00 _lowercase : Optional[int] = batch["text"] return batch _lowercase : Dict = train_dataset.map( snake_case , remove_columns=train_dataset.column_names , num_proc=data_args.preprocessing_num_workers , ) _lowercase : List[str] = eval_dataset.map( snake_case , remove_columns=eval_dataset.column_names , num_proc=data_args.preprocessing_num_workers , ) def prepare_dataset(snake_case ): # check that all files have the correct sampling rate assert ( len(set(batch["sampling_rate"] ) ) == 1 ), F'''Make sure all inputs have the same sampling rate of {processor.feature_extractor.sampling_rate}.''' _lowercase : str = processor( audio=batch["speech"] , text=batch["target_text"] , sampling_rate=batch["sampling_rate"][0] ) batch.update(snake_case ) return batch _lowercase : Any = train_dataset.map( snake_case , remove_columns=train_dataset.column_names , batch_size=training_args.per_device_train_batch_size , batched=snake_case , num_proc=data_args.preprocessing_num_workers , ) _lowercase : Dict = eval_dataset.map( snake_case , remove_columns=eval_dataset.column_names , batch_size=training_args.per_device_train_batch_size , batched=snake_case , num_proc=data_args.preprocessing_num_workers , ) # Metric _lowercase : Optional[int] = datasets.load_metric("wer" ) def compute_metrics(snake_case ): _lowercase : Dict = pred.predictions _lowercase : int = np.argmax(snake_case , axis=-1 ) _lowercase : str = processor.tokenizer.pad_token_id _lowercase : Optional[int] = processor.batch_decode(snake_case ) # we do not want to group tokens when computing the metrics _lowercase : int = processor.batch_decode(pred.label_ids , group_tokens=snake_case ) _lowercase : Optional[int] = wer_metric.compute(predictions=snake_case , references=snake_case ) return {"wer": wer} if model_args.freeze_feature_extractor: model.freeze_feature_extractor() # Data collator _lowercase : str = DataCollatorCTCWithPadding(processor=snake_case , padding=snake_case ) # Initialize our Trainer _lowercase : List[str] = CTCTrainer( model=snake_case , data_collator=snake_case , args=snake_case , compute_metrics=snake_case , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , tokenizer=processor.feature_extractor , ) # Training if training_args.do_train: if last_checkpoint is not None: _lowercase : Any = last_checkpoint elif os.path.isdir(model_args.model_name_or_path ): _lowercase : Tuple = model_args.model_name_or_path else: _lowercase : Any = None # Save the feature_extractor and the tokenizer if is_main_process(training_args.local_rank ): processor.save_pretrained(training_args.output_dir ) _lowercase : str = trainer.train(resume_from_checkpoint=snake_case ) trainer.save_model() _lowercase : List[str] = train_result.metrics _lowercase : List[str] = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(snake_case ) ) _lowercase : Any = min(snake_case , len(snake_case ) ) trainer.log_metrics("train" , snake_case ) trainer.save_metrics("train" , snake_case ) trainer.save_state() # Evaluation _lowercase : Dict = {} if training_args.do_eval: logger.info("*** Evaluate ***" ) _lowercase : Optional[Any] = trainer.evaluate() _lowercase : Optional[Any] = data_args.max_val_samples if data_args.max_val_samples is not None else len(snake_case ) _lowercase : Tuple = min(snake_case , len(snake_case ) ) trainer.log_metrics("eval" , snake_case ) trainer.save_metrics("eval" , snake_case ) return results if __name__ == "__main__": main()
199
1
def lowerCAmelCase_ ( __a , __a ) -> int: """simple docstring""" return int((input_a, input_a).count(0 ) == 0 ) def lowerCAmelCase_ ( ) -> None: """simple docstring""" assert and_gate(0 , 0 ) == 0 assert and_gate(0 , 1 ) == 0 assert and_gate(1 , 0 ) == 0 assert and_gate(1 , 1 ) == 1 if __name__ == "__main__": test_and_gate() print(and_gate(1, 0)) print(and_gate(0, 0)) print(and_gate(0, 1)) print(and_gate(1, 1))
10
from math import ceil, sqrt def lowerCAmelCase_ ( __a = 1000000 ) -> int: """simple docstring""" lowerCamelCase__: Optional[int] =0 for outer_width in range(3 , (limit // 4) + 2 ): if outer_width**2 > limit: lowerCamelCase__: Dict =max(ceil(sqrt(outer_width**2 - limit ) ) , 1 ) else: lowerCamelCase__: str =1 if (outer_width - hole_width_lower_bound) % 2: hole_width_lower_bound += 1 answer += (outer_width - hole_width_lower_bound - 2) // 2 + 1 return answer if __name__ == "__main__": print(f'{solution() = }')
10
1
from manim import * class UpperCAmelCase ( A__ ): def _SCREAMING_SNAKE_CASE (self : Optional[Any] ) -> int: '''simple docstring''' snake_case : str = Rectangle(height=0.5 , width=0.5 ) snake_case : str = Rectangle(height=0.25 , width=0.25 ) snake_case : str = Rectangle(height=0.46 , width=0.46 ).set_stroke(width=0 ) snake_case : str = [mem.copy() for i in range(6 )] snake_case : Any = [mem.copy() for i in range(6 )] snake_case : Tuple = VGroup(*lowerCamelCase__ ).arrange(lowerCamelCase__ , buff=0 ) snake_case : Any = VGroup(*lowerCamelCase__ ).arrange(lowerCamelCase__ , buff=0 ) snake_case : List[str] = VGroup(lowerCamelCase__ , lowerCamelCase__ ).arrange(lowerCamelCase__ , buff=0 ) snake_case : Tuple = Text("CPU" , font_size=24 ) snake_case : Any = Group(lowerCamelCase__ , lowerCamelCase__ ).arrange(lowerCamelCase__ , buff=0.5 , aligned_edge=lowerCamelCase__ ) cpu.move_to([-2.5, -0.5, 0] ) self.add(lowerCamelCase__ ) snake_case : int = [mem.copy() for i in range(4 )] snake_case : List[str] = VGroup(*lowerCamelCase__ ).arrange(lowerCamelCase__ , buff=0 ) snake_case : List[Any] = Text("GPU" , font_size=24 ) snake_case : Union[str, Any] = Group(lowerCamelCase__ , lowerCamelCase__ ).arrange(lowerCamelCase__ , buff=0.5 , aligned_edge=lowerCamelCase__ ) gpu.move_to([-1, -1, 0] ) self.add(lowerCamelCase__ ) snake_case : Any = [mem.copy() for i in range(6 )] snake_case : Optional[Any] = VGroup(*lowerCamelCase__ ).arrange(lowerCamelCase__ , buff=0 ) snake_case : List[Any] = Text("Model" , font_size=24 ) snake_case : List[str] = Group(lowerCamelCase__ , lowerCamelCase__ ).arrange(lowerCamelCase__ , buff=0.5 , aligned_edge=lowerCamelCase__ ) model.move_to([3, -1.0, 0] ) self.add(lowerCamelCase__ ) snake_case : Optional[int] = [] snake_case : Tuple = [] snake_case : Optional[int] = [] for i, rect in enumerate(lowerCamelCase__ ): rect.set_stroke(lowerCamelCase__ ) snake_case : Dict = Rectangle(height=0.46 / 4 , width=0.46 / 3 ).set_stroke(width=0.0 ).set_fill(lowerCamelCase__ , opacity=0.7 ) if i == 0: cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT ) , buff=0.02 , direction=lowerCamelCase__ ) cpu_target.set_x(cpu_target.get_x() + 0.1 ) elif i == 3: cpu_target.next_to(model_cpu_arr[0] , direction=lowerCamelCase__ , buff=0.0 ) else: cpu_target.next_to(model_cpu_arr[i - 1] , direction=lowerCamelCase__ , buff=0.0 ) self.add(lowerCamelCase__ ) model_cpu_arr.append(lowerCamelCase__ ) self.add(*lowerCamelCase__ , *lowerCamelCase__ , *lowerCamelCase__ ) snake_case : str = [mem.copy() for i in range(6 )] snake_case : List[Any] = VGroup(*lowerCamelCase__ ).arrange(lowerCamelCase__ , buff=0 ) snake_case : Dict = Text("Loaded Checkpoint" , font_size=24 ) snake_case : Optional[Any] = Group(lowerCamelCase__ , lowerCamelCase__ ).arrange(lowerCamelCase__ , buff=0.5 , aligned_edge=lowerCamelCase__ ) checkpoint.move_to([3, 0.5, 0] ) self.add(lowerCamelCase__ ) snake_case : str = [] snake_case : List[Any] = [] for i, rect in enumerate(lowerCamelCase__ ): snake_case : str = fill.copy().set_fill(lowerCamelCase__ , opacity=0.7 ) target.move_to(lowerCamelCase__ ) ckpt_arr.append(lowerCamelCase__ ) snake_case : List[Any] = target.copy() if i < 5: cpu_target.move_to(cpu_left_col_base[i + 1] ) else: cpu_target.move_to(cpu_right_col_base[i - 5] ) ckpt_cpu_arr.append(lowerCamelCase__ ) self.add(*lowerCamelCase__ , *lowerCamelCase__ ) snake_case : Tuple = Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) snake_case : Any = MarkupText( f"""<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model""" , font_size=18 , ) key_text.move_to([-5, 2.4, 0] ) self.add(lowerCamelCase__ , lowerCamelCase__ ) snake_case : Union[str, Any] = MarkupText( f"""<span fgcolor='{BLUE}'>●</span> Checkpoint""" , font_size=18 , ) blue_text.next_to(lowerCamelCase__ , DOWN * 2.4 , aligned_edge=key_text.get_left() ) self.add(lowerCamelCase__ ) snake_case : List[Any] = MarkupText( f"""Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device.""" , font_size=24 , ) step_a.move_to([2, 2, 0] ) snake_case : List[Any] = [meta_mem.copy() for i in range(6 )] snake_case : Optional[int] = [meta_mem.copy() for i in range(6 )] snake_case : Dict = VGroup(*lowerCamelCase__ ).arrange(lowerCamelCase__ , buff=0 ) snake_case : List[str] = VGroup(*lowerCamelCase__ ).arrange(lowerCamelCase__ , buff=0 ) snake_case : Dict = VGroup(lowerCamelCase__ , lowerCamelCase__ ).arrange(lowerCamelCase__ , buff=0 ) snake_case : str = Text("Disk" , font_size=24 ) snake_case : str = Group(lowerCamelCase__ , lowerCamelCase__ ).arrange(lowerCamelCase__ , buff=0.5 , aligned_edge=lowerCamelCase__ ) disk.move_to([-4.0, -1.25, 0] ) self.play(Write(lowerCamelCase__ , run_time=3 ) , Write(lowerCamelCase__ , run_time=1 ) , Create(lowerCamelCase__ , run_time=1 ) ) snake_case : str = [] for i, rect in enumerate(lowerCamelCase__ ): snake_case : Dict = rect.copy() target.generate_target() target.target.move_to(disk_left_col_base[i] ).scale(0.5 ) animations.append(MoveToTarget(lowerCamelCase__ , run_time=1.5 ) ) self.play(*lowerCamelCase__ ) self.play(FadeOut(lowerCamelCase__ ) ) snake_case : Union[str, Any] = MarkupText(f"""Then, the checkpoint is removed from memory\nthrough garbage collection.""" , font_size=24 ) step_a.move_to([2, 2, 0] ) self.play(Write(lowerCamelCase__ , run_time=3 ) ) self.play( FadeOut(lowerCamelCase__ , lowerCamelCase__ , *lowerCamelCase__ , *lowerCamelCase__ ) , ) self.wait()
356
def UpperCamelCase ( __lowerCamelCase : int ): if not isinstance(__lowerCamelCase , __lowerCamelCase ): raise TypeError("only integers accepted as input" ) else: snake_case : Dict = str(abs(__lowerCamelCase ) ) snake_case : Dict = [list(__lowerCamelCase ) for char in range(len(__lowerCamelCase ) )] for index in range(len(__lowerCamelCase ) ): num_transpositions[index].pop(__lowerCamelCase ) return max( int("".join(list(__lowerCamelCase ) ) ) for transposition in num_transpositions ) if __name__ == "__main__": __import__("""doctest""").testmod()
10
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging __A : Dict = logging.get_logger(__name__) __A : Union[str, Any] = { '''facebook/vit-mae-base''': '''https://huggingface.co/facebook/vit-mae-base/resolve/main/config.json''', # See all ViT MAE models at https://huggingface.co/models?filter=vit-mae } class _UpperCAmelCase ( _A ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = "vit_mae" def __init__( self : Dict , A : List[str]=7_68 , A : Any=12 , A : Union[str, Any]=12 , A : Tuple=30_72 , A : Any="gelu" , A : Tuple=0.0 , A : List[str]=0.0 , A : Tuple=0.02 , A : Tuple=1e-12 , A : int=2_24 , A : Dict=16 , A : int=3 , A : Tuple=True , A : Tuple=16 , A : Optional[Any]=5_12 , A : Union[str, Any]=8 , A : List[Any]=20_48 , A : Dict=0.75 , A : Any=False , **A : Optional[int] , ) -> Union[str, Any]: super().__init__(**A ) lowercase_ : List[Any] = hidden_size lowercase_ : str = num_hidden_layers lowercase_ : List[Any] = num_attention_heads lowercase_ : Any = intermediate_size lowercase_ : Optional[int] = hidden_act lowercase_ : List[Any] = hidden_dropout_prob lowercase_ : int = attention_probs_dropout_prob lowercase_ : int = initializer_range lowercase_ : Dict = layer_norm_eps lowercase_ : Optional[Any] = image_size lowercase_ : str = patch_size lowercase_ : Dict = num_channels lowercase_ : Any = qkv_bias lowercase_ : Union[str, Any] = decoder_num_attention_heads lowercase_ : Optional[Any] = decoder_hidden_size lowercase_ : List[str] = decoder_num_hidden_layers lowercase_ : List[Any] = decoder_intermediate_size lowercase_ : Optional[Any] = mask_ratio lowercase_ : Optional[Any] = norm_pix_loss
33
'''simple docstring''' from __future__ import annotations def lowerCAmelCase_ ( snake_case_ : list , snake_case_ : int | None = None , snake_case_ : int | None = None ) -> None: '''simple docstring''' if start is None: UpperCAmelCase_ = 0 if end is None: UpperCAmelCase_ = len(snake_case_ ) - 1 if start >= end: return UpperCAmelCase_ = (start + end) // 2 slowsort(snake_case_ , snake_case_ , snake_case_ ) slowsort(snake_case_ , mid + 1 , snake_case_ ) if sequence[end] < sequence[mid]: UpperCAmelCase_ , UpperCAmelCase_ = sequence[mid], sequence[end] slowsort(snake_case_ , snake_case_ , end - 1 ) if __name__ == "__main__": from doctest import testmod testmod()
1
0
'''simple docstring''' from __future__ import annotations import unittest from transformers import AutoTokenizer, PegasusConfig, is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFPegasusForConditionalGeneration, TFPegasusModel @require_tf class _lowerCamelCase : '''simple docstring''' A_ : Optional[Any] = PegasusConfig A_ : List[Any] = {} A_ : Any = """gelu""" def __init__( self : List[Any] , _A : List[Any] , _A : Dict=13 , _A : Dict=7 , _A : Any=True , _A : int=False , _A : List[str]=99 , _A : List[Any]=32 , _A : Optional[Any]=2 , _A : str=4 , _A : int=37 , _A : List[str]=0.1 , _A : str=0.1 , _A : Optional[Any]=40 , _A : List[str]=2 , _A : Tuple=1 , _A : List[str]=0 , ) -> List[str]: __magic_name__ : Optional[int] = parent __magic_name__ : Dict = batch_size __magic_name__ : Dict = seq_length __magic_name__ : Union[str, Any] = is_training __magic_name__ : Dict = use_labels __magic_name__ : Dict = vocab_size __magic_name__ : Dict = hidden_size __magic_name__ : Any = num_hidden_layers __magic_name__ : int = num_attention_heads __magic_name__ : Dict = intermediate_size __magic_name__ : int = hidden_dropout_prob __magic_name__ : Optional[int] = attention_probs_dropout_prob __magic_name__ : int = max_position_embeddings __magic_name__ : Dict = eos_token_id __magic_name__ : Dict = pad_token_id __magic_name__ : Tuple = bos_token_id def __lowerCAmelCase ( self : Dict ) -> int: __magic_name__ : Any = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) __magic_name__ : Optional[Any] = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) __magic_name__ : int = tf.concat([input_ids, eos_tensor] , axis=1 ) __magic_name__ : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __magic_name__ : Union[str, Any] = self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) __magic_name__ : int = prepare_pegasus_inputs_dict(_A , _A , _A ) return config, inputs_dict def __lowerCAmelCase ( self : Any , _A : int , _A : Dict ) -> str: __magic_name__ : List[Any] = TFPegasusModel(config=_A ).get_decoder() __magic_name__ : Optional[int] = inputs_dict['input_ids'] __magic_name__ : Union[str, Any] = input_ids[:1, :] __magic_name__ : Tuple = inputs_dict['attention_mask'][:1, :] __magic_name__ : Union[str, Any] = inputs_dict['head_mask'] __magic_name__ : Optional[Any] = 1 # first forward pass __magic_name__ : int = model(_A , attention_mask=_A , head_mask=_A , use_cache=_A ) __magic_name__ , __magic_name__ : List[Any] = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids __magic_name__ : Any = ids_tensor((self.batch_size, 3) , config.vocab_size ) __magic_name__ : List[str] = tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and __magic_name__ : List[str] = tf.concat([input_ids, next_tokens] , axis=-1 ) __magic_name__ : List[Any] = tf.concat([attention_mask, next_attn_mask] , axis=-1 ) __magic_name__ : str = model(_A , attention_mask=_A )[0] __magic_name__ : Union[str, Any] = model(_A , attention_mask=_A , past_key_values=_A )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice __magic_name__ : List[str] = int(ids_tensor((1,) , output_from_past.shape[-1] ) ) __magic_name__ : List[Any] = output_from_no_past[:, -3:, random_slice_idx] __magic_name__ : Optional[Any] = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(_A , _A , rtol=1E-3 ) def lowerCamelCase ( lowerCAmelCase : List[str] , lowerCAmelCase : Tuple , lowerCAmelCase : Optional[int] , lowerCAmelCase : List[str]=None , lowerCAmelCase : Optional[int]=None , lowerCAmelCase : Optional[Any]=None , lowerCAmelCase : List[Any]=None , lowerCAmelCase : Optional[Any]=None , ): """simple docstring""" if attention_mask is None: __magic_name__ : List[Any] = tf.cast(tf.math.not_equal(lowerCAmelCase , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: __magic_name__ : str = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: __magic_name__ : Dict = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: __magic_name__ : List[str] = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: __magic_name__ : Any = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class _lowerCamelCase ( lowercase__ , lowercase__ , unittest.TestCase ): '''simple docstring''' A_ : Any = (TFPegasusForConditionalGeneration, TFPegasusModel) if is_tf_available() else () A_ : List[str] = (TFPegasusForConditionalGeneration,) if is_tf_available() else () A_ : List[Any] = ( { """conversational""": TFPegasusForConditionalGeneration, """feature-extraction""": TFPegasusModel, """summarization""": TFPegasusForConditionalGeneration, """text2text-generation""": TFPegasusForConditionalGeneration, """translation""": TFPegasusForConditionalGeneration, } if is_tf_available() else {} ) A_ : Dict = True A_ : Optional[int] = False A_ : Dict = False def __lowerCAmelCase ( self : Dict ) -> str: __magic_name__ : Optional[Any] = TFPegasusModelTester(self ) __magic_name__ : str = ConfigTester(self , config_class=_A ) def __lowerCAmelCase ( self : Any ) -> Tuple: self.config_tester.run_common_tests() def __lowerCAmelCase ( self : Dict ) -> List[Any]: __magic_name__ : int = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*_A ) @require_sentencepiece @require_tokenizers @require_tf class _lowerCamelCase ( unittest.TestCase ): '''simple docstring''' A_ : Optional[Any] = [ """ PG&E stated it scheduled the blackouts in response to forecasts for high winds amid dry conditions. The aim is to reduce the risk of wildfires. Nearly 800 thousand customers were scheduled to be affected by the shutoffs which were expected to last through at least midday tomorrow.""", """ The London trio are up for best UK act and best album, as well as getting two nominations in the best song category.\"We got told like this morning 'Oh I think you're nominated'\", said Dappy.\"And I was like 'Oh yeah, which one?' And now we've got nominated for four awards. I mean, wow!\"Bandmate Fazer added: \"We thought it's best of us to come down and mingle with everyone and say hello to the cameras. And now we find we've got four nominations.\"The band have two shots at the best song prize, getting the nod for their Tynchy Stryder collaboration Number One, and single Strong Again.Their album Uncle B will also go up against records by the likes of Beyonce and Kanye West.N-Dubz picked up the best newcomer Mobo in 2007, but female member Tulisa said they wouldn't be too disappointed if they didn't win this time around.\"At the end of the day we're grateful to be where we are in our careers.\"If it don't happen then it don't happen - live to fight another day and keep on making albums and hits for the fans.\"Dappy also revealed they could be performing live several times on the night.The group will be doing Number One and also a possible rendition of the War Child single, I Got Soul.The charity song is a re-working of The Killers' All These Things That I've Done and is set to feature artists like Chipmunk, Ironik and Pixie Lott.This year's Mobos will be held outside of London for the first time, in Glasgow on 30 September.N-Dubz said they were looking forward to performing for their Scottish fans and boasted about their recent shows north of the border.\"We just done Edinburgh the other day,\" said Dappy.\"We smashed up an N-Dubz show over there. We done Aberdeen about three or four months ago - we smashed up that show over there! Everywhere we go we smash it up!\" """, ] A_ : Any = [ """California's largest electricity provider has cut power to hundreds of thousands of customers in an effort to""" """ reduce the risk of wildfires.""", """N-Dubz have revealed they\'re \"grateful\" to have been nominated for four Mobo Awards.""", ] # differs slightly from pytorch, likely due to numerical differences in linear layers A_ : Optional[Any] = """google/pegasus-xsum""" @cached_property def __lowerCAmelCase ( self : Tuple ) -> Any: return AutoTokenizer.from_pretrained(self.model_name ) @cached_property def __lowerCAmelCase ( self : List[str] ) -> Any: __magic_name__ : Any = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model def __lowerCAmelCase ( self : Optional[int] , **_A : Any ) -> Any: __magic_name__ : Optional[int] = self.translate_src_text(**_A ) assert self.expected_text == generated_words def __lowerCAmelCase ( self : List[str] , **_A : Optional[int] ) -> str: __magic_name__ : Union[str, Any] = self.tokenizer(self.src_text , **_A , padding=_A , return_tensors='tf' ) __magic_name__ : Dict = self.model.generate( model_inputs.input_ids , attention_mask=model_inputs.attention_mask , num_beams=2 , use_cache=_A , ) __magic_name__ : Optional[int] = self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=_A ) return generated_words @slow def __lowerCAmelCase ( self : List[str] ) -> Dict: self._assert_generated_batch_equal_expected()
275
'''simple docstring''' lowerCAmelCase :Union[str, Any] = ''' # Transformers installation ! pip install transformers datasets # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git ''' lowerCAmelCase :Union[str, Any] = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] lowerCAmelCase :Tuple = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
275
1
"""simple docstring""" import numpy as np def _SCREAMING_SNAKE_CASE ( __snake_case : np.ndarray , __snake_case : np.ndarray , __snake_case : float = 1e-12 , __snake_case : int = 1_00 , ): '''simple docstring''' assert np.shape(__snake_case )[0] == np.shape(__snake_case )[1] # Ensure proper dimensionality. assert np.shape(__snake_case )[0] == np.shape(__snake_case )[0] # Ensure inputs are either both complex or both real assert np.iscomplexobj(__snake_case ) == np.iscomplexobj(__snake_case ) lowercase = np.iscomplexobj(__snake_case ) if is_complex: # Ensure complex input_matrix is Hermitian assert np.array_equal(__snake_case , input_matrix.conj().T ) # Set convergence to False. Will define convergence when we exceed max_iterations # or when we have small changes from one iteration to next. lowercase = False lowercase = 0 lowercase = 0 lowercase = 1e12 while not convergence: # Multiple matrix by the vector. lowercase = np.dot(__snake_case , __snake_case ) # Normalize the resulting output vector. lowercase = w / np.linalg.norm(__snake_case ) # Find rayleigh quotient # (faster than usual b/c we know vector is normalized already) lowercase = vector.conj().T if is_complex else vector.T lowercase = np.dot(__snake_case , np.dot(__snake_case , __snake_case ) ) # Check convergence. lowercase = np.abs(lambda_ - lambda_previous ) / lambda_ iterations += 1 if error <= error_tol or iterations >= max_iterations: lowercase = True lowercase = lambda_ if is_complex: lowercase = np.real(lambda_ ) return lambda_, vector def _SCREAMING_SNAKE_CASE ( ): '''simple docstring''' lowercase = np.array([[41, 4, 20], [4, 26, 30], [20, 30, 50]] ) lowercase = np.array([41, 4, 20] ) lowercase = real_input_matrix.astype(np.complexaaa ) lowercase = np.triu(1j * complex_input_matrix , 1 ) complex_input_matrix += imag_matrix complex_input_matrix += -1 * imag_matrix.T lowercase = np.array([41, 4, 20] ).astype(np.complexaaa ) for problem_type in ["real", "complex"]: if problem_type == "real": lowercase = real_input_matrix lowercase = real_vector elif problem_type == "complex": lowercase = complex_input_matrix lowercase = complex_vector # Our implementation. lowercase , lowercase = power_iteration(__snake_case , __snake_case ) # Numpy implementation. # Get eigenvalues and eigenvectors using built-in numpy # eigh (eigh used for symmetric or hermetian matrices). lowercase , lowercase = np.linalg.eigh(__snake_case ) # Last eigenvalue is the maximum one. lowercase = eigen_values[-1] # Last column in this matrix is eigenvector corresponding to largest eigenvalue. lowercase = eigen_vectors[:, -1] # Check our implementation and numpy gives close answers. assert np.abs(eigen_value - eigen_value_max ) <= 1e-6 # Take absolute values element wise of each eigenvector. # as they are only unique to a minus sign. assert np.linalg.norm(np.abs(__snake_case ) - np.abs(__snake_case ) ) <= 1e-6 if __name__ == "__main__": import doctest doctest.testmod() test_power_iteration()
220
"""simple docstring""" import copy import json import os import tempfile from transformers import is_torch_available from .test_configuration_utils import config_common_kwargs class a ( a_ ): def __init__( self , _lowerCamelCase , _lowerCamelCase=None , _lowerCamelCase=True , _lowerCamelCase=None , **_lowerCamelCase ): lowercase = parent lowercase = config_class lowercase = has_text_modality lowercase = kwargs lowercase = common_properties def UpperCamelCase_ ( self ): lowercase = self.config_class(**self.inputs_dict ) lowercase = ( ['hidden_size', 'num_attention_heads', 'num_hidden_layers'] if self.common_properties is None else self.common_properties ) # Add common fields for text models if self.has_text_modality: common_properties.extend(['vocab_size'] ) # Test that config has the common properties as getters for prop in common_properties: self.parent.assertTrue(hasattr(_lowerCamelCase , _lowerCamelCase ) , msg=F'`{prop}` does not exist' ) # Test that config has the common properties as setter for idx, name in enumerate(_lowerCamelCase ): try: setattr(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) self.parent.assertEqual( getattr(_lowerCamelCase , _lowerCamelCase ) , _lowerCamelCase , msg=F'`{name} value {idx} expected, but was {getattr(_lowerCamelCase , _lowerCamelCase )}' ) except NotImplementedError: # Some models might not be able to implement setters for common_properties # In that case, a NotImplementedError is raised pass # Test if config class can be called with Config(prop_name=..) for idx, name in enumerate(_lowerCamelCase ): try: lowercase = self.config_class(**{name: idx} ) self.parent.assertEqual( getattr(_lowerCamelCase , _lowerCamelCase ) , _lowerCamelCase , msg=F'`{name} value {idx} expected, but was {getattr(_lowerCamelCase , _lowerCamelCase )}' ) except NotImplementedError: # Some models might not be able to implement setters for common_properties # In that case, a NotImplementedError is raised pass def UpperCamelCase_ ( self ): lowercase = self.config_class(**self.inputs_dict ) lowercase = json.loads(config.to_json_string() ) for key, value in self.inputs_dict.items(): self.parent.assertEqual(obj[key] , _lowerCamelCase ) def UpperCamelCase_ ( self ): lowercase = self.config_class(**self.inputs_dict ) with tempfile.TemporaryDirectory() as tmpdirname: lowercase = os.path.join(_lowerCamelCase , 'config.json' ) config_first.to_json_file(_lowerCamelCase ) lowercase = self.config_class.from_json_file(_lowerCamelCase ) self.parent.assertEqual(config_second.to_dict() , config_first.to_dict() ) def UpperCamelCase_ ( self ): lowercase = self.config_class(**self.inputs_dict ) with tempfile.TemporaryDirectory() as tmpdirname: config_first.save_pretrained(_lowerCamelCase ) lowercase = self.config_class.from_pretrained(_lowerCamelCase ) self.parent.assertEqual(config_second.to_dict() , config_first.to_dict() ) def UpperCamelCase_ ( self ): lowercase = self.config_class(**self.inputs_dict ) lowercase = 'test' with tempfile.TemporaryDirectory() as tmpdirname: lowercase = os.path.join(_lowerCamelCase , _lowerCamelCase ) config_first.save_pretrained(_lowerCamelCase ) lowercase = self.config_class.from_pretrained(_lowerCamelCase , subfolder=_lowerCamelCase ) self.parent.assertEqual(config_second.to_dict() , config_first.to_dict() ) def UpperCamelCase_ ( self ): lowercase = self.config_class(**self.inputs_dict , num_labels=5 ) self.parent.assertEqual(len(config.idalabel ) , 5 ) self.parent.assertEqual(len(config.labelaid ) , 5 ) lowercase = 3 self.parent.assertEqual(len(config.idalabel ) , 3 ) self.parent.assertEqual(len(config.labelaid ) , 3 ) def UpperCamelCase_ ( self ): if self.config_class.is_composition: return lowercase = self.config_class() self.parent.assertIsNotNone(_lowerCamelCase ) def UpperCamelCase_ ( self ): lowercase = copy.deepcopy(_lowerCamelCase ) lowercase = self.config_class(**_lowerCamelCase ) lowercase = [] for key, value in config_common_kwargs.items(): if key == "torch_dtype": if not is_torch_available(): continue else: import torch if config.torch_dtype != torch.floataa: wrong_values.append(('torch_dtype', config.torch_dtype, torch.floataa) ) elif getattr(_lowerCamelCase , _lowerCamelCase ) != value: wrong_values.append((key, getattr(_lowerCamelCase , _lowerCamelCase ), value) ) if len(_lowerCamelCase ) > 0: lowercase = '\n'.join([F'- {v[0]}: got {v[1]} instead of {v[2]}' for v in wrong_values] ) raise ValueError(F'The following keys were not properly set in the config:\n{errors}' ) def UpperCamelCase_ ( self ): self.create_and_test_config_common_properties() self.create_and_test_config_to_json_string() self.create_and_test_config_to_json_file() self.create_and_test_config_from_and_save_pretrained() self.create_and_test_config_from_and_save_pretrained_subfolder() self.create_and_test_config_with_num_labels() self.check_config_can_be_init_without_params() self.check_config_arguments_init()
220
1
"""simple docstring""" import unittest import numpy as np import torch from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class _lowerCAmelCase ( lowercase ,unittest.TestCase ): """simple docstring""" __UpperCAmelCase : List[str] = DDIMPipeline __UpperCAmelCase : Any = UNCONDITIONAL_IMAGE_GENERATION_PARAMS __UpperCAmelCase : str = PipelineTesterMixin.required_optional_params - { "num_images_per_prompt", "latents", "callback", "callback_steps", } __UpperCAmelCase : Dict = UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS __UpperCAmelCase : Dict = False def _lowercase ( self : Optional[int] ): torch.manual_seed(0 ) __lowercase = UNetaDModel( block_out_channels=(3_2, 6_4), layers_per_block=2, sample_size=3_2, in_channels=3, out_channels=3, down_block_types=("DownBlock2D", "AttnDownBlock2D"), up_block_types=("AttnUpBlock2D", "UpBlock2D"), ) __lowercase = DDIMScheduler() __lowercase = {"unet": unet, "scheduler": scheduler} return components def _lowercase ( self : Optional[Any], UpperCAmelCase__ : Optional[Any], UpperCAmelCase__ : List[str]=0 ): if str(UpperCAmelCase__ ).startswith("mps" ): __lowercase = torch.manual_seed(UpperCAmelCase__ ) else: __lowercase = torch.Generator(device=UpperCAmelCase__ ).manual_seed(UpperCAmelCase__ ) __lowercase = { "batch_size": 1, "generator": generator, "num_inference_steps": 2, "output_type": "numpy", } return inputs def _lowercase ( self : Any ): __lowercase = "cpu" __lowercase = self.get_dummy_components() __lowercase = self.pipeline_class(**UpperCAmelCase__ ) pipe.to(UpperCAmelCase__ ) pipe.set_progress_bar_config(disable=UpperCAmelCase__ ) __lowercase = self.get_dummy_inputs(UpperCAmelCase__ ) __lowercase = pipe(**UpperCAmelCase__ ).images __lowercase = image[0, -3:, -3:, -1] self.assertEqual(image.shape, (1, 3_2, 3_2, 3) ) __lowercase = np.array( [1.000E00, 5.717E-01, 4.717E-01, 1.000E00, 0.000E00, 1.000E00, 3.000E-04, 0.000E00, 9.000E-04] ) __lowercase = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(UpperCAmelCase__, 1E-3 ) def _lowercase ( self : int ): super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3 ) def _lowercase ( self : List[str] ): super().test_save_load_local(expected_max_difference=3E-3 ) def _lowercase ( self : List[str] ): super().test_save_load_optional_components(expected_max_difference=3E-3 ) def _lowercase ( self : List[str] ): super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class _lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def _lowercase ( self : str ): __lowercase = "google/ddpm-cifar10-32" __lowercase = UNetaDModel.from_pretrained(UpperCAmelCase__ ) __lowercase = DDIMScheduler() __lowercase = DDIMPipeline(unet=UpperCAmelCase__, scheduler=UpperCAmelCase__ ) ddim.to(UpperCAmelCase__ ) ddim.set_progress_bar_config(disable=UpperCAmelCase__ ) __lowercase = torch.manual_seed(0 ) __lowercase = ddim(generator=UpperCAmelCase__, eta=0.0, output_type="numpy" ).images __lowercase = image[0, -3:, -3:, -1] assert image.shape == (1, 3_2, 3_2, 3) __lowercase = np.array([0.1_723, 0.1_617, 0.1_600, 0.1_626, 0.1_497, 0.1_513, 0.1_505, 0.1_442, 0.1_453] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def _lowercase ( self : int ): __lowercase = "google/ddpm-ema-bedroom-256" __lowercase = UNetaDModel.from_pretrained(UpperCAmelCase__ ) __lowercase = DDIMScheduler.from_pretrained(UpperCAmelCase__ ) __lowercase = DDIMPipeline(unet=UpperCAmelCase__, scheduler=UpperCAmelCase__ ) ddpm.to(UpperCAmelCase__ ) ddpm.set_progress_bar_config(disable=UpperCAmelCase__ ) __lowercase = torch.manual_seed(0 ) __lowercase = ddpm(generator=UpperCAmelCase__, output_type="numpy" ).images __lowercase = image[0, -3:, -3:, -1] assert image.shape == (1, 2_5_6, 2_5_6, 3) __lowercase = np.array([0.0_060, 0.0_201, 0.0_344, 0.0_024, 0.0_018, 0.0_002, 0.0_022, 0.0_000, 0.0_069] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
368
"""simple docstring""" import unittest from transformers import ( MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TextClassificationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow from .test_pipelines_common import ANY # These 2 model types require different inputs than those of the usual text models. _a = {'LayoutLMv2Config', 'LayoutLMv3Config'} @is_pipeline_test class _lowerCAmelCase ( unittest.TestCase ): """simple docstring""" __UpperCAmelCase : str = MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING __UpperCAmelCase : str = TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING if model_mapping is not None: __UpperCAmelCase : Optional[Any] = {config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP} if tf_model_mapping is not None: __UpperCAmelCase : Tuple = { config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP } @require_torch def _lowercase ( self : Optional[int] ): __lowercase = pipeline( task="text-classification", model="hf-internal-testing/tiny-random-distilbert", framework="pt" ) __lowercase = text_classifier("This is great !" ) self.assertEqual(nested_simplify(UpperCAmelCase__ ), [{"label": "LABEL_0", "score": 0.504}] ) __lowercase = text_classifier("This is great !", top_k=2 ) self.assertEqual( nested_simplify(UpperCAmelCase__ ), [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}] ) __lowercase = text_classifier(["This is great !", "This is bad"], top_k=2 ) self.assertEqual( nested_simplify(UpperCAmelCase__ ), [ [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}], [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}], ], ) __lowercase = text_classifier("This is great !", top_k=1 ) self.assertEqual(nested_simplify(UpperCAmelCase__ ), [{"label": "LABEL_0", "score": 0.504}] ) # Legacy behavior __lowercase = text_classifier("This is great !", return_all_scores=UpperCAmelCase__ ) self.assertEqual(nested_simplify(UpperCAmelCase__ ), [{"label": "LABEL_0", "score": 0.504}] ) __lowercase = text_classifier("This is great !", return_all_scores=UpperCAmelCase__ ) self.assertEqual( nested_simplify(UpperCAmelCase__ ), [[{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}]] ) __lowercase = text_classifier(["This is great !", "Something else"], return_all_scores=UpperCAmelCase__ ) self.assertEqual( nested_simplify(UpperCAmelCase__ ), [ [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}], [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}], ], ) __lowercase = text_classifier(["This is great !", "Something else"], return_all_scores=UpperCAmelCase__ ) self.assertEqual( nested_simplify(UpperCAmelCase__ ), [ {"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_0", "score": 0.504}, ], ) @require_torch def _lowercase ( self : Dict ): import torch __lowercase = pipeline( task="text-classification", model="hf-internal-testing/tiny-random-distilbert", framework="pt", device=torch.device("cpu" ), ) __lowercase = text_classifier("This is great !" ) self.assertEqual(nested_simplify(UpperCAmelCase__ ), [{"label": "LABEL_0", "score": 0.504}] ) @require_tf def _lowercase ( self : Union[str, Any] ): __lowercase = pipeline( task="text-classification", model="hf-internal-testing/tiny-random-distilbert", framework="tf" ) __lowercase = text_classifier("This is great !" ) self.assertEqual(nested_simplify(UpperCAmelCase__ ), [{"label": "LABEL_0", "score": 0.504}] ) @slow @require_torch def _lowercase ( self : Dict ): __lowercase = pipeline("text-classification" ) __lowercase = text_classifier("This is great !" ) self.assertEqual(nested_simplify(UpperCAmelCase__ ), [{"label": "POSITIVE", "score": 1.0}] ) __lowercase = text_classifier("This is bad !" ) self.assertEqual(nested_simplify(UpperCAmelCase__ ), [{"label": "NEGATIVE", "score": 1.0}] ) __lowercase = text_classifier("Birds are a type of animal" ) self.assertEqual(nested_simplify(UpperCAmelCase__ ), [{"label": "POSITIVE", "score": 0.988}] ) @slow @require_tf def _lowercase ( self : Tuple ): __lowercase = pipeline("text-classification", framework="tf" ) __lowercase = text_classifier("This is great !" ) self.assertEqual(nested_simplify(UpperCAmelCase__ ), [{"label": "POSITIVE", "score": 1.0}] ) __lowercase = text_classifier("This is bad !" ) self.assertEqual(nested_simplify(UpperCAmelCase__ ), [{"label": "NEGATIVE", "score": 1.0}] ) __lowercase = text_classifier("Birds are a type of animal" ) self.assertEqual(nested_simplify(UpperCAmelCase__ ), [{"label": "POSITIVE", "score": 0.988}] ) def _lowercase ( self : Union[str, Any], UpperCAmelCase__ : str, UpperCAmelCase__ : int, UpperCAmelCase__ : Tuple ): __lowercase = TextClassificationPipeline(model=UpperCAmelCase__, tokenizer=UpperCAmelCase__ ) return text_classifier, ["HuggingFace is in", "This is another test"] def _lowercase ( self : Any, UpperCAmelCase__ : List[Any], UpperCAmelCase__ : str ): __lowercase = text_classifier.model # Small inputs because BartTokenizer tiny has maximum position embeddings = 22 __lowercase = "HuggingFace is in" __lowercase = text_classifier(UpperCAmelCase__ ) self.assertEqual(nested_simplify(UpperCAmelCase__ ), [{"label": ANY(UpperCAmelCase__ ), "score": ANY(UpperCAmelCase__ )}] ) self.assertTrue(outputs[0]["label"] in model.config.idalabel.values() ) __lowercase = ["HuggingFace is in ", "Paris is in France"] __lowercase = text_classifier(UpperCAmelCase__ ) self.assertEqual( nested_simplify(UpperCAmelCase__ ), [{"label": ANY(UpperCAmelCase__ ), "score": ANY(UpperCAmelCase__ )}, {"label": ANY(UpperCAmelCase__ ), "score": ANY(UpperCAmelCase__ )}], ) self.assertTrue(outputs[0]["label"] in model.config.idalabel.values() ) self.assertTrue(outputs[1]["label"] in model.config.idalabel.values() ) # Forcing to get all results with `top_k=None` # This is NOT the legacy format __lowercase = text_classifier(UpperCAmelCase__, top_k=UpperCAmelCase__ ) __lowercase = len(model.config.idalabel.values() ) self.assertEqual( nested_simplify(UpperCAmelCase__ ), [[{"label": ANY(UpperCAmelCase__ ), "score": ANY(UpperCAmelCase__ )}] * N, [{"label": ANY(UpperCAmelCase__ ), "score": ANY(UpperCAmelCase__ )}] * N], ) __lowercase = {"text": "HuggingFace is in ", "text_pair": "Paris is in France"} __lowercase = text_classifier(UpperCAmelCase__ ) self.assertEqual( nested_simplify(UpperCAmelCase__ ), {"label": ANY(UpperCAmelCase__ ), "score": ANY(UpperCAmelCase__ )}, ) self.assertTrue(outputs["label"] in model.config.idalabel.values() ) # This might be used a text pair, but tokenizer + pipe interaction # makes it hard to understand that it's not using the pair properly # https://github.com/huggingface/transformers/issues/17305 # We disabled this usage instead as it was outputting wrong outputs. __lowercase = [["HuggingFace is in ", "Paris is in France"]] with self.assertRaises(UpperCAmelCase__ ): text_classifier(UpperCAmelCase__ ) # This used to be valid for doing text pairs # We're keeping it working because of backward compatibility __lowercase = text_classifier([[["HuggingFace is in ", "Paris is in France"]]] ) self.assertEqual( nested_simplify(UpperCAmelCase__ ), [{"label": ANY(UpperCAmelCase__ ), "score": ANY(UpperCAmelCase__ )}], ) self.assertTrue(outputs[0]["label"] in model.config.idalabel.values() )
144
0
"""simple docstring""" from __future__ import annotations import pandas as pd def lowerCAmelCase_ ( snake_case_ : list[int] , snake_case_ : list[int] , snake_case_ : int ) ->Optional[Any]: lowerCamelCase__ : List[str] =[0] * no_of_processes lowerCamelCase__ : str =[0] * no_of_processes # Copy the burst time into remaining_time[] for i in range(snake_case_ ): lowerCamelCase__ : Dict =burst_time[i] lowerCamelCase__ : str =0 lowerCamelCase__ : Tuple =0 lowerCamelCase__ : Optional[int] =9_9_9_9_9_9_9_9_9 lowerCamelCase__ : int =0 lowerCamelCase__ : Union[str, Any] =False # Process until all processes are completed while complete != no_of_processes: for j in range(snake_case_ ): if arrival_time[j] <= increment_time and remaining_time[j] > 0: if remaining_time[j] < minm: lowerCamelCase__ : Tuple =remaining_time[j] lowerCamelCase__ : Any =j lowerCamelCase__ : Optional[int] =True if not check: increment_time += 1 continue remaining_time[short] -= 1 lowerCamelCase__ : Dict =remaining_time[short] if minm == 0: lowerCamelCase__ : Dict =9_9_9_9_9_9_9_9_9 if remaining_time[short] == 0: complete += 1 lowerCamelCase__ : Union[str, Any] =False # Find finish time of current process lowerCamelCase__ : Any =increment_time + 1 # Calculate waiting time lowerCamelCase__ : Optional[Any] =finish_time - arrival_time[short] lowerCamelCase__ : Union[str, Any] =finar - burst_time[short] if waiting_time[short] < 0: lowerCamelCase__ : List[Any] =0 # Increment time increment_time += 1 return waiting_time def lowerCAmelCase_ ( snake_case_ : list[int] , snake_case_ : int , snake_case_ : list[int] ) ->str: lowerCamelCase__ : Tuple =[0] * no_of_processes for i in range(snake_case_ ): lowerCamelCase__ : Any =burst_time[i] + waiting_time[i] return turn_around_time def lowerCAmelCase_ ( snake_case_ : list[int] , snake_case_ : list[int] , snake_case_ : int ) ->int: lowerCamelCase__ : Tuple =0 lowerCamelCase__ : Dict =0 for i in range(snake_case_ ): lowerCamelCase__ : List[Any] =total_waiting_time + waiting_time[i] lowerCamelCase__ : Tuple =total_turn_around_time + turn_around_time[i] print(f"""Average waiting time = {total_waiting_time / no_of_processes:.5f}""" ) print('Average turn around time =' , total_turn_around_time / no_of_processes ) if __name__ == "__main__": print("""Enter how many process you want to analyze""") lowerCAmelCase = int(input()) lowerCAmelCase = [0] * no_of_processes lowerCAmelCase = [0] * no_of_processes lowerCAmelCase = list(range(1, no_of_processes + 1)) for i in range(no_of_processes): print("""Enter the arrival time and burst time for process:--""" + str(i + 1)) lowerCAmelCase = map(int, input().split()) lowerCAmelCase = calculate_waitingtime(arrival_time, burst_time, no_of_processes) lowerCAmelCase = burst_time lowerCAmelCase = no_of_processes lowerCAmelCase = waiting_time lowerCAmelCase = calculate_turnaroundtime(bt, n, wt) calculate_average_times(waiting_time, turn_around_time, no_of_processes) lowerCAmelCase = pd.DataFrame( list(zip(processes, burst_time, arrival_time, waiting_time, turn_around_time)), columns=[ """Process""", """BurstTime""", """ArrivalTime""", """WaitingTime""", """TurnAroundTime""", ], ) # Printing the dataFrame pd.set_option("""display.max_rows""", fcfs.shape[0] + 1) print(fcfs)
126
import argparse import os import re # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_dummies.py lowerCamelCase : Union[str, Any] = "src/diffusers" # Matches is_xxx_available() lowerCamelCase : Dict = re.compile(r"is\_([a-z_]*)_available\(\)") # Matches from xxx import bla lowerCamelCase : Union[str, Any] = re.compile(r"\s+from\s+\S*\s+import\s+([^\(\s].*)\n") lowerCamelCase : Any = "\n{0} = None\n" lowerCamelCase : List[str] = "\nclass {0}(metaclass=DummyObject):\n _backends = {1}\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, {1})\n\n @classmethod\n def from_config(cls, *args, **kwargs):\n requires_backends(cls, {1})\n\n @classmethod\n def from_pretrained(cls, *args, **kwargs):\n requires_backends(cls, {1})\n" lowerCamelCase : str = "\ndef {0}(*args, **kwargs):\n requires_backends({0}, {1})\n" def _SCREAMING_SNAKE_CASE ( lowercase : Optional[Any] ): '''simple docstring''' lowerCamelCase_ = _re_backend.findall(lowercase ) if len(lowercase ) == 0: return None return "_and_".join(lowercase ) def _SCREAMING_SNAKE_CASE ( ): '''simple docstring''' with open(os.path.join(lowercase , '__init__.py' ) , 'r' , encoding='utf-8' , newline='\n' ) as f: lowerCamelCase_ = f.readlines() # Get to the point we do the actual imports for type checking lowerCamelCase_ = 0 lowerCamelCase_ = {} # Go through the end of the file while line_index < len(lowercase ): # If the line contains is_backend_available, we grab all objects associated with the `else` block lowerCamelCase_ = find_backend(lines[line_index] ) if backend is not None: while not lines[line_index].startswith('else:' ): line_index += 1 line_index += 1 lowerCamelCase_ = [] # Until we unindent, add backend objects to the list while line_index < len(lowercase ) and len(lines[line_index] ) > 1: lowerCamelCase_ = lines[line_index] lowerCamelCase_ = _re_single_line_import.search(lowercase ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(', ' ) ) elif line.startswith(' ' * 8 ): objects.append(line[8:-2] ) line_index += 1 if len(lowercase ) > 0: lowerCamelCase_ = objects else: line_index += 1 return backend_specific_objects def _SCREAMING_SNAKE_CASE ( lowercase : List[str] , lowercase : str ): '''simple docstring''' if name.isupper(): return DUMMY_CONSTANT.format(lowercase ) elif name.islower(): return DUMMY_FUNCTION.format(lowercase , lowercase ) else: return DUMMY_CLASS.format(lowercase , lowercase ) def _SCREAMING_SNAKE_CASE ( lowercase : Optional[int]=None ): '''simple docstring''' if backend_specific_objects is None: lowerCamelCase_ = read_init() # For special correspondence backend to module name as used in the function requires_modulename lowerCamelCase_ = {} for backend, objects in backend_specific_objects.items(): lowerCamelCase_ = '[' + ', '.join(f"""\"{b}\"""" for b in backend.split('_and_' ) ) + ']' lowerCamelCase_ = '# This file is autogenerated by the command `make fix-copies`, do not edit.\n' dummy_file += "from ..utils import DummyObject, requires_backends\n\n" dummy_file += "\n".join([create_dummy_object(lowercase , lowercase ) for o in objects] ) lowerCamelCase_ = dummy_file return dummy_files def _SCREAMING_SNAKE_CASE ( lowercase : Optional[int]=False ): '''simple docstring''' lowerCamelCase_ = create_dummy_files() # For special correspondence backend to shortcut as used in utils/dummy_xxx_objects.py lowerCamelCase_ = {'torch': 'pt'} # Locate actual dummy modules and read their content. lowerCamelCase_ = os.path.join(lowercase , 'utils' ) lowerCamelCase_ = { backend: os.path.join(lowercase , f"""dummy_{short_names.get(lowercase , lowercase )}_objects.py""" ) for backend in dummy_files.keys() } lowerCamelCase_ = {} for backend, file_path in dummy_file_paths.items(): if os.path.isfile(lowercase ): with open(lowercase , 'r' , encoding='utf-8' , newline='\n' ) as f: lowerCamelCase_ = f.read() else: lowerCamelCase_ = '' for backend in dummy_files.keys(): if dummy_files[backend] != actual_dummies[backend]: if overwrite: print( f"""Updating diffusers.utils.dummy_{short_names.get(lowercase , lowercase )}_objects.py as the main """ '__init__ has new objects.' ) with open(dummy_file_paths[backend] , 'w' , encoding='utf-8' , newline='\n' ) as f: f.write(dummy_files[backend] ) else: raise ValueError( 'The main __init__ has objects that are not present in ' f"""diffusers.utils.dummy_{short_names.get(lowercase , lowercase )}_objects.py. Run `make fix-copies` """ 'to fix this.' ) if __name__ == "__main__": lowerCamelCase : List[Any] = argparse.ArgumentParser() parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.") lowerCamelCase : Tuple = parser.parse_args() check_dummies(args.fix_and_overwrite)
204
0
"""simple docstring""" import json import os import unittest from transformers.models.gptsan_japanese.tokenization_gptsan_japanese import ( VOCAB_FILES_NAMES, GPTSanJapaneseTokenizer, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class _UpperCAmelCase( lowerCamelCase , unittest.TestCase ): lowercase__ = GPTSanJapaneseTokenizer lowercase__ = False lowercase__ = {'do_clean_text': False, 'add_prefix_space': False} def UpperCAmelCase ( self) -> List[Any]: '''simple docstring''' super().setUp() # fmt: off _UpperCamelCase = ['''こん''', '''こんに''', '''にちは''', '''ばんは''', '''世界,㔺界''', '''、''', '''。''', '''<BR>''', '''<SP>''', '''<TAB>''', '''<URL>''', '''<EMAIL>''', '''<TEL>''', '''<DATE>''', '''<PRICE>''', '''<BLOCK>''', '''<KIGOU>''', '''<U2000U2BFF>''', '''<|emoji1|>''', '''<unk>''', '''<|bagoftoken|>''', '''<|endoftext|>'''] # fmt: on _UpperCamelCase = {'''emoji''': {'''\ud83d\ude00''': '''<|emoji1|>'''}, '''emoji_inv''': {'''<|emoji1|>''': '''\ud83d\ude00'''}} # 😀 _UpperCamelCase = {'''unk_token''': '''<unk>'''} _UpperCamelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file''']) _UpperCamelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''emoji_file''']) with open(self.vocab_file , '''w''' , encoding='''utf-8''') as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens])) with open(self.emoji_file , '''w''') as emoji_writer: emoji_writer.write(json.dumps(__a)) def UpperCAmelCase ( self , **__a) -> Union[str, Any]: '''simple docstring''' kwargs.update(self.special_tokens_map) return GPTSanJapaneseTokenizer.from_pretrained(self.tmpdirname , **__a) def UpperCAmelCase ( self , __a) -> int: '''simple docstring''' _UpperCamelCase = '''こんにちは、世界。 \nこんばんは、㔺界。😀''' _UpperCamelCase = '''こんにちは、世界。 \nこんばんは、世界。😀''' return input_text, output_text def UpperCAmelCase ( self , __a) -> Tuple: '''simple docstring''' _UpperCamelCase , _UpperCamelCase = self.get_input_output_texts(__a) _UpperCamelCase = tokenizer.encode(__a , add_special_tokens=__a) _UpperCamelCase = tokenizer.decode(__a , clean_up_tokenization_spaces=__a) return text, ids def UpperCAmelCase ( self) -> Tuple: '''simple docstring''' pass # TODO add if relevant def UpperCAmelCase ( self) -> str: '''simple docstring''' pass # TODO add if relevant def UpperCAmelCase ( self) -> int: '''simple docstring''' pass # TODO add if relevant def UpperCAmelCase ( self) -> List[Any]: '''simple docstring''' _UpperCamelCase = self.get_tokenizer() # Testing tokenization _UpperCamelCase = '''こんにちは、世界。 こんばんは、㔺界。''' _UpperCamelCase = ['''こん''', '''にちは''', '''、''', '''世界''', '''。''', '''<SP>''', '''こん''', '''ばんは''', '''、''', '''㔺界''', '''。'''] _UpperCamelCase = tokenizer.tokenize(__a) self.assertListEqual(__a , __a) # Testing conversion to ids without special tokens _UpperCamelCase = [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6] _UpperCamelCase = tokenizer.convert_tokens_to_ids(__a) self.assertListEqual(__a , __a) # Testing conversion to ids with special tokens _UpperCamelCase = tokens + [tokenizer.unk_token] _UpperCamelCase = [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6, 19] _UpperCamelCase = tokenizer.convert_tokens_to_ids(__a) self.assertListEqual(__a , __a) def UpperCAmelCase ( self) -> str: '''simple docstring''' _UpperCamelCase = self.get_tokenizer() # Testing tokenization _UpperCamelCase = '''こんにちは、<|bagoftoken|>世界。こんばんは、<|bagoftoken|>㔺界。''' _UpperCamelCase = '''こんにちは、、、、世界。こんばんは、、、、世界。''' _UpperCamelCase = tokenizer.encode(__a) _UpperCamelCase = tokenizer.decode(__a) self.assertEqual(__a , __a) @slow def UpperCAmelCase ( self) -> str: '''simple docstring''' _UpperCamelCase = self.tokenizer_class.from_pretrained('''Tanrei/GPTSAN-japanese''') # Testing tokenization _UpperCamelCase = '''こんにちは、世界。''' _UpperCamelCase = '''こんばんは、㔺界。😀''' _UpperCamelCase = '''こんにちは、世界。こんばんは、世界。😀''' _UpperCamelCase = tokenizer.encode(prefix_text + input_text) _UpperCamelCase = tokenizer.encode('''''' , prefix_text=prefix_text + input_text) _UpperCamelCase = tokenizer.encode(__a , prefix_text=__a) _UpperCamelCase = tokenizer.decode(__a) _UpperCamelCase = tokenizer.decode(__a) _UpperCamelCase = tokenizer.decode(__a) self.assertEqual(__a , __a) self.assertEqual(__a , __a) self.assertEqual(__a , __a) @slow def UpperCAmelCase ( self) -> List[Any]: '''simple docstring''' _UpperCamelCase = self.tokenizer_class.from_pretrained('''Tanrei/GPTSAN-japanese''') # Testing tokenization _UpperCamelCase = '''こんにちは、世界。''' _UpperCamelCase = '''こんばんは、㔺界。😀''' _UpperCamelCase = len(tokenizer.encode(__a)) - 2 _UpperCamelCase = len(tokenizer.encode(__a)) - 2 _UpperCamelCase = [1] + [0] * (len_prefix + len_text + 1) _UpperCamelCase = [1] * (len_prefix + len_text + 1) + [0] _UpperCamelCase = [1] + [1] * (len_prefix) + [0] * (len_text + 1) _UpperCamelCase = tokenizer(prefix_text + input_text).token_type_ids _UpperCamelCase = tokenizer('''''' , prefix_text=prefix_text + input_text).token_type_ids _UpperCamelCase = tokenizer(__a , prefix_text=__a).token_type_ids self.assertListEqual(__a , __a) self.assertListEqual(__a , __a) self.assertListEqual(__a , __a) @slow def UpperCAmelCase ( self) -> Dict: '''simple docstring''' _UpperCamelCase = self.tokenizer_class.from_pretrained('''Tanrei/GPTSAN-japanese''') _UpperCamelCase = tokenizer.encode('''あンいワ''') _UpperCamelCase = tokenizer.encode('''''' , prefix_text='''あンいワ''') _UpperCamelCase = tokenizer.encode('''いワ''' , prefix_text='''あン''') self.assertEqual(tokenizer.decode(__a) , tokenizer.decode(__a)) self.assertEqual(tokenizer.decode(__a) , tokenizer.decode(__a)) self.assertNotEqual(__a , __a) self.assertNotEqual(__a , __a) self.assertEqual(x_token_a[1] , x_token_a[-1]) # SEG token self.assertEqual(x_token_a[1] , x_token_a[3]) # SEG token @slow def UpperCAmelCase ( self) -> Optional[Any]: '''simple docstring''' _UpperCamelCase = self.tokenizer_class.from_pretrained('''Tanrei/GPTSAN-japanese''') _UpperCamelCase = [['''武田信玄''', '''は、'''], ['''織田信長''', '''の配下の、''']] _UpperCamelCase = tokenizer(__a , padding=__a) _UpperCamelCase = tokenizer.batch_encode_plus(__a , padding=__a) # fmt: off _UpperCamelCase = [[3_59_93, 86_40, 2_59_48, 3_59_98, 3_06_47, 3_56_75, 3_59_99, 3_59_99], [3_59_93, 1_03_82, 98_68, 3_59_98, 3_06_46, 94_59, 3_06_46, 3_56_75]] _UpperCamelCase = [[1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0]] _UpperCamelCase = [[1, 1, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1]] # fmt: on self.assertListEqual(x_token.input_ids , __a) self.assertListEqual(x_token.token_type_ids , __a) self.assertListEqual(x_token.attention_mask , __a) self.assertListEqual(x_token_a.input_ids , __a) self.assertListEqual(x_token_a.token_type_ids , __a) self.assertListEqual(x_token_a.attention_mask , __a) def UpperCAmelCase ( self) -> Dict: '''simple docstring''' # Intentionally convert some words to accommodate character fluctuations unique to Japanese pass def UpperCAmelCase ( self) -> Dict: '''simple docstring''' # tokenizer has no padding token pass
100
"""simple docstring""" import warnings from diffusers import StableDiffusionInpaintPipeline as StableDiffusionInpaintPipeline # noqa F401 warnings.warn( """The `inpainting.py` script is outdated. Please use directly `from diffusers import""" """ StableDiffusionInpaintPipeline` instead.""" )
100
1
"""simple docstring""" from __future__ import annotations from fractions import Fraction def snake_case__ ( __lowerCamelCase : int , __lowerCamelCase : int ): """simple docstring""" return ( num != den and num % 10 == den // 10 and (num // 10) / (den % 10) == num / den ) def snake_case__ ( __lowerCamelCase : int ): """simple docstring""" lowerCamelCase__ : Optional[Any] =[] lowerCamelCase__ : List[Any] =11 lowerCamelCase__ : int =int('''1''' + '''0''' * digit_len ) for num in range(__lowerCamelCase , __lowerCamelCase ): while den <= 99: if (num != den) and (num % 10 == den // 10) and (den % 10 != 0): if is_digit_cancelling(__lowerCamelCase , __lowerCamelCase ): solutions.append(f'''{num}/{den}''' ) den += 1 num += 1 lowerCamelCase__ : Optional[int] =10 return solutions def snake_case__ ( __lowerCamelCase : int = 2 ): """simple docstring""" lowerCamelCase__ : str =1.0 for fraction in fraction_list(__lowerCamelCase ): lowerCamelCase__ : Optional[int] =Fraction(__lowerCamelCase ) result *= frac.denominator / frac.numerator return int(__lowerCamelCase ) if __name__ == "__main__": print(solution())
238
"""simple docstring""" from __future__ import annotations _lowercase : Dict = 1.6_021E-19 # units = C def snake_case__ ( __lowerCamelCase : float , __lowerCamelCase : float , __lowerCamelCase : float , ): """simple docstring""" if (conductivity, electron_conc, mobility).count(0 ) != 1: raise ValueError('''You cannot supply more or less than 2 values''' ) elif conductivity < 0: raise ValueError('''Conductivity cannot be negative''' ) elif electron_conc < 0: raise ValueError('''Electron concentration cannot be negative''' ) elif mobility < 0: raise ValueError('''mobility cannot be negative''' ) elif conductivity == 0: return ( "conductivity", mobility * electron_conc * ELECTRON_CHARGE, ) elif electron_conc == 0: return ( "electron_conc", conductivity / (mobility * ELECTRON_CHARGE), ) else: return ( "mobility", conductivity / (electron_conc * ELECTRON_CHARGE), ) if __name__ == "__main__": import doctest doctest.testmod()
238
1
from __future__ import annotations from numpy import array, cos, cross, floataa, radians, sin from numpy.typing import NDArray def __lowerCamelCase (UpperCAmelCase__ : float , UpperCAmelCase__ : float , UpperCAmelCase__ : bool = False ): if radian_mode: return [magnitude * cos(UpperCAmelCase__ ), magnitude * sin(UpperCAmelCase__ )] return [magnitude * cos(radians(UpperCAmelCase__ ) ), magnitude * sin(radians(UpperCAmelCase__ ) )] def __lowerCamelCase (UpperCAmelCase__ : NDArray[floataa] , UpperCAmelCase__ : NDArray[floataa] , UpperCAmelCase__ : float = 1_0**-1 ): SCREAMING_SNAKE_CASE = cross(UpperCAmelCase__ , UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = sum(UpperCAmelCase__ ) return abs(UpperCAmelCase__ ) < eps if __name__ == "__main__": # Test to check if it works _lowerCamelCase : Dict = array( [ polar_force(718.4, 1_80 - 30), polar_force(879.54, 45), polar_force(1_00, -90), ] ) _lowerCamelCase : NDArray[floataa] = array([[0, 0], [0, 0], [0, 0]]) assert in_static_equilibrium(forces, location) # Problem 1 in image_data/2D_problems.jpg _lowerCamelCase : Optional[Any] = array( [ polar_force(30 * 9.81, 15), polar_force(2_15, 1_80 - 45), polar_force(2_64, 90 - 30), ] ) _lowerCamelCase : Tuple = array([[0, 0], [0, 0], [0, 0]]) assert in_static_equilibrium(forces, location) # Problem in image_data/2D_problems_1.jpg _lowerCamelCase : int = array([[0, -20_00], [0, -12_00], [0, 1_56_00], [0, -1_24_00]]) _lowerCamelCase : Any = array([[0, 0], [6, 0], [10, 0], [12, 0]]) assert in_static_equilibrium(forces, location) import doctest doctest.testmod()
206
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import numpy as np import torch from ..models.clipseg import CLIPSegForImageSegmentation from ..utils import is_vision_available, requires_backends from .base import PipelineTool if is_vision_available(): from PIL import Image class lowercase ( a ): lowercase__ : Optional[Any] = ( """This is a tool that creates a segmentation mask of an image according to a label. It cannot create an image.""" """It takes two arguments named `image` which should be the original image, and `label` which should be a text """ """describing the elements what should be identified in the segmentation mask. The tool returns the mask.""" ) lowercase__ : Optional[int] = """CIDAS/clipseg-rd64-refined""" lowercase__ : Tuple = """image_segmenter""" lowercase__ : Optional[Any] = CLIPSegForImageSegmentation lowercase__ : int = ["""image""", """text"""] lowercase__ : List[str] = ["""image"""] def __init__( self : str , *_UpperCamelCase : str , **_UpperCamelCase : Union[str, Any] ) -> Optional[int]: '''simple docstring''' requires_backends(self , ["vision"] ) super().__init__(*_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : int , _UpperCamelCase : "Image" , _UpperCamelCase : str ) -> Optional[int]: '''simple docstring''' return self.pre_processor(text=[label] , images=[image] , padding=_UpperCamelCase , return_tensors="pt" ) def __snake_case( self : Union[str, Any] , _UpperCamelCase : str ) -> Union[str, Any]: '''simple docstring''' with torch.no_grad(): SCREAMING_SNAKE_CASE = self.model(**_UpperCamelCase ).logits return logits def __snake_case( self : Any , _UpperCamelCase : Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = outputs.cpu().detach().numpy() SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 1 return Image.fromarray((array * 255).astype(np.uinta ) )
206
1
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import OwlViTImageProcessor, OwlViTProcessor @require_vision class lowerCamelCase__( unittest.TestCase): def lowerCAmelCase__ ( self: Tuple ): __lowerCamelCase = tempfile.mkdtemp() # fmt: off __lowerCamelCase = ["""""", """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""] # fmt: on __lowerCamelCase = dict(zip(UpperCamelCase_ , range(len(UpperCamelCase_ ) ) ) ) __lowerCamelCase = ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""] __lowerCamelCase = {"""unk_token""": """<unk>"""} __lowerCamelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) __lowerCamelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(UpperCamelCase_ ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(UpperCamelCase_ ) ) __lowerCamelCase = { """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], } __lowerCamelCase = os.path.join(self.tmpdirname , UpperCamelCase_ ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(UpperCamelCase_ , UpperCamelCase_ ) def lowerCAmelCase__ ( self: List[str] , **UpperCamelCase_: str ): return CLIPTokenizer.from_pretrained(self.tmpdirname , pad_token="""!""" , **UpperCamelCase_ ) def lowerCAmelCase__ ( self: Optional[Any] , **UpperCamelCase_: str ): return CLIPTokenizerFast.from_pretrained(self.tmpdirname , pad_token="""!""" , **UpperCamelCase_ ) def lowerCAmelCase__ ( self: Union[str, Any] , **UpperCamelCase_: List[str] ): return OwlViTImageProcessor.from_pretrained(self.tmpdirname , **UpperCamelCase_ ) def lowerCAmelCase__ ( self: int ): shutil.rmtree(self.tmpdirname ) def lowerCAmelCase__ ( self: int ): __lowerCamelCase = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] __lowerCamelCase = [Image.fromarray(np.moveaxis(UpperCamelCase_ , 0 , -1 ) ) for x in image_inputs] return image_inputs def lowerCAmelCase__ ( self: Dict ): __lowerCamelCase = self.get_tokenizer() __lowerCamelCase = self.get_rust_tokenizer() __lowerCamelCase = self.get_image_processor() __lowerCamelCase = OwlViTProcessor(tokenizer=UpperCamelCase_ , image_processor=UpperCamelCase_ ) processor_slow.save_pretrained(self.tmpdirname ) __lowerCamelCase = OwlViTProcessor.from_pretrained(self.tmpdirname , use_fast=UpperCamelCase_ ) __lowerCamelCase = OwlViTProcessor(tokenizer=UpperCamelCase_ , image_processor=UpperCamelCase_ ) processor_fast.save_pretrained(self.tmpdirname ) __lowerCamelCase = OwlViTProcessor.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 , UpperCamelCase_ ) self.assertIsInstance(processor_fast.tokenizer , UpperCamelCase_ ) 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 , UpperCamelCase_ ) self.assertIsInstance(processor_fast.image_processor , UpperCamelCase_ ) def lowerCAmelCase__ ( self: Union[str, Any] ): __lowerCamelCase = OwlViTProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) __lowerCamelCase = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) __lowerCamelCase = self.get_image_processor(do_normalize=UpperCamelCase_ ) __lowerCamelCase = OwlViTProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=UpperCamelCase_ ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , UpperCamelCase_ ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , UpperCamelCase_ ) def lowerCAmelCase__ ( self: Optional[int] ): __lowerCamelCase = self.get_image_processor() __lowerCamelCase = self.get_tokenizer() __lowerCamelCase = OwlViTProcessor(tokenizer=UpperCamelCase_ , image_processor=UpperCamelCase_ ) __lowerCamelCase = self.prepare_image_inputs() __lowerCamelCase = image_processor(UpperCamelCase_ , return_tensors="""np""" ) __lowerCamelCase = processor(images=UpperCamelCase_ , 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 lowerCAmelCase__ ( self: Dict ): __lowerCamelCase = self.get_image_processor() __lowerCamelCase = self.get_tokenizer() __lowerCamelCase = OwlViTProcessor(tokenizer=UpperCamelCase_ , image_processor=UpperCamelCase_ ) __lowerCamelCase = """lower newer""" __lowerCamelCase = processor(text=UpperCamelCase_ , return_tensors="""np""" ) __lowerCamelCase = tokenizer(UpperCamelCase_ , return_tensors="""np""" ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key][0].tolist() , encoded_processor[key][0].tolist() ) def lowerCAmelCase__ ( self: Tuple ): __lowerCamelCase = self.get_image_processor() __lowerCamelCase = self.get_tokenizer() __lowerCamelCase = OwlViTProcessor(tokenizer=UpperCamelCase_ , image_processor=UpperCamelCase_ ) __lowerCamelCase = """lower newer""" __lowerCamelCase = self.prepare_image_inputs() __lowerCamelCase = processor(text=UpperCamelCase_ , images=UpperCamelCase_ ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with pytest.raises(UpperCamelCase_ ): processor() def lowerCAmelCase__ ( self: Optional[Any] ): __lowerCamelCase = """google/owlvit-base-patch32""" __lowerCamelCase = OwlViTProcessor.from_pretrained(UpperCamelCase_ ) __lowerCamelCase = ["""cat""", """nasa badge"""] __lowerCamelCase = processor(text=UpperCamelCase_ ) __lowerCamelCase = 16 self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """attention_mask"""] ) self.assertEqual(inputs["""input_ids"""].shape , (2, seq_length) ) # test if it raises when no input is passed with pytest.raises(UpperCamelCase_ ): processor() def lowerCAmelCase__ ( self: Any ): __lowerCamelCase = """google/owlvit-base-patch32""" __lowerCamelCase = OwlViTProcessor.from_pretrained(UpperCamelCase_ ) __lowerCamelCase = [["""cat""", """nasa badge"""], ["""person"""]] __lowerCamelCase = processor(text=UpperCamelCase_ ) __lowerCamelCase = 16 __lowerCamelCase = len(UpperCamelCase_ ) __lowerCamelCase = max([len(UpperCamelCase_ ) for texts in input_texts] ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """attention_mask"""] ) self.assertEqual(inputs["""input_ids"""].shape , (batch_size * num_max_text_queries, seq_length) ) # test if it raises when no input is passed with pytest.raises(UpperCamelCase_ ): processor() def lowerCAmelCase__ ( self: Optional[int] ): __lowerCamelCase = """google/owlvit-base-patch32""" __lowerCamelCase = OwlViTProcessor.from_pretrained(UpperCamelCase_ ) __lowerCamelCase = ["""cat""", """nasa badge"""] __lowerCamelCase = processor(text=UpperCamelCase_ ) __lowerCamelCase = 16 __lowerCamelCase = inputs["""input_ids"""] __lowerCamelCase = [ [4_94_06, 23_68, 4_94_07, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [4_94_06, 68_41, 1_13_01, 4_94_07, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ] self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """attention_mask"""] ) self.assertEqual(inputs["""input_ids"""].shape , (2, seq_length) ) self.assertListEqual(list(input_ids[0] ) , predicted_ids[0] ) self.assertListEqual(list(input_ids[1] ) , predicted_ids[1] ) def lowerCAmelCase__ ( self: Union[str, Any] ): __lowerCamelCase = self.get_image_processor() __lowerCamelCase = self.get_tokenizer() __lowerCamelCase = OwlViTProcessor(tokenizer=UpperCamelCase_ , image_processor=UpperCamelCase_ ) __lowerCamelCase = self.prepare_image_inputs() __lowerCamelCase = self.prepare_image_inputs() __lowerCamelCase = processor(images=UpperCamelCase_ , query_images=UpperCamelCase_ ) self.assertListEqual(list(inputs.keys() ) , ["""query_pixel_values""", """pixel_values"""] ) # test if it raises when no input is passed with pytest.raises(UpperCamelCase_ ): processor() def lowerCAmelCase__ ( self: str ): __lowerCamelCase = self.get_image_processor() __lowerCamelCase = self.get_tokenizer() __lowerCamelCase = OwlViTProcessor(tokenizer=UpperCamelCase_ , image_processor=UpperCamelCase_ ) __lowerCamelCase = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] __lowerCamelCase = processor.batch_decode(UpperCamelCase_ ) __lowerCamelCase = tokenizer.batch_decode(UpperCamelCase_ ) self.assertListEqual(UpperCamelCase_ , UpperCamelCase_ )
12
from __future__ import annotations from collections import namedtuple from dataclasses import dataclass @dataclass class lowerCamelCase__: UpperCAmelCase__ : int UpperCAmelCase__ : TreeNode | None = None UpperCAmelCase__ : TreeNode | None = None UpperCAmelCase_ = namedtuple('CoinsDistribResult', 'moves excess') def lowerCamelCase__ ( A__ : TreeNode | None ): '''simple docstring''' if root is None: return 0 # Validation def count_nodes(A__ : TreeNode | None ) -> int: if node is None: return 0 return count_nodes(node.left ) + count_nodes(node.right ) + 1 def count_coins(A__ : TreeNode | None ) -> int: if node is None: return 0 return count_coins(node.left ) + count_coins(node.right ) + node.data if count_nodes(A__ ) != count_coins(A__ ): raise ValueError("""The nodes number should be same as the number of coins""" ) # Main calculation def get_distrib(A__ : TreeNode | None ) -> CoinsDistribResult: if node is None: return CoinsDistribResult(0 , 1 ) __lowerCamelCase, __lowerCamelCase = get_distrib(node.left ) __lowerCamelCase, __lowerCamelCase = get_distrib(node.right ) __lowerCamelCase = 1 - left_distrib_excess __lowerCamelCase = 1 - right_distrib_excess __lowerCamelCase = ( left_distrib_moves + right_distrib_moves + abs(A__ ) + abs(A__ ) ) __lowerCamelCase = node.data - coins_to_left - coins_to_right return CoinsDistribResult(A__ , A__ ) return get_distrib(A__ )[0] if __name__ == "__main__": import doctest doctest.testmod()
12
1
"""simple docstring""" import unittest from parameterized import parameterized from transformers import AutoTokenizer, GPTNeoXConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( GPTNeoXForCausalLM, GPTNeoXForQuestionAnswering, GPTNeoXForSequenceClassification, GPTNeoXForTokenClassification, GPTNeoXModel, ) class __SCREAMING_SNAKE_CASE : def __init__( self : Optional[int] , snake_case : Optional[Any] , snake_case : Tuple=13 , snake_case : Dict=7 , snake_case : Optional[int]=True , snake_case : Union[str, Any]=True , snake_case : Dict=True , snake_case : Any=True , snake_case : List[str]=99 , snake_case : str=64 , snake_case : Optional[int]=5 , snake_case : str=4 , snake_case : List[Any]=37 , snake_case : Optional[Any]="gelu" , snake_case : List[str]=0.1 , snake_case : str=0.1 , snake_case : Optional[int]=512 , snake_case : Dict=16 , snake_case : List[Any]=2 , snake_case : Optional[int]=0.02 , snake_case : Any=3 , snake_case : Union[str, Any]=4 , snake_case : Dict=None , ): '''simple docstring''' A__ : Tuple = parent A__ : Union[str, Any] = batch_size A__ : List[str] = seq_length A__ : Optional[int] = is_training A__ : Dict = use_input_mask A__ : Any = use_token_type_ids A__ : Optional[Any] = use_labels A__ : List[str] = vocab_size A__ : Optional[int] = hidden_size A__ : Optional[Any] = num_hidden_layers A__ : Any = num_attention_heads A__ : List[Any] = intermediate_size A__ : Optional[Any] = hidden_act A__ : Optional[int] = hidden_dropout_prob A__ : Tuple = attention_probs_dropout_prob A__ : str = max_position_embeddings A__ : List[str] = type_vocab_size A__ : Union[str, Any] = type_sequence_label_size A__ : List[Any] = initializer_range A__ : Optional[int] = num_labels A__ : Dict = num_choices A__ : Dict = scope A__ : List[Any] = vocab_size - 1 def _UpperCamelCase ( self : List[Any] ): '''simple docstring''' A__ : Any = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ : List[Any] = None if self.use_input_mask: A__ : List[Any] = random_attention_mask([self.batch_size, self.seq_length] ) A__ : Union[str, Any] = None if self.use_labels: A__ : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) A__ : Tuple = self.get_config() return config, input_ids, input_mask, token_labels def _UpperCamelCase ( self : Union[str, Any] ): '''simple docstring''' return GPTNeoXConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=snake_case , initializer_range=self.initializer_range , pad_token_id=self.pad_token_id , ) def _UpperCamelCase ( self : List[str] ): '''simple docstring''' A__ , A__ , A__ , A__ : str = self.prepare_config_and_inputs() A__ : Union[str, Any] = True return config, input_ids, input_mask, token_labels def _UpperCamelCase ( self : Union[str, Any] , snake_case : Optional[int] , snake_case : List[str] , snake_case : int ): '''simple docstring''' A__ : Any = GPTNeoXModel(config=snake_case ) model.to(snake_case ) model.eval() A__ : int = model(snake_case , attention_mask=snake_case ) A__ : Optional[int] = model(snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _UpperCamelCase ( self : Union[str, Any] , snake_case : str , snake_case : Any , snake_case : Union[str, Any] ): '''simple docstring''' A__ : int = True A__ : str = GPTNeoXModel(snake_case ) model.to(snake_case ) model.eval() A__ : Tuple = model(snake_case , attention_mask=snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _UpperCamelCase ( self : Dict , snake_case : List[Any] , snake_case : str , snake_case : Optional[Any] , snake_case : Any ): '''simple docstring''' A__ : Any = GPTNeoXForCausalLM(config=snake_case ) model.to(snake_case ) model.eval() A__ : Tuple = model(snake_case , attention_mask=snake_case , labels=snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _UpperCamelCase ( self : List[str] , snake_case : Optional[Any] , snake_case : Optional[Any] , snake_case : Union[str, Any] , snake_case : Tuple ): '''simple docstring''' A__ : int = self.num_labels A__ : int = GPTNeoXForQuestionAnswering(snake_case ) model.to(snake_case ) model.eval() A__ : Optional[Any] = model(snake_case , attention_mask=snake_case ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def _UpperCamelCase ( self : str , snake_case : Tuple , snake_case : int , snake_case : int , snake_case : Dict ): '''simple docstring''' A__ : List[Any] = self.num_labels A__ : Tuple = GPTNeoXForSequenceClassification(snake_case ) model.to(snake_case ) model.eval() A__ : Any = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A__ : List[str] = model(snake_case , attention_mask=snake_case , labels=snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _UpperCamelCase ( self : Any , snake_case : Union[str, Any] , snake_case : int , snake_case : Tuple , snake_case : Any ): '''simple docstring''' A__ : Tuple = self.num_labels A__ : Any = GPTNeoXForTokenClassification(snake_case ) model.to(snake_case ) model.eval() A__ : Dict = model(snake_case , attention_mask=snake_case , labels=snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def _UpperCamelCase ( self : List[str] , snake_case : List[str] , snake_case : Tuple , snake_case : Any ): '''simple docstring''' A__ : Optional[int] = True A__ : Any = GPTNeoXForCausalLM(config=snake_case ) model.to(snake_case ) model.eval() # first forward pass A__ : Tuple = model(snake_case , attention_mask=snake_case , use_cache=snake_case ) A__ : str = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids A__ : Any = ids_tensor((self.batch_size, 3) , config.vocab_size ) A__ : Tuple = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and A__ : Any = torch.cat([input_ids, next_tokens] , dim=-1 ) A__ : Any = torch.cat([input_mask, next_mask] , dim=-1 ) A__ : Tuple = model(snake_case , attention_mask=snake_case , output_hidden_states=snake_case ) A__ : List[Any] = output_from_no_past["""hidden_states"""][0] A__ : List[str] = model( snake_case , attention_mask=snake_case , past_key_values=snake_case , output_hidden_states=snake_case , )["""hidden_states"""][0] # select random slice A__ : Tuple = ids_tensor((1,) , output_from_past.shape[-1] ).item() A__ : List[Any] = output_from_no_past[:, -3:, random_slice_idx].detach() A__ : Any = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(snake_case , snake_case , atol=1e-3 ) ) def _UpperCamelCase ( self : str ): '''simple docstring''' A__ : str = self.prepare_config_and_inputs() A__ , A__ , A__ , A__ : Dict = config_and_inputs A__ : Optional[Any] = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_torch class __SCREAMING_SNAKE_CASE ( UpperCamelCase , UpperCamelCase , UpperCamelCase , unittest.TestCase ): snake_case_ = ( ( GPTNeoXModel, GPTNeoXForCausalLM, GPTNeoXForQuestionAnswering, GPTNeoXForSequenceClassification, GPTNeoXForTokenClassification, ) if is_torch_available() else () ) snake_case_ = (GPTNeoXForCausalLM,) if is_torch_available() else () snake_case_ = ( { 'feature-extraction': GPTNeoXModel, 'question-answering': GPTNeoXForQuestionAnswering, 'text-classification': GPTNeoXForSequenceClassification, 'text-generation': GPTNeoXForCausalLM, 'token-classification': GPTNeoXForTokenClassification, 'zero-shot': GPTNeoXForSequenceClassification, } if is_torch_available() else {} ) snake_case_ = False snake_case_ = False snake_case_ = False snake_case_ = False def _UpperCamelCase ( self : Union[str, Any] ): '''simple docstring''' A__ : Any = GPTNeoXModelTester(self ) A__ : Any = ConfigTester(self , config_class=snake_case , hidden_size=64 , num_attention_heads=8 ) def _UpperCamelCase ( self : Union[str, Any] ): '''simple docstring''' self.config_tester.run_common_tests() def _UpperCamelCase ( self : Any ): '''simple docstring''' A__ , A__ , A__ , A__ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(snake_case , snake_case , snake_case ) def _UpperCamelCase ( self : Dict ): '''simple docstring''' A__ , A__ , A__ , A__ : Dict = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(snake_case , snake_case , snake_case ) def _UpperCamelCase ( self : Dict ): '''simple docstring''' A__ , A__ , A__ , A__ : List[str] = self.model_tester.prepare_config_and_inputs_for_decoder() A__ : Optional[Any] = None self.model_tester.create_and_check_model_as_decoder(snake_case , snake_case , snake_case ) def _UpperCamelCase ( self : str ): '''simple docstring''' A__ , A__ , A__ , A__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(snake_case , snake_case , snake_case ) def _UpperCamelCase ( self : Optional[Any] ): '''simple docstring''' A__ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_causal_lm(*snake_case ) def _UpperCamelCase ( self : List[str] ): '''simple docstring''' A__ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*snake_case ) def _UpperCamelCase ( self : str ): '''simple docstring''' A__ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*snake_case ) def _UpperCamelCase ( self : str ): '''simple docstring''' A__ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*snake_case ) @unittest.skip(reason="""Feed forward chunking is not implemented""" ) def _UpperCamelCase ( self : List[Any] ): '''simple docstring''' pass @parameterized.expand([("""linear""",), ("""dynamic""",)] ) def _UpperCamelCase ( self : Optional[Any] , snake_case : Optional[Any] ): '''simple docstring''' A__ , A__ : int = self.model_tester.prepare_config_and_inputs_for_common() A__ : List[Any] = ids_tensor([1, 10] , config.vocab_size ) A__ : str = ids_tensor([1, int(config.max_position_embeddings * 1.5 )] , config.vocab_size ) set_seed(42 ) # Fixed seed at init time so the two models get the same random weights A__ : Union[str, Any] = GPTNeoXModel(snake_case ) original_model.to(snake_case ) original_model.eval() A__ : Optional[int] = original_model(snake_case ).last_hidden_state A__ : List[str] = original_model(snake_case ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights A__ : Optional[int] = {"""type""": scaling_type, """factor""": 10.0} A__ : Optional[int] = GPTNeoXModel(snake_case ) scaled_model.to(snake_case ) scaled_model.eval() A__ : List[str] = scaled_model(snake_case ).last_hidden_state A__ : Tuple = scaled_model(snake_case ).last_hidden_state # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original # maximum sequence length, so the outputs for the short input should match. if scaling_type == "dynamic": self.assertTrue(torch.allclose(snake_case , snake_case , atol=1e-5 ) ) else: self.assertFalse(torch.allclose(snake_case , snake_case , atol=1e-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(snake_case , snake_case , atol=1e-5 ) ) @require_torch class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): @slow def _UpperCamelCase ( self : Tuple ): '''simple docstring''' A__ : Any = AutoTokenizer.from_pretrained("""EleutherAI/pythia-410m-deduped""" ) for checkpointing in [True, False]: A__ : Optional[Any] = GPTNeoXForCausalLM.from_pretrained("""EleutherAI/pythia-410m-deduped""" ) if checkpointing: model.gradient_checkpointing_enable() else: model.gradient_checkpointing_disable() model.to(snake_case ) A__ : Optional[Any] = tokenizer("""My favorite food is""" , return_tensors="""pt""" ).to(snake_case ) # The hub repo. is updated on 2023-04-04, resulting in poor outputs. # See: https://github.com/huggingface/transformers/pull/24193 A__ : Union[str, Any] = """My favorite food is a good old-fashioned, old-fashioned, old-fashioned.\n\nI'm not sure""" A__ : Tuple = model.generate(**snake_case , do_sample=snake_case , max_new_tokens=20 ) A__ : Tuple = tokenizer.batch_decode(snake_case )[0] self.assertEqual(snake_case , snake_case )
296
"""simple docstring""" # Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ..models.auto import AutoModelForVisionaSeq from ..utils import requires_backends from .base import PipelineTool if TYPE_CHECKING: from PIL import Image class __SCREAMING_SNAKE_CASE ( UpperCamelCase ): snake_case_ = 'Salesforce/blip-image-captioning-base' snake_case_ = ( 'This is a tool that generates a description of an image. It takes an input named `image` which should be the ' 'image to caption, and returns a text that contains the description in English.' ) snake_case_ = 'image_captioner' snake_case_ = AutoModelForVisionaSeq snake_case_ = ['image'] snake_case_ = ['text'] def __init__( self : int , *snake_case : Optional[int] , **snake_case : Optional[int] ): '''simple docstring''' requires_backends(self , ["""vision"""] ) super().__init__(*snake_case , **snake_case ) def _UpperCamelCase ( self : int , snake_case : "Image" ): '''simple docstring''' return self.pre_processor(images=snake_case , return_tensors="""pt""" ) def _UpperCamelCase ( self : int , snake_case : List[Any] ): '''simple docstring''' return self.model.generate(**snake_case ) def _UpperCamelCase ( self : Optional[int] , snake_case : Any ): '''simple docstring''' return self.pre_processor.batch_decode(snake_case , skip_special_tokens=snake_case )[0].strip()
296
1
'''simple docstring''' import io import itertools import json from dataclasses import dataclass from typing import Optional import pyarrow as pa import pyarrow.json as paj import datasets from datasets.table import table_cast from datasets.utils.file_utils import readline a : Optional[int] = datasets.utils.logging.get_logger(__name__) @dataclass class UpperCamelCase__ ( datasets.BuilderConfig ): """simple docstring""" SCREAMING_SNAKE_CASE__ : Optional[Any] = None SCREAMING_SNAKE_CASE__ : Tuple = "utf-8" SCREAMING_SNAKE_CASE__ : Dict = None SCREAMING_SNAKE_CASE__ : Any = None SCREAMING_SNAKE_CASE__ : List[str] = True # deprecated SCREAMING_SNAKE_CASE__ : List[Any] = None # deprecated SCREAMING_SNAKE_CASE__ : str = 10 << 20 # 10MB SCREAMING_SNAKE_CASE__ : Any = None class UpperCamelCase__ ( datasets.ArrowBasedBuilder ): """simple docstring""" SCREAMING_SNAKE_CASE__ : List[Any] = JsonConfig def A_ ( self ): '''simple docstring''' if self.config.block_size is not None: logger.warning("The JSON loader parameter `block_size` is deprecated. Please use `chunksize` instead" ) UpperCAmelCase : Tuple = self.config.block_size if self.config.use_threads is not True: logger.warning( "The JSON loader parameter `use_threads` is deprecated and doesn\'t have any effect anymore." ) if self.config.newlines_in_values is not None: raise ValueError("The JSON loader parameter `newlines_in_values` is no longer supported" ) return datasets.DatasetInfo(features=self.config.features ) def A_ ( self , snake_case ): '''simple docstring''' if not self.config.data_files: raise ValueError(f"At least one data file must be specified, but got data_files={self.config.data_files}" ) UpperCAmelCase : Tuple = dl_manager.download_and_extract(self.config.data_files ) if isinstance(snake_case , (str, list, tuple) ): UpperCAmelCase : Optional[int] = data_files if isinstance(snake_case , snake_case ): UpperCAmelCase : Optional[Any] = [files] UpperCAmelCase : Any = [dl_manager.iter_files(snake_case ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={"files": files} )] UpperCAmelCase : List[str] = [] for split_name, files in data_files.items(): if isinstance(snake_case , snake_case ): UpperCAmelCase : List[str] = [files] UpperCAmelCase : Union[str, Any] = [dl_manager.iter_files(snake_case ) for file in files] splits.append(datasets.SplitGenerator(name=snake_case , gen_kwargs={"files": files} ) ) return splits def A_ ( self , snake_case ): '''simple docstring''' if self.config.features is not None: # adding missing columns for column_name in set(self.config.features ) - set(pa_table.column_names ): UpperCAmelCase : Tuple = self.config.features.arrow_schema.field(snake_case ).type UpperCAmelCase : Tuple = pa_table.append_column(snake_case , pa.array([None] * len(snake_case ) , type=snake_case ) ) # more expensive cast to support nested structures with keys in a different order # allows str <-> int/float or str to Audio for example UpperCAmelCase : List[str] = table_cast(snake_case , self.config.features.arrow_schema ) return pa_table def A_ ( self , snake_case ): '''simple docstring''' for file_idx, file in enumerate(itertools.chain.from_iterable(snake_case ) ): # If the file is one json object and if we need to look at the list of items in one specific field if self.config.field is not None: with open(snake_case , encoding=self.config.encoding , errors=self.config.encoding_errors ) as f: UpperCAmelCase : str = json.load(snake_case ) # We keep only the field we are interested in UpperCAmelCase : List[Any] = dataset[self.config.field] # We accept two format: a list of dicts or a dict of lists if isinstance(snake_case , (list, tuple) ): UpperCAmelCase : str = set().union(*[row.keys() for row in dataset] ) UpperCAmelCase : List[Any] = {col: [row.get(snake_case ) for row in dataset] for col in keys} else: UpperCAmelCase : Any = dataset UpperCAmelCase : Dict = pa.Table.from_pydict(snake_case ) yield file_idx, self._cast_table(snake_case ) # If the file has one json object per line else: with open(snake_case , "rb" ) as f: UpperCAmelCase : Union[str, Any] = 0 # Use block_size equal to the chunk size divided by 32 to leverage multithreading # Set a default minimum value of 16kB if the chunk size is really small UpperCAmelCase : Any = max(self.config.chunksize // 3_2 , 1_6 << 1_0 ) UpperCAmelCase : str = ( self.config.encoding_errors if self.config.encoding_errors is not None else 'strict' ) while True: UpperCAmelCase : int = f.read(self.config.chunksize ) if not batch: break # Finish current line try: batch += f.readline() except (AttributeError, io.UnsupportedOperation): batch += readline(snake_case ) # PyArrow only accepts utf-8 encoded bytes if self.config.encoding != "utf-8": UpperCAmelCase : Tuple = batch.decode(self.config.encoding , errors=snake_case ).encode("utf-8" ) try: while True: try: UpperCAmelCase : Optional[Any] = paj.read_json( io.BytesIO(snake_case ) , read_options=paj.ReadOptions(block_size=snake_case ) ) break except (pa.ArrowInvalid, pa.ArrowNotImplementedError) as e: if ( isinstance(snake_case , pa.ArrowInvalid ) and "straddling" not in str(snake_case ) or block_size > len(snake_case ) ): raise else: # Increase the block size in case it was too small. # The block size will be reset for the next file. logger.debug( f"Batch of {len(snake_case )} bytes couldn\'t be parsed with block_size={block_size}. Retrying with block_size={block_size * 2}." ) block_size *= 2 except pa.ArrowInvalid as e: try: with open( snake_case , encoding=self.config.encoding , errors=self.config.encoding_errors ) as f: UpperCAmelCase : Dict = json.load(snake_case ) except json.JSONDecodeError: logger.error(f"Failed to read file \'{file}\' with error {type(snake_case )}: {e}" ) raise e # If possible, parse the file as a list of json objects and exit the loop if isinstance(snake_case , snake_case ): # list is the only sequence type supported in JSON try: UpperCAmelCase : Dict = set().union(*[row.keys() for row in dataset] ) UpperCAmelCase : Any = {col: [row.get(snake_case ) for row in dataset] for col in keys} UpperCAmelCase : List[Any] = pa.Table.from_pydict(snake_case ) except (pa.ArrowInvalid, AttributeError) as e: logger.error(f"Failed to read file \'{file}\' with error {type(snake_case )}: {e}" ) raise ValueError(f"Not able to read records in the JSON file at {file}." ) from None yield file_idx, self._cast_table(snake_case ) break else: logger.error(f"Failed to read file \'{file}\' with error {type(snake_case )}: {e}" ) raise ValueError( f"Not able to read records in the JSON file at {file}. " f"You should probably indicate the field of the JSON file containing your records. " f"This JSON file contain the following fields: {str(list(dataset.keys() ) )}. " f"Select the correct one and provide it as `field=\'XXX\'` to the dataset loading method. " ) from None # Uncomment for debugging (will print the Arrow table size and elements) # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}") # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows))) yield (file_idx, batch_idx), self._cast_table(snake_case ) batch_idx += 1
311
class A__ : def __init__( self : Dict , a : list[int] ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = len(a ) lowerCAmelCase__ : Optional[Any] = [0] * len_array if len_array > 0: lowerCAmelCase__ : List[Any] = array[0] for i in range(1 , a ): lowerCAmelCase__ : Optional[int] = self.prefix_sum[i - 1] + array[i] def _lowerCamelCase ( self : int , a : int , a : int ): '''simple docstring''' if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def _lowerCamelCase ( self : Optional[Any] , a : int ): '''simple docstring''' lowerCAmelCase__ : Any = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(a ) return False if __name__ == "__main__": import doctest doctest.testmod()
212
0
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 UpperCamelCase__ = True except (ImportError, AttributeError): UpperCamelCase__ = object def _UpperCamelCase (*a__ :int , **a__ :int ): """simple docstring""" pass UpperCamelCase__ = False UpperCamelCase__ = logging.get_logger("transformers-cli/serving") def _UpperCamelCase (a__ :Namespace ): """simple docstring""" UpperCamelCase__ = pipeline( task=args.task , model=args.model if args.model else None , config=args.config , tokenizer=args.tokenizer , device=args.device , ) return ServeCommand(a__ , args.host , args.port , args.workers ) class __SCREAMING_SNAKE_CASE ( _a ): snake_case : dict class __SCREAMING_SNAKE_CASE ( _a ): snake_case : List[str] snake_case : Optional[List[int]] class __SCREAMING_SNAKE_CASE ( _a ): snake_case : str class __SCREAMING_SNAKE_CASE ( _a ): snake_case : Any class __SCREAMING_SNAKE_CASE ( _a ): @staticmethod def _lowerCamelCase ( __lowerCAmelCase ): UpperCamelCase__ = 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 , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ): UpperCamelCase__ = pipeline UpperCamelCase__ = host UpperCamelCase__ = port UpperCamelCase__ = 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}""" ) UpperCamelCase__ = 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 _lowerCamelCase ( self ): run(self._app , host=self.host , port=self.port , workers=self.workers ) def _lowerCamelCase ( self ): return ServeModelInfoResult(infos=vars(self._pipeline.model.config ) ) def _lowerCamelCase ( self , __lowerCAmelCase = Body(__lowerCAmelCase , embed=__lowerCAmelCase ) , __lowerCAmelCase = Body(__lowerCAmelCase , embed=__lowerCAmelCase ) ): try: UpperCamelCase__ = self._pipeline.tokenizer.tokenize(__lowerCAmelCase ) if return_ids: UpperCamelCase__ = 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 _lowerCamelCase ( self , __lowerCAmelCase = Body(__lowerCAmelCase , embed=__lowerCAmelCase ) , __lowerCAmelCase = Body(__lowerCAmelCase , embed=__lowerCAmelCase ) , __lowerCAmelCase = Body(__lowerCAmelCase , embed=__lowerCAmelCase ) , ): try: UpperCamelCase__ = 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 _lowerCamelCase ( self , __lowerCAmelCase=Body(__lowerCAmelCase , embed=__lowerCAmelCase ) ): # Check we don't have empty string if len(__lowerCAmelCase ) == 0: return ServeForwardResult(output=[] , attention=[] ) try: # Forward through the model UpperCamelCase__ = self._pipeline(__lowerCAmelCase ) return ServeForwardResult(output=__lowerCAmelCase ) except Exception as e: raise HTTPException(500 , {"""error""": str(__lowerCAmelCase )} )
87
UpperCamelCase__ = { "meter": "m", "kilometer": "km", "megametre": "Mm", "gigametre": "Gm", "terametre": "Tm", "petametre": "Pm", "exametre": "Em", "zettametre": "Zm", "yottametre": "Ym", } # Exponent of the factor(meter) UpperCamelCase__ = { "m": 0, "km": 3, "Mm": 6, "Gm": 9, "Tm": 12, "Pm": 15, "Em": 18, "Zm": 21, "Ym": 24, } def _UpperCamelCase (a__ :float , a__ :str , a__ :str ): """simple docstring""" UpperCamelCase__ = from_type.lower().strip("""s""" ) UpperCamelCase__ = to_type.lower().strip("""s""" ) UpperCamelCase__ = UNIT_SYMBOL.get(a__ , a__ ) UpperCamelCase__ = UNIT_SYMBOL.get(a__ , a__ ) if from_sanitized not in METRIC_CONVERSION: UpperCamelCase__ = ( f"""Invalid 'from_type' value: {from_type!r}.\n""" f"""Conversion abbreviations are: {", ".join(a__ )}""" ) raise ValueError(a__ ) if to_sanitized not in METRIC_CONVERSION: UpperCamelCase__ = ( f"""Invalid 'to_type' value: {to_type!r}.\n""" f"""Conversion abbreviations are: {", ".join(a__ )}""" ) raise ValueError(a__ ) UpperCamelCase__ = METRIC_CONVERSION[from_sanitized] UpperCamelCase__ = METRIC_CONVERSION[to_sanitized] UpperCamelCase__ = 1 if from_exponent > to_exponent: UpperCamelCase__ = from_exponent - to_exponent else: UpperCamelCase__ = -(to_exponent - from_exponent) return value * pow(10 , a__ ) if __name__ == "__main__": from doctest import testmod testmod()
87
1
import unittest import numpy as np import torch from diffusers import ScoreSdeVePipeline, ScoreSdeVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class snake_case_ (unittest.TestCase ): @property def lowerCamelCase__( self :int ) -> str: torch.manual_seed(0 ) a__ = UNetaDModel( block_out_channels=(32, 64) ,layers_per_block=2 ,sample_size=32 ,in_channels=3 ,out_channels=3 ,down_block_types=('DownBlock2D', 'AttnDownBlock2D') ,up_block_types=('AttnUpBlock2D', 'UpBlock2D') ,) return model def lowerCamelCase__( self :Optional[Any] ) -> str: a__ = self.dummy_uncond_unet a__ = ScoreSdeVeScheduler() a__ = ScoreSdeVePipeline(unet=__snake_case ,scheduler=__snake_case ) sde_ve.to(__snake_case ) sde_ve.set_progress_bar_config(disable=__snake_case ) a__ = torch.manual_seed(0 ) a__ = sde_ve(num_inference_steps=2 ,output_type='numpy' ,generator=__snake_case ).images a__ = torch.manual_seed(0 ) a__ = sde_ve(num_inference_steps=2 ,output_type='numpy' ,generator=__snake_case ,return_dict=__snake_case )[ 0 ] a__ = image[0, -3:, -3:, -1] a__ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) a__ = np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 @slow @require_torch class snake_case_ (unittest.TestCase ): def lowerCamelCase__( self :Optional[Any] ) -> str: a__ = 'google/ncsnpp-church-256' a__ = UNetaDModel.from_pretrained(__snake_case ) a__ = ScoreSdeVeScheduler.from_pretrained(__snake_case ) a__ = ScoreSdeVePipeline(unet=__snake_case ,scheduler=__snake_case ) sde_ve.to(__snake_case ) sde_ve.set_progress_bar_config(disable=__snake_case ) a__ = torch.manual_seed(0 ) a__ = sde_ve(num_inference_steps=10 ,output_type='numpy' ,generator=__snake_case ).images a__ = image[0, -3:, -3:, -1] assert image.shape == (1, 2_56, 2_56, 3) a__ = np.array([0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
240
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available snake_case : str = { '''configuration_rag''': ['''RagConfig'''], '''retrieval_rag''': ['''RagRetriever'''], '''tokenization_rag''': ['''RagTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case : Any = [ '''RagModel''', '''RagPreTrainedModel''', '''RagSequenceForGeneration''', '''RagTokenForGeneration''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case : Optional[int] = [ '''TFRagModel''', '''TFRagPreTrainedModel''', '''TFRagSequenceForGeneration''', '''TFRagTokenForGeneration''', ] if TYPE_CHECKING: from .configuration_rag import RagConfig from .retrieval_rag import RagRetriever from .tokenization_rag import RagTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_rag import RagModel, RagPreTrainedModel, RagSequenceForGeneration, RagTokenForGeneration try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_rag import ( TFRagModel, TFRagPreTrainedModel, TFRagSequenceForGeneration, TFRagTokenForGeneration, ) else: import sys snake_case : int = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
240
1
"""simple docstring""" import warnings from ...configuration_utils import PretrainedConfig from ...utils import logging snake_case__ : str = logging.get_logger(__name__) snake_case__ : int = { '''RUCAIBox/mvp''': '''https://huggingface.co/RUCAIBox/mvp/resolve/main/config.json''', } class snake_case_( a__ ): __UpperCamelCase = '''mvp''' __UpperCamelCase = ['''past_key_values'''] __UpperCamelCase = {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''} def __init__( self : int , UpperCamelCase_ : Dict=5_0_2_6_7 , UpperCamelCase_ : List[Any]=1_0_2_4 , UpperCamelCase_ : str=1_2 , UpperCamelCase_ : Optional[Any]=4_0_9_6 , UpperCamelCase_ : List[str]=1_6 , UpperCamelCase_ : int=1_2 , UpperCamelCase_ : Any=4_0_9_6 , UpperCamelCase_ : Tuple=1_6 , UpperCamelCase_ : Any=0.0 , UpperCamelCase_ : Dict=0.0 , UpperCamelCase_ : int="gelu" , UpperCamelCase_ : Tuple=1_0_2_4 , UpperCamelCase_ : Any=0.1 , UpperCamelCase_ : Optional[Any]=0.0 , UpperCamelCase_ : Any=0.0 , UpperCamelCase_ : int=0.02 , UpperCamelCase_ : Dict=0.0 , UpperCamelCase_ : Tuple=False , UpperCamelCase_ : int=True , UpperCamelCase_ : Tuple=1 , UpperCamelCase_ : str=0 , UpperCamelCase_ : int=2 , UpperCamelCase_ : Optional[Any]=True , UpperCamelCase_ : Optional[int]=2 , UpperCamelCase_ : Dict=2 , UpperCamelCase_ : int=False , UpperCamelCase_ : int=1_0_0 , UpperCamelCase_ : Optional[int]=8_0_0 , **UpperCamelCase_ : List[Any] , ): lowerCAmelCase : Optional[Any] = vocab_size lowerCAmelCase : Dict = max_position_embeddings lowerCAmelCase : List[Any] = d_model lowerCAmelCase : List[Any] = encoder_ffn_dim lowerCAmelCase : Optional[Any] = encoder_layers lowerCAmelCase : Union[str, Any] = encoder_attention_heads lowerCAmelCase : List[Any] = decoder_ffn_dim lowerCAmelCase : Union[str, Any] = decoder_layers lowerCAmelCase : List[str] = decoder_attention_heads lowerCAmelCase : Union[str, Any] = dropout lowerCAmelCase : Optional[int] = attention_dropout lowerCAmelCase : Optional[int] = activation_dropout lowerCAmelCase : Tuple = activation_function lowerCAmelCase : int = init_std lowerCAmelCase : Any = encoder_layerdrop lowerCAmelCase : Union[str, Any] = decoder_layerdrop lowerCAmelCase : Optional[Any] = classifier_dropout lowerCAmelCase : List[Any] = use_cache lowerCAmelCase : Union[str, Any] = encoder_layers lowerCAmelCase : str = scale_embedding # scale factor will be sqrt(d_model) if True lowerCAmelCase : Union[str, Any] = use_prompt lowerCAmelCase : Union[str, Any] = prompt_length lowerCAmelCase : Tuple = prompt_mid_dim super().__init__( pad_token_id=UpperCamelCase_ , bos_token_id=UpperCamelCase_ , eos_token_id=UpperCamelCase_ , is_encoder_decoder=UpperCamelCase_ , decoder_start_token_id=UpperCamelCase_ , forced_eos_token_id=UpperCamelCase_ , **UpperCamelCase_ , ) if self.forced_bos_token_id is None and kwargs.get('''force_bos_token_to_be_generated''' , UpperCamelCase_ ): lowerCAmelCase : List[Any] = self.bos_token_id warnings.warn( F'''Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. ''' '''The config can simply be saved and uploaded again to be fixed.''' )
358
"""simple docstring""" import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_bert import BertTokenizer snake_case__ : str = logging.get_logger(__name__) snake_case__ : List[str] = {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''} snake_case__ : str = { '''vocab_file''': { '''bert-base-uncased''': '''https://huggingface.co/bert-base-uncased/resolve/main/vocab.txt''', '''bert-large-uncased''': '''https://huggingface.co/bert-large-uncased/resolve/main/vocab.txt''', '''bert-base-cased''': '''https://huggingface.co/bert-base-cased/resolve/main/vocab.txt''', '''bert-large-cased''': '''https://huggingface.co/bert-large-cased/resolve/main/vocab.txt''', '''bert-base-multilingual-uncased''': ( '''https://huggingface.co/bert-base-multilingual-uncased/resolve/main/vocab.txt''' ), '''bert-base-multilingual-cased''': '''https://huggingface.co/bert-base-multilingual-cased/resolve/main/vocab.txt''', '''bert-base-chinese''': '''https://huggingface.co/bert-base-chinese/resolve/main/vocab.txt''', '''bert-base-german-cased''': '''https://huggingface.co/bert-base-german-cased/resolve/main/vocab.txt''', '''bert-large-uncased-whole-word-masking''': ( '''https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/vocab.txt''' ), '''bert-large-cased-whole-word-masking''': ( '''https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/vocab.txt''' ), '''bert-large-uncased-whole-word-masking-finetuned-squad''': ( '''https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt''' ), '''bert-large-cased-whole-word-masking-finetuned-squad''': ( '''https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt''' ), '''bert-base-cased-finetuned-mrpc''': ( '''https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/vocab.txt''' ), '''bert-base-german-dbmdz-cased''': '''https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/vocab.txt''', '''bert-base-german-dbmdz-uncased''': ( '''https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/vocab.txt''' ), '''TurkuNLP/bert-base-finnish-cased-v1''': ( '''https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/vocab.txt''' ), '''TurkuNLP/bert-base-finnish-uncased-v1''': ( '''https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/vocab.txt''' ), '''wietsedv/bert-base-dutch-cased''': ( '''https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''bert-base-uncased''': '''https://huggingface.co/bert-base-uncased/resolve/main/tokenizer.json''', '''bert-large-uncased''': '''https://huggingface.co/bert-large-uncased/resolve/main/tokenizer.json''', '''bert-base-cased''': '''https://huggingface.co/bert-base-cased/resolve/main/tokenizer.json''', '''bert-large-cased''': '''https://huggingface.co/bert-large-cased/resolve/main/tokenizer.json''', '''bert-base-multilingual-uncased''': ( '''https://huggingface.co/bert-base-multilingual-uncased/resolve/main/tokenizer.json''' ), '''bert-base-multilingual-cased''': ( '''https://huggingface.co/bert-base-multilingual-cased/resolve/main/tokenizer.json''' ), '''bert-base-chinese''': '''https://huggingface.co/bert-base-chinese/resolve/main/tokenizer.json''', '''bert-base-german-cased''': '''https://huggingface.co/bert-base-german-cased/resolve/main/tokenizer.json''', '''bert-large-uncased-whole-word-masking''': ( '''https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/tokenizer.json''' ), '''bert-large-cased-whole-word-masking''': ( '''https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/tokenizer.json''' ), '''bert-large-uncased-whole-word-masking-finetuned-squad''': ( '''https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json''' ), '''bert-large-cased-whole-word-masking-finetuned-squad''': ( '''https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json''' ), '''bert-base-cased-finetuned-mrpc''': ( '''https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/tokenizer.json''' ), '''bert-base-german-dbmdz-cased''': ( '''https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/tokenizer.json''' ), '''bert-base-german-dbmdz-uncased''': ( '''https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/tokenizer.json''' ), '''TurkuNLP/bert-base-finnish-cased-v1''': ( '''https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/tokenizer.json''' ), '''TurkuNLP/bert-base-finnish-uncased-v1''': ( '''https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/tokenizer.json''' ), '''wietsedv/bert-base-dutch-cased''': ( '''https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/tokenizer.json''' ), }, } snake_case__ : Union[str, Any] = { '''bert-base-uncased''': 512, '''bert-large-uncased''': 512, '''bert-base-cased''': 512, '''bert-large-cased''': 512, '''bert-base-multilingual-uncased''': 512, '''bert-base-multilingual-cased''': 512, '''bert-base-chinese''': 512, '''bert-base-german-cased''': 512, '''bert-large-uncased-whole-word-masking''': 512, '''bert-large-cased-whole-word-masking''': 512, '''bert-large-uncased-whole-word-masking-finetuned-squad''': 512, '''bert-large-cased-whole-word-masking-finetuned-squad''': 512, '''bert-base-cased-finetuned-mrpc''': 512, '''bert-base-german-dbmdz-cased''': 512, '''bert-base-german-dbmdz-uncased''': 512, '''TurkuNLP/bert-base-finnish-cased-v1''': 512, '''TurkuNLP/bert-base-finnish-uncased-v1''': 512, '''wietsedv/bert-base-dutch-cased''': 512, } snake_case__ : Optional[Any] = { '''bert-base-uncased''': {'''do_lower_case''': True}, '''bert-large-uncased''': {'''do_lower_case''': True}, '''bert-base-cased''': {'''do_lower_case''': False}, '''bert-large-cased''': {'''do_lower_case''': False}, '''bert-base-multilingual-uncased''': {'''do_lower_case''': True}, '''bert-base-multilingual-cased''': {'''do_lower_case''': False}, '''bert-base-chinese''': {'''do_lower_case''': False}, '''bert-base-german-cased''': {'''do_lower_case''': False}, '''bert-large-uncased-whole-word-masking''': {'''do_lower_case''': True}, '''bert-large-cased-whole-word-masking''': {'''do_lower_case''': False}, '''bert-large-uncased-whole-word-masking-finetuned-squad''': {'''do_lower_case''': True}, '''bert-large-cased-whole-word-masking-finetuned-squad''': {'''do_lower_case''': False}, '''bert-base-cased-finetuned-mrpc''': {'''do_lower_case''': False}, '''bert-base-german-dbmdz-cased''': {'''do_lower_case''': False}, '''bert-base-german-dbmdz-uncased''': {'''do_lower_case''': True}, '''TurkuNLP/bert-base-finnish-cased-v1''': {'''do_lower_case''': False}, '''TurkuNLP/bert-base-finnish-uncased-v1''': {'''do_lower_case''': True}, '''wietsedv/bert-base-dutch-cased''': {'''do_lower_case''': False}, } class snake_case_( a__ ): __UpperCamelCase = VOCAB_FILES_NAMES __UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase = PRETRAINED_INIT_CONFIGURATION __UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase = BertTokenizer def __init__( self : int , UpperCamelCase_ : Union[str, Any]=None , UpperCamelCase_ : Optional[Any]=None , UpperCamelCase_ : str=True , UpperCamelCase_ : Dict="[UNK]" , UpperCamelCase_ : Any="[SEP]" , UpperCamelCase_ : Any="[PAD]" , UpperCamelCase_ : Tuple="[CLS]" , UpperCamelCase_ : List[Any]="[MASK]" , UpperCamelCase_ : Optional[Any]=True , UpperCamelCase_ : Tuple=None , **UpperCamelCase_ : Optional[int] , ): super().__init__( UpperCamelCase_ , tokenizer_file=UpperCamelCase_ , do_lower_case=UpperCamelCase_ , unk_token=UpperCamelCase_ , sep_token=UpperCamelCase_ , pad_token=UpperCamelCase_ , cls_token=UpperCamelCase_ , mask_token=UpperCamelCase_ , tokenize_chinese_chars=UpperCamelCase_ , strip_accents=UpperCamelCase_ , **UpperCamelCase_ , ) lowerCAmelCase : Any = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('''lowercase''' , UpperCamelCase_ ) != do_lower_case or normalizer_state.get('''strip_accents''' , UpperCamelCase_ ) != strip_accents or normalizer_state.get('''handle_chinese_chars''' , UpperCamelCase_ ) != tokenize_chinese_chars ): lowerCAmelCase : Optional[int] = getattr(UpperCamelCase_ , normalizer_state.pop('''type''' ) ) lowerCAmelCase : Tuple = do_lower_case lowerCAmelCase : Union[str, Any] = strip_accents lowerCAmelCase : Tuple = tokenize_chinese_chars lowerCAmelCase : str = normalizer_class(**UpperCamelCase_ ) lowerCAmelCase : Optional[int] = do_lower_case def lowerCamelCase__ ( self : List[str] , UpperCamelCase_ : Any , UpperCamelCase_ : Tuple=None ): lowerCAmelCase : List[Any] = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def lowerCamelCase__ ( self : List[Any] , UpperCamelCase_ : List[int] , UpperCamelCase_ : Optional[List[int]] = None ): lowerCAmelCase : Optional[Any] = [self.sep_token_id] lowerCAmelCase : Any = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def lowerCamelCase__ ( self : Dict , UpperCamelCase_ : str , UpperCamelCase_ : Optional[str] = None ): lowerCAmelCase : str = self._tokenizer.model.save(UpperCamelCase_ , name=UpperCamelCase_ ) return tuple(UpperCamelCase_ )
314
0
import uuid from typing import Any, Dict, List, Optional, Union from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, Pipeline if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch __lowerCAmelCase : str = logging.get_logger(__name__) class UpperCAmelCase_ : '''simple docstring''' def __init__( self : Optional[int] , UpperCamelCase__ : str = None , UpperCamelCase__ : uuid.UUID = None , UpperCamelCase__ : Any=None , UpperCamelCase__ : List[str]=None ) -> List[str]: """simple docstring""" if not conversation_id: __magic_name__ = uuid.uuida() if past_user_inputs is None: __magic_name__ = [] if generated_responses is None: __magic_name__ = [] __magic_name__ = conversation_id __magic_name__ = past_user_inputs __magic_name__ = generated_responses __magic_name__ = text def __eq__( self : Union[str, Any] , UpperCamelCase__ : List[Any] ) -> List[str]: """simple docstring""" if not isinstance(UpperCamelCase__ , UpperCamelCase__ ): return False if self.uuid == other.uuid: return True return ( self.new_user_input == other.new_user_input and self.past_user_inputs == other.past_user_inputs and self.generated_responses == other.generated_responses ) def _lowercase ( self : Optional[int] , UpperCamelCase__ : str , UpperCamelCase__ : bool = False ) -> Tuple: """simple docstring""" if self.new_user_input: if overwrite: logger.warning( F'''User input added while unprocessed input was existing: "{self.new_user_input}" was overwritten ''' F'''with: "{text}".''' ) __magic_name__ = text else: logger.warning( F'''User input added while unprocessed input was existing: "{self.new_user_input}" new input ''' F'''ignored: "{text}". Set `overwrite` to True to overwrite unprocessed user input''' ) else: __magic_name__ = text def _lowercase ( self : Union[str, Any] ) -> str: """simple docstring""" if self.new_user_input: self.past_user_inputs.append(self.new_user_input ) __magic_name__ = None def _lowercase ( self : Optional[Any] , UpperCamelCase__ : str ) -> Optional[int]: """simple docstring""" self.generated_responses.append(UpperCamelCase__ ) def _lowercase ( self : Tuple ) -> List[Any]: """simple docstring""" for user_input, generated_response in zip(self.past_user_inputs , self.generated_responses ): yield True, user_input yield False, generated_response if self.new_user_input: yield True, self.new_user_input def __repr__( self : Union[str, Any] ) -> Tuple: """simple docstring""" __magic_name__ = F'''Conversation id: {self.uuid} \n''' for is_user, text in self.iter_texts(): __magic_name__ = """user""" if is_user else """bot""" output += F'''{name} >> {text} \n''' return output @add_end_docstrings( _A , R""" min_length_for_response (`int`, *optional*, defaults to 32): The minimum length (in number of tokens) for a response. minimum_tokens (`int`, *optional*, defaults to 10): The minimum length of tokens to leave for a response. """ , ) class UpperCAmelCase_ ( _A ): '''simple docstring''' def __init__( self : List[str] , *UpperCamelCase__ : List[Any] , **UpperCamelCase__ : List[Any] ) -> str: """simple docstring""" super().__init__(*UpperCamelCase__ , **UpperCamelCase__ ) if self.tokenizer.pad_token_id is None: __magic_name__ = self.tokenizer.eos_token def _lowercase ( self : str , UpperCamelCase__ : Optional[Any]=None , UpperCamelCase__ : Tuple=None , UpperCamelCase__ : Tuple=None , **UpperCamelCase__ : Tuple ) -> Any: """simple docstring""" __magic_name__ = {} __magic_name__ = {} __magic_name__ = {} if min_length_for_response is not None: __magic_name__ = min_length_for_response if minimum_tokens is not None: __magic_name__ = minimum_tokens if "max_length" in generate_kwargs: __magic_name__ = generate_kwargs["""max_length"""] # self.max_length = generate_kwargs.get("max_length", self.model.config.max_length) if clean_up_tokenization_spaces is not None: __magic_name__ = clean_up_tokenization_spaces if generate_kwargs: forward_params.update(UpperCamelCase__ ) return preprocess_params, forward_params, postprocess_params def __call__( self : int , UpperCamelCase__ : Union[Conversation, List[Conversation]] , UpperCamelCase__ : Any=0 , **UpperCamelCase__ : List[Any] ) -> str: """simple docstring""" __magic_name__ = super().__call__(UpperCamelCase__ , num_workers=UpperCamelCase__ , **UpperCamelCase__ ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) and len(UpperCamelCase__ ) == 1: return outputs[0] return outputs def _lowercase ( self : List[Any] , UpperCamelCase__ : Conversation , UpperCamelCase__ : Union[str, Any]=32 ) -> Dict[str, Any]: """simple docstring""" if not isinstance(UpperCamelCase__ , UpperCamelCase__ ): raise ValueError("""ConversationalPipeline, expects Conversation as inputs""" ) if conversation.new_user_input is None: raise ValueError( F'''Conversation with UUID {type(conversation.uuid )} does not contain new user input to process. ''' """Add user inputs with the conversation's `add_user_input` method""" ) if hasattr(self.tokenizer , """_build_conversation_input_ids""" ): __magic_name__ = self.tokenizer._build_conversation_input_ids(UpperCamelCase__ ) else: # If the tokenizer cannot handle conversations, we default to only the old version __magic_name__ = self._legacy_parse_and_tokenize(UpperCamelCase__ ) if self.framework == "pt": __magic_name__ = torch.LongTensor([input_ids] ) elif self.framework == "tf": __magic_name__ = tf.constant([input_ids] ) return {"input_ids": input_ids, "conversation": conversation} def _lowercase ( self : Dict , UpperCamelCase__ : Dict , UpperCamelCase__ : List[str]=10 , **UpperCamelCase__ : int ) -> Dict: """simple docstring""" __magic_name__ = generate_kwargs.get("""max_length""" , self.model.config.max_length ) __magic_name__ = model_inputs["""input_ids"""].shape[1] if max_length - minimum_tokens < n: logger.warning(F'''Conversation input is to long ({n}), trimming it to ({max_length} - {minimum_tokens})''' ) __magic_name__ = max_length - minimum_tokens __magic_name__ = model_inputs["""input_ids"""][:, -trim:] if "attention_mask" in model_inputs: __magic_name__ = model_inputs["""attention_mask"""][:, -trim:] __magic_name__ = model_inputs.pop("""conversation""" ) __magic_name__ = max_length __magic_name__ = self.model.generate(**UpperCamelCase__ , **UpperCamelCase__ ) if self.model.config.is_encoder_decoder: __magic_name__ = 1 else: __magic_name__ = n return {"output_ids": output_ids[:, start_position:], "conversation": conversation} def _lowercase ( self : Optional[int] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : List[str]=True ) -> str: """simple docstring""" __magic_name__ = model_outputs["""output_ids"""] __magic_name__ = self.tokenizer.decode( output_ids[0] , skip_special_tokens=UpperCamelCase__ , clean_up_tokenization_spaces=UpperCamelCase__ , ) __magic_name__ = model_outputs["""conversation"""] conversation.mark_processed() conversation.append_response(UpperCamelCase__ ) return conversation def _lowercase ( self : List[Any] , UpperCamelCase__ : Conversation ) -> Dict: """simple docstring""" __magic_name__ = self.tokenizer.eos_token_id __magic_name__ = [] for is_user, text in conversation.iter_texts(): if eos_token_id is not None: input_ids.extend(self.tokenizer.encode(UpperCamelCase__ , add_special_tokens=UpperCamelCase__ ) + [eos_token_id] ) else: input_ids.extend(self.tokenizer.encode(UpperCamelCase__ , add_special_tokens=UpperCamelCase__ ) ) if len(UpperCamelCase__ ) > self.tokenizer.model_max_length: __magic_name__ = input_ids[-self.tokenizer.model_max_length :] return input_ids
88
from __future__ import annotations from collections.abc import Callable from typing import Any, Generic, TypeVar __A : Any = TypeVar('''T''') class __A ( Generic[T] ): def __init__( self : Dict , UpperCAmelCase_ : list[T] , UpperCAmelCase_ : Callable[[T, T], T] ): lowerCAmelCase : Any | T = None lowerCAmelCase : int = len(UpperCAmelCase_ ) lowerCAmelCase : list[T] = [any_type for _ in range(self.N )] + arr lowerCAmelCase : List[Any] = fnc self.build() def lowercase__ ( self : str ): for p in range(self.N - 1 , 0 , -1 ): lowerCAmelCase : Optional[Any] = self.fn(self.st[p * 2] , self.st[p * 2 + 1] ) def lowercase__ ( self : int , UpperCAmelCase_ : int , UpperCAmelCase_ : T ): p += self.N lowerCAmelCase : int = v while p > 1: lowerCAmelCase : List[Any] = p // 2 lowerCAmelCase : List[Any] = self.fn(self.st[p * 2] , self.st[p * 2 + 1] ) def lowercase__ ( self : Optional[Any] , UpperCAmelCase_ : int , UpperCAmelCase_ : int ): # noqa: E741 lowerCAmelCase , lowerCAmelCase : str = l + self.N, r + self.N lowerCAmelCase : T | None = None while l <= r: if l % 2 == 1: lowerCAmelCase : Any = self.st[l] if res is None else self.fn(UpperCAmelCase_ , self.st[l] ) if r % 2 == 0: lowerCAmelCase : Optional[int] = self.st[r] if res is None else self.fn(UpperCAmelCase_ , self.st[r] ) lowerCAmelCase , lowerCAmelCase : Optional[Any] = (l + 1) // 2, (r - 1) // 2 return res if __name__ == "__main__": from functools import reduce __A : str = [1, 10, -2, 9, -3, 8, 4, -7, 5, 6, 11, -12] __A : List[Any] = { 0: 7, 1: 2, 2: 6, 3: -14, 4: 5, 5: 4, 6: 7, 7: -10, 8: 9, 9: 10, 10: 12, 11: 1, } __A : Optional[int] = SegmentTree(test_array, min) __A : Optional[int] = SegmentTree(test_array, max) __A : Dict = SegmentTree(test_array, lambda a, b: a + b) def SCREAMING_SNAKE_CASE__ ( ) -> None: '''simple docstring''' for i in range(len(_UpperCAmelCase ) ): for j in range(_UpperCAmelCase, len(_UpperCAmelCase ) ): lowerCAmelCase : str = reduce(_UpperCAmelCase, test_array[i : j + 1] ) lowerCAmelCase : Dict = reduce(_UpperCAmelCase, test_array[i : j + 1] ) lowerCAmelCase : str = reduce(lambda _UpperCAmelCase, _UpperCAmelCase : a + b, test_array[i : j + 1] ) assert min_range == min_segment_tree.query(_UpperCAmelCase, _UpperCAmelCase ) assert max_range == max_segment_tree.query(_UpperCAmelCase, _UpperCAmelCase ) assert sum_range == sum_segment_tree.query(_UpperCAmelCase, _UpperCAmelCase ) test_all_segments() for index, value in test_updates.items(): __A : int = value min_segment_tree.update(index, value) max_segment_tree.update(index, value) sum_segment_tree.update(index, value) test_all_segments()
138
0
def SCREAMING_SNAKE_CASE__ ( lowercase = 1000 ) -> int: return sum(2 * a * ((a - 1) // 2) for a in range(3 ,n + 1 ) ) if __name__ == "__main__": print(solution())
176
def SCREAMING_SNAKE_CASE__ ( lowercase = 1000 ) -> int: snake_case : Optional[int] = 3 snake_case : List[Any] = 0 while a < n: if a % 3 == 0 or a % 5 == 0: result += a elif a % 15 == 0: result -= a a += 1 return result if __name__ == "__main__": print(f"""{solution() = }""")
176
1
"""simple docstring""" import math import torch from torch import nn from ..configuration_utils import ConfigMixin, register_to_config from .attention_processor import Attention from .embeddings import get_timestep_embedding from .modeling_utils import ModelMixin class __snake_case ( a__ , a__ ): @register_to_config def __init__( self , lowercase = 1_28 , lowercase = 2_56 , lowercase = 2000.0 , lowercase = 7_68 , lowercase = 12 , lowercase = 12 , lowercase = 64 , lowercase = 20_48 , lowercase = 0.1 , ) -> Optional[Any]: '''simple docstring''' super().__init__() a__: Union[str, Any] = nn.Sequential( nn.Linear(lowercase , d_model * 4 , bias=lowercase) , nn.SiLU() , nn.Linear(d_model * 4 , d_model * 4 , bias=lowercase) , nn.SiLU() , ) a__: Optional[Any] = nn.Embedding(lowercase , lowercase) a__: Union[str, Any] = False a__: int = nn.Linear(lowercase , lowercase , bias=lowercase) a__: Optional[Any] = nn.Dropout(p=lowercase) a__: Optional[Any] = nn.ModuleList() for lyr_num in range(lowercase): # FiLM conditional T5 decoder a__: int = DecoderLayer(d_model=lowercase , d_kv=lowercase , num_heads=lowercase , d_ff=lowercase , dropout_rate=lowercase) self.decoders.append(lowercase) a__: Tuple = TaLayerNorm(lowercase) a__: List[Any] = nn.Dropout(p=lowercase) a__: Tuple = nn.Linear(lowercase , lowercase , bias=lowercase) def lowerCamelCase_ ( self , lowercase , lowercase) -> List[str]: '''simple docstring''' a__: int = torch.mul(query_input.unsqueeze(-1) , key_input.unsqueeze(-2)) return mask.unsqueeze(-3) def lowerCamelCase_ ( self , lowercase , lowercase , lowercase) -> List[str]: '''simple docstring''' a__ , a__ , a__: Tuple = decoder_input_tokens.shape assert decoder_noise_time.shape == (batch,) # decoder_noise_time is in [0, 1), so rescale to expected timing range. a__: Optional[int] = get_timestep_embedding( decoder_noise_time * self.config.max_decoder_noise_time , embedding_dim=self.config.d_model , max_period=self.config.max_decoder_noise_time , ).to(dtype=self.dtype) a__: Optional[int] = self.conditioning_emb(lowercase).unsqueeze(1) assert conditioning_emb.shape == (batch, 1, self.config.d_model * 4) a__: int = decoder_input_tokens.shape[1] # If we want to use relative positions for audio context, we can just offset # this sequence by the length of encodings_and_masks. a__: Optional[int] = torch.broadcast_to( torch.arange(lowercase , device=decoder_input_tokens.device) , (batch, seq_length) , ) a__: List[Any] = self.position_encoding(lowercase) a__: Tuple = self.continuous_inputs_projection(lowercase) inputs += position_encodings a__: List[Any] = self.dropout(lowercase) # decoder: No padding present. a__: List[str] = torch.ones( decoder_input_tokens.shape[:2] , device=decoder_input_tokens.device , dtype=inputs.dtype) # Translate encoding masks to encoder-decoder masks. a__: List[str] = [(x, self.encoder_decoder_mask(lowercase , lowercase)) for x, y in encodings_and_masks] # cross attend style: concat encodings a__: str = torch.cat([x[0] for x in encodings_and_encdec_masks] , dim=1) a__: Optional[int] = torch.cat([x[1] for x in encodings_and_encdec_masks] , dim=-1) for lyr in self.decoders: a__: List[str] = lyr( lowercase , conditioning_emb=lowercase , encoder_hidden_states=lowercase , encoder_attention_mask=lowercase , )[0] a__: str = self.decoder_norm(lowercase) a__: Optional[int] = self.post_dropout(lowercase) a__: int = self.spec_out(lowercase) return spec_out class __snake_case ( nn.Module ): def __init__( self , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase=1e-6) -> Optional[Any]: '''simple docstring''' super().__init__() a__: List[str] = nn.ModuleList() # cond self attention: layer 0 self.layer.append( TaLayerSelfAttentionCond(d_model=lowercase , d_kv=lowercase , num_heads=lowercase , dropout_rate=lowercase)) # cross attention: layer 1 self.layer.append( TaLayerCrossAttention( d_model=lowercase , d_kv=lowercase , num_heads=lowercase , dropout_rate=lowercase , layer_norm_epsilon=lowercase , )) # Film Cond MLP + dropout: last layer self.layer.append( TaLayerFFCond(d_model=lowercase , d_ff=lowercase , dropout_rate=lowercase , layer_norm_epsilon=lowercase)) def lowerCamelCase_ ( self , lowercase , lowercase=None , lowercase=None , lowercase=None , lowercase=None , lowercase=None , ) -> str: '''simple docstring''' a__: Optional[int] = self.layer[0]( lowercase , conditioning_emb=lowercase , attention_mask=lowercase , ) if encoder_hidden_states is not None: a__: Union[str, Any] = torch.where(encoder_attention_mask > 0 , 0 , -1e10).to( encoder_hidden_states.dtype) a__: List[str] = self.layer[1]( lowercase , key_value_states=lowercase , attention_mask=lowercase , ) # Apply Film Conditional Feed Forward layer a__: List[Any] = self.layer[-1](lowercase , lowercase) return (hidden_states,) class __snake_case ( nn.Module ): def __init__( self , lowercase , lowercase , lowercase , lowercase) -> Union[str, Any]: '''simple docstring''' super().__init__() a__: Tuple = TaLayerNorm(lowercase) a__: int = TaFiLMLayer(in_features=d_model * 4 , out_features=lowercase) a__: List[str] = Attention(query_dim=lowercase , heads=lowercase , dim_head=lowercase , out_bias=lowercase , scale_qk=lowercase) a__: str = nn.Dropout(lowercase) def lowerCamelCase_ ( self , lowercase , lowercase=None , lowercase=None , ) -> Optional[Any]: '''simple docstring''' a__: Any = self.layer_norm(lowercase) if conditioning_emb is not None: a__: Tuple = self.FiLMLayer(lowercase , lowercase) # Self-attention block a__: Dict = self.attention(lowercase) a__: Optional[Any] = hidden_states + self.dropout(lowercase) return hidden_states class __snake_case ( nn.Module ): def __init__( self , lowercase , lowercase , lowercase , lowercase , lowercase) -> Optional[int]: '''simple docstring''' super().__init__() a__: Optional[Any] = Attention(query_dim=lowercase , heads=lowercase , dim_head=lowercase , out_bias=lowercase , scale_qk=lowercase) a__: Any = TaLayerNorm(lowercase , eps=lowercase) a__: int = nn.Dropout(lowercase) def lowerCamelCase_ ( self , lowercase , lowercase=None , lowercase=None , ) -> Tuple: '''simple docstring''' a__: List[str] = self.layer_norm(lowercase) a__: Optional[Any] = self.attention( lowercase , encoder_hidden_states=lowercase , attention_mask=attention_mask.squeeze(1) , ) a__: Optional[int] = hidden_states + self.dropout(lowercase) return layer_output class __snake_case ( nn.Module ): def __init__( self , lowercase , lowercase , lowercase , lowercase) -> Tuple: '''simple docstring''' super().__init__() a__: str = TaDenseGatedActDense(d_model=lowercase , d_ff=lowercase , dropout_rate=lowercase) a__: Tuple = TaFiLMLayer(in_features=d_model * 4 , out_features=lowercase) a__: Any = TaLayerNorm(lowercase , eps=lowercase) a__: Optional[Any] = nn.Dropout(lowercase) def lowerCamelCase_ ( self , lowercase , lowercase=None) -> Dict: '''simple docstring''' a__: Optional[Any] = self.layer_norm(lowercase) if conditioning_emb is not None: a__: Optional[int] = self.film(lowercase , lowercase) a__: Optional[int] = self.DenseReluDense(lowercase) a__: Optional[int] = hidden_states + self.dropout(lowercase) return hidden_states class __snake_case ( nn.Module ): def __init__( self , lowercase , lowercase , lowercase) -> List[Any]: '''simple docstring''' super().__init__() a__: Tuple = nn.Linear(lowercase , lowercase , bias=lowercase) a__: List[Any] = nn.Linear(lowercase , lowercase , bias=lowercase) a__: Optional[Any] = nn.Linear(lowercase , lowercase , bias=lowercase) a__: List[str] = nn.Dropout(lowercase) a__: List[Any] = NewGELUActivation() def lowerCamelCase_ ( self , lowercase) -> Any: '''simple docstring''' a__: Optional[int] = self.act(self.wi_a(lowercase)) a__: Tuple = self.wi_a(lowercase) a__: Union[str, Any] = hidden_gelu * hidden_linear a__: Union[str, Any] = self.dropout(lowercase) a__: Optional[int] = self.wo(lowercase) return hidden_states class __snake_case ( nn.Module ): def __init__( self , lowercase , lowercase=1e-6) -> int: '''simple docstring''' super().__init__() a__: Optional[int] = nn.Parameter(torch.ones(lowercase)) a__: Dict = eps def lowerCamelCase_ ( self , lowercase) -> str: '''simple docstring''' a__: List[Any] = hidden_states.to(torch.floataa).pow(2).mean(-1 , keepdim=lowercase) a__: Dict = hidden_states * torch.rsqrt(variance + self.variance_epsilon) # convert into half-precision if necessary if self.weight.dtype in [torch.floataa, torch.bfloataa]: a__: List[Any] = hidden_states.to(self.weight.dtype) return self.weight * hidden_states class __snake_case ( nn.Module ): def lowerCamelCase_ ( self , lowercase) -> int: '''simple docstring''' return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi) * (input + 0.044715 * torch.pow(lowercase , 3.0)))) class __snake_case ( nn.Module ): def __init__( self , lowercase , lowercase) -> Dict: '''simple docstring''' super().__init__() a__: Optional[Any] = nn.Linear(lowercase , out_features * 2 , bias=lowercase) def lowerCamelCase_ ( self , lowercase , lowercase) -> Dict: '''simple docstring''' a__: Optional[Any] = self.scale_bias(lowercase) a__ , a__: Tuple = torch.chunk(lowercase , 2 , -1) a__: Tuple = x * (1 + scale) + shift return x
290
'''simple docstring''' from __future__ import annotations import json import requests from bsa import BeautifulSoup from fake_useragent import UserAgent a__ : Tuple = {'UserAgent': UserAgent().random} def _UpperCamelCase ( __A ) -> dict: '''simple docstring''' UpperCamelCase__ = script.contents[0] UpperCamelCase__ = json.loads(data[data.find("{\"config\"" ) : -1] ) return info["entry_data"]["ProfilePage"][0]["graphql"]["user"] class lowercase_ : def __init__( self , a ): UpperCamelCase__ = f'''https://www.instagram.com/{username}/''' UpperCamelCase__ = self.get_json() def __a ( self ): UpperCamelCase__ = requests.get(self.url , headers=a ).text UpperCamelCase__ = BeautifulSoup(a , "html.parser" ).find_all("script" ) try: return extract_user_profile(scripts[4] ) except (json.decoder.JSONDecodeError, KeyError): return extract_user_profile(scripts[3] ) def __repr__( self ): return f'''{self.__class__.__name__}(\'{self.username}\')''' def __str__( self ): return f'''{self.fullname} ({self.username}) is {self.biography}''' @property def __a ( self ): return self.user_data["username"] @property def __a ( self ): return self.user_data["full_name"] @property def __a ( self ): return self.user_data["biography"] @property def __a ( self ): return self.user_data["business_email"] @property def __a ( self ): return self.user_data["external_url"] @property def __a ( self ): return self.user_data["edge_followed_by"]["count"] @property def __a ( self ): return self.user_data["edge_follow"]["count"] @property def __a ( self ): return self.user_data["edge_owner_to_timeline_media"]["count"] @property def __a ( self ): return self.user_data["profile_pic_url_hd"] @property def __a ( self ): return self.user_data["is_verified"] @property def __a ( self ): return self.user_data["is_private"] def _UpperCamelCase ( __A = "github" ) -> None: '''simple docstring''' import os if os.environ.get("CI" ): return # test failing on GitHub Actions UpperCamelCase__ = InstagramUser(__A ) assert instagram_user.user_data assert isinstance(instagram_user.user_data , __A ) assert instagram_user.username == username if username != "github": return assert instagram_user.fullname == "GitHub" assert instagram_user.biography == "Built for developers." assert instagram_user.number_of_posts > 150 assert instagram_user.number_of_followers > 120000 assert instagram_user.number_of_followings > 15 assert instagram_user.email == "support@github.com" assert instagram_user.website == "https://github.com/readme" assert instagram_user.profile_picture_url.startswith("https://instagram." ) assert instagram_user.is_verified is True assert instagram_user.is_private is False if __name__ == "__main__": import doctest doctest.testmod() a__ : Any = InstagramUser('github') print(instagram_user) print(F"""{instagram_user.number_of_posts = }""") print(F"""{instagram_user.number_of_followers = }""") print(F"""{instagram_user.number_of_followings = }""") print(F"""{instagram_user.email = }""") print(F"""{instagram_user.website = }""") print(F"""{instagram_user.profile_picture_url = }""") print(F"""{instagram_user.is_verified = }""") print(F"""{instagram_user.is_private = }""")
80
0
import random from typing import Any def UpperCamelCase ( snake_case__ : list ) -> int: for _ in range(len(__SCREAMING_SNAKE_CASE ) ): UpperCamelCase : List[Any] = random.randint(0 , len(__SCREAMING_SNAKE_CASE ) - 1 ) UpperCamelCase : str = random.randint(0 , len(__SCREAMING_SNAKE_CASE ) - 1 ) UpperCamelCase : str = data[b], data[a] return data if __name__ == "__main__": __UpperCAmelCase = [0, 1, 2, 3, 4, 5, 6, 7] __UpperCAmelCase = ['''python''', '''says''', '''hello''', '''!'''] print('''Fisher-Yates Shuffle:''') print('''List''', integers, strings) print('''FY Shuffle''', fisher_yates_shuffle(integers), fisher_yates_shuffle(strings))
370
import unittest from transformers import JukeboxTokenizer from transformers.testing_utils import require_torch class lowerCAmelCase_ ( unittest.TestCase ): UpperCAmelCase__ : Union[str, Any] = JukeboxTokenizer UpperCAmelCase__ : Optional[int] = { "artist": "Zac Brown Band", "genres": "Country", "lyrics": "I met a traveller from an antique land,\n Who said \"Two vast and trunkless legs of stone\n Stand in the desert. . . . Near them, on the sand,\n Half sunk a shattered visage lies, whose frown,\n And wrinkled lip, and sneer of cold command,\n Tell that its sculptor well those passions read\n Which yet survive, stamped on these lifeless things,\n The hand that mocked them, and the heart that fed;\n And on the pedestal, these words appear:\n My name is Ozymandias, King of Kings;\n Look on my Works, ye Mighty, and despair!\n Nothing beside remains. Round the decay\n Of that colossal Wreck, boundless and bare\n The lone and level sands stretch far away\n ", } @require_torch def snake_case_ ( self ) -> Optional[Any]: import torch UpperCamelCase : Tuple = JukeboxTokenizer.from_pretrained('openai/jukebox-1b-lyrics' ) UpperCamelCase : List[str] = tokenizer(**self.metas )['input_ids'] # fmt: off UpperCamelCase : Dict = [ torch.tensor([[ 0, 0, 0, 7169, 507, 9, 76, 39, 31, 46, 76, 27, 76, 46, 44, 27, 48, 31, 38, 38, 31, 44, 76, 32, 44, 41, 39, 76, 27, 40, 76, 27, 40, 46, 35, 43, 47, 31, 76, 38, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 23, 34, 41, 76, 45, 27, 35, 30, 76, 71, 20, 49, 41, 76, 48, 27, 45, 46, 76, 27, 40, 30, 76, 46, 44, 47, 40, 37, 38, 31, 45, 45, 76, 38, 31, 33, 45, 76, 41, 32, 76, 45, 46, 41, 40, 31, 78, 76, 76, 76, 76, 76, 76, 76, 76, 19, 46, 27, 40, 30, 76, 35, 40, 76, 46, 34, 31, 76, 30, 31, 45, 31, 44, 46, 63, 76, 63, 76, 63, 76, 63, 76, 14, 31, 27, 44, 76, 46, 34, 31, 39, 64, 76, 41, 40, 76, 46, 34, 31, 76, 45, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 8, 27, 38, 32, 76, 45, 47, 40, 37, 76, 27, 76, 45, 34, 27, 46, 46, 31, 44, 31, 30, 76, 48, 35, 45, 27, 33, 31, 76, 38, 35, 31, 45, 64, 76, 49, 34, 41, 45, 31, 76, 32, 44, 41, 49, 40, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 1, 40, 30, 76, 49, 44, 35, 40, 37, 38, 31, 30, 76, 38, 35, 42, 64, 76, 27, 40, 30, 76, 45, 40, 31, 31, 44, 76, 41, 32, 76, 29, 41, 38, 30, 76, 29, 41, 39, 39, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 31, 38, 38, 76, 46, 34, 27, 46, 76, 35, 46, 45, 76, 45, 29, 47, 38, 42, 46, 41, 44, 76, 49, 31, 38, 38, 76, 46, 34, 41, 45, 31, 76, 42, 27, 45, 45, 35, 41, 40, 45, 76, 44, 31, 27, 30, 78, 76, 76, 76, 76, 76, 76, 76, 76, 23, 34, 35, 29, 34, 76, 51, 31, 46, 76, 45, 47, 44, 48, 35, 48, 31, 64, 76, 45, 46, 27, 39, 42, 31, 30, 76, 41, 40, 76, 46, 34, 31, 45, 31, 76, 38, 35, 32, 31, 38, 31, 45, 45, 76, 46, 34, 35, 40, 33, 45, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 34, 31, 76, 34, 27, 40, 30, 76, 46, 34, 27, 46, 76, 39, 41, 29, 37, 31, 30, 76, 46, 34, 31, 39, 64, 76, 27, 40, 30, 76, 46, 34, 31, 76, 34, 31, 27, 44, 46, 76, 46, 34, 27, 46, 76, 32, 31, 30, 66, 78, 76, 76, 76, 76, 76, 76, 76, 76, 1, 40, 30, 76, 41, 40, 76, 46, 34, 31, 76, 42, 31, 30, 31, 45, 46, 27, 38, 64, 76, 46, 34, 31, 45, 31, 76, 49, 41, 44, 30, 45, 76, 27, 42, 42, 31, 27, 44, 65, 78, 76, 76, 76, 76, 76, 76, 76, 76, 13, 51, 76, 40, 27, 39, 31, 76, 35, 45, 76, 15, 52, 51, 39, 27, 40, 30, 35, 27, 45, 64, 76, 11, 35, 40, 33, 76, 41, 32, 76, 11, 35, 40, 33, 45, 66, 78, 76, 76, 76, 76, 76, 76, 76, 76, 12, 41, 41, 37, 76, 41, 40, 76, 39, 51, 76, 23, 41, 44, 37, 45, 64, 76, 51, 31, 76, 13, 35, 33, 34, 46, 51, 64, 76, 27, 40, 30, 76, 30, 31, 45, 42, 27, 35, 44, 67, 78, 76, 76, 76, 76, 76, 76, 76, 76, 14, 41, 46, 34, 35, 40, 33, 76, 28, 31, 45, 35, 30, 31, 76, 44, 31, 39, 27, 35, 40, 45, 63, 76, 18, 41, 47, 40, 30, 76, 46, 34, 31, 76, 30, 31, 29, 27, 51, 78, 76, 76, 76, 76, 76, 76, 76, 76, 15, 32, 76, 46, 34, 27, 46, 76, 29, 41, 38, 41, 45, 45, 27, 38, 76, 23, 44, 31, 29, 37, 64, 76, 28, 41, 47, 40, 30, 38, 31, 45, 45, 76, 27, 40, 30, 76, 28, 27, 44, 31, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 34, 31, 76, 38, 41, 40, 31, 76, 27, 40, 30, 76, 38, 31, 48, 31, 38, 76, 45, 27, 40, 30, 45, 76, 45, 46, 44, 31, 46, 29, 34, 76, 32, 27, 44, 76, 27, 49, 27, 51, 78, 76, 76, 76, 76, 76, 76, 76, 76]] ), torch.tensor([[0, 0, 0, 1069, 11]] ), torch.tensor([[0, 0, 0, 1069, 11]] ), ] # fmt: on self.assertTrue(torch.allclose(tokens[0], EXPECTED_OUTPUT[0] ) ) self.assertTrue(torch.allclose(tokens[1], EXPECTED_OUTPUT[1] ) ) self.assertTrue(torch.allclose(tokens[2], EXPECTED_OUTPUT[2] ) ) @require_torch def snake_case_ ( self ) -> Optional[Any]: import torch UpperCamelCase : str = JukeboxTokenizer.from_pretrained('openai/jukebox-5b-lyrics' ) UpperCamelCase : Dict = tokenizer(**self.metas )['input_ids'] # fmt: off UpperCamelCase : Optional[int] = [ torch.tensor([[ 0, 0, 0, 1069, 11, -1, -1, -1, -1, 9, 77, 39, 31, 46, 77, 27, 77, 46, 44, 27, 48, 31, 38, 38, 31, 44, 77, 32, 44, 41, 39, 77, 27, 40, 77, 27, 40, 46, 35, 43, 47, 31, 77, 38, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 23, 34, 41, 77, 45, 27, 35, 30, 77, 72, 20, 49, 41, 77, 48, 27, 45, 46, 77, 27, 40, 30, 77, 46, 44, 47, 40, 37, 38, 31, 45, 45, 77, 38, 31, 33, 45, 77, 41, 32, 77, 45, 46, 41, 40, 31, 79, 77, 77, 77, 77, 77, 77, 77, 77, 19, 46, 27, 40, 30, 77, 35, 40, 77, 46, 34, 31, 77, 30, 31, 45, 31, 44, 46, 63, 77, 63, 77, 63, 77, 63, 77, 14, 31, 27, 44, 77, 46, 34, 31, 39, 64, 77, 41, 40, 77, 46, 34, 31, 77, 45, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 8, 27, 38, 32, 77, 45, 47, 40, 37, 77, 27, 77, 45, 34, 27, 46, 46, 31, 44, 31, 30, 77, 48, 35, 45, 27, 33, 31, 77, 38, 35, 31, 45, 64, 77, 49, 34, 41, 45, 31, 77, 32, 44, 41, 49, 40, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 1, 40, 30, 77, 49, 44, 35, 40, 37, 38, 31, 30, 77, 38, 35, 42, 64, 77, 27, 40, 30, 77, 45, 40, 31, 31, 44, 77, 41, 32, 77, 29, 41, 38, 30, 77, 29, 41, 39, 39, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 31, 38, 38, 77, 46, 34, 27, 46, 77, 35, 46, 45, 77, 45, 29, 47, 38, 42, 46, 41, 44, 77, 49, 31, 38, 38, 77, 46, 34, 41, 45, 31, 77, 42, 27, 45, 45, 35, 41, 40, 45, 77, 44, 31, 27, 30, 79, 77, 77, 77, 77, 77, 77, 77, 77, 23, 34, 35, 29, 34, 77, 51, 31, 46, 77, 45, 47, 44, 48, 35, 48, 31, 64, 77, 45, 46, 27, 39, 42, 31, 30, 77, 41, 40, 77, 46, 34, 31, 45, 31, 77, 38, 35, 32, 31, 38, 31, 45, 45, 77, 46, 34, 35, 40, 33, 45, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 34, 31, 77, 34, 27, 40, 30, 77, 46, 34, 27, 46, 77, 39, 41, 29, 37, 31, 30, 77, 46, 34, 31, 39, 64, 77, 27, 40, 30, 77, 46, 34, 31, 77, 34, 31, 27, 44, 46, 77, 46, 34, 27, 46, 77, 32, 31, 30, 66, 79, 77, 77, 77, 77, 77, 77, 77, 77, 1, 40, 30, 77, 41, 40, 77, 46, 34, 31, 77, 42, 31, 30, 31, 45, 46, 27, 38, 64, 77, 46, 34, 31, 45, 31, 77, 49, 41, 44, 30, 45, 77, 27, 42, 42, 31, 27, 44, 65, 79, 77, 77, 77, 77, 77, 77, 77, 77, 13, 51, 77, 40, 27, 39, 31, 77, 35, 45, 77, 15, 52, 51, 39, 27, 40, 30, 35, 27, 45, 64, 77, 11, 35, 40, 33, 77, 41, 32, 77, 11, 35, 40, 33, 45, 66, 79, 77, 77, 77, 77, 77, 77, 77, 77, 12, 41, 41, 37, 77, 41, 40, 77, 39, 51, 77, 23, 41, 44, 37, 45, 64, 77, 51, 31, 77, 13, 35, 33, 34, 46, 51, 64, 77, 27, 40, 30, 77, 30, 31, 45, 42, 27, 35, 44, 67, 79, 77, 77, 77, 77, 77, 77, 77, 77, 14, 41, 46, 34, 35, 40, 33, 77, 28, 31, 45, 35, 30, 31, 77, 44, 31, 39, 27, 35, 40, 45, 63, 77, 18, 41, 47, 40, 30, 77, 46, 34, 31, 77, 30, 31, 29, 27, 51, 79, 77, 77, 77, 77, 77, 77, 77, 77, 15, 32, 77, 46, 34, 27, 46, 77, 29, 41, 38, 41, 45, 45, 27, 38, 77, 23, 44, 31, 29, 37, 64, 77, 28, 41, 47, 40, 30, 38, 31, 45, 45, 77, 27, 40, 30, 77, 28, 27, 44, 31, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 34, 31, 77, 38, 41, 40, 31, 77, 27, 40, 30, 77, 38, 31, 48, 31, 38, 77, 45, 27, 40, 30, 45, 77, 45, 46, 44, 31, 46, 29, 34, 77, 32, 27, 44, 77, 27, 49, 27, 51, 79, 77, 77, 77, 77, 77, 77, 77, 77]] ), torch.tensor([[0, 0, 0, 1069, 11, -1, -1, -1, -1]] ), torch.tensor([[0, 0, 0, 1069, 11, -1, -1, -1, -1]] ), ] # fmt: on self.assertTrue(torch.allclose(tokens[0], EXPECTED_OUTPUT[0] ) ) self.assertTrue(torch.allclose(tokens[1], EXPECTED_OUTPUT[1] ) ) self.assertTrue(torch.allclose(tokens[2], EXPECTED_OUTPUT[2] ) )
103
0
from collections import Counter from pathlib import Path from typing import Optional, Tuple import yaml class a__ ( yaml.SafeLoader ): def __UpperCamelCase ( self : str,_A : List[str] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Any = [self.constructed_objects[key_node] for key_node, _ in node.value] SCREAMING_SNAKE_CASE_ : List[str] = [tuple(_A ) if isinstance(_A,_A ) else key for key in keys] SCREAMING_SNAKE_CASE_ : Optional[int] = Counter(_A ) SCREAMING_SNAKE_CASE_ : Tuple = [key for key in counter if counter[key] > 1] if duplicate_keys: raise TypeError(F'Got duplicate yaml keys: {duplicate_keys}' ) def __UpperCamelCase ( self : Tuple,_A : Dict,_A : List[Any]=False ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = super().construct_mapping(_A,deep=_A ) self._check_no_duplicates_on_constructed_node(_A ) return mapping def _snake_case ( lowerCAmelCase : str ): """simple docstring""" SCREAMING_SNAKE_CASE_ : str = list(readme_content.splitlines() ) if full_content and full_content[0] == "---" and "---" in full_content[1:]: SCREAMING_SNAKE_CASE_ : List[Any] = full_content[1:].index("---" ) + 1 SCREAMING_SNAKE_CASE_ : int = "\n".join(full_content[1:sep_idx] ) return yamlblock, "\n".join(full_content[sep_idx + 1 :] ) return None, "\n".join(lowerCAmelCase ) class a__ ( A__ ): # class attributes A = {'train_eval_index'} # train-eval-index in the YAML metadata @classmethod def __UpperCamelCase ( cls : Any,_A : Path ): """simple docstring""" with open(_A,encoding="utf-8" ) as readme_file: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Dict = _split_yaml_from_readme(readme_file.read() ) if yaml_string is not None: return cls.from_yaml_string(_A ) else: return cls() def __UpperCamelCase ( self : Dict,_A : Path ): """simple docstring""" if path.exists(): with open(_A,encoding="utf-8" ) as readme_file: SCREAMING_SNAKE_CASE_ : int = readme_file.read() else: SCREAMING_SNAKE_CASE_ : Any = None SCREAMING_SNAKE_CASE_ : int = self._to_readme(_A ) with open(_A,"w",encoding="utf-8" ) as readme_file: readme_file.write(_A ) def __UpperCamelCase ( self : Optional[int],_A : Optional[str] = None ): """simple docstring""" if readme_content is not None: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Optional[Any] = _split_yaml_from_readme(_A ) SCREAMING_SNAKE_CASE_ : Tuple = "---\n" + self.to_yaml_string() + "---\n" + content else: SCREAMING_SNAKE_CASE_ : Dict = "---\n" + self.to_yaml_string() + "---\n" return full_content @classmethod def __UpperCamelCase ( cls : Dict,_A : str ): """simple docstring""" SCREAMING_SNAKE_CASE_ : str = yaml.load(_A,Loader=_NoDuplicateSafeLoader ) or {} # Convert the YAML keys to DatasetMetadata fields SCREAMING_SNAKE_CASE_ : Any = { (key.replace("-","_" ) if key.replace("-","_" ) in cls._FIELDS_WITH_DASHES else key): value for key, value in metadata_dict.items() } return cls(**_A ) def __UpperCamelCase ( self : Dict ): """simple docstring""" return yaml.safe_dump( { (key.replace("_","-" ) if key in self._FIELDS_WITH_DASHES else key): value for key, value in self.items() },sort_keys=_A,allow_unicode=_A,encoding="utf-8",).decode("utf-8" ) __lowerCamelCase : List[Any] = { '''image-classification''': [], '''translation''': [], '''image-segmentation''': [], '''fill-mask''': [], '''automatic-speech-recognition''': [], '''token-classification''': [], '''sentence-similarity''': [], '''audio-classification''': [], '''question-answering''': [], '''summarization''': [], '''zero-shot-classification''': [], '''table-to-text''': [], '''feature-extraction''': [], '''other''': [], '''multiple-choice''': [], '''text-classification''': [], '''text-to-image''': [], '''text2text-generation''': [], '''zero-shot-image-classification''': [], '''tabular-classification''': [], '''tabular-regression''': [], '''image-to-image''': [], '''tabular-to-text''': [], '''unconditional-image-generation''': [], '''text-retrieval''': [], '''text-to-speech''': [], '''object-detection''': [], '''audio-to-audio''': [], '''text-generation''': [], '''conversational''': [], '''table-question-answering''': [], '''visual-question-answering''': [], '''image-to-text''': [], '''reinforcement-learning''': [], '''voice-activity-detection''': [], '''time-series-forecasting''': [], '''document-question-answering''': [], } if __name__ == "__main__": from argparse import ArgumentParser __lowerCamelCase : List[Any] = ArgumentParser(usage='''Validate the yaml metadata block of a README.md file.''') ap.add_argument('''readme_filepath''') __lowerCamelCase : Dict = ap.parse_args() __lowerCamelCase : List[Any] = Path(args.readme_filepath) __lowerCamelCase : Optional[int] = DatasetMetadata.from_readme(readme_filepath) print(dataset_metadata) dataset_metadata.to_readme(readme_filepath)
18
import unittest from transformers import SqueezeBertConfig, is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, SqueezeBertModel, ) class a__ ( A__ ): def __init__( self : Tuple,_A : Optional[int],_A : Any=13,_A : List[str]=7,_A : int=True,_A : Dict=True,_A : Dict=False,_A : List[Any]=True,_A : Any=99,_A : Optional[int]=32,_A : Any=5,_A : List[Any]=4,_A : Dict=64,_A : Optional[Any]="gelu",_A : Tuple=0.1,_A : Any=0.1,_A : List[Any]=512,_A : Dict=16,_A : Optional[Any]=2,_A : Union[str, Any]=0.02,_A : List[str]=3,_A : Optional[Any]=4,_A : Union[str, Any]=None,_A : Tuple=2,_A : List[str]=2,_A : str=2,_A : Dict=2,_A : Optional[Any]=4,_A : Union[str, Any]=1,): """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = parent SCREAMING_SNAKE_CASE_ : Optional[int] = batch_size SCREAMING_SNAKE_CASE_ : Dict = seq_length SCREAMING_SNAKE_CASE_ : Dict = is_training SCREAMING_SNAKE_CASE_ : Optional[int] = use_input_mask SCREAMING_SNAKE_CASE_ : int = use_token_type_ids SCREAMING_SNAKE_CASE_ : Optional[int] = use_labels SCREAMING_SNAKE_CASE_ : Tuple = vocab_size SCREAMING_SNAKE_CASE_ : Any = hidden_size SCREAMING_SNAKE_CASE_ : Union[str, Any] = num_hidden_layers SCREAMING_SNAKE_CASE_ : Tuple = num_attention_heads SCREAMING_SNAKE_CASE_ : List[Any] = intermediate_size SCREAMING_SNAKE_CASE_ : List[str] = hidden_act SCREAMING_SNAKE_CASE_ : List[str] = hidden_dropout_prob SCREAMING_SNAKE_CASE_ : Optional[int] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ : str = max_position_embeddings SCREAMING_SNAKE_CASE_ : str = type_vocab_size SCREAMING_SNAKE_CASE_ : List[str] = type_sequence_label_size SCREAMING_SNAKE_CASE_ : Optional[Any] = initializer_range SCREAMING_SNAKE_CASE_ : Tuple = num_labels SCREAMING_SNAKE_CASE_ : List[Any] = num_choices SCREAMING_SNAKE_CASE_ : Dict = scope SCREAMING_SNAKE_CASE_ : int = q_groups SCREAMING_SNAKE_CASE_ : Tuple = k_groups SCREAMING_SNAKE_CASE_ : List[Any] = v_groups SCREAMING_SNAKE_CASE_ : Tuple = post_attention_groups SCREAMING_SNAKE_CASE_ : int = intermediate_groups SCREAMING_SNAKE_CASE_ : List[Any] = output_groups def __UpperCamelCase ( self : Union[str, Any] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[Any] = ids_tensor([self.batch_size, self.seq_length],self.vocab_size ) SCREAMING_SNAKE_CASE_ : List[Any] = None if self.use_input_mask: SCREAMING_SNAKE_CASE_ : List[str] = random_attention_mask([self.batch_size, self.seq_length] ) SCREAMING_SNAKE_CASE_ : Optional[Any] = None SCREAMING_SNAKE_CASE_ : Any = None SCREAMING_SNAKE_CASE_ : str = None if self.use_labels: SCREAMING_SNAKE_CASE_ : str = ids_tensor([self.batch_size],self.type_sequence_label_size ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length],self.num_labels ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = ids_tensor([self.batch_size],self.num_choices ) SCREAMING_SNAKE_CASE_ : Optional[int] = self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def __UpperCamelCase ( self : str ): """simple docstring""" return SqueezeBertConfig( embedding_size=self.hidden_size,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,attention_probs_dropout_prob=self.hidden_dropout_prob,attention_dropout=self.attention_probs_dropout_prob,max_position_embeddings=self.max_position_embeddings,initializer_range=self.initializer_range,q_groups=self.q_groups,k_groups=self.k_groups,v_groups=self.v_groups,post_attention_groups=self.post_attention_groups,intermediate_groups=self.intermediate_groups,output_groups=self.output_groups,) def __UpperCamelCase ( self : Tuple,_A : Union[str, Any],_A : Union[str, Any],_A : int,_A : Optional[int],_A : Union[str, Any],_A : Any ): """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = SqueezeBertModel(config=_A ) model.to(_A ) model.eval() SCREAMING_SNAKE_CASE_ : Any = model(_A,_A ) SCREAMING_SNAKE_CASE_ : List[str] = model(_A ) self.parent.assertEqual(result.last_hidden_state.shape,(self.batch_size, self.seq_length, self.hidden_size) ) def __UpperCamelCase ( self : Dict,_A : Any,_A : Tuple,_A : str,_A : Any,_A : Union[str, Any],_A : Any ): """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = SqueezeBertForMaskedLM(config=_A ) model.to(_A ) model.eval() SCREAMING_SNAKE_CASE_ : List[str] = model(_A,attention_mask=_A,labels=_A ) self.parent.assertEqual(result.logits.shape,(self.batch_size, self.seq_length, self.vocab_size) ) def __UpperCamelCase ( self : Optional[int],_A : Union[str, Any],_A : Union[str, Any],_A : Any,_A : Tuple,_A : int,_A : List[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = SqueezeBertForQuestionAnswering(config=_A ) model.to(_A ) model.eval() SCREAMING_SNAKE_CASE_ : Dict = model( _A,attention_mask=_A,start_positions=_A,end_positions=_A ) self.parent.assertEqual(result.start_logits.shape,(self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape,(self.batch_size, self.seq_length) ) def __UpperCamelCase ( self : List[Any],_A : List[str],_A : Tuple,_A : List[Any],_A : List[str],_A : List[str],_A : List[str] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : int = self.num_labels SCREAMING_SNAKE_CASE_ : List[str] = SqueezeBertForSequenceClassification(_A ) model.to(_A ) model.eval() SCREAMING_SNAKE_CASE_ : Dict = model(_A,attention_mask=_A,labels=_A ) self.parent.assertEqual(result.logits.shape,(self.batch_size, self.num_labels) ) def __UpperCamelCase ( self : str,_A : Optional[int],_A : str,_A : List[Any],_A : List[str],_A : str,_A : Dict ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[Any] = self.num_labels SCREAMING_SNAKE_CASE_ : Optional[int] = SqueezeBertForTokenClassification(config=_A ) model.to(_A ) model.eval() SCREAMING_SNAKE_CASE_ : Optional[int] = model(_A,attention_mask=_A,labels=_A ) self.parent.assertEqual(result.logits.shape,(self.batch_size, self.seq_length, self.num_labels) ) def __UpperCamelCase ( self : List[Any],_A : Tuple,_A : str,_A : Optional[Any],_A : int,_A : str,_A : List[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = self.num_choices SCREAMING_SNAKE_CASE_ : Union[str, Any] = SqueezeBertForMultipleChoice(config=_A ) model.to(_A ) model.eval() SCREAMING_SNAKE_CASE_ : Dict = input_ids.unsqueeze(1 ).expand(-1,self.num_choices,-1 ).contiguous() SCREAMING_SNAKE_CASE_ : str = input_mask.unsqueeze(1 ).expand(-1,self.num_choices,-1 ).contiguous() SCREAMING_SNAKE_CASE_ : Optional[int] = model( _A,attention_mask=_A,labels=_A,) self.parent.assertEqual(result.logits.shape,(self.batch_size, self.num_choices) ) def __UpperCamelCase ( self : Optional[int] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[Any] = self.prepare_config_and_inputs() ((SCREAMING_SNAKE_CASE_) , (SCREAMING_SNAKE_CASE_) , (SCREAMING_SNAKE_CASE_) , (SCREAMING_SNAKE_CASE_) , (SCREAMING_SNAKE_CASE_) , (SCREAMING_SNAKE_CASE_)) : Dict = config_and_inputs SCREAMING_SNAKE_CASE_ : Dict = {"input_ids": input_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class a__ ( A__ , A__ , unittest.TestCase ): A = ( ( SqueezeBertModel, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, ) if is_torch_available() else None ) A = ( { 'feature-extraction': SqueezeBertModel, 'fill-mask': SqueezeBertForMaskedLM, 'question-answering': SqueezeBertForQuestionAnswering, 'text-classification': SqueezeBertForSequenceClassification, 'token-classification': SqueezeBertForTokenClassification, 'zero-shot': SqueezeBertForSequenceClassification, } if is_torch_available() else {} ) A = False A = True A = False def __UpperCamelCase ( self : int ): """simple docstring""" SCREAMING_SNAKE_CASE_ : int = SqueezeBertModelTester(self ) SCREAMING_SNAKE_CASE_ : List[str] = ConfigTester(self,config_class=_A,dim=37 ) def __UpperCamelCase ( self : List[str] ): """simple docstring""" self.config_tester.run_common_tests() def __UpperCamelCase ( self : Tuple ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_model(*_A ) def __UpperCamelCase ( self : List[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_masked_lm(*_A ) def __UpperCamelCase ( self : int ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_question_answering(*_A ) def __UpperCamelCase ( self : Dict ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_sequence_classification(*_A ) def __UpperCamelCase ( self : Tuple ): """simple docstring""" SCREAMING_SNAKE_CASE_ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_token_classification(*_A ) def __UpperCamelCase ( self : Union[str, Any] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_multiple_choice(*_A ) @slow def __UpperCamelCase ( self : Any ): """simple docstring""" for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_ : Tuple = SqueezeBertModel.from_pretrained(_A ) self.assertIsNotNone(_A ) @require_sentencepiece @require_tokenizers @require_torch class a__ ( unittest.TestCase ): @slow def __UpperCamelCase ( self : int ): """simple docstring""" SCREAMING_SNAKE_CASE_ : str = SqueezeBertForSequenceClassification.from_pretrained("squeezebert/squeezebert-mnli" ) SCREAMING_SNAKE_CASE_ : Optional[int] = torch.tensor([[1, 2_9414, 232, 328, 740, 1140, 1_2695, 69, 13, 1588, 2]] ) SCREAMING_SNAKE_CASE_ : List[Any] = model(_A )[0] SCREAMING_SNAKE_CASE_ : Union[str, Any] = torch.Size((1, 3) ) self.assertEqual(output.shape,_A ) SCREAMING_SNAKE_CASE_ : int = torch.tensor([[0.6401, -0.0349, -0.6041]] ) self.assertTrue(torch.allclose(_A,_A,atol=1E-4 ) )
18
1
"""simple docstring""" from __future__ import annotations import csv import requests from bsa import BeautifulSoup def a_ ( _lowercase = "" ): _UpperCamelCase : Optional[int] = url or '''https://www.imdb.com/chart/top/?ref_=nv_mv_250''' _UpperCamelCase : int = BeautifulSoup(requests.get(_lowercase ).text , '''html.parser''' ) _UpperCamelCase : List[Any] = soup.find_all('''td''' , attrs='''titleColumn''' ) _UpperCamelCase : Optional[int] = soup.find_all('''td''' , class_='''ratingColumn imdbRating''' ) return { title.a.text: float(rating.strong.text ) for title, rating in zip(_lowercase , _lowercase ) } def a_ ( _lowercase = "IMDb_Top_250_Movies.csv" ): _UpperCamelCase : Optional[Any] = get_imdb_top_aaa_movies() with open(_lowercase , '''w''' , newline='''''' ) as out_file: _UpperCamelCase : List[str] = csv.writer(_lowercase ) writer.writerow(['''Movie title''', '''IMDb rating'''] ) for title, rating in movies.items(): writer.writerow([title, rating] ) if __name__ == "__main__": write_movies()
128
"""simple docstring""" import numpy as np import torch from imwatermark import WatermarkEncoder # Copied from https://github.com/Stability-AI/generative-models/blob/613af104c6b85184091d42d374fef420eddb356d/scripts/demo/streamlit_helpers.py#L66 UpperCamelCase_ =0b1_0_1_1_0_0_1_1_1_1_1_0_1_1_0_0_1_0_0_1_0_0_0_0_0_1_1_1_1_0_1_1_1_0_1_1_0_0_0_1_1_0_0_1_1_1_1_0 # bin(x)[2:] gives bits of x as str, use int to convert them to 0/1 UpperCamelCase_ =[int(bit) for bit in bin(WATERMARK_MESSAGE)[2:]] class _a : def __init__( self : str ) -> str: '''simple docstring''' _UpperCamelCase : str = WATERMARK_BITS _UpperCamelCase : Optional[int] = WatermarkEncoder() self.encoder.set_watermark('''bits''', self.watermark ) def snake_case ( self : Dict, lowerCAmelCase__ : torch.FloatTensor ) -> int: '''simple docstring''' if images.shape[-1] < 2_5_6: return images _UpperCamelCase : Union[str, Any] = (2_5_5 * (images / 2 + 0.5)).cpu().permute(0, 2, 3, 1 ).float().numpy() _UpperCamelCase : List[str] = [self.encoder.encode(lowerCAmelCase__, '''dwtDct''' ) for image in images] _UpperCamelCase : Dict = torch.from_numpy(np.array(lowerCAmelCase__ ) ).permute(0, 3, 1, 2 ) _UpperCamelCase : Optional[int] = torch.clamp(2 * (images / 2_5_5 - 0.5), min=-1.0, max=1.0 ) return images
128
1
"""simple docstring""" def A_ ( _lowerCAmelCase : int ): """simple docstring""" return str(_lowerCAmelCase ) == str(_lowerCAmelCase )[::-1] def A_ ( _lowerCAmelCase : int ): """simple docstring""" return int(_lowerCAmelCase ) + int(str(_lowerCAmelCase )[::-1] ) def A_ ( _lowerCAmelCase : int = 1_00_00 ): """simple docstring""" _a = [] for num in range(1, _lowerCAmelCase ): _a = 0 _a = num while iterations < 50: _a = sum_reverse(_lowerCAmelCase ) iterations += 1 if is_palindrome(_lowerCAmelCase ): break else: lychrel_nums.append(_lowerCAmelCase ) return len(_lowerCAmelCase ) if __name__ == "__main__": print(f'{solution() = }')
320
"""simple docstring""" import warnings from ...utils import logging from .image_processing_chinese_clip import ChineseCLIPImageProcessor __snake_case = logging.get_logger(__name__) class __lowerCamelCase ( a__ ): '''simple docstring''' def __init__( self , *__UpperCAmelCase , **__UpperCAmelCase ) -> None: warnings.warn( '''The class ChineseCLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers.''' ''' Please use ChineseCLIPImageProcessor instead.''' , __UpperCAmelCase , ) super().__init__(*__UpperCAmelCase , **__UpperCAmelCase )
320
1
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. _snake_case = 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 _UpperCamelCase ( snake_case__ ) -> Any: 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 _UpperCamelCase ( snake_case__ ) -> List[Any]: from transformers.testing_utils import pytest_addoption_shared pytest_addoption_shared(A_ ) def _UpperCamelCase ( snake_case__ ) -> Tuple: from transformers.testing_utils import pytest_terminal_summary_main __UpperCAmelCase : Optional[int] = terminalreporter.config.getoption("--make-reports" ) if make_reports: pytest_terminal_summary_main(A_, id=A_ ) def _UpperCamelCase ( snake_case__, snake_case__ ) -> Any: # If no tests are collected, pytest exists with code 5, which makes the CI fail. if exitstatus == 5: __UpperCAmelCase : List[Any] = 0 # Doctest custom flag to ignore output. _snake_case = doctest.register_optionflag('''IGNORE_RESULT''') _snake_case = doctest.OutputChecker class _snake_case ( a_ ): def _lowerCamelCase ( self: Dict , __lowerCamelCase: List[Any] , __lowerCamelCase: Dict , __lowerCamelCase: str ) -> Tuple: if IGNORE_RESULT & optionflags: return True return OutputChecker.check_output(self , lowercase_ , lowercase_ , lowercase_ ) _snake_case = CustomOutputChecker _snake_case = HfDoctestModule _snake_case = HfDocTestParser
358
import unittest import numpy as np from transformers import DistilBertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.distilbert.modeling_flax_distilbert import ( FlaxDistilBertForMaskedLM, FlaxDistilBertForMultipleChoice, FlaxDistilBertForQuestionAnswering, FlaxDistilBertForSequenceClassification, FlaxDistilBertForTokenClassification, FlaxDistilBertModel, ) class _snake_case ( unittest.TestCase ): def __init__( self: str , __lowerCamelCase: Optional[int] , __lowerCamelCase: Dict=13 , __lowerCamelCase: List[str]=7 , __lowerCamelCase: Optional[Any]=True , __lowerCamelCase: List[str]=True , __lowerCamelCase: int=True , __lowerCamelCase: List[Any]=True , __lowerCamelCase: Tuple=99 , __lowerCamelCase: List[str]=32 , __lowerCamelCase: Optional[Any]=5 , __lowerCamelCase: List[str]=4 , __lowerCamelCase: str=37 , __lowerCamelCase: Union[str, Any]="gelu" , __lowerCamelCase: int=0.1 , __lowerCamelCase: Optional[Any]=0.1 , __lowerCamelCase: Tuple=5_12 , __lowerCamelCase: int=16 , __lowerCamelCase: str=2 , __lowerCamelCase: Optional[Any]=0.02 , __lowerCamelCase: Optional[Any]=4 , ) -> str: __UpperCAmelCase : Union[str, Any] = parent __UpperCAmelCase : Optional[int] = batch_size __UpperCAmelCase : Optional[Any] = seq_length __UpperCAmelCase : Tuple = is_training __UpperCAmelCase : List[str] = use_attention_mask __UpperCAmelCase : Dict = use_token_type_ids __UpperCAmelCase : Optional[int] = use_labels __UpperCAmelCase : Optional[Any] = vocab_size __UpperCAmelCase : Union[str, Any] = hidden_size __UpperCAmelCase : Dict = num_hidden_layers __UpperCAmelCase : Dict = num_attention_heads __UpperCAmelCase : Tuple = intermediate_size __UpperCAmelCase : Union[str, Any] = hidden_act __UpperCAmelCase : Tuple = hidden_dropout_prob __UpperCAmelCase : str = attention_probs_dropout_prob __UpperCAmelCase : Optional[Any] = max_position_embeddings __UpperCAmelCase : Optional[int] = type_vocab_size __UpperCAmelCase : str = type_sequence_label_size __UpperCAmelCase : Tuple = initializer_range __UpperCAmelCase : str = num_choices def _lowerCamelCase ( self: Optional[Any] ) -> List[str]: __UpperCAmelCase : Any = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __UpperCAmelCase : str = None if self.use_attention_mask: __UpperCAmelCase : List[str] = random_attention_mask([self.batch_size, self.seq_length] ) __UpperCAmelCase : Any = DistilBertConfig( vocab_size=self.vocab_size , dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , hidden_dim=self.intermediate_size , hidden_act=self.hidden_act , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , tie_weights_=__lowerCamelCase , ) return config, input_ids, attention_mask def _lowerCamelCase ( self: str ) -> Any: __UpperCAmelCase : List[str] = self.prepare_config_and_inputs() __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase : Optional[int] = config_and_inputs __UpperCAmelCase : Any = {"input_ids": input_ids, "attention_mask": attention_mask} return config, inputs_dict @require_flax class _snake_case ( _lowercase , unittest.TestCase ): lowerCamelCase__: str = ( ( FlaxDistilBertModel, FlaxDistilBertForMaskedLM, FlaxDistilBertForMultipleChoice, FlaxDistilBertForQuestionAnswering, FlaxDistilBertForSequenceClassification, FlaxDistilBertForTokenClassification, FlaxDistilBertForQuestionAnswering, ) if is_flax_available() else () ) def _lowerCamelCase ( self: List[Any] ) -> Dict: __UpperCAmelCase : Union[str, Any] = FlaxDistilBertModelTester(self ) @slow def _lowerCamelCase ( self: Tuple ) -> Optional[Any]: for model_class_name in self.all_model_classes: __UpperCAmelCase : Optional[int] = model_class_name.from_pretrained("distilbert-base-uncased" ) __UpperCAmelCase : Dict = model(np.ones((1, 1) ) ) self.assertIsNotNone(__lowerCamelCase ) @require_flax class _snake_case ( unittest.TestCase ): @slow def _lowerCamelCase ( self: int ) -> List[Any]: __UpperCAmelCase : Dict = FlaxDistilBertModel.from_pretrained("distilbert-base-uncased" ) __UpperCAmelCase : Any = np.array([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]] ) __UpperCAmelCase : Optional[int] = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) __UpperCAmelCase : int = model(__lowerCamelCase , attention_mask=__lowerCamelCase )[0] __UpperCAmelCase : str = (1, 11, 7_68) self.assertEqual(output.shape , __lowerCamelCase ) __UpperCAmelCase : Optional[int] = np.array([[[-0.16_39, 0.32_99, 0.16_48], [-0.17_46, 0.32_89, 0.17_10], [-0.18_84, 0.33_57, 0.18_10]]] ) self.assertTrue(jnp.allclose(output[:, 1:4, 1:4] , __lowerCamelCase , atol=1e-4 ) )
342
0
'''simple docstring''' import copy from dataclasses import dataclass from pathlib import Path from typing import Dict, Optional, Union @dataclass class a_ : '''simple docstring''' UpperCamelCase = None UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False UpperCamelCase = None UpperCamelCase = None UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False UpperCamelCase = True UpperCamelCase = None UpperCamelCase = 1 UpperCamelCase = None UpperCamelCase = False UpperCamelCase = None UpperCamelCase = None def snake_case_( self ) -> List[Any]: return self.__class__(**{k: copy.deepcopy(__SCREAMING_SNAKE_CASE ) for k, v in self.__dict__.items()} )
58
import math import time from typing import Dict, List, Optional from torch.utils.data import Dataset from transformers import SeqaSeqTrainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput, speed_metrics if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class _A ( __UpperCAmelCase ): def __init__( self : List[Any] , *__SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Any=None , __SCREAMING_SNAKE_CASE : Union[str, Any]=None , **__SCREAMING_SNAKE_CASE : str): '''simple docstring''' super().__init__(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE) __a = eval_examples __a = post_process_function def _lowerCamelCase ( self : Dict , __SCREAMING_SNAKE_CASE : Optional[Dataset] = None , __SCREAMING_SNAKE_CASE : List[Any]=None , __SCREAMING_SNAKE_CASE : Optional[List[str]] = None , __SCREAMING_SNAKE_CASE : str = "eval" , **__SCREAMING_SNAKE_CASE : Any , ): '''simple docstring''' __a = gen_kwargs.copy() __a = ( gen_kwargs['''max_length'''] if gen_kwargs.get('''max_length''') is not None else self.args.generation_max_length ) __a = ( gen_kwargs['''num_beams'''] if gen_kwargs.get('''num_beams''') is not None else self.args.generation_num_beams ) __a = gen_kwargs __a = self.eval_dataset if eval_dataset is None else eval_dataset __a = self.get_eval_dataloader(__SCREAMING_SNAKE_CASE) __a = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. __a = self.compute_metrics __a = None __a = time.time() __a = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: __a = eval_loop( __SCREAMING_SNAKE_CASE , description='''Evaluation''' , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=__SCREAMING_SNAKE_CASE , metric_key_prefix=__SCREAMING_SNAKE_CASE , ) finally: __a = compute_metrics __a = self.args.eval_batch_size * self.args.world_size if F'{metric_key_prefix}_jit_compilation_time' in output.metrics: start_time += output.metrics[F'{metric_key_prefix}_jit_compilation_time'] output.metrics.update( speed_metrics( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size) , )) if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save: # Only the main node write the results by default __a = self.post_process_function(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) __a = self.compute_metrics(__SCREAMING_SNAKE_CASE) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys()): if not key.startswith(F'{metric_key_prefix}_'): __a = metrics.pop(__SCREAMING_SNAKE_CASE) metrics.update(output.metrics) else: __a = output.metrics if self.args.should_log: # Only the main node log the results by default self.log(__SCREAMING_SNAKE_CASE) if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report()) __a = self.callback_handler.on_evaluate(self.args , self.state , self.control , __SCREAMING_SNAKE_CASE) return metrics def _lowerCamelCase ( self : Tuple , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Tuple=None , __SCREAMING_SNAKE_CASE : str = "test" , **__SCREAMING_SNAKE_CASE : Dict): '''simple docstring''' __a = gen_kwargs.copy() __a = self.get_test_dataloader(__SCREAMING_SNAKE_CASE) # Temporarily disable metric computation, we will do it in the loop here. __a = self.compute_metrics __a = None __a = time.time() __a = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: __a = eval_loop( __SCREAMING_SNAKE_CASE , description='''Prediction''' , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=__SCREAMING_SNAKE_CASE , metric_key_prefix=__SCREAMING_SNAKE_CASE , ) finally: __a = compute_metrics __a = self.args.eval_batch_size * self.args.world_size if F'{metric_key_prefix}_jit_compilation_time' in output.metrics: start_time += output.metrics[F'{metric_key_prefix}_jit_compilation_time'] output.metrics.update( speed_metrics( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size) , )) if self.post_process_function is None or self.compute_metrics is None: return output __a = self.post_process_function(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , '''predict''') __a = self.compute_metrics(__SCREAMING_SNAKE_CASE) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys()): if not key.startswith(F'{metric_key_prefix}_'): __a = metrics.pop(__SCREAMING_SNAKE_CASE) metrics.update(output.metrics) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=__SCREAMING_SNAKE_CASE)
49
0
'''simple docstring''' def a ( lowerCamelCase__ = 10_00 ): '''simple docstring''' return sum(e for e in range(3 , lowerCamelCase__ ) if e % 3 == 0 or e % 5 == 0 ) if __name__ == "__main__": print(F"{solution() = }")
354
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCamelCase :Any = logging.get_logger(__name__) lowerCamelCase :List[Any] = { '''camembert-base''': '''https://huggingface.co/camembert-base/resolve/main/config.json''', '''umberto-commoncrawl-cased-v1''': ( '''https://huggingface.co/Musixmatch/umberto-commoncrawl-cased-v1/resolve/main/config.json''' ), '''umberto-wikipedia-uncased-v1''': ( '''https://huggingface.co/Musixmatch/umberto-wikipedia-uncased-v1/resolve/main/config.json''' ), } class _lowerCAmelCase ( __UpperCAmelCase ): __SCREAMING_SNAKE_CASE : Optional[int] = 'camembert' def __init__(self , lowercase=30522 , lowercase=768 , lowercase=12 , lowercase=12 , lowercase=3072 , lowercase="gelu" , lowercase=0.1 , lowercase=0.1 , lowercase=512 , lowercase=2 , lowercase=0.02 , lowercase=1E-12 , lowercase=1 , lowercase=0 , lowercase=2 , lowercase="absolute" , lowercase=True , lowercase=None , **lowercase , ): super().__init__(pad_token_id=lowercase , bos_token_id=lowercase , eos_token_id=lowercase , **lowercase ) A_ : List[Any] = vocab_size A_ : int = hidden_size A_ : Dict = num_hidden_layers A_ : Dict = num_attention_heads A_ : Optional[Any] = hidden_act A_ : str = intermediate_size A_ : int = hidden_dropout_prob A_ : List[Any] = attention_probs_dropout_prob A_ : Optional[Any] = max_position_embeddings A_ : Optional[int] = type_vocab_size A_ : int = initializer_range A_ : str = layer_norm_eps A_ : int = position_embedding_type A_ : Dict = use_cache A_ : Any = classifier_dropout class _lowerCAmelCase ( __UpperCAmelCase ): @property def _a (self ): if self.task == "multiple-choice": A_ : Optional[Any] = {0: """batch""", 1: """choice""", 2: """sequence"""} else: A_ : int = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
135
0
import warnings from ...utils import logging from .image_processing_owlvit import OwlViTImageProcessor UpperCAmelCase__ = logging.get_logger(__name__) class lowercase_ ( lowercase ): '''simple docstring''' def __init__( self : Union[str, Any] , *__UpperCAmelCase : str , **__UpperCAmelCase : Tuple ) ->None: """simple docstring""" warnings.warn( '''The class OwlViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use OwlViTImageProcessor instead.''' , __UpperCAmelCase , ) super().__init__(*__UpperCAmelCase , **__UpperCAmelCase )
0
'''simple docstring''' import random import sys import numpy as np from matplotlib import pyplot as plt from matplotlib.colors import ListedColormap lowercase_ = """Usage of script: script_name <size_of_canvas:int>""" lowercase_ = [0] * 100 + [1] * 10 random.shuffle(choice) def lowerCamelCase ( __lowerCamelCase : int ) ->list[list[bool]]: _SCREAMING_SNAKE_CASE = [[False for i in range(__lowerCamelCase )] for j in range(__lowerCamelCase )] return canvas def lowerCamelCase ( __lowerCamelCase : list[list[bool]] ) ->None: for i, row in enumerate(__lowerCamelCase ): for j, _ in enumerate(__lowerCamelCase ): _SCREAMING_SNAKE_CASE = bool(random.getrandbits(1 ) ) def lowerCamelCase ( __lowerCamelCase : list[list[bool]] ) ->list[list[bool]]: _SCREAMING_SNAKE_CASE = np.array(__lowerCamelCase ) _SCREAMING_SNAKE_CASE = np.array(create_canvas(current_canvas.shape[0] ) ) for r, row in enumerate(__lowerCamelCase ): for c, pt in enumerate(__lowerCamelCase ): _SCREAMING_SNAKE_CASE = __judge_point( __lowerCamelCase , current_canvas[r - 1 : r + 2, c - 1 : c + 2] ) _SCREAMING_SNAKE_CASE = next_gen_canvas del next_gen_canvas # cleaning memory as we move on. _SCREAMING_SNAKE_CASE = current_canvas.tolist() return return_canvas def lowerCamelCase ( __lowerCamelCase : bool , __lowerCamelCase : list[list[bool]] ) ->bool: _SCREAMING_SNAKE_CASE = 0 _SCREAMING_SNAKE_CASE = 0 # finding dead or alive neighbours count. for i in neighbours: for status in i: if status: alive += 1 else: dead += 1 # handling duplicate entry for focus pt. if pt: alive -= 1 else: dead -= 1 # running the rules of game here. _SCREAMING_SNAKE_CASE = pt if pt: if alive < 2: _SCREAMING_SNAKE_CASE = False elif alive == 2 or alive == 3: _SCREAMING_SNAKE_CASE = True elif alive > 3: _SCREAMING_SNAKE_CASE = False else: if alive == 3: _SCREAMING_SNAKE_CASE = True return state if __name__ == "__main__": if len(sys.argv) != 2: raise Exception(usage_doc) lowercase_ = int(sys.argv[1]) # main working structure of this module. lowercase_ = create_canvas(canvas_size) seed(c) lowercase_ , lowercase_ = plt.subplots() fig.show() lowercase_ = ListedColormap(["""w""", """k"""]) try: while True: lowercase_ = run(c) ax.matshow(c, cmap=cmap) fig.canvas.draw() ax.cla() except KeyboardInterrupt: # do nothing. pass
58
0
import warnings from ...utils import logging from .image_processing_glpn import GLPNImageProcessor SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) class __lowerCamelCase ( __A ): """simple docstring""" def __init__( self , *UpperCAmelCase , **UpperCAmelCase ) -> Dict: '''simple docstring''' warnings.warn( "The class GLPNFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use GLPNImageProcessor instead." , __lowercase , ) super().__init__(*__lowercase , **__lowercase )
359
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available SCREAMING_SNAKE_CASE__ = {"""configuration_mra""": ["""MRA_PRETRAINED_CONFIG_ARCHIVE_MAP""", """MraConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ """MRA_PRETRAINED_MODEL_ARCHIVE_LIST""", """MraForMaskedLM""", """MraForMultipleChoice""", """MraForQuestionAnswering""", """MraForSequenceClassification""", """MraForTokenClassification""", """MraLayer""", """MraModel""", """MraPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_mra import MRA_PRETRAINED_CONFIG_ARCHIVE_MAP, MraConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mra import ( MRA_PRETRAINED_MODEL_ARCHIVE_LIST, MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, MraLayer, MraModel, MraPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure)
297
0
'''simple docstring''' def UpperCamelCase( UpperCAmelCase_ ): UpperCAmelCase : int = '' for ch in key: if ch == " " or ch not in key_no_dups and ch.isalpha(): key_no_dups += ch return key_no_dups def UpperCamelCase( UpperCAmelCase_ ): UpperCAmelCase : Dict = [chr(i + 65 ) for i in range(26 )] # Remove duplicate characters from key UpperCAmelCase : List[str] = remove_duplicates(key.upper() ) UpperCAmelCase : Any = len(UpperCAmelCase_ ) # First fill cipher with key characters UpperCAmelCase : Optional[Any] = {alphabet[i]: char for i, char in enumerate(UpperCAmelCase_ )} # Then map remaining characters in alphabet to # the alphabet from the beginning for i in range(len(UpperCAmelCase_ ) , 26 ): UpperCAmelCase : str = alphabet[i - offset] # Ensure we are not mapping letters to letters previously mapped while char in key: offset -= 1 UpperCAmelCase : str = alphabet[i - offset] UpperCAmelCase : Dict = char return cipher_alphabet def UpperCamelCase( UpperCAmelCase_ , UpperCAmelCase_ ): return "".join(cipher_map.get(UpperCAmelCase_ , UpperCAmelCase_ ) for ch in message.upper() ) def UpperCamelCase( UpperCAmelCase_ , UpperCAmelCase_ ): UpperCAmelCase : List[str] = {v: k for k, v in cipher_map.items()} return "".join(rev_cipher_map.get(UpperCAmelCase_ , UpperCAmelCase_ ) for ch in message.upper() ) def UpperCamelCase( ): UpperCAmelCase : Optional[Any] = input('Enter message to encode or decode: ' ).strip() UpperCAmelCase : Optional[Any] = input('Enter keyword: ' ).strip() UpperCAmelCase : Optional[Any] = input('Encipher or decipher? E/D:' ).strip()[0].lower() try: UpperCAmelCase : List[Any] = {'e': encipher, 'd': decipher}[option] except KeyError: raise KeyError('invalid input option' ) UpperCAmelCase : List[str] = create_cipher_map(UpperCAmelCase_ ) print(func(UpperCAmelCase_ , UpperCAmelCase_ ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
151
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, is_vision_available, ) lowercase__ = {"processing_layoutxlm": ["LayoutXLMProcessor"]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ = ["LayoutXLMTokenizer"] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ = ["LayoutXLMTokenizerFast"] if TYPE_CHECKING: from .processing_layoutxlm import LayoutXLMProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutxlm import LayoutXLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutxlm_fast import LayoutXLMTokenizerFast else: import sys lowercase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
151
1
'''simple docstring''' from __future__ import annotations import typing from collections.abc import Iterable import numpy as np lowerCamelCase : Union[str, Any] = typing.Union[Iterable[float], Iterable[int], np.ndarray] # noqa: UP007 lowerCamelCase : Any = typing.Union[np.floataa, int, float] # noqa: UP007 def _lowerCAmelCase ( _UpperCamelCase : Vector , _UpperCamelCase : Vector ): """simple docstring""" return np.sqrt(np.sum((np.asarray(_UpperCamelCase ) - np.asarray(_UpperCamelCase )) ** 2 ) ) def _lowerCAmelCase ( _UpperCamelCase : Vector , _UpperCamelCase : Vector ): """simple docstring""" return sum((va - va) ** 2 for va, va in zip(_UpperCamelCase , _UpperCamelCase ) ) ** (1 / 2) if __name__ == "__main__": def _lowerCAmelCase ( ): """simple docstring""" from timeit import timeit print('Without Numpy' ) print( timeit( 'euclidean_distance_no_np([1, 2, 3], [4, 5, 6])' , number=1_00_00 , globals=globals() , ) ) print('With Numpy' ) print( timeit( 'euclidean_distance([1, 2, 3], [4, 5, 6])' , number=1_00_00 , globals=globals() , ) ) benchmark()
368
'''simple docstring''' def _lowerCAmelCase ( _UpperCamelCase : str ) -> bool: """simple docstring""" _SCREAMING_SNAKE_CASE =0 for ch in input_str: _SCREAMING_SNAKE_CASE =ord(_UpperCamelCase ) _SCREAMING_SNAKE_CASE =pow(2 , _UpperCamelCase ) # If we already turned on bit for current character's unicode if bitmap >> ch_unicode & 1 == 1: return False bitmap |= ch_bit_index_on return True if __name__ == "__main__": import doctest doctest.testmod()
114
0
'''simple docstring''' import warnings from contextlib import contextmanager from ...processing_utils import ProcessorMixin class A_ ( lowerCAmelCase_ ): _lowerCamelCase : Tuple = """Speech2TextFeatureExtractor""" _lowerCamelCase : Optional[Any] = """Speech2TextTokenizer""" def __init__( self : Dict , snake_case_ : Optional[Any] , snake_case_ : str ): super().__init__(snake_case_ , snake_case_ ) _UpperCAmelCase = self.feature_extractor _UpperCAmelCase = False def __call__( self : Optional[int] , *snake_case_ : Union[str, Any] , **snake_case_ : str ): # For backward compatibility if self._in_target_context_manager: return self.current_processor(*snake_case_ , **snake_case_ ) if "raw_speech" in kwargs: warnings.warn("Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead." ) _UpperCAmelCase = kwargs.pop("raw_speech" ) else: _UpperCAmelCase = kwargs.pop("audio" , snake_case_ ) _UpperCAmelCase = kwargs.pop("sampling_rate" , snake_case_ ) _UpperCAmelCase = kwargs.pop("text" , snake_case_ ) if len(snake_case_ ) > 0: _UpperCAmelCase = args[0] _UpperCAmelCase = args[1:] if audio is None and text is None: raise ValueError("You need to specify either an `audio` or `text` input to process." ) if audio is not None: _UpperCAmelCase = self.feature_extractor(snake_case_ , *snake_case_ , sampling_rate=snake_case_ , **snake_case_ ) if text is not None: _UpperCAmelCase = self.tokenizer(snake_case_ , **snake_case_ ) if text is None: return inputs elif audio is None: return encodings else: _UpperCAmelCase = encodings["input_ids"] return inputs def lowercase ( self : List[str] , *snake_case_ : List[str] , **snake_case_ : int ): return self.tokenizer.batch_decode(*snake_case_ , **snake_case_ ) def lowercase ( self : Optional[Any] , *snake_case_ : Dict , **snake_case_ : List[str] ): return self.tokenizer.decode(*snake_case_ , **snake_case_ ) @contextmanager def lowercase ( self : List[str] ): warnings.warn( "`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your " "labels by using the argument `text` of the regular `__call__` method (either in the same call as " "your audio inputs, or in a separate call." ) _UpperCAmelCase = True _UpperCAmelCase = self.tokenizer yield _UpperCAmelCase = self.feature_extractor _UpperCAmelCase = False
22
'''simple docstring''' import os import pytest import yaml from datasets.features.features import Features, Value from datasets.info import DatasetInfo, DatasetInfosDict @pytest.mark.parametrize( "files" , [ ["full:README.md", "dataset_infos.json"], ["empty:README.md", "dataset_infos.json"], ["dataset_infos.json"], ["full:README.md"], ] , ) def UpperCAmelCase_ ( __lowercase : Any , __lowercase : int ) -> int: '''simple docstring''' _UpperCAmelCase = tmp_path_factory.mktemp("dset_infos_dir" ) if "full:README.md" in files: with open(dataset_infos_dir / "README.md" , "w" ) as f: f.write("---\ndataset_info:\n dataset_size: 42\n---" ) if "empty:README.md" in files: with open(dataset_infos_dir / "README.md" , "w" ) as f: f.write("" ) # we want to support dataset_infos.json for backward compatibility if "dataset_infos.json" in files: with open(dataset_infos_dir / "dataset_infos.json" , "w" ) as f: f.write("{\"default\": {\"dataset_size\": 42}}" ) _UpperCAmelCase = DatasetInfosDict.from_directory(__lowercase ) assert dataset_infos assert dataset_infos["default"].dataset_size == 42 @pytest.mark.parametrize( "dataset_info" , [ DatasetInfo(), DatasetInfo( description="foo" , features=Features({"a": Value("int32" )} ) , builder_name="builder" , config_name="config" , version="1.0.0" , splits=[{"name": "train"}] , download_size=42 , ), ] , ) def UpperCAmelCase_ ( __lowercase : Tuple , __lowercase : DatasetInfo ) -> Any: '''simple docstring''' _UpperCAmelCase = str(__lowercase ) dataset_info.write_to_directory(__lowercase ) _UpperCAmelCase = DatasetInfo.from_directory(__lowercase ) assert dataset_info == reloaded assert os.path.exists(os.path.join(__lowercase , "dataset_info.json" ) ) def UpperCAmelCase_ ( ) -> Optional[Any]: '''simple docstring''' _UpperCAmelCase = DatasetInfo( description="foo" , citation="bar" , homepage="https://foo.bar" , license="CC0" , features=Features({"a": Value("int32" )} ) , post_processed={} , supervised_keys=() , task_templates=[] , builder_name="builder" , config_name="config" , version="1.0.0" , splits=[{"name": "train", "num_examples": 42}] , download_checksums={} , download_size=1337 , post_processing_size=442 , dataset_size=1234 , size_in_bytes=1337 + 442 + 1234 , ) _UpperCAmelCase = dataset_info._to_yaml_dict() assert sorted(__lowercase ) == sorted(DatasetInfo._INCLUDED_INFO_IN_YAML ) for key in DatasetInfo._INCLUDED_INFO_IN_YAML: assert key in dataset_info_yaml_dict assert isinstance(dataset_info_yaml_dict[key] , (list, dict, int, str) ) _UpperCAmelCase = yaml.safe_dump(__lowercase ) _UpperCAmelCase = yaml.safe_load(__lowercase ) assert dataset_info_yaml_dict == reloaded def UpperCAmelCase_ ( ) -> Optional[int]: '''simple docstring''' _UpperCAmelCase = DatasetInfo() _UpperCAmelCase = dataset_info._to_yaml_dict() assert dataset_info_yaml_dict == {} @pytest.mark.parametrize( "dataset_infos_dict" , [ DatasetInfosDict(), DatasetInfosDict({"default": DatasetInfo()} ), DatasetInfosDict({"my_config_name": DatasetInfo()} ), DatasetInfosDict( { "default": DatasetInfo( description="foo" , features=Features({"a": Value("int32" )} ) , builder_name="builder" , config_name="config" , version="1.0.0" , splits=[{"name": "train"}] , download_size=42 , ) } ), DatasetInfosDict( { "v1": DatasetInfo(dataset_size=42 ), "v2": DatasetInfo(dataset_size=1337 ), } ), ] , ) def UpperCAmelCase_ ( __lowercase : int , __lowercase : DatasetInfosDict ) -> Dict: '''simple docstring''' _UpperCAmelCase = str(__lowercase ) dataset_infos_dict.write_to_directory(__lowercase ) _UpperCAmelCase = DatasetInfosDict.from_directory(__lowercase ) # the config_name of the dataset_infos_dict take over the attribute for config_name, dataset_info in dataset_infos_dict.items(): _UpperCAmelCase = config_name # the yaml representation doesn't include fields like description or citation # so we just test that we can recover what we can from the yaml _UpperCAmelCase = DatasetInfo._from_yaml_dict(dataset_info._to_yaml_dict() ) assert dataset_infos_dict == reloaded if dataset_infos_dict: assert os.path.exists(os.path.join(__lowercase , "README.md" ) )
22
1
from __future__ import annotations from collections import namedtuple from dataclasses import dataclass @dataclass class _SCREAMING_SNAKE_CASE : '''simple docstring''' lowercase_ = 42 lowercase_ = None lowercase_ = None __A = namedtuple("CoinsDistribResult", "moves excess") def lowerCAmelCase_ ( __a ) -> int: """simple docstring""" if root is None: return 0 # Validation def count_nodes(__a ) -> int: if node is None: return 0 return count_nodes(node.left ) + count_nodes(node.right ) + 1 def count_coins(__a ) -> int: if node is None: return 0 return count_coins(node.left ) + count_coins(node.right ) + node.data if count_nodes(__a ) != count_coins(__a ): raise ValueError("The nodes number should be same as the number of coins" ) # Main calculation def get_distrib(__a ) -> CoinsDistribResult: if node is None: return CoinsDistribResult(0 , 1 ) lowerCamelCase__: Dict =get_distrib(node.left ) lowerCamelCase__: Tuple =get_distrib(node.right ) lowerCamelCase__: List[str] =1 - left_distrib_excess lowerCamelCase__: int =1 - right_distrib_excess lowerCamelCase__: List[str] =( left_distrib_moves + right_distrib_moves + abs(__a ) + abs(__a ) ) lowerCamelCase__: Optional[Any] =node.data - coins_to_left - coins_to_right return CoinsDistribResult(__a , __a ) return get_distrib(__a )[0] if __name__ == "__main__": import doctest doctest.testmod()
371
from argparse import ArgumentParser, Namespace from ..utils import logging from . import BaseTransformersCLICommand def lowerCAmelCase_ ( __a ) -> int: """simple docstring""" return ConvertCommand( args.model_type , args.tf_checkpoint , args.pytorch_dump_output , args.config , args.finetuning_task_name ) __A = "\ntransformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires\nTensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.\n" class _SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' @staticmethod def SCREAMING_SNAKE_CASE_ (UpperCAmelCase_ : ArgumentParser) ->str: '''simple docstring''' lowerCamelCase__: Dict =parser.add_parser( "convert" , help="CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints." , ) train_parser.add_argument("--model_type" , type=UpperCAmelCase_ , required=UpperCAmelCase_ , help="Model's type.") train_parser.add_argument( "--tf_checkpoint" , type=UpperCAmelCase_ , required=UpperCAmelCase_ , help="TensorFlow checkpoint path or folder.") train_parser.add_argument( "--pytorch_dump_output" , type=UpperCAmelCase_ , required=UpperCAmelCase_ , help="Path to the PyTorch saved model output.") train_parser.add_argument("--config" , type=UpperCAmelCase_ , default="" , help="Configuration file path or folder.") train_parser.add_argument( "--finetuning_task_name" , type=UpperCAmelCase_ , default=UpperCAmelCase_ , help="Optional fine-tuning task name if the TF model was a finetuned model." , ) train_parser.set_defaults(func=UpperCAmelCase_) def __init__(self : List[Any] , UpperCAmelCase_ : str , UpperCAmelCase_ : str , UpperCAmelCase_ : str , UpperCAmelCase_ : str , UpperCAmelCase_ : str , *UpperCAmelCase_ : Optional[int] , ) ->List[str]: '''simple docstring''' lowerCamelCase__: Dict =logging.get_logger("transformers-cli/converting") self._logger.info(F"""Loading model {model_type}""") lowerCamelCase__: Any =model_type lowerCamelCase__: Optional[int] =tf_checkpoint lowerCamelCase__: Any =pytorch_dump_output lowerCamelCase__: Union[str, Any] =config lowerCamelCase__: str =finetuning_task_name def SCREAMING_SNAKE_CASE_ (self : Union[str, Any]) ->Tuple: '''simple docstring''' if self._model_type == "albert": try: from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(UpperCAmelCase_) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output) elif self._model_type == "bert": try: from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(UpperCAmelCase_) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output) elif self._model_type == "funnel": try: from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(UpperCAmelCase_) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output) elif self._model_type == "t5": try: from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch except ImportError: raise ImportError(UpperCAmelCase_) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output) elif self._model_type == "gpt": from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import ( convert_openai_checkpoint_to_pytorch, ) convert_openai_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output) elif self._model_type == "transfo_xl": try: from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import ( convert_transfo_xl_checkpoint_to_pytorch, ) except ImportError: raise ImportError(UpperCAmelCase_) if "ckpt" in self._tf_checkpoint.lower(): lowerCamelCase__: Tuple =self._tf_checkpoint lowerCamelCase__: List[str] ="" else: lowerCamelCase__: Any =self._tf_checkpoint lowerCamelCase__: Dict ="" convert_transfo_xl_checkpoint_to_pytorch( UpperCAmelCase_ , self._config , self._pytorch_dump_output , UpperCAmelCase_) elif self._model_type == "gpt2": try: from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import ( convert_gpta_checkpoint_to_pytorch, ) except ImportError: raise ImportError(UpperCAmelCase_) convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output) elif self._model_type == "xlnet": try: from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import ( convert_xlnet_checkpoint_to_pytorch, ) except ImportError: raise ImportError(UpperCAmelCase_) convert_xlnet_checkpoint_to_pytorch( self._tf_checkpoint , self._config , self._pytorch_dump_output , self._finetuning_task_name) elif self._model_type == "xlm": from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import ( convert_xlm_checkpoint_to_pytorch, ) convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint , self._pytorch_dump_output) elif self._model_type == "lxmert": from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import ( convert_lxmert_checkpoint_to_pytorch, ) convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint , self._pytorch_dump_output) elif self._model_type == "rembert": from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import ( convert_rembert_tf_checkpoint_to_pytorch, ) convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output) else: raise ValueError( "--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]")
273
0
from typing import Optional, Union import torch from torch import nn from ...configuration_utils import ConfigMixin, register_to_config from ...models.modeling_utils import ModelMixin class A ( __UpperCAmelCase , __UpperCAmelCase ): @register_to_config def __init__( self, UpperCamelCase__ = 768, ): """simple docstring""" super().__init__() lowerCAmelCase_ = nn.Parameter(torch.zeros(1, UpperCamelCase__ ) ) lowerCAmelCase_ = nn.Parameter(torch.ones(1, UpperCamelCase__ ) ) def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__ = None, UpperCamelCase__ = None, ): """simple docstring""" lowerCAmelCase_ = nn.Parameter(self.mean.to(UpperCamelCase__ ).to(UpperCamelCase__ ) ) lowerCAmelCase_ = nn.Parameter(self.std.to(UpperCamelCase__ ).to(UpperCamelCase__ ) ) return self def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__ ): """simple docstring""" lowerCAmelCase_ = (embeds - self.mean) * 1.0 / self.std return embeds def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__ ): """simple docstring""" lowerCAmelCase_ = (embeds * self.std) + self.mean return embeds
278
import warnings from ...utils import logging from .image_processing_beit import BeitImageProcessor _A = logging.get_logger(__name__) class A ( __UpperCAmelCase ): def __init__( self, *UpperCamelCase__, **UpperCamelCase__ ): """simple docstring""" warnings.warn( '''The class BeitFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use BeitImageProcessor instead.''', UpperCamelCase__, ) super().__init__(*UpperCamelCase__, **UpperCamelCase__ )
278
1
'''simple docstring''' from .data_collator import ( DataCollatorForLanguageModeling, DataCollatorForPermutationLanguageModeling, DataCollatorForSeqaSeq, DataCollatorForSOP, DataCollatorForTokenClassification, DataCollatorForWholeWordMask, DataCollatorWithPadding, DefaultDataCollator, default_data_collator, ) from .metrics import glue_compute_metrics, xnli_compute_metrics from .processors import ( DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor, SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels, squad_convert_examples_to_features, xnli_output_modes, xnli_processors, xnli_tasks_num_labels, )
55
'''simple docstring''' from __future__ import annotations from collections.abc import Iterator from typing import Generic, TypeVar __snake_case =TypeVar("""T""") class UpperCAmelCase_ ( Generic[T] ): def __init__( self : int , UpperCAmelCase__ : T ) -> List[str]: lowerCAmelCase = data lowerCAmelCase = None def __str__( self : Optional[int] ) -> str: return F'''{self.data}''' class UpperCAmelCase_ ( Generic[T] ): def __init__( self : Optional[Any] ) -> None: lowerCAmelCase = None def __iter__( self : Any ) -> Iterator[T]: lowerCAmelCase = self.top while node: yield node.data lowerCAmelCase = node.next def __str__( self : str ) -> str: return "->".join([str(UpperCAmelCase__ ) for item in self] ) def __len__( self : Optional[int] ) -> int: return len(tuple(iter(self ) ) ) def __UpperCAmelCase ( self : Optional[Any] ) -> bool: return self.top is None def __UpperCAmelCase ( self : Dict , UpperCAmelCase__ : T ) -> None: lowerCAmelCase = Node(UpperCAmelCase__ ) if not self.is_empty(): lowerCAmelCase = self.top lowerCAmelCase = node def __UpperCAmelCase ( self : str ) -> T: if self.is_empty(): raise IndexError('pop from empty stack' ) assert isinstance(self.top , UpperCAmelCase__ ) lowerCAmelCase = self.top lowerCAmelCase = self.top.next return pop_node.data def __UpperCAmelCase ( self : List[Any] ) -> T: if self.is_empty(): raise IndexError('peek from empty stack' ) assert self.top is not None return self.top.data def __UpperCAmelCase ( self : str ) -> None: lowerCAmelCase = None if __name__ == "__main__": from doctest import testmod testmod()
55
1
import math def __UpperCamelCase ( lowerCAmelCase__ : int ): if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(UpperCAmelCase_ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def __UpperCamelCase ( lowerCAmelCase__ : float = 0.1 ): __a : List[Any] = 3 __a : List[str] = 3 while primes / (2 * j - 1) >= ratio: for i in range(j * j + j + 1 , (j + 2) * (j + 2) , j + 1 ): primes += is_prime(UpperCAmelCase_ ) j += 2 return j if __name__ == "__main__": import doctest doctest.testmod()
216
"""simple docstring""" import sacrebleu as scb from packaging import version from sacrebleu import CHRF import datasets SCREAMING_SNAKE_CASE_ : Dict = '\\n@inproceedings{popovic-2015-chrf,\n title = "chr{F}: character n-gram {F}-score for automatic {MT} evaluation",\n author = "Popovi{\'c}, Maja",\n booktitle = "Proceedings of the Tenth Workshop on Statistical Machine Translation",\n month = sep,\n year = "2015",\n address = "Lisbon, Portugal",\n publisher = "Association for Computational Linguistics",\n url = "https://aclanthology.org/W15-3049",\n doi = "10.18653/v1/W15-3049",\n pages = "392--395",\n}\n@inproceedings{popovic-2017-chrf,\n title = "chr{F}++: words helping character n-grams",\n author = "Popovi{\'c}, Maja",\n booktitle = "Proceedings of the Second Conference on Machine Translation",\n month = sep,\n year = "2017",\n address = "Copenhagen, Denmark",\n publisher = "Association for Computational Linguistics",\n url = "https://aclanthology.org/W17-4770",\n doi = "10.18653/v1/W17-4770",\n pages = "612--618",\n}\n@inproceedings{post-2018-call,\n title = "A Call for Clarity in Reporting {BLEU} Scores",\n author = "Post, Matt",\n booktitle = "Proceedings of the Third Conference on Machine Translation: Research Papers",\n month = oct,\n year = "2018",\n address = "Belgium, Brussels",\n publisher = "Association for Computational Linguistics",\n url = "https://www.aclweb.org/anthology/W18-6319",\n pages = "186--191",\n}\n' SCREAMING_SNAKE_CASE_ : str = '\\nChrF and ChrF++ are two MT evaluation metrics. They both use the F-score statistic for character n-gram matches,\nand ChrF++ adds word n-grams as well which correlates more strongly with direct assessment. We use the implementation\nthat is already present in sacrebleu.\n\nThe implementation here is slightly different from sacrebleu in terms of the required input format. The length of\nthe references and hypotheses lists need to be the same, so you may need to transpose your references compared to\nsacrebleu\'s required input format. See https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534\n\nSee the README.md file at https://github.com/mjpost/sacreBLEU#chrf--chrf for more information.\n' SCREAMING_SNAKE_CASE_ : List[str] = '\nProduces ChrF(++) scores for hypotheses given reference translations.\n\nArgs:\n predictions (list of str): The predicted sentences.\n references (list of list of str): The references. There should be one reference sub-list for each prediction sentence.\n char_order (int): Character n-gram order. Defaults to `6`.\n word_order (int): Word n-gram order. If equals to `2`, the metric is referred to as chrF++. Defaults to `0`.\n beta (int): Determine the importance of recall w.r.t precision. Defaults to `2`.\n lowercase (bool): if `True`, enables case-insensitivity. Defaults to `False`.\n whitespace (bool): If `True`, include whitespaces when extracting character n-grams.\n eps_smoothing (bool): If `True`, applies epsilon smoothing similar\n to reference chrF++.py, NLTK and Moses implementations. If `False`,\n it takes into account effective match order similar to sacreBLEU < 2.0.0. Defaults to `False`.\n\nReturns:\n \'score\' (float): The chrF (chrF++) score,\n \'char_order\' (int): The character n-gram order,\n \'word_order\' (int): The word n-gram order. If equals to 2, the metric is referred to as chrF++,\n \'beta\' (int): Determine the importance of recall w.r.t precision\n\nExamples:\n Example 1--a simple example of calculating chrF:\n >>> prediction = ["The relationship between cats and dogs is not exactly friendly.", "a good bookshop is just a genteel black hole that knows how to read."]\n >>> reference = [["The relationship between dogs and cats is not exactly friendly."], ["A good bookshop is just a genteel Black Hole that knows how to read."]]\n >>> chrf = datasets.load_metric("chrf")\n >>> results = chrf.compute(predictions=prediction, references=reference)\n >>> print(results)\n {\'score\': 84.64214891738334, \'char_order\': 6, \'word_order\': 0, \'beta\': 2}\n\n Example 2--the same example, but with the argument word_order=2, to calculate chrF++ instead of chrF:\n >>> prediction = ["The relationship between cats and dogs is not exactly friendly.", "a good bookshop is just a genteel black hole that knows how to read."]\n >>> reference = [["The relationship between dogs and cats is not exactly friendly."], ["A good bookshop is just a genteel Black Hole that knows how to read."]]\n >>> chrf = datasets.load_metric("chrf")\n >>> results = chrf.compute(predictions=prediction,\n ... references=reference,\n ... word_order=2)\n >>> print(results)\n {\'score\': 82.87263732906315, \'char_order\': 6, \'word_order\': 2, \'beta\': 2}\n\n Example 3--the same chrF++ example as above, but with `lowercase=True` to normalize all case:\n >>> prediction = ["The relationship between cats and dogs is not exactly friendly.", "a good bookshop is just a genteel black hole that knows how to read."]\n >>> reference = [["The relationship between dogs and cats is not exactly friendly."], ["A good bookshop is just a genteel Black Hole that knows how to read."]]\n >>> chrf = datasets.load_metric("chrf")\n >>> results = chrf.compute(predictions=prediction,\n ... references=reference,\n ... word_order=2,\n ... lowercase=True)\n >>> print(results)\n {\'score\': 92.12853119829202, \'char_order\': 6, \'word_order\': 2, \'beta\': 2}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION ) class a ( datasets.Metric ): """simple docstring""" def UpperCamelCase ( self: List[str] ): """simple docstring""" if version.parse(scb.__version__ ) < version.parse("""1.4.12""" ): raise ImportWarning( """To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn't match this condition.\n""" """You can install it with `pip install \"sacrebleu>=1.4.12\"`.""" ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage="""https://github.com/mjpost/sacreBLEU#chrf--chrf""" , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""string""" , id="""sequence""" ), """references""": datasets.Sequence(datasets.Value("""string""" , id="""sequence""" ) , id="""references""" ), } ) , codebase_urls=["""https://github.com/mjpost/sacreBLEU#chrf--chrf"""] , reference_urls=[ """https://github.com/m-popovic/chrF""", ] , ) def UpperCamelCase ( self: List[Any] , UpperCamelCase: Union[str, Any] , UpperCamelCase: Optional[Any] , UpperCamelCase: int = CHRF.CHAR_ORDER , UpperCamelCase: int = CHRF.WORD_ORDER , UpperCamelCase: int = CHRF.BETA , UpperCamelCase: bool = False , UpperCamelCase: bool = False , UpperCamelCase: bool = False , ): """simple docstring""" A__ = len(references[0] ) if any(len(UpperCamelCase ) != references_per_prediction for refs in references ): raise ValueError("""Sacrebleu requires the same number of references for each prediction""" ) A__ = [[refs[i] for refs in references] for i in range(UpperCamelCase )] A__ = CHRF(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) A__ = sb_chrf.corpus_score(UpperCamelCase , UpperCamelCase ) return { "score": output.score, "char_order": output.char_order, "word_order": output.word_order, "beta": output.beta, }
335
0
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 ( a_ ,a_ ) -> Tuple: __UpperCamelCase : Optional[Any] =XCLIPTextConfig() # derive patch size from model name __UpperCamelCase : int =model_name.find('patch' ) __UpperCamelCase : Union[str, Any] =int(model_name[start_idx + len('patch' ) : start_idx + len('patch' ) + 2] ) __UpperCamelCase : int =XCLIPVisionConfig(patch_size=a_ ,num_frames=a_ ) if "large" in model_name: __UpperCamelCase : Optional[Any] =768 __UpperCamelCase : List[Any] =3_072 __UpperCamelCase : Any =12 __UpperCamelCase : int =1_024 __UpperCamelCase : List[str] =4_096 __UpperCamelCase : List[Any] =16 __UpperCamelCase : Optional[Any] =24 __UpperCamelCase : Dict =768 __UpperCamelCase : Dict =3_072 if model_name == "xclip-large-patch14-16-frames": __UpperCamelCase : Union[str, Any] =336 __UpperCamelCase : Dict =XCLIPConfig.from_text_vision_configs(a_ ,a_ ) if "large" in model_name: __UpperCamelCase : str =768 return config def A ( a_ ) -> List[str]: # text encoder if name == "token_embedding.weight": __UpperCamelCase : Optional[Any] =name.replace('token_embedding.weight' ,'text_model.embeddings.token_embedding.weight' ) if name == "positional_embedding": __UpperCamelCase : Union[str, Any] =name.replace('positional_embedding' ,'text_model.embeddings.position_embedding.weight' ) if "ln_1" in name: __UpperCamelCase : Optional[Any] =name.replace('ln_1' ,'layer_norm1' ) if "ln_2" in name: __UpperCamelCase : Tuple =name.replace('ln_2' ,'layer_norm2' ) if "c_fc" in name: __UpperCamelCase : Optional[Any] =name.replace('c_fc' ,'fc1' ) if "c_proj" in name: __UpperCamelCase : List[str] =name.replace('c_proj' ,'fc2' ) if name.startswith('transformer.resblocks' ): __UpperCamelCase : Any =name.replace('transformer.resblocks' ,'text_model.encoder.layers' ) if "attn.out_proj" in name and "message" not in name: __UpperCamelCase : Optional[int] =name.replace('attn.out_proj' ,'self_attn.out_proj' ) if "ln_final" in name: __UpperCamelCase : Tuple =name.replace('ln_final' ,'text_model.final_layer_norm' ) # visual encoder if name == "visual.class_embedding": __UpperCamelCase : str =name.replace('visual.class_embedding' ,'vision_model.embeddings.class_embedding' ) if name == "visual.positional_embedding": __UpperCamelCase : List[Any] =name.replace('visual.positional_embedding' ,'vision_model.embeddings.position_embedding.weight' ) if name.startswith('visual.transformer.resblocks' ): __UpperCamelCase : Dict =name.replace('visual.transformer.resblocks' ,'vision_model.encoder.layers' ) if "visual.conv1" in name: __UpperCamelCase : str =name.replace('visual.conv1' ,'vision_model.embeddings.patch_embedding' ) if "visual.ln_pre" in name: __UpperCamelCase : Tuple =name.replace('visual.ln_pre' ,'vision_model.pre_layernorm' ) if "visual.ln_post" in name: __UpperCamelCase : Any =name.replace('visual.ln_post' ,'vision_model.post_layernorm' ) if "visual.proj" in name: __UpperCamelCase : int =name.replace('visual.proj' ,'visual_projection.weight' ) if "text_projection" in name: __UpperCamelCase : Optional[Any] =name.replace('text_projection' ,'text_projection.weight' ) # things on top if "prompts_visual_proj" in name: __UpperCamelCase : List[str] =name.replace('prompts_visual_proj' ,'prompts_visual_projection' ) if "prompts_visual_ln" in name: __UpperCamelCase : Dict =name.replace('prompts_visual_ln' ,'prompts_visual_layernorm' ) # mit if name == "mit.positional_embedding": __UpperCamelCase : Dict =name.replace('positional' ,'position' ) if name.startswith('mit.resblocks' ): __UpperCamelCase : Union[str, Any] =name.replace('mit.resblocks' ,'mit.encoder.layers' ) # prompts generator if name.startswith('prompts_generator.norm' ): __UpperCamelCase : Dict =name.replace('prompts_generator.norm' ,'prompts_generator.layernorm' ) return name def A ( a_ ,a_ ) -> Dict: for key in orig_state_dict.copy().keys(): __UpperCamelCase : Optional[int] =orig_state_dict.pop(a_ ) if "attn.in_proj" in key: __UpperCamelCase : List[Any] =key.split('.' ) if key.startswith('visual' ): __UpperCamelCase : List[str] =key_split[3] __UpperCamelCase : Dict =config.vision_config.hidden_size if "message_attn" in key: if "weight" in key: __UpperCamelCase : Any =val[ :dim, : ] __UpperCamelCase : List[Any] =val[ dim : dim * 2, : ] __UpperCamelCase : Dict =val[ -dim:, : ] else: __UpperCamelCase : Dict =val[ :dim ] __UpperCamelCase : List[str] =val[ dim : dim * 2 ] __UpperCamelCase : Union[str, Any] =val[ -dim: ] else: if "weight" in key: __UpperCamelCase : List[str] =val[ :dim, : ] __UpperCamelCase : int =val[ dim : dim * 2, : ] __UpperCamelCase : int =val[ -dim:, : ] else: __UpperCamelCase : int =val[:dim] __UpperCamelCase : str =val[ dim : dim * 2 ] __UpperCamelCase : Any =val[-dim:] elif key.startswith('mit' ): __UpperCamelCase : List[Any] =key_split[2] __UpperCamelCase : Any =config.vision_config.mit_hidden_size if "weight" in key: __UpperCamelCase : List[str] =val[:dim, :] __UpperCamelCase : Union[str, Any] =val[dim : dim * 2, :] __UpperCamelCase : Any =val[-dim:, :] else: __UpperCamelCase : Any =val[:dim] __UpperCamelCase : Optional[Any] =val[dim : dim * 2] __UpperCamelCase : Any =val[-dim:] else: __UpperCamelCase : Union[str, Any] =key_split[2] __UpperCamelCase : Dict =config.text_config.hidden_size if "weight" in key: __UpperCamelCase : Union[str, Any] =val[:dim, :] __UpperCamelCase : Union[str, Any] =val[ dim : dim * 2, : ] __UpperCamelCase : Tuple =val[-dim:, :] else: __UpperCamelCase : Optional[int] =val[:dim] __UpperCamelCase : List[str] =val[ dim : dim * 2 ] __UpperCamelCase : List[Any] =val[-dim:] else: __UpperCamelCase : Tuple =rename_key(a_ ) if new_key_name in ["visual_projection.weight", "text_projection.weight"]: __UpperCamelCase : str =val.T __UpperCamelCase : str =val return orig_state_dict def A ( a_ ) -> List[str]: if num_frames == 8: __UpperCamelCase : Dict ='eating_spaghetti_8_frames.npy' elif num_frames == 16: __UpperCamelCase : Tuple ='eating_spaghetti.npy' elif num_frames == 32: __UpperCamelCase : Any ='eating_spaghetti_32_frames.npy' __UpperCamelCase : List[str] =hf_hub_download( repo_id='hf-internal-testing/spaghetti-video' ,filename=a_ ,repo_type='dataset' ,) __UpperCamelCase : List[str] =np.load(a_ ) return list(a_ ) def A ( a_ ,a_=None ,a_=False ) -> List[Any]: __UpperCamelCase : Optional[int] ={ # 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', } __UpperCamelCase : str =model_to_url[model_name] __UpperCamelCase : Optional[Any] =8 if "16-frames" in model_name: __UpperCamelCase : Tuple =16 elif "shot" in model_name: __UpperCamelCase : Optional[int] =32 __UpperCamelCase : Any =get_xclip_config(a_ ,a_ ) __UpperCamelCase : Optional[Any] =XCLIPModel(a_ ) model.eval() if "drive" in checkpoint_url: __UpperCamelCase : List[Any] ='pytorch_model.bin' gdown.cached_download(a_ ,a_ ,quiet=a_ ) __UpperCamelCase : List[str] =torch.load(a_ ,map_location='cpu' )['model'] else: __UpperCamelCase : str =torch.hub.load_state_dict_from_url(a_ )['model'] __UpperCamelCase : Dict =convert_state_dict(a_ ,a_ ) __UpperCamelCase : List[Any] =XCLIPModel(a_ ) __UpperCamelCase , __UpperCamelCase : Any =model.load_state_dict(a_ ,strict=a_ ) assert missing_keys == ["text_model.embeddings.position_ids", "vision_model.embeddings.position_ids"] model.eval() __UpperCamelCase : Any =336 if model_name == 'xclip-large-patch14-16-frames' else 224 __UpperCamelCase : Dict =VideoMAEImageProcessor(size=a_ ) __UpperCamelCase : Tuple =CLIPTokenizer.from_pretrained('openai/clip-vit-base-patch32' ) __UpperCamelCase : Optional[Any] =CLIPTokenizerFast.from_pretrained('openai/clip-vit-base-patch32' ) __UpperCamelCase : Optional[int] =XCLIPProcessor(image_processor=a_ ,tokenizer=a_ ) __UpperCamelCase : Optional[Any] =prepare_video(a_ ) __UpperCamelCase : Optional[int] =processor( text=['playing sports', 'eating spaghetti', 'go shopping'] ,videos=a_ ,return_tensors='pt' ,padding=a_ ) print('Shape of pixel values:' ,inputs.pixel_values.shape ) with torch.no_grad(): __UpperCamelCase : Any =model(**a_ ) # Verify outputs __UpperCamelCase : str =outputs.logits_per_video __UpperCamelCase : Dict =logits_per_video.softmax(dim=1 ) print('Probs:' ,a_ ) # kinetics-400 if model_name == "xclip-base-patch32": __UpperCamelCase : Any =torch.tensor([[0.0_019, 0.9_951, 0.0_030]] ) elif model_name == "xclip-base-patch32-16-frames": __UpperCamelCase : Union[str, Any] =torch.tensor([[7.0999e-04, 9.9883e-01, 4.5580e-04]] ) elif model_name == "xclip-base-patch16": __UpperCamelCase : Union[str, Any] =torch.tensor([[0.0_083, 0.9_681, 0.0_236]] ) elif model_name == "xclip-base-patch16-16-frames": __UpperCamelCase : Dict =torch.tensor([[7.6937e-04, 9.9728e-01, 1.9473e-03]] ) elif model_name == "xclip-large-patch14": __UpperCamelCase : Union[str, Any] =torch.tensor([[0.0_062, 0.9_864, 0.0_075]] ) elif model_name == "xclip-large-patch14-16-frames": __UpperCamelCase : int =torch.tensor([[3.3877e-04, 9.9937e-01, 2.8888e-04]] ) # kinetics-600 elif model_name == "xclip-base-patch16-kinetics-600": __UpperCamelCase : List[Any] =torch.tensor([[0.0_555, 0.8_914, 0.0_531]] ) elif model_name == "xclip-base-patch16-kinetics-600-16-frames": __UpperCamelCase : Dict =torch.tensor([[3.8554e-04, 9.9929e-01, 3.2754e-04]] ) elif model_name == "xclip-large-patch14-kinetics-600": __UpperCamelCase : List[Any] =torch.tensor([[0.0_036, 0.9_920, 0.0_045]] ) # few shot elif model_name == "xclip-base-patch16-hmdb-2-shot": __UpperCamelCase : Optional[int] =torch.tensor([[7.1890e-06, 9.9994e-01, 5.6559e-05]] ) elif model_name == "xclip-base-patch16-hmdb-4-shot": __UpperCamelCase : Tuple =torch.tensor([[1.0320e-05, 9.9993e-01, 6.2435e-05]] ) elif model_name == "xclip-base-patch16-hmdb-8-shot": __UpperCamelCase : Union[str, Any] =torch.tensor([[4.1377e-06, 9.9990e-01, 9.8386e-05]] ) elif model_name == "xclip-base-patch16-hmdb-16-shot": __UpperCamelCase : Tuple =torch.tensor([[4.1347e-05, 9.9962e-01, 3.3411e-04]] ) elif model_name == "xclip-base-patch16-ucf-2-shot": __UpperCamelCase : Any =torch.tensor([[8.5857e-05, 9.9928e-01, 6.3291e-04]] ) elif model_name == "xclip-base-patch16-ucf-4-shot": __UpperCamelCase : List[str] =torch.tensor([[8.5857e-05, 9.9928e-01, 6.3291e-04]] ) elif model_name == "xclip-base-patch16-ucf-8-shot": __UpperCamelCase : Any =torch.tensor([[0.0_027, 0.9_904, 0.0_070]] ) elif model_name == "xclip-base-patch16-ucf-16-shot": __UpperCamelCase : Tuple =torch.tensor([[9.8219e-04, 9.9593e-01, 3.0863e-03]] ) # zero shot elif model_name == "xclip-base-patch16-zero-shot": __UpperCamelCase : List[str] =torch.tensor([[3.5082e-04, 9.9785e-01, 1.7966e-03]] ) else: raise ValueError(F'Model name {model_name} not supported' ) assert torch.allclose(a_ ,a_ ,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(a_ ) if push_to_hub: print('Pushing model, processor and slow tokenizer files to the hub...' ) model.push_to_hub(a_ ,organization='nielsr' ) processor.push_to_hub(a_ ,organization='nielsr' ) slow_tokenizer.push_to_hub(a_ ,organization='nielsr' ) if __name__ == "__main__": A_ :Optional[int] = 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.''' ) A_ :int = parser.parse_args() convert_xclip_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
364
import unittest import numpy as np import timeout_decorator # noqa from transformers import BlenderbotSmallConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor if is_flax_available(): import os # The slow tests are often failing with OOM error on GPU # This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed # but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html A_ :Tuple = '''platform''' import jax import jax.numpy as jnp from transformers.models.blenderbot_small.modeling_flax_blenderbot_small import ( FlaxBlenderbotSmallForConditionalGeneration, FlaxBlenderbotSmallModel, shift_tokens_right, ) def A ( a_ ,a_ ,a_=None ,a_=None ,a_=None ,a_=None ,a_=None ,a_=None ,) -> int: if attention_mask is None: __UpperCamelCase : Any =np.where(input_ids != config.pad_token_id ,1 ,0 ) if decoder_attention_mask is None: __UpperCamelCase : Optional[Any] =np.where(decoder_input_ids != config.pad_token_id ,1 ,0 ) if head_mask is None: __UpperCamelCase : str =np.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: __UpperCamelCase : Any =np.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: __UpperCamelCase : Optional[int] =np.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": attention_mask, } class __A : """simple docstring""" def __init__( self , lowerCamelCase__ , lowerCamelCase__=13 , lowerCamelCase__=7 , lowerCamelCase__=True , lowerCamelCase__=False , lowerCamelCase__=99 , lowerCamelCase__=16 , lowerCamelCase__=2 , lowerCamelCase__=4 , lowerCamelCase__=4 , lowerCamelCase__="gelu" , lowerCamelCase__=0.1 , lowerCamelCase__=0.1 , lowerCamelCase__=32 , lowerCamelCase__=2 , lowerCamelCase__=1 , lowerCamelCase__=0 , lowerCamelCase__=0.02 , ): """simple docstring""" __UpperCamelCase : Tuple =parent __UpperCamelCase : str =batch_size __UpperCamelCase : Optional[Any] =seq_length __UpperCamelCase : List[Any] =is_training __UpperCamelCase : int =use_labels __UpperCamelCase : int =vocab_size __UpperCamelCase : Any =hidden_size __UpperCamelCase : List[str] =num_hidden_layers __UpperCamelCase : Any =num_attention_heads __UpperCamelCase : int =intermediate_size __UpperCamelCase : List[Any] =hidden_act __UpperCamelCase : Optional[Any] =hidden_dropout_prob __UpperCamelCase : int =attention_probs_dropout_prob __UpperCamelCase : Tuple =max_position_embeddings __UpperCamelCase : List[Any] =eos_token_id __UpperCamelCase : Tuple =pad_token_id __UpperCamelCase : Any =bos_token_id __UpperCamelCase : Tuple =initializer_range def __lowercase ( self ): """simple docstring""" __UpperCamelCase : str =np.clip(ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) , 3 , self.vocab_size ) __UpperCamelCase : Any =np.concatenate((input_ids, 2 * np.ones((self.batch_size, 1) , dtype=np.intaa )) , -1 ) __UpperCamelCase : Any =shift_tokens_right(lowerCamelCase__ , 1 , 2 ) __UpperCamelCase : List[str] =BlenderbotSmallConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , initializer_range=self.initializer_range , use_cache=lowerCamelCase__ , ) __UpperCamelCase : Dict =prepare_blenderbot_inputs_dict(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) return config, inputs_dict def __lowercase ( self ): """simple docstring""" __UpperCamelCase , __UpperCamelCase : Optional[Any] =self.prepare_config_and_inputs() return config, inputs_dict def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase : Optional[Any] =20 __UpperCamelCase : Optional[int] =model_class_name(lowerCamelCase__ ) __UpperCamelCase : Tuple =model.encode(inputs_dict['input_ids'] ) __UpperCamelCase , __UpperCamelCase : Optional[Any] =( inputs_dict['decoder_input_ids'], inputs_dict['decoder_attention_mask'], ) __UpperCamelCase : Any =model.init_cache(decoder_input_ids.shape[0] , lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Tuple =jnp.ones((decoder_input_ids.shape[0], max_decoder_length) , dtype='i4' ) __UpperCamelCase : List[str] =jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) __UpperCamelCase : Any =model.decode( decoder_input_ids[:, :-1] , lowerCamelCase__ , decoder_attention_mask=lowerCamelCase__ , past_key_values=lowerCamelCase__ , decoder_position_ids=lowerCamelCase__ , ) __UpperCamelCase : List[str] =jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype='i4' ) __UpperCamelCase : Tuple =model.decode( decoder_input_ids[:, -1:] , lowerCamelCase__ , decoder_attention_mask=lowerCamelCase__ , past_key_values=outputs_cache.past_key_values , decoder_position_ids=lowerCamelCase__ , ) __UpperCamelCase : Tuple =model.decode(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Tuple =np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 , msg=f'Max diff is {diff}' ) def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase : Optional[Any] =20 __UpperCamelCase : int =model_class_name(lowerCamelCase__ ) __UpperCamelCase : Optional[int] =model.encode(inputs_dict['input_ids'] ) __UpperCamelCase , __UpperCamelCase : Any =( inputs_dict['decoder_input_ids'], inputs_dict['decoder_attention_mask'], ) __UpperCamelCase : int =jnp.concatenate( [ decoder_attention_mask, jnp.zeros((decoder_attention_mask.shape[0], max_decoder_length - decoder_attention_mask.shape[1]) ), ] , axis=-1 , ) __UpperCamelCase : List[str] =model.init_cache(decoder_input_ids.shape[0] , lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Any =jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) __UpperCamelCase : Any =model.decode( decoder_input_ids[:, :-1] , lowerCamelCase__ , decoder_attention_mask=lowerCamelCase__ , past_key_values=lowerCamelCase__ , decoder_position_ids=lowerCamelCase__ , ) __UpperCamelCase : Optional[Any] =jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype='i4' ) __UpperCamelCase : Tuple =model.decode( decoder_input_ids[:, -1:] , lowerCamelCase__ , past_key_values=outputs_cache.past_key_values , decoder_attention_mask=lowerCamelCase__ , decoder_position_ids=lowerCamelCase__ , ) __UpperCamelCase : Optional[int] =model.decode(lowerCamelCase__ , lowerCamelCase__ , decoder_attention_mask=lowerCamelCase__ ) __UpperCamelCase : Tuple =np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 , msg=f'Max diff is {diff}' ) @require_flax class __A ( unittest.TestCase ): """simple docstring""" UpperCamelCase__ : Any =9_9 def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Any =np.array( [ [71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 82, 2], [5, 97, 17, 39, 94, 40, 2], [76, 83, 94, 25, 70, 78, 2], [87, 59, 41, 35, 48, 66, 2], [55, 13, 16, 58, 5, 2, 1], # note padding [64, 27, 31, 51, 12, 75, 2], [52, 64, 86, 17, 83, 39, 2], [48, 61, 9, 24, 71, 82, 2], [26, 1, 60, 48, 22, 13, 2], [21, 5, 62, 28, 14, 76, 2], [45, 98, 37, 86, 59, 48, 2], [70, 70, 50, 9, 28, 0, 2], ] , dtype=np.intaa , ) __UpperCamelCase : int =input_ids.shape[0] __UpperCamelCase : List[str] =BlenderbotSmallConfig( vocab_size=self.vocab_size , d_model=24 , encoder_layers=2 , decoder_layers=2 , encoder_attention_heads=2 , decoder_attention_heads=2 , encoder_ffn_dim=32 , decoder_ffn_dim=32 , max_position_embeddings=48 , eos_token_id=2 , pad_token_id=1 , bos_token_id=0 , ) return config, input_ids, batch_size def __lowercase ( self ): """simple docstring""" __UpperCamelCase , __UpperCamelCase , __UpperCamelCase : int =self._get_config_and_data() __UpperCamelCase : Optional[Any] =FlaxBlenderbotSmallForConditionalGeneration(lowerCamelCase__ ) __UpperCamelCase : int =lm_model(input_ids=lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =(batch_size, input_ids.shape[1], config.vocab_size) self.assertEqual(outputs['logits'].shape , lowerCamelCase__ ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Union[str, Any] =BlenderbotSmallConfig( vocab_size=self.vocab_size , d_model=14 , encoder_layers=2 , decoder_layers=2 , encoder_attention_heads=2 , decoder_attention_heads=2 , encoder_ffn_dim=8 , decoder_ffn_dim=8 , max_position_embeddings=48 , ) __UpperCamelCase : List[str] =FlaxBlenderbotSmallForConditionalGeneration(lowerCamelCase__ ) __UpperCamelCase : Any =np.array([[71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 2, 1]] , dtype=np.intaa ) __UpperCamelCase : int =np.array([[82, 71, 82, 18, 2], [58, 68, 2, 1, 1]] , dtype=np.intaa ) __UpperCamelCase : Any =lm_model(input_ids=lowerCamelCase__ , decoder_input_ids=lowerCamelCase__ ) __UpperCamelCase : Optional[Any] =(*summary.shape, config.vocab_size) self.assertEqual(outputs['logits'].shape , lowerCamelCase__ ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[int] =np.array([[71, 82, 18, 33, 2, 1, 1], [68, 34, 26, 58, 30, 82, 2]] , dtype=np.intaa ) __UpperCamelCase : Tuple =shift_tokens_right(lowerCamelCase__ , 1 , 2 ) __UpperCamelCase : Optional[int] =np.equal(lowerCamelCase__ , 1 ).astype(np.floataa ).sum() __UpperCamelCase : Optional[Any] =np.equal(lowerCamelCase__ , 1 ).astype(np.floataa ).sum() self.assertEqual(shifted.shape , input_ids.shape ) self.assertEqual(lowerCamelCase__ , n_pad_before - 1 ) self.assertTrue(np.equal(shifted[:, 0] , 2 ).all() ) @require_flax class __A ( a , unittest.TestCase , a ): """simple docstring""" UpperCamelCase__ : Any =True UpperCamelCase__ : List[Any] =( ( FlaxBlenderbotSmallModel, FlaxBlenderbotSmallForConditionalGeneration, ) if is_flax_available() else () ) UpperCamelCase__ : Dict =(FlaxBlenderbotSmallForConditionalGeneration,) if is_flax_available() else () def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[int] =FlaxBlenderbotSmallModelTester(self ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase , __UpperCamelCase : Dict =self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase , __UpperCamelCase : Any =self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward_with_attn_mask(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase , __UpperCamelCase : Optional[int] =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): __UpperCamelCase : Tuple =self._prepare_for_class(lowerCamelCase__ , lowerCamelCase__ ) __UpperCamelCase : Union[str, Any] =model_class(lowerCamelCase__ ) @jax.jit def encode_jitted(lowerCamelCase__ , lowerCamelCase__=None , **lowerCamelCase__ ): return model.encode(input_ids=lowerCamelCase__ , attention_mask=lowerCamelCase__ ) with self.subTest('JIT Enabled' ): __UpperCamelCase : Union[str, Any] =encode_jitted(**lowerCamelCase__ ).to_tuple() with self.subTest('JIT Disabled' ): with jax.disable_jit(): __UpperCamelCase : Any =encode_jitted(**lowerCamelCase__ ).to_tuple() self.assertEqual(len(lowerCamelCase__ ) , len(lowerCamelCase__ ) ) for jitted_output, output in zip(lowerCamelCase__ , lowerCamelCase__ ): self.assertEqual(jitted_output.shape , output.shape ) def __lowercase ( self ): """simple docstring""" __UpperCamelCase , __UpperCamelCase : Optional[int] =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): __UpperCamelCase : Tuple =model_class(lowerCamelCase__ ) __UpperCamelCase : Tuple =model.encode(inputs_dict['input_ids'] , inputs_dict['attention_mask'] ) __UpperCamelCase : Any ={ 'decoder_input_ids': inputs_dict['decoder_input_ids'], 'decoder_attention_mask': inputs_dict['decoder_attention_mask'], 'encoder_outputs': encoder_outputs, } @jax.jit def decode_jitted(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ): return model.decode( decoder_input_ids=lowerCamelCase__ , decoder_attention_mask=lowerCamelCase__ , encoder_outputs=lowerCamelCase__ , ) with self.subTest('JIT Enabled' ): __UpperCamelCase : Optional[Any] =decode_jitted(**lowerCamelCase__ ).to_tuple() with self.subTest('JIT Disabled' ): with jax.disable_jit(): __UpperCamelCase : int =decode_jitted(**lowerCamelCase__ ).to_tuple() self.assertEqual(len(lowerCamelCase__ ) , len(lowerCamelCase__ ) ) for jitted_output, output in zip(lowerCamelCase__ , lowerCamelCase__ ): self.assertEqual(jitted_output.shape , output.shape ) @slow def __lowercase ( self ): """simple docstring""" for model_class_name in self.all_model_classes: __UpperCamelCase : Optional[Any] =model_class_name.from_pretrained('facebook/blenderbot_small-90M' ) # FlaxBlenderbotForSequenceClassification expects eos token in input_ids __UpperCamelCase : Optional[Any] =np.ones((1, 1) ) * model.config.eos_token_id __UpperCamelCase : Union[str, Any] =model(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ )
245
0
def lowercase ( SCREAMING_SNAKE_CASE__ : Any ) -> Optional[Any]: _snake_case : Optional[Any] = len(SCREAMING_SNAKE_CASE__ ) _snake_case : Union[str, Any] = sum(SCREAMING_SNAKE_CASE__ ) _snake_case : Union[str, Any] = [[False for x in range(s + 1 )] for y in range(n + 1 )] for i in range(1 , n + 1 ): _snake_case : str = True for i in range(1 , s + 1 ): _snake_case : List[str] = False for i in range(1 , n + 1 ): for j in range(1 , s + 1 ): _snake_case : str = dp[i][j - 1] if arr[i - 1] <= j: _snake_case : Optional[int] = dp[i][j] or dp[i - 1][j - arr[i - 1]] for j in range(int(s / 2 ) , -1 , -1 ): if dp[n][j] is True: _snake_case : Optional[Any] = s - 2 * j break return diff
317
from operator import delitem, getitem, setitem import pytest from data_structures.hashing.hash_map import HashMap def lowercase ( SCREAMING_SNAKE_CASE__ : Optional[int] ) -> int: return getitem, k def lowercase ( SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Optional[int] ) -> str: return setitem, k, v def lowercase ( SCREAMING_SNAKE_CASE__ : Tuple ) -> Optional[Any]: return delitem, k def lowercase ( SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : str , *SCREAMING_SNAKE_CASE__ : int ) -> Optional[int]: try: return fun(SCREAMING_SNAKE_CASE__ , *SCREAMING_SNAKE_CASE__ ), None except Exception as e: return None, e a__ = ( _set("""key_a""", """val_a"""), _set("""key_b""", """val_b"""), ) a__ = [ _set("""key_a""", """val_a"""), _set("""key_a""", """val_b"""), ] a__ = [ _set("""key_a""", """val_a"""), _set("""key_b""", """val_b"""), _del("""key_a"""), _del("""key_b"""), _set("""key_a""", """val_a"""), _del("""key_a"""), ] a__ = [ _get("""key_a"""), _del("""key_a"""), _set("""key_a""", """val_a"""), _del("""key_a"""), _del("""key_a"""), _get("""key_a"""), ] a__ = [ *[_set(x, x) for x in range(5)], # guaranteed upsize ] a__ = [ *[_set(x, x) for x in range(5)], # guaranteed upsize *[_del(x) for x in range(5)], _set("""key_a""", """val_b"""), ] @pytest.mark.parametrize( """operations""" , ( pytest.param(_add_items , id="""add items""" ), pytest.param(_overwrite_items , id="""overwrite items""" ), pytest.param(_delete_items , id="""delete items""" ), pytest.param(_access_absent_items , id="""access absent items""" ), pytest.param(_add_with_resize_up , id="""add with resize up""" ), pytest.param(_add_with_resize_down , id="""add with resize down""" ), ) , ) def lowercase ( SCREAMING_SNAKE_CASE__ : str ) -> Tuple: _snake_case : List[Any] = HashMap(initial_block_size=4 ) _snake_case : int = {} for _, (fun, *args) in enumerate(SCREAMING_SNAKE_CASE__ ): _snake_case , _snake_case : Tuple = _run_operation(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , *SCREAMING_SNAKE_CASE__ ) _snake_case , _snake_case : int = _run_operation(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , *SCREAMING_SNAKE_CASE__ ) assert my_res == py_res assert str(SCREAMING_SNAKE_CASE__ ) == str(SCREAMING_SNAKE_CASE__ ) assert set(SCREAMING_SNAKE_CASE__ ) == set(SCREAMING_SNAKE_CASE__ ) assert len(SCREAMING_SNAKE_CASE__ ) == len(SCREAMING_SNAKE_CASE__ ) assert set(my.items() ) == set(py.items() ) def lowercase ( ) -> Optional[int]: def is_public(SCREAMING_SNAKE_CASE__ : str ) -> bool: return not name.startswith("""_""" ) _snake_case : Tuple = {name for name in dir({} ) if is_public(SCREAMING_SNAKE_CASE__ )} _snake_case : Optional[Any] = {name for name in dir(HashMap() ) if is_public(SCREAMING_SNAKE_CASE__ )} assert dict_public_names > hash_public_names
317
1
'''simple docstring''' import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""", datefmt="""%m/%d/%Y %H:%M:%S""", level=logging.INFO ) snake_case__ : Optional[int] = logging.getLogger(__name__) def _lowerCamelCase ( lowerCamelCase_ : List[Any] , lowerCamelCase_ : List[Any] ): """simple docstring""" UpperCAmelCase_ : List[str] = np.argmax(lowerCamelCase_ , axis=1 ) return np.sum(outputs == labels ) def _lowerCamelCase ( lowerCamelCase_ : Optional[Any] ): """simple docstring""" with open(lowerCamelCase_ , encoding='utf_8' ) as f: UpperCAmelCase_ : List[Any] = csv.reader(lowerCamelCase_ ) UpperCAmelCase_ : List[str] = [] next(lowerCamelCase_ ) # skip the first line for line in tqdm(lowerCamelCase_ ): output.append((' '.join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def _lowerCamelCase ( lowerCamelCase_ : Tuple , lowerCamelCase_ : List[str] , lowerCamelCase_ : Dict , lowerCamelCase_ : List[Any] , lowerCamelCase_ : Any , lowerCamelCase_ : List[Any] ): """simple docstring""" UpperCAmelCase_ : str = [] for dataset in encoded_datasets: UpperCAmelCase_ : Any = len(lowerCamelCase_ ) UpperCAmelCase_ : Tuple = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) UpperCAmelCase_ : Union[str, Any] = np.zeros((n_batch, 2) , dtype=np.intaa ) UpperCAmelCase_ : Any = np.full((n_batch, 2, input_len) , fill_value=-100 , dtype=np.intaa ) UpperCAmelCase_ : Any = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(lowerCamelCase_ ): UpperCAmelCase_ : Tuple = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] UpperCAmelCase_ : Dict = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] UpperCAmelCase_ : Dict = with_conta UpperCAmelCase_ : Tuple = with_conta UpperCAmelCase_ : Dict = len(lowerCamelCase_ ) - 1 UpperCAmelCase_ : List[str] = len(lowerCamelCase_ ) - 1 UpperCAmelCase_ : int = with_conta UpperCAmelCase_ : Optional[Any] = with_conta UpperCAmelCase_ : Optional[Any] = mc_label UpperCAmelCase_ : List[Any] = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(lowerCamelCase_ ) for t in all_inputs ) ) return tensor_datasets def _lowerCamelCase ( ): """simple docstring""" UpperCAmelCase_ : Any = argparse.ArgumentParser() parser.add_argument('--model_name' , type=lowerCamelCase_ , default='openai-gpt' , help='pretrained model name' ) parser.add_argument('--do_train' , action='store_true' , help='Whether to run training.' ) parser.add_argument('--do_eval' , action='store_true' , help='Whether to run eval on the dev set.' ) parser.add_argument( '--output_dir' , default=lowerCamelCase_ , type=lowerCamelCase_ , required=lowerCamelCase_ , help='The output directory where the model predictions and checkpoints will be written.' , ) parser.add_argument('--train_dataset' , type=lowerCamelCase_ , default='' ) parser.add_argument('--eval_dataset' , type=lowerCamelCase_ , default='' ) parser.add_argument('--seed' , type=lowerCamelCase_ , default=42 ) parser.add_argument('--num_train_epochs' , type=lowerCamelCase_ , default=3 ) parser.add_argument('--train_batch_size' , type=lowerCamelCase_ , default=8 ) parser.add_argument('--eval_batch_size' , type=lowerCamelCase_ , default=16 ) parser.add_argument('--adam_epsilon' , default=1e-8 , type=lowerCamelCase_ , help='Epsilon for Adam optimizer.' ) parser.add_argument('--max_grad_norm' , type=lowerCamelCase_ , default=1 ) parser.add_argument( '--max_steps' , default=-1 , type=lowerCamelCase_ , help=( 'If > 0: set total number of training steps to perform. Override num_train_epochs.' ) , ) parser.add_argument( '--gradient_accumulation_steps' , type=lowerCamelCase_ , default=1 , help='Number of updates steps to accumulate before performing a backward/update pass.' , ) parser.add_argument('--learning_rate' , type=lowerCamelCase_ , default=6.25e-5 ) parser.add_argument('--warmup_steps' , default=0 , type=lowerCamelCase_ , help='Linear warmup over warmup_steps.' ) parser.add_argument('--lr_schedule' , type=lowerCamelCase_ , default='warmup_linear' ) parser.add_argument('--weight_decay' , type=lowerCamelCase_ , default=0.01 ) parser.add_argument('--lm_coef' , type=lowerCamelCase_ , default=0.9 ) parser.add_argument('--n_valid' , type=lowerCamelCase_ , default=374 ) parser.add_argument('--server_ip' , type=lowerCamelCase_ , default='' , help='Can be used for distant debugging.' ) parser.add_argument('--server_port' , type=lowerCamelCase_ , default='' , help='Can be used for distant debugging.' ) UpperCAmelCase_ : Optional[Any] = parser.parse_args() print(lowerCamelCase_ ) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print('Waiting for debugger attach' ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=lowerCamelCase_ ) ptvsd.wait_for_attach() random.seed(args.seed ) np.random.seed(args.seed ) torch.manual_seed(args.seed ) torch.cuda.manual_seed_all(args.seed ) UpperCAmelCase_ : Optional[Any] = torch.device('cuda' if torch.cuda.is_available() else 'cpu' ) UpperCAmelCase_ : Dict = torch.cuda.device_count() logger.info('device: {}, n_gpu {}'.format(lowerCamelCase_ , lowerCamelCase_ ) ) if not args.do_train and not args.do_eval: raise ValueError('At least one of `do_train` or `do_eval` must be True.' ) if not os.path.exists(args.output_dir ): os.makedirs(args.output_dir ) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset UpperCAmelCase_ : Optional[Any] = ['_start_', '_delimiter_', '_classify_'] UpperCAmelCase_ : Any = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(lowerCamelCase_ ) UpperCAmelCase_ : Union[str, Any] = tokenizer.convert_tokens_to_ids(lowerCamelCase_ ) UpperCAmelCase_ : Dict = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(lowerCamelCase_ ) ) model.to(lowerCamelCase_ ) # Load and encode the datasets def tokenize_and_encode(lowerCamelCase_ : str ): if isinstance(lowerCamelCase_ , lowerCamelCase_ ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(lowerCamelCase_ ) ) elif isinstance(lowerCamelCase_ , lowerCamelCase_ ): return obj return [tokenize_and_encode(lowerCamelCase_ ) for o in obj] logger.info('Encoding dataset...' ) UpperCAmelCase_ : Tuple = load_rocstories_dataset(args.train_dataset ) UpperCAmelCase_ : Dict = load_rocstories_dataset(args.eval_dataset ) UpperCAmelCase_ : Tuple = (train_dataset, eval_dataset) UpperCAmelCase_ : Optional[int] = tokenize_and_encode(lowerCamelCase_ ) # Compute the max input length for the Transformer UpperCAmelCase_ : Optional[Any] = model.config.n_positions // 2 - 2 UpperCAmelCase_ : Tuple = max( len(story[:max_length] ) + max(len(conta[:max_length] ) , len(conta[:max_length] ) ) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset ) UpperCAmelCase_ : List[Any] = min(lowerCamelCase_ , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders UpperCAmelCase_ : List[str] = pre_process_datasets(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , *lowerCamelCase_ ) UpperCAmelCase_ : str = tensor_datasets[0], tensor_datasets[1] UpperCAmelCase_ : Any = TensorDataset(*lowerCamelCase_ ) UpperCAmelCase_ : Optional[Any] = RandomSampler(lowerCamelCase_ ) UpperCAmelCase_ : List[Any] = DataLoader(lowerCamelCase_ , sampler=lowerCamelCase_ , batch_size=args.train_batch_size ) UpperCAmelCase_ : str = TensorDataset(*lowerCamelCase_ ) UpperCAmelCase_ : str = SequentialSampler(lowerCamelCase_ ) UpperCAmelCase_ : Any = DataLoader(lowerCamelCase_ , sampler=lowerCamelCase_ , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: UpperCAmelCase_ : Tuple = args.max_steps UpperCAmelCase_ : Union[str, Any] = args.max_steps // (len(lowerCamelCase_ ) // args.gradient_accumulation_steps) + 1 else: UpperCAmelCase_ : Dict = len(lowerCamelCase_ ) // args.gradient_accumulation_steps * args.num_train_epochs UpperCAmelCase_ : Optional[Any] = list(model.named_parameters() ) UpperCAmelCase_ : int = ['bias', 'LayerNorm.bias', 'LayerNorm.weight'] UpperCAmelCase_ : Any = [ { 'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay )], 'weight_decay': args.weight_decay, }, {'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay )], 'weight_decay': 0.0}, ] UpperCAmelCase_ : Dict = AdamW(lowerCamelCase_ , lr=args.learning_rate , eps=args.adam_epsilon ) UpperCAmelCase_ : Optional[Any] = get_linear_schedule_with_warmup( lowerCamelCase_ , num_warmup_steps=args.warmup_steps , num_training_steps=lowerCamelCase_ ) if args.do_train: UpperCAmelCase_ : Optional[Any] = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc='Epoch' ): UpperCAmelCase_ : Dict = 0 UpperCAmelCase_ : List[Any] = 0 UpperCAmelCase_ : List[str] = tqdm(lowerCamelCase_ , desc='Training' ) for step, batch in enumerate(lowerCamelCase_ ): UpperCAmelCase_ : Dict = tuple(t.to(lowerCamelCase_ ) for t in batch ) UpperCAmelCase_ : Optional[int] = batch UpperCAmelCase_ : List[str] = model(lowerCamelCase_ , mc_token_ids=lowerCamelCase_ , lm_labels=lowerCamelCase_ , mc_labels=lowerCamelCase_ ) UpperCAmelCase_ : List[Any] = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() UpperCAmelCase_ : int = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 UpperCAmelCase_ : List[str] = 'Training loss: {:.2e} lr: {:.2e}'.format(lowerCamelCase_ , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer UpperCAmelCase_ : List[Any] = model.module if hasattr(lowerCamelCase_ , 'module' ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` UpperCAmelCase_ : str = os.path.join(args.output_dir , lowerCamelCase_ ) UpperCAmelCase_ : Optional[int] = os.path.join(args.output_dir , lowerCamelCase_ ) torch.save(model_to_save.state_dict() , lowerCamelCase_ ) model_to_save.config.to_json_file(lowerCamelCase_ ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned UpperCAmelCase_ : Optional[int] = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) UpperCAmelCase_ : List[Any] = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(lowerCamelCase_ ) if args.do_eval: model.eval() UpperCAmelCase_ : Any = 0, 0 UpperCAmelCase_ : Tuple = 0, 0 for batch in tqdm(lowerCamelCase_ , desc='Evaluating' ): UpperCAmelCase_ : int = tuple(t.to(lowerCamelCase_ ) for t in batch ) UpperCAmelCase_ : Dict = batch with torch.no_grad(): UpperCAmelCase_ : Any = model( lowerCamelCase_ , mc_token_ids=lowerCamelCase_ , lm_labels=lowerCamelCase_ , mc_labels=lowerCamelCase_ ) UpperCAmelCase_ : Optional[Any] = mc_logits.detach().cpu().numpy() UpperCAmelCase_ : int = mc_labels.to('cpu' ).numpy() UpperCAmelCase_ : int = accuracy(lowerCamelCase_ , lowerCamelCase_ ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 UpperCAmelCase_ : List[str] = eval_loss / nb_eval_steps UpperCAmelCase_ : List[str] = eval_accuracy / nb_eval_examples UpperCAmelCase_ : List[Any] = tr_loss / nb_tr_steps if args.do_train else None UpperCAmelCase_ : Optional[int] = {'eval_loss': eval_loss, 'eval_accuracy': eval_accuracy, 'train_loss': train_loss} UpperCAmelCase_ : Optional[int] = os.path.join(args.output_dir , 'eval_results.txt' ) with open(lowerCamelCase_ , 'w' ) as writer: logger.info('***** Eval results *****' ) for key in sorted(result.keys() ): logger.info(' %s = %s' , lowerCamelCase_ , str(result[key] ) ) writer.write('%s = %s\n' % (key, str(result[key] )) ) if __name__ == "__main__": main()
356
'''simple docstring''' import math from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import SchedulerMixin, SchedulerOutput class __SCREAMING_SNAKE_CASE ( lowerCamelCase_ , lowerCamelCase_ ): '''simple docstring''' lowerCamelCase_ :Union[str, Any] = 1 @register_to_config def __init__( self , snake_case_ = 1_0_0_0 , snake_case_ = None ): '''simple docstring''' self.set_timesteps(snake_case_ ) # standard deviation of the initial noise distribution UpperCAmelCase_ : Union[str, Any] = 1.0 # For now we only support F-PNDM, i.e. the runge-kutta method # For more information on the algorithm please take a look at the paper: https://arxiv.org/pdf/2202.09778.pdf # mainly at formula (9), (12), (13) and the Algorithm 2. UpperCAmelCase_ : int = 4 # running values UpperCAmelCase_ : str = [] def _UpperCamelCase ( self , snake_case_ , snake_case_ = None ): '''simple docstring''' UpperCAmelCase_ : Union[str, Any] = num_inference_steps UpperCAmelCase_ : int = torch.linspace(1 , 0 , num_inference_steps + 1 )[:-1] UpperCAmelCase_ : Tuple = torch.cat([steps, torch.tensor([0.0] )] ) if self.config.trained_betas is not None: UpperCAmelCase_ : Optional[int] = torch.tensor(self.config.trained_betas , dtype=torch.floataa ) else: UpperCAmelCase_ : Tuple = torch.sin(steps * math.pi / 2 ) ** 2 UpperCAmelCase_ : Dict = (1.0 - self.betas**2) ** 0.5 UpperCAmelCase_ : str = (torch.atana(self.betas , self.alphas ) / math.pi * 2)[:-1] UpperCAmelCase_ : str = timesteps.to(snake_case_ ) UpperCAmelCase_ : Any = [] def _UpperCamelCase ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ = True , ): '''simple docstring''' if self.num_inference_steps is None: raise ValueError( 'Number of inference steps is \'None\', you need to run \'set_timesteps\' after creating the scheduler' ) UpperCAmelCase_ : Any = (self.timesteps == timestep).nonzero().item() UpperCAmelCase_ : Optional[Any] = timestep_index + 1 UpperCAmelCase_ : Dict = sample * self.betas[timestep_index] + model_output * self.alphas[timestep_index] self.ets.append(snake_case_ ) if len(self.ets ) == 1: UpperCAmelCase_ : Tuple = self.ets[-1] elif len(self.ets ) == 2: UpperCAmelCase_ : Any = (3 * self.ets[-1] - self.ets[-2]) / 2 elif len(self.ets ) == 3: UpperCAmelCase_ : List[str] = (2_3 * self.ets[-1] - 1_6 * self.ets[-2] + 5 * self.ets[-3]) / 1_2 else: UpperCAmelCase_ : Union[str, Any] = (1 / 2_4) * (5_5 * self.ets[-1] - 5_9 * self.ets[-2] + 3_7 * self.ets[-3] - 9 * self.ets[-4]) UpperCAmelCase_ : Union[str, Any] = self._get_prev_sample(snake_case_ , snake_case_ , snake_case_ , snake_case_ ) if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=snake_case_ ) def _UpperCamelCase ( self , snake_case_ , *snake_case_ , **snake_case_ ): '''simple docstring''' return sample def _UpperCamelCase ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ ): '''simple docstring''' UpperCAmelCase_ : int = self.alphas[timestep_index] UpperCAmelCase_ : Union[str, Any] = self.betas[timestep_index] UpperCAmelCase_ : Any = self.alphas[prev_timestep_index] UpperCAmelCase_ : Dict = self.betas[prev_timestep_index] UpperCAmelCase_ : List[Any] = (sample - sigma * ets) / max(snake_case_ , 1E-8 ) UpperCAmelCase_ : Tuple = next_alpha * pred + ets * next_sigma return prev_sample def __len__( self ): '''simple docstring''' return self.config.num_train_timesteps
274
0
import argparse import json import os import torch from torch import nn from transformers import NllbMoeConfig, NllbMoeModel from transformers.modeling_utils import dtype_byte_size from transformers.utils import WEIGHTS_INDEX_NAME, WEIGHTS_NAME def A_ ( _lowerCAmelCase ) -> List[Any]: UpperCamelCase : str = [ "encoder.version", "decoder.version", "model.encoder.version", "model.decoder.version", "decoder.output_projection.weight", "_float_tensor", "encoder.embed_positions._float_tensor", "decoder.embed_positions._float_tensor", ] for k in ignore_keys: state_dict.pop(_lowerCAmelCase , _lowerCAmelCase ) def A_ ( _lowerCAmelCase ) -> Dict: UpperCamelCase , UpperCamelCase : str = emb.weight.shape UpperCamelCase : int = nn.Linear(_lowerCAmelCase , _lowerCAmelCase , bias=_lowerCAmelCase ) UpperCamelCase : str = emb.weight.data return lin_layer def A_ ( _lowerCAmelCase , _lowerCAmelCase=None ) -> Dict: UpperCamelCase : Optional[int] = {} for old_key in state_dict.keys(): UpperCamelCase : Optional[Any] = old_key if "moe_layer.experts." in key: if expert_idx is not None: UpperCamelCase : Any = key.replace("moe_layer.experts.0" , F"""ffn.experts.expert_{expert_idx}""" ) else: UpperCamelCase : List[Any] = key.replace("moe_layer.experts." , "ffn.experts.expert_" ) if "gate" in key: UpperCamelCase : List[Any] = key.replace(".moe_layer.gate.wg" , ".ffn.router.classifier" ) if "fc2" and "experts" not in key: UpperCamelCase : Optional[Any] = key.replace(".fc2." , ".ffn.fc2." ) if "fc1" and "experts" not in key: UpperCamelCase : Dict = key.replace(".fc1." , ".ffn.fc1." ) if ".encoder_attn." in key: UpperCamelCase : List[str] = key.replace(".encoder_attn." , ".cross_attention." ) if "encoder_attn_layer_norm" in key: UpperCamelCase : Union[str, Any] = key.replace("encoder_attn_layer_norm" , "cross_attention_layer_norm" ) if "final_layer_norm" in key: UpperCamelCase : Dict = key.replace("final_layer_norm" , "ff_layer_norm" ) UpperCamelCase : Optional[int] = state_dict[old_key] return new_dict def A_ ( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase = WEIGHTS_NAME ) -> Optional[int]: UpperCamelCase : Optional[Any] = [] UpperCamelCase : Optional[int] = 0 os.makedirs(_lowerCAmelCase , exist_ok=_lowerCAmelCase ) for expert in range(_lowerCAmelCase ): UpperCamelCase : Tuple = switch_checkpoint_path + F"""-rank-{expert}.pt""" if os.path.isfile(_lowerCAmelCase ): UpperCamelCase : str = torch.load(_lowerCAmelCase )["model"] remove_ignore_keys_(_lowerCAmelCase ) UpperCamelCase : List[Any] = rename_fairseq_keys(_lowerCAmelCase , _lowerCAmelCase ) UpperCamelCase : Optional[int] = os.path.join( _lowerCAmelCase , weights_name.replace(".bin" , F"""-{len(_lowerCAmelCase )+1:05d}-of-???.bin""" ) ) torch.save(_lowerCAmelCase , _lowerCAmelCase ) sharded_state_dicts.append(expert_state.keys() ) total_size += sum([value.numel() for key, value in expert_state.items()] ) * dtype_byte_size( expert_state[list(_lowerCAmelCase )[0]].dtype ) # Add the last block UpperCamelCase : int = os.path.join(_lowerCAmelCase , weights_name.replace(".bin" , F"""-{len(_lowerCAmelCase )+1:05d}-of-???.bin""" ) ) UpperCamelCase : Optional[int] = torch.load(switch_checkpoint_path + "-shared.pt" )["model"] remove_ignore_keys_(_lowerCAmelCase ) UpperCamelCase : List[Any] = rename_fairseq_keys(_lowerCAmelCase , _lowerCAmelCase ) UpperCamelCase : List[str] = shared_weights["decoder.embed_tokens.weight"] sharded_state_dicts.append(shared_weights.keys() ) # If we only have the shared weights (dummy model/experts saved on the same file) if len(_lowerCAmelCase ) == 1: UpperCamelCase : Tuple = os.path.join(_lowerCAmelCase , _lowerCAmelCase ) torch.save(_lowerCAmelCase , _lowerCAmelCase ) return {weights_name: sharded_state_dicts[0]}, None else: torch.save(_lowerCAmelCase , _lowerCAmelCase ) # Otherwise, let's build the index UpperCamelCase : List[str] = {} for idx, shard in enumerate(_lowerCAmelCase ): UpperCamelCase : Optional[int] = weights_name.replace(".bin" , F"""-{idx+1:05d}-of-{len(_lowerCAmelCase ):05d}.bin""" ) UpperCamelCase : Union[str, Any] = os.path.join(_lowerCAmelCase , weights_name.replace(".bin" , F"""-{idx+1:05d}-of-???.bin""" ) ) os.rename(_lowerCAmelCase , os.path.join(_lowerCAmelCase , _lowerCAmelCase ) ) for key in shard: UpperCamelCase : Optional[Any] = shard_file # Add the metadata UpperCamelCase : Tuple = {"total_size": total_size} UpperCamelCase : str = {"metadata": metadata, "weight_map": weight_map} with open(os.path.join(_lowerCAmelCase , _lowerCAmelCase ) , "w" , encoding="utf-8" ) as f: UpperCamelCase : str = json.dumps(_lowerCAmelCase , indent=2 , sort_keys=_lowerCAmelCase ) + "\n" f.write(_lowerCAmelCase ) return metadata, index if __name__ == "__main__": __lowerCamelCase : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--nllb_moe_checkpoint_path""", default="""/home/arthur_huggingface_co/fairseq/weights/checkpoints/model_moe_54b/checkpoint_2_300000""", type=str, required=False, help="""Path to a directory containing a folder per layer. Follows the original Google format.""", ) parser.add_argument("""--dtype""", default="""float32""", type=str, required=False, help="""dtype of the saved model""") parser.add_argument( """--pytorch_dump_folder_path""", default="""/home/arthur_huggingface_co/fairseq/weights/checkpoints/hf-converted-moe-54b""", type=str, required=False, help="""Path to the output pytorch model.""", ) __lowerCamelCase : str = parser.parse_args() __lowerCamelCase , __lowerCamelCase : int = shard_on_the_fly( args.nllb_moe_checkpoint_path, args.pytorch_dump_folder_path, 128, args.dtype, ) __lowerCamelCase : str = NllbMoeConfig.from_pretrained( """facebook/nllb-200-3.3B""", encoder_sparse_step=4, decoder_sparse_step=4, num_experts=128 ) config.save_pretrained(args.pytorch_dump_folder_path) __lowerCamelCase : Dict = NllbMoeModel.from_pretrained(args.pytorch_dump_folder_path) print("""Done""") model.save_pretrained(args.pytorch_dump_folder_path)
52
import logging import math import os from dataclasses import dataclass, field from glob import glob from typing import Optional from torch.utils.data import ConcatDataset import transformers from transformers import ( CONFIG_MAPPING, MODEL_WITH_LM_HEAD_MAPPING, AutoConfig, AutoModelWithLMHead, AutoTokenizer, DataCollatorForLanguageModeling, DataCollatorForPermutationLanguageModeling, DataCollatorForWholeWordMask, HfArgumentParser, LineByLineTextDataset, LineByLineWithRefDataset, PreTrainedTokenizer, TextDataset, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process a_ : int = logging.getLogger(__name__) a_ : List[str] = list(MODEL_WITH_LM_HEAD_MAPPING.keys()) a_ : int = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class _snake_case : _lowercase : Optional[str] = field( default=A__ , metadata={ '''help''': ( '''The model checkpoint for weights initialization. Leave None if you want to train a model from''' ''' scratch.''' ) } , ) _lowercase : Optional[str] = field( default=A__ , metadata={'''help''': '''If training from scratch, pass a model type from the list: ''' + ''', '''.join(A__ )} , ) _lowercase : Optional[str] = field( default=A__ , metadata={'''help''': '''Pretrained config name or path if not the same as model_name'''} ) _lowercase : Optional[str] = field( default=A__ , metadata={'''help''': '''Pretrained tokenizer name or path if not the same as model_name'''} ) _lowercase : Optional[str] = field( default=A__ , metadata={'''help''': '''Where do you want to store the pretrained models downloaded from huggingface.co'''} , ) @dataclass class _snake_case : _lowercase : Optional[str] = field( default=A__ , metadata={'''help''': '''The input training data file (a text file).'''} ) _lowercase : Optional[str] = field( default=A__ , metadata={ '''help''': ( '''The input training data files (multiple files in glob format). ''' '''Very often splitting large files to smaller files can prevent tokenizer going out of memory''' ) } , ) _lowercase : Optional[str] = field( default=A__ , metadata={'''help''': '''An optional input evaluation data file to evaluate the perplexity on (a text file).'''} , ) _lowercase : Optional[str] = field( default=A__ , metadata={'''help''': '''An optional input train ref data file for whole word mask in Chinese.'''} , ) _lowercase : Optional[str] = field( default=A__ , metadata={'''help''': '''An optional input eval ref data file for whole word mask in Chinese.'''} , ) _lowercase : bool = field( default=A__ , metadata={'''help''': '''Whether distinct lines of text in the dataset are to be handled as distinct sequences.'''} , ) _lowercase : bool = field( default=A__ , metadata={'''help''': '''Train with masked-language modeling loss instead of language modeling.'''} ) _lowercase : bool = field(default=A__ , metadata={'''help''': '''Whether ot not to use whole word mask.'''} ) _lowercase : float = field( default=0.15 , metadata={'''help''': '''Ratio of tokens to mask for masked language modeling loss'''} ) _lowercase : float = field( default=1 / 6 , metadata={ '''help''': ( '''Ratio of length of a span of masked tokens to surrounding context length for permutation language''' ''' modeling.''' ) } , ) _lowercase : int = field( default=5 , metadata={'''help''': '''Maximum length of a span of masked tokens for permutation language modeling.'''} ) _lowercase : int = field( default=-1 , metadata={ '''help''': ( '''Optional input sequence length after tokenization.''' '''The training dataset will be truncated in block of this size for training.''' '''Default to the model max input length for single sentence inputs (take into account special tokens).''' ) } , ) _lowercase : bool = field( default=A__ , metadata={'''help''': '''Overwrite the cached training and evaluation sets'''} ) def lowerCamelCase__ (_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = False , _UpperCAmelCase = None , ): def _dataset(_UpperCAmelCase , _UpperCAmelCase=None): if args.line_by_line: if ref_path is not None: if not args.whole_word_mask or not args.mlm: raise ValueError('You need to set world whole masking and mlm to True for Chinese Whole Word Mask') return LineByLineWithRefDataset( tokenizer=_UpperCAmelCase , file_path=_UpperCAmelCase , block_size=args.block_size , ref_path=_UpperCAmelCase , ) return LineByLineTextDataset(tokenizer=_UpperCAmelCase , file_path=_UpperCAmelCase , block_size=args.block_size) else: return TextDataset( tokenizer=_UpperCAmelCase , file_path=_UpperCAmelCase , block_size=args.block_size , overwrite_cache=args.overwrite_cache , cache_dir=_UpperCAmelCase , ) if evaluate: return _dataset(args.eval_data_file , args.eval_ref_file) elif args.train_data_files: return ConcatDataset([_dataset(_UpperCAmelCase) for f in glob(args.train_data_files)]) else: return _dataset(args.train_data_file , args.train_ref_file) def lowerCamelCase__ (): # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. SCREAMING_SNAKE_CASE = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = parser.parse_args_into_dataclasses() if data_args.eval_data_file is None and training_args.do_eval: raise ValueError( 'Cannot do evaluation without an evaluation data file. Either supply a file to --eval_data_file ' 'or remove the --do_eval argument.') if ( os.path.exists(training_args.output_dir) and os.listdir(training_args.output_dir) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( F'''Output directory ({training_args.output_dir}) already exists and is not empty. Use''' ' --overwrite_output_dir to overcome.') # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( 'Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s' , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.local_rank != -1) , training_args.fpaa , ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('Training/evaluation parameters %s' , _UpperCAmelCase) # Set seed set_seed(training_args.seed) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. if model_args.config_name: SCREAMING_SNAKE_CASE = AutoConfig.from_pretrained(model_args.config_name , cache_dir=model_args.cache_dir) elif model_args.model_name_or_path: SCREAMING_SNAKE_CASE = AutoConfig.from_pretrained(model_args.model_name_or_path , cache_dir=model_args.cache_dir) else: SCREAMING_SNAKE_CASE = CONFIG_MAPPING[model_args.model_type]() logger.warning('You are instantiating a new config instance from scratch.') if model_args.tokenizer_name: SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained(model_args.tokenizer_name , cache_dir=model_args.cache_dir) elif model_args.model_name_or_path: SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained(model_args.model_name_or_path , cache_dir=model_args.cache_dir) else: raise ValueError( 'You are instantiating a new tokenizer from scratch. This is not supported, but you can do it from another' ' script, save it,and load it from here, using --tokenizer_name') if model_args.model_name_or_path: SCREAMING_SNAKE_CASE = AutoModelWithLMHead.from_pretrained( model_args.model_name_or_path , from_tf=bool('.ckpt' in model_args.model_name_or_path) , config=_UpperCAmelCase , cache_dir=model_args.cache_dir , ) else: logger.info('Training new model from scratch') SCREAMING_SNAKE_CASE = AutoModelWithLMHead.from_config(_UpperCAmelCase) model.resize_token_embeddings(len(_UpperCAmelCase)) if config.model_type in ["bert", "roberta", "distilbert", "camembert"] and not data_args.mlm: raise ValueError( 'BERT and RoBERTa-like models do not have LM heads but masked LM heads. They must be run using the' '--mlm flag (masked language modeling).') if data_args.block_size <= 0: SCREAMING_SNAKE_CASE = tokenizer.max_len # Our input block size will be the max possible for the model else: SCREAMING_SNAKE_CASE = min(data_args.block_size , tokenizer.max_len) # Get datasets SCREAMING_SNAKE_CASE = ( get_dataset(_UpperCAmelCase , tokenizer=_UpperCAmelCase , cache_dir=model_args.cache_dir) if training_args.do_train else None ) SCREAMING_SNAKE_CASE = ( get_dataset(_UpperCAmelCase , tokenizer=_UpperCAmelCase , evaluate=_UpperCAmelCase , cache_dir=model_args.cache_dir) if training_args.do_eval else None ) if config.model_type == "xlnet": SCREAMING_SNAKE_CASE = DataCollatorForPermutationLanguageModeling( tokenizer=_UpperCAmelCase , plm_probability=data_args.plm_probability , max_span_length=data_args.max_span_length , ) else: if data_args.mlm and data_args.whole_word_mask: SCREAMING_SNAKE_CASE = DataCollatorForWholeWordMask( tokenizer=_UpperCAmelCase , mlm_probability=data_args.mlm_probability) else: SCREAMING_SNAKE_CASE = DataCollatorForLanguageModeling( tokenizer=_UpperCAmelCase , mlm=data_args.mlm , mlm_probability=data_args.mlm_probability) # Initialize our Trainer SCREAMING_SNAKE_CASE = Trainer( model=_UpperCAmelCase , args=_UpperCAmelCase , data_collator=_UpperCAmelCase , train_dataset=_UpperCAmelCase , eval_dataset=_UpperCAmelCase , prediction_loss_only=_UpperCAmelCase , ) # Training if training_args.do_train: SCREAMING_SNAKE_CASE = ( model_args.model_name_or_path if model_args.model_name_or_path is not None and os.path.isdir(model_args.model_name_or_path) else None ) trainer.train(model_path=_UpperCAmelCase) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir) # Evaluation SCREAMING_SNAKE_CASE = {} if training_args.do_eval: logger.info('*** Evaluate ***') SCREAMING_SNAKE_CASE = trainer.evaluate() SCREAMING_SNAKE_CASE = math.exp(eval_output['eval_loss']) SCREAMING_SNAKE_CASE = {'perplexity': perplexity} SCREAMING_SNAKE_CASE = os.path.join(training_args.output_dir , 'eval_results_lm.txt') if trainer.is_world_master(): with open(_UpperCAmelCase , 'w') as writer: logger.info('***** Eval results *****') for key in sorted(result.keys()): logger.info(' %s = %s' , _UpperCAmelCase , str(result[key])) writer.write('%s = %s\n' % (key, str(result[key]))) results.update(_UpperCAmelCase) return results def lowerCamelCase__ (_UpperCAmelCase): # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
137
0
from __future__ import annotations import bisect def __lowercase ( _a , _a , _a = 0 , _a = -1 ): if hi < 0: snake_case_ : Any = len(SCREAMING_SNAKE_CASE_ ) while lo < hi: snake_case_ : List[str] = lo + (hi - lo) // 2 if sorted_collection[mid] < item: snake_case_ : List[Any] = mid + 1 else: snake_case_ : Any = mid return lo def __lowercase ( _a , _a , _a = 0 , _a = -1 ): if hi < 0: snake_case_ : Dict = len(SCREAMING_SNAKE_CASE_ ) while lo < hi: snake_case_ : Dict = lo + (hi - lo) // 2 if sorted_collection[mid] <= item: snake_case_ : Any = mid + 1 else: snake_case_ : List[Any] = mid return lo def __lowercase ( _a , _a , _a = 0 , _a = -1 ): sorted_collection.insert(bisect_left(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) def __lowercase ( _a , _a , _a = 0 , _a = -1 ): sorted_collection.insert(bisect_right(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) def __lowercase ( _a , _a ): snake_case_ : Any = 0 snake_case_ : List[str] = len(SCREAMING_SNAKE_CASE_ ) - 1 while left <= right: snake_case_ : Optional[int] = left + (right - left) // 2 snake_case_ : Any = sorted_collection[midpoint] if current_item == item: return midpoint elif item < current_item: snake_case_ : Any = midpoint - 1 else: snake_case_ : str = midpoint + 1 return None def __lowercase ( _a , _a ): snake_case_ : List[str] = bisect.bisect_left(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if index != len(SCREAMING_SNAKE_CASE_ ) and sorted_collection[index] == item: return index return None def __lowercase ( _a , _a , _a , _a ): if right < left: return None snake_case_ : int = left + (right - left) // 2 if sorted_collection[midpoint] == item: return midpoint elif sorted_collection[midpoint] > item: return binary_search_by_recursion(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , midpoint - 1 ) else: return binary_search_by_recursion(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , midpoint + 1 , SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": lowercase__ : str = input('''Enter numbers separated by comma:\n''').strip() lowercase__ : List[str] = sorted(int(item) for item in user_input.split(''',''')) lowercase__ : Any = int(input('''Enter a single number to be found in the list:\n''')) lowercase__ : Optional[int] = binary_search(collection, target) if result is None: print(f'{target} was not found in {collection}.') else: print(f'{target} was found at position {result} in {collection}.')
366
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, is_vision_available, ) lowercase__ : List[Any] = { '''configuration_clip''': [ '''CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''CLIPConfig''', '''CLIPOnnxConfig''', '''CLIPTextConfig''', '''CLIPVisionConfig''', ], '''processing_clip''': ['''CLIPProcessor'''], '''tokenization_clip''': ['''CLIPTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ : Any = ['''CLIPTokenizerFast'''] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ : List[Any] = ['''CLIPFeatureExtractor'''] lowercase__ : Any = ['''CLIPImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ : Optional[Any] = [ '''CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''CLIPModel''', '''CLIPPreTrainedModel''', '''CLIPTextModel''', '''CLIPTextModelWithProjection''', '''CLIPVisionModel''', '''CLIPVisionModelWithProjection''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ : Tuple = [ '''TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFCLIPModel''', '''TFCLIPPreTrainedModel''', '''TFCLIPTextModel''', '''TFCLIPVisionModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ : Dict = [ '''FlaxCLIPModel''', '''FlaxCLIPPreTrainedModel''', '''FlaxCLIPTextModel''', '''FlaxCLIPTextPreTrainedModel''', '''FlaxCLIPVisionModel''', '''FlaxCLIPVisionPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_clip import ( CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, CLIPConfig, CLIPOnnxConfig, CLIPTextConfig, CLIPVisionConfig, ) from .processing_clip import CLIPProcessor from .tokenization_clip import CLIPTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_clip_fast import CLIPTokenizerFast try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_clip import CLIPFeatureExtractor from .image_processing_clip import CLIPImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_clip import ( CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, CLIPModel, CLIPPreTrainedModel, CLIPTextModel, CLIPTextModelWithProjection, CLIPVisionModel, CLIPVisionModelWithProjection, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_clip import ( TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, TFCLIPModel, TFCLIPPreTrainedModel, TFCLIPTextModel, TFCLIPVisionModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_clip import ( FlaxCLIPModel, FlaxCLIPPreTrainedModel, FlaxCLIPTextModel, FlaxCLIPTextPreTrainedModel, FlaxCLIPVisionModel, FlaxCLIPVisionPreTrainedModel, ) else: import sys lowercase__ : Optional[int] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
155
0
from __future__ import annotations def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Any: if nth_term == "": return [""] lowerCAmelCase__ : Optional[Any] = int(a__ ) lowerCAmelCase__ : Union[str, Any] = int(a__ ) lowerCAmelCase__ : list[str] = [] for temp in range(int(a__ ) ): series.append(F'''1 / {pow(temp + 1 , int(a__ ) )}''' if series else '1' ) return series if __name__ == "__main__": import doctest doctest.testmod() lowerCamelCase__ = int(input("""Enter the last number (nth term) of the P-Series""")) lowerCamelCase__ = int(input("""Enter the power for P-Series""")) print("""Formula of P-Series => 1+1/2^p+1/3^p ..... 1/n^p""") print(p_series(nth_term, power))
212
import gc import random import unittest import numpy as np import torch from transformers import ( CLIPImageProcessor, CLIPTextConfig, CLIPTextModel, CLIPTokenizer, CLIPVisionConfig, CLIPVisionModelWithProjection, ) from diffusers import AutoencoderKL, DDIMScheduler, DDPMScheduler, StableUnCLIPImgaImgPipeline, UNetaDConditionModel from diffusers.pipelines.pipeline_utils import DiffusionPipeline from diffusers.pipelines.stable_diffusion.stable_unclip_image_normalizer import StableUnCLIPImageNormalizer from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import ( enable_full_determinism, floats_tensor, load_image, load_numpy, require_torch_gpu, skip_mps, slow, torch_device, ) from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS from ..test_pipelines_common import ( PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin, assert_mean_pixel_difference, ) enable_full_determinism() class a_ ( a__ , a__ , a__ , unittest.TestCase ): """simple docstring""" __SCREAMING_SNAKE_CASE : int = StableUnCLIPImgaImgPipeline __SCREAMING_SNAKE_CASE : List[str] = TEXT_GUIDED_IMAGE_VARIATION_PARAMS __SCREAMING_SNAKE_CASE : List[Any] = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS __SCREAMING_SNAKE_CASE : Any = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess __SCREAMING_SNAKE_CASE : Tuple = frozenset([] ) def __lowerCAmelCase ( self ) ->Union[str, Any]: SCREAMING_SNAKE_CASE : Optional[Any] = 32 SCREAMING_SNAKE_CASE : Tuple = embedder_hidden_size # image encoding components SCREAMING_SNAKE_CASE : int = CLIPImageProcessor(crop_size=32 , size=32 ) torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Optional[int] = CLIPVisionModelWithProjection( CLIPVisionConfig( hidden_size=_lowerCamelCase , projection_dim=_lowerCamelCase , num_hidden_layers=5 , num_attention_heads=4 , image_size=32 , intermediate_size=37 , patch_size=1 , ) ) # regular denoising components torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : int = StableUnCLIPImageNormalizer(embedding_dim=_lowerCamelCase ) SCREAMING_SNAKE_CASE : Tuple = DDPMScheduler(beta_schedule='''squaredcos_cap_v2''' ) torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : List[str] = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Any = CLIPTextModel( CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=_lowerCamelCase , projection_dim=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) ) torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Any = UNetaDConditionModel( sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''CrossAttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''CrossAttnUpBlock2D''') , block_out_channels=(32, 64) , attention_head_dim=(2, 4) , class_embed_type='''projection''' , projection_class_embeddings_input_dim=embedder_projection_dim * 2 , cross_attention_dim=_lowerCamelCase , layers_per_block=1 , upcast_attention=_lowerCamelCase , use_linear_projection=_lowerCamelCase , ) torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Optional[int] = DDIMScheduler( beta_schedule='''scaled_linear''' , beta_start=0.0_0_0_8_5 , beta_end=0.0_1_2 , prediction_type='''v_prediction''' , set_alpha_to_one=_lowerCamelCase , steps_offset=1 , ) torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Dict = AutoencoderKL() SCREAMING_SNAKE_CASE : Optional[Any] = { # image encoding components '''feature_extractor''': feature_extractor, '''image_encoder''': image_encoder.eval(), # image noising components '''image_normalizer''': image_normalizer.eval(), '''image_noising_scheduler''': image_noising_scheduler, # regular denoising components '''tokenizer''': tokenizer, '''text_encoder''': text_encoder.eval(), '''unet''': unet.eval(), '''scheduler''': scheduler, '''vae''': vae.eval(), } return components def __lowerCAmelCase ( self , _lowerCamelCase , _lowerCamelCase=0 , _lowerCamelCase=True ) ->Optional[int]: if str(_lowerCamelCase ).startswith('''mps''' ): SCREAMING_SNAKE_CASE : Optional[int] = torch.manual_seed(_lowerCamelCase ) else: SCREAMING_SNAKE_CASE : Union[str, Any] = torch.Generator(device=_lowerCamelCase ).manual_seed(_lowerCamelCase ) SCREAMING_SNAKE_CASE : List[str] = floats_tensor((1, 3, 32, 32) , rng=random.Random(_lowerCamelCase ) ).to(_lowerCamelCase ) if pil_image: SCREAMING_SNAKE_CASE : Any = input_image * 0.5 + 0.5 SCREAMING_SNAKE_CASE : int = input_image.clamp(0 , 1 ) SCREAMING_SNAKE_CASE : Union[str, Any] = input_image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() SCREAMING_SNAKE_CASE : List[str] = DiffusionPipeline.numpy_to_pil(_lowerCamelCase )[0] return { "prompt": "An anime racoon running a marathon", "image": input_image, "generator": generator, "num_inference_steps": 2, "output_type": "np", } @skip_mps def __lowerCAmelCase ( self ) ->Tuple: SCREAMING_SNAKE_CASE : Any = '''cpu''' # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE : Union[str, Any] = self.get_dummy_components() SCREAMING_SNAKE_CASE : Tuple = StableUnCLIPImgaImgPipeline(**_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 ) inputs.update({'''image_embeds''': None} ) SCREAMING_SNAKE_CASE : Optional[int] = sd_pipe(**_lowerCamelCase ).images SCREAMING_SNAKE_CASE : Dict = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) SCREAMING_SNAKE_CASE : Tuple = np.array([0.3_8_7_2, 0.7_2_2_4, 0.5_6_0_1, 0.4_7_4_1, 0.6_8_7_2, 0.5_8_1_4, 0.4_6_3_6, 0.3_8_6_7, 0.5_0_7_8] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def __lowerCAmelCase ( self ) ->Tuple: SCREAMING_SNAKE_CASE : str = torch_device in ['''cpu''', '''mps'''] self._test_attention_slicing_forward_pass(test_max_difference=_lowerCamelCase ) def __lowerCAmelCase ( self ) ->List[Any]: SCREAMING_SNAKE_CASE : Tuple = torch_device in ['''cpu''', '''mps'''] self._test_inference_batch_single_identical(test_max_difference=_lowerCamelCase ) @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def __lowerCAmelCase ( self ) ->Optional[int]: self._test_xformers_attention_forwardGenerator_pass(test_max_difference=_lowerCamelCase ) @slow @require_torch_gpu class a_ ( unittest.TestCase ): """simple docstring""" def __lowerCAmelCase ( self ) ->int: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __lowerCAmelCase ( self ) ->Tuple: SCREAMING_SNAKE_CASE : List[Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png''' ) SCREAMING_SNAKE_CASE : Optional[Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_l_img2img_anime_turtle_fp16.npy''' ) SCREAMING_SNAKE_CASE : List[Any] = StableUnCLIPImgaImgPipeline.from_pretrained( '''fusing/stable-unclip-2-1-l-img2img''' , torch_dtype=torch.floataa ) pipe.to(_lowerCamelCase ) pipe.set_progress_bar_config(disable=_lowerCamelCase ) # stable unclip will oom when integration tests are run on a V100, # so turn on memory savings pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() SCREAMING_SNAKE_CASE : Union[str, Any] = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : Tuple = pipe(_lowerCamelCase , '''anime turle''' , generator=_lowerCamelCase , output_type='''np''' ) SCREAMING_SNAKE_CASE : List[str] = output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(_lowerCamelCase , _lowerCamelCase ) def __lowerCAmelCase ( self ) ->Optional[Any]: SCREAMING_SNAKE_CASE : List[str] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png''' ) SCREAMING_SNAKE_CASE : Optional[int] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_h_img2img_anime_turtle_fp16.npy''' ) SCREAMING_SNAKE_CASE : List[str] = StableUnCLIPImgaImgPipeline.from_pretrained( '''fusing/stable-unclip-2-1-h-img2img''' , torch_dtype=torch.floataa ) pipe.to(_lowerCamelCase ) pipe.set_progress_bar_config(disable=_lowerCamelCase ) # stable unclip will oom when integration tests are run on a V100, # so turn on memory savings pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() SCREAMING_SNAKE_CASE : Any = torch.Generator(device='''cpu''' ).manual_seed(0 ) SCREAMING_SNAKE_CASE : Tuple = pipe(_lowerCamelCase , '''anime turle''' , generator=_lowerCamelCase , output_type='''np''' ) SCREAMING_SNAKE_CASE : List[str] = output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(_lowerCamelCase , _lowerCamelCase ) def __lowerCAmelCase ( self ) ->Any: SCREAMING_SNAKE_CASE : Union[str, Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png''' ) torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() SCREAMING_SNAKE_CASE : str = StableUnCLIPImgaImgPipeline.from_pretrained( '''fusing/stable-unclip-2-1-h-img2img''' , torch_dtype=torch.floataa ) SCREAMING_SNAKE_CASE : Dict = pipe.to(_lowerCamelCase ) pipe.set_progress_bar_config(disable=_lowerCamelCase ) pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() SCREAMING_SNAKE_CASE : Dict = pipe( _lowerCamelCase , '''anime turtle''' , num_inference_steps=2 , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Any = torch.cuda.max_memory_allocated() # make sure that less than 7 GB is allocated assert mem_bytes < 7 * 10**9
313
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging _a = logging.get_logger(__name__) _a = { 'naver-clova-ix/donut-base': 'https://huggingface.co/naver-clova-ix/donut-base/resolve/main/config.json', # See all Donut models at https://huggingface.co/models?filter=donut-swin } class A_ (lowercase__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Dict = """donut-swin""" SCREAMING_SNAKE_CASE__ : Union[str, Any] = { """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers""", } def __init__( self , lowercase_=224 , lowercase_=4 , lowercase_=3 , lowercase_=96 , lowercase_=[2, 2, 6, 2] , lowercase_=[3, 6, 12, 24] , lowercase_=7 , lowercase_=4.0 , lowercase_=True , lowercase_=0.0 , lowercase_=0.0 , lowercase_=0.1 , lowercase_="gelu" , lowercase_=False , lowercase_=0.02 , lowercase_=1E-5 , **lowercase_ , ): """simple docstring""" super().__init__(**lowercase_ ) UpperCAmelCase_ : List[Any] = image_size UpperCAmelCase_ : Optional[Any] = patch_size UpperCAmelCase_ : List[Any] = num_channels UpperCAmelCase_ : Optional[int] = embed_dim UpperCAmelCase_ : Optional[int] = depths UpperCAmelCase_ : List[str] = len(lowercase_ ) UpperCAmelCase_ : int = num_heads UpperCAmelCase_ : Dict = window_size UpperCAmelCase_ : Tuple = mlp_ratio UpperCAmelCase_ : List[str] = qkv_bias UpperCAmelCase_ : str = hidden_dropout_prob UpperCAmelCase_ : Optional[Any] = attention_probs_dropout_prob UpperCAmelCase_ : Tuple = drop_path_rate UpperCAmelCase_ : int = hidden_act UpperCAmelCase_ : Tuple = use_absolute_embeddings UpperCAmelCase_ : Optional[Any] = layer_norm_eps UpperCAmelCase_ : Optional[Any] = initializer_range # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model UpperCAmelCase_ : List[Any] = int(embed_dim * 2 ** (len(lowercase_ ) - 1) )
23
"""simple docstring""" 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 _a = logging.getLogger() @unittest.skip("""Temporarily disable the doc tests.""" ) @require_torch @require_tf @slow class A_ (unittest.TestCase ): '''simple docstring''' def UpperCamelCase__ ( self , lowercase_ , lowercase_ = None , lowercase_ = None , lowercase_ = None , lowercase_ = True , ): """simple docstring""" UpperCAmelCase_ : List[str] = [file for file in os.listdir(lowercase_ ) if os.path.isfile(os.path.join(lowercase_ , lowercase_ ) )] if identifier is not None: UpperCAmelCase_ : Dict = [file for file in files if identifier in file] if n_identifier is not None: if isinstance(lowercase_ , lowercase_ ): for n_ in n_identifier: UpperCAmelCase_ : str = [file for file in files if n_ not in file] else: UpperCAmelCase_ : Any = [file for file in files if n_identifier not in file] UpperCAmelCase_ : Union[str, Any] = ignore_files or [] ignore_files.append("__init__.py" ) UpperCAmelCase_ : Optional[int] = [file for file in files if file not in ignore_files] for file in files: # Open all files print("Testing" , lowercase_ ) if only_modules: UpperCAmelCase_ : str = file.split("." )[0] try: UpperCAmelCase_ : str = getattr(lowercase_ , lowercase_ ) UpperCAmelCase_ : Tuple = doctest.DocTestSuite(lowercase_ ) UpperCAmelCase_ : int = unittest.TextTestRunner().run(lowercase_ ) self.assertIs(len(result.failures ) , 0 ) except AttributeError: logger.info(F"""{module_identifier} is not a module.""" ) else: UpperCAmelCase_ : Optional[Any] = doctest.testfile(str(".." / directory / file ) , optionflags=doctest.ELLIPSIS ) self.assertIs(result.failed , 0 ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : int = Path("src/transformers" ) UpperCAmelCase_ : str = "modeling" UpperCAmelCase_ : Optional[Any] = [ "modeling_ctrl.py", "modeling_tf_ctrl.py", ] self.analyze_directory(lowercase_ , identifier=lowercase_ , ignore_files=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : List[Any] = Path("src/transformers" ) UpperCAmelCase_ : Any = "tokenization" self.analyze_directory(lowercase_ , identifier=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = Path("src/transformers" ) UpperCAmelCase_ : List[Any] = "configuration" self.analyze_directory(lowercase_ , identifier=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Union[str, Any] = Path("src/transformers" ) UpperCAmelCase_ : List[Any] = ["configuration", "modeling", "tokenization"] self.analyze_directory(lowercase_ , n_identifier=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Dict = Path("docs/source" ) UpperCAmelCase_ : Union[str, Any] = ["favicon.ico"] self.analyze_directory(lowercase_ , ignore_files=lowercase_ , only_modules=lowercase_ )
23
1
'''simple docstring''' import shutil import tempfile import unittest import numpy as np import pytest from transformers import is_speech_available, is_vision_available from transformers.testing_utils import require_torch if is_vision_available(): from transformers import TvltImageProcessor if is_speech_available(): from transformers import TvltFeatureExtractor from transformers import TvltProcessor @require_torch class __lowercase ( unittest.TestCase ): def UpperCAmelCase__ (self ): lowerCamelCase_ : int = '''ZinengTang/tvlt-base''' lowerCamelCase_ : Union[str, Any] = tempfile.mkdtemp() def UpperCAmelCase__ (self , **A ): return TvltImageProcessor.from_pretrained(self.checkpoint , **A ) def UpperCAmelCase__ (self , **A ): return TvltFeatureExtractor.from_pretrained(self.checkpoint , **A ) def UpperCAmelCase__ (self ): shutil.rmtree(self.tmpdirname ) def UpperCAmelCase__ (self ): lowerCamelCase_ : Optional[int] = self.get_image_processor() lowerCamelCase_ : Optional[Any] = self.get_feature_extractor() lowerCamelCase_ : Union[str, Any] = TvltProcessor(image_processor=A , feature_extractor=A ) processor.save_pretrained(self.tmpdirname ) lowerCamelCase_ : List[Any] = TvltProcessor.from_pretrained(self.tmpdirname ) self.assertIsInstance(processor.feature_extractor , A ) self.assertIsInstance(processor.image_processor , A ) def UpperCAmelCase__ (self ): lowerCamelCase_ : str = self.get_image_processor() lowerCamelCase_ : Union[str, Any] = self.get_feature_extractor() lowerCamelCase_ : Union[str, Any] = TvltProcessor(image_processor=A , feature_extractor=A ) lowerCamelCase_ : Union[str, Any] = np.ones([1_2_0_0_0] ) lowerCamelCase_ : str = feature_extractor(A , return_tensors='''np''' ) lowerCamelCase_ : int = processor(audio=A , return_tensors='''np''' ) for key in audio_dict.keys(): self.assertAlmostEqual(audio_dict[key].sum() , input_processor[key].sum() , delta=1E-2 ) def UpperCAmelCase__ (self ): lowerCamelCase_ : Any = self.get_image_processor() lowerCamelCase_ : Union[str, Any] = self.get_feature_extractor() lowerCamelCase_ : List[str] = TvltProcessor(image_processor=A , feature_extractor=A ) lowerCamelCase_ : str = np.ones([3, 2_2_4, 2_2_4] ) lowerCamelCase_ : Optional[Any] = image_processor(A , return_tensors='''np''' ) lowerCamelCase_ : str = processor(images=A , return_tensors='''np''' ) for key in image_dict.keys(): self.assertAlmostEqual(image_dict[key].sum() , input_processor[key].sum() , delta=1E-2 ) def UpperCAmelCase__ (self ): lowerCamelCase_ : int = self.get_image_processor() lowerCamelCase_ : Optional[int] = self.get_feature_extractor() lowerCamelCase_ : str = TvltProcessor(image_processor=A , feature_extractor=A ) lowerCamelCase_ : Optional[int] = np.ones([1_2_0_0_0] ) lowerCamelCase_ : int = np.ones([3, 2_2_4, 2_2_4] ) lowerCamelCase_ : Optional[Any] = processor(audio=A , images=A ) self.assertListEqual(list(inputs.keys() ) , ['''audio_values''', '''audio_mask''', '''pixel_values''', '''pixel_mask'''] ) # test if it raises when no input is passed with pytest.raises(A ): processor() def UpperCAmelCase__ (self ): lowerCamelCase_ : Optional[int] = self.get_image_processor() lowerCamelCase_ : int = self.get_feature_extractor() lowerCamelCase_ : List[Any] = TvltProcessor(image_processor=A , feature_extractor=A ) self.assertListEqual( processor.model_input_names , image_processor.model_input_names + feature_extractor.model_input_names , msg='''`processor` and `image_processor`+`feature_extractor` model input names do not match''' , )
318
'''simple docstring''' from itertools import permutations def lowercase_ ( _lowercase ) -> bool: '''simple docstring''' if num[3] % 2 != 0: return False if (num[2] + num[3] + num[4]) % 3 != 0: return False if num[5] % 5 != 0: return False lowerCamelCase_ : int = [7, 11, 13, 17] for i, test in enumerate(_lowercase ): if (num[i + 4] * 100 + num[i + 5] * 10 + num[i + 6]) % test != 0: return False return True def lowercase_ ( _lowercase = 10 ) -> int: '''simple docstring''' return sum( int(''''''.join(map(_lowercase , _lowercase ) ) ) for num in permutations(range(_lowercase ) ) if is_substring_divisible(_lowercase ) ) if __name__ == "__main__": print(f'{solution() = }')
318
1
from __future__ import annotations UpperCamelCase__ = 8.9_88e9 # units = N * m^s * C^-2 def lowerCAmelCase_ ( __A, __A, __A, __A ) -> dict[str, float]: '''simple docstring''' UpperCAmelCase__ = abs(chargea * chargea ) if (force, chargea, chargea, distance).count(0 ) != 1: raise ValueError("One and only one argument must be 0" ) if distance < 0: raise ValueError("Distance cannot be negative" ) if force == 0: UpperCAmelCase__ = COULOMBS_CONSTANT * charge_product / (distance**2) return {"force": force} elif chargea == 0: UpperCAmelCase__ = abs(__A ) * (distance**2) / (COULOMBS_CONSTANT * chargea) return {"charge1": chargea} elif chargea == 0: UpperCAmelCase__ = abs(__A ) * (distance**2) / (COULOMBS_CONSTANT * chargea) return {"charge2": chargea} elif distance == 0: UpperCAmelCase__ = (COULOMBS_CONSTANT * charge_product / abs(__A )) ** 0.5 return {"distance": distance} raise ValueError("Exactly one argument must be 0" ) if __name__ == "__main__": import doctest doctest.testmod()
370
from __future__ import annotations import unittest from transformers import RoFormerConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForMultipleChoice, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerModel, ) from transformers.models.roformer.modeling_tf_roformer import ( TFRoFormerSelfAttention, TFRoFormerSinusoidalPositionalEmbedding, ) class A : def __init__(self : int , __UpperCAmelCase : Tuple , __UpperCAmelCase : List[str]=1_3 , __UpperCAmelCase : Optional[Any]=7 , __UpperCAmelCase : Dict=True , __UpperCAmelCase : int=True , __UpperCAmelCase : Any=True , __UpperCAmelCase : int=True , __UpperCAmelCase : Dict=9_9 , __UpperCAmelCase : Optional[int]=3_2 , __UpperCAmelCase : Dict=2 , __UpperCAmelCase : Any=4 , __UpperCAmelCase : Tuple=3_7 , __UpperCAmelCase : List[Any]="gelu" , __UpperCAmelCase : Dict=0.1 , __UpperCAmelCase : int=0.1 , __UpperCAmelCase : str=5_1_2 , __UpperCAmelCase : Union[str, Any]=1_6 , __UpperCAmelCase : Tuple=2 , __UpperCAmelCase : Tuple=0.02 , __UpperCAmelCase : Optional[int]=3 , __UpperCAmelCase : str=4 , __UpperCAmelCase : Any=None , ) -> Dict: """simple docstring""" UpperCAmelCase__ = parent UpperCAmelCase__ = 1_3 UpperCAmelCase__ = 7 UpperCAmelCase__ = True UpperCAmelCase__ = True UpperCAmelCase__ = True UpperCAmelCase__ = True UpperCAmelCase__ = 9_9 UpperCAmelCase__ = 3_2 UpperCAmelCase__ = 2 UpperCAmelCase__ = 4 UpperCAmelCase__ = 3_7 UpperCAmelCase__ = "gelu" UpperCAmelCase__ = 0.1 UpperCAmelCase__ = 0.1 UpperCAmelCase__ = 5_1_2 UpperCAmelCase__ = 1_6 UpperCAmelCase__ = 2 UpperCAmelCase__ = 0.02 UpperCAmelCase__ = 3 UpperCAmelCase__ = 4 UpperCAmelCase__ = None def lowercase_ (self : Union[str, Any] ) -> str: """simple docstring""" UpperCAmelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase__ = None if self.use_input_mask: UpperCAmelCase__ = random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase__ = None if self.use_token_type_ids: UpperCAmelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) UpperCAmelCase__ = None UpperCAmelCase__ = None UpperCAmelCase__ = None if self.use_labels: UpperCAmelCase__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCAmelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCAmelCase__ = ids_tensor([self.batch_size] , self.num_choices ) UpperCAmelCase__ = RoFormerConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , return_dict=__UpperCAmelCase , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def lowercase_ (self : List[Any] , __UpperCAmelCase : str , __UpperCAmelCase : List[str] , __UpperCAmelCase : List[Any] , __UpperCAmelCase : List[str] , __UpperCAmelCase : List[str] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Any ) -> List[str]: """simple docstring""" UpperCAmelCase__ = TFRoFormerModel(config=__UpperCAmelCase ) UpperCAmelCase__ = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} UpperCAmelCase__ = [input_ids, input_mask] UpperCAmelCase__ = model(__UpperCAmelCase ) UpperCAmelCase__ = model(__UpperCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowercase_ (self : int , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Any , __UpperCAmelCase : Any , __UpperCAmelCase : List[str] , __UpperCAmelCase : Tuple , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : List[Any] ) -> Any: """simple docstring""" UpperCAmelCase__ = True UpperCAmelCase__ = TFRoFormerForCausalLM(config=__UpperCAmelCase ) UpperCAmelCase__ = { "input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids, } UpperCAmelCase__ = model(__UpperCAmelCase )["logits"] self.parent.assertListEqual( list(prediction_scores.numpy().shape ) , [self.batch_size, self.seq_length, self.vocab_size] ) def lowercase_ (self : str , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : int , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Any , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Any , __UpperCAmelCase : int ) -> Union[str, Any]: """simple docstring""" UpperCAmelCase__ = TFRoFormerForMaskedLM(config=__UpperCAmelCase ) UpperCAmelCase__ = { "input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids, } UpperCAmelCase__ = model(__UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowercase_ (self : Optional[Any] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : List[str] , __UpperCAmelCase : List[Any] , __UpperCAmelCase : Tuple , __UpperCAmelCase : str ) -> Optional[Any]: """simple docstring""" UpperCAmelCase__ = self.num_labels UpperCAmelCase__ = TFRoFormerForSequenceClassification(config=__UpperCAmelCase ) UpperCAmelCase__ = { "input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids, } UpperCAmelCase__ = model(__UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowercase_ (self : str , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : int , __UpperCAmelCase : Any , __UpperCAmelCase : Tuple , __UpperCAmelCase : List[Any] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Optional[Any] ) -> Any: """simple docstring""" UpperCAmelCase__ = self.num_choices UpperCAmelCase__ = TFRoFormerForMultipleChoice(config=__UpperCAmelCase ) UpperCAmelCase__ = tf.tile(tf.expand_dims(__UpperCAmelCase , 1 ) , (1, self.num_choices, 1) ) UpperCAmelCase__ = tf.tile(tf.expand_dims(__UpperCAmelCase , 1 ) , (1, self.num_choices, 1) ) UpperCAmelCase__ = tf.tile(tf.expand_dims(__UpperCAmelCase , 1 ) , (1, self.num_choices, 1) ) UpperCAmelCase__ = { "input_ids": multiple_choice_inputs_ids, "attention_mask": multiple_choice_input_mask, "token_type_ids": multiple_choice_token_type_ids, } UpperCAmelCase__ = model(__UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def lowercase_ (self : int , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Tuple , __UpperCAmelCase : int , __UpperCAmelCase : int , __UpperCAmelCase : List[str] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : int ) -> Union[str, Any]: """simple docstring""" UpperCAmelCase__ = self.num_labels UpperCAmelCase__ = TFRoFormerForTokenClassification(config=__UpperCAmelCase ) UpperCAmelCase__ = { "input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids, } UpperCAmelCase__ = model(__UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowercase_ (self : Dict , __UpperCAmelCase : Tuple , __UpperCAmelCase : Any , __UpperCAmelCase : Any , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Tuple , __UpperCAmelCase : Dict ) -> List[str]: """simple docstring""" UpperCAmelCase__ = TFRoFormerForQuestionAnswering(config=__UpperCAmelCase ) UpperCAmelCase__ = { "input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids, } UpperCAmelCase__ = model(__UpperCAmelCase ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def lowercase_ (self : Dict ) -> Dict: """simple docstring""" UpperCAmelCase__ = self.prepare_config_and_inputs() ( ( UpperCAmelCase__ ) , ( UpperCAmelCase__ ) , ( UpperCAmelCase__ ) , ( UpperCAmelCase__ ) , ( UpperCAmelCase__ ) , ( UpperCAmelCase__ ) , ( UpperCAmelCase__ ) , ) = config_and_inputs UpperCAmelCase__ = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask} return config, inputs_dict @require_tf class A ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): __UpperCAmelCase : str = ( ( TFRoFormerModel, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerForMultipleChoice, ) if is_tf_available() else () ) __UpperCAmelCase : List[str] = ( { 'feature-extraction': TFRoFormerModel, 'fill-mask': TFRoFormerForMaskedLM, 'question-answering': TFRoFormerForQuestionAnswering, 'text-classification': TFRoFormerForSequenceClassification, 'text-generation': TFRoFormerForCausalLM, 'token-classification': TFRoFormerForTokenClassification, 'zero-shot': TFRoFormerForSequenceClassification, } if is_tf_available() else {} ) __UpperCAmelCase : Optional[int] = False __UpperCAmelCase : List[Any] = False def lowercase_ (self : List[str] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Dict , __UpperCAmelCase : Any , __UpperCAmelCase : Dict , __UpperCAmelCase : Any ) -> int: """simple docstring""" if pipeline_test_casse_name == "TextGenerationPipelineTests": return True return False def lowercase_ (self : Union[str, Any] ) -> List[Any]: """simple docstring""" UpperCAmelCase__ = TFRoFormerModelTester(self ) UpperCAmelCase__ = ConfigTester(self , config_class=__UpperCAmelCase , hidden_size=3_7 ) def lowercase_ (self : Optional[int] ) -> Any: """simple docstring""" self.config_tester.run_common_tests() def lowercase_ (self : Union[str, Any] ) -> List[Any]: """simple docstring""" UpperCAmelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__UpperCAmelCase ) def lowercase_ (self : Dict ) -> Any: """simple docstring""" UpperCAmelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*__UpperCAmelCase ) def lowercase_ (self : Union[str, Any] ) -> str: """simple docstring""" UpperCAmelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head(*__UpperCAmelCase ) def lowercase_ (self : Optional[Any] ) -> Dict: """simple docstring""" UpperCAmelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*__UpperCAmelCase ) def lowercase_ (self : List[Any] ) -> List[str]: """simple docstring""" UpperCAmelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*__UpperCAmelCase ) def lowercase_ (self : int ) -> List[Any]: """simple docstring""" UpperCAmelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*__UpperCAmelCase ) def lowercase_ (self : List[str] ) -> Tuple: """simple docstring""" UpperCAmelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*__UpperCAmelCase ) @slow def lowercase_ (self : Any ) -> Optional[Any]: """simple docstring""" UpperCAmelCase__ = TFRoFormerModel.from_pretrained("junnyu/roformer_chinese_base" ) self.assertIsNotNone(__UpperCAmelCase ) @require_tf class A ( unittest.TestCase ): @slow def lowercase_ (self : List[Any] ) -> Any: """simple docstring""" UpperCAmelCase__ = TFRoFormerForMaskedLM.from_pretrained("junnyu/roformer_chinese_base" ) UpperCAmelCase__ = tf.constant([[0, 1, 2, 3, 4, 5]] ) UpperCAmelCase__ = model(__UpperCAmelCase )[0] # TODO Replace vocab size UpperCAmelCase__ = 5_0_0_0_0 UpperCAmelCase__ = [1, 6, vocab_size] self.assertEqual(output.shape , __UpperCAmelCase ) print(output[:, :3, :3] ) # TODO Replace values below with what was printed above. UpperCAmelCase__ = tf.constant( [ [ [-0.12053341, -1.0264901, 0.29221946], [-1.5133783, 0.197433, 0.15190607], [-5.0135403, -3.900256, -0.84038764], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , __UpperCAmelCase , atol=1E-4 ) @require_tf class A ( unittest.TestCase ): __UpperCAmelCase : Tuple = 1E-4 def lowercase_ (self : List[str] ) -> str: """simple docstring""" UpperCAmelCase__ = tf.constant([[4, 1_0]] ) UpperCAmelCase__ = TFRoFormerSinusoidalPositionalEmbedding(num_positions=6 , embedding_dim=6 ) UpperCAmelCase__ = emba(input_ids.shape ) UpperCAmelCase__ = tf.constant( [[0.0000, 0.0000, 0.0000, 1.0000, 1.0000, 1.0000], [0.8415, 0.0464, 0.0022, 0.5403, 0.9989, 1.0000]] ) tf.debugging.assert_near(__UpperCAmelCase , __UpperCAmelCase , atol=self.tolerance ) def lowercase_ (self : List[Any] ) -> int: """simple docstring""" UpperCAmelCase__ = tf.constant( [ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000], [0.8415, 0.8219, 0.8020, 0.7819, 0.7617], [0.9093, 0.9364, 0.9581, 0.9749, 0.9870], ] ) UpperCAmelCase__ = TFRoFormerSinusoidalPositionalEmbedding(num_positions=5_1_2 , embedding_dim=5_1_2 ) emba([2, 1_6, 5_1_2] ) UpperCAmelCase__ = emba.weight[:3, :5] tf.debugging.assert_near(__UpperCAmelCase , __UpperCAmelCase , atol=self.tolerance ) @require_tf class A ( unittest.TestCase ): __UpperCAmelCase : Any = 1E-4 def lowercase_ (self : Tuple ) -> int: """simple docstring""" UpperCAmelCase__ = tf.reshape(tf.range(2 * 1_2 * 1_6 * 6_4 , dtype=tf.floataa ) , shape=(2, 1_2, 1_6, 6_4) ) / 1_0_0 UpperCAmelCase__ = -tf.reshape(tf.range(2 * 1_2 * 1_6 * 6_4 , dtype=tf.floataa ) , shape=(2, 1_2, 1_6, 6_4) ) / 1_0_0 UpperCAmelCase__ = TFRoFormerSinusoidalPositionalEmbedding(num_positions=3_2 , embedding_dim=6_4 ) UpperCAmelCase__ = embed_positions([2, 1_6, 7_6_8] )[None, None, :, :] UpperCAmelCase__ , UpperCAmelCase__ = TFRoFormerSelfAttention.apply_rotary_position_embeddings( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) UpperCAmelCase__ = tf.constant( [ [0.0000, 0.0100, 0.0200, 0.0300, 0.0400, 0.0500, 0.0600, 0.0700], [-0.2012, 0.8897, 0.0263, 0.9401, 0.2074, 0.9463, 0.3481, 0.9343], [-1.7057, 0.6271, -1.2145, 1.3897, -0.6303, 1.7647, -0.1173, 1.8985], [-2.1731, -1.6397, -2.7358, 0.2854, -2.1840, 1.7183, -1.3018, 2.4871], [0.2717, -3.6173, -2.9206, -2.1988, -3.6638, 0.3858, -2.9155, 2.2980], [3.9859, -2.1580, -0.7984, -4.4904, -4.1181, -2.0252, -4.4782, 1.1253], ] ) UpperCAmelCase__ = tf.constant( [ [0.0000, -0.0100, -0.0200, -0.0300, -0.0400, -0.0500, -0.0600, -0.0700], [0.2012, -0.8897, -0.0263, -0.9401, -0.2074, -0.9463, -0.3481, -0.9343], [1.7057, -0.6271, 1.2145, -1.3897, 0.6303, -1.7647, 0.1173, -1.8985], [2.1731, 1.6397, 2.7358, -0.2854, 2.1840, -1.7183, 1.3018, -2.4871], [-0.2717, 3.6173, 2.9206, 2.1988, 3.6638, -0.3858, 2.9155, -2.2980], [-3.9859, 2.1580, 0.7984, 4.4904, 4.1181, 2.0252, 4.4782, -1.1253], ] ) tf.debugging.assert_near(query_layer[0, 0, :6, :8] , __UpperCAmelCase , atol=self.tolerance ) tf.debugging.assert_near(key_layer[0, 0, :6, :8] , __UpperCAmelCase , atol=self.tolerance )
143
0
"""simple docstring""" from pathlib import Path import numpy as np from PIL import Image def lowercase_ ( _UpperCAmelCase ): """simple docstring""" A_ , A_ , A_ : Union[str, Any] = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2] return 0.2989 * r + 0.5870 * g + 0.1140 * b def lowercase_ ( _UpperCAmelCase ): """simple docstring""" return (gray > 127) & (gray <= 255) def lowercase_ ( _UpperCAmelCase , _UpperCAmelCase ): """simple docstring""" A_ : List[Any] = np.zeros_like(_UpperCAmelCase ) A_ : Tuple = np.zeros( (image.shape[0] + kernel.shape[0] - 1, image.shape[1] + kernel.shape[1] - 1) ) # Copy image to padded image A_ : Tuple = image # Iterate over image & apply kernel for x in range(image.shape[1] ): for y in range(image.shape[0] ): A_ : int = ( kernel * image_padded[y : y + kernel.shape[0], x : x + kernel.shape[1]] ).sum() A_ : List[str] = int(summation > 0 ) return output if __name__ == "__main__": # read original image _lowerCamelCase : Tuple = Path(__file__).resolve().parent / 'image_data' / 'lena.jpg' _lowerCamelCase : Union[str, Any] = np.array(Image.open(lena_path)) # kernel to be applied _lowerCamelCase : Union[str, Any] = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]]) _lowerCamelCase : int = dilation(gray_to_binary(rgb_to_gray(lena)), structuring_element) # Save the output image _lowerCamelCase : Optional[Any] = Image.fromarray(output).convert('RGB') pil_img.save('result_dilation.png')
167
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available _lowerCamelCase : Any = { 'configuration_altclip': [ 'ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP', 'AltCLIPConfig', 'AltCLIPTextConfig', 'AltCLIPVisionConfig', ], 'processing_altclip': ['AltCLIPProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Union[str, Any] = [ 'ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST', 'AltCLIPPreTrainedModel', 'AltCLIPModel', 'AltCLIPTextModel', 'AltCLIPVisionModel', ] if TYPE_CHECKING: from .configuration_altclip import ( ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, AltCLIPConfig, AltCLIPTextConfig, AltCLIPVisionConfig, ) from .processing_altclip import AltCLIPProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_altclip import ( ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST, AltCLIPModel, AltCLIPPreTrainedModel, AltCLIPTextModel, AltCLIPVisionModel, ) else: import sys _lowerCamelCase : int = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
167
1
"""simple docstring""" import functools def lowercase_ ( __UpperCAmelCase , __UpperCAmelCase ) -> int: # 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 ) >= 366: raise ValueError("""All days elements should be less than 366""" ) lowerCAmelCase__ : Union[str, Any] = set(__UpperCAmelCase ) @functools.cache def dynamic_programming(__UpperCAmelCase ) -> int: if index > 365: 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()
362
"""simple docstring""" from argparse import ArgumentParser from . import BaseTransformersCLICommand def lowercase_ ( __UpperCAmelCase ) -> List[str]: return DownloadCommand(args.model , args.cache_dir , args.force , args.trust_remote_code ) class _lowerCamelCase ( a_ ): @staticmethod def _lowerCAmelCase ( UpperCamelCase : ArgumentParser ) -> List[Any]: """simple docstring""" lowerCAmelCase__ : int = parser.add_parser("""download""" ) download_parser.add_argument( """--cache-dir""" , type=UpperCamelCase , default=UpperCamelCase , help="""Path to location to store the models""" ) download_parser.add_argument( """--force""" , action="""store_true""" , help="""Force the model to be download even if already in cache-dir""" ) download_parser.add_argument( """--trust-remote-code""" , action="""store_true""" , help="""Whether or not to allow for custom models defined on the Hub in their own modeling files. Use only if you've reviewed the code as it will execute on your local machine""" , ) download_parser.add_argument("""model""" , type=UpperCamelCase , help="""Name of the model to download""" ) download_parser.set_defaults(func=UpperCamelCase ) def __init__( self : Optional[int] , UpperCamelCase : str , UpperCamelCase : str , UpperCamelCase : bool , UpperCamelCase : bool ) -> Any: """simple docstring""" lowerCAmelCase__ : int = model lowerCAmelCase__ : Union[str, Any] = cache lowerCAmelCase__ : Optional[int] = force lowerCAmelCase__ : Dict = trust_remote_code def _lowerCAmelCase ( self : int ) -> Any: """simple docstring""" from ..models.auto import AutoModel, AutoTokenizer AutoModel.from_pretrained( self._model , cache_dir=self._cache , force_download=self._force , trust_remote_code=self._trust_remote_code ) AutoTokenizer.from_pretrained( self._model , cache_dir=self._cache , force_download=self._force , trust_remote_code=self._trust_remote_code )
212
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging SCREAMING_SNAKE_CASE__ : Tuple = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ : int = { "junnyu/roformer_chinese_small": "https://huggingface.co/junnyu/roformer_chinese_small/resolve/main/config.json", "junnyu/roformer_chinese_base": "https://huggingface.co/junnyu/roformer_chinese_base/resolve/main/config.json", "junnyu/roformer_chinese_char_small": ( "https://huggingface.co/junnyu/roformer_chinese_char_small/resolve/main/config.json" ), "junnyu/roformer_chinese_char_base": ( "https://huggingface.co/junnyu/roformer_chinese_char_base/resolve/main/config.json" ), "junnyu/roformer_small_discriminator": ( "https://huggingface.co/junnyu/roformer_small_discriminator/resolve/main/config.json" ), "junnyu/roformer_small_generator": ( "https://huggingface.co/junnyu/roformer_small_generator/resolve/main/config.json" ), # See all RoFormer models at https://huggingface.co/models?filter=roformer } class lowerCAmelCase__ ( __lowercase ): a__ : Tuple = "roformer" def __init__( self : Tuple , SCREAMING_SNAKE_CASE__ : Optional[Any]=5_00_00 , SCREAMING_SNAKE_CASE__ : str=None , SCREAMING_SNAKE_CASE__ : Dict=7_68 , SCREAMING_SNAKE_CASE__ : int=12 , SCREAMING_SNAKE_CASE__ : str=12 , SCREAMING_SNAKE_CASE__ : Optional[int]=30_72 , SCREAMING_SNAKE_CASE__ : List[Any]="gelu" , SCREAMING_SNAKE_CASE__ : Tuple=0.1 , SCREAMING_SNAKE_CASE__ : int=0.1 , SCREAMING_SNAKE_CASE__ : Tuple=15_36 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=2 , SCREAMING_SNAKE_CASE__ : Tuple=0.02 , SCREAMING_SNAKE_CASE__ : Any=1e-12 , SCREAMING_SNAKE_CASE__ : int=0 , SCREAMING_SNAKE_CASE__ : str=False , SCREAMING_SNAKE_CASE__ : Union[str, Any]=True , **SCREAMING_SNAKE_CASE__ : List[Any] , ) -> Optional[int]: super().__init__(pad_token_id=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) __lowerCamelCase = vocab_size __lowerCamelCase = hidden_size if embedding_size is None else embedding_size __lowerCamelCase = hidden_size __lowerCamelCase = num_hidden_layers __lowerCamelCase = num_attention_heads __lowerCamelCase = hidden_act __lowerCamelCase = intermediate_size __lowerCamelCase = hidden_dropout_prob __lowerCamelCase = attention_probs_dropout_prob __lowerCamelCase = max_position_embeddings __lowerCamelCase = type_vocab_size __lowerCamelCase = initializer_range __lowerCamelCase = layer_norm_eps __lowerCamelCase = rotary_value __lowerCamelCase = use_cache class lowerCAmelCase__ ( __lowercase ): @property def __A ( self : Union[str, Any] ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": __lowerCamelCase = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: __lowerCamelCase = {0: '''batch''', 1: '''sequence'''} __lowerCamelCase = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis), ] )
270
'''simple docstring''' from ...processing_utils import ProcessorMixin class __a (lowerCamelCase ): __a : Dict = "SpeechT5FeatureExtractor" __a : Any = "SpeechT5Tokenizer" def __init__( self : Any , __magic_name__ : Union[str, Any] , __magic_name__ : Optional[int] ) -> int: """simple docstring""" super().__init__(__magic_name__ , __magic_name__ ) def __call__( self : List[str] , *__magic_name__ : str , **__magic_name__ : Optional[int] ) -> Union[str, Any]: """simple docstring""" UpperCAmelCase_ : List[str] = kwargs.pop('''audio''' , __magic_name__ ) UpperCAmelCase_ : Any = kwargs.pop('''text''' , __magic_name__ ) UpperCAmelCase_ : List[Any] = kwargs.pop('''text_target''' , __magic_name__ ) UpperCAmelCase_ : List[str] = kwargs.pop('''audio_target''' , __magic_name__ ) UpperCAmelCase_ : int = kwargs.pop('''sampling_rate''' , __magic_name__ ) if audio is not None and text is not None: raise ValueError( '''Cannot process both `audio` and `text` inputs. Did you mean `audio_target` or `text_target`?''' ) if audio_target is not None and text_target is not None: raise ValueError( '''Cannot process both `audio_target` and `text_target` inputs. Did you mean `audio` or `text`?''' ) if audio is None and audio_target is None and text is None and text_target is None: raise ValueError( '''You need to specify either an `audio`, `audio_target`, `text`, or `text_target` input to process.''' ) if audio is not None: UpperCAmelCase_ : int = self.feature_extractor(__magic_name__ , *__magic_name__ , sampling_rate=__magic_name__ , **__magic_name__ ) elif text is not None: UpperCAmelCase_ : Dict = self.tokenizer(__magic_name__ , **__magic_name__ ) else: UpperCAmelCase_ : Dict = None if audio_target is not None: UpperCAmelCase_ : Tuple = self.feature_extractor(audio_target=__magic_name__ , *__magic_name__ , sampling_rate=__magic_name__ , **__magic_name__ ) UpperCAmelCase_ : Optional[int] = targets['''input_values'''] elif text_target is not None: UpperCAmelCase_ : Any = self.tokenizer(__magic_name__ , **__magic_name__ ) UpperCAmelCase_ : str = targets['''input_ids'''] else: UpperCAmelCase_ : Tuple = None if inputs is None: return targets if targets is not None: UpperCAmelCase_ : int = labels UpperCAmelCase_ : Optional[Any] = targets.get('''attention_mask''' ) if decoder_attention_mask is not None: UpperCAmelCase_ : List[str] = decoder_attention_mask return inputs def UpperCAmelCase__ ( self : str , *__magic_name__ : Dict , **__magic_name__ : int ) -> Any: """simple docstring""" UpperCAmelCase_ : List[str] = kwargs.pop('''input_values''' , __magic_name__ ) UpperCAmelCase_ : int = kwargs.pop('''input_ids''' , __magic_name__ ) UpperCAmelCase_ : List[str] = kwargs.pop('''labels''' , __magic_name__ ) if input_values is not None and input_ids is not None: raise ValueError('''Cannot process both `input_values` and `input_ids` inputs.''' ) if input_values is None and input_ids is None and labels is None: raise ValueError( '''You need to specify either an `input_values`, `input_ids`, or `labels` input to be padded.''' ) if input_values is not None: UpperCAmelCase_ : int = self.feature_extractor.pad(__magic_name__ , *__magic_name__ , **__magic_name__ ) elif input_ids is not None: UpperCAmelCase_ : List[str] = self.tokenizer.pad(__magic_name__ , **__magic_name__ ) else: UpperCAmelCase_ : List[Any] = None if labels is not None: if "input_ids" in labels or (isinstance(__magic_name__ , __magic_name__ ) and "input_ids" in labels[0]): UpperCAmelCase_ : Union[str, Any] = self.tokenizer.pad(__magic_name__ , **__magic_name__ ) UpperCAmelCase_ : Tuple = targets['''input_ids'''] else: UpperCAmelCase_ : Union[str, Any] = self.feature_extractor.feature_size UpperCAmelCase_ : Optional[int] = self.feature_extractor.num_mel_bins UpperCAmelCase_ : int = self.feature_extractor.pad(__magic_name__ , *__magic_name__ , **__magic_name__ ) UpperCAmelCase_ : Optional[int] = feature_size_hack UpperCAmelCase_ : Any = targets['''input_values'''] else: UpperCAmelCase_ : List[str] = None if inputs is None: return targets if targets is not None: UpperCAmelCase_ : str = labels UpperCAmelCase_ : int = targets.get('''attention_mask''' ) if decoder_attention_mask is not None: UpperCAmelCase_ : Dict = decoder_attention_mask return inputs def UpperCAmelCase__ ( self : List[Any] , *__magic_name__ : int , **__magic_name__ : List[str] ) -> List[str]: """simple docstring""" return self.tokenizer.batch_decode(*__magic_name__ , **__magic_name__ ) def UpperCAmelCase__ ( self : Optional[int] , *__magic_name__ : int , **__magic_name__ : Any ) -> Dict: """simple docstring""" return self.tokenizer.decode(*__magic_name__ , **__magic_name__ )
125
0
import json import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from transformers import OneFormerImageProcessor from transformers.models.oneformer.image_processing_oneformer import binary_mask_to_rle from transformers.models.oneformer.modeling_oneformer import OneFormerForUniversalSegmentationOutput if is_vision_available(): from PIL import Image def __lowercase ( lowerCamelCase : Any , lowerCamelCase : Union[str, Any]="shi-labs/oneformer_demo" ): with open(hf_hub_download(lowerCamelCase , lowerCamelCase , repo_type='dataset' ) , 'r' ) as f: UpperCamelCase_ : List[str] = json.load(lowerCamelCase ) UpperCamelCase_ : int = {} UpperCamelCase_ : Tuple = [] UpperCamelCase_ : Optional[Any] = [] for key, info in class_info.items(): UpperCamelCase_ : str = info['name'] class_names.append(info['name'] ) if info["isthing"]: thing_ids.append(int(lowerCamelCase ) ) UpperCamelCase_ : int = thing_ids UpperCamelCase_ : str = class_names return metadata class _lowercase ( unittest.TestCase ): def __init__( self : Optional[int] , snake_case : Optional[int] , snake_case : Dict=7 , snake_case : Optional[int]=3 , snake_case : int=3_0 , snake_case : List[str]=4_0_0 , snake_case : Dict=None , snake_case : Optional[Any]=True , snake_case : List[str]=True , snake_case : Optional[Any]=[0.5, 0.5, 0.5] , snake_case : Optional[Any]=[0.5, 0.5, 0.5] , snake_case : Dict=1_0 , snake_case : Tuple=False , snake_case : Dict=2_5_5 , snake_case : Any="shi-labs/oneformer_demo" , snake_case : List[str]="ade20k_panoptic.json" , snake_case : Tuple=1_0 , ) -> Tuple: """simple docstring""" UpperCamelCase_ : Tuple = parent UpperCamelCase_ : Tuple = batch_size UpperCamelCase_ : List[Any] = num_channels UpperCamelCase_ : List[Any] = min_resolution UpperCamelCase_ : str = max_resolution UpperCamelCase_ : Optional[int] = do_resize UpperCamelCase_ : List[str] = {'shortest_edge': 3_2, 'longest_edge': 1_3_3_3} if size is None else size UpperCamelCase_ : Any = do_normalize UpperCamelCase_ : str = image_mean UpperCamelCase_ : Optional[Any] = image_std UpperCamelCase_ : Tuple = class_info_file UpperCamelCase_ : Any = prepare_metadata(snake_case , snake_case ) UpperCamelCase_ : Optional[int] = num_text UpperCamelCase_ : Tuple = repo_path # for the post_process_functions UpperCamelCase_ : Any = 2 UpperCamelCase_ : Union[str, Any] = 1_0 UpperCamelCase_ : int = 1_0 UpperCamelCase_ : Optional[int] = 3 UpperCamelCase_ : List[Any] = 4 UpperCamelCase_ : Any = num_labels UpperCamelCase_ : List[Any] = do_reduce_labels UpperCamelCase_ : int = ignore_index def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ) -> List[Any]: """simple docstring""" return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "num_labels": self.num_labels, "do_reduce_labels": self.do_reduce_labels, "ignore_index": self.ignore_index, "class_info_file": self.class_info_file, "metadata": self.metadata, "num_text": self.num_text, } def SCREAMING_SNAKE_CASE__ ( self : Dict , snake_case : str , snake_case : Tuple=False ) -> int: """simple docstring""" if not batched: UpperCamelCase_ : int = image_inputs[0] if isinstance(snake_case , Image.Image ): UpperCamelCase_, UpperCamelCase_ : Tuple = image.size else: UpperCamelCase_, UpperCamelCase_ : Optional[int] = image.shape[1], image.shape[2] if w < h: UpperCamelCase_ : List[Any] = int(self.size['shortest_edge'] * h / w ) UpperCamelCase_ : List[Any] = self.size['shortest_edge'] elif w > h: UpperCamelCase_ : Tuple = self.size['shortest_edge'] UpperCamelCase_ : Dict = int(self.size['shortest_edge'] * w / h ) else: UpperCamelCase_ : Tuple = self.size['shortest_edge'] UpperCamelCase_ : List[Any] = self.size['shortest_edge'] else: UpperCamelCase_ : List[str] = [] for image in image_inputs: UpperCamelCase_, UpperCamelCase_ : str = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) UpperCamelCase_ : Union[str, Any] = max(snake_case , key=lambda snake_case : item[0] )[0] UpperCamelCase_ : str = max(snake_case , key=lambda snake_case : item[1] )[1] return expected_height, expected_width def SCREAMING_SNAKE_CASE__ ( self : str ) -> Optional[Any]: """simple docstring""" return OneFormerForUniversalSegmentationOutput( # +1 for null class class_queries_logits=torch.randn((self.batch_size, self.num_queries, self.num_classes + 1) ) , masks_queries_logits=torch.randn((self.batch_size, self.num_queries, self.height, self.width) ) , ) @require_torch @require_vision class _lowercase ( snake_case_ , unittest.TestCase ): lowercase = OneFormerImageProcessor if (is_vision_available() and is_torch_available()) else None # only for test_image_processing_common.test_image_proc_to_json_string lowercase = image_processing_class def SCREAMING_SNAKE_CASE__ ( self : List[Any] ) -> Union[str, Any]: """simple docstring""" UpperCamelCase_ : Optional[int] = OneFormerImageProcessorTester(self ) @property def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ) -> Tuple: """simple docstring""" return self.image_processing_tester.prepare_image_processor_dict() def SCREAMING_SNAKE_CASE__ ( self : Any ) -> Dict: """simple docstring""" UpperCamelCase_ : Optional[int] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(snake_case , 'image_mean' ) ) self.assertTrue(hasattr(snake_case , 'image_std' ) ) self.assertTrue(hasattr(snake_case , 'do_normalize' ) ) self.assertTrue(hasattr(snake_case , 'do_resize' ) ) self.assertTrue(hasattr(snake_case , 'size' ) ) self.assertTrue(hasattr(snake_case , 'ignore_index' ) ) self.assertTrue(hasattr(snake_case , 'class_info_file' ) ) self.assertTrue(hasattr(snake_case , 'num_text' ) ) self.assertTrue(hasattr(snake_case , 'repo_path' ) ) self.assertTrue(hasattr(snake_case , 'metadata' ) ) self.assertTrue(hasattr(snake_case , 'do_reduce_labels' ) ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] ) -> Optional[int]: """simple docstring""" pass def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ) -> Tuple: """simple docstring""" UpperCamelCase_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images UpperCamelCase_ : Dict = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case , Image.Image ) # Test not batched input UpperCamelCase_ : Optional[Any] = image_processor(image_inputs[0] , ['semantic'] , return_tensors='pt' ).pixel_values UpperCamelCase_, UpperCamelCase_ : Tuple = self.image_processing_tester.get_expected_values(snake_case ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched UpperCamelCase_, UpperCamelCase_ : int = self.image_processing_tester.get_expected_values(snake_case , batched=snake_case ) UpperCamelCase_ : Any = image_processor( snake_case , ['semantic'] * len(snake_case ) , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] ) -> int: """simple docstring""" UpperCamelCase_ : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors UpperCamelCase_ : Optional[int] = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case , numpify=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case , np.ndarray ) # Test not batched input UpperCamelCase_ : str = image_processor(image_inputs[0] , ['semantic'] , return_tensors='pt' ).pixel_values UpperCamelCase_, UpperCamelCase_ : Union[str, Any] = self.image_processing_tester.get_expected_values(snake_case ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched UpperCamelCase_, UpperCamelCase_ : List[Any] = self.image_processing_tester.get_expected_values(snake_case , batched=snake_case ) UpperCamelCase_ : Union[str, Any] = image_processor( snake_case , ['semantic'] * len(snake_case ) , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def SCREAMING_SNAKE_CASE__ ( self : str ) -> Dict: """simple docstring""" UpperCamelCase_ : Dict = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors UpperCamelCase_ : Tuple = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case , torchify=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case , torch.Tensor ) # Test not batched input UpperCamelCase_ : Union[str, Any] = image_processor(image_inputs[0] , ['semantic'] , return_tensors='pt' ).pixel_values UpperCamelCase_, UpperCamelCase_ : List[Any] = self.image_processing_tester.get_expected_values(snake_case ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched UpperCamelCase_, UpperCamelCase_ : Dict = self.image_processing_tester.get_expected_values(snake_case , batched=snake_case ) UpperCamelCase_ : Union[str, Any] = image_processor( snake_case , ['semantic'] * len(snake_case ) , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def SCREAMING_SNAKE_CASE__ ( self : Any , snake_case : Tuple=False , snake_case : int=False , snake_case : List[Any]="np" ) -> Union[str, Any]: """simple docstring""" UpperCamelCase_ : List[str] = self.image_processing_class(**self.image_processor_dict ) # prepare image and target UpperCamelCase_ : Optional[int] = self.image_processing_tester.num_labels UpperCamelCase_ : List[str] = None UpperCamelCase_ : Union[str, Any] = None UpperCamelCase_ : Any = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case ) if with_segmentation_maps: UpperCamelCase_ : Any = num_labels if is_instance_map: UpperCamelCase_ : Tuple = list(range(snake_case ) ) * 2 UpperCamelCase_ : Tuple = dict(enumerate(snake_case ) ) UpperCamelCase_ : Optional[int] = [ np.random.randint(0 , high * 2 , (img.size[1], img.size[0]) ).astype(np.uinta ) for img in image_inputs ] if segmentation_type == "pil": UpperCamelCase_ : Optional[int] = [Image.fromarray(snake_case ) for annotation in annotations] UpperCamelCase_ : Optional[int] = image_processor( snake_case , ['semantic'] * len(snake_case ) , snake_case , return_tensors='pt' , instance_id_to_semantic_id=snake_case , pad_and_return_pixel_mask=snake_case , ) return inputs def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ) -> Optional[Any]: """simple docstring""" pass def SCREAMING_SNAKE_CASE__ ( self : int ) -> int: """simple docstring""" def common(snake_case : Tuple=False , snake_case : Tuple=None ): UpperCamelCase_ : Tuple = self.comm_get_image_processor_inputs( with_segmentation_maps=snake_case , is_instance_map=snake_case , segmentation_type=snake_case ) UpperCamelCase_ : Dict = inputs['mask_labels'] UpperCamelCase_ : int = inputs['class_labels'] UpperCamelCase_ : Union[str, Any] = inputs['pixel_values'] UpperCamelCase_ : List[str] = inputs['text_inputs'] # check the batch_size for mask_label, class_label, text_input in zip(snake_case , snake_case , snake_case ): self.assertEqual(mask_label.shape[0] , class_label.shape[0] ) # this ensure padding has happened self.assertEqual(mask_label.shape[1:] , pixel_values.shape[2:] ) self.assertEqual(len(snake_case ) , self.image_processing_tester.num_text ) common() common(is_instance_map=snake_case ) common(is_instance_map=snake_case , segmentation_type='pil' ) common(is_instance_map=snake_case , segmentation_type='pil' ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ) -> Tuple: """simple docstring""" UpperCamelCase_ : List[Any] = np.zeros((2_0, 5_0) ) UpperCamelCase_ : Dict = 1 UpperCamelCase_ : Tuple = 1 UpperCamelCase_ : Tuple = 1 UpperCamelCase_ : Tuple = binary_mask_to_rle(snake_case ) self.assertEqual(len(snake_case ) , 4 ) self.assertEqual(rle[0] , 2_1 ) self.assertEqual(rle[1] , 4_5 ) def SCREAMING_SNAKE_CASE__ ( self : str ) -> Optional[int]: """simple docstring""" UpperCamelCase_ : Any = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=7_7 , task_seq_length=7_7 , class_info_file='ade20k_panoptic.json' , num_text=self.image_processing_tester.num_text , repo_path='shi-labs/oneformer_demo' , ) UpperCamelCase_ : Tuple = self.image_processing_tester.get_fake_oneformer_outputs() UpperCamelCase_ : List[Any] = fature_extractor.post_process_semantic_segmentation(snake_case ) self.assertEqual(len(snake_case ) , self.image_processing_tester.batch_size ) self.assertEqual( segmentation[0].shape , ( self.image_processing_tester.height, self.image_processing_tester.width, ) , ) UpperCamelCase_ : str = [(1, 4) for i in range(self.image_processing_tester.batch_size )] UpperCamelCase_ : Optional[int] = fature_extractor.post_process_semantic_segmentation(snake_case , target_sizes=snake_case ) self.assertEqual(segmentation[0].shape , target_sizes[0] ) def SCREAMING_SNAKE_CASE__ ( self : str ) -> List[str]: """simple docstring""" UpperCamelCase_ : Tuple = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=7_7 , task_seq_length=7_7 , class_info_file='ade20k_panoptic.json' , num_text=self.image_processing_tester.num_text , repo_path='shi-labs/oneformer_demo' , ) UpperCamelCase_ : Any = self.image_processing_tester.get_fake_oneformer_outputs() UpperCamelCase_ : Union[str, Any] = image_processor.post_process_instance_segmentation(snake_case , threshold=0 ) self.assertTrue(len(snake_case ) == self.image_processing_tester.batch_size ) for el in segmentation: self.assertTrue('segmentation' in el ) self.assertTrue('segments_info' in el ) self.assertEqual(type(el['segments_info'] ) , snake_case ) self.assertEqual( el['segmentation'].shape , (self.image_processing_tester.height, self.image_processing_tester.width) ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] ) -> Optional[Any]: """simple docstring""" UpperCamelCase_ : List[Any] = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=7_7 , task_seq_length=7_7 , class_info_file='ade20k_panoptic.json' , num_text=self.image_processing_tester.num_text , repo_path='shi-labs/oneformer_demo' , ) UpperCamelCase_ : Any = self.image_processing_tester.get_fake_oneformer_outputs() UpperCamelCase_ : List[Any] = image_processor.post_process_panoptic_segmentation(snake_case , threshold=0 ) self.assertTrue(len(snake_case ) == self.image_processing_tester.batch_size ) for el in segmentation: self.assertTrue('segmentation' in el ) self.assertTrue('segments_info' in el ) self.assertEqual(type(el['segments_info'] ) , snake_case ) self.assertEqual( el['segmentation'].shape , (self.image_processing_tester.height, self.image_processing_tester.width) )
50
import copy import tempfile import unittest from transformers import MaMaaaConfig, is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device from transformers.utils import cached_property from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MaMaaaForConditionalGeneration, MaMaaaModel, MaMaaaTokenizer from transformers.models.mam_aaa.modeling_mam_aaa import MaMaaaDecoder, MaMaaaEncoder def __lowercase ( lowerCamelCase : str , lowerCamelCase : Tuple , lowerCamelCase : Optional[int] , lowerCamelCase : str=None , lowerCamelCase : Dict=None , lowerCamelCase : Optional[int]=None , lowerCamelCase : Dict=None , lowerCamelCase : Optional[int]=None , ): if attention_mask is None: UpperCamelCase_ : int = input_ids.ne(config.pad_token_id ) if decoder_attention_mask is None: UpperCamelCase_ : Dict = decoder_input_ids.ne(config.pad_token_id ) if head_mask is None: UpperCamelCase_ : Optional[Any] = torch.ones(config.encoder_layers , config.encoder_attention_heads , device=lowerCamelCase ) if decoder_head_mask is None: UpperCamelCase_ : int = torch.ones(config.decoder_layers , config.decoder_attention_heads , device=lowerCamelCase ) if cross_attn_head_mask is None: UpperCamelCase_ : Tuple = torch.ones(config.decoder_layers , config.decoder_attention_heads , device=lowerCamelCase ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } class _lowercase : def __init__( self : Union[str, Any] , snake_case : str , snake_case : str=1_3 , snake_case : Optional[int]=7 , snake_case : int=True , snake_case : str=False , snake_case : str=9_9 , snake_case : int=1_6 , snake_case : str=2 , snake_case : Dict=4 , snake_case : Tuple=4 , snake_case : List[Any]="relu" , snake_case : str=0.1 , snake_case : Any=0.1 , snake_case : List[str]=0.0 , snake_case : int=0.0 , snake_case : Any=2_0 , snake_case : Union[str, Any]=2 , snake_case : Tuple=1 , snake_case : Optional[int]=0 , ) -> int: """simple docstring""" UpperCamelCase_ : Tuple = parent UpperCamelCase_ : Optional[Any] = batch_size UpperCamelCase_ : Tuple = seq_length UpperCamelCase_ : Dict = is_training UpperCamelCase_ : Tuple = use_labels UpperCamelCase_ : Tuple = vocab_size UpperCamelCase_ : List[str] = hidden_size UpperCamelCase_ : List[str] = num_hidden_layers UpperCamelCase_ : Tuple = num_attention_heads UpperCamelCase_ : Dict = intermediate_size UpperCamelCase_ : Dict = hidden_act UpperCamelCase_ : int = hidden_dropout_prob UpperCamelCase_ : str = attention_probs_dropout_prob UpperCamelCase_ : List[Any] = encoder_layerdrop UpperCamelCase_ : Any = decoder_layerdrop UpperCamelCase_ : Tuple = max_position_embeddings UpperCamelCase_ : Dict = eos_token_id UpperCamelCase_ : int = pad_token_id UpperCamelCase_ : str = bos_token_id def SCREAMING_SNAKE_CASE__ ( self : str ) -> Optional[int]: """simple docstring""" UpperCamelCase_ : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCamelCase_ : Any = self.eos_token_id # Eos Token UpperCamelCase_ : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) # we need to clamp the input ids here to avoid having pad token in between # this is because for M2M100 the position_ids are prepared such that # all pad tokens have pos id = 2 and rest are between 2..seq_length # and the seq_length here is seq_length - num_pad_tokens # but when using past, there is no way of knowing if the past input ids had # pad tokens in them, which results in incorrect seq_lenth and which in turn results in # position_ids being off by num_pad_tokens in past input UpperCamelCase_ : str = input_ids.clamp(self.pad_token_id + 1 ) UpperCamelCase_ : List[Any] = decoder_input_ids.clamp(self.pad_token_id + 1 ) UpperCamelCase_ : str = self.get_config() UpperCamelCase_ : Any = prepare_mam_aaa_inputs_dict(snake_case , snake_case , snake_case ) return config, inputs_dict def SCREAMING_SNAKE_CASE__ ( self : str ) -> int: """simple docstring""" return MaMaaaConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , encoder_layerdrop=self.encoder_layerdrop , decoder_layerdrop=self.decoder_layerdrop , max_position_embeddings=self.max_position_embeddings , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , ) def SCREAMING_SNAKE_CASE__ ( self : Any ) -> List[str]: """simple docstring""" UpperCamelCase_, UpperCamelCase_ : List[str] = self.prepare_config_and_inputs() return config, inputs_dict def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , snake_case : List[Any] , snake_case : Optional[int] ) -> Dict: """simple docstring""" UpperCamelCase_ : str = MaMaaaModel(config=snake_case ).get_decoder().to(snake_case ).eval() UpperCamelCase_ : str = inputs_dict['input_ids'] UpperCamelCase_ : Any = inputs_dict['attention_mask'] UpperCamelCase_ : Optional[int] = inputs_dict['head_mask'] # first forward pass UpperCamelCase_ : int = model(snake_case , attention_mask=snake_case , head_mask=snake_case , use_cache=snake_case ) UpperCamelCase_, UpperCamelCase_ : int = outputs.to_tuple() # create hypothetical multiple next token and extent to next_input_ids UpperCamelCase_ : int = ids_tensor((self.batch_size, 3) , config.vocab_size ) UpperCamelCase_ : Optional[int] = ids_tensor((self.batch_size, 3) , 2 ) # append to next input_ids and UpperCamelCase_ : Tuple = torch.cat([input_ids, next_tokens] , dim=-1 ) UpperCamelCase_ : Optional[Any] = torch.cat([attention_mask, next_attn_mask] , dim=-1 ) UpperCamelCase_ : Union[str, Any] = model(snake_case , attention_mask=snake_case )['last_hidden_state'] UpperCamelCase_ : int = model(snake_case , attention_mask=snake_case , past_key_values=snake_case )[ 'last_hidden_state' ] # select random slice UpperCamelCase_ : List[str] = ids_tensor((1,) , output_from_past.shape[-1] ).item() UpperCamelCase_ : Tuple = output_from_no_past[:, -3:, random_slice_idx].detach() UpperCamelCase_ : List[str] = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(snake_case , snake_case , atol=1e-2 ) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , snake_case : int , snake_case : str ) -> Dict: """simple docstring""" UpperCamelCase_ : Tuple = MaMaaaModel(config=snake_case ).to(snake_case ).eval() UpperCamelCase_ : List[str] = model(**snake_case ) UpperCamelCase_ : List[Any] = outputs.encoder_last_hidden_state UpperCamelCase_ : Optional[int] = outputs.last_hidden_state with tempfile.TemporaryDirectory() as tmpdirname: UpperCamelCase_ : Optional[int] = model.get_encoder() encoder.save_pretrained(snake_case ) UpperCamelCase_ : Tuple = MaMaaaEncoder.from_pretrained(snake_case ).to(snake_case ) UpperCamelCase_ : Optional[Any] = encoder(inputs_dict['input_ids'] , attention_mask=inputs_dict['attention_mask'] )[ 0 ] self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1e-3 ) with tempfile.TemporaryDirectory() as tmpdirname: UpperCamelCase_ : int = model.get_decoder() decoder.save_pretrained(snake_case ) UpperCamelCase_ : int = MaMaaaDecoder.from_pretrained(snake_case ).to(snake_case ) UpperCamelCase_ : int = decoder( input_ids=inputs_dict['decoder_input_ids'] , attention_mask=inputs_dict['decoder_attention_mask'] , encoder_hidden_states=snake_case , encoder_attention_mask=inputs_dict['attention_mask'] , )[0] self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1e-3 ) @require_torch class _lowercase ( snake_case_ , snake_case_ , snake_case_ , unittest.TestCase ): lowercase = ( ( MaMaaaModel, MaMaaaForConditionalGeneration, ) if is_torch_available() else () ) lowercase = (MaMaaaForConditionalGeneration,) if is_torch_available() else () lowercase = ( { 'conversational': MaMaaaForConditionalGeneration, 'feature-extraction': MaMaaaModel, 'summarization': MaMaaaForConditionalGeneration, 'text2text-generation': MaMaaaForConditionalGeneration, 'translation': MaMaaaForConditionalGeneration, } if is_torch_available() else {} ) lowercase = True lowercase = True lowercase = False lowercase = False def SCREAMING_SNAKE_CASE__ ( self : List[Any] , snake_case : Union[str, Any] , snake_case : List[Any] , snake_case : str , snake_case : str , snake_case : Dict ) -> List[Any]: """simple docstring""" if pipeline_test_casse_name == "TranslationPipelineTests": # Get `ValueError: Translation requires a `src_lang` and a `tgt_lang` for this model`. # `M2M100Config` was never used in pipeline tests: cannot create a simple tokenizer. return True return False def SCREAMING_SNAKE_CASE__ ( self : Dict ) -> Optional[int]: """simple docstring""" UpperCamelCase_ : Tuple = MaMaaaModelTester(self ) UpperCamelCase_ : Optional[Any] = ConfigTester(self , config_class=snake_case ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ) -> Union[str, Any]: """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ) -> Union[str, Any]: """simple docstring""" UpperCamelCase_, UpperCamelCase_ : str = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: UpperCamelCase_ : int = model_class(snake_case ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(snake_case ) UpperCamelCase_, UpperCamelCase_ : str = model_class.from_pretrained(snake_case , output_loading_info=snake_case ) self.assertEqual(info['missing_keys'] , [] ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ) -> Tuple: """simple docstring""" UpperCamelCase_ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(*snake_case ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ) -> List[str]: """simple docstring""" UpperCamelCase_ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_encoder_decoder_model_standalone(*snake_case ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ) -> Tuple: """simple docstring""" UpperCamelCase_, UpperCamelCase_ : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in (MaMaaaModel, MaMaaaForConditionalGeneration): UpperCamelCase_ : Optional[Any] = model_class(snake_case ) model.to(snake_case ) model.eval() UpperCamelCase_ : List[Any] = copy.deepcopy(self._prepare_for_class(snake_case , snake_case ) ) if not self.is_encoder_decoder: UpperCamelCase_ : List[Any] = inputs['input_ids'] del inputs["input_ids"] else: UpperCamelCase_ : str = inputs['input_ids'] UpperCamelCase_ : List[str] = inputs.get('decoder_input_ids' , snake_case ) del inputs["input_ids"] inputs.pop('decoder_input_ids' , snake_case ) UpperCamelCase_ : List[str] = model.get_input_embeddings() if not self.is_encoder_decoder: UpperCamelCase_ : Tuple = wte(snake_case ) else: UpperCamelCase_ : Optional[int] = wte(snake_case ) UpperCamelCase_ : Optional[int] = wte(snake_case ) with torch.no_grad(): model(**snake_case )[0] def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ) -> List[str]: """simple docstring""" UpperCamelCase_, UpperCamelCase_ : int = self.model_tester.prepare_config_and_inputs() UpperCamelCase_ : str = input_dict['input_ids'] UpperCamelCase_ : int = input_ids.ne(1 ).to(snake_case ) UpperCamelCase_ : Dict = MaMaaaForConditionalGeneration(snake_case ).eval().to(snake_case ) if torch_device == "cuda": model.half() model.generate(snake_case , attention_mask=snake_case ) model.generate(num_beams=4 , do_sample=snake_case , early_stopping=snake_case , num_return_sequences=3 ) def __lowercase ( lowerCamelCase : List[Any] ): return torch.tensor(lowerCamelCase , dtype=torch.long , device=lowerCamelCase ) a_ = 1E-4 @require_torch @require_sentencepiece @require_tokenizers @slow class _lowercase ( unittest.TestCase ): @cached_property def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ) -> int: """simple docstring""" return MaMaaaTokenizer.from_pretrained('facebook/m2m100_418M' ) def SCREAMING_SNAKE_CASE__ ( self : str ) -> Union[str, Any]: """simple docstring""" UpperCamelCase_ : Union[str, Any] = MaMaaaModel.from_pretrained('facebook/m2m100_418M' ).to(snake_case ) UpperCamelCase_ : str = _long_tensor([[1_2_8_0_2_8, 9_8, 1_2, 3_0_5_2_7, 2_7_3_2, 1_5_9, 7_7_5_5, 6_1_9_0_4, 3_9_1_4_4, 3_8, 2]] ) UpperCamelCase_ : Dict = _long_tensor([[2, 1_2_8_0_2_8, 9_8, 1_2, 3_0_5_2_7, 2_7_3_2, 1_5_9, 7_7_5_5, 6_1_9_0_4, 3_9_1_4_4, 3_8]] ) UpperCamelCase_ : Optional[int] = prepare_mam_aaa_inputs_dict(model.config , snake_case , snake_case ) with torch.no_grad(): UpperCamelCase_ : Any = model(**snake_case )[0] UpperCamelCase_ : Tuple = torch.Size((1, 1_1, 1_0_2_4) ) self.assertEqual(output.shape , snake_case ) # change to expected output here UpperCamelCase_ : Dict = torch.tensor( [[-0.7780, -0.1676, 0.1038], [-6.7556, -1.3992, 0.0567], [-7.5383, -0.5920, -0.2779]] , device=snake_case ) self.assertTrue(torch.allclose(output[:, :3, :3] , snake_case , atol=snake_case ) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ) -> str: """simple docstring""" UpperCamelCase_ : List[str] = MaMaaaForConditionalGeneration.from_pretrained('facebook/m2m100_418M' ).to(snake_case ) # change to intended input UpperCamelCase_ : Tuple = _long_tensor([[1_2_8_0_2_8, 9_8, 1_2, 3_0_5_2_7, 2_7_3_2, 1_5_9, 7_7_5_5, 6_1_9_0_4, 3_9_1_4_4, 3_8, 2]] ) UpperCamelCase_ : Union[str, Any] = _long_tensor([[2, 1_2_8_0_2_8, 9_8, 1_2, 3_0_5_2_7, 2_7_3_2, 1_5_9, 7_7_5_5, 6_1_9_0_4, 3_9_1_4_4, 3_8]] ) UpperCamelCase_ : Dict = prepare_mam_aaa_inputs_dict(model.config , snake_case , snake_case ) with torch.no_grad(): UpperCamelCase_ : Dict = model(**snake_case )[0] UpperCamelCase_ : Union[str, Any] = torch.Size((1, 1_1, model.config.vocab_size) ) self.assertEqual(output.shape , snake_case ) # change to expected output here UpperCamelCase_ : Any = torch.tensor( [[-1.0448, -1.0411, 3.7992], [-3.2191, -3.2386, -1.3451], [-3.6210, -3.5993, 0.4925]] , device=snake_case ) self.assertTrue(torch.allclose(output[:, :3, :3] , snake_case , atol=snake_case ) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ) -> int: """simple docstring""" UpperCamelCase_ : str = MaMaaaForConditionalGeneration.from_pretrained('facebook/m2m100_418M' ).to(snake_case ) UpperCamelCase_ : Optional[int] = MaMaaaTokenizer.from_pretrained('facebook/m2m100_418M' , src_lang='fr' , tgt_lang='en' ) UpperCamelCase_ : Union[str, Any] = [ 'L\'affaire NSA souligne l\'absence totale de débat sur le renseignement', 'Selon moi, il y a deux niveaux de réponse de la part du gouvernement français.', 'Lorsque François Hollande téléphone à Barack Obama ou quand le ministre des affaires étrangères Laurent' ' Fabius convoque l\'ambassadeur des Etats-Unis, ils réagissent à une vraie découverte, qui est celle de' ' l\'ampleur de la surveillance américaine sur l\'ensemble des communications en France.', ] # The below article tests that we don't add any hypotheses outside of the top n_beams UpperCamelCase_ : Optional[Any] = tokenizer(snake_case , padding=snake_case , return_tensors='pt' ) UpperCamelCase_ : Dict = model.generate( input_ids=dct['input_ids'].to(snake_case ) , attention_mask=dct['attention_mask'].to(snake_case ) , num_beams=5 , forced_bos_token_id=tokenizer.get_lang_id('en' ) , ) UpperCamelCase_ : Optional[int] = [ 'The NSA case highlights the total absence of intelligence debate', 'I think there are two levels of response from the French government.', 'When François Hollande calls Barack Obama or when Foreign Minister Laurent Fabius calls the U.S.' ' Ambassador, they respond to a real discovery, which is that of the scale of U.S. surveillance on all' ' communications in France.', ] UpperCamelCase_ : List[str] = tokenizer.batch_decode( hypotheses_batch.tolist() , clean_up_tokenization_spaces=snake_case , skip_special_tokens=snake_case ) assert generated == expected_en
50
1
from typing import List, Optional, Tuple, Union import PIL import torch from torchvision import transforms from diffusers.pipeline_utils import DiffusionPipeline, ImagePipelineOutput from diffusers.schedulers import DDIMScheduler from diffusers.utils import randn_tensor a_ = transforms.Compose( [ transforms.Resize((256, 256)), transforms.ToTensor(), transforms.Normalize([0.5], [0.5]), ] ) def lowerCamelCase__ ( _a): if isinstance(__UpperCAmelCase , torch.Tensor): return image elif isinstance(__UpperCAmelCase , PIL.Image.Image): SCREAMING_SNAKE_CASE : List[Any] = [image] SCREAMING_SNAKE_CASE : Optional[int] = [trans(img.convert("RGB")) for img in image] SCREAMING_SNAKE_CASE : List[str] = torch.stack(__UpperCAmelCase) return image class _UpperCamelCase ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : List[Any] , a : Optional[Any] , a : int ) -> Optional[int]: """simple docstring""" super().__init__() # make sure scheduler can always be converted to DDIM SCREAMING_SNAKE_CASE : str = DDIMScheduler.from_config(scheduler.config ) self.register_modules(unet=_lowerCAmelCase , scheduler=_lowerCAmelCase ) def __UpperCamelCase ( self : Union[str, Any] , a : Tuple ) -> Dict: """simple docstring""" if strength < 0 or strength > 1: raise ValueError(F"The value of strength should in [0.0, 1.0] but is {strength}" ) def __UpperCamelCase ( self : Tuple , a : List[Any] , a : List[Any] , a : Tuple ) -> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE : str = min(int(num_inference_steps * strength ) , _lowerCAmelCase ) SCREAMING_SNAKE_CASE : int = max(num_inference_steps - init_timestep , 0 ) SCREAMING_SNAKE_CASE : List[str] = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def __UpperCamelCase ( self : Dict , a : Union[str, Any] , a : Tuple , a : Any , a : Dict , a : List[Any] , a : Any=None ) -> Union[str, Any]: """simple docstring""" if not isinstance(_lowerCAmelCase , (torch.Tensor, PIL.Image.Image, list) ): raise ValueError( F"`image` has to be of type `torch.Tensor`, `PIL.Image.Image` or list but is {type(_lowerCAmelCase )}" ) SCREAMING_SNAKE_CASE : Optional[int] = image.to(device=_lowerCAmelCase , dtype=_lowerCAmelCase ) if isinstance(_lowerCAmelCase , _lowerCAmelCase ) and len(_lowerCAmelCase ) != batch_size: raise ValueError( F"You have passed a list of generators of length {len(_lowerCAmelCase )}, but requested an effective batch" F" size of {batch_size}. Make sure the batch size matches the length of the generators." ) SCREAMING_SNAKE_CASE : str = init_latents.shape SCREAMING_SNAKE_CASE : Dict = randn_tensor(_lowerCAmelCase , generator=_lowerCAmelCase , device=_lowerCAmelCase , dtype=_lowerCAmelCase ) # get latents print("add noise to latents at timestep" , _lowerCAmelCase ) SCREAMING_SNAKE_CASE : str = self.scheduler.add_noise(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE : Optional[Any] = init_latents return latents @torch.no_grad() def __call__( self : str , a : Union[torch.FloatTensor, PIL.Image.Image] = None , a : float = 0.8 , a : int = 1 , a : Optional[Union[torch.Generator, List[torch.Generator]]] = None , a : float = 0.0 , a : int = 50 , a : Optional[bool] = None , a : Optional[str] = "pil" , a : bool = True , ) -> List[str]: """simple docstring""" self.check_inputs(_lowerCAmelCase ) # 2. Preprocess image SCREAMING_SNAKE_CASE : List[Any] = preprocess(_lowerCAmelCase ) # 3. set timesteps self.scheduler.set_timesteps(_lowerCAmelCase , device=self.device ) SCREAMING_SNAKE_CASE ,SCREAMING_SNAKE_CASE : List[str] = self.get_timesteps(_lowerCAmelCase , _lowerCAmelCase , self.device ) SCREAMING_SNAKE_CASE : Any = timesteps[:1].repeat(_lowerCAmelCase ) # 4. Prepare latent variables SCREAMING_SNAKE_CASE : int = self.prepare_latents(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , self.unet.dtype , self.device , _lowerCAmelCase ) SCREAMING_SNAKE_CASE : Tuple = latents # 5. Denoising loop for t in self.progress_bar(_lowerCAmelCase ): # 1. predict noise model_output SCREAMING_SNAKE_CASE : List[str] = self.unet(_lowerCAmelCase , _lowerCAmelCase ).sample # 2. predict previous mean of image x_t-1 and add variance depending on eta # eta corresponds to η in paper and should be between [0, 1] # do x_t -> x_t-1 SCREAMING_SNAKE_CASE : List[Any] = self.scheduler.step( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , eta=_lowerCAmelCase , use_clipped_model_output=_lowerCAmelCase , generator=_lowerCAmelCase , ).prev_sample SCREAMING_SNAKE_CASE : Optional[int] = (image / 2 + 0.5).clamp(0 , 1 ) SCREAMING_SNAKE_CASE : Union[str, Any] = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": SCREAMING_SNAKE_CASE : Union[str, Any] = self.numpy_to_pil(_lowerCAmelCase ) if not return_dict: return (image, latent_timestep.item()) return ImagePipelineOutput(images=_lowerCAmelCase )
76
import sys import webbrowser import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": print('Googling.....') lowerCamelCase__ : List[Any] = 'https://www.google.com/search?q=' + ' '.join(sys.argv[1:]) lowerCamelCase__ : List[Any] = requests.get(url, headers={'UserAgent': UserAgent().random}) # res.raise_for_status() with open('project1a.html', 'wb') as out_file: # only for knowing the class for data in res.iter_content(10_000): out_file.write(data) lowerCamelCase__ : int = BeautifulSoup(res.text, 'html.parser') lowerCamelCase__ : List[str] = list(soup.select('.eZt8xd'))[:5] print(len(links)) for link in links: if link.text == "Maps": webbrowser.open(link.get('href')) else: webbrowser.open(f'''https://google.com{link.get('href')}''')
225
0
import contextlib import faulthandler import io import multiprocessing import os import platform import signal import tempfile def __snake_case ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): __a = multiprocessing.Manager() __a = manager.list() __a = multiprocessing.Process(target=lowercase__ , args=(check_program, result, timeout) ) p.start() p.join(timeout=timeout + 1 ) if p.is_alive(): p.kill() if not result: result.append('''timed out''' ) return { "task_id": task_id, "passed": result[0] == "passed", "result": result[0], "completion_id": completion_id, } def __snake_case ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil __a = shutil.rmtree __a = os.rmdir __a = os.chdir # Disable functionalities that can make destructive changes to the test. reliability_guard() # Run program. try: __a = {} with swallow_io(): with time_limit(lowercase__ ): exec(lowercase__ , lowercase__ ) result.append('''passed''' ) except TimeoutException: result.append('''timed out''' ) except BaseException as e: result.append(f'failed: {e}' ) # Needed for cleaning up. __a = rmtree __a = rmdir __a = chdir @contextlib.contextmanager def __snake_case ( _UpperCAmelCase ): def signal_handler(_UpperCAmelCase , _UpperCAmelCase ): raise TimeoutException('''Timed out!''' ) signal.setitimer(signal.ITIMER_REAL , lowercase__ ) signal.signal(signal.SIGALRM , lowercase__ ) try: yield finally: signal.setitimer(signal.ITIMER_REAL , 0 ) @contextlib.contextmanager def __snake_case ( ): __a = WriteOnlyStringIO() with contextlib.redirect_stdout(lowercase__ ): with contextlib.redirect_stderr(lowercase__ ): with redirect_stdin(lowercase__ ): yield @contextlib.contextmanager def __snake_case ( ): with tempfile.TemporaryDirectory() as dirname: with chdir(lowercase__ ): yield dirname class _A ( _A ): pass class _A ( io.StringIO ): def _lowerCamelCase ( self : List[Any] , *__SCREAMING_SNAKE_CASE : Tuple , **__SCREAMING_SNAKE_CASE : List[Any]): '''simple docstring''' raise OSError def _lowerCamelCase ( self : Union[str, Any] , *__SCREAMING_SNAKE_CASE : int , **__SCREAMING_SNAKE_CASE : Tuple): '''simple docstring''' raise OSError def _lowerCamelCase ( self : Dict , *__SCREAMING_SNAKE_CASE : Any , **__SCREAMING_SNAKE_CASE : Union[str, Any]): '''simple docstring''' raise OSError def _lowerCamelCase ( self : Any , *__SCREAMING_SNAKE_CASE : int , **__SCREAMING_SNAKE_CASE : List[Any]): '''simple docstring''' return False class _A ( contextlib._RedirectStream ): # type: ignore UpperCamelCase__ : List[str] = '''stdin''' @contextlib.contextmanager def __snake_case ( _UpperCAmelCase ): if root == ".": yield return __a = os.getcwd() os.chdir(lowercase__ ) try: yield except BaseException as exc: raise exc finally: os.chdir(lowercase__ ) def __snake_case ( _UpperCAmelCase=None ): if maximum_memory_bytes is not None: import resource resource.setrlimit(resource.RLIMIT_AS , (maximum_memory_bytes, maximum_memory_bytes) ) resource.setrlimit(resource.RLIMIT_DATA , (maximum_memory_bytes, maximum_memory_bytes) ) if not platform.uname().system == "Darwin": resource.setrlimit(resource.RLIMIT_STACK , (maximum_memory_bytes, maximum_memory_bytes) ) faulthandler.disable() import builtins __a = None __a = None import os __a = '''1''' __a = None __a = None __a = None __a = None __a = None __a = None __a = None __a = None __a = None __a = None __a = None __a = None __a = None __a = None __a = None __a = None __a = None __a = None __a = None __a = None __a = None __a = None __a = None __a = None __a = None __a = None __a = None import shutil __a = None __a = None __a = None import subprocess __a = None # type: ignore __a = None import sys __a = None __a = None __a = None __a = None __a = None
363
def __snake_case ( _UpperCAmelCase , _UpperCAmelCase ): return base * power(_UpperCAmelCase , (exponent - 1) ) if exponent else 1 if __name__ == "__main__": print('''Raise base to the power of exponent using recursion...''') __snake_case :List[Any] = int(input('''Enter the base: ''').strip()) __snake_case :Dict = int(input('''Enter the exponent: ''').strip()) __snake_case :int = power(base, abs(exponent)) if exponent < 0: # power() does not properly deal w/ negative exponents __snake_case :Optional[Any] = 1 / result print(f'{base} to the power of {exponent} is {result}')
131
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging UpperCAmelCase_ : Dict = logging.get_logger(__name__) UpperCAmelCase_ : Any = { '''kssteven/ibert-roberta-base''': '''https://huggingface.co/kssteven/ibert-roberta-base/resolve/main/config.json''', '''kssteven/ibert-roberta-large''': '''https://huggingface.co/kssteven/ibert-roberta-large/resolve/main/config.json''', '''kssteven/ibert-roberta-large-mnli''': ( '''https://huggingface.co/kssteven/ibert-roberta-large-mnli/resolve/main/config.json''' ), } class _SCREAMING_SNAKE_CASE ( _a ): snake_case__ : Dict = """ibert""" def __init__( self : Dict , __lowerCamelCase : Dict=30_522 , __lowerCamelCase : Optional[Any]=768 , __lowerCamelCase : List[Any]=12 , __lowerCamelCase : Tuple=12 , __lowerCamelCase : int=3_072 , __lowerCamelCase : Union[str, Any]="gelu" , __lowerCamelCase : List[str]=0.1 , __lowerCamelCase : str=0.1 , __lowerCamelCase : str=512 , __lowerCamelCase : List[str]=2 , __lowerCamelCase : int=0.02 , __lowerCamelCase : int=1E-12 , __lowerCamelCase : Optional[Any]=1 , __lowerCamelCase : List[Any]=0 , __lowerCamelCase : Tuple=2 , __lowerCamelCase : int="absolute" , __lowerCamelCase : int=False , __lowerCamelCase : Union[str, Any]="none" , **__lowerCamelCase : Union[str, Any] , ): super().__init__(pad_token_id=__lowerCamelCase , bos_token_id=__lowerCamelCase , eos_token_id=__lowerCamelCase , **__lowerCamelCase ) UpperCamelCase :List[Any] = vocab_size UpperCamelCase :Optional[int] = hidden_size UpperCamelCase :List[Any] = num_hidden_layers UpperCamelCase :Optional[int] = num_attention_heads UpperCamelCase :List[str] = hidden_act UpperCamelCase :Tuple = intermediate_size UpperCamelCase :Any = hidden_dropout_prob UpperCamelCase :Union[str, Any] = attention_probs_dropout_prob UpperCamelCase :Any = max_position_embeddings UpperCamelCase :int = type_vocab_size UpperCamelCase :List[str] = initializer_range UpperCamelCase :str = layer_norm_eps UpperCamelCase :int = position_embedding_type UpperCamelCase :Optional[Any] = quant_mode UpperCamelCase :int = force_dequant class _SCREAMING_SNAKE_CASE ( _a ): @property def _A ( self : int ): if self.task == "multiple-choice": UpperCamelCase :Union[str, Any] = {0: """batch""", 1: """choice""", 2: """sequence"""} else: UpperCamelCase :int = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
38
from __future__ import annotations import numpy as np from numpy import floataa from numpy.typing import NDArray def __snake_case ( __UpperCamelCase : NDArray[floataa] ,__UpperCamelCase : NDArray[floataa] ,__UpperCamelCase : list[int] ,__UpperCamelCase : int ,): """simple docstring""" A_ , A_ = coefficient_matrix.shape A_ , A_ = constant_matrix.shape if rowsa != colsa: A_ = f'''Coefficient matrix dimensions must be nxn but received {rowsa}x{colsa}''' raise ValueError(__UpperCamelCase ) if colsa != 1: A_ = f'''Constant matrix must be nx1 but received {rowsa}x{colsa}''' raise ValueError(__UpperCamelCase ) if rowsa != rowsa: A_ = ( "Coefficient and constant matrices dimensions must be nxn and nx1 but " f'''received {rowsa}x{colsa} and {rowsa}x{colsa}''' ) raise ValueError(__UpperCamelCase ) if len(__UpperCamelCase ) != rowsa: A_ = ( "Number of initial values must be equal to number of rows in coefficient " f'''matrix but received {len(__UpperCamelCase )} and {rowsa}''' ) raise ValueError(__UpperCamelCase ) if iterations <= 0: raise ValueError("Iterations must be at least 1" ) A_ = np.concatenate( (coefficient_matrix, constant_matrix) ,axis=1 ) A_ , A_ = table.shape strictly_diagonally_dominant(__UpperCamelCase ) # Iterates the whole matrix for given number of times for _ in range(__UpperCamelCase ): A_ = [] for row in range(__UpperCamelCase ): A_ = 0 for col in range(__UpperCamelCase ): if col == row: A_ = table[row][col] elif col == cols - 1: A_ = table[row][col] else: temp += (-1) * table[row][col] * init_val[col] A_ = (temp + val) / denom new_val.append(__UpperCamelCase ) A_ = new_val return [float(__UpperCamelCase ) for i in new_val] def __snake_case ( __UpperCamelCase : NDArray[floataa] ): """simple docstring""" A_ , A_ = table.shape A_ = True for i in range(0 ,__UpperCamelCase ): A_ = 0 for j in range(0 ,cols - 1 ): if i == j: continue else: total += table[i][j] if table[i][i] <= total: raise ValueError("Coefficient matrix is not strictly diagonally dominant" ) return is_diagonally_dominant # Test Cases if __name__ == "__main__": import doctest doctest.testmod()
312
0
'''simple docstring''' 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 = None _lowerCAmelCase = logging.get_logger(__name__) _lowerCAmelCase = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} _lowerCAmelCase = { '''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 = { '''t5-small''': 512, '''t5-base''': 512, '''t5-large''': 512, '''t5-3b''': 512, '''t5-11b''': 512, } class _SCREAMING_SNAKE_CASE ( a__ ): __SCREAMING_SNAKE_CASE :Union[str, Any] = VOCAB_FILES_NAMES __SCREAMING_SNAKE_CASE :List[Any] = PRETRAINED_VOCAB_FILES_MAP __SCREAMING_SNAKE_CASE :int = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __SCREAMING_SNAKE_CASE :List[Any] = ["""input_ids""", """attention_mask"""] __SCREAMING_SNAKE_CASE :Dict = TaTokenizer __SCREAMING_SNAKE_CASE :List[int] = [] def __init__( self : Optional[int] , a__ : Tuple=None , a__ : Optional[Any]=None , a__ : Optional[int]="</s>" , a__ : Optional[Any]="<unk>" , a__ : List[str]="<pad>" , a__ : Dict=100 , a__ : List[Any]=None , **a__ : Tuple , ): # Add extra_ids to the special token list if extra_ids > 0 and additional_special_tokens is None: __magic_name__ = [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 __magic_name__ = len(set(filter(lambda a__ : 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__ , ) __magic_name__ = vocab_file __magic_name__ = False if not self.vocab_file else True __magic_name__ = extra_ids @staticmethod def snake_case__ ( a__ : List[Any] , a__ : List[Any] , a__ : List[str] ): if pretrained_model_name_or_path in TaTokenizerFast.max_model_input_sizes: __magic_name__ = 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 snake_case__ ( self : str , a__ : Optional[Any] , a__ : Optional[Any] = 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 __magic_name__ = 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 snake_case__ ( self : Any , a__ : Tuple , a__ : Tuple = None ): __magic_name__ = token_ids_a + [self.eos_token_id] if token_ids_a is None: return self.prefix_tokens + token_ids_a else: __magic_name__ = token_ids_a + [self.eos_token_id] return self.prefix_tokens + token_ids_a + token_ids_a def snake_case__ ( self : List[Any] , a__ : Any , a__ : Any = None ): __magic_name__ = [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 snake_case__ ( self : Optional[Any] ): return list( set(filter(lambda a__ : bool(re.search(r'''<extra_id_\d+>''' , lowerCAmelCase__ ) ) is not None , self.additional_special_tokens ) ) ) def snake_case__ ( self : Optional[Any] ): return [self.convert_tokens_to_ids(lowerCAmelCase__ ) for token in self.get_sentinel_tokens()]
363
'''simple docstring''' import json import sys import tempfile import unittest from pathlib import Path import transformers from transformers import ( CONFIG_MAPPING, FEATURE_EXTRACTOR_MAPPING, AutoConfig, AutoFeatureExtractor, WavaVecaConfig, WavaVecaFeatureExtractor, ) from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir sys.path.append(str(Path(__file__).parent.parent.parent.parent / "utils")) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 _lowerCAmelCase = get_tests_dir("fixtures") _lowerCAmelCase = get_tests_dir("fixtures/dummy_feature_extractor_config.json") _lowerCAmelCase = get_tests_dir("fixtures/dummy-config.json") class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): def snake_case__ ( self : Union[str, Any] ): __magic_name__ = 0 def snake_case__ ( self : Optional[int] ): __magic_name__ = AutoFeatureExtractor.from_pretrained('''facebook/wav2vec2-base-960h''' ) self.assertIsInstance(a__ , a__ ) def snake_case__ ( self : Optional[int] ): __magic_name__ = AutoFeatureExtractor.from_pretrained(a__ ) self.assertIsInstance(a__ , a__ ) def snake_case__ ( self : Optional[Any] ): with tempfile.TemporaryDirectory() as tmpdirname: __magic_name__ = WavaVecaConfig() # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally __magic_name__ = AutoFeatureExtractor.from_pretrained(a__ ).to_dict() config_dict.pop('''feature_extractor_type''' ) __magic_name__ = WavaVecaFeatureExtractor(**a__ ) # save in new folder model_config.save_pretrained(a__ ) config.save_pretrained(a__ ) __magic_name__ = AutoFeatureExtractor.from_pretrained(a__ ) # make sure private variable is not incorrectly saved __magic_name__ = json.loads(config.to_json_string() ) self.assertTrue('''_processor_class''' not in dict_as_saved ) self.assertIsInstance(a__ , a__ ) def snake_case__ ( self : Optional[Any] ): __magic_name__ = AutoFeatureExtractor.from_pretrained(a__ ) self.assertIsInstance(a__ , a__ ) def snake_case__ ( self : str ): with self.assertRaisesRegex( a__ , '''bert-base is not a local folder and is not a valid model identifier''' ): __magic_name__ = AutoFeatureExtractor.from_pretrained('''bert-base''' ) def snake_case__ ( self : str ): with self.assertRaisesRegex( a__ , r'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): __magic_name__ = AutoFeatureExtractor.from_pretrained(a__ , revision='''aaaaaa''' ) def snake_case__ ( self : Union[str, Any] ): with self.assertRaisesRegex( a__ , '''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.''' , ): __magic_name__ = AutoFeatureExtractor.from_pretrained('''hf-internal-testing/config-no-model''' ) def snake_case__ ( self : Dict ): # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(a__ ): __magic_name__ = AutoFeatureExtractor.from_pretrained( '''hf-internal-testing/test_dynamic_feature_extractor''' ) # If remote code is disabled, we can't load this config. with self.assertRaises(a__ ): __magic_name__ = AutoFeatureExtractor.from_pretrained( '''hf-internal-testing/test_dynamic_feature_extractor''' , trust_remote_code=a__ ) __magic_name__ = AutoFeatureExtractor.from_pretrained( '''hf-internal-testing/test_dynamic_feature_extractor''' , trust_remote_code=a__ ) self.assertEqual(feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) # Test feature extractor can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained(a__ ) __magic_name__ = AutoFeatureExtractor.from_pretrained(a__ , trust_remote_code=a__ ) self.assertEqual(reloaded_feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) def snake_case__ ( self : int ): try: AutoConfig.register('''custom''' , a__ ) AutoFeatureExtractor.register(a__ , a__ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(a__ ): AutoFeatureExtractor.register(a__ , a__ ) # Now that the config is registered, it can be used as any other config with the auto-API __magic_name__ = CustomFeatureExtractor.from_pretrained(a__ ) with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained(a__ ) __magic_name__ = AutoFeatureExtractor.from_pretrained(a__ ) self.assertIsInstance(a__ , a__ ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content: del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig] def snake_case__ ( self : int ): class _SCREAMING_SNAKE_CASE ( __a ): __SCREAMING_SNAKE_CASE :Optional[int] = True try: AutoConfig.register('''custom''' , a__ ) AutoFeatureExtractor.register(a__ , a__ ) # If remote code is not set, the default is to use local __magic_name__ = AutoFeatureExtractor.from_pretrained( '''hf-internal-testing/test_dynamic_feature_extractor''' ) self.assertEqual(feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) self.assertTrue(feature_extractor.is_local ) # If remote code is disabled, we load the local one. __magic_name__ = AutoFeatureExtractor.from_pretrained( '''hf-internal-testing/test_dynamic_feature_extractor''' , trust_remote_code=a__ ) self.assertEqual(feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) self.assertTrue(feature_extractor.is_local ) # If remote is enabled, we load from the Hub __magic_name__ = AutoFeatureExtractor.from_pretrained( '''hf-internal-testing/test_dynamic_feature_extractor''' , trust_remote_code=a__ ) self.assertEqual(feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) self.assertTrue(not hasattr(a__ , '''is_local''' ) ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content: del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]
98
0
'''simple docstring''' import copy from ...configuration_utils import PretrainedConfig from ...utils import add_start_docstrings _UpperCamelCase = R'\n [`RagConfig`] stores the configuration of a *RagModel*. Configuration objects inherit from [`PretrainedConfig`] and\n can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information.\n\n Args:\n title_sep (`str`, *optional*, defaults to `" / "`):\n Separator inserted between the title and the text of the retrieved document when calling [`RagRetriever`].\n doc_sep (`str`, *optional*, defaults to `" // "`):\n Separator inserted between the text of the retrieved document and the original input when calling\n [`RagRetriever`].\n n_docs (`int`, *optional*, defaults to 5):\n Number of documents to retrieve.\n max_combined_length (`int`, *optional*, defaults to 300):\n Max length of contextualized input returned by [`~RagRetriever.__call__`].\n retrieval_vector_size (`int`, *optional*, defaults to 768):\n Dimensionality of the document embeddings indexed by [`RagRetriever`].\n retrieval_batch_size (`int`, *optional*, defaults to 8):\n Retrieval batch size, defined as the number of queries issues concurrently to the faiss index encapsulated\n [`RagRetriever`].\n dataset (`str`, *optional*, defaults to `"wiki_dpr"`):\n A dataset identifier of the indexed dataset in HuggingFace Datasets (list all available datasets and ids\n using `datasets.list_datasets()`).\n dataset_split (`str`, *optional*, defaults to `"train"`)\n Which split of the `dataset` to load.\n index_name (`str`, *optional*, defaults to `"compressed"`)\n The index name of the index associated with the `dataset`. One can choose between `"legacy"`, `"exact"` and\n `"compressed"`.\n index_path (`str`, *optional*)\n The path to the serialized faiss index on disk.\n passages_path (`str`, *optional*):\n A path to text passages compatible with the faiss index. Required if using\n [`~models.rag.retrieval_rag.LegacyIndex`]\n use_dummy_dataset (`bool`, *optional*, defaults to `False`)\n Whether to load a "dummy" variant of the dataset specified by `dataset`.\n label_smoothing (`float`, *optional*, defaults to 0.0):\n Only relevant if `return_loss` is set to `True`. Controls the `epsilon` parameter value for label smoothing\n in the loss calculation. If set to 0, no label smoothing is performed.\n do_marginalize (`bool`, *optional*, defaults to `False`):\n If `True`, the logits are marginalized over all documents by making use of\n `torch.nn.functional.log_softmax`.\n reduce_loss (`bool`, *optional*, defaults to `False`):\n Whether or not to reduce the NLL loss using the `torch.Tensor.sum` operation.\n do_deduplication (`bool`, *optional*, defaults to `True`):\n Whether or not to deduplicate the generations from different context documents for a given input. Has to be\n set to `False` if used while training with distributed backend.\n exclude_bos_score (`bool`, *optional*, defaults to `False`):\n Whether or not to disregard the BOS token when computing the loss.\n output_retrieved(`bool`, *optional*, defaults to `False`):\n If set to `True`, `retrieved_doc_embeds`, `retrieved_doc_ids`, `context_input_ids` and\n `context_attention_mask` are returned. See returned tensors for more detail.\n use_cache (`bool`, *optional*, defaults to `True`):\n Whether or not the model should return the last key/values attentions (not used by all models).\n forced_eos_token_id (`int`, *optional*):\n The id of the token to force as the last generated token when `max_length` is reached. Usually set to\n `eos_token_id`.\n' @add_start_docstrings(SCREAMING_SNAKE_CASE_ ) class lowerCamelCase_ ( SCREAMING_SNAKE_CASE_ ): """simple docstring""" a_ ="""rag""" a_ =True def __init__( self : Any , _a : Tuple=None , _a : Union[str, Any]=True , _a : Tuple=None , _a : Optional[Any]=None , _a : str=None , _a : Union[str, Any]=None , _a : Union[str, Any]=None , _a : Union[str, Any]=" / " , _a : Tuple=" // " , _a : Optional[int]=5 , _a : Tuple=300 , _a : Optional[Any]=768 , _a : Dict=8 , _a : Union[str, Any]="wiki_dpr" , _a : str="train" , _a : List[Any]="compressed" , _a : Dict=None , _a : Optional[Any]=None , _a : Tuple=False , _a : Optional[Any]=False , _a : Tuple=0.0 , _a : Union[str, Any]=True , _a : List[str]=False , _a : int=False , _a : Optional[int]=False , _a : Optional[int]=True , _a : List[Any]=None , **_a : Union[str, Any] , ) -> Optional[int]: super().__init__( bos_token_id=_a , pad_token_id=_a , eos_token_id=_a , decoder_start_token_id=_a , forced_eos_token_id=_a , is_encoder_decoder=_a , prefix=_a , vocab_size=_a , **_a , ) assert ( "question_encoder" in kwargs and "generator" in kwargs ), "Config has to be initialized with question_encoder and generator config" __lowerCamelCase : List[str] = kwargs.pop('question_encoder' ) __lowerCamelCase : Tuple = question_encoder_config.pop('model_type' ) __lowerCamelCase : Optional[Any] = kwargs.pop('generator' ) __lowerCamelCase : Tuple = decoder_config.pop('model_type' ) from ..auto.configuration_auto import AutoConfig __lowerCamelCase : int = AutoConfig.for_model(_a , **_a ) __lowerCamelCase : Dict = AutoConfig.for_model(_a , **_a ) __lowerCamelCase : List[str] = reduce_loss __lowerCamelCase : Dict = label_smoothing __lowerCamelCase : List[str] = exclude_bos_score __lowerCamelCase : Optional[Any] = do_marginalize __lowerCamelCase : Tuple = title_sep __lowerCamelCase : Optional[Any] = doc_sep __lowerCamelCase : Dict = n_docs __lowerCamelCase : str = max_combined_length __lowerCamelCase : Optional[Any] = dataset __lowerCamelCase : List[Any] = dataset_split __lowerCamelCase : Optional[Any] = index_name __lowerCamelCase : Tuple = retrieval_vector_size __lowerCamelCase : Optional[int] = retrieval_batch_size __lowerCamelCase : List[Any] = passages_path __lowerCamelCase : Union[str, Any] = index_path __lowerCamelCase : str = use_dummy_dataset __lowerCamelCase : Optional[Any] = output_retrieved __lowerCamelCase : List[Any] = do_deduplication __lowerCamelCase : Union[str, Any] = use_cache if self.forced_eos_token_id is None: __lowerCamelCase : List[str] = getattr(self.generator , 'forced_eos_token_id' , _a ) @classmethod def _lowercase ( cls : int , _a : PretrainedConfig , _a : PretrainedConfig , **_a : List[str] ) -> PretrainedConfig: return cls(question_encoder=question_encoder_config.to_dict() , generator=generator_config.to_dict() , **_a ) def _lowercase ( self : List[Any] ) -> Any: __lowerCamelCase : List[Any] = copy.deepcopy(self.__dict__ ) __lowerCamelCase : Union[str, Any] = self.question_encoder.to_dict() __lowerCamelCase : Any = self.generator.to_dict() __lowerCamelCase : Tuple = self.__class__.model_type return output
208
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _UpperCamelCase = logging.get_logger(__name__) _UpperCamelCase = { 'xlm-roberta-base': 'https://huggingface.co/xlm-roberta-base/resolve/main/config.json', 'xlm-roberta-large': 'https://huggingface.co/xlm-roberta-large/resolve/main/config.json', 'xlm-roberta-large-finetuned-conll02-dutch': ( 'https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json' ), 'xlm-roberta-large-finetuned-conll02-spanish': ( 'https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json' ), 'xlm-roberta-large-finetuned-conll03-english': ( 'https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json' ), 'xlm-roberta-large-finetuned-conll03-german': ( 'https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json' ), } class lowerCamelCase_ ( SCREAMING_SNAKE_CASE_ ): """simple docstring""" a_ ="""xlm-roberta""" def __init__( self : Any , _a : Optional[Any]=3_0522 , _a : Optional[Any]=768 , _a : int=12 , _a : Tuple=12 , _a : str=3072 , _a : List[Any]="gelu" , _a : int=0.1 , _a : Optional[int]=0.1 , _a : Optional[int]=512 , _a : List[Any]=2 , _a : Optional[Any]=0.02 , _a : str=1e-12 , _a : str=1 , _a : str=0 , _a : Optional[Any]=2 , _a : Optional[int]="absolute" , _a : int=True , _a : Tuple=None , **_a : Any , ) -> Dict: super().__init__(pad_token_id=_a , bos_token_id=_a , eos_token_id=_a , **_a ) __lowerCamelCase : int = vocab_size __lowerCamelCase : Union[str, Any] = hidden_size __lowerCamelCase : int = num_hidden_layers __lowerCamelCase : Optional[int] = num_attention_heads __lowerCamelCase : List[str] = hidden_act __lowerCamelCase : Any = intermediate_size __lowerCamelCase : int = hidden_dropout_prob __lowerCamelCase : List[Any] = attention_probs_dropout_prob __lowerCamelCase : Optional[int] = max_position_embeddings __lowerCamelCase : Tuple = type_vocab_size __lowerCamelCase : Optional[int] = initializer_range __lowerCamelCase : Optional[int] = layer_norm_eps __lowerCamelCase : str = position_embedding_type __lowerCamelCase : List[Any] = use_cache __lowerCamelCase : int = classifier_dropout class lowerCamelCase_ ( SCREAMING_SNAKE_CASE_ ): """simple docstring""" @property def _lowercase ( self : Optional[Any] ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": __lowerCamelCase : int = {0: 'batch', 1: 'choice', 2: 'sequence'} else: __lowerCamelCase : Any = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ] )
208
1
'''simple docstring''' import inspect import unittest import numpy as np from transformers import BeitConfig from transformers.testing_utils import require_flax, require_vision, slow from transformers.utils import cached_property, is_flax_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor if is_flax_available(): import jax from transformers import FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling, FlaxBeitModel if is_vision_available(): from PIL import Image from transformers import BeitImageProcessor class A__ ( unittest.TestCase ): """simple docstring""" def __init__( self : List[str] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : int=1_0_0 , lowerCAmelCase__ : Any=1_3 , lowerCAmelCase__ : Union[str, Any]=3_0 , lowerCAmelCase__ : Optional[Any]=2 , lowerCAmelCase__ : List[Any]=3 , lowerCAmelCase__ : Dict=True , lowerCAmelCase__ : Optional[Any]=True , lowerCAmelCase__ : int=3_2 , lowerCAmelCase__ : Optional[int]=5 , lowerCAmelCase__ : Optional[Any]=4 , lowerCAmelCase__ : Tuple=3_7 , lowerCAmelCase__ : int="gelu" , lowerCAmelCase__ : Any=0.1 , lowerCAmelCase__ : Optional[int]=0.1 , lowerCAmelCase__ : Optional[int]=1_0 , lowerCAmelCase__ : Optional[int]=0.02 , lowerCAmelCase__ : List[Any]=3 , ) -> Union[str, Any]: """simple docstring""" _UpperCAmelCase : str = parent _UpperCAmelCase : List[Any] = vocab_size _UpperCAmelCase : Optional[int] = batch_size _UpperCAmelCase : List[Any] = image_size _UpperCAmelCase : int = patch_size _UpperCAmelCase : str = num_channels _UpperCAmelCase : str = is_training _UpperCAmelCase : Union[str, Any] = use_labels _UpperCAmelCase : Optional[Any] = hidden_size _UpperCAmelCase : List[Any] = num_hidden_layers _UpperCAmelCase : List[Any] = num_attention_heads _UpperCAmelCase : List[str] = intermediate_size _UpperCAmelCase : Tuple = hidden_act _UpperCAmelCase : Union[str, Any] = hidden_dropout_prob _UpperCAmelCase : str = attention_probs_dropout_prob _UpperCAmelCase : Any = type_sequence_label_size _UpperCAmelCase : str = initializer_range # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) _UpperCAmelCase : int = (image_size // patch_size) ** 2 _UpperCAmelCase : Dict = num_patches + 1 def _lowerCAmelCase ( self : int ) -> Tuple: """simple docstring""" _UpperCAmelCase : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _UpperCAmelCase : List[str] = None if self.use_labels: _UpperCAmelCase : Any = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _UpperCAmelCase : str = BeitConfig( vocab_size=self.vocab_size , image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=lowerCAmelCase__ , initializer_range=self.initializer_range , ) return config, pixel_values, labels def _lowerCAmelCase ( self : List[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : List[Any] ) -> List[Any]: """simple docstring""" _UpperCAmelCase : Optional[int] = FlaxBeitModel(config=lowerCAmelCase__ ) _UpperCAmelCase : List[Any] = model(lowerCAmelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowerCAmelCase ( self : Tuple , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : str ) -> Optional[int]: """simple docstring""" _UpperCAmelCase : List[Any] = FlaxBeitForMaskedImageModeling(config=lowerCAmelCase__ ) _UpperCAmelCase : Optional[int] = model(lowerCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length - 1, self.vocab_size) ) def _lowerCAmelCase ( self : Union[str, Any] , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Optional[Any] ) -> Optional[Any]: """simple docstring""" _UpperCAmelCase : Optional[Any] = self.type_sequence_label_size _UpperCAmelCase : Union[str, Any] = FlaxBeitForImageClassification(config=lowerCAmelCase__ ) _UpperCAmelCase : Optional[Any] = model(lowerCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images _UpperCAmelCase : str = 1 _UpperCAmelCase : str = FlaxBeitForImageClassification(lowerCAmelCase__ ) _UpperCAmelCase : Optional[int] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) _UpperCAmelCase : Optional[Any] = model(lowerCAmelCase__ ) def _lowerCAmelCase ( self : Union[str, Any] ) -> List[str]: """simple docstring""" _UpperCAmelCase : List[str] = self.prepare_config_and_inputs() ( ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ) : Union[str, Any] = config_and_inputs _UpperCAmelCase : str = {"pixel_values": pixel_values} return config, inputs_dict @require_flax class A__ ( UpperCamelCase , unittest.TestCase ): """simple docstring""" UpperCamelCase_ : Any = ( (FlaxBeitModel, FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling) if is_flax_available() else () ) def _lowerCAmelCase ( self : Any ) -> None: """simple docstring""" _UpperCAmelCase : Union[str, Any] = FlaxBeitModelTester(self ) _UpperCAmelCase : str = ConfigTester(self , config_class=lowerCAmelCase__ , has_text_modality=lowerCAmelCase__ , hidden_size=3_7 ) def _lowerCAmelCase ( self : Tuple ) -> Any: """simple docstring""" self.config_tester.run_common_tests() def _lowerCAmelCase ( self : List[Any] ) -> Tuple: """simple docstring""" _UpperCAmelCase , _UpperCAmelCase : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _UpperCAmelCase : List[str] = model_class(lowerCAmelCase__ ) _UpperCAmelCase : Optional[int] = inspect.signature(model.__call__ ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _UpperCAmelCase : Optional[int] = [*signature.parameters.keys()] _UpperCAmelCase : List[str] = ["pixel_values"] self.assertListEqual(arg_names[:1] , lowerCAmelCase__ ) def _lowerCAmelCase ( self : Union[str, Any] ) -> Dict: """simple docstring""" _UpperCAmelCase , _UpperCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): _UpperCAmelCase : Optional[Any] = self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__ ) _UpperCAmelCase : List[Any] = model_class(lowerCAmelCase__ ) @jax.jit def model_jitted(lowerCAmelCase__ : List[Any] , **lowerCAmelCase__ : Optional[int] ): return model(pixel_values=lowerCAmelCase__ , **lowerCAmelCase__ ) with self.subTest("JIT Enabled" ): _UpperCAmelCase : Any = model_jitted(**lowerCAmelCase__ ).to_tuple() with self.subTest("JIT Disabled" ): with jax.disable_jit(): _UpperCAmelCase : int = model_jitted(**lowerCAmelCase__ ).to_tuple() self.assertEqual(len(lowerCAmelCase__ ) , len(lowerCAmelCase__ ) ) for jitted_output, output in zip(lowerCAmelCase__ , lowerCAmelCase__ ): self.assertEqual(jitted_output.shape , output.shape ) def _lowerCAmelCase ( self : Optional[int] ) -> Any: """simple docstring""" _UpperCAmelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCAmelCase__ ) def _lowerCAmelCase ( self : Dict ) -> Any: """simple docstring""" _UpperCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase__ ) def _lowerCAmelCase ( self : Optional[int] ) -> List[Any]: """simple docstring""" _UpperCAmelCase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__ ) @slow def _lowerCAmelCase ( self : str ) -> Any: """simple docstring""" for model_class_name in self.all_model_classes: _UpperCAmelCase : Any = model_class_name.from_pretrained("microsoft/beit-base-patch16-224" ) _UpperCAmelCase : List[Any] = model(np.ones((1, 3, 2_2_4, 2_2_4) ) ) self.assertIsNotNone(lowerCAmelCase__ ) def __UpperCAmelCase ( ): _UpperCAmelCase : Tuple = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_vision @require_flax class A__ ( unittest.TestCase ): """simple docstring""" @cached_property def _lowerCAmelCase ( self : List[Any] ) -> Union[str, Any]: """simple docstring""" return BeitImageProcessor.from_pretrained("microsoft/beit-base-patch16-224" ) if is_vision_available() else None @slow def _lowerCAmelCase ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" _UpperCAmelCase : str = FlaxBeitForMaskedImageModeling.from_pretrained("microsoft/beit-base-patch16-224-pt22k" ) _UpperCAmelCase : Dict = self.default_image_processor _UpperCAmelCase : str = prepare_img() _UpperCAmelCase : Tuple = image_processor(images=lowerCAmelCase__ , return_tensors="np" ).pixel_values # prepare bool_masked_pos _UpperCAmelCase : Optional[int] = np.ones((1, 1_9_6) , dtype=lowerCAmelCase__ ) # forward pass _UpperCAmelCase : Optional[int] = model(pixel_values=lowerCAmelCase__ , bool_masked_pos=lowerCAmelCase__ ) _UpperCAmelCase : List[str] = outputs.logits # verify the logits _UpperCAmelCase : Dict = (1, 1_9_6, 8_1_9_2) self.assertEqual(logits.shape , lowerCAmelCase__ ) _UpperCAmelCase : Dict = np.array( [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]] ) self.assertTrue(np.allclose(logits[bool_masked_pos][:3, :3] , lowerCAmelCase__ , atol=1e-2 ) ) @slow def _lowerCAmelCase ( self : Dict ) -> int: """simple docstring""" _UpperCAmelCase : int = FlaxBeitForImageClassification.from_pretrained("microsoft/beit-base-patch16-224" ) _UpperCAmelCase : str = self.default_image_processor _UpperCAmelCase : str = prepare_img() _UpperCAmelCase : List[Any] = image_processor(images=lowerCAmelCase__ , return_tensors="np" ) # forward pass _UpperCAmelCase : Union[str, Any] = model(**lowerCAmelCase__ ) _UpperCAmelCase : Tuple = outputs.logits # verify the logits _UpperCAmelCase : str = (1, 1_0_0_0) self.assertEqual(logits.shape , lowerCAmelCase__ ) _UpperCAmelCase : Dict = np.array([-1.2385, -1.0987, -1.0108] ) self.assertTrue(np.allclose(logits[0, :3] , lowerCAmelCase__ , atol=1e-4 ) ) _UpperCAmelCase : List[Any] = 2_8_1 self.assertEqual(logits.argmax(-1 ).item() , lowerCAmelCase__ ) @slow def _lowerCAmelCase ( self : Optional[int] ) -> Optional[int]: """simple docstring""" _UpperCAmelCase : Dict = FlaxBeitForImageClassification.from_pretrained("microsoft/beit-large-patch16-224-pt22k-ft22k" ) _UpperCAmelCase : List[str] = self.default_image_processor _UpperCAmelCase : Optional[int] = prepare_img() _UpperCAmelCase : Tuple = image_processor(images=lowerCAmelCase__ , return_tensors="np" ) # forward pass _UpperCAmelCase : Optional[int] = model(**lowerCAmelCase__ ) _UpperCAmelCase : List[str] = outputs.logits # verify the logits _UpperCAmelCase : str = (1, 2_1_8_4_1) self.assertEqual(logits.shape , lowerCAmelCase__ ) _UpperCAmelCase : int = np.array([1.6881, -0.2787, 0.5901] ) self.assertTrue(np.allclose(logits[0, :3] , lowerCAmelCase__ , atol=1e-4 ) ) _UpperCAmelCase : int = 2_3_9_6 self.assertEqual(logits.argmax(-1 ).item() , lowerCAmelCase__ )
17
'''simple docstring''' from math import factorial def __UpperCAmelCase ( a_: int = 100 ): return sum(map(a_, str(factorial(a_ ) ) ) ) if __name__ == "__main__": print(solution(int(input('Enter the Number: ').strip())))
17
1
import argparse import pickle import numpy as np import torch from torch import nn from transformers import ReformerConfig, ReformerModelWithLMHead from transformers.utils import logging logging.set_verbosity_info() def A (__A : Optional[int] , __A : int , __A : str=None ) -> List[Any]: """simple docstring""" assert torch_layer.weight.shape == weight.shape, F"""{torch_layer} layer.weight does not match""" UpperCAmelCase_ = nn.Parameter(__A ) if bias is not None: assert torch_layer.bias.shape == bias.shape, F"""{torch_layer} layer.bias does not match""" UpperCAmelCase_ = nn.Parameter(__A ) def A (__A : Tuple , __A : Dict , __A : str ) -> Tuple: """simple docstring""" UpperCAmelCase_ = np.asarray(weights[0] ) UpperCAmelCase_ = np.asarray(weights[1] ) UpperCAmelCase_ = np.asarray(weights[2] ) set_param( torch_layer.self_attention.query_key , torch.tensor(__A ).transpose(1 , 2 ).contiguous().view(-1 , __A ) , ) set_param( torch_layer.self_attention.value , torch.tensor(__A ).transpose(1 , 2 ).contiguous().view(-1 , __A ) , ) set_param( torch_layer.output.dense , torch.tensor(__A ).view(-1 , __A ).contiguous().transpose(0 , 1 ) , ) def A (__A : Optional[Any] , __A : Any , __A : List[Any] ) -> int: """simple docstring""" UpperCAmelCase_ = np.asarray(weights[0] ) UpperCAmelCase_ = np.asarray(weights[1] ) UpperCAmelCase_ = np.asarray(weights[2] ) UpperCAmelCase_ = np.asarray(weights[3] ) set_param( torch_layer.self_attention.query , torch.tensor(__A ).transpose(1 , 2 ).contiguous().view(-1 , __A ) , ) set_param( torch_layer.self_attention.key , torch.tensor(__A ).transpose(1 , 2 ).contiguous().view(-1 , __A ) , ) set_param( torch_layer.self_attention.value , torch.tensor(__A ).transpose(1 , 2 ).contiguous().view(-1 , __A ) , ) set_param( torch_layer.output.dense , torch.tensor(__A ).view(-1 , __A ).contiguous().transpose(0 , 1 ) , ) def A (__A : int , __A : Union[str, Any] , __A : List[str] ) -> List[Any]: """simple docstring""" UpperCAmelCase_ = weights[0][0][0] UpperCAmelCase_ = np.asarray(layer_norm_a[0] ) UpperCAmelCase_ = np.asarray(layer_norm_a[1] ) set_param( torch_block.attention.layer_norm , torch.tensor(__A ) , torch.tensor(__A ) , ) # lsh weights + output UpperCAmelCase_ = weights[0][1] if len(__A ) < 4: set_layer_weights_in_torch_lsh(__A , torch_block.attention , __A ) else: set_layer_weights_in_torch_local(__A , torch_block.attention , __A ) # intermediate weighs UpperCAmelCase_ = weights[2][0][1][2] # Chunked Feed Forward if len(__A ) == 4: UpperCAmelCase_ = intermediate_weights[2] # layernorm 2 UpperCAmelCase_ = np.asarray(intermediate_weights[0][0] ) UpperCAmelCase_ = np.asarray(intermediate_weights[0][1] ) set_param( torch_block.feed_forward.layer_norm , torch.tensor(__A ) , torch.tensor(__A ) , ) # intermediate dense UpperCAmelCase_ = np.asarray(intermediate_weights[1][0] ) UpperCAmelCase_ = np.asarray(intermediate_weights[1][1] ) set_param( torch_block.feed_forward.dense.dense , torch.tensor(__A ).transpose(0 , 1 ).contiguous() , torch.tensor(__A ) , ) # intermediate out UpperCAmelCase_ = np.asarray(intermediate_weights[4][0] ) UpperCAmelCase_ = np.asarray(intermediate_weights[4][1] ) set_param( torch_block.feed_forward.output.dense , torch.tensor(__A ).transpose(0 , 1 ).contiguous() , torch.tensor(__A ) , ) def A (__A : Optional[int] , __A : Tuple , __A : Any ) -> Tuple: """simple docstring""" UpperCAmelCase_ = torch_model.reformer # word embeds UpperCAmelCase_ = np.asarray(weights[1] ) set_param( torch_model_reformer.embeddings.word_embeddings , torch.tensor(__A ) , ) if isinstance(weights[3] , __A ): UpperCAmelCase_ = torch_model_reformer.embeddings.position_embeddings for emb_idx in range(len(position_embeddings.weights ) ): UpperCAmelCase_ = np.asarray(weights[3][emb_idx][0] ) assert ( position_embeddings.weights[emb_idx].shape == emb_weights.shape ), F"""{position_embeddings[emb_idx]} emb does not match""" UpperCAmelCase_ = nn.Parameter(torch.tensor(__A ) ) UpperCAmelCase_ = weights[5] assert len(torch_model_reformer.encoder.layers ) * 4 == len( __A ), "HF and trax model do not have the same number of layers" for layer_idx, layer in enumerate(torch_model_reformer.encoder.layers ): UpperCAmelCase_ = trax_layer_weights[4 * layer_idx : 4 * (layer_idx + 1)] set_block_weights_in_torch(__A , __A , __A ) # output layer norm UpperCAmelCase_ = np.asarray(weights[7][0] ) UpperCAmelCase_ = np.asarray(weights[7][1] ) set_param( torch_model_reformer.encoder.layer_norm , torch.tensor(__A ) , torch.tensor(__A ) , ) # output embeddings UpperCAmelCase_ = np.asarray(weights[9][0] ) UpperCAmelCase_ = np.asarray(weights[9][1] ) set_param( torch_model.lm_head.decoder , torch.tensor(__A ).transpose(0 , 1 ).contiguous() , torch.tensor(__A ) , ) def A (__A : Tuple , __A : int , __A : str ) -> Union[str, Any]: """simple docstring""" UpperCAmelCase_ = ReformerConfig.from_json_file(__A ) print(F"""Building PyTorch model from configuration: {config}""" ) UpperCAmelCase_ = ReformerModelWithLMHead(__A ) with open(__A , '''rb''' ) as f: UpperCAmelCase_ = pickle.load(__A )['''weights'''] set_model_weights_in_torch(__A , __A , config.hidden_size ) # Save pytorch-model print(F"""Save PyTorch model to {pytorch_dump_path}""" ) torch.save(model.state_dict() , __A ) if __name__ == "__main__": snake_case_ : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--trax_model_pkl_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path." ) parser.add_argument( "--config_file", default=None, type=str, required=True, help=( "The config json file corresponding to the pre-trained Reformer model. \n" "This specifies the model architecture." ), ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) snake_case_ : List[Any] = parser.parse_args() convert_trax_checkpoint_to_pytorch(args.trax_model_pkl_path, args.config_file, args.pytorch_dump_path)
51
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 snake_case_ : List[str] = logging.get_logger(__name__) snake_case_ : Tuple = { "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 __snake_case ( a ): UpperCAmelCase__ : str = '''codegen''' UpperCAmelCase__ : int = { '''max_position_embeddings''': '''n_positions''', '''hidden_size''': '''n_embd''', '''num_attention_heads''': '''n_head''', '''num_hidden_layers''': '''n_layer''', } def __init__( self : Union[str, Any] , _snake_case : Union[str, Any]=50400 , _snake_case : Optional[int]=2048 , _snake_case : Union[str, Any]=2048 , _snake_case : List[str]=4096 , _snake_case : Any=28 , _snake_case : List[str]=16 , _snake_case : int=64 , _snake_case : Tuple=None , _snake_case : Dict="gelu_new" , _snake_case : Union[str, Any]=0.0 , _snake_case : Optional[Any]=0.0 , _snake_case : List[Any]=0.0 , _snake_case : List[Any]=1e-5 , _snake_case : List[str]=0.0_2 , _snake_case : Optional[Any]=True , _snake_case : int=50256 , _snake_case : Tuple=50256 , _snake_case : int=False , **_snake_case : Any , ): """simple docstring""" UpperCAmelCase_ = vocab_size UpperCAmelCase_ = n_ctx UpperCAmelCase_ = n_positions UpperCAmelCase_ = n_embd UpperCAmelCase_ = n_layer UpperCAmelCase_ = n_head UpperCAmelCase_ = n_inner UpperCAmelCase_ = rotary_dim UpperCAmelCase_ = activation_function UpperCAmelCase_ = resid_pdrop UpperCAmelCase_ = embd_pdrop UpperCAmelCase_ = attn_pdrop UpperCAmelCase_ = layer_norm_epsilon UpperCAmelCase_ = initializer_range UpperCAmelCase_ = use_cache UpperCAmelCase_ = bos_token_id UpperCAmelCase_ = eos_token_id super().__init__( bos_token_id=_snake_case , eos_token_id=_snake_case , tie_word_embeddings=_snake_case , **_snake_case) class __snake_case ( a ): def __init__( self : Tuple , _snake_case : PretrainedConfig , _snake_case : str = "default" , _snake_case : List[PatchingSpec] = None , _snake_case : bool = False , ): """simple docstring""" super().__init__(_snake_case , task=_snake_case , patching_specs=_snake_case , use_past=_snake_case) if not getattr(self._config , '''pad_token_id''' , _snake_case): # TODO: how to do that better? UpperCAmelCase_ = 0 @property def lowerCamelCase ( self : Optional[Any]): """simple docstring""" UpperCAmelCase_ = OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}}) if self.use_past: self.fill_with_past_key_values_(_snake_case , direction='''inputs''') UpperCAmelCase_ = {0: '''batch''', 1: '''past_sequence + sequence'''} else: UpperCAmelCase_ = {0: '''batch''', 1: '''sequence'''} return common_inputs @property def lowerCamelCase ( self : List[str]): """simple docstring""" return self._config.n_layer @property def lowerCamelCase ( self : int): """simple docstring""" return self._config.n_head def lowerCamelCase ( self : Optional[int] , _snake_case : PreTrainedTokenizer , _snake_case : int = -1 , _snake_case : int = -1 , _snake_case : bool = False , _snake_case : Optional[TensorType] = None , ): """simple docstring""" UpperCAmelCase_ = super(_snake_case , self).generate_dummy_inputs( _snake_case , batch_size=_snake_case , seq_length=_snake_case , is_pair=_snake_case , framework=_snake_case) # We need to order the input in the way they appears in the forward() UpperCAmelCase_ = 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 UpperCAmelCase_ , UpperCAmelCase_ = common_inputs['''input_ids'''].shape # Not using the same length for past_key_values UpperCAmelCase_ = seqlen + 2 UpperCAmelCase_ = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) UpperCAmelCase_ = [ (torch.zeros(_snake_case), torch.zeros(_snake_case)) for _ in range(self.num_layers) ] UpperCAmelCase_ = common_inputs['''attention_mask'''] if self.use_past: UpperCAmelCase_ = ordered_inputs['''attention_mask'''].dtype UpperCAmelCase_ = torch.cat( [ordered_inputs['''attention_mask'''], torch.ones(_snake_case , _snake_case , dtype=_snake_case)] , dim=1) return ordered_inputs @property def lowerCamelCase ( self : Union[str, Any]): """simple docstring""" return 13
51
1
import json import os import shutil import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoConfig, BertConfig, GPTaConfig from transformers.configuration_utils import PretrainedConfig from transformers.testing_utils import TOKEN, USER, is_staging_test sys.path.append(str(Path(__file__).parent.parent / "utils")) from test_module.custom_configuration import CustomConfig # noqa E402 _lowercase: Union[str, Any] = { '''return_dict''': False, '''output_hidden_states''': True, '''output_attentions''': True, '''torchscript''': True, '''torch_dtype''': '''float16''', '''use_bfloat16''': True, '''tf_legacy_loss''': True, '''pruned_heads''': {'''a''': 1}, '''tie_word_embeddings''': False, '''is_decoder''': True, '''cross_attention_hidden_size''': 128, '''add_cross_attention''': True, '''tie_encoder_decoder''': True, '''max_length''': 50, '''min_length''': 3, '''do_sample''': True, '''early_stopping''': True, '''num_beams''': 3, '''num_beam_groups''': 3, '''diversity_penalty''': 0.5, '''temperature''': 2.0, '''top_k''': 10, '''top_p''': 0.7, '''typical_p''': 0.2, '''repetition_penalty''': 0.8, '''length_penalty''': 0.8, '''no_repeat_ngram_size''': 5, '''encoder_no_repeat_ngram_size''': 5, '''bad_words_ids''': [1, 2, 3], '''num_return_sequences''': 3, '''chunk_size_feed_forward''': 5, '''output_scores''': True, '''return_dict_in_generate''': True, '''forced_bos_token_id''': 2, '''forced_eos_token_id''': 3, '''remove_invalid_values''': True, '''architectures''': ['''BertModel'''], '''finetuning_task''': '''translation''', '''id2label''': {0: '''label'''}, '''label2id''': {'''label''': '''0'''}, '''tokenizer_class''': '''BertTokenizerFast''', '''prefix''': '''prefix''', '''bos_token_id''': 6, '''pad_token_id''': 7, '''eos_token_id''': 8, '''sep_token_id''': 9, '''decoder_start_token_id''': 10, '''exponential_decay_length_penalty''': (5, 1.01), '''suppress_tokens''': [0, 1], '''begin_suppress_tokens''': 2, '''task_specific_params''': {'''translation''': '''some_params'''}, '''problem_type''': '''regression''', } @is_staging_test class _lowercase ( unittest.TestCase ): """simple docstring""" @classmethod def UpperCamelCase_ (cls ): """simple docstring""" a = TOKEN HfFolder.save_token(SCREAMING_SNAKE_CASE_ ) @classmethod def UpperCamelCase_ (cls ): """simple docstring""" try: delete_repo(token=cls._token , repo_id="test-config" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="valid_org/test-config-org" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="test-dynamic-config" ) except HTTPError: pass def UpperCamelCase_ (self ): """simple docstring""" a = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) config.push_to_hub("test-config" , use_auth_token=self._token ) a = BertConfig.from_pretrained(F'''{USER}/test-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(SCREAMING_SNAKE_CASE_ , getattr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) # Reset repo delete_repo(token=self._token , repo_id="test-config" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(SCREAMING_SNAKE_CASE_ , repo_id="test-config" , push_to_hub=SCREAMING_SNAKE_CASE_ , use_auth_token=self._token ) a = BertConfig.from_pretrained(F'''{USER}/test-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(SCREAMING_SNAKE_CASE_ , getattr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) def UpperCamelCase_ (self ): """simple docstring""" a = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) config.push_to_hub("valid_org/test-config-org" , use_auth_token=self._token ) a = BertConfig.from_pretrained("valid_org/test-config-org" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(SCREAMING_SNAKE_CASE_ , getattr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) # Reset repo delete_repo(token=self._token , repo_id="valid_org/test-config-org" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( SCREAMING_SNAKE_CASE_ , repo_id="valid_org/test-config-org" , push_to_hub=SCREAMING_SNAKE_CASE_ , use_auth_token=self._token ) a = BertConfig.from_pretrained("valid_org/test-config-org" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(SCREAMING_SNAKE_CASE_ , getattr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) def UpperCamelCase_ (self ): """simple docstring""" CustomConfig.register_for_auto_class() a = CustomConfig(attribute=42 ) config.push_to_hub("test-dynamic-config" , use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual(config.auto_map , {"AutoConfig": "custom_configuration.CustomConfig"} ) a = AutoConfig.from_pretrained(F'''{USER}/test-dynamic-config''' , trust_remote_code=SCREAMING_SNAKE_CASE_ ) # Can't make an isinstance check because the new_config is from the FakeConfig class of a dynamic module self.assertEqual(new_config.__class__.__name__ , "CustomConfig" ) self.assertEqual(new_config.attribute , 42 ) class _lowercase ( unittest.TestCase ): """simple docstring""" def UpperCamelCase_ (self ): """simple docstring""" a = GPTaConfig() # attempt to modify each of int/float/bool/str config records and verify they were updated a = c.n_embd + 1 # int a = c.resid_pdrop + 1.0 # float a = not c.scale_attn_weights # bool a = c.summary_type + """foo""" # str c.update_from_string( F'''n_embd={n_embd},resid_pdrop={resid_pdrop},scale_attn_weights={scale_attn_weights},summary_type={summary_type}''' ) self.assertEqual(SCREAMING_SNAKE_CASE_ , c.n_embd , "mismatch for key: n_embd" ) self.assertEqual(SCREAMING_SNAKE_CASE_ , c.resid_pdrop , "mismatch for key: resid_pdrop" ) self.assertEqual(SCREAMING_SNAKE_CASE_ , c.scale_attn_weights , "mismatch for key: scale_attn_weights" ) self.assertEqual(SCREAMING_SNAKE_CASE_ , c.summary_type , "mismatch for key: summary_type" ) def UpperCamelCase_ (self ): """simple docstring""" a = PretrainedConfig() a = [key for key in base_config.__dict__ if key not in config_common_kwargs] # If this part of the test fails, you have arguments to addin config_common_kwargs above. self.assertListEqual( SCREAMING_SNAKE_CASE_ , ["is_encoder_decoder", "_name_or_path", "_commit_hash", "transformers_version"] ) a = [key for key, value in config_common_kwargs.items() if value == getattr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )] if len(SCREAMING_SNAKE_CASE_ ) > 0: raise ValueError( "The following keys are set with the default values in" " `test_configuration_common.config_common_kwargs` pick another value for them:" F''' {', '.join(SCREAMING_SNAKE_CASE_ )}.''' ) def UpperCamelCase_ (self ): """simple docstring""" with self.assertRaises(SCREAMING_SNAKE_CASE_ ): # config is in subfolder, the following should not work without specifying the subfolder a = BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert-subfolder" ) a = BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert-subfolder" , subfolder="bert" ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) def UpperCamelCase_ (self ): """simple docstring""" a = mock.Mock() a = 500 a = {} a = HTTPError a = {} # Download this model to make sure it's in the cache. a = BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert" ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch("requests.Session.request" , return_value=SCREAMING_SNAKE_CASE_ ) as mock_head: a = BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert" ) # This check we did call the fake head request mock_head.assert_called() def UpperCamelCase_ (self ): """simple docstring""" a = BertConfig.from_pretrained( "https://huggingface.co/hf-internal-testing/tiny-random-bert/resolve/main/config.json" ) def UpperCamelCase_ (self ): """simple docstring""" a = AutoConfig.from_pretrained("bert-base-cased" ) a = ["""config.4.0.0.json"""] with tempfile.TemporaryDirectory() as tmp_dir: configuration.save_pretrained(SCREAMING_SNAKE_CASE_ ) a = 2 json.dump(configuration.to_dict() , open(os.path.join(SCREAMING_SNAKE_CASE_ , "config.4.0.0.json" ) , "w" ) ) # This should pick the new configuration file as the version of Transformers is > 4.0.0 a = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertEqual(new_configuration.hidden_size , 2 ) # Will need to be adjusted if we reach v42 and this test is still here. # Should pick the old configuration file as the version of Transformers is < 4.42.0 a = ["""config.42.0.0.json"""] a = 768 configuration.save_pretrained(SCREAMING_SNAKE_CASE_ ) shutil.move(os.path.join(SCREAMING_SNAKE_CASE_ , "config.4.0.0.json" ) , os.path.join(SCREAMING_SNAKE_CASE_ , "config.42.0.0.json" ) ) a = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertEqual(new_configuration.hidden_size , 768 ) def UpperCamelCase_ (self ): """simple docstring""" a = """hf-internal-testing/test-two-configs""" import transformers as new_transformers a = """v4.0.0""" a = new_transformers.models.auto.AutoConfig.from_pretrained( SCREAMING_SNAKE_CASE_ , return_unused_kwargs=SCREAMING_SNAKE_CASE_ ) self.assertEqual(new_configuration.hidden_size , 2 ) # This checks `_configuration_file` ia not kept in the kwargs by mistake. self.assertDictEqual(SCREAMING_SNAKE_CASE_ , {} ) # Testing an older version by monkey-patching the version in the module it's used. import transformers as old_transformers a = """v3.0.0""" a = old_transformers.models.auto.AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertEqual(old_configuration.hidden_size , 768 )
355
import unittest from parameterized import parameterized from transformers import LlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import LlamaForCausalLM, LlamaForSequenceClassification, LlamaModel, LlamaTokenizer class _lowercase : """simple docstring""" def __init__(self , lowerCamelCase_ , lowerCamelCase_=13 , lowerCamelCase_=7 , lowerCamelCase_=True , lowerCamelCase_=True , lowerCamelCase_=False , lowerCamelCase_=True , lowerCamelCase_=99 , lowerCamelCase_=32 , lowerCamelCase_=5 , lowerCamelCase_=4 , lowerCamelCase_=37 , lowerCamelCase_="gelu" , lowerCamelCase_=0.1 , lowerCamelCase_=0.1 , lowerCamelCase_=512 , lowerCamelCase_=16 , lowerCamelCase_=2 , lowerCamelCase_=0.02 , lowerCamelCase_=3 , lowerCamelCase_=4 , lowerCamelCase_=None , ): """simple docstring""" a = parent a = batch_size a = seq_length a = is_training a = use_input_mask a = use_token_type_ids a = use_labels a = vocab_size a = hidden_size a = num_hidden_layers a = num_attention_heads a = intermediate_size a = hidden_act a = hidden_dropout_prob a = attention_probs_dropout_prob a = max_position_embeddings a = type_vocab_size a = type_sequence_label_size a = initializer_range a = num_labels a = num_choices a = scope def UpperCamelCase_ (self ): """simple docstring""" a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) a = None if self.use_input_mask: a = random_attention_mask([self.batch_size, self.seq_length] ) a = None if self.use_token_type_ids: a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) a = None a = None a = None if self.use_labels: a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) a = ids_tensor([self.batch_size] , self.num_choices ) a = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCamelCase_ (self ): """simple docstring""" return LlamaConfig( 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 , ) def UpperCamelCase_ (self , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ): """simple docstring""" a = LlamaModel(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() a = model(lowerCamelCase_ , attention_mask=lowerCamelCase_ ) a = model(lowerCamelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCamelCase_ (self , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , ): """simple docstring""" a = True a = LlamaModel(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() a = model( lowerCamelCase_ , attention_mask=lowerCamelCase_ , encoder_hidden_states=lowerCamelCase_ , encoder_attention_mask=lowerCamelCase_ , ) a = model( lowerCamelCase_ , attention_mask=lowerCamelCase_ , encoder_hidden_states=lowerCamelCase_ , ) a = model(lowerCamelCase_ , attention_mask=lowerCamelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCamelCase_ (self , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , ): """simple docstring""" a = LlamaForCausalLM(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() a = model(lowerCamelCase_ , attention_mask=lowerCamelCase_ , labels=lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCamelCase_ (self , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , ): """simple docstring""" a = True a = True a = LlamaForCausalLM(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() # first forward pass a = model( lowerCamelCase_ , attention_mask=lowerCamelCase_ , encoder_hidden_states=lowerCamelCase_ , encoder_attention_mask=lowerCamelCase_ , use_cache=lowerCamelCase_ , ) a = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids a = ids_tensor((self.batch_size, 3) , config.vocab_size ) a = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and a = torch.cat([input_ids, next_tokens] , dim=-1 ) a = torch.cat([input_mask, next_mask] , dim=-1 ) a = model( lowerCamelCase_ , attention_mask=lowerCamelCase_ , encoder_hidden_states=lowerCamelCase_ , encoder_attention_mask=lowerCamelCase_ , output_hidden_states=lowerCamelCase_ , )["hidden_states"][0] a = model( lowerCamelCase_ , attention_mask=lowerCamelCase_ , encoder_hidden_states=lowerCamelCase_ , encoder_attention_mask=lowerCamelCase_ , past_key_values=lowerCamelCase_ , output_hidden_states=lowerCamelCase_ , )["hidden_states"][0] # select random slice a = ids_tensor((1,) , output_from_past.shape[-1] ).item() a = output_from_no_past[:, -3:, random_slice_idx].detach() a = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(lowerCamelCase_ , lowerCamelCase_ , atol=1E-3 ) ) def UpperCamelCase_ (self ): """simple docstring""" a = self.prepare_config_and_inputs() ( ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ) = config_and_inputs a = {"input_ids": input_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class _lowercase ( lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, unittest.TestCase ): """simple docstring""" __A = (LlamaModel, LlamaForCausalLM, LlamaForSequenceClassification) if is_torch_available() else () __A = (LlamaForCausalLM,) if is_torch_available() else () __A = ( { "feature-extraction": LlamaModel, "text-classification": LlamaForSequenceClassification, "text-generation": LlamaForCausalLM, "zero-shot": LlamaForSequenceClassification, } if is_torch_available() else {} ) __A = False __A = False def UpperCamelCase_ (self ): """simple docstring""" a = LlamaModelTester(self ) a = ConfigTester(self , config_class=lowerCamelCase_ , hidden_size=37 ) def UpperCamelCase_ (self ): """simple docstring""" self.config_tester.run_common_tests() def UpperCamelCase_ (self ): """simple docstring""" a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase_ ) def UpperCamelCase_ (self ): """simple docstring""" a = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: a = type self.model_tester.create_and_check_model(*lowerCamelCase_ ) def UpperCamelCase_ (self ): """simple docstring""" a , a = self.model_tester.prepare_config_and_inputs_for_common() a = 3 a = input_dict["input_ids"] a = input_ids.ne(1 ).to(lowerCamelCase_ ) a = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) a = LlamaForSequenceClassification(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() a = model(lowerCamelCase_ , attention_mask=lowerCamelCase_ , labels=lowerCamelCase_ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def UpperCamelCase_ (self ): """simple docstring""" a , a = self.model_tester.prepare_config_and_inputs_for_common() a = 3 a = "single_label_classification" a = input_dict["input_ids"] a = input_ids.ne(1 ).to(lowerCamelCase_ ) a = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) a = LlamaForSequenceClassification(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() a = model(lowerCamelCase_ , attention_mask=lowerCamelCase_ , labels=lowerCamelCase_ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def UpperCamelCase_ (self ): """simple docstring""" a , a = self.model_tester.prepare_config_and_inputs_for_common() a = 3 a = "multi_label_classification" a = input_dict["input_ids"] a = input_ids.ne(1 ).to(lowerCamelCase_ ) a = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) a = LlamaForSequenceClassification(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() a = model(lowerCamelCase_ , attention_mask=lowerCamelCase_ , labels=lowerCamelCase_ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip("LLaMA buffers include complex numbers, which breaks this test" ) def UpperCamelCase_ (self ): """simple docstring""" pass @parameterized.expand([("linear",), ("dynamic",)] ) def UpperCamelCase_ (self , lowerCamelCase_ ): """simple docstring""" a , a = self.model_tester.prepare_config_and_inputs_for_common() a = ids_tensor([1, 10] , config.vocab_size ) a = ids_tensor([1, int(config.max_position_embeddings * 1.5 )] , config.vocab_size ) set_seed(42 ) # Fixed seed at init time so the two models get the same random weights a = LlamaModel(lowerCamelCase_ ) original_model.to(lowerCamelCase_ ) original_model.eval() a = original_model(lowerCamelCase_ ).last_hidden_state a = original_model(lowerCamelCase_ ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights a = {"type": scaling_type, "factor": 10.0} a = LlamaModel(lowerCamelCase_ ) scaled_model.to(lowerCamelCase_ ) scaled_model.eval() a = scaled_model(lowerCamelCase_ ).last_hidden_state a = scaled_model(lowerCamelCase_ ).last_hidden_state # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original # maximum sequence length, so the outputs for the short input should match. if scaling_type == "dynamic": self.assertTrue(torch.allclose(lowerCamelCase_ , lowerCamelCase_ , atol=1E-5 ) ) else: self.assertFalse(torch.allclose(lowerCamelCase_ , lowerCamelCase_ , atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(lowerCamelCase_ , lowerCamelCase_ , atol=1E-5 ) ) @require_torch class _lowercase ( unittest.TestCase ): """simple docstring""" @unittest.skip("Logits are not exactly the same, once we fix the instabalities somehow, will update!" ) @slow def UpperCamelCase_ (self ): """simple docstring""" a = [1, 306, 4658, 278, 6593, 310, 2834, 338] a = LlamaForCausalLM.from_pretrained("meta-llama/Llama-2-7b-hf" , device_map="auto" ) a = model(torch.tensor([input_ids] ) ) # Expected mean on dim = -1 a = torch.tensor([[-6.6550, -4.1227, -4.9859, -3.2406, 0.8262, -3.0033, 1.2964, -3.3699]] ) torch.testing.assert_close(out.mean(-1 ) , lowerCamelCase_ , atol=1E-2 , rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off a = torch.tensor([-12.8281, -7.4453, -0.4639, -8.0625, -7.2500, -8.0000, -6.4883, -7.7695, -7.8438, -7.0312, -6.2188, -7.1328, -1.8496, 1.9961, -8.6250, -6.7227, -12.8281, -6.9492, -7.0742, -7.7852, -7.5820, -7.9062, -6.9375, -7.9805, -8.3438, -8.1562, -8.0469, -7.6250, -7.7422, -7.3398,] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] , lowerCamelCase_ , atol=1E-5 , rtol=1E-5 ) @unittest.skip("Logits are not exactly the same, once we fix the instabalities somehow, will update!" ) @slow def UpperCamelCase_ (self ): """simple docstring""" a = [1, 306, 4658, 278, 6593, 310, 2834, 338] a = LlamaForCausalLM.from_pretrained("meta-llama/Llama-2-13b-hf" , device_map="auto" ) a = model(torch.tensor(lowerCamelCase_ ) ) # Expected mean on dim = -1 a = torch.tensor([[-2.0622, -1.2794, -1.1638, -0.9788, -1.4603, -1.0238, -1.7893, -1.4411]] ) torch.testing.assert_close(out.mean(-1 ) , lowerCamelCase_ , atol=1E-2 , rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off a = torch.tensor([-8.1406, -8.0547, 2.7461, -1.2344, -0.1448, -1.8262, -1.0020, -1.8154, -1.6895, -1.8516, -2.3574, -0.9277, 3.7598, 6.5742, -1.2998, -0.1177, -8.1406, -2.9688, -2.9199, -3.1699, -3.5254, -2.3555, -2.7988, -3.4141, -2.8262, -4.5195, -3.3379, -3.3164, -2.7832, -3.0273] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] , lowerCamelCase_ , atol=1E-5 , rtol=1E-5 ) @unittest.skip("Logits are not exactly the same, once we fix the instabalities somehow, will update!" ) @slow def UpperCamelCase_ (self ): """simple docstring""" a = [1, 306, 4658, 278, 6593, 310, 2834, 338] a = LlamaForCausalLM.from_pretrained("meta-llama/Llama-2-13b-chat-hf" , device_map="auto" ) a = model(torch.tensor(lowerCamelCase_ ) ) # Expected mean on dim = -1 a = torch.tensor([[-0.8562, -1.8520, -0.7551, -0.4162, -1.5161, -1.2038, -2.4823, -2.3254]] ) torch.testing.assert_close(out.mean(-1 ) , lowerCamelCase_ , atol=1E-2 , rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off a = torch.tensor([-2.2227, 4.8828, 0.9023, -0.4578, -0.7871, -0.1033, -0.6221, -0.5786, -0.7803, -1.0674, -1.2920, -0.1570, 0.8008, 2.0723, -0.9497, 0.2771, -2.2227, -0.7612, -1.4346, -1.2061, -1.6426, -0.3000, -0.7139, -1.1934, -1.8691, -1.6973, -1.5947, -1.2705, -0.3523, -0.5513] ) # fmt: on torch.testing.assert_close(out.mean(-1 ) , lowerCamelCase_ , atol=1E-2 , rtol=1E-2 ) @unittest.skip( "Logits are not exactly the same, once we fix the instabalities somehow, will update! Also it is gonna be a `too_slow` test" ) @slow def UpperCamelCase_ (self ): """simple docstring""" a = [1, 306, 4658, 278, 6593, 310, 2834, 338] a = LlamaForCausalLM.from_pretrained("meta-llama/Llama-2-70b-hf" , device_map="auto" ) a = model(torch.tensor(lowerCamelCase_ ) ) a = torch.tensor( [[-4.2327, -3.3360, -4.6665, -4.7631, -1.8180, -3.4170, -1.4211, -3.1810]] , dtype=torch.floataa ) torch.testing.assert_close(out.mean(-1 ) , lowerCamelCase_ , atol=1E-2 , rtol=1E-2 ) # fmt: off a = torch.tensor([-9.4922, -3.9551, 1.7998, -5.6758, -5.1055, -5.8984, -4.8320, -6.8086, -6.5391, -5.6172, -5.5820, -5.5352, 1.7881, 3.6289, -6.5117, -3.4785, -9.5000, -6.0352, -6.8125, -6.0195, -6.6836, -5.4727, -6.2812, -6.0391, -7.3398, -7.4297, -7.4844, -6.5820, -5.8789, -5.5312] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] , lowerCamelCase_ , atol=1E-5 , rtol=1E-5 ) @unittest.skip("Model is curently gated" ) @slow def UpperCamelCase_ (self ): """simple docstring""" a = "Simply put, the theory of relativity states that 1) the laws of physics are the same everywhere in the universe and 2) the passage of time and the length of objects can vary depending on the observer\'s frame of reference.\n\nThe first part of the theory, that the laws of physics are the same everywhere, is known as the \"princi" a = "Simply put, the theory of relativity states that " a = LlamaTokenizer.from_pretrained("meta-llama/Llama-2-13b-chat-hf" ) a = tokenizer.encode(lowerCamelCase_ , return_tensors="pt" ) a = LlamaForCausalLM.from_pretrained( "meta-llama/Llama-2-13b-chat-hf" , device_map="sequential" , use_safetensors=lowerCamelCase_ ) # greedy generation outputs a = model.generate(lowerCamelCase_ , max_new_tokens=64 , top_p=lowerCamelCase_ , temperature=1 , do_sample=lowerCamelCase_ ) a = tokenizer.decode(generated_ids[0] , skip_special_tokens=lowerCamelCase_ ) self.assertEqual(lowerCamelCase_ , lowerCamelCase_ )
71
0
from typing import List import jiwer import jiwer.transforms as tr from packaging import version import datasets from datasets.config import PY_VERSION if PY_VERSION < version.parse("""3.8"""): import importlib_metadata else: import importlib.metadata as importlib_metadata UpperCamelCase = """""" if version.parse(importlib_metadata.version("""jiwer""")) < version.parse("""2.3.0"""): class _lowerCamelCase ( tr.AbstractTransform ): """simple docstring""" def __init__( self , _SCREAMING_SNAKE_CASE = " " )->int: '''simple docstring''' A_ : int = sentence_delimiter def _snake_case ( self , _SCREAMING_SNAKE_CASE )->Union[str, Any]: '''simple docstring''' return list(_SCREAMING_SNAKE_CASE ) def _snake_case ( self , _SCREAMING_SNAKE_CASE )->List[Any]: '''simple docstring''' A_ : List[Any] = [] for sent_idx, sentence in enumerate(_SCREAMING_SNAKE_CASE ): chars.extend(self.process_string(_SCREAMING_SNAKE_CASE ) ) if self.sentence_delimiter is not None and self.sentence_delimiter != "" and sent_idx < len(_SCREAMING_SNAKE_CASE ) - 1: chars.append(self.sentence_delimiter ) return chars UpperCamelCase = tr.Compose( [tr.RemoveMultipleSpaces(), tr.Strip(), SentencesToListOfCharacters(SENTENCE_DELIMITER)] ) else: UpperCamelCase = tr.Compose( [ tr.RemoveMultipleSpaces(), tr.Strip(), tr.ReduceToSingleSentence(SENTENCE_DELIMITER), tr.ReduceToListOfListOfChars(), ] ) UpperCamelCase = """\ @inproceedings{inproceedings, author = {Morris, Andrew and Maier, Viktoria and Green, Phil}, year = {2004}, month = {01}, pages = {}, title = {From WER and RIL to MER and WIL: improved evaluation measures for connected speech recognition.} } """ UpperCamelCase = """\ Character error rate (CER) is a common metric of the performance of an automatic speech recognition system. CER is similar to Word Error Rate (WER), but operates on character instead of word. Please refer to docs of WER for further information. Character error rate can be computed as: CER = (S + D + I) / N = (S + D + I) / (S + D + C) where S is the number of substitutions, D is the number of deletions, I is the number of insertions, C is the number of correct characters, N is the number of characters in the reference (N=S+D+C). CER's output is not always a number between 0 and 1, in particular when there is a high number of insertions. This value is often associated to the percentage of characters that were incorrectly predicted. The lower the value, the better the performance of the ASR system with a CER of 0 being a perfect score. """ UpperCamelCase = """ Computes CER score of transcribed segments against references. Args: references: list of references for each speech input. predictions: list of transcribtions to score. concatenate_texts: Whether or not to concatenate sentences before evaluation, set to True for more accurate result. Returns: (float): the character error rate Examples: >>> predictions = [\"this is the prediction\", \"there is an other sample\"] >>> references = [\"this is the reference\", \"there is another one\"] >>> cer = datasets.load_metric(\"cer\") >>> cer_score = cer.compute(predictions=predictions, references=references) >>> print(cer_score) 0.34146341463414637 """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _lowerCamelCase ( datasets.Metric ): """simple docstring""" def _snake_case ( self )->Dict: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , codebase_urls=['''https://github.com/jitsi/jiwer/'''] , reference_urls=[ '''https://en.wikipedia.org/wiki/Word_error_rate''', '''https://sites.google.com/site/textdigitisation/qualitymeasures/computingerrorrates''', ] , ) def _snake_case ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=False )->Optional[Any]: '''simple docstring''' if concatenate_texts: return jiwer.compute_measures( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , truth_transform=_SCREAMING_SNAKE_CASE , hypothesis_transform=_SCREAMING_SNAKE_CASE , )["wer"] A_ : Dict = 0 A_ : List[Any] = 0 for prediction, reference in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): A_ : int = jiwer.compute_measures( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , truth_transform=_SCREAMING_SNAKE_CASE , hypothesis_transform=_SCREAMING_SNAKE_CASE , ) incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"] total += measures["substitutions"] + measures["deletions"] + measures["hits"] return incorrect / total
186
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) UpperCamelCase = { """configuration_roberta""": ["""ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP""", """RobertaConfig""", """RobertaOnnxConfig"""], """tokenization_roberta""": ["""RobertaTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ["""RobertaTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ """ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST""", """RobertaForCausalLM""", """RobertaForMaskedLM""", """RobertaForMultipleChoice""", """RobertaForQuestionAnswering""", """RobertaForSequenceClassification""", """RobertaForTokenClassification""", """RobertaModel""", """RobertaPreTrainedModel""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ """TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFRobertaForCausalLM""", """TFRobertaForMaskedLM""", """TFRobertaForMultipleChoice""", """TFRobertaForQuestionAnswering""", """TFRobertaForSequenceClassification""", """TFRobertaForTokenClassification""", """TFRobertaMainLayer""", """TFRobertaModel""", """TFRobertaPreTrainedModel""", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ """FlaxRobertaForCausalLM""", """FlaxRobertaForMaskedLM""", """FlaxRobertaForMultipleChoice""", """FlaxRobertaForQuestionAnswering""", """FlaxRobertaForSequenceClassification""", """FlaxRobertaForTokenClassification""", """FlaxRobertaModel""", """FlaxRobertaPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_roberta import ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, RobertaConfig, RobertaOnnxConfig from .tokenization_roberta import RobertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roberta_fast import RobertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roberta import ( ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, RobertaForCausalLM, RobertaForMaskedLM, RobertaForMultipleChoice, RobertaForQuestionAnswering, RobertaForSequenceClassification, RobertaForTokenClassification, RobertaModel, RobertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roberta import ( TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFRobertaForCausalLM, TFRobertaForMaskedLM, TFRobertaForMultipleChoice, TFRobertaForQuestionAnswering, TFRobertaForSequenceClassification, TFRobertaForTokenClassification, TFRobertaMainLayer, TFRobertaModel, TFRobertaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, FlaxRobertaPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
186
1
'''simple docstring''' from . import ( albert, align, altclip, audio_spectrogram_transformer, auto, autoformer, bark, bart, barthez, bartpho, beit, bert, bert_generation, bert_japanese, bertweet, big_bird, bigbird_pegasus, biogpt, bit, blenderbot, blenderbot_small, blip, blip_a, bloom, bridgetower, byta, camembert, canine, chinese_clip, clap, clip, clipseg, codegen, conditional_detr, convbert, convnext, convnextva, cpm, cpmant, ctrl, cvt, dataavec, deberta, deberta_va, decision_transformer, deformable_detr, deit, deprecated, deta, detr, dialogpt, dinat, distilbert, dit, donut, dpr, dpt, efficientformer, efficientnet, electra, encodec, encoder_decoder, ernie, ernie_m, esm, falcon, flaubert, flava, fnet, focalnet, fsmt, funnel, git, glpn, gpta, gpt_bigcode, gpt_neo, gpt_neox, gpt_neox_japanese, gpt_swa, gptj, gptsan_japanese, graphormer, groupvit, herbert, hubert, ibert, imagegpt, informer, instructblip, jukebox, layoutlm, layoutlmva, layoutlmva, layoutxlm, led, levit, lilt, llama, longformer, longta, luke, lxmert, mam_aaa, marian, markuplm, maskaformer, maskformer, mbart, mbartaa, mega, megatron_bert, megatron_gpta, mgp_str, mluke, mobilebert, mobilenet_va, mobilenet_va, mobilevit, mobilevitva, mpnet, mra, mta, musicgen, mvp, nat, nezha, nllb, nllb_moe, nystromformer, oneformer, open_llama, openai, opt, owlvit, pegasus, pegasus_x, perceiver, phobert, pixastruct, plbart, poolformer, prophetnet, qdqbert, rag, realm, reformer, regnet, rembert, resnet, roberta, roberta_prelayernorm, roc_bert, roformer, rwkv, sam, segformer, sew, sew_d, speech_encoder_decoder, speech_to_text, speech_to_text_a, speechta, splinter, squeezebert, swiftformer, swin, swinasr, swinva, switch_transformers, ta, table_transformer, tapas, time_series_transformer, timesformer, timm_backbone, transfo_xl, trocr, tvlt, umta, unispeech, unispeech_sat, upernet, videomae, vilt, vision_encoder_decoder, vision_text_dual_encoder, visual_bert, vit, vit_hybrid, vit_mae, vit_msn, vivit, wavaveca, wavaveca_conformer, wavaveca_phoneme, wavaveca_with_lm, wavlm, whisper, x_clip, xglm, xlm, xlm_prophetnet, xlm_roberta, xlm_roberta_xl, xlnet, xmod, yolos, yoso, )
251
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase : Optional[Any] = logging.get_logger(__name__) lowerCAmelCase : int = { 'facebook/timesformer': 'https://huggingface.co/facebook/timesformer/resolve/main/config.json', } class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """timesformer""" def __init__( self , A_=224 , A_=16 , A_=3 , A_=8 , A_=768 , A_=12 , A_=12 , A_=3072 , A_="gelu" , A_=0.0 , A_=0.0 , A_=0.02 , A_=1e-6 , A_=True , A_="divided_space_time" , A_=0 , **A_ , )-> Union[str, Any]: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = image_size UpperCamelCase = patch_size UpperCamelCase = num_channels UpperCamelCase = num_frames UpperCamelCase = hidden_size UpperCamelCase = num_hidden_layers UpperCamelCase = num_attention_heads UpperCamelCase = intermediate_size UpperCamelCase = hidden_act UpperCamelCase = hidden_dropout_prob UpperCamelCase = attention_probs_dropout_prob UpperCamelCase = initializer_range UpperCamelCase = layer_norm_eps UpperCamelCase = qkv_bias UpperCamelCase = attention_type UpperCamelCase = drop_path_rate
251
1