code
stringlengths
82
54.1k
code_codestyle
int64
0
699
style_context
stringlengths
111
35.6k
style_context_codestyle
int64
0
699
label
int64
0
1
from __future__ import annotations from sys import maxsize from typing import Generic, TypeVar UpperCamelCase = TypeVar("T") def A ( lowercase__ : int ) -> int: return (position - 1) // 2 def A ( lowercase__ : int ) -> int: return (2 * position) + 1 def A ( lowercase__ : int ) -> int: return (2 * position) + 2 class lowerCAmelCase_ ( Generic[T] ): """simple docstring""" def __init__( self :Any ): UpperCamelCase__ :list[tuple[T, int]] = [] UpperCamelCase__ :dict[T, int] = {} UpperCamelCase__ :int = 0 def __len__( self :Dict ): return self.elements def __repr__( self :Tuple ): return str(self.heap ) def __a ( self :List[str] ): # Check if the priority queue is empty return self.elements == 0 def __a ( self :List[str] , lowerCamelCase__ :T , lowerCamelCase__ :int ): # Add an element with given priority to the queue self.heap.append((elem, weight) ) UpperCamelCase__ :Dict = self.elements self.elements += 1 self._bubble_up(lowerCamelCase__ ) def __a ( self :Tuple ): # Remove and return the element with lowest weight (highest priority) if self.elements > 1: self._swap_nodes(0 , self.elements - 1 ) UpperCamelCase__ , UpperCamelCase__ :Tuple = self.heap.pop() del self.position_map[elem] self.elements -= 1 if self.elements > 0: UpperCamelCase__ , UpperCamelCase__ :int = self.heap[0] self._bubble_down(lowerCamelCase__ ) return elem def __a ( self :Union[str, Any] , lowerCamelCase__ :T , lowerCamelCase__ :int ): # Update the weight of the given key UpperCamelCase__ :Optional[Any] = self.position_map[elem] UpperCamelCase__ :Dict = (elem, weight) if position > 0: UpperCamelCase__ :Optional[Any] = get_parent_position(lowerCamelCase__ ) UpperCamelCase__ , UpperCamelCase__ :Optional[Any] = self.heap[parent_position] if parent_weight > weight: self._bubble_up(lowerCamelCase__ ) else: self._bubble_down(lowerCamelCase__ ) else: self._bubble_down(lowerCamelCase__ ) def __a ( self :Optional[int] , lowerCamelCase__ :T ): # Place a node at the proper position (upward movement) [to be used internally # only] UpperCamelCase__ :int = self.position_map[elem] if curr_pos == 0: return None UpperCamelCase__ :Any = get_parent_position(lowerCamelCase__ ) UpperCamelCase__ , UpperCamelCase__ :str = self.heap[curr_pos] UpperCamelCase__ , UpperCamelCase__ :List[str] = self.heap[parent_position] if parent_weight > weight: self._swap_nodes(lowerCamelCase__ , lowerCamelCase__ ) return self._bubble_up(lowerCamelCase__ ) return None def __a ( self :Dict , lowerCamelCase__ :T ): # Place a node at the proper position (downward movement) [to be used # internally only] UpperCamelCase__ :Dict = self.position_map[elem] UpperCamelCase__ , UpperCamelCase__ :Tuple = self.heap[curr_pos] UpperCamelCase__ :Optional[int] = get_child_left_position(lowerCamelCase__ ) UpperCamelCase__ :int = get_child_right_position(lowerCamelCase__ ) if child_left_position < self.elements and child_right_position < self.elements: UpperCamelCase__ , UpperCamelCase__ :int = self.heap[child_left_position] UpperCamelCase__ , UpperCamelCase__ :Optional[int] = self.heap[child_right_position] if child_right_weight < child_left_weight and child_right_weight < weight: self._swap_nodes(lowerCamelCase__ , lowerCamelCase__ ) return self._bubble_down(lowerCamelCase__ ) if child_left_position < self.elements: UpperCamelCase__ , UpperCamelCase__ :Optional[Any] = self.heap[child_left_position] if child_left_weight < weight: self._swap_nodes(lowerCamelCase__ , lowerCamelCase__ ) return self._bubble_down(lowerCamelCase__ ) else: return None if child_right_position < self.elements: UpperCamelCase__ , UpperCamelCase__ :Union[str, Any] = self.heap[child_right_position] if child_right_weight < weight: self._swap_nodes(lowerCamelCase__ , lowerCamelCase__ ) return self._bubble_down(lowerCamelCase__ ) return None def __a ( self :List[Any] , lowerCamelCase__ :int , lowerCamelCase__ :int ): # Swap the nodes at the given positions UpperCamelCase__ :Optional[int] = self.heap[nodea_pos][0] UpperCamelCase__ :Optional[int] = self.heap[nodea_pos][0] UpperCamelCase__ , UpperCamelCase__ :Optional[int] = ( self.heap[nodea_pos], self.heap[nodea_pos], ) UpperCamelCase__ :Dict = nodea_pos UpperCamelCase__ :Any = nodea_pos class lowerCAmelCase_ ( Generic[T] ): """simple docstring""" def __init__( self :str ): UpperCamelCase__ :dict[T, dict[T, int]] = {} UpperCamelCase__ :int = 0 def __repr__( self :int ): return str(self.connections ) def __len__( self :Dict ): return self.nodes def __a ( self :List[Any] , lowerCamelCase__ :T ): # Add a node in the graph if it is not in the graph if node not in self.connections: UpperCamelCase__ :Optional[int] = {} self.nodes += 1 def __a ( self :Union[str, Any] , lowerCamelCase__ :T , lowerCamelCase__ :T , lowerCamelCase__ :int ): # Add an edge between 2 nodes in the graph self.add_node(lowerCamelCase__ ) self.add_node(lowerCamelCase__ ) UpperCamelCase__ :Tuple = weight UpperCamelCase__ :int = weight def A ( lowercase__ : GraphUndirectedWeighted[T] , ) -> tuple[dict[T, int], dict[T, T | None]]: UpperCamelCase__ :dict[T, int] = {node: maxsize for node in graph.connections} UpperCamelCase__ :dict[T, T | None] = {node: None for node in graph.connections} UpperCamelCase__ :MinPriorityQueue[T] = MinPriorityQueue() for node, weight in dist.items(): priority_queue.push(lowercase__ , lowercase__ ) if priority_queue.is_empty(): return dist, parent # initialization UpperCamelCase__ :Optional[Any] = priority_queue.extract_min() UpperCamelCase__ :List[str] = 0 for neighbour in graph.connections[node]: if dist[neighbour] > dist[node] + graph.connections[node][neighbour]: UpperCamelCase__ :str = dist[node] + graph.connections[node][neighbour] priority_queue.update_key(lowercase__ , dist[neighbour] ) UpperCamelCase__ :int = node # running prim's algorithm while not priority_queue.is_empty(): UpperCamelCase__ :int = priority_queue.extract_min() for neighbour in graph.connections[node]: if dist[neighbour] > dist[node] + graph.connections[node][neighbour]: UpperCamelCase__ :int = dist[node] + graph.connections[node][neighbour] priority_queue.update_key(lowercase__ , dist[neighbour] ) UpperCamelCase__ :List[Any] = node return dist, parent
45
def A ( lowercase__ : int ) -> bool: if num < 0: return False UpperCamelCase__ :int = num UpperCamelCase__ :int = 0 while num > 0: UpperCamelCase__ :Optional[int] = rev_num * 10 + (num % 10) num //= 10 return num_copy == rev_num if __name__ == "__main__": import doctest doctest.testmod()
45
1
from math import sqrt def A ( lowercase__ : int ) -> bool: if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(sqrt(lowercase__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def A ( lowercase__ : int = 1_0001 ) -> int: UpperCamelCase__ :Dict = 0 UpperCamelCase__ :Union[str, Any] = 1 while count != nth and number < 3: number += 1 if is_prime(lowercase__ ): count += 1 while count != nth: number += 2 if is_prime(lowercase__ ): count += 1 return number if __name__ == "__main__": print(f'''{solution() = }''')
45
from __future__ import annotations def A ( lowercase__ : list[int] ) -> bool: return len(set(lowercase__ ) ) == len(lowercase__ ) if __name__ == "__main__": import doctest doctest.testmod()
45
1
import logging import os from logging import ( CRITICAL, # NOQA DEBUG, # NOQA ERROR, # NOQA FATAL, # NOQA INFO, # NOQA NOTSET, # NOQA WARN, # NOQA WARNING, # NOQA ) from typing import Optional from tqdm import auto as tqdm_lib UpperCamelCase = { "debug": logging.DEBUG, "info": logging.INFO, "warning": logging.WARNING, "error": logging.ERROR, "critical": logging.CRITICAL, } UpperCamelCase = logging.WARNING def A ( ) -> Tuple: UpperCamelCase__ :List[Any] = os.getenv("""DATASETS_VERBOSITY""" , lowercase__ ) if env_level_str: if env_level_str in log_levels: return log_levels[env_level_str] else: logging.getLogger().warning( f"""Unknown option DATASETS_VERBOSITY={env_level_str}, """ f"""has to be one of: { ", ".join(log_levels.keys() ) }""" ) return _default_log_level def A ( ) -> str: return __name__.split(""".""" )[0] def A ( ) -> logging.Logger: return logging.getLogger(_get_library_name() ) def A ( ) -> None: # Apply our default configuration to the library root logger. UpperCamelCase__ :Union[str, Any] = _get_library_root_logger() library_root_logger.setLevel(_get_default_logging_level() ) def A ( ) -> None: UpperCamelCase__ :str = _get_library_root_logger() library_root_logger.setLevel(logging.NOTSET ) def A ( lowercase__ : Optional[str] = None ) -> logging.Logger: if name is None: UpperCamelCase__ :Dict = _get_library_name() return logging.getLogger(lowercase__ ) def A ( ) -> int: return _get_library_root_logger().getEffectiveLevel() def A ( lowercase__ : int ) -> None: _get_library_root_logger().setLevel(lowercase__ ) def A ( ) -> List[str]: return set_verbosity(lowercase__ ) def A ( ) -> List[Any]: return set_verbosity(lowercase__ ) def A ( ) -> Union[str, Any]: return set_verbosity(lowercase__ ) def A ( ) -> Optional[int]: return set_verbosity(lowercase__ ) def A ( ) -> None: UpperCamelCase__ :List[str] = False def A ( ) -> None: UpperCamelCase__ :Tuple = True # Configure the library root logger at the module level (singleton-like) _configure_library_root_logger() class lowerCAmelCase_ : """simple docstring""" def __init__( self :Union[str, Any] , *lowerCamelCase__ :Optional[int] , **lowerCamelCase__ :Dict ): # pylint: disable=unused-argument UpperCamelCase__ :List[Any] = args[0] if args else None def __iter__( self :Any ): return iter(self._iterator ) def __getattr__( self :str , lowerCamelCase__ :Union[str, Any] ): def empty_fn(*lowerCamelCase__ :str , **lowerCamelCase__ :List[Any] ): # pylint: disable=unused-argument return return empty_fn def __enter__( self :str ): return self def __exit__( self :Dict , lowerCamelCase__ :Dict , lowerCamelCase__ :int , lowerCamelCase__ :List[Any] ): return UpperCamelCase = True class lowerCAmelCase_ : """simple docstring""" def __call__( self :str , *lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :List[str]=False , **lowerCamelCase__ :Union[str, Any] ): if _tqdm_active and not disable: return tqdm_lib.tqdm(*lowerCamelCase__ , **lowerCamelCase__ ) else: return EmptyTqdm(*lowerCamelCase__ , **lowerCamelCase__ ) def __a ( self :List[Any] , *lowerCamelCase__ :List[str] , **lowerCamelCase__ :str ): UpperCamelCase__ :List[Any] = None if _tqdm_active: return tqdm_lib.tqdm.set_lock(*lowerCamelCase__ , **lowerCamelCase__ ) def __a ( self :Dict ): if _tqdm_active: return tqdm_lib.tqdm.get_lock() UpperCamelCase = _tqdm_cls() def A ( ) -> bool: global _tqdm_active return bool(_tqdm_active ) def A ( ) -> Optional[Any]: global _tqdm_active UpperCamelCase__ :Optional[int] = True def A ( ) -> Any: global _tqdm_active UpperCamelCase__ :int = False
45
from __future__ import annotations class lowerCAmelCase_ : """simple docstring""" def __init__( self :List[Any] , lowerCamelCase__ :int = 0 ): UpperCamelCase__ :List[str] = key def __a ( self :Optional[Any] , lowerCamelCase__ :str , lowerCamelCase__ :int ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__ :List[str] = key or self.__key or 1 # make sure key is an appropriate size key %= 2_55 return [chr(ord(lowerCamelCase__ ) ^ key ) for ch in content] def __a ( self :int , lowerCamelCase__ :str , lowerCamelCase__ :int ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__ :int = key or self.__key or 1 # make sure key is an appropriate size key %= 2_55 return [chr(ord(lowerCamelCase__ ) ^ key ) for ch in content] def __a ( self :Optional[Any] , lowerCamelCase__ :str , lowerCamelCase__ :int = 0 ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__ :Dict = key or self.__key or 1 # make sure key can be any size while key > 2_55: key -= 2_55 # This will be returned UpperCamelCase__ :List[str] = """""" for ch in content: ans += chr(ord(lowerCamelCase__ ) ^ key ) return ans def __a ( self :Any , lowerCamelCase__ :str , lowerCamelCase__ :int = 0 ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__ :Tuple = key or self.__key or 1 # make sure key can be any size while key > 2_55: key -= 2_55 # This will be returned UpperCamelCase__ :Optional[int] = """""" for ch in content: ans += chr(ord(lowerCamelCase__ ) ^ key ) return ans def __a ( self :Optional[Any] , lowerCamelCase__ :str , lowerCamelCase__ :int = 0 ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) try: with open(lowerCamelCase__ ) as fin, open("""encrypt.out""" , """w+""" ) as fout: # actual encrypt-process for line in fin: fout.write(self.encrypt_string(lowerCamelCase__ , lowerCamelCase__ ) ) except OSError: return False return True def __a ( self :Dict , lowerCamelCase__ :str , lowerCamelCase__ :int ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) try: with open(lowerCamelCase__ ) as fin, open("""decrypt.out""" , """w+""" ) as fout: # actual encrypt-process for line in fin: fout.write(self.decrypt_string(lowerCamelCase__ , lowerCamelCase__ ) ) except OSError: return False return True # Tests # crypt = XORCipher() # key = 67 # # test encrypt # print(crypt.encrypt("hallo welt",key)) # # test decrypt # print(crypt.decrypt(crypt.encrypt("hallo welt",key), key)) # # test encrypt_string # print(crypt.encrypt_string("hallo welt",key)) # # test decrypt_string # print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key)) # if (crypt.encrypt_file("test.txt",key)): # print("encrypt successful") # else: # print("encrypt unsuccessful") # if (crypt.decrypt_file("encrypt.out",key)): # print("decrypt successful") # else: # print("decrypt unsuccessful")
45
1
import dataclasses import re import string from typing import Any, Dict, Iterator, List, Mapping, Optional, Sequence, Tuple import numpy as np from . import residue_constants UpperCamelCase = Mapping[str, np.ndarray] UpperCamelCase = Mapping[str, Any] # Is a nested dict. UpperCamelCase = 0.01 @dataclasses.dataclass(frozen=lowercase ) class lowerCAmelCase_ : """simple docstring""" _snake_case : 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'. _snake_case : 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. _snake_case : np.ndarray # [num_res, num_atom_type] # Residue index as used in PDB. It is not necessarily continuous or 0-indexed. _snake_case : 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. _snake_case : np.ndarray # [num_res, num_atom_type] # Chain indices for multi-chain predictions _snake_case : Optional[np.ndarray] = None # Optional remark about the protein. Included as a comment in output PDB # files _snake_case : Optional[str] = None # Templates used to generate this protein (prediction-only) _snake_case : Optional[Sequence[str]] = None # Chain corresponding to each parent _snake_case : Optional[Sequence[int]] = None def A ( lowercase__ : str ) -> Protein: UpperCamelCase__ :Union[str, Any] = r"""(\[[A-Z]+\]\n)""" UpperCamelCase__ :List[str] = [tag.strip() for tag in re.split(lowercase__ , lowercase__ ) if len(lowercase__ ) > 0] UpperCamelCase__ :Iterator[Tuple[str, List[str]]] = zip(tags[0::2] , [l.split("""\n""" ) for l in tags[1::2]] ) UpperCamelCase__ :List[str] = ["N", "CA", "C"] UpperCamelCase__ :Optional[int] = None UpperCamelCase__ :Optional[int] = None UpperCamelCase__ :List[str] = None for g in groups: if "[PRIMARY]" == g[0]: UpperCamelCase__ :List[Any] = g[1][0].strip() for i in range(len(lowercase__ ) ): if seq[i] not in residue_constants.restypes: UpperCamelCase__ :List[str] = """X""" # FIXME: strings are immutable UpperCamelCase__ :List[Any] = np.array( [residue_constants.restype_order.get(lowercase__ , residue_constants.restype_num ) for res_symbol in seq] ) elif "[TERTIARY]" == g[0]: UpperCamelCase__ :List[List[float]] = [] for axis in range(3 ): tertiary.append(list(map(lowercase__ , g[1][axis].split() ) ) ) UpperCamelCase__ :Tuple = np.array(lowercase__ ) UpperCamelCase__ :Tuple = np.zeros((len(tertiary[0] ) // 3, residue_constants.atom_type_num, 3) ).astype(np.floataa ) for i, atom in enumerate(lowercase__ ): UpperCamelCase__ :Optional[int] = np.transpose(tertiary_np[:, i::3] ) atom_positions *= PICO_TO_ANGSTROM elif "[MASK]" == g[0]: UpperCamelCase__ :Dict = np.array(list(map({"""-""": 0, """+""": 1}.get , g[1][0].strip() ) ) ) UpperCamelCase__ :Any = np.zeros( ( len(lowercase__ ), residue_constants.atom_type_num, ) ).astype(np.floataa ) for i, atom in enumerate(lowercase__ ): UpperCamelCase__ :List[Any] = 1 atom_mask *= mask[..., None] assert aatype is not None return Protein( atom_positions=lowercase__ , atom_mask=lowercase__ , aatype=lowercase__ , residue_index=np.arange(len(lowercase__ ) ) , b_factors=lowercase__ , ) def A ( lowercase__ : Protein , lowercase__ : int = 0 ) -> List[str]: UpperCamelCase__ :List[str] = [] UpperCamelCase__ :Optional[Any] = prot.remark if remark is not None: pdb_headers.append(f"""REMARK {remark}""" ) UpperCamelCase__ :List[Any] = prot.parents UpperCamelCase__ :List[Any] = prot.parents_chain_index if parents is not None and parents_chain_index is not None: UpperCamelCase__ :List[Any] = [p for i, p in zip(lowercase__ , lowercase__ ) if i == chain_id] if parents is None or len(lowercase__ ) == 0: UpperCamelCase__ :str = ["""N/A"""] pdb_headers.append(f"""PARENT {" ".join(lowercase__ )}""" ) return pdb_headers def A ( lowercase__ : Protein , lowercase__ : str ) -> str: UpperCamelCase__ :List[str] = [] UpperCamelCase__ :Optional[int] = pdb_str.split("""\n""" ) UpperCamelCase__ :Tuple = prot.remark if remark is not None: out_pdb_lines.append(f"""REMARK {remark}""" ) UpperCamelCase__ :List[List[str]] if prot.parents is not None and len(prot.parents ) > 0: UpperCamelCase__ :Any = [] if prot.parents_chain_index is not None: UpperCamelCase__ :Dict[str, List[str]] = {} for p, i in zip(prot.parents , prot.parents_chain_index ): parent_dict.setdefault(str(lowercase__ ) , [] ) parent_dict[str(lowercase__ )].append(lowercase__ ) UpperCamelCase__ :Optional[Any] = max([int(lowercase__ ) for chain_idx in parent_dict] ) for i in range(max_idx + 1 ): UpperCamelCase__ :Union[str, Any] = parent_dict.get(str(lowercase__ ) , ["""N/A"""] ) parents_per_chain.append(lowercase__ ) else: parents_per_chain.append(list(prot.parents ) ) else: UpperCamelCase__ :Union[str, Any] = [["""N/A"""]] def make_parent_line(lowercase__ : Sequence[str] ) -> str: return f"""PARENT {" ".join(lowercase__ )}""" out_pdb_lines.append(make_parent_line(parents_per_chain[0] ) ) UpperCamelCase__ :Optional[int] = 0 for i, l in enumerate(lowercase__ ): if "PARENT" not in l and "REMARK" not in l: out_pdb_lines.append(lowercase__ ) if "TER" in l and "END" not in lines[i + 1]: chain_counter += 1 if not chain_counter >= len(lowercase__ ): UpperCamelCase__ :Optional[int] = parents_per_chain[chain_counter] else: UpperCamelCase__ :str = ["""N/A"""] out_pdb_lines.append(make_parent_line(lowercase__ ) ) return "\n".join(lowercase__ ) def A ( lowercase__ : Protein ) -> str: UpperCamelCase__ :Optional[int] = residue_constants.restypes + ["""X"""] def res_atoa(lowercase__ : int ) -> str: return residue_constants.restype_atoa.get(restypes[r] , """UNK""" ) UpperCamelCase__ :Optional[Any] = residue_constants.atom_types UpperCamelCase__ :List[str] = [] UpperCamelCase__ :Dict = prot.atom_mask UpperCamelCase__ :Dict = prot.aatype UpperCamelCase__ :List[str] = prot.atom_positions UpperCamelCase__ :Dict = prot.residue_index.astype(np.intaa ) UpperCamelCase__ :Optional[int] = prot.b_factors UpperCamelCase__ :Dict = prot.chain_index if np.any(aatype > residue_constants.restype_num ): raise ValueError("""Invalid aatypes.""" ) UpperCamelCase__ :Any = get_pdb_headers(lowercase__ ) if len(lowercase__ ) > 0: pdb_lines.extend(lowercase__ ) UpperCamelCase__ :Union[str, Any] = aatype.shape[0] UpperCamelCase__ :Union[str, Any] = 1 UpperCamelCase__ :Tuple = 0 UpperCamelCase__ :Union[str, Any] = string.ascii_uppercase UpperCamelCase__ :Tuple = None # Add all atom sites. for i in range(lowercase__ ): UpperCamelCase__ :str = res_atoa(aatype[i] ) for atom_name, pos, mask, b_factor in zip(lowercase__ , atom_positions[i] , atom_mask[i] , b_factors[i] ): if mask < 0.5: continue UpperCamelCase__ :Optional[int] = """ATOM""" UpperCamelCase__ :Union[str, Any] = atom_name if len(lowercase__ ) == 4 else f""" {atom_name}""" UpperCamelCase__ :Union[str, Any] = """""" UpperCamelCase__ :Dict = """""" UpperCamelCase__ :List[Any] = 1.00 UpperCamelCase__ :Any = atom_name[0] # Protein supports only C, N, O, S, this works. UpperCamelCase__ :int = """""" UpperCamelCase__ :Union[str, Any] = """A""" if chain_index is not None: UpperCamelCase__ :List[Any] = chain_tags[chain_index[i]] # PDB is a columnar format, every space matters here! UpperCamelCase__ :int = ( 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(lowercase__ ) atom_index += 1 UpperCamelCase__ :Dict = i == n - 1 if chain_index is not None: if i != n - 1 and chain_index[i + 1] != prev_chain_index: UpperCamelCase__ :List[str] = True UpperCamelCase__ :int = chain_index[i + 1] if should_terminate: # Close the chain. UpperCamelCase__ :Tuple = """TER""" UpperCamelCase__ :Any = ( f"""{chain_end:<6}{atom_index:>5} {res_atoa(aatype[i] ):>3} {chain_tag:>1}{residue_index[i]:>4}""" ) pdb_lines.append(lowercase__ ) 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(lowercase__ , lowercase__ ) ) pdb_lines.append("""END""" ) pdb_lines.append("""""" ) return "\n".join(lowercase__ ) def A ( lowercase__ : Protein ) -> np.ndarray: return residue_constants.STANDARD_ATOM_MASK[prot.aatype] def A ( lowercase__ : FeatureDict , lowercase__ : ModelOutput , lowercase__ : Optional[np.ndarray] = None , lowercase__ : Optional[np.ndarray] = None , lowercase__ : Optional[str] = None , lowercase__ : Optional[Sequence[str]] = None , lowercase__ : Optional[Sequence[int]] = 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=lowercase__ , remark=lowercase__ , parents=lowercase__ , parents_chain_index=lowercase__ , )
45
import random def A ( lowercase__ : Dict , lowercase__ : str , lowercase__ : Optional[Any] ) -> int: UpperCamelCase__ :List[Any] = a[left_index] UpperCamelCase__ :Dict = left_index + 1 for j in range(left_index + 1 , lowercase__ ): if a[j] < pivot: UpperCamelCase__ , UpperCamelCase__ :Optional[int] = a[i], a[j] i += 1 UpperCamelCase__ , UpperCamelCase__ :Tuple = a[i - 1], a[left_index] return i - 1 def A ( lowercase__ : Tuple , lowercase__ : Optional[int] , lowercase__ : Any ) -> Optional[int]: if left < right: UpperCamelCase__ :List[Any] = random.randint(lowercase__ , right - 1 ) UpperCamelCase__ , UpperCamelCase__ :Union[str, Any] = ( a[left], a[pivot], ) # switches the pivot with the left most bound UpperCamelCase__ :int = partition(lowercase__ , lowercase__ , lowercase__ ) quick_sort_random( lowercase__ , lowercase__ , lowercase__ ) # recursive quicksort to the left of the pivot point quick_sort_random( lowercase__ , pivot_index + 1 , lowercase__ ) # recursive quicksort to the right of the pivot point def A ( ) -> List[Any]: UpperCamelCase__ :str = input("""Enter numbers separated by a comma:\n""" ).strip() UpperCamelCase__ :int = [int(lowercase__ ) for item in user_input.split(""",""" )] quick_sort_random(lowercase__ , 0 , len(lowercase__ ) ) print(lowercase__ ) if __name__ == "__main__": main()
45
1
import unittest import numpy as np import torch from diffusers import PNDMPipeline, PNDMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" @property def __a ( self :int ): torch.manual_seed(0 ) UpperCamelCase__ :Optional[int] = 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 __a ( self :Optional[int] ): UpperCamelCase__ :Optional[Any] = self.dummy_uncond_unet UpperCamelCase__ :List[str] = PNDMScheduler() UpperCamelCase__ :Union[str, Any] = PNDMPipeline(unet=lowerCamelCase__ , scheduler=lowerCamelCase__ ) pndm.to(lowerCamelCase__ ) pndm.set_progress_bar_config(disable=lowerCamelCase__ ) UpperCamelCase__ :List[Any] = torch.manual_seed(0 ) UpperCamelCase__ :List[Any] = pndm(generator=lowerCamelCase__ , num_inference_steps=20 , output_type="""numpy""" ).images UpperCamelCase__ :Tuple = torch.manual_seed(0 ) UpperCamelCase__ :Tuple = pndm(generator=lowerCamelCase__ , num_inference_steps=20 , output_type="""numpy""" , return_dict=lowerCamelCase__ )[0] UpperCamelCase__ :Optional[Any] = image[0, -3:, -3:, -1] UpperCamelCase__ :Dict = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) UpperCamelCase__ :Optional[Any] = np.array([1.0, 1.0, 0.0, 1.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 lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __a ( self :Dict ): UpperCamelCase__ :Tuple = """google/ddpm-cifar10-32""" UpperCamelCase__ :Optional[Any] = UNetaDModel.from_pretrained(lowerCamelCase__ ) UpperCamelCase__ :Dict = PNDMScheduler() UpperCamelCase__ :Union[str, Any] = PNDMPipeline(unet=lowerCamelCase__ , scheduler=lowerCamelCase__ ) pndm.to(lowerCamelCase__ ) pndm.set_progress_bar_config(disable=lowerCamelCase__ ) UpperCamelCase__ :int = torch.manual_seed(0 ) UpperCamelCase__ :Tuple = pndm(generator=lowerCamelCase__ , output_type="""numpy""" ).images UpperCamelCase__ :str = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) UpperCamelCase__ :Dict = np.array([0.1564, 0.1_4645, 0.1406, 0.1_4715, 0.1_2425, 0.1_4045, 0.1_3115, 0.1_2175, 0.125] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
45
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { "shi-labs/dinat-mini-in1k-224": "https://huggingface.co/shi-labs/dinat-mini-in1k-224/resolve/main/config.json", # See all Dinat models at https://huggingface.co/models?filter=dinat } class lowerCAmelCase_ ( lowercase , lowercase ): """simple docstring""" _snake_case : Tuple = """dinat""" _snake_case : List[Any] = { """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers""", } def __init__( self :Optional[int] , lowerCamelCase__ :int=4 , lowerCamelCase__ :Union[str, Any]=3 , lowerCamelCase__ :List[Any]=64 , lowerCamelCase__ :Any=[3, 4, 6, 5] , lowerCamelCase__ :Tuple=[2, 4, 8, 16] , lowerCamelCase__ :Optional[int]=7 , lowerCamelCase__ :Tuple=[[1, 8, 1], [1, 4, 1, 4], [1, 2, 1, 2, 1, 2], [1, 1, 1, 1, 1]] , lowerCamelCase__ :Tuple=3.0 , lowerCamelCase__ :str=True , lowerCamelCase__ :Optional[int]=0.0 , lowerCamelCase__ :Optional[Any]=0.0 , lowerCamelCase__ :int=0.1 , lowerCamelCase__ :Optional[Any]="gelu" , lowerCamelCase__ :Optional[Any]=0.02 , lowerCamelCase__ :Union[str, Any]=1e-5 , lowerCamelCase__ :Optional[int]=0.0 , lowerCamelCase__ :List[str]=None , lowerCamelCase__ :str=None , **lowerCamelCase__ :List[Any] , ): super().__init__(**lowerCamelCase__ ) UpperCamelCase__ :Any = patch_size UpperCamelCase__ :Any = num_channels UpperCamelCase__ :int = embed_dim UpperCamelCase__ :Optional[Any] = depths UpperCamelCase__ :Any = len(lowerCamelCase__ ) UpperCamelCase__ :str = num_heads UpperCamelCase__ :Optional[int] = kernel_size UpperCamelCase__ :Optional[int] = dilations UpperCamelCase__ :Tuple = mlp_ratio UpperCamelCase__ :Dict = qkv_bias UpperCamelCase__ :List[str] = hidden_dropout_prob UpperCamelCase__ :List[str] = attention_probs_dropout_prob UpperCamelCase__ :Union[str, Any] = drop_path_rate UpperCamelCase__ :Tuple = hidden_act UpperCamelCase__ :List[Any] = layer_norm_eps UpperCamelCase__ :Optional[Any] = initializer_range # we set the hidden_size attribute in order to make Dinat work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model UpperCamelCase__ :Tuple = int(embed_dim * 2 ** (len(lowerCamelCase__ ) - 1) ) UpperCamelCase__ :Tuple = layer_scale_init_value UpperCamelCase__ :Optional[int] = ["""stem"""] + [f"""stage{idx}""" for idx in range(1 , len(lowerCamelCase__ ) + 1 )] UpperCamelCase__ , UpperCamelCase__ :List[str] = get_aligned_output_features_output_indices( out_features=lowerCamelCase__ , out_indices=lowerCamelCase__ , stage_names=self.stage_names )
45
1
import json import os from typing import Dict, List, Optional, Tuple import regex as re from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { "vocab_file": "vocab.json", "merges_file": "merges.txt", "tokenizer_config_file": "tokenizer_config.json", } UpperCamelCase = { "vocab_file": { "facebook/blenderbot_small-90M": "https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/vocab.json" }, "merges_file": { "facebook/blenderbot_small-90M": "https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/merges.txt" }, "tokenizer_config_file": { "facebook/blenderbot_small-90M": ( "https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/tokenizer_config.json" ) }, } UpperCamelCase = {"facebook/blenderbot_small-90M": 512} def A ( lowercase__ : Optional[int] ) -> List[Any]: UpperCamelCase__ :Union[str, Any] = set() UpperCamelCase__ :List[str] = word[0] for char in word[1:]: pairs.add((prev_char, char) ) UpperCamelCase__ :List[Any] = char UpperCamelCase__ :Any = set(lowercase__ ) return pairs class lowerCAmelCase_ ( lowercase ): """simple docstring""" _snake_case : str = VOCAB_FILES_NAMES _snake_case : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP _snake_case : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _snake_case : str = ["""input_ids""", """attention_mask"""] def __init__( self :List[str] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :str , lowerCamelCase__ :Tuple="__start__" , lowerCamelCase__ :Union[str, Any]="__end__" , lowerCamelCase__ :Dict="__unk__" , lowerCamelCase__ :List[Any]="__null__" , **lowerCamelCase__ :str , ): super().__init__(unk_token=lowerCamelCase__ , bos_token=lowerCamelCase__ , eos_token=lowerCamelCase__ , pad_token=lowerCamelCase__ , **lowerCamelCase__ ) with open(lowerCamelCase__ , encoding="""utf-8""" ) as vocab_handle: UpperCamelCase__ :Optional[int] = json.load(lowerCamelCase__ ) UpperCamelCase__ :List[str] = {v: k for k, v in self.encoder.items()} with open(lowerCamelCase__ , encoding="""utf-8""" ) as merges_handle: UpperCamelCase__ :Union[str, Any] = merges_handle.read().split("""\n""" )[1:-1] UpperCamelCase__ :List[Any] = [tuple(merge.split() ) for merge in merges] UpperCamelCase__ :List[str] = dict(zip(lowerCamelCase__ , range(len(lowerCamelCase__ ) ) ) ) UpperCamelCase__ :Union[str, Any] = {} @property def __a ( self :Dict ): return len(self.encoder ) def __a ( self :Optional[int] ): return dict(self.encoder , **self.added_tokens_encoder ) def __a ( self :Union[str, Any] , lowerCamelCase__ :str ): if token in self.cache: return self.cache[token] UpperCamelCase__ :List[str] = re.sub("""([.,!?()])""" , r""" \1""" , lowerCamelCase__ ) UpperCamelCase__ :Optional[int] = re.sub("""(')""" , r""" \1 """ , lowerCamelCase__ ) UpperCamelCase__ :Dict = re.sub(r"""\s{2,}""" , """ """ , lowerCamelCase__ ) if "\n" in token: UpperCamelCase__ :Dict = token.replace("""\n""" , """ __newln__""" ) UpperCamelCase__ :Dict = token.split(""" """ ) UpperCamelCase__ :Union[str, Any] = [] for token in tokens: if not len(lowerCamelCase__ ): continue UpperCamelCase__ :Any = token.lower() UpperCamelCase__ :Tuple = tuple(lowerCamelCase__ ) UpperCamelCase__ :List[str] = tuple(list(word[:-1] ) + [word[-1] + """</w>"""] ) UpperCamelCase__ :Optional[Any] = get_pairs(lowerCamelCase__ ) if not pairs: words.append(lowerCamelCase__ ) continue while True: UpperCamelCase__ :Optional[Any] = min(lowerCamelCase__ , key=lambda lowerCamelCase__ : self.bpe_ranks.get(lowerCamelCase__ , float("""inf""" ) ) ) if bigram not in self.bpe_ranks: break UpperCamelCase__ , UpperCamelCase__ :int = bigram UpperCamelCase__ :List[str] = [] UpperCamelCase__ :List[str] = 0 while i < len(lowerCamelCase__ ): try: UpperCamelCase__ :Optional[int] = word.index(lowerCamelCase__ , lowerCamelCase__ ) new_word.extend(word[i:j] ) UpperCamelCase__ :Dict = j except ValueError: new_word.extend(word[i:] ) break if word[i] == first and i < len(lowerCamelCase__ ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 UpperCamelCase__ :Dict = tuple(lowerCamelCase__ ) UpperCamelCase__ :Any = new_word if len(lowerCamelCase__ ) == 1: break else: UpperCamelCase__ :str = get_pairs(lowerCamelCase__ ) UpperCamelCase__ :Optional[int] = """@@ """.join(lowerCamelCase__ ) UpperCamelCase__ :List[str] = word[:-4] UpperCamelCase__ :Optional[int] = word words.append(lowerCamelCase__ ) return " ".join(lowerCamelCase__ ) def __a ( self :Dict , lowerCamelCase__ :str ): UpperCamelCase__ :Dict = [] UpperCamelCase__ :List[Any] = re.findall(r"""\S+\n?""" , lowerCamelCase__ ) for token in words: split_tokens.extend(list(self.bpe(lowerCamelCase__ ).split(""" """ ) ) ) return split_tokens def __a ( self :Union[str, Any] , lowerCamelCase__ :str ): UpperCamelCase__ :List[Any] = token.lower() return self.encoder.get(lowerCamelCase__ , self.encoder.get(self.unk_token ) ) def __a ( self :Optional[int] , lowerCamelCase__ :int ): return self.decoder.get(lowerCamelCase__ , self.unk_token ) def __a ( self :Optional[Any] , lowerCamelCase__ :List[str] ): UpperCamelCase__ :int = """ """.join(lowerCamelCase__ ).replace("""@@ """ , """""" ).strip() return out_string def __a ( self :Optional[int] , lowerCamelCase__ :str , lowerCamelCase__ :Optional[str] = None ): if not os.path.isdir(lowerCamelCase__ ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return UpperCamelCase__ :Dict = os.path.join( lowerCamelCase__ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) UpperCamelCase__ :List[Any] = os.path.join( lowerCamelCase__ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""merges_file"""] ) with open(lowerCamelCase__ , """w""" , encoding="""utf-8""" ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=lowerCamelCase__ , ensure_ascii=lowerCamelCase__ ) + """\n""" ) UpperCamelCase__ :Optional[Any] = 0 with open(lowerCamelCase__ , """w""" , encoding="""utf-8""" ) as writer: writer.write("""#version: 0.2\n""" ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda lowerCamelCase__ : kv[1] ): if index != token_index: logger.warning( f"""Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.""" """ Please check that the tokenizer is not corrupted!""" ) UpperCamelCase__ :str = token_index writer.write(""" """.join(lowerCamelCase__ ) + """\n""" ) index += 1 return vocab_file, merge_file
45
def A ( lowercase__ : int , lowercase__ : int ) -> int: return int(input_a == input_a == 0 ) def A ( ) -> None: print("""Truth Table of NOR Gate:""" ) print("""| Input 1 | Input 2 | Output |""" ) print(f"""| 0 | 0 | {nor_gate(0 , 0 )} |""" ) print(f"""| 0 | 1 | {nor_gate(0 , 1 )} |""" ) print(f"""| 1 | 0 | {nor_gate(1 , 0 )} |""" ) print(f"""| 1 | 1 | {nor_gate(1 , 1 )} |""" ) if __name__ == "__main__": import doctest doctest.testmod() main()
45
1
import numpy as np import torch import torch.nn as nn from transformers import CLIPConfig, CLIPVisionModelWithProjection, PreTrainedModel from ...utils import logging UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( lowercase ): """simple docstring""" _snake_case : Any = CLIPConfig _snake_case : List[Any] = ["""CLIPEncoderLayer"""] def __init__( self :Union[str, Any] , lowerCamelCase__ :CLIPConfig ): super().__init__(lowerCamelCase__ ) UpperCamelCase__ :List[str] = CLIPVisionModelWithProjection(config.vision_config ) UpperCamelCase__ :Optional[int] = nn.Linear(config.vision_config.projection_dim , 1 ) UpperCamelCase__ :Optional[Any] = nn.Linear(config.vision_config.projection_dim , 1 ) @torch.no_grad() def __a ( self :Optional[Any] , lowerCamelCase__ :Any , lowerCamelCase__ :Tuple , lowerCamelCase__ :Optional[int]=0.5 , lowerCamelCase__ :Tuple=0.5 ): UpperCamelCase__ :Optional[int] = self.vision_model(lowerCamelCase__ )[0] UpperCamelCase__ :str = self.p_head(lowerCamelCase__ ) UpperCamelCase__ :List[Any] = nsfw_detected.flatten() UpperCamelCase__ :Optional[int] = nsfw_detected > p_threshold UpperCamelCase__ :Dict = nsfw_detected.tolist() if any(lowerCamelCase__ ): logger.warning( """Potential NSFW content was detected in one or more images. A black image will be returned instead.""" """ Try again with a different prompt and/or seed.""" ) for idx, nsfw_detected_ in enumerate(lowerCamelCase__ ): if nsfw_detected_: UpperCamelCase__ :List[str] = np.zeros(images[idx].shape ) UpperCamelCase__ :Optional[Any] = self.w_head(lowerCamelCase__ ) UpperCamelCase__ :Dict = watermark_detected.flatten() UpperCamelCase__ :List[str] = watermark_detected > w_threshold UpperCamelCase__ :str = watermark_detected.tolist() if any(lowerCamelCase__ ): logger.warning( """Potential watermarked content was detected in one or more images. A black image will be returned instead.""" """ Try again with a different prompt and/or seed.""" ) for idx, watermark_detected_ in enumerate(lowerCamelCase__ ): if watermark_detected_: UpperCamelCase__ :Any = np.zeros(images[idx].shape ) return images, nsfw_detected, watermark_detected
45
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import GLPNImageProcessor class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __init__( self :Any , lowerCamelCase__ :Dict , lowerCamelCase__ :Optional[Any]=7 , lowerCamelCase__ :str=3 , lowerCamelCase__ :Optional[Any]=18 , lowerCamelCase__ :List[str]=30 , lowerCamelCase__ :str=4_00 , lowerCamelCase__ :Optional[int]=True , lowerCamelCase__ :Union[str, Any]=32 , lowerCamelCase__ :int=True , ): UpperCamelCase__ :List[Any] = parent UpperCamelCase__ :List[Any] = batch_size UpperCamelCase__ :Any = num_channels UpperCamelCase__ :List[str] = image_size UpperCamelCase__ :Dict = min_resolution UpperCamelCase__ :List[str] = max_resolution UpperCamelCase__ :str = do_resize UpperCamelCase__ :int = size_divisor UpperCamelCase__ :Optional[int] = do_rescale def __a ( self :str ): return { "do_resize": self.do_resize, "size_divisor": self.size_divisor, "do_rescale": self.do_rescale, } @require_torch @require_vision class lowerCAmelCase_ ( lowercase , unittest.TestCase ): """simple docstring""" _snake_case : Optional[int] = GLPNImageProcessor if is_vision_available() else None def __a ( self :Dict ): UpperCamelCase__ :Dict = GLPNImageProcessingTester(self ) @property def __a ( self :List[str] ): return self.image_processor_tester.prepare_image_processor_dict() def __a ( self :Optional[int] ): UpperCamelCase__ :Optional[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(lowerCamelCase__ , """do_resize""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """size_divisor""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """resample""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """do_rescale""" ) ) def __a ( self :Optional[int] ): pass def __a ( self :Tuple ): # Initialize image_processing UpperCamelCase__ :int = self.image_processing_class(**self.image_processor_dict ) # create random PIL images UpperCamelCase__ :str = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCamelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCamelCase__ , Image.Image ) # Test not batched input (GLPNImageProcessor doesn't support batching) UpperCamelCase__ :Tuple = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 ) self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 ) def __a ( self :str ): # Initialize image_processing UpperCamelCase__ :str = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors UpperCamelCase__ :Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCamelCase__ , numpify=lowerCamelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCamelCase__ , np.ndarray ) # Test not batched input (GLPNImageProcessor doesn't support batching) UpperCamelCase__ :List[str] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 ) self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 ) def __a ( self :Any ): # Initialize image_processing UpperCamelCase__ :List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors UpperCamelCase__ :Tuple = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCamelCase__ , torchify=lowerCamelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCamelCase__ , torch.Tensor ) # Test not batched input (GLPNImageProcessor doesn't support batching) UpperCamelCase__ :List[str] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 ) self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 )
45
1
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 = { "andreasmadsen/efficient_mlm_m0.40": ( "https://huggingface.co/andreasmadsen/efficient_mlm_m0.40/resolve/main/config.json" ), } class lowerCAmelCase_ ( lowercase ): """simple docstring""" _snake_case : Optional[Any] = """roberta-prelayernorm""" def __init__( self :Any , lowerCamelCase__ :List[Any]=5_02_65 , lowerCamelCase__ :Union[str, Any]=7_68 , lowerCamelCase__ :List[str]=12 , lowerCamelCase__ :List[Any]=12 , lowerCamelCase__ :str=30_72 , lowerCamelCase__ :Dict="gelu" , lowerCamelCase__ :Union[str, Any]=0.1 , lowerCamelCase__ :Tuple=0.1 , lowerCamelCase__ :Optional[Any]=5_12 , lowerCamelCase__ :List[str]=2 , lowerCamelCase__ :Any=0.02 , lowerCamelCase__ :str=1e-12 , lowerCamelCase__ :Union[str, Any]=1 , lowerCamelCase__ :str=0 , lowerCamelCase__ :str=2 , lowerCamelCase__ :str="absolute" , lowerCamelCase__ :Union[str, Any]=True , lowerCamelCase__ :str=None , **lowerCamelCase__ :Any , ): super().__init__(pad_token_id=lowerCamelCase__ , bos_token_id=lowerCamelCase__ , eos_token_id=lowerCamelCase__ , **lowerCamelCase__ ) UpperCamelCase__ :Optional[int] = vocab_size UpperCamelCase__ :int = hidden_size UpperCamelCase__ :Any = num_hidden_layers UpperCamelCase__ :Optional[Any] = num_attention_heads UpperCamelCase__ :Tuple = hidden_act UpperCamelCase__ :str = intermediate_size UpperCamelCase__ :List[Any] = hidden_dropout_prob UpperCamelCase__ :Union[str, Any] = attention_probs_dropout_prob UpperCamelCase__ :str = max_position_embeddings UpperCamelCase__ :Optional[int] = type_vocab_size UpperCamelCase__ :Any = initializer_range UpperCamelCase__ :List[str] = layer_norm_eps UpperCamelCase__ :Tuple = position_embedding_type UpperCamelCase__ :int = use_cache UpperCamelCase__ :List[Any] = classifier_dropout class lowerCAmelCase_ ( lowercase ): """simple docstring""" @property def __a ( self :List[Any] ): if self.task == "multiple-choice": UpperCamelCase__ :List[str] = {0: """batch""", 1: """choice""", 2: """sequence"""} else: UpperCamelCase__ :List[str] = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
45
import math def A ( lowercase__ : Tuple , lowercase__ : Union[str, Any] ) -> Optional[Any]: if 0 not in (x, y): # We use the relation x^y = y*log10(x), where 10 is the base. return y * math.logaa(lowercase__ ) else: if x == 0: # 0 raised to any number is 0 return 0 elif y == 0: return 1 # any number raised to 0 is 1 raise AssertionError("""This should never happen""" ) if __name__ == "__main__": # Main function # Read two numbers from input and typecast them to int using map function. # Here x is the base and y is the power. UpperCamelCase = "Enter the base and the power separated by a comma: " UpperCamelCase , UpperCamelCase = map(int, input(prompt).split(",")) UpperCamelCase , UpperCamelCase = map(int, input(prompt).split(",")) # We find the log of each number, using the function res(), which takes two # arguments. UpperCamelCase = res(xa, ya) UpperCamelCase = res(xa, ya) # We check for the largest number if resa > resa: print("Largest number is", xa, "^", ya) elif resa > resa: print("Largest number is", xa, "^", ya) else: print("Both are equal")
45
1
from __future__ import annotations import unittest import numpy as np from transformers import LayoutLMConfig, 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.models.layoutlm.modeling_tf_layoutlm import ( TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFLayoutLMForMaskedLM, TFLayoutLMForQuestionAnswering, TFLayoutLMForSequenceClassification, TFLayoutLMForTokenClassification, TFLayoutLMModel, ) class lowerCAmelCase_ : """simple docstring""" def __init__( self :Optional[Any] , lowerCamelCase__ :List[str] , lowerCamelCase__ :Tuple=13 , lowerCamelCase__ :Union[str, Any]=7 , lowerCamelCase__ :int=True , lowerCamelCase__ :Tuple=True , lowerCamelCase__ :Any=True , lowerCamelCase__ :Optional[int]=True , lowerCamelCase__ :Dict=99 , lowerCamelCase__ :Dict=32 , lowerCamelCase__ :Union[str, Any]=2 , lowerCamelCase__ :Optional[int]=4 , lowerCamelCase__ :Any=37 , lowerCamelCase__ :Optional[int]="gelu" , lowerCamelCase__ :int=0.1 , lowerCamelCase__ :int=0.1 , lowerCamelCase__ :Union[str, Any]=5_12 , lowerCamelCase__ :List[Any]=16 , lowerCamelCase__ :int=2 , lowerCamelCase__ :Optional[int]=0.02 , lowerCamelCase__ :Tuple=3 , lowerCamelCase__ :Optional[Any]=4 , lowerCamelCase__ :str=None , lowerCamelCase__ :Tuple=10_00 , ): UpperCamelCase__ :Dict = parent UpperCamelCase__ :int = batch_size UpperCamelCase__ :str = seq_length UpperCamelCase__ :Dict = is_training UpperCamelCase__ :Dict = use_input_mask UpperCamelCase__ :Optional[int] = use_token_type_ids UpperCamelCase__ :Optional[int] = use_labels UpperCamelCase__ :Tuple = vocab_size UpperCamelCase__ :Any = hidden_size UpperCamelCase__ :Union[str, Any] = num_hidden_layers UpperCamelCase__ :List[str] = num_attention_heads UpperCamelCase__ :Any = intermediate_size UpperCamelCase__ :int = hidden_act UpperCamelCase__ :Union[str, Any] = hidden_dropout_prob UpperCamelCase__ :Optional[Any] = attention_probs_dropout_prob UpperCamelCase__ :List[Any] = max_position_embeddings UpperCamelCase__ :List[str] = type_vocab_size UpperCamelCase__ :Optional[int] = type_sequence_label_size UpperCamelCase__ :Tuple = initializer_range UpperCamelCase__ :List[Any] = num_labels UpperCamelCase__ :int = num_choices UpperCamelCase__ :str = scope UpperCamelCase__ :Tuple = range_bbox def __a ( self :Dict ): UpperCamelCase__ :Dict = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) # convert bbox to numpy since TF does not support item assignment UpperCamelCase__ :Optional[int] = ids_tensor([self.batch_size, self.seq_length, 4] , self.range_bbox ).numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: UpperCamelCase__ :Dict = bbox[i, j, 3] UpperCamelCase__ :Dict = bbox[i, j, 1] UpperCamelCase__ :Dict = t if bbox[i, j, 2] < bbox[i, j, 0]: UpperCamelCase__ :int = bbox[i, j, 2] UpperCamelCase__ :Tuple = bbox[i, j, 0] UpperCamelCase__ :Dict = t UpperCamelCase__ :int = tf.convert_to_tensor(lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = None if self.use_input_mask: UpperCamelCase__ :int = random_attention_mask([self.batch_size, self.seq_length] ) UpperCamelCase__ :Optional[int] = None if self.use_token_type_ids: UpperCamelCase__ :Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) UpperCamelCase__ :Optional[int] = None UpperCamelCase__ :Union[str, Any] = None UpperCamelCase__ :Tuple = None if self.use_labels: UpperCamelCase__ :List[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase__ :Any = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCamelCase__ :List[str] = ids_tensor([self.batch_size] , self.num_choices ) UpperCamelCase__ :int = LayoutLMConfig( 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 config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def __a ( self :Union[str, Any] , lowerCamelCase__ :int , lowerCamelCase__ :str , lowerCamelCase__ :Tuple , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Tuple , lowerCamelCase__ :Any , lowerCamelCase__ :Dict , lowerCamelCase__ :List[Any] ): UpperCamelCase__ :List[str] = TFLayoutLMModel(config=lowerCamelCase__ ) UpperCamelCase__ :List[str] = model(lowerCamelCase__ , lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ ) UpperCamelCase__ :Tuple = model(lowerCamelCase__ , lowerCamelCase__ , token_type_ids=lowerCamelCase__ ) UpperCamelCase__ :Dict = model(lowerCamelCase__ , lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def __a ( self :Tuple , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Dict , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :str , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :List[Any] , lowerCamelCase__ :List[str] , lowerCamelCase__ :Optional[int] ): UpperCamelCase__ :Any = TFLayoutLMForMaskedLM(config=lowerCamelCase__ ) UpperCamelCase__ :str = model(lowerCamelCase__ , lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , labels=lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __a ( self :int , lowerCamelCase__ :int , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Tuple , lowerCamelCase__ :int , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Tuple , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Union[str, Any] ): UpperCamelCase__ :str = self.num_labels UpperCamelCase__ :int = TFLayoutLMForSequenceClassification(config=lowerCamelCase__ ) UpperCamelCase__ :List[str] = model(lowerCamelCase__ , lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __a ( self :Any , lowerCamelCase__ :Tuple , lowerCamelCase__ :int , lowerCamelCase__ :Dict , lowerCamelCase__ :Any , lowerCamelCase__ :Any , lowerCamelCase__ :List[str] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :List[Any] ): UpperCamelCase__ :List[Any] = self.num_labels UpperCamelCase__ :Dict = TFLayoutLMForTokenClassification(config=lowerCamelCase__ ) UpperCamelCase__ :str = model(lowerCamelCase__ , lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , labels=lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __a ( self :Union[str, Any] , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :List[str] , lowerCamelCase__ :List[Any] , lowerCamelCase__ :Tuple , lowerCamelCase__ :Dict , lowerCamelCase__ :Tuple , lowerCamelCase__ :Tuple , lowerCamelCase__ :Dict ): UpperCamelCase__ :Union[str, Any] = TFLayoutLMForQuestionAnswering(config=lowerCamelCase__ ) UpperCamelCase__ :List[str] = model(lowerCamelCase__ , lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __a ( self :Union[str, Any] ): UpperCamelCase__ :List[Any] = self.prepare_config_and_inputs() ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :Dict = config_and_inputs UpperCamelCase__ :Union[str, Any] = { """input_ids""": input_ids, """bbox""": bbox, """token_type_ids""": token_type_ids, """attention_mask""": input_mask, } return config, inputs_dict @require_tf class lowerCAmelCase_ ( lowercase , lowercase , unittest.TestCase ): """simple docstring""" _snake_case : int = ( ( TFLayoutLMModel, TFLayoutLMForMaskedLM, TFLayoutLMForTokenClassification, TFLayoutLMForSequenceClassification, TFLayoutLMForQuestionAnswering, ) if is_tf_available() else () ) _snake_case : int = ( { """feature-extraction""": TFLayoutLMModel, """fill-mask""": TFLayoutLMForMaskedLM, """text-classification""": TFLayoutLMForSequenceClassification, """token-classification""": TFLayoutLMForTokenClassification, """zero-shot""": TFLayoutLMForSequenceClassification, } if is_tf_available() else {} ) _snake_case : Tuple = False _snake_case : Dict = True _snake_case : Optional[int] = 10 def __a ( self :Union[str, Any] ): UpperCamelCase__ :Tuple = TFLayoutLMModelTester(self ) UpperCamelCase__ :int = ConfigTester(self , config_class=lowerCamelCase__ , hidden_size=37 ) def __a ( self :Optional[Any] ): self.config_tester.run_common_tests() def __a ( self :Optional[Any] ): UpperCamelCase__ :int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase__ ) def __a ( self :Union[str, Any] ): UpperCamelCase__ :Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase__ ) def __a ( self :Tuple ): UpperCamelCase__ :int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase__ ) def __a ( self :List[Any] ): UpperCamelCase__ :int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*lowerCamelCase__ ) def __a ( self :Tuple ): UpperCamelCase__ :Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*lowerCamelCase__ ) @slow def __a ( self :str ): for model_name in TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase__ :Optional[Any] = TFLayoutLMModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) @unittest.skip("""Onnx compliancy broke with TF 2.10""" ) def __a ( self :Dict ): pass def A ( ) -> Union[str, Any]: # Here we prepare a batch of 2 sequences to test a LayoutLM forward pass on: # fmt: off UpperCamelCase__ :List[str] = tf.convert_to_tensor([[101,1019,1014,1016,1037,1_2849,4747,1004,1_4246,2278,5439,4524,5002,2930,2193,2930,4341,3208,1005,1055,2171,2848,1_1300,3531,102],[101,4070,4034,7020,1024,3058,1015,1013,2861,1013,6070,1_9274,2772,6205,2_7814,1_6147,1_6147,4343,2047,1_0283,1_0969,1_4389,1012,2338,102]] ) # noqa: E231 UpperCamelCase__ :Union[str, Any] = tf.convert_to_tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],] ) # noqa: E231 UpperCamelCase__ :Tuple = tf.convert_to_tensor([[[0,0,0,0],[423,237,440,251],[427,272,441,287],[419,115,437,129],[961,885,992,912],[256,38,330,58],[256,38,330,58],[336,42,353,57],[360,39,401,56],[360,39,401,56],[411,39,471,59],[479,41,528,59],[533,39,630,60],[67,113,134,131],[141,115,209,132],[68,149,133,166],[141,149,187,164],[195,148,287,165],[195,148,287,165],[195,148,287,165],[295,148,349,165],[441,149,492,166],[497,149,546,164],[64,201,125,218],[1000,1000,1000,1000]],[[0,0,0,0],[662,150,754,166],[665,199,742,211],[519,213,554,228],[519,213,554,228],[134,433,187,454],[130,467,204,480],[130,467,204,480],[130,467,204,480],[130,467,204,480],[130,467,204,480],[314,469,376,482],[504,684,582,706],[941,825,973,900],[941,825,973,900],[941,825,973,900],[941,825,973,900],[610,749,652,765],[130,659,168,672],[176,657,237,672],[238,657,312,672],[443,653,628,672],[443,653,628,672],[716,301,825,317],[1000,1000,1000,1000]]] ) # noqa: E231 UpperCamelCase__ :List[str] = tf.convert_to_tensor([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]] ) # noqa: E231 # these are sequence labels (i.e. at the token level) UpperCamelCase__ :Dict = tf.convert_to_tensor([[-100,10,10,10,9,1,-100,7,7,-100,7,7,4,2,5,2,8,8,-100,-100,5,0,3,2,-100],[-100,12,12,12,-100,12,10,-100,-100,-100,-100,10,12,9,-100,-100,-100,10,10,10,9,12,-100,10,-100]] ) # noqa: E231 # fmt: on return input_ids, attention_mask, bbox, token_type_ids, labels @require_tf class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" @slow def __a ( self :List[str] ): UpperCamelCase__ :Tuple = TFLayoutLMModel.from_pretrained("""microsoft/layoutlm-base-uncased""" ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :List[str] = prepare_layoutlm_batch_inputs() # forward pass UpperCamelCase__ :int = model(input_ids=lowerCamelCase__ , bbox=lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ ) # test the sequence output on [0, :3, :3] UpperCamelCase__ :Optional[int] = tf.convert_to_tensor( [[0.1785, -0.1947, -0.0425], [-0.3254, -0.2807, 0.2553], [-0.5391, -0.3322, 0.3364]] , ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] , lowerCamelCase__ , atol=1e-3 ) ) # test the pooled output on [1, :3] UpperCamelCase__ :Tuple = tf.convert_to_tensor([-0.6580, -0.0214, 0.8552] ) self.assertTrue(np.allclose(outputs.pooler_output[1, :3] , lowerCamelCase__ , atol=1e-3 ) ) @slow def __a ( self :int ): # initialize model with randomly initialized sequence classification head UpperCamelCase__ :Optional[Any] = TFLayoutLMForSequenceClassification.from_pretrained("""microsoft/layoutlm-base-uncased""" , num_labels=2 ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Optional[Any] = prepare_layoutlm_batch_inputs() # forward pass UpperCamelCase__ :List[str] = model( input_ids=lowerCamelCase__ , bbox=lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , labels=tf.convert_to_tensor([1, 1] ) , ) # test whether we get a loss as a scalar UpperCamelCase__ :Optional[int] = outputs.loss UpperCamelCase__ :str = (2,) self.assertEqual(loss.shape , lowerCamelCase__ ) # test the shape of the logits UpperCamelCase__ :Any = outputs.logits UpperCamelCase__ :str = (2, 2) self.assertEqual(logits.shape , lowerCamelCase__ ) @slow def __a ( self :Dict ): # initialize model with randomly initialized token classification head UpperCamelCase__ :Tuple = TFLayoutLMForTokenClassification.from_pretrained("""microsoft/layoutlm-base-uncased""" , num_labels=13 ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :List[str] = prepare_layoutlm_batch_inputs() # forward pass UpperCamelCase__ :Optional[int] = model( input_ids=lowerCamelCase__ , bbox=lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , labels=lowerCamelCase__ ) # test the shape of the logits UpperCamelCase__ :Union[str, Any] = outputs.logits UpperCamelCase__ :Any = tf.convert_to_tensor((2, 25, 13) ) self.assertEqual(logits.shape , lowerCamelCase__ ) @slow def __a ( self :Optional[int] ): # initialize model with randomly initialized token classification head UpperCamelCase__ :Tuple = TFLayoutLMForQuestionAnswering.from_pretrained("""microsoft/layoutlm-base-uncased""" ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Optional[int] = prepare_layoutlm_batch_inputs() # forward pass UpperCamelCase__ :Optional[int] = model(input_ids=lowerCamelCase__ , bbox=lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ ) # test the shape of the logits UpperCamelCase__ :int = tf.convert_to_tensor((2, 25) ) self.assertEqual(outputs.start_logits.shape , lowerCamelCase__ ) self.assertEqual(outputs.end_logits.shape , lowerCamelCase__ )
45
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, 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 numpy as np import tensorflow as tf from transformers import ( TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST, FlaubertConfig, TFFlaubertForMultipleChoice, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForSequenceClassification, TFFlaubertForTokenClassification, TFFlaubertModel, TFFlaubertWithLMHeadModel, ) class lowerCAmelCase_ : """simple docstring""" def __init__( self :Dict , lowerCamelCase__ :List[str] , ): UpperCamelCase__ :Optional[int] = parent UpperCamelCase__ :int = 13 UpperCamelCase__ :Optional[int] = 7 UpperCamelCase__ :Dict = True UpperCamelCase__ :Dict = True UpperCamelCase__ :str = True UpperCamelCase__ :List[Any] = True UpperCamelCase__ :Any = True UpperCamelCase__ :Optional[int] = False UpperCamelCase__ :Optional[int] = False UpperCamelCase__ :Tuple = False UpperCamelCase__ :Optional[int] = 2 UpperCamelCase__ :List[str] = 99 UpperCamelCase__ :Optional[Any] = 0 UpperCamelCase__ :Any = 32 UpperCamelCase__ :List[str] = 2 UpperCamelCase__ :int = 4 UpperCamelCase__ :List[str] = 0.1 UpperCamelCase__ :Union[str, Any] = 0.1 UpperCamelCase__ :Union[str, Any] = 5_12 UpperCamelCase__ :List[str] = 16 UpperCamelCase__ :str = 2 UpperCamelCase__ :Optional[int] = 0.02 UpperCamelCase__ :Optional[int] = 3 UpperCamelCase__ :Optional[int] = 4 UpperCamelCase__ :Optional[int] = """last""" UpperCamelCase__ :Tuple = True UpperCamelCase__ :int = None UpperCamelCase__ :Dict = 0 def __a ( self :int ): UpperCamelCase__ :Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCamelCase__ :Any = random_attention_mask([self.batch_size, self.seq_length] , dtype=tf.floataa ) UpperCamelCase__ :Union[str, Any] = None if self.use_input_lengths: UpperCamelCase__ :Union[str, Any] = ( ids_tensor([self.batch_size] , vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length UpperCamelCase__ :List[str] = None if self.use_token_type_ids: UpperCamelCase__ :List[str] = ids_tensor([self.batch_size, self.seq_length] , self.n_langs ) UpperCamelCase__ :int = None UpperCamelCase__ :List[str] = None UpperCamelCase__ :List[str] = None if self.use_labels: UpperCamelCase__ :List[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase__ :Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCamelCase__ :str = ids_tensor([self.batch_size] , 2 , dtype=tf.floataa ) UpperCamelCase__ :int = ids_tensor([self.batch_size] , self.num_choices ) UpperCamelCase__ :List[Any] = FlaubertConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , bos_token_id=self.bos_token_id , ) return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def __a ( self :Union[str, Any] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :int , lowerCamelCase__ :List[str] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :int , lowerCamelCase__ :List[Any] , lowerCamelCase__ :List[Any] , lowerCamelCase__ :int , ): UpperCamelCase__ :int = TFFlaubertModel(config=lowerCamelCase__ ) UpperCamelCase__ :Union[str, Any] = {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids} UpperCamelCase__ :Optional[Any] = model(lowerCamelCase__ ) UpperCamelCase__ :Union[str, Any] = [input_ids, input_mask] UpperCamelCase__ :Optional[int] = model(lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __a ( self :Tuple , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Any , lowerCamelCase__ :int , lowerCamelCase__ :int , lowerCamelCase__ :List[str] , lowerCamelCase__ :Any , lowerCamelCase__ :Optional[Any] , ): UpperCamelCase__ :List[str] = TFFlaubertWithLMHeadModel(lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids} UpperCamelCase__ :Any = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __a ( self :Dict , lowerCamelCase__ :List[str] , lowerCamelCase__ :Dict , lowerCamelCase__ :Tuple , lowerCamelCase__ :str , lowerCamelCase__ :Any , lowerCamelCase__ :Tuple , lowerCamelCase__ :Any , lowerCamelCase__ :int , lowerCamelCase__ :Tuple , ): UpperCamelCase__ :int = TFFlaubertForQuestionAnsweringSimple(lowerCamelCase__ ) UpperCamelCase__ :int = {"""input_ids""": input_ids, """lengths""": input_lengths} UpperCamelCase__ :Optional[int] = model(lowerCamelCase__ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __a ( self :List[Any] , lowerCamelCase__ :List[str] , lowerCamelCase__ :List[str] , lowerCamelCase__ :str , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Tuple , lowerCamelCase__ :Tuple , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :int , lowerCamelCase__ :Optional[int] , ): UpperCamelCase__ :List[Any] = TFFlaubertForSequenceClassification(lowerCamelCase__ ) UpperCamelCase__ :List[str] = {"""input_ids""": input_ids, """lengths""": input_lengths} UpperCamelCase__ :List[str] = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __a ( self :Tuple , lowerCamelCase__ :str , lowerCamelCase__ :Any , lowerCamelCase__ :Tuple , lowerCamelCase__ :str , lowerCamelCase__ :Any , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :List[str] , lowerCamelCase__ :str , lowerCamelCase__ :Any , ): UpperCamelCase__ :Any = self.num_labels UpperCamelCase__ :Tuple = TFFlaubertForTokenClassification(config=lowerCamelCase__ ) UpperCamelCase__ :Any = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} UpperCamelCase__ :List[Any] = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __a ( self :Tuple , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Tuple , lowerCamelCase__ :Any , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :str , lowerCamelCase__ :Any , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Dict , lowerCamelCase__ :List[str] , ): UpperCamelCase__ :Optional[int] = self.num_choices UpperCamelCase__ :Dict = TFFlaubertForMultipleChoice(config=lowerCamelCase__ ) UpperCamelCase__ :Any = tf.tile(tf.expand_dims(lowerCamelCase__ , 1 ) , (1, self.num_choices, 1) ) UpperCamelCase__ :str = tf.tile(tf.expand_dims(lowerCamelCase__ , 1 ) , (1, self.num_choices, 1) ) UpperCamelCase__ :Any = tf.tile(tf.expand_dims(lowerCamelCase__ , 1 ) , (1, self.num_choices, 1) ) UpperCamelCase__ :int = { """input_ids""": multiple_choice_inputs_ids, """attention_mask""": multiple_choice_input_mask, """token_type_ids""": multiple_choice_token_type_ids, } UpperCamelCase__ :List[str] = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __a ( self :Tuple ): UpperCamelCase__ :str = self.prepare_config_and_inputs() ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :str = config_and_inputs UpperCamelCase__ :Optional[Any] = { """input_ids""": input_ids, """token_type_ids""": token_type_ids, """langs""": token_type_ids, """lengths""": input_lengths, } return config, inputs_dict @require_tf class lowerCAmelCase_ ( lowercase , lowercase , unittest.TestCase ): """simple docstring""" _snake_case : List[str] = ( ( TFFlaubertModel, TFFlaubertWithLMHeadModel, TFFlaubertForSequenceClassification, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForTokenClassification, TFFlaubertForMultipleChoice, ) if is_tf_available() else () ) _snake_case : List[Any] = ( (TFFlaubertWithLMHeadModel,) if is_tf_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable _snake_case : Optional[int] = ( { """feature-extraction""": TFFlaubertModel, """fill-mask""": TFFlaubertWithLMHeadModel, """question-answering""": TFFlaubertForQuestionAnsweringSimple, """text-classification""": TFFlaubertForSequenceClassification, """token-classification""": TFFlaubertForTokenClassification, """zero-shot""": TFFlaubertForSequenceClassification, } if is_tf_available() else {} ) _snake_case : List[Any] = False _snake_case : Tuple = False def __a ( self :Optional[int] , lowerCamelCase__ :List[Any] , lowerCamelCase__ :Dict , lowerCamelCase__ :int , lowerCamelCase__ :str , lowerCamelCase__ :List[Any] ): if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith("""Fast""" ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def __a ( self :List[str] ): UpperCamelCase__ :List[str] = TFFlaubertModelTester(self ) UpperCamelCase__ :Tuple = ConfigTester(self , config_class=lowerCamelCase__ , emb_dim=37 ) def __a ( self :int ): self.config_tester.run_common_tests() def __a ( self :List[str] ): UpperCamelCase__ :List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*lowerCamelCase__ ) def __a ( self :Tuple ): UpperCamelCase__ :Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*lowerCamelCase__ ) def __a ( self :Union[str, Any] ): UpperCamelCase__ :Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*lowerCamelCase__ ) def __a ( self :List[Any] ): UpperCamelCase__ :Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*lowerCamelCase__ ) def __a ( self :Any ): UpperCamelCase__ :Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_token_classification(*lowerCamelCase__ ) def __a ( self :List[Any] ): UpperCamelCase__ :Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_multiple_choice(*lowerCamelCase__ ) @slow def __a ( self :str ): for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase__ :Dict = TFFlaubertModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) @require_tf @require_sentencepiece @require_tokenizers class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" @slow def __a ( self :str ): UpperCamelCase__ :Tuple = TFFlaubertModel.from_pretrained("""jplu/tf-flaubert-small-cased""" ) UpperCamelCase__ :Optional[int] = tf.convert_to_tensor( [[0, 1_58, 7_35, 25_92, 14_24, 67_27, 82, 1]] , dtype=tf.intaa , ) # "J'aime flaubert !" UpperCamelCase__ :Optional[Any] = model(lowerCamelCase__ )[0] UpperCamelCase__ :Optional[int] = tf.TensorShape((1, 8, 5_12) ) self.assertEqual(output.shape , lowerCamelCase__ ) # compare the actual values for a slice. UpperCamelCase__ :str = tf.convert_to_tensor( [ [ [-1.876_8773, -1.56_6555, 0.2707_2418], [-1.692_0038, -0.587_3505, 1.932_9599], [-2.956_3985, -1.699_3835, 1.797_2052], ] ] , dtype=tf.floataa , ) self.assertTrue(np.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1e-4 ) )
45
1
import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DeformableDetrImageProcessor class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __init__( self :List[str] , lowerCamelCase__ :int , lowerCamelCase__ :List[str]=7 , lowerCamelCase__ :Any=3 , lowerCamelCase__ :Union[str, Any]=30 , lowerCamelCase__ :int=4_00 , lowerCamelCase__ :Optional[int]=True , lowerCamelCase__ :Tuple=None , lowerCamelCase__ :Tuple=True , lowerCamelCase__ :Optional[int]=[0.5, 0.5, 0.5] , lowerCamelCase__ :int=[0.5, 0.5, 0.5] , lowerCamelCase__ :Optional[Any]=True , lowerCamelCase__ :Optional[Any]=1 / 2_55 , lowerCamelCase__ :str=True , ): # by setting size["longest_edge"] > max_resolution we're effectively not testing this :p UpperCamelCase__ :List[Any] = size if size is not None else {"""shortest_edge""": 18, """longest_edge""": 13_33} UpperCamelCase__ :Union[str, Any] = parent UpperCamelCase__ :List[str] = batch_size UpperCamelCase__ :int = num_channels UpperCamelCase__ :int = min_resolution UpperCamelCase__ :Tuple = max_resolution UpperCamelCase__ :Union[str, Any] = do_resize UpperCamelCase__ :int = size UpperCamelCase__ :Optional[int] = do_normalize UpperCamelCase__ :Optional[Any] = image_mean UpperCamelCase__ :Optional[Any] = image_std UpperCamelCase__ :Dict = do_rescale UpperCamelCase__ :List[Any] = rescale_factor UpperCamelCase__ :Any = do_pad def __a ( self :Tuple ): return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def __a ( self :Union[str, Any] , lowerCamelCase__ :Dict , lowerCamelCase__ :Any=False ): if not batched: UpperCamelCase__ :Optional[Any] = image_inputs[0] if isinstance(lowerCamelCase__ , Image.Image ): UpperCamelCase__ , UpperCamelCase__ :str = image.size else: UpperCamelCase__ , UpperCamelCase__ :List[str] = image.shape[1], image.shape[2] if w < h: UpperCamelCase__ :int = int(self.size["""shortest_edge"""] * h / w ) UpperCamelCase__ :Dict = self.size["""shortest_edge"""] elif w > h: UpperCamelCase__ :Tuple = self.size["""shortest_edge"""] UpperCamelCase__ :int = int(self.size["""shortest_edge"""] * w / h ) else: UpperCamelCase__ :Optional[Any] = self.size["""shortest_edge"""] UpperCamelCase__ :Any = self.size["""shortest_edge"""] else: UpperCamelCase__ :Any = [] for image in image_inputs: UpperCamelCase__ , UpperCamelCase__ :Dict = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) UpperCamelCase__ :int = max(lowerCamelCase__ , key=lambda lowerCamelCase__ : item[0] )[0] UpperCamelCase__ :List[str] = max(lowerCamelCase__ , key=lambda lowerCamelCase__ : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class lowerCAmelCase_ ( lowercase , unittest.TestCase ): """simple docstring""" _snake_case : List[Any] = DeformableDetrImageProcessor if is_vision_available() else None def __a ( self :List[Any] ): UpperCamelCase__ :Union[str, Any] = DeformableDetrImageProcessingTester(self ) @property def __a ( self :Union[str, Any] ): return self.image_processor_tester.prepare_image_processor_dict() def __a ( self :Optional[int] ): UpperCamelCase__ :Optional[int] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(lowerCamelCase__ , """image_mean""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """image_std""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """do_normalize""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """do_resize""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """do_rescale""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """do_pad""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """size""" ) ) def __a ( self :int ): UpperCamelCase__ :Optional[Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""shortest_edge""": 18, """longest_edge""": 13_33} ) self.assertEqual(image_processor.do_pad , lowerCamelCase__ ) UpperCamelCase__ :str = self.image_processing_class.from_dict( self.image_processor_dict , size=42 , max_size=84 , pad_and_return_pixel_mask=lowerCamelCase__ ) self.assertEqual(image_processor.size , {"""shortest_edge""": 42, """longest_edge""": 84} ) self.assertEqual(image_processor.do_pad , lowerCamelCase__ ) def __a ( self :Optional[int] ): pass def __a ( self :str ): # Initialize image_processing UpperCamelCase__ :int = self.image_processing_class(**self.image_processor_dict ) # create random PIL images UpperCamelCase__ :List[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCamelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCamelCase__ , Image.Image ) # Test not batched input UpperCamelCase__ :Optional[Any] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values UpperCamelCase__ , UpperCamelCase__ :Dict = self.image_processor_tester.get_expected_values(lowerCamelCase__ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched UpperCamelCase__ , UpperCamelCase__ :Optional[Any] = self.image_processor_tester.get_expected_values(lowerCamelCase__ , batched=lowerCamelCase__ ) UpperCamelCase__ :Tuple = image_processing(lowerCamelCase__ , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __a ( self :Optional[Any] ): # Initialize image_processing UpperCamelCase__ :int = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors UpperCamelCase__ :Any = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCamelCase__ , numpify=lowerCamelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCamelCase__ , np.ndarray ) # Test not batched input UpperCamelCase__ :List[str] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values UpperCamelCase__ , UpperCamelCase__ :Any = self.image_processor_tester.get_expected_values(lowerCamelCase__ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched UpperCamelCase__ :List[str] = image_processing(lowerCamelCase__ , return_tensors="""pt""" ).pixel_values UpperCamelCase__ , UpperCamelCase__ :Tuple = self.image_processor_tester.get_expected_values(lowerCamelCase__ , batched=lowerCamelCase__ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __a ( self :Tuple ): # Initialize image_processing UpperCamelCase__ :Dict = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors UpperCamelCase__ :List[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCamelCase__ , torchify=lowerCamelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCamelCase__ , torch.Tensor ) # Test not batched input UpperCamelCase__ :str = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values UpperCamelCase__ , UpperCamelCase__ :str = self.image_processor_tester.get_expected_values(lowerCamelCase__ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched UpperCamelCase__ :List[str] = image_processing(lowerCamelCase__ , return_tensors="""pt""" ).pixel_values UpperCamelCase__ , UpperCamelCase__ :str = self.image_processor_tester.get_expected_values(lowerCamelCase__ , batched=lowerCamelCase__ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) @slow def __a ( self :Optional[int] ): # prepare image and target UpperCamelCase__ :List[str] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) with open("""./tests/fixtures/tests_samples/COCO/coco_annotations.txt""" , """r""" ) as f: UpperCamelCase__ :List[str] = json.loads(f.read() ) UpperCamelCase__ :Tuple = {"""image_id""": 3_97_69, """annotations""": target} # encode them UpperCamelCase__ :str = DeformableDetrImageProcessor() UpperCamelCase__ :Optional[Any] = image_processing(images=lowerCamelCase__ , annotations=lowerCamelCase__ , return_tensors="""pt""" ) # verify pixel values UpperCamelCase__ :Any = torch.Size([1, 3, 8_00, 10_66] ) self.assertEqual(encoding["""pixel_values"""].shape , lowerCamelCase__ ) UpperCamelCase__ :List[Any] = torch.tensor([0.2796, 0.3138, 0.3481] ) self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3] , lowerCamelCase__ , atol=1e-4 ) ) # verify area UpperCamelCase__ :Dict = torch.tensor([5887.9600, 1_1250.2061, 48_9353.8438, 83_7122.7500, 14_7967.5156, 16_5732.3438] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""] , lowerCamelCase__ ) ) # verify boxes UpperCamelCase__ :str = torch.Size([6, 4] ) self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape , lowerCamelCase__ ) UpperCamelCase__ :List[str] = torch.tensor([0.5503, 0.2765, 0.0604, 0.2215] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0] , lowerCamelCase__ , atol=1e-3 ) ) # verify image_id UpperCamelCase__ :Any = torch.tensor([3_97_69] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""] , lowerCamelCase__ ) ) # verify is_crowd UpperCamelCase__ :Any = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""] , lowerCamelCase__ ) ) # verify class_labels UpperCamelCase__ :Dict = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""] , lowerCamelCase__ ) ) # verify orig_size UpperCamelCase__ :Union[str, Any] = torch.tensor([4_80, 6_40] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""] , lowerCamelCase__ ) ) # verify size UpperCamelCase__ :int = torch.tensor([8_00, 10_66] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""] , lowerCamelCase__ ) ) @slow def __a ( self :Any ): # prepare image, target and masks_path UpperCamelCase__ :List[str] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) with open("""./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt""" , """r""" ) as f: UpperCamelCase__ :Tuple = json.loads(f.read() ) UpperCamelCase__ :Tuple = {"""file_name""": """000000039769.png""", """image_id""": 3_97_69, """segments_info""": target} UpperCamelCase__ :int = pathlib.Path("""./tests/fixtures/tests_samples/COCO/coco_panoptic""" ) # encode them UpperCamelCase__ :int = DeformableDetrImageProcessor(format="""coco_panoptic""" ) UpperCamelCase__ :List[Any] = image_processing(images=lowerCamelCase__ , annotations=lowerCamelCase__ , masks_path=lowerCamelCase__ , return_tensors="""pt""" ) # verify pixel values UpperCamelCase__ :int = torch.Size([1, 3, 8_00, 10_66] ) self.assertEqual(encoding["""pixel_values"""].shape , lowerCamelCase__ ) UpperCamelCase__ :Dict = torch.tensor([0.2796, 0.3138, 0.3481] ) self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3] , lowerCamelCase__ , atol=1e-4 ) ) # verify area UpperCamelCase__ :str = torch.tensor([14_7979.6875, 16_5527.0469, 48_4638.5938, 1_1292.9375, 5879.6562, 7634.1147] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""] , lowerCamelCase__ ) ) # verify boxes UpperCamelCase__ :Optional[Any] = torch.Size([6, 4] ) self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape , lowerCamelCase__ ) UpperCamelCase__ :Any = torch.tensor([0.2625, 0.5437, 0.4688, 0.8625] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0] , lowerCamelCase__ , atol=1e-3 ) ) # verify image_id UpperCamelCase__ :Dict = torch.tensor([3_97_69] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""] , lowerCamelCase__ ) ) # verify is_crowd UpperCamelCase__ :Tuple = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""] , lowerCamelCase__ ) ) # verify class_labels UpperCamelCase__ :Any = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""] , lowerCamelCase__ ) ) # verify masks UpperCamelCase__ :Optional[Any] = 82_28_73 self.assertEqual(encoding["""labels"""][0]["""masks"""].sum().item() , lowerCamelCase__ ) # verify orig_size UpperCamelCase__ :List[Any] = torch.tensor([4_80, 6_40] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""] , lowerCamelCase__ ) ) # verify size UpperCamelCase__ :Dict = torch.tensor([8_00, 10_66] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""] , lowerCamelCase__ ) )
45
import gc import tempfile import unittest import numpy as np import torch from diffusers import VersatileDiffusionPipeline from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device UpperCamelCase = False class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" pass @nightly @require_torch_gpu class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __a ( self :Union[str, Any] ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __a ( self :List[Any] ): UpperCamelCase__ :List[str] = VersatileDiffusionPipeline.from_pretrained("""shi-labs/versatile-diffusion""" , torch_dtype=torch.floataa ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) UpperCamelCase__ :Dict = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg""" ) UpperCamelCase__ :Any = torch.manual_seed(0 ) UpperCamelCase__ :Optional[int] = pipe.dual_guided( prompt="""first prompt""" , image=lowerCamelCase__ , text_to_image_strength=0.75 , generator=lowerCamelCase__ , guidance_scale=7.5 , num_inference_steps=2 , output_type="""numpy""" , ).images with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(lowerCamelCase__ ) UpperCamelCase__ :List[str] = VersatileDiffusionPipeline.from_pretrained(lowerCamelCase__ , torch_dtype=torch.floataa ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) UpperCamelCase__ :str = generator.manual_seed(0 ) UpperCamelCase__ :str = pipe.dual_guided( prompt="""first prompt""" , image=lowerCamelCase__ , text_to_image_strength=0.75 , generator=lowerCamelCase__ , guidance_scale=7.5 , num_inference_steps=2 , output_type="""numpy""" , ).images assert np.abs(image - new_image ).sum() < 1e-5, "Models don't have the same forward pass" def __a ( self :Dict ): UpperCamelCase__ :List[Any] = VersatileDiffusionPipeline.from_pretrained("""shi-labs/versatile-diffusion""" , torch_dtype=torch.floataa ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = """cyberpunk 2077""" UpperCamelCase__ :str = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg""" ) UpperCamelCase__ :str = torch.manual_seed(0 ) UpperCamelCase__ :Dict = pipe.dual_guided( prompt=lowerCamelCase__ , image=lowerCamelCase__ , text_to_image_strength=0.75 , generator=lowerCamelCase__ , guidance_scale=7.5 , num_inference_steps=50 , output_type="""numpy""" , ).images UpperCamelCase__ :Tuple = image[0, 2_53:2_56, 2_53:2_56, -1] assert image.shape == (1, 5_12, 5_12, 3) UpperCamelCase__ :Any = np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 UpperCamelCase__ :List[Any] = """A painting of a squirrel eating a burger """ UpperCamelCase__ :List[str] = torch.manual_seed(0 ) UpperCamelCase__ :Optional[int] = pipe.text_to_image( prompt=lowerCamelCase__ , generator=lowerCamelCase__ , guidance_scale=7.5 , num_inference_steps=50 , output_type="""numpy""" ).images UpperCamelCase__ :str = image[0, 2_53:2_56, 2_53:2_56, -1] assert image.shape == (1, 5_12, 5_12, 3) UpperCamelCase__ :Union[str, Any] = np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 UpperCamelCase__ :Optional[int] = pipe.image_variation(lowerCamelCase__ , generator=lowerCamelCase__ , output_type="""numpy""" ).images UpperCamelCase__ :int = image[0, 2_53:2_56, 2_53:2_56, -1] assert image.shape == (1, 5_12, 5_12, 3) UpperCamelCase__ :List[Any] = np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1
45
1
import random import sys import numpy as np from matplotlib import pyplot as plt from matplotlib.colors import ListedColormap UpperCamelCase = "Usage of script: script_name <size_of_canvas:int>" UpperCamelCase = [0] * 100 + [1] * 10 random.shuffle(choice) def A ( lowercase__ : int ) -> list[list[bool]]: UpperCamelCase__ :Union[str, Any] = [[False for i in range(lowercase__ )] for j in range(lowercase__ )] return canvas def A ( lowercase__ : list[list[bool]] ) -> None: for i, row in enumerate(lowercase__ ): for j, _ in enumerate(lowercase__ ): UpperCamelCase__ :Tuple = bool(random.getrandbits(1 ) ) def A ( lowercase__ : list[list[bool]] ) -> list[list[bool]]: UpperCamelCase__ :List[Any] = np.array(lowercase__ ) UpperCamelCase__ :Dict = np.array(create_canvas(current_canvas.shape[0] ) ) for r, row in enumerate(lowercase__ ): for c, pt in enumerate(lowercase__ ): UpperCamelCase__ :Optional[int] = __judge_point( lowercase__ , current_canvas[r - 1 : r + 2, c - 1 : c + 2] ) UpperCamelCase__ :Dict = next_gen_canvas del next_gen_canvas # cleaning memory as we move on. UpperCamelCase__ :list[list[bool]] = current_canvas.tolist() return return_canvas def A ( lowercase__ : bool , lowercase__ : list[list[bool]] ) -> bool: UpperCamelCase__ :List[Any] = 0 UpperCamelCase__ :Any = 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. UpperCamelCase__ :List[Any] = pt if pt: if alive < 2: UpperCamelCase__ :List[str] = False elif alive == 2 or alive == 3: UpperCamelCase__ :int = True elif alive > 3: UpperCamelCase__ :List[Any] = False else: if alive == 3: UpperCamelCase__ :Any = True return state if __name__ == "__main__": if len(sys.argv) != 2: raise Exception(usage_doc) UpperCamelCase = int(sys.argv[1]) # main working structure of this module. UpperCamelCase = create_canvas(canvas_size) seed(c) UpperCamelCase , UpperCamelCase = plt.subplots() fig.show() UpperCamelCase = ListedColormap(["w", "k"]) try: while True: UpperCamelCase = run(c) ax.matshow(c, cmap=cmap) fig.canvas.draw() ax.cla() except KeyboardInterrupt: # do nothing. pass
45
from __future__ import annotations import copy import inspect import unittest import numpy as np from transformers import is_tf_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST, TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, TFLayoutLMvaModel, ) if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class lowerCAmelCase_ : """simple docstring""" def __init__( self :Union[str, Any] , lowerCamelCase__ :Tuple , lowerCamelCase__ :List[str]=2 , lowerCamelCase__ :List[str]=3 , lowerCamelCase__ :List[str]=4 , lowerCamelCase__ :str=2 , lowerCamelCase__ :Optional[int]=7 , lowerCamelCase__ :List[Any]=True , lowerCamelCase__ :Optional[Any]=True , lowerCamelCase__ :Union[str, Any]=True , lowerCamelCase__ :Any=True , lowerCamelCase__ :Dict=99 , lowerCamelCase__ :Optional[Any]=36 , lowerCamelCase__ :str=2 , lowerCamelCase__ :List[Any]=4 , lowerCamelCase__ :Optional[Any]=37 , lowerCamelCase__ :Optional[int]="gelu" , lowerCamelCase__ :Any=0.1 , lowerCamelCase__ :List[Any]=0.1 , lowerCamelCase__ :List[Any]=5_12 , lowerCamelCase__ :str=16 , lowerCamelCase__ :Tuple=2 , lowerCamelCase__ :int=0.02 , lowerCamelCase__ :List[Any]=6 , lowerCamelCase__ :List[str]=6 , lowerCamelCase__ :Optional[int]=3 , lowerCamelCase__ :Optional[int]=4 , lowerCamelCase__ :int=None , lowerCamelCase__ :Optional[Any]=10_00 , ): UpperCamelCase__ :Any = parent UpperCamelCase__ :Union[str, Any] = batch_size UpperCamelCase__ :Dict = num_channels UpperCamelCase__ :Optional[Any] = image_size UpperCamelCase__ :Union[str, Any] = patch_size UpperCamelCase__ :Union[str, Any] = is_training UpperCamelCase__ :str = use_input_mask UpperCamelCase__ :int = use_token_type_ids UpperCamelCase__ :int = use_labels UpperCamelCase__ :List[Any] = vocab_size UpperCamelCase__ :List[str] = hidden_size UpperCamelCase__ :List[Any] = num_hidden_layers UpperCamelCase__ :List[str] = num_attention_heads UpperCamelCase__ :Tuple = intermediate_size UpperCamelCase__ :Any = hidden_act UpperCamelCase__ :Optional[int] = hidden_dropout_prob UpperCamelCase__ :Tuple = attention_probs_dropout_prob UpperCamelCase__ :Dict = max_position_embeddings UpperCamelCase__ :Tuple = type_vocab_size UpperCamelCase__ :Union[str, Any] = type_sequence_label_size UpperCamelCase__ :int = initializer_range UpperCamelCase__ :List[Any] = coordinate_size UpperCamelCase__ :Tuple = shape_size UpperCamelCase__ :Dict = num_labels UpperCamelCase__ :str = num_choices UpperCamelCase__ :Tuple = scope UpperCamelCase__ :str = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) UpperCamelCase__ :List[str] = text_seq_length UpperCamelCase__ :List[str] = (image_size // patch_size) ** 2 + 1 UpperCamelCase__ :Dict = self.text_seq_length + self.image_seq_length def __a ( self :Tuple ): UpperCamelCase__ :Optional[int] = ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size ) UpperCamelCase__ :int = ids_tensor([self.batch_size, self.text_seq_length, 4] , self.range_bbox ) UpperCamelCase__ :str = bbox.numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: UpperCamelCase__ :List[str] = bbox[i, j, 3] UpperCamelCase__ :Optional[int] = bbox[i, j, 1] UpperCamelCase__ :Optional[Any] = tmp_coordinate if bbox[i, j, 2] < bbox[i, j, 0]: UpperCamelCase__ :Tuple = bbox[i, j, 2] UpperCamelCase__ :Optional[Any] = bbox[i, j, 0] UpperCamelCase__ :List[str] = tmp_coordinate UpperCamelCase__ :Dict = tf.constant(lowerCamelCase__ ) UpperCamelCase__ :Union[str, Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCamelCase__ :Any = None if self.use_input_mask: UpperCamelCase__ :int = random_attention_mask([self.batch_size, self.text_seq_length] ) UpperCamelCase__ :Optional[Any] = None if self.use_token_type_ids: UpperCamelCase__ :Optional[int] = ids_tensor([self.batch_size, self.text_seq_length] , self.type_vocab_size ) UpperCamelCase__ :List[str] = None UpperCamelCase__ :Union[str, Any] = None if self.use_labels: UpperCamelCase__ :Union[str, Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase__ :Union[str, Any] = ids_tensor([self.batch_size, self.text_seq_length] , self.num_labels ) UpperCamelCase__ :Optional[int] = LayoutLMvaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , coordinate_size=self.coordinate_size , shape_size=self.shape_size , input_size=self.image_size , patch_size=self.patch_size , ) return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels def __a ( self :List[Any] , lowerCamelCase__ :str , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Dict , lowerCamelCase__ :str , lowerCamelCase__ :int , lowerCamelCase__ :Any ): UpperCamelCase__ :Dict = TFLayoutLMvaModel(config=lowerCamelCase__ ) # text + image UpperCamelCase__ :Tuple = model(lowerCamelCase__ , pixel_values=lowerCamelCase__ , training=lowerCamelCase__ ) UpperCamelCase__ :Tuple = model( lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , training=lowerCamelCase__ , ) UpperCamelCase__ :str = model(lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , training=lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # text only UpperCamelCase__ :Optional[int] = model(lowerCamelCase__ , training=lowerCamelCase__ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size) ) # image only UpperCamelCase__ :Tuple = model({"""pixel_values""": pixel_values} , training=lowerCamelCase__ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size) ) def __a ( self :Dict , lowerCamelCase__ :str , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Dict , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :str , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :str ): UpperCamelCase__ :Optional[Any] = self.num_labels UpperCamelCase__ :List[Any] = TFLayoutLMvaForSequenceClassification(config=lowerCamelCase__ ) UpperCamelCase__ :List[str] = model( lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , labels=lowerCamelCase__ , training=lowerCamelCase__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __a ( self :List[str] , lowerCamelCase__ :List[str] , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Dict , lowerCamelCase__ :List[Any] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Tuple , lowerCamelCase__ :List[str] ): UpperCamelCase__ :Union[str, Any] = self.num_labels UpperCamelCase__ :Dict = TFLayoutLMvaForTokenClassification(config=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = model( lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , labels=lowerCamelCase__ , training=lowerCamelCase__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels) ) def __a ( self :int , lowerCamelCase__ :Dict , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Any , lowerCamelCase__ :Dict , lowerCamelCase__ :Tuple , lowerCamelCase__ :Tuple ): UpperCamelCase__ :Dict = 2 UpperCamelCase__ :Tuple = TFLayoutLMvaForQuestionAnswering(config=lowerCamelCase__ ) UpperCamelCase__ :int = model( lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , start_positions=lowerCamelCase__ , end_positions=lowerCamelCase__ , training=lowerCamelCase__ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __a ( self :List[Any] ): UpperCamelCase__ :Union[str, Any] = self.prepare_config_and_inputs() ((UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__)) :Any = config_and_inputs UpperCamelCase__ :List[str] = { """input_ids""": input_ids, """bbox""": bbox, """pixel_values""": pixel_values, """token_type_ids""": token_type_ids, """attention_mask""": input_mask, } return config, inputs_dict @require_tf class lowerCAmelCase_ ( lowercase , lowercase , unittest.TestCase ): """simple docstring""" _snake_case : Dict = ( ( TFLayoutLMvaModel, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, ) if is_tf_available() else () ) _snake_case : Dict = ( {"""document-question-answering""": TFLayoutLMvaForQuestionAnswering, """feature-extraction""": TFLayoutLMvaModel} if is_tf_available() else {} ) _snake_case : Optional[int] = False _snake_case : List[str] = False _snake_case : Tuple = False def __a ( self :str , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Tuple , lowerCamelCase__ :int ): return True def __a ( self :Optional[int] , lowerCamelCase__ :int , lowerCamelCase__ :List[str] , lowerCamelCase__ :Optional[int]=False ): UpperCamelCase__ :List[str] = copy.deepcopy(lowerCamelCase__ ) if model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :Optional[int] = { k: tf.tile(tf.expand_dims(lowerCamelCase__ , 1 ) , (1, self.model_tester.num_choices) + (1,) * (v.ndim - 1) ) if isinstance(lowerCamelCase__ , tf.Tensor ) and v.ndim > 0 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :str = tf.ones(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :List[str] = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) UpperCamelCase__ :Union[str, Any] = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :Optional[Any] = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :Tuple = tf.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=tf.intaa ) return inputs_dict def __a ( self :Dict ): UpperCamelCase__ :List[Any] = TFLayoutLMvaModelTester(self ) UpperCamelCase__ :Optional[int] = ConfigTester(self , config_class=lowerCamelCase__ , hidden_size=37 ) def __a ( self :Any ): self.config_tester.run_common_tests() def __a ( self :Optional[int] ): UpperCamelCase__ , UpperCamelCase__ :Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase__ :Optional[int] = model_class(lowerCamelCase__ ) if getattr(lowerCamelCase__ , """hf_compute_loss""" , lowerCamelCase__ ): # The number of elements in the loss should be the same as the number of elements in the label UpperCamelCase__ :Union[str, Any] = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) UpperCamelCase__ :int = prepared_for_class[ sorted(prepared_for_class.keys() - inputs_dict.keys() , reverse=lowerCamelCase__ )[0] ] UpperCamelCase__ :Union[str, Any] = added_label.shape.as_list()[:1] # Test that model correctly compute the loss with kwargs UpperCamelCase__ :List[Any] = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = prepared_for_class.pop("""input_ids""" ) UpperCamelCase__ :List[str] = model(lowerCamelCase__ , **lowerCamelCase__ )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss when we mask some positions UpperCamelCase__ :Union[str, Any] = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = prepared_for_class.pop("""input_ids""" ) if "labels" in prepared_for_class: UpperCamelCase__ :List[str] = prepared_for_class["""labels"""].numpy() if len(labels.shape ) > 1 and labels.shape[1] != 1: UpperCamelCase__ :Optional[Any] = -1_00 UpperCamelCase__ :Union[str, Any] = tf.convert_to_tensor(lowerCamelCase__ ) UpperCamelCase__ :Tuple = model(lowerCamelCase__ , **lowerCamelCase__ )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) self.assertTrue(not np.any(np.isnan(loss.numpy() ) ) ) # Test that model correctly compute the loss with a dict UpperCamelCase__ :Optional[Any] = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) UpperCamelCase__ :Union[str, Any] = model(lowerCamelCase__ )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss with a tuple UpperCamelCase__ :Dict = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) # Get keys that were added with the _prepare_for_class function UpperCamelCase__ :str = prepared_for_class.keys() - inputs_dict.keys() UpperCamelCase__ :Tuple = inspect.signature(model.call ).parameters UpperCamelCase__ :str = list(signature.keys() ) # Create a dictionary holding the location of the tensors in the tuple UpperCamelCase__ :Any = {0: """input_ids"""} for label_key in label_keys: UpperCamelCase__ :Dict = signature_names.index(lowerCamelCase__ ) UpperCamelCase__ :Optional[int] = label_key UpperCamelCase__ :Optional[Any] = sorted(tuple_index_mapping.items() ) # Initialize a list with their default values, update the values and convert to a tuple UpperCamelCase__ :Any = [] for name in signature_names: if name != "kwargs": list_input.append(signature[name].default ) for index, value in sorted_tuple_index_mapping: UpperCamelCase__ :List[str] = prepared_for_class[value] UpperCamelCase__ :Union[str, Any] = tuple(lowerCamelCase__ ) # Send to model UpperCamelCase__ :str = model(tuple_input[:-1] )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) def __a ( self :Optional[int] ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Any ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :List[Any] = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: UpperCamelCase__ :Dict = type self.model_tester.create_and_check_model(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Tuple ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Optional[int] ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :List[str] ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) @slow def __a ( self :Optional[int] ): for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase__ :Dict = TFLayoutLMvaModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) def A ( ) -> List[str]: UpperCamelCase__ :List[Any] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_tf class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" @cached_property def __a ( self :Optional[Any] ): return LayoutLMvaImageProcessor(apply_ocr=lowerCamelCase__ ) if is_vision_available() else None @slow def __a ( self :Dict ): UpperCamelCase__ :List[str] = TFLayoutLMvaModel.from_pretrained("""microsoft/layoutlmv3-base""" ) UpperCamelCase__ :List[Any] = self.default_image_processor UpperCamelCase__ :str = prepare_img() UpperCamelCase__ :Any = image_processor(images=lowerCamelCase__ , return_tensors="""tf""" ).pixel_values UpperCamelCase__ :str = tf.constant([[1, 2]] ) UpperCamelCase__ :Any = tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]] ) , axis=0 ) # forward pass UpperCamelCase__ :Dict = model(input_ids=lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , training=lowerCamelCase__ ) # verify the logits UpperCamelCase__ :int = (1, 1_99, 7_68) self.assertEqual(outputs.last_hidden_state.shape , lowerCamelCase__ ) UpperCamelCase__ :List[Any] = tf.constant( [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]] ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] , lowerCamelCase__ , atol=1e-4 ) )
45
1
UpperCamelCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" def A ( lowercase__ : bytes ) -> bytes: # Make sure the supplied data is a bytes-like object if not isinstance(lowercase__ , lowercase__ ): UpperCamelCase__ :Dict = f"""a bytes-like object is required, not '{data.__class__.__name__}'""" raise TypeError(lowercase__ ) UpperCamelCase__ :Any = """""".join(bin(lowercase__ )[2:].zfill(8 ) for byte in data ) UpperCamelCase__ :Optional[Any] = len(lowercase__ ) % 6 != 0 if padding_needed: # The padding that will be added later UpperCamelCase__ :int = b"""=""" * ((6 - len(lowercase__ ) % 6) // 2) # Append binary_stream with arbitrary binary digits (0's by default) to make its # length a multiple of 6. binary_stream += "0" * (6 - len(lowercase__ ) % 6) else: UpperCamelCase__ :List[Any] = b"""""" # Encode every 6 binary digits to their corresponding Base64 character return ( "".join( B64_CHARSET[int(binary_stream[index : index + 6] , 2 )] for index in range(0 , len(lowercase__ ) , 6 ) ).encode() + padding ) def A ( lowercase__ : str ) -> bytes: # Make sure encoded_data is either a string or a bytes-like object if not isinstance(lowercase__ , lowercase__ ) and not isinstance(lowercase__ , lowercase__ ): UpperCamelCase__ :Dict = ( """argument should be a bytes-like object or ASCII string, """ f"""not '{encoded_data.__class__.__name__}'""" ) raise TypeError(lowercase__ ) # In case encoded_data is a bytes-like object, make sure it contains only # ASCII characters so we convert it to a string object if isinstance(lowercase__ , lowercase__ ): try: UpperCamelCase__ :List[str] = encoded_data.decode("""utf-8""" ) except UnicodeDecodeError: raise ValueError("""base64 encoded data should only contain ASCII characters""" ) UpperCamelCase__ :int = encoded_data.count("""=""" ) # Check if the encoded string contains non base64 characters if padding: assert all( char in B64_CHARSET for char in encoded_data[:-padding] ), "Invalid base64 character(s) found." else: assert all( char in B64_CHARSET for char in encoded_data ), "Invalid base64 character(s) found." # Check the padding assert len(lowercase__ ) % 4 == 0 and padding < 3, "Incorrect padding" if padding: # Remove padding if there is one UpperCamelCase__ :int = encoded_data[:-padding] UpperCamelCase__ :Optional[int] = """""".join( bin(B64_CHARSET.index(lowercase__ ) )[2:].zfill(6 ) for char in encoded_data )[: -padding * 2] else: UpperCamelCase__ :List[str] = """""".join( bin(B64_CHARSET.index(lowercase__ ) )[2:].zfill(6 ) for char in encoded_data ) UpperCamelCase__ :Optional[int] = [ int(binary_stream[index : index + 8] , 2 ) for index in range(0 , len(lowercase__ ) , 8 ) ] return bytes(lowercase__ ) if __name__ == "__main__": import doctest doctest.testmod()
45
import logging import os import sys from dataclasses import dataclass, field from typing import Optional import torch from datasets import load_dataset from torchvision.transforms import Compose, Lambda, Normalize, RandomHorizontalFlip, RandomResizedCrop, ToTensor from torchvision.transforms.functional import InterpolationMode import transformers from transformers import ( HfArgumentParser, Trainer, TrainingArguments, ViTImageProcessor, ViTMAEConfig, ViTMAEForPreTraining, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version UpperCamelCase = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.31.0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/image-pretraining/requirements.txt") @dataclass class lowerCAmelCase_ : """simple docstring""" _snake_case : Optional[str] = field( default="""cifar10""" , metadata={"""help""": """Name of a dataset from the datasets package"""} ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """The column name of the images in the files."""} ) _snake_case : Optional[str] = field(default=lowercase , metadata={"""help""": """A folder containing the training data."""} ) _snake_case : Optional[str] = field(default=lowercase , metadata={"""help""": """A folder containing the validation data."""} ) _snake_case : Optional[float] = field( default=0.15 , metadata={"""help""": """Percent to split off of train for validation."""} ) _snake_case : Optional[int] = field( default=lowercase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } , ) _snake_case : Optional[int] = field( default=lowercase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of evaluation examples to this """ """value if set.""" ) } , ) def __a ( self :List[str] ): UpperCamelCase__ :Optional[Any] = {} if self.train_dir is not None: UpperCamelCase__ :int = self.train_dir if self.validation_dir is not None: UpperCamelCase__ :List[str] = self.validation_dir UpperCamelCase__ :Optional[int] = data_files if data_files else None @dataclass class lowerCAmelCase_ : """simple docstring""" _snake_case : str = field( default=lowercase , metadata={ """help""": ( """The model checkpoint for weights initialization.Don't set if you want to train a model from scratch.""" ) } , ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """Pretrained config name or path if not the same as model_name_or_path"""} ) _snake_case : Optional[str] = field( default=lowercase , metadata={ """help""": ( """Override some existing default config settings when a model is trained from scratch. Example: """ """n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index""" ) } , ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """Where do you want to store the pretrained models downloaded from s3"""} ) _snake_case : str = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) _snake_case : str = field(default=lowercase , metadata={"""help""": """Name or path of preprocessor config."""} ) _snake_case : bool = field( default=lowercase , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) _snake_case : float = field( default=0.75 , metadata={"""help""": """The ratio of the number of masked tokens in the input sequence."""} ) _snake_case : bool = field( default=lowercase , metadata={"""help""": """Whether or not to train with normalized pixel values as target."""} ) @dataclass class lowerCAmelCase_ ( lowercase ): """simple docstring""" _snake_case : float = field( default=1e-3 , metadata={"""help""": """Base learning rate: absolute_lr = base_lr * total_batch_size / 256."""} ) def A ( lowercase__ : Union[str, Any] ) -> Dict: UpperCamelCase__ :Union[str, Any] = torch.stack([example["""pixel_values"""] for example in examples] ) return {"pixel_values": pixel_values} def A ( ) -> Optional[int]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. UpperCamelCase__ :Optional[int] = HfArgumentParser((ModelArguments, DataTrainingArguments, CustomTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(""".json""" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :str = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Union[str, Any] = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry("""run_mae""" , lowercase__ , lowercase__ ) # Setup logging logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() UpperCamelCase__ :List[str] = training_args.get_process_log_level() logger.setLevel(lowercase__ ) transformers.utils.logging.set_verbosity(lowercase__ ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( f"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}""" + f"""distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}""" ) logger.info(f"""Training/evaluation parameters {training_args}""" ) # Detecting last checkpoint. UpperCamelCase__ :Union[str, Any] = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: UpperCamelCase__ :List[str] = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( f"""Output directory ({training_args.output_dir}) already exists and is not empty. """ """Use --overwrite_output_dir to overcome.""" ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( f"""Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change """ """the `--output_dir` or add `--overwrite_output_dir` to train from scratch.""" ) # Initialize our dataset. UpperCamelCase__ :Tuple = load_dataset( data_args.dataset_name , data_args.dataset_config_name , data_files=data_args.data_files , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) # If we don't have a validation split, split off a percentage of train as validation. UpperCamelCase__ :int = None if """validation""" in ds.keys() else data_args.train_val_split if isinstance(data_args.train_val_split , lowercase__ ) and data_args.train_val_split > 0.0: UpperCamelCase__ :Optional[Any] = ds["""train"""].train_test_split(data_args.train_val_split ) UpperCamelCase__ :Union[str, Any] = split["""train"""] UpperCamelCase__ :Any = split["""test"""] # Load pretrained model and image processor # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. UpperCamelCase__ :Optional[int] = { """cache_dir""": model_args.cache_dir, """revision""": model_args.model_revision, """use_auth_token""": True if model_args.use_auth_token else None, } if model_args.config_name: UpperCamelCase__ :Any = ViTMAEConfig.from_pretrained(model_args.config_name , **lowercase__ ) elif model_args.model_name_or_path: UpperCamelCase__ :Union[str, Any] = ViTMAEConfig.from_pretrained(model_args.model_name_or_path , **lowercase__ ) else: UpperCamelCase__ :Optional[Any] = ViTMAEConfig() logger.warning("""You are instantiating a new config instance from scratch.""" ) if model_args.config_overrides is not None: logger.info(f"""Overriding config: {model_args.config_overrides}""" ) config.update_from_string(model_args.config_overrides ) logger.info(f"""New config: {config}""" ) # adapt config config.update( { """mask_ratio""": model_args.mask_ratio, """norm_pix_loss""": model_args.norm_pix_loss, } ) # create image processor if model_args.image_processor_name: UpperCamelCase__ :str = ViTImageProcessor.from_pretrained(model_args.image_processor_name , **lowercase__ ) elif model_args.model_name_or_path: UpperCamelCase__ :Dict = ViTImageProcessor.from_pretrained(model_args.model_name_or_path , **lowercase__ ) else: UpperCamelCase__ :Tuple = ViTImageProcessor() # create model if model_args.model_name_or_path: UpperCamelCase__ :Any = ViTMAEForPreTraining.from_pretrained( model_args.model_name_or_path , from_tf=bool(""".ckpt""" in model_args.model_name_or_path ) , config=lowercase__ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info("""Training new model from scratch""" ) UpperCamelCase__ :Optional[int] = ViTMAEForPreTraining(lowercase__ ) if training_args.do_train: UpperCamelCase__ :Optional[Any] = ds["""train"""].column_names else: UpperCamelCase__ :Union[str, Any] = ds["""validation"""].column_names if data_args.image_column_name is not None: UpperCamelCase__ :Union[str, Any] = data_args.image_column_name elif "image" in column_names: UpperCamelCase__ :Optional[Any] = """image""" elif "img" in column_names: UpperCamelCase__ :List[str] = """img""" else: UpperCamelCase__ :List[Any] = column_names[0] # transformations as done in original MAE paper # source: https://github.com/facebookresearch/mae/blob/main/main_pretrain.py if "shortest_edge" in image_processor.size: UpperCamelCase__ :List[str] = image_processor.size["""shortest_edge"""] else: UpperCamelCase__ :int = (image_processor.size["""height"""], image_processor.size["""width"""]) UpperCamelCase__ :Any = Compose( [ Lambda(lambda lowercase__ : img.convert("""RGB""" ) if img.mode != "RGB" else img ), RandomResizedCrop(lowercase__ , scale=(0.2, 1.0) , interpolation=InterpolationMode.BICUBIC ), RandomHorizontalFlip(), ToTensor(), Normalize(mean=image_processor.image_mean , std=image_processor.image_std ), ] ) def preprocess_images(lowercase__ : Tuple ): UpperCamelCase__ :List[Any] = [transforms(lowercase__ ) for image in examples[image_column_name]] return examples if training_args.do_train: if "train" not in ds: raise ValueError("""--do_train requires a train dataset""" ) if data_args.max_train_samples is not None: UpperCamelCase__ :Optional[int] = ds["""train"""].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) # Set the training transforms ds["train"].set_transform(lowercase__ ) if training_args.do_eval: if "validation" not in ds: raise ValueError("""--do_eval requires a validation dataset""" ) if data_args.max_eval_samples is not None: UpperCamelCase__ :Optional[Any] = ( ds["""validation"""].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms ds["validation"].set_transform(lowercase__ ) # Compute absolute learning rate UpperCamelCase__ :Tuple = ( training_args.train_batch_size * training_args.gradient_accumulation_steps * training_args.world_size ) if training_args.base_learning_rate is not None: UpperCamelCase__ :Any = training_args.base_learning_rate * total_train_batch_size / 256 # Initialize our trainer UpperCamelCase__ :Union[str, Any] = Trainer( model=lowercase__ , args=lowercase__ , train_dataset=ds["""train"""] if training_args.do_train else None , eval_dataset=ds["""validation"""] if training_args.do_eval else None , tokenizer=lowercase__ , data_collator=lowercase__ , ) # Training if training_args.do_train: UpperCamelCase__ :Any = None if training_args.resume_from_checkpoint is not None: UpperCamelCase__ :int = training_args.resume_from_checkpoint elif last_checkpoint is not None: UpperCamelCase__ :Dict = last_checkpoint UpperCamelCase__ :Union[str, Any] = trainer.train(resume_from_checkpoint=lowercase__ ) trainer.save_model() trainer.log_metrics("""train""" , train_result.metrics ) trainer.save_metrics("""train""" , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: UpperCamelCase__ :int = trainer.evaluate() trainer.log_metrics("""eval""" , lowercase__ ) trainer.save_metrics("""eval""" , lowercase__ ) # Write model card and (optionally) push to hub UpperCamelCase__ :Optional[int] = { """tasks""": """masked-auto-encoding""", """dataset""": data_args.dataset_name, """tags""": ["""masked-auto-encoding"""], } if training_args.push_to_hub: trainer.push_to_hub(**lowercase__ ) else: trainer.create_model_card(**lowercase__ ) def A ( lowercase__ : Union[str, Any] ) -> Dict: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
45
1
import numpy as np import skfuzzy as fuzz if __name__ == "__main__": # Create universe of discourse in Python using linspace () UpperCamelCase = np.linspace(start=0, stop=75, num=75, endpoint=True, retstep=False) # Create two fuzzy sets by defining any membership function # (trapmf(), gbellmf(), gaussmf(), etc). UpperCamelCase = [0, 25, 50] UpperCamelCase = [25, 50, 75] UpperCamelCase = fuzz.membership.trimf(X, abca) UpperCamelCase = fuzz.membership.trimf(X, abca) # Compute the different operations using inbuilt functions. UpperCamelCase = np.ones(75) UpperCamelCase = np.zeros((75,)) # 1. Union = max(µA(x), µB(x)) UpperCamelCase = fuzz.fuzzy_or(X, young, X, middle_aged)[1] # 2. Intersection = min(µA(x), µB(x)) UpperCamelCase = fuzz.fuzzy_and(X, young, X, middle_aged)[1] # 3. Complement (A) = (1- min(µA(x)) UpperCamelCase = fuzz.fuzzy_not(young) # 4. Difference (A/B) = min(µA(x),(1- µB(x))) UpperCamelCase = fuzz.fuzzy_and(X, young, X, fuzz.fuzzy_not(middle_aged)[1])[1] # 5. Algebraic Sum = [µA(x) + µB(x) – (µA(x) * µB(x))] UpperCamelCase = young + middle_aged - (young * middle_aged) # 6. Algebraic Product = (µA(x) * µB(x)) UpperCamelCase = young * middle_aged # 7. Bounded Sum = min[1,(µA(x), µB(x))] UpperCamelCase = fuzz.fuzzy_and(X, one, X, young + middle_aged)[1] # 8. Bounded difference = min[0,(µA(x), µB(x))] UpperCamelCase = fuzz.fuzzy_or(X, zero, X, young - middle_aged)[1] # max-min composition # max-product composition # Plot each set A, set B and each operation result using plot() and subplot(). from matplotlib import pyplot as plt plt.figure() plt.subplot(4, 3, 1) plt.plot(X, young) plt.title("Young") plt.grid(True) plt.subplot(4, 3, 2) plt.plot(X, middle_aged) plt.title("Middle aged") plt.grid(True) plt.subplot(4, 3, 3) plt.plot(X, union) plt.title("union") plt.grid(True) plt.subplot(4, 3, 4) plt.plot(X, intersection) plt.title("intersection") plt.grid(True) plt.subplot(4, 3, 5) plt.plot(X, complement_a) plt.title("complement_a") plt.grid(True) plt.subplot(4, 3, 6) plt.plot(X, difference) plt.title("difference a/b") plt.grid(True) plt.subplot(4, 3, 7) plt.plot(X, alg_sum) plt.title("alg_sum") plt.grid(True) plt.subplot(4, 3, 8) plt.plot(X, alg_product) plt.title("alg_product") plt.grid(True) plt.subplot(4, 3, 9) plt.plot(X, bdd_sum) plt.title("bdd_sum") plt.grid(True) plt.subplot(4, 3, 10) plt.plot(X, bdd_difference) plt.title("bdd_difference") plt.grid(True) plt.subplots_adjust(hspace=0.5) plt.show()
45
from __future__ import annotations def A ( lowercase__ : int ) -> list[int]: UpperCamelCase__ :Union[str, Any] = [True] * limit UpperCamelCase__ :int = False UpperCamelCase__ :Optional[Any] = False UpperCamelCase__ :str = True for i in range(3 , int(limit**0.5 + 1 ) , 2 ): UpperCamelCase__ :List[Any] = i * 2 while index < limit: UpperCamelCase__ :Tuple = False UpperCamelCase__ :Tuple = index + i UpperCamelCase__ :str = [2] for i in range(3 , lowercase__ , 2 ): if is_prime[i]: primes.append(lowercase__ ) return primes def A ( lowercase__ : int = 100_0000 ) -> int: UpperCamelCase__ :Any = prime_sieve(lowercase__ ) UpperCamelCase__ :Optional[int] = 0 UpperCamelCase__ :Optional[Any] = 0 for i in range(len(lowercase__ ) ): for j in range(i + length , len(lowercase__ ) ): UpperCamelCase__ :Any = sum(primes[i:j] ) if sol >= ceiling: break if sol in primes: UpperCamelCase__ :Union[str, Any] = j - i UpperCamelCase__ :Any = sol return largest if __name__ == "__main__": print(f'''{solution() = }''')
45
1
import json import logging import math import os import sys from dataclasses import dataclass, field from typing import Optional from datasets import Dataset, load_dataset import transformers from transformers import ( CONFIG_MAPPING, MODEL_FOR_MASKED_LM_MAPPING, AutoConfig, AutoModelForMaskedLM, AutoTokenizer, DataCollatorForWholeWordMask, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint, is_main_process UpperCamelCase = logging.getLogger(__name__) UpperCamelCase = list(MODEL_FOR_MASKED_LM_MAPPING.keys()) UpperCamelCase = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class lowerCAmelCase_ : """simple docstring""" _snake_case : Optional[str] = field( default=lowercase , metadata={ """help""": ( """The model checkpoint for weights initialization.Don't set if you want to train a model from scratch.""" ) } , ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """If training from scratch, pass a model type from the list: """ + """, """.join(lowercase )} , ) _snake_case : Optional[str] = field( default=lowercase , metadata={ """help""": ( """Override some existing default config settings when a model is trained from scratch. Example: """ """n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index""" ) } , ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} , ) _snake_case : bool = field( default=lowercase , metadata={"""help""": """Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."""} , ) _snake_case : str = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) _snake_case : bool = field( default=lowercase , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) def __a ( self :Tuple ): if self.config_overrides is not None and (self.config_name is not None or self.model_name_or_path is not None): raise ValueError( """--config_overrides can't be used in combination with --config_name or --model_name_or_path""" ) @dataclass class lowerCAmelCase_ : """simple docstring""" _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """The name of the dataset to use (via the datasets library)."""} ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} ) _snake_case : Optional[str] = field(default=lowercase , metadata={"""help""": """The input training data file (a text file)."""} ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """An optional input evaluation data file to evaluate the perplexity on (a text file)."""} , ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """An optional input train ref data file for whole word masking in Chinese."""} , ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """An optional input validation ref data file for whole word masking in Chinese."""} , ) _snake_case : bool = field( default=lowercase , metadata={"""help""": """Overwrite the cached training and evaluation sets"""} ) _snake_case : Optional[int] = field( default=5 , metadata={ """help""": """The percentage of the train set used as validation set in case there's no validation split""" } , ) _snake_case : Optional[int] = field( default=lowercase , metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated. Default to the max input length of the model.""" ) } , ) _snake_case : Optional[int] = field( default=lowercase , metadata={"""help""": """The number of processes to use for the preprocessing."""} , ) _snake_case : float = field( default=0.15 , metadata={"""help""": """Ratio of tokens to mask for masked language modeling loss"""} ) _snake_case : bool = field( default=lowercase , metadata={ """help""": ( """Whether to pad all samples to `max_seq_length`. """ """If False, will pad the samples dynamically when batching to the maximum length in the batch.""" ) } , ) def __a ( self :Dict ): if self.train_file is not None: UpperCamelCase__ :Optional[Any] = self.train_file.split(""".""" )[-1] assert extension in ["csv", "json", "txt"], "`train_file` should be a csv, a json or a txt file." if self.validation_file is not None: UpperCamelCase__ :Optional[int] = self.validation_file.split(""".""" )[-1] assert extension in ["csv", "json", "txt"], "`validation_file` should be a csv, a json or a txt file." def A ( lowercase__ : Optional[Any] , lowercase__ : str ) -> List[Any]: with open(lowercase__ , """r""" , encoding="""utf-8""" ) as f: UpperCamelCase__ :Dict = [json.loads(lowercase__ ) for line in f.read().splitlines() if (len(lowercase__ ) > 0 and not line.isspace())] assert len(lowercase__ ) == len(lowercase__ ) UpperCamelCase__ :int = {c: dataset[c] for c in dataset.column_names} UpperCamelCase__ :List[Any] = refs return Dataset.from_dict(lowercase__ ) def A ( ) -> Dict: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. UpperCamelCase__ :Any = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(""".json""" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :List[str] = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Dict = parser.parse_args_into_dataclasses() # Detecting last checkpoint. UpperCamelCase__ :int = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: UpperCamelCase__ :Optional[Any] = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( f"""Output directory ({training_args.output_dir}) already exists and is not empty. """ """Use --overwrite_output_dir to 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() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info("""Training/evaluation parameters %s""" , lowercase__ ) # Set seed before initializing model. set_seed(training_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). # # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.dataset_name is not None: # Downloading and loading a dataset from the hub. UpperCamelCase__ :List[str] = load_dataset(data_args.dataset_name , data_args.dataset_config_name ) if "validation" not in datasets.keys(): UpperCamelCase__ :Any = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=f"""train[:{data_args.validation_split_percentage}%]""" , ) UpperCamelCase__ :Dict = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=f"""train[{data_args.validation_split_percentage}%:]""" , ) else: UpperCamelCase__ :Union[str, Any] = {} if data_args.train_file is not None: UpperCamelCase__ :List[Any] = data_args.train_file if data_args.validation_file is not None: UpperCamelCase__ :str = data_args.validation_file UpperCamelCase__ :Tuple = data_args.train_file.split(""".""" )[-1] if extension == "txt": UpperCamelCase__ :List[str] = """text""" UpperCamelCase__ :Optional[int] = load_dataset(lowercase__ , data_files=lowercase__ ) # 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. # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. UpperCamelCase__ :Union[str, Any] = { """cache_dir""": model_args.cache_dir, """revision""": model_args.model_revision, """use_auth_token""": True if model_args.use_auth_token else None, } if model_args.config_name: UpperCamelCase__ :List[str] = AutoConfig.from_pretrained(model_args.config_name , **lowercase__ ) elif model_args.model_name_or_path: UpperCamelCase__ :Union[str, Any] = AutoConfig.from_pretrained(model_args.model_name_or_path , **lowercase__ ) else: UpperCamelCase__ :Union[str, Any] = CONFIG_MAPPING[model_args.model_type]() logger.warning("""You are instantiating a new config instance from scratch.""" ) if model_args.config_overrides is not None: logger.info(f"""Overriding config: {model_args.config_overrides}""" ) config.update_from_string(model_args.config_overrides ) logger.info(f"""New config: {config}""" ) UpperCamelCase__ :Union[str, Any] = { """cache_dir""": model_args.cache_dir, """use_fast""": model_args.use_fast_tokenizer, """revision""": model_args.model_revision, """use_auth_token""": True if model_args.use_auth_token else None, } if model_args.tokenizer_name: UpperCamelCase__ :Optional[int] = AutoTokenizer.from_pretrained(model_args.tokenizer_name , **lowercase__ ) elif model_args.model_name_or_path: UpperCamelCase__ :Any = AutoTokenizer.from_pretrained(model_args.model_name_or_path , **lowercase__ ) 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.""" ) if model_args.model_name_or_path: UpperCamelCase__ :Tuple = AutoModelForMaskedLM.from_pretrained( model_args.model_name_or_path , from_tf=bool(""".ckpt""" in model_args.model_name_or_path ) , config=lowercase__ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info("""Training new model from scratch""" ) UpperCamelCase__ :Optional[Any] = AutoModelForMaskedLM.from_config(lowercase__ ) model.resize_token_embeddings(len(lowercase__ ) ) # Preprocessing the datasets. # First we tokenize all the texts. if training_args.do_train: UpperCamelCase__ :Dict = datasets["""train"""].column_names else: UpperCamelCase__ :str = datasets["""validation"""].column_names UpperCamelCase__ :Optional[int] = """text""" if """text""" in column_names else column_names[0] UpperCamelCase__ :str = """max_length""" if data_args.pad_to_max_length else False def tokenize_function(lowercase__ : str ): # Remove empty lines UpperCamelCase__ :List[str] = [line for line in examples["""text"""] if len(lowercase__ ) > 0 and not line.isspace()] return tokenizer(examples["""text"""] , padding=lowercase__ , truncation=lowercase__ , max_length=data_args.max_seq_length ) UpperCamelCase__ :int = datasets.map( lowercase__ , batched=lowercase__ , num_proc=data_args.preprocessing_num_workers , remove_columns=[text_column_name] , load_from_cache_file=not data_args.overwrite_cache , ) # Add the chinese references if provided if data_args.train_ref_file is not None: UpperCamelCase__ :Tuple = add_chinese_references(tokenized_datasets["""train"""] , data_args.train_ref_file ) if data_args.validation_ref_file is not None: UpperCamelCase__ :Tuple = add_chinese_references( tokenized_datasets["""validation"""] , data_args.validation_ref_file ) # If we have ref files, need to avoid it removed by trainer UpperCamelCase__ :Optional[Any] = data_args.train_ref_file or data_args.validation_ref_file if has_ref: UpperCamelCase__ :List[str] = False # Data collator # This one will take care of randomly masking the tokens. UpperCamelCase__ :str = DataCollatorForWholeWordMask(tokenizer=lowercase__ , mlm_probability=data_args.mlm_probability ) # Initialize our Trainer UpperCamelCase__ :Union[str, Any] = Trainer( model=lowercase__ , args=lowercase__ , train_dataset=tokenized_datasets["""train"""] if training_args.do_train else None , eval_dataset=tokenized_datasets["""validation"""] if training_args.do_eval else None , tokenizer=lowercase__ , data_collator=lowercase__ , ) # Training if training_args.do_train: if last_checkpoint is not None: UpperCamelCase__ :List[Any] = last_checkpoint elif model_args.model_name_or_path is not None and os.path.isdir(model_args.model_name_or_path ): UpperCamelCase__ :int = model_args.model_name_or_path else: UpperCamelCase__ :Optional[Any] = None UpperCamelCase__ :List[Any] = trainer.train(resume_from_checkpoint=lowercase__ ) trainer.save_model() # Saves the tokenizer too for easy upload UpperCamelCase__ :int = os.path.join(training_args.output_dir , """train_results.txt""" ) if trainer.is_world_process_zero(): with open(lowercase__ , """w""" ) as writer: logger.info("""***** Train results *****""" ) for key, value in sorted(train_result.metrics.items() ): logger.info(f""" {key} = {value}""" ) writer.write(f"""{key} = {value}\n""" ) # Need to save the state, since Trainer.save_model saves only the tokenizer with the model trainer.state.save_to_json(os.path.join(training_args.output_dir , """trainer_state.json""" ) ) # Evaluation UpperCamelCase__ :Optional[Any] = {} if training_args.do_eval: logger.info("""*** Evaluate ***""" ) UpperCamelCase__ :str = trainer.evaluate() UpperCamelCase__ :Dict = math.exp(eval_output["""eval_loss"""] ) UpperCamelCase__ :int = perplexity UpperCamelCase__ :Union[str, Any] = os.path.join(training_args.output_dir , """eval_results_mlm_wwm.txt""" ) if trainer.is_world_process_zero(): with open(lowercase__ , """w""" ) as writer: logger.info("""***** Eval results *****""" ) for key, value in sorted(results.items() ): logger.info(f""" {key} = {value}""" ) writer.write(f"""{key} = {value}\n""" ) return results def A ( lowercase__ : Tuple ) -> Tuple: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
45
import unittest from transformers import GPTNeoXJapaneseConfig, is_torch_available from transformers.models.gpt_neox_japanese.tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer from transformers.testing_utils import 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 GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseModel class lowerCAmelCase_ : """simple docstring""" def __init__( self :Optional[Any] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Tuple=13 , lowerCamelCase__ :Tuple=7 , lowerCamelCase__ :Optional[Any]=True , lowerCamelCase__ :Union[str, Any]=True , lowerCamelCase__ :Optional[int]=True , lowerCamelCase__ :List[Any]=True , lowerCamelCase__ :List[str]=99 , lowerCamelCase__ :int=32 , lowerCamelCase__ :List[Any]=5 , lowerCamelCase__ :Tuple=4 , lowerCamelCase__ :List[Any]=4 , lowerCamelCase__ :str="gelu" , lowerCamelCase__ :Optional[Any]=0.0 , lowerCamelCase__ :Optional[int]=0.1 , lowerCamelCase__ :str=True , lowerCamelCase__ :Dict=5_12 , lowerCamelCase__ :Optional[Any]=16 , lowerCamelCase__ :Optional[Any]=2 , lowerCamelCase__ :Union[str, Any]=0.02 , lowerCamelCase__ :Union[str, Any]=3 , lowerCamelCase__ :int=4 , lowerCamelCase__ :str=None , ): UpperCamelCase__ :Optional[Any] = parent UpperCamelCase__ :Dict = batch_size UpperCamelCase__ :Tuple = seq_length UpperCamelCase__ :Dict = is_training UpperCamelCase__ :List[str] = use_input_mask UpperCamelCase__ :Optional[Any] = use_token_type_ids UpperCamelCase__ :Tuple = use_labels UpperCamelCase__ :int = vocab_size UpperCamelCase__ :Tuple = hidden_size UpperCamelCase__ :Optional[Any] = num_hidden_layers UpperCamelCase__ :int = num_attention_heads UpperCamelCase__ :Optional[int] = intermediate_multiple_size UpperCamelCase__ :Optional[Any] = hidden_act UpperCamelCase__ :Optional[int] = hidden_dropout UpperCamelCase__ :List[Any] = attention_dropout UpperCamelCase__ :List[str] = weight_tying UpperCamelCase__ :List[str] = max_position_embeddings UpperCamelCase__ :Dict = type_vocab_size UpperCamelCase__ :List[Any] = type_sequence_label_size UpperCamelCase__ :List[str] = initializer_range UpperCamelCase__ :int = num_labels UpperCamelCase__ :Dict = num_choices UpperCamelCase__ :Any = scope def __a ( self :Any ): UpperCamelCase__ :Tuple = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCamelCase__ :str = None if self.use_input_mask: UpperCamelCase__ :Optional[int] = random_attention_mask([self.batch_size, self.seq_length] ) UpperCamelCase__ :Union[str, Any] = None if self.use_labels: UpperCamelCase__ :Tuple = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCamelCase__ :Optional[Any] = self.get_config() return config, input_ids, input_mask, token_labels def __a ( self :Union[str, Any] ): return GPTNeoXJapaneseConfig( 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_multiple_size=self.intermediate_multiple_size , hidden_act=self.hidden_act , hidden_dropout=self.hidden_dropout , attention_dropout=self.attention_dropout , weight_tying=self.weight_tying , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=lowerCamelCase__ , initializer_range=self.initializer_range , ) def __a ( self :Union[str, Any] ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Optional[Any] = self.prepare_config_and_inputs() UpperCamelCase__ :Optional[int] = True return config, input_ids, input_mask, token_labels def __a ( self :List[str] , lowerCamelCase__ :List[Any] , lowerCamelCase__ :Any , lowerCamelCase__ :Any ): UpperCamelCase__ :Union[str, Any] = GPTNeoXJapaneseModel(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() UpperCamelCase__ :Tuple = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = model(lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __a ( self :Dict , lowerCamelCase__ :Dict , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :List[Any] ): UpperCamelCase__ :List[str] = True UpperCamelCase__ :int = GPTNeoXJapaneseModel(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() UpperCamelCase__ :Union[str, Any] = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __a ( self :List[Any] , lowerCamelCase__ :Tuple , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Any , lowerCamelCase__ :Optional[Any] ): UpperCamelCase__ :Any = GPTNeoXJapaneseForCausalLM(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() UpperCamelCase__ :Tuple = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ , labels=lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __a ( self :Any , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :List[str] ): UpperCamelCase__ :Union[str, Any] = True UpperCamelCase__ :List[str] = GPTNeoXJapaneseForCausalLM(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() # first forward pass UpperCamelCase__ :Optional[Any] = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ , use_cache=lowerCamelCase__ ) UpperCamelCase__ :List[Any] = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids UpperCamelCase__ :List[Any] = ids_tensor((self.batch_size, 3) , config.vocab_size ) UpperCamelCase__ :Optional[Any] = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and UpperCamelCase__ :Union[str, Any] = torch.cat([input_ids, next_tokens] , dim=-1 ) UpperCamelCase__ :Optional[int] = torch.cat([input_mask, next_mask] , dim=-1 ) UpperCamelCase__ :Union[str, Any] = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ , output_hidden_states=lowerCamelCase__ ) UpperCamelCase__ :Optional[int] = output_from_no_past["""hidden_states"""][0] UpperCamelCase__ :Union[str, Any] = model( lowerCamelCase__ , attention_mask=lowerCamelCase__ , past_key_values=lowerCamelCase__ , output_hidden_states=lowerCamelCase__ , )["""hidden_states"""][0] # select random slice UpperCamelCase__ :int = ids_tensor((1,) , output_from_past.shape[-1] ).item() UpperCamelCase__ :str = output_from_no_past[:, -3:, random_slice_idx].detach() UpperCamelCase__ :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(lowerCamelCase__ , lowerCamelCase__ , atol=1e-3 ) ) def __a ( self :Tuple ): UpperCamelCase__ :int = self.prepare_config_and_inputs() UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :List[Any] = config_and_inputs UpperCamelCase__ :Any = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_torch class lowerCAmelCase_ ( lowercase , lowercase , unittest.TestCase ): """simple docstring""" _snake_case : Dict = (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else () _snake_case : int = (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else () _snake_case : str = ( {"""feature-extraction""": GPTNeoXJapaneseModel, """text-generation""": GPTNeoXJapaneseForCausalLM} if is_torch_available() else {} ) _snake_case : Union[str, Any] = False _snake_case : Dict = False _snake_case : List[str] = False _snake_case : Optional[int] = False def __a ( self :List[Any] ): UpperCamelCase__ :Tuple = GPTNeoXJapaneseModelTester(self ) UpperCamelCase__ :Optional[Any] = ConfigTester(self , config_class=lowerCamelCase__ , hidden_size=37 ) def __a ( self :Dict ): self.config_tester.run_common_tests() def __a ( self :Any ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Any ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Union[str, Any] ): # This regression test was failing with PyTorch < 1.3 UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :List[str] = self.model_tester.prepare_config_and_inputs_for_decoder() UpperCamelCase__ :Dict = None self.model_tester.create_and_check_model_as_decoder(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :List[str] ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Union[str, Any] ): UpperCamelCase__ :int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_causal_lm(*lowerCamelCase__ ) @slow def __a ( self :int ): UpperCamelCase__ :int = """abeja/gpt-neox-japanese-2.7b""" UpperCamelCase__ :List[Any] = ["""データサイエンティストとは、""", """100年後に必要とされる会社は、""", """フルリモートの環境で働くために必要なことは、""", """国境の長いトンネルを抜けると""", """美味しい日本食といえば、"""] UpperCamelCase__ :Union[str, Any] = [ """データサイエンティストとは、データを分析し、ビジネスに役立つ知見を導き出す専門家のことです。""", """100年後に必要とされる会社は、「人」が中心の会社です。""", """フルリモートの環境で働くために必要なことは、「自分の時間をコントロールする」ことです。""", """国境の長いトンネルを抜けると、そこは雪国だった。""", """美味しい日本食といえば、やっぱりお寿司ですよね。""", ] UpperCamelCase__ :Any = GPTNeoXJapaneseTokenizer.from_pretrained(lowerCamelCase__ ) UpperCamelCase__ :List[Any] = GPTNeoXJapaneseForCausalLM.from_pretrained(lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = [] for prompt in prompts: UpperCamelCase__ :str = tokenizer(lowerCamelCase__ , return_tensors="""pt""" ).input_ids UpperCamelCase__ :Union[str, Any] = model.generate(lowerCamelCase__ , max_length=50 ) UpperCamelCase__ :Dict = tokenizer.batch_decode(lowerCamelCase__ , skip_special_tokens=lowerCamelCase__ ) predicted_outputs += generated_string self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ )
45
1
import unittest import numpy as np from transformers.testing_utils import is_flaky, require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DonutImageProcessor class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __init__( self :Any , lowerCamelCase__ :str , lowerCamelCase__ :Dict=7 , lowerCamelCase__ :Any=3 , lowerCamelCase__ :List[str]=18 , lowerCamelCase__ :Tuple=30 , lowerCamelCase__ :Optional[Any]=4_00 , lowerCamelCase__ :Optional[Any]=True , lowerCamelCase__ :Optional[Any]=None , lowerCamelCase__ :Dict=True , lowerCamelCase__ :Any=False , lowerCamelCase__ :List[Any]=True , lowerCamelCase__ :List[Any]=True , lowerCamelCase__ :Dict=[0.5, 0.5, 0.5] , lowerCamelCase__ :Tuple=[0.5, 0.5, 0.5] , ): UpperCamelCase__ :Any = parent UpperCamelCase__ :Dict = batch_size UpperCamelCase__ :Any = num_channels UpperCamelCase__ :Union[str, Any] = image_size UpperCamelCase__ :Union[str, Any] = min_resolution UpperCamelCase__ :int = max_resolution UpperCamelCase__ :Dict = do_resize UpperCamelCase__ :List[str] = size if size is not None else {"""height""": 18, """width""": 20} UpperCamelCase__ :List[Any] = do_thumbnail UpperCamelCase__ :Tuple = do_align_axis UpperCamelCase__ :Optional[int] = do_pad UpperCamelCase__ :Optional[Any] = do_normalize UpperCamelCase__ :Optional[Any] = image_mean UpperCamelCase__ :Optional[Any] = image_std def __a ( self :Optional[Any] ): return { "do_resize": self.do_resize, "size": self.size, "do_thumbnail": self.do_thumbnail, "do_align_long_axis": self.do_align_axis, "do_pad": self.do_pad, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class lowerCAmelCase_ ( lowercase , unittest.TestCase ): """simple docstring""" _snake_case : Union[str, Any] = DonutImageProcessor if is_vision_available() else None def __a ( self :Tuple ): UpperCamelCase__ :str = DonutImageProcessingTester(self ) @property def __a ( self :Optional[Any] ): return self.image_processor_tester.prepare_image_processor_dict() def __a ( self :Optional[Any] ): UpperCamelCase__ :Optional[int] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(lowerCamelCase__ , """do_resize""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """size""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """do_thumbnail""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """do_align_long_axis""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """do_pad""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """do_normalize""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """image_mean""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """image_std""" ) ) def __a ( self :Optional[Any] ): UpperCamelCase__ :Tuple = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""height""": 18, """width""": 20} ) UpperCamelCase__ :Union[str, Any] = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {"""height""": 42, """width""": 42} ) # Previous config had dimensions in (width, height) order UpperCamelCase__ :Any = self.image_processing_class.from_dict(self.image_processor_dict , size=(42, 84) ) self.assertEqual(image_processor.size , {"""height""": 84, """width""": 42} ) def __a ( self :List[str] ): pass @is_flaky() def __a ( self :Dict ): # Initialize image_processing UpperCamelCase__ :Tuple = self.image_processing_class(**self.image_processor_dict ) # create random PIL images UpperCamelCase__ :Tuple = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCamelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCamelCase__ , Image.Image ) # Test not batched input UpperCamelCase__ :List[str] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) # Test batched UpperCamelCase__ :str = image_processing(lowerCamelCase__ , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) @is_flaky() def __a ( self :Optional[int] ): # Initialize image_processing UpperCamelCase__ :List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors UpperCamelCase__ :str = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCamelCase__ , numpify=lowerCamelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCamelCase__ , np.ndarray ) # Test not batched input UpperCamelCase__ :Any = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) # Test batched UpperCamelCase__ :str = image_processing(lowerCamelCase__ , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) @is_flaky() def __a ( self :Optional[int] ): # Initialize image_processing UpperCamelCase__ :List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors UpperCamelCase__ :List[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCamelCase__ , torchify=lowerCamelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCamelCase__ , torch.Tensor ) # Test not batched input UpperCamelCase__ :List[Any] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) # Test batched UpperCamelCase__ :Optional[Any] = image_processing(lowerCamelCase__ , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , )
45
import numpy as np from matplotlib import pyplot as plt from sklearn.datasets import load_iris from sklearn.metrics import ConfusionMatrixDisplay from sklearn.model_selection import train_test_split from xgboost import XGBClassifier def A ( lowercase__ : dict ) -> tuple: return (data["data"], data["target"]) def A ( lowercase__ : np.ndarray , lowercase__ : np.ndarray ) -> XGBClassifier: UpperCamelCase__ :Tuple = XGBClassifier() classifier.fit(lowercase__ , lowercase__ ) return classifier def A ( ) -> None: UpperCamelCase__ :str = load_iris() UpperCamelCase__ , UpperCamelCase__ :int = data_handling(lowercase__ ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :int = train_test_split( lowercase__ , lowercase__ , test_size=0.25 ) UpperCamelCase__ :Optional[int] = iris["""target_names"""] # Create an XGBoost Classifier from the training data UpperCamelCase__ :Optional[Any] = xgboost(lowercase__ , lowercase__ ) # Display the confusion matrix of the classifier with both training and test sets ConfusionMatrixDisplay.from_estimator( lowercase__ , lowercase__ , lowercase__ , display_labels=lowercase__ , cmap="""Blues""" , normalize="""true""" , ) plt.title("""Normalized Confusion Matrix - IRIS Dataset""" ) plt.show() if __name__ == "__main__": import doctest doctest.testmod(verbose=True) main()
45
1
# 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_torch_available, is_vision_available UpperCamelCase = { "configuration_vivit": ["VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP", "VivitConfig"], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ["VivitImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ "VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST", "VivitModel", "VivitPreTrainedModel", "VivitForVideoClassification", ] if TYPE_CHECKING: from .configuration_vivit import VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, VivitConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_vivit import VivitImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vivit import ( VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST, VivitForVideoClassification, VivitModel, VivitPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
45
import multiprocessing import time from arguments import PretokenizationArguments from datasets import load_dataset from transformers import AutoTokenizer, HfArgumentParser def A ( lowercase__ : Optional[int] ) -> Optional[Any]: UpperCamelCase__ :Union[str, Any] = {} UpperCamelCase__ :Optional[int] = tokenizer(example["""content"""] , truncation=lowercase__ )["""input_ids"""] UpperCamelCase__ :int = len(example["""content"""] ) / len(output["""input_ids"""] ) return output UpperCamelCase = HfArgumentParser(PretokenizationArguments) UpperCamelCase = parser.parse_args() if args.num_workers is None: UpperCamelCase = multiprocessing.cpu_count() UpperCamelCase = AutoTokenizer.from_pretrained(args.tokenizer_dir) UpperCamelCase = time.time() UpperCamelCase = load_dataset(args.dataset_name, split="train") print(f'''Dataset loaded in {time.time()-t_start:.2f}s''') UpperCamelCase = time.time() UpperCamelCase = ds.map( tokenize, num_proc=args.num_workers, remove_columns=[ "repo_name", "path", "copies", "size", "content", "license", "hash", "line_mean", "line_max", "alpha_frac", "autogenerated", ], ) print(f'''Dataset tokenized in {time.time()-t_start:.2f}s''') UpperCamelCase = time.time() ds.push_to_hub(args.tokenized_data_repo) print(f'''Data pushed to the hub in {time.time()-t_start:.2f}s''')
45
1
import time from dataclasses import dataclass from multiprocessing import Pool from unittest import TestCase from unittest.mock import patch import multiprocess import numpy as np import pytest from datasets.utils.py_utils import ( NestedDataStructure, asdict, iflatmap_unordered, map_nested, temp_seed, temporary_assignment, zip_dict, ) from .utils import require_tf, require_torch def A ( lowercase__ : str ) -> Any: # picklable for multiprocessing return x.sum() def A ( lowercase__ : Dict ) -> List[Any]: # picklable for multiprocessing return i + 1 @dataclass class lowerCAmelCase_ : """simple docstring""" _snake_case : int _snake_case : str class lowerCAmelCase_ ( lowercase ): """simple docstring""" def __a ( self :Union[str, Any] ): UpperCamelCase__ :List[str] = {} UpperCamelCase__ :Dict = [] UpperCamelCase__ :List[Any] = 1 UpperCamelCase__ :Optional[Any] = [1, 2] UpperCamelCase__ :Dict = {"""a""": 1, """b""": 2} UpperCamelCase__ :Dict = {"""a""": [1, 2], """b""": [3, 4]} UpperCamelCase__ :List[Any] = {"""a""": {"""1""": 1}, """b""": 2} UpperCamelCase__ :Optional[Any] = {"""a""": 1, """b""": 2, """c""": 3, """d""": 4} UpperCamelCase__ :Union[str, Any] = {} UpperCamelCase__ :Tuple = [] UpperCamelCase__ :Dict = 2 UpperCamelCase__ :Optional[Any] = [2, 3] UpperCamelCase__ :Dict = {"""a""": 2, """b""": 3} UpperCamelCase__ :int = {"""a""": [2, 3], """b""": [4, 5]} UpperCamelCase__ :Optional[int] = {"""a""": {"""1""": 2}, """b""": 3} UpperCamelCase__ :Union[str, Any] = {"""a""": 2, """b""": 3, """c""": 4, """d""": 5} self.assertEqual(map_nested(lowerCamelCase__ , lowerCamelCase__ ) , lowerCamelCase__ ) self.assertEqual(map_nested(lowerCamelCase__ , lowerCamelCase__ ) , lowerCamelCase__ ) self.assertEqual(map_nested(lowerCamelCase__ , lowerCamelCase__ ) , lowerCamelCase__ ) self.assertEqual(map_nested(lowerCamelCase__ , lowerCamelCase__ ) , lowerCamelCase__ ) self.assertEqual(map_nested(lowerCamelCase__ , lowerCamelCase__ ) , lowerCamelCase__ ) self.assertEqual(map_nested(lowerCamelCase__ , lowerCamelCase__ ) , lowerCamelCase__ ) self.assertEqual(map_nested(lowerCamelCase__ , lowerCamelCase__ ) , lowerCamelCase__ ) self.assertEqual(map_nested(lowerCamelCase__ , lowerCamelCase__ ) , lowerCamelCase__ ) UpperCamelCase__ :Dict = 2 self.assertEqual(map_nested(lowerCamelCase__ , lowerCamelCase__ , num_proc=lowerCamelCase__ ) , lowerCamelCase__ ) self.assertEqual(map_nested(lowerCamelCase__ , lowerCamelCase__ , num_proc=lowerCamelCase__ ) , lowerCamelCase__ ) self.assertEqual(map_nested(lowerCamelCase__ , lowerCamelCase__ , num_proc=lowerCamelCase__ ) , lowerCamelCase__ ) self.assertEqual(map_nested(lowerCamelCase__ , lowerCamelCase__ , num_proc=lowerCamelCase__ ) , lowerCamelCase__ ) self.assertEqual(map_nested(lowerCamelCase__ , lowerCamelCase__ , num_proc=lowerCamelCase__ ) , lowerCamelCase__ ) self.assertEqual(map_nested(lowerCamelCase__ , lowerCamelCase__ , num_proc=lowerCamelCase__ ) , lowerCamelCase__ ) self.assertEqual(map_nested(lowerCamelCase__ , lowerCamelCase__ , num_proc=lowerCamelCase__ ) , lowerCamelCase__ ) self.assertEqual(map_nested(lowerCamelCase__ , lowerCamelCase__ , num_proc=lowerCamelCase__ ) , lowerCamelCase__ ) UpperCamelCase__ :int = {"""a""": np.eye(2 ), """b""": np.zeros(3 ), """c""": np.ones(2 )} UpperCamelCase__ :int = {"""a""": 2, """b""": 0, """c""": 2} UpperCamelCase__ :Optional[int] = { """a""": np.eye(2 ).astype(lowerCamelCase__ ), """b""": np.zeros(3 ).astype(lowerCamelCase__ ), """c""": np.ones(2 ).astype(lowerCamelCase__ ), } self.assertEqual(map_nested(lowerCamelCase__ , lowerCamelCase__ , map_numpy=lowerCamelCase__ ) , lowerCamelCase__ ) self.assertEqual( {k: v.tolist() for k, v in map_nested(lowerCamelCase__ , lowerCamelCase__ , map_numpy=lowerCamelCase__ ).items()} , {k: v.tolist() for k, v in expected_map_nested_sna_int.items()} , ) self.assertEqual(map_nested(lowerCamelCase__ , lowerCamelCase__ , map_numpy=lowerCamelCase__ , num_proc=lowerCamelCase__ ) , lowerCamelCase__ ) self.assertEqual( {k: v.tolist() for k, v in map_nested(lowerCamelCase__ , lowerCamelCase__ , map_numpy=lowerCamelCase__ , num_proc=lowerCamelCase__ ).items()} , {k: v.tolist() for k, v in expected_map_nested_sna_int.items()} , ) with self.assertRaises(lowerCamelCase__ ): # can't pickle a local lambda map_nested(lambda lowerCamelCase__ : x + 1 , lowerCamelCase__ , num_proc=lowerCamelCase__ ) def __a ( self :Tuple ): UpperCamelCase__ :List[str] = {"""a""": 1, """b""": 2} UpperCamelCase__ :Tuple = {"""a""": 3, """b""": 4} UpperCamelCase__ :Optional[Any] = {"""a""": 5, """b""": 6} UpperCamelCase__ :Union[str, Any] = sorted([("""a""", (1, 3, 5)), ("""b""", (2, 4, 6))] ) self.assertEqual(sorted(zip_dict(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) ) , lowerCamelCase__ ) def __a ( self :Optional[Any] ): class lowerCAmelCase_ : """simple docstring""" _snake_case : List[str] = """bar""" UpperCamelCase__ :List[str] = Foo() self.assertEqual(foo.my_attr , """bar""" ) with temporary_assignment(lowerCamelCase__ , """my_attr""" , """BAR""" ): self.assertEqual(foo.my_attr , """BAR""" ) self.assertEqual(foo.my_attr , """bar""" ) @pytest.mark.parametrize( """iterable_length, num_proc, expected_num_proc""" , [ (1, None, 1), (1, 1, 1), (2, None, 1), (2, 1, 1), (2, 2, 1), (2, 3, 1), (3, 2, 1), (16, 16, 16), (16, 17, 16), (17, 16, 16), ] , ) def A ( lowercase__ : str , lowercase__ : Any , lowercase__ : Tuple ) -> Optional[int]: with patch("""datasets.utils.py_utils._single_map_nested""" ) as mock_single_map_nested, patch( """datasets.parallel.parallel.Pool""" ) as mock_multiprocessing_pool: UpperCamelCase__ :Optional[int] = {f"""{i}""": i for i in range(lowercase__ )} UpperCamelCase__ :Any = map_nested(lambda lowercase__ : x + 10 , lowercase__ , num_proc=lowercase__ , parallel_min_length=16 ) if expected_num_proc == 1: assert mock_single_map_nested.called assert not mock_multiprocessing_pool.called else: assert not mock_single_map_nested.called assert mock_multiprocessing_pool.called assert mock_multiprocessing_pool.call_args[0][0] == expected_num_proc class lowerCAmelCase_ ( lowercase ): """simple docstring""" @require_tf def __a ( self :str ): import tensorflow as tf from tensorflow.keras import layers UpperCamelCase__ :List[Any] = layers.Dense(2 ) def gen_random_output(): UpperCamelCase__ :str = tf.random.uniform((1, 3) ) return model(lowerCamelCase__ ).numpy() with temp_seed(42 , set_tensorflow=lowerCamelCase__ ): UpperCamelCase__ :List[Any] = gen_random_output() with temp_seed(42 , set_tensorflow=lowerCamelCase__ ): UpperCamelCase__ :Tuple = gen_random_output() UpperCamelCase__ :Any = gen_random_output() np.testing.assert_equal(lowerCamelCase__ , lowerCamelCase__ ) self.assertGreater(np.abs(outa - outa ).sum() , 0 ) @require_torch def __a ( self :Union[str, Any] ): import torch def gen_random_output(): UpperCamelCase__ :Optional[int] = torch.nn.Linear(3 , 2 ) UpperCamelCase__ :List[Any] = torch.rand(1 , 3 ) return model(lowerCamelCase__ ).detach().numpy() with temp_seed(42 , set_pytorch=lowerCamelCase__ ): UpperCamelCase__ :Optional[int] = gen_random_output() with temp_seed(42 , set_pytorch=lowerCamelCase__ ): UpperCamelCase__ :Union[str, Any] = gen_random_output() UpperCamelCase__ :Optional[Any] = gen_random_output() np.testing.assert_equal(lowerCamelCase__ , lowerCamelCase__ ) self.assertGreater(np.abs(outa - outa ).sum() , 0 ) def __a ( self :List[Any] ): def gen_random_output(): return np.random.rand(1 , 3 ) with temp_seed(42 ): UpperCamelCase__ :Optional[int] = gen_random_output() with temp_seed(42 ): UpperCamelCase__ :str = gen_random_output() UpperCamelCase__ :Any = gen_random_output() np.testing.assert_equal(lowerCamelCase__ , lowerCamelCase__ ) self.assertGreater(np.abs(outa - outa ).sum() , 0 ) @pytest.mark.parametrize("""input_data""" , [{}] ) def A ( lowercase__ : Dict ) -> Union[str, Any]: UpperCamelCase__ :Tuple = NestedDataStructure(lowercase__ ).data assert output_data == input_data @pytest.mark.parametrize( """data, expected_output""" , [ ({}, []), ([], []), ("""foo""", ["""foo"""]), (["""foo""", """bar"""], ["""foo""", """bar"""]), ([["""foo""", """bar"""]], ["""foo""", """bar"""]), ([[["""foo"""], ["""bar"""]]], ["""foo""", """bar"""]), ([[["""foo"""], """bar"""]], ["""foo""", """bar"""]), ({"""a""": 1, """b""": 2}, [1, 2]), ({"""a""": [1, 2], """b""": [3, 4]}, [1, 2, 3, 4]), ({"""a""": [[1, 2]], """b""": [[3, 4]]}, [1, 2, 3, 4]), ({"""a""": [[1, 2]], """b""": [3, 4]}, [1, 2, 3, 4]), ({"""a""": [[[1], [2]]], """b""": [[[3], [4]]]}, [1, 2, 3, 4]), ({"""a""": [[[1], [2]]], """b""": [[3, 4]]}, [1, 2, 3, 4]), ({"""a""": [[[1], [2]]], """b""": [3, 4]}, [1, 2, 3, 4]), ({"""a""": [[[1], [2]]], """b""": [3, [4]]}, [1, 2, 3, 4]), ({"""a""": {"""1""": 1}, """b""": 2}, [1, 2]), ({"""a""": {"""1""": [1]}, """b""": 2}, [1, 2]), ({"""a""": {"""1""": [1]}, """b""": [2]}, [1, 2]), ] , ) def A ( lowercase__ : str , lowercase__ : Union[str, Any] ) -> List[Any]: UpperCamelCase__ :str = NestedDataStructure(lowercase__ ).flatten() assert output == expected_output def A ( ) -> Optional[Any]: UpperCamelCase__ :Optional[int] = A(x=1 , y="""foobar""" ) UpperCamelCase__ :Dict = {"""x""": 1, """y""": """foobar"""} assert asdict(lowercase__ ) == expected_output UpperCamelCase__ :Union[str, Any] = {"""a""": {"""b""": A(x=10 , y="""foo""" )}, """c""": [A(x=20 , y="""bar""" )]} UpperCamelCase__ :Dict = {"""a""": {"""b""": {"""x""": 10, """y""": """foo"""}}, """c""": [{"""x""": 20, """y""": """bar"""}]} assert asdict(lowercase__ ) == expected_output with pytest.raises(lowercase__ ): asdict([1, A(x=10 , y="""foo""" )] ) def A ( lowercase__ : str ) -> List[str]: return text.split() def A ( lowercase__ : Any ) -> str: yield (time.time(), content) time.sleep(2 ) yield (time.time(), content) def A ( ) -> str: with Pool(2 ) as pool: UpperCamelCase__ :List[Any] = list(iflatmap_unordered(lowercase__ , _split_text , kwargs_iterable=[{"""text""": """hello there"""}] * 10 ) ) assert out.count("""hello""" ) == 10 assert out.count("""there""" ) == 10 assert len(lowercase__ ) == 20 # check multiprocess from pathos (uses dill for pickling) with multiprocess.Pool(2 ) as pool: UpperCamelCase__ :Union[str, Any] = list(iflatmap_unordered(lowercase__ , _split_text , kwargs_iterable=[{"""text""": """hello there"""}] * 10 ) ) assert out.count("""hello""" ) == 10 assert out.count("""there""" ) == 10 assert len(lowercase__ ) == 20 # check that we get items as fast as possible with Pool(2 ) as pool: UpperCamelCase__ :Tuple = [] for yield_time, content in iflatmap_unordered( lowercase__ , _aseconds_generator_of_aitems_with_timing , kwargs_iterable=[{"""content""": """a"""}, {"""content""": """b"""}] ): assert yield_time < time.time() + 0.1, "we should each item directly after it was yielded" out.append(lowercase__ ) assert out.count("""a""" ) == 2 assert out.count("""b""" ) == 2 assert len(lowercase__ ) == 4
45
def A ( lowercase__ : int ) -> Optional[Any]: stooge(lowercase__ , 0 , len(lowercase__ ) - 1 ) return arr def A ( lowercase__ : Union[str, Any] , lowercase__ : Dict , lowercase__ : str ) -> List[str]: if i >= h: return # If first element is smaller than the last then swap them if arr[i] > arr[h]: UpperCamelCase__ , UpperCamelCase__ :List[str] = arr[h], arr[i] # If there are more than 2 elements in the array if h - i + 1 > 2: UpperCamelCase__ :Optional[int] = (int)((h - i + 1) / 3 ) # Recursively sort first 2/3 elements stooge(lowercase__ , lowercase__ , (h - t) ) # Recursively sort last 2/3 elements stooge(lowercase__ , i + t , (lowercase__) ) # Recursively sort first 2/3 elements stooge(lowercase__ , lowercase__ , (h - t) ) if __name__ == "__main__": UpperCamelCase = input("Enter numbers separated by a comma:\n").strip() UpperCamelCase = [int(item) for item in user_input.split(",")] print(stooge_sort(unsorted))
45
1
class lowerCAmelCase_ : """simple docstring""" def __init__( self :List[Any] ): UpperCamelCase__ :Any = 0 UpperCamelCase__ :str = 0 UpperCamelCase__ :Union[str, Any] = {} def __a ( self :List[Any] , lowerCamelCase__ :Optional[int] ): if vertex not in self.adjacency: UpperCamelCase__ :List[str] = {} self.num_vertices += 1 def __a ( self :Dict , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :List[str] ): self.add_vertex(lowerCamelCase__ ) self.add_vertex(lowerCamelCase__ ) if head == tail: return UpperCamelCase__ :List[Any] = weight UpperCamelCase__ :Optional[int] = weight def __a ( self :Optional[int] ): UpperCamelCase__ :Tuple = self.get_edges() for edge in edges: UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Dict = edge edges.remove((tail, head, weight) ) for i in range(len(lowerCamelCase__ ) ): UpperCamelCase__ :int = list(edges[i] ) edges.sort(key=lambda lowerCamelCase__ : e[2] ) for i in range(len(lowerCamelCase__ ) - 1 ): if edges[i][2] >= edges[i + 1][2]: UpperCamelCase__ :Any = edges[i][2] + 1 for edge in edges: UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :List[str] = edge UpperCamelCase__ :Union[str, Any] = weight UpperCamelCase__ :str = weight def __str__( self :int ): UpperCamelCase__ :str = """""" for tail in self.adjacency: for head in self.adjacency[tail]: UpperCamelCase__ :Optional[int] = self.adjacency[head][tail] string += f"""{head} -> {tail} == {weight}\n""" return string.rstrip("""\n""" ) def __a ( self :Dict ): UpperCamelCase__ :Optional[int] = [] for tail in self.adjacency: for head in self.adjacency[tail]: output.append((tail, head, self.adjacency[head][tail]) ) return output def __a ( self :int ): return self.adjacency.keys() @staticmethod def __a ( lowerCamelCase__ :Dict=None , lowerCamelCase__ :Tuple=None ): UpperCamelCase__ :Optional[int] = Graph() if vertices is None: UpperCamelCase__ :Optional[int] = [] if edges is None: UpperCamelCase__ :List[str] = [] for vertex in vertices: g.add_vertex(lowerCamelCase__ ) for edge in edges: g.add_edge(*lowerCamelCase__ ) return g class lowerCAmelCase_ : """simple docstring""" def __init__( self :List[Any] ): UpperCamelCase__ :str = {} UpperCamelCase__ :Union[str, Any] = {} def __len__( self :Union[str, Any] ): return len(self.parent ) def __a ( self :Union[str, Any] , lowerCamelCase__ :Tuple ): if item in self.parent: return self.find(lowerCamelCase__ ) UpperCamelCase__ :Any = item UpperCamelCase__ :Optional[Any] = 0 return item def __a ( self :Optional[int] , lowerCamelCase__ :Any ): if item not in self.parent: return self.make_set(lowerCamelCase__ ) if item != self.parent[item]: UpperCamelCase__ :Optional[int] = self.find(self.parent[item] ) return self.parent[item] def __a ( self :Optional[int] , lowerCamelCase__ :Tuple , lowerCamelCase__ :List[str] ): UpperCamelCase__ :Any = self.find(lowerCamelCase__ ) UpperCamelCase__ :List[str] = self.find(lowerCamelCase__ ) if roota == roota: return roota if self.rank[roota] > self.rank[roota]: UpperCamelCase__ :List[str] = roota return roota if self.rank[roota] < self.rank[roota]: UpperCamelCase__ :Dict = roota return roota if self.rank[roota] == self.rank[roota]: self.rank[roota] += 1 UpperCamelCase__ :str = roota return roota return None @staticmethod def __a ( lowerCamelCase__ :Tuple ): UpperCamelCase__ :Optional[Any] = graph.num_vertices UpperCamelCase__ :Optional[int] = Graph.UnionFind() UpperCamelCase__ :Union[str, Any] = [] while num_components > 1: UpperCamelCase__ :Union[str, Any] = {} for vertex in graph.get_vertices(): UpperCamelCase__ :str = -1 UpperCamelCase__ :str = graph.get_edges() for edge in edges: UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Tuple = edge edges.remove((tail, head, weight) ) for edge in edges: UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :str = edge UpperCamelCase__ :Dict = union_find.find(lowerCamelCase__ ) UpperCamelCase__ :Optional[int] = union_find.find(lowerCamelCase__ ) if seta != seta: if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: UpperCamelCase__ :int = [head, tail, weight] if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: UpperCamelCase__ :List[str] = [head, tail, weight] for vertex in cheap_edge: if cheap_edge[vertex] != -1: UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Union[str, Any] = cheap_edge[vertex] if union_find.find(lowerCamelCase__ ) != union_find.find(lowerCamelCase__ ): union_find.union(lowerCamelCase__ , lowerCamelCase__ ) mst_edges.append(cheap_edge[vertex] ) UpperCamelCase__ :List[Any] = num_components - 1 UpperCamelCase__ :List[str] = Graph.build(edges=lowerCamelCase__ ) return mst
45
import argparse import json import os from tensorflow.core.protobuf.saved_model_pba import SavedModel # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_copies.py UpperCamelCase = "." # Internal TensorFlow ops that can be safely ignored (mostly specific to a saved model) UpperCamelCase = [ "Assert", "AssignVariableOp", "EmptyTensorList", "MergeV2Checkpoints", "ReadVariableOp", "ResourceGather", "RestoreV2", "SaveV2", "ShardedFilename", "StatefulPartitionedCall", "StaticRegexFullMatch", "VarHandleOp", ] def A ( lowercase__ : Tuple , lowercase__ : Optional[Any] , lowercase__ : Dict ) -> List[Any]: UpperCamelCase__ :str = SavedModel() UpperCamelCase__ :List[str] = [] with open(os.path.join(lowercase__ , """utils""" , """tf_ops""" , """onnx.json""" ) ) as f: UpperCamelCase__ :str = json.load(lowercase__ )["""opsets"""] for i in range(1 , opset + 1 ): onnx_ops.extend(onnx_opsets[str(lowercase__ )] ) with open(lowercase__ , """rb""" ) as f: saved_model.ParseFromString(f.read() ) UpperCamelCase__ :Tuple = set() # Iterate over every metagraph in case there is more than one (a saved model can contain multiple graphs) for meta_graph in saved_model.meta_graphs: # Add operations in the graph definition model_op_names.update(node.op for node in meta_graph.graph_def.node ) # Go through the functions in the graph definition for func in meta_graph.graph_def.library.function: # Add operations in each function model_op_names.update(node.op for node in func.node_def ) # Convert to list, sorted if you want UpperCamelCase__ :Union[str, Any] = sorted(lowercase__ ) UpperCamelCase__ :List[Any] = [] for op in model_op_names: if op not in onnx_ops and op not in INTERNAL_OPS: incompatible_ops.append(lowercase__ ) if strict and len(lowercase__ ) > 0: raise Exception(f"""Found the following incompatible ops for the opset {opset}:\n""" + incompatible_ops ) elif len(lowercase__ ) > 0: print(f"""Found the following incompatible ops for the opset {opset}:""" ) print(*lowercase__ , sep="""\n""" ) else: print(f"""The saved model {saved_model_path} can properly be converted with ONNX.""" ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() parser.add_argument("--saved_model_path", help="Path of the saved model to check (the .pb file).") parser.add_argument( "--opset", default=12, type=int, help="The ONNX opset against which the model has to be tested." ) parser.add_argument( "--framework", choices=["onnx"], default="onnx", help="Frameworks against which to test the saved model." ) parser.add_argument( "--strict", action="store_true", help="Whether make the checking strict (raise errors) or not (raise warnings)" ) UpperCamelCase = parser.parse_args() if args.framework == "onnx": onnx_compliancy(args.saved_model_path, args.strict, args.opset)
45
1
import json import os from collections import Counter import torch import torchvision import torchvision.transforms as transforms from PIL import Image from torch import nn from torch.utils.data import Dataset UpperCamelCase = {1: (1, 1), 2: (2, 1), 3: (3, 1), 4: (2, 2), 5: (5, 1), 6: (3, 2), 7: (7, 1), 8: (4, 2), 9: (3, 3)} class lowerCAmelCase_ ( nn.Module ): """simple docstring""" def __init__( self :Optional[int] , lowerCamelCase__ :Optional[int] ): super().__init__() UpperCamelCase__ :Optional[int] = torchvision.models.resnetaaa(pretrained=lowerCamelCase__ ) UpperCamelCase__ :Union[str, Any] = list(model.children() )[:-2] UpperCamelCase__ :Dict = nn.Sequential(*lowerCamelCase__ ) UpperCamelCase__ :int = nn.AdaptiveAvgPoolad(POOLING_BREAKDOWN[args.num_image_embeds] ) def __a ( self :int , lowerCamelCase__ :Union[str, Any] ): # Bx3x224x224 -> Bx2048x7x7 -> Bx2048xN -> BxNx2048 UpperCamelCase__ :Dict = self.pool(self.model(lowerCamelCase__ ) ) UpperCamelCase__ :str = torch.flatten(lowerCamelCase__ , start_dim=2 ) UpperCamelCase__ :List[Any] = out.transpose(1 , 2 ).contiguous() return out # BxNx2048 class lowerCAmelCase_ ( lowercase ): """simple docstring""" def __init__( self :str , lowerCamelCase__ :int , lowerCamelCase__ :Any , lowerCamelCase__ :Any , lowerCamelCase__ :Dict , lowerCamelCase__ :int ): UpperCamelCase__ :str = [json.loads(lowerCamelCase__ ) for l in open(lowerCamelCase__ )] UpperCamelCase__ :Optional[Any] = os.path.dirname(lowerCamelCase__ ) UpperCamelCase__ :Union[str, Any] = tokenizer UpperCamelCase__ :List[str] = labels UpperCamelCase__ :Tuple = len(lowerCamelCase__ ) UpperCamelCase__ :Dict = max_seq_length UpperCamelCase__ :Tuple = transforms def __len__( self :Optional[Any] ): return len(self.data ) def __getitem__( self :List[Any] , lowerCamelCase__ :Any ): UpperCamelCase__ :Union[str, Any] = torch.LongTensor(self.tokenizer.encode(self.data[index]["""text"""] , add_special_tokens=lowerCamelCase__ ) ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Any = sentence[0], sentence[1:-1], sentence[-1] UpperCamelCase__ :Union[str, Any] = sentence[: self.max_seq_length] UpperCamelCase__ :Union[str, Any] = torch.zeros(self.n_classes ) UpperCamelCase__ :str = 1 UpperCamelCase__ :Tuple = Image.open(os.path.join(self.data_dir , self.data[index]["""img"""] ) ).convert("""RGB""" ) UpperCamelCase__ :Optional[Any] = self.transforms(lowerCamelCase__ ) return { "image_start_token": start_token, "image_end_token": end_token, "sentence": sentence, "image": image, "label": label, } def __a ( self :List[str] ): UpperCamelCase__ :Dict = Counter() for row in self.data: label_freqs.update(row["""label"""] ) return label_freqs def A ( lowercase__ : Tuple ) -> Union[str, Any]: UpperCamelCase__ :Tuple = [len(row["""sentence"""] ) for row in batch] UpperCamelCase__ , UpperCamelCase__ :int = len(lowercase__ ), max(lowercase__ ) UpperCamelCase__ :Union[str, Any] = torch.zeros(lowercase__ , lowercase__ , dtype=torch.long ) UpperCamelCase__ :List[str] = torch.zeros(lowercase__ , lowercase__ , dtype=torch.long ) for i_batch, (input_row, length) in enumerate(zip(lowercase__ , lowercase__ ) ): UpperCamelCase__ :Optional[Any] = input_row["""sentence"""] UpperCamelCase__ :Any = 1 UpperCamelCase__ :Any = torch.stack([row["""image"""] for row in batch] ) UpperCamelCase__ :Optional[int] = torch.stack([row["""label"""] for row in batch] ) UpperCamelCase__ :Optional[int] = torch.stack([row["""image_start_token"""] for row in batch] ) UpperCamelCase__ :Any = torch.stack([row["""image_end_token"""] for row in batch] ) return text_tensor, mask_tensor, img_tensor, img_start_token, img_end_token, tgt_tensor def A ( ) -> Any: return [ "Crime", "Drama", "Thriller", "Action", "Comedy", "Romance", "Documentary", "Short", "Mystery", "History", "Family", "Adventure", "Fantasy", "Sci-Fi", "Western", "Horror", "Sport", "War", "Music", "Musical", "Animation", "Biography", "Film-Noir", ] def A ( ) -> Tuple: return transforms.Compose( [ transforms.Resize(256 ), transforms.CenterCrop(224 ), transforms.ToTensor(), transforms.Normalize( mean=[0.46777044, 0.44531429, 0.40661017] , std=[0.12221994, 0.12145835, 0.14380469] , ), ] )
45
from __future__ import annotations def A ( lowercase__ : str , lowercase__ : list[str] | None = None , lowercase__ : dict[str, float] | None = None , lowercase__ : bool = False , ) -> tuple[int, float, str]: UpperCamelCase__ :Dict = cipher_alphabet or [chr(lowercase__ ) for i in range(97 , 123 )] # If the argument is None or the user provided an empty dictionary if not frequencies_dict: # Frequencies of letters in the english language (how much they show up) UpperCamelCase__ :Optional[Any] = { """a""": 0.08497, """b""": 0.01492, """c""": 0.02202, """d""": 0.04253, """e""": 0.11162, """f""": 0.02228, """g""": 0.02015, """h""": 0.06094, """i""": 0.07546, """j""": 0.00153, """k""": 0.01292, """l""": 0.04025, """m""": 0.02406, """n""": 0.06749, """o""": 0.07507, """p""": 0.01929, """q""": 0.00095, """r""": 0.07587, """s""": 0.06327, """t""": 0.09356, """u""": 0.02758, """v""": 0.00978, """w""": 0.02560, """x""": 0.00150, """y""": 0.01994, """z""": 0.00077, } else: # Custom frequencies dictionary UpperCamelCase__ :Optional[int] = frequencies_dict if not case_sensitive: UpperCamelCase__ :int = ciphertext.lower() # Chi squared statistic values UpperCamelCase__ :dict[int, tuple[float, str]] = {} # cycle through all of the shifts for shift in range(len(lowercase__ ) ): UpperCamelCase__ :int = """""" # decrypt the message with the shift for letter in ciphertext: try: # Try to index the letter in the alphabet UpperCamelCase__ :int = (alphabet_letters.index(letter.lower() ) - shift) % len( lowercase__ ) decrypted_with_shift += ( alphabet_letters[new_key].upper() if case_sensitive and letter.isupper() else alphabet_letters[new_key] ) except ValueError: # Append the character if it isn't in the alphabet decrypted_with_shift += letter UpperCamelCase__ :Optional[int] = 0.0 # Loop through each letter in the decoded message with the shift for letter in decrypted_with_shift: if case_sensitive: UpperCamelCase__ :Optional[int] = letter.lower() if letter in frequencies: # Get the amount of times the letter occurs in the message UpperCamelCase__ :Optional[int] = decrypted_with_shift.lower().count(lowercase__ ) # Get the excepcted amount of times the letter should appear based # on letter frequencies UpperCamelCase__ :Optional[int] = frequencies[letter] * occurrences # Complete the chi squared statistic formula UpperCamelCase__ :Dict = ((occurrences - expected) ** 2) / expected # Add the margin of error to the total chi squared statistic chi_squared_statistic += chi_letter_value else: if letter.lower() in frequencies: # Get the amount of times the letter occurs in the message UpperCamelCase__ :List[str] = decrypted_with_shift.count(lowercase__ ) # Get the excepcted amount of times the letter should appear based # on letter frequencies UpperCamelCase__ :Union[str, Any] = frequencies[letter] * occurrences # Complete the chi squared statistic formula UpperCamelCase__ :List[str] = ((occurrences - expected) ** 2) / expected # Add the margin of error to the total chi squared statistic chi_squared_statistic += chi_letter_value # Add the data to the chi_squared_statistic_values dictionary UpperCamelCase__ :Union[str, Any] = ( chi_squared_statistic, decrypted_with_shift, ) # Get the most likely cipher by finding the cipher with the smallest chi squared # statistic def chi_squared_statistic_values_sorting_key(lowercase__ : int ) -> tuple[float, str]: return chi_squared_statistic_values[key] UpperCamelCase__ :int = min( lowercase__ , key=lowercase__ , ) # Get all the data from the most likely cipher (key, decoded message) ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :Tuple = chi_squared_statistic_values[most_likely_cipher] # Return the data on the most likely shift return ( most_likely_cipher, most_likely_cipher_chi_squared_value, decoded_most_likely_cipher, )
45
1
def A ( lowercase__ : int , lowercase__ : int ) -> float: return base * power(lowercase__ , (exponent - 1) ) if exponent else 1 if __name__ == "__main__": print("Raise base to the power of exponent using recursion...") UpperCamelCase = int(input("Enter the base: ").strip()) UpperCamelCase = int(input("Enter the exponent: ").strip()) UpperCamelCase = power(base, abs(exponent)) if exponent < 0: # power() does not properly deal w/ negative exponents UpperCamelCase = 1 / result print(f'''{base} to the power of {exponent} is {result}''')
45
import warnings from ...utils import logging from .image_processing_mobilevit import MobileViTImageProcessor UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( lowercase ): """simple docstring""" def __init__( self :Union[str, Any] , *lowerCamelCase__ :Optional[int] , **lowerCamelCase__ :Dict ): warnings.warn( """The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.""" """ Please use MobileViTImageProcessor instead.""" , lowerCamelCase__ , ) super().__init__(*lowerCamelCase__ , **lowerCamelCase__ )
45
1
def __lowercase ( snake_case ): """simple docstring""" __magic_name__ :Optional[Any] = 0 for ch in input_str: __magic_name__ :int = ord(snake_case ) __magic_name__ :Dict = pow(2, snake_case ) # 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()
0
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 UpperCamelCase = get_tests_dir("fixtures") UpperCamelCase = get_tests_dir("fixtures/dummy_feature_extractor_config.json") UpperCamelCase = get_tests_dir("fixtures/dummy-config.json") class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __a ( self :Optional[int] ): UpperCamelCase__ :Optional[int] = 0 def __a ( self :str ): UpperCamelCase__ :Optional[Any] = AutoFeatureExtractor.from_pretrained("""facebook/wav2vec2-base-960h""" ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Dict ): UpperCamelCase__ :Dict = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Optional[int] ): with tempfile.TemporaryDirectory() as tmpdirname: UpperCamelCase__ :List[str] = WavaVecaConfig() # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally UpperCamelCase__ :Tuple = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ ).to_dict() config_dict.pop("""feature_extractor_type""" ) UpperCamelCase__ :Union[str, Any] = WavaVecaFeatureExtractor(**lowerCamelCase__ ) # save in new folder model_config.save_pretrained(lowerCamelCase__ ) config.save_pretrained(lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ ) # make sure private variable is not incorrectly saved UpperCamelCase__ :Tuple = json.loads(config.to_json_string() ) self.assertTrue("""_processor_class""" not in dict_as_saved ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Union[str, Any] ): UpperCamelCase__ :Dict = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Dict ): with self.assertRaisesRegex( lowerCamelCase__ , """bert-base is not a local folder and is not a valid model identifier""" ): UpperCamelCase__ :Dict = AutoFeatureExtractor.from_pretrained("""bert-base""" ) def __a ( self :List[Any] ): with self.assertRaisesRegex( lowerCamelCase__ , r"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)""" ): UpperCamelCase__ :Optional[int] = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ , revision="""aaaaaa""" ) def __a ( self :int ): with self.assertRaisesRegex( lowerCamelCase__ , """hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.""" , ): UpperCamelCase__ :List[Any] = AutoFeatureExtractor.from_pretrained("""hf-internal-testing/config-no-model""" ) def __a ( self :Optional[int] ): # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(lowerCamelCase__ ): UpperCamelCase__ :List[Any] = AutoFeatureExtractor.from_pretrained( """hf-internal-testing/test_dynamic_feature_extractor""" ) # If remote code is disabled, we can't load this config. with self.assertRaises(lowerCamelCase__ ): UpperCamelCase__ :Optional[Any] = AutoFeatureExtractor.from_pretrained( """hf-internal-testing/test_dynamic_feature_extractor""" , trust_remote_code=lowerCamelCase__ ) UpperCamelCase__ :str = AutoFeatureExtractor.from_pretrained( """hf-internal-testing/test_dynamic_feature_extractor""" , trust_remote_code=lowerCamelCase__ ) self.assertEqual(feature_extractor.__class__.__name__ , """NewFeatureExtractor""" ) # Test feature extractor can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained(lowerCamelCase__ ) UpperCamelCase__ :Any = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ , trust_remote_code=lowerCamelCase__ ) self.assertEqual(reloaded_feature_extractor.__class__.__name__ , """NewFeatureExtractor""" ) def __a ( self :Dict ): try: AutoConfig.register("""custom""" , lowerCamelCase__ ) AutoFeatureExtractor.register(lowerCamelCase__ , lowerCamelCase__ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(lowerCamelCase__ ): AutoFeatureExtractor.register(lowerCamelCase__ , lowerCamelCase__ ) # Now that the config is registered, it can be used as any other config with the auto-API UpperCamelCase__ :Any = CustomFeatureExtractor.from_pretrained(lowerCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained(lowerCamelCase__ ) UpperCamelCase__ :List[Any] = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) 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 __a ( self :Optional[int] ): class lowerCAmelCase_ ( lowercase ): """simple docstring""" _snake_case : Optional[int] = True try: AutoConfig.register("""custom""" , lowerCamelCase__ ) AutoFeatureExtractor.register(lowerCamelCase__ , lowerCamelCase__ ) # If remote code is not set, the default is to use local UpperCamelCase__ :Optional[Any] = 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. UpperCamelCase__ :str = AutoFeatureExtractor.from_pretrained( """hf-internal-testing/test_dynamic_feature_extractor""" , trust_remote_code=lowerCamelCase__ ) self.assertEqual(feature_extractor.__class__.__name__ , """NewFeatureExtractor""" ) self.assertTrue(feature_extractor.is_local ) # If remote is enabled, we load from the Hub UpperCamelCase__ :Optional[int] = AutoFeatureExtractor.from_pretrained( """hf-internal-testing/test_dynamic_feature_extractor""" , trust_remote_code=lowerCamelCase__ ) self.assertEqual(feature_extractor.__class__.__name__ , """NewFeatureExtractor""" ) self.assertTrue(not hasattr(lowerCamelCase__ , """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]
45
0
from ..utils import DummyObject, requires_backends class __lowerCamelCase (metaclass=_a ): _lowercase = ["""flax""", """transformers"""] def __init__( self: List[Any],*A_: List[str],**A_: int ): '''simple docstring''' requires_backends(self,['flax', 'transformers'] ) @classmethod def snake_case_ ( cls: Tuple,*A_: List[Any],**A_: Optional[int] ): '''simple docstring''' requires_backends(cls,['flax', 'transformers'] ) @classmethod def snake_case_ ( cls: Optional[Any],*A_: Optional[int],**A_: Tuple ): '''simple docstring''' requires_backends(cls,['flax', 'transformers'] ) class __lowerCamelCase (metaclass=_a ): _lowercase = ["""flax""", """transformers"""] def __init__( self: Optional[int],*A_: Tuple,**A_: str ): '''simple docstring''' requires_backends(self,['flax', 'transformers'] ) @classmethod def snake_case_ ( cls: Optional[int],*A_: Dict,**A_: Dict ): '''simple docstring''' requires_backends(cls,['flax', 'transformers'] ) @classmethod def snake_case_ ( cls: Any,*A_: List[str],**A_: Union[str, Any] ): '''simple docstring''' requires_backends(cls,['flax', 'transformers'] ) class __lowerCamelCase (metaclass=_a ): _lowercase = ["""flax""", """transformers"""] def __init__( self: Tuple,*A_: Dict,**A_: List[Any] ): '''simple docstring''' requires_backends(self,['flax', 'transformers'] ) @classmethod def snake_case_ ( cls: Union[str, Any],*A_: List[Any],**A_: Any ): '''simple docstring''' requires_backends(cls,['flax', 'transformers'] ) @classmethod def snake_case_ ( cls: Dict,*A_: Dict,**A_: Optional[Any] ): '''simple docstring''' requires_backends(cls,['flax', 'transformers'] ) class __lowerCamelCase (metaclass=_a ): _lowercase = ["""flax""", """transformers"""] def __init__( self: Dict,*A_: int,**A_: int ): '''simple docstring''' requires_backends(self,['flax', 'transformers'] ) @classmethod def snake_case_ ( cls: List[Any],*A_: int,**A_: int ): '''simple docstring''' requires_backends(cls,['flax', 'transformers'] ) @classmethod def snake_case_ ( cls: List[str],*A_: Union[str, Any],**A_: List[str] ): '''simple docstring''' requires_backends(cls,['flax', 'transformers'] )
1
import numpy as np import torch import tqdm from ...models.unet_ad import UNetaDModel from ...pipelines import DiffusionPipeline from ...utils import randn_tensor from ...utils.dummy_pt_objects import DDPMScheduler class lowerCAmelCase_ ( lowercase ): """simple docstring""" def __init__( self :int , lowerCamelCase__ :UNetaDModel , lowerCamelCase__ :UNetaDModel , lowerCamelCase__ :DDPMScheduler , lowerCamelCase__ :List[Any] , ): super().__init__() UpperCamelCase__ :Tuple = value_function UpperCamelCase__ :Optional[int] = unet UpperCamelCase__ :List[str] = scheduler UpperCamelCase__ :Dict = env UpperCamelCase__ :Dict = env.get_dataset() UpperCamelCase__ :Union[str, Any] = {} for key in self.data.keys(): try: UpperCamelCase__ :int = self.data[key].mean() except: # noqa: E722 pass UpperCamelCase__ :Any = {} for key in self.data.keys(): try: UpperCamelCase__ :int = self.data[key].std() except: # noqa: E722 pass UpperCamelCase__ :List[Any] = env.observation_space.shape[0] UpperCamelCase__ :List[str] = env.action_space.shape[0] def __a ( self :Union[str, Any] , lowerCamelCase__ :List[str] , lowerCamelCase__ :str ): return (x_in - self.means[key]) / self.stds[key] def __a ( self :int , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Tuple ): return x_in * self.stds[key] + self.means[key] def __a ( self :Any , lowerCamelCase__ :int ): if type(lowerCamelCase__ ) is dict: return {k: self.to_torch(lowerCamelCase__ ) for k, v in x_in.items()} elif torch.is_tensor(lowerCamelCase__ ): return x_in.to(self.unet.device ) return torch.tensor(lowerCamelCase__ , device=self.unet.device ) def __a ( self :Union[str, Any] , lowerCamelCase__ :List[Any] , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Tuple ): for key, val in cond.items(): UpperCamelCase__ :str = val.clone() return x_in def __a ( self :Union[str, Any] , lowerCamelCase__ :List[Any] , lowerCamelCase__ :int , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Optional[int] ): UpperCamelCase__ :Any = x.shape[0] UpperCamelCase__ :List[Any] = None for i in tqdm.tqdm(self.scheduler.timesteps ): # create batch of timesteps to pass into model UpperCamelCase__ :Optional[Any] = torch.full((batch_size,) , lowerCamelCase__ , device=self.unet.device , dtype=torch.long ) for _ in range(lowerCamelCase__ ): with torch.enable_grad(): x.requires_grad_() # permute to match dimension for pre-trained models UpperCamelCase__ :Dict = self.value_function(x.permute(0 , 2 , 1 ) , lowerCamelCase__ ).sample UpperCamelCase__ :List[Any] = torch.autograd.grad([y.sum()] , [x] )[0] UpperCamelCase__ :Union[str, Any] = self.scheduler._get_variance(lowerCamelCase__ ) UpperCamelCase__ :Any = torch.exp(0.5 * posterior_variance ) UpperCamelCase__ :Dict = model_std * grad UpperCamelCase__ :Optional[Any] = 0 UpperCamelCase__ :Dict = x.detach() UpperCamelCase__ :int = x + scale * grad UpperCamelCase__ :int = self.reset_xa(lowerCamelCase__ , lowerCamelCase__ , self.action_dim ) UpperCamelCase__ :List[str] = self.unet(x.permute(0 , 2 , 1 ) , lowerCamelCase__ ).sample.permute(0 , 2 , 1 ) # TODO: verify deprecation of this kwarg UpperCamelCase__ :List[str] = self.scheduler.step(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , predict_epsilon=lowerCamelCase__ )["""prev_sample"""] # apply conditions to the trajectory (set the initial state) UpperCamelCase__ :Optional[Any] = self.reset_xa(lowerCamelCase__ , lowerCamelCase__ , self.action_dim ) UpperCamelCase__ :Optional[int] = self.to_torch(lowerCamelCase__ ) return x, y def __call__( self :Optional[Any] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :str=64 , lowerCamelCase__ :Tuple=32 , lowerCamelCase__ :Dict=2 , lowerCamelCase__ :str=0.1 ): # normalize the observations and create batch dimension UpperCamelCase__ :List[str] = self.normalize(lowerCamelCase__ , """observations""" ) UpperCamelCase__ :List[str] = obs[None].repeat(lowerCamelCase__ , axis=0 ) UpperCamelCase__ :int = {0: self.to_torch(lowerCamelCase__ )} UpperCamelCase__ :Dict = (batch_size, planning_horizon, self.state_dim + self.action_dim) # generate initial noise and apply our conditions (to make the trajectories start at current state) UpperCamelCase__ :Any = randn_tensor(lowerCamelCase__ , device=self.unet.device ) UpperCamelCase__ :Optional[int] = self.reset_xa(lowerCamelCase__ , lowerCamelCase__ , self.action_dim ) UpperCamelCase__ :List[Any] = self.to_torch(lowerCamelCase__ ) # run the diffusion process UpperCamelCase__ , UpperCamelCase__ :Union[str, Any] = self.run_diffusion(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) # sort output trajectories by value UpperCamelCase__ :List[Any] = y.argsort(0 , descending=lowerCamelCase__ ).squeeze() UpperCamelCase__ :Dict = x[sorted_idx] UpperCamelCase__ :Tuple = sorted_values[:, :, : self.action_dim] UpperCamelCase__ :Optional[Any] = actions.detach().cpu().numpy() UpperCamelCase__ :Optional[int] = self.de_normalize(lowerCamelCase__ , key="""actions""" ) # select the action with the highest value if y is not None: UpperCamelCase__ :List[str] = 0 else: # if we didn't run value guiding, select a random action UpperCamelCase__ :Dict = np.random.randint(0 , lowerCamelCase__ ) UpperCamelCase__ :Tuple = denorm_actions[selected_index, 0] return denorm_actions
45
0
import unittest from transformers import JukeboxTokenizer from transformers.testing_utils import require_torch class lowerCamelCase__ ( unittest.TestCase): """simple docstring""" a__ : Tuple = JukeboxTokenizer a__ : Union[str, Any] = { "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 : str ) -> Union[str, Any]: import torch _A = JukeboxTokenizer.from_pretrained('''openai/jukebox-1b-lyrics''' ) _A = tokenizer(**self.metas )['''input_ids'''] # fmt: off _A = [ torch.tensor([[ 0, 0, 0, 71_69, 5_07, 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, 10_69, 11]] ), torch.tensor([[0, 0, 0, 10_69, 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 : Tuple ) -> Tuple: import torch _A = JukeboxTokenizer.from_pretrained('''openai/jukebox-5b-lyrics''' ) _A = tokenizer(**self.metas )['''input_ids'''] # fmt: off _A = [ torch.tensor([[ 0, 0, 0, 10_69, 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, 10_69, 11, -1, -1, -1, -1]] ), torch.tensor([[0, 0, 0, 10_69, 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] ) )
2
def A ( lowercase__ : int ) -> bool: if num < 0: return False UpperCamelCase__ :int = num UpperCamelCase__ :int = 0 while num > 0: UpperCamelCase__ :Optional[int] = rev_num * 10 + (num % 10) num //= 10 return num_copy == rev_num if __name__ == "__main__": import doctest doctest.testmod()
45
0
'''simple docstring''' from google.protobuf import descriptor as _descriptor from google.protobuf import descriptor_pool as _descriptor_pool from google.protobuf import symbol_database as _symbol_database from google.protobuf.internal import builder as _builder # @@protoc_insertion_point(imports) lowerCAmelCase : Dict = _symbol_database.Default() lowerCAmelCase : Union[str, Any] = _descriptor_pool.Default().AddSerializedFile( B'\n\x19sentencepiece_model.proto\x12\rsentencepiece"\x80\x0c\n\x0bTrainerSpec\x12\r\n\x05input\x18\x01 \x03(\t\x12\x14\n\x0cinput_format\x18\x07 \x01(\t\x12\x14\n\x0cmodel_prefix\x18\x02 \x01(\t\x12\x41\n\nmodel_type\x18\x03 \x01(\x0e\x32$.sentencepiece.TrainerSpec.ModelType:\x07UNIGRAM\x12\x18\n\nvocab_size\x18\x04 \x01(\x05:\x04\x38\x30\x30\x30\x12\x17\n\x0f\x61\x63\x63\x65pt_language\x18\x05 \x03(\t\x12 \n\x15self_test_sample_size\x18\x06 \x01(\x05:\x01\x30\x12*\n\x1b\x65nable_differential_privacy\x18\x32 \x01(\x08:\x05\x66\x61lse\x12+\n differential_privacy_noise_level\x18\x33 \x01(\x02:\x01\x30\x12\x32\n\'differential_privacy_clipping_threshold\x18\x34 \x01(\x04:\x01\x30\x12"\n\x12\x63haracter_coverage\x18\n \x01(\x02:\x06\x30.9995\x12\x1e\n\x13input_sentence_size\x18\x0b \x01(\x04:\x01\x30\x12$\n\x16shuffle_input_sentence\x18\x13 \x01(\x08:\x04true\x12 \n\x14mining_sentence_size\x18\x0c \x01(\x05\x42\x02\x18\x01\x12"\n\x16training_sentence_size\x18\r \x01(\x05\x42\x02\x18\x01\x12(\n\x17seed_sentencepiece_size\x18\x0e \x01(\x05:\x07\x31\x30\x30\x30\x30\x30\x30\x12\x1e\n\x10shrinking_factor\x18\x0f \x01(\x02:\x04\x30.75\x12!\n\x13max_sentence_length\x18\x12 \x01(\x05:\x04\x34\x31\x39\x32\x12\x17\n\x0bnum_threads\x18\x10 \x01(\x05:\x02\x31\x36\x12\x1d\n\x12num_sub_iterations\x18\x11 \x01(\x05:\x01\x32\x12$\n\x18max_sentencepiece_length\x18\x14 \x01(\x05:\x02\x31\x36\x12%\n\x17split_by_unicode_script\x18\x15 \x01(\x08:\x04true\x12\x1d\n\x0fsplit_by_number\x18\x17 \x01(\x08:\x04true\x12!\n\x13split_by_whitespace\x18\x16 \x01(\x08:\x04true\x12)\n\x1atreat_whitespace_as_suffix\x18\x18 \x01(\x08:\x05\x66\x61lse\x12+\n\x1c\x61llow_whitespace_only_pieces\x18\x1a \x01(\x08:\x05\x66\x61lse\x12\x1b\n\x0csplit_digits\x18\x19 \x01(\x08:\x05\x66\x61lse\x12#\n\x19pretokenization_delimiter\x18\x35 \x01(\t:\x00\x12\x17\n\x0f\x63ontrol_symbols\x18\x1e \x03(\t\x12\x1c\n\x14user_defined_symbols\x18\x1f \x03(\t\x12\x16\n\x0erequired_chars\x18$ \x01(\t\x12\x1c\n\rbyte_fallback\x18# \x01(\x08:\x05\x66\x61lse\x12+\n\x1dvocabulary_output_piece_score\x18 \x01(\x08:\x04true\x12\x1e\n\x10hard_vocab_limit\x18! \x01(\x08:\x04true\x12\x1c\n\ruse_all_vocab\x18" \x01(\x08:\x05\x66\x61lse\x12\x11\n\x06unk_id\x18( \x01(\x05:\x01\x30\x12\x11\n\x06\x62os_id\x18) \x01(\x05:\x01\x31\x12\x11\n\x06\x65os_id\x18* \x01(\x05:\x01\x32\x12\x12\n\x06pad_id\x18+ \x01(\x05:\x02-1\x12\x18\n\tunk_piece\x18- \x01(\t:\x05<unk>\x12\x16\n\tbos_piece\x18. \x01(\t:\x03<s>\x12\x17\n\teos_piece\x18/ \x01(\t:\x04</s>\x12\x18\n\tpad_piece\x18\x30 \x01(\t:\x05<pad>\x12\x1a\n\x0bunk_surface\x18, \x01(\t:\x05 \xe2\x81\x87 \x12+\n\x1ctrain_extremely_large_corpus\x18\x31 \x01(\x08:\x05\x66\x61lse"5\n\tModelType\x12\x0b\n\x07UNIGRAM\x10\x01\x12\x07\n\x03\x42PE\x10\x02\x12\x08\n\x04WORD\x10\x03\x12\x08\n\x04\x43HAR\x10\x04*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02"\xd1\x01\n\x0eNormalizerSpec\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x1c\n\x14precompiled_charsmap\x18\x02 \x01(\x0c\x12\x1e\n\x10\x61\x64\x64_dummy_prefix\x18\x03 \x01(\x08:\x04true\x12&\n\x18remove_extra_whitespaces\x18\x04 \x01(\x08:\x04true\x12 \n\x12\x65scape_whitespaces\x18\x05 \x01(\x08:\x04true\x12\x1e\n\x16normalization_rule_tsv\x18\x06 \x01(\t*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02"y\n\x0cSelfTestData\x12\x33\n\x07samples\x18\x01 \x03(\x0b\x32".sentencepiece.SelfTestData.Sample\x1a)\n\x06Sample\x12\r\n\x05input\x18\x01 \x01(\t\x12\x10\n\x08\x65xpected\x18\x02 \x01(\t*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02"\xfe\x03\n\nModelProto\x12\x37\n\x06pieces\x18\x01 \x03(\x0b\x32\'.sentencepiece.ModelProto.SentencePiece\x12\x30\n\x0ctrainer_spec\x18\x02 \x01(\x0b\x32\x1a.sentencepiece.TrainerSpec\x12\x36\n\x0fnormalizer_spec\x18\x03 \x01(\x0b\x32\x1d.sentencepiece.NormalizerSpec\x12\x33\n\x0eself_test_data\x18\x04 \x01(\x0b\x32\x1b.sentencepiece.SelfTestData\x12\x38\n\x11\x64\x65normalizer_spec\x18\x05 \x01(\x0b\x32\x1d.sentencepiece.NormalizerSpec\x1a\xd2\x01\n\rSentencePiece\x12\r\n\x05piece\x18\x01 \x01(\t\x12\r\n\x05score\x18\x02 \x01(\x02\x12\x42\n\x04type\x18\x03 \x01(\x0e\x32,.sentencepiece.ModelProto.SentencePiece.Type:\x06NORMAL"T\n\x04Type\x12\n\n\x06NORMAL\x10\x01\x12\x0b\n\x07UNKNOWN\x10\x02\x12\x0b\n\x07\x43ONTROL\x10\x03\x12\x10\n\x0cUSER_DEFINED\x10\x04\x12\x08\n\x04\x42YTE\x10\x06\x12\n\n\x06UNUSED\x10\x05*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02\x42\x02H\x03' ) lowerCAmelCase : Optional[Any] = globals() _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'sentencepiece_model_pb2', _globals) if _descriptor._USE_C_DESCRIPTORS is False: lowerCAmelCase : Optional[int] = None lowerCAmelCase : str = B'H\003' # (generated by protobuf compiler, but `_TRAINERSPEC` is not defined) # _TRAINERSPEC.fields_by_name["mining_sentence_size"]._options = None # _TRAINERSPEC.fields_by_name["mining_sentence_size"]._serialized_options = b"\030\001" # _TRAINERSPEC.fields_by_name["training_sentence_size"]._options = None # _TRAINERSPEC.fields_by_name["training_sentence_size"]._serialized_options = b"\030\001" lowerCAmelCase : Optional[int] = 45 lowerCAmelCase : Tuple = 15_81 lowerCAmelCase : Tuple = 15_17 lowerCAmelCase : Tuple = 15_70 lowerCAmelCase : Union[str, Any] = 15_84 lowerCAmelCase : Optional[int] = 17_93 lowerCAmelCase : int = 17_95 lowerCAmelCase : Dict = 19_16 lowerCAmelCase : List[Any] = 18_64 lowerCAmelCase : Any = 19_05 lowerCAmelCase : Any = 19_19 lowerCAmelCase : str = 24_29 lowerCAmelCase : str = 22_08 lowerCAmelCase : Any = 24_18 lowerCAmelCase : Dict = 23_23 lowerCAmelCase : Optional[int] = 24_07 # @@protoc_insertion_point(module_scope)
3
from __future__ import annotations def A ( lowercase__ : list[int] ) -> bool: return len(set(lowercase__ ) ) == len(lowercase__ ) if __name__ == "__main__": import doctest doctest.testmod()
45
0
"""simple docstring""" import importlib import sys from argparse import REMAINDER, ArgumentParser from pathlib import Path import torch_xla.distributed.xla_multiprocessing as xmp def _SCREAMING_SNAKE_CASE (): lowerCAmelCase = ArgumentParser( description=( 'PyTorch TPU distributed training launch ' 'helper utility that will spawn up ' 'multiple distributed processes' ) ) # Optional arguments for the launch helper parser.add_argument('--num_cores' , type=_UpperCAmelCase , default=1 , help='Number of TPU cores to use (1 or 8).' ) # positional parser.add_argument( 'training_script' , type=_UpperCAmelCase , help=( 'The full path to the single TPU training ' 'program/script to be launched in parallel, ' 'followed by all the arguments for the ' 'training script' ) , ) # rest from the training program parser.add_argument('training_script_args' , nargs=_UpperCAmelCase ) return parser.parse_args() def _SCREAMING_SNAKE_CASE (): lowerCAmelCase = parse_args() # Import training_script as a module. lowerCAmelCase = Path(args.training_script ) sys.path.append(str(script_fpath.parent.resolve() ) ) lowerCAmelCase = script_fpath.stem lowerCAmelCase = importlib.import_module(_UpperCAmelCase ) # Patch sys.argv lowerCAmelCase = [args.training_script] + args.training_script_args + ['--tpu_num_cores', str(args.num_cores )] xmp.spawn(mod._mp_fn , args=() , nprocs=args.num_cores ) if __name__ == "__main__": main()
4
from __future__ import annotations class lowerCAmelCase_ : """simple docstring""" def __init__( self :List[Any] , lowerCamelCase__ :int = 0 ): UpperCamelCase__ :List[str] = key def __a ( self :Optional[Any] , lowerCamelCase__ :str , lowerCamelCase__ :int ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__ :List[str] = key or self.__key or 1 # make sure key is an appropriate size key %= 2_55 return [chr(ord(lowerCamelCase__ ) ^ key ) for ch in content] def __a ( self :int , lowerCamelCase__ :str , lowerCamelCase__ :int ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__ :int = key or self.__key or 1 # make sure key is an appropriate size key %= 2_55 return [chr(ord(lowerCamelCase__ ) ^ key ) for ch in content] def __a ( self :Optional[Any] , lowerCamelCase__ :str , lowerCamelCase__ :int = 0 ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__ :Dict = key or self.__key or 1 # make sure key can be any size while key > 2_55: key -= 2_55 # This will be returned UpperCamelCase__ :List[str] = """""" for ch in content: ans += chr(ord(lowerCamelCase__ ) ^ key ) return ans def __a ( self :Any , lowerCamelCase__ :str , lowerCamelCase__ :int = 0 ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__ :Tuple = key or self.__key or 1 # make sure key can be any size while key > 2_55: key -= 2_55 # This will be returned UpperCamelCase__ :Optional[int] = """""" for ch in content: ans += chr(ord(lowerCamelCase__ ) ^ key ) return ans def __a ( self :Optional[Any] , lowerCamelCase__ :str , lowerCamelCase__ :int = 0 ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) try: with open(lowerCamelCase__ ) as fin, open("""encrypt.out""" , """w+""" ) as fout: # actual encrypt-process for line in fin: fout.write(self.encrypt_string(lowerCamelCase__ , lowerCamelCase__ ) ) except OSError: return False return True def __a ( self :Dict , lowerCamelCase__ :str , lowerCamelCase__ :int ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) try: with open(lowerCamelCase__ ) as fin, open("""decrypt.out""" , """w+""" ) as fout: # actual encrypt-process for line in fin: fout.write(self.decrypt_string(lowerCamelCase__ , lowerCamelCase__ ) ) except OSError: return False return True # Tests # crypt = XORCipher() # key = 67 # # test encrypt # print(crypt.encrypt("hallo welt",key)) # # test decrypt # print(crypt.decrypt(crypt.encrypt("hallo welt",key), key)) # # test encrypt_string # print(crypt.encrypt_string("hallo welt",key)) # # test decrypt_string # print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key)) # if (crypt.encrypt_file("test.txt",key)): # print("encrypt successful") # else: # print("encrypt unsuccessful") # if (crypt.decrypt_file("encrypt.out",key)): # print("decrypt successful") # else: # print("decrypt unsuccessful")
45
0
'''simple docstring''' from __future__ import annotations from math import gcd def A (__lowerCamelCase :int , __lowerCamelCase :int = 2 , __lowerCamelCase :int = 1 , __lowerCamelCase :int = 3 , ): # A value less than 2 can cause an infinite loop in the algorithm. if num < 2: raise ValueError("""The input value cannot be less than 2""" ) # Because of the relationship between ``f(f(x))`` and ``f(x)``, this # algorithm struggles to find factors that are divisible by two. # As a workaround, we specifically check for two and even inputs. # See: https://math.stackexchange.com/a/2856214/165820 if num > 2 and num % 2 == 0: return 2 # Pollard's Rho algorithm requires a function that returns pseudorandom # values between 0 <= X < ``num``. It doesn't need to be random in the # sense that the output value is cryptographically secure or difficult # to calculate, it only needs to be random in the sense that all output # values should be equally likely to appear. # For this reason, Pollard suggested using ``f(x) = (x**2 - 1) % num`` # However, the success of Pollard's algorithm isn't guaranteed and is # determined in part by the initial seed and the chosen random function. # To make retries easier, we will instead use ``f(x) = (x**2 + C) % num`` # where ``C`` is a value that we can modify between each attempt. def rand_fn(__lowerCamelCase :int , __lowerCamelCase :int , __lowerCamelCase :int ) -> int: return (pow(__lowerCamelCase , 2 ) + step) % modulus for _ in range(__lowerCamelCase ): # These track the position within the cycle detection logic. _lowerCAmelCase = seed _lowerCAmelCase = seed while True: # At each iteration, the tortoise moves one step and the hare moves two. _lowerCAmelCase = rand_fn(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) _lowerCAmelCase = rand_fn(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) _lowerCAmelCase = rand_fn(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) # At some point both the tortoise and the hare will enter a cycle whose # length ``p`` is a divisor of ``num``. Once in that cycle, at some point # the tortoise and hare will end up on the same value modulo ``p``. # We can detect when this happens because the position difference between # the tortoise and the hare will share a common divisor with ``num``. _lowerCAmelCase = gcd(hare - tortoise , __lowerCamelCase ) if divisor == 1: # No common divisor yet, just keep searching. continue else: # We found a common divisor! if divisor == num: # Unfortunately, the divisor is ``num`` itself and is useless. break else: # The divisor is a nontrivial factor of ``num``! return divisor # If we made it here, then this attempt failed. # We need to pick a new starting seed for the tortoise and hare # in addition to a new step value for the random function. # To keep this example implementation deterministic, the # new values will be generated based on currently available # values instead of using something like ``random.randint``. # We can use the hare's position as the new seed. # This is actually what Richard Brent's the "optimized" variant does. _lowerCAmelCase = hare # The new step value for the random function can just be incremented. # At first the results will be similar to what the old function would # have produced, but the value will quickly diverge after a bit. step += 1 # We haven't found a divisor within the requested number of attempts. # We were unlucky or ``num`` itself is actually prime. return None if __name__ == "__main__": import argparse _lowercase = argparse.ArgumentParser() parser.add_argument( """num""", type=int, help="""The value to find a divisor of""", ) parser.add_argument( """--attempts""", type=int, default=3, help="""The number of attempts before giving up""", ) _lowercase = parser.parse_args() _lowercase = pollard_rho(args.num, attempts=args.attempts) if divisor is None: print(F"""{args.num} is probably prime""") else: _lowercase = args.num // divisor print(F"""{args.num} = {divisor} * {quotient}""")
5
import random def A ( lowercase__ : Dict , lowercase__ : str , lowercase__ : Optional[Any] ) -> int: UpperCamelCase__ :List[Any] = a[left_index] UpperCamelCase__ :Dict = left_index + 1 for j in range(left_index + 1 , lowercase__ ): if a[j] < pivot: UpperCamelCase__ , UpperCamelCase__ :Optional[int] = a[i], a[j] i += 1 UpperCamelCase__ , UpperCamelCase__ :Tuple = a[i - 1], a[left_index] return i - 1 def A ( lowercase__ : Tuple , lowercase__ : Optional[int] , lowercase__ : Any ) -> Optional[int]: if left < right: UpperCamelCase__ :List[Any] = random.randint(lowercase__ , right - 1 ) UpperCamelCase__ , UpperCamelCase__ :Union[str, Any] = ( a[left], a[pivot], ) # switches the pivot with the left most bound UpperCamelCase__ :int = partition(lowercase__ , lowercase__ , lowercase__ ) quick_sort_random( lowercase__ , lowercase__ , lowercase__ ) # recursive quicksort to the left of the pivot point quick_sort_random( lowercase__ , pivot_index + 1 , lowercase__ ) # recursive quicksort to the right of the pivot point def A ( ) -> List[Any]: UpperCamelCase__ :str = input("""Enter numbers separated by a comma:\n""" ).strip() UpperCamelCase__ :int = [int(lowercase__ ) for item in user_input.split(""",""" )] quick_sort_random(lowercase__ , 0 , len(lowercase__ ) ) print(lowercase__ ) if __name__ == "__main__": main()
45
0
import argparse import torch from transformers import ( WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaForAudioFrameClassification, WavaVecaForSequenceClassification, WavaVecaForXVector, logging, ) logging.set_verbosity_info() _lowerCamelCase = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE__ ( UpperCamelCase__: Dict , UpperCamelCase__: Union[str, Any] , UpperCamelCase__: Dict ): SCREAMING_SNAKE_CASE__ = WavaVecaForSequenceClassification.from_pretrained(UpperCamelCase__ , config=UpperCamelCase__ ) SCREAMING_SNAKE_CASE__ = downstream_dict["""projector.weight"""] SCREAMING_SNAKE_CASE__ = downstream_dict["""projector.bias"""] SCREAMING_SNAKE_CASE__ = downstream_dict["""model.post_net.linear.weight"""] SCREAMING_SNAKE_CASE__ = downstream_dict["""model.post_net.linear.bias"""] return model def SCREAMING_SNAKE_CASE__ ( UpperCamelCase__: Dict , UpperCamelCase__: Dict , UpperCamelCase__: Any ): SCREAMING_SNAKE_CASE__ = WavaVecaForAudioFrameClassification.from_pretrained(UpperCamelCase__ , config=UpperCamelCase__ ) SCREAMING_SNAKE_CASE__ = downstream_dict["""model.linear.weight"""] SCREAMING_SNAKE_CASE__ = downstream_dict["""model.linear.bias"""] return model def SCREAMING_SNAKE_CASE__ ( UpperCamelCase__: Any , UpperCamelCase__: List[str] , UpperCamelCase__: Any ): SCREAMING_SNAKE_CASE__ = WavaVecaForXVector.from_pretrained(UpperCamelCase__ , config=UpperCamelCase__ ) SCREAMING_SNAKE_CASE__ = downstream_dict["""connector.weight"""] SCREAMING_SNAKE_CASE__ = downstream_dict["""connector.bias"""] for i, kernel_size in enumerate(hf_config.tdnn_kernel ): SCREAMING_SNAKE_CASE__ = downstream_dict[ f'''model.framelevel_feature_extractor.module.{i}.kernel.weight''' ] SCREAMING_SNAKE_CASE__ = downstream_dict[f'''model.framelevel_feature_extractor.module.{i}.kernel.bias'''] SCREAMING_SNAKE_CASE__ = downstream_dict["""model.utterancelevel_feature_extractor.linear1.weight"""] SCREAMING_SNAKE_CASE__ = downstream_dict["""model.utterancelevel_feature_extractor.linear1.bias"""] SCREAMING_SNAKE_CASE__ = downstream_dict["""model.utterancelevel_feature_extractor.linear2.weight"""] SCREAMING_SNAKE_CASE__ = downstream_dict["""model.utterancelevel_feature_extractor.linear2.bias"""] SCREAMING_SNAKE_CASE__ = downstream_dict["""objective.W"""] return model @torch.no_grad() def SCREAMING_SNAKE_CASE__ ( UpperCamelCase__: Any , UpperCamelCase__: Tuple , UpperCamelCase__: Optional[Any] , UpperCamelCase__: Union[str, Any] ): SCREAMING_SNAKE_CASE__ = torch.load(UpperCamelCase__ , map_location="""cpu""" ) SCREAMING_SNAKE_CASE__ = checkpoint["""Downstream"""] SCREAMING_SNAKE_CASE__ = WavaVecaConfig.from_pretrained(UpperCamelCase__ ) SCREAMING_SNAKE_CASE__ = WavaVecaFeatureExtractor.from_pretrained( UpperCamelCase__ , return_attention_mask=UpperCamelCase__ , do_normalize=UpperCamelCase__ ) SCREAMING_SNAKE_CASE__ = hf_config.architectures[0] if arch.endswith("""ForSequenceClassification""" ): SCREAMING_SNAKE_CASE__ = convert_classification(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) elif arch.endswith("""ForAudioFrameClassification""" ): SCREAMING_SNAKE_CASE__ = convert_diarization(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) elif arch.endswith("""ForXVector""" ): SCREAMING_SNAKE_CASE__ = convert_xvector(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) else: raise NotImplementedError(f'''S3PRL weights conversion is not supported for {arch}''' ) if hf_config.use_weighted_layer_sum: SCREAMING_SNAKE_CASE__ = checkpoint["""Featurizer"""]["""weights"""] hf_feature_extractor.save_pretrained(UpperCamelCase__ ) hf_model.save_pretrained(UpperCamelCase__ ) if __name__ == "__main__": _lowerCamelCase = argparse.ArgumentParser() parser.add_argument( '--base_model_name', default=None, type=str, help='Name of the huggingface pretrained base model.' ) parser.add_argument('--config_path', default=None, type=str, help='Path to the huggingface classifier config.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to the s3prl checkpoint.') parser.add_argument('--model_dump_path', default=None, type=str, help='Path to the final converted model.') _lowerCamelCase = parser.parse_args() convert_saprl_checkpoint(args.base_model_name, args.config_path, args.checkpoint_path, args.model_dump_path)
6
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { "shi-labs/dinat-mini-in1k-224": "https://huggingface.co/shi-labs/dinat-mini-in1k-224/resolve/main/config.json", # See all Dinat models at https://huggingface.co/models?filter=dinat } class lowerCAmelCase_ ( lowercase , lowercase ): """simple docstring""" _snake_case : Tuple = """dinat""" _snake_case : List[Any] = { """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers""", } def __init__( self :Optional[int] , lowerCamelCase__ :int=4 , lowerCamelCase__ :Union[str, Any]=3 , lowerCamelCase__ :List[Any]=64 , lowerCamelCase__ :Any=[3, 4, 6, 5] , lowerCamelCase__ :Tuple=[2, 4, 8, 16] , lowerCamelCase__ :Optional[int]=7 , lowerCamelCase__ :Tuple=[[1, 8, 1], [1, 4, 1, 4], [1, 2, 1, 2, 1, 2], [1, 1, 1, 1, 1]] , lowerCamelCase__ :Tuple=3.0 , lowerCamelCase__ :str=True , lowerCamelCase__ :Optional[int]=0.0 , lowerCamelCase__ :Optional[Any]=0.0 , lowerCamelCase__ :int=0.1 , lowerCamelCase__ :Optional[Any]="gelu" , lowerCamelCase__ :Optional[Any]=0.02 , lowerCamelCase__ :Union[str, Any]=1e-5 , lowerCamelCase__ :Optional[int]=0.0 , lowerCamelCase__ :List[str]=None , lowerCamelCase__ :str=None , **lowerCamelCase__ :List[Any] , ): super().__init__(**lowerCamelCase__ ) UpperCamelCase__ :Any = patch_size UpperCamelCase__ :Any = num_channels UpperCamelCase__ :int = embed_dim UpperCamelCase__ :Optional[Any] = depths UpperCamelCase__ :Any = len(lowerCamelCase__ ) UpperCamelCase__ :str = num_heads UpperCamelCase__ :Optional[int] = kernel_size UpperCamelCase__ :Optional[int] = dilations UpperCamelCase__ :Tuple = mlp_ratio UpperCamelCase__ :Dict = qkv_bias UpperCamelCase__ :List[str] = hidden_dropout_prob UpperCamelCase__ :List[str] = attention_probs_dropout_prob UpperCamelCase__ :Union[str, Any] = drop_path_rate UpperCamelCase__ :Tuple = hidden_act UpperCamelCase__ :List[Any] = layer_norm_eps UpperCamelCase__ :Optional[Any] = initializer_range # we set the hidden_size attribute in order to make Dinat work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model UpperCamelCase__ :Tuple = int(embed_dim * 2 ** (len(lowerCamelCase__ ) - 1) ) UpperCamelCase__ :Tuple = layer_scale_init_value UpperCamelCase__ :Optional[int] = ["""stem"""] + [f"""stage{idx}""" for idx in range(1 , len(lowerCamelCase__ ) + 1 )] UpperCamelCase__ , UpperCamelCase__ :List[str] = get_aligned_output_features_output_indices( out_features=lowerCamelCase__ , out_indices=lowerCamelCase__ , stage_names=self.stage_names )
45
0
"""simple docstring""" import argparse import json import os import time import zipfile from get_ci_error_statistics import download_artifact, get_artifacts_links from transformers import logging a = logging.get_logger(__name__) def _snake_case ( _snake_case : List[str] , _snake_case : Any ) -> Dict: '''simple docstring''' _A = set() _A = [] def parse_line(_snake_case : Optional[Any] ): for line in fp: if isinstance(_snake_case , _snake_case ): _A = line.decode('UTF-8' ) if "warnings summary (final)" in line: continue # This means we are outside the body of a warning elif not line.startswith(' ' ): # process a single warning and move it to `selected_warnings`. if len(_snake_case ) > 0: _A = '\n'.join(_snake_case ) # Only keep the warnings specified in `targets` if any(F''': {x}: ''' in warning for x in targets ): selected_warnings.add(_snake_case ) buffer.clear() continue else: _A = line.strip() buffer.append(_snake_case ) if from_gh: for filename in os.listdir(_snake_case ): _A = os.path.join(_snake_case , _snake_case ) if not os.path.isdir(_snake_case ): # read the file if filename != "warnings.txt": continue with open(_snake_case ) as fp: parse_line(_snake_case ) else: try: with zipfile.ZipFile(_snake_case ) as z: for filename in z.namelist(): if not os.path.isdir(_snake_case ): # read the file if filename != "warnings.txt": continue with z.open(_snake_case ) as fp: parse_line(_snake_case ) except Exception: logger.warning( F'''{artifact_path} is either an invalid zip file or something else wrong. This file is skipped.''' ) return selected_warnings def _snake_case ( _snake_case : Union[str, Any] , _snake_case : List[str] ) -> Dict: '''simple docstring''' _A = set() _A = [os.path.join(_snake_case , _snake_case ) for p in os.listdir(_snake_case ) if (p.endswith('.zip' ) or from_gh)] for p in paths: selected_warnings.update(extract_warnings_from_single_artifact(_snake_case , _snake_case ) ) return selected_warnings if __name__ == "__main__": def _snake_case ( _snake_case : int ) -> Dict: '''simple docstring''' return values.split(',' ) a = argparse.ArgumentParser() # Required parameters parser.add_argument('''--workflow_run_id''', type=str, required=True, help='''A GitHub Actions workflow run id.''') parser.add_argument( '''--output_dir''', type=str, required=True, help='''Where to store the downloaded artifacts and other result files.''', ) parser.add_argument('''--token''', default=None, type=str, help='''A token that has actions:read permission.''') # optional parameters parser.add_argument( '''--targets''', default='''DeprecationWarning,UserWarning,FutureWarning''', type=list_str, help='''Comma-separated list of target warning(s) which we want to extract.''', ) parser.add_argument( '''--from_gh''', action='''store_true''', help='''If running from a GitHub action workflow and collecting warnings from its artifacts.''', ) a = parser.parse_args() a = args.from_gh if from_gh: # The artifacts have to be downloaded using `actions/download-artifact@v3` pass else: os.makedirs(args.output_dir, exist_ok=True) # get download links a = get_artifacts_links(args.workflow_run_id, token=args.token) with open(os.path.join(args.output_dir, '''artifacts.json'''), '''w''', encoding='''UTF-8''') as fp: json.dump(artifacts, fp, ensure_ascii=False, indent=4) # download artifacts for idx, (name, url) in enumerate(artifacts.items()): print(name) print(url) print('''=''' * 80) download_artifact(name, url, args.output_dir, args.token) # Be gentle to GitHub time.sleep(1) # extract warnings from artifacts a = extract_warnings(args.output_dir, args.targets) a = sorted(selected_warnings) with open(os.path.join(args.output_dir, '''selected_warnings.json'''), '''w''', encoding='''UTF-8''') as fp: json.dump(selected_warnings, fp, ensure_ascii=False, indent=4)
7
def A ( lowercase__ : int , lowercase__ : int ) -> int: return int(input_a == input_a == 0 ) def A ( ) -> None: print("""Truth Table of NOR Gate:""" ) print("""| Input 1 | Input 2 | Output |""" ) print(f"""| 0 | 0 | {nor_gate(0 , 0 )} |""" ) print(f"""| 0 | 1 | {nor_gate(0 , 1 )} |""" ) print(f"""| 1 | 0 | {nor_gate(1 , 0 )} |""" ) print(f"""| 1 | 1 | {nor_gate(1 , 1 )} |""" ) if __name__ == "__main__": import doctest doctest.testmod() main()
45
0
'''simple docstring''' import math class SCREAMING_SNAKE_CASE : def __init__( self , _UpperCAmelCase=0): # a graph with Node 0,1,...,N-1 '''simple docstring''' __A : List[str] = n __A : List[str] = [ [math.inf for j in range(0 , _UpperCAmelCase)] for i in range(0 , _UpperCAmelCase) ] # adjacency matrix for weight __A : List[str] = [ [math.inf for j in range(0 , _UpperCAmelCase)] for i in range(0 , _UpperCAmelCase) ] # dp[i][j] stores minimum distance from i to j def SCREAMING_SNAKE_CASE ( self , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase): '''simple docstring''' __A : Tuple = w def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' for k in range(0 , self.n): for i in range(0 , self.n): for j in range(0 , self.n): __A : List[str] = min(self.dp[i][j] , self.dp[i][k] + self.dp[k][j]) def SCREAMING_SNAKE_CASE ( self , _UpperCAmelCase , _UpperCAmelCase): '''simple docstring''' return self.dp[u][v] if __name__ == "__main__": lowercase__ : Tuple = Graph(5) graph.add_edge(0, 2, 9) graph.add_edge(0, 4, 10) graph.add_edge(1, 3, 5) graph.add_edge(2, 3, 7) graph.add_edge(3, 0, 10) graph.add_edge(3, 1, 2) graph.add_edge(3, 2, 1) graph.add_edge(3, 4, 6) graph.add_edge(4, 1, 3) graph.add_edge(4, 2, 4) graph.add_edge(4, 3, 9) graph.floyd_warshall() graph.show_min(1, 4) graph.show_min(0, 3)
8
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import GLPNImageProcessor class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __init__( self :Any , lowerCamelCase__ :Dict , lowerCamelCase__ :Optional[Any]=7 , lowerCamelCase__ :str=3 , lowerCamelCase__ :Optional[Any]=18 , lowerCamelCase__ :List[str]=30 , lowerCamelCase__ :str=4_00 , lowerCamelCase__ :Optional[int]=True , lowerCamelCase__ :Union[str, Any]=32 , lowerCamelCase__ :int=True , ): UpperCamelCase__ :List[Any] = parent UpperCamelCase__ :List[Any] = batch_size UpperCamelCase__ :Any = num_channels UpperCamelCase__ :List[str] = image_size UpperCamelCase__ :Dict = min_resolution UpperCamelCase__ :List[str] = max_resolution UpperCamelCase__ :str = do_resize UpperCamelCase__ :int = size_divisor UpperCamelCase__ :Optional[int] = do_rescale def __a ( self :str ): return { "do_resize": self.do_resize, "size_divisor": self.size_divisor, "do_rescale": self.do_rescale, } @require_torch @require_vision class lowerCAmelCase_ ( lowercase , unittest.TestCase ): """simple docstring""" _snake_case : Optional[int] = GLPNImageProcessor if is_vision_available() else None def __a ( self :Dict ): UpperCamelCase__ :Dict = GLPNImageProcessingTester(self ) @property def __a ( self :List[str] ): return self.image_processor_tester.prepare_image_processor_dict() def __a ( self :Optional[int] ): UpperCamelCase__ :Optional[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(lowerCamelCase__ , """do_resize""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """size_divisor""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """resample""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """do_rescale""" ) ) def __a ( self :Optional[int] ): pass def __a ( self :Tuple ): # Initialize image_processing UpperCamelCase__ :int = self.image_processing_class(**self.image_processor_dict ) # create random PIL images UpperCamelCase__ :str = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCamelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCamelCase__ , Image.Image ) # Test not batched input (GLPNImageProcessor doesn't support batching) UpperCamelCase__ :Tuple = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 ) self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 ) def __a ( self :str ): # Initialize image_processing UpperCamelCase__ :str = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors UpperCamelCase__ :Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCamelCase__ , numpify=lowerCamelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCamelCase__ , np.ndarray ) # Test not batched input (GLPNImageProcessor doesn't support batching) UpperCamelCase__ :List[str] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 ) self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 ) def __a ( self :Any ): # Initialize image_processing UpperCamelCase__ :List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors UpperCamelCase__ :Tuple = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCamelCase__ , torchify=lowerCamelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCamelCase__ , torch.Tensor ) # Test not batched input (GLPNImageProcessor doesn't support batching) UpperCamelCase__ :List[str] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 ) self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 )
45
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available SCREAMING_SNAKE_CASE__ = { '''configuration_longt5''': ['''LONGT5_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''LongT5Config''', '''LongT5OnnxConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''LONGT5_PRETRAINED_MODEL_ARCHIVE_LIST''', '''LongT5EncoderModel''', '''LongT5ForConditionalGeneration''', '''LongT5Model''', '''LongT5PreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ '''FlaxLongT5ForConditionalGeneration''', '''FlaxLongT5Model''', '''FlaxLongT5PreTrainedModel''', ] if TYPE_CHECKING: from .configuration_longta import LONGT5_PRETRAINED_CONFIG_ARCHIVE_MAP, LongTaConfig, LongTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_longta import ( LONGT5_PRETRAINED_MODEL_ARCHIVE_LIST, LongTaEncoderModel, LongTaForConditionalGeneration, LongTaModel, LongTaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_longta import ( FlaxLongTaForConditionalGeneration, FlaxLongTaModel, FlaxLongTaPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
9
import math def A ( lowercase__ : Tuple , lowercase__ : Union[str, Any] ) -> Optional[Any]: if 0 not in (x, y): # We use the relation x^y = y*log10(x), where 10 is the base. return y * math.logaa(lowercase__ ) else: if x == 0: # 0 raised to any number is 0 return 0 elif y == 0: return 1 # any number raised to 0 is 1 raise AssertionError("""This should never happen""" ) if __name__ == "__main__": # Main function # Read two numbers from input and typecast them to int using map function. # Here x is the base and y is the power. UpperCamelCase = "Enter the base and the power separated by a comma: " UpperCamelCase , UpperCamelCase = map(int, input(prompt).split(",")) UpperCamelCase , UpperCamelCase = map(int, input(prompt).split(",")) # We find the log of each number, using the function res(), which takes two # arguments. UpperCamelCase = res(xa, ya) UpperCamelCase = res(xa, ya) # We check for the largest number if resa > resa: print("Largest number is", xa, "^", ya) elif resa > resa: print("Largest number is", xa, "^", ya) else: print("Both are equal")
45
0
import math from collections import defaultdict from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin, SchedulerOutput def _snake_case ( __snake_case , __snake_case=0.999 , __snake_case="cosine" , ): if alpha_transform_type == "cosine": def alpha_bar_fn(__snake_case ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(__snake_case ): return math.exp(t * -12.0 ) else: raise ValueError(f"""Unsupported alpha_tranform_type: {alpha_transform_type}""" ) _UpperCamelCase = [] for i in range(__snake_case ): _UpperCamelCase = i / num_diffusion_timesteps _UpperCamelCase = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(__snake_case ) / alpha_bar_fn(__snake_case ) , __snake_case ) ) return torch.tensor(__snake_case , dtype=torch.floataa ) class lowerCAmelCase_ ( __lowercase, __lowercase ): UpperCAmelCase = [e.name for e in KarrasDiffusionSchedulers] UpperCAmelCase = 2 @register_to_config def __init__( self : Optional[Any] , _A : int = 1000 , _A : float = 0.0_0085 , _A : float = 0.012 , _A : str = "linear" , _A : Optional[Union[np.ndarray, List[float]]] = None , _A : str = "epsilon" , _A : str = "linspace" , _A : int = 0 , ): if trained_betas is not None: _UpperCamelCase = torch.tensor(_A , dtype=torch.floataa ) elif beta_schedule == "linear": _UpperCamelCase = torch.linspace(_A , _A , _A , dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. _UpperCamelCase = ( torch.linspace(beta_start**0.5 , beta_end**0.5 , _A , dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule _UpperCamelCase = betas_for_alpha_bar(_A ) else: raise NotImplementedError(F"""{beta_schedule} does is not implemented for {self.__class__}""" ) _UpperCamelCase = 1.0 - self.betas _UpperCamelCase = torch.cumprod(self.alphas , dim=0 ) # set all values self.set_timesteps(_A , _A , _A ) def UpperCamelCase_ ( self : Union[str, Any] , _A : Dict , _A : List[str]=None ): if schedule_timesteps is None: _UpperCamelCase = self.timesteps _UpperCamelCase = (schedule_timesteps == timestep).nonzero() # The sigma index that is taken for the **very** first `step` # is always the second index (or the last index if there is only 1) # This way we can ensure we don't accidentally skip a sigma in # case we start in the middle of the denoising schedule (e.g. for image-to-image) if len(self._index_counter ) == 0: _UpperCamelCase = 1 if len(_A ) > 1 else 0 else: _UpperCamelCase = timestep.cpu().item() if torch.is_tensor(_A ) else timestep _UpperCamelCase = self._index_counter[timestep_int] return indices[pos].item() @property def UpperCamelCase_ ( self : str ): # standard deviation of the initial noise distribution if self.config.timestep_spacing in ["linspace", "trailing"]: return self.sigmas.max() return (self.sigmas.max() ** 2 + 1) ** 0.5 def UpperCamelCase_ ( self : str , _A : torch.FloatTensor , _A : Union[float, torch.FloatTensor] , ): _UpperCamelCase = self.index_for_timestep(_A ) if self.state_in_first_order: _UpperCamelCase = self.sigmas[step_index] else: _UpperCamelCase = self.sigmas_interpol[step_index] _UpperCamelCase = sample / ((sigma**2 + 1) ** 0.5) return sample def UpperCamelCase_ ( self : Optional[Any] , _A : int , _A : Union[str, torch.device] = None , _A : Optional[int] = None , ): _UpperCamelCase = num_inference_steps _UpperCamelCase = num_train_timesteps or self.config.num_train_timesteps # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891 if self.config.timestep_spacing == "linspace": _UpperCamelCase = np.linspace(0 , num_train_timesteps - 1 , _A , dtype=_A )[::-1].copy() elif self.config.timestep_spacing == "leading": _UpperCamelCase = num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 _UpperCamelCase = (np.arange(0 , _A ) * step_ratio).round()[::-1].copy().astype(_A ) timesteps += self.config.steps_offset elif self.config.timestep_spacing == "trailing": _UpperCamelCase = num_train_timesteps / self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 _UpperCamelCase = (np.arange(_A , 0 , -step_ratio )).round().copy().astype(_A ) timesteps -= 1 else: raise ValueError( F"""{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'.""" ) _UpperCamelCase = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5 ) _UpperCamelCase = torch.from_numpy(np.log(_A ) ).to(_A ) _UpperCamelCase = np.interp(_A , np.arange(0 , len(_A ) ) , _A ) _UpperCamelCase = np.concatenate([sigmas, [0.0]] ).astype(np.floataa ) _UpperCamelCase = torch.from_numpy(_A ).to(device=_A ) # interpolate sigmas _UpperCamelCase = sigmas.log().lerp(sigmas.roll(1 ).log() , 0.5 ).exp() _UpperCamelCase = torch.cat([sigmas[:1], sigmas[1:].repeat_interleave(2 ), sigmas[-1:]] ) _UpperCamelCase = torch.cat( [sigmas_interpol[:1], sigmas_interpol[1:].repeat_interleave(2 ), sigmas_interpol[-1:]] ) if str(_A ).startswith('''mps''' ): # mps does not support float64 _UpperCamelCase = torch.from_numpy(_A ).to(_A , dtype=torch.floataa ) else: _UpperCamelCase = torch.from_numpy(_A ).to(_A ) # interpolate timesteps _UpperCamelCase = self.sigma_to_t(_A ).to(_A , dtype=timesteps.dtype ) _UpperCamelCase = torch.stack((timesteps_interpol[1:-1, None], timesteps[1:, None]) , dim=-1 ).flatten() _UpperCamelCase = torch.cat([timesteps[:1], interleaved_timesteps] ) _UpperCamelCase = None # for exp beta schedules, such as the one for `pipeline_shap_e.py` # we need an index counter _UpperCamelCase = defaultdict(_A ) def UpperCamelCase_ ( self : Any , _A : List[Any] ): # get log sigma _UpperCamelCase = sigma.log() # get distribution _UpperCamelCase = log_sigma - self.log_sigmas[:, None] # get sigmas range _UpperCamelCase = dists.ge(0 ).cumsum(dim=0 ).argmax(dim=0 ).clamp(max=self.log_sigmas.shape[0] - 2 ) _UpperCamelCase = low_idx + 1 _UpperCamelCase = self.log_sigmas[low_idx] _UpperCamelCase = self.log_sigmas[high_idx] # interpolate sigmas _UpperCamelCase = (low - log_sigma) / (low - high) _UpperCamelCase = w.clamp(0 , 1 ) # transform interpolation to time range _UpperCamelCase = (1 - w) * low_idx + w * high_idx _UpperCamelCase = t.view(sigma.shape ) return t @property def UpperCamelCase_ ( self : Optional[Any] ): return self.sample is None def UpperCamelCase_ ( self : Optional[int] , _A : Union[torch.FloatTensor, np.ndarray] , _A : Union[float, torch.FloatTensor] , _A : Union[torch.FloatTensor, np.ndarray] , _A : bool = True , ): _UpperCamelCase = self.index_for_timestep(_A ) # advance index counter by 1 _UpperCamelCase = timestep.cpu().item() if torch.is_tensor(_A ) else timestep self._index_counter[timestep_int] += 1 if self.state_in_first_order: _UpperCamelCase = self.sigmas[step_index] _UpperCamelCase = self.sigmas_interpol[step_index + 1] _UpperCamelCase = self.sigmas[step_index + 1] else: # 2nd order / KDPM2's method _UpperCamelCase = self.sigmas[step_index - 1] _UpperCamelCase = self.sigmas_interpol[step_index] _UpperCamelCase = self.sigmas[step_index] # currently only gamma=0 is supported. This usually works best anyways. # We can support gamma in the future but then need to scale the timestep before # passing it to the model which requires a change in API _UpperCamelCase = 0 _UpperCamelCase = sigma * (gamma + 1) # Note: sigma_hat == sigma for now # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise if self.config.prediction_type == "epsilon": _UpperCamelCase = sigma_hat if self.state_in_first_order else sigma_interpol _UpperCamelCase = sample - sigma_input * model_output elif self.config.prediction_type == "v_prediction": _UpperCamelCase = sigma_hat if self.state_in_first_order else sigma_interpol _UpperCamelCase = model_output * (-sigma_input / (sigma_input**2 + 1) ** 0.5) + ( sample / (sigma_input**2 + 1) ) elif self.config.prediction_type == "sample": raise NotImplementedError('''prediction_type not implemented yet: sample''' ) else: raise ValueError( F"""prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`""" ) if self.state_in_first_order: # 2. Convert to an ODE derivative for 1st order _UpperCamelCase = (sample - pred_original_sample) / sigma_hat # 3. delta timestep _UpperCamelCase = sigma_interpol - sigma_hat # store for 2nd order step _UpperCamelCase = sample else: # DPM-Solver-2 # 2. Convert to an ODE derivative for 2nd order _UpperCamelCase = (sample - pred_original_sample) / sigma_interpol # 3. delta timestep _UpperCamelCase = sigma_next - sigma_hat _UpperCamelCase = self.sample _UpperCamelCase = None _UpperCamelCase = sample + derivative * dt if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=_A ) def UpperCamelCase_ ( self : List[Any] , _A : torch.FloatTensor , _A : torch.FloatTensor , _A : torch.FloatTensor , ): # Make sure sigmas and timesteps have the same device and dtype as original_samples _UpperCamelCase = self.sigmas.to(device=original_samples.device , dtype=original_samples.dtype ) if original_samples.device.type == "mps" and torch.is_floating_point(_A ): # mps does not support float64 _UpperCamelCase = self.timesteps.to(original_samples.device , dtype=torch.floataa ) _UpperCamelCase = timesteps.to(original_samples.device , dtype=torch.floataa ) else: _UpperCamelCase = self.timesteps.to(original_samples.device ) _UpperCamelCase = timesteps.to(original_samples.device ) _UpperCamelCase = [self.index_for_timestep(_A , _A ) for t in timesteps] _UpperCamelCase = sigmas[step_indices].flatten() while len(sigma.shape ) < len(original_samples.shape ): _UpperCamelCase = sigma.unsqueeze(-1 ) _UpperCamelCase = original_samples + noise * sigma return noisy_samples def __len__( self : List[str] ): return self.config.num_train_timesteps
10
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, 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 numpy as np import tensorflow as tf from transformers import ( TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST, FlaubertConfig, TFFlaubertForMultipleChoice, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForSequenceClassification, TFFlaubertForTokenClassification, TFFlaubertModel, TFFlaubertWithLMHeadModel, ) class lowerCAmelCase_ : """simple docstring""" def __init__( self :Dict , lowerCamelCase__ :List[str] , ): UpperCamelCase__ :Optional[int] = parent UpperCamelCase__ :int = 13 UpperCamelCase__ :Optional[int] = 7 UpperCamelCase__ :Dict = True UpperCamelCase__ :Dict = True UpperCamelCase__ :str = True UpperCamelCase__ :List[Any] = True UpperCamelCase__ :Any = True UpperCamelCase__ :Optional[int] = False UpperCamelCase__ :Optional[int] = False UpperCamelCase__ :Tuple = False UpperCamelCase__ :Optional[int] = 2 UpperCamelCase__ :List[str] = 99 UpperCamelCase__ :Optional[Any] = 0 UpperCamelCase__ :Any = 32 UpperCamelCase__ :List[str] = 2 UpperCamelCase__ :int = 4 UpperCamelCase__ :List[str] = 0.1 UpperCamelCase__ :Union[str, Any] = 0.1 UpperCamelCase__ :Union[str, Any] = 5_12 UpperCamelCase__ :List[str] = 16 UpperCamelCase__ :str = 2 UpperCamelCase__ :Optional[int] = 0.02 UpperCamelCase__ :Optional[int] = 3 UpperCamelCase__ :Optional[int] = 4 UpperCamelCase__ :Optional[int] = """last""" UpperCamelCase__ :Tuple = True UpperCamelCase__ :int = None UpperCamelCase__ :Dict = 0 def __a ( self :int ): UpperCamelCase__ :Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCamelCase__ :Any = random_attention_mask([self.batch_size, self.seq_length] , dtype=tf.floataa ) UpperCamelCase__ :Union[str, Any] = None if self.use_input_lengths: UpperCamelCase__ :Union[str, Any] = ( ids_tensor([self.batch_size] , vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length UpperCamelCase__ :List[str] = None if self.use_token_type_ids: UpperCamelCase__ :List[str] = ids_tensor([self.batch_size, self.seq_length] , self.n_langs ) UpperCamelCase__ :int = None UpperCamelCase__ :List[str] = None UpperCamelCase__ :List[str] = None if self.use_labels: UpperCamelCase__ :List[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase__ :Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCamelCase__ :str = ids_tensor([self.batch_size] , 2 , dtype=tf.floataa ) UpperCamelCase__ :int = ids_tensor([self.batch_size] , self.num_choices ) UpperCamelCase__ :List[Any] = FlaubertConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , bos_token_id=self.bos_token_id , ) return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def __a ( self :Union[str, Any] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :int , lowerCamelCase__ :List[str] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :int , lowerCamelCase__ :List[Any] , lowerCamelCase__ :List[Any] , lowerCamelCase__ :int , ): UpperCamelCase__ :int = TFFlaubertModel(config=lowerCamelCase__ ) UpperCamelCase__ :Union[str, Any] = {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids} UpperCamelCase__ :Optional[Any] = model(lowerCamelCase__ ) UpperCamelCase__ :Union[str, Any] = [input_ids, input_mask] UpperCamelCase__ :Optional[int] = model(lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __a ( self :Tuple , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Any , lowerCamelCase__ :int , lowerCamelCase__ :int , lowerCamelCase__ :List[str] , lowerCamelCase__ :Any , lowerCamelCase__ :Optional[Any] , ): UpperCamelCase__ :List[str] = TFFlaubertWithLMHeadModel(lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids} UpperCamelCase__ :Any = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __a ( self :Dict , lowerCamelCase__ :List[str] , lowerCamelCase__ :Dict , lowerCamelCase__ :Tuple , lowerCamelCase__ :str , lowerCamelCase__ :Any , lowerCamelCase__ :Tuple , lowerCamelCase__ :Any , lowerCamelCase__ :int , lowerCamelCase__ :Tuple , ): UpperCamelCase__ :int = TFFlaubertForQuestionAnsweringSimple(lowerCamelCase__ ) UpperCamelCase__ :int = {"""input_ids""": input_ids, """lengths""": input_lengths} UpperCamelCase__ :Optional[int] = model(lowerCamelCase__ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __a ( self :List[Any] , lowerCamelCase__ :List[str] , lowerCamelCase__ :List[str] , lowerCamelCase__ :str , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Tuple , lowerCamelCase__ :Tuple , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :int , lowerCamelCase__ :Optional[int] , ): UpperCamelCase__ :List[Any] = TFFlaubertForSequenceClassification(lowerCamelCase__ ) UpperCamelCase__ :List[str] = {"""input_ids""": input_ids, """lengths""": input_lengths} UpperCamelCase__ :List[str] = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __a ( self :Tuple , lowerCamelCase__ :str , lowerCamelCase__ :Any , lowerCamelCase__ :Tuple , lowerCamelCase__ :str , lowerCamelCase__ :Any , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :List[str] , lowerCamelCase__ :str , lowerCamelCase__ :Any , ): UpperCamelCase__ :Any = self.num_labels UpperCamelCase__ :Tuple = TFFlaubertForTokenClassification(config=lowerCamelCase__ ) UpperCamelCase__ :Any = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} UpperCamelCase__ :List[Any] = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __a ( self :Tuple , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Tuple , lowerCamelCase__ :Any , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :str , lowerCamelCase__ :Any , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Dict , lowerCamelCase__ :List[str] , ): UpperCamelCase__ :Optional[int] = self.num_choices UpperCamelCase__ :Dict = TFFlaubertForMultipleChoice(config=lowerCamelCase__ ) UpperCamelCase__ :Any = tf.tile(tf.expand_dims(lowerCamelCase__ , 1 ) , (1, self.num_choices, 1) ) UpperCamelCase__ :str = tf.tile(tf.expand_dims(lowerCamelCase__ , 1 ) , (1, self.num_choices, 1) ) UpperCamelCase__ :Any = tf.tile(tf.expand_dims(lowerCamelCase__ , 1 ) , (1, self.num_choices, 1) ) UpperCamelCase__ :int = { """input_ids""": multiple_choice_inputs_ids, """attention_mask""": multiple_choice_input_mask, """token_type_ids""": multiple_choice_token_type_ids, } UpperCamelCase__ :List[str] = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __a ( self :Tuple ): UpperCamelCase__ :str = self.prepare_config_and_inputs() ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :str = config_and_inputs UpperCamelCase__ :Optional[Any] = { """input_ids""": input_ids, """token_type_ids""": token_type_ids, """langs""": token_type_ids, """lengths""": input_lengths, } return config, inputs_dict @require_tf class lowerCAmelCase_ ( lowercase , lowercase , unittest.TestCase ): """simple docstring""" _snake_case : List[str] = ( ( TFFlaubertModel, TFFlaubertWithLMHeadModel, TFFlaubertForSequenceClassification, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForTokenClassification, TFFlaubertForMultipleChoice, ) if is_tf_available() else () ) _snake_case : List[Any] = ( (TFFlaubertWithLMHeadModel,) if is_tf_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable _snake_case : Optional[int] = ( { """feature-extraction""": TFFlaubertModel, """fill-mask""": TFFlaubertWithLMHeadModel, """question-answering""": TFFlaubertForQuestionAnsweringSimple, """text-classification""": TFFlaubertForSequenceClassification, """token-classification""": TFFlaubertForTokenClassification, """zero-shot""": TFFlaubertForSequenceClassification, } if is_tf_available() else {} ) _snake_case : List[Any] = False _snake_case : Tuple = False def __a ( self :Optional[int] , lowerCamelCase__ :List[Any] , lowerCamelCase__ :Dict , lowerCamelCase__ :int , lowerCamelCase__ :str , lowerCamelCase__ :List[Any] ): if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith("""Fast""" ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def __a ( self :List[str] ): UpperCamelCase__ :List[str] = TFFlaubertModelTester(self ) UpperCamelCase__ :Tuple = ConfigTester(self , config_class=lowerCamelCase__ , emb_dim=37 ) def __a ( self :int ): self.config_tester.run_common_tests() def __a ( self :List[str] ): UpperCamelCase__ :List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*lowerCamelCase__ ) def __a ( self :Tuple ): UpperCamelCase__ :Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*lowerCamelCase__ ) def __a ( self :Union[str, Any] ): UpperCamelCase__ :Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*lowerCamelCase__ ) def __a ( self :List[Any] ): UpperCamelCase__ :Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*lowerCamelCase__ ) def __a ( self :Any ): UpperCamelCase__ :Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_token_classification(*lowerCamelCase__ ) def __a ( self :List[Any] ): UpperCamelCase__ :Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_multiple_choice(*lowerCamelCase__ ) @slow def __a ( self :str ): for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase__ :Dict = TFFlaubertModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) @require_tf @require_sentencepiece @require_tokenizers class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" @slow def __a ( self :str ): UpperCamelCase__ :Tuple = TFFlaubertModel.from_pretrained("""jplu/tf-flaubert-small-cased""" ) UpperCamelCase__ :Optional[int] = tf.convert_to_tensor( [[0, 1_58, 7_35, 25_92, 14_24, 67_27, 82, 1]] , dtype=tf.intaa , ) # "J'aime flaubert !" UpperCamelCase__ :Optional[Any] = model(lowerCamelCase__ )[0] UpperCamelCase__ :Optional[int] = tf.TensorShape((1, 8, 5_12) ) self.assertEqual(output.shape , lowerCamelCase__ ) # compare the actual values for a slice. UpperCamelCase__ :str = tf.convert_to_tensor( [ [ [-1.876_8773, -1.56_6555, 0.2707_2418], [-1.692_0038, -0.587_3505, 1.932_9599], [-2.956_3985, -1.699_3835, 1.797_2052], ] ] , dtype=tf.floataa , ) self.assertTrue(np.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1e-4 ) )
45
0
'''simple docstring''' from __future__ import annotations import math def lowerCAmelCase (__A): """simple docstring""" if num <= 0: _a = F'''{num}: Invalid input, please enter a positive integer.''' raise ValueError(__A) _a = [True] * (num + 1) _a = [] _a = 2 _a = int(math.sqrt(__A)) while start <= end: # If start is a prime if sieve[start] is True: prime.append(__A) # Set multiples of start be False for i in range(start * start , num + 1 , __A): if sieve[i] is True: _a = False start += 1 for j in range(end + 1 , num + 1): if sieve[j] is True: prime.append(__A) return prime if __name__ == "__main__": print(prime_sieve(int(input("Enter a positive integer: ").strip())))
11
import gc import tempfile import unittest import numpy as np import torch from diffusers import VersatileDiffusionPipeline from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device UpperCamelCase = False class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" pass @nightly @require_torch_gpu class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __a ( self :Union[str, Any] ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __a ( self :List[Any] ): UpperCamelCase__ :List[str] = VersatileDiffusionPipeline.from_pretrained("""shi-labs/versatile-diffusion""" , torch_dtype=torch.floataa ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) UpperCamelCase__ :Dict = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg""" ) UpperCamelCase__ :Any = torch.manual_seed(0 ) UpperCamelCase__ :Optional[int] = pipe.dual_guided( prompt="""first prompt""" , image=lowerCamelCase__ , text_to_image_strength=0.75 , generator=lowerCamelCase__ , guidance_scale=7.5 , num_inference_steps=2 , output_type="""numpy""" , ).images with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(lowerCamelCase__ ) UpperCamelCase__ :List[str] = VersatileDiffusionPipeline.from_pretrained(lowerCamelCase__ , torch_dtype=torch.floataa ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) UpperCamelCase__ :str = generator.manual_seed(0 ) UpperCamelCase__ :str = pipe.dual_guided( prompt="""first prompt""" , image=lowerCamelCase__ , text_to_image_strength=0.75 , generator=lowerCamelCase__ , guidance_scale=7.5 , num_inference_steps=2 , output_type="""numpy""" , ).images assert np.abs(image - new_image ).sum() < 1e-5, "Models don't have the same forward pass" def __a ( self :Dict ): UpperCamelCase__ :List[Any] = VersatileDiffusionPipeline.from_pretrained("""shi-labs/versatile-diffusion""" , torch_dtype=torch.floataa ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = """cyberpunk 2077""" UpperCamelCase__ :str = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg""" ) UpperCamelCase__ :str = torch.manual_seed(0 ) UpperCamelCase__ :Dict = pipe.dual_guided( prompt=lowerCamelCase__ , image=lowerCamelCase__ , text_to_image_strength=0.75 , generator=lowerCamelCase__ , guidance_scale=7.5 , num_inference_steps=50 , output_type="""numpy""" , ).images UpperCamelCase__ :Tuple = image[0, 2_53:2_56, 2_53:2_56, -1] assert image.shape == (1, 5_12, 5_12, 3) UpperCamelCase__ :Any = np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 UpperCamelCase__ :List[Any] = """A painting of a squirrel eating a burger """ UpperCamelCase__ :List[str] = torch.manual_seed(0 ) UpperCamelCase__ :Optional[int] = pipe.text_to_image( prompt=lowerCamelCase__ , generator=lowerCamelCase__ , guidance_scale=7.5 , num_inference_steps=50 , output_type="""numpy""" ).images UpperCamelCase__ :str = image[0, 2_53:2_56, 2_53:2_56, -1] assert image.shape == (1, 5_12, 5_12, 3) UpperCamelCase__ :Union[str, Any] = np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 UpperCamelCase__ :Optional[int] = pipe.image_variation(lowerCamelCase__ , generator=lowerCamelCase__ , output_type="""numpy""" ).images UpperCamelCase__ :int = image[0, 2_53:2_56, 2_53:2_56, -1] assert image.shape == (1, 5_12, 5_12, 3) UpperCamelCase__ :List[Any] = np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1
45
0
lowerCamelCase__ : Tuple = { """Pillow""": """Pillow""", """accelerate""": """accelerate>=0.11.0""", """compel""": """compel==0.1.8""", """black""": """black~=23.1""", """datasets""": """datasets""", """filelock""": """filelock""", """flax""": """flax>=0.4.1""", """hf-doc-builder""": """hf-doc-builder>=0.3.0""", """huggingface-hub""": """huggingface-hub>=0.13.2""", """requests-mock""": """requests-mock==1.10.0""", """importlib_metadata""": """importlib_metadata""", """invisible-watermark""": """invisible-watermark""", """isort""": """isort>=5.5.4""", """jax""": """jax>=0.2.8,!=0.3.2""", """jaxlib""": """jaxlib>=0.1.65""", """Jinja2""": """Jinja2""", """k-diffusion""": """k-diffusion>=0.0.12""", """torchsde""": """torchsde""", """note_seq""": """note_seq""", """librosa""": """librosa""", """numpy""": """numpy""", """omegaconf""": """omegaconf""", """parameterized""": """parameterized""", """protobuf""": """protobuf>=3.20.3,<4""", """pytest""": """pytest""", """pytest-timeout""": """pytest-timeout""", """pytest-xdist""": """pytest-xdist""", """ruff""": """ruff>=0.0.241""", """safetensors""": """safetensors""", """sentencepiece""": """sentencepiece>=0.1.91,!=0.1.92""", """scipy""": """scipy""", """onnx""": """onnx""", """regex""": """regex!=2019.12.17""", """requests""": """requests""", """tensorboard""": """tensorboard""", """torch""": """torch>=1.4""", """torchvision""": """torchvision""", """transformers""": """transformers>=4.25.1""", """urllib3""": """urllib3<=2.0.0""", }
12
from __future__ import annotations import copy import inspect import unittest import numpy as np from transformers import is_tf_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST, TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, TFLayoutLMvaModel, ) if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class lowerCAmelCase_ : """simple docstring""" def __init__( self :Union[str, Any] , lowerCamelCase__ :Tuple , lowerCamelCase__ :List[str]=2 , lowerCamelCase__ :List[str]=3 , lowerCamelCase__ :List[str]=4 , lowerCamelCase__ :str=2 , lowerCamelCase__ :Optional[int]=7 , lowerCamelCase__ :List[Any]=True , lowerCamelCase__ :Optional[Any]=True , lowerCamelCase__ :Union[str, Any]=True , lowerCamelCase__ :Any=True , lowerCamelCase__ :Dict=99 , lowerCamelCase__ :Optional[Any]=36 , lowerCamelCase__ :str=2 , lowerCamelCase__ :List[Any]=4 , lowerCamelCase__ :Optional[Any]=37 , lowerCamelCase__ :Optional[int]="gelu" , lowerCamelCase__ :Any=0.1 , lowerCamelCase__ :List[Any]=0.1 , lowerCamelCase__ :List[Any]=5_12 , lowerCamelCase__ :str=16 , lowerCamelCase__ :Tuple=2 , lowerCamelCase__ :int=0.02 , lowerCamelCase__ :List[Any]=6 , lowerCamelCase__ :List[str]=6 , lowerCamelCase__ :Optional[int]=3 , lowerCamelCase__ :Optional[int]=4 , lowerCamelCase__ :int=None , lowerCamelCase__ :Optional[Any]=10_00 , ): UpperCamelCase__ :Any = parent UpperCamelCase__ :Union[str, Any] = batch_size UpperCamelCase__ :Dict = num_channels UpperCamelCase__ :Optional[Any] = image_size UpperCamelCase__ :Union[str, Any] = patch_size UpperCamelCase__ :Union[str, Any] = is_training UpperCamelCase__ :str = use_input_mask UpperCamelCase__ :int = use_token_type_ids UpperCamelCase__ :int = use_labels UpperCamelCase__ :List[Any] = vocab_size UpperCamelCase__ :List[str] = hidden_size UpperCamelCase__ :List[Any] = num_hidden_layers UpperCamelCase__ :List[str] = num_attention_heads UpperCamelCase__ :Tuple = intermediate_size UpperCamelCase__ :Any = hidden_act UpperCamelCase__ :Optional[int] = hidden_dropout_prob UpperCamelCase__ :Tuple = attention_probs_dropout_prob UpperCamelCase__ :Dict = max_position_embeddings UpperCamelCase__ :Tuple = type_vocab_size UpperCamelCase__ :Union[str, Any] = type_sequence_label_size UpperCamelCase__ :int = initializer_range UpperCamelCase__ :List[Any] = coordinate_size UpperCamelCase__ :Tuple = shape_size UpperCamelCase__ :Dict = num_labels UpperCamelCase__ :str = num_choices UpperCamelCase__ :Tuple = scope UpperCamelCase__ :str = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) UpperCamelCase__ :List[str] = text_seq_length UpperCamelCase__ :List[str] = (image_size // patch_size) ** 2 + 1 UpperCamelCase__ :Dict = self.text_seq_length + self.image_seq_length def __a ( self :Tuple ): UpperCamelCase__ :Optional[int] = ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size ) UpperCamelCase__ :int = ids_tensor([self.batch_size, self.text_seq_length, 4] , self.range_bbox ) UpperCamelCase__ :str = bbox.numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: UpperCamelCase__ :List[str] = bbox[i, j, 3] UpperCamelCase__ :Optional[int] = bbox[i, j, 1] UpperCamelCase__ :Optional[Any] = tmp_coordinate if bbox[i, j, 2] < bbox[i, j, 0]: UpperCamelCase__ :Tuple = bbox[i, j, 2] UpperCamelCase__ :Optional[Any] = bbox[i, j, 0] UpperCamelCase__ :List[str] = tmp_coordinate UpperCamelCase__ :Dict = tf.constant(lowerCamelCase__ ) UpperCamelCase__ :Union[str, Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCamelCase__ :Any = None if self.use_input_mask: UpperCamelCase__ :int = random_attention_mask([self.batch_size, self.text_seq_length] ) UpperCamelCase__ :Optional[Any] = None if self.use_token_type_ids: UpperCamelCase__ :Optional[int] = ids_tensor([self.batch_size, self.text_seq_length] , self.type_vocab_size ) UpperCamelCase__ :List[str] = None UpperCamelCase__ :Union[str, Any] = None if self.use_labels: UpperCamelCase__ :Union[str, Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase__ :Union[str, Any] = ids_tensor([self.batch_size, self.text_seq_length] , self.num_labels ) UpperCamelCase__ :Optional[int] = LayoutLMvaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , coordinate_size=self.coordinate_size , shape_size=self.shape_size , input_size=self.image_size , patch_size=self.patch_size , ) return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels def __a ( self :List[Any] , lowerCamelCase__ :str , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Dict , lowerCamelCase__ :str , lowerCamelCase__ :int , lowerCamelCase__ :Any ): UpperCamelCase__ :Dict = TFLayoutLMvaModel(config=lowerCamelCase__ ) # text + image UpperCamelCase__ :Tuple = model(lowerCamelCase__ , pixel_values=lowerCamelCase__ , training=lowerCamelCase__ ) UpperCamelCase__ :Tuple = model( lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , training=lowerCamelCase__ , ) UpperCamelCase__ :str = model(lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , training=lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # text only UpperCamelCase__ :Optional[int] = model(lowerCamelCase__ , training=lowerCamelCase__ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size) ) # image only UpperCamelCase__ :Tuple = model({"""pixel_values""": pixel_values} , training=lowerCamelCase__ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size) ) def __a ( self :Dict , lowerCamelCase__ :str , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Dict , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :str , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :str ): UpperCamelCase__ :Optional[Any] = self.num_labels UpperCamelCase__ :List[Any] = TFLayoutLMvaForSequenceClassification(config=lowerCamelCase__ ) UpperCamelCase__ :List[str] = model( lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , labels=lowerCamelCase__ , training=lowerCamelCase__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __a ( self :List[str] , lowerCamelCase__ :List[str] , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Dict , lowerCamelCase__ :List[Any] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Tuple , lowerCamelCase__ :List[str] ): UpperCamelCase__ :Union[str, Any] = self.num_labels UpperCamelCase__ :Dict = TFLayoutLMvaForTokenClassification(config=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = model( lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , labels=lowerCamelCase__ , training=lowerCamelCase__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels) ) def __a ( self :int , lowerCamelCase__ :Dict , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Any , lowerCamelCase__ :Dict , lowerCamelCase__ :Tuple , lowerCamelCase__ :Tuple ): UpperCamelCase__ :Dict = 2 UpperCamelCase__ :Tuple = TFLayoutLMvaForQuestionAnswering(config=lowerCamelCase__ ) UpperCamelCase__ :int = model( lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , start_positions=lowerCamelCase__ , end_positions=lowerCamelCase__ , training=lowerCamelCase__ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __a ( self :List[Any] ): UpperCamelCase__ :Union[str, Any] = self.prepare_config_and_inputs() ((UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__)) :Any = config_and_inputs UpperCamelCase__ :List[str] = { """input_ids""": input_ids, """bbox""": bbox, """pixel_values""": pixel_values, """token_type_ids""": token_type_ids, """attention_mask""": input_mask, } return config, inputs_dict @require_tf class lowerCAmelCase_ ( lowercase , lowercase , unittest.TestCase ): """simple docstring""" _snake_case : Dict = ( ( TFLayoutLMvaModel, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, ) if is_tf_available() else () ) _snake_case : Dict = ( {"""document-question-answering""": TFLayoutLMvaForQuestionAnswering, """feature-extraction""": TFLayoutLMvaModel} if is_tf_available() else {} ) _snake_case : Optional[int] = False _snake_case : List[str] = False _snake_case : Tuple = False def __a ( self :str , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Tuple , lowerCamelCase__ :int ): return True def __a ( self :Optional[int] , lowerCamelCase__ :int , lowerCamelCase__ :List[str] , lowerCamelCase__ :Optional[int]=False ): UpperCamelCase__ :List[str] = copy.deepcopy(lowerCamelCase__ ) if model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :Optional[int] = { k: tf.tile(tf.expand_dims(lowerCamelCase__ , 1 ) , (1, self.model_tester.num_choices) + (1,) * (v.ndim - 1) ) if isinstance(lowerCamelCase__ , tf.Tensor ) and v.ndim > 0 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :str = tf.ones(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :List[str] = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) UpperCamelCase__ :Union[str, Any] = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :Optional[Any] = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :Tuple = tf.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=tf.intaa ) return inputs_dict def __a ( self :Dict ): UpperCamelCase__ :List[Any] = TFLayoutLMvaModelTester(self ) UpperCamelCase__ :Optional[int] = ConfigTester(self , config_class=lowerCamelCase__ , hidden_size=37 ) def __a ( self :Any ): self.config_tester.run_common_tests() def __a ( self :Optional[int] ): UpperCamelCase__ , UpperCamelCase__ :Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase__ :Optional[int] = model_class(lowerCamelCase__ ) if getattr(lowerCamelCase__ , """hf_compute_loss""" , lowerCamelCase__ ): # The number of elements in the loss should be the same as the number of elements in the label UpperCamelCase__ :Union[str, Any] = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) UpperCamelCase__ :int = prepared_for_class[ sorted(prepared_for_class.keys() - inputs_dict.keys() , reverse=lowerCamelCase__ )[0] ] UpperCamelCase__ :Union[str, Any] = added_label.shape.as_list()[:1] # Test that model correctly compute the loss with kwargs UpperCamelCase__ :List[Any] = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = prepared_for_class.pop("""input_ids""" ) UpperCamelCase__ :List[str] = model(lowerCamelCase__ , **lowerCamelCase__ )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss when we mask some positions UpperCamelCase__ :Union[str, Any] = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = prepared_for_class.pop("""input_ids""" ) if "labels" in prepared_for_class: UpperCamelCase__ :List[str] = prepared_for_class["""labels"""].numpy() if len(labels.shape ) > 1 and labels.shape[1] != 1: UpperCamelCase__ :Optional[Any] = -1_00 UpperCamelCase__ :Union[str, Any] = tf.convert_to_tensor(lowerCamelCase__ ) UpperCamelCase__ :Tuple = model(lowerCamelCase__ , **lowerCamelCase__ )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) self.assertTrue(not np.any(np.isnan(loss.numpy() ) ) ) # Test that model correctly compute the loss with a dict UpperCamelCase__ :Optional[Any] = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) UpperCamelCase__ :Union[str, Any] = model(lowerCamelCase__ )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss with a tuple UpperCamelCase__ :Dict = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) # Get keys that were added with the _prepare_for_class function UpperCamelCase__ :str = prepared_for_class.keys() - inputs_dict.keys() UpperCamelCase__ :Tuple = inspect.signature(model.call ).parameters UpperCamelCase__ :str = list(signature.keys() ) # Create a dictionary holding the location of the tensors in the tuple UpperCamelCase__ :Any = {0: """input_ids"""} for label_key in label_keys: UpperCamelCase__ :Dict = signature_names.index(lowerCamelCase__ ) UpperCamelCase__ :Optional[int] = label_key UpperCamelCase__ :Optional[Any] = sorted(tuple_index_mapping.items() ) # Initialize a list with their default values, update the values and convert to a tuple UpperCamelCase__ :Any = [] for name in signature_names: if name != "kwargs": list_input.append(signature[name].default ) for index, value in sorted_tuple_index_mapping: UpperCamelCase__ :List[str] = prepared_for_class[value] UpperCamelCase__ :Union[str, Any] = tuple(lowerCamelCase__ ) # Send to model UpperCamelCase__ :str = model(tuple_input[:-1] )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) def __a ( self :Optional[int] ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Any ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :List[Any] = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: UpperCamelCase__ :Dict = type self.model_tester.create_and_check_model(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Tuple ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Optional[int] ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :List[str] ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) @slow def __a ( self :Optional[int] ): for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase__ :Dict = TFLayoutLMvaModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) def A ( ) -> List[str]: UpperCamelCase__ :List[Any] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_tf class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" @cached_property def __a ( self :Optional[Any] ): return LayoutLMvaImageProcessor(apply_ocr=lowerCamelCase__ ) if is_vision_available() else None @slow def __a ( self :Dict ): UpperCamelCase__ :List[str] = TFLayoutLMvaModel.from_pretrained("""microsoft/layoutlmv3-base""" ) UpperCamelCase__ :List[Any] = self.default_image_processor UpperCamelCase__ :str = prepare_img() UpperCamelCase__ :Any = image_processor(images=lowerCamelCase__ , return_tensors="""tf""" ).pixel_values UpperCamelCase__ :str = tf.constant([[1, 2]] ) UpperCamelCase__ :Any = tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]] ) , axis=0 ) # forward pass UpperCamelCase__ :Dict = model(input_ids=lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , training=lowerCamelCase__ ) # verify the logits UpperCamelCase__ :int = (1, 1_99, 7_68) self.assertEqual(outputs.last_hidden_state.shape , lowerCamelCase__ ) UpperCamelCase__ :List[Any] = tf.constant( [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]] ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] , lowerCamelCase__ , atol=1e-4 ) )
45
0
'''simple docstring''' A__ : str = [ [0, 16, 13, 0, 0, 0], [0, 0, 10, 12, 0, 0], [0, 4, 0, 0, 14, 0], [0, 0, 9, 0, 0, 20], [0, 0, 0, 7, 0, 4], [0, 0, 0, 0, 0, 0], ] def UpperCAmelCase__ ( UpperCAmelCase_ : Dict , UpperCAmelCase_ : Any , UpperCAmelCase_ : str , UpperCAmelCase_ : Dict ) -> int: # Return True if there is node that has not iterated. __lowerCamelCase : Optional[int] = [False] * len(UpperCAmelCase_ ) __lowerCamelCase : List[Any] = [s] __lowerCamelCase : str = True while queue: __lowerCamelCase : Dict = queue.pop(0 ) for ind in range(len(graph[u] ) ): if visited[ind] is False and graph[u][ind] > 0: queue.append(UpperCAmelCase_ ) __lowerCamelCase : Tuple = True __lowerCamelCase : List[str] = u return visited[t] def UpperCAmelCase__ ( UpperCAmelCase_ : Any , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : List[Any] ) -> List[Any]: __lowerCamelCase : str = [-1] * (len(UpperCAmelCase_ )) __lowerCamelCase : Union[str, Any] = 0 __lowerCamelCase : int = [] __lowerCamelCase : str = [i[:] for i in graph] # Record original cut, copy. while bfs(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ): __lowerCamelCase : Tuple = float('Inf' ) __lowerCamelCase : List[str] = sink while s != source: # Find the minimum value in select path __lowerCamelCase : Union[str, Any] = min(UpperCAmelCase_ , graph[parent[s]][s] ) __lowerCamelCase : Union[str, Any] = parent[s] max_flow += path_flow __lowerCamelCase : List[Any] = sink while v != source: __lowerCamelCase : Tuple = parent[v] graph[u][v] -= path_flow graph[v][u] += path_flow __lowerCamelCase : List[Any] = parent[v] for i in range(len(UpperCAmelCase_ ) ): for j in range(len(graph[0] ) ): if graph[i][j] == 0 and temp[i][j] > 0: res.append((i, j) ) return res if __name__ == "__main__": print(mincut(test_graph, source=0, sink=5))
13
import logging import os import sys from dataclasses import dataclass, field from typing import Optional import torch from datasets import load_dataset from torchvision.transforms import Compose, Lambda, Normalize, RandomHorizontalFlip, RandomResizedCrop, ToTensor from torchvision.transforms.functional import InterpolationMode import transformers from transformers import ( HfArgumentParser, Trainer, TrainingArguments, ViTImageProcessor, ViTMAEConfig, ViTMAEForPreTraining, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version UpperCamelCase = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.31.0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/image-pretraining/requirements.txt") @dataclass class lowerCAmelCase_ : """simple docstring""" _snake_case : Optional[str] = field( default="""cifar10""" , metadata={"""help""": """Name of a dataset from the datasets package"""} ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """The column name of the images in the files."""} ) _snake_case : Optional[str] = field(default=lowercase , metadata={"""help""": """A folder containing the training data."""} ) _snake_case : Optional[str] = field(default=lowercase , metadata={"""help""": """A folder containing the validation data."""} ) _snake_case : Optional[float] = field( default=0.15 , metadata={"""help""": """Percent to split off of train for validation."""} ) _snake_case : Optional[int] = field( default=lowercase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } , ) _snake_case : Optional[int] = field( default=lowercase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of evaluation examples to this """ """value if set.""" ) } , ) def __a ( self :List[str] ): UpperCamelCase__ :Optional[Any] = {} if self.train_dir is not None: UpperCamelCase__ :int = self.train_dir if self.validation_dir is not None: UpperCamelCase__ :List[str] = self.validation_dir UpperCamelCase__ :Optional[int] = data_files if data_files else None @dataclass class lowerCAmelCase_ : """simple docstring""" _snake_case : str = field( default=lowercase , metadata={ """help""": ( """The model checkpoint for weights initialization.Don't set if you want to train a model from scratch.""" ) } , ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """Pretrained config name or path if not the same as model_name_or_path"""} ) _snake_case : Optional[str] = field( default=lowercase , metadata={ """help""": ( """Override some existing default config settings when a model is trained from scratch. Example: """ """n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index""" ) } , ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """Where do you want to store the pretrained models downloaded from s3"""} ) _snake_case : str = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) _snake_case : str = field(default=lowercase , metadata={"""help""": """Name or path of preprocessor config."""} ) _snake_case : bool = field( default=lowercase , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) _snake_case : float = field( default=0.75 , metadata={"""help""": """The ratio of the number of masked tokens in the input sequence."""} ) _snake_case : bool = field( default=lowercase , metadata={"""help""": """Whether or not to train with normalized pixel values as target."""} ) @dataclass class lowerCAmelCase_ ( lowercase ): """simple docstring""" _snake_case : float = field( default=1e-3 , metadata={"""help""": """Base learning rate: absolute_lr = base_lr * total_batch_size / 256."""} ) def A ( lowercase__ : Union[str, Any] ) -> Dict: UpperCamelCase__ :Union[str, Any] = torch.stack([example["""pixel_values"""] for example in examples] ) return {"pixel_values": pixel_values} def A ( ) -> Optional[int]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. UpperCamelCase__ :Optional[int] = HfArgumentParser((ModelArguments, DataTrainingArguments, CustomTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(""".json""" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :str = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Union[str, Any] = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry("""run_mae""" , lowercase__ , lowercase__ ) # Setup logging logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() UpperCamelCase__ :List[str] = training_args.get_process_log_level() logger.setLevel(lowercase__ ) transformers.utils.logging.set_verbosity(lowercase__ ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( f"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}""" + f"""distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}""" ) logger.info(f"""Training/evaluation parameters {training_args}""" ) # Detecting last checkpoint. UpperCamelCase__ :Union[str, Any] = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: UpperCamelCase__ :List[str] = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( f"""Output directory ({training_args.output_dir}) already exists and is not empty. """ """Use --overwrite_output_dir to overcome.""" ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( f"""Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change """ """the `--output_dir` or add `--overwrite_output_dir` to train from scratch.""" ) # Initialize our dataset. UpperCamelCase__ :Tuple = load_dataset( data_args.dataset_name , data_args.dataset_config_name , data_files=data_args.data_files , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) # If we don't have a validation split, split off a percentage of train as validation. UpperCamelCase__ :int = None if """validation""" in ds.keys() else data_args.train_val_split if isinstance(data_args.train_val_split , lowercase__ ) and data_args.train_val_split > 0.0: UpperCamelCase__ :Optional[Any] = ds["""train"""].train_test_split(data_args.train_val_split ) UpperCamelCase__ :Union[str, Any] = split["""train"""] UpperCamelCase__ :Any = split["""test"""] # Load pretrained model and image processor # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. UpperCamelCase__ :Optional[int] = { """cache_dir""": model_args.cache_dir, """revision""": model_args.model_revision, """use_auth_token""": True if model_args.use_auth_token else None, } if model_args.config_name: UpperCamelCase__ :Any = ViTMAEConfig.from_pretrained(model_args.config_name , **lowercase__ ) elif model_args.model_name_or_path: UpperCamelCase__ :Union[str, Any] = ViTMAEConfig.from_pretrained(model_args.model_name_or_path , **lowercase__ ) else: UpperCamelCase__ :Optional[Any] = ViTMAEConfig() logger.warning("""You are instantiating a new config instance from scratch.""" ) if model_args.config_overrides is not None: logger.info(f"""Overriding config: {model_args.config_overrides}""" ) config.update_from_string(model_args.config_overrides ) logger.info(f"""New config: {config}""" ) # adapt config config.update( { """mask_ratio""": model_args.mask_ratio, """norm_pix_loss""": model_args.norm_pix_loss, } ) # create image processor if model_args.image_processor_name: UpperCamelCase__ :str = ViTImageProcessor.from_pretrained(model_args.image_processor_name , **lowercase__ ) elif model_args.model_name_or_path: UpperCamelCase__ :Dict = ViTImageProcessor.from_pretrained(model_args.model_name_or_path , **lowercase__ ) else: UpperCamelCase__ :Tuple = ViTImageProcessor() # create model if model_args.model_name_or_path: UpperCamelCase__ :Any = ViTMAEForPreTraining.from_pretrained( model_args.model_name_or_path , from_tf=bool(""".ckpt""" in model_args.model_name_or_path ) , config=lowercase__ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info("""Training new model from scratch""" ) UpperCamelCase__ :Optional[int] = ViTMAEForPreTraining(lowercase__ ) if training_args.do_train: UpperCamelCase__ :Optional[Any] = ds["""train"""].column_names else: UpperCamelCase__ :Union[str, Any] = ds["""validation"""].column_names if data_args.image_column_name is not None: UpperCamelCase__ :Union[str, Any] = data_args.image_column_name elif "image" in column_names: UpperCamelCase__ :Optional[Any] = """image""" elif "img" in column_names: UpperCamelCase__ :List[str] = """img""" else: UpperCamelCase__ :List[Any] = column_names[0] # transformations as done in original MAE paper # source: https://github.com/facebookresearch/mae/blob/main/main_pretrain.py if "shortest_edge" in image_processor.size: UpperCamelCase__ :List[str] = image_processor.size["""shortest_edge"""] else: UpperCamelCase__ :int = (image_processor.size["""height"""], image_processor.size["""width"""]) UpperCamelCase__ :Any = Compose( [ Lambda(lambda lowercase__ : img.convert("""RGB""" ) if img.mode != "RGB" else img ), RandomResizedCrop(lowercase__ , scale=(0.2, 1.0) , interpolation=InterpolationMode.BICUBIC ), RandomHorizontalFlip(), ToTensor(), Normalize(mean=image_processor.image_mean , std=image_processor.image_std ), ] ) def preprocess_images(lowercase__ : Tuple ): UpperCamelCase__ :List[Any] = [transforms(lowercase__ ) for image in examples[image_column_name]] return examples if training_args.do_train: if "train" not in ds: raise ValueError("""--do_train requires a train dataset""" ) if data_args.max_train_samples is not None: UpperCamelCase__ :Optional[int] = ds["""train"""].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) # Set the training transforms ds["train"].set_transform(lowercase__ ) if training_args.do_eval: if "validation" not in ds: raise ValueError("""--do_eval requires a validation dataset""" ) if data_args.max_eval_samples is not None: UpperCamelCase__ :Optional[Any] = ( ds["""validation"""].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms ds["validation"].set_transform(lowercase__ ) # Compute absolute learning rate UpperCamelCase__ :Tuple = ( training_args.train_batch_size * training_args.gradient_accumulation_steps * training_args.world_size ) if training_args.base_learning_rate is not None: UpperCamelCase__ :Any = training_args.base_learning_rate * total_train_batch_size / 256 # Initialize our trainer UpperCamelCase__ :Union[str, Any] = Trainer( model=lowercase__ , args=lowercase__ , train_dataset=ds["""train"""] if training_args.do_train else None , eval_dataset=ds["""validation"""] if training_args.do_eval else None , tokenizer=lowercase__ , data_collator=lowercase__ , ) # Training if training_args.do_train: UpperCamelCase__ :Any = None if training_args.resume_from_checkpoint is not None: UpperCamelCase__ :int = training_args.resume_from_checkpoint elif last_checkpoint is not None: UpperCamelCase__ :Dict = last_checkpoint UpperCamelCase__ :Union[str, Any] = trainer.train(resume_from_checkpoint=lowercase__ ) trainer.save_model() trainer.log_metrics("""train""" , train_result.metrics ) trainer.save_metrics("""train""" , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: UpperCamelCase__ :int = trainer.evaluate() trainer.log_metrics("""eval""" , lowercase__ ) trainer.save_metrics("""eval""" , lowercase__ ) # Write model card and (optionally) push to hub UpperCamelCase__ :Optional[int] = { """tasks""": """masked-auto-encoding""", """dataset""": data_args.dataset_name, """tags""": ["""masked-auto-encoding"""], } if training_args.push_to_hub: trainer.push_to_hub(**lowercase__ ) else: trainer.create_model_card(**lowercase__ ) def A ( lowercase__ : Union[str, Any] ) -> Dict: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
45
0
import math import sys def __UpperCAmelCase ( __a : str ) -> str: """simple docstring""" _a : Union[str, Any] = '''''' try: with open(__a ,'''rb''' ) as binary_file: _a : List[str] = binary_file.read() for dat in data: _a : str = F"""{dat:08b}""" result += curr_byte return result except OSError: print('''File not accessible''' ) sys.exit() def __UpperCAmelCase ( __a : str ) -> str: """simple docstring""" _a : Any = {'''0''': '''0''', '''1''': '''1'''} _a , _a : Union[str, Any] = '''''', '''''' _a : Union[str, Any] = len(__a ) for i in range(len(__a ) ): curr_string += data_bits[i] if curr_string not in lexicon: continue _a : Any = lexicon[curr_string] result += last_match_id _a : Optional[Any] = last_match_id + '''0''' if math.loga(__a ).is_integer(): _a : str = {} for curr_key in list(__a ): _a : Optional[int] = lexicon.pop(__a ) _a : Union[str, Any] = new_lex _a : Tuple = last_match_id + '''1''' index += 1 _a : Dict = '''''' return result def __UpperCAmelCase ( __a : str ,__a : str ) -> None: """simple docstring""" _a : List[str] = 8 try: with open(__a ,'''wb''' ) as opened_file: _a : Optional[int] = [ to_write[i : i + byte_length] for i in range(0 ,len(__a ) ,__a ) ] if len(result_byte_array[-1] ) % byte_length == 0: result_byte_array.append('''10000000''' ) else: result_byte_array[-1] += "1" + "0" * ( byte_length - len(result_byte_array[-1] ) - 1 ) for elem in result_byte_array[:-1]: opened_file.write(int(__a ,2 ).to_bytes(1 ,byteorder='''big''' ) ) except OSError: print('''File not accessible''' ) sys.exit() def __UpperCAmelCase ( __a : str ) -> str: """simple docstring""" _a : Union[str, Any] = 0 for letter in data_bits: if letter == "1": break counter += 1 _a : Union[str, Any] = data_bits[counter:] _a : Optional[Any] = data_bits[counter + 1 :] return data_bits def __UpperCAmelCase ( __a : str ,__a : str ) -> None: """simple docstring""" _a : List[Any] = read_file_binary(__a ) _a : Any = remove_prefix(__a ) _a : Tuple = decompress_data(__a ) write_file_binary(__a ,__a ) if __name__ == "__main__": compress(sys.argv[1], sys.argv[2])
14
from __future__ import annotations def A ( lowercase__ : int ) -> list[int]: UpperCamelCase__ :Union[str, Any] = [True] * limit UpperCamelCase__ :int = False UpperCamelCase__ :Optional[Any] = False UpperCamelCase__ :str = True for i in range(3 , int(limit**0.5 + 1 ) , 2 ): UpperCamelCase__ :List[Any] = i * 2 while index < limit: UpperCamelCase__ :Tuple = False UpperCamelCase__ :Tuple = index + i UpperCamelCase__ :str = [2] for i in range(3 , lowercase__ , 2 ): if is_prime[i]: primes.append(lowercase__ ) return primes def A ( lowercase__ : int = 100_0000 ) -> int: UpperCamelCase__ :Any = prime_sieve(lowercase__ ) UpperCamelCase__ :Optional[int] = 0 UpperCamelCase__ :Optional[Any] = 0 for i in range(len(lowercase__ ) ): for j in range(i + length , len(lowercase__ ) ): UpperCamelCase__ :Any = sum(primes[i:j] ) if sol >= ceiling: break if sol in primes: UpperCamelCase__ :Union[str, Any] = j - i UpperCamelCase__ :Any = sol return largest if __name__ == "__main__": print(f'''{solution() = }''')
45
0
def UpperCamelCase ( __magic_name__ : list ) -> list: """simple docstring""" def merge(__magic_name__ : list , __magic_name__ : list ) -> list: def _merge(): while left and right: yield (left if left[0] <= right[0] else right).pop(0 ) yield from left yield from right return list(_merge() ) if len(__magic_name__ ) <= 1: return collection lowercase__ = len(__magic_name__ ) // 2 return merge(merge_sort(collection[:mid] ) , merge_sort(collection[mid:] ) ) if __name__ == "__main__": import doctest doctest.testmod() A : Any = input('Enter numbers separated by a comma:\n').strip() A : str = [int(item) for item in user_input.split(',')] print(*merge_sort(unsorted), sep=',')
15
import unittest from transformers import GPTNeoXJapaneseConfig, is_torch_available from transformers.models.gpt_neox_japanese.tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer from transformers.testing_utils import 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 GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseModel class lowerCAmelCase_ : """simple docstring""" def __init__( self :Optional[Any] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Tuple=13 , lowerCamelCase__ :Tuple=7 , lowerCamelCase__ :Optional[Any]=True , lowerCamelCase__ :Union[str, Any]=True , lowerCamelCase__ :Optional[int]=True , lowerCamelCase__ :List[Any]=True , lowerCamelCase__ :List[str]=99 , lowerCamelCase__ :int=32 , lowerCamelCase__ :List[Any]=5 , lowerCamelCase__ :Tuple=4 , lowerCamelCase__ :List[Any]=4 , lowerCamelCase__ :str="gelu" , lowerCamelCase__ :Optional[Any]=0.0 , lowerCamelCase__ :Optional[int]=0.1 , lowerCamelCase__ :str=True , lowerCamelCase__ :Dict=5_12 , lowerCamelCase__ :Optional[Any]=16 , lowerCamelCase__ :Optional[Any]=2 , lowerCamelCase__ :Union[str, Any]=0.02 , lowerCamelCase__ :Union[str, Any]=3 , lowerCamelCase__ :int=4 , lowerCamelCase__ :str=None , ): UpperCamelCase__ :Optional[Any] = parent UpperCamelCase__ :Dict = batch_size UpperCamelCase__ :Tuple = seq_length UpperCamelCase__ :Dict = is_training UpperCamelCase__ :List[str] = use_input_mask UpperCamelCase__ :Optional[Any] = use_token_type_ids UpperCamelCase__ :Tuple = use_labels UpperCamelCase__ :int = vocab_size UpperCamelCase__ :Tuple = hidden_size UpperCamelCase__ :Optional[Any] = num_hidden_layers UpperCamelCase__ :int = num_attention_heads UpperCamelCase__ :Optional[int] = intermediate_multiple_size UpperCamelCase__ :Optional[Any] = hidden_act UpperCamelCase__ :Optional[int] = hidden_dropout UpperCamelCase__ :List[Any] = attention_dropout UpperCamelCase__ :List[str] = weight_tying UpperCamelCase__ :List[str] = max_position_embeddings UpperCamelCase__ :Dict = type_vocab_size UpperCamelCase__ :List[Any] = type_sequence_label_size UpperCamelCase__ :List[str] = initializer_range UpperCamelCase__ :int = num_labels UpperCamelCase__ :Dict = num_choices UpperCamelCase__ :Any = scope def __a ( self :Any ): UpperCamelCase__ :Tuple = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCamelCase__ :str = None if self.use_input_mask: UpperCamelCase__ :Optional[int] = random_attention_mask([self.batch_size, self.seq_length] ) UpperCamelCase__ :Union[str, Any] = None if self.use_labels: UpperCamelCase__ :Tuple = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCamelCase__ :Optional[Any] = self.get_config() return config, input_ids, input_mask, token_labels def __a ( self :Union[str, Any] ): return GPTNeoXJapaneseConfig( 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_multiple_size=self.intermediate_multiple_size , hidden_act=self.hidden_act , hidden_dropout=self.hidden_dropout , attention_dropout=self.attention_dropout , weight_tying=self.weight_tying , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=lowerCamelCase__ , initializer_range=self.initializer_range , ) def __a ( self :Union[str, Any] ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Optional[Any] = self.prepare_config_and_inputs() UpperCamelCase__ :Optional[int] = True return config, input_ids, input_mask, token_labels def __a ( self :List[str] , lowerCamelCase__ :List[Any] , lowerCamelCase__ :Any , lowerCamelCase__ :Any ): UpperCamelCase__ :Union[str, Any] = GPTNeoXJapaneseModel(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() UpperCamelCase__ :Tuple = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = model(lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __a ( self :Dict , lowerCamelCase__ :Dict , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :List[Any] ): UpperCamelCase__ :List[str] = True UpperCamelCase__ :int = GPTNeoXJapaneseModel(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() UpperCamelCase__ :Union[str, Any] = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __a ( self :List[Any] , lowerCamelCase__ :Tuple , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Any , lowerCamelCase__ :Optional[Any] ): UpperCamelCase__ :Any = GPTNeoXJapaneseForCausalLM(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() UpperCamelCase__ :Tuple = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ , labels=lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __a ( self :Any , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :List[str] ): UpperCamelCase__ :Union[str, Any] = True UpperCamelCase__ :List[str] = GPTNeoXJapaneseForCausalLM(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() # first forward pass UpperCamelCase__ :Optional[Any] = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ , use_cache=lowerCamelCase__ ) UpperCamelCase__ :List[Any] = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids UpperCamelCase__ :List[Any] = ids_tensor((self.batch_size, 3) , config.vocab_size ) UpperCamelCase__ :Optional[Any] = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and UpperCamelCase__ :Union[str, Any] = torch.cat([input_ids, next_tokens] , dim=-1 ) UpperCamelCase__ :Optional[int] = torch.cat([input_mask, next_mask] , dim=-1 ) UpperCamelCase__ :Union[str, Any] = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ , output_hidden_states=lowerCamelCase__ ) UpperCamelCase__ :Optional[int] = output_from_no_past["""hidden_states"""][0] UpperCamelCase__ :Union[str, Any] = model( lowerCamelCase__ , attention_mask=lowerCamelCase__ , past_key_values=lowerCamelCase__ , output_hidden_states=lowerCamelCase__ , )["""hidden_states"""][0] # select random slice UpperCamelCase__ :int = ids_tensor((1,) , output_from_past.shape[-1] ).item() UpperCamelCase__ :str = output_from_no_past[:, -3:, random_slice_idx].detach() UpperCamelCase__ :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(lowerCamelCase__ , lowerCamelCase__ , atol=1e-3 ) ) def __a ( self :Tuple ): UpperCamelCase__ :int = self.prepare_config_and_inputs() UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :List[Any] = config_and_inputs UpperCamelCase__ :Any = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_torch class lowerCAmelCase_ ( lowercase , lowercase , unittest.TestCase ): """simple docstring""" _snake_case : Dict = (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else () _snake_case : int = (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else () _snake_case : str = ( {"""feature-extraction""": GPTNeoXJapaneseModel, """text-generation""": GPTNeoXJapaneseForCausalLM} if is_torch_available() else {} ) _snake_case : Union[str, Any] = False _snake_case : Dict = False _snake_case : List[str] = False _snake_case : Optional[int] = False def __a ( self :List[Any] ): UpperCamelCase__ :Tuple = GPTNeoXJapaneseModelTester(self ) UpperCamelCase__ :Optional[Any] = ConfigTester(self , config_class=lowerCamelCase__ , hidden_size=37 ) def __a ( self :Dict ): self.config_tester.run_common_tests() def __a ( self :Any ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Any ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Union[str, Any] ): # This regression test was failing with PyTorch < 1.3 UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :List[str] = self.model_tester.prepare_config_and_inputs_for_decoder() UpperCamelCase__ :Dict = None self.model_tester.create_and_check_model_as_decoder(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :List[str] ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Union[str, Any] ): UpperCamelCase__ :int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_causal_lm(*lowerCamelCase__ ) @slow def __a ( self :int ): UpperCamelCase__ :int = """abeja/gpt-neox-japanese-2.7b""" UpperCamelCase__ :List[Any] = ["""データサイエンティストとは、""", """100年後に必要とされる会社は、""", """フルリモートの環境で働くために必要なことは、""", """国境の長いトンネルを抜けると""", """美味しい日本食といえば、"""] UpperCamelCase__ :Union[str, Any] = [ """データサイエンティストとは、データを分析し、ビジネスに役立つ知見を導き出す専門家のことです。""", """100年後に必要とされる会社は、「人」が中心の会社です。""", """フルリモートの環境で働くために必要なことは、「自分の時間をコントロールする」ことです。""", """国境の長いトンネルを抜けると、そこは雪国だった。""", """美味しい日本食といえば、やっぱりお寿司ですよね。""", ] UpperCamelCase__ :Any = GPTNeoXJapaneseTokenizer.from_pretrained(lowerCamelCase__ ) UpperCamelCase__ :List[Any] = GPTNeoXJapaneseForCausalLM.from_pretrained(lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = [] for prompt in prompts: UpperCamelCase__ :str = tokenizer(lowerCamelCase__ , return_tensors="""pt""" ).input_ids UpperCamelCase__ :Union[str, Any] = model.generate(lowerCamelCase__ , max_length=50 ) UpperCamelCase__ :Dict = tokenizer.batch_decode(lowerCamelCase__ , skip_special_tokens=lowerCamelCase__ ) predicted_outputs += generated_string self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ )
45
0
from __future__ import annotations def __a ( A__ : list , A__ : int , A__ : int , A__ : int ): SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = input_list[low:mid], input_list[mid : high + 1] while left and right: result.append((left if left[0] <= right[0] else right).pop(0 ) ) SCREAMING_SNAKE_CASE = result + left + right return input_list def __a ( A__ : list ): if len(A__ ) <= 1: return input_list SCREAMING_SNAKE_CASE = list(A__ ) # iteration for two-way merging SCREAMING_SNAKE_CASE = 2 while p <= len(A__ ): # getting low, high and middle value for merge-sort of single list for i in range(0 , len(A__ ) , A__ ): SCREAMING_SNAKE_CASE = i SCREAMING_SNAKE_CASE = i + p - 1 SCREAMING_SNAKE_CASE = (low + high + 1) // 2 SCREAMING_SNAKE_CASE = merge(A__ , A__ , A__ , A__ ) # final merge of last two parts if p * 2 >= len(A__ ): SCREAMING_SNAKE_CASE = i SCREAMING_SNAKE_CASE = merge(A__ , 0 , A__ , len(A__ ) - 1 ) break p *= 2 return input_list if __name__ == "__main__": __A : Any = input('Enter numbers separated by a comma:\n').strip() if user_input == "": __A : Union[str, Any] = [] else: __A : Optional[Any] = [int(item.strip()) for item in user_input.split(',')] print(iter_merge_sort(unsorted))
16
import numpy as np from matplotlib import pyplot as plt from sklearn.datasets import load_iris from sklearn.metrics import ConfusionMatrixDisplay from sklearn.model_selection import train_test_split from xgboost import XGBClassifier def A ( lowercase__ : dict ) -> tuple: return (data["data"], data["target"]) def A ( lowercase__ : np.ndarray , lowercase__ : np.ndarray ) -> XGBClassifier: UpperCamelCase__ :Tuple = XGBClassifier() classifier.fit(lowercase__ , lowercase__ ) return classifier def A ( ) -> None: UpperCamelCase__ :str = load_iris() UpperCamelCase__ , UpperCamelCase__ :int = data_handling(lowercase__ ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :int = train_test_split( lowercase__ , lowercase__ , test_size=0.25 ) UpperCamelCase__ :Optional[int] = iris["""target_names"""] # Create an XGBoost Classifier from the training data UpperCamelCase__ :Optional[Any] = xgboost(lowercase__ , lowercase__ ) # Display the confusion matrix of the classifier with both training and test sets ConfusionMatrixDisplay.from_estimator( lowercase__ , lowercase__ , lowercase__ , display_labels=lowercase__ , cmap="""Blues""" , normalize="""true""" , ) plt.title("""Normalized Confusion Matrix - IRIS Dataset""" ) plt.show() if __name__ == "__main__": import doctest doctest.testmod(verbose=True) main()
45
0
import unittest import numpy as np from transformers import RobertaPreLayerNormConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.roberta_prelayernorm.modeling_flax_roberta_prelayernorm import ( FlaxRobertaPreLayerNormForCausalLM, FlaxRobertaPreLayerNormForMaskedLM, FlaxRobertaPreLayerNormForMultipleChoice, FlaxRobertaPreLayerNormForQuestionAnswering, FlaxRobertaPreLayerNormForSequenceClassification, FlaxRobertaPreLayerNormForTokenClassification, FlaxRobertaPreLayerNormModel, ) class lowerCamelCase_ ( unittest.TestCase ): def __init__( self : int , __A : int , __A : Optional[Any]=13 , __A : Any=7 , __A : Any=True , __A : List[Any]=True , __A : str=True , __A : int=True , __A : Optional[Any]=99 , __A : Dict=32 , __A : int=5 , __A : Optional[int]=4 , __A : Dict=37 , __A : List[str]="gelu" , __A : str=0.1 , __A : int=0.1 , __A : Optional[int]=512 , __A : Optional[int]=16 , __A : List[str]=2 , __A : List[Any]=0.0_2 , __A : int=4 , ): __A : Tuple = parent __A : Union[str, Any] = batch_size __A : int = seq_length __A : Any = is_training __A : Optional[int] = use_attention_mask __A : List[Any] = use_token_type_ids __A : Optional[int] = use_labels __A : Optional[Any] = vocab_size __A : Tuple = hidden_size __A : int = num_hidden_layers __A : List[Any] = num_attention_heads __A : Optional[Any] = intermediate_size __A : Optional[int] = hidden_act __A : Any = hidden_dropout_prob __A : List[Any] = attention_probs_dropout_prob __A : Any = max_position_embeddings __A : Tuple = type_vocab_size __A : List[str] = type_sequence_label_size __A : List[str] = initializer_range __A : List[Any] = num_choices def lowerCAmelCase_ ( self : List[Any] ): __A : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __A : Optional[Any] = None if self.use_attention_mask: __A : Optional[int] = random_attention_mask([self.batch_size, self.seq_length] ) __A : Any = None if self.use_token_type_ids: __A : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __A : Dict = RobertaPreLayerNormConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=__A , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def lowerCAmelCase_ ( self : int ): __A : Optional[int] = self.prepare_config_and_inputs() __A , __A , __A , __A : Tuple = config_and_inputs __A : str = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict def lowerCAmelCase_ ( self : Tuple ): __A : Tuple = self.prepare_config_and_inputs() __A , __A , __A , __A : List[Any] = config_and_inputs __A : Optional[int] = True __A : int = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) __A : List[str] = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, encoder_hidden_states, encoder_attention_mask, ) @require_flax # Copied from tests.models.roberta.test_modelling_flax_roberta.FlaxRobertaPreLayerNormModelTest with ROBERTA->ROBERTA_PRELAYERNORM,Roberta->RobertaPreLayerNorm,roberta-base->andreasmadsen/efficient_mlm_m0.40 class lowerCamelCase_ ( _lowercase , unittest.TestCase ): _lowercase : str = True _lowercase : Any = ( ( FlaxRobertaPreLayerNormModel, FlaxRobertaPreLayerNormForCausalLM, FlaxRobertaPreLayerNormForMaskedLM, FlaxRobertaPreLayerNormForSequenceClassification, FlaxRobertaPreLayerNormForTokenClassification, FlaxRobertaPreLayerNormForMultipleChoice, FlaxRobertaPreLayerNormForQuestionAnswering, ) if is_flax_available() else () ) def lowerCAmelCase_ ( self : List[str] ): __A : List[Any] = FlaxRobertaPreLayerNormModelTester(self ) @slow def lowerCAmelCase_ ( self : Any ): for model_class_name in self.all_model_classes: __A : List[Any] = model_class_name.from_pretrained("""andreasmadsen/efficient_mlm_m0.40""" , from_pt=__A ) __A : Dict = model(np.ones((1, 1) ) ) self.assertIsNotNone(__A ) @require_flax class lowerCamelCase_ ( unittest.TestCase ): @slow def lowerCAmelCase_ ( self : int ): __A : Dict = FlaxRobertaPreLayerNormForMaskedLM.from_pretrained("""andreasmadsen/efficient_mlm_m0.40""" , from_pt=__A ) __A : str = np.array([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]] , dtype=jnp.intaa ) __A : List[Any] = model(__A )[0] __A : str = [1, 11, 5_0265] self.assertEqual(list(output.shape ) , __A ) # compare the actual values for a slice. __A : int = np.array( [[[4_0.4_8_8_0, 1_8.0_1_9_9, -5.2_3_6_7], [-1.8_8_7_7, -4.0_8_8_5, 1_0.7_0_8_5], [-2.2_6_1_3, -5.6_1_1_0, 7.2_6_6_5]]] , dtype=np.floataa ) self.assertTrue(np.allclose(output[:, :3, :3] , __A , atol=1e-4 ) ) @slow def lowerCAmelCase_ ( self : Any ): __A : Union[str, Any] = FlaxRobertaPreLayerNormModel.from_pretrained("""andreasmadsen/efficient_mlm_m0.40""" , from_pt=__A ) __A : Dict = np.array([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]] , dtype=jnp.intaa ) __A : Optional[int] = model(__A )[0] # compare the actual values for a slice. __A : Optional[int] = np.array( [[[0.0_2_0_8, -0.0_3_5_6, 0.0_2_3_7], [-0.1_5_6_9, -0.0_4_1_1, -0.2_6_2_6], [0.1_8_7_9, 0.0_1_2_5, -0.0_0_8_9]]] , dtype=np.floataa ) self.assertTrue(np.allclose(output[:, :3, :3] , __A , atol=1e-4 ) )
17
import multiprocessing import time from arguments import PretokenizationArguments from datasets import load_dataset from transformers import AutoTokenizer, HfArgumentParser def A ( lowercase__ : Optional[int] ) -> Optional[Any]: UpperCamelCase__ :Union[str, Any] = {} UpperCamelCase__ :Optional[int] = tokenizer(example["""content"""] , truncation=lowercase__ )["""input_ids"""] UpperCamelCase__ :int = len(example["""content"""] ) / len(output["""input_ids"""] ) return output UpperCamelCase = HfArgumentParser(PretokenizationArguments) UpperCamelCase = parser.parse_args() if args.num_workers is None: UpperCamelCase = multiprocessing.cpu_count() UpperCamelCase = AutoTokenizer.from_pretrained(args.tokenizer_dir) UpperCamelCase = time.time() UpperCamelCase = load_dataset(args.dataset_name, split="train") print(f'''Dataset loaded in {time.time()-t_start:.2f}s''') UpperCamelCase = time.time() UpperCamelCase = ds.map( tokenize, num_proc=args.num_workers, remove_columns=[ "repo_name", "path", "copies", "size", "content", "license", "hash", "line_mean", "line_max", "alpha_frac", "autogenerated", ], ) print(f'''Dataset tokenized in {time.time()-t_start:.2f}s''') UpperCamelCase = time.time() ds.push_to_hub(args.tokenized_data_repo) print(f'''Data pushed to the hub in {time.time()-t_start:.2f}s''')
45
0
'''simple docstring''' def __a(SCREAMING_SNAKE_CASE_ : list , SCREAMING_SNAKE_CASE_ : int = 0 ): '''simple docstring''' _lowerCAmelCase = length or len(SCREAMING_SNAKE_CASE_ ) _lowerCAmelCase = False for i in range(length - 1 ): if list_data[i] > list_data[i + 1]: _lowerCAmelCase , _lowerCAmelCase = list_data[i + 1], list_data[i] _lowerCAmelCase = True return list_data if not swapped else bubble_sort(SCREAMING_SNAKE_CASE_ , length - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
18
def A ( lowercase__ : int ) -> Optional[Any]: stooge(lowercase__ , 0 , len(lowercase__ ) - 1 ) return arr def A ( lowercase__ : Union[str, Any] , lowercase__ : Dict , lowercase__ : str ) -> List[str]: if i >= h: return # If first element is smaller than the last then swap them if arr[i] > arr[h]: UpperCamelCase__ , UpperCamelCase__ :List[str] = arr[h], arr[i] # If there are more than 2 elements in the array if h - i + 1 > 2: UpperCamelCase__ :Optional[int] = (int)((h - i + 1) / 3 ) # Recursively sort first 2/3 elements stooge(lowercase__ , lowercase__ , (h - t) ) # Recursively sort last 2/3 elements stooge(lowercase__ , i + t , (lowercase__) ) # Recursively sort first 2/3 elements stooge(lowercase__ , lowercase__ , (h - t) ) if __name__ == "__main__": UpperCamelCase = input("Enter numbers separated by a comma:\n").strip() UpperCamelCase = [int(item) for item in user_input.split(",")] print(stooge_sort(unsorted))
45
0
"""simple docstring""" import re import warnings from contextlib import contextmanager from ...processing_utils import ProcessorMixin class _UpperCAmelCase( lowerCamelCase ): lowercase__ = ['image_processor', 'tokenizer'] lowercase__ = 'AutoImageProcessor' lowercase__ = 'AutoTokenizer' def __init__( self , __a=None , __a=None , **__a) -> Optional[Any]: '''simple docstring''' _UpperCamelCase = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , __a , ) _UpperCamelCase = kwargs.pop('''feature_extractor''') _UpperCamelCase = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''') if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''') super().__init__(__a , __a) _UpperCamelCase = self.image_processor _UpperCamelCase = False def __call__( self , *__a , **__a) -> Optional[int]: '''simple docstring''' # For backward compatibility if self._in_target_context_manager: return self.current_processor(*__a , **__a) _UpperCamelCase = kwargs.pop('''images''' , __a) _UpperCamelCase = kwargs.pop('''text''' , __a) if len(__a) > 0: _UpperCamelCase = args[0] _UpperCamelCase = args[1:] if images is None and text is None: raise ValueError('''You need to specify either an `images` or `text` input to process.''') if images is not None: _UpperCamelCase = self.image_processor(__a , *__a , **__a) if text is not None: _UpperCamelCase = self.tokenizer(__a , **__a) if text is None: return inputs elif images is None: return encodings else: _UpperCamelCase = encodings['''input_ids'''] return inputs def UpperCAmelCase ( self , *__a , **__a) -> List[Any]: '''simple docstring''' return self.tokenizer.batch_decode(*__a , **__a) def UpperCAmelCase ( self , *__a , **__a) -> List[str]: '''simple docstring''' return self.tokenizer.decode(*__a , **__a) @contextmanager def UpperCAmelCase ( self) -> Any: '''simple docstring''' warnings.warn( '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your ''' '''labels by using the argument `text` of the regular `__call__` method (either in the same call as ''' '''your images inputs, or in a separate call.''') _UpperCamelCase = True _UpperCamelCase = self.tokenizer yield _UpperCamelCase = self.image_processor _UpperCamelCase = False def UpperCAmelCase ( self , __a , __a=False , __a=None) -> Any: '''simple docstring''' if added_vocab is None: _UpperCamelCase = self.tokenizer.get_added_vocab() _UpperCamelCase = {} while tokens: _UpperCamelCase = re.search(R'''<s_(.*?)>''' , __a , re.IGNORECASE) if start_token is None: break _UpperCamelCase = start_token.group(1) _UpperCamelCase = re.search(RF'''</s_{key}>''' , __a , re.IGNORECASE) _UpperCamelCase = start_token.group() if end_token is None: _UpperCamelCase = tokens.replace(__a , '''''') else: _UpperCamelCase = end_token.group() _UpperCamelCase = re.escape(__a) _UpperCamelCase = re.escape(__a) _UpperCamelCase = re.search(F'''{start_token_escaped}(.*?){end_token_escaped}''' , __a , re.IGNORECASE) if content is not None: _UpperCamelCase = content.group(1).strip() if r"<s_" in content and r"</s_" in content: # non-leaf node _UpperCamelCase = self.tokenajson(__a , is_inner_value=__a , added_vocab=__a) if value: if len(__a) == 1: _UpperCamelCase = value[0] _UpperCamelCase = value else: # leaf nodes _UpperCamelCase = [] for leaf in content.split(R'''<sep/>'''): _UpperCamelCase = leaf.strip() if leaf in added_vocab and leaf[0] == "<" and leaf[-2:] == "/>": _UpperCamelCase = leaf[1:-2] # for categorical special tokens output[key].append(__a) if len(output[key]) == 1: _UpperCamelCase = output[key][0] _UpperCamelCase = tokens[tokens.find(__a) + len(__a) :].strip() if tokens[:6] == r"<sep/>": # non-leaf nodes return [output] + self.tokenajson(tokens[6:] , is_inner_value=__a , added_vocab=__a) if len(__a): return [output] if is_inner_value else output else: return [] if is_inner_value else {"text_sequence": tokens} @property def UpperCAmelCase ( self) -> Tuple: '''simple docstring''' warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , __a , ) return self.image_processor_class @property def UpperCAmelCase ( self) -> Tuple: '''simple docstring''' warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , __a , ) return self.image_processor
19
import argparse import json import os from tensorflow.core.protobuf.saved_model_pba import SavedModel # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_copies.py UpperCamelCase = "." # Internal TensorFlow ops that can be safely ignored (mostly specific to a saved model) UpperCamelCase = [ "Assert", "AssignVariableOp", "EmptyTensorList", "MergeV2Checkpoints", "ReadVariableOp", "ResourceGather", "RestoreV2", "SaveV2", "ShardedFilename", "StatefulPartitionedCall", "StaticRegexFullMatch", "VarHandleOp", ] def A ( lowercase__ : Tuple , lowercase__ : Optional[Any] , lowercase__ : Dict ) -> List[Any]: UpperCamelCase__ :str = SavedModel() UpperCamelCase__ :List[str] = [] with open(os.path.join(lowercase__ , """utils""" , """tf_ops""" , """onnx.json""" ) ) as f: UpperCamelCase__ :str = json.load(lowercase__ )["""opsets"""] for i in range(1 , opset + 1 ): onnx_ops.extend(onnx_opsets[str(lowercase__ )] ) with open(lowercase__ , """rb""" ) as f: saved_model.ParseFromString(f.read() ) UpperCamelCase__ :Tuple = set() # Iterate over every metagraph in case there is more than one (a saved model can contain multiple graphs) for meta_graph in saved_model.meta_graphs: # Add operations in the graph definition model_op_names.update(node.op for node in meta_graph.graph_def.node ) # Go through the functions in the graph definition for func in meta_graph.graph_def.library.function: # Add operations in each function model_op_names.update(node.op for node in func.node_def ) # Convert to list, sorted if you want UpperCamelCase__ :Union[str, Any] = sorted(lowercase__ ) UpperCamelCase__ :List[Any] = [] for op in model_op_names: if op not in onnx_ops and op not in INTERNAL_OPS: incompatible_ops.append(lowercase__ ) if strict and len(lowercase__ ) > 0: raise Exception(f"""Found the following incompatible ops for the opset {opset}:\n""" + incompatible_ops ) elif len(lowercase__ ) > 0: print(f"""Found the following incompatible ops for the opset {opset}:""" ) print(*lowercase__ , sep="""\n""" ) else: print(f"""The saved model {saved_model_path} can properly be converted with ONNX.""" ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() parser.add_argument("--saved_model_path", help="Path of the saved model to check (the .pb file).") parser.add_argument( "--opset", default=12, type=int, help="The ONNX opset against which the model has to be tested." ) parser.add_argument( "--framework", choices=["onnx"], default="onnx", help="Frameworks against which to test the saved model." ) parser.add_argument( "--strict", action="store_true", help="Whether make the checking strict (raise errors) or not (raise warnings)" ) UpperCamelCase = parser.parse_args() if args.framework == "onnx": onnx_compliancy(args.saved_model_path, args.strict, args.opset)
45
0
import inspect import unittest import numpy as np from tests.test_modeling_common import floats_tensor from transformers import MaskaFormerConfig, is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MaskaFormerForUniversalSegmentation, MaskaFormerModel if is_vision_available(): from transformers import MaskaFormerImageProcessor if is_vision_available(): from PIL import Image class lowercase_ : def __init__( self , lowercase_ , lowercase_=2 , lowercase_=True , lowercase_=False , lowercase_=10 , lowercase_=3 , lowercase_=32 * 8 , lowercase_=32 * 8 , lowercase_=4 , lowercase_=64 , ) -> Union[str, Any]: a__ =parent a__ =batch_size a__ =is_training a__ =use_auxiliary_loss a__ =num_queries a__ =num_channels a__ =min_size a__ =max_size a__ =num_labels a__ =hidden_dim a__ =hidden_dim def __UpperCamelCase ( self) -> int: a__ =floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]).to( lowercase_) a__ =torch.ones([self.batch_size, self.min_size, self.max_size] , device=lowercase_) a__ =( torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size] , device=lowercase_) > 0.5 ).float() a__ =(torch.rand((self.batch_size, self.num_labels) , device=lowercase_) > 0.5).long() a__ =self.get_config() return config, pixel_values, pixel_mask, mask_labels, class_labels def __UpperCamelCase ( self) -> List[Any]: a__ =MaskaFormerConfig( hidden_size=self.hidden_dim , ) a__ =self.num_queries a__ =self.num_labels a__ =[1, 1, 1, 1] a__ =self.num_channels a__ =64 a__ =128 a__ =self.hidden_dim a__ =self.hidden_dim a__ =self.hidden_dim return config def __UpperCamelCase ( self) -> str: a__ , a__ , a__ , a__ , a__ =self.prepare_config_and_inputs() a__ ={'pixel_values': pixel_values, 'pixel_mask': pixel_mask} return config, inputs_dict def __UpperCamelCase ( self , lowercase_ , lowercase_) -> Union[str, Any]: a__ =output.encoder_hidden_states a__ =output.pixel_decoder_hidden_states a__ =output.transformer_decoder_hidden_states self.parent.assertTrue(len(lowercase_) , len(config.backbone_config.depths)) self.parent.assertTrue(len(lowercase_) , len(config.backbone_config.depths)) self.parent.assertTrue(len(lowercase_) , config.decoder_layers) def __UpperCamelCase ( self , lowercase_ , lowercase_ , lowercase_ , lowercase_=False) -> Any: with torch.no_grad(): a__ =MaskaFormerModel(config=lowercase_) model.to(lowercase_) model.eval() a__ =model(pixel_values=lowercase_ , pixel_mask=lowercase_) a__ =model(lowercase_ , output_hidden_states=lowercase_) self.parent.assertEqual( output.transformer_decoder_last_hidden_state.shape , (self.batch_size, self.num_queries, self.hidden_dim) , ) # let's ensure the other two hidden state exists self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None) self.parent.assertTrue(output.encoder_last_hidden_state is not None) if output_hidden_states: self.check_output_hidden_state(lowercase_ , lowercase_) def __UpperCamelCase ( self , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_) -> Any: a__ =MaskaFormerForUniversalSegmentation(config=lowercase_) model.to(lowercase_) model.eval() def comm_check_on_output(lowercase_): # let's still check that all the required stuff is there self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None) self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None) self.parent.assertTrue(result.encoder_last_hidden_state is not None) # okay, now we need to check the logits shape # due to the encoder compression, masks have a //4 spatial size self.parent.assertEqual( result.masks_queries_logits.shape , (self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4) , ) # + 1 for null class self.parent.assertEqual( result.class_queries_logits.shape , (self.batch_size, self.num_queries, self.num_labels + 1)) with torch.no_grad(): a__ =model(pixel_values=lowercase_ , pixel_mask=lowercase_) a__ =model(lowercase_) comm_check_on_output(lowercase_) a__ =model( pixel_values=lowercase_ , pixel_mask=lowercase_ , mask_labels=lowercase_ , class_labels=lowercase_) comm_check_on_output(lowercase_) self.parent.assertTrue(result.loss is not None) self.parent.assertEqual(result.loss.shape , torch.Size([1])) @require_torch class lowercase_ (lowercase__ , lowercase__ , unittest.TestCase ): snake_case =(MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else () snake_case ={'feature-extraction': MaskaFormerModel} if is_torch_available() else {} snake_case =False snake_case =False snake_case =False snake_case =False def __UpperCamelCase ( self) -> Optional[int]: a__ =MaskaFormerModelTester(self) a__ =ConfigTester(self , config_class=lowercase_ , has_text_modality=lowercase_) def __UpperCamelCase ( self) -> Union[str, Any]: self.config_tester.run_common_tests() def __UpperCamelCase ( self) -> List[str]: a__ , a__ =self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskaformer_model(lowercase_ , **lowercase_ , output_hidden_states=lowercase_) def __UpperCamelCase ( self) -> List[str]: a__ =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*lowercase_) @unittest.skip(reason='Mask2Former does not use inputs_embeds') def __UpperCamelCase ( self) -> str: pass @unittest.skip(reason='Mask2Former does not have a get_input_embeddings method') def __UpperCamelCase ( self) -> Tuple: pass @unittest.skip(reason='Mask2Former is not a generative model') def __UpperCamelCase ( self) -> List[str]: pass @unittest.skip(reason='Mask2Former does not use token embeddings') def __UpperCamelCase ( self) -> Tuple: pass @require_torch_multi_gpu @unittest.skip( reason='Mask2Former has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`') def __UpperCamelCase ( self) -> Union[str, Any]: pass @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.') def __UpperCamelCase ( self) -> str: pass def __UpperCamelCase ( self) -> Optional[Any]: a__ , a__ =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a__ =model_class(lowercase_) a__ =inspect.signature(model.forward) # signature.parameters is an OrderedDict => so arg_names order is deterministic a__ =[*signature.parameters.keys()] a__ =['pixel_values'] self.assertListEqual(arg_names[:1] , lowercase_) @slow def __UpperCamelCase ( self) -> Any: for model_name in ["facebook/mask2former-swin-small-coco-instance"]: a__ =MaskaFormerModel.from_pretrained(lowercase_) self.assertIsNotNone(lowercase_) def __UpperCamelCase ( self) -> Dict: a__ =(self.model_tester.min_size,) * 2 a__ ={ 'pixel_values': torch.randn((2, 3, *size) , device=lowercase_), 'mask_labels': torch.randn((2, 10, *size) , device=lowercase_), 'class_labels': torch.zeros(2 , 10 , device=lowercase_).long(), } a__ =self.model_tester.get_config() a__ =MaskaFormerForUniversalSegmentation(lowercase_).to(lowercase_) a__ =model(**lowercase_) self.assertTrue(outputs.loss is not None) def __UpperCamelCase ( self) -> Tuple: a__ , a__ =self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskaformer_model(lowercase_ , **lowercase_ , output_hidden_states=lowercase_) def __UpperCamelCase ( self) -> int: a__ , a__ =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a__ =model_class(lowercase_).to(lowercase_) a__ =model(**lowercase_ , output_attentions=lowercase_) self.assertTrue(outputs.attentions is not None) def __UpperCamelCase ( self) -> Union[str, Any]: if not self.model_tester.is_training: return a__ =self.all_model_classes[1] a__ , a__ , a__ , a__ , a__ =self.model_tester.prepare_config_and_inputs() a__ =model_class(lowercase_) model.to(lowercase_) model.train() a__ =model(lowercase_ , mask_labels=lowercase_ , class_labels=lowercase_).loss loss.backward() def __UpperCamelCase ( self) -> Union[str, Any]: a__ =self.all_model_classes[1] a__ , a__ , a__ , a__ , a__ =self.model_tester.prepare_config_and_inputs() a__ =True a__ =True a__ =model_class(lowercase_).to(lowercase_) model.train() a__ =model(lowercase_ , mask_labels=lowercase_ , class_labels=lowercase_) a__ =outputs.encoder_hidden_states[0] encoder_hidden_states.retain_grad() a__ =outputs.pixel_decoder_hidden_states[0] pixel_decoder_hidden_states.retain_grad() a__ =outputs.transformer_decoder_hidden_states[0] transformer_decoder_hidden_states.retain_grad() a__ =outputs.attentions[0] attentions.retain_grad() outputs.loss.backward(retain_graph=lowercase_) self.assertIsNotNone(encoder_hidden_states.grad) self.assertIsNotNone(pixel_decoder_hidden_states.grad) self.assertIsNotNone(transformer_decoder_hidden_states.grad) self.assertIsNotNone(attentions.grad) _lowerCAmelCase: str = 1e-4 def _lowercase( ): a__ =Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_vision @slow class lowercase_ (unittest.TestCase ): @cached_property def __UpperCamelCase ( self) -> Tuple: return "facebook/mask2former-swin-small-coco-instance" @cached_property def __UpperCamelCase ( self) -> Optional[Any]: return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints) if is_vision_available() else None def __UpperCamelCase ( self) -> str: a__ =MaskaFormerModel.from_pretrained(self.model_checkpoints).to(lowercase_) a__ =self.default_image_processor a__ =prepare_img() a__ =image_processor(lowercase_ , return_tensors='pt').to(lowercase_) a__ =inputs['pixel_values'].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0) # check size self.assertEqual(lowercase_ , (1, 3, 384, 384)) with torch.no_grad(): a__ =model(**lowercase_) a__ =torch.tensor( [[-0.27_90, -1.07_17, -1.16_68], [-0.51_28, -0.31_28, -0.49_87], [-0.58_32, 0.19_71, -0.01_97]]).to(lowercase_) self.assertTrue( torch.allclose( outputs.encoder_last_hidden_state[0, 0, :3, :3] , lowercase_ , atol=lowercase_)) a__ =torch.tensor( [[0.89_73, 1.18_47, 1.17_76], [1.19_34, 1.50_40, 1.51_28], [1.11_53, 1.44_86, 1.49_51]]).to(lowercase_) self.assertTrue( torch.allclose( outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3] , lowercase_ , atol=lowercase_)) a__ =torch.tensor( [[2.11_52, 1.70_00, -0.86_03], [1.58_08, 1.80_04, -0.93_53], [1.60_43, 1.74_95, -0.59_99]]).to(lowercase_) self.assertTrue( torch.allclose( outputs.transformer_decoder_last_hidden_state[0, :3, :3] , lowercase_ , atol=lowercase_)) def __UpperCamelCase ( self) -> Any: a__ =MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints).to(lowercase_).eval() a__ =self.default_image_processor a__ =prepare_img() a__ =image_processor(lowercase_ , return_tensors='pt').to(lowercase_) a__ =inputs['pixel_values'].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0) # check size self.assertEqual(lowercase_ , (1, 3, 384, 384)) with torch.no_grad(): a__ =model(**lowercase_) # masks_queries_logits a__ =outputs.masks_queries_logits self.assertEqual( masks_queries_logits.shape , (1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)) a__ =[ [-8.78_39, -9.00_56, -8.81_21], [-7.41_04, -7.03_13, -6.54_01], [-6.61_05, -6.34_27, -6.46_75], ] a__ =torch.tensor(lowercase_).to(lowercase_) self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3] , lowercase_ , atol=lowercase_)) # class_queries_logits a__ =outputs.class_queries_logits self.assertEqual(class_queries_logits.shape , (1, model.config.num_queries, model.config.num_labels + 1)) a__ =torch.tensor( [ [1.83_24, -8.08_35, -4.19_22], [0.84_50, -9.00_50, -3.60_53], [0.30_45, -7.72_93, -3.02_75], ]).to(lowercase_) self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3] , lowercase_ , atol=lowercase_)) def __UpperCamelCase ( self) -> Optional[Any]: a__ =MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints).to(lowercase_).eval() a__ =self.default_image_processor a__ =image_processor( [np.zeros((3, 800, 1333)), np.zeros((3, 800, 1333))] , segmentation_maps=[np.zeros((384, 384)).astype(np.floataa), np.zeros((384, 384)).astype(np.floataa)] , return_tensors='pt' , ) a__ =inputs['pixel_values'].to(lowercase_) a__ =[el.to(lowercase_) for el in inputs['mask_labels']] a__ =[el.to(lowercase_) for el in inputs['class_labels']] with torch.no_grad(): a__ =model(**lowercase_) self.assertTrue(outputs.loss is not None)
20
from __future__ import annotations def A ( lowercase__ : str , lowercase__ : list[str] | None = None , lowercase__ : dict[str, float] | None = None , lowercase__ : bool = False , ) -> tuple[int, float, str]: UpperCamelCase__ :Dict = cipher_alphabet or [chr(lowercase__ ) for i in range(97 , 123 )] # If the argument is None or the user provided an empty dictionary if not frequencies_dict: # Frequencies of letters in the english language (how much they show up) UpperCamelCase__ :Optional[Any] = { """a""": 0.08497, """b""": 0.01492, """c""": 0.02202, """d""": 0.04253, """e""": 0.11162, """f""": 0.02228, """g""": 0.02015, """h""": 0.06094, """i""": 0.07546, """j""": 0.00153, """k""": 0.01292, """l""": 0.04025, """m""": 0.02406, """n""": 0.06749, """o""": 0.07507, """p""": 0.01929, """q""": 0.00095, """r""": 0.07587, """s""": 0.06327, """t""": 0.09356, """u""": 0.02758, """v""": 0.00978, """w""": 0.02560, """x""": 0.00150, """y""": 0.01994, """z""": 0.00077, } else: # Custom frequencies dictionary UpperCamelCase__ :Optional[int] = frequencies_dict if not case_sensitive: UpperCamelCase__ :int = ciphertext.lower() # Chi squared statistic values UpperCamelCase__ :dict[int, tuple[float, str]] = {} # cycle through all of the shifts for shift in range(len(lowercase__ ) ): UpperCamelCase__ :int = """""" # decrypt the message with the shift for letter in ciphertext: try: # Try to index the letter in the alphabet UpperCamelCase__ :int = (alphabet_letters.index(letter.lower() ) - shift) % len( lowercase__ ) decrypted_with_shift += ( alphabet_letters[new_key].upper() if case_sensitive and letter.isupper() else alphabet_letters[new_key] ) except ValueError: # Append the character if it isn't in the alphabet decrypted_with_shift += letter UpperCamelCase__ :Optional[int] = 0.0 # Loop through each letter in the decoded message with the shift for letter in decrypted_with_shift: if case_sensitive: UpperCamelCase__ :Optional[int] = letter.lower() if letter in frequencies: # Get the amount of times the letter occurs in the message UpperCamelCase__ :Optional[int] = decrypted_with_shift.lower().count(lowercase__ ) # Get the excepcted amount of times the letter should appear based # on letter frequencies UpperCamelCase__ :Optional[int] = frequencies[letter] * occurrences # Complete the chi squared statistic formula UpperCamelCase__ :Dict = ((occurrences - expected) ** 2) / expected # Add the margin of error to the total chi squared statistic chi_squared_statistic += chi_letter_value else: if letter.lower() in frequencies: # Get the amount of times the letter occurs in the message UpperCamelCase__ :List[str] = decrypted_with_shift.count(lowercase__ ) # Get the excepcted amount of times the letter should appear based # on letter frequencies UpperCamelCase__ :Union[str, Any] = frequencies[letter] * occurrences # Complete the chi squared statistic formula UpperCamelCase__ :List[str] = ((occurrences - expected) ** 2) / expected # Add the margin of error to the total chi squared statistic chi_squared_statistic += chi_letter_value # Add the data to the chi_squared_statistic_values dictionary UpperCamelCase__ :Union[str, Any] = ( chi_squared_statistic, decrypted_with_shift, ) # Get the most likely cipher by finding the cipher with the smallest chi squared # statistic def chi_squared_statistic_values_sorting_key(lowercase__ : int ) -> tuple[float, str]: return chi_squared_statistic_values[key] UpperCamelCase__ :int = min( lowercase__ , key=lowercase__ , ) # Get all the data from the most likely cipher (key, decoded message) ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :Tuple = chi_squared_statistic_values[most_likely_cipher] # Return the data on the most likely shift return ( most_likely_cipher, most_likely_cipher_chi_squared_value, decoded_most_likely_cipher, )
45
0
import os import tempfile import unittest from pathlib import Path from transformers import AutoConfig, is_tf_available from transformers.testing_utils import require_tf if is_tf_available(): import tensorflow as tf from transformers import TensorFlowBenchmark, TensorFlowBenchmarkArguments @require_tf class __A ( unittest.TestCase ): def A__ ( self :List[str] , __snake_case :List[str] ): '''simple docstring''' for model_result in results.values(): for batch_size, sequence_length in zip(model_result["""bs"""] , model_result["""ss"""] ): __magic_name__ : str =model_result["""result"""][batch_size][sequence_length] self.assertIsNotNone(__snake_case ) def A__ ( self :List[str] ): '''simple docstring''' __magic_name__ : Dict ="""sshleifer/tiny-gpt2""" __magic_name__ : Optional[int] =TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=__snake_case , inference=__snake_case , sequence_lengths=[8] , batch_sizes=[1] , eager_mode=__snake_case , multi_process=__snake_case , ) __magic_name__ : List[str] =TensorFlowBenchmark(__snake_case ) __magic_name__ : List[Any] =benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def A__ ( self :Optional[Any] ): '''simple docstring''' __magic_name__ : Optional[int] ="""sgugger/tiny-distilbert-classification""" __magic_name__ : List[str] =TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=__snake_case , inference=__snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__snake_case , only_pretrain_model=__snake_case , ) __magic_name__ : Dict =TensorFlowBenchmark(__snake_case ) __magic_name__ : List[Any] =benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def A__ ( self :Optional[Any] ): '''simple docstring''' __magic_name__ : str ="""sshleifer/tiny-gpt2""" __magic_name__ : List[Any] =TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=__snake_case , inference=__snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__snake_case , ) __magic_name__ : int =TensorFlowBenchmark(__snake_case ) __magic_name__ : List[Any] =benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def A__ ( self :Optional[int] ): '''simple docstring''' __magic_name__ : int ="""sshleifer/tiny-gpt2""" __magic_name__ : Optional[int] =AutoConfig.from_pretrained(__snake_case ) __magic_name__ : Optional[int] =TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=__snake_case , inference=__snake_case , sequence_lengths=[8] , batch_sizes=[1] , eager_mode=__snake_case , multi_process=__snake_case , ) __magic_name__ : str =TensorFlowBenchmark(__snake_case , [config] ) __magic_name__ : List[Any] =benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def A__ ( self :Union[str, Any] ): '''simple docstring''' __magic_name__ : List[Any] ="""sshleifer/tiny-gpt2""" __magic_name__ : Tuple =AutoConfig.from_pretrained(__snake_case ) __magic_name__ : Dict =TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=__snake_case , inference=__snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__snake_case , ) __magic_name__ : Union[str, Any] =TensorFlowBenchmark(__snake_case , [config] ) __magic_name__ : str =benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def A__ ( self :Union[str, Any] ): '''simple docstring''' __magic_name__ : Optional[int] ="""sshleifer/tiny-gpt2""" __magic_name__ : Optional[int] =TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=__snake_case , inference=__snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__snake_case , ) __magic_name__ : Optional[Any] =TensorFlowBenchmark(__snake_case ) __magic_name__ : Any =benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) def A__ ( self :Optional[Any] ): '''simple docstring''' __magic_name__ : Optional[Any] ="""sshleifer/tiny-gpt2""" __magic_name__ : Tuple =AutoConfig.from_pretrained(__snake_case ) __magic_name__ : str =TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=__snake_case , inference=__snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__snake_case , ) __magic_name__ : Optional[Any] =TensorFlowBenchmark(__snake_case , [config] ) __magic_name__ : int =benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) def A__ ( self :Optional[int] ): '''simple docstring''' __magic_name__ : List[str] ="""patrickvonplaten/t5-tiny-random""" __magic_name__ : Optional[Any] =AutoConfig.from_pretrained(__snake_case ) __magic_name__ : str =TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=__snake_case , inference=__snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__snake_case , ) __magic_name__ : List[Any] =TensorFlowBenchmark(__snake_case , configs=[config] ) __magic_name__ : Union[str, Any] =benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) @unittest.skipIf(is_tf_available() and len(tf.config.list_physical_devices("""GPU""" ) ) == 0 , """Cannot do xla on CPU.""" ) def A__ ( self :Union[str, Any] ): '''simple docstring''' __magic_name__ : List[Any] ="""sshleifer/tiny-gpt2""" __magic_name__ : List[Any] =TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=__snake_case , inference=__snake_case , sequence_lengths=[8] , batch_sizes=[1] , use_xla=__snake_case , multi_process=__snake_case , ) __magic_name__ : List[str] =TensorFlowBenchmark(__snake_case ) __magic_name__ : Optional[Any] =benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def A__ ( self :Any ): '''simple docstring''' __magic_name__ : int ="""sshleifer/tiny-gpt2""" with tempfile.TemporaryDirectory() as tmp_dir: __magic_name__ : Tuple =TensorFlowBenchmarkArguments( models=[MODEL_ID] , inference=__snake_case , save_to_csv=__snake_case , sequence_lengths=[8] , batch_sizes=[1] , inference_time_csv_file=os.path.join(__snake_case , """inf_time.csv""" ) , inference_memory_csv_file=os.path.join(__snake_case , """inf_mem.csv""" ) , env_info_csv_file=os.path.join(__snake_case , """env.csv""" ) , multi_process=__snake_case , ) __magic_name__ : Optional[Any] =TensorFlowBenchmark(__snake_case ) benchmark.run() self.assertTrue(Path(os.path.join(__snake_case , """inf_time.csv""" ) ).exists() ) self.assertTrue(Path(os.path.join(__snake_case , """inf_mem.csv""" ) ).exists() ) self.assertTrue(Path(os.path.join(__snake_case , """env.csv""" ) ).exists() ) def A__ ( self :str ): '''simple docstring''' __magic_name__ : int ="""sshleifer/tiny-gpt2""" def _check_summary_is_not_empty(__snake_case :Dict ): self.assertTrue(hasattr(__snake_case , """sequential""" ) ) self.assertTrue(hasattr(__snake_case , """cumulative""" ) ) self.assertTrue(hasattr(__snake_case , """current""" ) ) self.assertTrue(hasattr(__snake_case , """total""" ) ) with tempfile.TemporaryDirectory() as tmp_dir: __magic_name__ : List[Any] =TensorFlowBenchmarkArguments( models=[MODEL_ID] , inference=__snake_case , sequence_lengths=[8] , batch_sizes=[1] , log_filename=os.path.join(__snake_case , """log.txt""" ) , log_print=__snake_case , trace_memory_line_by_line=__snake_case , eager_mode=__snake_case , multi_process=__snake_case , ) __magic_name__ : List[Any] =TensorFlowBenchmark(__snake_case ) __magic_name__ : Optional[int] =benchmark.run() _check_summary_is_not_empty(result.inference_summary ) self.assertTrue(Path(os.path.join(__snake_case , """log.txt""" ) ).exists() )
21
import warnings from ...utils import logging from .image_processing_mobilevit import MobileViTImageProcessor UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( lowercase ): """simple docstring""" def __init__( self :Union[str, Any] , *lowerCamelCase__ :Optional[int] , **lowerCamelCase__ :Dict ): warnings.warn( """The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.""" """ Please use MobileViTImageProcessor instead.""" , lowerCamelCase__ , ) super().__init__(*lowerCamelCase__ , **lowerCamelCase__ )
45
0
'''simple docstring''' from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, convert_to_rgb, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging _snake_case : List[str] = logging.get_logger(__name__) if is_vision_available(): import PIL class A ( _a ): lowercase_ = ['pixel_values'] def __init__( self : Tuple , lowerCAmelCase_ : bool = True , lowerCAmelCase_ : Dict[str, int] = None , lowerCAmelCase_ : PILImageResampling = PILImageResampling.BICUBIC , lowerCAmelCase_ : bool = True , lowerCAmelCase_ : Dict[str, int] = None , lowerCAmelCase_ : bool = True , lowerCAmelCase_ : Union[int, float] = 1 / 2_55 , lowerCAmelCase_ : bool = True , lowerCAmelCase_ : Optional[Union[float, List[float]]] = None , lowerCAmelCase_ : Optional[Union[float, List[float]]] = None , lowerCAmelCase_ : bool = True , **lowerCAmelCase_ : Dict , ) -> None: """simple docstring""" super().__init__(**lowerCAmelCase_ ) _a = size if size is not None else {'''shortest_edge''': 2_24} _a = get_size_dict(lowerCAmelCase_ , default_to_square=lowerCAmelCase_ ) _a = crop_size if crop_size is not None else {'''height''': 2_24, '''width''': 2_24} _a = get_size_dict(lowerCAmelCase_ , default_to_square=lowerCAmelCase_ , param_name='''crop_size''' ) _a = do_resize _a = size _a = resample _a = do_center_crop _a = crop_size _a = do_rescale _a = rescale_factor _a = do_normalize _a = image_mean if image_mean is not None else OPENAI_CLIP_MEAN _a = image_std if image_std is not None else OPENAI_CLIP_STD _a = do_convert_rgb def __lowerCAmelCase ( self : List[Any] , lowerCAmelCase_ : np.ndarray , lowerCAmelCase_ : Dict[str, int] , lowerCAmelCase_ : PILImageResampling = PILImageResampling.BICUBIC , lowerCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **lowerCAmelCase_ : Optional[Any] , ) -> np.ndarray: """simple docstring""" _a = get_size_dict(lowerCAmelCase_ , default_to_square=lowerCAmelCase_ ) if "shortest_edge" not in size: raise ValueError(F'The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}' ) _a = get_resize_output_image_size(lowerCAmelCase_ , size=size['''shortest_edge'''] , default_to_square=lowerCAmelCase_ ) return resize(lowerCAmelCase_ , size=lowerCAmelCase_ , resample=lowerCAmelCase_ , data_format=lowerCAmelCase_ , **lowerCAmelCase_ ) def __lowerCAmelCase ( self : List[Any] , lowerCAmelCase_ : np.ndarray , lowerCAmelCase_ : Dict[str, int] , lowerCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **lowerCAmelCase_ : Any , ) -> np.ndarray: """simple docstring""" _a = get_size_dict(lowerCAmelCase_ ) if "height" not in size or "width" not in size: raise ValueError(F'The `size` parameter must contain the keys (height, width). Got {size.keys()}' ) return center_crop(lowerCAmelCase_ , size=(size['''height'''], size['''width''']) , data_format=lowerCAmelCase_ , **lowerCAmelCase_ ) def __lowerCAmelCase ( self : List[str] , lowerCAmelCase_ : np.ndarray , lowerCAmelCase_ : Union[int, float] , lowerCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **lowerCAmelCase_ : Tuple , ) -> Optional[int]: """simple docstring""" return rescale(lowerCAmelCase_ , scale=lowerCAmelCase_ , data_format=lowerCAmelCase_ , **lowerCAmelCase_ ) def __lowerCAmelCase ( self : Dict , lowerCAmelCase_ : np.ndarray , lowerCAmelCase_ : Union[float, List[float]] , lowerCAmelCase_ : Union[float, List[float]] , lowerCAmelCase_ : Optional[Union[str, ChannelDimension]] = None , **lowerCAmelCase_ : Tuple , ) -> np.ndarray: """simple docstring""" return normalize(lowerCAmelCase_ , mean=lowerCAmelCase_ , std=lowerCAmelCase_ , data_format=lowerCAmelCase_ , **lowerCAmelCase_ ) def __lowerCAmelCase ( self : List[str] , lowerCAmelCase_ : ImageInput , lowerCAmelCase_ : bool = None , lowerCAmelCase_ : Dict[str, int] = None , lowerCAmelCase_ : PILImageResampling = None , lowerCAmelCase_ : bool = None , lowerCAmelCase_ : int = None , lowerCAmelCase_ : bool = None , lowerCAmelCase_ : float = None , lowerCAmelCase_ : bool = None , lowerCAmelCase_ : Optional[Union[float, List[float]]] = None , lowerCAmelCase_ : Optional[Union[float, List[float]]] = None , lowerCAmelCase_ : bool = None , lowerCAmelCase_ : Optional[Union[str, TensorType]] = None , lowerCAmelCase_ : Optional[ChannelDimension] = ChannelDimension.FIRST , **lowerCAmelCase_ : Any , ) -> PIL.Image.Image: """simple docstring""" _a = do_resize if do_resize is not None else self.do_resize _a = size if size is not None else self.size _a = get_size_dict(lowerCAmelCase_ , param_name='''size''' , default_to_square=lowerCAmelCase_ ) _a = resample if resample is not None else self.resample _a = do_center_crop if do_center_crop is not None else self.do_center_crop _a = crop_size if crop_size is not None else self.crop_size _a = get_size_dict(lowerCAmelCase_ , param_name='''crop_size''' , default_to_square=lowerCAmelCase_ ) _a = do_rescale if do_rescale is not None else self.do_rescale _a = rescale_factor if rescale_factor is not None else self.rescale_factor _a = do_normalize if do_normalize is not None else self.do_normalize _a = image_mean if image_mean is not None else self.image_mean _a = image_std if image_std is not None else self.image_std _a = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb _a = make_list_of_images(lowerCAmelCase_ ) if not valid_images(lowerCAmelCase_ ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None: raise ValueError('''Size must be specified if do_resize is True.''' ) if do_center_crop and crop_size is None: raise ValueError('''Crop size must be specified if do_center_crop is True.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) # PIL RGBA images are converted to RGB if do_convert_rgb: _a = [convert_to_rgb(lowerCAmelCase_ ) for image in images] # All transformations expect numpy arrays. _a = [to_numpy_array(lowerCAmelCase_ ) for image in images] if do_resize: _a = [self.resize(image=lowerCAmelCase_ , size=lowerCAmelCase_ , resample=lowerCAmelCase_ ) for image in images] if do_center_crop: _a = [self.center_crop(image=lowerCAmelCase_ , size=lowerCAmelCase_ ) for image in images] if do_rescale: _a = [self.rescale(image=lowerCAmelCase_ , scale=lowerCAmelCase_ ) for image in images] if do_normalize: _a = [self.normalize(image=lowerCAmelCase_ , mean=lowerCAmelCase_ , std=lowerCAmelCase_ ) for image in images] _a = [to_channel_dimension_format(lowerCAmelCase_ , lowerCAmelCase_ ) for image in images] _a = {'''pixel_values''': images} return BatchFeature(data=lowerCAmelCase_ , tensor_type=lowerCAmelCase_ )
22
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 UpperCamelCase = get_tests_dir("fixtures") UpperCamelCase = get_tests_dir("fixtures/dummy_feature_extractor_config.json") UpperCamelCase = get_tests_dir("fixtures/dummy-config.json") class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __a ( self :Optional[int] ): UpperCamelCase__ :Optional[int] = 0 def __a ( self :str ): UpperCamelCase__ :Optional[Any] = AutoFeatureExtractor.from_pretrained("""facebook/wav2vec2-base-960h""" ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Dict ): UpperCamelCase__ :Dict = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Optional[int] ): with tempfile.TemporaryDirectory() as tmpdirname: UpperCamelCase__ :List[str] = WavaVecaConfig() # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally UpperCamelCase__ :Tuple = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ ).to_dict() config_dict.pop("""feature_extractor_type""" ) UpperCamelCase__ :Union[str, Any] = WavaVecaFeatureExtractor(**lowerCamelCase__ ) # save in new folder model_config.save_pretrained(lowerCamelCase__ ) config.save_pretrained(lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ ) # make sure private variable is not incorrectly saved UpperCamelCase__ :Tuple = json.loads(config.to_json_string() ) self.assertTrue("""_processor_class""" not in dict_as_saved ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Union[str, Any] ): UpperCamelCase__ :Dict = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Dict ): with self.assertRaisesRegex( lowerCamelCase__ , """bert-base is not a local folder and is not a valid model identifier""" ): UpperCamelCase__ :Dict = AutoFeatureExtractor.from_pretrained("""bert-base""" ) def __a ( self :List[Any] ): with self.assertRaisesRegex( lowerCamelCase__ , r"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)""" ): UpperCamelCase__ :Optional[int] = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ , revision="""aaaaaa""" ) def __a ( self :int ): with self.assertRaisesRegex( lowerCamelCase__ , """hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.""" , ): UpperCamelCase__ :List[Any] = AutoFeatureExtractor.from_pretrained("""hf-internal-testing/config-no-model""" ) def __a ( self :Optional[int] ): # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(lowerCamelCase__ ): UpperCamelCase__ :List[Any] = AutoFeatureExtractor.from_pretrained( """hf-internal-testing/test_dynamic_feature_extractor""" ) # If remote code is disabled, we can't load this config. with self.assertRaises(lowerCamelCase__ ): UpperCamelCase__ :Optional[Any] = AutoFeatureExtractor.from_pretrained( """hf-internal-testing/test_dynamic_feature_extractor""" , trust_remote_code=lowerCamelCase__ ) UpperCamelCase__ :str = AutoFeatureExtractor.from_pretrained( """hf-internal-testing/test_dynamic_feature_extractor""" , trust_remote_code=lowerCamelCase__ ) self.assertEqual(feature_extractor.__class__.__name__ , """NewFeatureExtractor""" ) # Test feature extractor can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained(lowerCamelCase__ ) UpperCamelCase__ :Any = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ , trust_remote_code=lowerCamelCase__ ) self.assertEqual(reloaded_feature_extractor.__class__.__name__ , """NewFeatureExtractor""" ) def __a ( self :Dict ): try: AutoConfig.register("""custom""" , lowerCamelCase__ ) AutoFeatureExtractor.register(lowerCamelCase__ , lowerCamelCase__ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(lowerCamelCase__ ): AutoFeatureExtractor.register(lowerCamelCase__ , lowerCamelCase__ ) # Now that the config is registered, it can be used as any other config with the auto-API UpperCamelCase__ :Any = CustomFeatureExtractor.from_pretrained(lowerCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained(lowerCamelCase__ ) UpperCamelCase__ :List[Any] = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) 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 __a ( self :Optional[int] ): class lowerCAmelCase_ ( lowercase ): """simple docstring""" _snake_case : Optional[int] = True try: AutoConfig.register("""custom""" , lowerCamelCase__ ) AutoFeatureExtractor.register(lowerCamelCase__ , lowerCamelCase__ ) # If remote code is not set, the default is to use local UpperCamelCase__ :Optional[Any] = 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. UpperCamelCase__ :str = AutoFeatureExtractor.from_pretrained( """hf-internal-testing/test_dynamic_feature_extractor""" , trust_remote_code=lowerCamelCase__ ) self.assertEqual(feature_extractor.__class__.__name__ , """NewFeatureExtractor""" ) self.assertTrue(feature_extractor.is_local ) # If remote is enabled, we load from the Hub UpperCamelCase__ :Optional[int] = AutoFeatureExtractor.from_pretrained( """hf-internal-testing/test_dynamic_feature_extractor""" , trust_remote_code=lowerCamelCase__ ) self.assertEqual(feature_extractor.__class__.__name__ , """NewFeatureExtractor""" ) self.assertTrue(not hasattr(lowerCamelCase__ , """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]
45
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging snake_case__ : str = logging.get_logger(__name__) snake_case__ : List[str] = { """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 _a ( UpperCAmelCase__ ): """simple docstring""" A_ = """xlm-roberta""" def __init__( self , _UpperCAmelCase=30522 , _UpperCAmelCase=768 , _UpperCAmelCase=12 , _UpperCAmelCase=12 , _UpperCAmelCase=3072 , _UpperCAmelCase="gelu" , _UpperCAmelCase=0.1 , _UpperCAmelCase=0.1 , _UpperCAmelCase=512 , _UpperCAmelCase=2 , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=1e-12 , _UpperCAmelCase=1 , _UpperCAmelCase=0 , _UpperCAmelCase=2 , _UpperCAmelCase="absolute" , _UpperCAmelCase=True , _UpperCAmelCase=None , **_UpperCAmelCase , ) -> List[str]: super().__init__(pad_token_id=_UpperCAmelCase , bos_token_id=_UpperCAmelCase , eos_token_id=_UpperCAmelCase , **_UpperCAmelCase ) UpperCamelCase_ = vocab_size UpperCamelCase_ = hidden_size UpperCamelCase_ = num_hidden_layers UpperCamelCase_ = num_attention_heads UpperCamelCase_ = hidden_act UpperCamelCase_ = intermediate_size UpperCamelCase_ = hidden_dropout_prob UpperCamelCase_ = attention_probs_dropout_prob UpperCamelCase_ = max_position_embeddings UpperCamelCase_ = type_vocab_size UpperCamelCase_ = initializer_range UpperCamelCase_ = layer_norm_eps UpperCamelCase_ = position_embedding_type UpperCamelCase_ = use_cache UpperCamelCase_ = classifier_dropout class _a ( UpperCAmelCase__ ): """simple docstring""" @property def _UpperCAmelCase ( self ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": UpperCamelCase_ = {0: 'batch', 1: 'choice', 2: 'sequence'} else: UpperCamelCase_ = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ] )
23
import numpy as np import torch import tqdm from ...models.unet_ad import UNetaDModel from ...pipelines import DiffusionPipeline from ...utils import randn_tensor from ...utils.dummy_pt_objects import DDPMScheduler class lowerCAmelCase_ ( lowercase ): """simple docstring""" def __init__( self :int , lowerCamelCase__ :UNetaDModel , lowerCamelCase__ :UNetaDModel , lowerCamelCase__ :DDPMScheduler , lowerCamelCase__ :List[Any] , ): super().__init__() UpperCamelCase__ :Tuple = value_function UpperCamelCase__ :Optional[int] = unet UpperCamelCase__ :List[str] = scheduler UpperCamelCase__ :Dict = env UpperCamelCase__ :Dict = env.get_dataset() UpperCamelCase__ :Union[str, Any] = {} for key in self.data.keys(): try: UpperCamelCase__ :int = self.data[key].mean() except: # noqa: E722 pass UpperCamelCase__ :Any = {} for key in self.data.keys(): try: UpperCamelCase__ :int = self.data[key].std() except: # noqa: E722 pass UpperCamelCase__ :List[Any] = env.observation_space.shape[0] UpperCamelCase__ :List[str] = env.action_space.shape[0] def __a ( self :Union[str, Any] , lowerCamelCase__ :List[str] , lowerCamelCase__ :str ): return (x_in - self.means[key]) / self.stds[key] def __a ( self :int , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Tuple ): return x_in * self.stds[key] + self.means[key] def __a ( self :Any , lowerCamelCase__ :int ): if type(lowerCamelCase__ ) is dict: return {k: self.to_torch(lowerCamelCase__ ) for k, v in x_in.items()} elif torch.is_tensor(lowerCamelCase__ ): return x_in.to(self.unet.device ) return torch.tensor(lowerCamelCase__ , device=self.unet.device ) def __a ( self :Union[str, Any] , lowerCamelCase__ :List[Any] , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Tuple ): for key, val in cond.items(): UpperCamelCase__ :str = val.clone() return x_in def __a ( self :Union[str, Any] , lowerCamelCase__ :List[Any] , lowerCamelCase__ :int , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Optional[int] ): UpperCamelCase__ :Any = x.shape[0] UpperCamelCase__ :List[Any] = None for i in tqdm.tqdm(self.scheduler.timesteps ): # create batch of timesteps to pass into model UpperCamelCase__ :Optional[Any] = torch.full((batch_size,) , lowerCamelCase__ , device=self.unet.device , dtype=torch.long ) for _ in range(lowerCamelCase__ ): with torch.enable_grad(): x.requires_grad_() # permute to match dimension for pre-trained models UpperCamelCase__ :Dict = self.value_function(x.permute(0 , 2 , 1 ) , lowerCamelCase__ ).sample UpperCamelCase__ :List[Any] = torch.autograd.grad([y.sum()] , [x] )[0] UpperCamelCase__ :Union[str, Any] = self.scheduler._get_variance(lowerCamelCase__ ) UpperCamelCase__ :Any = torch.exp(0.5 * posterior_variance ) UpperCamelCase__ :Dict = model_std * grad UpperCamelCase__ :Optional[Any] = 0 UpperCamelCase__ :Dict = x.detach() UpperCamelCase__ :int = x + scale * grad UpperCamelCase__ :int = self.reset_xa(lowerCamelCase__ , lowerCamelCase__ , self.action_dim ) UpperCamelCase__ :List[str] = self.unet(x.permute(0 , 2 , 1 ) , lowerCamelCase__ ).sample.permute(0 , 2 , 1 ) # TODO: verify deprecation of this kwarg UpperCamelCase__ :List[str] = self.scheduler.step(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , predict_epsilon=lowerCamelCase__ )["""prev_sample"""] # apply conditions to the trajectory (set the initial state) UpperCamelCase__ :Optional[Any] = self.reset_xa(lowerCamelCase__ , lowerCamelCase__ , self.action_dim ) UpperCamelCase__ :Optional[int] = self.to_torch(lowerCamelCase__ ) return x, y def __call__( self :Optional[Any] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :str=64 , lowerCamelCase__ :Tuple=32 , lowerCamelCase__ :Dict=2 , lowerCamelCase__ :str=0.1 ): # normalize the observations and create batch dimension UpperCamelCase__ :List[str] = self.normalize(lowerCamelCase__ , """observations""" ) UpperCamelCase__ :List[str] = obs[None].repeat(lowerCamelCase__ , axis=0 ) UpperCamelCase__ :int = {0: self.to_torch(lowerCamelCase__ )} UpperCamelCase__ :Dict = (batch_size, planning_horizon, self.state_dim + self.action_dim) # generate initial noise and apply our conditions (to make the trajectories start at current state) UpperCamelCase__ :Any = randn_tensor(lowerCamelCase__ , device=self.unet.device ) UpperCamelCase__ :Optional[int] = self.reset_xa(lowerCamelCase__ , lowerCamelCase__ , self.action_dim ) UpperCamelCase__ :List[Any] = self.to_torch(lowerCamelCase__ ) # run the diffusion process UpperCamelCase__ , UpperCamelCase__ :Union[str, Any] = self.run_diffusion(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) # sort output trajectories by value UpperCamelCase__ :List[Any] = y.argsort(0 , descending=lowerCamelCase__ ).squeeze() UpperCamelCase__ :Dict = x[sorted_idx] UpperCamelCase__ :Tuple = sorted_values[:, :, : self.action_dim] UpperCamelCase__ :Optional[Any] = actions.detach().cpu().numpy() UpperCamelCase__ :Optional[int] = self.de_normalize(lowerCamelCase__ , key="""actions""" ) # select the action with the highest value if y is not None: UpperCamelCase__ :List[str] = 0 else: # if we didn't run value guiding, select a random action UpperCamelCase__ :Dict = np.random.randint(0 , lowerCamelCase__ ) UpperCamelCase__ :Tuple = denorm_actions[selected_index, 0] return denorm_actions
45
0
'''simple docstring''' from math import acos, sin from typing import List, Tuple, Union import numpy as np import torch from PIL import Image from ...models import AutoencoderKL, UNetaDConditionModel from ...schedulers import DDIMScheduler, DDPMScheduler from ...utils import randn_tensor from ..pipeline_utils import AudioPipelineOutput, BaseOutput, DiffusionPipeline, ImagePipelineOutput from .mel import Mel class lowerCAmelCase ( __lowerCAmelCase): __lowercase : str = ['''vqvae'''] def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , ) -> Union[str, Any]: '''simple docstring''' super().__init__() self.register_modules(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE , mel=__SCREAMING_SNAKE_CASE , vqvae=__SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self ) -> int: '''simple docstring''' return 50 if isinstance(self.scheduler , __SCREAMING_SNAKE_CASE ) else 1000 @torch.no_grad() def __call__( self , __SCREAMING_SNAKE_CASE = 1 , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = 0 , __SCREAMING_SNAKE_CASE = 0 , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = 0 , __SCREAMING_SNAKE_CASE = 0 , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = 0 , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE=True , ) -> Union[ Union[AudioPipelineOutput, ImagePipelineOutput], Tuple[List[Image.Image], Tuple[int, List[np.ndarray]]], ]: '''simple docstring''' __snake_case = steps or self.get_default_steps() self.scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) __snake_case = step_generator or generator # For backwards compatibility if type(self.unet.config.sample_size ) == int: __snake_case = (self.unet.config.sample_size, self.unet.config.sample_size) if noise is None: __snake_case = randn_tensor( ( batch_size, self.unet.config.in_channels, self.unet.config.sample_size[0], self.unet.config.sample_size[1], ) , generator=__SCREAMING_SNAKE_CASE , device=self.device , ) __snake_case = noise __snake_case = None if audio_file is not None or raw_audio is not None: self.mel.load_audio(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) __snake_case = self.mel.audio_slice_to_image(__SCREAMING_SNAKE_CASE ) __snake_case = np.frombuffer(input_image.tobytes() , dtype='''uint8''' ).reshape( (input_image.height, input_image.width) ) __snake_case = (input_image / 255) * 2 - 1 __snake_case = torch.tensor(input_image[np.newaxis, :, :] , dtype=torch.float ).to(self.device ) if self.vqvae is not None: __snake_case = self.vqvae.encode(torch.unsqueeze(__SCREAMING_SNAKE_CASE , 0 ) ).latent_dist.sample( generator=__SCREAMING_SNAKE_CASE )[0] __snake_case = self.vqvae.config.scaling_factor * input_images if start_step > 0: __snake_case = self.scheduler.add_noise(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , self.scheduler.timesteps[start_step - 1] ) __snake_case = ( self.unet.config.sample_size[1] * self.mel.get_sample_rate() / self.mel.x_res / self.mel.hop_length ) __snake_case = int(mask_start_secs * pixels_per_second ) __snake_case = int(mask_end_secs * pixels_per_second ) __snake_case = self.scheduler.add_noise(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , torch.tensor(self.scheduler.timesteps[start_step:] ) ) for step, t in enumerate(self.progress_bar(self.scheduler.timesteps[start_step:] ) ): if isinstance(self.unet , __SCREAMING_SNAKE_CASE ): __snake_case = self.unet(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )['''sample'''] else: __snake_case = self.unet(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )['''sample'''] if isinstance(self.scheduler , __SCREAMING_SNAKE_CASE ): __snake_case = self.scheduler.step( model_output=__SCREAMING_SNAKE_CASE , timestep=__SCREAMING_SNAKE_CASE , sample=__SCREAMING_SNAKE_CASE , eta=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , )['''prev_sample'''] else: __snake_case = self.scheduler.step( model_output=__SCREAMING_SNAKE_CASE , timestep=__SCREAMING_SNAKE_CASE , sample=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , )['''prev_sample'''] if mask is not None: if mask_start > 0: __snake_case = mask[:, step, :, :mask_start] if mask_end > 0: __snake_case = mask[:, step, :, -mask_end:] if self.vqvae is not None: # 0.18215 was scaling factor used in training to ensure unit variance __snake_case = 1 / self.vqvae.config.scaling_factor * images __snake_case = self.vqvae.decode(__SCREAMING_SNAKE_CASE )['''sample'''] __snake_case = (images / 2 + 0.5).clamp(0 , 1 ) __snake_case = images.cpu().permute(0 , 2 , 3 , 1 ).numpy() __snake_case = (images * 255).round().astype('''uint8''' ) __snake_case = list( (Image.fromarray(_[:, :, 0] ) for _ in images) if images.shape[3] == 1 else (Image.fromarray(__SCREAMING_SNAKE_CASE , mode='''RGB''' ).convert('''L''' ) for _ in images) ) __snake_case = [self.mel.image_to_audio(__SCREAMING_SNAKE_CASE ) for _ in images] if not return_dict: return images, (self.mel.get_sample_rate(), audios) return BaseOutput(**AudioPipelineOutput(np.array(__SCREAMING_SNAKE_CASE )[:, np.newaxis, :] ) , **ImagePipelineOutput(__SCREAMING_SNAKE_CASE ) ) @torch.no_grad() def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 50 ) -> np.ndarray: '''simple docstring''' assert isinstance(self.scheduler , __SCREAMING_SNAKE_CASE ) self.scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) __snake_case = np.array( [np.frombuffer(image.tobytes() , dtype='''uint8''' ).reshape((1, image.height, image.width) ) for image in images] ) __snake_case = (sample / 255) * 2 - 1 __snake_case = torch.Tensor(__SCREAMING_SNAKE_CASE ).to(self.device ) for t in self.progress_bar(torch.flip(self.scheduler.timesteps , (0,) ) ): __snake_case = t - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps __snake_case = self.scheduler.alphas_cumprod[t] __snake_case = ( self.scheduler.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.scheduler.final_alpha_cumprod ) __snake_case = 1 - alpha_prod_t __snake_case = self.unet(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )['''sample'''] __snake_case = (1 - alpha_prod_t_prev) ** 0.5 * model_output __snake_case = (sample - pred_sample_direction) * alpha_prod_t_prev ** (-0.5) __snake_case = sample * alpha_prod_t ** 0.5 + beta_prod_t ** 0.5 * model_output return sample @staticmethod def lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) -> torch.Tensor: '''simple docstring''' __snake_case = acos(torch.dot(torch.flatten(__SCREAMING_SNAKE_CASE ) , torch.flatten(__SCREAMING_SNAKE_CASE ) ) / torch.norm(__SCREAMING_SNAKE_CASE ) / torch.norm(__SCREAMING_SNAKE_CASE ) ) return sin((1 - alpha) * theta ) * xa / sin(__SCREAMING_SNAKE_CASE ) + sin(alpha * theta ) * xa / sin(__SCREAMING_SNAKE_CASE )
24
def A ( lowercase__ : int ) -> bool: if num < 0: return False UpperCamelCase__ :int = num UpperCamelCase__ :int = 0 while num > 0: UpperCamelCase__ :Optional[int] = rev_num * 10 + (num % 10) num //= 10 return num_copy == rev_num if __name__ == "__main__": import doctest doctest.testmod()
45
0
def lowerCamelCase__ ( _a , _a): return 1 if input_a == input_a else 0 def lowerCamelCase__ ( ): assert xnor_gate(0 , 0) == 1 assert xnor_gate(0 , 1) == 0 assert xnor_gate(1 , 0) == 0 assert xnor_gate(1 , 1) == 1 if __name__ == "__main__": print(xnor_gate(0, 0)) print(xnor_gate(0, 1)) print(xnor_gate(1, 0)) print(xnor_gate(1, 1))
25
from __future__ import annotations def A ( lowercase__ : list[int] ) -> bool: return len(set(lowercase__ ) ) == len(lowercase__ ) if __name__ == "__main__": import doctest doctest.testmod()
45
0
'''simple docstring''' from typing import Optional from torch import nn from .transformer_ad import TransformeraDModel, TransformeraDModelOutput class _A ( nn.Module ): def __init__( self : Optional[Any] , __magic_name__ : int = 16 , __magic_name__ : int = 88 , __magic_name__ : Optional[int] = None , __magic_name__ : int = 1 , __magic_name__ : float = 0.0 , __magic_name__ : int = 32 , __magic_name__ : Optional[int] = None , __magic_name__ : bool = False , __magic_name__ : Optional[int] = None , __magic_name__ : Optional[int] = None , __magic_name__ : str = "geglu" , __magic_name__ : Optional[int] = None , ) -> Any: """simple docstring""" super().__init__() __snake_case : List[Any] = nn.ModuleList( [ TransformeraDModel( num_attention_heads=__magic_name__ , attention_head_dim=__magic_name__ , in_channels=__magic_name__ , num_layers=__magic_name__ , dropout=__magic_name__ , norm_num_groups=__magic_name__ , cross_attention_dim=__magic_name__ , attention_bias=__magic_name__ , sample_size=__magic_name__ , num_vector_embeds=__magic_name__ , activation_fn=__magic_name__ , num_embeds_ada_norm=__magic_name__ , ) for _ in range(2 ) ] ) # Variables that can be set by a pipeline: # The ratio of transformer1 to transformer2's output states to be combined during inference __snake_case : List[Any] = 0.5 # The shape of `encoder_hidden_states` is expected to be # `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)` __snake_case : Any = [77, 2_57] # Which transformer to use to encode which condition. # E.g. `(1, 0)` means that we'll use `transformers[1](conditions[0])` and `transformers[0](conditions[1])` __snake_case : int = [1, 0] def lowercase__ ( self : List[str] , __magic_name__ : Optional[Any] , __magic_name__ : str , __magic_name__ : List[str]=None , __magic_name__ : int=None , __magic_name__ : Optional[Any]=None , __magic_name__ : bool = True , ) -> Dict: """simple docstring""" __snake_case : Optional[int] = hidden_states __snake_case : int = [] __snake_case : int = 0 # attention_mask is not used yet for i in range(2 ): # for each of the two transformers, pass the corresponding condition tokens __snake_case : Tuple = encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]] __snake_case : Tuple = self.transformer_index_for_condition[i] __snake_case : Optional[Any] = self.transformers[transformer_index]( __magic_name__ , encoder_hidden_states=__magic_name__ , timestep=__magic_name__ , cross_attention_kwargs=__magic_name__ , return_dict=__magic_name__ , )[0] encoded_states.append(encoded_state - input_states ) tokens_start += self.condition_lengths[i] __snake_case : List[Any] = encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio) __snake_case : List[str] = output_states + input_states if not return_dict: return (output_states,) return TransformeraDModelOutput(sample=__magic_name__ )
26
from __future__ import annotations class lowerCAmelCase_ : """simple docstring""" def __init__( self :List[Any] , lowerCamelCase__ :int = 0 ): UpperCamelCase__ :List[str] = key def __a ( self :Optional[Any] , lowerCamelCase__ :str , lowerCamelCase__ :int ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__ :List[str] = key or self.__key or 1 # make sure key is an appropriate size key %= 2_55 return [chr(ord(lowerCamelCase__ ) ^ key ) for ch in content] def __a ( self :int , lowerCamelCase__ :str , lowerCamelCase__ :int ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__ :int = key or self.__key or 1 # make sure key is an appropriate size key %= 2_55 return [chr(ord(lowerCamelCase__ ) ^ key ) for ch in content] def __a ( self :Optional[Any] , lowerCamelCase__ :str , lowerCamelCase__ :int = 0 ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__ :Dict = key or self.__key or 1 # make sure key can be any size while key > 2_55: key -= 2_55 # This will be returned UpperCamelCase__ :List[str] = """""" for ch in content: ans += chr(ord(lowerCamelCase__ ) ^ key ) return ans def __a ( self :Any , lowerCamelCase__ :str , lowerCamelCase__ :int = 0 ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__ :Tuple = key or self.__key or 1 # make sure key can be any size while key > 2_55: key -= 2_55 # This will be returned UpperCamelCase__ :Optional[int] = """""" for ch in content: ans += chr(ord(lowerCamelCase__ ) ^ key ) return ans def __a ( self :Optional[Any] , lowerCamelCase__ :str , lowerCamelCase__ :int = 0 ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) try: with open(lowerCamelCase__ ) as fin, open("""encrypt.out""" , """w+""" ) as fout: # actual encrypt-process for line in fin: fout.write(self.encrypt_string(lowerCamelCase__ , lowerCamelCase__ ) ) except OSError: return False return True def __a ( self :Dict , lowerCamelCase__ :str , lowerCamelCase__ :int ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) try: with open(lowerCamelCase__ ) as fin, open("""decrypt.out""" , """w+""" ) as fout: # actual encrypt-process for line in fin: fout.write(self.decrypt_string(lowerCamelCase__ , lowerCamelCase__ ) ) except OSError: return False return True # Tests # crypt = XORCipher() # key = 67 # # test encrypt # print(crypt.encrypt("hallo welt",key)) # # test decrypt # print(crypt.decrypt(crypt.encrypt("hallo welt",key), key)) # # test encrypt_string # print(crypt.encrypt_string("hallo welt",key)) # # test decrypt_string # print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key)) # if (crypt.encrypt_file("test.txt",key)): # print("encrypt successful") # else: # print("encrypt unsuccessful") # if (crypt.decrypt_file("encrypt.out",key)): # print("decrypt successful") # else: # print("decrypt unsuccessful")
45
0
from ...utils import is_note_seq_available, is_transformers_available, is_torch_available from ...utils import OptionalDependencyNotAvailable try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .notes_encoder import SpectrogramNotesEncoder from .continous_encoder import SpectrogramContEncoder from .pipeline_spectrogram_diffusion import ( SpectrogramContEncoder, SpectrogramDiffusionPipeline, TaFilmDecoder, ) try: if not (is_transformers_available() and is_torch_available() and is_note_seq_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_transformers_and_torch_and_note_seq_objects import * # noqa F403 else: from .midi_utils import MidiProcessor
27
import random def A ( lowercase__ : Dict , lowercase__ : str , lowercase__ : Optional[Any] ) -> int: UpperCamelCase__ :List[Any] = a[left_index] UpperCamelCase__ :Dict = left_index + 1 for j in range(left_index + 1 , lowercase__ ): if a[j] < pivot: UpperCamelCase__ , UpperCamelCase__ :Optional[int] = a[i], a[j] i += 1 UpperCamelCase__ , UpperCamelCase__ :Tuple = a[i - 1], a[left_index] return i - 1 def A ( lowercase__ : Tuple , lowercase__ : Optional[int] , lowercase__ : Any ) -> Optional[int]: if left < right: UpperCamelCase__ :List[Any] = random.randint(lowercase__ , right - 1 ) UpperCamelCase__ , UpperCamelCase__ :Union[str, Any] = ( a[left], a[pivot], ) # switches the pivot with the left most bound UpperCamelCase__ :int = partition(lowercase__ , lowercase__ , lowercase__ ) quick_sort_random( lowercase__ , lowercase__ , lowercase__ ) # recursive quicksort to the left of the pivot point quick_sort_random( lowercase__ , pivot_index + 1 , lowercase__ ) # recursive quicksort to the right of the pivot point def A ( ) -> List[Any]: UpperCamelCase__ :str = input("""Enter numbers separated by a comma:\n""" ).strip() UpperCamelCase__ :int = [int(lowercase__ ) for item in user_input.split(""",""" )] quick_sort_random(lowercase__ , 0 , len(lowercase__ ) ) print(lowercase__ ) if __name__ == "__main__": main()
45
0
'''simple docstring''' from ..utils import DummyObject, requires_backends class _a ( metaclass=SCREAMING_SNAKE_CASE ): '''simple docstring''' A : List[str] = ['''flax'''] def __init__( self, *A, **A ): '''simple docstring''' requires_backends(self, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) class _a ( metaclass=SCREAMING_SNAKE_CASE ): '''simple docstring''' A : List[str] = ['''flax'''] def __init__( self, *A, **A ): '''simple docstring''' requires_backends(self, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) class _a ( metaclass=SCREAMING_SNAKE_CASE ): '''simple docstring''' A : str = ['''flax'''] def __init__( self, *A, **A ): '''simple docstring''' requires_backends(self, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) class _a ( metaclass=SCREAMING_SNAKE_CASE ): '''simple docstring''' A : Dict = ['''flax'''] def __init__( self, *A, **A ): '''simple docstring''' requires_backends(self, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) class _a ( metaclass=SCREAMING_SNAKE_CASE ): '''simple docstring''' A : Union[str, Any] = ['''flax'''] def __init__( self, *A, **A ): '''simple docstring''' requires_backends(self, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) class _a ( metaclass=SCREAMING_SNAKE_CASE ): '''simple docstring''' A : Any = ['''flax'''] def __init__( self, *A, **A ): '''simple docstring''' requires_backends(self, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) class _a ( metaclass=SCREAMING_SNAKE_CASE ): '''simple docstring''' A : List[str] = ['''flax'''] def __init__( self, *A, **A ): '''simple docstring''' requires_backends(self, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) class _a ( metaclass=SCREAMING_SNAKE_CASE ): '''simple docstring''' A : Any = ['''flax'''] def __init__( self, *A, **A ): '''simple docstring''' requires_backends(self, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) class _a ( metaclass=SCREAMING_SNAKE_CASE ): '''simple docstring''' A : List[str] = ['''flax'''] def __init__( self, *A, **A ): '''simple docstring''' requires_backends(self, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) class _a ( metaclass=SCREAMING_SNAKE_CASE ): '''simple docstring''' A : Tuple = ['''flax'''] def __init__( self, *A, **A ): '''simple docstring''' requires_backends(self, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) class _a ( metaclass=SCREAMING_SNAKE_CASE ): '''simple docstring''' A : Optional[int] = ['''flax'''] def __init__( self, *A, **A ): '''simple docstring''' requires_backends(self, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) class _a ( metaclass=SCREAMING_SNAKE_CASE ): '''simple docstring''' A : Optional[int] = ['''flax'''] def __init__( self, *A, **A ): '''simple docstring''' requires_backends(self, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) class _a ( metaclass=SCREAMING_SNAKE_CASE ): '''simple docstring''' A : int = ['''flax'''] def __init__( self, *A, **A ): '''simple docstring''' requires_backends(self, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] ) @classmethod def UpperCamelCase_ ( cls, *A, **A ): '''simple docstring''' requires_backends(cls, ['flax'] )
28
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { "shi-labs/dinat-mini-in1k-224": "https://huggingface.co/shi-labs/dinat-mini-in1k-224/resolve/main/config.json", # See all Dinat models at https://huggingface.co/models?filter=dinat } class lowerCAmelCase_ ( lowercase , lowercase ): """simple docstring""" _snake_case : Tuple = """dinat""" _snake_case : List[Any] = { """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers""", } def __init__( self :Optional[int] , lowerCamelCase__ :int=4 , lowerCamelCase__ :Union[str, Any]=3 , lowerCamelCase__ :List[Any]=64 , lowerCamelCase__ :Any=[3, 4, 6, 5] , lowerCamelCase__ :Tuple=[2, 4, 8, 16] , lowerCamelCase__ :Optional[int]=7 , lowerCamelCase__ :Tuple=[[1, 8, 1], [1, 4, 1, 4], [1, 2, 1, 2, 1, 2], [1, 1, 1, 1, 1]] , lowerCamelCase__ :Tuple=3.0 , lowerCamelCase__ :str=True , lowerCamelCase__ :Optional[int]=0.0 , lowerCamelCase__ :Optional[Any]=0.0 , lowerCamelCase__ :int=0.1 , lowerCamelCase__ :Optional[Any]="gelu" , lowerCamelCase__ :Optional[Any]=0.02 , lowerCamelCase__ :Union[str, Any]=1e-5 , lowerCamelCase__ :Optional[int]=0.0 , lowerCamelCase__ :List[str]=None , lowerCamelCase__ :str=None , **lowerCamelCase__ :List[Any] , ): super().__init__(**lowerCamelCase__ ) UpperCamelCase__ :Any = patch_size UpperCamelCase__ :Any = num_channels UpperCamelCase__ :int = embed_dim UpperCamelCase__ :Optional[Any] = depths UpperCamelCase__ :Any = len(lowerCamelCase__ ) UpperCamelCase__ :str = num_heads UpperCamelCase__ :Optional[int] = kernel_size UpperCamelCase__ :Optional[int] = dilations UpperCamelCase__ :Tuple = mlp_ratio UpperCamelCase__ :Dict = qkv_bias UpperCamelCase__ :List[str] = hidden_dropout_prob UpperCamelCase__ :List[str] = attention_probs_dropout_prob UpperCamelCase__ :Union[str, Any] = drop_path_rate UpperCamelCase__ :Tuple = hidden_act UpperCamelCase__ :List[Any] = layer_norm_eps UpperCamelCase__ :Optional[Any] = initializer_range # we set the hidden_size attribute in order to make Dinat work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model UpperCamelCase__ :Tuple = int(embed_dim * 2 ** (len(lowerCamelCase__ ) - 1) ) UpperCamelCase__ :Tuple = layer_scale_init_value UpperCamelCase__ :Optional[int] = ["""stem"""] + [f"""stage{idx}""" for idx in range(1 , len(lowerCamelCase__ ) + 1 )] UpperCamelCase__ , UpperCamelCase__ :List[str] = get_aligned_output_features_output_indices( out_features=lowerCamelCase__ , out_indices=lowerCamelCase__ , stage_names=self.stage_names )
45
0
"""simple docstring""" import random def lowercase ( lowerCAmelCase__ ): lowerCamelCase_ = num - 1 lowerCamelCase_ = 0 while s % 2 == 0: lowerCamelCase_ = s // 2 t += 1 for _ in range(5 ): lowerCamelCase_ = random.randrange(2 ,num - 1 ) lowerCamelCase_ = pow(lowerCAmelCase__ ,lowerCAmelCase__ ,lowerCAmelCase__ ) if v != 1: lowerCamelCase_ = 0 while v != (num - 1): if i == t - 1: return False else: lowerCamelCase_ = i + 1 lowerCamelCase_ = (v**2) % num return True def lowercase ( lowerCAmelCase__ ): if num < 2: return False lowerCamelCase_ = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, ] if num in low_primes: return True for prime in low_primes: if (num % prime) == 0: return False return rabin_miller(lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ = 1_024 ): while True: lowerCamelCase_ = random.randrange(2 ** (keysize - 1) ,2 ** (keysize) ) if is_prime_low_num(lowerCAmelCase__ ): return num if __name__ == "__main__": A_ = generate_large_prime() print(("""Prime number:""", num)) print(("""is_prime_low_num:""", is_prime_low_num(num)))
29
def A ( lowercase__ : int , lowercase__ : int ) -> int: return int(input_a == input_a == 0 ) def A ( ) -> None: print("""Truth Table of NOR Gate:""" ) print("""| Input 1 | Input 2 | Output |""" ) print(f"""| 0 | 0 | {nor_gate(0 , 0 )} |""" ) print(f"""| 0 | 1 | {nor_gate(0 , 1 )} |""" ) print(f"""| 1 | 0 | {nor_gate(1 , 0 )} |""" ) print(f"""| 1 | 1 | {nor_gate(1 , 1 )} |""" ) if __name__ == "__main__": import doctest doctest.testmod() main()
45
0
def lowerCamelCase__ ( _lowercase , _lowercase ): '''simple docstring''' while a != 0: UpperCAmelCase_, UpperCAmelCase_ : Optional[int] = b % a, a return b def lowerCamelCase__ ( _lowercase , _lowercase ): '''simple docstring''' if gcd(_lowercase , _lowercase ) != 1: UpperCAmelCase_ : int = f'''mod inverse of {a!r} and {m!r} does not exist''' raise ValueError(_lowercase ) UpperCAmelCase_, UpperCAmelCase_, UpperCAmelCase_ : Union[str, Any] = 1, 0, a UpperCAmelCase_, UpperCAmelCase_, UpperCAmelCase_ : Dict = 0, 1, m while va != 0: UpperCAmelCase_ : List[Any] = ua // va UpperCAmelCase_, UpperCAmelCase_, UpperCAmelCase_, UpperCAmelCase_, UpperCAmelCase_, UpperCAmelCase_ : Any = (ua - q * va), (ua - q * va), (ua - q * va), va, va, va return ua % m
30
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import GLPNImageProcessor class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __init__( self :Any , lowerCamelCase__ :Dict , lowerCamelCase__ :Optional[Any]=7 , lowerCamelCase__ :str=3 , lowerCamelCase__ :Optional[Any]=18 , lowerCamelCase__ :List[str]=30 , lowerCamelCase__ :str=4_00 , lowerCamelCase__ :Optional[int]=True , lowerCamelCase__ :Union[str, Any]=32 , lowerCamelCase__ :int=True , ): UpperCamelCase__ :List[Any] = parent UpperCamelCase__ :List[Any] = batch_size UpperCamelCase__ :Any = num_channels UpperCamelCase__ :List[str] = image_size UpperCamelCase__ :Dict = min_resolution UpperCamelCase__ :List[str] = max_resolution UpperCamelCase__ :str = do_resize UpperCamelCase__ :int = size_divisor UpperCamelCase__ :Optional[int] = do_rescale def __a ( self :str ): return { "do_resize": self.do_resize, "size_divisor": self.size_divisor, "do_rescale": self.do_rescale, } @require_torch @require_vision class lowerCAmelCase_ ( lowercase , unittest.TestCase ): """simple docstring""" _snake_case : Optional[int] = GLPNImageProcessor if is_vision_available() else None def __a ( self :Dict ): UpperCamelCase__ :Dict = GLPNImageProcessingTester(self ) @property def __a ( self :List[str] ): return self.image_processor_tester.prepare_image_processor_dict() def __a ( self :Optional[int] ): UpperCamelCase__ :Optional[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(lowerCamelCase__ , """do_resize""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """size_divisor""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """resample""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """do_rescale""" ) ) def __a ( self :Optional[int] ): pass def __a ( self :Tuple ): # Initialize image_processing UpperCamelCase__ :int = self.image_processing_class(**self.image_processor_dict ) # create random PIL images UpperCamelCase__ :str = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCamelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCamelCase__ , Image.Image ) # Test not batched input (GLPNImageProcessor doesn't support batching) UpperCamelCase__ :Tuple = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 ) self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 ) def __a ( self :str ): # Initialize image_processing UpperCamelCase__ :str = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors UpperCamelCase__ :Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCamelCase__ , numpify=lowerCamelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCamelCase__ , np.ndarray ) # Test not batched input (GLPNImageProcessor doesn't support batching) UpperCamelCase__ :List[str] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 ) self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 ) def __a ( self :Any ): # Initialize image_processing UpperCamelCase__ :List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors UpperCamelCase__ :Tuple = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCamelCase__ , torchify=lowerCamelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCamelCase__ , torch.Tensor ) # Test not batched input (GLPNImageProcessor doesn't support batching) UpperCamelCase__ :List[str] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 ) self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 )
45
0
import warnings from ...utils import logging from .image_processing_mobilevit import MobileViTImageProcessor lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : str , *_lowerCAmelCase : Tuple , **_lowerCAmelCase : int ): warnings.warn( 'The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use MobileViTImageProcessor instead.' , _lowerCAmelCase , ) super().__init__(*_lowerCAmelCase , **_lowerCAmelCase )
31
import math def A ( lowercase__ : Tuple , lowercase__ : Union[str, Any] ) -> Optional[Any]: if 0 not in (x, y): # We use the relation x^y = y*log10(x), where 10 is the base. return y * math.logaa(lowercase__ ) else: if x == 0: # 0 raised to any number is 0 return 0 elif y == 0: return 1 # any number raised to 0 is 1 raise AssertionError("""This should never happen""" ) if __name__ == "__main__": # Main function # Read two numbers from input and typecast them to int using map function. # Here x is the base and y is the power. UpperCamelCase = "Enter the base and the power separated by a comma: " UpperCamelCase , UpperCamelCase = map(int, input(prompt).split(",")) UpperCamelCase , UpperCamelCase = map(int, input(prompt).split(",")) # We find the log of each number, using the function res(), which takes two # arguments. UpperCamelCase = res(xa, ya) UpperCamelCase = res(xa, ya) # We check for the largest number if resa > resa: print("Largest number is", xa, "^", ya) elif resa > resa: print("Largest number is", xa, "^", ya) else: print("Both are equal")
45
0
import enum import os from hashlib import shaaaa from typing import Optional from .. import config from .logging import get_logger UpperCAmelCase_ = get_logger(__name__) class __UpperCamelCase ( enum.Enum ): __A : int = """all_checks""" __A : Tuple = """basic_checks""" __A : List[Any] = """no_checks""" class __UpperCamelCase ( A__ ): pass class __UpperCamelCase ( A__ ): pass class __UpperCamelCase ( A__ ): pass class __UpperCamelCase ( A__ ): pass def A__ ( SCREAMING_SNAKE_CASE_ : Optional[dict] , SCREAMING_SNAKE_CASE_ : dict , SCREAMING_SNAKE_CASE_ : Dict=None ) -> Union[str, Any]: """simple docstring""" if expected_checksums is None: logger.info('''Unable to verify checksums.''' ) return if len(set(SCREAMING_SNAKE_CASE_ ) - set(SCREAMING_SNAKE_CASE_ ) ) > 0: raise ExpectedMoreDownloadedFiles(str(set(SCREAMING_SNAKE_CASE_ ) - set(SCREAMING_SNAKE_CASE_ ) ) ) if len(set(SCREAMING_SNAKE_CASE_ ) - set(SCREAMING_SNAKE_CASE_ ) ) > 0: raise UnexpectedDownloadedFile(str(set(SCREAMING_SNAKE_CASE_ ) - set(SCREAMING_SNAKE_CASE_ ) ) ) _UpperCAmelCase = [url for url in expected_checksums if expected_checksums[url] != recorded_checksums[url]] _UpperCAmelCase = ''' for ''' + verification_name if verification_name is not None else '''''' if len(SCREAMING_SNAKE_CASE_ ) > 0: raise NonMatchingChecksumError( F'''Checksums didn\'t match{for_verification_name}:\n''' F'''{bad_urls}\n''' '''Set `verification_mode=\'no_checks\'` to skip checksums verification and ignore this error''' ) logger.info('''All the checksums matched successfully''' + for_verification_name ) class __UpperCamelCase ( A__ ): pass class __UpperCamelCase ( A__ ): pass class __UpperCamelCase ( A__ ): pass class __UpperCamelCase ( A__ ): pass def A__ ( SCREAMING_SNAKE_CASE_ : Optional[dict] , SCREAMING_SNAKE_CASE_ : dict ) -> int: """simple docstring""" if expected_splits is None: logger.info('''Unable to verify splits sizes.''' ) return if len(set(SCREAMING_SNAKE_CASE_ ) - set(SCREAMING_SNAKE_CASE_ ) ) > 0: raise ExpectedMoreSplits(str(set(SCREAMING_SNAKE_CASE_ ) - set(SCREAMING_SNAKE_CASE_ ) ) ) if len(set(SCREAMING_SNAKE_CASE_ ) - set(SCREAMING_SNAKE_CASE_ ) ) > 0: raise UnexpectedSplits(str(set(SCREAMING_SNAKE_CASE_ ) - set(SCREAMING_SNAKE_CASE_ ) ) ) _UpperCAmelCase = [ {'''expected''': expected_splits[name], '''recorded''': recorded_splits[name]} for name in expected_splits if expected_splits[name].num_examples != recorded_splits[name].num_examples ] if len(SCREAMING_SNAKE_CASE_ ) > 0: raise NonMatchingSplitsSizesError(str(SCREAMING_SNAKE_CASE_ ) ) logger.info('''All the splits matched successfully.''' ) def A__ ( SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : bool = True ) -> dict: """simple docstring""" if record_checksum: _UpperCAmelCase = shaaaa() with open(SCREAMING_SNAKE_CASE_ , '''rb''' ) as f: for chunk in iter(lambda: f.read(1 << 20 ) , B'''''' ): m.update(SCREAMING_SNAKE_CASE_ ) _UpperCAmelCase = m.hexdigest() else: _UpperCAmelCase = None return {"num_bytes": os.path.getsize(SCREAMING_SNAKE_CASE_ ), "checksum": checksum} def A__ ( SCREAMING_SNAKE_CASE_ : Any ) -> Optional[Any]: """simple docstring""" if dataset_size and config.IN_MEMORY_MAX_SIZE: return dataset_size < config.IN_MEMORY_MAX_SIZE else: return False
32
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, 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 numpy as np import tensorflow as tf from transformers import ( TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST, FlaubertConfig, TFFlaubertForMultipleChoice, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForSequenceClassification, TFFlaubertForTokenClassification, TFFlaubertModel, TFFlaubertWithLMHeadModel, ) class lowerCAmelCase_ : """simple docstring""" def __init__( self :Dict , lowerCamelCase__ :List[str] , ): UpperCamelCase__ :Optional[int] = parent UpperCamelCase__ :int = 13 UpperCamelCase__ :Optional[int] = 7 UpperCamelCase__ :Dict = True UpperCamelCase__ :Dict = True UpperCamelCase__ :str = True UpperCamelCase__ :List[Any] = True UpperCamelCase__ :Any = True UpperCamelCase__ :Optional[int] = False UpperCamelCase__ :Optional[int] = False UpperCamelCase__ :Tuple = False UpperCamelCase__ :Optional[int] = 2 UpperCamelCase__ :List[str] = 99 UpperCamelCase__ :Optional[Any] = 0 UpperCamelCase__ :Any = 32 UpperCamelCase__ :List[str] = 2 UpperCamelCase__ :int = 4 UpperCamelCase__ :List[str] = 0.1 UpperCamelCase__ :Union[str, Any] = 0.1 UpperCamelCase__ :Union[str, Any] = 5_12 UpperCamelCase__ :List[str] = 16 UpperCamelCase__ :str = 2 UpperCamelCase__ :Optional[int] = 0.02 UpperCamelCase__ :Optional[int] = 3 UpperCamelCase__ :Optional[int] = 4 UpperCamelCase__ :Optional[int] = """last""" UpperCamelCase__ :Tuple = True UpperCamelCase__ :int = None UpperCamelCase__ :Dict = 0 def __a ( self :int ): UpperCamelCase__ :Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCamelCase__ :Any = random_attention_mask([self.batch_size, self.seq_length] , dtype=tf.floataa ) UpperCamelCase__ :Union[str, Any] = None if self.use_input_lengths: UpperCamelCase__ :Union[str, Any] = ( ids_tensor([self.batch_size] , vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length UpperCamelCase__ :List[str] = None if self.use_token_type_ids: UpperCamelCase__ :List[str] = ids_tensor([self.batch_size, self.seq_length] , self.n_langs ) UpperCamelCase__ :int = None UpperCamelCase__ :List[str] = None UpperCamelCase__ :List[str] = None if self.use_labels: UpperCamelCase__ :List[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase__ :Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCamelCase__ :str = ids_tensor([self.batch_size] , 2 , dtype=tf.floataa ) UpperCamelCase__ :int = ids_tensor([self.batch_size] , self.num_choices ) UpperCamelCase__ :List[Any] = FlaubertConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , bos_token_id=self.bos_token_id , ) return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def __a ( self :Union[str, Any] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :int , lowerCamelCase__ :List[str] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :int , lowerCamelCase__ :List[Any] , lowerCamelCase__ :List[Any] , lowerCamelCase__ :int , ): UpperCamelCase__ :int = TFFlaubertModel(config=lowerCamelCase__ ) UpperCamelCase__ :Union[str, Any] = {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids} UpperCamelCase__ :Optional[Any] = model(lowerCamelCase__ ) UpperCamelCase__ :Union[str, Any] = [input_ids, input_mask] UpperCamelCase__ :Optional[int] = model(lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __a ( self :Tuple , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Any , lowerCamelCase__ :int , lowerCamelCase__ :int , lowerCamelCase__ :List[str] , lowerCamelCase__ :Any , lowerCamelCase__ :Optional[Any] , ): UpperCamelCase__ :List[str] = TFFlaubertWithLMHeadModel(lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids} UpperCamelCase__ :Any = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __a ( self :Dict , lowerCamelCase__ :List[str] , lowerCamelCase__ :Dict , lowerCamelCase__ :Tuple , lowerCamelCase__ :str , lowerCamelCase__ :Any , lowerCamelCase__ :Tuple , lowerCamelCase__ :Any , lowerCamelCase__ :int , lowerCamelCase__ :Tuple , ): UpperCamelCase__ :int = TFFlaubertForQuestionAnsweringSimple(lowerCamelCase__ ) UpperCamelCase__ :int = {"""input_ids""": input_ids, """lengths""": input_lengths} UpperCamelCase__ :Optional[int] = model(lowerCamelCase__ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __a ( self :List[Any] , lowerCamelCase__ :List[str] , lowerCamelCase__ :List[str] , lowerCamelCase__ :str , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Tuple , lowerCamelCase__ :Tuple , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :int , lowerCamelCase__ :Optional[int] , ): UpperCamelCase__ :List[Any] = TFFlaubertForSequenceClassification(lowerCamelCase__ ) UpperCamelCase__ :List[str] = {"""input_ids""": input_ids, """lengths""": input_lengths} UpperCamelCase__ :List[str] = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __a ( self :Tuple , lowerCamelCase__ :str , lowerCamelCase__ :Any , lowerCamelCase__ :Tuple , lowerCamelCase__ :str , lowerCamelCase__ :Any , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :List[str] , lowerCamelCase__ :str , lowerCamelCase__ :Any , ): UpperCamelCase__ :Any = self.num_labels UpperCamelCase__ :Tuple = TFFlaubertForTokenClassification(config=lowerCamelCase__ ) UpperCamelCase__ :Any = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} UpperCamelCase__ :List[Any] = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __a ( self :Tuple , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Tuple , lowerCamelCase__ :Any , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :str , lowerCamelCase__ :Any , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Dict , lowerCamelCase__ :List[str] , ): UpperCamelCase__ :Optional[int] = self.num_choices UpperCamelCase__ :Dict = TFFlaubertForMultipleChoice(config=lowerCamelCase__ ) UpperCamelCase__ :Any = tf.tile(tf.expand_dims(lowerCamelCase__ , 1 ) , (1, self.num_choices, 1) ) UpperCamelCase__ :str = tf.tile(tf.expand_dims(lowerCamelCase__ , 1 ) , (1, self.num_choices, 1) ) UpperCamelCase__ :Any = tf.tile(tf.expand_dims(lowerCamelCase__ , 1 ) , (1, self.num_choices, 1) ) UpperCamelCase__ :int = { """input_ids""": multiple_choice_inputs_ids, """attention_mask""": multiple_choice_input_mask, """token_type_ids""": multiple_choice_token_type_ids, } UpperCamelCase__ :List[str] = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __a ( self :Tuple ): UpperCamelCase__ :str = self.prepare_config_and_inputs() ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :str = config_and_inputs UpperCamelCase__ :Optional[Any] = { """input_ids""": input_ids, """token_type_ids""": token_type_ids, """langs""": token_type_ids, """lengths""": input_lengths, } return config, inputs_dict @require_tf class lowerCAmelCase_ ( lowercase , lowercase , unittest.TestCase ): """simple docstring""" _snake_case : List[str] = ( ( TFFlaubertModel, TFFlaubertWithLMHeadModel, TFFlaubertForSequenceClassification, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForTokenClassification, TFFlaubertForMultipleChoice, ) if is_tf_available() else () ) _snake_case : List[Any] = ( (TFFlaubertWithLMHeadModel,) if is_tf_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable _snake_case : Optional[int] = ( { """feature-extraction""": TFFlaubertModel, """fill-mask""": TFFlaubertWithLMHeadModel, """question-answering""": TFFlaubertForQuestionAnsweringSimple, """text-classification""": TFFlaubertForSequenceClassification, """token-classification""": TFFlaubertForTokenClassification, """zero-shot""": TFFlaubertForSequenceClassification, } if is_tf_available() else {} ) _snake_case : List[Any] = False _snake_case : Tuple = False def __a ( self :Optional[int] , lowerCamelCase__ :List[Any] , lowerCamelCase__ :Dict , lowerCamelCase__ :int , lowerCamelCase__ :str , lowerCamelCase__ :List[Any] ): if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith("""Fast""" ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def __a ( self :List[str] ): UpperCamelCase__ :List[str] = TFFlaubertModelTester(self ) UpperCamelCase__ :Tuple = ConfigTester(self , config_class=lowerCamelCase__ , emb_dim=37 ) def __a ( self :int ): self.config_tester.run_common_tests() def __a ( self :List[str] ): UpperCamelCase__ :List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*lowerCamelCase__ ) def __a ( self :Tuple ): UpperCamelCase__ :Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*lowerCamelCase__ ) def __a ( self :Union[str, Any] ): UpperCamelCase__ :Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*lowerCamelCase__ ) def __a ( self :List[Any] ): UpperCamelCase__ :Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*lowerCamelCase__ ) def __a ( self :Any ): UpperCamelCase__ :Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_token_classification(*lowerCamelCase__ ) def __a ( self :List[Any] ): UpperCamelCase__ :Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_multiple_choice(*lowerCamelCase__ ) @slow def __a ( self :str ): for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase__ :Dict = TFFlaubertModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) @require_tf @require_sentencepiece @require_tokenizers class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" @slow def __a ( self :str ): UpperCamelCase__ :Tuple = TFFlaubertModel.from_pretrained("""jplu/tf-flaubert-small-cased""" ) UpperCamelCase__ :Optional[int] = tf.convert_to_tensor( [[0, 1_58, 7_35, 25_92, 14_24, 67_27, 82, 1]] , dtype=tf.intaa , ) # "J'aime flaubert !" UpperCamelCase__ :Optional[Any] = model(lowerCamelCase__ )[0] UpperCamelCase__ :Optional[int] = tf.TensorShape((1, 8, 5_12) ) self.assertEqual(output.shape , lowerCamelCase__ ) # compare the actual values for a slice. UpperCamelCase__ :str = tf.convert_to_tensor( [ [ [-1.876_8773, -1.56_6555, 0.2707_2418], [-1.692_0038, -0.587_3505, 1.932_9599], [-2.956_3985, -1.699_3835, 1.797_2052], ] ] , dtype=tf.floataa , ) self.assertTrue(np.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1e-4 ) )
45
0
import re import warnings from contextlib import contextmanager from ...processing_utils import ProcessorMixin class __magic_name__ (snake_case_ ): '''simple docstring''' __lowercase : List[str] = ['image_processor', 'tokenizer'] __lowercase : str = 'AutoImageProcessor' __lowercase : Dict = 'AutoTokenizer' def __init__( self:int , _a:List[str]=None , _a:Optional[Any]=None , **_a:List[str] ): snake_case__ = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , _a , ) snake_case__ = kwargs.pop('''feature_extractor''' ) snake_case__ = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(_a , _a ) snake_case__ = self.image_processor snake_case__ = False def __call__( self:Optional[int] , *_a:str , **_a:int ): # For backward compatibility if self._in_target_context_manager: return self.current_processor(*_a , **_a ) snake_case__ = kwargs.pop('''images''' , _a ) snake_case__ = kwargs.pop('''text''' , _a ) if len(_a ) > 0: snake_case__ = args[0] snake_case__ = args[1:] if images is None and text is None: raise ValueError('''You need to specify either an `images` or `text` input to process.''' ) if images is not None: snake_case__ = self.image_processor(_a , *_a , **_a ) if text is not None: snake_case__ = self.tokenizer(_a , **_a ) if text is None: return inputs elif images is None: return encodings else: snake_case__ = encodings['''input_ids'''] return inputs def SCREAMING_SNAKE_CASE__ ( self:Union[str, Any] , *_a:Union[str, Any] , **_a:Any ): return self.tokenizer.batch_decode(*_a , **_a ) def SCREAMING_SNAKE_CASE__ ( self:Tuple , *_a:Union[str, Any] , **_a:Optional[int] ): return self.tokenizer.decode(*_a , **_a ) @contextmanager def SCREAMING_SNAKE_CASE__ ( self:Tuple ): warnings.warn( '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your ''' '''labels by using the argument `text` of the regular `__call__` method (either in the same call as ''' '''your images inputs, or in a separate call.''' ) snake_case__ = True snake_case__ = self.tokenizer yield snake_case__ = self.image_processor snake_case__ = False def SCREAMING_SNAKE_CASE__ ( self:List[str] , _a:Dict , _a:Dict=False , _a:Optional[int]=None ): if added_vocab is None: snake_case__ = self.tokenizer.get_added_vocab() snake_case__ = {} while tokens: snake_case__ = re.search(r'''<s_(.*?)>''' , _a , re.IGNORECASE ) if start_token is None: break snake_case__ = start_token.group(1 ) snake_case__ = re.search(rF"""</s_{key}>""" , _a , re.IGNORECASE ) snake_case__ = start_token.group() if end_token is None: snake_case__ = tokens.replace(_a , '''''' ) else: snake_case__ = end_token.group() snake_case__ = re.escape(_a ) snake_case__ = re.escape(_a ) snake_case__ = re.search(F"""{start_token_escaped}(.*?){end_token_escaped}""" , _a , re.IGNORECASE ) if content is not None: snake_case__ = content.group(1 ).strip() if r"<s_" in content and r"</s_" in content: # non-leaf node snake_case__ = self.tokenajson(_a , is_inner_value=_a , added_vocab=_a ) if value: if len(_a ) == 1: snake_case__ = value[0] snake_case__ = value else: # leaf nodes snake_case__ = [] for leaf in content.split(r'''<sep/>''' ): snake_case__ = leaf.strip() if leaf in added_vocab and leaf[0] == "<" and leaf[-2:] == "/>": snake_case__ = leaf[1:-2] # for categorical special tokens output[key].append(_a ) if len(output[key] ) == 1: snake_case__ = output[key][0] snake_case__ = tokens[tokens.find(_a ) + len(_a ) :].strip() if tokens[:6] == r"<sep/>": # non-leaf nodes return [output] + self.tokenajson(tokens[6:] , is_inner_value=_a , added_vocab=_a ) if len(_a ): return [output] if is_inner_value else output else: return [] if is_inner_value else {"text_sequence": tokens} @property def SCREAMING_SNAKE_CASE__ ( self:Optional[Any] ): warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , _a , ) return self.image_processor_class @property def SCREAMING_SNAKE_CASE__ ( self:Optional[int] ): warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , _a , ) return self.image_processor
33
import gc import tempfile import unittest import numpy as np import torch from diffusers import VersatileDiffusionPipeline from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device UpperCamelCase = False class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" pass @nightly @require_torch_gpu class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __a ( self :Union[str, Any] ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __a ( self :List[Any] ): UpperCamelCase__ :List[str] = VersatileDiffusionPipeline.from_pretrained("""shi-labs/versatile-diffusion""" , torch_dtype=torch.floataa ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) UpperCamelCase__ :Dict = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg""" ) UpperCamelCase__ :Any = torch.manual_seed(0 ) UpperCamelCase__ :Optional[int] = pipe.dual_guided( prompt="""first prompt""" , image=lowerCamelCase__ , text_to_image_strength=0.75 , generator=lowerCamelCase__ , guidance_scale=7.5 , num_inference_steps=2 , output_type="""numpy""" , ).images with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(lowerCamelCase__ ) UpperCamelCase__ :List[str] = VersatileDiffusionPipeline.from_pretrained(lowerCamelCase__ , torch_dtype=torch.floataa ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) UpperCamelCase__ :str = generator.manual_seed(0 ) UpperCamelCase__ :str = pipe.dual_guided( prompt="""first prompt""" , image=lowerCamelCase__ , text_to_image_strength=0.75 , generator=lowerCamelCase__ , guidance_scale=7.5 , num_inference_steps=2 , output_type="""numpy""" , ).images assert np.abs(image - new_image ).sum() < 1e-5, "Models don't have the same forward pass" def __a ( self :Dict ): UpperCamelCase__ :List[Any] = VersatileDiffusionPipeline.from_pretrained("""shi-labs/versatile-diffusion""" , torch_dtype=torch.floataa ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = """cyberpunk 2077""" UpperCamelCase__ :str = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg""" ) UpperCamelCase__ :str = torch.manual_seed(0 ) UpperCamelCase__ :Dict = pipe.dual_guided( prompt=lowerCamelCase__ , image=lowerCamelCase__ , text_to_image_strength=0.75 , generator=lowerCamelCase__ , guidance_scale=7.5 , num_inference_steps=50 , output_type="""numpy""" , ).images UpperCamelCase__ :Tuple = image[0, 2_53:2_56, 2_53:2_56, -1] assert image.shape == (1, 5_12, 5_12, 3) UpperCamelCase__ :Any = np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 UpperCamelCase__ :List[Any] = """A painting of a squirrel eating a burger """ UpperCamelCase__ :List[str] = torch.manual_seed(0 ) UpperCamelCase__ :Optional[int] = pipe.text_to_image( prompt=lowerCamelCase__ , generator=lowerCamelCase__ , guidance_scale=7.5 , num_inference_steps=50 , output_type="""numpy""" ).images UpperCamelCase__ :str = image[0, 2_53:2_56, 2_53:2_56, -1] assert image.shape == (1, 5_12, 5_12, 3) UpperCamelCase__ :Union[str, Any] = np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 UpperCamelCase__ :Optional[int] = pipe.image_variation(lowerCamelCase__ , generator=lowerCamelCase__ , output_type="""numpy""" ).images UpperCamelCase__ :int = image[0, 2_53:2_56, 2_53:2_56, -1] assert image.shape == (1, 5_12, 5_12, 3) UpperCamelCase__ :List[Any] = np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1
45
0
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices SCREAMING_SNAKE_CASE_ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE_ = { 'microsoft/swin-tiny-patch4-window7-224': ( 'https://huggingface.co/microsoft/swin-tiny-patch4-window7-224/resolve/main/config.json' ), # See all Swin models at https://huggingface.co/models?filter=swin } class snake_case_ ( lowerCamelCase_ , lowerCamelCase_ ): """simple docstring""" A_ = '''swin''' A_ = { '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers''', } def __init__( self , lowerCamelCase_=2_2_4 , lowerCamelCase_=4 , lowerCamelCase_=3 , lowerCamelCase_=9_6 , lowerCamelCase_=[2, 2, 6, 2] , lowerCamelCase_=[3, 6, 1_2, 2_4] , lowerCamelCase_=7 , lowerCamelCase_=4.0 , lowerCamelCase_=True , lowerCamelCase_=0.0 , lowerCamelCase_=0.0 , lowerCamelCase_=0.1 , lowerCamelCase_="gelu" , lowerCamelCase_=False , lowerCamelCase_=0.02 , lowerCamelCase_=1e-5 , lowerCamelCase_=3_2 , lowerCamelCase_=None , lowerCamelCase_=None , **lowerCamelCase_ , ) -> Any: super().__init__(**lowerCamelCase_) UpperCamelCase = image_size UpperCamelCase = patch_size UpperCamelCase = num_channels UpperCamelCase = embed_dim UpperCamelCase = depths UpperCamelCase = len(lowerCamelCase_) UpperCamelCase = num_heads UpperCamelCase = window_size UpperCamelCase = mlp_ratio UpperCamelCase = qkv_bias UpperCamelCase = hidden_dropout_prob UpperCamelCase = attention_probs_dropout_prob UpperCamelCase = drop_path_rate UpperCamelCase = hidden_act UpperCamelCase = use_absolute_embeddings UpperCamelCase = layer_norm_eps UpperCamelCase = initializer_range UpperCamelCase = encoder_stride # 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 = int(embed_dim * 2 ** (len(lowerCamelCase_) - 1)) UpperCamelCase = ['''stem'''] + [F'stage{idx}' for idx in range(1 , len(lowerCamelCase_) + 1)] UpperCamelCase , UpperCamelCase = get_aligned_output_features_output_indices( out_features=lowerCamelCase_ , out_indices=lowerCamelCase_ , stage_names=self.stage_names) class snake_case_ ( lowerCamelCase_ ): """simple docstring""" A_ = version.parse('''1.11''' ) @property def UpperCAmelCase__ ( self) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ]) @property def UpperCAmelCase__ ( self) -> float: return 1e-4
34
from __future__ import annotations import copy import inspect import unittest import numpy as np from transformers import is_tf_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST, TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, TFLayoutLMvaModel, ) if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class lowerCAmelCase_ : """simple docstring""" def __init__( self :Union[str, Any] , lowerCamelCase__ :Tuple , lowerCamelCase__ :List[str]=2 , lowerCamelCase__ :List[str]=3 , lowerCamelCase__ :List[str]=4 , lowerCamelCase__ :str=2 , lowerCamelCase__ :Optional[int]=7 , lowerCamelCase__ :List[Any]=True , lowerCamelCase__ :Optional[Any]=True , lowerCamelCase__ :Union[str, Any]=True , lowerCamelCase__ :Any=True , lowerCamelCase__ :Dict=99 , lowerCamelCase__ :Optional[Any]=36 , lowerCamelCase__ :str=2 , lowerCamelCase__ :List[Any]=4 , lowerCamelCase__ :Optional[Any]=37 , lowerCamelCase__ :Optional[int]="gelu" , lowerCamelCase__ :Any=0.1 , lowerCamelCase__ :List[Any]=0.1 , lowerCamelCase__ :List[Any]=5_12 , lowerCamelCase__ :str=16 , lowerCamelCase__ :Tuple=2 , lowerCamelCase__ :int=0.02 , lowerCamelCase__ :List[Any]=6 , lowerCamelCase__ :List[str]=6 , lowerCamelCase__ :Optional[int]=3 , lowerCamelCase__ :Optional[int]=4 , lowerCamelCase__ :int=None , lowerCamelCase__ :Optional[Any]=10_00 , ): UpperCamelCase__ :Any = parent UpperCamelCase__ :Union[str, Any] = batch_size UpperCamelCase__ :Dict = num_channels UpperCamelCase__ :Optional[Any] = image_size UpperCamelCase__ :Union[str, Any] = patch_size UpperCamelCase__ :Union[str, Any] = is_training UpperCamelCase__ :str = use_input_mask UpperCamelCase__ :int = use_token_type_ids UpperCamelCase__ :int = use_labels UpperCamelCase__ :List[Any] = vocab_size UpperCamelCase__ :List[str] = hidden_size UpperCamelCase__ :List[Any] = num_hidden_layers UpperCamelCase__ :List[str] = num_attention_heads UpperCamelCase__ :Tuple = intermediate_size UpperCamelCase__ :Any = hidden_act UpperCamelCase__ :Optional[int] = hidden_dropout_prob UpperCamelCase__ :Tuple = attention_probs_dropout_prob UpperCamelCase__ :Dict = max_position_embeddings UpperCamelCase__ :Tuple = type_vocab_size UpperCamelCase__ :Union[str, Any] = type_sequence_label_size UpperCamelCase__ :int = initializer_range UpperCamelCase__ :List[Any] = coordinate_size UpperCamelCase__ :Tuple = shape_size UpperCamelCase__ :Dict = num_labels UpperCamelCase__ :str = num_choices UpperCamelCase__ :Tuple = scope UpperCamelCase__ :str = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) UpperCamelCase__ :List[str] = text_seq_length UpperCamelCase__ :List[str] = (image_size // patch_size) ** 2 + 1 UpperCamelCase__ :Dict = self.text_seq_length + self.image_seq_length def __a ( self :Tuple ): UpperCamelCase__ :Optional[int] = ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size ) UpperCamelCase__ :int = ids_tensor([self.batch_size, self.text_seq_length, 4] , self.range_bbox ) UpperCamelCase__ :str = bbox.numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: UpperCamelCase__ :List[str] = bbox[i, j, 3] UpperCamelCase__ :Optional[int] = bbox[i, j, 1] UpperCamelCase__ :Optional[Any] = tmp_coordinate if bbox[i, j, 2] < bbox[i, j, 0]: UpperCamelCase__ :Tuple = bbox[i, j, 2] UpperCamelCase__ :Optional[Any] = bbox[i, j, 0] UpperCamelCase__ :List[str] = tmp_coordinate UpperCamelCase__ :Dict = tf.constant(lowerCamelCase__ ) UpperCamelCase__ :Union[str, Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCamelCase__ :Any = None if self.use_input_mask: UpperCamelCase__ :int = random_attention_mask([self.batch_size, self.text_seq_length] ) UpperCamelCase__ :Optional[Any] = None if self.use_token_type_ids: UpperCamelCase__ :Optional[int] = ids_tensor([self.batch_size, self.text_seq_length] , self.type_vocab_size ) UpperCamelCase__ :List[str] = None UpperCamelCase__ :Union[str, Any] = None if self.use_labels: UpperCamelCase__ :Union[str, Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase__ :Union[str, Any] = ids_tensor([self.batch_size, self.text_seq_length] , self.num_labels ) UpperCamelCase__ :Optional[int] = LayoutLMvaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , coordinate_size=self.coordinate_size , shape_size=self.shape_size , input_size=self.image_size , patch_size=self.patch_size , ) return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels def __a ( self :List[Any] , lowerCamelCase__ :str , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Dict , lowerCamelCase__ :str , lowerCamelCase__ :int , lowerCamelCase__ :Any ): UpperCamelCase__ :Dict = TFLayoutLMvaModel(config=lowerCamelCase__ ) # text + image UpperCamelCase__ :Tuple = model(lowerCamelCase__ , pixel_values=lowerCamelCase__ , training=lowerCamelCase__ ) UpperCamelCase__ :Tuple = model( lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , training=lowerCamelCase__ , ) UpperCamelCase__ :str = model(lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , training=lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # text only UpperCamelCase__ :Optional[int] = model(lowerCamelCase__ , training=lowerCamelCase__ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size) ) # image only UpperCamelCase__ :Tuple = model({"""pixel_values""": pixel_values} , training=lowerCamelCase__ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size) ) def __a ( self :Dict , lowerCamelCase__ :str , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Dict , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :str , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :str ): UpperCamelCase__ :Optional[Any] = self.num_labels UpperCamelCase__ :List[Any] = TFLayoutLMvaForSequenceClassification(config=lowerCamelCase__ ) UpperCamelCase__ :List[str] = model( lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , labels=lowerCamelCase__ , training=lowerCamelCase__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __a ( self :List[str] , lowerCamelCase__ :List[str] , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Dict , lowerCamelCase__ :List[Any] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Tuple , lowerCamelCase__ :List[str] ): UpperCamelCase__ :Union[str, Any] = self.num_labels UpperCamelCase__ :Dict = TFLayoutLMvaForTokenClassification(config=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = model( lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , labels=lowerCamelCase__ , training=lowerCamelCase__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels) ) def __a ( self :int , lowerCamelCase__ :Dict , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Any , lowerCamelCase__ :Dict , lowerCamelCase__ :Tuple , lowerCamelCase__ :Tuple ): UpperCamelCase__ :Dict = 2 UpperCamelCase__ :Tuple = TFLayoutLMvaForQuestionAnswering(config=lowerCamelCase__ ) UpperCamelCase__ :int = model( lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , start_positions=lowerCamelCase__ , end_positions=lowerCamelCase__ , training=lowerCamelCase__ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __a ( self :List[Any] ): UpperCamelCase__ :Union[str, Any] = self.prepare_config_and_inputs() ((UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__)) :Any = config_and_inputs UpperCamelCase__ :List[str] = { """input_ids""": input_ids, """bbox""": bbox, """pixel_values""": pixel_values, """token_type_ids""": token_type_ids, """attention_mask""": input_mask, } return config, inputs_dict @require_tf class lowerCAmelCase_ ( lowercase , lowercase , unittest.TestCase ): """simple docstring""" _snake_case : Dict = ( ( TFLayoutLMvaModel, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, ) if is_tf_available() else () ) _snake_case : Dict = ( {"""document-question-answering""": TFLayoutLMvaForQuestionAnswering, """feature-extraction""": TFLayoutLMvaModel} if is_tf_available() else {} ) _snake_case : Optional[int] = False _snake_case : List[str] = False _snake_case : Tuple = False def __a ( self :str , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Tuple , lowerCamelCase__ :int ): return True def __a ( self :Optional[int] , lowerCamelCase__ :int , lowerCamelCase__ :List[str] , lowerCamelCase__ :Optional[int]=False ): UpperCamelCase__ :List[str] = copy.deepcopy(lowerCamelCase__ ) if model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :Optional[int] = { k: tf.tile(tf.expand_dims(lowerCamelCase__ , 1 ) , (1, self.model_tester.num_choices) + (1,) * (v.ndim - 1) ) if isinstance(lowerCamelCase__ , tf.Tensor ) and v.ndim > 0 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :str = tf.ones(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :List[str] = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) UpperCamelCase__ :Union[str, Any] = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :Optional[Any] = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :Tuple = tf.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=tf.intaa ) return inputs_dict def __a ( self :Dict ): UpperCamelCase__ :List[Any] = TFLayoutLMvaModelTester(self ) UpperCamelCase__ :Optional[int] = ConfigTester(self , config_class=lowerCamelCase__ , hidden_size=37 ) def __a ( self :Any ): self.config_tester.run_common_tests() def __a ( self :Optional[int] ): UpperCamelCase__ , UpperCamelCase__ :Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase__ :Optional[int] = model_class(lowerCamelCase__ ) if getattr(lowerCamelCase__ , """hf_compute_loss""" , lowerCamelCase__ ): # The number of elements in the loss should be the same as the number of elements in the label UpperCamelCase__ :Union[str, Any] = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) UpperCamelCase__ :int = prepared_for_class[ sorted(prepared_for_class.keys() - inputs_dict.keys() , reverse=lowerCamelCase__ )[0] ] UpperCamelCase__ :Union[str, Any] = added_label.shape.as_list()[:1] # Test that model correctly compute the loss with kwargs UpperCamelCase__ :List[Any] = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = prepared_for_class.pop("""input_ids""" ) UpperCamelCase__ :List[str] = model(lowerCamelCase__ , **lowerCamelCase__ )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss when we mask some positions UpperCamelCase__ :Union[str, Any] = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = prepared_for_class.pop("""input_ids""" ) if "labels" in prepared_for_class: UpperCamelCase__ :List[str] = prepared_for_class["""labels"""].numpy() if len(labels.shape ) > 1 and labels.shape[1] != 1: UpperCamelCase__ :Optional[Any] = -1_00 UpperCamelCase__ :Union[str, Any] = tf.convert_to_tensor(lowerCamelCase__ ) UpperCamelCase__ :Tuple = model(lowerCamelCase__ , **lowerCamelCase__ )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) self.assertTrue(not np.any(np.isnan(loss.numpy() ) ) ) # Test that model correctly compute the loss with a dict UpperCamelCase__ :Optional[Any] = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) UpperCamelCase__ :Union[str, Any] = model(lowerCamelCase__ )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss with a tuple UpperCamelCase__ :Dict = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) # Get keys that were added with the _prepare_for_class function UpperCamelCase__ :str = prepared_for_class.keys() - inputs_dict.keys() UpperCamelCase__ :Tuple = inspect.signature(model.call ).parameters UpperCamelCase__ :str = list(signature.keys() ) # Create a dictionary holding the location of the tensors in the tuple UpperCamelCase__ :Any = {0: """input_ids"""} for label_key in label_keys: UpperCamelCase__ :Dict = signature_names.index(lowerCamelCase__ ) UpperCamelCase__ :Optional[int] = label_key UpperCamelCase__ :Optional[Any] = sorted(tuple_index_mapping.items() ) # Initialize a list with their default values, update the values and convert to a tuple UpperCamelCase__ :Any = [] for name in signature_names: if name != "kwargs": list_input.append(signature[name].default ) for index, value in sorted_tuple_index_mapping: UpperCamelCase__ :List[str] = prepared_for_class[value] UpperCamelCase__ :Union[str, Any] = tuple(lowerCamelCase__ ) # Send to model UpperCamelCase__ :str = model(tuple_input[:-1] )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) def __a ( self :Optional[int] ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Any ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :List[Any] = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: UpperCamelCase__ :Dict = type self.model_tester.create_and_check_model(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Tuple ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Optional[int] ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :List[str] ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) @slow def __a ( self :Optional[int] ): for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase__ :Dict = TFLayoutLMvaModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) def A ( ) -> List[str]: UpperCamelCase__ :List[Any] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_tf class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" @cached_property def __a ( self :Optional[Any] ): return LayoutLMvaImageProcessor(apply_ocr=lowerCamelCase__ ) if is_vision_available() else None @slow def __a ( self :Dict ): UpperCamelCase__ :List[str] = TFLayoutLMvaModel.from_pretrained("""microsoft/layoutlmv3-base""" ) UpperCamelCase__ :List[Any] = self.default_image_processor UpperCamelCase__ :str = prepare_img() UpperCamelCase__ :Any = image_processor(images=lowerCamelCase__ , return_tensors="""tf""" ).pixel_values UpperCamelCase__ :str = tf.constant([[1, 2]] ) UpperCamelCase__ :Any = tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]] ) , axis=0 ) # forward pass UpperCamelCase__ :Dict = model(input_ids=lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , training=lowerCamelCase__ ) # verify the logits UpperCamelCase__ :int = (1, 1_99, 7_68) self.assertEqual(outputs.last_hidden_state.shape , lowerCamelCase__ ) UpperCamelCase__ :List[Any] = tf.constant( [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]] ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] , lowerCamelCase__ , atol=1e-4 ) )
45
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available a_ :Optional[Any] = { 'configuration_roc_bert': ['ROC_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'RoCBertConfig'], 'tokenization_roc_bert': ['RoCBertTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: pass try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ :Union[str, Any] = [ 'ROC_BERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'RoCBertForCausalLM', 'RoCBertForMaskedLM', 'RoCBertForMultipleChoice', 'RoCBertForPreTraining', 'RoCBertForQuestionAnswering', 'RoCBertForSequenceClassification', 'RoCBertForTokenClassification', 'RoCBertLayer', 'RoCBertModel', 'RoCBertPreTrainedModel', 'load_tf_weights_in_roc_bert', ] if TYPE_CHECKING: from .configuration_roc_bert import ROC_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RoCBertConfig from .tokenization_roc_bert import RoCBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: raise OptionalDependencyNotAvailable() try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roc_bert import ( ROC_BERT_PRETRAINED_MODEL_ARCHIVE_LIST, RoCBertForCausalLM, RoCBertForMaskedLM, RoCBertForMultipleChoice, RoCBertForPreTraining, RoCBertForQuestionAnswering, RoCBertForSequenceClassification, RoCBertForTokenClassification, RoCBertLayer, RoCBertModel, RoCBertPreTrainedModel, load_tf_weights_in_roc_bert, ) else: import sys a_ :List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
35
import logging import os import sys from dataclasses import dataclass, field from typing import Optional import torch from datasets import load_dataset from torchvision.transforms import Compose, Lambda, Normalize, RandomHorizontalFlip, RandomResizedCrop, ToTensor from torchvision.transforms.functional import InterpolationMode import transformers from transformers import ( HfArgumentParser, Trainer, TrainingArguments, ViTImageProcessor, ViTMAEConfig, ViTMAEForPreTraining, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version UpperCamelCase = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.31.0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/image-pretraining/requirements.txt") @dataclass class lowerCAmelCase_ : """simple docstring""" _snake_case : Optional[str] = field( default="""cifar10""" , metadata={"""help""": """Name of a dataset from the datasets package"""} ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """The column name of the images in the files."""} ) _snake_case : Optional[str] = field(default=lowercase , metadata={"""help""": """A folder containing the training data."""} ) _snake_case : Optional[str] = field(default=lowercase , metadata={"""help""": """A folder containing the validation data."""} ) _snake_case : Optional[float] = field( default=0.15 , metadata={"""help""": """Percent to split off of train for validation."""} ) _snake_case : Optional[int] = field( default=lowercase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } , ) _snake_case : Optional[int] = field( default=lowercase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of evaluation examples to this """ """value if set.""" ) } , ) def __a ( self :List[str] ): UpperCamelCase__ :Optional[Any] = {} if self.train_dir is not None: UpperCamelCase__ :int = self.train_dir if self.validation_dir is not None: UpperCamelCase__ :List[str] = self.validation_dir UpperCamelCase__ :Optional[int] = data_files if data_files else None @dataclass class lowerCAmelCase_ : """simple docstring""" _snake_case : str = field( default=lowercase , metadata={ """help""": ( """The model checkpoint for weights initialization.Don't set if you want to train a model from scratch.""" ) } , ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """Pretrained config name or path if not the same as model_name_or_path"""} ) _snake_case : Optional[str] = field( default=lowercase , metadata={ """help""": ( """Override some existing default config settings when a model is trained from scratch. Example: """ """n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index""" ) } , ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """Where do you want to store the pretrained models downloaded from s3"""} ) _snake_case : str = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) _snake_case : str = field(default=lowercase , metadata={"""help""": """Name or path of preprocessor config."""} ) _snake_case : bool = field( default=lowercase , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) _snake_case : float = field( default=0.75 , metadata={"""help""": """The ratio of the number of masked tokens in the input sequence."""} ) _snake_case : bool = field( default=lowercase , metadata={"""help""": """Whether or not to train with normalized pixel values as target."""} ) @dataclass class lowerCAmelCase_ ( lowercase ): """simple docstring""" _snake_case : float = field( default=1e-3 , metadata={"""help""": """Base learning rate: absolute_lr = base_lr * total_batch_size / 256."""} ) def A ( lowercase__ : Union[str, Any] ) -> Dict: UpperCamelCase__ :Union[str, Any] = torch.stack([example["""pixel_values"""] for example in examples] ) return {"pixel_values": pixel_values} def A ( ) -> Optional[int]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. UpperCamelCase__ :Optional[int] = HfArgumentParser((ModelArguments, DataTrainingArguments, CustomTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(""".json""" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :str = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Union[str, Any] = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry("""run_mae""" , lowercase__ , lowercase__ ) # Setup logging logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() UpperCamelCase__ :List[str] = training_args.get_process_log_level() logger.setLevel(lowercase__ ) transformers.utils.logging.set_verbosity(lowercase__ ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( f"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}""" + f"""distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}""" ) logger.info(f"""Training/evaluation parameters {training_args}""" ) # Detecting last checkpoint. UpperCamelCase__ :Union[str, Any] = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: UpperCamelCase__ :List[str] = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( f"""Output directory ({training_args.output_dir}) already exists and is not empty. """ """Use --overwrite_output_dir to overcome.""" ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( f"""Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change """ """the `--output_dir` or add `--overwrite_output_dir` to train from scratch.""" ) # Initialize our dataset. UpperCamelCase__ :Tuple = load_dataset( data_args.dataset_name , data_args.dataset_config_name , data_files=data_args.data_files , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) # If we don't have a validation split, split off a percentage of train as validation. UpperCamelCase__ :int = None if """validation""" in ds.keys() else data_args.train_val_split if isinstance(data_args.train_val_split , lowercase__ ) and data_args.train_val_split > 0.0: UpperCamelCase__ :Optional[Any] = ds["""train"""].train_test_split(data_args.train_val_split ) UpperCamelCase__ :Union[str, Any] = split["""train"""] UpperCamelCase__ :Any = split["""test"""] # Load pretrained model and image processor # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. UpperCamelCase__ :Optional[int] = { """cache_dir""": model_args.cache_dir, """revision""": model_args.model_revision, """use_auth_token""": True if model_args.use_auth_token else None, } if model_args.config_name: UpperCamelCase__ :Any = ViTMAEConfig.from_pretrained(model_args.config_name , **lowercase__ ) elif model_args.model_name_or_path: UpperCamelCase__ :Union[str, Any] = ViTMAEConfig.from_pretrained(model_args.model_name_or_path , **lowercase__ ) else: UpperCamelCase__ :Optional[Any] = ViTMAEConfig() logger.warning("""You are instantiating a new config instance from scratch.""" ) if model_args.config_overrides is not None: logger.info(f"""Overriding config: {model_args.config_overrides}""" ) config.update_from_string(model_args.config_overrides ) logger.info(f"""New config: {config}""" ) # adapt config config.update( { """mask_ratio""": model_args.mask_ratio, """norm_pix_loss""": model_args.norm_pix_loss, } ) # create image processor if model_args.image_processor_name: UpperCamelCase__ :str = ViTImageProcessor.from_pretrained(model_args.image_processor_name , **lowercase__ ) elif model_args.model_name_or_path: UpperCamelCase__ :Dict = ViTImageProcessor.from_pretrained(model_args.model_name_or_path , **lowercase__ ) else: UpperCamelCase__ :Tuple = ViTImageProcessor() # create model if model_args.model_name_or_path: UpperCamelCase__ :Any = ViTMAEForPreTraining.from_pretrained( model_args.model_name_or_path , from_tf=bool(""".ckpt""" in model_args.model_name_or_path ) , config=lowercase__ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info("""Training new model from scratch""" ) UpperCamelCase__ :Optional[int] = ViTMAEForPreTraining(lowercase__ ) if training_args.do_train: UpperCamelCase__ :Optional[Any] = ds["""train"""].column_names else: UpperCamelCase__ :Union[str, Any] = ds["""validation"""].column_names if data_args.image_column_name is not None: UpperCamelCase__ :Union[str, Any] = data_args.image_column_name elif "image" in column_names: UpperCamelCase__ :Optional[Any] = """image""" elif "img" in column_names: UpperCamelCase__ :List[str] = """img""" else: UpperCamelCase__ :List[Any] = column_names[0] # transformations as done in original MAE paper # source: https://github.com/facebookresearch/mae/blob/main/main_pretrain.py if "shortest_edge" in image_processor.size: UpperCamelCase__ :List[str] = image_processor.size["""shortest_edge"""] else: UpperCamelCase__ :int = (image_processor.size["""height"""], image_processor.size["""width"""]) UpperCamelCase__ :Any = Compose( [ Lambda(lambda lowercase__ : img.convert("""RGB""" ) if img.mode != "RGB" else img ), RandomResizedCrop(lowercase__ , scale=(0.2, 1.0) , interpolation=InterpolationMode.BICUBIC ), RandomHorizontalFlip(), ToTensor(), Normalize(mean=image_processor.image_mean , std=image_processor.image_std ), ] ) def preprocess_images(lowercase__ : Tuple ): UpperCamelCase__ :List[Any] = [transforms(lowercase__ ) for image in examples[image_column_name]] return examples if training_args.do_train: if "train" not in ds: raise ValueError("""--do_train requires a train dataset""" ) if data_args.max_train_samples is not None: UpperCamelCase__ :Optional[int] = ds["""train"""].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) # Set the training transforms ds["train"].set_transform(lowercase__ ) if training_args.do_eval: if "validation" not in ds: raise ValueError("""--do_eval requires a validation dataset""" ) if data_args.max_eval_samples is not None: UpperCamelCase__ :Optional[Any] = ( ds["""validation"""].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms ds["validation"].set_transform(lowercase__ ) # Compute absolute learning rate UpperCamelCase__ :Tuple = ( training_args.train_batch_size * training_args.gradient_accumulation_steps * training_args.world_size ) if training_args.base_learning_rate is not None: UpperCamelCase__ :Any = training_args.base_learning_rate * total_train_batch_size / 256 # Initialize our trainer UpperCamelCase__ :Union[str, Any] = Trainer( model=lowercase__ , args=lowercase__ , train_dataset=ds["""train"""] if training_args.do_train else None , eval_dataset=ds["""validation"""] if training_args.do_eval else None , tokenizer=lowercase__ , data_collator=lowercase__ , ) # Training if training_args.do_train: UpperCamelCase__ :Any = None if training_args.resume_from_checkpoint is not None: UpperCamelCase__ :int = training_args.resume_from_checkpoint elif last_checkpoint is not None: UpperCamelCase__ :Dict = last_checkpoint UpperCamelCase__ :Union[str, Any] = trainer.train(resume_from_checkpoint=lowercase__ ) trainer.save_model() trainer.log_metrics("""train""" , train_result.metrics ) trainer.save_metrics("""train""" , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: UpperCamelCase__ :int = trainer.evaluate() trainer.log_metrics("""eval""" , lowercase__ ) trainer.save_metrics("""eval""" , lowercase__ ) # Write model card and (optionally) push to hub UpperCamelCase__ :Optional[int] = { """tasks""": """masked-auto-encoding""", """dataset""": data_args.dataset_name, """tags""": ["""masked-auto-encoding"""], } if training_args.push_to_hub: trainer.push_to_hub(**lowercase__ ) else: trainer.create_model_card(**lowercase__ ) def A ( lowercase__ : Union[str, Any] ) -> Dict: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
45
0
import importlib.util import json import os import warnings from dataclasses import dataclass, field import torch from ..training_args import TrainingArguments from ..utils import cached_property, is_sagemaker_dp_enabled, logging __lowercase : List[str] = logging.get_logger(__name__) def lowercase ( ) -> Optional[Any]: '''simple docstring''' snake_case : int = os.getenv("""SM_HP_MP_PARAMETERS""" , """{}""" ) try: # Parse it and check the field "partitions" is included, it is required for model parallel. snake_case : str = json.loads(__A ) if "partitions" not in smp_options: return False except json.JSONDecodeError: return False # Get the sagemaker specific framework parameters from mpi_options variable. snake_case : List[Any] = os.getenv("""SM_FRAMEWORK_PARAMS""" , """{}""" ) try: # Parse it and check the field "sagemaker_distributed_dataparallel_enabled". snake_case : int = json.loads(__A ) if not mpi_options.get("""sagemaker_mpi_enabled""" , __A ): return False except json.JSONDecodeError: return False # Lastly, check if the `smdistributed` module is present. return importlib.util.find_spec("""smdistributed""" ) is not None if is_sagemaker_model_parallel_available(): import smdistributed.modelparallel.torch as smp smp.init() @dataclass class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : str = field( default='''''' , metadata={'''help''': '''Used by the SageMaker launcher to send mp-specific args. Ignored in SageMakerTrainer'''} , ) def snake_case_ ( self ): '''simple docstring''' super().__post_init__() warnings.warn( """`SageMakerTrainingArguments` is deprecated and will be removed in v5 of Transformers. You can use """ """`TrainingArguments` instead.""" ,SCREAMING_SNAKE_CASE_ ,) @cached_property def snake_case_ ( self ): '''simple docstring''' logger.info("""PyTorch: setting up devices""" ) if torch.distributed.is_available() and torch.distributed.is_initialized() and self.local_rank == -1: logger.warning( """torch.distributed process group is initialized, but local_rank == -1. """ """In order to use Torch DDP, launch your script with `python -m torch.distributed.launch""" ) if self.no_cuda: snake_case : Tuple = torch.device("""cpu""" ) snake_case : Optional[Any] = 0 elif is_sagemaker_model_parallel_available(): snake_case : Tuple = smp.local_rank() snake_case : List[str] = torch.device("""cuda""" ,SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = 1 elif is_sagemaker_dp_enabled(): import smdistributed.dataparallel.torch.torch_smddp # noqa: F401 torch.distributed.init_process_group(backend="""smddp""" ,timeout=self.ddp_timeout_delta ) snake_case : Union[str, Any] = int(os.getenv("""SMDATAPARALLEL_LOCAL_RANK""" ) ) snake_case : List[str] = torch.device("""cuda""" ,self.local_rank ) snake_case : Tuple = 1 elif self.local_rank == -1: # if n_gpu is > 1 we'll use nn.DataParallel. # If you only want to use a specific subset of GPUs use `CUDA_VISIBLE_DEVICES=0` # Explicitly set CUDA to the first (index 0) CUDA device, otherwise `set_device` will # trigger an error that a device index is missing. Index 0 takes into account the # GPUs available in the environment, so `CUDA_VISIBLE_DEVICES=1,2` with `cuda:0` # will use the first GPU in that env, i.e. GPU#1 snake_case : Any = torch.device("""cuda:0""" if torch.cuda.is_available() else """cpu""" ) # Sometimes the line in the postinit has not been run before we end up here, so just checking we're not at # the default value. snake_case : Optional[int] = torch.cuda.device_count() else: # Here, we'll use torch.distributed. # Initializes the distributed backend which will take care of synchronizing nodes/GPUs if not torch.distributed.is_initialized(): torch.distributed.init_process_group(backend="""nccl""" ,timeout=self.ddp_timeout_delta ) snake_case : Tuple = torch.device("""cuda""" ,self.local_rank ) snake_case : Optional[int] = 1 if device.type == "cuda": torch.cuda.set_device(SCREAMING_SNAKE_CASE_ ) return device @property def snake_case_ ( self ): '''simple docstring''' if is_sagemaker_model_parallel_available(): return smp.dp_size() return super().world_size @property def snake_case_ ( self ): '''simple docstring''' return not is_sagemaker_model_parallel_available() @property def snake_case_ ( self ): '''simple docstring''' return False
36
from __future__ import annotations def A ( lowercase__ : int ) -> list[int]: UpperCamelCase__ :Union[str, Any] = [True] * limit UpperCamelCase__ :int = False UpperCamelCase__ :Optional[Any] = False UpperCamelCase__ :str = True for i in range(3 , int(limit**0.5 + 1 ) , 2 ): UpperCamelCase__ :List[Any] = i * 2 while index < limit: UpperCamelCase__ :Tuple = False UpperCamelCase__ :Tuple = index + i UpperCamelCase__ :str = [2] for i in range(3 , lowercase__ , 2 ): if is_prime[i]: primes.append(lowercase__ ) return primes def A ( lowercase__ : int = 100_0000 ) -> int: UpperCamelCase__ :Any = prime_sieve(lowercase__ ) UpperCamelCase__ :Optional[int] = 0 UpperCamelCase__ :Optional[Any] = 0 for i in range(len(lowercase__ ) ): for j in range(i + length , len(lowercase__ ) ): UpperCamelCase__ :Any = sum(primes[i:j] ) if sol >= ceiling: break if sol in primes: UpperCamelCase__ :Union[str, Any] = j - i UpperCamelCase__ :Any = sol return largest if __name__ == "__main__": print(f'''{solution() = }''')
45
0
import random import unittest from torch.utils.data import BatchSampler, DataLoader, IterableDataset from accelerate import Accelerator from accelerate.data_loader import ( BatchSamplerShard, DataLoaderDispatcher, DataLoaderShard, IterableDatasetShard, SkipBatchSampler, SkipDataLoader, skip_first_batches, ) class A__ ( A__ ): """simple docstring""" def __init__( self : Any , lowerCamelCase__ : str=0.01 , lowerCamelCase__ : str=1_000 ): a__ : Union[str, Any] = p_stop a__ : List[str] = max_length def __iter__( self : str ): a__ : Optional[int] = 0 a__ : Any = False while not stop and count < self.max_length: yield count count += 1 a__ : List[str] = random.random() < self.p_stop class A__ ( unittest.TestCase ): """simple docstring""" def _UpperCamelCase( self : Tuple , lowerCamelCase__ : List[Any] , lowerCamelCase__ : Dict , lowerCamelCase__ : List[str]=False , lowerCamelCase__ : Optional[int]=True ): a__ : Union[str, Any] = [ BatchSamplerShard(lowerCamelCase__ , 2 , lowerCamelCase__ , split_batches=lowerCamelCase__ , even_batches=lowerCamelCase__ ) for i in range(2 ) ] a__ : Optional[int] = [list(lowerCamelCase__ ) for batch_sampler_shard in batch_sampler_shards] if not split_batches: self.assertListEqual([len(lowerCamelCase__ ) for shard in batch_sampler_shards] , [len(lowerCamelCase__ ) for e in expected] ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) def _UpperCamelCase( self : List[str] ): # Check the shards when the dataset is a round multiple of total batch size. a__ : Union[str, Any] = BatchSampler(range(24 ) , batch_size=3 , drop_last=lowerCamelCase__ ) a__ : Union[str, Any] = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]], [[3, 4, 5], [9, 10, 11], [15, 16, 17], [21, 22, 23]], ] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ ) a__ : Tuple = BatchSampler(range(24 ) , batch_size=3 , drop_last=lowerCamelCase__ ) # Expected shouldn't change self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ ) # Check the shards when the dataset is a round multiple of batch size but not total batch size. a__ : int = BatchSampler(range(21 ) , batch_size=3 , drop_last=lowerCamelCase__ ) a__ : Union[str, Any] = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]], [[3, 4, 5], [9, 10, 11], [15, 16, 17], [0, 1, 2]], ] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ ) a__ : int = BatchSampler(range(21 ) , batch_size=3 , drop_last=lowerCamelCase__ ) a__ : Optional[int] = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ ) # Check the shards when the dataset is not a round multiple of batch size but has a multiple of # num_processes batch. a__ : Optional[int] = BatchSampler(range(22 ) , batch_size=3 , drop_last=lowerCamelCase__ ) a__ : int = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]], [[3, 4, 5], [9, 10, 11], [15, 16, 17], [21, 0, 1]], ] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ ) a__ : Union[str, Any] = BatchSampler(range(22 ) , batch_size=3 , drop_last=lowerCamelCase__ ) a__ : Any = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ ) # Check the shards when the dataset is not a round multiple of batch size but and has not a multiple of # num_processes batch. a__ : List[str] = BatchSampler(range(20 ) , batch_size=3 , drop_last=lowerCamelCase__ ) a__ : str = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 0]], [[3, 4, 5], [9, 10, 11], [15, 16, 17], [1, 2, 3]], ] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ ) a__ : List[Any] = BatchSampler(range(20 ) , batch_size=3 , drop_last=lowerCamelCase__ ) a__ : int = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ ) # Check the shards when the dataset is very small. a__ : str = BatchSampler(range(2 ) , batch_size=3 , drop_last=lowerCamelCase__ ) a__ : List[Any] = [[[0, 1, 0]], [[1, 0, 1]]] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ ) a__ : List[str] = BatchSampler(range(2 ) , batch_size=3 , drop_last=lowerCamelCase__ ) a__ : int = [[], []] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ ) def _UpperCamelCase( self : List[Any] ): # Check the shards when the dataset is a round multiple of batch size. a__ : int = BatchSampler(range(24 ) , batch_size=4 , drop_last=lowerCamelCase__ ) a__ : str = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17], [20, 21]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19], [22, 23]], ] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , split_batches=lowerCamelCase__ ) a__ : str = BatchSampler(range(24 ) , batch_size=4 , drop_last=lowerCamelCase__ ) # Expected shouldn't change self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , split_batches=lowerCamelCase__ ) # Check the shards when the dataset is not a round multiple of batch size. a__ : Any = BatchSampler(range(22 ) , batch_size=4 , drop_last=lowerCamelCase__ ) a__ : Any = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17], [20, 21]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19], [0, 1]], ] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , split_batches=lowerCamelCase__ ) a__ : Any = BatchSampler(range(22 ) , batch_size=4 , drop_last=lowerCamelCase__ ) a__ : Optional[int] = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19]], ] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , split_batches=lowerCamelCase__ ) # Check the shards when the dataset is not a round multiple of batch size or num_processes. a__ : List[Any] = BatchSampler(range(21 ) , batch_size=4 , drop_last=lowerCamelCase__ ) a__ : int = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17], [20, 0]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19], [1, 2]], ] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , split_batches=lowerCamelCase__ ) a__ : List[str] = BatchSampler(range(21 ) , batch_size=4 , drop_last=lowerCamelCase__ ) a__ : Tuple = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19]], ] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , split_batches=lowerCamelCase__ ) # Check the shards when the dataset is very small. a__ : Optional[int] = BatchSampler(range(2 ) , batch_size=4 , drop_last=lowerCamelCase__ ) a__ : Optional[int] = [[[0, 1]], [[0, 1]]] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , split_batches=lowerCamelCase__ ) a__ : Optional[int] = BatchSampler(range(2 ) , batch_size=4 , drop_last=lowerCamelCase__ ) a__ : str = [[], []] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , split_batches=lowerCamelCase__ ) def _UpperCamelCase( self : Tuple ): # Check the shards when the dataset is a round multiple of total batch size. a__ : str = BatchSampler(range(24 ) , batch_size=3 , drop_last=lowerCamelCase__ ) a__ : Optional[Any] = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]], [[3, 4, 5], [9, 10, 11], [15, 16, 17], [21, 22, 23]], ] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , even_batches=lowerCamelCase__ ) a__ : str = BatchSampler(range(24 ) , batch_size=3 , drop_last=lowerCamelCase__ ) # Expected shouldn't change self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , even_batches=lowerCamelCase__ ) # Check the shards when the dataset is a round multiple of batch size but not total batch size. a__ : int = BatchSampler(range(21 ) , batch_size=3 , drop_last=lowerCamelCase__ ) a__ : int = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , even_batches=lowerCamelCase__ ) a__ : Any = BatchSampler(range(21 ) , batch_size=3 , drop_last=lowerCamelCase__ ) a__ : str = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , even_batches=lowerCamelCase__ ) # Check the shards when the dataset is not a round multiple of batch size but has a multiple of # num_processes batch. a__ : Optional[int] = BatchSampler(range(22 ) , batch_size=3 , drop_last=lowerCamelCase__ ) a__ : Any = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]], [[3, 4, 5], [9, 10, 11], [15, 16, 17], [21]], ] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , even_batches=lowerCamelCase__ ) a__ : int = BatchSampler(range(22 ) , batch_size=3 , drop_last=lowerCamelCase__ ) a__ : int = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , even_batches=lowerCamelCase__ ) # Check the shards when the dataset is not a round multiple of batch size but and has not a multiple of # num_processes batch. a__ : int = BatchSampler(range(20 ) , batch_size=3 , drop_last=lowerCamelCase__ ) a__ : int = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , even_batches=lowerCamelCase__ ) a__ : Optional[int] = BatchSampler(range(20 ) , batch_size=3 , drop_last=lowerCamelCase__ ) a__ : Tuple = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , even_batches=lowerCamelCase__ ) # Check the shards when the dataset is very small. a__ : int = BatchSampler(range(2 ) , batch_size=3 , drop_last=lowerCamelCase__ ) a__ : List[str] = [[[0, 1]], []] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , even_batches=lowerCamelCase__ ) a__ : List[str] = BatchSampler(range(2 ) , batch_size=3 , drop_last=lowerCamelCase__ ) a__ : Tuple = [[], []] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , even_batches=lowerCamelCase__ ) def _UpperCamelCase( self : Tuple ): # Check the shards when the dataset is a round multiple of batch size. a__ : int = BatchSampler(range(24 ) , batch_size=4 , drop_last=lowerCamelCase__ ) a__ : Tuple = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17], [20, 21]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19], [22, 23]], ] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , split_batches=lowerCamelCase__ , even_batches=lowerCamelCase__ ) a__ : str = BatchSampler(range(24 ) , batch_size=4 , drop_last=lowerCamelCase__ ) # Expected shouldn't change self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , split_batches=lowerCamelCase__ , even_batches=lowerCamelCase__ ) # Check the shards when the dataset is not a round multiple of batch size. a__ : Tuple = BatchSampler(range(22 ) , batch_size=4 , drop_last=lowerCamelCase__ ) a__ : Dict = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17], [20, 21]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19]], ] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , split_batches=lowerCamelCase__ , even_batches=lowerCamelCase__ ) a__ : List[Any] = BatchSampler(range(22 ) , batch_size=4 , drop_last=lowerCamelCase__ ) a__ : List[Any] = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19]], ] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , split_batches=lowerCamelCase__ , even_batches=lowerCamelCase__ ) # Check the shards when the dataset is not a round multiple of batch size or num_processes. a__ : Tuple = BatchSampler(range(21 ) , batch_size=4 , drop_last=lowerCamelCase__ ) a__ : Dict = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17], [20]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19]], ] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , split_batches=lowerCamelCase__ , even_batches=lowerCamelCase__ ) a__ : Union[str, Any] = BatchSampler(range(21 ) , batch_size=4 , drop_last=lowerCamelCase__ ) a__ : Union[str, Any] = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19]], ] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , split_batches=lowerCamelCase__ , even_batches=lowerCamelCase__ ) # Check the shards when the dataset is very small. a__ : Tuple = BatchSampler(range(2 ) , batch_size=4 , drop_last=lowerCamelCase__ ) a__ : Union[str, Any] = [[[0, 1]], []] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , split_batches=lowerCamelCase__ , even_batches=lowerCamelCase__ ) a__ : Union[str, Any] = BatchSampler(range(2 ) , batch_size=4 , drop_last=lowerCamelCase__ ) a__ : List[str] = [[], []] self.check_batch_sampler_shards(lowerCamelCase__ , lowerCamelCase__ , split_batches=lowerCamelCase__ , even_batches=lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] ): a__ : List[str] = [[0, 1, 2], [3, 4], [5, 6, 7, 8], [9, 10, 11], [12, 13]] a__ : Optional[Any] = [BatchSamplerShard(lowerCamelCase__ , 2 , lowerCamelCase__ , even_batches=lowerCamelCase__ ) for i in range(2 )] self.assertEqual(len(batch_sampler_shards[0] ) , 3 ) self.assertEqual(len(batch_sampler_shards[1] ) , 2 ) self.assertListEqual(list(batch_sampler_shards[0] ) , [[0, 1, 2], [5, 6, 7, 8], [12, 13]] ) self.assertListEqual(list(batch_sampler_shards[1] ) , [[3, 4], [9, 10, 11]] ) def _UpperCamelCase( self : Tuple , lowerCamelCase__ : Dict , lowerCamelCase__ : List[str] , lowerCamelCase__ : str , lowerCamelCase__ : Union[str, Any]=False , lowerCamelCase__ : str=2 , lowerCamelCase__ : List[Any]=False ): random.seed(lowerCamelCase__ ) a__ : int = list(lowerCamelCase__ ) a__ : Dict = [ IterableDatasetShard( lowerCamelCase__ , batch_size=lowerCamelCase__ , drop_last=lowerCamelCase__ , num_processes=lowerCamelCase__ , process_index=lowerCamelCase__ , split_batches=lowerCamelCase__ , ) for i in range(lowerCamelCase__ ) ] a__ : Union[str, Any] = [] for iterable_dataset_shard in iterable_dataset_shards: # Since our random iterable dataset will be... random... we need to use a seed to get reproducible results. random.seed(lowerCamelCase__ ) iterable_dataset_lists.append(list(lowerCamelCase__ ) ) a__ : Dict = batch_size // num_processes if split_batches else batch_size # All iterable dataset shard should have the same length, a round multiple of shard_batch_size a__ : Dict = iterable_dataset_lists[0] for l in iterable_dataset_lists[1:]: self.assertEqual(len(lowerCamelCase__ ) , len(lowerCamelCase__ ) ) self.assertTrue(len(lowerCamelCase__ ) % shard_batch_size == 0 ) a__ : List[Any] = [] for idx in range(0 , len(lowerCamelCase__ ) , lowerCamelCase__ ): for l in iterable_dataset_lists: observed += l[idx : idx + shard_batch_size] if not drop_last: while len(lowerCamelCase__ ) < len(lowerCamelCase__ ): reference += reference self.assertListEqual(lowerCamelCase__ , reference[: len(lowerCamelCase__ )] ) def _UpperCamelCase( self : Tuple ): a__ : Union[str, Any] = 42 a__ : str = RandomIterableDataset() self.check_iterable_dataset_shards(lowerCamelCase__ , lowerCamelCase__ , batch_size=4 , drop_last=lowerCamelCase__ , split_batches=lowerCamelCase__ ) self.check_iterable_dataset_shards(lowerCamelCase__ , lowerCamelCase__ , batch_size=4 , drop_last=lowerCamelCase__ , split_batches=lowerCamelCase__ ) self.check_iterable_dataset_shards(lowerCamelCase__ , lowerCamelCase__ , batch_size=4 , drop_last=lowerCamelCase__ , split_batches=lowerCamelCase__ ) self.check_iterable_dataset_shards(lowerCamelCase__ , lowerCamelCase__ , batch_size=4 , drop_last=lowerCamelCase__ , split_batches=lowerCamelCase__ ) # Edge case with a very small dataset a__ : Union[str, Any] = RandomIterableDataset(max_length=2 ) self.check_iterable_dataset_shards(lowerCamelCase__ , lowerCamelCase__ , batch_size=4 , drop_last=lowerCamelCase__ , split_batches=lowerCamelCase__ ) self.check_iterable_dataset_shards(lowerCamelCase__ , lowerCamelCase__ , batch_size=4 , drop_last=lowerCamelCase__ , split_batches=lowerCamelCase__ ) self.check_iterable_dataset_shards(lowerCamelCase__ , lowerCamelCase__ , batch_size=4 , drop_last=lowerCamelCase__ , split_batches=lowerCamelCase__ ) self.check_iterable_dataset_shards(lowerCamelCase__ , lowerCamelCase__ , batch_size=4 , drop_last=lowerCamelCase__ , split_batches=lowerCamelCase__ ) def _UpperCamelCase( self : Optional[Any] ): a__ : int = BatchSampler(range(16 ) , batch_size=4 , drop_last=lowerCamelCase__ ) a__ : str = SkipBatchSampler(lowerCamelCase__ , 2 ) self.assertListEqual(list(lowerCamelCase__ ) , [[8, 9, 10, 11], [12, 13, 14, 15]] ) def _UpperCamelCase( self : List[Any] ): a__ : Dict = SkipDataLoader(list(range(16 ) ) , batch_size=4 , skip_batches=2 ) self.assertListEqual([t.tolist() for t in dataloader] , [[8, 9, 10, 11], [12, 13, 14, 15]] ) def _UpperCamelCase( self : List[str] ): a__ : Optional[Any] = DataLoader(list(range(16 ) ) , batch_size=4 ) a__ : Optional[int] = skip_first_batches(lowerCamelCase__ , num_batches=2 ) self.assertListEqual([t.tolist() for t in new_dataloader] , [[8, 9, 10, 11], [12, 13, 14, 15]] ) def _UpperCamelCase( self : Any ): a__ : List[str] = DataLoaderShard(list(range(16 ) ) , batch_size=4 ) for idx, _ in enumerate(lowerCamelCase__ ): self.assertEqual(dataloader.end_of_dataloader , idx == 3 ) # Test it also works on the second iteration for idx, _ in enumerate(lowerCamelCase__ ): self.assertEqual(dataloader.end_of_dataloader , idx == 3 ) def _UpperCamelCase( self : Optional[Any] ): Accelerator() a__ : List[Any] = DataLoaderDispatcher(range(16 ) , batch_size=4 ) for idx, _ in enumerate(lowerCamelCase__ ): self.assertEqual(dataloader.end_of_dataloader , idx == 3 ) # Test it also works on the second iteration for idx, _ in enumerate(lowerCamelCase__ ): self.assertEqual(dataloader.end_of_dataloader , idx == 3 )
37
import unittest from transformers import GPTNeoXJapaneseConfig, is_torch_available from transformers.models.gpt_neox_japanese.tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer from transformers.testing_utils import 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 GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseModel class lowerCAmelCase_ : """simple docstring""" def __init__( self :Optional[Any] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Tuple=13 , lowerCamelCase__ :Tuple=7 , lowerCamelCase__ :Optional[Any]=True , lowerCamelCase__ :Union[str, Any]=True , lowerCamelCase__ :Optional[int]=True , lowerCamelCase__ :List[Any]=True , lowerCamelCase__ :List[str]=99 , lowerCamelCase__ :int=32 , lowerCamelCase__ :List[Any]=5 , lowerCamelCase__ :Tuple=4 , lowerCamelCase__ :List[Any]=4 , lowerCamelCase__ :str="gelu" , lowerCamelCase__ :Optional[Any]=0.0 , lowerCamelCase__ :Optional[int]=0.1 , lowerCamelCase__ :str=True , lowerCamelCase__ :Dict=5_12 , lowerCamelCase__ :Optional[Any]=16 , lowerCamelCase__ :Optional[Any]=2 , lowerCamelCase__ :Union[str, Any]=0.02 , lowerCamelCase__ :Union[str, Any]=3 , lowerCamelCase__ :int=4 , lowerCamelCase__ :str=None , ): UpperCamelCase__ :Optional[Any] = parent UpperCamelCase__ :Dict = batch_size UpperCamelCase__ :Tuple = seq_length UpperCamelCase__ :Dict = is_training UpperCamelCase__ :List[str] = use_input_mask UpperCamelCase__ :Optional[Any] = use_token_type_ids UpperCamelCase__ :Tuple = use_labels UpperCamelCase__ :int = vocab_size UpperCamelCase__ :Tuple = hidden_size UpperCamelCase__ :Optional[Any] = num_hidden_layers UpperCamelCase__ :int = num_attention_heads UpperCamelCase__ :Optional[int] = intermediate_multiple_size UpperCamelCase__ :Optional[Any] = hidden_act UpperCamelCase__ :Optional[int] = hidden_dropout UpperCamelCase__ :List[Any] = attention_dropout UpperCamelCase__ :List[str] = weight_tying UpperCamelCase__ :List[str] = max_position_embeddings UpperCamelCase__ :Dict = type_vocab_size UpperCamelCase__ :List[Any] = type_sequence_label_size UpperCamelCase__ :List[str] = initializer_range UpperCamelCase__ :int = num_labels UpperCamelCase__ :Dict = num_choices UpperCamelCase__ :Any = scope def __a ( self :Any ): UpperCamelCase__ :Tuple = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCamelCase__ :str = None if self.use_input_mask: UpperCamelCase__ :Optional[int] = random_attention_mask([self.batch_size, self.seq_length] ) UpperCamelCase__ :Union[str, Any] = None if self.use_labels: UpperCamelCase__ :Tuple = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCamelCase__ :Optional[Any] = self.get_config() return config, input_ids, input_mask, token_labels def __a ( self :Union[str, Any] ): return GPTNeoXJapaneseConfig( 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_multiple_size=self.intermediate_multiple_size , hidden_act=self.hidden_act , hidden_dropout=self.hidden_dropout , attention_dropout=self.attention_dropout , weight_tying=self.weight_tying , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=lowerCamelCase__ , initializer_range=self.initializer_range , ) def __a ( self :Union[str, Any] ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Optional[Any] = self.prepare_config_and_inputs() UpperCamelCase__ :Optional[int] = True return config, input_ids, input_mask, token_labels def __a ( self :List[str] , lowerCamelCase__ :List[Any] , lowerCamelCase__ :Any , lowerCamelCase__ :Any ): UpperCamelCase__ :Union[str, Any] = GPTNeoXJapaneseModel(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() UpperCamelCase__ :Tuple = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = model(lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __a ( self :Dict , lowerCamelCase__ :Dict , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :List[Any] ): UpperCamelCase__ :List[str] = True UpperCamelCase__ :int = GPTNeoXJapaneseModel(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() UpperCamelCase__ :Union[str, Any] = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __a ( self :List[Any] , lowerCamelCase__ :Tuple , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Any , lowerCamelCase__ :Optional[Any] ): UpperCamelCase__ :Any = GPTNeoXJapaneseForCausalLM(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() UpperCamelCase__ :Tuple = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ , labels=lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __a ( self :Any , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :List[str] ): UpperCamelCase__ :Union[str, Any] = True UpperCamelCase__ :List[str] = GPTNeoXJapaneseForCausalLM(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() # first forward pass UpperCamelCase__ :Optional[Any] = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ , use_cache=lowerCamelCase__ ) UpperCamelCase__ :List[Any] = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids UpperCamelCase__ :List[Any] = ids_tensor((self.batch_size, 3) , config.vocab_size ) UpperCamelCase__ :Optional[Any] = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and UpperCamelCase__ :Union[str, Any] = torch.cat([input_ids, next_tokens] , dim=-1 ) UpperCamelCase__ :Optional[int] = torch.cat([input_mask, next_mask] , dim=-1 ) UpperCamelCase__ :Union[str, Any] = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ , output_hidden_states=lowerCamelCase__ ) UpperCamelCase__ :Optional[int] = output_from_no_past["""hidden_states"""][0] UpperCamelCase__ :Union[str, Any] = model( lowerCamelCase__ , attention_mask=lowerCamelCase__ , past_key_values=lowerCamelCase__ , output_hidden_states=lowerCamelCase__ , )["""hidden_states"""][0] # select random slice UpperCamelCase__ :int = ids_tensor((1,) , output_from_past.shape[-1] ).item() UpperCamelCase__ :str = output_from_no_past[:, -3:, random_slice_idx].detach() UpperCamelCase__ :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(lowerCamelCase__ , lowerCamelCase__ , atol=1e-3 ) ) def __a ( self :Tuple ): UpperCamelCase__ :int = self.prepare_config_and_inputs() UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :List[Any] = config_and_inputs UpperCamelCase__ :Any = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_torch class lowerCAmelCase_ ( lowercase , lowercase , unittest.TestCase ): """simple docstring""" _snake_case : Dict = (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else () _snake_case : int = (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else () _snake_case : str = ( {"""feature-extraction""": GPTNeoXJapaneseModel, """text-generation""": GPTNeoXJapaneseForCausalLM} if is_torch_available() else {} ) _snake_case : Union[str, Any] = False _snake_case : Dict = False _snake_case : List[str] = False _snake_case : Optional[int] = False def __a ( self :List[Any] ): UpperCamelCase__ :Tuple = GPTNeoXJapaneseModelTester(self ) UpperCamelCase__ :Optional[Any] = ConfigTester(self , config_class=lowerCamelCase__ , hidden_size=37 ) def __a ( self :Dict ): self.config_tester.run_common_tests() def __a ( self :Any ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Any ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Union[str, Any] ): # This regression test was failing with PyTorch < 1.3 UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :List[str] = self.model_tester.prepare_config_and_inputs_for_decoder() UpperCamelCase__ :Dict = None self.model_tester.create_and_check_model_as_decoder(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :List[str] ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Union[str, Any] ): UpperCamelCase__ :int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_causal_lm(*lowerCamelCase__ ) @slow def __a ( self :int ): UpperCamelCase__ :int = """abeja/gpt-neox-japanese-2.7b""" UpperCamelCase__ :List[Any] = ["""データサイエンティストとは、""", """100年後に必要とされる会社は、""", """フルリモートの環境で働くために必要なことは、""", """国境の長いトンネルを抜けると""", """美味しい日本食といえば、"""] UpperCamelCase__ :Union[str, Any] = [ """データサイエンティストとは、データを分析し、ビジネスに役立つ知見を導き出す専門家のことです。""", """100年後に必要とされる会社は、「人」が中心の会社です。""", """フルリモートの環境で働くために必要なことは、「自分の時間をコントロールする」ことです。""", """国境の長いトンネルを抜けると、そこは雪国だった。""", """美味しい日本食といえば、やっぱりお寿司ですよね。""", ] UpperCamelCase__ :Any = GPTNeoXJapaneseTokenizer.from_pretrained(lowerCamelCase__ ) UpperCamelCase__ :List[Any] = GPTNeoXJapaneseForCausalLM.from_pretrained(lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = [] for prompt in prompts: UpperCamelCase__ :str = tokenizer(lowerCamelCase__ , return_tensors="""pt""" ).input_ids UpperCamelCase__ :Union[str, Any] = model.generate(lowerCamelCase__ , max_length=50 ) UpperCamelCase__ :Dict = tokenizer.batch_decode(lowerCamelCase__ , skip_special_tokens=lowerCamelCase__ ) predicted_outputs += generated_string self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ )
45
0
'''simple docstring''' import gc import random import unittest import numpy as np import torch from PIL import Image from diffusers import ( DDIMScheduler, KandinskyVaaInpaintPipeline, KandinskyVaaPriorPipeline, UNetaDConditionModel, VQModel, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class __snake_case ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = KandinskyVaaInpaintPipeline lowerCamelCase__ = ['''image_embeds''', '''negative_image_embeds''', '''image''', '''mask_image'''] lowerCamelCase__ = [ '''image_embeds''', '''negative_image_embeds''', '''image''', '''mask_image''', ] lowerCamelCase__ = [ '''generator''', '''height''', '''width''', '''latents''', '''guidance_scale''', '''num_inference_steps''', '''return_dict''', '''guidance_scale''', '''num_images_per_prompt''', '''output_type''', '''return_dict''', ] lowerCamelCase__ = False @property def __UpperCamelCase ( self ): return 3_2 @property def __UpperCamelCase ( self ): return 3_2 @property def __UpperCamelCase ( self ): return self.time_input_dim @property def __UpperCamelCase ( self ): return self.time_input_dim * 4 @property def __UpperCamelCase ( self ): return 1_0_0 @property def __UpperCamelCase ( self ): torch.manual_seed(0 ) snake_case__ : Any = { """in_channels""": 9, # Out channels is double in channels because predicts mean and variance """out_channels""": 8, """addition_embed_type""": """image""", """down_block_types""": ("""ResnetDownsampleBlock2D""", """SimpleCrossAttnDownBlock2D"""), """up_block_types""": ("""SimpleCrossAttnUpBlock2D""", """ResnetUpsampleBlock2D"""), """mid_block_type""": """UNetMidBlock2DSimpleCrossAttn""", """block_out_channels""": (self.block_out_channels_a, self.block_out_channels_a * 2), """layers_per_block""": 1, """encoder_hid_dim""": self.text_embedder_hidden_size, """encoder_hid_dim_type""": """image_proj""", """cross_attention_dim""": self.cross_attention_dim, """attention_head_dim""": 4, """resnet_time_scale_shift""": """scale_shift""", """class_embed_type""": None, } snake_case__ : int = UNetaDConditionModel(**__SCREAMING_SNAKE_CASE ) return model @property def __UpperCamelCase ( self ): return { "block_out_channels": [3_2, 6_4], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 1_2, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def __UpperCamelCase ( self ): torch.manual_seed(0 ) snake_case__ : Tuple = VQModel(**self.dummy_movq_kwargs ) return model def __UpperCamelCase ( self ): snake_case__ : Optional[int] = self.dummy_unet snake_case__ : Optional[int] = self.dummy_movq snake_case__ : Optional[int] = DDIMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule="""linear""" , beta_start=0.0_0085 , beta_end=0.012 , clip_sample=__SCREAMING_SNAKE_CASE , set_alpha_to_one=__SCREAMING_SNAKE_CASE , steps_offset=1 , prediction_type="""epsilon""" , thresholding=__SCREAMING_SNAKE_CASE , ) snake_case__ : Any = { """unet""": unet, """scheduler""": scheduler, """movq""": movq, } return components def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=0 ): snake_case__ : Dict = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) snake_case__ : Any = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(seed + 1 ) ).to( __SCREAMING_SNAKE_CASE ) # create init_image snake_case__ : Any = floats_tensor((1, 3, 6_4, 6_4) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) snake_case__ : List[Any] = image.cpu().permute(0 , 2 , 3 , 1 )[0] snake_case__ : int = Image.fromarray(np.uinta(__SCREAMING_SNAKE_CASE ) ).convert("""RGB""" ).resize((2_5_6, 2_5_6) ) # create mask snake_case__ : Optional[Any] = np.ones((6_4, 6_4) , dtype=np.floataa ) snake_case__ : Optional[int] = 0 if str(__SCREAMING_SNAKE_CASE ).startswith("""mps""" ): snake_case__ : List[Any] = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: snake_case__ : Optional[Any] = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = { """image""": init_image, """mask_image""": mask, """image_embeds""": image_embeds, """negative_image_embeds""": negative_image_embeds, """generator""": generator, """height""": 6_4, """width""": 6_4, """num_inference_steps""": 2, """guidance_scale""": 4.0, """output_type""": """np""", } return inputs def __UpperCamelCase ( self ): snake_case__ : Any = """cpu""" snake_case__ : Optional[Any] = self.get_dummy_components() snake_case__ : List[str] = self.pipeline_class(**__SCREAMING_SNAKE_CASE ) snake_case__ : str = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) snake_case__ : str = pipe(**self.get_dummy_inputs(__SCREAMING_SNAKE_CASE ) ) snake_case__ : Optional[Any] = output.images snake_case__ : Union[str, Any] = pipe( **self.get_dummy_inputs(__SCREAMING_SNAKE_CASE ) , return_dict=__SCREAMING_SNAKE_CASE , )[0] snake_case__ : int = image[0, -3:, -3:, -1] snake_case__ : Optional[int] = image_from_tuple[0, -3:, -3:, -1] print(f"image.shape {image.shape}" ) assert image.shape == (1, 6_4, 6_4, 3) snake_case__ : str = np.array( [0.5077_5903, 0.4952_7195, 0.4882_4543, 0.5019_2237, 0.4864_4906, 0.4937_3814, 0.478_0598, 0.4723_4827, 0.4832_7848] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), f" expected_slice {expected_slice}, but got {image_slice.flatten()}" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), f" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}" def __UpperCamelCase ( self ): super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class __snake_case ( unittest.TestCase ): '''simple docstring''' def __UpperCamelCase ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __UpperCamelCase ( self ): snake_case__ : Optional[Any] = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinskyv22/kandinskyv22_inpaint_cat_with_hat_fp16.npy""" ) snake_case__ : Any = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinsky/cat.png""" ) snake_case__ : Tuple = np.ones((7_6_8, 7_6_8) , dtype=np.floataa ) snake_case__ : str = 0 snake_case__ : List[str] = """a hat""" snake_case__ : Union[str, Any] = KandinskyVaaPriorPipeline.from_pretrained( """kandinsky-community/kandinsky-2-2-prior""" , torch_dtype=torch.floataa ) pipe_prior.to(__SCREAMING_SNAKE_CASE ) snake_case__ : Union[str, Any] = KandinskyVaaInpaintPipeline.from_pretrained( """kandinsky-community/kandinsky-2-2-decoder-inpaint""" , torch_dtype=torch.floataa ) snake_case__ : Union[str, Any] = pipeline.to(__SCREAMING_SNAKE_CASE ) pipeline.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = torch.Generator(device="""cpu""" ).manual_seed(0 ) snake_case__ , snake_case__ : int = pipe_prior( __SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , num_inference_steps=5 , negative_prompt="""""" , ).to_tuple() snake_case__ : List[str] = pipeline( image=__SCREAMING_SNAKE_CASE , mask_image=__SCREAMING_SNAKE_CASE , image_embeds=__SCREAMING_SNAKE_CASE , negative_image_embeds=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , num_inference_steps=1_0_0 , height=7_6_8 , width=7_6_8 , output_type="""np""" , ) snake_case__ : List[Any] = output.images[0] assert image.shape == (7_6_8, 7_6_8, 3) assert_mean_pixel_difference(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )
38
import numpy as np from matplotlib import pyplot as plt from sklearn.datasets import load_iris from sklearn.metrics import ConfusionMatrixDisplay from sklearn.model_selection import train_test_split from xgboost import XGBClassifier def A ( lowercase__ : dict ) -> tuple: return (data["data"], data["target"]) def A ( lowercase__ : np.ndarray , lowercase__ : np.ndarray ) -> XGBClassifier: UpperCamelCase__ :Tuple = XGBClassifier() classifier.fit(lowercase__ , lowercase__ ) return classifier def A ( ) -> None: UpperCamelCase__ :str = load_iris() UpperCamelCase__ , UpperCamelCase__ :int = data_handling(lowercase__ ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :int = train_test_split( lowercase__ , lowercase__ , test_size=0.25 ) UpperCamelCase__ :Optional[int] = iris["""target_names"""] # Create an XGBoost Classifier from the training data UpperCamelCase__ :Optional[Any] = xgboost(lowercase__ , lowercase__ ) # Display the confusion matrix of the classifier with both training and test sets ConfusionMatrixDisplay.from_estimator( lowercase__ , lowercase__ , lowercase__ , display_labels=lowercase__ , cmap="""Blues""" , normalize="""true""" , ) plt.title("""Normalized Confusion Matrix - IRIS Dataset""" ) plt.show() if __name__ == "__main__": import doctest doctest.testmod(verbose=True) main()
45
0
def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): if not numbers: return 0 if not isinstance(SCREAMING_SNAKE_CASE__ , (list, tuple) ) or not all( isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) for number in numbers ): raise ValueError('''numbers must be an iterable of integers''' ) snake_case_ = snake_case_ = snake_case_ = numbers[0] for i in range(1 , len(SCREAMING_SNAKE_CASE__ ) ): # update the maximum and minimum subarray products snake_case_ = numbers[i] if number < 0: snake_case_, snake_case_ = min_till_now, max_till_now snake_case_ = max(SCREAMING_SNAKE_CASE__ , max_till_now * number ) snake_case_ = min(SCREAMING_SNAKE_CASE__ , min_till_now * number ) # update the maximum product found till now snake_case_ = max(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return max_prod
39
import multiprocessing import time from arguments import PretokenizationArguments from datasets import load_dataset from transformers import AutoTokenizer, HfArgumentParser def A ( lowercase__ : Optional[int] ) -> Optional[Any]: UpperCamelCase__ :Union[str, Any] = {} UpperCamelCase__ :Optional[int] = tokenizer(example["""content"""] , truncation=lowercase__ )["""input_ids"""] UpperCamelCase__ :int = len(example["""content"""] ) / len(output["""input_ids"""] ) return output UpperCamelCase = HfArgumentParser(PretokenizationArguments) UpperCamelCase = parser.parse_args() if args.num_workers is None: UpperCamelCase = multiprocessing.cpu_count() UpperCamelCase = AutoTokenizer.from_pretrained(args.tokenizer_dir) UpperCamelCase = time.time() UpperCamelCase = load_dataset(args.dataset_name, split="train") print(f'''Dataset loaded in {time.time()-t_start:.2f}s''') UpperCamelCase = time.time() UpperCamelCase = ds.map( tokenize, num_proc=args.num_workers, remove_columns=[ "repo_name", "path", "copies", "size", "content", "license", "hash", "line_mean", "line_max", "alpha_frac", "autogenerated", ], ) print(f'''Dataset tokenized in {time.time()-t_start:.2f}s''') UpperCamelCase = time.time() ds.push_to_hub(args.tokenized_data_repo) print(f'''Data pushed to the hub in {time.time()-t_start:.2f}s''')
45
0
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( SwiftFormerConfig, SwiftFormerForImageClassification, ViTImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() __UpperCAmelCase = logging.get_logger(__name__) __UpperCAmelCase = torch.device('''cpu''') def UpperCamelCase ( ) -> Tuple: UpperCamelCase : List[str] = 'http://images.cocodataset.org/val2017/000000039769.jpg' UpperCamelCase : Tuple = Image.open(requests.get(snake_case__ , stream=snake_case__ ).raw ) return im def UpperCamelCase ( snake_case__ : str ) -> int: if swiftformer_name == "swiftformer_xs": return torch.tensor([-2.1_703E00, 2.1_107E00, -2.0_811E00, 8.8_685E-01, 2.4_360E-01] ) elif swiftformer_name == "swiftformer_s": return torch.tensor([3.9_636E-01, 2.3_478E-01, -1.6_963E00, -1.7_381E00, -8.6_337E-01] ) elif swiftformer_name == "swiftformer_l1": return torch.tensor([-4.2_768E-01, -4.7_429E-01, -1.0_897E00, -1.0_248E00, 3.5_523E-02] ) elif swiftformer_name == "swiftformer_l3": return torch.tensor([-2.5_330E-01, 2.4_211E-01, -6.0_185E-01, -8.2_789E-01, -6.0_446E-02] ) def UpperCamelCase ( snake_case__ : List[Any] , snake_case__ : Optional[int] , snake_case__ : List[Any] ) -> str: UpperCamelCase : Optional[int] = dct.pop(snake_case__ ) UpperCamelCase : int = val def UpperCamelCase ( snake_case__ : Optional[int] ) -> Optional[Any]: UpperCamelCase : List[str] = [] for k in state_dict.keys(): UpperCamelCase : Optional[Any] = k if ".pwconv" in k: UpperCamelCase : Dict = k_new.replace('.pwconv' , '.point_wise_conv' ) if ".dwconv" in k: UpperCamelCase : Optional[Any] = k_new.replace('.dwconv' , '.depth_wise_conv' ) if ".Proj." in k: UpperCamelCase : Optional[Any] = k_new.replace('.Proj.' , '.proj.' ) if "patch_embed" in k_new: UpperCamelCase : Optional[Any] = k_new.replace('patch_embed' , 'swiftformer.patch_embed.patch_embedding' ) if "network" in k_new: UpperCamelCase : Dict = k_new.split('.' ) if ls[2].isdigit(): UpperCamelCase : Union[str, Any] = 'swiftformer.encoder.network.' + ls[1] + '.blocks.' + ls[2] + '.' + '.'.join(ls[3:] ) else: UpperCamelCase : int = k_new.replace('network' , 'swiftformer.encoder.network' ) rename_keys.append((k, k_new) ) return rename_keys @torch.no_grad() def UpperCamelCase ( snake_case__ : List[Any] , snake_case__ : List[Any] , snake_case__ : str ) -> List[Any]: UpperCamelCase : Dict = SwiftFormerConfig() # dataset (ImageNet-21k only or also fine-tuned on ImageNet 2012), patch_size and image_size UpperCamelCase : str = 1000 UpperCamelCase : List[Any] = 'huggingface/label-files' UpperCamelCase : str = 'imagenet-1k-id2label.json' UpperCamelCase : List[Any] = json.load(open(hf_hub_download(snake_case__ , snake_case__ , repo_type='dataset' ) , 'r' ) ) UpperCamelCase : List[Any] = {int(snake_case__ ): v for k, v in idalabel.items()} UpperCamelCase : List[Any] = idalabel UpperCamelCase : Dict = {v: k for k, v in idalabel.items()} # size of the architecture if swiftformer_name == "swiftformer_xs": UpperCamelCase : Any = [3, 3, 6, 4] UpperCamelCase : Optional[Any] = [48, 56, 112, 220] elif swiftformer_name == "swiftformer_s": UpperCamelCase : Optional[Any] = [3, 3, 9, 6] UpperCamelCase : str = [48, 64, 168, 224] elif swiftformer_name == "swiftformer_l1": UpperCamelCase : Optional[Any] = [4, 3, 10, 5] UpperCamelCase : Union[str, Any] = [48, 96, 192, 384] elif swiftformer_name == "swiftformer_l3": UpperCamelCase : List[Any] = [4, 4, 12, 6] UpperCamelCase : List[str] = [64, 128, 320, 512] # load state_dict of original model, remove and rename some keys if original_ckpt: if original_ckpt.startswith('https' ): UpperCamelCase : Any = torch.hub.load_state_dict_from_url(snake_case__ , map_location='cpu' , check_hash=snake_case__ ) else: UpperCamelCase : Optional[Any] = torch.load(snake_case__ , map_location='cpu' ) UpperCamelCase : int = checkpoint UpperCamelCase : str = create_rename_keys(snake_case__ ) for rename_key_src, rename_key_dest in rename_keys: rename_key(snake_case__ , snake_case__ , snake_case__ ) # load HuggingFace model UpperCamelCase : str = SwiftFormerForImageClassification(snake_case__ ).eval() hf_model.load_state_dict(snake_case__ ) # prepare test inputs UpperCamelCase : Union[str, Any] = prepare_img() UpperCamelCase : Optional[int] = ViTImageProcessor.from_pretrained('preprocessor_config' ) UpperCamelCase : int = processor(images=snake_case__ , return_tensors='pt' ) # compare outputs from both models UpperCamelCase : Tuple = get_expected_output(snake_case__ ) UpperCamelCase : Optional[Any] = hf_model(inputs['pixel_values'] ).logits assert hf_logits.shape == torch.Size([1, 1000] ) assert torch.allclose(hf_logits[0, 0:5] , snake_case__ , atol=1E-3 ) Path(snake_case__ ).mkdir(exist_ok=snake_case__ ) print(F"""Saving model {swiftformer_name} to {pytorch_dump_folder_path}""" ) hf_model.save_pretrained(snake_case__ ) if __name__ == "__main__": __UpperCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--swiftformer_name''', default='''swiftformer_xs''', choices=['''swiftformer_xs''', '''swiftformer_s''', '''swiftformer_l1''', '''swiftformer_l3'''], type=str, help='''Name of the SwiftFormer model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default='''./converted_outputs/''', type=str, help='''Path to the output PyTorch model directory.''', ) parser.add_argument('''--original_ckpt''', default=None, type=str, help='''Path to the original model checkpoint.''') __UpperCAmelCase = parser.parse_args() convert_swiftformer_checkpoint(args.swiftformer_name, args.pytorch_dump_folder_path, args.original_ckpt)
40
def A ( lowercase__ : int ) -> Optional[Any]: stooge(lowercase__ , 0 , len(lowercase__ ) - 1 ) return arr def A ( lowercase__ : Union[str, Any] , lowercase__ : Dict , lowercase__ : str ) -> List[str]: if i >= h: return # If first element is smaller than the last then swap them if arr[i] > arr[h]: UpperCamelCase__ , UpperCamelCase__ :List[str] = arr[h], arr[i] # If there are more than 2 elements in the array if h - i + 1 > 2: UpperCamelCase__ :Optional[int] = (int)((h - i + 1) / 3 ) # Recursively sort first 2/3 elements stooge(lowercase__ , lowercase__ , (h - t) ) # Recursively sort last 2/3 elements stooge(lowercase__ , i + t , (lowercase__) ) # Recursively sort first 2/3 elements stooge(lowercase__ , lowercase__ , (h - t) ) if __name__ == "__main__": UpperCamelCase = input("Enter numbers separated by a comma:\n").strip() UpperCamelCase = [int(item) for item in user_input.split(",")] print(stooge_sort(unsorted))
45
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available lowerCAmelCase__ = { '''configuration_poolformer''': [ '''POOLFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''PoolFormerConfig''', '''PoolFormerOnnxConfig''', ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ['''PoolFormerFeatureExtractor'''] lowerCAmelCase__ = ['''PoolFormerImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''PoolFormerForImageClassification''', '''PoolFormerModel''', '''PoolFormerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_poolformer import ( POOLFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, PoolFormerConfig, PoolFormerOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_poolformer import PoolFormerFeatureExtractor from .image_processing_poolformer import PoolFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_poolformer import ( POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, PoolFormerForImageClassification, PoolFormerModel, PoolFormerPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
41
import argparse import json import os from tensorflow.core.protobuf.saved_model_pba import SavedModel # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_copies.py UpperCamelCase = "." # Internal TensorFlow ops that can be safely ignored (mostly specific to a saved model) UpperCamelCase = [ "Assert", "AssignVariableOp", "EmptyTensorList", "MergeV2Checkpoints", "ReadVariableOp", "ResourceGather", "RestoreV2", "SaveV2", "ShardedFilename", "StatefulPartitionedCall", "StaticRegexFullMatch", "VarHandleOp", ] def A ( lowercase__ : Tuple , lowercase__ : Optional[Any] , lowercase__ : Dict ) -> List[Any]: UpperCamelCase__ :str = SavedModel() UpperCamelCase__ :List[str] = [] with open(os.path.join(lowercase__ , """utils""" , """tf_ops""" , """onnx.json""" ) ) as f: UpperCamelCase__ :str = json.load(lowercase__ )["""opsets"""] for i in range(1 , opset + 1 ): onnx_ops.extend(onnx_opsets[str(lowercase__ )] ) with open(lowercase__ , """rb""" ) as f: saved_model.ParseFromString(f.read() ) UpperCamelCase__ :Tuple = set() # Iterate over every metagraph in case there is more than one (a saved model can contain multiple graphs) for meta_graph in saved_model.meta_graphs: # Add operations in the graph definition model_op_names.update(node.op for node in meta_graph.graph_def.node ) # Go through the functions in the graph definition for func in meta_graph.graph_def.library.function: # Add operations in each function model_op_names.update(node.op for node in func.node_def ) # Convert to list, sorted if you want UpperCamelCase__ :Union[str, Any] = sorted(lowercase__ ) UpperCamelCase__ :List[Any] = [] for op in model_op_names: if op not in onnx_ops and op not in INTERNAL_OPS: incompatible_ops.append(lowercase__ ) if strict and len(lowercase__ ) > 0: raise Exception(f"""Found the following incompatible ops for the opset {opset}:\n""" + incompatible_ops ) elif len(lowercase__ ) > 0: print(f"""Found the following incompatible ops for the opset {opset}:""" ) print(*lowercase__ , sep="""\n""" ) else: print(f"""The saved model {saved_model_path} can properly be converted with ONNX.""" ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() parser.add_argument("--saved_model_path", help="Path of the saved model to check (the .pb file).") parser.add_argument( "--opset", default=12, type=int, help="The ONNX opset against which the model has to be tested." ) parser.add_argument( "--framework", choices=["onnx"], default="onnx", help="Frameworks against which to test the saved model." ) parser.add_argument( "--strict", action="store_true", help="Whether make the checking strict (raise errors) or not (raise warnings)" ) UpperCamelCase = parser.parse_args() if args.framework == "onnx": onnx_compliancy(args.saved_model_path, args.strict, args.opset)
45
0
'''simple docstring''' import argparse from pathlib import Path import torch from packaging import version from torch.onnx import export from diffusers import AutoencoderKL A_ = version.parse(version.parse(torch.__version__).base_version) < version.parse("1.11") def _UpperCamelCase ( __UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase=False ,) -> Any: output_path.parent.mkdir(parents=__UpperCamelCase ,exist_ok=__UpperCamelCase ) # PyTorch deprecated the `enable_onnx_checker` and `use_external_data_format` arguments in v1.11, # so we check the torch version for backwards compatibility if is_torch_less_than_1_11: export( __UpperCamelCase ,__UpperCamelCase ,f=output_path.as_posix() ,input_names=__UpperCamelCase ,output_names=__UpperCamelCase ,dynamic_axes=__UpperCamelCase ,do_constant_folding=__UpperCamelCase ,use_external_data_format=__UpperCamelCase ,enable_onnx_checker=__UpperCamelCase ,opset_version=__UpperCamelCase ,) else: export( __UpperCamelCase ,__UpperCamelCase ,f=output_path.as_posix() ,input_names=__UpperCamelCase ,output_names=__UpperCamelCase ,dynamic_axes=__UpperCamelCase ,do_constant_folding=__UpperCamelCase ,opset_version=__UpperCamelCase ,) @torch.no_grad() def _UpperCamelCase ( __UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase = False ) -> Dict: lowerCamelCase_ = torch.floataa if fpaa else torch.floataa if fpaa and torch.cuda.is_available(): lowerCamelCase_ = 'cuda' elif fpaa and not torch.cuda.is_available(): raise ValueError('`float16` model export is only supported on GPUs with CUDA' ) else: lowerCamelCase_ = 'cpu' lowerCamelCase_ = Path(__UpperCamelCase ) # VAE DECODER lowerCamelCase_ = AutoencoderKL.from_pretrained(model_path + '/vae' ) lowerCamelCase_ = vae_decoder.config.latent_channels # forward only through the decoder part lowerCamelCase_ = vae_decoder.decode onnx_export( __UpperCamelCase ,model_args=( torch.randn(1 ,__UpperCamelCase ,25 ,25 ).to(device=__UpperCamelCase ,dtype=__UpperCamelCase ), False, ) ,output_path=output_path / 'vae_decoder' / 'model.onnx' ,ordered_input_names=['latent_sample', 'return_dict'] ,output_names=['sample'] ,dynamic_axes={ 'latent_sample': {0: 'batch', 1: 'channels', 2: 'height', 3: 'width'}, } ,opset=__UpperCamelCase ,) del vae_decoder if __name__ == "__main__": A_ = argparse.ArgumentParser() parser.add_argument( "--model_path", type=str, required=True, help="Path to the `diffusers` checkpoint to convert (either a local directory or on the Hub).", ) parser.add_argument("--output_path", type=str, required=True, help="Path to the output model.") parser.add_argument( "--opset", default=14, type=int, help="The version of the ONNX operator set to use.", ) parser.add_argument("--fp16", action="store_true", default=False, help="Export the models in `float16` mode") A_ = parser.parse_args() print(args.output_path) convert_models(args.model_path, args.output_path, args.opset, args.fpaa) print("SD: Done: ONNX")
42
from __future__ import annotations def A ( lowercase__ : str , lowercase__ : list[str] | None = None , lowercase__ : dict[str, float] | None = None , lowercase__ : bool = False , ) -> tuple[int, float, str]: UpperCamelCase__ :Dict = cipher_alphabet or [chr(lowercase__ ) for i in range(97 , 123 )] # If the argument is None or the user provided an empty dictionary if not frequencies_dict: # Frequencies of letters in the english language (how much they show up) UpperCamelCase__ :Optional[Any] = { """a""": 0.08497, """b""": 0.01492, """c""": 0.02202, """d""": 0.04253, """e""": 0.11162, """f""": 0.02228, """g""": 0.02015, """h""": 0.06094, """i""": 0.07546, """j""": 0.00153, """k""": 0.01292, """l""": 0.04025, """m""": 0.02406, """n""": 0.06749, """o""": 0.07507, """p""": 0.01929, """q""": 0.00095, """r""": 0.07587, """s""": 0.06327, """t""": 0.09356, """u""": 0.02758, """v""": 0.00978, """w""": 0.02560, """x""": 0.00150, """y""": 0.01994, """z""": 0.00077, } else: # Custom frequencies dictionary UpperCamelCase__ :Optional[int] = frequencies_dict if not case_sensitive: UpperCamelCase__ :int = ciphertext.lower() # Chi squared statistic values UpperCamelCase__ :dict[int, tuple[float, str]] = {} # cycle through all of the shifts for shift in range(len(lowercase__ ) ): UpperCamelCase__ :int = """""" # decrypt the message with the shift for letter in ciphertext: try: # Try to index the letter in the alphabet UpperCamelCase__ :int = (alphabet_letters.index(letter.lower() ) - shift) % len( lowercase__ ) decrypted_with_shift += ( alphabet_letters[new_key].upper() if case_sensitive and letter.isupper() else alphabet_letters[new_key] ) except ValueError: # Append the character if it isn't in the alphabet decrypted_with_shift += letter UpperCamelCase__ :Optional[int] = 0.0 # Loop through each letter in the decoded message with the shift for letter in decrypted_with_shift: if case_sensitive: UpperCamelCase__ :Optional[int] = letter.lower() if letter in frequencies: # Get the amount of times the letter occurs in the message UpperCamelCase__ :Optional[int] = decrypted_with_shift.lower().count(lowercase__ ) # Get the excepcted amount of times the letter should appear based # on letter frequencies UpperCamelCase__ :Optional[int] = frequencies[letter] * occurrences # Complete the chi squared statistic formula UpperCamelCase__ :Dict = ((occurrences - expected) ** 2) / expected # Add the margin of error to the total chi squared statistic chi_squared_statistic += chi_letter_value else: if letter.lower() in frequencies: # Get the amount of times the letter occurs in the message UpperCamelCase__ :List[str] = decrypted_with_shift.count(lowercase__ ) # Get the excepcted amount of times the letter should appear based # on letter frequencies UpperCamelCase__ :Union[str, Any] = frequencies[letter] * occurrences # Complete the chi squared statistic formula UpperCamelCase__ :List[str] = ((occurrences - expected) ** 2) / expected # Add the margin of error to the total chi squared statistic chi_squared_statistic += chi_letter_value # Add the data to the chi_squared_statistic_values dictionary UpperCamelCase__ :Union[str, Any] = ( chi_squared_statistic, decrypted_with_shift, ) # Get the most likely cipher by finding the cipher with the smallest chi squared # statistic def chi_squared_statistic_values_sorting_key(lowercase__ : int ) -> tuple[float, str]: return chi_squared_statistic_values[key] UpperCamelCase__ :int = min( lowercase__ , key=lowercase__ , ) # Get all the data from the most likely cipher (key, decoded message) ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :Tuple = chi_squared_statistic_values[most_likely_cipher] # Return the data on the most likely shift return ( most_likely_cipher, most_likely_cipher_chi_squared_value, decoded_most_likely_cipher, )
45
0
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase = logging.get_logger(__name__) lowerCAmelCase = {'openai-gpt': 'https://huggingface.co/openai-gpt/resolve/main/config.json'} class _a ( UpperCamelCase__ ): _lowercase : List[Any] = '''openai-gpt''' _lowercase : 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] , UpperCamelCase_: Tuple=40_478 , UpperCamelCase_: Any=512 , UpperCamelCase_: List[str]=768 , UpperCamelCase_: Tuple=12 , UpperCamelCase_: List[str]=12 , UpperCamelCase_: List[str]="gelu" , UpperCamelCase_: Optional[int]=0.1 , UpperCamelCase_: Optional[int]=0.1 , UpperCamelCase_: List[str]=0.1 , UpperCamelCase_: Any=1E-5 , UpperCamelCase_: Optional[int]=0.02 , UpperCamelCase_: List[str]="cls_index" , UpperCamelCase_: int=True , UpperCamelCase_: Any=None , UpperCamelCase_: Dict=True , UpperCamelCase_: str=0.1 , **UpperCamelCase_: Any , ) -> Any: """simple docstring""" lowercase__ = vocab_size lowercase__ = n_positions lowercase__ = n_embd lowercase__ = n_layer lowercase__ = n_head lowercase__ = afn lowercase__ = resid_pdrop lowercase__ = embd_pdrop lowercase__ = attn_pdrop lowercase__ = layer_norm_epsilon lowercase__ = initializer_range lowercase__ = summary_type lowercase__ = summary_use_proj lowercase__ = summary_activation lowercase__ = summary_first_dropout lowercase__ = summary_proj_to_labels super().__init__(**UpperCamelCase_ )
43
import warnings from ...utils import logging from .image_processing_mobilevit import MobileViTImageProcessor UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( lowercase ): """simple docstring""" def __init__( self :Union[str, Any] , *lowerCamelCase__ :Optional[int] , **lowerCamelCase__ :Dict ): warnings.warn( """The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.""" """ Please use MobileViTImageProcessor instead.""" , lowerCamelCase__ , ) super().__init__(*lowerCamelCase__ , **lowerCamelCase__ )
45
0
'''simple docstring''' import inspect import unittest import numpy as np from tests.test_modeling_common import floats_tensor from transformers import MaskaFormerConfig, is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MaskaFormerForUniversalSegmentation, MaskaFormerModel if is_vision_available(): from transformers import MaskaFormerImageProcessor if is_vision_available(): from PIL import Image class UpperCAmelCase__ : def __init__( self : List[Any],__A : Dict,__A : Tuple=2,__A : int=True,__A : List[Any]=False,__A : Optional[Any]=1_0,__A : List[Any]=3,__A : int=3_2 * 8,__A : Optional[int]=3_2 * 8,__A : str=4,__A : List[Any]=6_4,): _lowerCamelCase : str = parent _lowerCamelCase : Optional[int] = batch_size _lowerCamelCase : Union[str, Any] = is_training _lowerCamelCase : Dict = use_auxiliary_loss _lowerCamelCase : Optional[Any] = num_queries _lowerCamelCase : Union[str, Any] = num_channels _lowerCamelCase : str = min_size _lowerCamelCase : int = max_size _lowerCamelCase : Union[str, Any] = num_labels _lowerCamelCase : Any = hidden_dim _lowerCamelCase : Any = hidden_dim def lowerCamelCase_ ( self : Any ): _lowerCamelCase : Union[str, Any] = floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size] ).to( __A ) _lowerCamelCase : int = torch.ones([self.batch_size, self.min_size, self.max_size],device=__A ) _lowerCamelCase : Any = ( torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size],device=__A ) > 0.5 ).float() _lowerCamelCase : int = (torch.rand((self.batch_size, self.num_labels),device=__A ) > 0.5).long() _lowerCamelCase : str = self.get_config() return config, pixel_values, pixel_mask, mask_labels, class_labels def lowerCamelCase_ ( self : str ): _lowerCamelCase : Union[str, Any] = MaskaFormerConfig( hidden_size=self.hidden_dim,) _lowerCamelCase : Any = self.num_queries _lowerCamelCase : Union[str, Any] = self.num_labels _lowerCamelCase : Optional[Any] = [1, 1, 1, 1] _lowerCamelCase : Optional[int] = self.num_channels _lowerCamelCase : Dict = 6_4 _lowerCamelCase : List[Any] = 1_2_8 _lowerCamelCase : List[Any] = self.hidden_dim _lowerCamelCase : int = self.hidden_dim _lowerCamelCase : Tuple = self.hidden_dim return config def lowerCamelCase_ ( self : Optional[int] ): _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase : Optional[int] = self.prepare_config_and_inputs() _lowerCamelCase : List[str] = {"pixel_values": pixel_values, "pixel_mask": pixel_mask} return config, inputs_dict def lowerCamelCase_ ( self : int,__A : Dict,__A : List[str] ): _lowerCamelCase : Any = output.encoder_hidden_states _lowerCamelCase : Any = output.pixel_decoder_hidden_states _lowerCamelCase : int = output.transformer_decoder_hidden_states self.parent.assertTrue(len(__A ),len(config.backbone_config.depths ) ) self.parent.assertTrue(len(__A ),len(config.backbone_config.depths ) ) self.parent.assertTrue(len(__A ),config.decoder_layers ) def lowerCamelCase_ ( self : Optional[Any],__A : Dict,__A : Union[str, Any],__A : Any,__A : List[Any]=False ): with torch.no_grad(): _lowerCamelCase : Any = MaskaFormerModel(config=__A ) model.to(__A ) model.eval() _lowerCamelCase : Optional[Any] = model(pixel_values=__A,pixel_mask=__A ) _lowerCamelCase : Any = model(__A,output_hidden_states=__A ) self.parent.assertEqual( output.transformer_decoder_last_hidden_state.shape,(self.batch_size, self.num_queries, self.hidden_dim),) # let's ensure the other two hidden state exists self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None ) self.parent.assertTrue(output.encoder_last_hidden_state is not None ) if output_hidden_states: self.check_output_hidden_state(__A,__A ) def lowerCamelCase_ ( self : Tuple,__A : Optional[int],__A : List[Any],__A : str,__A : int,__A : List[str] ): _lowerCamelCase : Union[str, Any] = MaskaFormerForUniversalSegmentation(config=__A ) model.to(__A ) model.eval() def comm_check_on_output(__A : Optional[Any] ): # let's still check that all the required stuff is there self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None ) self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None ) self.parent.assertTrue(result.encoder_last_hidden_state is not None ) # okay, now we need to check the logits shape # due to the encoder compression, masks have a //4 spatial size self.parent.assertEqual( result.masks_queries_logits.shape,(self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4),) # + 1 for null class self.parent.assertEqual( result.class_queries_logits.shape,(self.batch_size, self.num_queries, self.num_labels + 1) ) with torch.no_grad(): _lowerCamelCase : int = model(pixel_values=__A,pixel_mask=__A ) _lowerCamelCase : Union[str, Any] = model(__A ) comm_check_on_output(__A ) _lowerCamelCase : int = model( pixel_values=__A,pixel_mask=__A,mask_labels=__A,class_labels=__A ) comm_check_on_output(__A ) self.parent.assertTrue(result.loss is not None ) self.parent.assertEqual(result.loss.shape,torch.Size([1] ) ) @require_torch class UpperCAmelCase__ ( A , A , unittest.TestCase ): lowerCAmelCase_ = (MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else () lowerCAmelCase_ = {'feature-extraction': MaskaFormerModel} if is_torch_available() else {} lowerCAmelCase_ = False lowerCAmelCase_ = False lowerCAmelCase_ = False lowerCAmelCase_ = False def lowerCamelCase_ ( self : Optional[Any] ): _lowerCamelCase : Optional[Any] = MaskaFormerModelTester(self ) _lowerCamelCase : Any = ConfigTester(self,config_class=__A,has_text_modality=__A ) def lowerCamelCase_ ( self : str ): self.config_tester.run_common_tests() def lowerCamelCase_ ( self : Optional[int] ): _lowerCamelCase , _lowerCamelCase : Dict = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskaformer_model(__A,**__A,output_hidden_states=__A ) def lowerCamelCase_ ( self : Optional[int] ): _lowerCamelCase : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*__A ) @unittest.skip(reason="Mask2Former does not use inputs_embeds" ) def lowerCamelCase_ ( self : int ): pass @unittest.skip(reason="Mask2Former does not have a get_input_embeddings method" ) def lowerCamelCase_ ( self : Tuple ): pass @unittest.skip(reason="Mask2Former is not a generative model" ) def lowerCamelCase_ ( self : Optional[Any] ): pass @unittest.skip(reason="Mask2Former does not use token embeddings" ) def lowerCamelCase_ ( self : Dict ): pass @require_torch_multi_gpu @unittest.skip( reason="Mask2Former has some layers using `add_module` which doesn't work well with `nn.DataParallel`" ) def lowerCamelCase_ ( self : int ): pass @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." ) def lowerCamelCase_ ( self : Tuple ): pass def lowerCamelCase_ ( self : Tuple ): _lowerCamelCase , _lowerCamelCase : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _lowerCamelCase : Tuple = model_class(__A ) _lowerCamelCase : Optional[Any] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _lowerCamelCase : List[Any] = [*signature.parameters.keys()] _lowerCamelCase : List[Any] = ["pixel_values"] self.assertListEqual(arg_names[:1],__A ) @slow def lowerCamelCase_ ( self : List[str] ): for model_name in ["facebook/mask2former-swin-small-coco-instance"]: _lowerCamelCase : Optional[int] = MaskaFormerModel.from_pretrained(__A ) self.assertIsNotNone(__A ) def lowerCamelCase_ ( self : Any ): _lowerCamelCase : str = (self.model_tester.min_size,) * 2 _lowerCamelCase : Union[str, Any] = { "pixel_values": torch.randn((2, 3, *size),device=__A ), "mask_labels": torch.randn((2, 1_0, *size),device=__A ), "class_labels": torch.zeros(2,1_0,device=__A ).long(), } _lowerCamelCase : List[str] = self.model_tester.get_config() _lowerCamelCase : Any = MaskaFormerForUniversalSegmentation(__A ).to(__A ) _lowerCamelCase : str = model(**__A ) self.assertTrue(outputs.loss is not None ) def lowerCamelCase_ ( self : Tuple ): _lowerCamelCase , _lowerCamelCase : int = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskaformer_model(__A,**__A,output_hidden_states=__A ) def lowerCamelCase_ ( self : Optional[Any] ): _lowerCamelCase , _lowerCamelCase : List[str] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _lowerCamelCase : Union[str, Any] = model_class(__A ).to(__A ) _lowerCamelCase : List[str] = model(**__A,output_attentions=__A ) self.assertTrue(outputs.attentions is not None ) def lowerCamelCase_ ( self : List[str] ): if not self.model_tester.is_training: return _lowerCamelCase : str = self.all_model_classes[1] _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs() _lowerCamelCase : Optional[Any] = model_class(__A ) model.to(__A ) model.train() _lowerCamelCase : Optional[int] = model(__A,mask_labels=__A,class_labels=__A ).loss loss.backward() def lowerCamelCase_ ( self : List[str] ): _lowerCamelCase : Optional[int] = self.all_model_classes[1] _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs() _lowerCamelCase : Any = True _lowerCamelCase : Optional[Any] = True _lowerCamelCase : str = model_class(__A ).to(__A ) model.train() _lowerCamelCase : str = model(__A,mask_labels=__A,class_labels=__A ) _lowerCamelCase : int = outputs.encoder_hidden_states[0] encoder_hidden_states.retain_grad() _lowerCamelCase : List[str] = outputs.pixel_decoder_hidden_states[0] pixel_decoder_hidden_states.retain_grad() _lowerCamelCase : Optional[int] = outputs.transformer_decoder_hidden_states[0] transformer_decoder_hidden_states.retain_grad() _lowerCamelCase : int = outputs.attentions[0] attentions.retain_grad() outputs.loss.backward(retain_graph=__A ) self.assertIsNotNone(encoder_hidden_states.grad ) self.assertIsNotNone(pixel_decoder_hidden_states.grad ) self.assertIsNotNone(transformer_decoder_hidden_states.grad ) self.assertIsNotNone(attentions.grad ) UpperCAmelCase_ : List[str] = 1E-4 def A_ ( ): """simple docstring""" _lowerCamelCase : Dict = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_vision @slow class UpperCAmelCase__ ( unittest.TestCase ): @cached_property def lowerCamelCase_ ( self : Optional[int] ): return "facebook/mask2former-swin-small-coco-instance" @cached_property def lowerCamelCase_ ( self : int ): return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints ) if is_vision_available() else None def lowerCamelCase_ ( self : Optional[Any] ): _lowerCamelCase : str = MaskaFormerModel.from_pretrained(self.model_checkpoints ).to(__A ) _lowerCamelCase : Union[str, Any] = self.default_image_processor _lowerCamelCase : str = prepare_img() _lowerCamelCase : List[str] = image_processor(__A,return_tensors="pt" ).to(__A ) _lowerCamelCase : List[Any] = inputs["pixel_values"].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 3_2) == 0 and (inputs_shape[-2] % 3_2) == 0 ) # check size self.assertEqual(__A,(1, 3, 3_8_4, 3_8_4) ) with torch.no_grad(): _lowerCamelCase : Optional[Any] = model(**__A ) _lowerCamelCase : Union[str, Any] = torch.tensor( [[-0.2790, -1.0717, -1.1668], [-0.5128, -0.3128, -0.4987], [-0.5832, 0.1971, -0.0197]] ).to(__A ) self.assertTrue( torch.allclose( outputs.encoder_last_hidden_state[0, 0, :3, :3],__A,atol=__A ) ) _lowerCamelCase : str = torch.tensor( [[0.8973, 1.1847, 1.1776], [1.1934, 1.5040, 1.5128], [1.1153, 1.4486, 1.4951]] ).to(__A ) self.assertTrue( torch.allclose( outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3],__A,atol=__A ) ) _lowerCamelCase : Optional[int] = torch.tensor( [[2.1152, 1.7000, -0.8603], [1.5808, 1.8004, -0.9353], [1.6043, 1.7495, -0.5999]] ).to(__A ) self.assertTrue( torch.allclose( outputs.transformer_decoder_last_hidden_state[0, :3, :3],__A,atol=__A ) ) def lowerCamelCase_ ( self : Any ): _lowerCamelCase : int = MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints ).to(__A ).eval() _lowerCamelCase : Union[str, Any] = self.default_image_processor _lowerCamelCase : str = prepare_img() _lowerCamelCase : Union[str, Any] = image_processor(__A,return_tensors="pt" ).to(__A ) _lowerCamelCase : List[Any] = inputs["pixel_values"].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 3_2) == 0 and (inputs_shape[-2] % 3_2) == 0 ) # check size self.assertEqual(__A,(1, 3, 3_8_4, 3_8_4) ) with torch.no_grad(): _lowerCamelCase : Tuple = model(**__A ) # masks_queries_logits _lowerCamelCase : Any = outputs.masks_queries_logits self.assertEqual( masks_queries_logits.shape,(1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4) ) _lowerCamelCase : Optional[Any] = [ [-8.7839, -9.0056, -8.8121], [-7.4104, -7.0313, -6.5401], [-6.6105, -6.3427, -6.4675], ] _lowerCamelCase : Optional[int] = torch.tensor(__A ).to(__A ) self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3],__A,atol=__A ) ) # class_queries_logits _lowerCamelCase : Tuple = outputs.class_queries_logits self.assertEqual(class_queries_logits.shape,(1, model.config.num_queries, model.config.num_labels + 1) ) _lowerCamelCase : Optional[int] = torch.tensor( [ [1.8324, -8.0835, -4.1922], [0.8450, -9.0050, -3.6053], [0.3045, -7.7293, -3.0275], ] ).to(__A ) self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3],__A,atol=__A ) ) def lowerCamelCase_ ( self : Optional[int] ): _lowerCamelCase : List[str] = MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints ).to(__A ).eval() _lowerCamelCase : Dict = self.default_image_processor _lowerCamelCase : Tuple = image_processor( [np.zeros((3, 8_0_0, 1_3_3_3) ), np.zeros((3, 8_0_0, 1_3_3_3) )],segmentation_maps=[np.zeros((3_8_4, 3_8_4) ).astype(np.floataa ), np.zeros((3_8_4, 3_8_4) ).astype(np.floataa )],return_tensors="pt",) _lowerCamelCase : Any = inputs["pixel_values"].to(__A ) _lowerCamelCase : Optional[Any] = [el.to(__A ) for el in inputs["mask_labels"]] _lowerCamelCase : Optional[Any] = [el.to(__A ) for el in inputs["class_labels"]] with torch.no_grad(): _lowerCamelCase : Optional[int] = model(**__A ) self.assertTrue(outputs.loss is not None )
44
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 UpperCamelCase = get_tests_dir("fixtures") UpperCamelCase = get_tests_dir("fixtures/dummy_feature_extractor_config.json") UpperCamelCase = get_tests_dir("fixtures/dummy-config.json") class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __a ( self :Optional[int] ): UpperCamelCase__ :Optional[int] = 0 def __a ( self :str ): UpperCamelCase__ :Optional[Any] = AutoFeatureExtractor.from_pretrained("""facebook/wav2vec2-base-960h""" ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Dict ): UpperCamelCase__ :Dict = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Optional[int] ): with tempfile.TemporaryDirectory() as tmpdirname: UpperCamelCase__ :List[str] = WavaVecaConfig() # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally UpperCamelCase__ :Tuple = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ ).to_dict() config_dict.pop("""feature_extractor_type""" ) UpperCamelCase__ :Union[str, Any] = WavaVecaFeatureExtractor(**lowerCamelCase__ ) # save in new folder model_config.save_pretrained(lowerCamelCase__ ) config.save_pretrained(lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ ) # make sure private variable is not incorrectly saved UpperCamelCase__ :Tuple = json.loads(config.to_json_string() ) self.assertTrue("""_processor_class""" not in dict_as_saved ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Union[str, Any] ): UpperCamelCase__ :Dict = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Dict ): with self.assertRaisesRegex( lowerCamelCase__ , """bert-base is not a local folder and is not a valid model identifier""" ): UpperCamelCase__ :Dict = AutoFeatureExtractor.from_pretrained("""bert-base""" ) def __a ( self :List[Any] ): with self.assertRaisesRegex( lowerCamelCase__ , r"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)""" ): UpperCamelCase__ :Optional[int] = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ , revision="""aaaaaa""" ) def __a ( self :int ): with self.assertRaisesRegex( lowerCamelCase__ , """hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.""" , ): UpperCamelCase__ :List[Any] = AutoFeatureExtractor.from_pretrained("""hf-internal-testing/config-no-model""" ) def __a ( self :Optional[int] ): # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(lowerCamelCase__ ): UpperCamelCase__ :List[Any] = AutoFeatureExtractor.from_pretrained( """hf-internal-testing/test_dynamic_feature_extractor""" ) # If remote code is disabled, we can't load this config. with self.assertRaises(lowerCamelCase__ ): UpperCamelCase__ :Optional[Any] = AutoFeatureExtractor.from_pretrained( """hf-internal-testing/test_dynamic_feature_extractor""" , trust_remote_code=lowerCamelCase__ ) UpperCamelCase__ :str = AutoFeatureExtractor.from_pretrained( """hf-internal-testing/test_dynamic_feature_extractor""" , trust_remote_code=lowerCamelCase__ ) self.assertEqual(feature_extractor.__class__.__name__ , """NewFeatureExtractor""" ) # Test feature extractor can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained(lowerCamelCase__ ) UpperCamelCase__ :Any = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ , trust_remote_code=lowerCamelCase__ ) self.assertEqual(reloaded_feature_extractor.__class__.__name__ , """NewFeatureExtractor""" ) def __a ( self :Dict ): try: AutoConfig.register("""custom""" , lowerCamelCase__ ) AutoFeatureExtractor.register(lowerCamelCase__ , lowerCamelCase__ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(lowerCamelCase__ ): AutoFeatureExtractor.register(lowerCamelCase__ , lowerCamelCase__ ) # Now that the config is registered, it can be used as any other config with the auto-API UpperCamelCase__ :Any = CustomFeatureExtractor.from_pretrained(lowerCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained(lowerCamelCase__ ) UpperCamelCase__ :List[Any] = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) 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 __a ( self :Optional[int] ): class lowerCAmelCase_ ( lowercase ): """simple docstring""" _snake_case : Optional[int] = True try: AutoConfig.register("""custom""" , lowerCamelCase__ ) AutoFeatureExtractor.register(lowerCamelCase__ , lowerCamelCase__ ) # If remote code is not set, the default is to use local UpperCamelCase__ :Optional[Any] = 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. UpperCamelCase__ :str = AutoFeatureExtractor.from_pretrained( """hf-internal-testing/test_dynamic_feature_extractor""" , trust_remote_code=lowerCamelCase__ ) self.assertEqual(feature_extractor.__class__.__name__ , """NewFeatureExtractor""" ) self.assertTrue(feature_extractor.is_local ) # If remote is enabled, we load from the Hub UpperCamelCase__ :Optional[int] = AutoFeatureExtractor.from_pretrained( """hf-internal-testing/test_dynamic_feature_extractor""" , trust_remote_code=lowerCamelCase__ ) self.assertEqual(feature_extractor.__class__.__name__ , """NewFeatureExtractor""" ) self.assertTrue(not hasattr(lowerCamelCase__ , """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]
45
0
"""simple docstring""" import unittest from transformers import AutoConfig, AutoTokenizer, BertConfig, TensorType, is_flax_available from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, slow if is_flax_available(): import jax from transformers.models.auto.modeling_flax_auto import FlaxAutoModel from transformers.models.bert.modeling_flax_bert import FlaxBertModel from transformers.models.roberta.modeling_flax_roberta import FlaxRobertaModel @require_flax class A_ ( unittest.TestCase ): @slow def _lowercase ( self: List[str] ): '''simple docstring''' for model_name in ["bert-base-cased", "bert-large-uncased"]: with self.subTest(__lowerCAmelCase ): _lowerCamelCase : List[Any] = AutoConfig.from_pretrained(__lowerCAmelCase ) self.assertIsNotNone(__lowerCAmelCase ) self.assertIsInstance(__lowerCAmelCase ,__lowerCAmelCase ) _lowerCamelCase : Optional[int] = FlaxAutoModel.from_pretrained(__lowerCAmelCase ) self.assertIsNotNone(__lowerCAmelCase ) self.assertIsInstance(__lowerCAmelCase ,__lowerCAmelCase ) @slow def _lowercase ( self: int ): '''simple docstring''' for model_name in ["roberta-base", "roberta-large"]: with self.subTest(__lowerCAmelCase ): _lowerCamelCase : int = AutoConfig.from_pretrained(__lowerCAmelCase ) self.assertIsNotNone(__lowerCAmelCase ) self.assertIsInstance(__lowerCAmelCase ,__lowerCAmelCase ) _lowerCamelCase : Union[str, Any] = FlaxAutoModel.from_pretrained(__lowerCAmelCase ) self.assertIsNotNone(__lowerCAmelCase ) self.assertIsInstance(__lowerCAmelCase ,__lowerCAmelCase ) @slow def _lowercase ( self: int ): '''simple docstring''' for model_name in ["bert-base-cased", "bert-large-uncased"]: _lowerCamelCase : List[str] = AutoTokenizer.from_pretrained(__lowerCAmelCase ) _lowerCamelCase : str = FlaxBertModel.from_pretrained(__lowerCAmelCase ) _lowerCamelCase : Union[str, Any] = tokenizer("Do you support jax jitted function?" ,return_tensors=TensorType.JAX ) @jax.jit def eval(**__lowerCAmelCase: Union[str, Any] ): return model(**__lowerCAmelCase ) eval(**__lowerCAmelCase ).block_until_ready() @slow def _lowercase ( self: Union[str, Any] ): '''simple docstring''' for model_name in ["roberta-base", "roberta-large"]: _lowerCamelCase : List[Any] = AutoTokenizer.from_pretrained(__lowerCAmelCase ) _lowerCamelCase : List[Any] = FlaxRobertaModel.from_pretrained(__lowerCAmelCase ) _lowerCamelCase : Any = tokenizer("Do you support jax jitted function?" ,return_tensors=TensorType.JAX ) @jax.jit def eval(**__lowerCAmelCase: int ): return model(**__lowerCAmelCase ) eval(**__lowerCAmelCase ).block_until_ready() def _lowercase ( self: Any ): '''simple docstring''' with self.assertRaisesRegex( __lowerCAmelCase ,"bert-base is not a local folder and is not a valid model identifier" ): _lowerCamelCase : Union[str, Any] = FlaxAutoModel.from_pretrained("bert-base" ) def _lowercase ( self: int ): '''simple docstring''' with self.assertRaisesRegex( __lowerCAmelCase ,r"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)" ): _lowerCamelCase : Union[str, Any] = FlaxAutoModel.from_pretrained(__lowerCAmelCase ,revision="aaaaaa" ) def _lowercase ( self: int ): '''simple docstring''' with self.assertRaisesRegex( __lowerCAmelCase ,"hf-internal-testing/config-no-model does not appear to have a file named flax_model.msgpack" ,): _lowerCamelCase : Union[str, Any] = FlaxAutoModel.from_pretrained("hf-internal-testing/config-no-model" ) def _lowercase ( self: Optional[int] ): '''simple docstring''' with self.assertRaisesRegex(__lowerCAmelCase ,"Use `from_pt=True` to load this model" ): _lowerCamelCase : Any = FlaxAutoModel.from_pretrained("hf-internal-testing/tiny-bert-pt-only" )
46
import numpy as np import torch import tqdm from ...models.unet_ad import UNetaDModel from ...pipelines import DiffusionPipeline from ...utils import randn_tensor from ...utils.dummy_pt_objects import DDPMScheduler class lowerCAmelCase_ ( lowercase ): """simple docstring""" def __init__( self :int , lowerCamelCase__ :UNetaDModel , lowerCamelCase__ :UNetaDModel , lowerCamelCase__ :DDPMScheduler , lowerCamelCase__ :List[Any] , ): super().__init__() UpperCamelCase__ :Tuple = value_function UpperCamelCase__ :Optional[int] = unet UpperCamelCase__ :List[str] = scheduler UpperCamelCase__ :Dict = env UpperCamelCase__ :Dict = env.get_dataset() UpperCamelCase__ :Union[str, Any] = {} for key in self.data.keys(): try: UpperCamelCase__ :int = self.data[key].mean() except: # noqa: E722 pass UpperCamelCase__ :Any = {} for key in self.data.keys(): try: UpperCamelCase__ :int = self.data[key].std() except: # noqa: E722 pass UpperCamelCase__ :List[Any] = env.observation_space.shape[0] UpperCamelCase__ :List[str] = env.action_space.shape[0] def __a ( self :Union[str, Any] , lowerCamelCase__ :List[str] , lowerCamelCase__ :str ): return (x_in - self.means[key]) / self.stds[key] def __a ( self :int , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Tuple ): return x_in * self.stds[key] + self.means[key] def __a ( self :Any , lowerCamelCase__ :int ): if type(lowerCamelCase__ ) is dict: return {k: self.to_torch(lowerCamelCase__ ) for k, v in x_in.items()} elif torch.is_tensor(lowerCamelCase__ ): return x_in.to(self.unet.device ) return torch.tensor(lowerCamelCase__ , device=self.unet.device ) def __a ( self :Union[str, Any] , lowerCamelCase__ :List[Any] , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Tuple ): for key, val in cond.items(): UpperCamelCase__ :str = val.clone() return x_in def __a ( self :Union[str, Any] , lowerCamelCase__ :List[Any] , lowerCamelCase__ :int , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Optional[int] ): UpperCamelCase__ :Any = x.shape[0] UpperCamelCase__ :List[Any] = None for i in tqdm.tqdm(self.scheduler.timesteps ): # create batch of timesteps to pass into model UpperCamelCase__ :Optional[Any] = torch.full((batch_size,) , lowerCamelCase__ , device=self.unet.device , dtype=torch.long ) for _ in range(lowerCamelCase__ ): with torch.enable_grad(): x.requires_grad_() # permute to match dimension for pre-trained models UpperCamelCase__ :Dict = self.value_function(x.permute(0 , 2 , 1 ) , lowerCamelCase__ ).sample UpperCamelCase__ :List[Any] = torch.autograd.grad([y.sum()] , [x] )[0] UpperCamelCase__ :Union[str, Any] = self.scheduler._get_variance(lowerCamelCase__ ) UpperCamelCase__ :Any = torch.exp(0.5 * posterior_variance ) UpperCamelCase__ :Dict = model_std * grad UpperCamelCase__ :Optional[Any] = 0 UpperCamelCase__ :Dict = x.detach() UpperCamelCase__ :int = x + scale * grad UpperCamelCase__ :int = self.reset_xa(lowerCamelCase__ , lowerCamelCase__ , self.action_dim ) UpperCamelCase__ :List[str] = self.unet(x.permute(0 , 2 , 1 ) , lowerCamelCase__ ).sample.permute(0 , 2 , 1 ) # TODO: verify deprecation of this kwarg UpperCamelCase__ :List[str] = self.scheduler.step(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , predict_epsilon=lowerCamelCase__ )["""prev_sample"""] # apply conditions to the trajectory (set the initial state) UpperCamelCase__ :Optional[Any] = self.reset_xa(lowerCamelCase__ , lowerCamelCase__ , self.action_dim ) UpperCamelCase__ :Optional[int] = self.to_torch(lowerCamelCase__ ) return x, y def __call__( self :Optional[Any] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :str=64 , lowerCamelCase__ :Tuple=32 , lowerCamelCase__ :Dict=2 , lowerCamelCase__ :str=0.1 ): # normalize the observations and create batch dimension UpperCamelCase__ :List[str] = self.normalize(lowerCamelCase__ , """observations""" ) UpperCamelCase__ :List[str] = obs[None].repeat(lowerCamelCase__ , axis=0 ) UpperCamelCase__ :int = {0: self.to_torch(lowerCamelCase__ )} UpperCamelCase__ :Dict = (batch_size, planning_horizon, self.state_dim + self.action_dim) # generate initial noise and apply our conditions (to make the trajectories start at current state) UpperCamelCase__ :Any = randn_tensor(lowerCamelCase__ , device=self.unet.device ) UpperCamelCase__ :Optional[int] = self.reset_xa(lowerCamelCase__ , lowerCamelCase__ , self.action_dim ) UpperCamelCase__ :List[Any] = self.to_torch(lowerCamelCase__ ) # run the diffusion process UpperCamelCase__ , UpperCamelCase__ :Union[str, Any] = self.run_diffusion(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) # sort output trajectories by value UpperCamelCase__ :List[Any] = y.argsort(0 , descending=lowerCamelCase__ ).squeeze() UpperCamelCase__ :Dict = x[sorted_idx] UpperCamelCase__ :Tuple = sorted_values[:, :, : self.action_dim] UpperCamelCase__ :Optional[Any] = actions.detach().cpu().numpy() UpperCamelCase__ :Optional[int] = self.de_normalize(lowerCamelCase__ , key="""actions""" ) # select the action with the highest value if y is not None: UpperCamelCase__ :List[str] = 0 else: # if we didn't run value guiding, select a random action UpperCamelCase__ :Dict = np.random.randint(0 , lowerCamelCase__ ) UpperCamelCase__ :Tuple = denorm_actions[selected_index, 0] return denorm_actions
45
0
import unittest import torch from diffusers import DDIMScheduler, DDPMScheduler, UNetaDModel from diffusers.training_utils import set_seed from diffusers.utils.testing_utils import slow SCREAMING_SNAKE_CASE__ = False class _UpperCamelCase( unittest.TestCase ): def __lowerCAmelCase ( self : List[str] , SCREAMING_SNAKE_CASE__ : Union[str, Any]=3_2 ): '''simple docstring''' set_seed(0 ) __a : Optional[int] = UNetaDModel(sample_size=SCREAMING_SNAKE_CASE__ , in_channels=3 , out_channels=3 ) __a : Tuple = torch.optim.SGD(model.parameters() , lr=0.0_001 ) return model, optimizer @slow def __lowerCAmelCase ( self : int ): '''simple docstring''' __a : Any = 'cpu' # ensure full determinism without setting the CUBLAS_WORKSPACE_CONFIG env variable __a : int = DDPMScheduler( num_train_timesteps=1_0_0_0 , beta_start=0.0_001 , beta_end=0.02 , beta_schedule='linear' , clip_sample=SCREAMING_SNAKE_CASE__ , ) __a : Dict = DDIMScheduler( num_train_timesteps=1_0_0_0 , beta_start=0.0_001 , beta_end=0.02 , beta_schedule='linear' , clip_sample=SCREAMING_SNAKE_CASE__ , ) assert ddpm_scheduler.config.num_train_timesteps == ddim_scheduler.config.num_train_timesteps # shared batches for DDPM and DDIM set_seed(0 ) __a : Union[str, Any] = [torch.randn((4, 3, 3_2, 3_2) ).clip(-1 , 1 ).to(SCREAMING_SNAKE_CASE__ ) for _ in range(4 )] __a : Union[str, Any] = [torch.randn((4, 3, 3_2, 3_2) ).to(SCREAMING_SNAKE_CASE__ ) for _ in range(4 )] __a : int = [torch.randint(0 , 1_0_0_0 , (4,) ).long().to(SCREAMING_SNAKE_CASE__ ) for _ in range(4 )] # train with a DDPM scheduler __a , __a : List[str] = self.get_model_optimizer(resolution=3_2 ) model.train().to(SCREAMING_SNAKE_CASE__ ) for i in range(4 ): optimizer.zero_grad() __a : Union[str, Any] = ddpm_scheduler.add_noise(clean_images[i] , noise[i] , timesteps[i] ) __a : int = model(SCREAMING_SNAKE_CASE__ , timesteps[i] ).sample __a : Union[str, Any] = torch.nn.functional.mse_loss(SCREAMING_SNAKE_CASE__ , noise[i] ) loss.backward() optimizer.step() del model, optimizer # recreate the model and optimizer, and retry with DDIM __a , __a : List[Any] = self.get_model_optimizer(resolution=3_2 ) model.train().to(SCREAMING_SNAKE_CASE__ ) for i in range(4 ): optimizer.zero_grad() __a : Optional[int] = ddim_scheduler.add_noise(clean_images[i] , noise[i] , timesteps[i] ) __a : Union[str, Any] = model(SCREAMING_SNAKE_CASE__ , timesteps[i] ).sample __a : List[Any] = torch.nn.functional.mse_loss(SCREAMING_SNAKE_CASE__ , noise[i] ) loss.backward() optimizer.step() del model, optimizer self.assertTrue(torch.allclose(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , atol=1e-5 ) ) self.assertTrue(torch.allclose(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , atol=1e-5 ) )
47
def A ( lowercase__ : int ) -> bool: if num < 0: return False UpperCamelCase__ :int = num UpperCamelCase__ :int = 0 while num > 0: UpperCamelCase__ :Optional[int] = rev_num * 10 + (num % 10) num //= 10 return num_copy == rev_num if __name__ == "__main__": import doctest doctest.testmod()
45
0
'''simple docstring''' def A ( UpperCamelCase_ : int ) -> List[str]: '''simple docstring''' lowerCAmelCase__ ,lowerCAmelCase__ = [], [] while len(UpperCamelCase_ ) > 1: lowerCAmelCase__ ,lowerCAmelCase__ = min(UpperCamelCase_ ), max(UpperCamelCase_ ) start.append(UpperCamelCase_ ) end.append(UpperCamelCase_ ) collection.remove(UpperCamelCase_ ) collection.remove(UpperCamelCase_ ) end.reverse() return start + collection + end if __name__ == "__main__": UpperCAmelCase__ : str = input("Enter numbers separated by a comma:\n").strip() UpperCAmelCase__ : str = [int(item) for item in user_input.split(",")] print(*merge_sort(unsorted), sep=",")
48
from __future__ import annotations def A ( lowercase__ : list[int] ) -> bool: return len(set(lowercase__ ) ) == len(lowercase__ ) if __name__ == "__main__": import doctest doctest.testmod()
45
0
"""simple docstring""" from __future__ import annotations import inspect import unittest import numpy as np from transformers import ResNetConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFResNetForImageClassification, TFResNetModel from transformers.models.resnet.modeling_tf_resnet import TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class _UpperCAmelCase : def __init__( self : List[Any] , _lowercase : List[Any] , _lowercase : Optional[int]=3 , _lowercase : str=32 , _lowercase : List[str]=3 , _lowercase : str=10 , _lowercase : Optional[int]=[10, 20, 30, 40] , _lowercase : Optional[Any]=[1, 1, 2, 1] , _lowercase : Optional[Any]=True , _lowercase : List[str]=True , _lowercase : Union[str, Any]="relu" , _lowercase : List[Any]=3 , _lowercase : Tuple=None , ): __UpperCAmelCase = parent __UpperCAmelCase = batch_size __UpperCAmelCase = image_size __UpperCAmelCase = num_channels __UpperCAmelCase = embeddings_size __UpperCAmelCase = hidden_sizes __UpperCAmelCase = depths __UpperCAmelCase = is_training __UpperCAmelCase = use_labels __UpperCAmelCase = hidden_act __UpperCAmelCase = num_labels __UpperCAmelCase = scope __UpperCAmelCase = len(_lowercase ) def a ( self : Any ): __UpperCAmelCase = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __UpperCAmelCase = None if self.use_labels: __UpperCAmelCase = ids_tensor([self.batch_size] , self.num_labels ) __UpperCAmelCase = self.get_config() return config, pixel_values, labels def a ( self : List[str] ): return ResNetConfig( 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 a ( self : Optional[int] , _lowercase : Any , _lowercase : Optional[int] , _lowercase : List[Any] ): __UpperCAmelCase = TFResNetModel(config=_lowercase ) __UpperCAmelCase = model(_lowercase ) # expected last hidden states: B, C, H // 32, W // 32 self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def a ( self : Any , _lowercase : List[str] , _lowercase : Optional[Any] , _lowercase : Tuple ): __UpperCAmelCase = self.num_labels __UpperCAmelCase = TFResNetForImageClassification(_lowercase ) __UpperCAmelCase = model(_lowercase , labels=_lowercase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def a ( self : int ): __UpperCAmelCase = self.prepare_config_and_inputs() __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = config_and_inputs __UpperCAmelCase = {'''pixel_values''': pixel_values} return config, inputs_dict @require_tf class _UpperCAmelCase ( _lowerCAmelCase , _lowerCAmelCase , unittest.TestCase ): a__ : str = (TFResNetModel, TFResNetForImageClassification) if is_tf_available() else () a__ : Optional[Any] = ( {"feature-extraction": TFResNetModel, "image-classification": TFResNetForImageClassification} if is_tf_available() else {} ) a__ : str = False a__ : int = False a__ : Union[str, Any] = False a__ : Optional[int] = False a__ : List[str] = False def a ( self : str ): __UpperCAmelCase = TFResNetModelTester(self ) __UpperCAmelCase = ConfigTester(self , config_class=_lowercase , has_text_modality=_lowercase ) def a ( self : Tuple ): 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 a ( self : int ): return @unittest.skip(reason='''ResNet does not use inputs_embeds''' ) def a ( self : Union[str, Any] ): pass @unittest.skip(reason='''ResNet does not support input and output embeddings''' ) def a ( self : Tuple ): pass def a ( self : Any ): __UpperCAmelCase , __UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __UpperCAmelCase = model_class(_lowercase ) __UpperCAmelCase = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic __UpperCAmelCase = [*signature.parameters.keys()] __UpperCAmelCase = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _lowercase ) def a ( self : Union[str, Any] ): __UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_lowercase ) def a ( self : Optional[Any] ): def check_hidden_states_output(_lowercase : str , _lowercase : Optional[Any] , _lowercase : int ): __UpperCAmelCase = model_class(_lowercase ) __UpperCAmelCase = model(**self._prepare_for_class(_lowercase , _lowercase ) ) __UpperCAmelCase = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states __UpperCAmelCase = self.model_tester.num_stages self.assertEqual(len(_lowercase ) , expected_num_stages + 1 ) # ResNet's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) __UpperCAmelCase , __UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() __UpperCAmelCase = ['''basic''', '''bottleneck'''] for model_class in self.all_model_classes: for layer_type in layers_type: __UpperCAmelCase = layer_type __UpperCAmelCase = True check_hidden_states_output(_lowercase , _lowercase , _lowercase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] __UpperCAmelCase = True check_hidden_states_output(_lowercase , _lowercase , _lowercase ) def a ( self : int ): __UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_lowercase ) @slow def a ( self : Optional[Any] ): for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __UpperCAmelCase = TFResNetModel.from_pretrained(_lowercase ) self.assertIsNotNone(_lowercase ) def lowercase__ ( ): __UpperCAmelCase = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_tf @require_vision class _UpperCAmelCase ( unittest.TestCase ): @cached_property def a ( self : Optional[Any] ): return ( AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def a ( self : Tuple ): __UpperCAmelCase = TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) __UpperCAmelCase = self.default_image_processor __UpperCAmelCase = prepare_img() __UpperCAmelCase = image_processor(images=_lowercase , return_tensors='''tf''' ) # forward pass __UpperCAmelCase = model(**_lowercase ) # verify the logits __UpperCAmelCase = tf.TensorShape((1, 10_00) ) self.assertEqual(outputs.logits.shape , _lowercase ) __UpperCAmelCase = tf.constant([-11.1_069, -9.7_877, -8.3_777] ) self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() , _lowercase , atol=1E-4 ) )
49
from __future__ import annotations class lowerCAmelCase_ : """simple docstring""" def __init__( self :List[Any] , lowerCamelCase__ :int = 0 ): UpperCamelCase__ :List[str] = key def __a ( self :Optional[Any] , lowerCamelCase__ :str , lowerCamelCase__ :int ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__ :List[str] = key or self.__key or 1 # make sure key is an appropriate size key %= 2_55 return [chr(ord(lowerCamelCase__ ) ^ key ) for ch in content] def __a ( self :int , lowerCamelCase__ :str , lowerCamelCase__ :int ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__ :int = key or self.__key or 1 # make sure key is an appropriate size key %= 2_55 return [chr(ord(lowerCamelCase__ ) ^ key ) for ch in content] def __a ( self :Optional[Any] , lowerCamelCase__ :str , lowerCamelCase__ :int = 0 ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__ :Dict = key or self.__key or 1 # make sure key can be any size while key > 2_55: key -= 2_55 # This will be returned UpperCamelCase__ :List[str] = """""" for ch in content: ans += chr(ord(lowerCamelCase__ ) ^ key ) return ans def __a ( self :Any , lowerCamelCase__ :str , lowerCamelCase__ :int = 0 ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__ :Tuple = key or self.__key or 1 # make sure key can be any size while key > 2_55: key -= 2_55 # This will be returned UpperCamelCase__ :Optional[int] = """""" for ch in content: ans += chr(ord(lowerCamelCase__ ) ^ key ) return ans def __a ( self :Optional[Any] , lowerCamelCase__ :str , lowerCamelCase__ :int = 0 ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) try: with open(lowerCamelCase__ ) as fin, open("""encrypt.out""" , """w+""" ) as fout: # actual encrypt-process for line in fin: fout.write(self.encrypt_string(lowerCamelCase__ , lowerCamelCase__ ) ) except OSError: return False return True def __a ( self :Dict , lowerCamelCase__ :str , lowerCamelCase__ :int ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) try: with open(lowerCamelCase__ ) as fin, open("""decrypt.out""" , """w+""" ) as fout: # actual encrypt-process for line in fin: fout.write(self.decrypt_string(lowerCamelCase__ , lowerCamelCase__ ) ) except OSError: return False return True # Tests # crypt = XORCipher() # key = 67 # # test encrypt # print(crypt.encrypt("hallo welt",key)) # # test decrypt # print(crypt.decrypt(crypt.encrypt("hallo welt",key), key)) # # test encrypt_string # print(crypt.encrypt_string("hallo welt",key)) # # test decrypt_string # print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key)) # if (crypt.encrypt_file("test.txt",key)): # print("encrypt successful") # else: # print("encrypt unsuccessful") # if (crypt.decrypt_file("encrypt.out",key)): # print("decrypt successful") # else: # print("decrypt unsuccessful")
45
0
'''simple docstring''' import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase : Dict = logging.get_logger(__name__) UpperCamelCase : Optional[Any] = { 'asapp/sew-tiny-100k': 'https://huggingface.co/asapp/sew-tiny-100k/resolve/main/config.json', # See all SEW models at https://huggingface.co/models?filter=sew } class UpperCamelCase__ (a ): '''simple docstring''' _UpperCamelCase = 'sew' def __init__( self ,_lowerCAmelCase=32 ,_lowerCAmelCase=7_68 ,_lowerCAmelCase=12 ,_lowerCAmelCase=12 ,_lowerCAmelCase=30_72 ,_lowerCAmelCase=2 ,_lowerCAmelCase="gelu" ,_lowerCAmelCase=0.1 ,_lowerCAmelCase=0.1 ,_lowerCAmelCase=0.1 ,_lowerCAmelCase=0.0 ,_lowerCAmelCase=0.1 ,_lowerCAmelCase=0.1 ,_lowerCAmelCase=0.02 ,_lowerCAmelCase=1E-5 ,_lowerCAmelCase="group" ,_lowerCAmelCase="gelu" ,_lowerCAmelCase=(64, 1_28, 1_28, 1_28, 1_28, 2_56, 2_56, 2_56, 2_56, 5_12, 5_12, 5_12, 5_12) ,_lowerCAmelCase=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) ,_lowerCAmelCase=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) ,_lowerCAmelCase=False ,_lowerCAmelCase=1_28 ,_lowerCAmelCase=16 ,_lowerCAmelCase=True ,_lowerCAmelCase=0.05 ,_lowerCAmelCase=10 ,_lowerCAmelCase=2 ,_lowerCAmelCase=0.0 ,_lowerCAmelCase=10 ,_lowerCAmelCase=0 ,_lowerCAmelCase="mean" ,_lowerCAmelCase=False ,_lowerCAmelCase=False ,_lowerCAmelCase=2_56 ,_lowerCAmelCase=0 ,_lowerCAmelCase=1 ,_lowerCAmelCase=2 ,**_lowerCAmelCase ,): super().__init__(**_lowerCAmelCase ,pad_token_id=_lowerCAmelCase ,bos_token_id=_lowerCAmelCase ,eos_token_id=_lowerCAmelCase ) lowerCamelCase__ = hidden_size lowerCamelCase__ = feat_extract_norm lowerCamelCase__ = feat_extract_activation lowerCamelCase__ = list(_lowerCAmelCase ) lowerCamelCase__ = list(_lowerCAmelCase ) lowerCamelCase__ = list(_lowerCAmelCase ) lowerCamelCase__ = conv_bias lowerCamelCase__ = num_conv_pos_embeddings lowerCamelCase__ = num_conv_pos_embedding_groups lowerCamelCase__ = len(self.conv_dim ) lowerCamelCase__ = num_hidden_layers lowerCamelCase__ = intermediate_size lowerCamelCase__ = squeeze_factor lowerCamelCase__ = hidden_act lowerCamelCase__ = num_attention_heads lowerCamelCase__ = hidden_dropout lowerCamelCase__ = attention_dropout lowerCamelCase__ = activation_dropout lowerCamelCase__ = feat_proj_dropout lowerCamelCase__ = final_dropout lowerCamelCase__ = layerdrop lowerCamelCase__ = layer_norm_eps lowerCamelCase__ = initializer_range lowerCamelCase__ = vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( """Configuration for convolutional layers is incorrect.""" """It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,""" F'''but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)''' F'''= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 lowerCamelCase__ = apply_spec_augment lowerCamelCase__ = mask_time_prob lowerCamelCase__ = mask_time_length lowerCamelCase__ = mask_time_min_masks lowerCamelCase__ = mask_feature_prob lowerCamelCase__ = mask_feature_length lowerCamelCase__ = mask_feature_min_masks # ctc loss lowerCamelCase__ = ctc_loss_reduction lowerCamelCase__ = ctc_zero_infinity # sequence classification lowerCamelCase__ = use_weighted_layer_sum lowerCamelCase__ = classifier_proj_size @property def UpperCamelCase_ ( self ): return functools.reduce(operator.mul ,self.conv_stride ,1 )
50
import random def A ( lowercase__ : Dict , lowercase__ : str , lowercase__ : Optional[Any] ) -> int: UpperCamelCase__ :List[Any] = a[left_index] UpperCamelCase__ :Dict = left_index + 1 for j in range(left_index + 1 , lowercase__ ): if a[j] < pivot: UpperCamelCase__ , UpperCamelCase__ :Optional[int] = a[i], a[j] i += 1 UpperCamelCase__ , UpperCamelCase__ :Tuple = a[i - 1], a[left_index] return i - 1 def A ( lowercase__ : Tuple , lowercase__ : Optional[int] , lowercase__ : Any ) -> Optional[int]: if left < right: UpperCamelCase__ :List[Any] = random.randint(lowercase__ , right - 1 ) UpperCamelCase__ , UpperCamelCase__ :Union[str, Any] = ( a[left], a[pivot], ) # switches the pivot with the left most bound UpperCamelCase__ :int = partition(lowercase__ , lowercase__ , lowercase__ ) quick_sort_random( lowercase__ , lowercase__ , lowercase__ ) # recursive quicksort to the left of the pivot point quick_sort_random( lowercase__ , pivot_index + 1 , lowercase__ ) # recursive quicksort to the right of the pivot point def A ( ) -> List[Any]: UpperCamelCase__ :str = input("""Enter numbers separated by a comma:\n""" ).strip() UpperCamelCase__ :int = [int(lowercase__ ) for item in user_input.split(""",""" )] quick_sort_random(lowercase__ , 0 , len(lowercase__ ) ) print(lowercase__ ) if __name__ == "__main__": main()
45
0
'''simple docstring''' import argparse import json import logging import os import sys from unittest.mock import patch from transformers.testing_utils import TestCasePlus, get_gpu_count, slow a__ : List[str] = [ os.path.join(os.path.dirname(__file__), dirname) for dirname in [ 'text-classification', 'language-modeling', 'summarization', 'token-classification', 'question-answering', ] ] sys.path.extend(SRC_DIRS) if SRC_DIRS is not None: import run_clm_flax import run_flax_glue import run_flax_ner import run_mlm_flax import run_qa import run_summarization_flax import run_ta_mlm_flax logging.basicConfig(level=logging.DEBUG) a__ : Optional[Any] = logging.getLogger() def __snake_case ( ) -> List[Any]: """simple docstring""" UpperCAmelCase = argparse.ArgumentParser() parser.add_argument('''-f''' ) UpperCAmelCase = parser.parse_args() return args.f def __snake_case ( SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any]="eval" ) -> int: """simple docstring""" UpperCAmelCase = os.path.join(SCREAMING_SNAKE_CASE_ , f"{split}_results.json" ) if os.path.exists(SCREAMING_SNAKE_CASE_ ): with open(SCREAMING_SNAKE_CASE_ , '''r''' ) as f: return json.load(SCREAMING_SNAKE_CASE_ ) raise ValueError(f"can't find {path}" ) a__ : Union[str, Any] = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class lowerCAmelCase__ ( UpperCAmelCase_ ): '''simple docstring''' def __snake_case ( self : str ): UpperCAmelCase = self.get_auto_remove_tmp_dir() UpperCAmelCase = f"\n run_glue.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 --eval_steps=2\n --warmup_steps=2\n --seed=42\n --max_seq_length=128\n ".split() with patch.object(a__ , '''argv''' , a__ ): run_flax_glue.main() UpperCAmelCase = get_results(a__ ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.75 ) @slow def __snake_case ( self : List[Any] ): UpperCAmelCase = self.get_auto_remove_tmp_dir() UpperCAmelCase = f"\n run_clm_flax.py\n --model_name_or_path distilgpt2\n --train_file ./tests/fixtures/sample_text.txt\n --validation_file ./tests/fixtures/sample_text.txt\n --do_train\n --do_eval\n --block_size 128\n --per_device_train_batch_size 4\n --per_device_eval_batch_size 4\n --num_train_epochs 2\n --logging_steps 2 --eval_steps 2\n --output_dir {tmp_dir}\n --overwrite_output_dir\n ".split() with patch.object(a__ , '''argv''' , a__ ): run_clm_flax.main() UpperCAmelCase = get_results(a__ ) self.assertLess(result['''eval_perplexity'''] , 100 ) @slow def __snake_case ( self : Optional[Any] ): UpperCAmelCase = self.get_auto_remove_tmp_dir() UpperCAmelCase = f"\n run_summarization.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 --test_file tests/fixtures/tests_samples/xsum/sample.json\n --output_dir {tmp_dir}\n --overwrite_output_dir\n --num_train_epochs=3\n --warmup_steps=8\n --do_train\n --do_eval\n --do_predict\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --predict_with_generate\n ".split() with patch.object(a__ , '''argv''' , a__ ): run_summarization_flax.main() UpperCAmelCase = get_results(a__ , split='''test''' ) self.assertGreaterEqual(result['''test_rouge1'''] , 10 ) self.assertGreaterEqual(result['''test_rouge2'''] , 2 ) self.assertGreaterEqual(result['''test_rougeL'''] , 7 ) self.assertGreaterEqual(result['''test_rougeLsum'''] , 7 ) @slow def __snake_case ( self : List[Any] ): UpperCAmelCase = self.get_auto_remove_tmp_dir() UpperCAmelCase = f"\n run_mlm.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 --overwrite_output_dir\n --max_seq_length 128\n --per_device_train_batch_size 4\n --per_device_eval_batch_size 4\n --logging_steps 2 --eval_steps 2\n --do_train\n --do_eval\n --num_train_epochs=1\n ".split() with patch.object(a__ , '''argv''' , a__ ): run_mlm_flax.main() UpperCAmelCase = get_results(a__ ) self.assertLess(result['''eval_perplexity'''] , 42 ) @slow def __snake_case ( self : str ): UpperCAmelCase = self.get_auto_remove_tmp_dir() UpperCAmelCase = f"\n run_t5_mlm_flax.py\n --model_name_or_path t5-small\n --train_file ./tests/fixtures/sample_text.txt\n --validation_file ./tests/fixtures/sample_text.txt\n --do_train\n --do_eval\n --max_seq_length 128\n --per_device_train_batch_size 4\n --per_device_eval_batch_size 4\n --num_train_epochs 2\n --logging_steps 2 --eval_steps 2\n --output_dir {tmp_dir}\n --overwrite_output_dir\n ".split() with patch.object(a__ , '''argv''' , a__ ): run_ta_mlm_flax.main() UpperCAmelCase = get_results(a__ ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.42 ) @slow def __snake_case ( self : Tuple ): # with so little data distributed training needs more epochs to get the score on par with 0/1 gpu UpperCAmelCase = 7 if get_gpu_count() > 1 else 2 UpperCAmelCase = self.get_auto_remove_tmp_dir() UpperCAmelCase = f"\n run_flax_ner.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 --overwrite_output_dir\n --do_train\n --do_eval\n --warmup_steps=2\n --learning_rate=2e-4\n --logging_steps 2 --eval_steps 2\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=2\n --num_train_epochs={epochs}\n --seed 7\n ".split() with patch.object(a__ , '''argv''' , a__ ): run_flax_ner.main() UpperCAmelCase = get_results(a__ ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.75 ) self.assertGreaterEqual(result['''eval_f1'''] , 0.3 ) @slow def __snake_case ( self : str ): UpperCAmelCase = self.get_auto_remove_tmp_dir() UpperCAmelCase = f"\n run_qa.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 --overwrite_output_dir\n --num_train_epochs=3\n --warmup_steps=2\n --do_train\n --do_eval\n --logging_steps 2 --eval_steps 2\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n ".split() with patch.object(a__ , '''argv''' , a__ ): run_qa.main() UpperCAmelCase = get_results(a__ ) self.assertGreaterEqual(result['''eval_f1'''] , 30 ) self.assertGreaterEqual(result['''eval_exact'''] , 30 )
51
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { "shi-labs/dinat-mini-in1k-224": "https://huggingface.co/shi-labs/dinat-mini-in1k-224/resolve/main/config.json", # See all Dinat models at https://huggingface.co/models?filter=dinat } class lowerCAmelCase_ ( lowercase , lowercase ): """simple docstring""" _snake_case : Tuple = """dinat""" _snake_case : List[Any] = { """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers""", } def __init__( self :Optional[int] , lowerCamelCase__ :int=4 , lowerCamelCase__ :Union[str, Any]=3 , lowerCamelCase__ :List[Any]=64 , lowerCamelCase__ :Any=[3, 4, 6, 5] , lowerCamelCase__ :Tuple=[2, 4, 8, 16] , lowerCamelCase__ :Optional[int]=7 , lowerCamelCase__ :Tuple=[[1, 8, 1], [1, 4, 1, 4], [1, 2, 1, 2, 1, 2], [1, 1, 1, 1, 1]] , lowerCamelCase__ :Tuple=3.0 , lowerCamelCase__ :str=True , lowerCamelCase__ :Optional[int]=0.0 , lowerCamelCase__ :Optional[Any]=0.0 , lowerCamelCase__ :int=0.1 , lowerCamelCase__ :Optional[Any]="gelu" , lowerCamelCase__ :Optional[Any]=0.02 , lowerCamelCase__ :Union[str, Any]=1e-5 , lowerCamelCase__ :Optional[int]=0.0 , lowerCamelCase__ :List[str]=None , lowerCamelCase__ :str=None , **lowerCamelCase__ :List[Any] , ): super().__init__(**lowerCamelCase__ ) UpperCamelCase__ :Any = patch_size UpperCamelCase__ :Any = num_channels UpperCamelCase__ :int = embed_dim UpperCamelCase__ :Optional[Any] = depths UpperCamelCase__ :Any = len(lowerCamelCase__ ) UpperCamelCase__ :str = num_heads UpperCamelCase__ :Optional[int] = kernel_size UpperCamelCase__ :Optional[int] = dilations UpperCamelCase__ :Tuple = mlp_ratio UpperCamelCase__ :Dict = qkv_bias UpperCamelCase__ :List[str] = hidden_dropout_prob UpperCamelCase__ :List[str] = attention_probs_dropout_prob UpperCamelCase__ :Union[str, Any] = drop_path_rate UpperCamelCase__ :Tuple = hidden_act UpperCamelCase__ :List[Any] = layer_norm_eps UpperCamelCase__ :Optional[Any] = initializer_range # we set the hidden_size attribute in order to make Dinat work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model UpperCamelCase__ :Tuple = int(embed_dim * 2 ** (len(lowerCamelCase__ ) - 1) ) UpperCamelCase__ :Tuple = layer_scale_init_value UpperCamelCase__ :Optional[int] = ["""stem"""] + [f"""stage{idx}""" for idx in range(1 , len(lowerCamelCase__ ) + 1 )] UpperCamelCase__ , UpperCamelCase__ :List[str] = get_aligned_output_features_output_indices( out_features=lowerCamelCase__ , out_indices=lowerCamelCase__ , stage_names=self.stage_names )
45
0
"""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 = { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/config.json''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/config.json''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/config.json''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/config.json''', '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/config.json''', '''roberta-large-openai-detector''': '''https://huggingface.co/roberta-large-openai-detector/resolve/main/config.json''', } class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = '''roberta''' def __init__( self , _UpperCAmelCase=50265 , _UpperCAmelCase=768 , _UpperCAmelCase=12 , _UpperCAmelCase=12 , _UpperCAmelCase=3072 , _UpperCAmelCase="gelu" , _UpperCAmelCase=0.1 , _UpperCAmelCase=0.1 , _UpperCAmelCase=512 , _UpperCAmelCase=2 , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=1e-1_2 , _UpperCAmelCase=1 , _UpperCAmelCase=0 , _UpperCAmelCase=2 , _UpperCAmelCase="absolute" , _UpperCAmelCase=True , _UpperCAmelCase=None , **_UpperCAmelCase , ): super().__init__(pad_token_id=_UpperCAmelCase , bos_token_id=_UpperCAmelCase , eos_token_id=_UpperCAmelCase , **_UpperCAmelCase ) __a : Dict = vocab_size __a : Optional[int] = hidden_size __a : Optional[int] = num_hidden_layers __a : Optional[Any] = num_attention_heads __a : Tuple = hidden_act __a : int = intermediate_size __a : List[Any] = hidden_dropout_prob __a : Union[str, Any] = attention_probs_dropout_prob __a : Dict = max_position_embeddings __a : Optional[int] = type_vocab_size __a : Union[str, Any] = initializer_range __a : int = layer_norm_eps __a : List[Any] = position_embedding_type __a : Optional[int] = use_cache __a : Optional[Any] = classifier_dropout class __lowercase ( _UpperCamelCase ): '''simple docstring''' @property def _lowerCamelCase ( self ): if self.task == "multiple-choice": __a : int = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: __a : Any = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ] )
52
def A ( lowercase__ : int , lowercase__ : int ) -> int: return int(input_a == input_a == 0 ) def A ( ) -> None: print("""Truth Table of NOR Gate:""" ) print("""| Input 1 | Input 2 | Output |""" ) print(f"""| 0 | 0 | {nor_gate(0 , 0 )} |""" ) print(f"""| 0 | 1 | {nor_gate(0 , 1 )} |""" ) print(f"""| 1 | 0 | {nor_gate(1 , 0 )} |""" ) print(f"""| 1 | 1 | {nor_gate(1 , 1 )} |""" ) if __name__ == "__main__": import doctest doctest.testmod() main()
45
0
import json import os import unittest from transformers.models.biogpt.tokenization_biogpt import VOCAB_FILES_NAMES, BioGptTokenizer from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class _UpperCAmelCase ( _UpperCamelCase , unittest.TestCase ): """simple docstring""" a_ = BioGptTokenizer a_ = False def lowercase ( self : Union[str, Any] ) -> Union[str, Any]: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt __lowerCAmelCase = [ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', 'w</w>', 'r</w>', 't</w>', 'lo', 'low', 'er</w>', 'low</w>', 'lowest</w>', 'newer</w>', 'wider</w>', '<unk>', ] __lowerCAmelCase = dict(zip(lowerCAmelCase_ , range(len(lowerCAmelCase_ ) ) ) ) __lowerCAmelCase = ['l o 123', 'lo w 1456', 'e r</w> 1789', ''] __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' ) as fp: fp.write(json.dumps(lowerCAmelCase_ ) ) with open(self.merges_file , 'w' ) as fp: fp.write('\n'.join(lowerCAmelCase_ ) ) def lowercase ( self : List[Any] , lowerCAmelCase_ : Optional[Any] ) -> int: __lowerCAmelCase = 'lower newer' __lowerCAmelCase = 'lower newer' return input_text, output_text def lowercase ( self : Optional[Any] ) -> int: __lowerCAmelCase = BioGptTokenizer(self.vocab_file , self.merges_file ) __lowerCAmelCase = 'lower' __lowerCAmelCase = ['low', 'er</w>'] __lowerCAmelCase = tokenizer.tokenize(lowerCAmelCase_ ) self.assertListEqual(lowerCAmelCase_ , lowerCAmelCase_ ) __lowerCAmelCase = tokens + ['<unk>'] __lowerCAmelCase = [1_4, 1_5, 2_0] self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase_ ) , lowerCAmelCase_ ) @slow def lowercase ( self : int ) -> Union[str, Any]: __lowerCAmelCase = BioGptTokenizer.from_pretrained('microsoft/biogpt' ) __lowerCAmelCase = tokenizer.encode('sequence builders' , add_special_tokens=lowerCAmelCase_ ) __lowerCAmelCase = tokenizer.encode('multi-sequence build' , add_special_tokens=lowerCAmelCase_ ) __lowerCAmelCase = tokenizer.build_inputs_with_special_tokens(lowerCAmelCase_ ) __lowerCAmelCase = tokenizer.build_inputs_with_special_tokens(lowerCAmelCase_ , lowerCAmelCase_ ) self.assertTrue(encoded_sentence == [2] + text ) self.assertTrue(encoded_pair == [2] + text + [2] + text_a )
53
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import GLPNImageProcessor class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __init__( self :Any , lowerCamelCase__ :Dict , lowerCamelCase__ :Optional[Any]=7 , lowerCamelCase__ :str=3 , lowerCamelCase__ :Optional[Any]=18 , lowerCamelCase__ :List[str]=30 , lowerCamelCase__ :str=4_00 , lowerCamelCase__ :Optional[int]=True , lowerCamelCase__ :Union[str, Any]=32 , lowerCamelCase__ :int=True , ): UpperCamelCase__ :List[Any] = parent UpperCamelCase__ :List[Any] = batch_size UpperCamelCase__ :Any = num_channels UpperCamelCase__ :List[str] = image_size UpperCamelCase__ :Dict = min_resolution UpperCamelCase__ :List[str] = max_resolution UpperCamelCase__ :str = do_resize UpperCamelCase__ :int = size_divisor UpperCamelCase__ :Optional[int] = do_rescale def __a ( self :str ): return { "do_resize": self.do_resize, "size_divisor": self.size_divisor, "do_rescale": self.do_rescale, } @require_torch @require_vision class lowerCAmelCase_ ( lowercase , unittest.TestCase ): """simple docstring""" _snake_case : Optional[int] = GLPNImageProcessor if is_vision_available() else None def __a ( self :Dict ): UpperCamelCase__ :Dict = GLPNImageProcessingTester(self ) @property def __a ( self :List[str] ): return self.image_processor_tester.prepare_image_processor_dict() def __a ( self :Optional[int] ): UpperCamelCase__ :Optional[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(lowerCamelCase__ , """do_resize""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """size_divisor""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """resample""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """do_rescale""" ) ) def __a ( self :Optional[int] ): pass def __a ( self :Tuple ): # Initialize image_processing UpperCamelCase__ :int = self.image_processing_class(**self.image_processor_dict ) # create random PIL images UpperCamelCase__ :str = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCamelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCamelCase__ , Image.Image ) # Test not batched input (GLPNImageProcessor doesn't support batching) UpperCamelCase__ :Tuple = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 ) self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 ) def __a ( self :str ): # Initialize image_processing UpperCamelCase__ :str = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors UpperCamelCase__ :Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCamelCase__ , numpify=lowerCamelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCamelCase__ , np.ndarray ) # Test not batched input (GLPNImageProcessor doesn't support batching) UpperCamelCase__ :List[str] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 ) self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 ) def __a ( self :Any ): # Initialize image_processing UpperCamelCase__ :List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors UpperCamelCase__ :Tuple = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCamelCase__ , torchify=lowerCamelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCamelCase__ , torch.Tensor ) # Test not batched input (GLPNImageProcessor doesn't support batching) UpperCamelCase__ :List[str] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 ) self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 )
45
0
from ...configuration_utils import PretrainedConfig from ...utils import logging __lowercase : List[Any] =logging.get_logger(__name__) __lowercase : Optional[int] ={"""openai-gpt""": """https://huggingface.co/openai-gpt/resolve/main/config.json"""} class A ( __lowercase ): _snake_case ='''openai-gpt''' _snake_case ={ '''max_position_embeddings''': '''n_positions''', '''hidden_size''': '''n_embd''', '''num_attention_heads''': '''n_head''', '''num_hidden_layers''': '''n_layer''', } def __init__( self: List[str] , _lowerCAmelCase: List[str]=4_0478 , _lowerCAmelCase: Any=512 , _lowerCAmelCase: Optional[Any]=768 , _lowerCAmelCase: str=12 , _lowerCAmelCase: str=12 , _lowerCAmelCase: Any="gelu" , _lowerCAmelCase: str=0.1 , _lowerCAmelCase: Optional[int]=0.1 , _lowerCAmelCase: str=0.1 , _lowerCAmelCase: Optional[Any]=1e-5 , _lowerCAmelCase: Optional[int]=0.02 , _lowerCAmelCase: Dict="cls_index" , _lowerCAmelCase: List[Any]=True , _lowerCAmelCase: List[Any]=None , _lowerCAmelCase: Tuple=True , _lowerCAmelCase: List[str]=0.1 , **_lowerCAmelCase: int , ) -> Dict: '''simple docstring''' UpperCAmelCase_ =vocab_size UpperCAmelCase_ =n_positions UpperCAmelCase_ =n_embd UpperCAmelCase_ =n_layer UpperCAmelCase_ =n_head UpperCAmelCase_ =afn UpperCAmelCase_ =resid_pdrop UpperCAmelCase_ =embd_pdrop UpperCAmelCase_ =attn_pdrop UpperCAmelCase_ =layer_norm_epsilon UpperCAmelCase_ =initializer_range UpperCAmelCase_ =summary_type UpperCAmelCase_ =summary_use_proj UpperCAmelCase_ =summary_activation UpperCAmelCase_ =summary_first_dropout UpperCAmelCase_ =summary_proj_to_labels super().__init__(**_lowerCAmelCase )
54
import math def A ( lowercase__ : Tuple , lowercase__ : Union[str, Any] ) -> Optional[Any]: if 0 not in (x, y): # We use the relation x^y = y*log10(x), where 10 is the base. return y * math.logaa(lowercase__ ) else: if x == 0: # 0 raised to any number is 0 return 0 elif y == 0: return 1 # any number raised to 0 is 1 raise AssertionError("""This should never happen""" ) if __name__ == "__main__": # Main function # Read two numbers from input and typecast them to int using map function. # Here x is the base and y is the power. UpperCamelCase = "Enter the base and the power separated by a comma: " UpperCamelCase , UpperCamelCase = map(int, input(prompt).split(",")) UpperCamelCase , UpperCamelCase = map(int, input(prompt).split(",")) # We find the log of each number, using the function res(), which takes two # arguments. UpperCamelCase = res(xa, ya) UpperCamelCase = res(xa, ya) # We check for the largest number if resa > resa: print("Largest number is", xa, "^", ya) elif resa > resa: print("Largest number is", xa, "^", ya) else: print("Both are equal")
45
0
import copy import os import cva import numpy as np from matplotlib import pyplot as plt class UpperCAmelCase : '''simple docstring''' def __init__( self : str ): __A = "" __A = "" __A = [] __A = 0 __A = 2_56 __A = 0 __A = 0 __A = 0 __A = 0 def UpperCamelCase_ ( self : Union[str, Any] ,A : Dict ): __A = cva.imread(A ,0 ) __A = copy.deepcopy(self.img ) __A , __A , __A = plt.hist(self.img.ravel() ,2_56 ,[0, 2_56] ,label="x" ) __A = np.sum(A ) for i in range(len(A ) ): __A = x[i] / self.k self.sk += prk __A = (self.L - 1) * self.sk if self.rem != 0: __A = int(last % last ) __A = int(last + 1 if self.rem >= 0.5 else last ) self.last_list.append(A ) __A = int(np.ma.count(self.img ) / self.img[1].size ) __A = self.img[1].size for i in range(self.number_of_cols ): for j in range(self.number_of_rows ): __A = self.img[j][i] if num != self.last_list[num]: __A = self.last_list[num] cva.imwrite("output_data/output.jpg" ,self.img ) def UpperCamelCase_ ( self : Optional[Any] ): plt.hist(self.img.ravel() ,2_56 ,[0, 2_56] ) def UpperCamelCase_ ( self : Any ): cva.imshow("Output-Image" ,self.img ) cva.imshow("Input-Image" ,self.original_image ) cva.waitKey(50_00 ) cva.destroyAllWindows() if __name__ == "__main__": SCREAMING_SNAKE_CASE :Union[str, Any] = os.path.join(os.path.basename(__file__), 'image_data/input.jpg') SCREAMING_SNAKE_CASE :Tuple = ConstantStretch() stretcher.stretch(file_path) stretcher.plot_histogram() stretcher.show_image()
55
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, 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 numpy as np import tensorflow as tf from transformers import ( TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST, FlaubertConfig, TFFlaubertForMultipleChoice, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForSequenceClassification, TFFlaubertForTokenClassification, TFFlaubertModel, TFFlaubertWithLMHeadModel, ) class lowerCAmelCase_ : """simple docstring""" def __init__( self :Dict , lowerCamelCase__ :List[str] , ): UpperCamelCase__ :Optional[int] = parent UpperCamelCase__ :int = 13 UpperCamelCase__ :Optional[int] = 7 UpperCamelCase__ :Dict = True UpperCamelCase__ :Dict = True UpperCamelCase__ :str = True UpperCamelCase__ :List[Any] = True UpperCamelCase__ :Any = True UpperCamelCase__ :Optional[int] = False UpperCamelCase__ :Optional[int] = False UpperCamelCase__ :Tuple = False UpperCamelCase__ :Optional[int] = 2 UpperCamelCase__ :List[str] = 99 UpperCamelCase__ :Optional[Any] = 0 UpperCamelCase__ :Any = 32 UpperCamelCase__ :List[str] = 2 UpperCamelCase__ :int = 4 UpperCamelCase__ :List[str] = 0.1 UpperCamelCase__ :Union[str, Any] = 0.1 UpperCamelCase__ :Union[str, Any] = 5_12 UpperCamelCase__ :List[str] = 16 UpperCamelCase__ :str = 2 UpperCamelCase__ :Optional[int] = 0.02 UpperCamelCase__ :Optional[int] = 3 UpperCamelCase__ :Optional[int] = 4 UpperCamelCase__ :Optional[int] = """last""" UpperCamelCase__ :Tuple = True UpperCamelCase__ :int = None UpperCamelCase__ :Dict = 0 def __a ( self :int ): UpperCamelCase__ :Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCamelCase__ :Any = random_attention_mask([self.batch_size, self.seq_length] , dtype=tf.floataa ) UpperCamelCase__ :Union[str, Any] = None if self.use_input_lengths: UpperCamelCase__ :Union[str, Any] = ( ids_tensor([self.batch_size] , vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length UpperCamelCase__ :List[str] = None if self.use_token_type_ids: UpperCamelCase__ :List[str] = ids_tensor([self.batch_size, self.seq_length] , self.n_langs ) UpperCamelCase__ :int = None UpperCamelCase__ :List[str] = None UpperCamelCase__ :List[str] = None if self.use_labels: UpperCamelCase__ :List[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase__ :Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCamelCase__ :str = ids_tensor([self.batch_size] , 2 , dtype=tf.floataa ) UpperCamelCase__ :int = ids_tensor([self.batch_size] , self.num_choices ) UpperCamelCase__ :List[Any] = FlaubertConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , bos_token_id=self.bos_token_id , ) return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def __a ( self :Union[str, Any] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :int , lowerCamelCase__ :List[str] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :int , lowerCamelCase__ :List[Any] , lowerCamelCase__ :List[Any] , lowerCamelCase__ :int , ): UpperCamelCase__ :int = TFFlaubertModel(config=lowerCamelCase__ ) UpperCamelCase__ :Union[str, Any] = {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids} UpperCamelCase__ :Optional[Any] = model(lowerCamelCase__ ) UpperCamelCase__ :Union[str, Any] = [input_ids, input_mask] UpperCamelCase__ :Optional[int] = model(lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __a ( self :Tuple , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Any , lowerCamelCase__ :int , lowerCamelCase__ :int , lowerCamelCase__ :List[str] , lowerCamelCase__ :Any , lowerCamelCase__ :Optional[Any] , ): UpperCamelCase__ :List[str] = TFFlaubertWithLMHeadModel(lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids} UpperCamelCase__ :Any = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __a ( self :Dict , lowerCamelCase__ :List[str] , lowerCamelCase__ :Dict , lowerCamelCase__ :Tuple , lowerCamelCase__ :str , lowerCamelCase__ :Any , lowerCamelCase__ :Tuple , lowerCamelCase__ :Any , lowerCamelCase__ :int , lowerCamelCase__ :Tuple , ): UpperCamelCase__ :int = TFFlaubertForQuestionAnsweringSimple(lowerCamelCase__ ) UpperCamelCase__ :int = {"""input_ids""": input_ids, """lengths""": input_lengths} UpperCamelCase__ :Optional[int] = model(lowerCamelCase__ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __a ( self :List[Any] , lowerCamelCase__ :List[str] , lowerCamelCase__ :List[str] , lowerCamelCase__ :str , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Tuple , lowerCamelCase__ :Tuple , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :int , lowerCamelCase__ :Optional[int] , ): UpperCamelCase__ :List[Any] = TFFlaubertForSequenceClassification(lowerCamelCase__ ) UpperCamelCase__ :List[str] = {"""input_ids""": input_ids, """lengths""": input_lengths} UpperCamelCase__ :List[str] = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __a ( self :Tuple , lowerCamelCase__ :str , lowerCamelCase__ :Any , lowerCamelCase__ :Tuple , lowerCamelCase__ :str , lowerCamelCase__ :Any , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :List[str] , lowerCamelCase__ :str , lowerCamelCase__ :Any , ): UpperCamelCase__ :Any = self.num_labels UpperCamelCase__ :Tuple = TFFlaubertForTokenClassification(config=lowerCamelCase__ ) UpperCamelCase__ :Any = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} UpperCamelCase__ :List[Any] = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __a ( self :Tuple , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Tuple , lowerCamelCase__ :Any , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :str , lowerCamelCase__ :Any , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Dict , lowerCamelCase__ :List[str] , ): UpperCamelCase__ :Optional[int] = self.num_choices UpperCamelCase__ :Dict = TFFlaubertForMultipleChoice(config=lowerCamelCase__ ) UpperCamelCase__ :Any = tf.tile(tf.expand_dims(lowerCamelCase__ , 1 ) , (1, self.num_choices, 1) ) UpperCamelCase__ :str = tf.tile(tf.expand_dims(lowerCamelCase__ , 1 ) , (1, self.num_choices, 1) ) UpperCamelCase__ :Any = tf.tile(tf.expand_dims(lowerCamelCase__ , 1 ) , (1, self.num_choices, 1) ) UpperCamelCase__ :int = { """input_ids""": multiple_choice_inputs_ids, """attention_mask""": multiple_choice_input_mask, """token_type_ids""": multiple_choice_token_type_ids, } UpperCamelCase__ :List[str] = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __a ( self :Tuple ): UpperCamelCase__ :str = self.prepare_config_and_inputs() ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :str = config_and_inputs UpperCamelCase__ :Optional[Any] = { """input_ids""": input_ids, """token_type_ids""": token_type_ids, """langs""": token_type_ids, """lengths""": input_lengths, } return config, inputs_dict @require_tf class lowerCAmelCase_ ( lowercase , lowercase , unittest.TestCase ): """simple docstring""" _snake_case : List[str] = ( ( TFFlaubertModel, TFFlaubertWithLMHeadModel, TFFlaubertForSequenceClassification, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForTokenClassification, TFFlaubertForMultipleChoice, ) if is_tf_available() else () ) _snake_case : List[Any] = ( (TFFlaubertWithLMHeadModel,) if is_tf_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable _snake_case : Optional[int] = ( { """feature-extraction""": TFFlaubertModel, """fill-mask""": TFFlaubertWithLMHeadModel, """question-answering""": TFFlaubertForQuestionAnsweringSimple, """text-classification""": TFFlaubertForSequenceClassification, """token-classification""": TFFlaubertForTokenClassification, """zero-shot""": TFFlaubertForSequenceClassification, } if is_tf_available() else {} ) _snake_case : List[Any] = False _snake_case : Tuple = False def __a ( self :Optional[int] , lowerCamelCase__ :List[Any] , lowerCamelCase__ :Dict , lowerCamelCase__ :int , lowerCamelCase__ :str , lowerCamelCase__ :List[Any] ): if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith("""Fast""" ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def __a ( self :List[str] ): UpperCamelCase__ :List[str] = TFFlaubertModelTester(self ) UpperCamelCase__ :Tuple = ConfigTester(self , config_class=lowerCamelCase__ , emb_dim=37 ) def __a ( self :int ): self.config_tester.run_common_tests() def __a ( self :List[str] ): UpperCamelCase__ :List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*lowerCamelCase__ ) def __a ( self :Tuple ): UpperCamelCase__ :Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*lowerCamelCase__ ) def __a ( self :Union[str, Any] ): UpperCamelCase__ :Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*lowerCamelCase__ ) def __a ( self :List[Any] ): UpperCamelCase__ :Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*lowerCamelCase__ ) def __a ( self :Any ): UpperCamelCase__ :Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_token_classification(*lowerCamelCase__ ) def __a ( self :List[Any] ): UpperCamelCase__ :Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_multiple_choice(*lowerCamelCase__ ) @slow def __a ( self :str ): for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase__ :Dict = TFFlaubertModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) @require_tf @require_sentencepiece @require_tokenizers class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" @slow def __a ( self :str ): UpperCamelCase__ :Tuple = TFFlaubertModel.from_pretrained("""jplu/tf-flaubert-small-cased""" ) UpperCamelCase__ :Optional[int] = tf.convert_to_tensor( [[0, 1_58, 7_35, 25_92, 14_24, 67_27, 82, 1]] , dtype=tf.intaa , ) # "J'aime flaubert !" UpperCamelCase__ :Optional[Any] = model(lowerCamelCase__ )[0] UpperCamelCase__ :Optional[int] = tf.TensorShape((1, 8, 5_12) ) self.assertEqual(output.shape , lowerCamelCase__ ) # compare the actual values for a slice. UpperCamelCase__ :str = tf.convert_to_tensor( [ [ [-1.876_8773, -1.56_6555, 0.2707_2418], [-1.692_0038, -0.587_3505, 1.932_9599], [-2.956_3985, -1.699_3835, 1.797_2052], ] ] , dtype=tf.floataa , ) self.assertTrue(np.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1e-4 ) )
45
0
'''simple docstring''' import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES from ...utils import logging from ..auto import CONFIG_MAPPING _a : List[Any] = logging.get_logger(__name__) _a : str = { "Salesforce/instruct-blip-flan-t5": "https://huggingface.co/Salesforce/instruct-blip-flan-t5/resolve/main/config.json", } class _lowercase ( __lowercase ): _SCREAMING_SNAKE_CASE : Any = "instructblip_vision_model" def __init__( self : int , SCREAMING_SNAKE_CASE_ : Optional[Any]=1408 , SCREAMING_SNAKE_CASE_ : str=6144 , SCREAMING_SNAKE_CASE_ : List[Any]=39 , SCREAMING_SNAKE_CASE_ : List[str]=16 , SCREAMING_SNAKE_CASE_ : Optional[int]=224 , SCREAMING_SNAKE_CASE_ : List[str]=14 , SCREAMING_SNAKE_CASE_ : Dict="gelu" , SCREAMING_SNAKE_CASE_ : Any=1e-6 , SCREAMING_SNAKE_CASE_ : Optional[Any]=0.0 , SCREAMING_SNAKE_CASE_ : Any=1e-10 , SCREAMING_SNAKE_CASE_ : Any=True , **SCREAMING_SNAKE_CASE_ : List[Any] , ) -> int: super().__init__(**SCREAMING_SNAKE_CASE_ ) __snake_case = hidden_size __snake_case = intermediate_size __snake_case = num_hidden_layers __snake_case = num_attention_heads __snake_case = patch_size __snake_case = image_size __snake_case = initializer_range __snake_case = attention_dropout __snake_case = layer_norm_eps __snake_case = hidden_act __snake_case = qkv_bias @classmethod def a ( cls : Any , SCREAMING_SNAKE_CASE_ : Union[str, os.PathLike] , **SCREAMING_SNAKE_CASE_ : List[str] ) -> "PretrainedConfig": cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE_ ) __snake_case , __snake_case = cls.get_config_dict(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) # get the vision config dict if we are loading from InstructBlipConfig if config_dict.get('model_type' ) == "instructblip": __snake_case = config_dict['vision_config'] if "model_type" in config_dict and hasattr(cls , 'model_type' ) and config_dict["model_type"] != cls.model_type: logger.warning( f'You are using a model of type {config_dict["model_type"]} to instantiate a model of type ' f'{cls.model_type}. This is not supported for all configurations of models and can yield errors.' ) return cls.from_dict(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) class _lowercase ( __lowercase ): _SCREAMING_SNAKE_CASE : Union[str, Any] = "instructblip_qformer" def __init__( self : List[str] , SCREAMING_SNAKE_CASE_ : List[Any]=3_0522 , SCREAMING_SNAKE_CASE_ : str=768 , SCREAMING_SNAKE_CASE_ : List[str]=12 , SCREAMING_SNAKE_CASE_ : Optional[Any]=12 , SCREAMING_SNAKE_CASE_ : Dict=3072 , SCREAMING_SNAKE_CASE_ : Dict="gelu" , SCREAMING_SNAKE_CASE_ : int=0.1 , SCREAMING_SNAKE_CASE_ : List[Any]=0.1 , SCREAMING_SNAKE_CASE_ : List[str]=512 , SCREAMING_SNAKE_CASE_ : Optional[int]=0.0_2 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=1e-12 , SCREAMING_SNAKE_CASE_ : int=0 , SCREAMING_SNAKE_CASE_ : int="absolute" , SCREAMING_SNAKE_CASE_ : Tuple=2 , SCREAMING_SNAKE_CASE_ : str=1408 , **SCREAMING_SNAKE_CASE_ : List[str] , ) -> Any: super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) __snake_case = vocab_size __snake_case = hidden_size __snake_case = num_hidden_layers __snake_case = num_attention_heads __snake_case = hidden_act __snake_case = intermediate_size __snake_case = hidden_dropout_prob __snake_case = attention_probs_dropout_prob __snake_case = max_position_embeddings __snake_case = initializer_range __snake_case = layer_norm_eps __snake_case = position_embedding_type __snake_case = cross_attention_frequency __snake_case = encoder_hidden_size @classmethod def a ( cls : List[Any] , SCREAMING_SNAKE_CASE_ : Union[str, os.PathLike] , **SCREAMING_SNAKE_CASE_ : Dict ) -> "PretrainedConfig": cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE_ ) __snake_case , __snake_case = cls.get_config_dict(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) # get the qformer config dict if we are loading from InstructBlipConfig if config_dict.get('model_type' ) == "instructblip": __snake_case = config_dict['qformer_config'] if "model_type" in config_dict and hasattr(cls , 'model_type' ) and config_dict["model_type"] != cls.model_type: logger.warning( f'You are using a model of type {config_dict["model_type"]} to instantiate a model of type ' f'{cls.model_type}. This is not supported for all configurations of models and can yield errors.' ) return cls.from_dict(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) class _lowercase ( __lowercase ): _SCREAMING_SNAKE_CASE : Dict = "instructblip" _SCREAMING_SNAKE_CASE : str = True def __init__( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[Any]=None , SCREAMING_SNAKE_CASE_ : str=None , SCREAMING_SNAKE_CASE_ : List[str]=None , SCREAMING_SNAKE_CASE_ : List[str]=32 , **SCREAMING_SNAKE_CASE_ : str ) -> int: super().__init__(**SCREAMING_SNAKE_CASE_ ) if vision_config is None: __snake_case = {} logger.info('vision_config is None. initializing the InstructBlipVisionConfig with default values.' ) if qformer_config is None: __snake_case = {} logger.info('qformer_config is None. Initializing the InstructBlipQFormerConfig with default values.' ) if text_config is None: __snake_case = {} logger.info('text_config is None. Initializing the text config with default values (`OPTConfig`).' ) __snake_case = InstructBlipVisionConfig(**SCREAMING_SNAKE_CASE_ ) __snake_case = InstructBlipQFormerConfig(**SCREAMING_SNAKE_CASE_ ) __snake_case = text_config['model_type'] if 'model_type' in text_config else 'opt' __snake_case = CONFIG_MAPPING[text_model_type](**SCREAMING_SNAKE_CASE_ ) __snake_case = self.text_config.tie_word_embeddings __snake_case = self.text_config.is_encoder_decoder __snake_case = num_query_tokens __snake_case = self.vision_config.hidden_size __snake_case = self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES __snake_case = 1.0 __snake_case = 0.0_2 @classmethod def a ( cls : Union[str, Any] , SCREAMING_SNAKE_CASE_ : InstructBlipVisionConfig , SCREAMING_SNAKE_CASE_ : InstructBlipQFormerConfig , SCREAMING_SNAKE_CASE_ : PretrainedConfig , **SCREAMING_SNAKE_CASE_ : Tuple , ) -> Optional[Any]: return cls( vision_config=vision_config.to_dict() , qformer_config=qformer_config.to_dict() , text_config=text_config.to_dict() , **SCREAMING_SNAKE_CASE_ , ) def a ( self : Optional[int] ) -> Any: __snake_case = copy.deepcopy(self.__dict__ ) __snake_case = self.vision_config.to_dict() __snake_case = self.qformer_config.to_dict() __snake_case = self.text_config.to_dict() __snake_case = self.__class__.model_type return output
56
import gc import tempfile import unittest import numpy as np import torch from diffusers import VersatileDiffusionPipeline from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device UpperCamelCase = False class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" pass @nightly @require_torch_gpu class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __a ( self :Union[str, Any] ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __a ( self :List[Any] ): UpperCamelCase__ :List[str] = VersatileDiffusionPipeline.from_pretrained("""shi-labs/versatile-diffusion""" , torch_dtype=torch.floataa ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) UpperCamelCase__ :Dict = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg""" ) UpperCamelCase__ :Any = torch.manual_seed(0 ) UpperCamelCase__ :Optional[int] = pipe.dual_guided( prompt="""first prompt""" , image=lowerCamelCase__ , text_to_image_strength=0.75 , generator=lowerCamelCase__ , guidance_scale=7.5 , num_inference_steps=2 , output_type="""numpy""" , ).images with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(lowerCamelCase__ ) UpperCamelCase__ :List[str] = VersatileDiffusionPipeline.from_pretrained(lowerCamelCase__ , torch_dtype=torch.floataa ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) UpperCamelCase__ :str = generator.manual_seed(0 ) UpperCamelCase__ :str = pipe.dual_guided( prompt="""first prompt""" , image=lowerCamelCase__ , text_to_image_strength=0.75 , generator=lowerCamelCase__ , guidance_scale=7.5 , num_inference_steps=2 , output_type="""numpy""" , ).images assert np.abs(image - new_image ).sum() < 1e-5, "Models don't have the same forward pass" def __a ( self :Dict ): UpperCamelCase__ :List[Any] = VersatileDiffusionPipeline.from_pretrained("""shi-labs/versatile-diffusion""" , torch_dtype=torch.floataa ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = """cyberpunk 2077""" UpperCamelCase__ :str = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg""" ) UpperCamelCase__ :str = torch.manual_seed(0 ) UpperCamelCase__ :Dict = pipe.dual_guided( prompt=lowerCamelCase__ , image=lowerCamelCase__ , text_to_image_strength=0.75 , generator=lowerCamelCase__ , guidance_scale=7.5 , num_inference_steps=50 , output_type="""numpy""" , ).images UpperCamelCase__ :Tuple = image[0, 2_53:2_56, 2_53:2_56, -1] assert image.shape == (1, 5_12, 5_12, 3) UpperCamelCase__ :Any = np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 UpperCamelCase__ :List[Any] = """A painting of a squirrel eating a burger """ UpperCamelCase__ :List[str] = torch.manual_seed(0 ) UpperCamelCase__ :Optional[int] = pipe.text_to_image( prompt=lowerCamelCase__ , generator=lowerCamelCase__ , guidance_scale=7.5 , num_inference_steps=50 , output_type="""numpy""" ).images UpperCamelCase__ :str = image[0, 2_53:2_56, 2_53:2_56, -1] assert image.shape == (1, 5_12, 5_12, 3) UpperCamelCase__ :Union[str, Any] = np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 UpperCamelCase__ :Optional[int] = pipe.image_variation(lowerCamelCase__ , generator=lowerCamelCase__ , output_type="""numpy""" ).images UpperCamelCase__ :int = image[0, 2_53:2_56, 2_53:2_56, -1] assert image.shape == (1, 5_12, 5_12, 3) UpperCamelCase__ :List[Any] = np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1
45
0
from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_herbert import HerbertTokenizer A_ : Optional[int] = logging.get_logger(__name__) A_ : Optional[Any] = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'} A_ : Dict = { 'vocab_file': { 'allegro/herbert-base-cased': 'https://huggingface.co/allegro/herbert-base-cased/resolve/main/vocab.json' }, 'merges_file': { 'allegro/herbert-base-cased': 'https://huggingface.co/allegro/herbert-base-cased/resolve/main/merges.txt' }, } A_ : Optional[Any] = {'allegro/herbert-base-cased': 514} A_ : Dict = {} class _lowerCAmelCase( UpperCAmelCase_ ): """simple docstring""" a : str =VOCAB_FILES_NAMES a : Tuple =PRETRAINED_VOCAB_FILES_MAP a : Optional[Any] =PRETRAINED_INIT_CONFIGURATION a : List[Any] =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES a : Optional[Any] =HerbertTokenizer def __init__( self , _lowerCamelCase=None , _lowerCamelCase=None , _lowerCamelCase=None , _lowerCamelCase="<s>" , _lowerCamelCase="<unk>" , _lowerCamelCase="<pad>" , _lowerCamelCase="<mask>" , _lowerCamelCase="</s>" , **_lowerCamelCase , ): super().__init__( _lowerCamelCase , _lowerCamelCase , tokenizer_file=_lowerCamelCase , cls_token=_lowerCamelCase , unk_token=_lowerCamelCase , pad_token=_lowerCamelCase , mask_token=_lowerCamelCase , sep_token=_lowerCamelCase , **_lowerCamelCase , ) def _a ( self , _lowerCamelCase , _lowerCamelCase = None ): UpperCamelCase_: List[str] = [self.cls_token_id] UpperCamelCase_: List[str] = [self.sep_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def _a ( self , _lowerCamelCase , _lowerCamelCase = None , _lowerCamelCase = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_lowerCamelCase , token_ids_a=_lowerCamelCase , already_has_special_tokens=_lowerCamelCase ) if token_ids_a is None: return [1] + ([0] * len(_lowerCamelCase )) + [1] return [1] + ([0] * len(_lowerCamelCase )) + [1] + ([0] * len(_lowerCamelCase )) + [1] def _a ( self , _lowerCamelCase , _lowerCamelCase = None ): UpperCamelCase_: Any = [self.sep_token_id] UpperCamelCase_: Dict = [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 _a ( self , _lowerCamelCase , _lowerCamelCase = None ): UpperCamelCase_: Union[str, Any] = self._tokenizer.model.save(_lowerCamelCase , name=_lowerCamelCase ) return tuple(_lowerCamelCase )
57
from __future__ import annotations import copy import inspect import unittest import numpy as np from transformers import is_tf_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST, TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, TFLayoutLMvaModel, ) if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class lowerCAmelCase_ : """simple docstring""" def __init__( self :Union[str, Any] , lowerCamelCase__ :Tuple , lowerCamelCase__ :List[str]=2 , lowerCamelCase__ :List[str]=3 , lowerCamelCase__ :List[str]=4 , lowerCamelCase__ :str=2 , lowerCamelCase__ :Optional[int]=7 , lowerCamelCase__ :List[Any]=True , lowerCamelCase__ :Optional[Any]=True , lowerCamelCase__ :Union[str, Any]=True , lowerCamelCase__ :Any=True , lowerCamelCase__ :Dict=99 , lowerCamelCase__ :Optional[Any]=36 , lowerCamelCase__ :str=2 , lowerCamelCase__ :List[Any]=4 , lowerCamelCase__ :Optional[Any]=37 , lowerCamelCase__ :Optional[int]="gelu" , lowerCamelCase__ :Any=0.1 , lowerCamelCase__ :List[Any]=0.1 , lowerCamelCase__ :List[Any]=5_12 , lowerCamelCase__ :str=16 , lowerCamelCase__ :Tuple=2 , lowerCamelCase__ :int=0.02 , lowerCamelCase__ :List[Any]=6 , lowerCamelCase__ :List[str]=6 , lowerCamelCase__ :Optional[int]=3 , lowerCamelCase__ :Optional[int]=4 , lowerCamelCase__ :int=None , lowerCamelCase__ :Optional[Any]=10_00 , ): UpperCamelCase__ :Any = parent UpperCamelCase__ :Union[str, Any] = batch_size UpperCamelCase__ :Dict = num_channels UpperCamelCase__ :Optional[Any] = image_size UpperCamelCase__ :Union[str, Any] = patch_size UpperCamelCase__ :Union[str, Any] = is_training UpperCamelCase__ :str = use_input_mask UpperCamelCase__ :int = use_token_type_ids UpperCamelCase__ :int = use_labels UpperCamelCase__ :List[Any] = vocab_size UpperCamelCase__ :List[str] = hidden_size UpperCamelCase__ :List[Any] = num_hidden_layers UpperCamelCase__ :List[str] = num_attention_heads UpperCamelCase__ :Tuple = intermediate_size UpperCamelCase__ :Any = hidden_act UpperCamelCase__ :Optional[int] = hidden_dropout_prob UpperCamelCase__ :Tuple = attention_probs_dropout_prob UpperCamelCase__ :Dict = max_position_embeddings UpperCamelCase__ :Tuple = type_vocab_size UpperCamelCase__ :Union[str, Any] = type_sequence_label_size UpperCamelCase__ :int = initializer_range UpperCamelCase__ :List[Any] = coordinate_size UpperCamelCase__ :Tuple = shape_size UpperCamelCase__ :Dict = num_labels UpperCamelCase__ :str = num_choices UpperCamelCase__ :Tuple = scope UpperCamelCase__ :str = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) UpperCamelCase__ :List[str] = text_seq_length UpperCamelCase__ :List[str] = (image_size // patch_size) ** 2 + 1 UpperCamelCase__ :Dict = self.text_seq_length + self.image_seq_length def __a ( self :Tuple ): UpperCamelCase__ :Optional[int] = ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size ) UpperCamelCase__ :int = ids_tensor([self.batch_size, self.text_seq_length, 4] , self.range_bbox ) UpperCamelCase__ :str = bbox.numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: UpperCamelCase__ :List[str] = bbox[i, j, 3] UpperCamelCase__ :Optional[int] = bbox[i, j, 1] UpperCamelCase__ :Optional[Any] = tmp_coordinate if bbox[i, j, 2] < bbox[i, j, 0]: UpperCamelCase__ :Tuple = bbox[i, j, 2] UpperCamelCase__ :Optional[Any] = bbox[i, j, 0] UpperCamelCase__ :List[str] = tmp_coordinate UpperCamelCase__ :Dict = tf.constant(lowerCamelCase__ ) UpperCamelCase__ :Union[str, Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCamelCase__ :Any = None if self.use_input_mask: UpperCamelCase__ :int = random_attention_mask([self.batch_size, self.text_seq_length] ) UpperCamelCase__ :Optional[Any] = None if self.use_token_type_ids: UpperCamelCase__ :Optional[int] = ids_tensor([self.batch_size, self.text_seq_length] , self.type_vocab_size ) UpperCamelCase__ :List[str] = None UpperCamelCase__ :Union[str, Any] = None if self.use_labels: UpperCamelCase__ :Union[str, Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase__ :Union[str, Any] = ids_tensor([self.batch_size, self.text_seq_length] , self.num_labels ) UpperCamelCase__ :Optional[int] = LayoutLMvaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , coordinate_size=self.coordinate_size , shape_size=self.shape_size , input_size=self.image_size , patch_size=self.patch_size , ) return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels def __a ( self :List[Any] , lowerCamelCase__ :str , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Dict , lowerCamelCase__ :str , lowerCamelCase__ :int , lowerCamelCase__ :Any ): UpperCamelCase__ :Dict = TFLayoutLMvaModel(config=lowerCamelCase__ ) # text + image UpperCamelCase__ :Tuple = model(lowerCamelCase__ , pixel_values=lowerCamelCase__ , training=lowerCamelCase__ ) UpperCamelCase__ :Tuple = model( lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , training=lowerCamelCase__ , ) UpperCamelCase__ :str = model(lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , training=lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # text only UpperCamelCase__ :Optional[int] = model(lowerCamelCase__ , training=lowerCamelCase__ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size) ) # image only UpperCamelCase__ :Tuple = model({"""pixel_values""": pixel_values} , training=lowerCamelCase__ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size) ) def __a ( self :Dict , lowerCamelCase__ :str , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Dict , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :str , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :str ): UpperCamelCase__ :Optional[Any] = self.num_labels UpperCamelCase__ :List[Any] = TFLayoutLMvaForSequenceClassification(config=lowerCamelCase__ ) UpperCamelCase__ :List[str] = model( lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , labels=lowerCamelCase__ , training=lowerCamelCase__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __a ( self :List[str] , lowerCamelCase__ :List[str] , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Dict , lowerCamelCase__ :List[Any] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Tuple , lowerCamelCase__ :List[str] ): UpperCamelCase__ :Union[str, Any] = self.num_labels UpperCamelCase__ :Dict = TFLayoutLMvaForTokenClassification(config=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = model( lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , labels=lowerCamelCase__ , training=lowerCamelCase__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels) ) def __a ( self :int , lowerCamelCase__ :Dict , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Any , lowerCamelCase__ :Dict , lowerCamelCase__ :Tuple , lowerCamelCase__ :Tuple ): UpperCamelCase__ :Dict = 2 UpperCamelCase__ :Tuple = TFLayoutLMvaForQuestionAnswering(config=lowerCamelCase__ ) UpperCamelCase__ :int = model( lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , start_positions=lowerCamelCase__ , end_positions=lowerCamelCase__ , training=lowerCamelCase__ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __a ( self :List[Any] ): UpperCamelCase__ :Union[str, Any] = self.prepare_config_and_inputs() ((UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__)) :Any = config_and_inputs UpperCamelCase__ :List[str] = { """input_ids""": input_ids, """bbox""": bbox, """pixel_values""": pixel_values, """token_type_ids""": token_type_ids, """attention_mask""": input_mask, } return config, inputs_dict @require_tf class lowerCAmelCase_ ( lowercase , lowercase , unittest.TestCase ): """simple docstring""" _snake_case : Dict = ( ( TFLayoutLMvaModel, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, ) if is_tf_available() else () ) _snake_case : Dict = ( {"""document-question-answering""": TFLayoutLMvaForQuestionAnswering, """feature-extraction""": TFLayoutLMvaModel} if is_tf_available() else {} ) _snake_case : Optional[int] = False _snake_case : List[str] = False _snake_case : Tuple = False def __a ( self :str , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Tuple , lowerCamelCase__ :int ): return True def __a ( self :Optional[int] , lowerCamelCase__ :int , lowerCamelCase__ :List[str] , lowerCamelCase__ :Optional[int]=False ): UpperCamelCase__ :List[str] = copy.deepcopy(lowerCamelCase__ ) if model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :Optional[int] = { k: tf.tile(tf.expand_dims(lowerCamelCase__ , 1 ) , (1, self.model_tester.num_choices) + (1,) * (v.ndim - 1) ) if isinstance(lowerCamelCase__ , tf.Tensor ) and v.ndim > 0 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :str = tf.ones(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :List[str] = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) UpperCamelCase__ :Union[str, Any] = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :Optional[Any] = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :Tuple = tf.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=tf.intaa ) return inputs_dict def __a ( self :Dict ): UpperCamelCase__ :List[Any] = TFLayoutLMvaModelTester(self ) UpperCamelCase__ :Optional[int] = ConfigTester(self , config_class=lowerCamelCase__ , hidden_size=37 ) def __a ( self :Any ): self.config_tester.run_common_tests() def __a ( self :Optional[int] ): UpperCamelCase__ , UpperCamelCase__ :Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase__ :Optional[int] = model_class(lowerCamelCase__ ) if getattr(lowerCamelCase__ , """hf_compute_loss""" , lowerCamelCase__ ): # The number of elements in the loss should be the same as the number of elements in the label UpperCamelCase__ :Union[str, Any] = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) UpperCamelCase__ :int = prepared_for_class[ sorted(prepared_for_class.keys() - inputs_dict.keys() , reverse=lowerCamelCase__ )[0] ] UpperCamelCase__ :Union[str, Any] = added_label.shape.as_list()[:1] # Test that model correctly compute the loss with kwargs UpperCamelCase__ :List[Any] = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = prepared_for_class.pop("""input_ids""" ) UpperCamelCase__ :List[str] = model(lowerCamelCase__ , **lowerCamelCase__ )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss when we mask some positions UpperCamelCase__ :Union[str, Any] = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = prepared_for_class.pop("""input_ids""" ) if "labels" in prepared_for_class: UpperCamelCase__ :List[str] = prepared_for_class["""labels"""].numpy() if len(labels.shape ) > 1 and labels.shape[1] != 1: UpperCamelCase__ :Optional[Any] = -1_00 UpperCamelCase__ :Union[str, Any] = tf.convert_to_tensor(lowerCamelCase__ ) UpperCamelCase__ :Tuple = model(lowerCamelCase__ , **lowerCamelCase__ )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) self.assertTrue(not np.any(np.isnan(loss.numpy() ) ) ) # Test that model correctly compute the loss with a dict UpperCamelCase__ :Optional[Any] = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) UpperCamelCase__ :Union[str, Any] = model(lowerCamelCase__ )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss with a tuple UpperCamelCase__ :Dict = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) # Get keys that were added with the _prepare_for_class function UpperCamelCase__ :str = prepared_for_class.keys() - inputs_dict.keys() UpperCamelCase__ :Tuple = inspect.signature(model.call ).parameters UpperCamelCase__ :str = list(signature.keys() ) # Create a dictionary holding the location of the tensors in the tuple UpperCamelCase__ :Any = {0: """input_ids"""} for label_key in label_keys: UpperCamelCase__ :Dict = signature_names.index(lowerCamelCase__ ) UpperCamelCase__ :Optional[int] = label_key UpperCamelCase__ :Optional[Any] = sorted(tuple_index_mapping.items() ) # Initialize a list with their default values, update the values and convert to a tuple UpperCamelCase__ :Any = [] for name in signature_names: if name != "kwargs": list_input.append(signature[name].default ) for index, value in sorted_tuple_index_mapping: UpperCamelCase__ :List[str] = prepared_for_class[value] UpperCamelCase__ :Union[str, Any] = tuple(lowerCamelCase__ ) # Send to model UpperCamelCase__ :str = model(tuple_input[:-1] )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) def __a ( self :Optional[int] ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Any ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :List[Any] = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: UpperCamelCase__ :Dict = type self.model_tester.create_and_check_model(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Tuple ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Optional[int] ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :List[str] ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) @slow def __a ( self :Optional[int] ): for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase__ :Dict = TFLayoutLMvaModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) def A ( ) -> List[str]: UpperCamelCase__ :List[Any] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_tf class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" @cached_property def __a ( self :Optional[Any] ): return LayoutLMvaImageProcessor(apply_ocr=lowerCamelCase__ ) if is_vision_available() else None @slow def __a ( self :Dict ): UpperCamelCase__ :List[str] = TFLayoutLMvaModel.from_pretrained("""microsoft/layoutlmv3-base""" ) UpperCamelCase__ :List[Any] = self.default_image_processor UpperCamelCase__ :str = prepare_img() UpperCamelCase__ :Any = image_processor(images=lowerCamelCase__ , return_tensors="""tf""" ).pixel_values UpperCamelCase__ :str = tf.constant([[1, 2]] ) UpperCamelCase__ :Any = tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]] ) , axis=0 ) # forward pass UpperCamelCase__ :Dict = model(input_ids=lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , training=lowerCamelCase__ ) # verify the logits UpperCamelCase__ :int = (1, 1_99, 7_68) self.assertEqual(outputs.last_hidden_state.shape , lowerCamelCase__ ) UpperCamelCase__ :List[Any] = tf.constant( [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]] ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] , lowerCamelCase__ , atol=1e-4 ) )
45
0
"""simple docstring""" from PIL import Image def __lowerCAmelCase ( __UpperCamelCase : Image , __UpperCamelCase : float ): '''simple docstring''' def brightness(__UpperCamelCase : int ) -> float: return 1_2_8 + level + (c - 1_2_8) if not -255.0 <= level <= 255.0: raise ValueError("""level must be between -255.0 (black) and 255.0 (white)""" ) return img.point(__UpperCamelCase ) if __name__ == "__main__": # Load image with Image.open('''image_data/lena.jpg''') as img: # Change brightness to 100 __lowerCAmelCase : Tuple = change_brightness(img, 100) brigt_img.save('''image_data/lena_brightness.png''', format='''png''')
58
import logging import os import sys from dataclasses import dataclass, field from typing import Optional import torch from datasets import load_dataset from torchvision.transforms import Compose, Lambda, Normalize, RandomHorizontalFlip, RandomResizedCrop, ToTensor from torchvision.transforms.functional import InterpolationMode import transformers from transformers import ( HfArgumentParser, Trainer, TrainingArguments, ViTImageProcessor, ViTMAEConfig, ViTMAEForPreTraining, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version UpperCamelCase = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.31.0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/image-pretraining/requirements.txt") @dataclass class lowerCAmelCase_ : """simple docstring""" _snake_case : Optional[str] = field( default="""cifar10""" , metadata={"""help""": """Name of a dataset from the datasets package"""} ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """The column name of the images in the files."""} ) _snake_case : Optional[str] = field(default=lowercase , metadata={"""help""": """A folder containing the training data."""} ) _snake_case : Optional[str] = field(default=lowercase , metadata={"""help""": """A folder containing the validation data."""} ) _snake_case : Optional[float] = field( default=0.15 , metadata={"""help""": """Percent to split off of train for validation."""} ) _snake_case : Optional[int] = field( default=lowercase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } , ) _snake_case : Optional[int] = field( default=lowercase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of evaluation examples to this """ """value if set.""" ) } , ) def __a ( self :List[str] ): UpperCamelCase__ :Optional[Any] = {} if self.train_dir is not None: UpperCamelCase__ :int = self.train_dir if self.validation_dir is not None: UpperCamelCase__ :List[str] = self.validation_dir UpperCamelCase__ :Optional[int] = data_files if data_files else None @dataclass class lowerCAmelCase_ : """simple docstring""" _snake_case : str = field( default=lowercase , metadata={ """help""": ( """The model checkpoint for weights initialization.Don't set if you want to train a model from scratch.""" ) } , ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """Pretrained config name or path if not the same as model_name_or_path"""} ) _snake_case : Optional[str] = field( default=lowercase , metadata={ """help""": ( """Override some existing default config settings when a model is trained from scratch. Example: """ """n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index""" ) } , ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """Where do you want to store the pretrained models downloaded from s3"""} ) _snake_case : str = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) _snake_case : str = field(default=lowercase , metadata={"""help""": """Name or path of preprocessor config."""} ) _snake_case : bool = field( default=lowercase , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) _snake_case : float = field( default=0.75 , metadata={"""help""": """The ratio of the number of masked tokens in the input sequence."""} ) _snake_case : bool = field( default=lowercase , metadata={"""help""": """Whether or not to train with normalized pixel values as target."""} ) @dataclass class lowerCAmelCase_ ( lowercase ): """simple docstring""" _snake_case : float = field( default=1e-3 , metadata={"""help""": """Base learning rate: absolute_lr = base_lr * total_batch_size / 256."""} ) def A ( lowercase__ : Union[str, Any] ) -> Dict: UpperCamelCase__ :Union[str, Any] = torch.stack([example["""pixel_values"""] for example in examples] ) return {"pixel_values": pixel_values} def A ( ) -> Optional[int]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. UpperCamelCase__ :Optional[int] = HfArgumentParser((ModelArguments, DataTrainingArguments, CustomTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(""".json""" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :str = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Union[str, Any] = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry("""run_mae""" , lowercase__ , lowercase__ ) # Setup logging logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() UpperCamelCase__ :List[str] = training_args.get_process_log_level() logger.setLevel(lowercase__ ) transformers.utils.logging.set_verbosity(lowercase__ ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( f"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}""" + f"""distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}""" ) logger.info(f"""Training/evaluation parameters {training_args}""" ) # Detecting last checkpoint. UpperCamelCase__ :Union[str, Any] = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: UpperCamelCase__ :List[str] = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( f"""Output directory ({training_args.output_dir}) already exists and is not empty. """ """Use --overwrite_output_dir to overcome.""" ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( f"""Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change """ """the `--output_dir` or add `--overwrite_output_dir` to train from scratch.""" ) # Initialize our dataset. UpperCamelCase__ :Tuple = load_dataset( data_args.dataset_name , data_args.dataset_config_name , data_files=data_args.data_files , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) # If we don't have a validation split, split off a percentage of train as validation. UpperCamelCase__ :int = None if """validation""" in ds.keys() else data_args.train_val_split if isinstance(data_args.train_val_split , lowercase__ ) and data_args.train_val_split > 0.0: UpperCamelCase__ :Optional[Any] = ds["""train"""].train_test_split(data_args.train_val_split ) UpperCamelCase__ :Union[str, Any] = split["""train"""] UpperCamelCase__ :Any = split["""test"""] # Load pretrained model and image processor # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. UpperCamelCase__ :Optional[int] = { """cache_dir""": model_args.cache_dir, """revision""": model_args.model_revision, """use_auth_token""": True if model_args.use_auth_token else None, } if model_args.config_name: UpperCamelCase__ :Any = ViTMAEConfig.from_pretrained(model_args.config_name , **lowercase__ ) elif model_args.model_name_or_path: UpperCamelCase__ :Union[str, Any] = ViTMAEConfig.from_pretrained(model_args.model_name_or_path , **lowercase__ ) else: UpperCamelCase__ :Optional[Any] = ViTMAEConfig() logger.warning("""You are instantiating a new config instance from scratch.""" ) if model_args.config_overrides is not None: logger.info(f"""Overriding config: {model_args.config_overrides}""" ) config.update_from_string(model_args.config_overrides ) logger.info(f"""New config: {config}""" ) # adapt config config.update( { """mask_ratio""": model_args.mask_ratio, """norm_pix_loss""": model_args.norm_pix_loss, } ) # create image processor if model_args.image_processor_name: UpperCamelCase__ :str = ViTImageProcessor.from_pretrained(model_args.image_processor_name , **lowercase__ ) elif model_args.model_name_or_path: UpperCamelCase__ :Dict = ViTImageProcessor.from_pretrained(model_args.model_name_or_path , **lowercase__ ) else: UpperCamelCase__ :Tuple = ViTImageProcessor() # create model if model_args.model_name_or_path: UpperCamelCase__ :Any = ViTMAEForPreTraining.from_pretrained( model_args.model_name_or_path , from_tf=bool(""".ckpt""" in model_args.model_name_or_path ) , config=lowercase__ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info("""Training new model from scratch""" ) UpperCamelCase__ :Optional[int] = ViTMAEForPreTraining(lowercase__ ) if training_args.do_train: UpperCamelCase__ :Optional[Any] = ds["""train"""].column_names else: UpperCamelCase__ :Union[str, Any] = ds["""validation"""].column_names if data_args.image_column_name is not None: UpperCamelCase__ :Union[str, Any] = data_args.image_column_name elif "image" in column_names: UpperCamelCase__ :Optional[Any] = """image""" elif "img" in column_names: UpperCamelCase__ :List[str] = """img""" else: UpperCamelCase__ :List[Any] = column_names[0] # transformations as done in original MAE paper # source: https://github.com/facebookresearch/mae/blob/main/main_pretrain.py if "shortest_edge" in image_processor.size: UpperCamelCase__ :List[str] = image_processor.size["""shortest_edge"""] else: UpperCamelCase__ :int = (image_processor.size["""height"""], image_processor.size["""width"""]) UpperCamelCase__ :Any = Compose( [ Lambda(lambda lowercase__ : img.convert("""RGB""" ) if img.mode != "RGB" else img ), RandomResizedCrop(lowercase__ , scale=(0.2, 1.0) , interpolation=InterpolationMode.BICUBIC ), RandomHorizontalFlip(), ToTensor(), Normalize(mean=image_processor.image_mean , std=image_processor.image_std ), ] ) def preprocess_images(lowercase__ : Tuple ): UpperCamelCase__ :List[Any] = [transforms(lowercase__ ) for image in examples[image_column_name]] return examples if training_args.do_train: if "train" not in ds: raise ValueError("""--do_train requires a train dataset""" ) if data_args.max_train_samples is not None: UpperCamelCase__ :Optional[int] = ds["""train"""].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) # Set the training transforms ds["train"].set_transform(lowercase__ ) if training_args.do_eval: if "validation" not in ds: raise ValueError("""--do_eval requires a validation dataset""" ) if data_args.max_eval_samples is not None: UpperCamelCase__ :Optional[Any] = ( ds["""validation"""].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms ds["validation"].set_transform(lowercase__ ) # Compute absolute learning rate UpperCamelCase__ :Tuple = ( training_args.train_batch_size * training_args.gradient_accumulation_steps * training_args.world_size ) if training_args.base_learning_rate is not None: UpperCamelCase__ :Any = training_args.base_learning_rate * total_train_batch_size / 256 # Initialize our trainer UpperCamelCase__ :Union[str, Any] = Trainer( model=lowercase__ , args=lowercase__ , train_dataset=ds["""train"""] if training_args.do_train else None , eval_dataset=ds["""validation"""] if training_args.do_eval else None , tokenizer=lowercase__ , data_collator=lowercase__ , ) # Training if training_args.do_train: UpperCamelCase__ :Any = None if training_args.resume_from_checkpoint is not None: UpperCamelCase__ :int = training_args.resume_from_checkpoint elif last_checkpoint is not None: UpperCamelCase__ :Dict = last_checkpoint UpperCamelCase__ :Union[str, Any] = trainer.train(resume_from_checkpoint=lowercase__ ) trainer.save_model() trainer.log_metrics("""train""" , train_result.metrics ) trainer.save_metrics("""train""" , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: UpperCamelCase__ :int = trainer.evaluate() trainer.log_metrics("""eval""" , lowercase__ ) trainer.save_metrics("""eval""" , lowercase__ ) # Write model card and (optionally) push to hub UpperCamelCase__ :Optional[int] = { """tasks""": """masked-auto-encoding""", """dataset""": data_args.dataset_name, """tags""": ["""masked-auto-encoding"""], } if training_args.push_to_hub: trainer.push_to_hub(**lowercase__ ) else: trainer.create_model_card(**lowercase__ ) def A ( lowercase__ : Union[str, Any] ) -> Dict: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
45
0
import os import shutil import tempfile import unittest import numpy as np from transformers import AutoTokenizer, BarkProcessor from transformers.testing_utils import require_torch, slow @require_torch class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' def SCREAMING_SNAKE_CASE_ (self : List[str]) ->str: '''simple docstring''' lowerCamelCase__: Union[str, Any] ="ylacombe/bark-small" lowerCamelCase__: Tuple =tempfile.mkdtemp() lowerCamelCase__: Tuple ="en_speaker_1" lowerCamelCase__: Optional[int] ="This is a test string" lowerCamelCase__: List[str] ="speaker_embeddings_path.json" lowerCamelCase__: int ="speaker_embeddings" def SCREAMING_SNAKE_CASE_ (self : Optional[Any] , **UpperCAmelCase_ : Any) ->Tuple: '''simple docstring''' return AutoTokenizer.from_pretrained(self.checkpoint , **UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ (self : List[str]) ->Union[str, Any]: '''simple docstring''' shutil.rmtree(self.tmpdirname) def SCREAMING_SNAKE_CASE_ (self : int) ->Any: '''simple docstring''' lowerCamelCase__: List[Any] =self.get_tokenizer() lowerCamelCase__: List[str] =BarkProcessor(tokenizer=UpperCAmelCase_) processor.save_pretrained(self.tmpdirname) lowerCamelCase__: Dict =BarkProcessor.from_pretrained(self.tmpdirname) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab()) @slow def SCREAMING_SNAKE_CASE_ (self : Tuple) ->Tuple: '''simple docstring''' lowerCamelCase__: Tuple =BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) processor.save_pretrained( self.tmpdirname , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , speaker_embeddings_directory=self.speaker_embeddings_directory , ) lowerCamelCase__: Dict =self.get_tokenizer(bos_token="(BOS)" , eos_token="(EOS)") lowerCamelCase__: Any =BarkProcessor.from_pretrained( self.tmpdirname , self.speaker_embeddings_dict_path , bos_token="(BOS)" , eos_token="(EOS)" , ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab()) def SCREAMING_SNAKE_CASE_ (self : List[Any]) ->int: '''simple docstring''' lowerCamelCase__: Any =BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) lowerCamelCase__: List[str] =35 lowerCamelCase__: Optional[Any] =2 lowerCamelCase__: Optional[Any] =8 lowerCamelCase__: Optional[int] ={ "semantic_prompt": np.ones(UpperCAmelCase_), "coarse_prompt": np.ones((nb_codebooks_coarse, seq_len)), "fine_prompt": np.ones((nb_codebooks_total, seq_len)), } # test providing already loaded voice_preset lowerCamelCase__: Any =processor(text=self.input_string , voice_preset=UpperCAmelCase_) lowerCamelCase__: int =inputs["history_prompt"] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(UpperCAmelCase_ , np.array([])).tolist()) # test loading voice preset from npz file lowerCamelCase__: Union[str, Any] =os.path.join(self.tmpdirname , "file.npz") np.savez(UpperCAmelCase_ , **UpperCAmelCase_) lowerCamelCase__: Tuple =processor(text=self.input_string , voice_preset=UpperCAmelCase_) lowerCamelCase__: Optional[Any] =inputs["history_prompt"] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(UpperCAmelCase_ , np.array([])).tolist()) # test loading voice preset from the hub lowerCamelCase__: Any =processor(text=self.input_string , voice_preset=self.voice_preset) def SCREAMING_SNAKE_CASE_ (self : Tuple) ->Union[str, Any]: '''simple docstring''' lowerCamelCase__: str =self.get_tokenizer() lowerCamelCase__: Dict =BarkProcessor(tokenizer=UpperCAmelCase_) lowerCamelCase__: List[Any] =processor(text=self.input_string) lowerCamelCase__: Optional[int] =tokenizer( self.input_string , padding="max_length" , max_length=256 , add_special_tokens=UpperCAmelCase_ , return_attention_mask=UpperCAmelCase_ , return_token_type_ids=UpperCAmelCase_ , ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key].squeeze().tolist())
59
from __future__ import annotations def A ( lowercase__ : int ) -> list[int]: UpperCamelCase__ :Union[str, Any] = [True] * limit UpperCamelCase__ :int = False UpperCamelCase__ :Optional[Any] = False UpperCamelCase__ :str = True for i in range(3 , int(limit**0.5 + 1 ) , 2 ): UpperCamelCase__ :List[Any] = i * 2 while index < limit: UpperCamelCase__ :Tuple = False UpperCamelCase__ :Tuple = index + i UpperCamelCase__ :str = [2] for i in range(3 , lowercase__ , 2 ): if is_prime[i]: primes.append(lowercase__ ) return primes def A ( lowercase__ : int = 100_0000 ) -> int: UpperCamelCase__ :Any = prime_sieve(lowercase__ ) UpperCamelCase__ :Optional[int] = 0 UpperCamelCase__ :Optional[Any] = 0 for i in range(len(lowercase__ ) ): for j in range(i + length , len(lowercase__ ) ): UpperCamelCase__ :Any = sum(primes[i:j] ) if sol >= ceiling: break if sol in primes: UpperCamelCase__ :Union[str, Any] = j - i UpperCamelCase__ :Any = sol return largest if __name__ == "__main__": print(f'''{solution() = }''')
45
0
import unittest import numpy as np import torch from .utils_summarization import build_mask, compute_token_type_ids, process_story, truncate_or_pad class __lowerCAmelCase ( unittest.TestCase ): def lowerCamelCase (self ) -> List[Any]: '''simple docstring''' snake_case_ : Union[str, Any] = 10 def lowerCamelCase (self ) -> Tuple: '''simple docstring''' snake_case_ : Optional[int] = [1, 2, 3, 4] snake_case_ : Optional[Any] = [1, 2, 3, 4, 0, 0, 0, 0, 0, 0] self.assertEqual(truncate_or_pad(__magic_name__ , self.block_size , 0 ) , __magic_name__ ) def lowerCamelCase (self ) -> str: '''simple docstring''' snake_case_ : Optional[int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] snake_case_ : Any = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] self.assertEqual(truncate_or_pad(__magic_name__ , self.block_size , 0 ) , __magic_name__ ) def lowerCamelCase (self ) -> Optional[int]: '''simple docstring''' snake_case_ : Optional[Any] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] snake_case_ : List[Any] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] self.assertEqual(truncate_or_pad(__magic_name__ , self.block_size , 0 ) , __magic_name__ ) def lowerCamelCase (self ) -> Optional[int]: '''simple docstring''' snake_case_ : Any = '''It was the year of Our Lord one thousand seven hundred and seventy-five.\n\nSpiritual revelations were conceded to England at that favoured period, as at this.''' snake_case_ , snake_case_ : Optional[int] = process_story(__magic_name__ ) self.assertEqual(__magic_name__ , [] ) def lowerCamelCase (self ) -> Tuple: '''simple docstring''' snake_case_ : int = '''''' snake_case_ , snake_case_ : List[str] = process_story(__magic_name__ ) self.assertEqual(__magic_name__ , [] ) self.assertEqual(__magic_name__ , [] ) def lowerCamelCase (self ) -> str: '''simple docstring''' snake_case_ : Tuple = ( '''It was the year of Our Lord one thousand seven hundred and ''' '''seventy-five\n\nSpiritual revelations were conceded to England ''' '''at that favoured period, as at this.\n@highlight\n\nIt was the best of times''' ) snake_case_ , snake_case_ : List[Any] = process_story(__magic_name__ ) snake_case_ : Any = [ '''It was the year of Our Lord one thousand seven hundred and seventy-five.''', '''Spiritual revelations were conceded to England at that favoured period, as at this.''', ] self.assertEqual(__magic_name__ , __magic_name__ ) snake_case_ : int = ['''It was the best of times.'''] self.assertEqual(__magic_name__ , __magic_name__ ) def lowerCamelCase (self ) -> int: '''simple docstring''' snake_case_ : List[str] = torch.tensor([1, 2, 3, 4] ) snake_case_ : Any = torch.tensor([1, 1, 1, 1] ) np.testing.assert_array_equal(build_mask(__magic_name__ , 0 ).numpy() , expected.numpy() ) def lowerCamelCase (self ) -> int: '''simple docstring''' snake_case_ : Any = torch.tensor([1, 2, 3, 4, 23, 23, 23] ) snake_case_ : Dict = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(__magic_name__ , 23 ).numpy() , expected.numpy() ) def lowerCamelCase (self ) -> str: '''simple docstring''' snake_case_ : Tuple = torch.tensor([8, 2, 3, 4, 1, 1, 1] ) snake_case_ : Optional[Any] = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(__magic_name__ , 1 ).numpy() , expected.numpy() ) def lowerCamelCase (self ) -> List[Any]: '''simple docstring''' snake_case_ : List[str] = 101 snake_case_ : int = torch.tensor([[1, 2, 3, 4, 5, 6], [1, 2, 3, 101, 5, 6], [1, 101, 3, 4, 101, 6]] ) snake_case_ : Optional[Any] = torch.tensor([[1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 0, 0], [1, 0, 0, 0, 1, 1]] ) snake_case_ : List[str] = compute_token_type_ids(__magic_name__ , __magic_name__ ) np.testing.assert_array_equal(__magic_name__ , __magic_name__ )
60
import unittest from transformers import GPTNeoXJapaneseConfig, is_torch_available from transformers.models.gpt_neox_japanese.tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer from transformers.testing_utils import 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 GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseModel class lowerCAmelCase_ : """simple docstring""" def __init__( self :Optional[Any] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Tuple=13 , lowerCamelCase__ :Tuple=7 , lowerCamelCase__ :Optional[Any]=True , lowerCamelCase__ :Union[str, Any]=True , lowerCamelCase__ :Optional[int]=True , lowerCamelCase__ :List[Any]=True , lowerCamelCase__ :List[str]=99 , lowerCamelCase__ :int=32 , lowerCamelCase__ :List[Any]=5 , lowerCamelCase__ :Tuple=4 , lowerCamelCase__ :List[Any]=4 , lowerCamelCase__ :str="gelu" , lowerCamelCase__ :Optional[Any]=0.0 , lowerCamelCase__ :Optional[int]=0.1 , lowerCamelCase__ :str=True , lowerCamelCase__ :Dict=5_12 , lowerCamelCase__ :Optional[Any]=16 , lowerCamelCase__ :Optional[Any]=2 , lowerCamelCase__ :Union[str, Any]=0.02 , lowerCamelCase__ :Union[str, Any]=3 , lowerCamelCase__ :int=4 , lowerCamelCase__ :str=None , ): UpperCamelCase__ :Optional[Any] = parent UpperCamelCase__ :Dict = batch_size UpperCamelCase__ :Tuple = seq_length UpperCamelCase__ :Dict = is_training UpperCamelCase__ :List[str] = use_input_mask UpperCamelCase__ :Optional[Any] = use_token_type_ids UpperCamelCase__ :Tuple = use_labels UpperCamelCase__ :int = vocab_size UpperCamelCase__ :Tuple = hidden_size UpperCamelCase__ :Optional[Any] = num_hidden_layers UpperCamelCase__ :int = num_attention_heads UpperCamelCase__ :Optional[int] = intermediate_multiple_size UpperCamelCase__ :Optional[Any] = hidden_act UpperCamelCase__ :Optional[int] = hidden_dropout UpperCamelCase__ :List[Any] = attention_dropout UpperCamelCase__ :List[str] = weight_tying UpperCamelCase__ :List[str] = max_position_embeddings UpperCamelCase__ :Dict = type_vocab_size UpperCamelCase__ :List[Any] = type_sequence_label_size UpperCamelCase__ :List[str] = initializer_range UpperCamelCase__ :int = num_labels UpperCamelCase__ :Dict = num_choices UpperCamelCase__ :Any = scope def __a ( self :Any ): UpperCamelCase__ :Tuple = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCamelCase__ :str = None if self.use_input_mask: UpperCamelCase__ :Optional[int] = random_attention_mask([self.batch_size, self.seq_length] ) UpperCamelCase__ :Union[str, Any] = None if self.use_labels: UpperCamelCase__ :Tuple = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCamelCase__ :Optional[Any] = self.get_config() return config, input_ids, input_mask, token_labels def __a ( self :Union[str, Any] ): return GPTNeoXJapaneseConfig( 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_multiple_size=self.intermediate_multiple_size , hidden_act=self.hidden_act , hidden_dropout=self.hidden_dropout , attention_dropout=self.attention_dropout , weight_tying=self.weight_tying , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=lowerCamelCase__ , initializer_range=self.initializer_range , ) def __a ( self :Union[str, Any] ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Optional[Any] = self.prepare_config_and_inputs() UpperCamelCase__ :Optional[int] = True return config, input_ids, input_mask, token_labels def __a ( self :List[str] , lowerCamelCase__ :List[Any] , lowerCamelCase__ :Any , lowerCamelCase__ :Any ): UpperCamelCase__ :Union[str, Any] = GPTNeoXJapaneseModel(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() UpperCamelCase__ :Tuple = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = model(lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __a ( self :Dict , lowerCamelCase__ :Dict , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :List[Any] ): UpperCamelCase__ :List[str] = True UpperCamelCase__ :int = GPTNeoXJapaneseModel(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() UpperCamelCase__ :Union[str, Any] = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __a ( self :List[Any] , lowerCamelCase__ :Tuple , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Any , lowerCamelCase__ :Optional[Any] ): UpperCamelCase__ :Any = GPTNeoXJapaneseForCausalLM(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() UpperCamelCase__ :Tuple = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ , labels=lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __a ( self :Any , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :List[str] ): UpperCamelCase__ :Union[str, Any] = True UpperCamelCase__ :List[str] = GPTNeoXJapaneseForCausalLM(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() # first forward pass UpperCamelCase__ :Optional[Any] = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ , use_cache=lowerCamelCase__ ) UpperCamelCase__ :List[Any] = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids UpperCamelCase__ :List[Any] = ids_tensor((self.batch_size, 3) , config.vocab_size ) UpperCamelCase__ :Optional[Any] = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and UpperCamelCase__ :Union[str, Any] = torch.cat([input_ids, next_tokens] , dim=-1 ) UpperCamelCase__ :Optional[int] = torch.cat([input_mask, next_mask] , dim=-1 ) UpperCamelCase__ :Union[str, Any] = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ , output_hidden_states=lowerCamelCase__ ) UpperCamelCase__ :Optional[int] = output_from_no_past["""hidden_states"""][0] UpperCamelCase__ :Union[str, Any] = model( lowerCamelCase__ , attention_mask=lowerCamelCase__ , past_key_values=lowerCamelCase__ , output_hidden_states=lowerCamelCase__ , )["""hidden_states"""][0] # select random slice UpperCamelCase__ :int = ids_tensor((1,) , output_from_past.shape[-1] ).item() UpperCamelCase__ :str = output_from_no_past[:, -3:, random_slice_idx].detach() UpperCamelCase__ :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(lowerCamelCase__ , lowerCamelCase__ , atol=1e-3 ) ) def __a ( self :Tuple ): UpperCamelCase__ :int = self.prepare_config_and_inputs() UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :List[Any] = config_and_inputs UpperCamelCase__ :Any = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_torch class lowerCAmelCase_ ( lowercase , lowercase , unittest.TestCase ): """simple docstring""" _snake_case : Dict = (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else () _snake_case : int = (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else () _snake_case : str = ( {"""feature-extraction""": GPTNeoXJapaneseModel, """text-generation""": GPTNeoXJapaneseForCausalLM} if is_torch_available() else {} ) _snake_case : Union[str, Any] = False _snake_case : Dict = False _snake_case : List[str] = False _snake_case : Optional[int] = False def __a ( self :List[Any] ): UpperCamelCase__ :Tuple = GPTNeoXJapaneseModelTester(self ) UpperCamelCase__ :Optional[Any] = ConfigTester(self , config_class=lowerCamelCase__ , hidden_size=37 ) def __a ( self :Dict ): self.config_tester.run_common_tests() def __a ( self :Any ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Any ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Union[str, Any] ): # This regression test was failing with PyTorch < 1.3 UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :List[str] = self.model_tester.prepare_config_and_inputs_for_decoder() UpperCamelCase__ :Dict = None self.model_tester.create_and_check_model_as_decoder(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :List[str] ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Union[str, Any] ): UpperCamelCase__ :int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_causal_lm(*lowerCamelCase__ ) @slow def __a ( self :int ): UpperCamelCase__ :int = """abeja/gpt-neox-japanese-2.7b""" UpperCamelCase__ :List[Any] = ["""データサイエンティストとは、""", """100年後に必要とされる会社は、""", """フルリモートの環境で働くために必要なことは、""", """国境の長いトンネルを抜けると""", """美味しい日本食といえば、"""] UpperCamelCase__ :Union[str, Any] = [ """データサイエンティストとは、データを分析し、ビジネスに役立つ知見を導き出す専門家のことです。""", """100年後に必要とされる会社は、「人」が中心の会社です。""", """フルリモートの環境で働くために必要なことは、「自分の時間をコントロールする」ことです。""", """国境の長いトンネルを抜けると、そこは雪国だった。""", """美味しい日本食といえば、やっぱりお寿司ですよね。""", ] UpperCamelCase__ :Any = GPTNeoXJapaneseTokenizer.from_pretrained(lowerCamelCase__ ) UpperCamelCase__ :List[Any] = GPTNeoXJapaneseForCausalLM.from_pretrained(lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = [] for prompt in prompts: UpperCamelCase__ :str = tokenizer(lowerCamelCase__ , return_tensors="""pt""" ).input_ids UpperCamelCase__ :Union[str, Any] = model.generate(lowerCamelCase__ , max_length=50 ) UpperCamelCase__ :Dict = tokenizer.batch_decode(lowerCamelCase__ , skip_special_tokens=lowerCamelCase__ ) predicted_outputs += generated_string self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ )
45
0
class __lowerCamelCase : """simple docstring""" def __init__( self : int , SCREAMING_SNAKE_CASE__ : list ) -> None: lowerCAmelCase__ = set_counts lowerCAmelCase__ = max(SCREAMING_SNAKE_CASE__ ) lowerCAmelCase__ = len(SCREAMING_SNAKE_CASE__ ) lowerCAmelCase__ = [1] * num_sets lowerCAmelCase__ = list(range(SCREAMING_SNAKE_CASE__ ) ) def a ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : int ) -> bool: lowerCAmelCase__ = self.get_parent(SCREAMING_SNAKE_CASE__ ) lowerCAmelCase__ = self.get_parent(SCREAMING_SNAKE_CASE__ ) if src_parent == dst_parent: return False if self.ranks[dst_parent] >= self.ranks[src_parent]: self.set_counts[dst_parent] += self.set_counts[src_parent] lowerCAmelCase__ = 0 lowerCAmelCase__ = dst_parent if self.ranks[dst_parent] == self.ranks[src_parent]: self.ranks[dst_parent] += 1 lowerCAmelCase__ = self.set_counts[dst_parent] else: self.set_counts[src_parent] += self.set_counts[dst_parent] lowerCAmelCase__ = 0 lowerCAmelCase__ = src_parent lowerCAmelCase__ = self.set_counts[src_parent] lowerCAmelCase__ = max(self.max_set , SCREAMING_SNAKE_CASE__ ) return True def a ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : int ) -> int: if self.parents[disj_set] == disj_set: return disj_set lowerCAmelCase__ = self.get_parent(self.parents[disj_set] ) return self.parents[disj_set]
61
import numpy as np from matplotlib import pyplot as plt from sklearn.datasets import load_iris from sklearn.metrics import ConfusionMatrixDisplay from sklearn.model_selection import train_test_split from xgboost import XGBClassifier def A ( lowercase__ : dict ) -> tuple: return (data["data"], data["target"]) def A ( lowercase__ : np.ndarray , lowercase__ : np.ndarray ) -> XGBClassifier: UpperCamelCase__ :Tuple = XGBClassifier() classifier.fit(lowercase__ , lowercase__ ) return classifier def A ( ) -> None: UpperCamelCase__ :str = load_iris() UpperCamelCase__ , UpperCamelCase__ :int = data_handling(lowercase__ ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :int = train_test_split( lowercase__ , lowercase__ , test_size=0.25 ) UpperCamelCase__ :Optional[int] = iris["""target_names"""] # Create an XGBoost Classifier from the training data UpperCamelCase__ :Optional[Any] = xgboost(lowercase__ , lowercase__ ) # Display the confusion matrix of the classifier with both training and test sets ConfusionMatrixDisplay.from_estimator( lowercase__ , lowercase__ , lowercase__ , display_labels=lowercase__ , cmap="""Blues""" , normalize="""true""" , ) plt.title("""Normalized Confusion Matrix - IRIS Dataset""" ) plt.show() if __name__ == "__main__": import doctest doctest.testmod(verbose=True) main()
45
0
import argparse import requests import torch # pip3 install salesforce-lavis # I'm actually installing a slightly modified version: pip3 install git+https://github.com/nielsrogge/LAVIS.git@fix_lavis from lavis.models import load_model_and_preprocess from PIL import Image from transformers import ( AutoTokenizer, BlipaConfig, BlipaForConditionalGeneration, BlipaProcessor, BlipaVisionConfig, BlipImageProcessor, OPTConfig, TaConfig, ) from transformers.utils.constants import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD def lowerCamelCase__ ( ): """simple docstring""" SCREAMING_SNAKE_CASE : Union[str, Any] = "https://storage.googleapis.com/sfr-vision-language-research/LAVIS/assets/merlion.png" SCREAMING_SNAKE_CASE : Any = Image.open(requests.get(lowercase , stream=lowercase ).raw ).convert("RGB" ) return image def lowerCamelCase__ ( lowercase ): """simple docstring""" SCREAMING_SNAKE_CASE : int = [] # fmt: off # vision encoder rename_keys.append(("visual_encoder.cls_token", "vision_model.embeddings.class_embedding") ) rename_keys.append(("visual_encoder.pos_embed", "vision_model.embeddings.position_embedding") ) rename_keys.append(("visual_encoder.patch_embed.proj.weight", "vision_model.embeddings.patch_embedding.weight") ) rename_keys.append(("visual_encoder.patch_embed.proj.bias", "vision_model.embeddings.patch_embedding.bias") ) rename_keys.append(("ln_vision.weight", "vision_model.post_layernorm.weight") ) rename_keys.append(("ln_vision.bias", "vision_model.post_layernorm.bias") ) for i in range(config.vision_config.num_hidden_layers ): rename_keys.append((F'''visual_encoder.blocks.{i}.norm1.weight''', F'''vision_model.encoder.layers.{i}.layer_norm1.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.norm1.bias''', F'''vision_model.encoder.layers.{i}.layer_norm1.bias''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.norm2.weight''', F'''vision_model.encoder.layers.{i}.layer_norm2.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.norm2.bias''', F'''vision_model.encoder.layers.{i}.layer_norm2.bias''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.attn.qkv.weight''', F'''vision_model.encoder.layers.{i}.self_attn.qkv.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.attn.proj.weight''', F'''vision_model.encoder.layers.{i}.self_attn.projection.weight''',) ) rename_keys.append((F'''visual_encoder.blocks.{i}.attn.proj.bias''', F'''vision_model.encoder.layers.{i}.self_attn.projection.bias''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.mlp.fc1.weight''', F'''vision_model.encoder.layers.{i}.mlp.fc1.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.mlp.fc1.bias''', F'''vision_model.encoder.layers.{i}.mlp.fc1.bias''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.mlp.fc2.weight''', F'''vision_model.encoder.layers.{i}.mlp.fc2.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.mlp.fc2.bias''', F'''vision_model.encoder.layers.{i}.mlp.fc2.bias''') ) # QFormer rename_keys.append(("Qformer.bert.embeddings.LayerNorm.weight", "qformer.layernorm.weight") ) rename_keys.append(("Qformer.bert.embeddings.LayerNorm.bias", "qformer.layernorm.bias") ) # fmt: on return rename_keys def lowerCamelCase__ ( lowercase , lowercase , lowercase ): """simple docstring""" SCREAMING_SNAKE_CASE : Union[str, Any] = dct.pop(lowercase ) SCREAMING_SNAKE_CASE : Union[str, Any] = val def lowerCamelCase__ ( lowercase , lowercase ): """simple docstring""" for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases SCREAMING_SNAKE_CASE : Union[str, Any] = state_dict.pop(F'''visual_encoder.blocks.{i}.attn.q_bias''' ) SCREAMING_SNAKE_CASE : Any = state_dict.pop(F'''visual_encoder.blocks.{i}.attn.v_bias''' ) # next, set bias in the state dict SCREAMING_SNAKE_CASE : Optional[int] = torch.cat((q_bias, torch.zeros_like(lowercase , requires_grad=lowercase ), v_bias) ) SCREAMING_SNAKE_CASE : Dict = qkv_bias def lowerCamelCase__ ( lowercase , lowercase ): """simple docstring""" SCREAMING_SNAKE_CASE : List[Any] = 364 if "coco" in model_name else 224 SCREAMING_SNAKE_CASE : Union[str, Any] = BlipaVisionConfig(image_size=lowercase ).to_dict() # make sure the models have proper bos_token_id and eos_token_id set (important for generation) # seems like flan-T5 models don't have bos_token_id properly set? if "opt-2.7b" in model_name: SCREAMING_SNAKE_CASE : Dict = OPTConfig.from_pretrained("facebook/opt-2.7b" , eos_token_id=lowercase ).to_dict() elif "opt-6.7b" in model_name: SCREAMING_SNAKE_CASE : int = OPTConfig.from_pretrained("facebook/opt-6.7b" , eos_token_id=lowercase ).to_dict() elif "t5-xl" in model_name: SCREAMING_SNAKE_CASE : Optional[Any] = TaConfig.from_pretrained("google/flan-t5-xl" , dense_act_fn="gelu" , bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: SCREAMING_SNAKE_CASE : Optional[int] = TaConfig.from_pretrained("google/flan-t5-xxl" , dense_act_fn="gelu" , bos_token_id=1 ).to_dict() SCREAMING_SNAKE_CASE : Tuple = BlipaConfig(vision_config=lowercase , text_config=lowercase ) return config, image_size @torch.no_grad() def lowerCamelCase__ ( lowercase , lowercase=None , lowercase=False ): """simple docstring""" SCREAMING_SNAKE_CASE : List[str] = ( AutoTokenizer.from_pretrained("facebook/opt-2.7b" ) if "opt" in model_name else AutoTokenizer.from_pretrained("google/flan-t5-xl" ) ) SCREAMING_SNAKE_CASE : Optional[Any] = tokenizer("\n" , add_special_tokens=lowercase ).input_ids[0] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[Any] = get_blipa_config(lowercase , eos_token_id=lowercase ) SCREAMING_SNAKE_CASE : int = BlipaForConditionalGeneration(lowercase ).eval() SCREAMING_SNAKE_CASE : Optional[Any] = { "blip2-opt-2.7b": ("blip2_opt", "pretrain_opt2.7b"), "blip2-opt-6.7b": ("blip2_opt", "pretrain_opt6.7b"), "blip2-opt-2.7b-coco": ("blip2_opt", "caption_coco_opt2.7b"), "blip2-opt-6.7b-coco": ("blip2_opt", "caption_coco_opt6.7b"), "blip2-flan-t5-xl": ("blip2_t5", "pretrain_flant5xl"), "blip2-flan-t5-xl-coco": ("blip2_t5", "caption_coco_flant5xl"), "blip2-flan-t5-xxl": ("blip2_t5", "pretrain_flant5xxl"), } SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Any = model_name_to_original[model_name] # load original model print("Loading original model..." ) SCREAMING_SNAKE_CASE : Tuple = "cuda" if torch.cuda.is_available() else "cpu" SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : List[Any] = load_model_and_preprocess( name=lowercase , model_type=lowercase , is_eval=lowercase , device=lowercase ) original_model.eval() print("Done!" ) # update state dict keys SCREAMING_SNAKE_CASE : Tuple = original_model.state_dict() SCREAMING_SNAKE_CASE : int = create_rename_keys(lowercase ) for src, dest in rename_keys: rename_key(lowercase , lowercase , lowercase ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): SCREAMING_SNAKE_CASE : Optional[Any] = state_dict.pop(lowercase ) if key.startswith("Qformer.bert" ): SCREAMING_SNAKE_CASE : Any = key.replace("Qformer.bert" , "qformer" ) if "attention.self" in key: SCREAMING_SNAKE_CASE : Dict = key.replace("self" , "attention" ) if "opt_proj" in key: SCREAMING_SNAKE_CASE : Dict = key.replace("opt_proj" , "language_projection" ) if "t5_proj" in key: SCREAMING_SNAKE_CASE : int = key.replace("t5_proj" , "language_projection" ) if key.startswith("opt" ): SCREAMING_SNAKE_CASE : Optional[int] = key.replace("opt" , "language" ) if key.startswith("t5" ): SCREAMING_SNAKE_CASE : Union[str, Any] = key.replace("t5" , "language" ) SCREAMING_SNAKE_CASE : Optional[Any] = val # read in qv biases read_in_q_v_bias(lowercase , lowercase ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Any = hf_model.load_state_dict(lowercase , strict=lowercase ) assert len(lowercase ) == 0 assert unexpected_keys == ["qformer.embeddings.position_ids"] SCREAMING_SNAKE_CASE : Dict = load_demo_image() SCREAMING_SNAKE_CASE : str = vis_processors["eval"](lowercase ).unsqueeze(0 ).to(lowercase ) SCREAMING_SNAKE_CASE : int = tokenizer(["\n"] , return_tensors="pt" ).input_ids.to(lowercase ) # create processor SCREAMING_SNAKE_CASE : List[Any] = BlipImageProcessor( size={"height": image_size, "width": image_size} , image_mean=lowercase , image_std=lowercase ) SCREAMING_SNAKE_CASE : Optional[int] = BlipaProcessor(image_processor=lowercase , tokenizer=lowercase ) SCREAMING_SNAKE_CASE : Dict = processor(images=lowercase , return_tensors="pt" ).pixel_values.to(lowercase ) # make sure processor creates exact same pixel values assert torch.allclose(lowercase , lowercase ) original_model.to(lowercase ) hf_model.to(lowercase ) with torch.no_grad(): if "opt" in model_name: SCREAMING_SNAKE_CASE : List[str] = original_model({"image": original_pixel_values, "text_input": [""]} ).logits SCREAMING_SNAKE_CASE : int = hf_model(lowercase , lowercase ).logits else: SCREAMING_SNAKE_CASE : Dict = original_model( {"image": original_pixel_values, "text_input": ["\n"], "text_output": ["\n"]} ).logits SCREAMING_SNAKE_CASE : List[Any] = input_ids.masked_fill(input_ids == tokenizer.pad_token_id , -100 ) SCREAMING_SNAKE_CASE : Any = hf_model(lowercase , lowercase , labels=lowercase ).logits assert original_logits.shape == logits.shape print("First values of original logits:" , original_logits[0, :3, :3] ) print("First values of HF logits:" , logits[0, :3, :3] ) # assert values if model_name == "blip2-flan-t5-xl": SCREAMING_SNAKE_CASE : Any = torch.tensor( [[-41.5850, -4.4440, -8.9922], [-47.4322, -5.9143, -1.7340]] , device=lowercase ) assert torch.allclose(logits[0, :3, :3] , lowercase , atol=1E-4 ) elif model_name == "blip2-flan-t5-xl-coco": SCREAMING_SNAKE_CASE : Dict = torch.tensor( [[-57.0109, -9.8967, -12.6280], [-68.6578, -12.7191, -10.5065]] , device=lowercase ) else: # cast to same type SCREAMING_SNAKE_CASE : Tuple = logits.dtype assert torch.allclose(original_logits.to(lowercase ) , lowercase , atol=1E-2 ) print("Looks ok!" ) print("Generating a caption..." ) SCREAMING_SNAKE_CASE : str = "" SCREAMING_SNAKE_CASE : int = tokenizer(lowercase , return_tensors="pt" ).input_ids.to(lowercase ) SCREAMING_SNAKE_CASE : Union[str, Any] = original_model.generate({"image": original_pixel_values} ) SCREAMING_SNAKE_CASE : List[str] = hf_model.generate( lowercase , lowercase , do_sample=lowercase , num_beams=5 , max_length=30 , min_length=1 , top_p=0.9 , repetition_penalty=1.0 , length_penalty=1.0 , temperature=1 , ) print("Original generation:" , lowercase ) SCREAMING_SNAKE_CASE : List[str] = input_ids.shape[1] SCREAMING_SNAKE_CASE : Dict = processor.batch_decode(outputs[:, prompt_length:] , skip_special_tokens=lowercase ) SCREAMING_SNAKE_CASE : Any = [text.strip() for text in output_text] print("HF generation:" , lowercase ) if pytorch_dump_folder_path is not None: processor.save_pretrained(lowercase ) hf_model.save_pretrained(lowercase ) if push_to_hub: processor.push_to_hub(F'''nielsr/{model_name}''' ) hf_model.push_to_hub(F'''nielsr/{model_name}''' ) if __name__ == "__main__": snake_case = argparse.ArgumentParser() snake_case = [ """blip2-opt-2.7b""", """blip2-opt-6.7b""", """blip2-opt-2.7b-coco""", """blip2-opt-6.7b-coco""", """blip2-flan-t5-xl""", """blip2-flan-t5-xl-coco""", """blip2-flan-t5-xxl""", ] parser.add_argument( """--model_name""", default="""blip2-opt-2.7b""", choices=choices, type=str, help="""Path to hf config.json of model to convert""", ) parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether to push the model and processor to the hub after converting""", ) snake_case = parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
62
import multiprocessing import time from arguments import PretokenizationArguments from datasets import load_dataset from transformers import AutoTokenizer, HfArgumentParser def A ( lowercase__ : Optional[int] ) -> Optional[Any]: UpperCamelCase__ :Union[str, Any] = {} UpperCamelCase__ :Optional[int] = tokenizer(example["""content"""] , truncation=lowercase__ )["""input_ids"""] UpperCamelCase__ :int = len(example["""content"""] ) / len(output["""input_ids"""] ) return output UpperCamelCase = HfArgumentParser(PretokenizationArguments) UpperCamelCase = parser.parse_args() if args.num_workers is None: UpperCamelCase = multiprocessing.cpu_count() UpperCamelCase = AutoTokenizer.from_pretrained(args.tokenizer_dir) UpperCamelCase = time.time() UpperCamelCase = load_dataset(args.dataset_name, split="train") print(f'''Dataset loaded in {time.time()-t_start:.2f}s''') UpperCamelCase = time.time() UpperCamelCase = ds.map( tokenize, num_proc=args.num_workers, remove_columns=[ "repo_name", "path", "copies", "size", "content", "license", "hash", "line_mean", "line_max", "alpha_frac", "autogenerated", ], ) print(f'''Dataset tokenized in {time.time()-t_start:.2f}s''') UpperCamelCase = time.time() ds.push_to_hub(args.tokenized_data_repo) print(f'''Data pushed to the hub in {time.time()-t_start:.2f}s''')
45
0
def lowerCamelCase__ ( __lowerCamelCase : int ): __UpperCAmelCase : List[str] = [1] __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase : Any = 0, 0, 0 __UpperCAmelCase : Any = ugly_nums[ia] * 2 __UpperCAmelCase : Optional[Any] = ugly_nums[ia] * 3 __UpperCAmelCase : List[Any] = ugly_nums[ia] * 5 for _ in range(1 , __lowerCamelCase ): __UpperCAmelCase : str = min(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) ugly_nums.append(__lowerCamelCase ) if next_num == next_a: ia += 1 __UpperCAmelCase : Dict = ugly_nums[ia] * 2 if next_num == next_a: ia += 1 __UpperCAmelCase : List[str] = ugly_nums[ia] * 3 if next_num == next_a: ia += 1 __UpperCAmelCase : int = ugly_nums[ia] * 5 return ugly_nums[-1] if __name__ == "__main__": from doctest import testmod testmod(verbose=True) print(f"""{ugly_numbers(200) = }""")
63
def A ( lowercase__ : int ) -> Optional[Any]: stooge(lowercase__ , 0 , len(lowercase__ ) - 1 ) return arr def A ( lowercase__ : Union[str, Any] , lowercase__ : Dict , lowercase__ : str ) -> List[str]: if i >= h: return # If first element is smaller than the last then swap them if arr[i] > arr[h]: UpperCamelCase__ , UpperCamelCase__ :List[str] = arr[h], arr[i] # If there are more than 2 elements in the array if h - i + 1 > 2: UpperCamelCase__ :Optional[int] = (int)((h - i + 1) / 3 ) # Recursively sort first 2/3 elements stooge(lowercase__ , lowercase__ , (h - t) ) # Recursively sort last 2/3 elements stooge(lowercase__ , i + t , (lowercase__) ) # Recursively sort first 2/3 elements stooge(lowercase__ , lowercase__ , (h - t) ) if __name__ == "__main__": UpperCamelCase = input("Enter numbers separated by a comma:\n").strip() UpperCamelCase = [int(item) for item in user_input.split(",")] print(stooge_sort(unsorted))
45
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_tf_available, is_torch_available, ) lowercase_ : List[Any] = { 'configuration_speech_to_text': ['SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Speech2TextConfig'], 'processing_speech_to_text': ['Speech2TextProcessor'], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ : Optional[Any] = ['Speech2TextTokenizer'] try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ : Any = ['Speech2TextFeatureExtractor'] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ : Optional[int] = [ 'TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFSpeech2TextForConditionalGeneration', 'TFSpeech2TextModel', 'TFSpeech2TextPreTrainedModel', ] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ : Optional[int] = [ 'SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST', 'Speech2TextForConditionalGeneration', 'Speech2TextModel', 'Speech2TextPreTrainedModel', ] if TYPE_CHECKING: from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig from .processing_speech_to_text import SpeechaTextProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_speech_to_text import SpeechaTextTokenizer try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_speech_to_text import ( TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, TFSpeechaTextForConditionalGeneration, TFSpeechaTextModel, TFSpeechaTextPreTrainedModel, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speech_to_text import ( SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, SpeechaTextForConditionalGeneration, SpeechaTextModel, SpeechaTextPreTrainedModel, ) else: import sys lowercase_ : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
64
import argparse import json import os from tensorflow.core.protobuf.saved_model_pba import SavedModel # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_copies.py UpperCamelCase = "." # Internal TensorFlow ops that can be safely ignored (mostly specific to a saved model) UpperCamelCase = [ "Assert", "AssignVariableOp", "EmptyTensorList", "MergeV2Checkpoints", "ReadVariableOp", "ResourceGather", "RestoreV2", "SaveV2", "ShardedFilename", "StatefulPartitionedCall", "StaticRegexFullMatch", "VarHandleOp", ] def A ( lowercase__ : Tuple , lowercase__ : Optional[Any] , lowercase__ : Dict ) -> List[Any]: UpperCamelCase__ :str = SavedModel() UpperCamelCase__ :List[str] = [] with open(os.path.join(lowercase__ , """utils""" , """tf_ops""" , """onnx.json""" ) ) as f: UpperCamelCase__ :str = json.load(lowercase__ )["""opsets"""] for i in range(1 , opset + 1 ): onnx_ops.extend(onnx_opsets[str(lowercase__ )] ) with open(lowercase__ , """rb""" ) as f: saved_model.ParseFromString(f.read() ) UpperCamelCase__ :Tuple = set() # Iterate over every metagraph in case there is more than one (a saved model can contain multiple graphs) for meta_graph in saved_model.meta_graphs: # Add operations in the graph definition model_op_names.update(node.op for node in meta_graph.graph_def.node ) # Go through the functions in the graph definition for func in meta_graph.graph_def.library.function: # Add operations in each function model_op_names.update(node.op for node in func.node_def ) # Convert to list, sorted if you want UpperCamelCase__ :Union[str, Any] = sorted(lowercase__ ) UpperCamelCase__ :List[Any] = [] for op in model_op_names: if op not in onnx_ops and op not in INTERNAL_OPS: incompatible_ops.append(lowercase__ ) if strict and len(lowercase__ ) > 0: raise Exception(f"""Found the following incompatible ops for the opset {opset}:\n""" + incompatible_ops ) elif len(lowercase__ ) > 0: print(f"""Found the following incompatible ops for the opset {opset}:""" ) print(*lowercase__ , sep="""\n""" ) else: print(f"""The saved model {saved_model_path} can properly be converted with ONNX.""" ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() parser.add_argument("--saved_model_path", help="Path of the saved model to check (the .pb file).") parser.add_argument( "--opset", default=12, type=int, help="The ONNX opset against which the model has to be tested." ) parser.add_argument( "--framework", choices=["onnx"], default="onnx", help="Frameworks against which to test the saved model." ) parser.add_argument( "--strict", action="store_true", help="Whether make the checking strict (raise errors) or not (raise warnings)" ) UpperCamelCase = parser.parse_args() if args.framework == "onnx": onnx_compliancy(args.saved_model_path, args.strict, args.opset)
45
0
"""simple docstring""" __UpperCAmelCase = {str(digit): digit**5 for digit in range(10)} def lowerCAmelCase ( __UpperCamelCase ): '''simple docstring''' return sum(DIGITS_FIFTH_POWER[digit] for digit in str(__UpperCamelCase ) ) def lowerCAmelCase ( ): '''simple docstring''' return sum( number for number in range(1000 , 1000000 ) if number == digits_fifth_powers_sum(__UpperCamelCase ) ) if __name__ == "__main__": print(solution())
65
from __future__ import annotations def A ( lowercase__ : str , lowercase__ : list[str] | None = None , lowercase__ : dict[str, float] | None = None , lowercase__ : bool = False , ) -> tuple[int, float, str]: UpperCamelCase__ :Dict = cipher_alphabet or [chr(lowercase__ ) for i in range(97 , 123 )] # If the argument is None or the user provided an empty dictionary if not frequencies_dict: # Frequencies of letters in the english language (how much they show up) UpperCamelCase__ :Optional[Any] = { """a""": 0.08497, """b""": 0.01492, """c""": 0.02202, """d""": 0.04253, """e""": 0.11162, """f""": 0.02228, """g""": 0.02015, """h""": 0.06094, """i""": 0.07546, """j""": 0.00153, """k""": 0.01292, """l""": 0.04025, """m""": 0.02406, """n""": 0.06749, """o""": 0.07507, """p""": 0.01929, """q""": 0.00095, """r""": 0.07587, """s""": 0.06327, """t""": 0.09356, """u""": 0.02758, """v""": 0.00978, """w""": 0.02560, """x""": 0.00150, """y""": 0.01994, """z""": 0.00077, } else: # Custom frequencies dictionary UpperCamelCase__ :Optional[int] = frequencies_dict if not case_sensitive: UpperCamelCase__ :int = ciphertext.lower() # Chi squared statistic values UpperCamelCase__ :dict[int, tuple[float, str]] = {} # cycle through all of the shifts for shift in range(len(lowercase__ ) ): UpperCamelCase__ :int = """""" # decrypt the message with the shift for letter in ciphertext: try: # Try to index the letter in the alphabet UpperCamelCase__ :int = (alphabet_letters.index(letter.lower() ) - shift) % len( lowercase__ ) decrypted_with_shift += ( alphabet_letters[new_key].upper() if case_sensitive and letter.isupper() else alphabet_letters[new_key] ) except ValueError: # Append the character if it isn't in the alphabet decrypted_with_shift += letter UpperCamelCase__ :Optional[int] = 0.0 # Loop through each letter in the decoded message with the shift for letter in decrypted_with_shift: if case_sensitive: UpperCamelCase__ :Optional[int] = letter.lower() if letter in frequencies: # Get the amount of times the letter occurs in the message UpperCamelCase__ :Optional[int] = decrypted_with_shift.lower().count(lowercase__ ) # Get the excepcted amount of times the letter should appear based # on letter frequencies UpperCamelCase__ :Optional[int] = frequencies[letter] * occurrences # Complete the chi squared statistic formula UpperCamelCase__ :Dict = ((occurrences - expected) ** 2) / expected # Add the margin of error to the total chi squared statistic chi_squared_statistic += chi_letter_value else: if letter.lower() in frequencies: # Get the amount of times the letter occurs in the message UpperCamelCase__ :List[str] = decrypted_with_shift.count(lowercase__ ) # Get the excepcted amount of times the letter should appear based # on letter frequencies UpperCamelCase__ :Union[str, Any] = frequencies[letter] * occurrences # Complete the chi squared statistic formula UpperCamelCase__ :List[str] = ((occurrences - expected) ** 2) / expected # Add the margin of error to the total chi squared statistic chi_squared_statistic += chi_letter_value # Add the data to the chi_squared_statistic_values dictionary UpperCamelCase__ :Union[str, Any] = ( chi_squared_statistic, decrypted_with_shift, ) # Get the most likely cipher by finding the cipher with the smallest chi squared # statistic def chi_squared_statistic_values_sorting_key(lowercase__ : int ) -> tuple[float, str]: return chi_squared_statistic_values[key] UpperCamelCase__ :int = min( lowercase__ , key=lowercase__ , ) # Get all the data from the most likely cipher (key, decoded message) ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :Tuple = chi_squared_statistic_values[most_likely_cipher] # Return the data on the most likely shift return ( most_likely_cipher, most_likely_cipher_chi_squared_value, decoded_most_likely_cipher, )
45
0
import itertools import json import os import unittest from transformers import AddedToken, LongformerTokenizer, LongformerTokenizerFast from transformers.models.longformer.tokenization_longformer import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowerCAmelCase_ ( __snake_case , unittest.TestCase ): _UpperCamelCase : int = LongformerTokenizer _UpperCamelCase : List[str] = True _UpperCamelCase : int = LongformerTokenizerFast _UpperCamelCase : Optional[Any] = True def __a ( self ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt _lowercase : Optional[int] = [ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', '\u0120', '\u0120l', '\u0120n', '\u0120lo', '\u0120low', 'er', '\u0120lowest', '\u0120newer', '\u0120wider', '<unk>', ] _lowercase : List[str] = dict(zip(_lowerCAmelCase , range(len(_lowerCAmelCase ) ) ) ) _lowercase : Union[str, Any] = ['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', ''] _lowercase : Optional[int] = {'unk_token': '<unk>'} _lowercase : str = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) _lowercase : str = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(_lowerCAmelCase ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(_lowerCAmelCase ) ) def __a ( self , **_lowerCAmelCase ): kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **_lowerCAmelCase ) def __a ( self , **_lowerCAmelCase ): kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **_lowerCAmelCase ) def __a ( self , _lowerCAmelCase ): _lowercase : Dict = 'lower newer' _lowercase : Union[str, Any] = 'lower newer' return input_text, output_text def __a ( self ): _lowercase : List[Any] = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) _lowercase : str = 'lower newer' _lowercase : List[Any] = ['l', 'o', 'w', 'er', '\u0120', 'n', 'e', 'w', 'er'] _lowercase : Optional[int] = tokenizer.tokenize(_lowerCAmelCase ) # , add_prefix_space=True) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) _lowercase : Dict = tokens + [tokenizer.unk_token] _lowercase : Optional[Any] = [0, 1, 2, 1_5, 1_0, 9, 3, 2, 1_5, 1_9] self.assertListEqual(tokenizer.convert_tokens_to_ids(_lowerCAmelCase ) , _lowerCAmelCase ) def __a ( self ): _lowercase : List[Any] = self.get_tokenizer() self.assertListEqual(tokenizer.encode('Hello world!' , add_special_tokens=_lowerCAmelCase ) , [0, 3_1_4_1_4, 2_3_2, 3_2_8, 2] ) self.assertListEqual( tokenizer.encode('Hello world! cécé herlolip 418' , add_special_tokens=_lowerCAmelCase ) , [0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2] , ) @slow def __a ( self ): _lowercase : Any = self.tokenizer_class.from_pretrained('allenai/longformer-base-4096' ) _lowercase : Optional[Any] = tokenizer.encode('sequence builders' , add_special_tokens=_lowerCAmelCase ) _lowercase : str = tokenizer.encode('multi-sequence build' , add_special_tokens=_lowerCAmelCase ) _lowercase : Any = tokenizer.encode( 'sequence builders' , add_special_tokens=_lowerCAmelCase , add_prefix_space=_lowerCAmelCase ) _lowercase : List[str] = tokenizer.encode( 'sequence builders' , 'multi-sequence build' , add_special_tokens=_lowerCAmelCase , add_prefix_space=_lowerCAmelCase ) _lowercase : Union[str, Any] = tokenizer.build_inputs_with_special_tokens(_lowerCAmelCase ) _lowercase : Dict = tokenizer.build_inputs_with_special_tokens(_lowerCAmelCase , _lowerCAmelCase ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def __a ( self ): _lowercase : List[str] = self.get_tokenizer() _lowercase : Tuple = 'Encode this sequence.' _lowercase : List[Any] = tokenizer.byte_encoder[' '.encode('utf-8' )[0]] # Testing encoder arguments _lowercase : int = tokenizer.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase , add_prefix_space=_lowerCAmelCase ) _lowercase : Any = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(_lowerCAmelCase , _lowerCAmelCase ) _lowercase : Dict = tokenizer.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase , add_prefix_space=_lowerCAmelCase ) _lowercase : List[Any] = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(_lowerCAmelCase , _lowerCAmelCase ) tokenizer.add_special_tokens({'bos_token': '<s>'} ) _lowercase : Optional[Any] = tokenizer.encode(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) _lowercase : Dict = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(_lowerCAmelCase , _lowerCAmelCase ) # Testing spaces after special tokens _lowercase : int = '<mask>' tokenizer.add_special_tokens( {'mask_token': AddedToken(_lowerCAmelCase , lstrip=_lowerCAmelCase , rstrip=_lowerCAmelCase )} ) # mask token has a left space _lowercase : int = tokenizer.convert_tokens_to_ids(_lowerCAmelCase ) _lowercase : List[Any] = 'Encode <mask> sequence' _lowercase : List[Any] = 'Encode <mask>sequence' _lowercase : Union[str, Any] = tokenizer.encode(_lowerCAmelCase ) _lowercase : int = encoded.index(_lowerCAmelCase ) _lowercase : Optional[Any] = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(_lowerCAmelCase , _lowerCAmelCase ) _lowercase : Tuple = tokenizer.encode(_lowerCAmelCase ) _lowercase : Optional[Any] = encoded.index(_lowerCAmelCase ) _lowercase : int = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(_lowerCAmelCase , _lowerCAmelCase ) def __a ( self ): pass def __a ( self ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): _lowercase : Dict = self.rust_tokenizer_class.from_pretrained(_lowerCAmelCase , **_lowerCAmelCase ) _lowercase : Dict = self.tokenizer_class.from_pretrained(_lowerCAmelCase , **_lowerCAmelCase ) _lowercase : List[str] = 'A, <mask> AllenNLP sentence.' _lowercase : Any = tokenizer_r.encode_plus(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase , return_token_type_ids=_lowerCAmelCase ) _lowercase : Any = tokenizer_p.encode_plus(_lowerCAmelCase , add_special_tokens=_lowerCAmelCase , return_token_type_ids=_lowerCAmelCase ) # token_type_ids should put 0 everywhere self.assertEqual(sum(tokens_r['token_type_ids'] ) , sum(tokens_p['token_type_ids'] ) ) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEqual( sum(tokens_r['attention_mask'] ) / len(tokens_r['attention_mask'] ) , sum(tokens_p['attention_mask'] ) / len(tokens_p['attention_mask'] ) , ) _lowercase : str = tokenizer_r.convert_ids_to_tokens(tokens_r['input_ids'] ) _lowercase : Any = tokenizer_p.convert_ids_to_tokens(tokens_p['input_ids'] ) # Rust correctly handles the space before the mask while python doesnt self.assertSequenceEqual(tokens_p['input_ids'] , [0, 2_5_0, 6, 5_0_2_6_4, 3_8_2_3, 4_8_7, 2_1_9_9_2, 3_6_4_5, 4, 2] ) self.assertSequenceEqual(tokens_r['input_ids'] , [0, 2_5_0, 6, 5_0_2_6_4, 3_8_2_3, 4_8_7, 2_1_9_9_2, 3_6_4_5, 4, 2] ) self.assertSequenceEqual( _lowerCAmelCase , ['<s>', 'A', ',', '<mask>', 'ĠAllen', 'N', 'LP', 'Ġsentence', '.', '</s>'] ) self.assertSequenceEqual( _lowerCAmelCase , ['<s>', 'A', ',', '<mask>', 'ĠAllen', 'N', 'LP', 'Ġsentence', '.', '</s>'] ) def __a ( self ): for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): _lowercase : Any = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=_lowerCAmelCase , add_prefix_space=_lowerCAmelCase , trim_offsets=_lowerCAmelCase ) _lowercase : str = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) _lowercase : Optional[Any] = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state['add_prefix_space'] , _lowerCAmelCase ) self.assertEqual(post_processor_state['add_prefix_space'] , _lowerCAmelCase ) self.assertEqual(post_processor_state['trim_offsets'] , _lowerCAmelCase ) def __a ( self ): # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` and # `trim_offsets` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): _lowercase : int = 'hello' # `hello` is a token in the vocabulary of `pretrained_name` _lowercase : str = F"""{text_of_1_token} {text_of_1_token}""" _lowercase : Union[str, Any] = self.rust_tokenizer_class.from_pretrained( _lowerCAmelCase , use_fast=_lowerCAmelCase , add_prefix_space=_lowerCAmelCase , trim_offsets=_lowerCAmelCase ) _lowercase : Optional[int] = tokenizer_r(_lowerCAmelCase , return_offsets_mapping=_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_lowerCAmelCase )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_lowerCAmelCase ) + 1, len(_lowerCAmelCase ) + 1 + len(_lowerCAmelCase )) , ) _lowercase : str = self.rust_tokenizer_class.from_pretrained( _lowerCAmelCase , use_fast=_lowerCAmelCase , add_prefix_space=_lowerCAmelCase , trim_offsets=_lowerCAmelCase ) _lowercase : Union[str, Any] = tokenizer_r(_lowerCAmelCase , return_offsets_mapping=_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_lowerCAmelCase )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_lowerCAmelCase ) + 1, len(_lowerCAmelCase ) + 1 + len(_lowerCAmelCase )) , ) _lowercase : Optional[Any] = self.rust_tokenizer_class.from_pretrained( _lowerCAmelCase , use_fast=_lowerCAmelCase , add_prefix_space=_lowerCAmelCase , trim_offsets=_lowerCAmelCase ) _lowercase : Dict = tokenizer_r(_lowerCAmelCase , return_offsets_mapping=_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_lowerCAmelCase )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_lowerCAmelCase ), len(_lowerCAmelCase ) + 1 + len(_lowerCAmelCase )) , ) _lowercase : int = self.rust_tokenizer_class.from_pretrained( _lowerCAmelCase , use_fast=_lowerCAmelCase , add_prefix_space=_lowerCAmelCase , trim_offsets=_lowerCAmelCase ) _lowercase : int = tokenizer_r(_lowerCAmelCase , return_offsets_mapping=_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_lowerCAmelCase )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_lowerCAmelCase ), len(_lowerCAmelCase ) + 1 + len(_lowerCAmelCase )) , ) _lowercase : Optional[int] = F""" {text}""" # tokenizer_r = self.rust_tokenizer_class.from_pretrained( # pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True # ) # encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False) # self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token))) # self.assertEqual( # encoding.offset_mapping[1], # (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)), # ) _lowercase : Optional[Any] = self.rust_tokenizer_class.from_pretrained( _lowerCAmelCase , use_fast=_lowerCAmelCase , add_prefix_space=_lowerCAmelCase , trim_offsets=_lowerCAmelCase ) _lowercase : Optional[Any] = tokenizer_r(_lowerCAmelCase , return_offsets_mapping=_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(_lowerCAmelCase )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_lowerCAmelCase ) + 1, 1 + len(_lowerCAmelCase ) + 1 + len(_lowerCAmelCase )) , ) _lowercase : List[Any] = self.rust_tokenizer_class.from_pretrained( _lowerCAmelCase , use_fast=_lowerCAmelCase , add_prefix_space=_lowerCAmelCase , trim_offsets=_lowerCAmelCase ) _lowercase : List[Any] = tokenizer_r(_lowerCAmelCase , return_offsets_mapping=_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(_lowerCAmelCase )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_lowerCAmelCase ), 1 + len(_lowerCAmelCase ) + 1 + len(_lowerCAmelCase )) , ) _lowercase : Tuple = self.rust_tokenizer_class.from_pretrained( _lowerCAmelCase , use_fast=_lowerCAmelCase , add_prefix_space=_lowerCAmelCase , trim_offsets=_lowerCAmelCase ) _lowercase : Union[str, Any] = tokenizer_r(_lowerCAmelCase , return_offsets_mapping=_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(_lowerCAmelCase )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_lowerCAmelCase ), 1 + len(_lowerCAmelCase ) + 1 + len(_lowerCAmelCase )) , )
66
import warnings from ...utils import logging from .image_processing_mobilevit import MobileViTImageProcessor UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( lowercase ): """simple docstring""" def __init__( self :Union[str, Any] , *lowerCamelCase__ :Optional[int] , **lowerCamelCase__ :Dict ): warnings.warn( """The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.""" """ Please use MobileViTImageProcessor instead.""" , lowerCamelCase__ , ) super().__init__(*lowerCamelCase__ , **lowerCamelCase__ )
45
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available snake_case = { """configuration_timesformer""": ["""TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """TimesformerConfig"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case = [ """TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""", """TimesformerModel""", """TimesformerForVideoClassification""", """TimesformerPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_timesformer import ( TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimesformerForVideoClassification, TimesformerModel, TimesformerPreTrainedModel, ) else: import sys snake_case = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
67
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 UpperCamelCase = get_tests_dir("fixtures") UpperCamelCase = get_tests_dir("fixtures/dummy_feature_extractor_config.json") UpperCamelCase = get_tests_dir("fixtures/dummy-config.json") class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __a ( self :Optional[int] ): UpperCamelCase__ :Optional[int] = 0 def __a ( self :str ): UpperCamelCase__ :Optional[Any] = AutoFeatureExtractor.from_pretrained("""facebook/wav2vec2-base-960h""" ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Dict ): UpperCamelCase__ :Dict = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Optional[int] ): with tempfile.TemporaryDirectory() as tmpdirname: UpperCamelCase__ :List[str] = WavaVecaConfig() # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally UpperCamelCase__ :Tuple = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ ).to_dict() config_dict.pop("""feature_extractor_type""" ) UpperCamelCase__ :Union[str, Any] = WavaVecaFeatureExtractor(**lowerCamelCase__ ) # save in new folder model_config.save_pretrained(lowerCamelCase__ ) config.save_pretrained(lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ ) # make sure private variable is not incorrectly saved UpperCamelCase__ :Tuple = json.loads(config.to_json_string() ) self.assertTrue("""_processor_class""" not in dict_as_saved ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Union[str, Any] ): UpperCamelCase__ :Dict = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Dict ): with self.assertRaisesRegex( lowerCamelCase__ , """bert-base is not a local folder and is not a valid model identifier""" ): UpperCamelCase__ :Dict = AutoFeatureExtractor.from_pretrained("""bert-base""" ) def __a ( self :List[Any] ): with self.assertRaisesRegex( lowerCamelCase__ , r"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)""" ): UpperCamelCase__ :Optional[int] = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ , revision="""aaaaaa""" ) def __a ( self :int ): with self.assertRaisesRegex( lowerCamelCase__ , """hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.""" , ): UpperCamelCase__ :List[Any] = AutoFeatureExtractor.from_pretrained("""hf-internal-testing/config-no-model""" ) def __a ( self :Optional[int] ): # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(lowerCamelCase__ ): UpperCamelCase__ :List[Any] = AutoFeatureExtractor.from_pretrained( """hf-internal-testing/test_dynamic_feature_extractor""" ) # If remote code is disabled, we can't load this config. with self.assertRaises(lowerCamelCase__ ): UpperCamelCase__ :Optional[Any] = AutoFeatureExtractor.from_pretrained( """hf-internal-testing/test_dynamic_feature_extractor""" , trust_remote_code=lowerCamelCase__ ) UpperCamelCase__ :str = AutoFeatureExtractor.from_pretrained( """hf-internal-testing/test_dynamic_feature_extractor""" , trust_remote_code=lowerCamelCase__ ) self.assertEqual(feature_extractor.__class__.__name__ , """NewFeatureExtractor""" ) # Test feature extractor can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained(lowerCamelCase__ ) UpperCamelCase__ :Any = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ , trust_remote_code=lowerCamelCase__ ) self.assertEqual(reloaded_feature_extractor.__class__.__name__ , """NewFeatureExtractor""" ) def __a ( self :Dict ): try: AutoConfig.register("""custom""" , lowerCamelCase__ ) AutoFeatureExtractor.register(lowerCamelCase__ , lowerCamelCase__ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(lowerCamelCase__ ): AutoFeatureExtractor.register(lowerCamelCase__ , lowerCamelCase__ ) # Now that the config is registered, it can be used as any other config with the auto-API UpperCamelCase__ :Any = CustomFeatureExtractor.from_pretrained(lowerCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained(lowerCamelCase__ ) UpperCamelCase__ :List[Any] = AutoFeatureExtractor.from_pretrained(lowerCamelCase__ ) self.assertIsInstance(lowerCamelCase__ , lowerCamelCase__ ) 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 __a ( self :Optional[int] ): class lowerCAmelCase_ ( lowercase ): """simple docstring""" _snake_case : Optional[int] = True try: AutoConfig.register("""custom""" , lowerCamelCase__ ) AutoFeatureExtractor.register(lowerCamelCase__ , lowerCamelCase__ ) # If remote code is not set, the default is to use local UpperCamelCase__ :Optional[Any] = 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. UpperCamelCase__ :str = AutoFeatureExtractor.from_pretrained( """hf-internal-testing/test_dynamic_feature_extractor""" , trust_remote_code=lowerCamelCase__ ) self.assertEqual(feature_extractor.__class__.__name__ , """NewFeatureExtractor""" ) self.assertTrue(feature_extractor.is_local ) # If remote is enabled, we load from the Hub UpperCamelCase__ :Optional[int] = AutoFeatureExtractor.from_pretrained( """hf-internal-testing/test_dynamic_feature_extractor""" , trust_remote_code=lowerCamelCase__ ) self.assertEqual(feature_extractor.__class__.__name__ , """NewFeatureExtractor""" ) self.assertTrue(not hasattr(lowerCamelCase__ , """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]
45
0
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import numpy as np from utils_multiple_choice import MultipleChoiceDataset, Split, processors import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process __A = logging.getLogger(__name__) def lowercase__ ( A_: int , A_: Optional[Any] ) -> int: """simple docstring""" return (preds == labels).mean() @dataclass class _A : """simple docstring""" lowerCamelCase : str = field( metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} ) lowerCamelCase : Optional[str] = field( default=UpperCamelCase , metadata={'help': 'Pretrained config name or path if not the same as model_name'} ) lowerCamelCase : Optional[str] = field( default=UpperCamelCase , metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'} ) lowerCamelCase : Optional[str] = field( default=UpperCamelCase , metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'} , ) @dataclass class _A : """simple docstring""" lowerCamelCase : str = field(metadata={'help': 'The name of the task to train on: ' + ', '.join(processors.keys() )} ) lowerCamelCase : str = field(metadata={'help': 'Should contain the data files for the task.'} ) lowerCamelCase : int = field( default=128 , metadata={ 'help': ( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) } , ) lowerCamelCase : bool = field( default=UpperCamelCase , metadata={'help': 'Overwrite the cached training and evaluation sets'} ) def lowercase__ ( ) -> str: """simple docstring""" __UpperCAmelCase =HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase =parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( F'''Output directory ({training_args.output_dir}) already exists and is not empty. Use''' """ --overwrite_output_dir to overcome.""" ) # Setup logging logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , level=logging.INFO 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""" , A_ ) # Set seed set_seed(training_args.seed ) try: __UpperCAmelCase =processors[data_args.task_name]() __UpperCAmelCase =processor.get_labels() __UpperCAmelCase =len(A_ ) except KeyError: raise ValueError("""Task not found: %s""" % (data_args.task_name) ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. __UpperCAmelCase =AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=A_ , finetuning_task=data_args.task_name , cache_dir=model_args.cache_dir , ) __UpperCAmelCase =AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) __UpperCAmelCase =AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path , from_tf=bool(""".ckpt""" in model_args.model_name_or_path ) , config=A_ , cache_dir=model_args.cache_dir , ) # Get datasets __UpperCAmelCase =( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=A_ , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.train , ) if training_args.do_train else None ) __UpperCAmelCase =( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=A_ , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.dev , ) if training_args.do_eval else None ) def compute_metrics(A_: EvalPrediction ) -> Dict: __UpperCAmelCase =np.argmax(p.predictions , axis=1 ) return {"acc": simple_accuracy(A_ , p.label_ids )} # Data collator __UpperCAmelCase =DataCollatorWithPadding(A_ , pad_to_multiple_of=8 ) if training_args.fpaa else None # Initialize our Trainer __UpperCAmelCase =Trainer( model=A_ , args=A_ , train_dataset=A_ , eval_dataset=A_ , compute_metrics=A_ , data_collator=A_ , ) # Training if training_args.do_train: trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) 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 __UpperCAmelCase ={} if training_args.do_eval: logger.info("""*** Evaluate ***""" ) __UpperCAmelCase =trainer.evaluate() __UpperCAmelCase =os.path.join(training_args.output_dir , """eval_results.txt""" ) if trainer.is_world_master(): with open(A_ , """w""" ) as writer: logger.info("""***** Eval results *****""" ) for key, value in result.items(): logger.info(""" %s = %s""" , A_ , A_ ) writer.write("""%s = %s\n""" % (key, value) ) results.update(A_ ) return results def lowercase__ ( A_: Union[str, Any] ) -> Any: """simple docstring""" main() if __name__ == "__main__": main()
68
import numpy as np import torch import tqdm from ...models.unet_ad import UNetaDModel from ...pipelines import DiffusionPipeline from ...utils import randn_tensor from ...utils.dummy_pt_objects import DDPMScheduler class lowerCAmelCase_ ( lowercase ): """simple docstring""" def __init__( self :int , lowerCamelCase__ :UNetaDModel , lowerCamelCase__ :UNetaDModel , lowerCamelCase__ :DDPMScheduler , lowerCamelCase__ :List[Any] , ): super().__init__() UpperCamelCase__ :Tuple = value_function UpperCamelCase__ :Optional[int] = unet UpperCamelCase__ :List[str] = scheduler UpperCamelCase__ :Dict = env UpperCamelCase__ :Dict = env.get_dataset() UpperCamelCase__ :Union[str, Any] = {} for key in self.data.keys(): try: UpperCamelCase__ :int = self.data[key].mean() except: # noqa: E722 pass UpperCamelCase__ :Any = {} for key in self.data.keys(): try: UpperCamelCase__ :int = self.data[key].std() except: # noqa: E722 pass UpperCamelCase__ :List[Any] = env.observation_space.shape[0] UpperCamelCase__ :List[str] = env.action_space.shape[0] def __a ( self :Union[str, Any] , lowerCamelCase__ :List[str] , lowerCamelCase__ :str ): return (x_in - self.means[key]) / self.stds[key] def __a ( self :int , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Tuple ): return x_in * self.stds[key] + self.means[key] def __a ( self :Any , lowerCamelCase__ :int ): if type(lowerCamelCase__ ) is dict: return {k: self.to_torch(lowerCamelCase__ ) for k, v in x_in.items()} elif torch.is_tensor(lowerCamelCase__ ): return x_in.to(self.unet.device ) return torch.tensor(lowerCamelCase__ , device=self.unet.device ) def __a ( self :Union[str, Any] , lowerCamelCase__ :List[Any] , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Tuple ): for key, val in cond.items(): UpperCamelCase__ :str = val.clone() return x_in def __a ( self :Union[str, Any] , lowerCamelCase__ :List[Any] , lowerCamelCase__ :int , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Optional[int] ): UpperCamelCase__ :Any = x.shape[0] UpperCamelCase__ :List[Any] = None for i in tqdm.tqdm(self.scheduler.timesteps ): # create batch of timesteps to pass into model UpperCamelCase__ :Optional[Any] = torch.full((batch_size,) , lowerCamelCase__ , device=self.unet.device , dtype=torch.long ) for _ in range(lowerCamelCase__ ): with torch.enable_grad(): x.requires_grad_() # permute to match dimension for pre-trained models UpperCamelCase__ :Dict = self.value_function(x.permute(0 , 2 , 1 ) , lowerCamelCase__ ).sample UpperCamelCase__ :List[Any] = torch.autograd.grad([y.sum()] , [x] )[0] UpperCamelCase__ :Union[str, Any] = self.scheduler._get_variance(lowerCamelCase__ ) UpperCamelCase__ :Any = torch.exp(0.5 * posterior_variance ) UpperCamelCase__ :Dict = model_std * grad UpperCamelCase__ :Optional[Any] = 0 UpperCamelCase__ :Dict = x.detach() UpperCamelCase__ :int = x + scale * grad UpperCamelCase__ :int = self.reset_xa(lowerCamelCase__ , lowerCamelCase__ , self.action_dim ) UpperCamelCase__ :List[str] = self.unet(x.permute(0 , 2 , 1 ) , lowerCamelCase__ ).sample.permute(0 , 2 , 1 ) # TODO: verify deprecation of this kwarg UpperCamelCase__ :List[str] = self.scheduler.step(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , predict_epsilon=lowerCamelCase__ )["""prev_sample"""] # apply conditions to the trajectory (set the initial state) UpperCamelCase__ :Optional[Any] = self.reset_xa(lowerCamelCase__ , lowerCamelCase__ , self.action_dim ) UpperCamelCase__ :Optional[int] = self.to_torch(lowerCamelCase__ ) return x, y def __call__( self :Optional[Any] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :str=64 , lowerCamelCase__ :Tuple=32 , lowerCamelCase__ :Dict=2 , lowerCamelCase__ :str=0.1 ): # normalize the observations and create batch dimension UpperCamelCase__ :List[str] = self.normalize(lowerCamelCase__ , """observations""" ) UpperCamelCase__ :List[str] = obs[None].repeat(lowerCamelCase__ , axis=0 ) UpperCamelCase__ :int = {0: self.to_torch(lowerCamelCase__ )} UpperCamelCase__ :Dict = (batch_size, planning_horizon, self.state_dim + self.action_dim) # generate initial noise and apply our conditions (to make the trajectories start at current state) UpperCamelCase__ :Any = randn_tensor(lowerCamelCase__ , device=self.unet.device ) UpperCamelCase__ :Optional[int] = self.reset_xa(lowerCamelCase__ , lowerCamelCase__ , self.action_dim ) UpperCamelCase__ :List[Any] = self.to_torch(lowerCamelCase__ ) # run the diffusion process UpperCamelCase__ , UpperCamelCase__ :Union[str, Any] = self.run_diffusion(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) # sort output trajectories by value UpperCamelCase__ :List[Any] = y.argsort(0 , descending=lowerCamelCase__ ).squeeze() UpperCamelCase__ :Dict = x[sorted_idx] UpperCamelCase__ :Tuple = sorted_values[:, :, : self.action_dim] UpperCamelCase__ :Optional[Any] = actions.detach().cpu().numpy() UpperCamelCase__ :Optional[int] = self.de_normalize(lowerCamelCase__ , key="""actions""" ) # select the action with the highest value if y is not None: UpperCamelCase__ :List[str] = 0 else: # if we didn't run value guiding, select a random action UpperCamelCase__ :Dict = np.random.randint(0 , lowerCamelCase__ ) UpperCamelCase__ :Tuple = denorm_actions[selected_index, 0] return denorm_actions
45
0
'''simple docstring''' import math def __UpperCAmelCase ( _UpperCAmelCase : list , _UpperCAmelCase : int = 0 , _UpperCAmelCase : int = 0 ) -> list: __snake_case = end or len(_UpperCAmelCase ) for i in range(_UpperCAmelCase , _UpperCAmelCase ): __snake_case = i __snake_case = array[i] while temp_index != start and temp_index_value < array[temp_index - 1]: __snake_case = array[temp_index - 1] temp_index -= 1 __snake_case = temp_index_value return array def __UpperCAmelCase ( _UpperCAmelCase : list , _UpperCAmelCase : int , _UpperCAmelCase : int ) -> None: # Max Heap __snake_case = index __snake_case = 2 * index + 1 # Left Node __snake_case = 2 * index + 2 # Right Node if left_index < heap_size and array[largest] < array[left_index]: __snake_case = left_index if right_index < heap_size and array[largest] < array[right_index]: __snake_case = right_index if largest != index: __snake_case , __snake_case = array[largest], array[index] heapify(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) def __UpperCAmelCase ( _UpperCAmelCase : list ) -> list: __snake_case = len(_UpperCAmelCase ) for i in range(n // 2 , -1 , -1 ): heapify(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) for i in range(n - 1 , 0 , -1 ): __snake_case , __snake_case = array[0], array[i] heapify(_UpperCAmelCase , 0 , _UpperCAmelCase ) return array def __UpperCAmelCase ( _UpperCAmelCase : list , _UpperCAmelCase : int , _UpperCAmelCase : int , _UpperCAmelCase : int ) -> int: if (array[first_index] > array[middle_index]) != ( array[first_index] > array[last_index] ): return array[first_index] elif (array[middle_index] > array[first_index]) != ( array[middle_index] > array[last_index] ): return array[middle_index] else: return array[last_index] def __UpperCAmelCase ( _UpperCAmelCase : list , _UpperCAmelCase : int , _UpperCAmelCase : int , _UpperCAmelCase : int ) -> int: __snake_case = low __snake_case = high while True: while array[i] < pivot: i += 1 j -= 1 while pivot < array[j]: j -= 1 if i >= j: return i __snake_case , __snake_case = array[j], array[i] i += 1 def __UpperCAmelCase ( _UpperCAmelCase : list ) -> list: if len(_UpperCAmelCase ) == 0: return array __snake_case = 2 * math.ceil(math.loga(len(_UpperCAmelCase ) ) ) __snake_case = 16 return intro_sort(_UpperCAmelCase , 0 , len(_UpperCAmelCase ) , _UpperCAmelCase , _UpperCAmelCase ) def __UpperCAmelCase ( _UpperCAmelCase : list , _UpperCAmelCase : int , _UpperCAmelCase : int , _UpperCAmelCase : int , _UpperCAmelCase : int ) -> list: while end - start > size_threshold: if max_depth == 0: return heap_sort(_UpperCAmelCase ) max_depth -= 1 __snake_case = median_of_a(_UpperCAmelCase , _UpperCAmelCase , start + ((end - start) // 2) + 1 , end - 1 ) __snake_case = partition(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) intro_sort(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) __snake_case = p return insertion_sort(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) if __name__ == "__main__": import doctest doctest.testmod() a : Dict = input('''Enter numbers separated by a comma : ''').strip() a : str = [float(item) for item in user_input.split(''',''')] print(sort(unsorted))
69
def A ( lowercase__ : int ) -> bool: if num < 0: return False UpperCamelCase__ :int = num UpperCamelCase__ :int = 0 while num > 0: UpperCamelCase__ :Optional[int] = rev_num * 10 + (num % 10) num //= 10 return num_copy == rev_num if __name__ == "__main__": import doctest doctest.testmod()
45
0
from PIL import Image def _SCREAMING_SNAKE_CASE ( lowercase : Image ): '''simple docstring''' lowerCamelCase_ , lowerCamelCase_ = image.size lowerCamelCase_ = 0 lowerCamelCase_ = image.load() for i in range(lowercase ): for j in range(lowercase ): lowerCamelCase_ = pixels[j, i] mean += pixel mean //= width * height for j in range(lowercase ): for i in range(lowercase ): lowerCamelCase_ = 2_55 if pixels[i, j] > mean else 0 return image if __name__ == "__main__": lowerCamelCase : Optional[Any] = mean_threshold(Image.open("path_to_image").convert("L")) image.save("output_image_path")
70
from __future__ import annotations def A ( lowercase__ : list[int] ) -> bool: return len(set(lowercase__ ) ) == len(lowercase__ ) if __name__ == "__main__": import doctest doctest.testmod()
45
0
'''simple docstring''' import json import os import re import sys import urllib.request import requests from bsa import BeautifulSoup _lowerCamelCase = { """User-Agent""": """Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36""" """ (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582""" } def a__ ( _SCREAMING_SNAKE_CASE : str = "dhaka" , _SCREAMING_SNAKE_CASE : int = 5 ) -> int: """simple docstring""" UpperCAmelCase_ : Any = min(_SCREAMING_SNAKE_CASE , 50 ) # Prevent abuse! UpperCAmelCase_ : Optional[int] = { "q": query, "tbm": "isch", "hl": "en", "ijn": "0", } UpperCAmelCase_ : int = requests.get("https://www.google.com/search" , params=_SCREAMING_SNAKE_CASE , headers=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[Any] = BeautifulSoup(html.text , "html.parser" ) UpperCAmelCase_ : Optional[int] = "".join( re.findall(r"AF_initDataCallback\(([^<]+)\);" , str(soup.select("script" ) ) ) ) UpperCAmelCase_ : List[str] = json.dumps(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[int] = json.loads(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Dict = re.findall( r"\[\"GRID_STATE0\",null,\[\[1,\[0,\".*?\",(.*),\"All\"," , _SCREAMING_SNAKE_CASE , ) if not matched_google_image_data: return 0 UpperCAmelCase_ : Optional[Any] = re.sub( r"\[\"(https\:\/\/encrypted-tbn0\.gstatic\.com\/images\?.*?)\",\d+,\d+\]" , "" , str(_SCREAMING_SNAKE_CASE ) , ) UpperCAmelCase_ : Dict = re.findall( r"(?:'|,),\[\"(https:|http.*?)\",\d+,\d+\]" , _SCREAMING_SNAKE_CASE , ) for index, fixed_full_res_image in enumerate(_SCREAMING_SNAKE_CASE ): if index >= max_images: return index UpperCAmelCase_ : str = bytes(_SCREAMING_SNAKE_CASE , "ascii" ).decode( "unicode-escape" ) UpperCAmelCase_ : Any = bytes(_SCREAMING_SNAKE_CASE , "ascii" ).decode( "unicode-escape" ) UpperCAmelCase_ : int = urllib.request.build_opener() UpperCAmelCase_ : Dict = [ ( "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" " (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582", ) ] urllib.request.install_opener(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Union[str, Any] = F'''query_{query.replace(" " , "_" )}''' if not os.path.exists(_SCREAMING_SNAKE_CASE ): os.makedirs(_SCREAMING_SNAKE_CASE ) urllib.request.urlretrieve( # noqa: S310 _SCREAMING_SNAKE_CASE , F'''{path_name}/original_size_img_{index}.jpg''' ) return index if __name__ == "__main__": try: _lowerCamelCase = download_images_from_google_query(sys.argv[1]) print(f"""{image_count} images were downloaded to disk.""") except IndexError: print("""Please provide a search term.""") raise
71
from __future__ import annotations class lowerCAmelCase_ : """simple docstring""" def __init__( self :List[Any] , lowerCamelCase__ :int = 0 ): UpperCamelCase__ :List[str] = key def __a ( self :Optional[Any] , lowerCamelCase__ :str , lowerCamelCase__ :int ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__ :List[str] = key or self.__key or 1 # make sure key is an appropriate size key %= 2_55 return [chr(ord(lowerCamelCase__ ) ^ key ) for ch in content] def __a ( self :int , lowerCamelCase__ :str , lowerCamelCase__ :int ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__ :int = key or self.__key or 1 # make sure key is an appropriate size key %= 2_55 return [chr(ord(lowerCamelCase__ ) ^ key ) for ch in content] def __a ( self :Optional[Any] , lowerCamelCase__ :str , lowerCamelCase__ :int = 0 ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__ :Dict = key or self.__key or 1 # make sure key can be any size while key > 2_55: key -= 2_55 # This will be returned UpperCamelCase__ :List[str] = """""" for ch in content: ans += chr(ord(lowerCamelCase__ ) ^ key ) return ans def __a ( self :Any , lowerCamelCase__ :str , lowerCamelCase__ :int = 0 ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__ :Tuple = key or self.__key or 1 # make sure key can be any size while key > 2_55: key -= 2_55 # This will be returned UpperCamelCase__ :Optional[int] = """""" for ch in content: ans += chr(ord(lowerCamelCase__ ) ^ key ) return ans def __a ( self :Optional[Any] , lowerCamelCase__ :str , lowerCamelCase__ :int = 0 ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) try: with open(lowerCamelCase__ ) as fin, open("""encrypt.out""" , """w+""" ) as fout: # actual encrypt-process for line in fin: fout.write(self.encrypt_string(lowerCamelCase__ , lowerCamelCase__ ) ) except OSError: return False return True def __a ( self :Dict , lowerCamelCase__ :str , lowerCamelCase__ :int ): assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ) try: with open(lowerCamelCase__ ) as fin, open("""decrypt.out""" , """w+""" ) as fout: # actual encrypt-process for line in fin: fout.write(self.decrypt_string(lowerCamelCase__ , lowerCamelCase__ ) ) except OSError: return False return True # Tests # crypt = XORCipher() # key = 67 # # test encrypt # print(crypt.encrypt("hallo welt",key)) # # test decrypt # print(crypt.decrypt(crypt.encrypt("hallo welt",key), key)) # # test encrypt_string # print(crypt.encrypt_string("hallo welt",key)) # # test decrypt_string # print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key)) # if (crypt.encrypt_file("test.txt",key)): # print("encrypt successful") # else: # print("encrypt unsuccessful") # if (crypt.decrypt_file("encrypt.out",key)): # print("decrypt successful") # else: # print("decrypt unsuccessful")
45
0
'''simple docstring''' import copy from ...configuration_utils import PretrainedConfig from ...utils import logging _UpperCAmelCase : Optional[Any] = logging.get_logger(__name__) class __magic_name__ ( __SCREAMING_SNAKE_CASE ): UpperCamelCase__ = 'encoder-decoder' UpperCamelCase__ = True def __init__( self , **snake_case_ ): super().__init__(**snake_case_ ) assert ( "encoder" in kwargs and "decoder" in kwargs ), "Config has to be initialized with encoder and decoder config" lowercase =kwargs.pop('''encoder''' ) lowercase =encoder_config.pop('''model_type''' ) lowercase =kwargs.pop('''decoder''' ) lowercase =decoder_config.pop('''model_type''' ) from ..auto.configuration_auto import AutoConfig lowercase =AutoConfig.for_model(snake_case_ , **snake_case_ ) lowercase =AutoConfig.for_model(snake_case_ , **snake_case_ ) lowercase =True @classmethod def _A( cls , snake_case_ , snake_case_ , **snake_case_ ): logger.info('''Set `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config''' ) lowercase =True lowercase =True return cls(encoder=encoder_config.to_dict() , decoder=decoder_config.to_dict() , **snake_case_ ) def _A( self ): lowercase =copy.deepcopy(self.__dict__ ) lowercase =self.encoder.to_dict() lowercase =self.decoder.to_dict() lowercase =self.__class__.model_type return output
72
import random def A ( lowercase__ : Dict , lowercase__ : str , lowercase__ : Optional[Any] ) -> int: UpperCamelCase__ :List[Any] = a[left_index] UpperCamelCase__ :Dict = left_index + 1 for j in range(left_index + 1 , lowercase__ ): if a[j] < pivot: UpperCamelCase__ , UpperCamelCase__ :Optional[int] = a[i], a[j] i += 1 UpperCamelCase__ , UpperCamelCase__ :Tuple = a[i - 1], a[left_index] return i - 1 def A ( lowercase__ : Tuple , lowercase__ : Optional[int] , lowercase__ : Any ) -> Optional[int]: if left < right: UpperCamelCase__ :List[Any] = random.randint(lowercase__ , right - 1 ) UpperCamelCase__ , UpperCamelCase__ :Union[str, Any] = ( a[left], a[pivot], ) # switches the pivot with the left most bound UpperCamelCase__ :int = partition(lowercase__ , lowercase__ , lowercase__ ) quick_sort_random( lowercase__ , lowercase__ , lowercase__ ) # recursive quicksort to the left of the pivot point quick_sort_random( lowercase__ , pivot_index + 1 , lowercase__ ) # recursive quicksort to the right of the pivot point def A ( ) -> List[Any]: UpperCamelCase__ :str = input("""Enter numbers separated by a comma:\n""" ).strip() UpperCamelCase__ :int = [int(lowercase__ ) for item in user_input.split(""",""" )] quick_sort_random(lowercase__ , 0 , len(lowercase__ ) ) print(lowercase__ ) if __name__ == "__main__": main()
45
0
import argparse import requests import torch # pip3 install salesforce-lavis # I'm actually installing a slightly modified version: pip3 install git+https://github.com/nielsrogge/LAVIS.git@fix_lavis from lavis.models import load_model_and_preprocess from PIL import Image from transformers import ( AutoTokenizer, BlipaConfig, BlipaForConditionalGeneration, BlipaProcessor, BlipaVisionConfig, BlipImageProcessor, OPTConfig, TaConfig, ) from transformers.utils.constants import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD def lowerCamelCase__ (): SCREAMING_SNAKE_CASE = 'https://storage.googleapis.com/sfr-vision-language-research/LAVIS/assets/merlion.png' SCREAMING_SNAKE_CASE = Image.open(requests.get(_UpperCAmelCase , stream=_UpperCAmelCase).raw).convert('RGB') return image def lowerCamelCase__ (_UpperCAmelCase): SCREAMING_SNAKE_CASE = [] # fmt: off # vision encoder rename_keys.append(('visual_encoder.cls_token', 'vision_model.embeddings.class_embedding')) rename_keys.append(('visual_encoder.pos_embed', 'vision_model.embeddings.position_embedding')) rename_keys.append(('visual_encoder.patch_embed.proj.weight', 'vision_model.embeddings.patch_embedding.weight')) rename_keys.append(('visual_encoder.patch_embed.proj.bias', 'vision_model.embeddings.patch_embedding.bias')) rename_keys.append(('ln_vision.weight', 'vision_model.post_layernorm.weight')) rename_keys.append(('ln_vision.bias', 'vision_model.post_layernorm.bias')) for i in range(config.vision_config.num_hidden_layers): rename_keys.append((F'''visual_encoder.blocks.{i}.norm1.weight''', F'''vision_model.encoder.layers.{i}.layer_norm1.weight''')) rename_keys.append((F'''visual_encoder.blocks.{i}.norm1.bias''', F'''vision_model.encoder.layers.{i}.layer_norm1.bias''')) rename_keys.append((F'''visual_encoder.blocks.{i}.norm2.weight''', F'''vision_model.encoder.layers.{i}.layer_norm2.weight''')) rename_keys.append((F'''visual_encoder.blocks.{i}.norm2.bias''', F'''vision_model.encoder.layers.{i}.layer_norm2.bias''')) rename_keys.append((F'''visual_encoder.blocks.{i}.attn.qkv.weight''', F'''vision_model.encoder.layers.{i}.self_attn.qkv.weight''')) rename_keys.append((F'''visual_encoder.blocks.{i}.attn.proj.weight''', F'''vision_model.encoder.layers.{i}.self_attn.projection.weight''',)) rename_keys.append((F'''visual_encoder.blocks.{i}.attn.proj.bias''', F'''vision_model.encoder.layers.{i}.self_attn.projection.bias''')) rename_keys.append((F'''visual_encoder.blocks.{i}.mlp.fc1.weight''', F'''vision_model.encoder.layers.{i}.mlp.fc1.weight''')) rename_keys.append((F'''visual_encoder.blocks.{i}.mlp.fc1.bias''', F'''vision_model.encoder.layers.{i}.mlp.fc1.bias''')) rename_keys.append((F'''visual_encoder.blocks.{i}.mlp.fc2.weight''', F'''vision_model.encoder.layers.{i}.mlp.fc2.weight''')) rename_keys.append((F'''visual_encoder.blocks.{i}.mlp.fc2.bias''', F'''vision_model.encoder.layers.{i}.mlp.fc2.bias''')) # QFormer rename_keys.append(('Qformer.bert.embeddings.LayerNorm.weight', 'qformer.layernorm.weight')) rename_keys.append(('Qformer.bert.embeddings.LayerNorm.bias', 'qformer.layernorm.bias')) # fmt: on return rename_keys def lowerCamelCase__ (_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase): SCREAMING_SNAKE_CASE = dct.pop(_UpperCAmelCase) SCREAMING_SNAKE_CASE = val def lowerCamelCase__ (_UpperCAmelCase , _UpperCAmelCase): for i in range(config.vision_config.num_hidden_layers): # read in original q and v biases SCREAMING_SNAKE_CASE = state_dict.pop(F'''visual_encoder.blocks.{i}.attn.q_bias''') SCREAMING_SNAKE_CASE = state_dict.pop(F'''visual_encoder.blocks.{i}.attn.v_bias''') # next, set bias in the state dict SCREAMING_SNAKE_CASE = torch.cat((q_bias, torch.zeros_like(_UpperCAmelCase , requires_grad=_UpperCAmelCase), v_bias)) SCREAMING_SNAKE_CASE = qkv_bias def lowerCamelCase__ (_UpperCAmelCase , _UpperCAmelCase): SCREAMING_SNAKE_CASE = 364 if 'coco' in model_name else 224 SCREAMING_SNAKE_CASE = BlipaVisionConfig(image_size=_UpperCAmelCase).to_dict() # make sure the models have proper bos_token_id and eos_token_id set (important for generation) # seems like flan-T5 models don't have bos_token_id properly set? if "opt-2.7b" in model_name: SCREAMING_SNAKE_CASE = OPTConfig.from_pretrained('facebook/opt-2.7b' , eos_token_id=_UpperCAmelCase).to_dict() elif "opt-6.7b" in model_name: SCREAMING_SNAKE_CASE = OPTConfig.from_pretrained('facebook/opt-6.7b' , eos_token_id=_UpperCAmelCase).to_dict() elif "t5-xl" in model_name: SCREAMING_SNAKE_CASE = TaConfig.from_pretrained('google/flan-t5-xl' , dense_act_fn='gelu' , bos_token_id=1).to_dict() elif "t5-xxl" in model_name: SCREAMING_SNAKE_CASE = TaConfig.from_pretrained('google/flan-t5-xxl' , dense_act_fn='gelu' , bos_token_id=1).to_dict() SCREAMING_SNAKE_CASE = BlipaConfig(vision_config=_UpperCAmelCase , text_config=_UpperCAmelCase) return config, image_size @torch.no_grad() def lowerCamelCase__ (_UpperCAmelCase , _UpperCAmelCase=None , _UpperCAmelCase=False): SCREAMING_SNAKE_CASE = ( AutoTokenizer.from_pretrained('facebook/opt-2.7b') if 'opt' in model_name else AutoTokenizer.from_pretrained('google/flan-t5-xl') ) SCREAMING_SNAKE_CASE = tokenizer('\n' , add_special_tokens=_UpperCAmelCase).input_ids[0] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = get_blipa_config(_UpperCAmelCase , eos_token_id=_UpperCAmelCase) SCREAMING_SNAKE_CASE = BlipaForConditionalGeneration(_UpperCAmelCase).eval() SCREAMING_SNAKE_CASE = { 'blip2-opt-2.7b': ('blip2_opt', 'pretrain_opt2.7b'), 'blip2-opt-6.7b': ('blip2_opt', 'pretrain_opt6.7b'), 'blip2-opt-2.7b-coco': ('blip2_opt', 'caption_coco_opt2.7b'), 'blip2-opt-6.7b-coco': ('blip2_opt', 'caption_coco_opt6.7b'), 'blip2-flan-t5-xl': ('blip2_t5', 'pretrain_flant5xl'), 'blip2-flan-t5-xl-coco': ('blip2_t5', 'caption_coco_flant5xl'), 'blip2-flan-t5-xxl': ('blip2_t5', 'pretrain_flant5xxl'), } SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = model_name_to_original[model_name] # load original model print('Loading original model...') SCREAMING_SNAKE_CASE = 'cuda' if torch.cuda.is_available() else 'cpu' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = load_model_and_preprocess( name=_UpperCAmelCase , model_type=_UpperCAmelCase , is_eval=_UpperCAmelCase , device=_UpperCAmelCase) original_model.eval() print('Done!') # update state dict keys SCREAMING_SNAKE_CASE = original_model.state_dict() SCREAMING_SNAKE_CASE = create_rename_keys(_UpperCAmelCase) for src, dest in rename_keys: rename_key(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): SCREAMING_SNAKE_CASE = state_dict.pop(_UpperCAmelCase) if key.startswith('Qformer.bert'): SCREAMING_SNAKE_CASE = key.replace('Qformer.bert' , 'qformer') if "attention.self" in key: SCREAMING_SNAKE_CASE = key.replace('self' , 'attention') if "opt_proj" in key: SCREAMING_SNAKE_CASE = key.replace('opt_proj' , 'language_projection') if "t5_proj" in key: SCREAMING_SNAKE_CASE = key.replace('t5_proj' , 'language_projection') if key.startswith('opt'): SCREAMING_SNAKE_CASE = key.replace('opt' , 'language') if key.startswith('t5'): SCREAMING_SNAKE_CASE = key.replace('t5' , 'language') SCREAMING_SNAKE_CASE = val # read in qv biases read_in_q_v_bias(_UpperCAmelCase , _UpperCAmelCase) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = hf_model.load_state_dict(_UpperCAmelCase , strict=_UpperCAmelCase) assert len(_UpperCAmelCase) == 0 assert unexpected_keys == ["qformer.embeddings.position_ids"] SCREAMING_SNAKE_CASE = load_demo_image() SCREAMING_SNAKE_CASE = vis_processors['eval'](_UpperCAmelCase).unsqueeze(0).to(_UpperCAmelCase) SCREAMING_SNAKE_CASE = tokenizer(['\n'] , return_tensors='pt').input_ids.to(_UpperCAmelCase) # create processor SCREAMING_SNAKE_CASE = BlipImageProcessor( size={'height': image_size, 'width': image_size} , image_mean=_UpperCAmelCase , image_std=_UpperCAmelCase) SCREAMING_SNAKE_CASE = BlipaProcessor(image_processor=_UpperCAmelCase , tokenizer=_UpperCAmelCase) SCREAMING_SNAKE_CASE = processor(images=_UpperCAmelCase , return_tensors='pt').pixel_values.to(_UpperCAmelCase) # make sure processor creates exact same pixel values assert torch.allclose(_UpperCAmelCase , _UpperCAmelCase) original_model.to(_UpperCAmelCase) hf_model.to(_UpperCAmelCase) with torch.no_grad(): if "opt" in model_name: SCREAMING_SNAKE_CASE = original_model({'image': original_pixel_values, 'text_input': ['']}).logits SCREAMING_SNAKE_CASE = hf_model(_UpperCAmelCase , _UpperCAmelCase).logits else: SCREAMING_SNAKE_CASE = original_model( {'image': original_pixel_values, 'text_input': ['\n'], 'text_output': ['\n']}).logits SCREAMING_SNAKE_CASE = input_ids.masked_fill(input_ids == tokenizer.pad_token_id , -100) SCREAMING_SNAKE_CASE = hf_model(_UpperCAmelCase , _UpperCAmelCase , labels=_UpperCAmelCase).logits assert original_logits.shape == logits.shape print('First values of original logits:' , original_logits[0, :3, :3]) print('First values of HF logits:' , logits[0, :3, :3]) # assert values if model_name == "blip2-flan-t5-xl": SCREAMING_SNAKE_CASE = torch.tensor( [[-41.58_50, -4.44_40, -8.99_22], [-47.43_22, -5.91_43, -1.73_40]] , device=_UpperCAmelCase) assert torch.allclose(logits[0, :3, :3] , _UpperCAmelCase , atol=1e-4) elif model_name == "blip2-flan-t5-xl-coco": SCREAMING_SNAKE_CASE = torch.tensor( [[-57.01_09, -9.89_67, -12.62_80], [-68.65_78, -12.71_91, -10.50_65]] , device=_UpperCAmelCase) else: # cast to same type SCREAMING_SNAKE_CASE = logits.dtype assert torch.allclose(original_logits.to(_UpperCAmelCase) , _UpperCAmelCase , atol=1e-2) print('Looks ok!') print('Generating a caption...') SCREAMING_SNAKE_CASE = '' SCREAMING_SNAKE_CASE = tokenizer(_UpperCAmelCase , return_tensors='pt').input_ids.to(_UpperCAmelCase) SCREAMING_SNAKE_CASE = original_model.generate({'image': original_pixel_values}) SCREAMING_SNAKE_CASE = hf_model.generate( _UpperCAmelCase , _UpperCAmelCase , do_sample=_UpperCAmelCase , num_beams=5 , max_length=30 , min_length=1 , top_p=0.9 , repetition_penalty=1.0 , length_penalty=1.0 , temperature=1 , ) print('Original generation:' , _UpperCAmelCase) SCREAMING_SNAKE_CASE = input_ids.shape[1] SCREAMING_SNAKE_CASE = processor.batch_decode(outputs[:, prompt_length:] , skip_special_tokens=_UpperCAmelCase) SCREAMING_SNAKE_CASE = [text.strip() for text in output_text] print('HF generation:' , _UpperCAmelCase) if pytorch_dump_folder_path is not None: processor.save_pretrained(_UpperCAmelCase) hf_model.save_pretrained(_UpperCAmelCase) if push_to_hub: processor.push_to_hub(F'''nielsr/{model_name}''') hf_model.push_to_hub(F'''nielsr/{model_name}''') if __name__ == "__main__": a_ : str = argparse.ArgumentParser() a_ : Optional[int] = [ 'blip2-opt-2.7b', 'blip2-opt-6.7b', 'blip2-opt-2.7b-coco', 'blip2-opt-6.7b-coco', 'blip2-flan-t5-xl', 'blip2-flan-t5-xl-coco', 'blip2-flan-t5-xxl', ] parser.add_argument( '--model_name', default='blip2-opt-2.7b', choices=choices, type=str, help='Path to hf config.json of model to convert', ) parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument( '--push_to_hub', action='store_true', help='Whether to push the model and processor to the hub after converting', ) a_ : int = parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
73
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { "shi-labs/dinat-mini-in1k-224": "https://huggingface.co/shi-labs/dinat-mini-in1k-224/resolve/main/config.json", # See all Dinat models at https://huggingface.co/models?filter=dinat } class lowerCAmelCase_ ( lowercase , lowercase ): """simple docstring""" _snake_case : Tuple = """dinat""" _snake_case : List[Any] = { """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers""", } def __init__( self :Optional[int] , lowerCamelCase__ :int=4 , lowerCamelCase__ :Union[str, Any]=3 , lowerCamelCase__ :List[Any]=64 , lowerCamelCase__ :Any=[3, 4, 6, 5] , lowerCamelCase__ :Tuple=[2, 4, 8, 16] , lowerCamelCase__ :Optional[int]=7 , lowerCamelCase__ :Tuple=[[1, 8, 1], [1, 4, 1, 4], [1, 2, 1, 2, 1, 2], [1, 1, 1, 1, 1]] , lowerCamelCase__ :Tuple=3.0 , lowerCamelCase__ :str=True , lowerCamelCase__ :Optional[int]=0.0 , lowerCamelCase__ :Optional[Any]=0.0 , lowerCamelCase__ :int=0.1 , lowerCamelCase__ :Optional[Any]="gelu" , lowerCamelCase__ :Optional[Any]=0.02 , lowerCamelCase__ :Union[str, Any]=1e-5 , lowerCamelCase__ :Optional[int]=0.0 , lowerCamelCase__ :List[str]=None , lowerCamelCase__ :str=None , **lowerCamelCase__ :List[Any] , ): super().__init__(**lowerCamelCase__ ) UpperCamelCase__ :Any = patch_size UpperCamelCase__ :Any = num_channels UpperCamelCase__ :int = embed_dim UpperCamelCase__ :Optional[Any] = depths UpperCamelCase__ :Any = len(lowerCamelCase__ ) UpperCamelCase__ :str = num_heads UpperCamelCase__ :Optional[int] = kernel_size UpperCamelCase__ :Optional[int] = dilations UpperCamelCase__ :Tuple = mlp_ratio UpperCamelCase__ :Dict = qkv_bias UpperCamelCase__ :List[str] = hidden_dropout_prob UpperCamelCase__ :List[str] = attention_probs_dropout_prob UpperCamelCase__ :Union[str, Any] = drop_path_rate UpperCamelCase__ :Tuple = hidden_act UpperCamelCase__ :List[Any] = layer_norm_eps UpperCamelCase__ :Optional[Any] = initializer_range # we set the hidden_size attribute in order to make Dinat work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model UpperCamelCase__ :Tuple = int(embed_dim * 2 ** (len(lowerCamelCase__ ) - 1) ) UpperCamelCase__ :Tuple = layer_scale_init_value UpperCamelCase__ :Optional[int] = ["""stem"""] + [f"""stage{idx}""" for idx in range(1 , len(lowerCamelCase__ ) + 1 )] UpperCamelCase__ , UpperCamelCase__ :List[str] = get_aligned_output_features_output_indices( out_features=lowerCamelCase__ , out_indices=lowerCamelCase__ , stage_names=self.stage_names )
45
0
def a__ ( snake_case ): """simple docstring""" if n == 1 or not isinstance(snake_case , snake_case ): return 0 elif n == 2: return 1 else: __SCREAMING_SNAKE_CASE : Any = [0, 1] for i in range(2 , n + 1 ): sequence.append(sequence[i - 1] + sequence[i - 2] ) return sequence[n] def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Any = 0 __SCREAMING_SNAKE_CASE : Optional[int] = 2 while digits < n: index += 1 __SCREAMING_SNAKE_CASE : List[Any] = len(str(fibonacci(snake_case ) ) ) return index def a__ ( snake_case = 1_000 ): """simple docstring""" return fibonacci_digits_index(snake_case ) if __name__ == "__main__": print(solution(int(str(input()).strip())))
74
def A ( lowercase__ : int , lowercase__ : int ) -> int: return int(input_a == input_a == 0 ) def A ( ) -> None: print("""Truth Table of NOR Gate:""" ) print("""| Input 1 | Input 2 | Output |""" ) print(f"""| 0 | 0 | {nor_gate(0 , 0 )} |""" ) print(f"""| 0 | 1 | {nor_gate(0 , 1 )} |""" ) print(f"""| 1 | 0 | {nor_gate(1 , 0 )} |""" ) print(f"""| 1 | 1 | {nor_gate(1 , 1 )} |""" ) if __name__ == "__main__": import doctest doctest.testmod() main()
45
0
'''simple docstring''' from __future__ import annotations class lowerCamelCase_ : def __init__( self : int , _A : list[list[int]] ): '''simple docstring''' UpperCAmelCase__ : Tuple = 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__ : Any = 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__ : Union[str, Any] = rows else: UpperCAmelCase__ : str = [] def lowercase_ ( self : Optional[Any] ): '''simple docstring''' return [[row[i] for row in self.rows] for i in range(len(self.rows[0] ) )] @property def lowercase_ ( self : Optional[int] ): '''simple docstring''' return len(self.rows ) @property def lowercase_ ( self : int ): '''simple docstring''' return len(self.rows[0] ) @property def lowercase_ ( self : List[Any] ): '''simple docstring''' return (self.num_rows, self.num_columns) @property def lowercase_ ( self : List[Any] ): '''simple docstring''' return self.order[0] == self.order[1] def lowercase_ ( self : Optional[Any] ): '''simple docstring''' UpperCAmelCase__ : str = [ [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 lowercase_ ( self : Any ): '''simple docstring''' 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 lowercase_ ( self : Dict ): '''simple docstring''' return bool(self.determinant() ) def lowercase_ ( self : int , _A : int , _A : int ): '''simple docstring''' UpperCAmelCase__ : Union[str, Any] = [ [ 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 lowercase_ ( self : Union[str, Any] , _A : int , _A : int ): '''simple docstring''' if (row + column) % 2 == 0: return self.get_minor(_A , _A ) return -1 * self.get_minor(_A , _A ) def lowercase_ ( self : int ): '''simple docstring''' return Matrix( [ [self.get_minor(_A , _A ) for column in range(self.num_columns )] for row in range(self.num_rows ) ] ) def lowercase_ ( self : Optional[Any] ): '''simple docstring''' 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 lowercase_ ( self : Optional[Any] ): '''simple docstring''' UpperCAmelCase__ : Tuple = [ [self.cofactors().rows[column][row] for column in range(self.num_columns )] for row in range(self.num_rows ) ] return Matrix(_A ) def lowercase_ ( self : List[Any] ): '''simple docstring''' UpperCAmelCase__ : Union[str, Any] = 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 : int ): '''simple docstring''' return str(self.rows ) def __str__( self : Any ): '''simple docstring''' 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 lowercase_ ( self : Tuple , _A : list[int] , _A : int | None = None ): '''simple docstring''' UpperCAmelCase__ : Optional[Any] = 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__ : List[Any] = self.rows[0:position] + [row] + self.rows[position:] def lowercase_ ( self : Any , _A : list[int] , _A : int | None = None ): '''simple docstring''' UpperCAmelCase__ : List[str] = 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__ : Dict = [self.rows[i] + [column[i]] for i in range(self.num_rows )] else: UpperCAmelCase__ : Union[str, Any] = [ self.rows[i][0:position] + [column[i]] + self.rows[i][position:] for i in range(self.num_rows ) ] def __eq__( self : Optional[int] , _A : object ): '''simple docstring''' if not isinstance(_A , _A ): return NotImplemented return self.rows == other.rows def __ne__( self : Optional[int] , _A : object ): '''simple docstring''' return not self == other def __neg__( self : str ): '''simple docstring''' return self * -1 def __add__( self : Dict , _A : Matrix ): '''simple docstring''' 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 : int , _A : Matrix ): '''simple docstring''' 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 : Tuple , _A : Matrix | int | float ): '''simple docstring''' 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 : Tuple , _A : int ): '''simple docstring''' 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__ : List[str] = self for _ in range(other - 1 ): result *= self return result @classmethod def lowercase_ ( cls : str , _A : list[int] , _A : list[int] ): '''simple docstring''' return sum(row[i] * column[i] for i in range(len(_A ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
75
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import GLPNImageProcessor class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __init__( self :Any , lowerCamelCase__ :Dict , lowerCamelCase__ :Optional[Any]=7 , lowerCamelCase__ :str=3 , lowerCamelCase__ :Optional[Any]=18 , lowerCamelCase__ :List[str]=30 , lowerCamelCase__ :str=4_00 , lowerCamelCase__ :Optional[int]=True , lowerCamelCase__ :Union[str, Any]=32 , lowerCamelCase__ :int=True , ): UpperCamelCase__ :List[Any] = parent UpperCamelCase__ :List[Any] = batch_size UpperCamelCase__ :Any = num_channels UpperCamelCase__ :List[str] = image_size UpperCamelCase__ :Dict = min_resolution UpperCamelCase__ :List[str] = max_resolution UpperCamelCase__ :str = do_resize UpperCamelCase__ :int = size_divisor UpperCamelCase__ :Optional[int] = do_rescale def __a ( self :str ): return { "do_resize": self.do_resize, "size_divisor": self.size_divisor, "do_rescale": self.do_rescale, } @require_torch @require_vision class lowerCAmelCase_ ( lowercase , unittest.TestCase ): """simple docstring""" _snake_case : Optional[int] = GLPNImageProcessor if is_vision_available() else None def __a ( self :Dict ): UpperCamelCase__ :Dict = GLPNImageProcessingTester(self ) @property def __a ( self :List[str] ): return self.image_processor_tester.prepare_image_processor_dict() def __a ( self :Optional[int] ): UpperCamelCase__ :Optional[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(lowerCamelCase__ , """do_resize""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """size_divisor""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """resample""" ) ) self.assertTrue(hasattr(lowerCamelCase__ , """do_rescale""" ) ) def __a ( self :Optional[int] ): pass def __a ( self :Tuple ): # Initialize image_processing UpperCamelCase__ :int = self.image_processing_class(**self.image_processor_dict ) # create random PIL images UpperCamelCase__ :str = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCamelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCamelCase__ , Image.Image ) # Test not batched input (GLPNImageProcessor doesn't support batching) UpperCamelCase__ :Tuple = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 ) self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 ) def __a ( self :str ): # Initialize image_processing UpperCamelCase__ :str = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors UpperCamelCase__ :Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCamelCase__ , numpify=lowerCamelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCamelCase__ , np.ndarray ) # Test not batched input (GLPNImageProcessor doesn't support batching) UpperCamelCase__ :List[str] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 ) self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 ) def __a ( self :Any ): # Initialize image_processing UpperCamelCase__ :List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors UpperCamelCase__ :Tuple = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCamelCase__ , torchify=lowerCamelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCamelCase__ , torch.Tensor ) # Test not batched input (GLPNImageProcessor doesn't support batching) UpperCamelCase__ :List[str] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 ) self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 )
45
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging a_ = logging.get_logger(__name__) a_ = { 'microsoft/swinv2-tiny-patch4-window8-256': ( 'https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json' ), } class UpperCAmelCase_ ( snake_case ): UpperCamelCase ="swinv2" UpperCamelCase ={ "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self , UpperCamelCase_=2_24 , UpperCamelCase_=4 , UpperCamelCase_=3 , UpperCamelCase_=96 , UpperCamelCase_=[2, 2, 6, 2] , UpperCamelCase_=[3, 6, 12, 24] , UpperCamelCase_=7 , UpperCamelCase_=4.0 , UpperCamelCase_=True , UpperCamelCase_=0.0 , UpperCamelCase_=0.0 , UpperCamelCase_=0.1 , UpperCamelCase_="gelu" , UpperCamelCase_=False , UpperCamelCase_=0.0_2 , UpperCamelCase_=1E-5 , UpperCamelCase_=32 , **UpperCamelCase_ , ) -> List[Any]: super().__init__(**UpperCamelCase_ ) __lowercase : Union[str, Any] = image_size __lowercase : Optional[int] = patch_size __lowercase : int = num_channels __lowercase : List[str] = embed_dim __lowercase : List[Any] = depths __lowercase : List[str] = len(UpperCamelCase_ ) __lowercase : List[Any] = num_heads __lowercase : Optional[int] = window_size __lowercase : int = mlp_ratio __lowercase : Any = qkv_bias __lowercase : Optional[int] = hidden_dropout_prob __lowercase : Tuple = attention_probs_dropout_prob __lowercase : Optional[int] = drop_path_rate __lowercase : Tuple = hidden_act __lowercase : Tuple = use_absolute_embeddings __lowercase : Dict = layer_norm_eps __lowercase : Dict = initializer_range __lowercase : Optional[Any] = encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model __lowercase : Optional[int] = int(embed_dim * 2 ** (len(UpperCamelCase_ ) - 1) ) __lowercase : Union[str, Any] = (0, 0, 0, 0)
76
import math def A ( lowercase__ : Tuple , lowercase__ : Union[str, Any] ) -> Optional[Any]: if 0 not in (x, y): # We use the relation x^y = y*log10(x), where 10 is the base. return y * math.logaa(lowercase__ ) else: if x == 0: # 0 raised to any number is 0 return 0 elif y == 0: return 1 # any number raised to 0 is 1 raise AssertionError("""This should never happen""" ) if __name__ == "__main__": # Main function # Read two numbers from input and typecast them to int using map function. # Here x is the base and y is the power. UpperCamelCase = "Enter the base and the power separated by a comma: " UpperCamelCase , UpperCamelCase = map(int, input(prompt).split(",")) UpperCamelCase , UpperCamelCase = map(int, input(prompt).split(",")) # We find the log of each number, using the function res(), which takes two # arguments. UpperCamelCase = res(xa, ya) UpperCamelCase = res(xa, ya) # We check for the largest number if resa > resa: print("Largest number is", xa, "^", ya) elif resa > resa: print("Largest number is", xa, "^", ya) else: print("Both are equal")
45
0
"""simple docstring""" import secrets from random import shuffle from string import ascii_letters, ascii_lowercase, ascii_uppercase, digits, punctuation def _UpperCamelCase ( UpperCamelCase = 8 ) -> str: """simple docstring""" __UpperCAmelCase : List[Any] = ascii_letters + digits + punctuation return "".join(secrets.choice(UpperCamelCase ) for _ in range(UpperCamelCase ) ) def _UpperCamelCase ( UpperCamelCase , UpperCamelCase ) -> str: """simple docstring""" # Password Generator = full boot with random_number, random_letters, and # random_character FUNCTIONS # Put your code here... i -= len(UpperCamelCase ) __UpperCAmelCase : int = i // 3 __UpperCAmelCase : Optional[int] = i % 3 # chars = chars_incl + random_letters(ascii_letters, i / 3 + remainder) + # random_number(digits, i / 3) + random_characters(punctuation, i / 3) __UpperCAmelCase : Optional[Any] = ( chars_incl + random(UpperCamelCase , quotient + remainder ) + random(UpperCamelCase , UpperCamelCase ) + random(UpperCamelCase , UpperCamelCase ) ) __UpperCAmelCase : Union[str, Any] = list(UpperCamelCase ) shuffle(UpperCamelCase ) return "".join(UpperCamelCase ) # random is a generalised function for letters, characters and numbers def _UpperCamelCase ( UpperCamelCase , UpperCamelCase ) -> str: """simple docstring""" return "".join(secrets.choice(UpperCamelCase ) for _ in range(UpperCamelCase ) ) def _UpperCamelCase ( UpperCamelCase , UpperCamelCase ) -> Optional[Any]: """simple docstring""" pass # Put your code here... def _UpperCamelCase ( UpperCamelCase , UpperCamelCase ) -> Any: """simple docstring""" pass # Put your code here... def _UpperCamelCase ( UpperCamelCase , UpperCamelCase ) -> Union[str, Any]: """simple docstring""" pass # Put your code here... def _UpperCamelCase ( UpperCamelCase , UpperCamelCase = 8 ) -> bool: """simple docstring""" if len(UpperCamelCase ) < min_length: # Your Password must be at least 8 characters long return False __UpperCAmelCase : Tuple = any(char in ascii_uppercase for char in password ) __UpperCAmelCase : Optional[int] = any(char in ascii_lowercase for char in password ) __UpperCAmelCase : Optional[int] = any(char in digits for char in password ) __UpperCAmelCase : Any = any(char in punctuation for char in password ) return upper and lower and num and spec_char # Passwords should contain UPPERCASE, lowerase # numbers, and special characters def _UpperCamelCase ( ) -> Dict: """simple docstring""" __UpperCAmelCase : str = int(input("Please indicate the max length of your password: " ).strip() ) __UpperCAmelCase : Dict = input( "Please indicate the characters that must be in your password: " ).strip() print("Password generated:" , password_generator(UpperCamelCase ) ) print( "Alternative Password generated:" , alternative_password_generator(UpperCamelCase , UpperCamelCase ) , ) print("[If you are thinking of using this passsword, You better save it.]" ) if __name__ == "__main__": main()
77
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, 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 numpy as np import tensorflow as tf from transformers import ( TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST, FlaubertConfig, TFFlaubertForMultipleChoice, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForSequenceClassification, TFFlaubertForTokenClassification, TFFlaubertModel, TFFlaubertWithLMHeadModel, ) class lowerCAmelCase_ : """simple docstring""" def __init__( self :Dict , lowerCamelCase__ :List[str] , ): UpperCamelCase__ :Optional[int] = parent UpperCamelCase__ :int = 13 UpperCamelCase__ :Optional[int] = 7 UpperCamelCase__ :Dict = True UpperCamelCase__ :Dict = True UpperCamelCase__ :str = True UpperCamelCase__ :List[Any] = True UpperCamelCase__ :Any = True UpperCamelCase__ :Optional[int] = False UpperCamelCase__ :Optional[int] = False UpperCamelCase__ :Tuple = False UpperCamelCase__ :Optional[int] = 2 UpperCamelCase__ :List[str] = 99 UpperCamelCase__ :Optional[Any] = 0 UpperCamelCase__ :Any = 32 UpperCamelCase__ :List[str] = 2 UpperCamelCase__ :int = 4 UpperCamelCase__ :List[str] = 0.1 UpperCamelCase__ :Union[str, Any] = 0.1 UpperCamelCase__ :Union[str, Any] = 5_12 UpperCamelCase__ :List[str] = 16 UpperCamelCase__ :str = 2 UpperCamelCase__ :Optional[int] = 0.02 UpperCamelCase__ :Optional[int] = 3 UpperCamelCase__ :Optional[int] = 4 UpperCamelCase__ :Optional[int] = """last""" UpperCamelCase__ :Tuple = True UpperCamelCase__ :int = None UpperCamelCase__ :Dict = 0 def __a ( self :int ): UpperCamelCase__ :Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCamelCase__ :Any = random_attention_mask([self.batch_size, self.seq_length] , dtype=tf.floataa ) UpperCamelCase__ :Union[str, Any] = None if self.use_input_lengths: UpperCamelCase__ :Union[str, Any] = ( ids_tensor([self.batch_size] , vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length UpperCamelCase__ :List[str] = None if self.use_token_type_ids: UpperCamelCase__ :List[str] = ids_tensor([self.batch_size, self.seq_length] , self.n_langs ) UpperCamelCase__ :int = None UpperCamelCase__ :List[str] = None UpperCamelCase__ :List[str] = None if self.use_labels: UpperCamelCase__ :List[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase__ :Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCamelCase__ :str = ids_tensor([self.batch_size] , 2 , dtype=tf.floataa ) UpperCamelCase__ :int = ids_tensor([self.batch_size] , self.num_choices ) UpperCamelCase__ :List[Any] = FlaubertConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , bos_token_id=self.bos_token_id , ) return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def __a ( self :Union[str, Any] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :int , lowerCamelCase__ :List[str] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :int , lowerCamelCase__ :List[Any] , lowerCamelCase__ :List[Any] , lowerCamelCase__ :int , ): UpperCamelCase__ :int = TFFlaubertModel(config=lowerCamelCase__ ) UpperCamelCase__ :Union[str, Any] = {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids} UpperCamelCase__ :Optional[Any] = model(lowerCamelCase__ ) UpperCamelCase__ :Union[str, Any] = [input_ids, input_mask] UpperCamelCase__ :Optional[int] = model(lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __a ( self :Tuple , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Any , lowerCamelCase__ :int , lowerCamelCase__ :int , lowerCamelCase__ :List[str] , lowerCamelCase__ :Any , lowerCamelCase__ :Optional[Any] , ): UpperCamelCase__ :List[str] = TFFlaubertWithLMHeadModel(lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids} UpperCamelCase__ :Any = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __a ( self :Dict , lowerCamelCase__ :List[str] , lowerCamelCase__ :Dict , lowerCamelCase__ :Tuple , lowerCamelCase__ :str , lowerCamelCase__ :Any , lowerCamelCase__ :Tuple , lowerCamelCase__ :Any , lowerCamelCase__ :int , lowerCamelCase__ :Tuple , ): UpperCamelCase__ :int = TFFlaubertForQuestionAnsweringSimple(lowerCamelCase__ ) UpperCamelCase__ :int = {"""input_ids""": input_ids, """lengths""": input_lengths} UpperCamelCase__ :Optional[int] = model(lowerCamelCase__ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __a ( self :List[Any] , lowerCamelCase__ :List[str] , lowerCamelCase__ :List[str] , lowerCamelCase__ :str , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Tuple , lowerCamelCase__ :Tuple , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :int , lowerCamelCase__ :Optional[int] , ): UpperCamelCase__ :List[Any] = TFFlaubertForSequenceClassification(lowerCamelCase__ ) UpperCamelCase__ :List[str] = {"""input_ids""": input_ids, """lengths""": input_lengths} UpperCamelCase__ :List[str] = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __a ( self :Tuple , lowerCamelCase__ :str , lowerCamelCase__ :Any , lowerCamelCase__ :Tuple , lowerCamelCase__ :str , lowerCamelCase__ :Any , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :List[str] , lowerCamelCase__ :str , lowerCamelCase__ :Any , ): UpperCamelCase__ :Any = self.num_labels UpperCamelCase__ :Tuple = TFFlaubertForTokenClassification(config=lowerCamelCase__ ) UpperCamelCase__ :Any = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} UpperCamelCase__ :List[Any] = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __a ( self :Tuple , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Tuple , lowerCamelCase__ :Any , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :str , lowerCamelCase__ :Any , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Dict , lowerCamelCase__ :List[str] , ): UpperCamelCase__ :Optional[int] = self.num_choices UpperCamelCase__ :Dict = TFFlaubertForMultipleChoice(config=lowerCamelCase__ ) UpperCamelCase__ :Any = tf.tile(tf.expand_dims(lowerCamelCase__ , 1 ) , (1, self.num_choices, 1) ) UpperCamelCase__ :str = tf.tile(tf.expand_dims(lowerCamelCase__ , 1 ) , (1, self.num_choices, 1) ) UpperCamelCase__ :Any = tf.tile(tf.expand_dims(lowerCamelCase__ , 1 ) , (1, self.num_choices, 1) ) UpperCamelCase__ :int = { """input_ids""": multiple_choice_inputs_ids, """attention_mask""": multiple_choice_input_mask, """token_type_ids""": multiple_choice_token_type_ids, } UpperCamelCase__ :List[str] = model(lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __a ( self :Tuple ): UpperCamelCase__ :str = self.prepare_config_and_inputs() ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :str = config_and_inputs UpperCamelCase__ :Optional[Any] = { """input_ids""": input_ids, """token_type_ids""": token_type_ids, """langs""": token_type_ids, """lengths""": input_lengths, } return config, inputs_dict @require_tf class lowerCAmelCase_ ( lowercase , lowercase , unittest.TestCase ): """simple docstring""" _snake_case : List[str] = ( ( TFFlaubertModel, TFFlaubertWithLMHeadModel, TFFlaubertForSequenceClassification, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForTokenClassification, TFFlaubertForMultipleChoice, ) if is_tf_available() else () ) _snake_case : List[Any] = ( (TFFlaubertWithLMHeadModel,) if is_tf_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable _snake_case : Optional[int] = ( { """feature-extraction""": TFFlaubertModel, """fill-mask""": TFFlaubertWithLMHeadModel, """question-answering""": TFFlaubertForQuestionAnsweringSimple, """text-classification""": TFFlaubertForSequenceClassification, """token-classification""": TFFlaubertForTokenClassification, """zero-shot""": TFFlaubertForSequenceClassification, } if is_tf_available() else {} ) _snake_case : List[Any] = False _snake_case : Tuple = False def __a ( self :Optional[int] , lowerCamelCase__ :List[Any] , lowerCamelCase__ :Dict , lowerCamelCase__ :int , lowerCamelCase__ :str , lowerCamelCase__ :List[Any] ): if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith("""Fast""" ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def __a ( self :List[str] ): UpperCamelCase__ :List[str] = TFFlaubertModelTester(self ) UpperCamelCase__ :Tuple = ConfigTester(self , config_class=lowerCamelCase__ , emb_dim=37 ) def __a ( self :int ): self.config_tester.run_common_tests() def __a ( self :List[str] ): UpperCamelCase__ :List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*lowerCamelCase__ ) def __a ( self :Tuple ): UpperCamelCase__ :Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*lowerCamelCase__ ) def __a ( self :Union[str, Any] ): UpperCamelCase__ :Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*lowerCamelCase__ ) def __a ( self :List[Any] ): UpperCamelCase__ :Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*lowerCamelCase__ ) def __a ( self :Any ): UpperCamelCase__ :Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_token_classification(*lowerCamelCase__ ) def __a ( self :List[Any] ): UpperCamelCase__ :Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_multiple_choice(*lowerCamelCase__ ) @slow def __a ( self :str ): for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase__ :Dict = TFFlaubertModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) @require_tf @require_sentencepiece @require_tokenizers class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" @slow def __a ( self :str ): UpperCamelCase__ :Tuple = TFFlaubertModel.from_pretrained("""jplu/tf-flaubert-small-cased""" ) UpperCamelCase__ :Optional[int] = tf.convert_to_tensor( [[0, 1_58, 7_35, 25_92, 14_24, 67_27, 82, 1]] , dtype=tf.intaa , ) # "J'aime flaubert !" UpperCamelCase__ :Optional[Any] = model(lowerCamelCase__ )[0] UpperCamelCase__ :Optional[int] = tf.TensorShape((1, 8, 5_12) ) self.assertEqual(output.shape , lowerCamelCase__ ) # compare the actual values for a slice. UpperCamelCase__ :str = tf.convert_to_tensor( [ [ [-1.876_8773, -1.56_6555, 0.2707_2418], [-1.692_0038, -0.587_3505, 1.932_9599], [-2.956_3985, -1.699_3835, 1.797_2052], ] ] , dtype=tf.floataa , ) self.assertTrue(np.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1e-4 ) )
45
0
'''simple docstring''' import warnings from contextlib import contextmanager from ....processing_utils import ProcessorMixin class __A ( UpperCamelCase__ ): a__ : int = """MCTCTFeatureExtractor""" a__ : int = """AutoTokenizer""" def __init__(self : Dict , __a : Tuple , __a : Optional[Any] ): super().__init__(__a , __a ) UpperCAmelCase_ = self.feature_extractor UpperCAmelCase_ = False def __call__(self : Dict , *__a : int , **__a : str ): # For backward compatibility if self._in_target_context_manager: return self.current_processor(*__a , **__a ) 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" , __a ) UpperCAmelCase_ = kwargs.pop("sampling_rate" , __a ) UpperCAmelCase_ = kwargs.pop("text" , __a ) if len(__a ) > 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(__a , *__a , sampling_rate=__a , **__a ) if text is not None: UpperCAmelCase_ = self.tokenizer(__a , **__a ) if text is None: return inputs elif audio is None: return encodings else: UpperCAmelCase_ = encodings["input_ids"] return inputs def _lowercase (self : List[Any] , *__a : List[Any] , **__a : int ): return self.tokenizer.batch_decode(*__a , **__a ) def _lowercase (self : Optional[int] , *__a : str , **__a : List[Any] ): # For backward compatibility if self._in_target_context_manager: return self.current_processor.pad(*__a , **__a ) UpperCAmelCase_ = kwargs.pop("input_features" , __a ) UpperCAmelCase_ = kwargs.pop("labels" , __a ) if len(__a ) > 0: UpperCAmelCase_ = args[0] UpperCAmelCase_ = args[1:] if input_features is not None: UpperCAmelCase_ = self.feature_extractor.pad(__a , *__a , **__a ) if labels is not None: UpperCAmelCase_ = self.tokenizer.pad(__a , **__a ) if labels is None: return input_features elif input_features is None: return labels else: UpperCAmelCase_ = labels["input_ids"] return input_features def _lowercase (self : Optional[int] , *__a : List[Any] , **__a : Tuple ): return self.tokenizer.decode(*__a , **__a ) @contextmanager def _lowercase (self : Any ): 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
78
import gc import tempfile import unittest import numpy as np import torch from diffusers import VersatileDiffusionPipeline from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device UpperCamelCase = False class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" pass @nightly @require_torch_gpu class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __a ( self :Union[str, Any] ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __a ( self :List[Any] ): UpperCamelCase__ :List[str] = VersatileDiffusionPipeline.from_pretrained("""shi-labs/versatile-diffusion""" , torch_dtype=torch.floataa ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) UpperCamelCase__ :Dict = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg""" ) UpperCamelCase__ :Any = torch.manual_seed(0 ) UpperCamelCase__ :Optional[int] = pipe.dual_guided( prompt="""first prompt""" , image=lowerCamelCase__ , text_to_image_strength=0.75 , generator=lowerCamelCase__ , guidance_scale=7.5 , num_inference_steps=2 , output_type="""numpy""" , ).images with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(lowerCamelCase__ ) UpperCamelCase__ :List[str] = VersatileDiffusionPipeline.from_pretrained(lowerCamelCase__ , torch_dtype=torch.floataa ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) UpperCamelCase__ :str = generator.manual_seed(0 ) UpperCamelCase__ :str = pipe.dual_guided( prompt="""first prompt""" , image=lowerCamelCase__ , text_to_image_strength=0.75 , generator=lowerCamelCase__ , guidance_scale=7.5 , num_inference_steps=2 , output_type="""numpy""" , ).images assert np.abs(image - new_image ).sum() < 1e-5, "Models don't have the same forward pass" def __a ( self :Dict ): UpperCamelCase__ :List[Any] = VersatileDiffusionPipeline.from_pretrained("""shi-labs/versatile-diffusion""" , torch_dtype=torch.floataa ) pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = """cyberpunk 2077""" UpperCamelCase__ :str = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg""" ) UpperCamelCase__ :str = torch.manual_seed(0 ) UpperCamelCase__ :Dict = pipe.dual_guided( prompt=lowerCamelCase__ , image=lowerCamelCase__ , text_to_image_strength=0.75 , generator=lowerCamelCase__ , guidance_scale=7.5 , num_inference_steps=50 , output_type="""numpy""" , ).images UpperCamelCase__ :Tuple = image[0, 2_53:2_56, 2_53:2_56, -1] assert image.shape == (1, 5_12, 5_12, 3) UpperCamelCase__ :Any = np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 UpperCamelCase__ :List[Any] = """A painting of a squirrel eating a burger """ UpperCamelCase__ :List[str] = torch.manual_seed(0 ) UpperCamelCase__ :Optional[int] = pipe.text_to_image( prompt=lowerCamelCase__ , generator=lowerCamelCase__ , guidance_scale=7.5 , num_inference_steps=50 , output_type="""numpy""" ).images UpperCamelCase__ :str = image[0, 2_53:2_56, 2_53:2_56, -1] assert image.shape == (1, 5_12, 5_12, 3) UpperCamelCase__ :Union[str, Any] = np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 UpperCamelCase__ :Optional[int] = pipe.image_variation(lowerCamelCase__ , generator=lowerCamelCase__ , output_type="""numpy""" ).images UpperCamelCase__ :int = image[0, 2_53:2_56, 2_53:2_56, -1] assert image.shape == (1, 5_12, 5_12, 3) UpperCamelCase__ :List[Any] = np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1
45
0
from __future__ import annotations from math import pi from typing import Protocol import matplotlib.pyplot as plt import numpy as np class UpperCAmelCase_ ( __lowerCamelCase ): def __UpperCAmelCase ( self , _lowerCAmelCase ): return 0.0 def _lowerCamelCase ( __lowerCamelCase , __lowerCamelCase ) -> tuple[int | float, int | float]: '''simple docstring''' UpperCAmelCase__ : Union[str, Any] = min([-20, np.min(fft_results[1 : samplerate // 2 - 1] )] ) UpperCAmelCase__ : int = max([20, np.max(fft_results[1 : samplerate // 2 - 1] )] ) return lowest, highest def _lowerCamelCase ( __lowerCamelCase , __lowerCamelCase ) -> None: '''simple docstring''' UpperCAmelCase__ : Optional[Any] = 512 UpperCAmelCase__ : int = [1] + [0] * (size - 1) UpperCAmelCase__ : str = [filter_type.process(__lowerCamelCase ) for item in inputs] UpperCAmelCase__ : List[str] = [0] * (samplerate - size) # zero-padding outputs += filler UpperCAmelCase__ : List[str] = np.abs(np.fft.fft(__lowerCamelCase ) ) UpperCAmelCase__ : Any = 20 * np.logaa(__lowerCamelCase ) # Frequencies on log scale from 24 to nyquist frequency plt.xlim(24 , samplerate / 2 - 1 ) plt.xlabel("""Frequency (Hz)""" ) plt.xscale("""log""" ) # Display within reasonable bounds UpperCAmelCase__ : int = get_bounds(__lowerCamelCase , __lowerCamelCase ) plt.ylim(max([-80, bounds[0]] ) , min([80, bounds[1]] ) ) plt.ylabel("""Gain (dB)""" ) plt.plot(__lowerCamelCase ) plt.show() def _lowerCamelCase ( __lowerCamelCase , __lowerCamelCase ) -> None: '''simple docstring''' UpperCAmelCase__ : Optional[Any] = 512 UpperCAmelCase__ : Any = [1] + [0] * (size - 1) UpperCAmelCase__ : int = [filter_type.process(__lowerCamelCase ) for item in inputs] UpperCAmelCase__ : Optional[Any] = [0] * (samplerate - size) # zero-padding outputs += filler UpperCAmelCase__ : Optional[int] = np.angle(np.fft.fft(__lowerCamelCase ) ) # Frequencies on log scale from 24 to nyquist frequency plt.xlim(24 , samplerate / 2 - 1 ) plt.xlabel("""Frequency (Hz)""" ) plt.xscale("""log""" ) plt.ylim(-2 * pi , 2 * pi ) plt.ylabel("""Phase shift (Radians)""" ) plt.plot(np.unwrap(__lowerCamelCase , -2 * pi ) ) plt.show()
79
from __future__ import annotations import copy import inspect import unittest import numpy as np from transformers import is_tf_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST, TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, TFLayoutLMvaModel, ) if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class lowerCAmelCase_ : """simple docstring""" def __init__( self :Union[str, Any] , lowerCamelCase__ :Tuple , lowerCamelCase__ :List[str]=2 , lowerCamelCase__ :List[str]=3 , lowerCamelCase__ :List[str]=4 , lowerCamelCase__ :str=2 , lowerCamelCase__ :Optional[int]=7 , lowerCamelCase__ :List[Any]=True , lowerCamelCase__ :Optional[Any]=True , lowerCamelCase__ :Union[str, Any]=True , lowerCamelCase__ :Any=True , lowerCamelCase__ :Dict=99 , lowerCamelCase__ :Optional[Any]=36 , lowerCamelCase__ :str=2 , lowerCamelCase__ :List[Any]=4 , lowerCamelCase__ :Optional[Any]=37 , lowerCamelCase__ :Optional[int]="gelu" , lowerCamelCase__ :Any=0.1 , lowerCamelCase__ :List[Any]=0.1 , lowerCamelCase__ :List[Any]=5_12 , lowerCamelCase__ :str=16 , lowerCamelCase__ :Tuple=2 , lowerCamelCase__ :int=0.02 , lowerCamelCase__ :List[Any]=6 , lowerCamelCase__ :List[str]=6 , lowerCamelCase__ :Optional[int]=3 , lowerCamelCase__ :Optional[int]=4 , lowerCamelCase__ :int=None , lowerCamelCase__ :Optional[Any]=10_00 , ): UpperCamelCase__ :Any = parent UpperCamelCase__ :Union[str, Any] = batch_size UpperCamelCase__ :Dict = num_channels UpperCamelCase__ :Optional[Any] = image_size UpperCamelCase__ :Union[str, Any] = patch_size UpperCamelCase__ :Union[str, Any] = is_training UpperCamelCase__ :str = use_input_mask UpperCamelCase__ :int = use_token_type_ids UpperCamelCase__ :int = use_labels UpperCamelCase__ :List[Any] = vocab_size UpperCamelCase__ :List[str] = hidden_size UpperCamelCase__ :List[Any] = num_hidden_layers UpperCamelCase__ :List[str] = num_attention_heads UpperCamelCase__ :Tuple = intermediate_size UpperCamelCase__ :Any = hidden_act UpperCamelCase__ :Optional[int] = hidden_dropout_prob UpperCamelCase__ :Tuple = attention_probs_dropout_prob UpperCamelCase__ :Dict = max_position_embeddings UpperCamelCase__ :Tuple = type_vocab_size UpperCamelCase__ :Union[str, Any] = type_sequence_label_size UpperCamelCase__ :int = initializer_range UpperCamelCase__ :List[Any] = coordinate_size UpperCamelCase__ :Tuple = shape_size UpperCamelCase__ :Dict = num_labels UpperCamelCase__ :str = num_choices UpperCamelCase__ :Tuple = scope UpperCamelCase__ :str = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) UpperCamelCase__ :List[str] = text_seq_length UpperCamelCase__ :List[str] = (image_size // patch_size) ** 2 + 1 UpperCamelCase__ :Dict = self.text_seq_length + self.image_seq_length def __a ( self :Tuple ): UpperCamelCase__ :Optional[int] = ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size ) UpperCamelCase__ :int = ids_tensor([self.batch_size, self.text_seq_length, 4] , self.range_bbox ) UpperCamelCase__ :str = bbox.numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: UpperCamelCase__ :List[str] = bbox[i, j, 3] UpperCamelCase__ :Optional[int] = bbox[i, j, 1] UpperCamelCase__ :Optional[Any] = tmp_coordinate if bbox[i, j, 2] < bbox[i, j, 0]: UpperCamelCase__ :Tuple = bbox[i, j, 2] UpperCamelCase__ :Optional[Any] = bbox[i, j, 0] UpperCamelCase__ :List[str] = tmp_coordinate UpperCamelCase__ :Dict = tf.constant(lowerCamelCase__ ) UpperCamelCase__ :Union[str, Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCamelCase__ :Any = None if self.use_input_mask: UpperCamelCase__ :int = random_attention_mask([self.batch_size, self.text_seq_length] ) UpperCamelCase__ :Optional[Any] = None if self.use_token_type_ids: UpperCamelCase__ :Optional[int] = ids_tensor([self.batch_size, self.text_seq_length] , self.type_vocab_size ) UpperCamelCase__ :List[str] = None UpperCamelCase__ :Union[str, Any] = None if self.use_labels: UpperCamelCase__ :Union[str, Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase__ :Union[str, Any] = ids_tensor([self.batch_size, self.text_seq_length] , self.num_labels ) UpperCamelCase__ :Optional[int] = LayoutLMvaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , coordinate_size=self.coordinate_size , shape_size=self.shape_size , input_size=self.image_size , patch_size=self.patch_size , ) return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels def __a ( self :List[Any] , lowerCamelCase__ :str , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Dict , lowerCamelCase__ :str , lowerCamelCase__ :int , lowerCamelCase__ :Any ): UpperCamelCase__ :Dict = TFLayoutLMvaModel(config=lowerCamelCase__ ) # text + image UpperCamelCase__ :Tuple = model(lowerCamelCase__ , pixel_values=lowerCamelCase__ , training=lowerCamelCase__ ) UpperCamelCase__ :Tuple = model( lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , training=lowerCamelCase__ , ) UpperCamelCase__ :str = model(lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , training=lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # text only UpperCamelCase__ :Optional[int] = model(lowerCamelCase__ , training=lowerCamelCase__ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size) ) # image only UpperCamelCase__ :Tuple = model({"""pixel_values""": pixel_values} , training=lowerCamelCase__ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size) ) def __a ( self :Dict , lowerCamelCase__ :str , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Dict , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :str , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :str ): UpperCamelCase__ :Optional[Any] = self.num_labels UpperCamelCase__ :List[Any] = TFLayoutLMvaForSequenceClassification(config=lowerCamelCase__ ) UpperCamelCase__ :List[str] = model( lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , labels=lowerCamelCase__ , training=lowerCamelCase__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __a ( self :List[str] , lowerCamelCase__ :List[str] , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Dict , lowerCamelCase__ :List[Any] , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Tuple , lowerCamelCase__ :List[str] ): UpperCamelCase__ :Union[str, Any] = self.num_labels UpperCamelCase__ :Dict = TFLayoutLMvaForTokenClassification(config=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = model( lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , labels=lowerCamelCase__ , training=lowerCamelCase__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels) ) def __a ( self :int , lowerCamelCase__ :Dict , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Any , lowerCamelCase__ :Dict , lowerCamelCase__ :Tuple , lowerCamelCase__ :Tuple ): UpperCamelCase__ :Dict = 2 UpperCamelCase__ :Tuple = TFLayoutLMvaForQuestionAnswering(config=lowerCamelCase__ ) UpperCamelCase__ :int = model( lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , attention_mask=lowerCamelCase__ , token_type_ids=lowerCamelCase__ , start_positions=lowerCamelCase__ , end_positions=lowerCamelCase__ , training=lowerCamelCase__ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __a ( self :List[Any] ): UpperCamelCase__ :Union[str, Any] = self.prepare_config_and_inputs() ((UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__) , (UpperCamelCase__)) :Any = config_and_inputs UpperCamelCase__ :List[str] = { """input_ids""": input_ids, """bbox""": bbox, """pixel_values""": pixel_values, """token_type_ids""": token_type_ids, """attention_mask""": input_mask, } return config, inputs_dict @require_tf class lowerCAmelCase_ ( lowercase , lowercase , unittest.TestCase ): """simple docstring""" _snake_case : Dict = ( ( TFLayoutLMvaModel, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, ) if is_tf_available() else () ) _snake_case : Dict = ( {"""document-question-answering""": TFLayoutLMvaForQuestionAnswering, """feature-extraction""": TFLayoutLMvaModel} if is_tf_available() else {} ) _snake_case : Optional[int] = False _snake_case : List[str] = False _snake_case : Tuple = False def __a ( self :str , lowerCamelCase__ :Optional[int] , lowerCamelCase__ :Union[str, Any] , lowerCamelCase__ :Optional[Any] , lowerCamelCase__ :Tuple , lowerCamelCase__ :int ): return True def __a ( self :Optional[int] , lowerCamelCase__ :int , lowerCamelCase__ :List[str] , lowerCamelCase__ :Optional[int]=False ): UpperCamelCase__ :List[str] = copy.deepcopy(lowerCamelCase__ ) if model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :Optional[int] = { k: tf.tile(tf.expand_dims(lowerCamelCase__ , 1 ) , (1, self.model_tester.num_choices) + (1,) * (v.ndim - 1) ) if isinstance(lowerCamelCase__ , tf.Tensor ) and v.ndim > 0 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :str = tf.ones(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :List[str] = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) UpperCamelCase__ :Union[str, Any] = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :Optional[Any] = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(lowerCamelCase__ ): UpperCamelCase__ :Tuple = tf.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=tf.intaa ) return inputs_dict def __a ( self :Dict ): UpperCamelCase__ :List[Any] = TFLayoutLMvaModelTester(self ) UpperCamelCase__ :Optional[int] = ConfigTester(self , config_class=lowerCamelCase__ , hidden_size=37 ) def __a ( self :Any ): self.config_tester.run_common_tests() def __a ( self :Optional[int] ): UpperCamelCase__ , UpperCamelCase__ :Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase__ :Optional[int] = model_class(lowerCamelCase__ ) if getattr(lowerCamelCase__ , """hf_compute_loss""" , lowerCamelCase__ ): # The number of elements in the loss should be the same as the number of elements in the label UpperCamelCase__ :Union[str, Any] = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) UpperCamelCase__ :int = prepared_for_class[ sorted(prepared_for_class.keys() - inputs_dict.keys() , reverse=lowerCamelCase__ )[0] ] UpperCamelCase__ :Union[str, Any] = added_label.shape.as_list()[:1] # Test that model correctly compute the loss with kwargs UpperCamelCase__ :List[Any] = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = prepared_for_class.pop("""input_ids""" ) UpperCamelCase__ :List[str] = model(lowerCamelCase__ , **lowerCamelCase__ )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss when we mask some positions UpperCamelCase__ :Union[str, Any] = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = prepared_for_class.pop("""input_ids""" ) if "labels" in prepared_for_class: UpperCamelCase__ :List[str] = prepared_for_class["""labels"""].numpy() if len(labels.shape ) > 1 and labels.shape[1] != 1: UpperCamelCase__ :Optional[Any] = -1_00 UpperCamelCase__ :Union[str, Any] = tf.convert_to_tensor(lowerCamelCase__ ) UpperCamelCase__ :Tuple = model(lowerCamelCase__ , **lowerCamelCase__ )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) self.assertTrue(not np.any(np.isnan(loss.numpy() ) ) ) # Test that model correctly compute the loss with a dict UpperCamelCase__ :Optional[Any] = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) UpperCamelCase__ :Union[str, Any] = model(lowerCamelCase__ )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss with a tuple UpperCamelCase__ :Dict = self._prepare_for_class(inputs_dict.copy() , lowerCamelCase__ , return_labels=lowerCamelCase__ ) # Get keys that were added with the _prepare_for_class function UpperCamelCase__ :str = prepared_for_class.keys() - inputs_dict.keys() UpperCamelCase__ :Tuple = inspect.signature(model.call ).parameters UpperCamelCase__ :str = list(signature.keys() ) # Create a dictionary holding the location of the tensors in the tuple UpperCamelCase__ :Any = {0: """input_ids"""} for label_key in label_keys: UpperCamelCase__ :Dict = signature_names.index(lowerCamelCase__ ) UpperCamelCase__ :Optional[int] = label_key UpperCamelCase__ :Optional[Any] = sorted(tuple_index_mapping.items() ) # Initialize a list with their default values, update the values and convert to a tuple UpperCamelCase__ :Any = [] for name in signature_names: if name != "kwargs": list_input.append(signature[name].default ) for index, value in sorted_tuple_index_mapping: UpperCamelCase__ :List[str] = prepared_for_class[value] UpperCamelCase__ :Union[str, Any] = tuple(lowerCamelCase__ ) # Send to model UpperCamelCase__ :str = model(tuple_input[:-1] )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) def __a ( self :Optional[int] ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Any ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :List[Any] = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: UpperCamelCase__ :Dict = type self.model_tester.create_and_check_model(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Tuple ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :Optional[int] ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def __a ( self :List[str] ): ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ) :Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) @slow def __a ( self :Optional[int] ): for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase__ :Dict = TFLayoutLMvaModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) def A ( ) -> List[str]: UpperCamelCase__ :List[Any] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_tf class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" @cached_property def __a ( self :Optional[Any] ): return LayoutLMvaImageProcessor(apply_ocr=lowerCamelCase__ ) if is_vision_available() else None @slow def __a ( self :Dict ): UpperCamelCase__ :List[str] = TFLayoutLMvaModel.from_pretrained("""microsoft/layoutlmv3-base""" ) UpperCamelCase__ :List[Any] = self.default_image_processor UpperCamelCase__ :str = prepare_img() UpperCamelCase__ :Any = image_processor(images=lowerCamelCase__ , return_tensors="""tf""" ).pixel_values UpperCamelCase__ :str = tf.constant([[1, 2]] ) UpperCamelCase__ :Any = tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]] ) , axis=0 ) # forward pass UpperCamelCase__ :Dict = model(input_ids=lowerCamelCase__ , bbox=lowerCamelCase__ , pixel_values=lowerCamelCase__ , training=lowerCamelCase__ ) # verify the logits UpperCamelCase__ :int = (1, 1_99, 7_68) self.assertEqual(outputs.last_hidden_state.shape , lowerCamelCase__ ) UpperCamelCase__ :List[Any] = tf.constant( [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]] ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] , lowerCamelCase__ , atol=1e-4 ) )
45
0
import itertools import random import unittest import numpy as np from transformers import is_speech_available from transformers.testing_utils import require_torch, require_torchaudio from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_speech_available(): from transformers import SpeechaTextFeatureExtractor __UpperCamelCase : Tuple = random.Random() def snake_case ( lowerCamelCase , lowerCamelCase=1.0 , lowerCamelCase=None , lowerCamelCase=None ): '''simple docstring''' if rng is None: __lowercase = global_rng __lowercase = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch @require_torchaudio class __UpperCamelCase ( unittest.TestCase ): def __init__( self : List[str] , _lowerCAmelCase : str , _lowerCAmelCase : Any=7 , _lowerCAmelCase : Dict=400 , _lowerCAmelCase : Any=2000 , _lowerCAmelCase : Dict=24 , _lowerCAmelCase : Optional[int]=24 , _lowerCAmelCase : Any=0.0 , _lowerCAmelCase : int=1_6000 , _lowerCAmelCase : Any=True , _lowerCAmelCase : List[Any]=True , ) -> str: """simple docstring""" __lowercase = parent __lowercase = batch_size __lowercase = min_seq_length __lowercase = max_seq_length __lowercase = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) __lowercase = feature_size __lowercase = num_mel_bins __lowercase = padding_value __lowercase = sampling_rate __lowercase = return_attention_mask __lowercase = do_normalize def _a ( self : List[str] ) -> str: """simple docstring""" return { "feature_size": self.feature_size, "num_mel_bins": self.num_mel_bins, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "return_attention_mask": self.return_attention_mask, "do_normalize": self.do_normalize, } def _a ( self : int , _lowerCAmelCase : Tuple=False , _lowerCAmelCase : str=False ) -> Union[str, Any]: """simple docstring""" def _flatten(_lowerCAmelCase : Optional[Any] ): return list(itertools.chain(*_lowerCAmelCase ) ) if equal_length: __lowercase = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size __lowercase = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: __lowercase = [np.asarray(_lowerCAmelCase ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class __UpperCamelCase ( _lowerCAmelCase , unittest.TestCase ): __snake_case :Optional[Any] = SpeechaTextFeatureExtractor if is_speech_available() else None def _a ( self : Union[str, Any] ) -> str: """simple docstring""" __lowercase = SpeechaTextFeatureExtractionTester(self ) def _a ( self : Tuple , _lowerCAmelCase : List[Any] ) -> Tuple: """simple docstring""" self.assertTrue(np.all(np.mean(_lowerCAmelCase , axis=0 ) < 1e-3 ) ) self.assertTrue(np.all(np.abs(np.var(_lowerCAmelCase , axis=0 ) - 1 ) < 1e-3 ) ) def _a ( self : int ) -> Any: """simple docstring""" __lowercase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 __lowercase = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] __lowercase = [np.asarray(_lowerCAmelCase ) for speech_input in speech_inputs] # Test feature size __lowercase = feature_extractor(_lowerCAmelCase , padding=_lowerCAmelCase , return_tensors="""np""" ).input_features self.assertTrue(input_features.ndim == 3 ) self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size ) # Test not batched input __lowercase = feature_extractor(speech_inputs[0] , return_tensors="""np""" ).input_features __lowercase = feature_extractor(np_speech_inputs[0] , return_tensors="""np""" ).input_features self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1e-3 ) ) # Test batched __lowercase = feature_extractor(_lowerCAmelCase , return_tensors="""np""" ).input_features __lowercase = feature_extractor(_lowerCAmelCase , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1e-3 ) ) # Test 2-D numpy arrays are batched. __lowercase = [floats_list((1, x) )[0] for x in (800, 800, 800)] __lowercase = np.asarray(_lowerCAmelCase ) __lowercase = feature_extractor(_lowerCAmelCase , return_tensors="""np""" ).input_features __lowercase = feature_extractor(_lowerCAmelCase , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase , _lowerCAmelCase ): self.assertTrue(np.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1e-3 ) ) def _a ( self : Union[str, Any] ) -> Optional[int]: """simple docstring""" __lowercase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) __lowercase = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] __lowercase = ["""longest""", """max_length""", """do_not_pad"""] __lowercase = [None, 16, None] for max_length, padding in zip(_lowerCAmelCase , _lowerCAmelCase ): __lowercase = feature_extractor( _lowerCAmelCase , padding=_lowerCAmelCase , max_length=_lowerCAmelCase , return_attention_mask=_lowerCAmelCase ) __lowercase = inputs.input_features __lowercase = inputs.attention_mask __lowercase = [np.sum(_lowerCAmelCase ) for x in attention_mask] self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]] ) self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]] ) def _a ( self : Any ) -> Any: """simple docstring""" __lowercase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) __lowercase = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] __lowercase = ["""longest""", """max_length""", """do_not_pad"""] __lowercase = [None, 16, None] for max_length, padding in zip(_lowerCAmelCase , _lowerCAmelCase ): __lowercase = feature_extractor( _lowerCAmelCase , max_length=_lowerCAmelCase , padding=_lowerCAmelCase , return_tensors="""np""" , return_attention_mask=_lowerCAmelCase ) __lowercase = inputs.input_features __lowercase = inputs.attention_mask __lowercase = [np.sum(_lowerCAmelCase ) for x in attention_mask] self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]] ) self.assertTrue(input_features[0][fbank_feat_lengths[0] :].sum() < 1e-6 ) self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]] ) self.assertTrue(input_features[0][fbank_feat_lengths[1] :].sum() < 1e-6 ) self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]] ) def _a ( self : Any ) -> Optional[int]: """simple docstring""" __lowercase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) __lowercase = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] __lowercase = feature_extractor( _lowerCAmelCase , padding="""max_length""" , max_length=4 , truncation=_lowerCAmelCase , return_tensors="""np""" , return_attention_mask=_lowerCAmelCase , ) __lowercase = inputs.input_features __lowercase = inputs.attention_mask __lowercase = np.sum(attention_mask == 1 , axis=1 ) self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1] ) self._check_zero_mean_unit_variance(input_features[2] ) def _a ( self : Optional[int] ) -> Any: """simple docstring""" __lowercase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) __lowercase = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] __lowercase = feature_extractor( _lowerCAmelCase , padding="""longest""" , max_length=4 , truncation=_lowerCAmelCase , return_tensors="""np""" , return_attention_mask=_lowerCAmelCase , ) __lowercase = inputs.input_features __lowercase = inputs.attention_mask __lowercase = np.sum(attention_mask == 1 , axis=1 ) self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]] ) self._check_zero_mean_unit_variance(input_features[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertEqual(input_features.shape , (3, 4, 24) ) __lowercase = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] __lowercase = feature_extractor( _lowerCAmelCase , padding="""longest""" , max_length=16 , truncation=_lowerCAmelCase , return_tensors="""np""" , return_attention_mask=_lowerCAmelCase , ) __lowercase = inputs.input_features __lowercase = inputs.attention_mask __lowercase = np.sum(attention_mask == 1 , axis=1 ) self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]] ) self._check_zero_mean_unit_variance(input_features[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertEqual(input_features.shape , (3, 6, 24) ) def _a ( self : Dict ) -> Union[str, Any]: """simple docstring""" import torch __lowercase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) __lowercase = np.random.rand(100 , 32 ).astype(np.floataa ) __lowercase = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: __lowercase = feature_extractor.pad([{"""input_features""": inputs}] , return_tensors="""np""" ) self.assertTrue(np_processed.input_features.dtype == np.floataa ) __lowercase = feature_extractor.pad([{"""input_features""": inputs}] , return_tensors="""pt""" ) self.assertTrue(pt_processed.input_features.dtype == torch.floataa ) def _a ( self : List[str] , _lowerCAmelCase : Optional[int] ) -> Union[str, Any]: """simple docstring""" from datasets import load_dataset __lowercase = load_dataset("""hf-internal-testing/librispeech_asr_dummy""" , """clean""" , split="""validation""" ) # automatic decoding with librispeech __lowercase = ds.sort("""id""" ).select(range(_lowerCAmelCase ) )[:num_samples]["""audio"""] return [x["array"] for x in speech_samples] def _a ( self : int ) -> Any: """simple docstring""" __lowercase = np.array([ -1.5_745, -1.7_713, -1.7_020, -1.6_069, -1.2_250, -1.1_105, -0.9_072, -0.8_241, -1.2_310, -0.8_098, -0.3_320, -0.4_101, -0.7_985, -0.4_996, -0.8_213, -0.9_128, -1.0_420, -1.1_286, -1.0_440, -0.7_999, -0.8_405, -1.2_275, -1.5_443, -1.4_625, ] ) # fmt: on __lowercase = self._load_datasamples(1 ) __lowercase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) __lowercase = feature_extractor(_lowerCAmelCase , return_tensors="""pt""" ).input_features self.assertEquals(input_features.shape , (1, 584, 24) ) self.assertTrue(np.allclose(input_features[0, 0, :30] , _lowerCAmelCase , atol=1e-4 ) )
80
import logging import os import sys from dataclasses import dataclass, field from typing import Optional import torch from datasets import load_dataset from torchvision.transforms import Compose, Lambda, Normalize, RandomHorizontalFlip, RandomResizedCrop, ToTensor from torchvision.transforms.functional import InterpolationMode import transformers from transformers import ( HfArgumentParser, Trainer, TrainingArguments, ViTImageProcessor, ViTMAEConfig, ViTMAEForPreTraining, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version UpperCamelCase = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.31.0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/image-pretraining/requirements.txt") @dataclass class lowerCAmelCase_ : """simple docstring""" _snake_case : Optional[str] = field( default="""cifar10""" , metadata={"""help""": """Name of a dataset from the datasets package"""} ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """The column name of the images in the files."""} ) _snake_case : Optional[str] = field(default=lowercase , metadata={"""help""": """A folder containing the training data."""} ) _snake_case : Optional[str] = field(default=lowercase , metadata={"""help""": """A folder containing the validation data."""} ) _snake_case : Optional[float] = field( default=0.15 , metadata={"""help""": """Percent to split off of train for validation."""} ) _snake_case : Optional[int] = field( default=lowercase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } , ) _snake_case : Optional[int] = field( default=lowercase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of evaluation examples to this """ """value if set.""" ) } , ) def __a ( self :List[str] ): UpperCamelCase__ :Optional[Any] = {} if self.train_dir is not None: UpperCamelCase__ :int = self.train_dir if self.validation_dir is not None: UpperCamelCase__ :List[str] = self.validation_dir UpperCamelCase__ :Optional[int] = data_files if data_files else None @dataclass class lowerCAmelCase_ : """simple docstring""" _snake_case : str = field( default=lowercase , metadata={ """help""": ( """The model checkpoint for weights initialization.Don't set if you want to train a model from scratch.""" ) } , ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """Pretrained config name or path if not the same as model_name_or_path"""} ) _snake_case : Optional[str] = field( default=lowercase , metadata={ """help""": ( """Override some existing default config settings when a model is trained from scratch. Example: """ """n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index""" ) } , ) _snake_case : Optional[str] = field( default=lowercase , metadata={"""help""": """Where do you want to store the pretrained models downloaded from s3"""} ) _snake_case : str = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) _snake_case : str = field(default=lowercase , metadata={"""help""": """Name or path of preprocessor config."""} ) _snake_case : bool = field( default=lowercase , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) _snake_case : float = field( default=0.75 , metadata={"""help""": """The ratio of the number of masked tokens in the input sequence."""} ) _snake_case : bool = field( default=lowercase , metadata={"""help""": """Whether or not to train with normalized pixel values as target."""} ) @dataclass class lowerCAmelCase_ ( lowercase ): """simple docstring""" _snake_case : float = field( default=1e-3 , metadata={"""help""": """Base learning rate: absolute_lr = base_lr * total_batch_size / 256."""} ) def A ( lowercase__ : Union[str, Any] ) -> Dict: UpperCamelCase__ :Union[str, Any] = torch.stack([example["""pixel_values"""] for example in examples] ) return {"pixel_values": pixel_values} def A ( ) -> Optional[int]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. UpperCamelCase__ :Optional[int] = HfArgumentParser((ModelArguments, DataTrainingArguments, CustomTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(""".json""" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :str = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Union[str, Any] = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry("""run_mae""" , lowercase__ , lowercase__ ) # Setup logging logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() UpperCamelCase__ :List[str] = training_args.get_process_log_level() logger.setLevel(lowercase__ ) transformers.utils.logging.set_verbosity(lowercase__ ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( f"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}""" + f"""distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}""" ) logger.info(f"""Training/evaluation parameters {training_args}""" ) # Detecting last checkpoint. UpperCamelCase__ :Union[str, Any] = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: UpperCamelCase__ :List[str] = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( f"""Output directory ({training_args.output_dir}) already exists and is not empty. """ """Use --overwrite_output_dir to overcome.""" ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( f"""Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change """ """the `--output_dir` or add `--overwrite_output_dir` to train from scratch.""" ) # Initialize our dataset. UpperCamelCase__ :Tuple = load_dataset( data_args.dataset_name , data_args.dataset_config_name , data_files=data_args.data_files , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) # If we don't have a validation split, split off a percentage of train as validation. UpperCamelCase__ :int = None if """validation""" in ds.keys() else data_args.train_val_split if isinstance(data_args.train_val_split , lowercase__ ) and data_args.train_val_split > 0.0: UpperCamelCase__ :Optional[Any] = ds["""train"""].train_test_split(data_args.train_val_split ) UpperCamelCase__ :Union[str, Any] = split["""train"""] UpperCamelCase__ :Any = split["""test"""] # Load pretrained model and image processor # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. UpperCamelCase__ :Optional[int] = { """cache_dir""": model_args.cache_dir, """revision""": model_args.model_revision, """use_auth_token""": True if model_args.use_auth_token else None, } if model_args.config_name: UpperCamelCase__ :Any = ViTMAEConfig.from_pretrained(model_args.config_name , **lowercase__ ) elif model_args.model_name_or_path: UpperCamelCase__ :Union[str, Any] = ViTMAEConfig.from_pretrained(model_args.model_name_or_path , **lowercase__ ) else: UpperCamelCase__ :Optional[Any] = ViTMAEConfig() logger.warning("""You are instantiating a new config instance from scratch.""" ) if model_args.config_overrides is not None: logger.info(f"""Overriding config: {model_args.config_overrides}""" ) config.update_from_string(model_args.config_overrides ) logger.info(f"""New config: {config}""" ) # adapt config config.update( { """mask_ratio""": model_args.mask_ratio, """norm_pix_loss""": model_args.norm_pix_loss, } ) # create image processor if model_args.image_processor_name: UpperCamelCase__ :str = ViTImageProcessor.from_pretrained(model_args.image_processor_name , **lowercase__ ) elif model_args.model_name_or_path: UpperCamelCase__ :Dict = ViTImageProcessor.from_pretrained(model_args.model_name_or_path , **lowercase__ ) else: UpperCamelCase__ :Tuple = ViTImageProcessor() # create model if model_args.model_name_or_path: UpperCamelCase__ :Any = ViTMAEForPreTraining.from_pretrained( model_args.model_name_or_path , from_tf=bool(""".ckpt""" in model_args.model_name_or_path ) , config=lowercase__ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info("""Training new model from scratch""" ) UpperCamelCase__ :Optional[int] = ViTMAEForPreTraining(lowercase__ ) if training_args.do_train: UpperCamelCase__ :Optional[Any] = ds["""train"""].column_names else: UpperCamelCase__ :Union[str, Any] = ds["""validation"""].column_names if data_args.image_column_name is not None: UpperCamelCase__ :Union[str, Any] = data_args.image_column_name elif "image" in column_names: UpperCamelCase__ :Optional[Any] = """image""" elif "img" in column_names: UpperCamelCase__ :List[str] = """img""" else: UpperCamelCase__ :List[Any] = column_names[0] # transformations as done in original MAE paper # source: https://github.com/facebookresearch/mae/blob/main/main_pretrain.py if "shortest_edge" in image_processor.size: UpperCamelCase__ :List[str] = image_processor.size["""shortest_edge"""] else: UpperCamelCase__ :int = (image_processor.size["""height"""], image_processor.size["""width"""]) UpperCamelCase__ :Any = Compose( [ Lambda(lambda lowercase__ : img.convert("""RGB""" ) if img.mode != "RGB" else img ), RandomResizedCrop(lowercase__ , scale=(0.2, 1.0) , interpolation=InterpolationMode.BICUBIC ), RandomHorizontalFlip(), ToTensor(), Normalize(mean=image_processor.image_mean , std=image_processor.image_std ), ] ) def preprocess_images(lowercase__ : Tuple ): UpperCamelCase__ :List[Any] = [transforms(lowercase__ ) for image in examples[image_column_name]] return examples if training_args.do_train: if "train" not in ds: raise ValueError("""--do_train requires a train dataset""" ) if data_args.max_train_samples is not None: UpperCamelCase__ :Optional[int] = ds["""train"""].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) # Set the training transforms ds["train"].set_transform(lowercase__ ) if training_args.do_eval: if "validation" not in ds: raise ValueError("""--do_eval requires a validation dataset""" ) if data_args.max_eval_samples is not None: UpperCamelCase__ :Optional[Any] = ( ds["""validation"""].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms ds["validation"].set_transform(lowercase__ ) # Compute absolute learning rate UpperCamelCase__ :Tuple = ( training_args.train_batch_size * training_args.gradient_accumulation_steps * training_args.world_size ) if training_args.base_learning_rate is not None: UpperCamelCase__ :Any = training_args.base_learning_rate * total_train_batch_size / 256 # Initialize our trainer UpperCamelCase__ :Union[str, Any] = Trainer( model=lowercase__ , args=lowercase__ , train_dataset=ds["""train"""] if training_args.do_train else None , eval_dataset=ds["""validation"""] if training_args.do_eval else None , tokenizer=lowercase__ , data_collator=lowercase__ , ) # Training if training_args.do_train: UpperCamelCase__ :Any = None if training_args.resume_from_checkpoint is not None: UpperCamelCase__ :int = training_args.resume_from_checkpoint elif last_checkpoint is not None: UpperCamelCase__ :Dict = last_checkpoint UpperCamelCase__ :Union[str, Any] = trainer.train(resume_from_checkpoint=lowercase__ ) trainer.save_model() trainer.log_metrics("""train""" , train_result.metrics ) trainer.save_metrics("""train""" , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: UpperCamelCase__ :int = trainer.evaluate() trainer.log_metrics("""eval""" , lowercase__ ) trainer.save_metrics("""eval""" , lowercase__ ) # Write model card and (optionally) push to hub UpperCamelCase__ :Optional[int] = { """tasks""": """masked-auto-encoding""", """dataset""": data_args.dataset_name, """tags""": ["""masked-auto-encoding"""], } if training_args.push_to_hub: trainer.push_to_hub(**lowercase__ ) else: trainer.create_model_card(**lowercase__ ) def A ( lowercase__ : Union[str, Any] ) -> Dict: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
45
0