code
stringlengths
81
54k
code_codestyle
int64
0
721
style_context
stringlengths
91
41.9k
style_context_codestyle
int64
0
699
label
int64
0
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available __lowerCAmelCase = {'configuration_glpn': ['GLPN_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GLPNConfig']} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = ['GLPNFeatureExtractor'] __lowerCAmelCase = ['GLPNImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ 'GLPN_PRETRAINED_MODEL_ARCHIVE_LIST', 'GLPNForDepthEstimation', 'GLPNLayer', 'GLPNModel', 'GLPNPreTrainedModel', ] if TYPE_CHECKING: from .configuration_glpn import GLPN_PRETRAINED_CONFIG_ARCHIVE_MAP, GLPNConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_glpn import GLPNFeatureExtractor from .image_processing_glpn import GLPNImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_glpn import ( GLPN_PRETRAINED_MODEL_ARCHIVE_LIST, GLPNForDepthEstimation, GLPNLayer, GLPNModel, GLPNPreTrainedModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
708
'''simple docstring''' def _UpperCAmelCase ( __A : str , __A : str ): def get_matched_characters(__A : str , __A : str ) -> str: a_ : Union[str, Any] = [] a_ : int = min(len(_stra ) , len(_stra ) ) // 2 for i, l in enumerate(_stra ): a_ : Any = int(max(0 , i - limit ) ) a_ : Union[str, Any] = int(min(i + limit + 1 , len(_stra ) ) ) if l in _stra[left:right]: matched.append(__A ) a_ : Any = f'{_stra[0:_stra.index(__A )]} {_stra[_stra.index(__A ) + 1:]}' return "".join(__A ) # matching characters a_ : Optional[Any] = get_matched_characters(__A , __A ) a_ : int = get_matched_characters(__A , __A ) a_ : Any = len(__A ) # transposition a_ : List[Any] = ( len([(ca, ca) for ca, ca in zip(__A , __A ) if ca != ca] ) // 2 ) if not match_count: a_ : Dict = 0.0 else: a_ : Optional[int] = ( 1 / 3 * ( match_count / len(__A ) + match_count / len(__A ) + (match_count - transpositions) / match_count ) ) # common prefix up to 4 characters a_ : List[str] = 0 for ca, ca in zip(stra[:4] , stra[:4] ): if ca == ca: prefix_len += 1 else: break return jaro + 0.1 * prefix_len * (1 - jaro) if __name__ == "__main__": import doctest doctest.testmod() print(jaro_winkler('hello', 'world'))
666
0
'''simple docstring''' from unittest.mock import Mock, patch from file_transfer.send_file import send_file @patch('''socket.socket''' ) @patch('''builtins.open''' ) def _UpperCAmelCase ( __A : Union[str, Any] , __A : Any ): a_ : Optional[Any] = Mock() a_ : List[Any] = conn, Mock() a_ : Dict = iter([1, None] ) a_ : Optional[Any] = lambda __A : next(__A ) # ===== invoke ===== send_file(filename='''mytext.txt''' , testing=__A ) # ===== ensurance ===== sock.assert_called_once() sock.return_value.bind.assert_called_once() sock.return_value.listen.assert_called_once() sock.return_value.accept.assert_called_once() conn.recv.assert_called_once() file.return_value.__enter__.assert_called_once() file.return_value.__enter__.return_value.read.assert_called() conn.send.assert_called_once() conn.close.assert_called_once() sock.return_value.shutdown.assert_called_once() sock.return_value.close.assert_called_once()
709
'''simple docstring''' import torch from transformers import AutoModel class SCREAMING_SNAKE_CASE ( torch.nn.Module ): def __init__( self : Optional[int] , __SCREAMING_SNAKE_CASE : int="sayef/fsner-bert-base-uncased" ) -> str: super(__SCREAMING_SNAKE_CASE , self ).__init__() a_ : str = AutoModel.from_pretrained(__SCREAMING_SNAKE_CASE , return_dict=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = torch.nn.CosineSimilarity(3 , 1e-08 ) a_ : Dict = torch.nn.Softmax(dim=1 ) def SCREAMING_SNAKE_CASE ( self : str , **__SCREAMING_SNAKE_CASE : int ) -> str: return self.bert(**__SCREAMING_SNAKE_CASE ).last_hidden_state def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : List[Any] ) -> Optional[int]: return token_embeddings.sum(2 , keepdim=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : int=1 ) -> Dict: return self.softmax(T * self.cos(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[int] ) -> Any: a_ : Dict = W_supports['''sizes'''].tolist() a_ : Tuple = W_supports['''start_token_id'''].item() a_ : List[Any] = W_supports['''end_token_id'''].item() del W_supports["sizes"] del W_supports["start_token_id"] del W_supports["end_token_id"] a_ : int = self.BERT(**__SCREAMING_SNAKE_CASE ) a_ : Any = self.BERT(**__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = None a_ : Tuple = None a_ : List[str] = W_supports['''input_ids'''] == start_token_id a_ : Dict = W_supports['''input_ids'''] == end_token_id for i, size in enumerate(__SCREAMING_SNAKE_CASE ): if i == 0: a_ : str = 0 else: a_ : str = support_sizes[i - 1] a_ : Union[str, Any] = S[s : s + size][start_token_masks[s : s + size]] a_ : Tuple = S[s : s + size][end_token_masks[s : s + size]] a_ : Tuple = torch.matmul(q[i] , s_start.T ).sum(1 ).softmax(0 ) a_ : Optional[Any] = torch.matmul(q[i] , s_end.T ).sum(1 ).softmax(0 ) if p_starts is not None: a_ : Any = torch.vstack((p_starts, p_start) ) a_ : Dict = torch.vstack((p_ends, p_end) ) else: a_ : Optional[int] = p_start a_ : List[Any] = p_end return p_starts, p_ends
666
0
'''simple docstring''' def _UpperCAmelCase ( __A : int ): if not head: return True # split the list to two parts a_ : Union[str, Any] = head.next, head while fast and fast.next: a_ : Union[str, Any] = fast.next.next a_ : List[Any] = slow.next a_ : Optional[Any] = slow.next a_ : Union[str, Any] = None # Don't forget here! But forget still works! # reverse the second part a_ : List[str] = None while second: a_ : str = second.next a_ : str = node a_ : Tuple = second a_ : List[str] = nxt # compare two parts # second part has the same or one less node while node: if node.val != head.val: return False a_ : List[str] = node.next a_ : int = head.next return True def _UpperCAmelCase ( __A : str ): if not head or not head.next: return True # 1. Get the midpoint (slow) a_ : Tuple = head while fast and fast.next: a_ : Dict = fast.next.next, slow.next # 2. Push the second half into the stack a_ : Any = [slow.val] while slow.next: a_ : Dict = slow.next stack.append(slow.val ) # 3. Comparison while stack: if stack.pop() != cur.val: return False a_ : Optional[Any] = cur.next return True def _UpperCAmelCase ( __A : Optional[int] ): if not head or not head.next: return True a_ : int = {} a_ : Optional[int] = 0 while head: if head.val in d: d[head.val].append(snake_case_ ) else: a_ : List[Any] = [pos] a_ : int = head.next pos += 1 a_ : Tuple = pos - 1 a_ : str = 0 for v in d.values(): if len(snake_case_ ) % 2 != 0: middle += 1 else: a_ : Dict = 0 for i in range(0 , len(snake_case_ ) ): if v[i] + v[len(snake_case_ ) - 1 - step] != checksum: return False step += 1 if middle > 1: return False return True
710
'''simple docstring''' import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import ClassLabel, Features, Image from .base import TaskTemplate @dataclass(frozen=SCREAMING_SNAKE_CASE_ ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = field(default="image-classification" , metadata={"include_in_asdict_even_if_is_default": True} ) snake_case__ = Features({"image": Image()} ) snake_case__ = Features({"labels": ClassLabel} ) snake_case__ = "image" snake_case__ = "labels" def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : List[Any] ) -> Any: if self.label_column not in features: raise ValueError(f'Column {self.label_column} is not present in features.' ) if not isinstance(features[self.label_column] , __SCREAMING_SNAKE_CASE ): raise ValueError(f'Column {self.label_column} is not a ClassLabel.' ) a_ : Optional[int] = copy.deepcopy(self ) a_ : int = self.label_schema.copy() a_ : Tuple = features[self.label_column] a_ : str = label_schema return task_template @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict[str, str]: return { self.image_column: "image", self.label_column: "labels", }
666
0
'''simple docstring''' from typing import Dict, List, Optional from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = { 'nielsr/canine-s': 2_048, } # Unicode defines 1,114,112 total “codepoints” __lowerCAmelCase = 1_114_112 # Below: Constants defining canonical codepoints for special, pseudo-characters. # Copied from https://github.com/google-research/language/blob/master/language/canine/special_codepoints.py __lowerCAmelCase = 0 __lowerCAmelCase = 0XE_000 __lowerCAmelCase = 0XE_001 __lowerCAmelCase = 0XE_002 __lowerCAmelCase = 0XE_003 __lowerCAmelCase = 0XE_004 # Maps special codepoints to human-readable names. __lowerCAmelCase = { # Special symbols are represented using codepoints values that are valid, # but designated as "Private Use", meaning that they will never be assigned # characters by the Unicode Consortium, and are thus safe for use here. # # NOTE: Do *NOT* add any sort of [UNK_CHAR] here. They are explicitly # excluded and should fail with a hard error. CLS: "[CLS]", SEP: "[SEP]", BOS: "[BOS]", MASK: "[MASK]", PAD: "[PAD]", RESERVED: "[RESERVED]", } # Maps special codepoint human-readable names to their codepoint values. __lowerCAmelCase = {name: codepoint for codepoint, name in SPECIAL_CODEPOINTS.items()} class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self : int , __SCREAMING_SNAKE_CASE : List[str]=chr(UpperCamelCase__ ) , __SCREAMING_SNAKE_CASE : Any=chr(UpperCamelCase__ ) , __SCREAMING_SNAKE_CASE : Tuple=chr(UpperCamelCase__ ) , __SCREAMING_SNAKE_CASE : Tuple=chr(UpperCamelCase__ ) , __SCREAMING_SNAKE_CASE : Optional[Any]=chr(UpperCamelCase__ ) , __SCREAMING_SNAKE_CASE : Optional[Any]=chr(UpperCamelCase__ ) , __SCREAMING_SNAKE_CASE : int=False , __SCREAMING_SNAKE_CASE : List[str]=2048 , **__SCREAMING_SNAKE_CASE : Optional[int] , ) -> List[Any]: a_ : Any = AddedToken(UpperCamelCase__ , lstrip=UpperCamelCase__ , rstrip=UpperCamelCase__ ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else bos_token a_ : Union[str, Any] = AddedToken(UpperCamelCase__ , lstrip=UpperCamelCase__ , rstrip=UpperCamelCase__ ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else eos_token a_ : Dict = AddedToken(UpperCamelCase__ , lstrip=UpperCamelCase__ , rstrip=UpperCamelCase__ ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else sep_token a_ : str = AddedToken(UpperCamelCase__ , lstrip=UpperCamelCase__ , rstrip=UpperCamelCase__ ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else cls_token a_ : Optional[int] = AddedToken(UpperCamelCase__ , lstrip=UpperCamelCase__ , rstrip=UpperCamelCase__ ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else pad_token # Mask token behave like a normal word, i.e. include the space before it a_ : str = AddedToken(UpperCamelCase__ , lstrip=UpperCamelCase__ , rstrip=UpperCamelCase__ ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else mask_token super().__init__( bos_token=UpperCamelCase__ , eos_token=UpperCamelCase__ , sep_token=UpperCamelCase__ , cls_token=UpperCamelCase__ , pad_token=UpperCamelCase__ , mask_token=UpperCamelCase__ , add_prefix_space=UpperCamelCase__ , model_max_length=UpperCamelCase__ , **UpperCamelCase__ , ) # Creates a mapping for looking up the IDs of special symbols. a_ : Dict[str, int] = {} for codepoint, name in SPECIAL_CODEPOINTS.items(): a_ : Optional[Any] = codepoint # Creates a mapping for looking up the string forms of special symbol IDs. a_ : Dict[int, str] = { codepoint: name for name, codepoint in self._special_codepoints.items() } a_ : Optional[int] = UNICODE_VOCAB_SIZE a_ : Dict = len(self._special_codepoints ) @property def SCREAMING_SNAKE_CASE ( self : Any ) -> Optional[int]: return self._unicode_vocab_size def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Tuple ) -> int: return list(UpperCamelCase__ ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[int] ) -> List[Any]: try: return ord(UpperCamelCase__ ) except TypeError: raise ValueError(f'invalid token: \'{token}\'' ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Any ) -> Union[str, Any]: try: if index in SPECIAL_CODEPOINTS: return SPECIAL_CODEPOINTS[index] return chr(UpperCamelCase__ ) except TypeError: raise ValueError(f'invalid id: {index}' ) def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : Dict ) -> Any: return "".join(UpperCamelCase__ ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Optional[int] = None ) -> Optional[Any]: a_ : Any = [self.sep_token_id] a_ : Union[str, Any] = [self.cls_token_id] a_ : int = cls + token_ids_a + sep if token_ids_a is not None: result += token_ids_a + sep return result def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : str = None , __SCREAMING_SNAKE_CASE : Tuple = False ) -> int: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=UpperCamelCase__ , token_ids_a=UpperCamelCase__ , already_has_special_tokens=UpperCamelCase__ ) a_ : Tuple = [1] + ([0] * len(UpperCamelCase__ )) + [1] if token_ids_a is not None: result += ([0] * len(UpperCamelCase__ )) + [1] return result def SCREAMING_SNAKE_CASE ( self : Tuple , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : str = None ) -> Optional[int]: a_ : Optional[int] = [self.sep_token_id] a_ : Dict = [self.cls_token_id] a_ : List[str] = len(cls + token_ids_a + sep ) * [0] if token_ids_a is not None: result += len(token_ids_a + sep ) * [1] return result def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Optional[Any] = None ) -> int: return ()
711
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( __A : tuple[int, int] , __A : int ): a_ , a_ : List[str] = position a_ : Optional[int] = [ (y + 1, x + 2), (y - 1, x + 2), (y + 1, x - 2), (y - 1, x - 2), (y + 2, x + 1), (y + 2, x - 1), (y - 2, x + 1), (y - 2, x - 1), ] a_ : Any = [] for position in positions: a_ , a_ : Dict = position if 0 <= y_test < n and 0 <= x_test < n: permissible_positions.append(__A ) return permissible_positions def _UpperCAmelCase ( __A : list[list[int]] ): return not any(elem == 0 for row in board for elem in row ) def _UpperCAmelCase ( __A : list[list[int]] , __A : tuple[int, int] , __A : int ): if is_complete(__A ): return True for position in get_valid_pos(__A , len(__A ) ): a_ , a_ : Dict = position if board[y][x] == 0: a_ : Optional[Any] = curr + 1 if open_knight_tour_helper(__A , __A , curr + 1 ): return True a_ : Tuple = 0 return False def _UpperCAmelCase ( __A : int ): a_ : List[str] = [[0 for i in range(__A )] for j in range(__A )] for i in range(__A ): for j in range(__A ): a_ : Optional[Any] = 1 if open_knight_tour_helper(__A , (i, j) , 1 ): return board a_ : Union[str, Any] = 0 a_ : Dict = f'Open Kight Tour cannot be performed on a board of size {n}' raise ValueError(__A ) if __name__ == "__main__": import doctest doctest.testmod()
666
0
'''simple docstring''' import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...utils import logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = { 'BAAI/AltCLIP': 'https://huggingface.co/BAAI/AltCLIP/resolve/main/config.json', # See all AltCLIP models at https://huggingface.co/models?filter=altclip } class SCREAMING_SNAKE_CASE ( lowerCAmelCase__ ): snake_case__ = "altclip_text_model" def __init__( self : str , __SCREAMING_SNAKE_CASE : str=25_0002 , __SCREAMING_SNAKE_CASE : Any=1024 , __SCREAMING_SNAKE_CASE : List[str]=24 , __SCREAMING_SNAKE_CASE : Dict=16 , __SCREAMING_SNAKE_CASE : List[str]=4096 , __SCREAMING_SNAKE_CASE : Dict="gelu" , __SCREAMING_SNAKE_CASE : Optional[Any]=0.1 , __SCREAMING_SNAKE_CASE : Tuple=0.1 , __SCREAMING_SNAKE_CASE : List[Any]=514 , __SCREAMING_SNAKE_CASE : Optional[int]=1 , __SCREAMING_SNAKE_CASE : Optional[Any]=0.02 , __SCREAMING_SNAKE_CASE : Tuple=0.02 , __SCREAMING_SNAKE_CASE : int=1e-05 , __SCREAMING_SNAKE_CASE : Any=1 , __SCREAMING_SNAKE_CASE : Dict=0 , __SCREAMING_SNAKE_CASE : Any=2 , __SCREAMING_SNAKE_CASE : int="absolute" , __SCREAMING_SNAKE_CASE : Dict=True , __SCREAMING_SNAKE_CASE : Union[str, Any]=768 , **__SCREAMING_SNAKE_CASE : Union[str, Any] , ) -> Any: super().__init__(pad_token_id=_lowerCamelCase , bos_token_id=_lowerCamelCase , eos_token_id=_lowerCamelCase , **_lowerCamelCase ) a_ : Tuple = vocab_size a_ : List[str] = hidden_size a_ : List[str] = num_hidden_layers a_ : Tuple = num_attention_heads a_ : Optional[Any] = hidden_act a_ : List[Any] = intermediate_size a_ : Optional[int] = hidden_dropout_prob a_ : Tuple = attention_probs_dropout_prob a_ : str = max_position_embeddings a_ : List[str] = type_vocab_size a_ : Optional[Any] = initializer_range a_ : List[Any] = initializer_factor a_ : Optional[Any] = layer_norm_eps a_ : Optional[Any] = position_embedding_type a_ : Dict = use_cache a_ : Union[str, Any] = project_dim class SCREAMING_SNAKE_CASE ( lowerCAmelCase__ ): snake_case__ = "altclip_vision_model" def __init__( self : str , __SCREAMING_SNAKE_CASE : Optional[Any]=768 , __SCREAMING_SNAKE_CASE : List[Any]=3072 , __SCREAMING_SNAKE_CASE : Union[str, Any]=512 , __SCREAMING_SNAKE_CASE : Optional[int]=12 , __SCREAMING_SNAKE_CASE : Dict=12 , __SCREAMING_SNAKE_CASE : List[Any]=3 , __SCREAMING_SNAKE_CASE : str=224 , __SCREAMING_SNAKE_CASE : Optional[int]=32 , __SCREAMING_SNAKE_CASE : List[Any]="quick_gelu" , __SCREAMING_SNAKE_CASE : Dict=1e-5 , __SCREAMING_SNAKE_CASE : Union[str, Any]=0.0 , __SCREAMING_SNAKE_CASE : Optional[Any]=0.02 , __SCREAMING_SNAKE_CASE : Union[str, Any]=1.0 , **__SCREAMING_SNAKE_CASE : List[Any] , ) -> str: super().__init__(**_lowerCamelCase ) a_ : List[Any] = hidden_size a_ : Optional[Any] = intermediate_size a_ : Any = projection_dim a_ : List[str] = num_hidden_layers a_ : Tuple = num_attention_heads a_ : Dict = num_channels a_ : Optional[Any] = patch_size a_ : Tuple = image_size a_ : Any = initializer_range a_ : Union[str, Any] = initializer_factor a_ : int = attention_dropout a_ : int = layer_norm_eps a_ : str = hidden_act @classmethod def SCREAMING_SNAKE_CASE ( cls : Optional[int] , __SCREAMING_SNAKE_CASE : List[Any] , **__SCREAMING_SNAKE_CASE : Any ) -> "PretrainedConfig": cls._set_token_in_kwargs(_lowerCamelCase ) a_ , a_ : Tuple = cls.get_config_dict(_lowerCamelCase , **_lowerCamelCase ) # get the vision config dict if we are loading from AltCLIPConfig if config_dict.get('''model_type''' ) == "altclip": a_ : List[str] = config_dict['''vision_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f'You are using a model of type {config_dict["model_type"]} to instantiate a model of type ' f'{cls.model_type}. This is not supported for all configurations of models and can yield errors.' ) return cls.from_dict(_lowerCamelCase , **_lowerCamelCase ) class SCREAMING_SNAKE_CASE ( lowerCAmelCase__ ): snake_case__ = "altclip" snake_case__ = True def __init__( self : Optional[Any] , __SCREAMING_SNAKE_CASE : Union[str, Any]=None , __SCREAMING_SNAKE_CASE : Union[str, Any]=None , __SCREAMING_SNAKE_CASE : int=768 , __SCREAMING_SNAKE_CASE : int=2.6592 , **__SCREAMING_SNAKE_CASE : Dict ) -> List[Any]: a_ : List[str] = kwargs.pop('''text_config_dict''' , _lowerCamelCase ) a_ : Tuple = kwargs.pop('''vision_config_dict''' , _lowerCamelCase ) super().__init__(**_lowerCamelCase ) # Instead of simply assigning `[text|vision]_config_dict` to `[text|vision]_config`, we use the values in # `[text|vision]_config_dict` to update the values in `[text|vision]_config`. The values should be same in most # cases, but we don't want to break anything regarding `_config_dict` that existed before commit `8827e1b2`. if text_config_dict is not None: if text_config is None: a_ : str = {} # This is the complete result when using `text_config_dict`. a_ : List[str] = AltCLIPTextConfig(**_lowerCamelCase ).to_dict() # Give a warning if the values exist in both `_text_config_dict` and `text_config` but being different. for key, value in _text_config_dict.items(): if key in text_config and value != text_config[key] and key not in ["transformers_version"]: # If specified in `text_config_dict` if key in text_config_dict: a_ : Any = ( f'`{key}` is found in both `text_config_dict` and `text_config` but with different values. ' f'The value `text_config_dict[\"{key}\"]` will be used instead.' ) # If inferred from default argument values (just to be super careful) else: a_ : List[str] = ( f'`text_config_dict` is provided which will be used to initialize `AltCLIPTextConfig`. The ' f'value `text_config[\"{key}\"]` will be overriden.' ) logger.warning(_lowerCamelCase ) # Update all values in `text_config` with the ones in `_text_config_dict`. text_config.update(_text_config_dict ) if vision_config_dict is not None: if vision_config is None: a_ : str = {} # This is the complete result when using `vision_config_dict`. a_ : Optional[int] = AltCLIPVisionConfig(**_lowerCamelCase ).to_dict() # convert keys to string instead of integer if "id2label" in _vision_config_dict: a_ : Any = { str(_lowerCamelCase ): value for key, value in _vision_config_dict['''id2label'''].items() } # Give a warning if the values exist in both `_vision_config_dict` and `vision_config` but being different. for key, value in _vision_config_dict.items(): if key in vision_config and value != vision_config[key] and key not in ["transformers_version"]: # If specified in `vision_config_dict` if key in vision_config_dict: a_ : str = ( f'`{key}` is found in both `vision_config_dict` and `vision_config` but with different ' f'values. The value `vision_config_dict[\"{key}\"]` will be used instead.' ) # If inferred from default argument values (just to be super careful) else: a_ : str = ( f'`vision_config_dict` is provided which will be used to initialize `AltCLIPVisionConfig`. ' f'The value `vision_config[\"{key}\"]` will be overriden.' ) logger.warning(_lowerCamelCase ) # Update all values in `vision_config` with the ones in `_vision_config_dict`. vision_config.update(_vision_config_dict ) if text_config is None: a_ : Any = {} logger.info('''`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values.''' ) if vision_config is None: a_ : List[Any] = {} logger.info('''`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values.''' ) a_ : List[Any] = AltCLIPTextConfig(**_lowerCamelCase ) a_ : int = AltCLIPVisionConfig(**_lowerCamelCase ) a_ : List[Any] = projection_dim a_ : int = logit_scale_init_value a_ : int = 1.0 @classmethod def SCREAMING_SNAKE_CASE ( cls : Any , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : Dict , **__SCREAMING_SNAKE_CASE : Dict ) -> List[Any]: return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **_lowerCamelCase ) def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> List[Any]: a_ : List[Any] = copy.deepcopy(self.__dict__ ) a_ : Tuple = self.text_config.to_dict() a_ : List[str] = self.vision_config.to_dict() a_ : Any = self.__class__.model_type return output
712
'''simple docstring''' import warnings warnings.warn( 'memory_utils has been reorganized to utils.memory. Import `find_executable_batchsize` from the main `__init__`: ' '`from accelerate import find_executable_batch_size` to avoid this warning.', FutureWarning, )
666
0
'''simple docstring''' import json import os import re import unittest from transformers import CodeGenTokenizer, CodeGenTokenizerFast from transformers.models.codegen.tokenization_codegen import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class SCREAMING_SNAKE_CASE ( _A , unittest.TestCase ): snake_case__ = CodeGenTokenizer snake_case__ = CodeGenTokenizerFast snake_case__ = True snake_case__ = {"add_prefix_space": True} snake_case__ = False def SCREAMING_SNAKE_CASE ( self : List[str] ) -> List[Any]: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt a_ : Tuple = [ "l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "\u0120", "\u0120l", "\u0120n", "\u0120lo", "\u0120low", "er", "\u0120lowest", "\u0120newer", "\u0120wider", "<unk>", "<|endoftext|>", ] a_ : List[str] = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) a_ : Optional[int] = ["#version: 0.2", "\u0120 l", "\u0120l o", "\u0120lo w", "e r", ""] a_ : str = {"unk_token": "<unk>"} a_ : Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) a_ : str = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : List[str] , **__SCREAMING_SNAKE_CASE : Union[str, Any] ) -> Any: kwargs.update(self.special_tokens_map ) return CodeGenTokenizer.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : int , **__SCREAMING_SNAKE_CASE : List[Any] ) -> List[Any]: kwargs.update(self.special_tokens_map ) return CodeGenTokenizerFast.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Any , __SCREAMING_SNAKE_CASE : Union[str, Any] ) -> Tuple: a_ : Any = "lower newer" a_ : Dict = "lower newer" return input_text, output_text def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Optional[int]: a_ : Optional[Any] = CodeGenTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) a_ : Optional[Any] = "lower newer" a_ : Optional[int] = ["\u0120low", "er", "\u0120", "n", "e", "w", "er"] a_ : List[str] = tokenizer.tokenize(__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : List[str] = tokens + [tokenizer.unk_token] a_ : List[Any] = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Any: if not self.test_rust_tokenizer: return a_ : str = self.get_tokenizer() a_ : List[Any] = self.get_rust_tokenizer(add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Tuple = "lower newer" # Testing tokenization a_ : Optional[Any] = tokenizer.tokenize(__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = rust_tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Testing conversion to ids without special tokens a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : List[Any] = rust_tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Testing conversion to ids with special tokens a_ : Any = self.get_rust_tokenizer(add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : int = rust_tokenizer.encode(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Testing the unknown token a_ : Tuple = tokens + [rust_tokenizer.unk_token] a_ : int = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(rust_tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : str , *__SCREAMING_SNAKE_CASE : Dict , **__SCREAMING_SNAKE_CASE : int ) -> Union[str, Any]: # It's very difficult to mix/test pretokenization with byte-level # And get both CodeGen and Roberta to work at the same time (mostly an issue of adding a space before the string) pass def SCREAMING_SNAKE_CASE ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : Union[str, Any]=15 ) -> List[str]: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): a_ : Dict = self.rust_tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) # Simple input a_ : List[str] = "This is a simple input" a_ : int = ["This is a simple input 1", "This is a simple input 2"] a_ : Dict = ("This is a simple input", "This is a pair") a_ : Dict = [ ("This is a simple input 1", "This is a simple input 2"), ("This is a simple pair 1", "This is a simple pair 2"), ] # Simple input tests self.assertRaises(__SCREAMING_SNAKE_CASE , tokenizer_r.encode , __SCREAMING_SNAKE_CASE , max_length=__SCREAMING_SNAKE_CASE , padding='''max_length''' ) # Simple input self.assertRaises(__SCREAMING_SNAKE_CASE , tokenizer_r.encode_plus , __SCREAMING_SNAKE_CASE , max_length=__SCREAMING_SNAKE_CASE , padding='''max_length''' ) # Simple input self.assertRaises( __SCREAMING_SNAKE_CASE , tokenizer_r.batch_encode_plus , __SCREAMING_SNAKE_CASE , max_length=__SCREAMING_SNAKE_CASE , padding='''max_length''' , ) # Pair input self.assertRaises(__SCREAMING_SNAKE_CASE , tokenizer_r.encode , __SCREAMING_SNAKE_CASE , max_length=__SCREAMING_SNAKE_CASE , padding='''max_length''' ) # Pair input self.assertRaises(__SCREAMING_SNAKE_CASE , tokenizer_r.encode_plus , __SCREAMING_SNAKE_CASE , max_length=__SCREAMING_SNAKE_CASE , padding='''max_length''' ) # Pair input self.assertRaises( __SCREAMING_SNAKE_CASE , tokenizer_r.batch_encode_plus , __SCREAMING_SNAKE_CASE , max_length=__SCREAMING_SNAKE_CASE , padding='''max_length''' , ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> int: a_ : Tuple = CodeGenTokenizer.from_pretrained(self.tmpdirname , pad_token='''<pad>''' ) # Simple input a_ : List[Any] = "This is a simple input" a_ : Union[str, Any] = ["This is a simple input looooooooong", "This is a simple input"] a_ : int = ("This is a simple input", "This is a pair") a_ : Union[str, Any] = [ ("This is a simple input loooooong", "This is a simple input"), ("This is a simple pair loooooong", "This is a simple pair"), ] a_ : Tuple = tokenizer.pad_token_id a_ : Optional[Any] = tokenizer(__SCREAMING_SNAKE_CASE , padding='''max_length''' , max_length=30 , return_tensors='''np''' ) a_ : List[Any] = tokenizer(__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , truncate=__SCREAMING_SNAKE_CASE , return_tensors='''np''' ) a_ : Any = tokenizer(*__SCREAMING_SNAKE_CASE , padding='''max_length''' , max_length=60 , return_tensors='''np''' ) a_ : List[str] = tokenizer(__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , truncate=__SCREAMING_SNAKE_CASE , return_tensors='''np''' ) # s # test single string max_length padding self.assertEqual(out_s['''input_ids'''].shape[-1] , 30 ) self.assertTrue(pad_token_id in out_s['''input_ids'''] ) self.assertTrue(0 in out_s['''attention_mask'''] ) # s2 # test automatic padding self.assertEqual(out_sa['''input_ids'''].shape[-1] , 33 ) # long slice doesn't have padding self.assertFalse(pad_token_id in out_sa['''input_ids'''][0] ) self.assertFalse(0 in out_sa['''attention_mask'''][0] ) # short slice does have padding self.assertTrue(pad_token_id in out_sa['''input_ids'''][1] ) self.assertTrue(0 in out_sa['''attention_mask'''][1] ) # p # test single pair max_length padding self.assertEqual(out_p['''input_ids'''].shape[-1] , 60 ) self.assertTrue(pad_token_id in out_p['''input_ids'''] ) self.assertTrue(0 in out_p['''attention_mask'''] ) # p2 # test automatic padding pair self.assertEqual(out_pa['''input_ids'''].shape[-1] , 52 ) # long slice pair doesn't have padding self.assertFalse(pad_token_id in out_pa['''input_ids'''][0] ) self.assertFalse(0 in out_pa['''attention_mask'''][0] ) # short slice pair does have padding self.assertTrue(pad_token_id in out_pa['''input_ids'''][1] ) self.assertTrue(0 in out_pa['''attention_mask'''][1] ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> str: a_ : List[str] = "$$$" a_ : Dict = CodeGenTokenizer.from_pretrained(self.tmpdirname , bos_token=__SCREAMING_SNAKE_CASE , add_bos_token=__SCREAMING_SNAKE_CASE ) a_ : int = "This is a simple input" a_ : Optional[Any] = ["This is a simple input 1", "This is a simple input 2"] a_ : Any = tokenizer.bos_token_id a_ : Optional[Any] = tokenizer(__SCREAMING_SNAKE_CASE ) a_ : int = tokenizer(__SCREAMING_SNAKE_CASE ) self.assertEqual(out_s.input_ids[0] , __SCREAMING_SNAKE_CASE ) self.assertTrue(all(o[0] == bos_token_id for o in out_sa.input_ids ) ) a_ : int = tokenizer.decode(out_s.input_ids ) a_ : Tuple = tokenizer.batch_decode(out_sa.input_ids ) self.assertEqual(decode_s.split()[0] , __SCREAMING_SNAKE_CASE ) self.assertTrue(all(d.split()[0] == bos_token for d in decode_sa ) ) @slow def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : List[Any] = CodeGenTokenizer.from_pretrained('''Salesforce/codegen-350M-mono''' ) a_ : List[str] = "\nif len_a > len_b:\n result = a\nelse:\n result = b\n\n\n\n#" a_ : str = "\nif len_a > len_b: result = a\nelse: result = b" a_ : str = tokenizer.encode(__SCREAMING_SNAKE_CASE ) a_ : Dict = ["^#", re.escape('''<|endoftext|>''' ), "^'''", "^\"\"\"", "\n\n\n"] a_ : str = tokenizer.decode(__SCREAMING_SNAKE_CASE , truncate_before_pattern=__SCREAMING_SNAKE_CASE ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> str: pass
713
'''simple docstring''' import requests from bsa import BeautifulSoup def _UpperCAmelCase ( __A : str , __A : dict ): a_ : Tuple = BeautifulSoup(requests.get(__A , params=__A ).content , '''html.parser''' ) a_ : List[str] = soup.find('''div''' , attrs={'''class''': '''gs_ri'''} ) a_ : List[str] = div.find('''div''' , attrs={'''class''': '''gs_fl'''} ).find_all('''a''' ) return anchors[2].get_text() if __name__ == "__main__": __lowerCAmelCase = { 'title': ( 'Precisely geometry controlled microsupercapacitors for ultrahigh areal ' 'capacitance, volumetric capacitance, and energy density' ), 'journal': 'Chem. Mater.', 'volume': 30, 'pages': '3979-3990', 'year': 2_018, 'hl': 'en', } print(get_citation('https://scholar.google.com/scholar_lookup', params=params))
666
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCAmelCase = { 'configuration_time_series_transformer': [ 'TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'TimeSeriesTransformerConfig', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ 'TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'TimeSeriesTransformerForPrediction', 'TimeSeriesTransformerModel', 'TimeSeriesTransformerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimeSeriesTransformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimeSeriesTransformerForPrediction, TimeSeriesTransformerModel, TimeSeriesTransformerPreTrainedModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
714
'''simple docstring''' import argparse from torch import nn # transformers_old should correspond to branch `save_old_prophetnet_model_structure` here # original prophetnet_checkpoints are saved under `patrickvonplaten/..._old` respectively from transformers_old.modeling_prophetnet import ( ProphetNetForConditionalGeneration as ProphetNetForConditionalGenerationOld, ) from transformers_old.modeling_xlm_prophetnet import ( XLMProphetNetForConditionalGeneration as XLMProphetNetForConditionalGenerationOld, ) from transformers import ProphetNetForConditionalGeneration, XLMProphetNetForConditionalGeneration, logging __lowerCAmelCase = logging.get_logger(__name__) logging.set_verbosity_info() def _UpperCAmelCase ( __A : str , __A : str ): if "xprophetnet" in prophetnet_checkpoint_path: a_ : Tuple = XLMProphetNetForConditionalGenerationOld.from_pretrained(__A ) a_ , a_ : Optional[Any] = XLMProphetNetForConditionalGeneration.from_pretrained( __A , output_loading_info=__A ) else: a_ : List[Any] = ProphetNetForConditionalGenerationOld.from_pretrained(__A ) a_ , a_ : Any = ProphetNetForConditionalGeneration.from_pretrained( __A , output_loading_info=__A ) a_ : str = ['''key_proj''', '''value_proj''', '''query_proj'''] a_ : Tuple = { '''self_attn''': '''ngram_self_attn''', '''cross_attn''': '''encoder_attn''', '''cross_attn_layer_norm''': '''encoder_attn_layer_norm''', '''feed_forward_layer_norm''': '''final_layer_norm''', '''feed_forward''': '''''', '''intermediate''': '''fc1''', '''output''': '''fc2''', '''key_proj''': '''k_proj''', '''query_proj''': '''q_proj''', '''value_proj''': '''v_proj''', '''word_embeddings''': '''embed_tokens''', '''embeddings_layer_norm''': '''emb_layer_norm''', '''relative_pos_embeddings''': '''relative_linear''', '''ngram_embeddings''': '''ngram_input_embed''', '''position_embeddings''': '''embed_positions''', } for key in loading_info["missing_keys"]: a_ : List[str] = key.split('''.''' ) if attributes[0] == "lm_head": a_ : List[str] = prophet a_ : Dict = prophet_old else: a_ : str = prophet.prophetnet a_ : int = prophet_old.model a_ : str = False for attribute in attributes: if attribute in mapping: a_ : Dict = mapping[attribute] if not hasattr(__A , __A ) and len(__A ) > 0: a_ : List[str] = attribute elif hasattr(__A , __A ): a_ : Union[str, Any] = attribute if attribute == "weight": assert old_model.weight.shape == model.weight.shape, "Shapes have to match!" a_ : Tuple = old_model.weight logger.info(f'{attribute} is initialized.' ) a_ : Union[str, Any] = True break elif attribute == "bias": assert old_model.bias.shape == model.bias.shape, "Shapes have to match!" a_ : Union[str, Any] = old_model.bias logger.info(f'{attribute} is initialized' ) a_ : Dict = True break elif attribute in special_keys and hasattr(__A , '''in_proj_weight''' ): a_ : Tuple = old_model.in_proj_weight.shape[0] // 3 a_ : Any = getattr(__A , __A ) param.weight.shape == old_model.in_proj_weight[:embed_dim, :].shape, "Shapes have to match" param.bias.shape == old_model.in_proj_bias[:embed_dim].shape, "Shapes have to match" if attribute == "query_proj": a_ : Union[str, Any] = nn.Parameter(old_model.in_proj_weight[:embed_dim, :] ) a_ : Optional[Any] = nn.Parameter(old_model.in_proj_bias[:embed_dim] ) elif attribute == "key_proj": a_ : List[Any] = nn.Parameter(old_model.in_proj_weight[embed_dim : 2 * embed_dim, :] ) a_ : Optional[int] = nn.Parameter(old_model.in_proj_bias[embed_dim : 2 * embed_dim] ) elif attribute == "value_proj": a_ : Tuple = nn.Parameter(old_model.in_proj_weight[2 * embed_dim :, :] ) a_ : Any = nn.Parameter(old_model.in_proj_bias[2 * embed_dim :] ) a_ : Dict = True break elif attribute == "position_embeddings": assert ( model.position_embeddings.weight.shape[-1] == old_model.embed_positions.weight.shape[-1] ), "Hidden size has to match" assert model.position_embeddings.weight.shape[0] == 5_12, "We want 512 position_embeddings." a_ : Union[str, Any] = nn.Parameter(old_model.embed_positions.weight[:5_12, :] ) a_ : Optional[Any] = True break if attribute.isdigit(): a_ : Union[str, Any] = model[int(__A )] a_ : str = old_model[int(__A )] else: a_ : Tuple = getattr(__A , __A ) if old_attribute == "": a_ : List[str] = old_model else: if not hasattr(__A , __A ): raise ValueError(f'{old_model} does not have {old_attribute}' ) a_ : Optional[Any] = getattr(__A , __A ) if not is_key_init: raise ValueError(f'{key} was not correctly initialized!' ) print(f'Saving model to {pytorch_dump_folder_path}' ) prophet.save_pretrained(__A ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '--prophetnet_checkpoint_path', default=None, type=str, required=True, help='Path the official PyTorch dump.' ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) __lowerCAmelCase = parser.parse_args() convert_prophetnet_checkpoint_to_pytorch(args.prophetnet_checkpoint_path, args.pytorch_dump_folder_path)
666
0
import argparse import os # New Code # import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils import find_executable_batch_size ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to ensure out-of-memory errors never # interrupt training, and builds off the `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## __lowerCAmelCase = 16 __lowerCAmelCase = 32 def _UpperCAmelCase ( __A : Accelerator , __A : int = 16 ): a_ : int = AutoTokenizer.from_pretrained('''bert-base-cased''' ) a_ : List[Any] = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(__A : Optional[Any] ): # max_length=None => use the model max length (it's actually the default) a_ : Tuple = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=UpperCamelCase__ , max_length=UpperCamelCase__ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): a_ : List[str] = datasets.map( UpperCamelCase__ , batched=UpperCamelCase__ , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library a_ : Any = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(__A : str ): # On TPU it's best to pad everything to the same length or training will be very slow. a_ : List[str] = 1_28 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": a_ : Any = 16 elif accelerator.mixed_precision != "no": a_ : List[Any] = 8 else: a_ : Tuple = None return tokenizer.pad( UpperCamelCase__ , padding='''longest''' , max_length=UpperCamelCase__ , pad_to_multiple_of=UpperCamelCase__ , return_tensors='''pt''' , ) # Instantiate dataloaders. a_ : List[str] = DataLoader( tokenized_datasets['''train'''] , shuffle=UpperCamelCase__ , collate_fn=UpperCamelCase__ , batch_size=UpperCamelCase__ ) a_ : Union[str, Any] = DataLoader( tokenized_datasets['''validation'''] , shuffle=UpperCamelCase__ , collate_fn=UpperCamelCase__ , batch_size=UpperCamelCase__ ) return train_dataloader, eval_dataloader # For testing only if os.environ.get('TESTING_MOCKED_DATALOADERS', None) == "1": from accelerate.test_utils.training import mocked_dataloaders __lowerCAmelCase = mocked_dataloaders # noqa: F811 def _UpperCAmelCase ( __A : str , __A : str ): # For testing only if os.environ.get('''TESTING_MOCKED_DATALOADERS''' , UpperCamelCase__ ) == "1": a_ : Tuple = 2 # Initialize accelerator a_ : Optional[int] = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs a_ : Any = config['''lr'''] a_ : Dict = int(config['''num_epochs'''] ) a_ : Union[str, Any] = int(config['''seed'''] ) a_ : List[str] = int(config['''batch_size'''] ) a_ : List[str] = evaluate.load('''glue''' , '''mrpc''' ) # New Code # # We now can define an inner training loop function. It should take a batch size as the only parameter, # and build the dataloaders in there. # It also gets our decorator @find_executable_batch_size(starting_batch_size=UpperCamelCase__ ) def inner_training_loop(__A : Any ): # And now just move everything below under this function # We need to bring in the Accelerator object from earlier nonlocal accelerator # And reset all of its attributes that could hold onto any memory: accelerator.free_memory() # Then we can declare the model, optimizer, and everything else: set_seed(UpperCamelCase__ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) a_ : Optional[int] = AutoModelForSequenceClassification.from_pretrained('''bert-base-cased''' , return_dict=UpperCamelCase__ ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). a_ : str = model.to(accelerator.device ) # Instantiate optimizer a_ : List[str] = AdamW(params=model.parameters() , lr=UpperCamelCase__ ) a_ , a_ : List[Any] = get_dataloaders(UpperCamelCase__ , UpperCamelCase__ ) # Instantiate scheduler a_ : Optional[Any] = get_linear_schedule_with_warmup( optimizer=UpperCamelCase__ , num_warmup_steps=1_00 , num_training_steps=(len(UpperCamelCase__ ) * num_epochs) , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. a_ , a_ , a_ , a_ , a_ : Union[str, Any] = accelerator.prepare( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) # Now we train the model for epoch in range(UpperCamelCase__ ): model.train() for step, batch in enumerate(UpperCamelCase__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) a_ : Tuple = model(**UpperCamelCase__ ) a_ : List[Any] = outputs.loss accelerator.backward(UpperCamelCase__ ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(UpperCamelCase__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): a_ : Union[str, Any] = model(**UpperCamelCase__ ) a_ : Optional[Any] = outputs.logits.argmax(dim=-1 ) a_ , a_ : List[str] = accelerator.gather_for_metrics((predictions, batch['''labels''']) ) metric.add_batch( predictions=UpperCamelCase__ , references=UpperCamelCase__ , ) a_ : Any = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f'epoch {epoch}:' , UpperCamelCase__ ) # New Code # # And call it at the end with no arguments # Note: You could also refactor this outside of your training loop function inner_training_loop() def _UpperCAmelCase ( ): a_ : str = argparse.ArgumentParser(description='''Simple example of training script.''' ) parser.add_argument( '''--mixed_precision''' , type=UpperCamelCase__ , default=UpperCamelCase__ , choices=['''no''', '''fp16''', '''bf16''', '''fp8'''] , help='''Whether to use mixed precision. Choose''' '''between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.''' '''and an Nvidia Ampere GPU.''' , ) parser.add_argument('''--cpu''' , action='''store_true''' , help='''If passed, will train on the CPU.''' ) a_ : Optional[Any] = parser.parse_args() a_ : Union[str, Any] = {'''lr''': 2E-5, '''num_epochs''': 3, '''seed''': 42, '''batch_size''': 16} training_function(UpperCamelCase__ , UpperCamelCase__ ) if __name__ == "__main__": main()
715
'''simple docstring''' import re import string import numpy as np import datasets __lowerCAmelCase = '\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n' __lowerCAmelCase = '\nArgs:\n predictions: List of predicted texts.\n references: List of reference texts.\n regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n ignore when calculating the exact matches. Note: these regexes are removed\n from the input data before the changes based on the options below (e.g. ignore_case,\n ignore_punctuation, ignore_numbers) are applied.\n ignore_case: Boolean, defaults to False. If true, turns everything\n to lowercase so that capitalization differences are ignored.\n ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\n ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\nReturns:\n exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 25.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 50.0\n\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 75.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n >>> print(round(results["exact_match"], 1))\n 100.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["The cat sat on the mat.", "Theaters are great.", "It\'s like comparing oranges and apples."]\n >>> preds = ["The cat sat on the mat?", "Theaters are great.", "It\'s like comparing apples and oranges."]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 33.3\n\n' __lowerCAmelCase = '\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class SCREAMING_SNAKE_CASE ( datasets.Metric ): def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> int: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , reference_urls=[] , ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Dict=None , __SCREAMING_SNAKE_CASE : int=False , __SCREAMING_SNAKE_CASE : Optional[int]=False , __SCREAMING_SNAKE_CASE : Dict=False , ) -> str: if regexes_to_ignore is not None: for s in regexes_to_ignore: a_ : Optional[Any] = np.array([re.sub(__SCREAMING_SNAKE_CASE , '''''' , __SCREAMING_SNAKE_CASE ) for x in predictions] ) a_ : int = np.array([re.sub(__SCREAMING_SNAKE_CASE , '''''' , __SCREAMING_SNAKE_CASE ) for x in references] ) else: a_ : List[str] = np.asarray(__SCREAMING_SNAKE_CASE ) a_ : Any = np.asarray(__SCREAMING_SNAKE_CASE ) if ignore_case: a_ : List[str] = np.char.lower(__SCREAMING_SNAKE_CASE ) a_ : List[Any] = np.char.lower(__SCREAMING_SNAKE_CASE ) if ignore_punctuation: a_ : Any = string.punctuation.maketrans('''''' , '''''' , string.punctuation ) a_ : Union[str, Any] = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : int = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) if ignore_numbers: a_ : int = string.digits.maketrans('''''' , '''''' , string.digits ) a_ : Optional[int] = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : Dict = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = predictions == references return {"exact_match": np.mean(__SCREAMING_SNAKE_CASE ) * 100}
666
0
'''simple docstring''' import warnings from ..trainer import Trainer from ..utils import logging __lowerCAmelCase = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE ( __UpperCAmelCase ): def __init__( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Any=None , **__SCREAMING_SNAKE_CASE : int ) -> List[Any]: warnings.warn( '''`SageMakerTrainer` is deprecated and will be removed in v5 of Transformers. You can use `Trainer` ''' '''instead.''' , __SCREAMING_SNAKE_CASE , ) super().__init__(args=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE )
716
'''simple docstring''' import gc import unittest import numpy as np import torch from diffusers import ( AudioDiffusionPipeline, AutoencoderKL, DDIMScheduler, DDPMScheduler, DiffusionPipeline, Mel, UNetaDConditionModel, UNetaDModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : Dict ) -> List[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> List[Any]: torch.manual_seed(0 ) a_ : Tuple = UNetaDModel( sample_size=(32, 64) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''') , ) return model @property def SCREAMING_SNAKE_CASE ( self : int ) -> List[Any]: torch.manual_seed(0 ) a_ : Any = UNetaDConditionModel( sample_size=(64, 32) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''CrossAttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''CrossAttnUpBlock2D''') , cross_attention_dim=10 , ) return model @property def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: torch.manual_seed(0 ) a_ : List[Any] = AutoencoderKL( sample_size=(128, 64) , in_channels=1 , out_channels=1 , latent_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''DownEncoderBlock2D''', '''DownEncoderBlock2D''') , up_block_types=('''UpDecoderBlock2D''', '''UpDecoderBlock2D''') , ) a_ : List[Any] = UNetaDModel( sample_size=(64, 32) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''') , ) return vqvae, unet @slow def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union[str, Any]: a_ : Dict = '''cpu''' # ensure determinism for the device-dependent torch.Generator a_ : Union[str, Any] = Mel( x_res=self.dummy_unet.config.sample_size[1] , y_res=self.dummy_unet.config.sample_size[0] , ) a_ : Any = DDPMScheduler() a_ : str = AudioDiffusionPipeline(vqvae=__SCREAMING_SNAKE_CASE , unet=self.dummy_unet , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : List[Any] = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : List[str] = pipe(generator=__SCREAMING_SNAKE_CASE , steps=4 ) a_ : List[Any] = output.audios[0] a_ : Dict = output.images[0] a_ : Dict = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : Optional[Any] = pipe(generator=__SCREAMING_SNAKE_CASE , steps=4 , return_dict=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output[0][0] assert audio.shape == (1, (self.dummy_unet.config.sample_size[1] - 1) * mel.hop_length) assert ( image.height == self.dummy_unet.config.sample_size[0] and image.width == self.dummy_unet.config.sample_size[1] ) a_ : Dict = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : str = np.frombuffer(image_from_tuple.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([69, 255, 255, 255, 0, 0, 77, 181, 12, 127] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() == 0 a_ : str = Mel( x_res=self.dummy_vqvae_and_unet[0].config.sample_size[1] , y_res=self.dummy_vqvae_and_unet[0].config.sample_size[0] , ) a_ : int = DDIMScheduler() a_ : Dict = self.dummy_vqvae_and_unet a_ : List[str] = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0] , unet=dummy_vqvae_and_unet[1] , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : Any = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) np.random.seed(0 ) a_ : List[str] = np.random.uniform(-1 , 1 , ((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,) ) a_ : int = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : int = pipe(raw_audio=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , start_step=5 , steps=10 ) a_ : List[str] = output.images[0] assert ( image.height == self.dummy_vqvae_and_unet[0].config.sample_size[0] and image.width == self.dummy_vqvae_and_unet[0].config.sample_size[1] ) a_ : Optional[Any] = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([120, 117, 110, 109, 138, 167, 138, 148, 132, 121] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 a_ : List[str] = self.dummy_unet_condition a_ : Dict = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0] , unet=__SCREAMING_SNAKE_CASE , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : int = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) np.random.seed(0 ) a_ : Any = torch.rand((1, 1, 10) ) a_ : Tuple = pipe(generator=__SCREAMING_SNAKE_CASE , encoding=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output.images[0] a_ : Dict = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([107, 103, 120, 127, 142, 122, 113, 122, 97, 111] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 @slow @require_torch_gpu class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Any: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Any: a_ : Any = torch_device a_ : Optional[int] = DiffusionPipeline.from_pretrained('''teticio/audio-diffusion-ddim-256''' ) a_ : Dict = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : str = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : List[str] = pipe(generator=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output.audios[0] a_ : Tuple = output.images[0] assert audio.shape == (1, (pipe.unet.config.sample_size[1] - 1) * pipe.mel.hop_length) assert image.height == pipe.unet.config.sample_size[0] and image.width == pipe.unet.config.sample_size[1] a_ : str = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : Tuple = np.array([151, 167, 154, 144, 122, 134, 121, 105, 70, 26] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0
666
0
def _UpperCAmelCase ( __A : Any ): a_ : Any = [0] * len(__A ) a_ : List[str] = [] a_ : List[str] = [1] * len(__A ) for values in graph.values(): for i in values: indegree[i] += 1 for i in range(len(__A ) ): if indegree[i] == 0: queue.append(__A ) while queue: a_ : Optional[Any] = queue.pop(0 ) for x in graph[vertex]: indegree[x] -= 1 if long_dist[vertex] + 1 > long_dist[x]: a_ : int = long_dist[vertex] + 1 if indegree[x] == 0: queue.append(__A ) print(max(__A ) ) # Adjacency list of Graph __lowerCAmelCase = {0: [2, 3, 4], 1: [2, 7], 2: [5], 3: [5, 7], 4: [7], 5: [6], 6: [7], 7: []} longest_distance(graph)
717
'''simple docstring''' import argparse import json from pathlib import Path import requests import torch from huggingface_hub import cached_download, hf_hub_download, hf_hub_url from PIL import Image from transformers import DetaConfig, DetaForObjectDetection, DetaImageProcessor, SwinConfig from transformers.utils import logging logging.set_verbosity_info() __lowerCAmelCase = logging.get_logger(__name__) def _UpperCAmelCase ( __A : Union[str, Any] ): a_ : Tuple = SwinConfig( embed_dim=1_92 , depths=(2, 2, 18, 2) , num_heads=(6, 12, 24, 48) , window_size=12 , out_features=['''stage2''', '''stage3''', '''stage4'''] , ) a_ : List[Any] = DetaConfig( backbone_config=__A , num_queries=9_00 , encoder_ffn_dim=20_48 , decoder_ffn_dim=20_48 , num_feature_levels=5 , assign_first_stage=__A , with_box_refine=__A , two_stage=__A , ) # set labels a_ : Optional[Any] = '''huggingface/label-files''' if "o365" in model_name: a_ : Optional[Any] = 3_66 a_ : Tuple = '''object365-id2label.json''' else: a_ : Any = 91 a_ : Union[str, Any] = '''coco-detection-id2label.json''' a_ : Tuple = num_labels a_ : str = json.load(open(cached_download(hf_hub_url(__A , __A , repo_type='''dataset''' ) ) , '''r''' ) ) a_ : Optional[int] = {int(__A ): v for k, v in idalabel.items()} a_ : int = idalabel a_ : Dict = {v: k for k, v in idalabel.items()} return config def _UpperCAmelCase ( __A : List[str] ): a_ : Tuple = [] # stem # fmt: off rename_keys.append(('''backbone.0.body.patch_embed.proj.weight''', '''model.backbone.model.embeddings.patch_embeddings.projection.weight''') ) rename_keys.append(('''backbone.0.body.patch_embed.proj.bias''', '''model.backbone.model.embeddings.patch_embeddings.projection.bias''') ) rename_keys.append(('''backbone.0.body.patch_embed.norm.weight''', '''model.backbone.model.embeddings.norm.weight''') ) rename_keys.append(('''backbone.0.body.patch_embed.norm.bias''', '''model.backbone.model.embeddings.norm.bias''') ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm1.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm1.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_bias_table', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_index', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.proj.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.proj.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm2.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm2.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.bias') ) if i < 3: rename_keys.append((f'backbone.0.body.layers.{i}.downsample.reduction.weight', f'model.backbone.model.encoder.layers.{i}.downsample.reduction.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.downsample.norm.weight', f'model.backbone.model.encoder.layers.{i}.downsample.norm.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.downsample.norm.bias', f'model.backbone.model.encoder.layers.{i}.downsample.norm.bias') ) rename_keys.append(('''backbone.0.body.norm1.weight''', '''model.backbone.model.hidden_states_norms.stage2.weight''') ) rename_keys.append(('''backbone.0.body.norm1.bias''', '''model.backbone.model.hidden_states_norms.stage2.bias''') ) rename_keys.append(('''backbone.0.body.norm2.weight''', '''model.backbone.model.hidden_states_norms.stage3.weight''') ) rename_keys.append(('''backbone.0.body.norm2.bias''', '''model.backbone.model.hidden_states_norms.stage3.bias''') ) rename_keys.append(('''backbone.0.body.norm3.weight''', '''model.backbone.model.hidden_states_norms.stage4.weight''') ) rename_keys.append(('''backbone.0.body.norm3.bias''', '''model.backbone.model.hidden_states_norms.stage4.bias''') ) # transformer encoder for i in range(config.encoder_layers ): rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.sampling_offsets.weight', f'model.encoder.layers.{i}.self_attn.sampling_offsets.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.sampling_offsets.bias', f'model.encoder.layers.{i}.self_attn.sampling_offsets.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.attention_weights.weight', f'model.encoder.layers.{i}.self_attn.attention_weights.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.attention_weights.bias', f'model.encoder.layers.{i}.self_attn.attention_weights.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.value_proj.weight', f'model.encoder.layers.{i}.self_attn.value_proj.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.value_proj.bias', f'model.encoder.layers.{i}.self_attn.value_proj.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.output_proj.weight', f'model.encoder.layers.{i}.self_attn.output_proj.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.output_proj.bias', f'model.encoder.layers.{i}.self_attn.output_proj.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm1.weight', f'model.encoder.layers.{i}.self_attn_layer_norm.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm1.bias', f'model.encoder.layers.{i}.self_attn_layer_norm.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear1.weight', f'model.encoder.layers.{i}.fc1.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear1.bias', f'model.encoder.layers.{i}.fc1.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear2.weight', f'model.encoder.layers.{i}.fc2.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear2.bias', f'model.encoder.layers.{i}.fc2.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm2.weight', f'model.encoder.layers.{i}.final_layer_norm.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm2.bias', f'model.encoder.layers.{i}.final_layer_norm.bias') ) # transformer decoder for i in range(config.decoder_layers ): rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.sampling_offsets.weight', f'model.decoder.layers.{i}.encoder_attn.sampling_offsets.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.sampling_offsets.bias', f'model.decoder.layers.{i}.encoder_attn.sampling_offsets.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.attention_weights.weight', f'model.decoder.layers.{i}.encoder_attn.attention_weights.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.attention_weights.bias', f'model.decoder.layers.{i}.encoder_attn.attention_weights.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.value_proj.weight', f'model.decoder.layers.{i}.encoder_attn.value_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.value_proj.bias', f'model.decoder.layers.{i}.encoder_attn.value_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.output_proj.weight', f'model.decoder.layers.{i}.encoder_attn.output_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.output_proj.bias', f'model.decoder.layers.{i}.encoder_attn.output_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm1.weight', f'model.decoder.layers.{i}.encoder_attn_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm1.bias', f'model.decoder.layers.{i}.encoder_attn_layer_norm.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.self_attn.out_proj.weight', f'model.decoder.layers.{i}.self_attn.out_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.self_attn.out_proj.bias', f'model.decoder.layers.{i}.self_attn.out_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm2.weight', f'model.decoder.layers.{i}.self_attn_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm2.bias', f'model.decoder.layers.{i}.self_attn_layer_norm.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear1.weight', f'model.decoder.layers.{i}.fc1.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear1.bias', f'model.decoder.layers.{i}.fc1.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear2.weight', f'model.decoder.layers.{i}.fc2.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear2.bias', f'model.decoder.layers.{i}.fc2.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm3.weight', f'model.decoder.layers.{i}.final_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm3.bias', f'model.decoder.layers.{i}.final_layer_norm.bias') ) # fmt: on return rename_keys def _UpperCAmelCase ( __A : str , __A : int , __A : Tuple ): a_ : str = dct.pop(__A ) a_ : Dict = val def _UpperCAmelCase ( __A : List[str] , __A : Optional[int] ): a_ : str = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): a_ : Tuple = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) a_ : List[str] = state_dict.pop(f'backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.weight' ) a_ : str = state_dict.pop(f'backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.bias' ) # next, add query, keys and values (in that order) to the state dict a_ : Optional[Any] = in_proj_weight[:dim, :] a_ : List[Any] = in_proj_bias[: dim] a_ : Optional[Any] = in_proj_weight[ dim : dim * 2, : ] a_ : Union[str, Any] = in_proj_bias[ dim : dim * 2 ] a_ : Optional[int] = in_proj_weight[ -dim :, : ] a_ : int = in_proj_bias[-dim :] # fmt: on def _UpperCAmelCase ( __A : Dict , __A : Dict ): # transformer decoder self-attention layers a_ : Any = config.d_model for i in range(config.decoder_layers ): # read in weights + bias of input projection layer of self-attention a_ : int = state_dict.pop(f'transformer.decoder.layers.{i}.self_attn.in_proj_weight' ) a_ : Any = state_dict.pop(f'transformer.decoder.layers.{i}.self_attn.in_proj_bias' ) # next, add query, keys and values (in that order) to the state dict a_ : Dict = in_proj_weight[:hidden_size, :] a_ : Tuple = in_proj_bias[:hidden_size] a_ : Any = in_proj_weight[ hidden_size : hidden_size * 2, : ] a_ : Tuple = in_proj_bias[hidden_size : hidden_size * 2] a_ : Optional[int] = in_proj_weight[-hidden_size:, :] a_ : int = in_proj_bias[-hidden_size:] def _UpperCAmelCase ( ): a_ : Union[str, Any] = '''http://images.cocodataset.org/val2017/000000039769.jpg''' a_ : List[str] = Image.open(requests.get(__A , stream=__A ).raw ) return im @torch.no_grad() def _UpperCAmelCase ( __A : int , __A : int , __A : Any ): a_ : Union[str, Any] = get_deta_config(__A ) # load original state dict if model_name == "deta-swin-large": a_ : Optional[Any] = hf_hub_download(repo_id='''nielsr/deta-checkpoints''' , filename='''adet_swin_ft.pth''' ) elif model_name == "deta-swin-large-o365": a_ : List[str] = hf_hub_download(repo_id='''jozhang97/deta-swin-l-o365''' , filename='''deta_swin_pt_o365.pth''' ) else: raise ValueError(f'Model name {model_name} not supported' ) a_ : List[Any] = torch.load(__A , map_location='''cpu''' )['''model'''] # original state dict for name, param in state_dict.items(): print(__A , param.shape ) # rename keys a_ : Union[str, Any] = create_rename_keys(__A ) for src, dest in rename_keys: rename_key(__A , __A , __A ) read_in_swin_q_k_v(__A , config.backbone_config ) read_in_decoder_q_k_v(__A , __A ) # fix some prefixes for key in state_dict.copy().keys(): if "transformer.decoder.class_embed" in key or "transformer.decoder.bbox_embed" in key: a_ : Optional[Any] = state_dict.pop(__A ) a_ : int = val if "input_proj" in key: a_ : str = state_dict.pop(__A ) a_ : Optional[Any] = val if "level_embed" in key or "pos_trans" in key or "pix_trans" in key or "enc_output" in key: a_ : List[str] = state_dict.pop(__A ) a_ : List[Any] = val # finally, create HuggingFace model and load state dict a_ : Dict = DetaForObjectDetection(__A ) model.load_state_dict(__A ) model.eval() a_ : int = '''cuda''' if torch.cuda.is_available() else '''cpu''' model.to(__A ) # load image processor a_ : List[Any] = DetaImageProcessor(format='''coco_detection''' ) # verify our conversion on image a_ : Dict = prepare_img() a_ : Optional[int] = processor(images=__A , return_tensors='''pt''' ) a_ : Any = encoding['''pixel_values'''] a_ : int = model(pixel_values.to(__A ) ) # verify logits print('''Logits:''' , outputs.logits[0, :3, :3] ) print('''Boxes:''' , outputs.pred_boxes[0, :3, :3] ) if model_name == "deta-swin-large": a_ : Optional[int] = torch.tensor( [[-7.6308, -2.8485, -5.3737], [-7.2037, -4.5505, -4.8027], [-7.2943, -4.2611, -4.6617]] ) a_ : Tuple = torch.tensor([[0.4987, 0.4969, 0.9999], [0.2549, 0.5498, 0.4805], [0.5498, 0.2757, 0.0569]] ) elif model_name == "deta-swin-large-o365": a_ : Union[str, Any] = torch.tensor( [[-8.0122, -3.5720, -4.9717], [-8.1547, -3.6886, -4.6389], [-7.6610, -3.6194, -5.0134]] ) a_ : Any = torch.tensor([[0.2523, 0.5549, 0.4881], [0.7715, 0.4149, 0.4601], [0.5503, 0.2753, 0.0575]] ) assert torch.allclose(outputs.logits[0, :3, :3] , expected_logits.to(__A ) , atol=1E-4 ) assert torch.allclose(outputs.pred_boxes[0, :3, :3] , expected_boxes.to(__A ) , atol=1E-4 ) print('''Everything ok!''' ) if pytorch_dump_folder_path: # Save model and processor logger.info(f'Saving PyTorch model and processor to {pytorch_dump_folder_path}...' ) Path(__A ).mkdir(exist_ok=__A ) model.save_pretrained(__A ) processor.save_pretrained(__A ) # Push to hub if push_to_hub: print('''Pushing model and processor to hub...''' ) model.push_to_hub(f'jozhang97/{model_name}' ) processor.push_to_hub(f'jozhang97/{model_name}' ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() parser.add_argument( '--model_name', type=str, default='deta-swin-large', choices=['deta-swin-large', 'deta-swin-large-o365'], help='Name of the model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the folder to output PyTorch model.', ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.' ) __lowerCAmelCase = parser.parse_args() convert_deta_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
666
0
'''simple docstring''' import os import tempfile from functools import partial from unittest import TestCase from unittest.mock import patch import datasets import datasets.config from .utils import require_beam class SCREAMING_SNAKE_CASE ( datasets.BeamBasedBuilder ): def SCREAMING_SNAKE_CASE ( self : Dict ) -> int: return datasets.DatasetInfo( features=datasets.Features({'''content''': datasets.Value('''string''' )} ) , supervised_keys=__A , ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : List[str] ) -> str: return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'''examples''': get_test_dummy_examples()} )] def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : List[str] ) -> Dict: import apache_beam as beam return pipeline | "Load Examples" >> beam.Create(__A ) class SCREAMING_SNAKE_CASE ( datasets.BeamBasedBuilder ): def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Optional[int]: return datasets.DatasetInfo( features=datasets.Features({'''a''': datasets.Sequence({'''b''': datasets.Value('''string''' )} )} ) , supervised_keys=__A , ) def SCREAMING_SNAKE_CASE ( self : int , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : str ) -> Tuple: return [ datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'''examples''': get_test_nested_examples()} ) ] def SCREAMING_SNAKE_CASE ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : int ) -> Tuple: import apache_beam as beam return pipeline | "Load Examples" >> beam.Create(__A ) def _UpperCAmelCase ( ): return [(i, {"content": content}) for i, content in enumerate(['''foo''', '''bar''', '''foobar'''] )] def _UpperCAmelCase ( ): return [(i, {"a": {"b": [content]}}) for i, content in enumerate(['''foo''', '''bar''', '''foobar'''] )] class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): @require_beam def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> str: a_ : str = len(get_test_dummy_examples() ) with tempfile.TemporaryDirectory() as tmp_cache_dir: a_ : Optional[Any] = DummyBeamDataset(cache_dir=__A , beam_runner='''DirectRunner''' ) builder.download_and_prepare() self.assertTrue( os.path.exists( os.path.join(__A , builder.name , '''default''' , '''0.0.0''' , f'{builder.name}-train.arrow' ) ) ) self.assertDictEqual(builder.info.features , datasets.Features({'''content''': datasets.Value('''string''' )} ) ) a_ : Union[str, Any] = builder.as_dataset() self.assertEqual(dset['''train'''].num_rows , __A ) self.assertEqual(dset['''train'''].info.splits['''train'''].num_examples , __A ) self.assertDictEqual(dset['''train'''][0] , get_test_dummy_examples()[0][1] ) self.assertDictEqual( dset['''train'''][expected_num_examples - 1] , get_test_dummy_examples()[expected_num_examples - 1][1] ) self.assertTrue( os.path.exists(os.path.join(__A , builder.name , '''default''' , '''0.0.0''' , '''dataset_info.json''' ) ) ) del dset @require_beam def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: import apache_beam as beam a_ : Any = beam.io.parquetio.WriteToParquet a_ : Union[str, Any] = len(get_test_dummy_examples() ) with tempfile.TemporaryDirectory() as tmp_cache_dir: a_ : Optional[int] = DummyBeamDataset(cache_dir=__A , beam_runner='''DirectRunner''' ) with patch('''apache_beam.io.parquetio.WriteToParquet''' ) as write_parquet_mock: a_ : str = partial(__A , num_shards=2 ) builder.download_and_prepare() self.assertTrue( os.path.exists( os.path.join( __A , builder.name , '''default''' , '''0.0.0''' , f'{builder.name}-train-00000-of-00002.arrow' ) ) ) self.assertTrue( os.path.exists( os.path.join( __A , builder.name , '''default''' , '''0.0.0''' , f'{builder.name}-train-00000-of-00002.arrow' ) ) ) self.assertDictEqual(builder.info.features , datasets.Features({'''content''': datasets.Value('''string''' )} ) ) a_ : str = builder.as_dataset() self.assertEqual(dset['''train'''].num_rows , __A ) self.assertEqual(dset['''train'''].info.splits['''train'''].num_examples , __A ) # Order is not preserved when sharding, so we just check that all the elements are there self.assertListEqual(sorted(dset['''train''']['''content'''] ) , sorted(['''foo''', '''bar''', '''foobar'''] ) ) self.assertTrue( os.path.exists(os.path.join(__A , builder.name , '''default''' , '''0.0.0''' , '''dataset_info.json''' ) ) ) del dset @require_beam def SCREAMING_SNAKE_CASE ( self : List[str] ) -> int: with tempfile.TemporaryDirectory() as tmp_cache_dir: a_ : Union[str, Any] = DummyBeamDataset(cache_dir=__A ) self.assertRaises(datasets.builder.MissingBeamOptions , builder.download_and_prepare ) @require_beam def SCREAMING_SNAKE_CASE ( self : List[str] ) -> List[Any]: a_ : Tuple = len(get_test_nested_examples() ) with tempfile.TemporaryDirectory() as tmp_cache_dir: a_ : Union[str, Any] = NestedBeamDataset(cache_dir=__A , beam_runner='''DirectRunner''' ) builder.download_and_prepare() self.assertTrue( os.path.exists( os.path.join(__A , builder.name , '''default''' , '''0.0.0''' , f'{builder.name}-train.arrow' ) ) ) self.assertDictEqual( builder.info.features , datasets.Features({'''a''': datasets.Sequence({'''b''': datasets.Value('''string''' )} )} ) ) a_ : str = builder.as_dataset() self.assertEqual(dset['''train'''].num_rows , __A ) self.assertEqual(dset['''train'''].info.splits['''train'''].num_examples , __A ) self.assertDictEqual(dset['''train'''][0] , get_test_nested_examples()[0][1] ) self.assertDictEqual( dset['''train'''][expected_num_examples - 1] , get_test_nested_examples()[expected_num_examples - 1][1] ) self.assertTrue( os.path.exists(os.path.join(__A , builder.name , '''default''' , '''0.0.0''' , '''dataset_info.json''' ) ) ) del dset
718
'''simple docstring''' import unittest import numpy as np import torch from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = DDIMPipeline snake_case__ = UNCONDITIONAL_IMAGE_GENERATION_PARAMS snake_case__ = PipelineTesterMixin.required_optional_params - { "num_images_per_prompt", "latents", "callback", "callback_steps", } snake_case__ = UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS snake_case__ = False def SCREAMING_SNAKE_CASE ( self : Any ) -> Optional[Any]: torch.manual_seed(0 ) a_ : 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''') , ) a_ : str = DDIMScheduler() a_ : Union[str, Any] = {'''unet''': unet, '''scheduler''': scheduler} return components def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Tuple=0 ) -> str: if str(__SCREAMING_SNAKE_CASE ).startswith('''mps''' ): a_ : Dict = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: a_ : Union[str, Any] = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = { '''batch_size''': 1, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union[str, Any]: a_ : Dict = '''cpu''' a_ : List[Any] = self.get_dummy_components() a_ : List[str] = self.pipeline_class(**__SCREAMING_SNAKE_CASE ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = self.get_dummy_inputs(__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = pipe(**__SCREAMING_SNAKE_CASE ).images a_ : List[str] = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 32, 32, 3) ) a_ : int = np.array( [1.000e00, 5.717e-01, 4.717e-01, 1.000e00, 0.000e00, 1.000e00, 3.000e-04, 0.000e00, 9.000e-04] ) a_ : Union[str, Any] = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(__SCREAMING_SNAKE_CASE , 1e-3 ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> str: super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : str ) -> Optional[int]: super().test_save_load_local(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : Any ) -> List[str]: super().test_save_load_optional_components(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : str ) -> Optional[int]: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : str ) -> Any: a_ : Optional[Any] = '''google/ddpm-cifar10-32''' a_ : Optional[Any] = UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Dict = DDIMScheduler() a_ : List[str] = DDIMPipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) ddim.to(__SCREAMING_SNAKE_CASE ) ddim.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = torch.manual_seed(0 ) a_ : Tuple = ddim(generator=__SCREAMING_SNAKE_CASE , eta=0.0 , output_type='''numpy''' ).images a_ : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) a_ : List[str] = np.array([0.1723, 0.1617, 0.1600, 0.1626, 0.1497, 0.1513, 0.1505, 0.1442, 0.1453] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> str: a_ : int = '''google/ddpm-ema-bedroom-256''' a_ : str = UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Tuple = DDIMScheduler.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = DDIMPipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) ddpm.to(__SCREAMING_SNAKE_CASE ) ddpm.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = torch.manual_seed(0 ) a_ : List[Any] = ddpm(generator=__SCREAMING_SNAKE_CASE , output_type='''numpy''' ).images a_ : Optional[int] = image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) a_ : Optional[Any] = np.array([0.0060, 0.0201, 0.0344, 0.0024, 0.0018, 0.0002, 0.0022, 0.0000, 0.0069] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
666
0
'''simple docstring''' from tempfile import TemporaryDirectory from unittest import TestCase from unittest.mock import MagicMock, patch from transformers import AutoModel, TFAutoModel from transformers.onnx import FeaturesManager from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch @require_torch @require_tf class SCREAMING_SNAKE_CASE ( __snake_case ): def SCREAMING_SNAKE_CASE ( self : int ) -> str: a_ : Union[str, Any] = SMALL_MODEL_IDENTIFIER a_ : List[Any] = """pt""" a_ : Union[str, Any] = """tf""" def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Dict ) -> str: a_ : Tuple = AutoModel.from_pretrained(self.test_model ) model_pt.save_pretrained(_lowercase ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Union[str, Any] ) -> Tuple: a_ : List[str] = TFAutoModel.from_pretrained(self.test_model , from_pt=_lowercase ) model_tf.save_pretrained(_lowercase ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> Tuple: a_ : Any = """mock_framework""" # Framework provided - return whatever the user provides a_ : str = FeaturesManager.determine_framework(self.test_model , _lowercase ) self.assertEqual(_lowercase , _lowercase ) # Local checkpoint and framework provided - return provided framework # PyTorch checkpoint with TemporaryDirectory() as local_pt_ckpt: self._setup_pt_ckpt(_lowercase ) a_ : int = FeaturesManager.determine_framework(_lowercase , _lowercase ) self.assertEqual(_lowercase , _lowercase ) # TensorFlow checkpoint with TemporaryDirectory() as local_tf_ckpt: self._setup_tf_ckpt(_lowercase ) a_ : List[str] = FeaturesManager.determine_framework(_lowercase , _lowercase ) self.assertEqual(_lowercase , _lowercase ) def SCREAMING_SNAKE_CASE ( self : Any ) -> Optional[int]: with TemporaryDirectory() as local_pt_ckpt: self._setup_pt_ckpt(_lowercase ) a_ : List[Any] = FeaturesManager.determine_framework(_lowercase ) self.assertEqual(_lowercase , self.framework_pt ) # TensorFlow checkpoint with TemporaryDirectory() as local_tf_ckpt: self._setup_tf_ckpt(_lowercase ) a_ : str = FeaturesManager.determine_framework(_lowercase ) self.assertEqual(_lowercase , self.framework_tf ) # Invalid local checkpoint with TemporaryDirectory() as local_invalid_ckpt: with self.assertRaises(_lowercase ): a_ : str = FeaturesManager.determine_framework(_lowercase ) def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> str: a_ : List[str] = MagicMock(return_value=_lowercase ) with patch('''transformers.onnx.features.is_tf_available''' , _lowercase ): a_ : Dict = FeaturesManager.determine_framework(self.test_model ) self.assertEqual(_lowercase , self.framework_pt ) # PyTorch not in environment -> use TensorFlow a_ : Tuple = MagicMock(return_value=_lowercase ) with patch('''transformers.onnx.features.is_torch_available''' , _lowercase ): a_ : Union[str, Any] = FeaturesManager.determine_framework(self.test_model ) self.assertEqual(_lowercase , self.framework_tf ) # Both in environment -> use PyTorch a_ : List[Any] = MagicMock(return_value=_lowercase ) a_ : List[Any] = MagicMock(return_value=_lowercase ) with patch('''transformers.onnx.features.is_tf_available''' , _lowercase ), patch( '''transformers.onnx.features.is_torch_available''' , _lowercase ): a_ : int = FeaturesManager.determine_framework(self.test_model ) self.assertEqual(_lowercase , self.framework_pt ) # Both not in environment -> raise error a_ : int = MagicMock(return_value=_lowercase ) a_ : List[str] = MagicMock(return_value=_lowercase ) with patch('''transformers.onnx.features.is_tf_available''' , _lowercase ), patch( '''transformers.onnx.features.is_torch_available''' , _lowercase ): with self.assertRaises(_lowercase ): a_ : Optional[Any] = FeaturesManager.determine_framework(self.test_model )
719
'''simple docstring''' from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import pyarrow as pa if TYPE_CHECKING: from .features import FeatureType @dataclass class SCREAMING_SNAKE_CASE : snake_case__ = 42 snake_case__ = None # Automatically constructed snake_case__ = "dict" snake_case__ = None snake_case__ = field(default="Translation" , init=SCREAMING_SNAKE_CASE_ , repr=SCREAMING_SNAKE_CASE_ ) def __call__( self : Dict ) -> Tuple: return pa.struct({lang: pa.string() for lang in sorted(self.languages )} ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Value return {k: Value('''string''' ) for k in sorted(self.languages )} @dataclass class SCREAMING_SNAKE_CASE : snake_case__ = None snake_case__ = None snake_case__ = None # Automatically constructed snake_case__ = "dict" snake_case__ = None snake_case__ = field(default="TranslationVariableLanguages" , init=SCREAMING_SNAKE_CASE_ , repr=SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : List[str] = sorted(set(self.languages ) ) if self.languages else None a_ : Optional[Any] = len(self.languages ) if self.languages else None def __call__( self : Any ) -> Optional[Any]: return pa.struct({'''language''': pa.list_(pa.string() ), '''translation''': pa.list_(pa.string() )} ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Optional[Any]: a_ : str = set(self.languages ) if self.languages and set(__SCREAMING_SNAKE_CASE ) - lang_set: raise ValueError( f'Some languages in example ({", ".join(sorted(set(__SCREAMING_SNAKE_CASE ) - lang_set ) )}) are not in valid set ({", ".join(__SCREAMING_SNAKE_CASE )}).' ) # Convert dictionary into tuples, splitting out cases where there are # multiple translations for a single language. a_ : int = [] for lang, text in translation_dict.items(): if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): translation_tuples.append((lang, text) ) else: translation_tuples.extend([(lang, el) for el in text] ) # Ensure translations are in ascending order by language code. a_ , a_ : List[Any] = zip(*sorted(__SCREAMING_SNAKE_CASE ) ) return {"language": languages, "translation": translations} def SCREAMING_SNAKE_CASE ( self : Any ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Sequence, Value return { "language": Sequence(Value('''string''' ) ), "translation": Sequence(Value('''string''' ) ), }
666
0
'''simple docstring''' from ..utils import DummyObject, requires_backends class SCREAMING_SNAKE_CASE ( metaclass=SCREAMING_SNAKE_CASE_ ): snake_case__ = ['''torch''', '''transformers''', '''onnx'''] def __init__( self : Any , *__SCREAMING_SNAKE_CASE : Optional[int] , **__SCREAMING_SNAKE_CASE : str ) -> Optional[Any]: requires_backends(self , ['''torch''', '''transformers''', '''onnx'''] ) @classmethod def SCREAMING_SNAKE_CASE ( cls : Union[str, Any] , *__SCREAMING_SNAKE_CASE : Tuple , **__SCREAMING_SNAKE_CASE : Dict ) -> Tuple: requires_backends(cls , ['''torch''', '''transformers''', '''onnx'''] ) @classmethod def SCREAMING_SNAKE_CASE ( cls : str , *__SCREAMING_SNAKE_CASE : int , **__SCREAMING_SNAKE_CASE : Tuple ) -> List[Any]: requires_backends(cls , ['''torch''', '''transformers''', '''onnx'''] ) class SCREAMING_SNAKE_CASE ( metaclass=SCREAMING_SNAKE_CASE_ ): snake_case__ = ['''torch''', '''transformers''', '''onnx'''] def __init__( self : Union[str, Any] , *__SCREAMING_SNAKE_CASE : Tuple , **__SCREAMING_SNAKE_CASE : Union[str, Any] ) -> Union[str, Any]: requires_backends(self , ['''torch''', '''transformers''', '''onnx'''] ) @classmethod def SCREAMING_SNAKE_CASE ( cls : Tuple , *__SCREAMING_SNAKE_CASE : Union[str, Any] , **__SCREAMING_SNAKE_CASE : Optional[Any] ) -> Optional[Any]: requires_backends(cls , ['''torch''', '''transformers''', '''onnx'''] ) @classmethod def SCREAMING_SNAKE_CASE ( cls : Optional[Any] , *__SCREAMING_SNAKE_CASE : Union[str, Any] , **__SCREAMING_SNAKE_CASE : Tuple ) -> Dict: requires_backends(cls , ['''torch''', '''transformers''', '''onnx'''] ) class SCREAMING_SNAKE_CASE ( metaclass=SCREAMING_SNAKE_CASE_ ): snake_case__ = ['''torch''', '''transformers''', '''onnx'''] def __init__( self : Tuple , *__SCREAMING_SNAKE_CASE : Dict , **__SCREAMING_SNAKE_CASE : Any ) -> int: requires_backends(self , ['''torch''', '''transformers''', '''onnx'''] ) @classmethod def SCREAMING_SNAKE_CASE ( cls : Dict , *__SCREAMING_SNAKE_CASE : int , **__SCREAMING_SNAKE_CASE : Union[str, Any] ) -> Optional[Any]: requires_backends(cls , ['''torch''', '''transformers''', '''onnx'''] ) @classmethod def SCREAMING_SNAKE_CASE ( cls : List[Any] , *__SCREAMING_SNAKE_CASE : Optional[int] , **__SCREAMING_SNAKE_CASE : Optional[Any] ) -> Tuple: requires_backends(cls , ['''torch''', '''transformers''', '''onnx'''] ) class SCREAMING_SNAKE_CASE ( metaclass=SCREAMING_SNAKE_CASE_ ): snake_case__ = ['''torch''', '''transformers''', '''onnx'''] def __init__( self : Optional[Any] , *__SCREAMING_SNAKE_CASE : Optional[int] , **__SCREAMING_SNAKE_CASE : int ) -> List[str]: requires_backends(self , ['''torch''', '''transformers''', '''onnx'''] ) @classmethod def SCREAMING_SNAKE_CASE ( cls : str , *__SCREAMING_SNAKE_CASE : Tuple , **__SCREAMING_SNAKE_CASE : Optional[Any] ) -> List[str]: requires_backends(cls , ['''torch''', '''transformers''', '''onnx'''] ) @classmethod def SCREAMING_SNAKE_CASE ( cls : Dict , *__SCREAMING_SNAKE_CASE : str , **__SCREAMING_SNAKE_CASE : Union[str, Any] ) -> int: requires_backends(cls , ['''torch''', '''transformers''', '''onnx'''] ) class SCREAMING_SNAKE_CASE ( metaclass=SCREAMING_SNAKE_CASE_ ): snake_case__ = ['''torch''', '''transformers''', '''onnx'''] def __init__( self : List[str] , *__SCREAMING_SNAKE_CASE : str , **__SCREAMING_SNAKE_CASE : str ) -> Optional[int]: requires_backends(self , ['''torch''', '''transformers''', '''onnx'''] ) @classmethod def SCREAMING_SNAKE_CASE ( cls : Tuple , *__SCREAMING_SNAKE_CASE : Optional[int] , **__SCREAMING_SNAKE_CASE : Any ) -> Optional[Any]: requires_backends(cls , ['''torch''', '''transformers''', '''onnx'''] ) @classmethod def SCREAMING_SNAKE_CASE ( cls : List[Any] , *__SCREAMING_SNAKE_CASE : Optional[Any] , **__SCREAMING_SNAKE_CASE : int ) -> str: requires_backends(cls , ['''torch''', '''transformers''', '''onnx'''] ) class SCREAMING_SNAKE_CASE ( metaclass=SCREAMING_SNAKE_CASE_ ): snake_case__ = ['''torch''', '''transformers''', '''onnx'''] def __init__( self : str , *__SCREAMING_SNAKE_CASE : str , **__SCREAMING_SNAKE_CASE : Union[str, Any] ) -> Any: requires_backends(self , ['''torch''', '''transformers''', '''onnx'''] ) @classmethod def SCREAMING_SNAKE_CASE ( cls : Optional[Any] , *__SCREAMING_SNAKE_CASE : Any , **__SCREAMING_SNAKE_CASE : str ) -> Any: requires_backends(cls , ['''torch''', '''transformers''', '''onnx'''] ) @classmethod def SCREAMING_SNAKE_CASE ( cls : Dict , *__SCREAMING_SNAKE_CASE : str , **__SCREAMING_SNAKE_CASE : str ) -> Tuple: requires_backends(cls , ['''torch''', '''transformers''', '''onnx'''] )
720
'''simple docstring''' import json import os import pickle import shutil import tempfile from unittest import TestCase from unittest.mock import patch import numpy as np from datasets import Dataset from transformers import is_faiss_available from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bart.tokenization_bart import BartTokenizer from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.dpr.tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.retrieval_rag import CustomHFIndex, RagRetriever from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_sentencepiece, require_tokenizers, require_torch if is_faiss_available(): import faiss @require_faiss class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): def SCREAMING_SNAKE_CASE ( self : Dict ) -> Dict: a_ : Union[str, Any] = tempfile.mkdtemp() a_ : Union[str, Any] = 8 # DPR tok a_ : Dict = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] a_ : str = os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = os.path.join(__SCREAMING_SNAKE_CASE , DPR_VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) # BART tok a_ : Union[str, Any] = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] a_ : int = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) a_ : int = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] a_ : Optional[int] = {'''unk_token''': '''<unk>'''} a_ : List[str] = os.path.join(self.tmpdirname , '''bart_tokenizer''' ) os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) a_ : Tuple = os.path.join(__SCREAMING_SNAKE_CASE , BART_VOCAB_FILES_NAMES['''vocab_file'''] ) a_ : int = os.path.join(__SCREAMING_SNAKE_CASE , BART_VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> DPRQuestionEncoderTokenizer: return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : str ) -> DPRContextEncoderTokenizer: return DPRContextEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> BartTokenizer: return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''bart_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Union[str, Any]: shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> Union[str, Any]: a_ : str = Dataset.from_dict( { '''id''': ['''0''', '''1'''], '''text''': ['''foo''', '''bar'''], '''title''': ['''Foo''', '''Bar'''], '''embeddings''': [np.ones(self.retrieval_vector_size ), 2 * np.ones(self.retrieval_vector_size )], } ) dataset.add_faiss_index('''embeddings''' , string_factory='''Flat''' , metric_type=faiss.METRIC_INNER_PRODUCT ) return dataset def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Tuple: a_ : List[str] = self.get_dummy_dataset() a_ : Tuple = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , ) with patch('''transformers.models.rag.retrieval_rag.load_dataset''' ) as mock_load_dataset: a_ : Tuple = dataset a_ : Any = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) return retriever def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : bool ) -> Dict: a_ : Dict = self.get_dummy_dataset() a_ : Dict = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''custom''' , ) if from_disk: a_ : Optional[int] = os.path.join(self.tmpdirname , '''dataset''' ) a_ : str = os.path.join(self.tmpdirname , '''index.faiss''' ) dataset.get_index('''embeddings''' ).save(os.path.join(self.tmpdirname , '''index.faiss''' ) ) dataset.drop_index('''embeddings''' ) dataset.save_to_disk(os.path.join(self.tmpdirname , '''dataset''' ) ) del dataset a_ : int = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) else: a_ : Optional[Any] = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , index=CustomHFIndex(config.retrieval_vector_size , __SCREAMING_SNAKE_CASE ) , ) return retriever def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Optional[int]: a_ : str = Dataset.from_dict( { '''id''': ['''0''', '''1'''], '''text''': ['''foo''', '''bar'''], '''title''': ['''Foo''', '''Bar'''], '''embeddings''': [np.ones(self.retrieval_vector_size + 1 ), 2 * np.ones(self.retrieval_vector_size + 1 )], } ) dataset.add_faiss_index('''embeddings''' , string_factory='''Flat''' , metric_type=faiss.METRIC_INNER_PRODUCT ) a_ : Optional[int] = os.path.join(self.tmpdirname , '''hf_bert_base.hnswSQ8_correct_phi_128.c_index''' ) dataset.save_faiss_index('''embeddings''' , index_file_name + '''.index.dpr''' ) pickle.dump(dataset['''id'''] , open(index_file_name + '''.index_meta.dpr''' , '''wb''' ) ) a_ : Union[str, Any] = os.path.join(self.tmpdirname , '''psgs_w100.tsv.pkl''' ) a_ : Dict = {sample['''id''']: [sample['''text'''], sample['''title''']] for sample in dataset} pickle.dump(__SCREAMING_SNAKE_CASE , open(__SCREAMING_SNAKE_CASE , '''wb''' ) ) a_ : Optional[Any] = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''legacy''' , index_path=self.tmpdirname , ) a_ : int = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() ) return retriever def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : Optional[Any] = 1 a_ : Dict = self.get_dummy_canonical_hf_index_retriever() a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : str = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : str = self.get_dummy_canonical_hf_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: with patch('''transformers.models.rag.retrieval_rag.load_dataset''' ) as mock_load_dataset: a_ : List[str] = self.get_dummy_dataset() retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : List[str] = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> int: a_ : Union[str, Any] = 1 a_ : Optional[Any] = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) a_ : List[Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : Any = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : str ) -> List[str]: a_ : Dict = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : List[str] = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Dict = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Optional[int]: a_ : Union[str, Any] = 1 a_ : str = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : Tuple = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : int ) -> Dict: a_ : List[str] = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Dict = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Any: a_ : str = 1 a_ : Tuple = self.get_dummy_legacy_index_retriever() a_ : Union[str, Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : Any = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''text'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''text'''][0] , '''bar''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''text'''][0] , '''foo''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[int]: a_ : List[str] = self.get_dummy_legacy_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : List[str] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Optional[Any] = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) @require_torch @require_tokenizers @require_sentencepiece def SCREAMING_SNAKE_CASE ( self : int ) -> List[str]: import torch a_ : Any = 1 a_ : List[Any] = self.get_dummy_canonical_hf_index_retriever() a_ : Union[str, Any] = [[5, 7], [10, 11]] a_ : Optional[int] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : str = retriever(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE ) a_ , a_ , a_ : List[str] = ( out['''context_input_ids'''], out['''context_attention_mask'''], out['''retrieved_doc_embeds'''], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , np.ndarray ) a_ : Any = retriever( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE , return_tensors='''pt''' , ) a_ , a_ , a_ , a_ : str = ( # noqa: F841 out['''context_input_ids'''], out['''context_attention_mask'''], out['''retrieved_doc_embeds'''], out['''doc_ids'''], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) @require_torch @require_tokenizers @require_sentencepiece def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Any: a_ : str = self.get_dpr_ctx_encoder_tokenizer() a_ : Tuple = 1 a_ : Any = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) retriever.set_ctx_encoder_tokenizer(__SCREAMING_SNAKE_CASE ) a_ : Dict = [[5, 7], [10, 11]] a_ : List[str] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : List[Any] = retriever(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual( len(__SCREAMING_SNAKE_CASE ) , 6 ) # check whether the retriever output consist of 6 attributes including tokenized docs self.assertEqual( all(k in out for k in ('''tokenized_doc_ids''', '''tokenized_doc_attention_mask''') ) , __SCREAMING_SNAKE_CASE ) # check for doc token related keys in dictionary.
666
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_torch_available, ) __lowerCAmelCase = { 'configuration_trocr': ['TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP', 'TrOCRConfig'], 'processing_trocr': ['TrOCRProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ 'TROCR_PRETRAINED_MODEL_ARCHIVE_LIST', 'TrOCRForCausalLM', 'TrOCRPreTrainedModel', ] if TYPE_CHECKING: from .configuration_trocr import TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP, TrOCRConfig from .processing_trocr import TrOCRProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_trocr import TROCR_PRETRAINED_MODEL_ARCHIVE_LIST, TrOCRForCausalLM, TrOCRPreTrainedModel else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
721
'''simple docstring''' from math import pi, sqrt, tan def _UpperCAmelCase ( __A : float ): if side_length < 0: raise ValueError('''surface_area_cube() only accepts non-negative values''' ) return 6 * side_length**2 def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if length < 0 or breadth < 0 or height < 0: raise ValueError('''surface_area_cuboid() only accepts non-negative values''' ) return 2 * ((length * breadth) + (breadth * height) + (length * height)) def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''surface_area_sphere() only accepts non-negative values''' ) return 4 * pi * radius**2 def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''surface_area_hemisphere() only accepts non-negative values''' ) return 3 * pi * radius**2 def _UpperCAmelCase ( __A : float , __A : float ): if radius < 0 or height < 0: raise ValueError('''surface_area_cone() only accepts non-negative values''' ) return pi * radius * (radius + (height**2 + radius**2) ** 0.5) def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if radius_a < 0 or radius_a < 0 or height < 0: raise ValueError( '''surface_area_conical_frustum() only accepts non-negative values''' ) a_ : Any = (height**2 + (radius_a - radius_a) ** 2) ** 0.5 return pi * ((slant_height * (radius_a + radius_a)) + radius_a**2 + radius_a**2) def _UpperCAmelCase ( __A : float , __A : float ): if radius < 0 or height < 0: raise ValueError('''surface_area_cylinder() only accepts non-negative values''' ) return 2 * pi * radius * (height + radius) def _UpperCAmelCase ( __A : float , __A : float ): if torus_radius < 0 or tube_radius < 0: raise ValueError('''surface_area_torus() only accepts non-negative values''' ) if torus_radius < tube_radius: raise ValueError( '''surface_area_torus() does not support spindle or self intersecting tori''' ) return 4 * pow(__A , 2 ) * torus_radius * tube_radius def _UpperCAmelCase ( __A : float , __A : float ): if length < 0 or width < 0: raise ValueError('''area_rectangle() only accepts non-negative values''' ) return length * width def _UpperCAmelCase ( __A : float ): if side_length < 0: raise ValueError('''area_square() only accepts non-negative values''' ) return side_length**2 def _UpperCAmelCase ( __A : float , __A : float ): if base < 0 or height < 0: raise ValueError('''area_triangle() only accepts non-negative values''' ) return (base * height) / 2 def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if sidea < 0 or sidea < 0 or sidea < 0: raise ValueError('''area_triangle_three_sides() only accepts non-negative values''' ) elif sidea + sidea < sidea or sidea + sidea < sidea or sidea + sidea < sidea: raise ValueError('''Given three sides do not form a triangle''' ) a_ : int = (sidea + sidea + sidea) / 2 a_ : Optional[Any] = sqrt( semi_perimeter * (semi_perimeter - sidea) * (semi_perimeter - sidea) * (semi_perimeter - sidea) ) return area def _UpperCAmelCase ( __A : float , __A : float ): if base < 0 or height < 0: raise ValueError('''area_parallelogram() only accepts non-negative values''' ) return base * height def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if basea < 0 or basea < 0 or height < 0: raise ValueError('''area_trapezium() only accepts non-negative values''' ) return 1 / 2 * (basea + basea) * height def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''area_circle() only accepts non-negative values''' ) return pi * radius**2 def _UpperCAmelCase ( __A : float , __A : float ): if radius_x < 0 or radius_y < 0: raise ValueError('''area_ellipse() only accepts non-negative values''' ) return pi * radius_x * radius_y def _UpperCAmelCase ( __A : float , __A : float ): if diagonal_a < 0 or diagonal_a < 0: raise ValueError('''area_rhombus() only accepts non-negative values''' ) return 1 / 2 * diagonal_a * diagonal_a def _UpperCAmelCase ( __A : int , __A : float ): if not isinstance(__A , __A ) or sides < 3: raise ValueError( '''area_reg_polygon() only accepts integers greater than or \ equal to three as number of sides''' ) elif length < 0: raise ValueError( '''area_reg_polygon() only accepts non-negative values as \ length of a side''' ) return (sides * length**2) / (4 * tan(pi / sides )) return (sides * length**2) / (4 * tan(pi / sides )) if __name__ == "__main__": import doctest doctest.testmod(verbose=True) # verbose so we can see methods missing tests print('[DEMO] Areas of various geometric shapes: \n') print(F"""Rectangle: {area_rectangle(10, 20) = }""") print(F"""Square: {area_square(10) = }""") print(F"""Triangle: {area_triangle(10, 10) = }""") print(F"""Triangle: {area_triangle_three_sides(5, 12, 13) = }""") print(F"""Parallelogram: {area_parallelogram(10, 20) = }""") print(F"""Rhombus: {area_rhombus(10, 20) = }""") print(F"""Trapezium: {area_trapezium(10, 20, 30) = }""") print(F"""Circle: {area_circle(20) = }""") print(F"""Ellipse: {area_ellipse(10, 20) = }""") print('\nSurface Areas of various geometric shapes: \n') print(F"""Cube: {surface_area_cube(20) = }""") print(F"""Cuboid: {surface_area_cuboid(10, 20, 30) = }""") print(F"""Sphere: {surface_area_sphere(20) = }""") print(F"""Hemisphere: {surface_area_hemisphere(20) = }""") print(F"""Cone: {surface_area_cone(10, 20) = }""") print(F"""Conical Frustum: {surface_area_conical_frustum(10, 20, 30) = }""") print(F"""Cylinder: {surface_area_cylinder(10, 20) = }""") print(F"""Torus: {surface_area_torus(20, 10) = }""") print(F"""Equilateral Triangle: {area_reg_polygon(3, 10) = }""") print(F"""Square: {area_reg_polygon(4, 10) = }""") print(F"""Reqular Pentagon: {area_reg_polygon(5, 10) = }""")
666
0
'''simple docstring''' import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto.configuration_auto import CONFIG_MAPPING __lowerCAmelCase = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "upernet" def __init__( self : Dict , __SCREAMING_SNAKE_CASE : Dict=None , __SCREAMING_SNAKE_CASE : int=512 , __SCREAMING_SNAKE_CASE : List[Any]=0.02 , __SCREAMING_SNAKE_CASE : Dict=[1, 2, 3, 6] , __SCREAMING_SNAKE_CASE : List[str]=True , __SCREAMING_SNAKE_CASE : Tuple=0.4 , __SCREAMING_SNAKE_CASE : Optional[int]=384 , __SCREAMING_SNAKE_CASE : str=256 , __SCREAMING_SNAKE_CASE : Dict=1 , __SCREAMING_SNAKE_CASE : Tuple=False , __SCREAMING_SNAKE_CASE : List[Any]=255 , **__SCREAMING_SNAKE_CASE : Optional[int] , ) -> str: super().__init__(**UpperCamelCase__ ) if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) a_ : Dict = CONFIG_MAPPING['''resnet'''](out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4'''] ) elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): a_ : Any = backbone_config.get('''model_type''' ) a_ : Union[str, Any] = CONFIG_MAPPING[backbone_model_type] a_ : Optional[Any] = config_class.from_dict(UpperCamelCase__ ) a_ : int = backbone_config a_ : Optional[int] = hidden_size a_ : str = initializer_range a_ : Dict = pool_scales a_ : Optional[int] = use_auxiliary_head a_ : Any = auxiliary_loss_weight a_ : Tuple = auxiliary_in_channels a_ : int = auxiliary_channels a_ : List[str] = auxiliary_num_convs a_ : Optional[Any] = auxiliary_concat_input a_ : List[Any] = loss_ignore_index def SCREAMING_SNAKE_CASE ( self : List[str] ) -> List[Any]: a_ : int = copy.deepcopy(self.__dict__ ) a_ : Dict = self.backbone_config.to_dict() a_ : Tuple = self.__class__.model_type return output
700
'''simple docstring''' import random import unittest import torch from diffusers import IFInpaintingSuperResolutionPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = IFInpaintingSuperResolutionPipeline snake_case__ = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"width", "height"} snake_case__ = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({"original_image"} ) snake_case__ = PipelineTesterMixin.required_optional_params - {"latents"} def SCREAMING_SNAKE_CASE ( self : str ) -> List[str]: return self._get_superresolution_dummy_components() def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Dict=0 ) -> List[Any]: if str(__SCREAMING_SNAKE_CASE ).startswith('''mps''' ): a_ : Optional[int] = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: a_ : str = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) a_ : Dict = floats_tensor((1, 3, 16, 16) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = floats_tensor((1, 3, 32, 32) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) a_ : Tuple = floats_tensor((1, 3, 32, 32) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': image, '''original_image''': original_image, '''mask_image''': mask_image, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) def SCREAMING_SNAKE_CASE ( self : int ) -> int: self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def SCREAMING_SNAKE_CASE ( self : str ) -> Tuple: # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder super().test_save_load_floataa(expected_max_diff=1e-1 ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Optional[int]: self._test_attention_slicing_forward_pass(expected_max_diff=1e-2 ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Optional[Any]: self._test_save_load_local() def SCREAMING_SNAKE_CASE ( self : Dict ) -> Optional[Any]: self._test_inference_batch_single_identical( expected_max_diff=1e-2 , )
666
0
import os def _UpperCAmelCase ( ): with open(os.path.dirname(__A ) + '''/grid.txt''' ) as f: a_ : Any = [] # noqa: E741 for _ in range(20 ): l.append([int(__A ) for x in f.readline().split()] ) a_ : str = 0 # right for i in range(20 ): for j in range(17 ): a_ : Optional[Any] = l[i][j] * l[i][j + 1] * l[i][j + 2] * l[i][j + 3] if temp > maximum: a_ : int = temp # down for i in range(17 ): for j in range(20 ): a_ : Optional[Any] = l[i][j] * l[i + 1][j] * l[i + 2][j] * l[i + 3][j] if temp > maximum: a_ : int = temp # diagonal 1 for i in range(17 ): for j in range(17 ): a_ : Any = l[i][j] * l[i + 1][j + 1] * l[i + 2][j + 2] * l[i + 3][j + 3] if temp > maximum: a_ : str = temp # diagonal 2 for i in range(17 ): for j in range(3 , 20 ): a_ : int = l[i][j] * l[i + 1][j - 1] * l[i + 2][j - 2] * l[i + 3][j - 3] if temp > maximum: a_ : List[Any] = temp return maximum if __name__ == "__main__": print(solution())
701
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCAmelCase = { 'configuration_git': ['GIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GitConfig', 'GitVisionConfig'], 'processing_git': ['GitProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ 'GIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'GitForCausalLM', 'GitModel', 'GitPreTrainedModel', 'GitVisionModel', ] if TYPE_CHECKING: from .configuration_git import GIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GitConfig, GitVisionConfig from .processing_git import GitProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_git import ( GIT_PRETRAINED_MODEL_ARCHIVE_LIST, GitForCausalLM, GitModel, GitPreTrainedModel, GitVisionModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
666
0
'''simple docstring''' import random import unittest import torch from diffusers import IFImgaImgSuperResolutionPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class SCREAMING_SNAKE_CASE ( _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): snake_case__ = IFImgaImgSuperResolutionPipeline snake_case__ = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"width", "height"} snake_case__ = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"original_image"} ) snake_case__ = PipelineTesterMixin.required_optional_params - {"latents"} def SCREAMING_SNAKE_CASE ( self : List[str] ) -> str: return self._get_superresolution_dummy_components() def SCREAMING_SNAKE_CASE ( self : List[Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Tuple=0 ) -> Union[str, Any]: if str(lowercase__ ).startswith('''mps''' ): a_ : Union[str, Any] = torch.manual_seed(lowercase__ ) else: a_ : Any = torch.Generator(device=lowercase__ ).manual_seed(lowercase__ ) a_ : List[Any] = floats_tensor((1, 3, 32, 32) , rng=random.Random(lowercase__ ) ).to(lowercase__ ) a_ : Optional[int] = floats_tensor((1, 3, 16, 16) , rng=random.Random(lowercase__ ) ).to(lowercase__ ) a_ : Optional[Any] = { "prompt": "A painting of a squirrel eating a burger", "image": image, "original_image": original_image, "generator": generator, "num_inference_steps": 2, "output_type": "numpy", } return inputs @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> str: self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Union[str, Any]: self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Dict: super().test_save_load_floataa(expected_max_diff=1e-1 ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Dict: self._test_attention_slicing_forward_pass(expected_max_diff=1e-2 ) def SCREAMING_SNAKE_CASE ( self : int ) -> Union[str, Any]: self._test_save_load_local() def SCREAMING_SNAKE_CASE ( self : Dict ) -> Any: self._test_inference_batch_single_identical( expected_max_diff=1e-2 , )
702
'''simple docstring''' from unittest.mock import patch import pyspark from datasets.packaged_modules.spark.spark import ( Spark, SparkExamplesIterable, _generate_iterable_examples, ) from ..utils import ( require_dill_gt_0_3_2, require_not_windows, ) def _UpperCAmelCase ( __A : List[str] , __A : List[Any] ): a_ : Any = [] for part_id in partition_order: a_ : str = df.where(f'SPARK_PARTITION_ID() = {part_id}' ).collect() for row_idx, row in enumerate(__A ): expected_row_ids_and_row_dicts.append((f'{part_id}_{row_idx}', row.asDict()) ) return expected_row_ids_and_row_dicts @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : List[str] = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : Union[str, Any] = spark.range(1_00 ).repartition(1 ) a_ : Any = Spark(__A ) # The id ints will be converted to Pyarrow int64s, so each row will be 8 bytes. Setting a max_shard_size of 16 means # that each partition can hold 2 rows. spark_builder._repartition_df_if_needed(max_shard_size=16 ) # Given that the dataframe has 100 rows and each partition has 2 rows, we expect 50 partitions. assert spark_builder.df.rdd.getNumPartitions() == 50 @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : List[Any] = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : int = spark.range(10 ).repartition(2 ) a_ : Tuple = [1, 0] a_ : List[str] = _generate_iterable_examples(__A , __A ) # Reverse the partitions. a_ : int = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , __A ) for i, (row_id, row_dict) in enumerate(generate_fn() ): a_ , a_ : List[Any] = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : int = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : str = spark.range(10 ).repartition(1 ) a_ : Tuple = SparkExamplesIterable(__A ) assert it.n_shards == 1 for i, (row_id, row_dict) in enumerate(__A ): assert row_id == f'0_{i}' assert row_dict == {"id": i} @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : Tuple = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : str = spark.range(30 ).repartition(3 ) # Mock the generator so that shuffle reverses the partition indices. with patch('''numpy.random.Generator''' ) as generator_mock: a_ : Union[str, Any] = lambda __A : x.reverse() a_ : Any = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [2, 1, 0] ) a_ : str = SparkExamplesIterable(__A ).shuffle_data_sources(__A ) assert shuffled_it.n_shards == 3 for i, (row_id, row_dict) in enumerate(__A ): a_ , a_ : Optional[Any] = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : int = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : List[str] = spark.range(20 ).repartition(4 ) # Partitions 0 and 2 a_ : Dict = SparkExamplesIterable(__A ).shard_data_sources(worker_id=0 , num_workers=2 ) assert shard_it_a.n_shards == 2 a_ : Optional[Any] = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [0, 2] ) for i, (row_id, row_dict) in enumerate(__A ): a_ , a_ : Tuple = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict # Partitions 1 and 3 a_ : List[Any] = SparkExamplesIterable(__A ).shard_data_sources(worker_id=1 , num_workers=2 ) assert shard_it_a.n_shards == 2 a_ : Optional[int] = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [1, 3] ) for i, (row_id, row_dict) in enumerate(__A ): a_ , a_ : Any = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : Any = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : List[Any] = spark.range(1_00 ).repartition(1 ) a_ : Optional[Any] = Spark(__A ) # Choose a small max_shard_size for maximum partitioning. spark_builder._repartition_df_if_needed(max_shard_size=1 ) # The new number of partitions should not be greater than the number of rows. assert spark_builder.df.rdd.getNumPartitions() == 1_00
666
0
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( __A : Tuple ): if len(SCREAMING_SNAKE_CASE_ ) == 0: return [] a_ : List[str] = min(SCREAMING_SNAKE_CASE_ ), max(SCREAMING_SNAKE_CASE_ ) a_ : Optional[int] = int(max_value - min_value ) + 1 a_ : list[list] = [[] for _ in range(SCREAMING_SNAKE_CASE_ )] for i in my_list: buckets[int(i - min_value )].append(SCREAMING_SNAKE_CASE_ ) return [v for bucket in buckets for v in sorted(SCREAMING_SNAKE_CASE_ )] if __name__ == "__main__": from doctest import testmod testmod() assert bucket_sort([4, 5, 3, 2, 1]) == [1, 2, 3, 4, 5] assert bucket_sort([0, 1, -10, 15, 2, -2]) == [-10, -2, 0, 1, 2, 15]
703
'''simple docstring''' from collections import OrderedDict from typing import TYPE_CHECKING, Any, List, Mapping, Optional from packaging import version if TYPE_CHECKING: from ... import PreTrainedTokenizer, TensorType from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import is_torch_available, logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = { 'bigscience/bloom': 'https://huggingface.co/bigscience/bloom/resolve/main/config.json', 'bigscience/bloom-560m': 'https://huggingface.co/bigscience/bloom-560m/blob/main/config.json', 'bigscience/bloom-1b1': 'https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json', 'bigscience/bloom-1b7': 'https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json', 'bigscience/bloom-3b': 'https://huggingface.co/bigscience/bloom-3b/blob/main/config.json', 'bigscience/bloom-7b1': 'https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json', } class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "bloom" snake_case__ = ["past_key_values"] snake_case__ = { "num_hidden_layers": "n_layer", "num_attention_heads": "n_head", } def __init__( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : int=25_0880 , __SCREAMING_SNAKE_CASE : Dict=64 , __SCREAMING_SNAKE_CASE : Tuple=2 , __SCREAMING_SNAKE_CASE : int=8 , __SCREAMING_SNAKE_CASE : Any=1e-5 , __SCREAMING_SNAKE_CASE : Optional[Any]=0.02 , __SCREAMING_SNAKE_CASE : Union[str, Any]=True , __SCREAMING_SNAKE_CASE : int=1 , __SCREAMING_SNAKE_CASE : Any=2 , __SCREAMING_SNAKE_CASE : Optional[Any]=False , __SCREAMING_SNAKE_CASE : Optional[Any]=0.0 , __SCREAMING_SNAKE_CASE : str=0.0 , __SCREAMING_SNAKE_CASE : List[Any]=1 , __SCREAMING_SNAKE_CASE : List[str]=False , **__SCREAMING_SNAKE_CASE : str , ) -> Any: a_ : Optional[int] = vocab_size # Backward compatibility with n_embed kwarg a_ : Any = kwargs.pop('''n_embed''' , __SCREAMING_SNAKE_CASE ) a_ : Optional[int] = hidden_size if n_embed is None else n_embed a_ : int = n_layer a_ : str = n_head a_ : Optional[int] = layer_norm_epsilon a_ : Dict = initializer_range a_ : List[str] = use_cache a_ : Dict = pretraining_tp a_ : Optional[Any] = apply_residual_connection_post_layernorm a_ : Optional[Any] = hidden_dropout a_ : List[str] = attention_dropout a_ : Dict = bos_token_id a_ : Optional[int] = eos_token_id a_ : Any = slow_but_exact super().__init__(bos_token_id=__SCREAMING_SNAKE_CASE , eos_token_id=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = version.parse("1.12" ) def __init__( self : Tuple , __SCREAMING_SNAKE_CASE : PretrainedConfig , __SCREAMING_SNAKE_CASE : str = "default" , __SCREAMING_SNAKE_CASE : List[PatchingSpec] = None , __SCREAMING_SNAKE_CASE : bool = False , ) -> Optional[Any]: super().__init__(__SCREAMING_SNAKE_CASE , task=__SCREAMING_SNAKE_CASE , patching_specs=__SCREAMING_SNAKE_CASE , use_past=__SCREAMING_SNAKE_CASE ) if not getattr(self._config , '''pad_token_id''' , __SCREAMING_SNAKE_CASE ): # TODO: how to do that better? a_ : Tuple = 0 @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Mapping[str, Mapping[int, str]]: a_ : Optional[Any] = OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}} ) if self.use_past: # BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344 self.fill_with_past_key_values_(__SCREAMING_SNAKE_CASE , direction='''inputs''' , inverted_values_shape=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = {0: '''batch''', 1: '''past_sequence + sequence'''} else: a_ : Union[str, Any] = {0: '''batch''', 1: '''sequence'''} return common_inputs @property def SCREAMING_SNAKE_CASE ( self : Any ) -> int: return self._config.n_layer @property def SCREAMING_SNAKE_CASE ( self : int ) -> int: return self._config.n_head @property def SCREAMING_SNAKE_CASE ( self : int ) -> float: return 1e-3 def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : "PreTrainedTokenizer" , __SCREAMING_SNAKE_CASE : int = -1 , __SCREAMING_SNAKE_CASE : int = -1 , __SCREAMING_SNAKE_CASE : bool = False , __SCREAMING_SNAKE_CASE : Optional["TensorType"] = None , ) -> Mapping[str, Any]: a_ : Dict = super(__SCREAMING_SNAKE_CASE , self ).generate_dummy_inputs( __SCREAMING_SNAKE_CASE , batch_size=__SCREAMING_SNAKE_CASE , seq_length=__SCREAMING_SNAKE_CASE , is_pair=__SCREAMING_SNAKE_CASE , framework=__SCREAMING_SNAKE_CASE ) # We need to order the input in the way they appears in the forward() a_ : Union[str, Any] = OrderedDict({'''input_ids''': common_inputs['''input_ids''']} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch a_ , a_ : Any = common_inputs['''input_ids'''].shape # Not using the same length for past_key_values a_ : str = seqlen + 2 a_ : Any = self._config.hidden_size // self.num_attention_heads a_ : Optional[int] = ( batch * self.num_attention_heads, head_dim, past_key_values_length, ) a_ : Any = ( batch * self.num_attention_heads, past_key_values_length, head_dim, ) a_ : List[str] = [ (torch.zeros(__SCREAMING_SNAKE_CASE ), torch.zeros(__SCREAMING_SNAKE_CASE )) for _ in range(self.num_layers ) ] a_ : Union[str, Any] = common_inputs['''attention_mask'''] if self.use_past: a_ : Optional[int] = ordered_inputs['''attention_mask'''].dtype a_ : List[Any] = torch.cat( [ordered_inputs['''attention_mask'''], torch.ones(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , dtype=__SCREAMING_SNAKE_CASE )] , dim=1 ) return ordered_inputs @property def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> int: return 13
666
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig __lowerCAmelCase = { 'google/tapas-base-finetuned-sqa': ( 'https://huggingface.co/google/tapas-base-finetuned-sqa/resolve/main/config.json' ), 'google/tapas-base-finetuned-wtq': ( 'https://huggingface.co/google/tapas-base-finetuned-wtq/resolve/main/config.json' ), 'google/tapas-base-finetuned-wikisql-supervised': ( 'https://huggingface.co/google/tapas-base-finetuned-wikisql-supervised/resolve/main/config.json' ), 'google/tapas-base-finetuned-tabfact': ( 'https://huggingface.co/google/tapas-base-finetuned-tabfact/resolve/main/config.json' ), } class SCREAMING_SNAKE_CASE ( __A ): snake_case__ = "tapas" def __init__( self : str , __SCREAMING_SNAKE_CASE : int=3_0522 , __SCREAMING_SNAKE_CASE : Optional[Any]=768 , __SCREAMING_SNAKE_CASE : Any=12 , __SCREAMING_SNAKE_CASE : Tuple=12 , __SCREAMING_SNAKE_CASE : str=3072 , __SCREAMING_SNAKE_CASE : List[Any]="gelu" , __SCREAMING_SNAKE_CASE : Union[str, Any]=0.1 , __SCREAMING_SNAKE_CASE : Union[str, Any]=0.1 , __SCREAMING_SNAKE_CASE : str=1024 , __SCREAMING_SNAKE_CASE : Optional[int]=[3, 256, 256, 2, 256, 256, 10] , __SCREAMING_SNAKE_CASE : Any=0.02 , __SCREAMING_SNAKE_CASE : Dict=1e-12 , __SCREAMING_SNAKE_CASE : Tuple=0 , __SCREAMING_SNAKE_CASE : Union[str, Any]=10.0 , __SCREAMING_SNAKE_CASE : Tuple=0 , __SCREAMING_SNAKE_CASE : Optional[int]=1.0 , __SCREAMING_SNAKE_CASE : List[Any]=None , __SCREAMING_SNAKE_CASE : Optional[int]=1.0 , __SCREAMING_SNAKE_CASE : Any=False , __SCREAMING_SNAKE_CASE : str=None , __SCREAMING_SNAKE_CASE : Any=1.0 , __SCREAMING_SNAKE_CASE : List[Any]=1.0 , __SCREAMING_SNAKE_CASE : Any=False , __SCREAMING_SNAKE_CASE : Optional[Any]=False , __SCREAMING_SNAKE_CASE : Optional[int]="ratio" , __SCREAMING_SNAKE_CASE : List[str]=None , __SCREAMING_SNAKE_CASE : List[Any]=None , __SCREAMING_SNAKE_CASE : Optional[int]=64 , __SCREAMING_SNAKE_CASE : Optional[int]=32 , __SCREAMING_SNAKE_CASE : Tuple=False , __SCREAMING_SNAKE_CASE : str=True , __SCREAMING_SNAKE_CASE : Dict=False , __SCREAMING_SNAKE_CASE : List[Any]=False , __SCREAMING_SNAKE_CASE : int=True , __SCREAMING_SNAKE_CASE : str=False , __SCREAMING_SNAKE_CASE : List[str]=None , __SCREAMING_SNAKE_CASE : Union[str, Any]=None , **__SCREAMING_SNAKE_CASE : Any , ) -> List[str]: super().__init__(pad_token_id=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) # BERT hyperparameters (with updated max_position_embeddings and type_vocab_sizes) a_ : str = vocab_size a_ : str = hidden_size a_ : Optional[Any] = num_hidden_layers a_ : Any = num_attention_heads a_ : List[str] = hidden_act a_ : Tuple = intermediate_size a_ : Optional[int] = hidden_dropout_prob a_ : Optional[int] = attention_probs_dropout_prob a_ : Tuple = max_position_embeddings a_ : int = type_vocab_sizes a_ : Optional[Any] = initializer_range a_ : Any = layer_norm_eps # Fine-tuning task hyperparameters a_ : int = positive_label_weight a_ : int = num_aggregation_labels a_ : Optional[int] = aggregation_loss_weight a_ : List[str] = use_answer_as_supervision a_ : List[str] = answer_loss_importance a_ : List[Any] = use_normalized_answer_loss a_ : Dict = huber_loss_delta a_ : List[str] = temperature a_ : List[str] = aggregation_temperature a_ : int = use_gumbel_for_cells a_ : Tuple = use_gumbel_for_aggregation a_ : int = average_approximation_function a_ : List[str] = cell_selection_preference a_ : Tuple = answer_loss_cutoff a_ : str = max_num_rows a_ : Optional[int] = max_num_columns a_ : Tuple = average_logits_per_cell a_ : Optional[int] = select_one_column a_ : Tuple = allow_empty_column_selection a_ : int = init_cell_selection_weights_to_zero a_ : Optional[Any] = reset_position_index_per_cell a_ : Any = disable_per_token_loss # Aggregation hyperparameters a_ : Dict = aggregation_labels a_ : Union[str, Any] = no_aggregation_label_index if isinstance(self.aggregation_labels , __SCREAMING_SNAKE_CASE ): a_ : List[str] = {int(__SCREAMING_SNAKE_CASE ): v for k, v in aggregation_labels.items()}
704
'''simple docstring''' import sys __lowerCAmelCase = ( '73167176531330624919225119674426574742355349194934' '96983520312774506326239578318016984801869478851843' '85861560789112949495459501737958331952853208805511' '12540698747158523863050715693290963295227443043557' '66896648950445244523161731856403098711121722383113' '62229893423380308135336276614282806444486645238749' '30358907296290491560440772390713810515859307960866' '70172427121883998797908792274921901699720888093776' '65727333001053367881220235421809751254540594752243' '52584907711670556013604839586446706324415722155397' '53697817977846174064955149290862569321978468622482' '83972241375657056057490261407972968652414535100474' '82166370484403199890008895243450658541227588666881' '16427171479924442928230863465674813919123162824586' '17866458359124566529476545682848912883142607690042' '24219022671055626321111109370544217506941658960408' '07198403850962455444362981230987879927244284909188' '84580156166097919133875499200524063689912560717606' '05886116467109405077541002256983155200055935729725' '71636269561882670428252483600823257530420752963450' ) def _UpperCAmelCase ( __A : str ): a_ : Tuple = 1 for digit in s: product *= int(__A ) return product def _UpperCAmelCase ( __A : str = N ): a_ : Dict = -sys.maxsize - 1 a_ : Optional[int] = n[:13] a_ : str = 13 while cur_index < len(__A ) - 13: if int(n[cur_index] ) >= int(substr[0] ): a_ : Tuple = substr[1:] + n[cur_index] cur_index += 1 else: a_ : Dict = max(__A , str_eval(__A ) ) a_ : List[str] = n[cur_index : cur_index + 13] cur_index += 13 return largest_product if __name__ == "__main__": print(F"""{solution() = }""")
666
0
'''simple docstring''' import heapq def _UpperCAmelCase ( __A : Any ): a_ : Optional[Any] = [] # for each node and his adjacency list add them and the rank of the node to queue # using heapq module the queue will be filled like a Priority Queue # heapq works with a min priority queue, so I used -1*len(v) to build it for key, value in graph.items(): # O(log(n)) heapq.heappush(__snake_case , [-1 * len(__snake_case ), (key, value)] ) # chosen_vertices = set of chosen vertices a_ : List[str] = set() # while queue isn't empty and there are still edges # (queue[0][0] is the rank of the node with max rank) while queue and queue[0][0] != 0: # extract vertex with max rank from queue and add it to chosen_vertices a_ : Tuple = heapq.heappop(__snake_case )[1][0] chosen_vertices.add(__snake_case ) # Remove all arcs adjacent to argmax for elem in queue: # if v haven't adjacent node, skip if elem[0] == 0: continue # if argmax is reachable from elem # remove argmax from elem's adjacent list and update his rank if argmax in elem[1][1]: a_ : int = elem[1][1].index(__snake_case ) del elem[1][1][index] elem[0] += 1 # re-order the queue heapq.heapify(__snake_case ) return chosen_vertices if __name__ == "__main__": import doctest doctest.testmod() __lowerCAmelCase = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]} print(F"""Minimum vertex cover:\n{greedy_min_vertex_cover(graph)}""")
705
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( __A : list[int] ): a_ : int = len(__A ) // 2 # choose the middle 3 elements a_ : Dict = lst[m - 1 : m + 2] # if middle element is peak if three[1] > three[0] and three[1] > three[2]: return three[1] # if increasing, recurse on right elif three[0] < three[2]: if len(lst[:m] ) == 2: m -= 1 return peak(lst[m:] ) # decreasing else: if len(lst[:m] ) == 2: m += 1 return peak(lst[:m] ) if __name__ == "__main__": import doctest doctest.testmod()
666
0
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( __A : str ): if len(lowerCAmelCase__ ) < 2: raise ValueError('''Monogons and Digons are not polygons in the Euclidean space''' ) if any(i <= 0 for i in nums ): raise ValueError('''All values must be greater than 0''' ) a_ : Dict = nums.copy() copy_nums.sort() return copy_nums[-1] < sum(copy_nums[:-1] ) if __name__ == "__main__": import doctest doctest.testmod()
706
'''simple docstring''' 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 SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = LongformerTokenizer snake_case__ = True snake_case__ = LongformerTokenizerFast snake_case__ = True def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt a_ : Tuple = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] a_ : Optional[Any] = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) a_ : Union[str, Any] = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] a_ : Any = {'''unk_token''': '''<unk>'''} a_ : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) a_ : Optional[int] = 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(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : Any , **__SCREAMING_SNAKE_CASE : Any ) -> int: kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , **__SCREAMING_SNAKE_CASE : List[Any] ) -> List[str]: kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : List[Any] ) -> Any: a_ : Union[str, Any] = '''lower newer''' a_ : List[Any] = '''lower newer''' return input_text, output_text def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: a_ : Optional[Any] = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) a_ : List[str] = '''lower newer''' a_ : str = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] a_ : Optional[int] = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) # , add_prefix_space=True) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Dict = tokens + [tokenizer.unk_token] a_ : Any = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Optional[Any]: a_ : Union[str, Any] = self.get_tokenizer() self.assertListEqual(tokenizer.encode('''Hello world!''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) , [0, 3_1414, 232, 328, 2] ) self.assertListEqual( tokenizer.encode('''Hello world! cécé herlolip 418''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) , [0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2] , ) @slow def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[Any]: a_ : Dict = self.tokenizer_class.from_pretrained('''allenai/longformer-base-4096''' ) a_ : Tuple = tokenizer.encode('''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer.encode('''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.encode( '''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.encode( '''sequence builders''' , '''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : List[str] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def SCREAMING_SNAKE_CASE ( self : str ) -> int: a_ : str = self.get_tokenizer() a_ : int = '''Encode this sequence.''' a_ : List[str] = tokenizer.byte_encoder[''' '''.encode('''utf-8''' )[0]] # Testing encoder arguments a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) tokenizer.add_special_tokens({'''bos_token''': '''<s>'''} ) a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Testing spaces after special tokens a_ : Optional[Any] = '''<mask>''' tokenizer.add_special_tokens( {'''mask_token''': AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE )} ) # mask token has a left space a_ : Optional[int] = tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) a_ : List[Any] = '''Encode <mask> sequence''' a_ : List[str] = '''Encode <mask>sequence''' a_ : int = tokenizer.encode(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = encoded.index(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer.encode(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = encoded.index(__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Union[str, Any]: pass def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Any: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): a_ : Any = self.rust_tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) a_ : Any = self.tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) a_ : str = '''A, <mask> AllenNLP sentence.''' a_ : List[Any] = tokenizer_r.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer_p.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE ) # 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'''] ) , ) a_ : str = tokenizer_r.convert_ids_to_tokens(tokens_r['''input_ids'''] ) a_ : Tuple = 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, 250, 6, 5_0264, 3823, 487, 2_1992, 3645, 4, 2] ) self.assertSequenceEqual(tokens_r['''input_ids'''] , [0, 250, 6, 5_0264, 3823, 487, 2_1992, 3645, 4, 2] ) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>'''] ) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>'''] ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[Any]: for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): a_ : Any = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) a_ : str = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state['''add_prefix_space'''] , __SCREAMING_SNAKE_CASE ) self.assertEqual(post_processor_state['''add_prefix_space'''] , __SCREAMING_SNAKE_CASE ) self.assertEqual(post_processor_state['''trim_offsets'''] , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[int]: # 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})' ): a_ : Dict = '''hello''' # `hello` is a token in the vocabulary of `pretrained_name` a_ : Union[str, Any] = f'{text_of_1_token} {text_of_1_token}' a_ : Any = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ) + 1, len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Any = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ) + 1, len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : int = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ), len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Tuple = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ), len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Union[str, Any] = 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)), # ) a_ : str = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : int = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ) + 1, 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : int = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ), 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Optional[int] = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : int = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ), 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , )
666
0
'''simple docstring''' from typing import Optional, Tuple, Union import flax import flax.linen as nn import jax import jax.numpy as jnp from flax.core.frozen_dict import FrozenDict from ..configuration_utils import ConfigMixin, flax_register_to_config from ..utils import BaseOutput from .embeddings_flax import FlaxTimestepEmbedding, FlaxTimesteps from .modeling_flax_utils import FlaxModelMixin from .unet_ad_blocks_flax import ( FlaxCrossAttnDownBlockaD, FlaxDownBlockaD, FlaxUNetMidBlockaDCrossAttn, ) @flax.struct.dataclass class SCREAMING_SNAKE_CASE ( __lowerCamelCase ): snake_case__ = 42 snake_case__ = 42 class SCREAMING_SNAKE_CASE ( nn.Module ): snake_case__ = 42 snake_case__ = (16, 32, 96, 256) snake_case__ = jnp.floataa def SCREAMING_SNAKE_CASE ( self : Any ) -> Optional[Any]: a_ : Union[str, Any] = nn.Conv( self.block_out_channels[0] , kernel_size=(3, 3) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) a_ : Optional[int] = [] for i in range(len(self.block_out_channels ) - 1 ): a_ : str = self.block_out_channels[i] a_ : List[Any] = self.block_out_channels[i + 1] a_ : Tuple = nn.Conv( a_ , kernel_size=(3, 3) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) blocks.append(a_ ) a_ : Optional[Any] = nn.Conv( a_ , kernel_size=(3, 3) , strides=(2, 2) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) blocks.append(a_ ) a_ : Union[str, Any] = blocks a_ : int = nn.Conv( self.conditioning_embedding_channels , kernel_size=(3, 3) , padding=((1, 1), (1, 1)) , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) def __call__( self : Dict , __SCREAMING_SNAKE_CASE : Any ) -> int: a_ : Optional[Any] = self.conv_in(a_ ) a_ : Optional[Any] = nn.silu(a_ ) for block in self.blocks: a_ : str = block(a_ ) a_ : str = nn.silu(a_ ) a_ : Any = self.conv_out(a_ ) return embedding @flax_register_to_config class SCREAMING_SNAKE_CASE ( nn.Module , __lowerCamelCase , __lowerCamelCase ): snake_case__ = 32 snake_case__ = 4 snake_case__ = ( "CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "DownBlock2D", ) snake_case__ = False snake_case__ = (320, 640, 1280, 1280) snake_case__ = 2 snake_case__ = 8 snake_case__ = None snake_case__ = 1280 snake_case__ = 0.0 snake_case__ = False snake_case__ = jnp.floataa snake_case__ = True snake_case__ = 0 snake_case__ = "rgb" snake_case__ = (16, 32, 96, 256) def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> List[Any]: # init input tensors a_ : Tuple = (1, self.in_channels, self.sample_size, self.sample_size) a_ : Union[str, Any] = jnp.zeros(a_ , dtype=jnp.floataa ) a_ : int = jnp.ones((1,) , dtype=jnp.intaa ) a_ : int = jnp.zeros((1, 1, self.cross_attention_dim) , dtype=jnp.floataa ) a_ : Union[str, Any] = (1, 3, self.sample_size * 8, self.sample_size * 8) a_ : List[str] = jnp.zeros(a_ , dtype=jnp.floataa ) a_ : List[str] = jax.random.split(a_ ) a_ : Dict = {"params": params_rng, "dropout": dropout_rng} return self.init(a_ , a_ , a_ , a_ , a_ )["params"] def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> List[str]: a_ : Tuple = self.block_out_channels a_ : Optional[Any] = block_out_channels[0] * 4 # If `num_attention_heads` is not defined (which is the case for most models) # it will default to `attention_head_dim`. This looks weird upon first reading it and it is. # The reason for this behavior is to correct for incorrectly named variables that were introduced # when this library was created. The incorrect naming was only discovered much later in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131 # Changing `attention_head_dim` to `num_attention_heads` for 40,000+ configurations is too backwards breaking # which is why we correct for the naming here. a_ : List[str] = self.num_attention_heads or self.attention_head_dim # input a_ : Dict = nn.Conv( block_out_channels[0] , kernel_size=(3, 3) , strides=(1, 1) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) # time a_ : Union[str, Any] = FlaxTimesteps( block_out_channels[0] , flip_sin_to_cos=self.flip_sin_to_cos , freq_shift=self.config.freq_shift ) a_ : int = FlaxTimestepEmbedding(a_ , dtype=self.dtype ) a_ : List[str] = FlaxControlNetConditioningEmbedding( conditioning_embedding_channels=block_out_channels[0] , block_out_channels=self.conditioning_embedding_out_channels , ) a_ : Dict = self.only_cross_attention if isinstance(a_ , a_ ): a_ : List[Any] = (only_cross_attention,) * len(self.down_block_types ) if isinstance(a_ , a_ ): a_ : Optional[Any] = (num_attention_heads,) * len(self.down_block_types ) # down a_ : Tuple = [] a_ : List[str] = [] a_ : Dict = block_out_channels[0] a_ : Any = nn.Conv( a_ , kernel_size=(1, 1) , padding='''VALID''' , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) controlnet_down_blocks.append(a_ ) for i, down_block_type in enumerate(self.down_block_types ): a_ : List[str] = output_channel a_ : Any = block_out_channels[i] a_ : Optional[Any] = i == len(a_ ) - 1 if down_block_type == "CrossAttnDownBlock2D": a_ : Dict = FlaxCrossAttnDownBlockaD( in_channels=a_ , out_channels=a_ , dropout=self.dropout , num_layers=self.layers_per_block , num_attention_heads=num_attention_heads[i] , add_downsample=not is_final_block , use_linear_projection=self.use_linear_projection , only_cross_attention=only_cross_attention[i] , dtype=self.dtype , ) else: a_ : Dict = FlaxDownBlockaD( in_channels=a_ , out_channels=a_ , dropout=self.dropout , num_layers=self.layers_per_block , add_downsample=not is_final_block , dtype=self.dtype , ) down_blocks.append(a_ ) for _ in range(self.layers_per_block ): a_ : Dict = nn.Conv( a_ , kernel_size=(1, 1) , padding='''VALID''' , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) controlnet_down_blocks.append(a_ ) if not is_final_block: a_ : Union[str, Any] = nn.Conv( a_ , kernel_size=(1, 1) , padding='''VALID''' , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) controlnet_down_blocks.append(a_ ) a_ : List[str] = down_blocks a_ : Union[str, Any] = controlnet_down_blocks # mid a_ : Optional[int] = block_out_channels[-1] a_ : List[str] = FlaxUNetMidBlockaDCrossAttn( in_channels=a_ , dropout=self.dropout , num_attention_heads=num_attention_heads[-1] , use_linear_projection=self.use_linear_projection , dtype=self.dtype , ) a_ : str = nn.Conv( a_ , kernel_size=(1, 1) , padding='''VALID''' , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) def __call__( self : int , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Any = 1.0 , __SCREAMING_SNAKE_CASE : List[Any] = True , __SCREAMING_SNAKE_CASE : Optional[int] = False , ) -> List[Any]: a_ : int = self.controlnet_conditioning_channel_order if channel_order == "bgr": a_ : int = jnp.flip(a_ , axis=1 ) # 1. time if not isinstance(a_ , jnp.ndarray ): a_ : List[str] = jnp.array([timesteps] , dtype=jnp.intaa ) elif isinstance(a_ , jnp.ndarray ) and len(timesteps.shape ) == 0: a_ : str = timesteps.astype(dtype=jnp.floataa ) a_ : Any = jnp.expand_dims(a_ , 0 ) a_ : Optional[int] = self.time_proj(a_ ) a_ : Any = self.time_embedding(a_ ) # 2. pre-process a_ : Union[str, Any] = jnp.transpose(a_ , (0, 2, 3, 1) ) a_ : Tuple = self.conv_in(a_ ) a_ : Tuple = jnp.transpose(a_ , (0, 2, 3, 1) ) a_ : int = self.controlnet_cond_embedding(a_ ) sample += controlnet_cond # 3. down a_ : str = (sample,) for down_block in self.down_blocks: if isinstance(a_ , a_ ): a_ : Dict = down_block(a_ , a_ , a_ , deterministic=not train ) else: a_ : Tuple = down_block(a_ , a_ , deterministic=not train ) down_block_res_samples += res_samples # 4. mid a_ : List[str] = self.mid_block(a_ , a_ , a_ , deterministic=not train ) # 5. contronet blocks a_ : Union[str, Any] = () for down_block_res_sample, controlnet_block in zip(a_ , self.controlnet_down_blocks ): a_ : Optional[int] = controlnet_block(a_ ) controlnet_down_block_res_samples += (down_block_res_sample,) a_ : List[Any] = controlnet_down_block_res_samples a_ : Any = self.controlnet_mid_block(a_ ) # 6. scaling a_ : Optional[int] = [sample * conditioning_scale for sample in down_block_res_samples] mid_block_res_sample *= conditioning_scale if not return_dict: return (down_block_res_samples, mid_block_res_sample) return FlaxControlNetOutput( down_block_res_samples=a_ , mid_block_res_sample=a_ )
707
'''simple docstring''' import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = {'vocab_file': 'sentencepiece.bpe.model'} __lowerCAmelCase = { 'vocab_file': { 'moussaKam/mbarthez': 'https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model', 'moussaKam/barthez': 'https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model', 'moussaKam/barthez-orangesum-title': ( 'https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model' ), }, } __lowerCAmelCase = { 'moussaKam/mbarthez': 1_024, 'moussaKam/barthez': 1_024, 'moussaKam/barthez-orangesum-title': 1_024, } __lowerCAmelCase = '▁' class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = VOCAB_FILES_NAMES snake_case__ = PRETRAINED_VOCAB_FILES_MAP snake_case__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case__ = ["input_ids", "attention_mask"] def __init__( self : Dict , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Dict="<s>" , __SCREAMING_SNAKE_CASE : List[Any]="</s>" , __SCREAMING_SNAKE_CASE : List[str]="</s>" , __SCREAMING_SNAKE_CASE : List[str]="<s>" , __SCREAMING_SNAKE_CASE : Dict="<unk>" , __SCREAMING_SNAKE_CASE : int="<pad>" , __SCREAMING_SNAKE_CASE : Tuple="<mask>" , __SCREAMING_SNAKE_CASE : Optional[Dict[str, Any]] = None , **__SCREAMING_SNAKE_CASE : Optional[Any] , ) -> None: # Mask token behave like a normal word, i.e. include the space before it a_ : Tuple = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else mask_token a_ : List[str] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , sp_model_kwargs=self.sp_model_kwargs , **__SCREAMING_SNAKE_CASE , ) a_ : Tuple = vocab_file a_ : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(__SCREAMING_SNAKE_CASE ) ) a_ : Optional[Any] = {'''<s>''': 0, '''<pad>''': 1, '''</s>''': 2, '''<unk>''': 3} a_ : Any = len(self.sp_model ) - 1 a_ : Optional[Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None ) -> List[int]: if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] a_ : List[str] = [self.cls_token_id] a_ : Optional[Any] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None , __SCREAMING_SNAKE_CASE : bool = False ) -> List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__SCREAMING_SNAKE_CASE , token_ids_a=__SCREAMING_SNAKE_CASE , already_has_special_tokens=__SCREAMING_SNAKE_CASE ) if token_ids_a is None: return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1, 1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None ) -> List[int]: a_ : List[str] = [self.sep_token_id] a_ : Any = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def SCREAMING_SNAKE_CASE ( self : Any ) -> Union[str, Any]: return len(self.sp_model ) def SCREAMING_SNAKE_CASE ( self : str ) -> Tuple: a_ : int = {self.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def SCREAMING_SNAKE_CASE ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : str ) -> List[str]: return self.sp_model.encode(__SCREAMING_SNAKE_CASE , out_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : List[Any] ) -> Dict: if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] a_ : Optional[int] = self.sp_model.PieceToId(__SCREAMING_SNAKE_CASE ) return spm_id if spm_id else self.unk_token_id def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : int ) -> str: if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Optional[int]: a_ : Dict = [] a_ : List[Any] = '''''' a_ : Dict = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) + token a_ : Dict = True a_ : Optional[Any] = [] else: current_sub_tokens.append(__SCREAMING_SNAKE_CASE ) a_ : Tuple = False out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) return out_string.strip() def __getstate__( self : Dict ) -> int: a_ : Dict = self.__dict__.copy() a_ : List[str] = None return state def __setstate__( self : Dict , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Any: a_ : Optional[Any] = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): a_ : Union[str, Any] = {} a_ : int = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Optional[str] = None ) -> Tuple[str]: if not os.path.isdir(__SCREAMING_SNAKE_CASE ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return a_ : Union[str, Any] = os.path.join( __SCREAMING_SNAKE_CASE , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__SCREAMING_SNAKE_CASE ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __SCREAMING_SNAKE_CASE ) elif not os.path.isfile(self.vocab_file ): with open(__SCREAMING_SNAKE_CASE , '''wb''' ) as fi: a_ : Any = self.sp_model.serialized_model_proto() fi.write(__SCREAMING_SNAKE_CASE ) return (out_vocab_file,)
666
0
'''simple docstring''' import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_torch, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_torch_available, is_vision_available if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import MgpstrProcessor, ViTImageProcessor @require_torch @require_vision class SCREAMING_SNAKE_CASE ( unittest.TestCase ): snake_case__ = ViTImageProcessor if is_vision_available() else None @property def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Dict: return self.image_processor_tester.prepare_image_processor_dict() def SCREAMING_SNAKE_CASE ( self : Any ) -> int: a_ : List[Any] = (3, 32, 128) a_ : Optional[int] = tempfile.mkdtemp() # fmt: off a_ : Any = ['''[GO]''', '''[s]''', '''0''', '''1''', '''2''', '''3''', '''4''', '''5''', '''6''', '''7''', '''8''', '''9''', '''a''', '''b''', '''c''', '''d''', '''e''', '''f''', '''g''', '''h''', '''i''', '''j''', '''k''', '''l''', '''m''', '''n''', '''o''', '''p''', '''q''', '''r''', '''s''', '''t''', '''u''', '''v''', '''w''', '''x''', '''y''', '''z'''] # fmt: on a_ : int = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) a_ : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE ) + '''\n''' ) a_ : Any = { '''do_normalize''': False, '''do_resize''': True, '''image_processor_type''': '''ViTImageProcessor''', '''resample''': 3, '''size''': {'''height''': 32, '''width''': 128}, } a_ : int = os.path.join(self.tmpdirname , __SCREAMING_SNAKE_CASE ) with open(self.image_processor_file , '''w''' , encoding='''utf-8''' ) as fp: json.dump(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , **__SCREAMING_SNAKE_CASE : Dict ) -> Optional[Any]: return MgpstrTokenizer.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : str , **__SCREAMING_SNAKE_CASE : Dict ) -> Optional[int]: return ViTImageProcessor.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Any: shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Dict: a_ : List[str] = np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta ) a_ : List[str] = Image.fromarray(np.moveaxis(__SCREAMING_SNAKE_CASE , 0 , -1 ) ) return image_input def SCREAMING_SNAKE_CASE ( self : List[str] ) -> int: a_ : str = self.get_tokenizer() a_ : Tuple = self.get_image_processor() a_ : List[Any] = MgpstrProcessor(tokenizer=__SCREAMING_SNAKE_CASE , image_processor=__SCREAMING_SNAKE_CASE ) processor.save_pretrained(self.tmpdirname ) a_ : Any = MgpstrProcessor.from_pretrained(self.tmpdirname , use_fast=__SCREAMING_SNAKE_CASE ) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.char_tokenizer , __SCREAMING_SNAKE_CASE ) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Dict: a_ : Dict = self.get_tokenizer() a_ : Optional[int] = self.get_image_processor() a_ : Union[str, Any] = MgpstrProcessor(tokenizer=__SCREAMING_SNAKE_CASE , image_processor=__SCREAMING_SNAKE_CASE ) processor.save_pretrained(self.tmpdirname ) a_ : str = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) a_ : List[Any] = self.get_image_processor(do_normalize=__SCREAMING_SNAKE_CASE , padding_value=1.0 ) a_ : str = MgpstrProcessor.from_pretrained( self.tmpdirname , bos_token='''(BOS)''' , eos_token='''(EOS)''' , do_normalize=__SCREAMING_SNAKE_CASE , padding_value=1.0 ) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.char_tokenizer , __SCREAMING_SNAKE_CASE ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> Any: a_ : Optional[int] = self.get_image_processor() a_ : Optional[Any] = self.get_tokenizer() a_ : str = MgpstrProcessor(tokenizer=__SCREAMING_SNAKE_CASE , image_processor=__SCREAMING_SNAKE_CASE ) a_ : int = self.prepare_image_inputs() a_ : List[Any] = image_processor(__SCREAMING_SNAKE_CASE , return_tensors='''np''' ) a_ : List[Any] = processor(images=__SCREAMING_SNAKE_CASE , return_tensors='''np''' ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1e-2 ) def SCREAMING_SNAKE_CASE ( self : Any ) -> Any: a_ : List[str] = self.get_image_processor() a_ : str = self.get_tokenizer() a_ : Optional[Any] = MgpstrProcessor(tokenizer=__SCREAMING_SNAKE_CASE , image_processor=__SCREAMING_SNAKE_CASE ) a_ : List[str] = '''test''' a_ : List[str] = processor(text=__SCREAMING_SNAKE_CASE ) a_ : int = tokenizer(__SCREAMING_SNAKE_CASE ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> List[str]: a_ : str = self.get_image_processor() a_ : str = self.get_tokenizer() a_ : Dict = MgpstrProcessor(tokenizer=__SCREAMING_SNAKE_CASE , image_processor=__SCREAMING_SNAKE_CASE ) a_ : List[Any] = '''test''' a_ : str = self.prepare_image_inputs() a_ : Optional[Any] = processor(text=__SCREAMING_SNAKE_CASE , images=__SCREAMING_SNAKE_CASE ) self.assertListEqual(list(inputs.keys() ) , ['''pixel_values''', '''labels'''] ) # test if it raises when no input is passed with pytest.raises(__SCREAMING_SNAKE_CASE ): processor() def SCREAMING_SNAKE_CASE ( self : Any ) -> str: a_ : Optional[int] = self.get_image_processor() a_ : Union[str, Any] = self.get_tokenizer() a_ : Dict = MgpstrProcessor(tokenizer=__SCREAMING_SNAKE_CASE , image_processor=__SCREAMING_SNAKE_CASE ) a_ : List[str] = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9], [3, 4, 3, 1, 1, 8, 9]] a_ : Optional[int] = processor.char_decode(__SCREAMING_SNAKE_CASE ) a_ : List[Any] = tokenizer.batch_decode(__SCREAMING_SNAKE_CASE ) a_ : Dict = [seq.replace(''' ''' , '''''' ) for seq in decoded_tok] self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Optional[int]: a_ : Any = self.get_image_processor() a_ : Dict = self.get_tokenizer() a_ : List[str] = MgpstrProcessor(tokenizer=__SCREAMING_SNAKE_CASE , image_processor=__SCREAMING_SNAKE_CASE ) a_ : Any = None a_ : Optional[int] = self.prepare_image_inputs() a_ : List[Any] = processor(text=__SCREAMING_SNAKE_CASE , images=__SCREAMING_SNAKE_CASE ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Union[str, Any]: a_ : Dict = self.get_image_processor() a_ : int = self.get_tokenizer() a_ : List[Any] = MgpstrProcessor(tokenizer=__SCREAMING_SNAKE_CASE , image_processor=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = torch.randn(1 , 27 , 38 ) a_ : Tuple = torch.randn(1 , 27 , 5_0257 ) a_ : List[str] = torch.randn(1 , 27 , 3_0522 ) a_ : Optional[int] = processor.batch_decode([char_input, bpe_input, wp_input] ) self.assertListEqual(list(results.keys() ) , ['''generated_text''', '''scores''', '''char_preds''', '''bpe_preds''', '''wp_preds'''] )
708
'''simple docstring''' def _UpperCAmelCase ( __A : str , __A : str ): def get_matched_characters(__A : str , __A : str ) -> str: a_ : Union[str, Any] = [] a_ : int = min(len(_stra ) , len(_stra ) ) // 2 for i, l in enumerate(_stra ): a_ : Any = int(max(0 , i - limit ) ) a_ : Union[str, Any] = int(min(i + limit + 1 , len(_stra ) ) ) if l in _stra[left:right]: matched.append(__A ) a_ : Any = f'{_stra[0:_stra.index(__A )]} {_stra[_stra.index(__A ) + 1:]}' return "".join(__A ) # matching characters a_ : Optional[Any] = get_matched_characters(__A , __A ) a_ : int = get_matched_characters(__A , __A ) a_ : Any = len(__A ) # transposition a_ : List[Any] = ( len([(ca, ca) for ca, ca in zip(__A , __A ) if ca != ca] ) // 2 ) if not match_count: a_ : Dict = 0.0 else: a_ : Optional[int] = ( 1 / 3 * ( match_count / len(__A ) + match_count / len(__A ) + (match_count - transpositions) / match_count ) ) # common prefix up to 4 characters a_ : List[str] = 0 for ca, ca in zip(stra[:4] , stra[:4] ): if ca == ca: prefix_len += 1 else: break return jaro + 0.1 * prefix_len * (1 - jaro) if __name__ == "__main__": import doctest doctest.testmod() print(jaro_winkler('hello', 'world'))
666
0
'''simple docstring''' import webbrowser from sys import argv from urllib.parse import parse_qs, quote import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": __lowerCAmelCase = '%20'.join(argv[1:]) if len(argv) > 1 else quote(str(input('Search: '))) print('Googling.....') __lowerCAmelCase = F"""https://www.google.com/search?q={query}&num=100""" __lowerCAmelCase = requests.get( url, headers={'User-Agent': str(UserAgent().random)}, ) try: __lowerCAmelCase = ( BeautifulSoup(res.text, 'html.parser') .find('div', attrs={'class': 'yuRUbf'}) .find('a') .get('href') ) except AttributeError: __lowerCAmelCase = parse_qs( BeautifulSoup(res.text, 'html.parser') .find('div', attrs={'class': 'kCrYT'}) .find('a') .get('href') )['url'][0] webbrowser.open(link)
709
'''simple docstring''' import torch from transformers import AutoModel class SCREAMING_SNAKE_CASE ( torch.nn.Module ): def __init__( self : Optional[int] , __SCREAMING_SNAKE_CASE : int="sayef/fsner-bert-base-uncased" ) -> str: super(__SCREAMING_SNAKE_CASE , self ).__init__() a_ : str = AutoModel.from_pretrained(__SCREAMING_SNAKE_CASE , return_dict=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = torch.nn.CosineSimilarity(3 , 1e-08 ) a_ : Dict = torch.nn.Softmax(dim=1 ) def SCREAMING_SNAKE_CASE ( self : str , **__SCREAMING_SNAKE_CASE : int ) -> str: return self.bert(**__SCREAMING_SNAKE_CASE ).last_hidden_state def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : List[Any] ) -> Optional[int]: return token_embeddings.sum(2 , keepdim=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : int=1 ) -> Dict: return self.softmax(T * self.cos(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[int] ) -> Any: a_ : Dict = W_supports['''sizes'''].tolist() a_ : Tuple = W_supports['''start_token_id'''].item() a_ : List[Any] = W_supports['''end_token_id'''].item() del W_supports["sizes"] del W_supports["start_token_id"] del W_supports["end_token_id"] a_ : int = self.BERT(**__SCREAMING_SNAKE_CASE ) a_ : Any = self.BERT(**__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = None a_ : Tuple = None a_ : List[str] = W_supports['''input_ids'''] == start_token_id a_ : Dict = W_supports['''input_ids'''] == end_token_id for i, size in enumerate(__SCREAMING_SNAKE_CASE ): if i == 0: a_ : str = 0 else: a_ : str = support_sizes[i - 1] a_ : Union[str, Any] = S[s : s + size][start_token_masks[s : s + size]] a_ : Tuple = S[s : s + size][end_token_masks[s : s + size]] a_ : Tuple = torch.matmul(q[i] , s_start.T ).sum(1 ).softmax(0 ) a_ : Optional[Any] = torch.matmul(q[i] , s_end.T ).sum(1 ).softmax(0 ) if p_starts is not None: a_ : Any = torch.vstack((p_starts, p_start) ) a_ : Dict = torch.vstack((p_ends, p_end) ) else: a_ : Optional[int] = p_start a_ : List[Any] = p_end return p_starts, p_ends
666
0
'''simple docstring''' import os import re import sys import traceback import warnings from pathlib import Path from typing import Dict, Optional, Union from uuid import uuida from huggingface_hub import HfFolder, ModelCard, ModelCardData, hf_hub_download, whoami from huggingface_hub.file_download import REGEX_COMMIT_HASH from huggingface_hub.utils import ( EntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError, is_jinja_available, ) from packaging import version from requests import HTTPError from .. import __version__ from .constants import ( DEPRECATED_REVISION_ARGS, DIFFUSERS_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, ) from .import_utils import ( ENV_VARS_TRUE_VALUES, _flax_version, _jax_version, _onnxruntime_version, _torch_version, is_flax_available, is_onnx_available, is_torch_available, ) from .logging import get_logger __lowerCAmelCase = get_logger(__name__) __lowerCAmelCase = Path(__file__).parent / 'model_card_template.md' __lowerCAmelCase = uuida().hex __lowerCAmelCase = os.getenv('HF_HUB_OFFLINE', '').upper() in ENV_VARS_TRUE_VALUES __lowerCAmelCase = os.getenv('DISABLE_TELEMETRY', '').upper() in ENV_VARS_TRUE_VALUES __lowerCAmelCase = HUGGINGFACE_CO_RESOLVE_ENDPOINT + '/api/telemetry/' def _UpperCAmelCase ( __A : List[str] = None ): a_ : Dict = f'diffusers/{__version__}; python/{sys.version.split()[0]}; session_id/{SESSION_ID}' if DISABLE_TELEMETRY or HF_HUB_OFFLINE: return ua + "; telemetry/off" if is_torch_available(): ua += f'; torch/{_torch_version}' if is_flax_available(): ua += f'; jax/{_jax_version}' ua += f'; flax/{_flax_version}' if is_onnx_available(): ua += f'; onnxruntime/{_onnxruntime_version}' # CI will set this value to True if os.environ.get('''DIFFUSERS_IS_CI''' , '''''' ).upper() in ENV_VARS_TRUE_VALUES: ua += "; is_ci/true" if isinstance(A_ , A_ ): ua += "; " + "; ".join(f'{k}/{v}' for k, v in user_agent.items() ) elif isinstance(A_ , A_ ): ua += "; " + user_agent return ua def _UpperCAmelCase ( __A : Any , __A : List[str] = None , __A : Union[str, Any] = None ): if token is None: a_ : List[str] = HfFolder.get_token() if organization is None: a_ : List[Any] = whoami(A_ )['''name'''] return f'{username}/{model_id}' else: return f'{organization}/{model_id}' def _UpperCAmelCase ( __A : List[str] , __A : int ): if not is_jinja_available(): raise ValueError( '''Modelcard rendering is based on Jinja templates.''' ''' Please make sure to have `jinja` installed before using `create_model_card`.''' ''' To install it, please run `pip install Jinja2`.''' ) if hasattr(A_ , '''local_rank''' ) and args.local_rank not in [-1, 0]: return a_ : List[Any] = args.hub_token if hasattr(A_ , '''hub_token''' ) else None a_ : Any = get_full_repo_name(A_ , token=A_ ) a_ : Optional[Any] = ModelCard.from_template( card_data=ModelCardData( # Card metadata object that will be converted to YAML block language='''en''' , license='''apache-2.0''' , library_name='''diffusers''' , tags=[] , datasets=args.dataset_name , metrics=[] , ) , template_path=A_ , model_name=A_ , repo_name=A_ , dataset_name=args.dataset_name if hasattr(A_ , '''dataset_name''' ) else None , learning_rate=args.learning_rate , train_batch_size=args.train_batch_size , eval_batch_size=args.eval_batch_size , gradient_accumulation_steps=( args.gradient_accumulation_steps if hasattr(A_ , '''gradient_accumulation_steps''' ) else None ) , adam_betaa=args.adam_betaa if hasattr(A_ , '''adam_beta1''' ) else None , adam_betaa=args.adam_betaa if hasattr(A_ , '''adam_beta2''' ) else None , adam_weight_decay=args.adam_weight_decay if hasattr(A_ , '''adam_weight_decay''' ) else None , adam_epsilon=args.adam_epsilon if hasattr(A_ , '''adam_epsilon''' ) else None , lr_scheduler=args.lr_scheduler if hasattr(A_ , '''lr_scheduler''' ) else None , lr_warmup_steps=args.lr_warmup_steps if hasattr(A_ , '''lr_warmup_steps''' ) else None , ema_inv_gamma=args.ema_inv_gamma if hasattr(A_ , '''ema_inv_gamma''' ) else None , ema_power=args.ema_power if hasattr(A_ , '''ema_power''' ) else None , ema_max_decay=args.ema_max_decay if hasattr(A_ , '''ema_max_decay''' ) else None , mixed_precision=args.mixed_precision , ) a_ : Optional[Any] = os.path.join(args.output_dir , '''README.md''' ) model_card.save(A_ ) def _UpperCAmelCase ( __A : List[Any] , __A : List[str] = None ): if resolved_file is None or commit_hash is not None: return commit_hash a_ : int = str(Path(A_ ).as_posix() ) a_ : str = re.search(R'''snapshots/([^/]+)/''' , A_ ) if search is None: return None a_ : Union[str, Any] = search.groups()[0] return commit_hash if REGEX_COMMIT_HASH.match(A_ ) else None # Old default cache path, potentially to be migrated. # This logic was more or less taken from `transformers`, with the following differences: # - Diffusers doesn't use custom environment variables to specify the cache path. # - There is no need to migrate the cache format, just move the files to the new location. __lowerCAmelCase = os.path.expanduser( os.getenv('HF_HOME', os.path.join(os.getenv('XDG_CACHE_HOME', '~/.cache'), 'huggingface')) ) __lowerCAmelCase = os.path.join(hf_cache_home, 'diffusers') def _UpperCAmelCase ( __A : Optional[int] = None , __A : Dict = None ): if new_cache_dir is None: a_ : Optional[int] = DIFFUSERS_CACHE if old_cache_dir is None: a_ : Union[str, Any] = old_diffusers_cache a_ : List[Any] = Path(A_ ).expanduser() a_ : Union[str, Any] = Path(A_ ).expanduser() for old_blob_path in old_cache_dir.glob('''**/blobs/*''' ): if old_blob_path.is_file() and not old_blob_path.is_symlink(): a_ : Optional[Any] = new_cache_dir / old_blob_path.relative_to(A_ ) new_blob_path.parent.mkdir(parents=A_ , exist_ok=A_ ) os.replace(A_ , A_ ) try: os.symlink(A_ , A_ ) except OSError: logger.warning( '''Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded.''' ) # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). __lowerCAmelCase = os.path.join(DIFFUSERS_CACHE, 'version_diffusers_cache.txt') if not os.path.isfile(cache_version_file): __lowerCAmelCase = 0 else: with open(cache_version_file) as f: try: __lowerCAmelCase = int(f.read()) except ValueError: __lowerCAmelCase = 0 if cache_version < 1: __lowerCAmelCase = os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0 if old_cache_is_not_empty: logger.warning( 'The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your ' 'existing cached models. This is a one-time operation, you can interrupt it or run it ' 'later by calling `diffusers.utils.hub_utils.move_cache()`.' ) try: move_cache() except Exception as e: __lowerCAmelCase = '\n'.join(traceback.format_tb(e.__traceback__)) logger.error( F"""There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease """ 'file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole ' 'message and we will do our best to help.' ) if cache_version < 1: try: os.makedirs(DIFFUSERS_CACHE, exist_ok=True) with open(cache_version_file, 'w') as f: f.write('1') except Exception: logger.warning( F"""There was a problem when trying to write in your cache folder ({DIFFUSERS_CACHE}). Please, ensure """ 'the directory exists and can be written to.' ) def _UpperCAmelCase ( __A : List[str] , __A : Optional[int] = None ): if variant is not None: a_ : Any = weights_name.split('''.''' ) a_ : Optional[int] = splits[:-1] + [variant] + splits[-1:] a_ : Tuple = '''.'''.join(A_ ) return weights_name def _UpperCAmelCase ( __A : Optional[Any] , *, __A : Union[str, Any] , __A : int , __A : int , __A : Union[str, Any] , __A : Tuple , __A : Union[str, Any] , __A : Optional[int] , __A : str , __A : Union[str, Any] , __A : Optional[Any] , __A : Union[str, Any]=None , ): a_ : Optional[Any] = str(A_ ) if os.path.isfile(A_ ): return pretrained_model_name_or_path elif os.path.isdir(A_ ): if os.path.isfile(os.path.join(A_ , A_ ) ): # Load from a PyTorch checkpoint a_ : Union[str, Any] = os.path.join(A_ , A_ ) return model_file elif subfolder is not None and os.path.isfile( os.path.join(A_ , A_ , A_ ) ): a_ : List[Any] = os.path.join(A_ , A_ , A_ ) return model_file else: raise EnvironmentError( f'Error no file named {weights_name} found in directory {pretrained_model_name_or_path}.' ) else: # 1. First check if deprecated way of loading from branches is used if ( revision in DEPRECATED_REVISION_ARGS and (weights_name == WEIGHTS_NAME or weights_name == SAFETENSORS_WEIGHTS_NAME) and version.parse(version.parse(A_ ).base_version ) >= version.parse('''0.20.0''' ) ): try: a_ : Optional[int] = hf_hub_download( A_ , filename=_add_variant(A_ , A_ ) , cache_dir=A_ , force_download=A_ , proxies=A_ , resume_download=A_ , local_files_only=A_ , use_auth_token=A_ , user_agent=A_ , subfolder=A_ , revision=revision or commit_hash , ) warnings.warn( f'Loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'` is deprecated. Loading instead from `revision=\'main\'` with `variant={revision}`. Loading model variants via `revision=\'{revision}\'` will be removed in diffusers v1. Please use `variant=\'{revision}\'` instead.' , A_ , ) return model_file except: # noqa: E722 warnings.warn( f'You are loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant=\'{revision}\'` instead. However, it appears that {pretrained_model_name_or_path} currently does not have a {_add_variant(A_ , A_ )} file in the \'main\' branch of {pretrained_model_name_or_path}. \n The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title \'{pretrained_model_name_or_path} is missing {_add_variant(A_ , A_ )}\' so that the correct variant file can be added.' , A_ , ) try: # 2. Load model file as usual a_ : int = hf_hub_download( A_ , filename=A_ , cache_dir=A_ , force_download=A_ , proxies=A_ , resume_download=A_ , local_files_only=A_ , use_auth_token=A_ , user_agent=A_ , subfolder=A_ , revision=revision or commit_hash , ) return model_file except RepositoryNotFoundError: raise EnvironmentError( f'{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier ' '''listed on \'https://huggingface.co/models\'\nIf this is a private repository, make sure to pass a ''' '''token having permission to this repo with `use_auth_token` or log in with `huggingface-cli ''' '''login`.''' ) except RevisionNotFoundError: raise EnvironmentError( f'{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for ' '''this model name. Check the model page at ''' f'\'https://huggingface.co/{pretrained_model_name_or_path}\' for available revisions.' ) except EntryNotFoundError: raise EnvironmentError( f'{pretrained_model_name_or_path} does not appear to have a file named {weights_name}.' ) except HTTPError as err: raise EnvironmentError( f'There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n{err}' ) except ValueError: raise EnvironmentError( f'We couldn\'t connect to \'{HUGGINGFACE_CO_RESOLVE_ENDPOINT}\' to load this model, couldn\'t find it' f' in the cached files and it looks like {pretrained_model_name_or_path} is not the path to a' f' directory containing a file named {weights_name} or' ''' \nCheckout your internet connection or see how to run the library in''' ''' offline mode at \'https://huggingface.co/docs/diffusers/installation#offline-mode\'.''' ) except EnvironmentError: raise EnvironmentError( f'Can\'t load the model for \'{pretrained_model_name_or_path}\'. If you were trying to load it from ' '''\'https://huggingface.co/models\', make sure you don\'t have a local directory with the same name. ''' f'Otherwise, make sure \'{pretrained_model_name_or_path}\' is the correct path to a directory ' f'containing a file named {weights_name}' )
710
'''simple docstring''' import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import ClassLabel, Features, Image from .base import TaskTemplate @dataclass(frozen=SCREAMING_SNAKE_CASE_ ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = field(default="image-classification" , metadata={"include_in_asdict_even_if_is_default": True} ) snake_case__ = Features({"image": Image()} ) snake_case__ = Features({"labels": ClassLabel} ) snake_case__ = "image" snake_case__ = "labels" def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : List[Any] ) -> Any: if self.label_column not in features: raise ValueError(f'Column {self.label_column} is not present in features.' ) if not isinstance(features[self.label_column] , __SCREAMING_SNAKE_CASE ): raise ValueError(f'Column {self.label_column} is not a ClassLabel.' ) a_ : Optional[int] = copy.deepcopy(self ) a_ : int = self.label_schema.copy() a_ : Tuple = features[self.label_column] a_ : str = label_schema return task_template @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict[str, str]: return { self.image_column: "image", self.label_column: "labels", }
666
0
'''simple docstring''' def _UpperCAmelCase ( __A : Any , __A : Optional[int] ): a_ : Any = 1 # To kept the Calculated Value # Since C(n, k) = C(n, n-k) if k > (n - k): a_ : List[str] = n - k # Calculate C(n,k) for i in range(__A ): result *= n - i result //= i + 1 return result def _UpperCAmelCase ( __A : Union[str, Any] ): return binomial_coefficient(2 * node_count , __A ) // (node_count + 1) def _UpperCAmelCase ( __A : List[str] ): if n < 0: raise ValueError('''factorial() not defined for negative values''' ) a_ : str = 1 for i in range(1 , n + 1 ): result *= i return result def _UpperCAmelCase ( __A : Dict ): return catalan_number(__A ) * factorial(__A ) if __name__ == "__main__": __lowerCAmelCase = int(input('Enter the number of nodes: ').strip() or 0) if node_count <= 0: raise ValueError('We need some nodes to work with.') print( F"""Given {node_count} nodes, there are {binary_tree_count(node_count)} """ F"""binary trees and {catalan_number(node_count)} binary search trees.""" )
711
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( __A : tuple[int, int] , __A : int ): a_ , a_ : List[str] = position a_ : Optional[int] = [ (y + 1, x + 2), (y - 1, x + 2), (y + 1, x - 2), (y - 1, x - 2), (y + 2, x + 1), (y + 2, x - 1), (y - 2, x + 1), (y - 2, x - 1), ] a_ : Any = [] for position in positions: a_ , a_ : Dict = position if 0 <= y_test < n and 0 <= x_test < n: permissible_positions.append(__A ) return permissible_positions def _UpperCAmelCase ( __A : list[list[int]] ): return not any(elem == 0 for row in board for elem in row ) def _UpperCAmelCase ( __A : list[list[int]] , __A : tuple[int, int] , __A : int ): if is_complete(__A ): return True for position in get_valid_pos(__A , len(__A ) ): a_ , a_ : Dict = position if board[y][x] == 0: a_ : Optional[Any] = curr + 1 if open_knight_tour_helper(__A , __A , curr + 1 ): return True a_ : Tuple = 0 return False def _UpperCAmelCase ( __A : int ): a_ : List[str] = [[0 for i in range(__A )] for j in range(__A )] for i in range(__A ): for j in range(__A ): a_ : Optional[Any] = 1 if open_knight_tour_helper(__A , (i, j) , 1 ): return board a_ : Union[str, Any] = 0 a_ : Dict = f'Open Kight Tour cannot be performed on a board of size {n}' raise ValueError(__A ) if __name__ == "__main__": import doctest doctest.testmod()
666
0
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( __A : Dict ): a_ : int = str(__snake_case ) return n == n[::-1] def _UpperCAmelCase ( __A : Tuple = 1_00_00_00 ): a_ : Union[str, Any] = 0 for i in range(1 , __snake_case ): if is_palindrome(__snake_case ) and is_palindrome(bin(__snake_case ).split('''b''' )[1] ): total += i return total if __name__ == "__main__": print(solution(int(str(input().strip()))))
712
'''simple docstring''' import warnings warnings.warn( 'memory_utils has been reorganized to utils.memory. Import `find_executable_batchsize` from the main `__init__`: ' '`from accelerate import find_executable_batch_size` to avoid this warning.', FutureWarning, )
666
0
'''simple docstring''' import logging from pathlib import Path import numpy as np import pytorch_lightning as pl import torch from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint from pytorch_lightning.utilities import rank_zero_only from utils_rag import save_json def _UpperCAmelCase ( __A : Optional[int] ): a_ : Union[str, Any] = filter(lambda __A : p.requires_grad , model.parameters() ) a_ : List[str] = sum([np.prod(p.size() ) for p in model_parameters] ) return params __lowerCAmelCase = logging.getLogger(__name__) def _UpperCAmelCase ( __A : Union[str, Any] , __A : Any ): if metric == "rouge2": a_ : Union[str, Any] = """{val_avg_rouge2:.4f}-{step_count}""" elif metric == "bleu": a_ : Optional[Any] = """{val_avg_bleu:.4f}-{step_count}""" elif metric == "em": a_ : List[Any] = """{val_avg_em:.4f}-{step_count}""" else: raise NotImplementedError( f'seq2seq callbacks only support rouge2 and bleu, got {metric}, You can make your own by adding to this' ''' function.''' ) a_ : str = ModelCheckpoint( dirpath=lowerCamelCase_ , filename=lowerCamelCase_ , monitor=f'val_{metric}' , mode='''max''' , save_top_k=3 , every_n_epochs=1 , ) return checkpoint_callback def _UpperCAmelCase ( __A : Any , __A : Optional[Any] ): return EarlyStopping( monitor=f'val_{metric}' , mode='''min''' if '''loss''' in metric else '''max''' , patience=lowerCamelCase_ , verbose=lowerCamelCase_ , ) class SCREAMING_SNAKE_CASE ( pl.Callback ): def SCREAMING_SNAKE_CASE ( self : Any , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : str ) -> Tuple: a_ : Tuple = {f'lr_group_{i}': param["""lr"""] for i, param in enumerate(pl_module.trainer.optimizers[0].param_groups )} pl_module.logger.log_metrics(lowerCamelCase_ ) @rank_zero_only def SCREAMING_SNAKE_CASE ( self : List[Any] , __SCREAMING_SNAKE_CASE : pl.Trainer , __SCREAMING_SNAKE_CASE : pl.LightningModule , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : List[str]=True ) -> int: logger.info(f'***** {type_path} results at step {trainer.global_step:05d} *****' ) a_ : Union[str, Any] = trainer.callback_metrics trainer.logger.log_metrics({k: v for k, v in metrics.items() if k not in ['''log''', '''progress_bar''', '''preds''']} ) # Log results a_ : Union[str, Any] = Path(pl_module.hparams.output_dir ) if type_path == "test": a_ : Optional[Any] = od / """test_results.txt""" a_ : Tuple = od / """test_generations.txt""" else: # this never gets hit. I prefer not to save intermediate generations, and results are in metrics.json # If people want this it will be easy enough to add back. a_ : Optional[int] = od / f'{type_path}_results/{trainer.global_step:05d}.txt' a_ : int = od / f'{type_path}_generations/{trainer.global_step:05d}.txt' results_file.parent.mkdir(exist_ok=lowerCamelCase_ ) generations_file.parent.mkdir(exist_ok=lowerCamelCase_ ) with open(lowerCamelCase_ , '''a+''' ) as writer: for key in sorted(lowerCamelCase_ ): if key in ["log", "progress_bar", "preds"]: continue a_ : Any = metrics[key] if isinstance(lowerCamelCase_ , torch.Tensor ): a_ : Optional[int] = val.item() a_ : Union[str, Any] = f'{key}: {val:.6f}\n' writer.write(lowerCamelCase_ ) if not save_generations: return if "preds" in metrics: a_ : List[str] = """\n""".join(metrics['''preds'''] ) generations_file.open('''w+''' ).write(lowerCamelCase_ ) @rank_zero_only def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Any ) -> Optional[int]: try: a_ : Any = pl_module.model.model.num_parameters() except AttributeError: a_ : List[str] = pl_module.model.num_parameters() a_ : str = count_trainable_parameters(lowerCamelCase_ ) # mp stands for million parameters trainer.logger.log_metrics({'''n_params''': npars, '''mp''': npars / 1e6, '''grad_mp''': n_trainable_pars / 1e6} ) @rank_zero_only def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : pl.Trainer , __SCREAMING_SNAKE_CASE : pl.LightningModule ) -> Union[str, Any]: save_json(pl_module.metrics , pl_module.metrics_save_path ) return self._write_logs(lowerCamelCase_ , lowerCamelCase_ , '''test''' ) @rank_zero_only def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : pl.Trainer , __SCREAMING_SNAKE_CASE : Any ) -> List[str]: save_json(pl_module.metrics , pl_module.metrics_save_path ) # Uncommenting this will save val generations # return self._write_logs(trainer, pl_module, "valid")
713
'''simple docstring''' import requests from bsa import BeautifulSoup def _UpperCAmelCase ( __A : str , __A : dict ): a_ : Tuple = BeautifulSoup(requests.get(__A , params=__A ).content , '''html.parser''' ) a_ : List[str] = soup.find('''div''' , attrs={'''class''': '''gs_ri'''} ) a_ : List[str] = div.find('''div''' , attrs={'''class''': '''gs_fl'''} ).find_all('''a''' ) return anchors[2].get_text() if __name__ == "__main__": __lowerCAmelCase = { 'title': ( 'Precisely geometry controlled microsupercapacitors for ultrahigh areal ' 'capacitance, volumetric capacitance, and energy density' ), 'journal': 'Chem. Mater.', 'volume': 30, 'pages': '3979-3990', 'year': 2_018, 'hl': 'en', } print(get_citation('https://scholar.google.com/scholar_lookup', params=params))
666
0
'''simple docstring''' from typing import Optional import torch import torch.utils.checkpoint from torch import Tensor, nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import ( BackboneOutput, BaseModelOutputWithNoAttention, BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention, ) from ...modeling_utils import PreTrainedModel from ...utils import ( add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging, replace_return_docstrings, ) from ...utils.backbone_utils import BackboneMixin from .configuration_resnet import ResNetConfig __lowerCAmelCase = logging.get_logger(__name__) # General docstring __lowerCAmelCase = 'ResNetConfig' # Base docstring __lowerCAmelCase = 'microsoft/resnet-50' __lowerCAmelCase = [1, 2_048, 7, 7] # Image classification docstring __lowerCAmelCase = 'microsoft/resnet-50' __lowerCAmelCase = 'tiger cat' __lowerCAmelCase = [ 'microsoft/resnet-50', # See all resnet models at https://huggingface.co/models?filter=resnet ] class SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : Any , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int = 3 , __SCREAMING_SNAKE_CASE : int = 1 , __SCREAMING_SNAKE_CASE : str = "relu" ) -> List[Any]: super().__init__() a_ : Union[str, Any] = nn.Convad( UpperCamelCase_ , UpperCamelCase_ , kernel_size=UpperCamelCase_ , stride=UpperCamelCase_ , padding=kernel_size // 2 , bias=UpperCamelCase_ ) a_ : List[str] = nn.BatchNormad(UpperCamelCase_ ) a_ : Optional[Any] = ACTaFN[activation] if activation is not None else nn.Identity() def SCREAMING_SNAKE_CASE ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : Tensor ) -> int: a_ : str = self.convolution(UpperCamelCase_ ) a_ : List[str] = self.normalization(UpperCamelCase_ ) a_ : Dict = self.activation(UpperCamelCase_ ) return hidden_state class SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : ResNetConfig ) -> Union[str, Any]: super().__init__() a_ : Any = ResNetConvLayer( config.num_channels , config.embedding_size , kernel_size=7 , stride=2 , activation=config.hidden_act ) a_ : Optional[int] = nn.MaxPoolad(kernel_size=3 , stride=2 , padding=1 ) a_ : Dict = config.num_channels def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : Tensor ) -> Any: a_ : List[str] = pixel_values.shape[1] if num_channels != self.num_channels: raise ValueError( '''Make sure that the channel dimension of the pixel values match with the one set in the configuration.''' ) a_ : Tuple = self.embedder(UpperCamelCase_ ) a_ : str = self.pooler(UpperCamelCase_ ) return embedding class SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : Tuple , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int = 2 ) -> List[Any]: super().__init__() a_ : Union[str, Any] = nn.Convad(UpperCamelCase_ , UpperCamelCase_ , kernel_size=1 , stride=UpperCamelCase_ , bias=UpperCamelCase_ ) a_ : Dict = nn.BatchNormad(UpperCamelCase_ ) def SCREAMING_SNAKE_CASE ( self : int , __SCREAMING_SNAKE_CASE : Tensor ) -> Optional[int]: a_ : Any = self.convolution(UpperCamelCase_ ) a_ : str = self.normalization(UpperCamelCase_ ) return hidden_state class SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : Optional[Any] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int = 1 , __SCREAMING_SNAKE_CASE : str = "relu" ) -> str: super().__init__() a_ : Tuple = in_channels != out_channels or stride != 1 a_ : Optional[int] = ( ResNetShortCut(UpperCamelCase_ , UpperCamelCase_ , stride=UpperCamelCase_ ) if should_apply_shortcut else nn.Identity() ) a_ : Optional[Any] = nn.Sequential( ResNetConvLayer(UpperCamelCase_ , UpperCamelCase_ , stride=UpperCamelCase_ ) , ResNetConvLayer(UpperCamelCase_ , UpperCamelCase_ , activation=UpperCamelCase_ ) , ) a_ : List[str] = ACTaFN[activation] def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : Tuple ) -> str: a_ : Tuple = hidden_state a_ : str = self.layer(UpperCamelCase_ ) a_ : int = self.shortcut(UpperCamelCase_ ) hidden_state += residual a_ : Any = self.activation(UpperCamelCase_ ) return hidden_state class SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : Optional[Any] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int = 1 , __SCREAMING_SNAKE_CASE : str = "relu" , __SCREAMING_SNAKE_CASE : int = 4 ) -> List[Any]: super().__init__() a_ : str = in_channels != out_channels or stride != 1 a_ : Any = out_channels // reduction a_ : str = ( ResNetShortCut(UpperCamelCase_ , UpperCamelCase_ , stride=UpperCamelCase_ ) if should_apply_shortcut else nn.Identity() ) a_ : List[Any] = nn.Sequential( ResNetConvLayer(UpperCamelCase_ , UpperCamelCase_ , kernel_size=1 ) , ResNetConvLayer(UpperCamelCase_ , UpperCamelCase_ , stride=UpperCamelCase_ ) , ResNetConvLayer(UpperCamelCase_ , UpperCamelCase_ , kernel_size=1 , activation=UpperCamelCase_ ) , ) a_ : List[str] = ACTaFN[activation] def SCREAMING_SNAKE_CASE ( self : int , __SCREAMING_SNAKE_CASE : List[str] ) -> Optional[int]: a_ : Union[str, Any] = hidden_state a_ : Optional[int] = self.layer(UpperCamelCase_ ) a_ : str = self.shortcut(UpperCamelCase_ ) hidden_state += residual a_ : List[Any] = self.activation(UpperCamelCase_ ) return hidden_state class SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : ResNetConfig , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int = 2 , __SCREAMING_SNAKE_CASE : int = 2 , ) -> int: super().__init__() a_ : Union[str, Any] = ResNetBottleNeckLayer if config.layer_type == '''bottleneck''' else ResNetBasicLayer a_ : str = nn.Sequential( # downsampling is done in the first layer with stride of 2 layer(UpperCamelCase_ , UpperCamelCase_ , stride=UpperCamelCase_ , activation=config.hidden_act ) , *[layer(UpperCamelCase_ , UpperCamelCase_ , activation=config.hidden_act ) for _ in range(depth - 1 )] , ) def SCREAMING_SNAKE_CASE ( self : Any , __SCREAMING_SNAKE_CASE : Tensor ) -> List[str]: a_ : Union[str, Any] = input for layer in self.layers: a_ : str = layer(UpperCamelCase_ ) return hidden_state class SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : Any , __SCREAMING_SNAKE_CASE : ResNetConfig ) -> List[str]: super().__init__() a_ : Dict = nn.ModuleList([] ) # based on `downsample_in_first_stage` the first layer of the first stage may or may not downsample the input self.stages.append( ResNetStage( UpperCamelCase_ , config.embedding_size , config.hidden_sizes[0] , stride=2 if config.downsample_in_first_stage else 1 , depth=config.depths[0] , ) ) a_ : Optional[int] = zip(config.hidden_sizes , config.hidden_sizes[1:] ) for (in_channels, out_channels), depth in zip(UpperCamelCase_ , config.depths[1:] ): self.stages.append(ResNetStage(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , depth=UpperCamelCase_ ) ) def SCREAMING_SNAKE_CASE ( self : Tuple , __SCREAMING_SNAKE_CASE : Tensor , __SCREAMING_SNAKE_CASE : bool = False , __SCREAMING_SNAKE_CASE : bool = True ) -> Dict: a_ : Tuple = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: a_ : int = hidden_states + (hidden_state,) a_ : Any = stage_module(UpperCamelCase_ ) if output_hidden_states: a_ : Tuple = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return BaseModelOutputWithNoAttention( last_hidden_state=UpperCamelCase_ , hidden_states=UpperCamelCase_ , ) class SCREAMING_SNAKE_CASE ( __lowerCamelCase ): snake_case__ = ResNetConfig snake_case__ = 'resnet' snake_case__ = 'pixel_values' snake_case__ = True def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : Any ) -> Optional[Any]: if isinstance(UpperCamelCase_ , nn.Convad ): nn.init.kaiming_normal_(module.weight , mode='''fan_out''' , nonlinearity='''relu''' ) elif isinstance(UpperCamelCase_ , (nn.BatchNormad, nn.GroupNorm) ): nn.init.constant_(module.weight , 1 ) nn.init.constant_(module.bias , 0 ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : List[str]=False ) -> Any: if isinstance(UpperCamelCase_ , UpperCamelCase_ ): a_ : List[Any] = value __lowerCAmelCase = r'\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n\n Parameters:\n config ([`ResNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n' __lowerCAmelCase = r'\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`ConvNextImageProcessor.__call__`] for details.\n\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( "The bare ResNet model outputting raw features without any specific head on top." , __lowerCamelCase , ) class SCREAMING_SNAKE_CASE ( __lowerCamelCase ): def __init__( self : int , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Dict: super().__init__(UpperCamelCase_ ) a_ : List[str] = config a_ : Union[str, Any] = ResNetEmbeddings(UpperCamelCase_ ) a_ : Tuple = ResNetEncoder(UpperCamelCase_ ) a_ : str = nn.AdaptiveAvgPoolad((1, 1) ) # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UpperCamelCase_ ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=UpperCamelCase_ , config_class=_CONFIG_FOR_DOC , modality='''vision''' , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : Tensor , __SCREAMING_SNAKE_CASE : Optional[bool] = None , __SCREAMING_SNAKE_CASE : Optional[bool] = None ) -> int: a_ : Dict = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) a_ : Tuple = return_dict if return_dict is not None else self.config.use_return_dict a_ : Any = self.embedder(UpperCamelCase_ ) a_ : Optional[Any] = self.encoder( UpperCamelCase_ , output_hidden_states=UpperCamelCase_ , return_dict=UpperCamelCase_ ) a_ : List[str] = encoder_outputs[0] a_ : Any = self.pooler(UpperCamelCase_ ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=UpperCamelCase_ , pooler_output=UpperCamelCase_ , hidden_states=encoder_outputs.hidden_states , ) @add_start_docstrings( "\n ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n " , __lowerCamelCase , ) class SCREAMING_SNAKE_CASE ( __lowerCamelCase ): def __init__( self : str , __SCREAMING_SNAKE_CASE : int ) -> int: super().__init__(UpperCamelCase_ ) a_ : Dict = config.num_labels a_ : Optional[Any] = ResNetModel(UpperCamelCase_ ) # classification head a_ : Optional[Any] = nn.Sequential( nn.Flatten() , nn.Linear(config.hidden_sizes[-1] , config.num_labels ) if config.num_labels > 0 else nn.Identity() , ) # initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UpperCamelCase_ ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=UpperCamelCase_ , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[torch.FloatTensor] = None , __SCREAMING_SNAKE_CASE : Optional[torch.LongTensor] = None , __SCREAMING_SNAKE_CASE : Optional[bool] = None , __SCREAMING_SNAKE_CASE : Optional[bool] = None , ) -> Union[str, Any]: a_ : Any = return_dict if return_dict is not None else self.config.use_return_dict a_ : Union[str, Any] = self.resnet(UpperCamelCase_ , output_hidden_states=UpperCamelCase_ , return_dict=UpperCamelCase_ ) a_ : str = outputs.pooler_output if return_dict else outputs[1] a_ : List[Any] = self.classifier(UpperCamelCase_ ) a_ : Optional[int] = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: a_ : int = '''regression''' elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): a_ : Union[str, Any] = '''single_label_classification''' else: a_ : Tuple = '''multi_label_classification''' if self.config.problem_type == "regression": a_ : List[str] = MSELoss() if self.num_labels == 1: a_ : str = loss_fct(logits.squeeze() , labels.squeeze() ) else: a_ : int = loss_fct(UpperCamelCase_ , UpperCamelCase_ ) elif self.config.problem_type == "single_label_classification": a_ : str = CrossEntropyLoss() a_ : Union[str, Any] = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": a_ : Tuple = BCEWithLogitsLoss() a_ : Optional[Any] = loss_fct(UpperCamelCase_ , UpperCamelCase_ ) if not return_dict: a_ : Tuple = (logits,) + outputs[2:] return (loss,) + output if loss is not None else output return ImageClassifierOutputWithNoAttention(loss=UpperCamelCase_ , logits=UpperCamelCase_ , hidden_states=outputs.hidden_states ) @add_start_docstrings( "\n ResNet backbone, to be used with frameworks like DETR and MaskFormer.\n " , __lowerCamelCase , ) class SCREAMING_SNAKE_CASE ( __lowerCamelCase , __lowerCamelCase ): def __init__( self : Dict , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> List[str]: super().__init__(UpperCamelCase_ ) super()._init_backbone(UpperCamelCase_ ) a_ : int = [config.embedding_size] + config.hidden_sizes a_ : List[str] = ResNetEmbeddings(UpperCamelCase_ ) a_ : Tuple = ResNetEncoder(UpperCamelCase_ ) # initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UpperCamelCase_ ) @replace_return_docstrings(output_type=UpperCamelCase_ , config_class=_CONFIG_FOR_DOC ) def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : Tensor , __SCREAMING_SNAKE_CASE : Optional[bool] = None , __SCREAMING_SNAKE_CASE : Optional[bool] = None ) -> Tuple: a_ : Tuple = return_dict if return_dict is not None else self.config.use_return_dict a_ : Any = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) a_ : Any = self.embedder(UpperCamelCase_ ) a_ : List[Any] = self.encoder(UpperCamelCase_ , output_hidden_states=UpperCamelCase_ , return_dict=UpperCamelCase_ ) a_ : str = outputs.hidden_states a_ : int = () for idx, stage in enumerate(self.stage_names ): if stage in self.out_features: feature_maps += (hidden_states[idx],) if not return_dict: a_ : List[Any] = (feature_maps,) if output_hidden_states: output += (outputs.hidden_states,) return output return BackboneOutput( feature_maps=UpperCamelCase_ , hidden_states=outputs.hidden_states if output_hidden_states else None , attentions=UpperCamelCase_ , )
714
'''simple docstring''' import argparse from torch import nn # transformers_old should correspond to branch `save_old_prophetnet_model_structure` here # original prophetnet_checkpoints are saved under `patrickvonplaten/..._old` respectively from transformers_old.modeling_prophetnet import ( ProphetNetForConditionalGeneration as ProphetNetForConditionalGenerationOld, ) from transformers_old.modeling_xlm_prophetnet import ( XLMProphetNetForConditionalGeneration as XLMProphetNetForConditionalGenerationOld, ) from transformers import ProphetNetForConditionalGeneration, XLMProphetNetForConditionalGeneration, logging __lowerCAmelCase = logging.get_logger(__name__) logging.set_verbosity_info() def _UpperCAmelCase ( __A : str , __A : str ): if "xprophetnet" in prophetnet_checkpoint_path: a_ : Tuple = XLMProphetNetForConditionalGenerationOld.from_pretrained(__A ) a_ , a_ : Optional[Any] = XLMProphetNetForConditionalGeneration.from_pretrained( __A , output_loading_info=__A ) else: a_ : List[Any] = ProphetNetForConditionalGenerationOld.from_pretrained(__A ) a_ , a_ : Any = ProphetNetForConditionalGeneration.from_pretrained( __A , output_loading_info=__A ) a_ : str = ['''key_proj''', '''value_proj''', '''query_proj'''] a_ : Tuple = { '''self_attn''': '''ngram_self_attn''', '''cross_attn''': '''encoder_attn''', '''cross_attn_layer_norm''': '''encoder_attn_layer_norm''', '''feed_forward_layer_norm''': '''final_layer_norm''', '''feed_forward''': '''''', '''intermediate''': '''fc1''', '''output''': '''fc2''', '''key_proj''': '''k_proj''', '''query_proj''': '''q_proj''', '''value_proj''': '''v_proj''', '''word_embeddings''': '''embed_tokens''', '''embeddings_layer_norm''': '''emb_layer_norm''', '''relative_pos_embeddings''': '''relative_linear''', '''ngram_embeddings''': '''ngram_input_embed''', '''position_embeddings''': '''embed_positions''', } for key in loading_info["missing_keys"]: a_ : List[str] = key.split('''.''' ) if attributes[0] == "lm_head": a_ : List[str] = prophet a_ : Dict = prophet_old else: a_ : str = prophet.prophetnet a_ : int = prophet_old.model a_ : str = False for attribute in attributes: if attribute in mapping: a_ : Dict = mapping[attribute] if not hasattr(__A , __A ) and len(__A ) > 0: a_ : List[str] = attribute elif hasattr(__A , __A ): a_ : Union[str, Any] = attribute if attribute == "weight": assert old_model.weight.shape == model.weight.shape, "Shapes have to match!" a_ : Tuple = old_model.weight logger.info(f'{attribute} is initialized.' ) a_ : Union[str, Any] = True break elif attribute == "bias": assert old_model.bias.shape == model.bias.shape, "Shapes have to match!" a_ : Union[str, Any] = old_model.bias logger.info(f'{attribute} is initialized' ) a_ : Dict = True break elif attribute in special_keys and hasattr(__A , '''in_proj_weight''' ): a_ : Tuple = old_model.in_proj_weight.shape[0] // 3 a_ : Any = getattr(__A , __A ) param.weight.shape == old_model.in_proj_weight[:embed_dim, :].shape, "Shapes have to match" param.bias.shape == old_model.in_proj_bias[:embed_dim].shape, "Shapes have to match" if attribute == "query_proj": a_ : Union[str, Any] = nn.Parameter(old_model.in_proj_weight[:embed_dim, :] ) a_ : Optional[Any] = nn.Parameter(old_model.in_proj_bias[:embed_dim] ) elif attribute == "key_proj": a_ : List[Any] = nn.Parameter(old_model.in_proj_weight[embed_dim : 2 * embed_dim, :] ) a_ : Optional[int] = nn.Parameter(old_model.in_proj_bias[embed_dim : 2 * embed_dim] ) elif attribute == "value_proj": a_ : Tuple = nn.Parameter(old_model.in_proj_weight[2 * embed_dim :, :] ) a_ : Any = nn.Parameter(old_model.in_proj_bias[2 * embed_dim :] ) a_ : Dict = True break elif attribute == "position_embeddings": assert ( model.position_embeddings.weight.shape[-1] == old_model.embed_positions.weight.shape[-1] ), "Hidden size has to match" assert model.position_embeddings.weight.shape[0] == 5_12, "We want 512 position_embeddings." a_ : Union[str, Any] = nn.Parameter(old_model.embed_positions.weight[:5_12, :] ) a_ : Optional[Any] = True break if attribute.isdigit(): a_ : Union[str, Any] = model[int(__A )] a_ : str = old_model[int(__A )] else: a_ : Tuple = getattr(__A , __A ) if old_attribute == "": a_ : List[str] = old_model else: if not hasattr(__A , __A ): raise ValueError(f'{old_model} does not have {old_attribute}' ) a_ : Optional[Any] = getattr(__A , __A ) if not is_key_init: raise ValueError(f'{key} was not correctly initialized!' ) print(f'Saving model to {pytorch_dump_folder_path}' ) prophet.save_pretrained(__A ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '--prophetnet_checkpoint_path', default=None, type=str, required=True, help='Path the official PyTorch dump.' ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) __lowerCAmelCase = parser.parse_args() convert_prophetnet_checkpoint_to_pytorch(args.prophetnet_checkpoint_path, args.pytorch_dump_folder_path)
666
0
from importlib import import_module from .logging import get_logger __lowerCAmelCase = get_logger(__name__) class SCREAMING_SNAKE_CASE : def __init__( self : int , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : Optional[Any]=None ) -> Any: a_ : Any = attrs or [] if module is not None: for key in module.__dict__: if key in attrs or not key.startswith('''__''' ): setattr(self , __SCREAMING_SNAKE_CASE , getattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) a_ : Tuple = module._original_module if isinstance(__SCREAMING_SNAKE_CASE , _PatchedModuleObj ) else module class SCREAMING_SNAKE_CASE : snake_case__ = [] def __init__( self : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Optional[int]=None ) -> Dict: a_ : List[str] = obj a_ : int = target a_ : List[Any] = new a_ : Union[str, Any] = target.split('''.''' )[0] a_ : List[Any] = {} a_ : str = attrs or [] def __enter__( self : Any ) -> Tuple: a_ : Dict = self.target.split('''.''' ) # Patch modules: # it's used to patch attributes of submodules like "os.path.join"; # in this case we need to patch "os" and "os.path" for i in range(len(__SCREAMING_SNAKE_CASE ) ): try: a_ : Optional[Any] = import_module('''.'''.join(submodules[: i + 1] ) ) except ModuleNotFoundError: continue # We iterate over all the globals in self.obj in case we find "os" or "os.path" for attr in self.obj.__dir__(): a_ : List[str] = getattr(self.obj , __SCREAMING_SNAKE_CASE ) # We don't check for the name of the global, but rather if its value *is* "os" or "os.path". # This allows to patch renamed modules like "from os import path as ospath". if obj_attr is submodule or ( (isinstance(__SCREAMING_SNAKE_CASE , _PatchedModuleObj ) and obj_attr._original_module is submodule) ): a_ : Tuple = obj_attr # patch at top level setattr(self.obj , __SCREAMING_SNAKE_CASE , _PatchedModuleObj(__SCREAMING_SNAKE_CASE , attrs=self.attrs ) ) a_ : Union[str, Any] = getattr(self.obj , __SCREAMING_SNAKE_CASE ) # construct lower levels patches for key in submodules[i + 1 :]: setattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , _PatchedModuleObj(getattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) , attrs=self.attrs ) ) a_ : Any = getattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # finally set the target attribute setattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , self.new ) # Patch attribute itself: # it's used for builtins like "open", # and also to patch "os.path.join" we may also need to patch "join" # itself if it was imported as "from os.path import join". if submodules: # if it's an attribute of a submodule like "os.path.join" try: a_ : List[Any] = getattr(import_module('''.'''.join(__SCREAMING_SNAKE_CASE ) ) , __SCREAMING_SNAKE_CASE ) except (AttributeError, ModuleNotFoundError): return # We iterate over all the globals in self.obj in case we find "os.path.join" for attr in self.obj.__dir__(): # We don't check for the name of the global, but rather if its value *is* "os.path.join". # This allows to patch renamed attributes like "from os.path import join as pjoin". if getattr(self.obj , __SCREAMING_SNAKE_CASE ) is attr_value: a_ : Optional[int] = getattr(self.obj , __SCREAMING_SNAKE_CASE ) setattr(self.obj , __SCREAMING_SNAKE_CASE , self.new ) elif target_attr in globals()["__builtins__"]: # if it'a s builtin like "open" a_ : Any = globals()["__builtins__"][target_attr] setattr(self.obj , __SCREAMING_SNAKE_CASE , self.new ) else: raise RuntimeError(f'Tried to patch attribute {target_attr} instead of a submodule.' ) def __exit__( self : Union[str, Any] , *__SCREAMING_SNAKE_CASE : List[Any] ) -> Optional[int]: for attr in list(self.original ): setattr(self.obj , __SCREAMING_SNAKE_CASE , self.original.pop(__SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Optional[int]: self.__enter__() self._active_patches.append(self ) def SCREAMING_SNAKE_CASE ( self : Any ) -> int: try: self._active_patches.remove(self ) except ValueError: # If the patch hasn't been started this will fail return None return self.__exit__()
715
'''simple docstring''' import re import string import numpy as np import datasets __lowerCAmelCase = '\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n' __lowerCAmelCase = '\nArgs:\n predictions: List of predicted texts.\n references: List of reference texts.\n regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n ignore when calculating the exact matches. Note: these regexes are removed\n from the input data before the changes based on the options below (e.g. ignore_case,\n ignore_punctuation, ignore_numbers) are applied.\n ignore_case: Boolean, defaults to False. If true, turns everything\n to lowercase so that capitalization differences are ignored.\n ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\n ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\nReturns:\n exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 25.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 50.0\n\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 75.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n >>> print(round(results["exact_match"], 1))\n 100.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["The cat sat on the mat.", "Theaters are great.", "It\'s like comparing oranges and apples."]\n >>> preds = ["The cat sat on the mat?", "Theaters are great.", "It\'s like comparing apples and oranges."]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 33.3\n\n' __lowerCAmelCase = '\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class SCREAMING_SNAKE_CASE ( datasets.Metric ): def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> int: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , reference_urls=[] , ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Dict=None , __SCREAMING_SNAKE_CASE : int=False , __SCREAMING_SNAKE_CASE : Optional[int]=False , __SCREAMING_SNAKE_CASE : Dict=False , ) -> str: if regexes_to_ignore is not None: for s in regexes_to_ignore: a_ : Optional[Any] = np.array([re.sub(__SCREAMING_SNAKE_CASE , '''''' , __SCREAMING_SNAKE_CASE ) for x in predictions] ) a_ : int = np.array([re.sub(__SCREAMING_SNAKE_CASE , '''''' , __SCREAMING_SNAKE_CASE ) for x in references] ) else: a_ : List[str] = np.asarray(__SCREAMING_SNAKE_CASE ) a_ : Any = np.asarray(__SCREAMING_SNAKE_CASE ) if ignore_case: a_ : List[str] = np.char.lower(__SCREAMING_SNAKE_CASE ) a_ : List[Any] = np.char.lower(__SCREAMING_SNAKE_CASE ) if ignore_punctuation: a_ : Any = string.punctuation.maketrans('''''' , '''''' , string.punctuation ) a_ : Union[str, Any] = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : int = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) if ignore_numbers: a_ : int = string.digits.maketrans('''''' , '''''' , string.digits ) a_ : Optional[int] = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : Dict = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = predictions == references return {"exact_match": np.mean(__SCREAMING_SNAKE_CASE ) * 100}
666
0
'''simple docstring''' import unittest import numpy as np def _UpperCAmelCase ( __A : Optional[Any] , __A : Optional[int] , __A : List[str] , __A : Union[str, Any] = None , ): a_ : List[str] = np.shape(a__ ) a_ : Union[str, Any] = np.shape(a__ ) a_ : List[str] = np.shape(a__ ) if shape_a[0] != shape_b[0]: a_ : Union[str, Any] = ( '''Expected the same number of rows for A and B. ''' f'Instead found A of size {shape_a} and B of size {shape_b}' ) raise ValueError(a__ ) if shape_b[1] != shape_c[1]: a_ : Optional[Any] = ( '''Expected the same number of columns for B and C. ''' f'Instead found B of size {shape_b} and C of size {shape_c}' ) raise ValueError(a__ ) a_ : Optional[Any] = pseudo_inv if a_inv is None: try: a_ : List[str] = np.linalg.inv(a__ ) except np.linalg.LinAlgError: raise ValueError( '''Input matrix A is not invertible. Cannot compute Schur complement.''' ) return mat_c - mat_b.T @ a_inv @ mat_b class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : Dict ) -> None: a_ : Dict = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) a_ : List[str] = np.array([[0, 3], [3, 0], [2, 3]] ) a_ : Union[str, Any] = np.array([[2, 1], [6, 3]] ) a_ : Optional[int] = schur_complement(lowercase_ , lowercase_ , lowercase_ ) a_ : Tuple = np.block([[a, b], [b.T, c]] ) a_ : str = np.linalg.det(lowercase_ ) a_ : Dict = np.linalg.det(lowercase_ ) a_ : Tuple = np.linalg.det(lowercase_ ) self.assertAlmostEqual(lowercase_ , det_a * det_s ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> None: a_ : str = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) a_ : Union[str, Any] = np.array([[0, 3], [3, 0], [2, 3]] ) a_ : Tuple = np.array([[2, 1], [6, 3]] ) with self.assertRaises(lowercase_ ): schur_complement(lowercase_ , lowercase_ , lowercase_ ) def SCREAMING_SNAKE_CASE ( self : int ) -> None: a_ : List[Any] = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) a_ : int = np.array([[0, 3], [3, 0], [2, 3]] ) a_ : Any = np.array([[2, 1, 3], [6, 3, 5]] ) with self.assertRaises(lowercase_ ): schur_complement(lowercase_ , lowercase_ , lowercase_ ) if __name__ == "__main__": import doctest doctest.testmod() unittest.main()
716
'''simple docstring''' import gc import unittest import numpy as np import torch from diffusers import ( AudioDiffusionPipeline, AutoencoderKL, DDIMScheduler, DDPMScheduler, DiffusionPipeline, Mel, UNetaDConditionModel, UNetaDModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : Dict ) -> List[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> List[Any]: torch.manual_seed(0 ) a_ : Tuple = UNetaDModel( sample_size=(32, 64) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''') , ) return model @property def SCREAMING_SNAKE_CASE ( self : int ) -> List[Any]: torch.manual_seed(0 ) a_ : Any = UNetaDConditionModel( sample_size=(64, 32) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''CrossAttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''CrossAttnUpBlock2D''') , cross_attention_dim=10 , ) return model @property def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: torch.manual_seed(0 ) a_ : List[Any] = AutoencoderKL( sample_size=(128, 64) , in_channels=1 , out_channels=1 , latent_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''DownEncoderBlock2D''', '''DownEncoderBlock2D''') , up_block_types=('''UpDecoderBlock2D''', '''UpDecoderBlock2D''') , ) a_ : List[Any] = UNetaDModel( sample_size=(64, 32) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''') , ) return vqvae, unet @slow def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union[str, Any]: a_ : Dict = '''cpu''' # ensure determinism for the device-dependent torch.Generator a_ : Union[str, Any] = Mel( x_res=self.dummy_unet.config.sample_size[1] , y_res=self.dummy_unet.config.sample_size[0] , ) a_ : Any = DDPMScheduler() a_ : str = AudioDiffusionPipeline(vqvae=__SCREAMING_SNAKE_CASE , unet=self.dummy_unet , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : List[Any] = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : List[str] = pipe(generator=__SCREAMING_SNAKE_CASE , steps=4 ) a_ : List[Any] = output.audios[0] a_ : Dict = output.images[0] a_ : Dict = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : Optional[Any] = pipe(generator=__SCREAMING_SNAKE_CASE , steps=4 , return_dict=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output[0][0] assert audio.shape == (1, (self.dummy_unet.config.sample_size[1] - 1) * mel.hop_length) assert ( image.height == self.dummy_unet.config.sample_size[0] and image.width == self.dummy_unet.config.sample_size[1] ) a_ : Dict = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : str = np.frombuffer(image_from_tuple.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([69, 255, 255, 255, 0, 0, 77, 181, 12, 127] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() == 0 a_ : str = Mel( x_res=self.dummy_vqvae_and_unet[0].config.sample_size[1] , y_res=self.dummy_vqvae_and_unet[0].config.sample_size[0] , ) a_ : int = DDIMScheduler() a_ : Dict = self.dummy_vqvae_and_unet a_ : List[str] = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0] , unet=dummy_vqvae_and_unet[1] , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : Any = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) np.random.seed(0 ) a_ : List[str] = np.random.uniform(-1 , 1 , ((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,) ) a_ : int = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : int = pipe(raw_audio=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , start_step=5 , steps=10 ) a_ : List[str] = output.images[0] assert ( image.height == self.dummy_vqvae_and_unet[0].config.sample_size[0] and image.width == self.dummy_vqvae_and_unet[0].config.sample_size[1] ) a_ : Optional[Any] = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([120, 117, 110, 109, 138, 167, 138, 148, 132, 121] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 a_ : List[str] = self.dummy_unet_condition a_ : Dict = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0] , unet=__SCREAMING_SNAKE_CASE , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : int = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) np.random.seed(0 ) a_ : Any = torch.rand((1, 1, 10) ) a_ : Tuple = pipe(generator=__SCREAMING_SNAKE_CASE , encoding=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output.images[0] a_ : Dict = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([107, 103, 120, 127, 142, 122, 113, 122, 97, 111] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 @slow @require_torch_gpu class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Any: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Any: a_ : Any = torch_device a_ : Optional[int] = DiffusionPipeline.from_pretrained('''teticio/audio-diffusion-ddim-256''' ) a_ : Dict = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : str = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : List[str] = pipe(generator=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output.audios[0] a_ : Tuple = output.images[0] assert audio.shape == (1, (pipe.unet.config.sample_size[1] - 1) * pipe.mel.hop_length) assert image.height == pipe.unet.config.sample_size[0] and image.width == pipe.unet.config.sample_size[1] a_ : str = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : Tuple = np.array([151, 167, 154, 144, 122, 134, 121, 105, 70, 26] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0
666
0
import os import socket from contextlib import contextmanager import torch from ..commands.config.default import write_basic_config # noqa: F401 from ..state import PartialState from .dataclasses import DistributedType from .imports import is_deepspeed_available, is_tpu_available from .transformer_engine import convert_model from .versions import is_torch_version if is_deepspeed_available(): from deepspeed import DeepSpeedEngine if is_tpu_available(check_device=False): import torch_xla.core.xla_model as xm def _UpperCAmelCase ( __A : str ): if is_torch_version('''<''' , '''2.0.0''' ) or not hasattr(snake_case__ , '''_dynamo''' ): return False return isinstance(snake_case__ , torch._dynamo.eval_frame.OptimizedModule ) def _UpperCAmelCase ( __A : Union[str, Any] , __A : bool = True ): a_ : Any = (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel) a_ : int = is_compiled_module(snake_case__ ) if is_compiled: a_ : str = model a_ : Dict = model._orig_mod if is_deepspeed_available(): options += (DeepSpeedEngine,) while isinstance(snake_case__ , snake_case__ ): a_ : Optional[Any] = model.module if not keep_fpaa_wrapper: a_ : str = getattr(snake_case__ , '''forward''' ) a_ : Tuple = model.__dict__.pop('''_original_forward''' , snake_case__ ) if original_forward is not None: while hasattr(snake_case__ , '''__wrapped__''' ): a_ : Dict = forward.__wrapped__ if forward == original_forward: break a_ : str = forward if getattr(snake_case__ , '''_converted_to_transformer_engine''' , snake_case__ ): convert_model(snake_case__ , to_transformer_engine=snake_case__ ) if is_compiled: a_ : Optional[int] = model a_ : str = compiled_model return model def _UpperCAmelCase ( ): PartialState().wait_for_everyone() def _UpperCAmelCase ( __A : Optional[int] , __A : Optional[int] ): if PartialState().distributed_type == DistributedType.TPU: xm.save(snake_case__ , snake_case__ ) elif PartialState().local_process_index == 0: torch.save(snake_case__ , snake_case__ ) @contextmanager def _UpperCAmelCase ( **__A : Tuple ): for key, value in kwargs.items(): a_ : Tuple = str(snake_case__ ) yield for key in kwargs: if key.upper() in os.environ: del os.environ[key.upper()] def _UpperCAmelCase ( __A : Any ): if not hasattr(snake_case__ , '''__qualname__''' ) and not hasattr(snake_case__ , '''__name__''' ): a_ : Optional[Any] = getattr(snake_case__ , '''__class__''' , snake_case__ ) if hasattr(snake_case__ , '''__qualname__''' ): return obj.__qualname__ if hasattr(snake_case__ , '''__name__''' ): return obj.__name__ return str(snake_case__ ) def _UpperCAmelCase ( __A : List[str] , __A : Optional[int] ): for key, value in source.items(): if isinstance(snake_case__ , snake_case__ ): a_ : Union[str, Any] = destination.setdefault(snake_case__ , {} ) merge_dicts(snake_case__ , snake_case__ ) else: a_ : int = value return destination def _UpperCAmelCase ( __A : int = None ): if port is None: a_ : List[str] = 2_95_00 with socket.socket(socket.AF_INET , socket.SOCK_STREAM ) as s: return s.connect_ex(('''localhost''', port) ) == 0
717
'''simple docstring''' import argparse import json from pathlib import Path import requests import torch from huggingface_hub import cached_download, hf_hub_download, hf_hub_url from PIL import Image from transformers import DetaConfig, DetaForObjectDetection, DetaImageProcessor, SwinConfig from transformers.utils import logging logging.set_verbosity_info() __lowerCAmelCase = logging.get_logger(__name__) def _UpperCAmelCase ( __A : Union[str, Any] ): a_ : Tuple = SwinConfig( embed_dim=1_92 , depths=(2, 2, 18, 2) , num_heads=(6, 12, 24, 48) , window_size=12 , out_features=['''stage2''', '''stage3''', '''stage4'''] , ) a_ : List[Any] = DetaConfig( backbone_config=__A , num_queries=9_00 , encoder_ffn_dim=20_48 , decoder_ffn_dim=20_48 , num_feature_levels=5 , assign_first_stage=__A , with_box_refine=__A , two_stage=__A , ) # set labels a_ : Optional[Any] = '''huggingface/label-files''' if "o365" in model_name: a_ : Optional[Any] = 3_66 a_ : Tuple = '''object365-id2label.json''' else: a_ : Any = 91 a_ : Union[str, Any] = '''coco-detection-id2label.json''' a_ : Tuple = num_labels a_ : str = json.load(open(cached_download(hf_hub_url(__A , __A , repo_type='''dataset''' ) ) , '''r''' ) ) a_ : Optional[int] = {int(__A ): v for k, v in idalabel.items()} a_ : int = idalabel a_ : Dict = {v: k for k, v in idalabel.items()} return config def _UpperCAmelCase ( __A : List[str] ): a_ : Tuple = [] # stem # fmt: off rename_keys.append(('''backbone.0.body.patch_embed.proj.weight''', '''model.backbone.model.embeddings.patch_embeddings.projection.weight''') ) rename_keys.append(('''backbone.0.body.patch_embed.proj.bias''', '''model.backbone.model.embeddings.patch_embeddings.projection.bias''') ) rename_keys.append(('''backbone.0.body.patch_embed.norm.weight''', '''model.backbone.model.embeddings.norm.weight''') ) rename_keys.append(('''backbone.0.body.patch_embed.norm.bias''', '''model.backbone.model.embeddings.norm.bias''') ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm1.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm1.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_bias_table', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_index', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.proj.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.proj.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm2.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm2.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.bias') ) if i < 3: rename_keys.append((f'backbone.0.body.layers.{i}.downsample.reduction.weight', f'model.backbone.model.encoder.layers.{i}.downsample.reduction.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.downsample.norm.weight', f'model.backbone.model.encoder.layers.{i}.downsample.norm.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.downsample.norm.bias', f'model.backbone.model.encoder.layers.{i}.downsample.norm.bias') ) rename_keys.append(('''backbone.0.body.norm1.weight''', '''model.backbone.model.hidden_states_norms.stage2.weight''') ) rename_keys.append(('''backbone.0.body.norm1.bias''', '''model.backbone.model.hidden_states_norms.stage2.bias''') ) rename_keys.append(('''backbone.0.body.norm2.weight''', '''model.backbone.model.hidden_states_norms.stage3.weight''') ) rename_keys.append(('''backbone.0.body.norm2.bias''', '''model.backbone.model.hidden_states_norms.stage3.bias''') ) rename_keys.append(('''backbone.0.body.norm3.weight''', '''model.backbone.model.hidden_states_norms.stage4.weight''') ) rename_keys.append(('''backbone.0.body.norm3.bias''', '''model.backbone.model.hidden_states_norms.stage4.bias''') ) # transformer encoder for i in range(config.encoder_layers ): rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.sampling_offsets.weight', f'model.encoder.layers.{i}.self_attn.sampling_offsets.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.sampling_offsets.bias', f'model.encoder.layers.{i}.self_attn.sampling_offsets.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.attention_weights.weight', f'model.encoder.layers.{i}.self_attn.attention_weights.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.attention_weights.bias', f'model.encoder.layers.{i}.self_attn.attention_weights.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.value_proj.weight', f'model.encoder.layers.{i}.self_attn.value_proj.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.value_proj.bias', f'model.encoder.layers.{i}.self_attn.value_proj.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.output_proj.weight', f'model.encoder.layers.{i}.self_attn.output_proj.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.output_proj.bias', f'model.encoder.layers.{i}.self_attn.output_proj.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm1.weight', f'model.encoder.layers.{i}.self_attn_layer_norm.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm1.bias', f'model.encoder.layers.{i}.self_attn_layer_norm.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear1.weight', f'model.encoder.layers.{i}.fc1.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear1.bias', f'model.encoder.layers.{i}.fc1.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear2.weight', f'model.encoder.layers.{i}.fc2.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear2.bias', f'model.encoder.layers.{i}.fc2.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm2.weight', f'model.encoder.layers.{i}.final_layer_norm.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm2.bias', f'model.encoder.layers.{i}.final_layer_norm.bias') ) # transformer decoder for i in range(config.decoder_layers ): rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.sampling_offsets.weight', f'model.decoder.layers.{i}.encoder_attn.sampling_offsets.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.sampling_offsets.bias', f'model.decoder.layers.{i}.encoder_attn.sampling_offsets.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.attention_weights.weight', f'model.decoder.layers.{i}.encoder_attn.attention_weights.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.attention_weights.bias', f'model.decoder.layers.{i}.encoder_attn.attention_weights.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.value_proj.weight', f'model.decoder.layers.{i}.encoder_attn.value_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.value_proj.bias', f'model.decoder.layers.{i}.encoder_attn.value_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.output_proj.weight', f'model.decoder.layers.{i}.encoder_attn.output_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.output_proj.bias', f'model.decoder.layers.{i}.encoder_attn.output_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm1.weight', f'model.decoder.layers.{i}.encoder_attn_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm1.bias', f'model.decoder.layers.{i}.encoder_attn_layer_norm.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.self_attn.out_proj.weight', f'model.decoder.layers.{i}.self_attn.out_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.self_attn.out_proj.bias', f'model.decoder.layers.{i}.self_attn.out_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm2.weight', f'model.decoder.layers.{i}.self_attn_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm2.bias', f'model.decoder.layers.{i}.self_attn_layer_norm.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear1.weight', f'model.decoder.layers.{i}.fc1.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear1.bias', f'model.decoder.layers.{i}.fc1.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear2.weight', f'model.decoder.layers.{i}.fc2.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear2.bias', f'model.decoder.layers.{i}.fc2.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm3.weight', f'model.decoder.layers.{i}.final_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm3.bias', f'model.decoder.layers.{i}.final_layer_norm.bias') ) # fmt: on return rename_keys def _UpperCAmelCase ( __A : str , __A : int , __A : Tuple ): a_ : str = dct.pop(__A ) a_ : Dict = val def _UpperCAmelCase ( __A : List[str] , __A : Optional[int] ): a_ : str = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): a_ : Tuple = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) a_ : List[str] = state_dict.pop(f'backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.weight' ) a_ : str = state_dict.pop(f'backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.bias' ) # next, add query, keys and values (in that order) to the state dict a_ : Optional[Any] = in_proj_weight[:dim, :] a_ : List[Any] = in_proj_bias[: dim] a_ : Optional[Any] = in_proj_weight[ dim : dim * 2, : ] a_ : Union[str, Any] = in_proj_bias[ dim : dim * 2 ] a_ : Optional[int] = in_proj_weight[ -dim :, : ] a_ : int = in_proj_bias[-dim :] # fmt: on def _UpperCAmelCase ( __A : Dict , __A : Dict ): # transformer decoder self-attention layers a_ : Any = config.d_model for i in range(config.decoder_layers ): # read in weights + bias of input projection layer of self-attention a_ : int = state_dict.pop(f'transformer.decoder.layers.{i}.self_attn.in_proj_weight' ) a_ : Any = state_dict.pop(f'transformer.decoder.layers.{i}.self_attn.in_proj_bias' ) # next, add query, keys and values (in that order) to the state dict a_ : Dict = in_proj_weight[:hidden_size, :] a_ : Tuple = in_proj_bias[:hidden_size] a_ : Any = in_proj_weight[ hidden_size : hidden_size * 2, : ] a_ : Tuple = in_proj_bias[hidden_size : hidden_size * 2] a_ : Optional[int] = in_proj_weight[-hidden_size:, :] a_ : int = in_proj_bias[-hidden_size:] def _UpperCAmelCase ( ): a_ : Union[str, Any] = '''http://images.cocodataset.org/val2017/000000039769.jpg''' a_ : List[str] = Image.open(requests.get(__A , stream=__A ).raw ) return im @torch.no_grad() def _UpperCAmelCase ( __A : int , __A : int , __A : Any ): a_ : Union[str, Any] = get_deta_config(__A ) # load original state dict if model_name == "deta-swin-large": a_ : Optional[Any] = hf_hub_download(repo_id='''nielsr/deta-checkpoints''' , filename='''adet_swin_ft.pth''' ) elif model_name == "deta-swin-large-o365": a_ : List[str] = hf_hub_download(repo_id='''jozhang97/deta-swin-l-o365''' , filename='''deta_swin_pt_o365.pth''' ) else: raise ValueError(f'Model name {model_name} not supported' ) a_ : List[Any] = torch.load(__A , map_location='''cpu''' )['''model'''] # original state dict for name, param in state_dict.items(): print(__A , param.shape ) # rename keys a_ : Union[str, Any] = create_rename_keys(__A ) for src, dest in rename_keys: rename_key(__A , __A , __A ) read_in_swin_q_k_v(__A , config.backbone_config ) read_in_decoder_q_k_v(__A , __A ) # fix some prefixes for key in state_dict.copy().keys(): if "transformer.decoder.class_embed" in key or "transformer.decoder.bbox_embed" in key: a_ : Optional[Any] = state_dict.pop(__A ) a_ : int = val if "input_proj" in key: a_ : str = state_dict.pop(__A ) a_ : Optional[Any] = val if "level_embed" in key or "pos_trans" in key or "pix_trans" in key or "enc_output" in key: a_ : List[str] = state_dict.pop(__A ) a_ : List[Any] = val # finally, create HuggingFace model and load state dict a_ : Dict = DetaForObjectDetection(__A ) model.load_state_dict(__A ) model.eval() a_ : int = '''cuda''' if torch.cuda.is_available() else '''cpu''' model.to(__A ) # load image processor a_ : List[Any] = DetaImageProcessor(format='''coco_detection''' ) # verify our conversion on image a_ : Dict = prepare_img() a_ : Optional[int] = processor(images=__A , return_tensors='''pt''' ) a_ : Any = encoding['''pixel_values'''] a_ : int = model(pixel_values.to(__A ) ) # verify logits print('''Logits:''' , outputs.logits[0, :3, :3] ) print('''Boxes:''' , outputs.pred_boxes[0, :3, :3] ) if model_name == "deta-swin-large": a_ : Optional[int] = torch.tensor( [[-7.6308, -2.8485, -5.3737], [-7.2037, -4.5505, -4.8027], [-7.2943, -4.2611, -4.6617]] ) a_ : Tuple = torch.tensor([[0.4987, 0.4969, 0.9999], [0.2549, 0.5498, 0.4805], [0.5498, 0.2757, 0.0569]] ) elif model_name == "deta-swin-large-o365": a_ : Union[str, Any] = torch.tensor( [[-8.0122, -3.5720, -4.9717], [-8.1547, -3.6886, -4.6389], [-7.6610, -3.6194, -5.0134]] ) a_ : Any = torch.tensor([[0.2523, 0.5549, 0.4881], [0.7715, 0.4149, 0.4601], [0.5503, 0.2753, 0.0575]] ) assert torch.allclose(outputs.logits[0, :3, :3] , expected_logits.to(__A ) , atol=1E-4 ) assert torch.allclose(outputs.pred_boxes[0, :3, :3] , expected_boxes.to(__A ) , atol=1E-4 ) print('''Everything ok!''' ) if pytorch_dump_folder_path: # Save model and processor logger.info(f'Saving PyTorch model and processor to {pytorch_dump_folder_path}...' ) Path(__A ).mkdir(exist_ok=__A ) model.save_pretrained(__A ) processor.save_pretrained(__A ) # Push to hub if push_to_hub: print('''Pushing model and processor to hub...''' ) model.push_to_hub(f'jozhang97/{model_name}' ) processor.push_to_hub(f'jozhang97/{model_name}' ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() parser.add_argument( '--model_name', type=str, default='deta-swin-large', choices=['deta-swin-large', 'deta-swin-large-o365'], help='Name of the model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the folder to output PyTorch model.', ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.' ) __lowerCAmelCase = parser.parse_args() convert_deta_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
666
0
'''simple docstring''' def _UpperCAmelCase ( __A : Optional[int] ): if not isinstance(__A , __A ): a_ : str = f'Input value of [number={number}] must be an integer' raise TypeError(__A ) if number < 1: a_ : Union[str, Any] = f'Input value of [number={number}] must be > 0' raise ValueError(__A ) a_ : Union[str, Any] = 1 for i in range(1 , __A ): current_number *= 4 * i - 2 current_number //= i + 1 return current_number if __name__ == "__main__": import doctest doctest.testmod()
718
'''simple docstring''' import unittest import numpy as np import torch from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = DDIMPipeline snake_case__ = UNCONDITIONAL_IMAGE_GENERATION_PARAMS snake_case__ = PipelineTesterMixin.required_optional_params - { "num_images_per_prompt", "latents", "callback", "callback_steps", } snake_case__ = UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS snake_case__ = False def SCREAMING_SNAKE_CASE ( self : Any ) -> Optional[Any]: torch.manual_seed(0 ) a_ : 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''') , ) a_ : str = DDIMScheduler() a_ : Union[str, Any] = {'''unet''': unet, '''scheduler''': scheduler} return components def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Tuple=0 ) -> str: if str(__SCREAMING_SNAKE_CASE ).startswith('''mps''' ): a_ : Dict = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: a_ : Union[str, Any] = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = { '''batch_size''': 1, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union[str, Any]: a_ : Dict = '''cpu''' a_ : List[Any] = self.get_dummy_components() a_ : List[str] = self.pipeline_class(**__SCREAMING_SNAKE_CASE ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = self.get_dummy_inputs(__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = pipe(**__SCREAMING_SNAKE_CASE ).images a_ : List[str] = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 32, 32, 3) ) a_ : int = np.array( [1.000e00, 5.717e-01, 4.717e-01, 1.000e00, 0.000e00, 1.000e00, 3.000e-04, 0.000e00, 9.000e-04] ) a_ : Union[str, Any] = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(__SCREAMING_SNAKE_CASE , 1e-3 ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> str: super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : str ) -> Optional[int]: super().test_save_load_local(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : Any ) -> List[str]: super().test_save_load_optional_components(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : str ) -> Optional[int]: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : str ) -> Any: a_ : Optional[Any] = '''google/ddpm-cifar10-32''' a_ : Optional[Any] = UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Dict = DDIMScheduler() a_ : List[str] = DDIMPipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) ddim.to(__SCREAMING_SNAKE_CASE ) ddim.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = torch.manual_seed(0 ) a_ : Tuple = ddim(generator=__SCREAMING_SNAKE_CASE , eta=0.0 , output_type='''numpy''' ).images a_ : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) a_ : List[str] = np.array([0.1723, 0.1617, 0.1600, 0.1626, 0.1497, 0.1513, 0.1505, 0.1442, 0.1453] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> str: a_ : int = '''google/ddpm-ema-bedroom-256''' a_ : str = UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Tuple = DDIMScheduler.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = DDIMPipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) ddpm.to(__SCREAMING_SNAKE_CASE ) ddpm.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = torch.manual_seed(0 ) a_ : List[Any] = ddpm(generator=__SCREAMING_SNAKE_CASE , output_type='''numpy''' ).images a_ : Optional[int] = image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) a_ : Optional[Any] = np.array([0.0060, 0.0201, 0.0344, 0.0024, 0.0018, 0.0002, 0.0022, 0.0000, 0.0069] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
666
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_torch_available, ) __lowerCAmelCase = { """configuration_trocr""": ["""TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP""", """TrOCRConfig"""], """processing_trocr""": ["""TrOCRProcessor"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ """TROCR_PRETRAINED_MODEL_ARCHIVE_LIST""", """TrOCRForCausalLM""", """TrOCRPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_trocr import TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP, TrOCRConfig from .processing_trocr import TrOCRProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_trocr import TROCR_PRETRAINED_MODEL_ARCHIVE_LIST, TrOCRForCausalLM, TrOCRPreTrainedModel else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
719
'''simple docstring''' from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import pyarrow as pa if TYPE_CHECKING: from .features import FeatureType @dataclass class SCREAMING_SNAKE_CASE : snake_case__ = 42 snake_case__ = None # Automatically constructed snake_case__ = "dict" snake_case__ = None snake_case__ = field(default="Translation" , init=SCREAMING_SNAKE_CASE_ , repr=SCREAMING_SNAKE_CASE_ ) def __call__( self : Dict ) -> Tuple: return pa.struct({lang: pa.string() for lang in sorted(self.languages )} ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Value return {k: Value('''string''' ) for k in sorted(self.languages )} @dataclass class SCREAMING_SNAKE_CASE : snake_case__ = None snake_case__ = None snake_case__ = None # Automatically constructed snake_case__ = "dict" snake_case__ = None snake_case__ = field(default="TranslationVariableLanguages" , init=SCREAMING_SNAKE_CASE_ , repr=SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : List[str] = sorted(set(self.languages ) ) if self.languages else None a_ : Optional[Any] = len(self.languages ) if self.languages else None def __call__( self : Any ) -> Optional[Any]: return pa.struct({'''language''': pa.list_(pa.string() ), '''translation''': pa.list_(pa.string() )} ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Optional[Any]: a_ : str = set(self.languages ) if self.languages and set(__SCREAMING_SNAKE_CASE ) - lang_set: raise ValueError( f'Some languages in example ({", ".join(sorted(set(__SCREAMING_SNAKE_CASE ) - lang_set ) )}) are not in valid set ({", ".join(__SCREAMING_SNAKE_CASE )}).' ) # Convert dictionary into tuples, splitting out cases where there are # multiple translations for a single language. a_ : int = [] for lang, text in translation_dict.items(): if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): translation_tuples.append((lang, text) ) else: translation_tuples.extend([(lang, el) for el in text] ) # Ensure translations are in ascending order by language code. a_ , a_ : List[Any] = zip(*sorted(__SCREAMING_SNAKE_CASE ) ) return {"language": languages, "translation": translations} def SCREAMING_SNAKE_CASE ( self : Any ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Sequence, Value return { "language": Sequence(Value('''string''' ) ), "translation": Sequence(Value('''string''' ) ), }
666
0
'''simple docstring''' from dataclasses import dataclass from typing import Optional import numpy as np import torch import torch.nn as nn from ..utils import BaseOutput, is_torch_version, randn_tensor from .attention_processor import SpatialNorm from .unet_ad_blocks import UNetMidBlockaD, get_down_block, get_up_block @dataclass class SCREAMING_SNAKE_CASE ( __lowerCamelCase ): snake_case__ = 42 class SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : int , __SCREAMING_SNAKE_CASE : str=3 , __SCREAMING_SNAKE_CASE : str=3 , __SCREAMING_SNAKE_CASE : str=("DownEncoderBlock2D",) , __SCREAMING_SNAKE_CASE : Any=(64,) , __SCREAMING_SNAKE_CASE : Union[str, Any]=2 , __SCREAMING_SNAKE_CASE : Union[str, Any]=32 , __SCREAMING_SNAKE_CASE : Union[str, Any]="silu" , __SCREAMING_SNAKE_CASE : int=True , ) -> List[Any]: super().__init__() a_ : Tuple = layers_per_block a_ : Any = torch.nn.Convad( SCREAMING_SNAKE_CASE_ , block_out_channels[0] , kernel_size=3 , stride=1 , padding=1 , ) a_ : Any = None a_ : Union[str, Any] = nn.ModuleList([] ) # down a_ : Tuple = block_out_channels[0] for i, down_block_type in enumerate(SCREAMING_SNAKE_CASE_ ): a_ : List[Any] = output_channel a_ : Dict = block_out_channels[i] a_ : int = i == len(SCREAMING_SNAKE_CASE_ ) - 1 a_ : Any = get_down_block( SCREAMING_SNAKE_CASE_ , num_layers=self.layers_per_block , in_channels=SCREAMING_SNAKE_CASE_ , out_channels=SCREAMING_SNAKE_CASE_ , add_downsample=not is_final_block , resnet_eps=1e-6 , downsample_padding=0 , resnet_act_fn=SCREAMING_SNAKE_CASE_ , resnet_groups=SCREAMING_SNAKE_CASE_ , attention_head_dim=SCREAMING_SNAKE_CASE_ , temb_channels=SCREAMING_SNAKE_CASE_ , ) self.down_blocks.append(SCREAMING_SNAKE_CASE_ ) # mid a_ : str = UNetMidBlockaD( in_channels=block_out_channels[-1] , resnet_eps=1e-6 , resnet_act_fn=SCREAMING_SNAKE_CASE_ , output_scale_factor=1 , resnet_time_scale_shift='''default''' , attention_head_dim=block_out_channels[-1] , resnet_groups=SCREAMING_SNAKE_CASE_ , temb_channels=SCREAMING_SNAKE_CASE_ , ) # out a_ : Tuple = nn.GroupNorm(num_channels=block_out_channels[-1] , num_groups=SCREAMING_SNAKE_CASE_ , eps=1e-6 ) a_ : Any = nn.SiLU() a_ : Dict = 2 * out_channels if double_z else out_channels a_ : Any = nn.Convad(block_out_channels[-1] , SCREAMING_SNAKE_CASE_ , 3 , padding=1 ) a_ : int = False def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : Tuple ) -> Any: a_ : Union[str, Any] = x a_ : Optional[Any] = self.conv_in(SCREAMING_SNAKE_CASE_ ) if self.training and self.gradient_checkpointing: def create_custom_forward(__SCREAMING_SNAKE_CASE : Union[str, Any] ): def custom_forward(*__SCREAMING_SNAKE_CASE : Optional[int] ): return module(*SCREAMING_SNAKE_CASE_ ) return custom_forward # down if is_torch_version('''>=''' , '''1.11.0''' ): for down_block in self.down_blocks: a_ : List[Any] = torch.utils.checkpoint.checkpoint( create_custom_forward(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ , use_reentrant=SCREAMING_SNAKE_CASE_ ) # middle a_ : str = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , SCREAMING_SNAKE_CASE_ , use_reentrant=SCREAMING_SNAKE_CASE_ ) else: for down_block in self.down_blocks: a_ : Dict = torch.utils.checkpoint.checkpoint(create_custom_forward(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) # middle a_ : Any = torch.utils.checkpoint.checkpoint(create_custom_forward(self.mid_block ) , SCREAMING_SNAKE_CASE_ ) else: # down for down_block in self.down_blocks: a_ : List[str] = down_block(SCREAMING_SNAKE_CASE_ ) # middle a_ : Union[str, Any] = self.mid_block(SCREAMING_SNAKE_CASE_ ) # post-process a_ : Any = self.conv_norm_out(SCREAMING_SNAKE_CASE_ ) a_ : Union[str, Any] = self.conv_act(SCREAMING_SNAKE_CASE_ ) a_ : Any = self.conv_out(SCREAMING_SNAKE_CASE_ ) return sample class SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : Dict , __SCREAMING_SNAKE_CASE : Dict=3 , __SCREAMING_SNAKE_CASE : Optional[Any]=3 , __SCREAMING_SNAKE_CASE : str=("UpDecoderBlock2D",) , __SCREAMING_SNAKE_CASE : Optional[int]=(64,) , __SCREAMING_SNAKE_CASE : Union[str, Any]=2 , __SCREAMING_SNAKE_CASE : Union[str, Any]=32 , __SCREAMING_SNAKE_CASE : Tuple="silu" , __SCREAMING_SNAKE_CASE : Optional[int]="group" , ) -> str: super().__init__() a_ : List[Any] = layers_per_block a_ : Union[str, Any] = nn.Convad( SCREAMING_SNAKE_CASE_ , block_out_channels[-1] , kernel_size=3 , stride=1 , padding=1 , ) a_ : str = None a_ : Dict = nn.ModuleList([] ) a_ : Union[str, Any] = in_channels if norm_type == '''spatial''' else None # mid a_ : Optional[int] = UNetMidBlockaD( in_channels=block_out_channels[-1] , resnet_eps=1e-6 , resnet_act_fn=SCREAMING_SNAKE_CASE_ , output_scale_factor=1 , resnet_time_scale_shift='''default''' if norm_type == '''group''' else norm_type , attention_head_dim=block_out_channels[-1] , resnet_groups=SCREAMING_SNAKE_CASE_ , temb_channels=SCREAMING_SNAKE_CASE_ , ) # up a_ : Tuple = list(reversed(SCREAMING_SNAKE_CASE_ ) ) a_ : List[str] = reversed_block_out_channels[0] for i, up_block_type in enumerate(SCREAMING_SNAKE_CASE_ ): a_ : List[Any] = output_channel a_ : Dict = reversed_block_out_channels[i] a_ : List[str] = i == len(SCREAMING_SNAKE_CASE_ ) - 1 a_ : List[str] = get_up_block( SCREAMING_SNAKE_CASE_ , num_layers=self.layers_per_block + 1 , in_channels=SCREAMING_SNAKE_CASE_ , out_channels=SCREAMING_SNAKE_CASE_ , prev_output_channel=SCREAMING_SNAKE_CASE_ , add_upsample=not is_final_block , resnet_eps=1e-6 , resnet_act_fn=SCREAMING_SNAKE_CASE_ , resnet_groups=SCREAMING_SNAKE_CASE_ , attention_head_dim=SCREAMING_SNAKE_CASE_ , temb_channels=SCREAMING_SNAKE_CASE_ , resnet_time_scale_shift=SCREAMING_SNAKE_CASE_ , ) self.up_blocks.append(SCREAMING_SNAKE_CASE_ ) a_ : str = output_channel # out if norm_type == "spatial": a_ : Dict = SpatialNorm(block_out_channels[0] , SCREAMING_SNAKE_CASE_ ) else: a_ : Tuple = nn.GroupNorm(num_channels=block_out_channels[0] , num_groups=SCREAMING_SNAKE_CASE_ , eps=1e-6 ) a_ : Tuple = nn.SiLU() a_ : Any = nn.Convad(block_out_channels[0] , SCREAMING_SNAKE_CASE_ , 3 , padding=1 ) a_ : int = False def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Optional[Any]=None ) -> str: a_ : str = z a_ : List[Any] = self.conv_in(SCREAMING_SNAKE_CASE_ ) a_ : Any = next(iter(self.up_blocks.parameters() ) ).dtype if self.training and self.gradient_checkpointing: def create_custom_forward(__SCREAMING_SNAKE_CASE : Tuple ): def custom_forward(*__SCREAMING_SNAKE_CASE : List[Any] ): return module(*SCREAMING_SNAKE_CASE_ ) return custom_forward if is_torch_version('''>=''' , '''1.11.0''' ): # middle a_ : Optional[int] = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , use_reentrant=SCREAMING_SNAKE_CASE_ ) a_ : Union[str, Any] = sample.to(SCREAMING_SNAKE_CASE_ ) # up for up_block in self.up_blocks: a_ : List[Any] = torch.utils.checkpoint.checkpoint( create_custom_forward(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , use_reentrant=SCREAMING_SNAKE_CASE_ ) else: # middle a_ : List[str] = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) a_ : int = sample.to(SCREAMING_SNAKE_CASE_ ) # up for up_block in self.up_blocks: a_ : List[str] = torch.utils.checkpoint.checkpoint(create_custom_forward(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else: # middle a_ : Tuple = self.mid_block(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) a_ : Optional[int] = sample.to(SCREAMING_SNAKE_CASE_ ) # up for up_block in self.up_blocks: a_ : Optional[int] = up_block(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # post-process if latent_embeds is None: a_ : str = self.conv_norm_out(SCREAMING_SNAKE_CASE_ ) else: a_ : Tuple = self.conv_norm_out(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) a_ : Optional[int] = self.conv_act(SCREAMING_SNAKE_CASE_ ) a_ : Optional[int] = self.conv_out(SCREAMING_SNAKE_CASE_ ) return sample class SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : Optional[int] , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Optional[Any]=None , __SCREAMING_SNAKE_CASE : int="random" , __SCREAMING_SNAKE_CASE : List[str]=False , __SCREAMING_SNAKE_CASE : Dict=True ) -> Tuple: super().__init__() a_ : int = n_e a_ : Dict = vq_embed_dim a_ : Any = beta a_ : List[Any] = legacy a_ : Dict = nn.Embedding(self.n_e , self.vq_embed_dim ) self.embedding.weight.data.uniform_(-1.0 / self.n_e , 1.0 / self.n_e ) a_ : List[Any] = remap if self.remap is not None: self.register_buffer('''used''' , torch.tensor(np.load(self.remap ) ) ) a_ : Dict = self.used.shape[0] a_ : List[str] = unknown_index # "random" or "extra" or integer if self.unknown_index == "extra": a_ : List[Any] = self.re_embed a_ : Optional[int] = self.re_embed + 1 print( f'Remapping {self.n_e} indices to {self.re_embed} indices. ' f'Using {self.unknown_index} for unknown indices.' ) else: a_ : Any = n_e a_ : List[Any] = sane_index_shape def SCREAMING_SNAKE_CASE ( self : List[Any] , __SCREAMING_SNAKE_CASE : List[Any] ) -> Any: a_ : List[Any] = inds.shape assert len(SCREAMING_SNAKE_CASE_ ) > 1 a_ : Dict = inds.reshape(ishape[0] , -1 ) a_ : List[Any] = self.used.to(SCREAMING_SNAKE_CASE_ ) a_ : int = (inds[:, :, None] == used[None, None, ...]).long() a_ : Union[str, Any] = match.argmax(-1 ) a_ : List[str] = match.sum(2 ) < 1 if self.unknown_index == "random": a_ : Optional[int] = torch.randint(0 , self.re_embed , size=new[unknown].shape ).to(device=new.device ) else: a_ : Dict = self.unknown_index return new.reshape(SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Dict ) -> str: a_ : List[Any] = inds.shape assert len(SCREAMING_SNAKE_CASE_ ) > 1 a_ : Optional[Any] = inds.reshape(ishape[0] , -1 ) a_ : Any = self.used.to(SCREAMING_SNAKE_CASE_ ) if self.re_embed > self.used.shape[0]: # extra token a_ : List[Any] = 0 # simply set to zero a_ : List[Any] = torch.gather(used[None, :][inds.shape[0] * [0], :] , 1 , SCREAMING_SNAKE_CASE_ ) return back.reshape(SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> List[str]: # reshape z -> (batch, height, width, channel) and flatten a_ : List[str] = z.permute(0 , 2 , 3 , 1 ).contiguous() a_ : Tuple = z.view(-1 , self.vq_embed_dim ) # distances from z to embeddings e_j (z - e)^2 = z^2 + e^2 - 2 e * z a_ : Union[str, Any] = torch.argmin(torch.cdist(SCREAMING_SNAKE_CASE_ , self.embedding.weight ) , dim=1 ) a_ : Union[str, Any] = self.embedding(SCREAMING_SNAKE_CASE_ ).view(z.shape ) a_ : List[Any] = None a_ : List[Any] = None # compute loss for embedding if not self.legacy: a_ : List[str] = self.beta * torch.mean((z_q.detach() - z) ** 2 ) + torch.mean((z_q - z.detach()) ** 2 ) else: a_ : Optional[int] = torch.mean((z_q.detach() - z) ** 2 ) + self.beta * torch.mean((z_q - z.detach()) ** 2 ) # preserve gradients a_ : str = z + (z_q - z).detach() # reshape back to match original input shape a_ : Optional[Any] = z_q.permute(0 , 3 , 1 , 2 ).contiguous() if self.remap is not None: a_ : Union[str, Any] = min_encoding_indices.reshape(z.shape[0] , -1 ) # add batch axis a_ : List[str] = self.remap_to_used(SCREAMING_SNAKE_CASE_ ) a_ : Dict = min_encoding_indices.reshape(-1 , 1 ) # flatten if self.sane_index_shape: a_ : Optional[int] = min_encoding_indices.reshape(z_q.shape[0] , z_q.shape[2] , z_q.shape[3] ) return z_q, loss, (perplexity, min_encodings, min_encoding_indices) def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : str ) -> Optional[Any]: # shape specifying (batch, height, width, channel) if self.remap is not None: a_ : str = indices.reshape(shape[0] , -1 ) # add batch axis a_ : Tuple = self.unmap_to_all(SCREAMING_SNAKE_CASE_ ) a_ : Tuple = indices.reshape(-1 ) # flatten again # get quantized latent vectors a_ : str = self.embedding(SCREAMING_SNAKE_CASE_ ) if shape is not None: a_ : Optional[int] = z_q.view(SCREAMING_SNAKE_CASE_ ) # reshape back to match original input shape a_ : Optional[Any] = z_q.permute(0 , 3 , 1 , 2 ).contiguous() return z_q class SCREAMING_SNAKE_CASE ( __lowerCamelCase ): def __init__( self : List[str] , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : Union[str, Any]=False ) -> Any: a_ : Dict = parameters a_ , a_ : int = torch.chunk(SCREAMING_SNAKE_CASE_ , 2 , dim=1 ) a_ : str = torch.clamp(self.logvar , -30.0 , 20.0 ) a_ : Dict = deterministic a_ : Optional[Any] = torch.exp(0.5 * self.logvar ) a_ : Any = torch.exp(self.logvar ) if self.deterministic: a_ : List[str] = torch.zeros_like( self.mean , device=self.parameters.device , dtype=self.parameters.dtype ) def SCREAMING_SNAKE_CASE ( self : List[Any] , __SCREAMING_SNAKE_CASE : Any = None ) -> Tuple: # make sure sample is on the same device as the parameters and has same dtype a_ : Any = randn_tensor( self.mean.shape , generator=SCREAMING_SNAKE_CASE_ , device=self.parameters.device , dtype=self.parameters.dtype ) a_ : Any = self.mean + self.std * sample return x def SCREAMING_SNAKE_CASE ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : str=None ) -> Optional[Any]: if self.deterministic: return torch.Tensor([0.0] ) else: if other is None: return 0.5 * torch.sum(torch.pow(self.mean , 2 ) + self.var - 1.0 - self.logvar , dim=[1, 2, 3] ) else: return 0.5 * torch.sum( torch.pow(self.mean - other.mean , 2 ) / other.var + self.var / other.var - 1.0 - self.logvar + other.logvar , dim=[1, 2, 3] , ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Dict=[1, 2, 3] ) -> int: if self.deterministic: return torch.Tensor([0.0] ) a_ : Optional[int] = np.log(2.0 * np.pi ) return 0.5 * torch.sum(logtwopi + self.logvar + torch.pow(sample - self.mean , 2 ) / self.var , dim=SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE ( self : Any ) -> Any: return self.mean
720
'''simple docstring''' import json import os import pickle import shutil import tempfile from unittest import TestCase from unittest.mock import patch import numpy as np from datasets import Dataset from transformers import is_faiss_available from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bart.tokenization_bart import BartTokenizer from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.dpr.tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.retrieval_rag import CustomHFIndex, RagRetriever from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_sentencepiece, require_tokenizers, require_torch if is_faiss_available(): import faiss @require_faiss class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): def SCREAMING_SNAKE_CASE ( self : Dict ) -> Dict: a_ : Union[str, Any] = tempfile.mkdtemp() a_ : Union[str, Any] = 8 # DPR tok a_ : Dict = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] a_ : str = os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = os.path.join(__SCREAMING_SNAKE_CASE , DPR_VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) # BART tok a_ : Union[str, Any] = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] a_ : int = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) a_ : int = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] a_ : Optional[int] = {'''unk_token''': '''<unk>'''} a_ : List[str] = os.path.join(self.tmpdirname , '''bart_tokenizer''' ) os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) a_ : Tuple = os.path.join(__SCREAMING_SNAKE_CASE , BART_VOCAB_FILES_NAMES['''vocab_file'''] ) a_ : int = os.path.join(__SCREAMING_SNAKE_CASE , BART_VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> DPRQuestionEncoderTokenizer: return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : str ) -> DPRContextEncoderTokenizer: return DPRContextEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> BartTokenizer: return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''bart_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Union[str, Any]: shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> Union[str, Any]: a_ : str = Dataset.from_dict( { '''id''': ['''0''', '''1'''], '''text''': ['''foo''', '''bar'''], '''title''': ['''Foo''', '''Bar'''], '''embeddings''': [np.ones(self.retrieval_vector_size ), 2 * np.ones(self.retrieval_vector_size )], } ) dataset.add_faiss_index('''embeddings''' , string_factory='''Flat''' , metric_type=faiss.METRIC_INNER_PRODUCT ) return dataset def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Tuple: a_ : List[str] = self.get_dummy_dataset() a_ : Tuple = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , ) with patch('''transformers.models.rag.retrieval_rag.load_dataset''' ) as mock_load_dataset: a_ : Tuple = dataset a_ : Any = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) return retriever def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : bool ) -> Dict: a_ : Dict = self.get_dummy_dataset() a_ : Dict = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''custom''' , ) if from_disk: a_ : Optional[int] = os.path.join(self.tmpdirname , '''dataset''' ) a_ : str = os.path.join(self.tmpdirname , '''index.faiss''' ) dataset.get_index('''embeddings''' ).save(os.path.join(self.tmpdirname , '''index.faiss''' ) ) dataset.drop_index('''embeddings''' ) dataset.save_to_disk(os.path.join(self.tmpdirname , '''dataset''' ) ) del dataset a_ : int = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) else: a_ : Optional[Any] = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , index=CustomHFIndex(config.retrieval_vector_size , __SCREAMING_SNAKE_CASE ) , ) return retriever def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Optional[int]: a_ : str = Dataset.from_dict( { '''id''': ['''0''', '''1'''], '''text''': ['''foo''', '''bar'''], '''title''': ['''Foo''', '''Bar'''], '''embeddings''': [np.ones(self.retrieval_vector_size + 1 ), 2 * np.ones(self.retrieval_vector_size + 1 )], } ) dataset.add_faiss_index('''embeddings''' , string_factory='''Flat''' , metric_type=faiss.METRIC_INNER_PRODUCT ) a_ : Optional[int] = os.path.join(self.tmpdirname , '''hf_bert_base.hnswSQ8_correct_phi_128.c_index''' ) dataset.save_faiss_index('''embeddings''' , index_file_name + '''.index.dpr''' ) pickle.dump(dataset['''id'''] , open(index_file_name + '''.index_meta.dpr''' , '''wb''' ) ) a_ : Union[str, Any] = os.path.join(self.tmpdirname , '''psgs_w100.tsv.pkl''' ) a_ : Dict = {sample['''id''']: [sample['''text'''], sample['''title''']] for sample in dataset} pickle.dump(__SCREAMING_SNAKE_CASE , open(__SCREAMING_SNAKE_CASE , '''wb''' ) ) a_ : Optional[Any] = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''legacy''' , index_path=self.tmpdirname , ) a_ : int = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() ) return retriever def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : Optional[Any] = 1 a_ : Dict = self.get_dummy_canonical_hf_index_retriever() a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : str = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : str = self.get_dummy_canonical_hf_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: with patch('''transformers.models.rag.retrieval_rag.load_dataset''' ) as mock_load_dataset: a_ : List[str] = self.get_dummy_dataset() retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : List[str] = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> int: a_ : Union[str, Any] = 1 a_ : Optional[Any] = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) a_ : List[Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : Any = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : str ) -> List[str]: a_ : Dict = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : List[str] = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Dict = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Optional[int]: a_ : Union[str, Any] = 1 a_ : str = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : Tuple = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : int ) -> Dict: a_ : List[str] = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Dict = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Any: a_ : str = 1 a_ : Tuple = self.get_dummy_legacy_index_retriever() a_ : Union[str, Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : Any = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''text'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''text'''][0] , '''bar''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''text'''][0] , '''foo''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[int]: a_ : List[str] = self.get_dummy_legacy_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : List[str] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Optional[Any] = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) @require_torch @require_tokenizers @require_sentencepiece def SCREAMING_SNAKE_CASE ( self : int ) -> List[str]: import torch a_ : Any = 1 a_ : List[Any] = self.get_dummy_canonical_hf_index_retriever() a_ : Union[str, Any] = [[5, 7], [10, 11]] a_ : Optional[int] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : str = retriever(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE ) a_ , a_ , a_ : List[str] = ( out['''context_input_ids'''], out['''context_attention_mask'''], out['''retrieved_doc_embeds'''], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , np.ndarray ) a_ : Any = retriever( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE , return_tensors='''pt''' , ) a_ , a_ , a_ , a_ : str = ( # noqa: F841 out['''context_input_ids'''], out['''context_attention_mask'''], out['''retrieved_doc_embeds'''], out['''doc_ids'''], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) @require_torch @require_tokenizers @require_sentencepiece def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Any: a_ : str = self.get_dpr_ctx_encoder_tokenizer() a_ : Tuple = 1 a_ : Any = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) retriever.set_ctx_encoder_tokenizer(__SCREAMING_SNAKE_CASE ) a_ : Dict = [[5, 7], [10, 11]] a_ : List[str] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : List[Any] = retriever(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual( len(__SCREAMING_SNAKE_CASE ) , 6 ) # check whether the retriever output consist of 6 attributes including tokenized docs self.assertEqual( all(k in out for k in ('''tokenized_doc_ids''', '''tokenized_doc_attention_mask''') ) , __SCREAMING_SNAKE_CASE ) # check for doc token related keys in dictionary.
666
0
'''simple docstring''' import copy import os from typing import TYPE_CHECKING, List, Union if TYPE_CHECKING: pass from ...configuration_utils import PretrainedConfig from ...utils import logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = { """kakaobrain/align-base""": """https://huggingface.co/kakaobrain/align-base/resolve/main/config.json""", } class SCREAMING_SNAKE_CASE ( _snake_case ): snake_case__ = """align_text_model""" def __init__( self : Any , __SCREAMING_SNAKE_CASE : int=3_0522 , __SCREAMING_SNAKE_CASE : List[str]=768 , __SCREAMING_SNAKE_CASE : Any=12 , __SCREAMING_SNAKE_CASE : Optional[Any]=12 , __SCREAMING_SNAKE_CASE : Tuple=3072 , __SCREAMING_SNAKE_CASE : int="gelu" , __SCREAMING_SNAKE_CASE : List[str]=0.1 , __SCREAMING_SNAKE_CASE : Dict=0.1 , __SCREAMING_SNAKE_CASE : Dict=512 , __SCREAMING_SNAKE_CASE : Union[str, Any]=2 , __SCREAMING_SNAKE_CASE : int=0.02 , __SCREAMING_SNAKE_CASE : Tuple=1e-12 , __SCREAMING_SNAKE_CASE : Union[str, Any]=0 , __SCREAMING_SNAKE_CASE : Optional[int]="absolute" , __SCREAMING_SNAKE_CASE : Any=True , **__SCREAMING_SNAKE_CASE : Optional[Any] , ) -> str: super().__init__(**snake_case_ ) a_ : Optional[int] = vocab_size a_ : List[str] = hidden_size a_ : Optional[int] = num_hidden_layers a_ : Dict = num_attention_heads a_ : List[Any] = hidden_act a_ : Optional[int] = intermediate_size a_ : Tuple = hidden_dropout_prob a_ : Tuple = attention_probs_dropout_prob a_ : Dict = max_position_embeddings a_ : Optional[Any] = type_vocab_size a_ : Union[str, Any] = initializer_range a_ : Union[str, Any] = layer_norm_eps a_ : List[str] = position_embedding_type a_ : int = use_cache a_ : Union[str, Any] = pad_token_id @classmethod def SCREAMING_SNAKE_CASE ( cls : int , __SCREAMING_SNAKE_CASE : Optional[Any] , **__SCREAMING_SNAKE_CASE : Tuple ) -> List[str]: cls._set_token_in_kwargs(snake_case_ ) a_ : Union[str, Any] = cls.get_config_dict(snake_case_ , **snake_case_ ) # get the text config dict if we are loading from AlignConfig if config_dict.get('''model_type''' ) == "align": a_ : Dict = config_dict["text_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(snake_case_ , **snake_case_ ) class SCREAMING_SNAKE_CASE ( _snake_case ): snake_case__ = """align_vision_model""" def __init__( self : List[Any] , __SCREAMING_SNAKE_CASE : List[str] = 3 , __SCREAMING_SNAKE_CASE : str = 600 , __SCREAMING_SNAKE_CASE : Dict = 2.0 , __SCREAMING_SNAKE_CASE : Dict = 3.1 , __SCREAMING_SNAKE_CASE : List[str] = 8 , __SCREAMING_SNAKE_CASE : Union[str, Any] = [3, 3, 5, 3, 5, 5, 3] , __SCREAMING_SNAKE_CASE : int = [32, 16, 24, 40, 80, 112, 192] , __SCREAMING_SNAKE_CASE : int = [16, 24, 40, 80, 112, 192, 320] , __SCREAMING_SNAKE_CASE : Optional[int] = [] , __SCREAMING_SNAKE_CASE : Tuple = [1, 2, 2, 2, 1, 2, 1] , __SCREAMING_SNAKE_CASE : Optional[Any] = [1, 2, 2, 3, 3, 4, 1] , __SCREAMING_SNAKE_CASE : Tuple = [1, 6, 6, 6, 6, 6, 6] , __SCREAMING_SNAKE_CASE : Optional[int] = 0.25 , __SCREAMING_SNAKE_CASE : List[str] = "swish" , __SCREAMING_SNAKE_CASE : List[str] = 2560 , __SCREAMING_SNAKE_CASE : List[Any] = "mean" , __SCREAMING_SNAKE_CASE : Optional[Any] = 0.02 , __SCREAMING_SNAKE_CASE : List[Any] = 0.001 , __SCREAMING_SNAKE_CASE : Optional[Any] = 0.99 , __SCREAMING_SNAKE_CASE : Optional[Any] = 0.2 , **__SCREAMING_SNAKE_CASE : Tuple , ) -> Tuple: super().__init__(**snake_case_ ) a_ : Optional[int] = num_channels a_ : Any = image_size a_ : List[Any] = width_coefficient a_ : Tuple = depth_coefficient a_ : int = depth_divisor a_ : Any = kernel_sizes a_ : Dict = in_channels a_ : Union[str, Any] = out_channels a_ : Optional[int] = depthwise_padding a_ : Optional[Any] = strides a_ : Union[str, Any] = num_block_repeats a_ : Tuple = expand_ratios a_ : Tuple = squeeze_expansion_ratio a_ : Union[str, Any] = hidden_act a_ : int = hidden_dim a_ : Optional[Any] = pooling_type a_ : Union[str, Any] = initializer_range a_ : List[str] = batch_norm_eps a_ : Optional[int] = batch_norm_momentum a_ : str = drop_connect_rate a_ : str = sum(snake_case_ ) * 4 @classmethod def SCREAMING_SNAKE_CASE ( cls : List[str] , __SCREAMING_SNAKE_CASE : List[str] , **__SCREAMING_SNAKE_CASE : Optional[int] ) -> str: cls._set_token_in_kwargs(snake_case_ ) a_ : List[Any] = cls.get_config_dict(snake_case_ , **snake_case_ ) # get the vision config dict if we are loading from AlignConfig if config_dict.get('''model_type''' ) == "align": a_ : List[Any] = 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(snake_case_ , **snake_case_ ) class SCREAMING_SNAKE_CASE ( _snake_case ): snake_case__ = """align""" snake_case__ = True def __init__( self : List[str] , __SCREAMING_SNAKE_CASE : Union[str, Any]=None , __SCREAMING_SNAKE_CASE : Union[str, Any]=None , __SCREAMING_SNAKE_CASE : Dict=640 , __SCREAMING_SNAKE_CASE : Optional[Any]=1.0 , __SCREAMING_SNAKE_CASE : int=0.02 , **__SCREAMING_SNAKE_CASE : Tuple , ) -> Optional[Any]: super().__init__(**snake_case_ ) if text_config is None: a_ : Union[str, Any] = {} logger.info('''text_config is None. Initializing the AlignTextConfig with default values.''' ) if vision_config is None: a_ : int = {} logger.info('''vision_config is None. Initializing the AlignVisionConfig with default values.''' ) a_ : str = AlignTextConfig(**snake_case_ ) a_ : Dict = AlignVisionConfig(**snake_case_ ) a_ : int = projection_dim a_ : int = temperature_init_value a_ : Dict = initializer_range @classmethod def SCREAMING_SNAKE_CASE ( cls : Optional[Any] , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : List[str] , **__SCREAMING_SNAKE_CASE : Optional[Any] ) -> str: return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **snake_case_ ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Tuple: a_ : Optional[Any] = copy.deepcopy(self.__dict__ ) a_ : List[str] = self.text_config.to_dict() a_ : Dict = self.vision_config.to_dict() a_ : Dict = self.__class__.model_type return output
721
'''simple docstring''' from math import pi, sqrt, tan def _UpperCAmelCase ( __A : float ): if side_length < 0: raise ValueError('''surface_area_cube() only accepts non-negative values''' ) return 6 * side_length**2 def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if length < 0 or breadth < 0 or height < 0: raise ValueError('''surface_area_cuboid() only accepts non-negative values''' ) return 2 * ((length * breadth) + (breadth * height) + (length * height)) def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''surface_area_sphere() only accepts non-negative values''' ) return 4 * pi * radius**2 def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''surface_area_hemisphere() only accepts non-negative values''' ) return 3 * pi * radius**2 def _UpperCAmelCase ( __A : float , __A : float ): if radius < 0 or height < 0: raise ValueError('''surface_area_cone() only accepts non-negative values''' ) return pi * radius * (radius + (height**2 + radius**2) ** 0.5) def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if radius_a < 0 or radius_a < 0 or height < 0: raise ValueError( '''surface_area_conical_frustum() only accepts non-negative values''' ) a_ : Any = (height**2 + (radius_a - radius_a) ** 2) ** 0.5 return pi * ((slant_height * (radius_a + radius_a)) + radius_a**2 + radius_a**2) def _UpperCAmelCase ( __A : float , __A : float ): if radius < 0 or height < 0: raise ValueError('''surface_area_cylinder() only accepts non-negative values''' ) return 2 * pi * radius * (height + radius) def _UpperCAmelCase ( __A : float , __A : float ): if torus_radius < 0 or tube_radius < 0: raise ValueError('''surface_area_torus() only accepts non-negative values''' ) if torus_radius < tube_radius: raise ValueError( '''surface_area_torus() does not support spindle or self intersecting tori''' ) return 4 * pow(__A , 2 ) * torus_radius * tube_radius def _UpperCAmelCase ( __A : float , __A : float ): if length < 0 or width < 0: raise ValueError('''area_rectangle() only accepts non-negative values''' ) return length * width def _UpperCAmelCase ( __A : float ): if side_length < 0: raise ValueError('''area_square() only accepts non-negative values''' ) return side_length**2 def _UpperCAmelCase ( __A : float , __A : float ): if base < 0 or height < 0: raise ValueError('''area_triangle() only accepts non-negative values''' ) return (base * height) / 2 def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if sidea < 0 or sidea < 0 or sidea < 0: raise ValueError('''area_triangle_three_sides() only accepts non-negative values''' ) elif sidea + sidea < sidea or sidea + sidea < sidea or sidea + sidea < sidea: raise ValueError('''Given three sides do not form a triangle''' ) a_ : int = (sidea + sidea + sidea) / 2 a_ : Optional[Any] = sqrt( semi_perimeter * (semi_perimeter - sidea) * (semi_perimeter - sidea) * (semi_perimeter - sidea) ) return area def _UpperCAmelCase ( __A : float , __A : float ): if base < 0 or height < 0: raise ValueError('''area_parallelogram() only accepts non-negative values''' ) return base * height def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if basea < 0 or basea < 0 or height < 0: raise ValueError('''area_trapezium() only accepts non-negative values''' ) return 1 / 2 * (basea + basea) * height def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''area_circle() only accepts non-negative values''' ) return pi * radius**2 def _UpperCAmelCase ( __A : float , __A : float ): if radius_x < 0 or radius_y < 0: raise ValueError('''area_ellipse() only accepts non-negative values''' ) return pi * radius_x * radius_y def _UpperCAmelCase ( __A : float , __A : float ): if diagonal_a < 0 or diagonal_a < 0: raise ValueError('''area_rhombus() only accepts non-negative values''' ) return 1 / 2 * diagonal_a * diagonal_a def _UpperCAmelCase ( __A : int , __A : float ): if not isinstance(__A , __A ) or sides < 3: raise ValueError( '''area_reg_polygon() only accepts integers greater than or \ equal to three as number of sides''' ) elif length < 0: raise ValueError( '''area_reg_polygon() only accepts non-negative values as \ length of a side''' ) return (sides * length**2) / (4 * tan(pi / sides )) return (sides * length**2) / (4 * tan(pi / sides )) if __name__ == "__main__": import doctest doctest.testmod(verbose=True) # verbose so we can see methods missing tests print('[DEMO] Areas of various geometric shapes: \n') print(F"""Rectangle: {area_rectangle(10, 20) = }""") print(F"""Square: {area_square(10) = }""") print(F"""Triangle: {area_triangle(10, 10) = }""") print(F"""Triangle: {area_triangle_three_sides(5, 12, 13) = }""") print(F"""Parallelogram: {area_parallelogram(10, 20) = }""") print(F"""Rhombus: {area_rhombus(10, 20) = }""") print(F"""Trapezium: {area_trapezium(10, 20, 30) = }""") print(F"""Circle: {area_circle(20) = }""") print(F"""Ellipse: {area_ellipse(10, 20) = }""") print('\nSurface Areas of various geometric shapes: \n') print(F"""Cube: {surface_area_cube(20) = }""") print(F"""Cuboid: {surface_area_cuboid(10, 20, 30) = }""") print(F"""Sphere: {surface_area_sphere(20) = }""") print(F"""Hemisphere: {surface_area_hemisphere(20) = }""") print(F"""Cone: {surface_area_cone(10, 20) = }""") print(F"""Conical Frustum: {surface_area_conical_frustum(10, 20, 30) = }""") print(F"""Cylinder: {surface_area_cylinder(10, 20) = }""") print(F"""Torus: {surface_area_torus(20, 10) = }""") print(F"""Equilateral Triangle: {area_reg_polygon(3, 10) = }""") print(F"""Square: {area_reg_polygon(4, 10) = }""") print(F"""Reqular Pentagon: {area_reg_polygon(5, 10) = }""")
666
0
'''simple docstring''' from dataclasses import dataclass from typing import Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, apply_forward_hook from .modeling_utils import ModelMixin from .vae import Decoder, DecoderOutput, Encoder, VectorQuantizer @dataclass class SCREAMING_SNAKE_CASE ( lowercase__ ): snake_case__ = 42 class SCREAMING_SNAKE_CASE ( lowercase__ , lowercase__ ): @register_to_config def __init__( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : int = 3 , __SCREAMING_SNAKE_CASE : int = 3 , __SCREAMING_SNAKE_CASE : Tuple[str] = ("DownEncoderBlock2D",) , __SCREAMING_SNAKE_CASE : Tuple[str] = ("UpDecoderBlock2D",) , __SCREAMING_SNAKE_CASE : Tuple[int] = (64,) , __SCREAMING_SNAKE_CASE : int = 1 , __SCREAMING_SNAKE_CASE : str = "silu" , __SCREAMING_SNAKE_CASE : int = 3 , __SCREAMING_SNAKE_CASE : int = 32 , __SCREAMING_SNAKE_CASE : int = 256 , __SCREAMING_SNAKE_CASE : int = 32 , __SCREAMING_SNAKE_CASE : Optional[int] = None , __SCREAMING_SNAKE_CASE : float = 0.1_8215 , __SCREAMING_SNAKE_CASE : str = "group" , ) -> List[str]: super().__init__() # pass init params to Encoder a_ : str = Encoder( in_channels=UpperCAmelCase__ , out_channels=UpperCAmelCase__ , down_block_types=UpperCAmelCase__ , block_out_channels=UpperCAmelCase__ , layers_per_block=UpperCAmelCase__ , act_fn=UpperCAmelCase__ , norm_num_groups=UpperCAmelCase__ , double_z=UpperCAmelCase__ , ) a_ : List[str] = vq_embed_dim if vq_embed_dim is not None else latent_channels a_ : Any = nn.Convad(UpperCAmelCase__ , UpperCAmelCase__ , 1 ) a_ : List[str] = VectorQuantizer(UpperCAmelCase__ , UpperCAmelCase__ , beta=0.25 , remap=UpperCAmelCase__ , sane_index_shape=UpperCAmelCase__ ) a_ : Optional[int] = nn.Convad(UpperCAmelCase__ , UpperCAmelCase__ , 1 ) # pass init params to Decoder a_ : str = Decoder( in_channels=UpperCAmelCase__ , out_channels=UpperCAmelCase__ , up_block_types=UpperCAmelCase__ , block_out_channels=UpperCAmelCase__ , layers_per_block=UpperCAmelCase__ , act_fn=UpperCAmelCase__ , norm_num_groups=UpperCAmelCase__ , norm_type=UpperCAmelCase__ , ) @apply_forward_hook def SCREAMING_SNAKE_CASE ( self : List[Any] , __SCREAMING_SNAKE_CASE : torch.FloatTensor , __SCREAMING_SNAKE_CASE : bool = True ) -> Union[str, Any]: a_ : Any = self.encoder(UpperCAmelCase__ ) a_ : Tuple = self.quant_conv(UpperCAmelCase__ ) if not return_dict: return (h,) return VQEncoderOutput(latents=UpperCAmelCase__ ) @apply_forward_hook def SCREAMING_SNAKE_CASE ( self : Tuple , __SCREAMING_SNAKE_CASE : torch.FloatTensor , __SCREAMING_SNAKE_CASE : bool = False , __SCREAMING_SNAKE_CASE : bool = True ) -> List[Any]: # also go through quantization layer if not force_not_quantize: a_ : Union[str, Any] = self.quantize(UpperCAmelCase__ ) else: a_ : List[str] = h a_ : Union[str, Any] = self.post_quant_conv(UpperCAmelCase__ ) a_ : int = self.decoder(UpperCAmelCase__ , quant if self.config.norm_type == '''spatial''' else None ) if not return_dict: return (dec,) return DecoderOutput(sample=UpperCAmelCase__ ) def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : torch.FloatTensor , __SCREAMING_SNAKE_CASE : bool = True ) -> Any: a_ : List[str] = sample a_ : Optional[Any] = self.encode(UpperCAmelCase__ ).latents a_ : Tuple = self.decode(UpperCAmelCase__ ).sample if not return_dict: return (dec,) return DecoderOutput(sample=UpperCAmelCase__ )
700
'''simple docstring''' import random import unittest import torch from diffusers import IFInpaintingSuperResolutionPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = IFInpaintingSuperResolutionPipeline snake_case__ = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"width", "height"} snake_case__ = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({"original_image"} ) snake_case__ = PipelineTesterMixin.required_optional_params - {"latents"} def SCREAMING_SNAKE_CASE ( self : str ) -> List[str]: return self._get_superresolution_dummy_components() def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Dict=0 ) -> List[Any]: if str(__SCREAMING_SNAKE_CASE ).startswith('''mps''' ): a_ : Optional[int] = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: a_ : str = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) a_ : Dict = floats_tensor((1, 3, 16, 16) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = floats_tensor((1, 3, 32, 32) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) a_ : Tuple = floats_tensor((1, 3, 32, 32) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': image, '''original_image''': original_image, '''mask_image''': mask_image, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) def SCREAMING_SNAKE_CASE ( self : int ) -> int: self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def SCREAMING_SNAKE_CASE ( self : str ) -> Tuple: # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder super().test_save_load_floataa(expected_max_diff=1e-1 ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Optional[int]: self._test_attention_slicing_forward_pass(expected_max_diff=1e-2 ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Optional[Any]: self._test_save_load_local() def SCREAMING_SNAKE_CASE ( self : Dict ) -> Optional[Any]: self._test_inference_batch_single_identical( expected_max_diff=1e-2 , )
666
0
from typing import Any, Dict, Optional import torch import torch.nn.functional as F from torch import nn from ..utils import maybe_allow_in_graph from .activations import get_activation from .attention_processor import Attention from .embeddings import CombinedTimestepLabelEmbeddings @maybe_allow_in_graph class SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : Tuple , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Tuple=0.0 , __SCREAMING_SNAKE_CASE : Optional[int] = None , __SCREAMING_SNAKE_CASE : str = "geglu" , __SCREAMING_SNAKE_CASE : Optional[int] = None , __SCREAMING_SNAKE_CASE : bool = False , __SCREAMING_SNAKE_CASE : bool = False , __SCREAMING_SNAKE_CASE : bool = False , __SCREAMING_SNAKE_CASE : bool = False , __SCREAMING_SNAKE_CASE : bool = True , __SCREAMING_SNAKE_CASE : str = "layer_norm" , __SCREAMING_SNAKE_CASE : bool = False , ) -> List[Any]: super().__init__() a_ : List[Any] = only_cross_attention a_ : Optional[Any] = (num_embeds_ada_norm is not None) and norm_type == '''ada_norm_zero''' a_ : Dict = (num_embeds_ada_norm is not None) and norm_type == '''ada_norm''' if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None: raise ValueError( f'`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to' f' define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}.' ) # Define 3 blocks. Each block has its own normalization layer. # 1. Self-Attn if self.use_ada_layer_norm: a_ : List[Any] = AdaLayerNorm(_lowerCamelCase , _lowerCamelCase ) elif self.use_ada_layer_norm_zero: a_ : List[str] = AdaLayerNormZero(_lowerCamelCase , _lowerCamelCase ) else: a_ : Optional[int] = nn.LayerNorm(_lowerCamelCase , elementwise_affine=_lowerCamelCase ) a_ : List[Any] = Attention( query_dim=_lowerCamelCase , heads=_lowerCamelCase , dim_head=_lowerCamelCase , dropout=_lowerCamelCase , bias=_lowerCamelCase , cross_attention_dim=cross_attention_dim if only_cross_attention else None , upcast_attention=_lowerCamelCase , ) # 2. Cross-Attn if cross_attention_dim is not None or double_self_attention: # We currently only use AdaLayerNormZero for self attention where there will only be one attention block. # I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during # the second cross attention block. a_ : int = ( AdaLayerNorm(_lowerCamelCase , _lowerCamelCase ) if self.use_ada_layer_norm else nn.LayerNorm(_lowerCamelCase , elementwise_affine=_lowerCamelCase ) ) a_ : Optional[int] = Attention( query_dim=_lowerCamelCase , cross_attention_dim=cross_attention_dim if not double_self_attention else None , heads=_lowerCamelCase , dim_head=_lowerCamelCase , dropout=_lowerCamelCase , bias=_lowerCamelCase , upcast_attention=_lowerCamelCase , ) # is self-attn if encoder_hidden_states is none else: a_ : str = None a_ : List[Any] = None # 3. Feed-forward a_ : Optional[Any] = nn.LayerNorm(_lowerCamelCase , elementwise_affine=_lowerCamelCase ) a_ : Dict = FeedForward(_lowerCamelCase , dropout=_lowerCamelCase , activation_fn=_lowerCamelCase , final_dropout=_lowerCamelCase ) # let chunk size default to None a_ : List[str] = None a_ : Optional[int] = 0 def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : int ) -> Optional[Any]: # Sets chunk feed-forward a_ : List[Any] = chunk_size a_ : Dict = dim def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : torch.FloatTensor , __SCREAMING_SNAKE_CASE : Optional[torch.FloatTensor] = None , __SCREAMING_SNAKE_CASE : Optional[torch.FloatTensor] = None , __SCREAMING_SNAKE_CASE : Optional[torch.FloatTensor] = None , __SCREAMING_SNAKE_CASE : Optional[torch.LongTensor] = None , __SCREAMING_SNAKE_CASE : Dict[str, Any] = None , __SCREAMING_SNAKE_CASE : Optional[torch.LongTensor] = None , ) -> Tuple: # Notice that normalization is always applied before the real computation in the following blocks. # 1. Self-Attention if self.use_ada_layer_norm: a_ : List[str] = self.norma(_lowerCamelCase , _lowerCamelCase ) elif self.use_ada_layer_norm_zero: a_ , a_ , a_ , a_ , a_ : Optional[int] = self.norma( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , hidden_dtype=hidden_states.dtype ) else: a_ : Optional[Any] = self.norma(_lowerCamelCase ) a_ : List[str] = cross_attention_kwargs if cross_attention_kwargs is not None else {} a_ : Tuple = self.attna( _lowerCamelCase , encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None , attention_mask=_lowerCamelCase , **_lowerCamelCase , ) if self.use_ada_layer_norm_zero: a_ : Optional[int] = gate_msa.unsqueeze(1 ) * attn_output a_ : str = attn_output + hidden_states # 2. Cross-Attention if self.attna is not None: a_ : List[str] = ( self.norma(_lowerCamelCase , _lowerCamelCase ) if self.use_ada_layer_norm else self.norma(_lowerCamelCase ) ) a_ : Optional[int] = self.attna( _lowerCamelCase , encoder_hidden_states=_lowerCamelCase , attention_mask=_lowerCamelCase , **_lowerCamelCase , ) a_ : List[str] = attn_output + hidden_states # 3. Feed-forward a_ : str = self.norma(_lowerCamelCase ) if self.use_ada_layer_norm_zero: a_ : int = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None] if self._chunk_size is not None: # "feed_forward_chunk_size" can be used to save memory if norm_hidden_states.shape[self._chunk_dim] % self._chunk_size != 0: raise ValueError( f'`hidden_states` dimension to be chunked: {norm_hidden_states.shape[self._chunk_dim]} has to be divisible by chunk size: {self._chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`.' ) a_ : Dict = norm_hidden_states.shape[self._chunk_dim] // self._chunk_size a_ : Optional[int] = torch.cat( [self.ff(_lowerCamelCase ) for hid_slice in norm_hidden_states.chunk(_lowerCamelCase , dim=self._chunk_dim )] , dim=self._chunk_dim , ) else: a_ : Optional[int] = self.ff(_lowerCamelCase ) if self.use_ada_layer_norm_zero: a_ : Tuple = gate_mlp.unsqueeze(1 ) * ff_output a_ : Optional[int] = ff_output + hidden_states return hidden_states class SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : Any , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Optional[int] = None , __SCREAMING_SNAKE_CASE : int = 4 , __SCREAMING_SNAKE_CASE : float = 0.0 , __SCREAMING_SNAKE_CASE : str = "geglu" , __SCREAMING_SNAKE_CASE : bool = False , ) -> Optional[int]: super().__init__() a_ : Any = int(dim * mult ) a_ : int = dim_out if dim_out is not None else dim if activation_fn == "gelu": a_ : Optional[int] = GELU(_lowerCamelCase , _lowerCamelCase ) if activation_fn == "gelu-approximate": a_ : Tuple = GELU(_lowerCamelCase , _lowerCamelCase , approximate='''tanh''' ) elif activation_fn == "geglu": a_ : Union[str, Any] = GEGLU(_lowerCamelCase , _lowerCamelCase ) elif activation_fn == "geglu-approximate": a_ : Any = ApproximateGELU(_lowerCamelCase , _lowerCamelCase ) a_ : Any = nn.ModuleList([] ) # project in self.net.append(_lowerCamelCase ) # project dropout self.net.append(nn.Dropout(_lowerCamelCase ) ) # project out self.net.append(nn.Linear(_lowerCamelCase , _lowerCamelCase ) ) # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout if final_dropout: self.net.append(nn.Dropout(_lowerCamelCase ) ) def SCREAMING_SNAKE_CASE ( self : int , __SCREAMING_SNAKE_CASE : Any ) -> Any: for module in self.net: a_ : Union[str, Any] = module(_lowerCamelCase ) return hidden_states class SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : List[Any] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : str = "none" ) -> int: super().__init__() a_ : List[Any] = nn.Linear(_lowerCamelCase , _lowerCamelCase ) a_ : Optional[int] = approximate def SCREAMING_SNAKE_CASE ( self : List[Any] , __SCREAMING_SNAKE_CASE : List[str] ) -> List[Any]: if gate.device.type != "mps": return F.gelu(_lowerCamelCase , approximate=self.approximate ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) , approximate=self.approximate ).to(dtype=gate.dtype ) def SCREAMING_SNAKE_CASE ( self : Tuple , __SCREAMING_SNAKE_CASE : Union[str, Any] ) -> Union[str, Any]: a_ : str = self.proj(_lowerCamelCase ) a_ : str = self.gelu(_lowerCamelCase ) return hidden_states class SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : Any , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int ) -> str: super().__init__() a_ : List[Any] = nn.Linear(_lowerCamelCase , dim_out * 2 ) def SCREAMING_SNAKE_CASE ( self : Any , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Dict: if gate.device.type != "mps": return F.gelu(_lowerCamelCase ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) ).to(dtype=gate.dtype ) def SCREAMING_SNAKE_CASE ( self : int , __SCREAMING_SNAKE_CASE : Union[str, Any] ) -> Dict: a_ , a_ : List[str] = self.proj(_lowerCamelCase ).chunk(2 , dim=-1 ) return hidden_states * self.gelu(_lowerCamelCase ) class SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : Optional[int] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int ) -> Union[str, Any]: super().__init__() a_ : Any = nn.Linear(_lowerCamelCase , _lowerCamelCase ) def SCREAMING_SNAKE_CASE ( self : Tuple , __SCREAMING_SNAKE_CASE : List[Any] ) -> List[Any]: a_ : Dict = self.proj(_lowerCamelCase ) return x * torch.sigmoid(1.702 * x ) class SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : Dict , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : Tuple ) -> Optional[int]: super().__init__() a_ : int = nn.Embedding(_lowerCamelCase , _lowerCamelCase ) a_ : int = nn.SiLU() a_ : int = nn.Linear(_lowerCamelCase , embedding_dim * 2 ) a_ : List[Any] = nn.LayerNorm(_lowerCamelCase , elementwise_affine=_lowerCamelCase ) def SCREAMING_SNAKE_CASE ( self : Tuple , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Optional[Any]: a_ : List[Any] = self.linear(self.silu(self.emb(_lowerCamelCase ) ) ) a_ , a_ : Tuple = torch.chunk(_lowerCamelCase , 2 ) a_ : List[Any] = self.norm(_lowerCamelCase ) * (1 + scale) + shift return x class SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : int , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : int ) -> List[str]: super().__init__() a_ : int = CombinedTimestepLabelEmbeddings(_lowerCamelCase , _lowerCamelCase ) a_ : str = nn.SiLU() a_ : Any = nn.Linear(_lowerCamelCase , 6 * embedding_dim , bias=_lowerCamelCase ) a_ : Optional[int] = nn.LayerNorm(_lowerCamelCase , elementwise_affine=_lowerCamelCase , eps=1e-6 ) def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : List[Any]=None ) -> Any: a_ : Tuple = self.linear(self.silu(self.emb(_lowerCamelCase , _lowerCamelCase , hidden_dtype=_lowerCamelCase ) ) ) a_ , a_ , a_ , a_ , a_ , a_ : List[Any] = emb.chunk(6 , dim=1 ) a_ : Union[str, Any] = self.norm(_lowerCamelCase ) * (1 + scale_msa[:, None]) + shift_msa[:, None] return x, gate_msa, shift_mlp, scale_mlp, gate_mlp class SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : Optional[int] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Optional[str] = None , __SCREAMING_SNAKE_CASE : float = 1e-5 ) -> str: super().__init__() a_ : Any = num_groups a_ : Optional[int] = eps if act_fn is None: a_ : Tuple = None else: a_ : List[str] = get_activation(_lowerCamelCase ) a_ : Dict = nn.Linear(_lowerCamelCase , out_dim * 2 ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : Union[str, Any] ) -> Tuple: if self.act: a_ : Union[str, Any] = self.act(_lowerCamelCase ) a_ : List[Any] = self.linear(_lowerCamelCase ) a_ : str = emb[:, :, None, None] a_ , a_ : int = emb.chunk(2 , dim=1 ) a_ : Any = F.group_norm(_lowerCamelCase , self.num_groups , eps=self.eps ) a_ : int = x * (1 + scale) + shift return x
701
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCAmelCase = { 'configuration_git': ['GIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GitConfig', 'GitVisionConfig'], 'processing_git': ['GitProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ 'GIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'GitForCausalLM', 'GitModel', 'GitPreTrainedModel', 'GitVisionModel', ] if TYPE_CHECKING: from .configuration_git import GIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GitConfig, GitVisionConfig from .processing_git import GitProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_git import ( GIT_PRETRAINED_MODEL_ARCHIVE_LIST, GitForCausalLM, GitModel, GitPreTrainedModel, GitVisionModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
666
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = { 'microsoft/focalnet-tiny': 'https://huggingface.co/microsoft/focalnet-tiny/resolve/main/config.json', } class SCREAMING_SNAKE_CASE ( lowerCamelCase__ , lowerCamelCase__ ): snake_case__ = "focalnet" def __init__( self : Optional[int] , __SCREAMING_SNAKE_CASE : Optional[int]=224 , __SCREAMING_SNAKE_CASE : Optional[int]=4 , __SCREAMING_SNAKE_CASE : Dict=3 , __SCREAMING_SNAKE_CASE : str=96 , __SCREAMING_SNAKE_CASE : Tuple=False , __SCREAMING_SNAKE_CASE : int=[192, 384, 768, 768] , __SCREAMING_SNAKE_CASE : Tuple=[2, 2, 6, 2] , __SCREAMING_SNAKE_CASE : Union[str, Any]=[2, 2, 2, 2] , __SCREAMING_SNAKE_CASE : int=[3, 3, 3, 3] , __SCREAMING_SNAKE_CASE : str="gelu" , __SCREAMING_SNAKE_CASE : int=4.0 , __SCREAMING_SNAKE_CASE : List[str]=0.0 , __SCREAMING_SNAKE_CASE : Optional[Any]=0.1 , __SCREAMING_SNAKE_CASE : List[Any]=False , __SCREAMING_SNAKE_CASE : Union[str, Any]=1e-4 , __SCREAMING_SNAKE_CASE : List[str]=False , __SCREAMING_SNAKE_CASE : int=False , __SCREAMING_SNAKE_CASE : int=False , __SCREAMING_SNAKE_CASE : List[str]=0.02 , __SCREAMING_SNAKE_CASE : Dict=1e-5 , __SCREAMING_SNAKE_CASE : Dict=32 , __SCREAMING_SNAKE_CASE : Tuple=None , __SCREAMING_SNAKE_CASE : str=None , **__SCREAMING_SNAKE_CASE : int , ) -> List[str]: super().__init__(**__lowerCamelCase ) a_ : int = image_size a_ : int = patch_size a_ : Union[str, Any] = num_channels a_ : Union[str, Any] = embed_dim a_ : Union[str, Any] = use_conv_embed a_ : List[str] = hidden_sizes a_ : Any = depths a_ : Any = focal_levels a_ : Union[str, Any] = focal_windows a_ : Optional[Any] = hidden_act a_ : Dict = mlp_ratio a_ : Tuple = hidden_dropout_prob a_ : Dict = drop_path_rate a_ : Dict = use_layerscale a_ : Optional[int] = layerscale_value a_ : List[Any] = use_post_layernorm a_ : int = use_post_layernorm_in_modulation a_ : int = normalize_modulator a_ : Optional[Any] = initializer_range a_ : Tuple = layer_norm_eps a_ : Tuple = encoder_stride a_ : str = ['''stem'''] + [f'stage{idx}' for idx in range(1 , len(self.depths ) + 1 )] a_ : Dict = get_aligned_output_features_output_indices( out_features=__lowerCamelCase , out_indices=__lowerCamelCase , stage_names=self.stage_names )
702
'''simple docstring''' from unittest.mock import patch import pyspark from datasets.packaged_modules.spark.spark import ( Spark, SparkExamplesIterable, _generate_iterable_examples, ) from ..utils import ( require_dill_gt_0_3_2, require_not_windows, ) def _UpperCAmelCase ( __A : List[str] , __A : List[Any] ): a_ : Any = [] for part_id in partition_order: a_ : str = df.where(f'SPARK_PARTITION_ID() = {part_id}' ).collect() for row_idx, row in enumerate(__A ): expected_row_ids_and_row_dicts.append((f'{part_id}_{row_idx}', row.asDict()) ) return expected_row_ids_and_row_dicts @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : List[str] = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : Union[str, Any] = spark.range(1_00 ).repartition(1 ) a_ : Any = Spark(__A ) # The id ints will be converted to Pyarrow int64s, so each row will be 8 bytes. Setting a max_shard_size of 16 means # that each partition can hold 2 rows. spark_builder._repartition_df_if_needed(max_shard_size=16 ) # Given that the dataframe has 100 rows and each partition has 2 rows, we expect 50 partitions. assert spark_builder.df.rdd.getNumPartitions() == 50 @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : List[Any] = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : int = spark.range(10 ).repartition(2 ) a_ : Tuple = [1, 0] a_ : List[str] = _generate_iterable_examples(__A , __A ) # Reverse the partitions. a_ : int = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , __A ) for i, (row_id, row_dict) in enumerate(generate_fn() ): a_ , a_ : List[Any] = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : int = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : str = spark.range(10 ).repartition(1 ) a_ : Tuple = SparkExamplesIterable(__A ) assert it.n_shards == 1 for i, (row_id, row_dict) in enumerate(__A ): assert row_id == f'0_{i}' assert row_dict == {"id": i} @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : Tuple = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : str = spark.range(30 ).repartition(3 ) # Mock the generator so that shuffle reverses the partition indices. with patch('''numpy.random.Generator''' ) as generator_mock: a_ : Union[str, Any] = lambda __A : x.reverse() a_ : Any = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [2, 1, 0] ) a_ : str = SparkExamplesIterable(__A ).shuffle_data_sources(__A ) assert shuffled_it.n_shards == 3 for i, (row_id, row_dict) in enumerate(__A ): a_ , a_ : Optional[Any] = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : int = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : List[str] = spark.range(20 ).repartition(4 ) # Partitions 0 and 2 a_ : Dict = SparkExamplesIterable(__A ).shard_data_sources(worker_id=0 , num_workers=2 ) assert shard_it_a.n_shards == 2 a_ : Optional[Any] = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [0, 2] ) for i, (row_id, row_dict) in enumerate(__A ): a_ , a_ : Tuple = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict # Partitions 1 and 3 a_ : List[Any] = SparkExamplesIterable(__A ).shard_data_sources(worker_id=1 , num_workers=2 ) assert shard_it_a.n_shards == 2 a_ : Optional[int] = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [1, 3] ) for i, (row_id, row_dict) in enumerate(__A ): a_ , a_ : Any = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : Any = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : List[Any] = spark.range(1_00 ).repartition(1 ) a_ : Optional[Any] = Spark(__A ) # Choose a small max_shard_size for maximum partitioning. spark_builder._repartition_df_if_needed(max_shard_size=1 ) # The new number of partitions should not be greater than the number of rows. assert spark_builder.df.rdd.getNumPartitions() == 1_00
666
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available __lowerCAmelCase = { "configuration_groupvit": [ "GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP", "GroupViTConfig", "GroupViTOnnxConfig", "GroupViTTextConfig", "GroupViTVisionConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ "GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST", "GroupViTModel", "GroupViTPreTrainedModel", "GroupViTTextModel", "GroupViTVisionModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ "TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST", "TFGroupViTModel", "TFGroupViTPreTrainedModel", "TFGroupViTTextModel", "TFGroupViTVisionModel", ] if TYPE_CHECKING: from .configuration_groupvit import ( GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GroupViTConfig, GroupViTOnnxConfig, GroupViTTextConfig, GroupViTVisionConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_groupvit import ( GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, GroupViTModel, GroupViTPreTrainedModel, GroupViTTextModel, GroupViTVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_groupvit import ( TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFGroupViTModel, TFGroupViTPreTrainedModel, TFGroupViTTextModel, TFGroupViTVisionModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
703
'''simple docstring''' from collections import OrderedDict from typing import TYPE_CHECKING, Any, List, Mapping, Optional from packaging import version if TYPE_CHECKING: from ... import PreTrainedTokenizer, TensorType from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import is_torch_available, logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = { 'bigscience/bloom': 'https://huggingface.co/bigscience/bloom/resolve/main/config.json', 'bigscience/bloom-560m': 'https://huggingface.co/bigscience/bloom-560m/blob/main/config.json', 'bigscience/bloom-1b1': 'https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json', 'bigscience/bloom-1b7': 'https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json', 'bigscience/bloom-3b': 'https://huggingface.co/bigscience/bloom-3b/blob/main/config.json', 'bigscience/bloom-7b1': 'https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json', } class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "bloom" snake_case__ = ["past_key_values"] snake_case__ = { "num_hidden_layers": "n_layer", "num_attention_heads": "n_head", } def __init__( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : int=25_0880 , __SCREAMING_SNAKE_CASE : Dict=64 , __SCREAMING_SNAKE_CASE : Tuple=2 , __SCREAMING_SNAKE_CASE : int=8 , __SCREAMING_SNAKE_CASE : Any=1e-5 , __SCREAMING_SNAKE_CASE : Optional[Any]=0.02 , __SCREAMING_SNAKE_CASE : Union[str, Any]=True , __SCREAMING_SNAKE_CASE : int=1 , __SCREAMING_SNAKE_CASE : Any=2 , __SCREAMING_SNAKE_CASE : Optional[Any]=False , __SCREAMING_SNAKE_CASE : Optional[Any]=0.0 , __SCREAMING_SNAKE_CASE : str=0.0 , __SCREAMING_SNAKE_CASE : List[Any]=1 , __SCREAMING_SNAKE_CASE : List[str]=False , **__SCREAMING_SNAKE_CASE : str , ) -> Any: a_ : Optional[int] = vocab_size # Backward compatibility with n_embed kwarg a_ : Any = kwargs.pop('''n_embed''' , __SCREAMING_SNAKE_CASE ) a_ : Optional[int] = hidden_size if n_embed is None else n_embed a_ : int = n_layer a_ : str = n_head a_ : Optional[int] = layer_norm_epsilon a_ : Dict = initializer_range a_ : List[str] = use_cache a_ : Dict = pretraining_tp a_ : Optional[Any] = apply_residual_connection_post_layernorm a_ : Optional[Any] = hidden_dropout a_ : List[str] = attention_dropout a_ : Dict = bos_token_id a_ : Optional[int] = eos_token_id a_ : Any = slow_but_exact super().__init__(bos_token_id=__SCREAMING_SNAKE_CASE , eos_token_id=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = version.parse("1.12" ) def __init__( self : Tuple , __SCREAMING_SNAKE_CASE : PretrainedConfig , __SCREAMING_SNAKE_CASE : str = "default" , __SCREAMING_SNAKE_CASE : List[PatchingSpec] = None , __SCREAMING_SNAKE_CASE : bool = False , ) -> Optional[Any]: super().__init__(__SCREAMING_SNAKE_CASE , task=__SCREAMING_SNAKE_CASE , patching_specs=__SCREAMING_SNAKE_CASE , use_past=__SCREAMING_SNAKE_CASE ) if not getattr(self._config , '''pad_token_id''' , __SCREAMING_SNAKE_CASE ): # TODO: how to do that better? a_ : Tuple = 0 @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Mapping[str, Mapping[int, str]]: a_ : Optional[Any] = OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}} ) if self.use_past: # BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344 self.fill_with_past_key_values_(__SCREAMING_SNAKE_CASE , direction='''inputs''' , inverted_values_shape=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = {0: '''batch''', 1: '''past_sequence + sequence'''} else: a_ : Union[str, Any] = {0: '''batch''', 1: '''sequence'''} return common_inputs @property def SCREAMING_SNAKE_CASE ( self : Any ) -> int: return self._config.n_layer @property def SCREAMING_SNAKE_CASE ( self : int ) -> int: return self._config.n_head @property def SCREAMING_SNAKE_CASE ( self : int ) -> float: return 1e-3 def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : "PreTrainedTokenizer" , __SCREAMING_SNAKE_CASE : int = -1 , __SCREAMING_SNAKE_CASE : int = -1 , __SCREAMING_SNAKE_CASE : bool = False , __SCREAMING_SNAKE_CASE : Optional["TensorType"] = None , ) -> Mapping[str, Any]: a_ : Dict = super(__SCREAMING_SNAKE_CASE , self ).generate_dummy_inputs( __SCREAMING_SNAKE_CASE , batch_size=__SCREAMING_SNAKE_CASE , seq_length=__SCREAMING_SNAKE_CASE , is_pair=__SCREAMING_SNAKE_CASE , framework=__SCREAMING_SNAKE_CASE ) # We need to order the input in the way they appears in the forward() a_ : Union[str, Any] = OrderedDict({'''input_ids''': common_inputs['''input_ids''']} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch a_ , a_ : Any = common_inputs['''input_ids'''].shape # Not using the same length for past_key_values a_ : str = seqlen + 2 a_ : Any = self._config.hidden_size // self.num_attention_heads a_ : Optional[int] = ( batch * self.num_attention_heads, head_dim, past_key_values_length, ) a_ : Any = ( batch * self.num_attention_heads, past_key_values_length, head_dim, ) a_ : List[str] = [ (torch.zeros(__SCREAMING_SNAKE_CASE ), torch.zeros(__SCREAMING_SNAKE_CASE )) for _ in range(self.num_layers ) ] a_ : Union[str, Any] = common_inputs['''attention_mask'''] if self.use_past: a_ : Optional[int] = ordered_inputs['''attention_mask'''].dtype a_ : List[Any] = torch.cat( [ordered_inputs['''attention_mask'''], torch.ones(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , dtype=__SCREAMING_SNAKE_CASE )] , dim=1 ) return ordered_inputs @property def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> int: return 13
666
0
'''simple docstring''' import argparse import json import gdown import numpy as np import torch from huggingface_hub import hf_hub_download from transformers import ( VideoMAEConfig, VideoMAEForPreTraining, VideoMAEForVideoClassification, VideoMAEImageProcessor, ) def _UpperCAmelCase ( __A : Any ): a_ : Any = VideoMAEConfig() set_architecture_configs(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if "finetuned" not in model_name: a_ : Dict = False if "finetuned" in model_name: a_ : List[str] = "huggingface/label-files" if "kinetics" in model_name: a_ : List[Any] = 4_00 a_ : int = "kinetics400-id2label.json" elif "ssv2" in model_name: a_ : Union[str, Any] = 1_74 a_ : Any = "something-something-v2-id2label.json" else: raise ValueError('''Model name should either contain \'kinetics\' or \'ssv2\' in case it\'s fine-tuned.''' ) a_ : str = json.load(open(hf_hub_download(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , repo_type='''dataset''' ) , '''r''' ) ) a_ : List[str] = {int(__SCREAMING_SNAKE_CASE ): v for k, v in idalabel.items()} a_ : Optional[int] = idalabel a_ : int = {v: k for k, v in idalabel.items()} return config def _UpperCAmelCase ( __A : Any , __A : str ): if "small" in model_name: a_ : Optional[int] = 3_84 a_ : Tuple = 15_36 a_ : Dict = 12 a_ : int = 16 a_ : Optional[int] = 12 a_ : Union[str, Any] = 3 a_ : int = 1_92 a_ : Optional[int] = 7_68 elif "large" in model_name: a_ : Any = 10_24 a_ : List[str] = 40_96 a_ : Tuple = 24 a_ : Tuple = 16 a_ : Union[str, Any] = 12 a_ : List[str] = 8 a_ : Optional[int] = 5_12 a_ : Optional[Any] = 20_48 elif "huge" in model_name: a_ : List[Any] = 12_80 a_ : Dict = 51_20 a_ : List[Any] = 32 a_ : Optional[int] = 16 a_ : List[str] = 12 a_ : List[Any] = 8 a_ : Optional[int] = 6_40 a_ : Any = 25_60 elif "base" not in model_name: raise ValueError('''Model name should include either \"small\", \"base\", \"large\", or \"huge\"''' ) def _UpperCAmelCase ( __A : Optional[Any] ): if "encoder." in name: a_ : int = name.replace('''encoder.''' , '''''' ) if "cls_token" in name: a_ : Dict = name.replace('''cls_token''' , '''videomae.embeddings.cls_token''' ) if "decoder_pos_embed" in name: a_ : Tuple = name.replace('''decoder_pos_embed''' , '''decoder.decoder_pos_embed''' ) if "pos_embed" in name and "decoder" not in name: a_ : Dict = name.replace('''pos_embed''' , '''videomae.embeddings.position_embeddings''' ) if "patch_embed.proj" in name: a_ : List[Any] = name.replace('''patch_embed.proj''' , '''videomae.embeddings.patch_embeddings.projection''' ) if "patch_embed.norm" in name: a_ : str = name.replace('''patch_embed.norm''' , '''videomae.embeddings.norm''' ) if "decoder.blocks" in name: a_ : Optional[int] = name.replace('''decoder.blocks''' , '''decoder.decoder_layers''' ) if "blocks" in name: a_ : int = name.replace('''blocks''' , '''videomae.encoder.layer''' ) if "attn.proj" in name: a_ : Union[str, Any] = name.replace('''attn.proj''' , '''attention.output.dense''' ) if "attn" in name and "bias" not in name: a_ : Tuple = name.replace('''attn''' , '''attention.self''' ) if "attn" in name: a_ : Tuple = name.replace('''attn''' , '''attention.attention''' ) if "norm1" in name: a_ : int = name.replace('''norm1''' , '''layernorm_before''' ) if "norm2" in name: a_ : List[Any] = name.replace('''norm2''' , '''layernorm_after''' ) if "mlp.fc1" in name: a_ : Any = name.replace('''mlp.fc1''' , '''intermediate.dense''' ) if "mlp.fc2" in name: a_ : int = name.replace('''mlp.fc2''' , '''output.dense''' ) if "decoder_embed" in name: a_ : List[str] = name.replace('''decoder_embed''' , '''decoder.decoder_embed''' ) if "decoder_norm" in name: a_ : Union[str, Any] = name.replace('''decoder_norm''' , '''decoder.decoder_norm''' ) if "decoder_pred" in name: a_ : Optional[Any] = name.replace('''decoder_pred''' , '''decoder.decoder_pred''' ) if "norm.weight" in name and "decoder" not in name and "fc" not in name: a_ : List[str] = name.replace('''norm.weight''' , '''videomae.layernorm.weight''' ) if "norm.bias" in name and "decoder" not in name and "fc" not in name: a_ : Optional[Any] = name.replace('''norm.bias''' , '''videomae.layernorm.bias''' ) if "head" in name and "decoder" not in name: a_ : Optional[int] = name.replace('''head''' , '''classifier''' ) return name def _UpperCAmelCase ( __A : Any , __A : Optional[Any] ): for key in orig_state_dict.copy().keys(): a_ : Dict = orig_state_dict.pop(__SCREAMING_SNAKE_CASE ) if key.startswith('''encoder.''' ): a_ : List[str] = key.replace('''encoder.''' , '''''' ) if "qkv" in key: a_ : Optional[Any] = key.split('''.''' ) if key.startswith('''decoder.blocks''' ): a_ : str = config.decoder_hidden_size a_ : str = int(key_split[2] ) a_ : int = "decoder.decoder_layers." if "weight" in key: a_ : Any = val[:dim, :] a_ : Any = val[dim : dim * 2, :] a_ : Union[str, Any] = val[-dim:, :] else: a_ : Dict = config.hidden_size a_ : List[str] = int(key_split[1] ) a_ : Dict = "videomae.encoder.layer." if "weight" in key: a_ : Optional[Any] = val[:dim, :] a_ : Dict = val[dim : dim * 2, :] a_ : Optional[Any] = val[-dim:, :] else: a_ : Union[str, Any] = val return orig_state_dict def _UpperCAmelCase ( ): a_ : List[str] = hf_hub_download( repo_id='''hf-internal-testing/spaghetti-video''' , filename='''eating_spaghetti.npy''' , repo_type='''dataset''' ) a_ : Union[str, Any] = np.load(__SCREAMING_SNAKE_CASE ) return list(__SCREAMING_SNAKE_CASE ) def _UpperCAmelCase ( __A : Optional[Any] , __A : int , __A : str , __A : Dict ): a_ : List[str] = get_videomae_config(__SCREAMING_SNAKE_CASE ) if "finetuned" in model_name: a_ : int = VideoMAEForVideoClassification(__SCREAMING_SNAKE_CASE ) else: a_ : str = VideoMAEForPreTraining(__SCREAMING_SNAKE_CASE ) # download original checkpoint, hosted on Google Drive a_ : Dict = "pytorch_model.bin" gdown.cached_download(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , quiet=__SCREAMING_SNAKE_CASE ) a_ : str = torch.load(__SCREAMING_SNAKE_CASE , map_location='''cpu''' ) if "model" in files: a_ : str = files["model"] else: a_ : Optional[Any] = files["module"] a_ : Any = convert_state_dict(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) model.load_state_dict(__SCREAMING_SNAKE_CASE ) model.eval() # verify model on basic input a_ : Tuple = VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5] , image_std=[0.5, 0.5, 0.5] ) a_ : Union[str, Any] = prepare_video() a_ : str = image_processor(__SCREAMING_SNAKE_CASE , return_tensors='''pt''' ) if "finetuned" not in model_name: a_ : Tuple = hf_hub_download(repo_id='''hf-internal-testing/bool-masked-pos''' , filename='''bool_masked_pos.pt''' ) a_ : List[str] = torch.load(__SCREAMING_SNAKE_CASE ) a_ : int = model(**__SCREAMING_SNAKE_CASE ) a_ : int = outputs.logits a_ : Dict = [ "videomae-small-finetuned-kinetics", "videomae-small-finetuned-ssv2", # Kinetics-400 checkpoints (short = pretrained only for 800 epochs instead of 1600) "videomae-base-short", "videomae-base-short-finetuned-kinetics", "videomae-base", "videomae-base-finetuned-kinetics", "videomae-large", "videomae-large-finetuned-kinetics", "videomae-huge-finetuned-kinetics", # Something-Something-v2 checkpoints (short = pretrained only for 800 epochs instead of 2400) "videomae-base-short-ssv2", "videomae-base-short-finetuned-ssv2", "videomae-base-ssv2", "videomae-base-finetuned-ssv2", ] # NOTE: logits were tested with image_mean and image_std equal to [0.5, 0.5, 0.5] and [0.5, 0.5, 0.5] if model_name == "videomae-small-finetuned-kinetics": a_ : Tuple = torch.Size([1, 4_00] ) a_ : Dict = torch.tensor([-0.9291, -0.4061, -0.9307] ) elif model_name == "videomae-small-finetuned-ssv2": a_ : Union[str, Any] = torch.Size([1, 1_74] ) a_ : str = torch.tensor([0.2671, -0.4689, -0.8235] ) elif model_name == "videomae-base": a_ : List[str] = torch.Size([1, 14_08, 15_36] ) a_ : Dict = torch.tensor([[0.7739, 0.7968, 0.7089], [0.6701, 0.7487, 0.6209], [0.4287, 0.5158, 0.4773]] ) elif model_name == "videomae-base-short": a_ : Any = torch.Size([1, 14_08, 15_36] ) a_ : Any = torch.tensor([[0.7994, 0.9612, 0.8508], [0.7401, 0.8958, 0.8302], [0.5862, 0.7468, 0.7325]] ) # we verified the loss both for normalized and unnormalized targets for this one a_ : Tuple = torch.tensor([0.5142] ) if config.norm_pix_loss else torch.tensor([0.6469] ) elif model_name == "videomae-large": a_ : Union[str, Any] = torch.Size([1, 14_08, 15_36] ) a_ : Any = torch.tensor([[0.7149, 0.7997, 0.6966], [0.6768, 0.7869, 0.6948], [0.5139, 0.6221, 0.5605]] ) elif model_name == "videomae-large-finetuned-kinetics": a_ : Tuple = torch.Size([1, 4_00] ) a_ : Any = torch.tensor([0.0771, 0.0011, -0.3625] ) elif model_name == "videomae-huge-finetuned-kinetics": a_ : Optional[Any] = torch.Size([1, 4_00] ) a_ : str = torch.tensor([0.2433, 0.1632, -0.4894] ) elif model_name == "videomae-base-short-finetuned-kinetics": a_ : Optional[int] = torch.Size([1, 4_00] ) a_ : List[Any] = torch.tensor([0.6588, 0.0990, -0.2493] ) elif model_name == "videomae-base-finetuned-kinetics": a_ : Optional[int] = torch.Size([1, 4_00] ) a_ : Dict = torch.tensor([0.3669, -0.0688, -0.2421] ) elif model_name == "videomae-base-short-ssv2": a_ : List[Any] = torch.Size([1, 14_08, 15_36] ) a_ : Optional[Any] = torch.tensor([[0.4712, 0.5296, 0.5786], [0.2278, 0.2729, 0.4026], [0.0352, 0.0730, 0.2506]] ) elif model_name == "videomae-base-short-finetuned-ssv2": a_ : List[Any] = torch.Size([1, 1_74] ) a_ : Union[str, Any] = torch.tensor([-0.0537, -0.1539, -0.3266] ) elif model_name == "videomae-base-ssv2": a_ : List[str] = torch.Size([1, 14_08, 15_36] ) a_ : Optional[Any] = torch.tensor([[0.8131, 0.8727, 0.8546], [0.7366, 0.9377, 0.8870], [0.5935, 0.8874, 0.8564]] ) elif model_name == "videomae-base-finetuned-ssv2": a_ : int = torch.Size([1, 1_74] ) a_ : Union[str, Any] = torch.tensor([0.1961, -0.8337, -0.6389] ) else: raise ValueError(f'Model name not supported. Should be one of {model_names}' ) # verify logits assert logits.shape == expected_shape if "finetuned" in model_name: assert torch.allclose(logits[0, :3] , __SCREAMING_SNAKE_CASE , atol=1E-4 ) else: print('''Logits:''' , logits[0, :3, :3] ) assert torch.allclose(logits[0, :3, :3] , __SCREAMING_SNAKE_CASE , atol=1E-4 ) print('''Logits ok!''' ) # verify loss, if applicable if model_name == "videomae-base-short": a_ : int = outputs.loss assert torch.allclose(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , atol=1E-4 ) print('''Loss ok!''' ) if pytorch_dump_folder_path is not None: print(f'Saving model and image processor to {pytorch_dump_folder_path}' ) image_processor.save_pretrained(__SCREAMING_SNAKE_CASE ) model.save_pretrained(__SCREAMING_SNAKE_CASE ) if push_to_hub: print('''Pushing to the hub...''' ) model.push_to_hub(__SCREAMING_SNAKE_CASE , organization='''nielsr''' ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '--checkpoint_url', default='https://drive.google.com/u/1/uc?id=1tEhLyskjb755TJ65ptsrafUG2llSwQE1&amp;export=download&amp;confirm=t&amp;uuid=aa3276eb-fb7e-482a-adec-dc7171df14c4', type=str, help=( 'URL of the original PyTorch checkpoint (on Google Drive) you\'d like to convert. Should be a direct' ' download link.' ), ) parser.add_argument( '--pytorch_dump_folder_path', default='/Users/nielsrogge/Documents/VideoMAE/Test', type=str, help='Path to the output PyTorch model directory.', ) parser.add_argument('--model_name', default='videomae-base', type=str, help='Name of the model.') parser.add_argument( '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.' ) __lowerCAmelCase = parser.parse_args() convert_videomae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.model_name, args.push_to_hub)
704
'''simple docstring''' import sys __lowerCAmelCase = ( '73167176531330624919225119674426574742355349194934' '96983520312774506326239578318016984801869478851843' '85861560789112949495459501737958331952853208805511' '12540698747158523863050715693290963295227443043557' '66896648950445244523161731856403098711121722383113' '62229893423380308135336276614282806444486645238749' '30358907296290491560440772390713810515859307960866' '70172427121883998797908792274921901699720888093776' '65727333001053367881220235421809751254540594752243' '52584907711670556013604839586446706324415722155397' '53697817977846174064955149290862569321978468622482' '83972241375657056057490261407972968652414535100474' '82166370484403199890008895243450658541227588666881' '16427171479924442928230863465674813919123162824586' '17866458359124566529476545682848912883142607690042' '24219022671055626321111109370544217506941658960408' '07198403850962455444362981230987879927244284909188' '84580156166097919133875499200524063689912560717606' '05886116467109405077541002256983155200055935729725' '71636269561882670428252483600823257530420752963450' ) def _UpperCAmelCase ( __A : str ): a_ : Tuple = 1 for digit in s: product *= int(__A ) return product def _UpperCAmelCase ( __A : str = N ): a_ : Dict = -sys.maxsize - 1 a_ : Optional[int] = n[:13] a_ : str = 13 while cur_index < len(__A ) - 13: if int(n[cur_index] ) >= int(substr[0] ): a_ : Tuple = substr[1:] + n[cur_index] cur_index += 1 else: a_ : Dict = max(__A , str_eval(__A ) ) a_ : List[str] = n[cur_index : cur_index + 13] cur_index += 13 return largest_product if __name__ == "__main__": print(F"""{solution() = }""")
666
0
'''simple docstring''' import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = { "facebook/wav2vec2-base-960h": "https://huggingface.co/facebook/wav2vec2-base-960h/resolve/main/config.json", # See all Wav2Vec2 models at https://huggingface.co/models?filter=wav2vec2 } class SCREAMING_SNAKE_CASE ( _UpperCAmelCase ): snake_case__ = '''wav2vec2''' def __init__( self : Any , __SCREAMING_SNAKE_CASE : str=32 , __SCREAMING_SNAKE_CASE : Dict=768 , __SCREAMING_SNAKE_CASE : int=12 , __SCREAMING_SNAKE_CASE : Tuple=12 , __SCREAMING_SNAKE_CASE : Optional[Any]=3072 , __SCREAMING_SNAKE_CASE : List[Any]="gelu" , __SCREAMING_SNAKE_CASE : int=0.1 , __SCREAMING_SNAKE_CASE : Any=0.1 , __SCREAMING_SNAKE_CASE : Optional[int]=0.1 , __SCREAMING_SNAKE_CASE : Optional[Any]=0.0 , __SCREAMING_SNAKE_CASE : Union[str, Any]=0.0 , __SCREAMING_SNAKE_CASE : List[Any]=0.1 , __SCREAMING_SNAKE_CASE : List[Any]=0.1 , __SCREAMING_SNAKE_CASE : Union[str, Any]=0.02 , __SCREAMING_SNAKE_CASE : List[str]=1e-5 , __SCREAMING_SNAKE_CASE : Optional[int]="group" , __SCREAMING_SNAKE_CASE : str="gelu" , __SCREAMING_SNAKE_CASE : Union[str, Any]=(512, 512, 512, 512, 512, 512, 512) , __SCREAMING_SNAKE_CASE : Union[str, Any]=(5, 2, 2, 2, 2, 2, 2) , __SCREAMING_SNAKE_CASE : int=(10, 3, 3, 3, 3, 2, 2) , __SCREAMING_SNAKE_CASE : str=False , __SCREAMING_SNAKE_CASE : Union[str, Any]=128 , __SCREAMING_SNAKE_CASE : str=16 , __SCREAMING_SNAKE_CASE : str=False , __SCREAMING_SNAKE_CASE : str=True , __SCREAMING_SNAKE_CASE : List[Any]=0.05 , __SCREAMING_SNAKE_CASE : List[Any]=10 , __SCREAMING_SNAKE_CASE : Any=2 , __SCREAMING_SNAKE_CASE : Optional[int]=0.0 , __SCREAMING_SNAKE_CASE : Dict=10 , __SCREAMING_SNAKE_CASE : Optional[int]=0 , __SCREAMING_SNAKE_CASE : int=320 , __SCREAMING_SNAKE_CASE : Optional[int]=2 , __SCREAMING_SNAKE_CASE : Tuple=0.1 , __SCREAMING_SNAKE_CASE : Optional[Any]=100 , __SCREAMING_SNAKE_CASE : Tuple=256 , __SCREAMING_SNAKE_CASE : Dict=256 , __SCREAMING_SNAKE_CASE : Union[str, Any]=0.1 , __SCREAMING_SNAKE_CASE : Tuple="sum" , __SCREAMING_SNAKE_CASE : Any=False , __SCREAMING_SNAKE_CASE : List[str]=False , __SCREAMING_SNAKE_CASE : Union[str, Any]=256 , __SCREAMING_SNAKE_CASE : Optional[Any]=(512, 512, 512, 512, 1500) , __SCREAMING_SNAKE_CASE : List[Any]=(5, 3, 3, 1, 1) , __SCREAMING_SNAKE_CASE : Dict=(1, 2, 3, 1, 1) , __SCREAMING_SNAKE_CASE : Tuple=512 , __SCREAMING_SNAKE_CASE : List[str]=0 , __SCREAMING_SNAKE_CASE : Any=1 , __SCREAMING_SNAKE_CASE : Dict=2 , __SCREAMING_SNAKE_CASE : Optional[Any]=False , __SCREAMING_SNAKE_CASE : List[Any]=3 , __SCREAMING_SNAKE_CASE : str=2 , __SCREAMING_SNAKE_CASE : Tuple=3 , __SCREAMING_SNAKE_CASE : Dict=None , __SCREAMING_SNAKE_CASE : Tuple=None , **__SCREAMING_SNAKE_CASE : Any , ) -> Union[str, Any]: super().__init__(**A_ , pad_token_id=A_ , bos_token_id=A_ , eos_token_id=A_ ) a_ : List[Any] = hidden_size a_ : Optional[Any] = feat_extract_norm a_ : List[Any] = feat_extract_activation a_ : int = list(A_ ) a_ : List[Any] = list(A_ ) a_ : Optional[Any] = list(A_ ) a_ : Union[str, Any] = conv_bias a_ : List[Any] = num_conv_pos_embeddings a_ : Union[str, Any] = num_conv_pos_embedding_groups a_ : Tuple = len(self.conv_dim ) a_ : Dict = num_hidden_layers a_ : str = intermediate_size a_ : Optional[int] = hidden_act a_ : Optional[int] = num_attention_heads a_ : Optional[int] = hidden_dropout a_ : int = attention_dropout a_ : List[str] = activation_dropout a_ : List[Any] = feat_proj_dropout a_ : List[Any] = final_dropout a_ : str = layerdrop a_ : Optional[Any] = layer_norm_eps a_ : List[Any] = initializer_range a_ : Optional[Any] = vocab_size a_ : Optional[int] = do_stable_layer_norm a_ : Optional[int] = use_weighted_layer_sum if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==''' ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =''' f' {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,' f' `len(config.conv_kernel) = {len(self.conv_kernel )}`.' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 a_ : List[str] = apply_spec_augment a_ : List[str] = mask_time_prob a_ : List[str] = mask_time_length a_ : int = mask_time_min_masks a_ : Optional[int] = mask_feature_prob a_ : Optional[int] = mask_feature_length a_ : Optional[int] = mask_feature_min_masks # parameters for pretraining with codevector quantized representations a_ : Union[str, Any] = num_codevectors_per_group a_ : List[str] = num_codevector_groups a_ : List[Any] = contrastive_logits_temperature a_ : Optional[int] = feat_quantizer_dropout a_ : Optional[Any] = num_negatives a_ : List[Any] = codevector_dim a_ : str = proj_codevector_dim a_ : int = diversity_loss_weight # ctc loss a_ : int = ctc_loss_reduction a_ : int = ctc_zero_infinity # adapter a_ : Dict = add_adapter a_ : str = adapter_kernel_size a_ : int = adapter_stride a_ : str = num_adapter_layers a_ : str = output_hidden_size or hidden_size a_ : Dict = adapter_attn_dim # SequenceClassification-specific parameter. Feel free to ignore for other classes. a_ : Dict = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. a_ : List[Any] = list(A_ ) a_ : Any = list(A_ ) a_ : Tuple = list(A_ ) a_ : Tuple = xvector_output_dim @property def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> str: return functools.reduce(operator.mul , self.conv_stride , 1 )
705
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( __A : list[int] ): a_ : int = len(__A ) // 2 # choose the middle 3 elements a_ : Dict = lst[m - 1 : m + 2] # if middle element is peak if three[1] > three[0] and three[1] > three[2]: return three[1] # if increasing, recurse on right elif three[0] < three[2]: if len(lst[:m] ) == 2: m -= 1 return peak(lst[m:] ) # decreasing else: if len(lst[:m] ) == 2: m += 1 return peak(lst[:m] ) if __name__ == "__main__": import doctest doctest.testmod()
666
0
'''simple docstring''' def _UpperCAmelCase ( __A : int ): a_ : Any = 1 for i in range(1 , num + 1 ): fact *= i return fact def _UpperCAmelCase ( __A : int ): a_ : Optional[int] = 0 while number > 0: a_ : str = number % 10 sum_of_digits += last_digit a_ : str = number // 10 # Removing the last_digit from the given number return sum_of_digits def _UpperCAmelCase ( __A : int = 1_00 ): a_ : int = factorial(_lowerCamelCase ) a_ : Optional[int] = split_and_add(_lowerCamelCase ) return result if __name__ == "__main__": print(solution(int(input('''Enter the Number: ''').strip())))
706
'''simple docstring''' 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 SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = LongformerTokenizer snake_case__ = True snake_case__ = LongformerTokenizerFast snake_case__ = True def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt a_ : Tuple = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] a_ : Optional[Any] = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) a_ : Union[str, Any] = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] a_ : Any = {'''unk_token''': '''<unk>'''} a_ : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) a_ : Optional[int] = 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(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : Any , **__SCREAMING_SNAKE_CASE : Any ) -> int: kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , **__SCREAMING_SNAKE_CASE : List[Any] ) -> List[str]: kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : List[Any] ) -> Any: a_ : Union[str, Any] = '''lower newer''' a_ : List[Any] = '''lower newer''' return input_text, output_text def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: a_ : Optional[Any] = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) a_ : List[str] = '''lower newer''' a_ : str = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] a_ : Optional[int] = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) # , add_prefix_space=True) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Dict = tokens + [tokenizer.unk_token] a_ : Any = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Optional[Any]: a_ : Union[str, Any] = self.get_tokenizer() self.assertListEqual(tokenizer.encode('''Hello world!''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) , [0, 3_1414, 232, 328, 2] ) self.assertListEqual( tokenizer.encode('''Hello world! cécé herlolip 418''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) , [0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2] , ) @slow def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[Any]: a_ : Dict = self.tokenizer_class.from_pretrained('''allenai/longformer-base-4096''' ) a_ : Tuple = tokenizer.encode('''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer.encode('''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.encode( '''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.encode( '''sequence builders''' , '''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : List[str] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def SCREAMING_SNAKE_CASE ( self : str ) -> int: a_ : str = self.get_tokenizer() a_ : int = '''Encode this sequence.''' a_ : List[str] = tokenizer.byte_encoder[''' '''.encode('''utf-8''' )[0]] # Testing encoder arguments a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) tokenizer.add_special_tokens({'''bos_token''': '''<s>'''} ) a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Testing spaces after special tokens a_ : Optional[Any] = '''<mask>''' tokenizer.add_special_tokens( {'''mask_token''': AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE )} ) # mask token has a left space a_ : Optional[int] = tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) a_ : List[Any] = '''Encode <mask> sequence''' a_ : List[str] = '''Encode <mask>sequence''' a_ : int = tokenizer.encode(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = encoded.index(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer.encode(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = encoded.index(__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Union[str, Any]: pass def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Any: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): a_ : Any = self.rust_tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) a_ : Any = self.tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) a_ : str = '''A, <mask> AllenNLP sentence.''' a_ : List[Any] = tokenizer_r.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer_p.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE ) # 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'''] ) , ) a_ : str = tokenizer_r.convert_ids_to_tokens(tokens_r['''input_ids'''] ) a_ : Tuple = 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, 250, 6, 5_0264, 3823, 487, 2_1992, 3645, 4, 2] ) self.assertSequenceEqual(tokens_r['''input_ids'''] , [0, 250, 6, 5_0264, 3823, 487, 2_1992, 3645, 4, 2] ) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>'''] ) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>'''] ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[Any]: for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): a_ : Any = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) a_ : str = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state['''add_prefix_space'''] , __SCREAMING_SNAKE_CASE ) self.assertEqual(post_processor_state['''add_prefix_space'''] , __SCREAMING_SNAKE_CASE ) self.assertEqual(post_processor_state['''trim_offsets'''] , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[int]: # 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})' ): a_ : Dict = '''hello''' # `hello` is a token in the vocabulary of `pretrained_name` a_ : Union[str, Any] = f'{text_of_1_token} {text_of_1_token}' a_ : Any = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ) + 1, len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Any = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ) + 1, len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : int = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ), len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Tuple = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ), len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Union[str, Any] = 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)), # ) a_ : str = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : int = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ) + 1, 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : int = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ), 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Optional[int] = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : int = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ), 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , )
666
0
'''simple docstring''' 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 LevitImageProcessor class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def __init__( self : int , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Optional[Any]=7 , __SCREAMING_SNAKE_CASE : Optional[Any]=3 , __SCREAMING_SNAKE_CASE : Tuple=18 , __SCREAMING_SNAKE_CASE : Any=30 , __SCREAMING_SNAKE_CASE : str=400 , __SCREAMING_SNAKE_CASE : List[str]=True , __SCREAMING_SNAKE_CASE : List[Any]=None , __SCREAMING_SNAKE_CASE : List[Any]=True , __SCREAMING_SNAKE_CASE : List[str]=None , __SCREAMING_SNAKE_CASE : str=True , __SCREAMING_SNAKE_CASE : Dict=[0.5, 0.5, 0.5] , __SCREAMING_SNAKE_CASE : Dict=[0.5, 0.5, 0.5] , ) -> List[str]: a_ : int = size if size is not None else {'''shortest_edge''': 18} a_ : Any = crop_size if crop_size is not None else {'''height''': 18, '''width''': 18} a_ : int = parent a_ : Any = batch_size a_ : int = num_channels a_ : List[Any] = image_size a_ : Optional[int] = min_resolution a_ : Optional[Any] = max_resolution a_ : int = do_resize a_ : Optional[Any] = size a_ : str = do_center_crop a_ : List[Any] = crop_size a_ : str = do_normalize a_ : Tuple = image_mean a_ : List[str] = image_std def SCREAMING_SNAKE_CASE ( self : str ) -> Tuple: return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "do_center_crop": self.do_center_crop, "size": self.size, "crop_size": self.crop_size, } @require_torch @require_vision class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = LevitImageProcessor if is_vision_available() else None def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Tuple: a_ : Union[str, Any] = LevitImageProcessingTester(self ) @property def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Dict: return self.image_processor_tester.prepare_image_processor_dict() def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union[str, Any]: a_ : str = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__UpperCamelCase , '''image_mean''' ) ) self.assertTrue(hasattr(__UpperCamelCase , '''image_std''' ) ) self.assertTrue(hasattr(__UpperCamelCase , '''do_normalize''' ) ) self.assertTrue(hasattr(__UpperCamelCase , '''do_resize''' ) ) self.assertTrue(hasattr(__UpperCamelCase , '''do_center_crop''' ) ) self.assertTrue(hasattr(__UpperCamelCase , '''size''' ) ) def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Any: a_ : int = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''shortest_edge''': 18} ) self.assertEqual(image_processor.crop_size , {'''height''': 18, '''width''': 18} ) a_ : Union[str, Any] = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {'''shortest_edge''': 42} ) self.assertEqual(image_processor.crop_size , {'''height''': 84, '''width''': 84} ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> List[Any]: pass def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Tuple: # Initialize image_processing a_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images a_ : Tuple = prepare_image_inputs(self.image_processor_tester , equal_resolution=__UpperCamelCase ) for image in image_inputs: self.assertIsInstance(__UpperCamelCase , Image.Image ) # Test not batched input a_ : Dict = 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.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched a_ : Union[str, Any] = image_processing(__UpperCamelCase , 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.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def SCREAMING_SNAKE_CASE ( self : str ) -> Tuple: # Initialize image_processing a_ : Dict = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors a_ : List[str] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__UpperCamelCase , numpify=__UpperCamelCase ) for image in image_inputs: self.assertIsInstance(__UpperCamelCase , np.ndarray ) # Test not batched input a_ : 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.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched a_ : int = image_processing(__UpperCamelCase , 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.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Optional[Any]: # Initialize image_processing a_ : Union[str, Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors a_ : Union[str, Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__UpperCamelCase , torchify=__UpperCamelCase ) for image in image_inputs: self.assertIsInstance(__UpperCamelCase , torch.Tensor ) # Test not batched input a_ : 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.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched a_ : Optional[int] = image_processing(__UpperCamelCase , 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.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , )
707
'''simple docstring''' import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = {'vocab_file': 'sentencepiece.bpe.model'} __lowerCAmelCase = { 'vocab_file': { 'moussaKam/mbarthez': 'https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model', 'moussaKam/barthez': 'https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model', 'moussaKam/barthez-orangesum-title': ( 'https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model' ), }, } __lowerCAmelCase = { 'moussaKam/mbarthez': 1_024, 'moussaKam/barthez': 1_024, 'moussaKam/barthez-orangesum-title': 1_024, } __lowerCAmelCase = '▁' class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = VOCAB_FILES_NAMES snake_case__ = PRETRAINED_VOCAB_FILES_MAP snake_case__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case__ = ["input_ids", "attention_mask"] def __init__( self : Dict , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Dict="<s>" , __SCREAMING_SNAKE_CASE : List[Any]="</s>" , __SCREAMING_SNAKE_CASE : List[str]="</s>" , __SCREAMING_SNAKE_CASE : List[str]="<s>" , __SCREAMING_SNAKE_CASE : Dict="<unk>" , __SCREAMING_SNAKE_CASE : int="<pad>" , __SCREAMING_SNAKE_CASE : Tuple="<mask>" , __SCREAMING_SNAKE_CASE : Optional[Dict[str, Any]] = None , **__SCREAMING_SNAKE_CASE : Optional[Any] , ) -> None: # Mask token behave like a normal word, i.e. include the space before it a_ : Tuple = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else mask_token a_ : List[str] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , sp_model_kwargs=self.sp_model_kwargs , **__SCREAMING_SNAKE_CASE , ) a_ : Tuple = vocab_file a_ : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(__SCREAMING_SNAKE_CASE ) ) a_ : Optional[Any] = {'''<s>''': 0, '''<pad>''': 1, '''</s>''': 2, '''<unk>''': 3} a_ : Any = len(self.sp_model ) - 1 a_ : Optional[Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None ) -> List[int]: if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] a_ : List[str] = [self.cls_token_id] a_ : Optional[Any] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None , __SCREAMING_SNAKE_CASE : bool = False ) -> List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__SCREAMING_SNAKE_CASE , token_ids_a=__SCREAMING_SNAKE_CASE , already_has_special_tokens=__SCREAMING_SNAKE_CASE ) if token_ids_a is None: return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1, 1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None ) -> List[int]: a_ : List[str] = [self.sep_token_id] a_ : Any = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def SCREAMING_SNAKE_CASE ( self : Any ) -> Union[str, Any]: return len(self.sp_model ) def SCREAMING_SNAKE_CASE ( self : str ) -> Tuple: a_ : int = {self.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def SCREAMING_SNAKE_CASE ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : str ) -> List[str]: return self.sp_model.encode(__SCREAMING_SNAKE_CASE , out_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : List[Any] ) -> Dict: if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] a_ : Optional[int] = self.sp_model.PieceToId(__SCREAMING_SNAKE_CASE ) return spm_id if spm_id else self.unk_token_id def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : int ) -> str: if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Optional[int]: a_ : Dict = [] a_ : List[Any] = '''''' a_ : Dict = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) + token a_ : Dict = True a_ : Optional[Any] = [] else: current_sub_tokens.append(__SCREAMING_SNAKE_CASE ) a_ : Tuple = False out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) return out_string.strip() def __getstate__( self : Dict ) -> int: a_ : Dict = self.__dict__.copy() a_ : List[str] = None return state def __setstate__( self : Dict , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Any: a_ : Optional[Any] = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): a_ : Union[str, Any] = {} a_ : int = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Optional[str] = None ) -> Tuple[str]: if not os.path.isdir(__SCREAMING_SNAKE_CASE ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return a_ : Union[str, Any] = os.path.join( __SCREAMING_SNAKE_CASE , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__SCREAMING_SNAKE_CASE ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __SCREAMING_SNAKE_CASE ) elif not os.path.isfile(self.vocab_file ): with open(__SCREAMING_SNAKE_CASE , '''wb''' ) as fi: a_ : Any = self.sp_model.serialized_model_proto() fi.write(__SCREAMING_SNAKE_CASE ) return (out_vocab_file,)
666
0
'''simple docstring''' import math def _UpperCAmelCase ( __A : int ): a_ : Tuple = 0 a_ : List[Any] = 0 while num > 0: a_ : Tuple = num % 8 a_ : int = octal + (remainder * math.floor(math.pow(10 , __A ) )) counter += 1 a_ : Dict = math.floor(num / 8 ) # basically /= 8 without remainder if any # This formatting removes trailing '.0' from `octal`. return f'0o{int(__A )}' def _UpperCAmelCase ( ): print('''\n2 in octal is:''' ) print(decimal_to_octal(2 ) ) # = 2 print('''\n8 in octal is:''' ) print(decimal_to_octal(8 ) ) # = 10 print('''\n65 in octal is:''' ) print(decimal_to_octal(65 ) ) # = 101 print('''\n216 in octal is:''' ) print(decimal_to_octal(2_16 ) ) # = 330 print('''\n512 in octal is:''' ) print(decimal_to_octal(5_12 ) ) # = 1000 print('''\n''' ) if __name__ == "__main__": main()
708
'''simple docstring''' def _UpperCAmelCase ( __A : str , __A : str ): def get_matched_characters(__A : str , __A : str ) -> str: a_ : Union[str, Any] = [] a_ : int = min(len(_stra ) , len(_stra ) ) // 2 for i, l in enumerate(_stra ): a_ : Any = int(max(0 , i - limit ) ) a_ : Union[str, Any] = int(min(i + limit + 1 , len(_stra ) ) ) if l in _stra[left:right]: matched.append(__A ) a_ : Any = f'{_stra[0:_stra.index(__A )]} {_stra[_stra.index(__A ) + 1:]}' return "".join(__A ) # matching characters a_ : Optional[Any] = get_matched_characters(__A , __A ) a_ : int = get_matched_characters(__A , __A ) a_ : Any = len(__A ) # transposition a_ : List[Any] = ( len([(ca, ca) for ca, ca in zip(__A , __A ) if ca != ca] ) // 2 ) if not match_count: a_ : Dict = 0.0 else: a_ : Optional[int] = ( 1 / 3 * ( match_count / len(__A ) + match_count / len(__A ) + (match_count - transpositions) / match_count ) ) # common prefix up to 4 characters a_ : List[str] = 0 for ca, ca in zip(stra[:4] , stra[:4] ): if ca == ca: prefix_len += 1 else: break return jaro + 0.1 * prefix_len * (1 - jaro) if __name__ == "__main__": import doctest doctest.testmod() print(jaro_winkler('hello', 'world'))
666
0
'''simple docstring''' from dataclasses import dataclass, field from typing import Optional from transformers import AutoConfig, AutoImageProcessor, AutoTokenizer, FlaxVisionEncoderDecoderModel, HfArgumentParser @dataclass class SCREAMING_SNAKE_CASE : snake_case__ = field( metadata={"help": "The output directory where the model will be written."} , ) snake_case__ = field( metadata={ "help": ( "The encoder model checkpoint for weights initialization." "Don't set if you want to train an encoder model from scratch." ) } , ) snake_case__ = field( metadata={ "help": ( "The decoder model checkpoint for weights initialization." "Don't set if you want to train a decoder model from scratch." ) } , ) snake_case__ = field( default=__A , metadata={"help": "Pretrained encoder config name or path if not the same as encoder_model_name"} ) snake_case__ = field( default=__A , metadata={"help": "Pretrained decoder config name or path if not the same as decoder_model_name"} ) def _UpperCAmelCase ( ): a_ : int = HfArgumentParser((ModelArguments,) ) (a_ ) : Optional[Any] = parser.parse_args_into_dataclasses() # Load pretrained model and tokenizer # Use explicit specified encoder config if model_args.encoder_config_name: a_ : str = AutoConfig.from_pretrained(model_args.encoder_config_name ) # Use pretrained encoder model's config else: a_ : Any = AutoConfig.from_pretrained(model_args.encoder_model_name_or_path ) # Use explicit specified decoder config if model_args.decoder_config_name: a_ : Any = AutoConfig.from_pretrained(model_args.decoder_config_name ) # Use pretrained decoder model's config else: a_ : Optional[int] = AutoConfig.from_pretrained(model_args.decoder_model_name_or_path ) # necessary for `from_encoder_decoder_pretrained` when `decoder_config` is passed a_ : int = True a_ : Union[str, Any] = True a_ : Tuple = FlaxVisionEncoderDecoderModel.from_encoder_decoder_pretrained( encoder_pretrained_model_name_or_path=model_args.encoder_model_name_or_path , decoder_pretrained_model_name_or_path=model_args.decoder_model_name_or_path , encoder_config=lowerCAmelCase__ , decoder_config=lowerCAmelCase__ , ) # GPT2 only has bos/eos tokens but not decoder_start/pad tokens a_ : List[str] = decoder_config.decoder_start_token_id a_ : Union[str, Any] = decoder_config.pad_token_id if decoder_start_token_id is None: a_ : Optional[int] = decoder_config.bos_token_id if pad_token_id is None: a_ : Dict = decoder_config.eos_token_id # This is necessary to make Flax's generate() work a_ : int = decoder_config.eos_token_id a_ : str = decoder_start_token_id a_ : Optional[Any] = pad_token_id a_ : str = AutoImageProcessor.from_pretrained(model_args.encoder_model_name_or_path ) a_ : List[str] = AutoTokenizer.from_pretrained(model_args.decoder_model_name_or_path ) a_ : Any = tokenizer.convert_ids_to_tokens(model.config.pad_token_id ) model.save_pretrained(model_args.output_dir ) image_processor.save_pretrained(model_args.output_dir ) tokenizer.save_pretrained(model_args.output_dir ) if __name__ == "__main__": main()
709
'''simple docstring''' import torch from transformers import AutoModel class SCREAMING_SNAKE_CASE ( torch.nn.Module ): def __init__( self : Optional[int] , __SCREAMING_SNAKE_CASE : int="sayef/fsner-bert-base-uncased" ) -> str: super(__SCREAMING_SNAKE_CASE , self ).__init__() a_ : str = AutoModel.from_pretrained(__SCREAMING_SNAKE_CASE , return_dict=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = torch.nn.CosineSimilarity(3 , 1e-08 ) a_ : Dict = torch.nn.Softmax(dim=1 ) def SCREAMING_SNAKE_CASE ( self : str , **__SCREAMING_SNAKE_CASE : int ) -> str: return self.bert(**__SCREAMING_SNAKE_CASE ).last_hidden_state def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : List[Any] ) -> Optional[int]: return token_embeddings.sum(2 , keepdim=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : int=1 ) -> Dict: return self.softmax(T * self.cos(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[int] ) -> Any: a_ : Dict = W_supports['''sizes'''].tolist() a_ : Tuple = W_supports['''start_token_id'''].item() a_ : List[Any] = W_supports['''end_token_id'''].item() del W_supports["sizes"] del W_supports["start_token_id"] del W_supports["end_token_id"] a_ : int = self.BERT(**__SCREAMING_SNAKE_CASE ) a_ : Any = self.BERT(**__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = None a_ : Tuple = None a_ : List[str] = W_supports['''input_ids'''] == start_token_id a_ : Dict = W_supports['''input_ids'''] == end_token_id for i, size in enumerate(__SCREAMING_SNAKE_CASE ): if i == 0: a_ : str = 0 else: a_ : str = support_sizes[i - 1] a_ : Union[str, Any] = S[s : s + size][start_token_masks[s : s + size]] a_ : Tuple = S[s : s + size][end_token_masks[s : s + size]] a_ : Tuple = torch.matmul(q[i] , s_start.T ).sum(1 ).softmax(0 ) a_ : Optional[Any] = torch.matmul(q[i] , s_end.T ).sum(1 ).softmax(0 ) if p_starts is not None: a_ : Any = torch.vstack((p_starts, p_start) ) a_ : Dict = torch.vstack((p_ends, p_end) ) else: a_ : Optional[int] = p_start a_ : List[Any] = p_end return p_starts, p_ends
666
0
'''simple docstring''' import logging import sys from dataclasses import dataclass, field from typing import Any, Dict, List, Optional, Union import librosa import torch from datasets import DatasetDict, load_dataset from packaging import version from torch import nn from transformers import ( HfArgumentParser, Trainer, TrainingArguments, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaForPreTraining, is_apex_available, trainer_utils, ) from transformers.models.wavaveca.modeling_wavaveca import _compute_mask_indices if is_apex_available(): from apex import amp if version.parse(version.parse(torch.__version__).base_version) >= version.parse('1.6'): __lowerCAmelCase = True from torch.cuda.amp import autocast __lowerCAmelCase = logging.getLogger(__name__) @dataclass class SCREAMING_SNAKE_CASE : snake_case__ = field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} ) snake_case__ = field( default=a__ , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) snake_case__ = field( default=a__ , metadata={"help": "Whether to freeze the feature extractor layers of the model."} ) snake_case__ = field( default=a__ , metadata={"help": "Whether to log verbose messages or not."} , ) snake_case__ = field( default=2.0 , metadata={"help": "Maximum temperature for gumbel softmax."} ) snake_case__ = field( default=0.5 , metadata={"help": "Minimum temperature for gumbel softmax."} ) snake_case__ = field( default=0.999_995 , metadata={"help": "Decay of gumbel temperature during training."} ) def _UpperCAmelCase ( __A : Dict , __A : Any ): logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' , datefmt='''%m/%d/%Y %H:%M:%S''' , handlers=[logging.StreamHandler(sys.stdout )] , ) a_ : Union[str, Any] = logging.WARNING if model_args.verbose_logging: a_ : str = logging.DEBUG elif trainer_utils.is_main_process(training_args.local_rank ): a_ : Any = logging.INFO logger.setLevel(__A ) @dataclass class SCREAMING_SNAKE_CASE : snake_case__ = field( default=a__ , metadata={"help": "The name of the dataset to use (via the datasets library)."} ) snake_case__ = field( default=a__ , metadata={"help": "The configuration name of the dataset to use (via the datasets library)."} ) snake_case__ = field( default="train" , metadata={ "help": "The name of the training data set split to use (via the datasets library). Defaults to 'train'" } , ) snake_case__ = field( default="validation" , metadata={ "help": ( "The name of the validation data set split to use (via the datasets library). Defaults to 'validation'" ) } , ) snake_case__ = field( default="file" , metadata={"help": "Column in the dataset that contains speech file path. Defaults to 'file'"} , ) snake_case__ = field( default=a__ , metadata={"help": "Overwrite the cached preprocessed datasets or not."} ) snake_case__ = field( default=1 , metadata={ "help": "The percentage of the train set used as validation set in case there's no validation split" } , ) snake_case__ = field( default=a__ , metadata={"help": "The number of processes to use for the preprocessing."} , ) snake_case__ = field( default=20.0 , metadata={"help": "Filter audio files that are longer than `max_duration_in_seconds` seconds"} ) @dataclass class SCREAMING_SNAKE_CASE : snake_case__ = 42 snake_case__ = 42 snake_case__ = "longest" snake_case__ = None snake_case__ = None def __call__( self : Optional[int] , __SCREAMING_SNAKE_CASE : List[str] ) -> Dict[str, torch.Tensor]: # reformat list to dict and set to pytorch format a_ : str = self.feature_extractor.pad( lowerCamelCase_ , max_length=self.max_length , padding=self.padding , pad_to_multiple_of=self.pad_to_multiple_of , return_tensors='''pt''' , ) a_ : int = self.model._get_feat_extract_output_lengths(batch['''input_values'''].shape[-1] ) a_ : str = batch['''input_values'''].shape[0] # make sure that no loss is computed on padded inputs if batch["attention_mask"] is not None: # compute real output lengths according to convolution formula a_ : int = self.model._get_feat_extract_output_lengths(batch['''attention_mask'''].sum(-1 ) ).to( torch.long ) a_ : Optional[Any] = torch.zeros( (batch_size, mask_indices_seq_length) , dtype=torch.long , device=batch['''input_values'''].device ) # these two operations makes sure that all values # before the output lengths indices are attended to a_ : Any = 1 a_ : Optional[int] = attention_mask.flip([-1] ).cumsum(-1 ).flip([-1] ).bool() # sample randomly masked indices a_ : Union[str, Any] = _compute_mask_indices( (batch_size, mask_indices_seq_length) , self.model.config.mask_time_prob , self.model.config.mask_time_length , attention_mask=lowerCamelCase_ , min_masks=2 , ) return batch class SCREAMING_SNAKE_CASE ( a__ ): def __init__( self : str , *__SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : List[Any]=1 , __SCREAMING_SNAKE_CASE : int=0 , __SCREAMING_SNAKE_CASE : Any=1.0 , **__SCREAMING_SNAKE_CASE : Tuple ) -> List[Any]: super().__init__(*lowerCamelCase_ , **lowerCamelCase_ ) a_ : Dict = 0 a_ : Union[str, Any] = max_gumbel_temp a_ : str = min_gumbel_temp a_ : Dict = gumbel_temp_decay def SCREAMING_SNAKE_CASE ( self : Tuple , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Dict ) -> torch.Tensor: model.train() a_ : Dict = self._prepare_inputs(lowerCamelCase_ ) if self.use_amp: with autocast(): a_ : Dict = self.compute_loss(lowerCamelCase_ , lowerCamelCase_ ) else: a_ : List[str] = self.compute_loss(lowerCamelCase_ , lowerCamelCase_ ) if self.args.n_gpu > 1 or self.deepspeed: if model.module.config.ctc_loss_reduction == "mean": a_ : List[Any] = loss.mean() elif model.module.config.ctc_loss_reduction == "sum": a_ : List[Any] = loss.sum() / (inputs['''mask_time_indices''']).sum() else: raise ValueError(f'{model.config.ctc_loss_reduction} is not valid. Choose one of [\'mean\', \'sum\']' ) if self.args.gradient_accumulation_steps > 1: a_ : Tuple = loss / self.args.gradient_accumulation_steps if self.use_amp: self.scaler.scale(lowerCamelCase_ ).backward() elif self.use_apex: with amp.scale_loss(lowerCamelCase_ , self.optimizer ) as scaled_loss: scaled_loss.backward() elif self.deepspeed: self.deepspeed.backward(lowerCamelCase_ ) else: loss.backward() self.num_update_step += 1 # make sure gumbel softmax temperature is decayed if self.args.n_gpu > 1 or self.deepspeed: model.module.set_gumbel_temperature( max(self.max_gumbel_temp * self.gumbel_temp_decay**self.num_update_step , self.min_gumbel_temp ) ) else: model.set_gumbel_temperature( max(self.max_gumbel_temp * self.gumbel_temp_decay**self.num_update_step , self.min_gumbel_temp ) ) return loss.detach() def _UpperCAmelCase ( ): # 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. a_ : Any = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) a_ , a_ , a_ : Dict = parser.parse_args_into_dataclasses() configure_logger(__A , __A ) # Downloading and loading a dataset from the hub. a_ : int = load_dataset(data_args.dataset_name , data_args.dataset_config_name , cache_dir=model_args.cache_dir ) if "validation" not in datasets.keys(): # make sure only "validation" and "train" keys remain" a_ : Any = DatasetDict() a_ : List[Any] = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=f'{data_args.train_split_name}[:{data_args.validation_split_percentage}%]' , cache_dir=model_args.cache_dir , ) a_ : str = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=f'{data_args.train_split_name}[{data_args.validation_split_percentage}%:]' , cache_dir=model_args.cache_dir , ) else: # make sure only "validation" and "train" keys remain" a_ : str = DatasetDict() a_ : Optional[Any] = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split='''validation''' , cache_dir=model_args.cache_dir , ) a_ : Dict = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=f'{data_args.train_split_name}' , cache_dir=model_args.cache_dir , ) # only normalized-inputs-training is supported a_ : Optional[int] = WavaVecaFeatureExtractor.from_pretrained( model_args.model_name_or_path , cache_dir=model_args.cache_dir , do_normalize=__A ) def prepare_dataset(__A : Tuple ): # check that all files have the correct sampling rate a_ , a_ : Union[str, Any] = librosa.load(batch[data_args.speech_file_column] , sr=feature_extractor.sampling_rate ) return batch # load audio files into numpy arrays a_ : int = datasets.map( __A , num_proc=data_args.preprocessing_num_workers , remove_columns=datasets['''train'''].column_names ) # filter audio files that are too long a_ : Optional[Any] = vectorized_datasets.filter( lambda __A : len(data['''speech'''] ) < int(data_args.max_duration_in_seconds * feature_extractor.sampling_rate ) ) def normalize(__A : Tuple ): return feature_extractor(batch['''speech'''] , sampling_rate=feature_extractor.sampling_rate ) # normalize and transform to `BatchFeatures` a_ : int = vectorized_datasets.map( __A , batched=__A , num_proc=data_args.preprocessing_num_workers , load_from_cache_file=not data_args.overwrite_cache , remove_columns=vectorized_datasets['''train'''].column_names , ) # pretraining is only supported for "newer" stable layer norm architecture # apply_spec_augment has to be True, mask_feature_prob has to be 0.0 a_ : int = WavaVecaConfig.from_pretrained( model_args.model_name_or_path , cache_dir=model_args.cache_dir , gradient_checkpointing=training_args.gradient_checkpointing , ) if not config.do_stable_layer_norm or config.feat_extract_norm != "layer": raise ValueError( '''PreTraining is only supported for ``config.do_stable_layer_norm=True`` and''' ''' ``config.feat_extract_norm=\'layer\'''' ) a_ : Any = WavaVecaForPreTraining(__A ) a_ : int = DataCollatorForWavaVecaPretraining(model=__A , feature_extractor=__A ) a_ : int = WavaVecaPreTrainer( model=__A , data_collator=__A , args=__A , train_dataset=vectorized_datasets['''train'''] , eval_dataset=vectorized_datasets['''validation'''] , tokenizer=__A , max_gumbel_temp=model_args.max_gumbel_temperature , min_gumbel_temp=model_args.min_gumbel_temperature , gumbel_temp_decay=model_args.gumbel_temperature_decay , ) trainer.train() if __name__ == "__main__": main()
710
'''simple docstring''' import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import ClassLabel, Features, Image from .base import TaskTemplate @dataclass(frozen=SCREAMING_SNAKE_CASE_ ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = field(default="image-classification" , metadata={"include_in_asdict_even_if_is_default": True} ) snake_case__ = Features({"image": Image()} ) snake_case__ = Features({"labels": ClassLabel} ) snake_case__ = "image" snake_case__ = "labels" def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : List[Any] ) -> Any: if self.label_column not in features: raise ValueError(f'Column {self.label_column} is not present in features.' ) if not isinstance(features[self.label_column] , __SCREAMING_SNAKE_CASE ): raise ValueError(f'Column {self.label_column} is not a ClassLabel.' ) a_ : Optional[int] = copy.deepcopy(self ) a_ : int = self.label_schema.copy() a_ : Tuple = features[self.label_column] a_ : str = label_schema return task_template @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict[str, str]: return { self.image_column: "image", self.label_column: "labels", }
666
0
'''simple docstring''' from typing import Any def _UpperCAmelCase ( __A : list , __A : list , __A : dict , __A : dict , __A : dict , ): _validation( __A , __A , __A , __A , __A , ) # Creates data structures and fill initial step a_ : Union[str, Any] = {} a_ : Tuple = {} for state in states_space: a_ : Any = observations_space[0] a_ : List[str] = ( initial_probabilities[state] * emission_probabilities[state][observation] ) a_ : Union[str, Any] = None # Fills the data structure with the probabilities of # different transitions and pointers to previous states for o in range(1 , len(__A ) ): a_ : Optional[int] = observations_space[o] a_ : Optional[Any] = observations_space[o - 1] for state in states_space: # Calculates the argmax for probability function a_ : str = '''''' a_ : int = -1 for k_state in states_space: a_ : int = ( probabilities[(k_state, prior_observation)] * transition_probabilities[k_state][state] * emission_probabilities[state][observation] ) if probability > max_probability: a_ : int = probability a_ : Optional[int] = k_state # Update probabilities and pointers dicts a_ : List[str] = ( probabilities[(arg_max, prior_observation)] * transition_probabilities[arg_max][state] * emission_probabilities[state][observation] ) a_ : Dict = arg_max # The final observation a_ : Tuple = observations_space[len(__A ) - 1] # argmax for given final observation a_ : List[str] = '''''' a_ : int = -1 for k_state in states_space: a_ : str = probabilities[(k_state, final_observation)] if probability > max_probability: a_ : Optional[Any] = probability a_ : List[str] = k_state a_ : List[str] = arg_max # Process pointers backwards a_ : Union[str, Any] = last_state a_ : str = [] for o in range(len(__A ) - 1 , -1 , -1 ): result.append(__A ) a_ : Optional[int] = pointers[previous, observations_space[o]] result.reverse() return result def _UpperCAmelCase ( __A : Any , __A : Any , __A : Any , __A : Any , __A : Any , ): _validate_not_empty( __A , __A , __A , __A , __A , ) _validate_lists(__A , __A ) _validate_dicts( __A , __A , __A ) def _UpperCAmelCase ( __A : Any , __A : Any , __A : Any , __A : Any , __A : Any , ): if not all( [ observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities, ] ): raise ValueError('''There\'s an empty parameter''' ) def _UpperCAmelCase ( __A : Any , __A : Any ): _validate_list(__A , '''observations_space''' ) _validate_list(__A , '''states_space''' ) def _UpperCAmelCase ( __A : Any , __A : str ): if not isinstance(_object , __A ): a_ : List[str] = f'{var_name} must be a list' raise ValueError(__A ) else: for x in _object: if not isinstance(__A , __A ): a_ : Optional[int] = f'{var_name} must be a list of strings' raise ValueError(__A ) def _UpperCAmelCase ( __A : Any , __A : Any , __A : Any , ): _validate_dict(__A , '''initial_probabilities''' , __A ) _validate_nested_dict(__A , '''transition_probabilities''' ) _validate_nested_dict(__A , '''emission_probabilities''' ) def _UpperCAmelCase ( __A : Any , __A : str ): _validate_dict(_object , __A , __A ) for x in _object.values(): _validate_dict(__A , __A , __A , __A ) def _UpperCAmelCase ( __A : Any , __A : str , __A : type , __A : bool = False ): if not isinstance(_object , __A ): a_ : Dict = f'{var_name} must be a dict' raise ValueError(__A ) if not all(isinstance(__A , __A ) for x in _object ): a_ : List[str] = f'{var_name} all keys must be strings' raise ValueError(__A ) if not all(isinstance(__A , __A ) for x in _object.values() ): a_ : Dict = '''nested dictionary ''' if nested else '''''' a_ : int = f'{var_name} {nested_text}all values must be {value_type.__name__}' raise ValueError(__A ) if __name__ == "__main__": from doctest import testmod testmod()
711
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( __A : tuple[int, int] , __A : int ): a_ , a_ : List[str] = position a_ : Optional[int] = [ (y + 1, x + 2), (y - 1, x + 2), (y + 1, x - 2), (y - 1, x - 2), (y + 2, x + 1), (y + 2, x - 1), (y - 2, x + 1), (y - 2, x - 1), ] a_ : Any = [] for position in positions: a_ , a_ : Dict = position if 0 <= y_test < n and 0 <= x_test < n: permissible_positions.append(__A ) return permissible_positions def _UpperCAmelCase ( __A : list[list[int]] ): return not any(elem == 0 for row in board for elem in row ) def _UpperCAmelCase ( __A : list[list[int]] , __A : tuple[int, int] , __A : int ): if is_complete(__A ): return True for position in get_valid_pos(__A , len(__A ) ): a_ , a_ : Dict = position if board[y][x] == 0: a_ : Optional[Any] = curr + 1 if open_knight_tour_helper(__A , __A , curr + 1 ): return True a_ : Tuple = 0 return False def _UpperCAmelCase ( __A : int ): a_ : List[str] = [[0 for i in range(__A )] for j in range(__A )] for i in range(__A ): for j in range(__A ): a_ : Optional[Any] = 1 if open_knight_tour_helper(__A , (i, j) , 1 ): return board a_ : Union[str, Any] = 0 a_ : Dict = f'Open Kight Tour cannot be performed on a board of size {n}' raise ValueError(__A ) if __name__ == "__main__": import doctest doctest.testmod()
666
0
'''simple docstring''' import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert import BertTokenizer __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} __lowerCAmelCase = { "vocab_file": { "facebook/dpr-ctx_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt" ), "facebook/dpr-ctx_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "facebook/dpr-ctx_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json" ), "facebook/dpr-ctx_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json" ), }, } __lowerCAmelCase = { "vocab_file": { "facebook/dpr-question_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt" ), "facebook/dpr-question_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "facebook/dpr-question_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json" ), "facebook/dpr-question_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json" ), }, } __lowerCAmelCase = { "vocab_file": { "facebook/dpr-reader-single-nq-base": ( "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt" ), "facebook/dpr-reader-multiset-base": ( "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "facebook/dpr-reader-single-nq-base": ( "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json" ), "facebook/dpr-reader-multiset-base": ( "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json" ), }, } __lowerCAmelCase = { "facebook/dpr-ctx_encoder-single-nq-base": 512, "facebook/dpr-ctx_encoder-multiset-base": 512, } __lowerCAmelCase = { "facebook/dpr-question_encoder-single-nq-base": 512, "facebook/dpr-question_encoder-multiset-base": 512, } __lowerCAmelCase = { "facebook/dpr-reader-single-nq-base": 512, "facebook/dpr-reader-multiset-base": 512, } __lowerCAmelCase = { "facebook/dpr-ctx_encoder-single-nq-base": {"do_lower_case": True}, "facebook/dpr-ctx_encoder-multiset-base": {"do_lower_case": True}, } __lowerCAmelCase = { "facebook/dpr-question_encoder-single-nq-base": {"do_lower_case": True}, "facebook/dpr-question_encoder-multiset-base": {"do_lower_case": True}, } __lowerCAmelCase = { "facebook/dpr-reader-single-nq-base": {"do_lower_case": True}, "facebook/dpr-reader-multiset-base": {"do_lower_case": True}, } class SCREAMING_SNAKE_CASE ( __UpperCAmelCase ): snake_case__ = VOCAB_FILES_NAMES snake_case__ = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP snake_case__ = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case__ = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION class SCREAMING_SNAKE_CASE ( __UpperCAmelCase ): snake_case__ = VOCAB_FILES_NAMES snake_case__ = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP snake_case__ = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case__ = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION __lowerCAmelCase = collections.namedtuple( 'DPRSpanPrediction', ['span_score', 'relevance_score', 'doc_id', 'start_index', 'end_index', 'text'] ) __lowerCAmelCase = collections.namedtuple('DPRReaderOutput', ['start_logits', 'end_logits', 'relevance_logits']) __lowerCAmelCase = r"\n Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`.\n It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers),\n using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)`\n with the format:\n\n ```\n [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids>\n ```\n\n Args:\n questions (`str` or `List[str]`):\n The questions to be encoded. You can specify one question for many passages. In this case, the question\n will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in\n `titles` or `texts`.\n titles (`str` or `List[str]`):\n The passages titles to be encoded. This can be a string or a list of strings if there are several passages.\n texts (`str` or `List[str]`):\n The passages texts to be encoded. This can be a string or a list of strings if there are several passages.\n padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`):\n Activates and controls padding. Accepts the following values:\n\n - `True` or `'longest'`: Pad to the longest sequence in the batch (or no padding if only a single sequence\n if provided).\n - `'max_length'`: Pad to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided.\n - `False` or `'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of different\n lengths).\n truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`):\n Activates and controls truncation. Accepts the following values:\n\n - `True` or `'longest_first'`: Truncate to a maximum length specified with the argument `max_length` or to\n the maximum acceptable input length for the model if that argument is not provided. This will truncate\n token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch\n of pairs) is provided.\n - `'only_first'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the first\n sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `'only_second'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the\n second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `False` or `'do_not_truncate'` (default): No truncation (i.e., can output batch with sequence lengths\n greater than the model maximum admissible input size).\n max_length (`int`, *optional*):\n Controls the maximum length to use by one of the truncation/padding parameters.\n\n If left unset or set to `None`, this will use the predefined model maximum length if a maximum length\n is required by one of the truncation/padding parameters. If the model has no specific maximum input\n length (like XLNet) truncation/padding to a maximum length will be deactivated.\n return_tensors (`str` or [`~utils.TensorType`], *optional*):\n If set, will return tensors instead of list of python integers. Acceptable values are:\n\n - `'tf'`: Return TensorFlow `tf.constant` objects.\n - `'pt'`: Return PyTorch `torch.Tensor` objects.\n - `'np'`: Return Numpy `np.ndarray` objects.\n return_attention_mask (`bool`, *optional*):\n Whether or not to return the attention mask. If not set, will return the attention mask according to the\n specific tokenizer's default, defined by the `return_outputs` attribute.\n\n [What are attention masks?](../glossary#attention-mask)\n\n Returns:\n `Dict[str, List[List[int]]]`: A dictionary with the following keys:\n\n - `input_ids`: List of token ids to be fed to a model.\n - `attention_mask`: List of indices specifying which tokens should be attended to by the model.\n " @add_start_docstrings(__UpperCAmelCase ) class SCREAMING_SNAKE_CASE : def __call__( self : Any , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Optional[str] = None , __SCREAMING_SNAKE_CASE : Optional[str] = None , __SCREAMING_SNAKE_CASE : Union[bool, str] = False , __SCREAMING_SNAKE_CASE : Union[bool, str] = False , __SCREAMING_SNAKE_CASE : Optional[int] = None , __SCREAMING_SNAKE_CASE : Optional[Union[str, TensorType]] = None , __SCREAMING_SNAKE_CASE : Optional[bool] = None , **__SCREAMING_SNAKE_CASE : List[str] , ) -> str: if titles is None and texts is None: return super().__call__( lowerCAmelCase_ , padding=lowerCAmelCase_ , truncation=lowerCAmelCase_ , max_length=lowerCAmelCase_ , return_tensors=lowerCAmelCase_ , return_attention_mask=lowerCAmelCase_ , **lowerCAmelCase_ , ) elif titles is None or texts is None: a_ : List[str] = titles if texts is None else texts return super().__call__( lowerCAmelCase_ , lowerCAmelCase_ , padding=lowerCAmelCase_ , truncation=lowerCAmelCase_ , max_length=lowerCAmelCase_ , return_tensors=lowerCAmelCase_ , return_attention_mask=lowerCAmelCase_ , **lowerCAmelCase_ , ) a_ : Union[str, Any] = titles if not isinstance(lowerCAmelCase_ , lowerCAmelCase_ ) else [titles] a_ : Any = texts if not isinstance(lowerCAmelCase_ , lowerCAmelCase_ ) else [texts] a_ : int = len(lowerCAmelCase_ ) a_ : str = questions if not isinstance(lowerCAmelCase_ , lowerCAmelCase_ ) else [questions] * n_passages if len(lowerCAmelCase_ ) != len(lowerCAmelCase_ ): raise ValueError( f'There should be as many titles than texts but got {len(lowerCAmelCase_ )} titles and {len(lowerCAmelCase_ )} texts.' ) a_ : Union[str, Any] = super().__call__(lowerCAmelCase_ , lowerCAmelCase_ , padding=lowerCAmelCase_ , truncation=lowerCAmelCase_ )['''input_ids'''] a_ : Optional[int] = super().__call__(lowerCAmelCase_ , add_special_tokens=lowerCAmelCase_ , padding=lowerCAmelCase_ , truncation=lowerCAmelCase_ )['''input_ids'''] a_ : Tuple = { '''input_ids''': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(lowerCAmelCase_ , lowerCAmelCase_ ) ] } if return_attention_mask is not False: a_ : Optional[Any] = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) a_ : Optional[int] = attention_mask return self.pad(lowerCAmelCase_ , padding=lowerCAmelCase_ , max_length=lowerCAmelCase_ , return_tensors=lowerCAmelCase_ ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : BatchEncoding , __SCREAMING_SNAKE_CASE : DPRReaderOutput , __SCREAMING_SNAKE_CASE : int = 16 , __SCREAMING_SNAKE_CASE : int = 64 , __SCREAMING_SNAKE_CASE : int = 4 , ) -> List[Any]: a_ : Any = reader_input['''input_ids'''] a_ , a_ , a_ : Union[str, Any] = reader_output[:3] a_ : str = len(lowerCAmelCase_ ) a_ : List[Any] = sorted(range(lowerCAmelCase_ ) , reverse=lowerCAmelCase_ , key=relevance_logits.__getitem__ ) a_ : Optional[int] = [] for doc_id in sorted_docs: a_ : Tuple = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence a_ : Any = sequence_ids.index(self.sep_token_id , 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: a_ : Union[str, Any] = sequence_ids.index(self.pad_token_id ) else: a_ : int = len(lowerCAmelCase_ ) a_ : Union[str, Any] = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=lowerCAmelCase_ , top_spans=lowerCAmelCase_ , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=lowerCAmelCase_ , start_index=lowerCAmelCase_ , end_index=lowerCAmelCase_ , text=self.decode(sequence_ids[start_index : end_index + 1] ) , ) ) if len(lowerCAmelCase_ ) >= num_spans: break return nbest_spans_predictions[:num_spans] def SCREAMING_SNAKE_CASE ( self : Tuple , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int , ) -> List[Any]: a_ : str = [] for start_index, start_score in enumerate(lowerCAmelCase_ ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) a_ : int = sorted(lowerCAmelCase_ , key=lambda __SCREAMING_SNAKE_CASE : x[1] , reverse=lowerCAmelCase_ ) a_ : str = [] for (start_index, end_index), score in scores: if start_index > end_index: raise ValueError(f'Wrong span indices: [{start_index}:{end_index}]' ) a_ : Optional[int] = end_index - start_index + 1 if length > max_answer_length: raise ValueError(f'Span is too long: {length} > {max_answer_length}' ) if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(lowerCAmelCase_ ) == top_spans: break return chosen_span_intervals @add_end_docstrings(__UpperCAmelCase ) class SCREAMING_SNAKE_CASE ( __UpperCAmelCase , __UpperCAmelCase ): snake_case__ = VOCAB_FILES_NAMES snake_case__ = READER_PRETRAINED_VOCAB_FILES_MAP snake_case__ = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case__ = READER_PRETRAINED_INIT_CONFIGURATION snake_case__ = ["input_ids", "attention_mask"]
712
'''simple docstring''' import warnings warnings.warn( 'memory_utils has been reorganized to utils.memory. Import `find_executable_batchsize` from the main `__init__`: ' '`from accelerate import find_executable_batch_size` to avoid this warning.', FutureWarning, )
666
0
'''simple docstring''' import argparse import json import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils.deepspeed import DummyOptim, DummyScheduler __lowerCAmelCase = 16 __lowerCAmelCase = 32 def _UpperCAmelCase ( __A : Accelerator , __A : int = 16 , __A : str = "bert-base-cased" ): a_ : Union[str, Any] = AutoTokenizer.from_pretrained(a_ ) a_ : Optional[int] = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(__A : List[Any] ): # max_length=None => use the model max length (it's actually the default) a_ : Tuple = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=a_ , max_length=a_ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset a_ : Optional[int] = datasets.map( a_ , batched=a_ , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , load_from_cache_file=a_ ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library a_ : Any = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(__A : Dict ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(a_ , padding='''max_length''' , max_length=1_28 , return_tensors='''pt''' ) return tokenizer.pad(a_ , padding='''longest''' , return_tensors='''pt''' ) # Instantiate dataloaders. a_ : int = DataLoader( tokenized_datasets['''train'''] , shuffle=a_ , collate_fn=a_ , batch_size=a_ ) a_ : int = DataLoader( tokenized_datasets['''validation'''] , shuffle=a_ , collate_fn=a_ , batch_size=a_ ) return train_dataloader, eval_dataloader def _UpperCAmelCase ( __A : Optional[Any] , __A : Union[str, Any] ): # Initialize accelerator a_ : str = Accelerator() # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs a_ : Optional[int] = config['''lr'''] a_ : Optional[int] = int(config['''num_epochs'''] ) a_ : Dict = int(config['''seed'''] ) a_ : str = int(config['''batch_size'''] ) a_ : List[str] = args.model_name_or_path set_seed(a_ ) a_ : List[Any] = get_dataloaders(a_ , a_ , a_ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) a_ : List[str] = AutoModelForSequenceClassification.from_pretrained(a_ , return_dict=a_ ) # Instantiate optimizer a_ : Tuple = ( AdamW if accelerator.state.deepspeed_plugin is None or '''optimizer''' not in accelerator.state.deepspeed_plugin.deepspeed_config else DummyOptim ) a_ : Any = optimizer_cls(params=model.parameters() , lr=a_ ) if accelerator.state.deepspeed_plugin is not None: a_ : int = accelerator.state.deepspeed_plugin.deepspeed_config[ '''gradient_accumulation_steps''' ] else: a_ : Dict = 1 a_ : List[Any] = (len(a_ ) * num_epochs) // gradient_accumulation_steps # Instantiate scheduler if ( accelerator.state.deepspeed_plugin is None or "scheduler" not in accelerator.state.deepspeed_plugin.deepspeed_config ): a_ : Optional[int] = get_linear_schedule_with_warmup( optimizer=a_ , num_warmup_steps=0 , num_training_steps=a_ , ) else: a_ : Optional[int] = DummyScheduler(a_ , total_num_steps=a_ , warmup_num_steps=0 ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. a_ : Tuple = accelerator.prepare( a_ , a_ , a_ , a_ , a_ ) # We need to keep track of how many total steps we have iterated over a_ : Tuple = 0 # We also need to keep track of the stating epoch so files are named properly a_ : Tuple = 0 # Now we train the model a_ : Tuple = evaluate.load('''glue''' , '''mrpc''' ) a_ : Optional[Any] = 0 a_ : Tuple = {} for epoch in range(a_ , a_ ): model.train() for step, batch in enumerate(a_ ): a_ : Any = model(**a_ ) a_ : int = outputs.loss a_ : List[Any] = loss / gradient_accumulation_steps accelerator.backward(a_ ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 model.eval() a_ : str = 0 for step, batch in enumerate(a_ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): a_ : int = model(**a_ ) a_ : List[Any] = outputs.logits.argmax(dim=-1 ) # It is slightly faster to call this once, than multiple times a_ : Optional[Any] = accelerator.gather( (predictions, batch['''labels''']) ) # If we are in a multiprocess environment, the last batch has duplicates if accelerator.use_distributed: if step == len(a_ ) - 1: a_ : Any = predictions[: len(eval_dataloader.dataset ) - samples_seen] a_ : Any = references[: len(eval_dataloader.dataset ) - samples_seen] else: samples_seen += references.shape[0] metric.add_batch( predictions=a_ , references=a_ , ) a_ : Tuple = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f'epoch {epoch}:' , a_ ) a_ : int = eval_metric['''accuracy'''] if best_performance < eval_metric["accuracy"]: a_ : Dict = eval_metric['''accuracy'''] if args.performance_lower_bound is not None: assert ( args.performance_lower_bound <= best_performance ), f'Best performance metric {best_performance} is lower than the lower bound {args.performance_lower_bound}' accelerator.wait_for_everyone() if accelerator.is_main_process: with open(os.path.join(args.output_dir , '''all_results.json''' ) , '''w''' ) as f: json.dump(a_ , a_ ) def _UpperCAmelCase ( ): a_ : str = argparse.ArgumentParser(description='''Simple example of training script tracking peak GPU memory usage.''' ) parser.add_argument( '''--model_name_or_path''' , type=a_ , default='''bert-base-cased''' , help='''Path to pretrained model or model identifier from huggingface.co/models.''' , required=a_ , ) parser.add_argument( '''--output_dir''' , type=a_ , default='''.''' , help='''Optional save directory where all checkpoint folders will be stored. Default is the current working directory.''' , ) parser.add_argument( '''--performance_lower_bound''' , type=a_ , default=a_ , help='''Optional lower bound for the performance metric. If set, the training will throw error when the performance metric drops below this value.''' , ) parser.add_argument( '''--num_epochs''' , type=a_ , default=3 , help='''Number of train epochs.''' , ) a_ : Optional[Any] = parser.parse_args() a_ : Optional[int] = {'''lr''': 2E-5, '''num_epochs''': args.num_epochs, '''seed''': 42, '''batch_size''': 16} training_function(a_ , a_ ) if __name__ == "__main__": main()
713
'''simple docstring''' import requests from bsa import BeautifulSoup def _UpperCAmelCase ( __A : str , __A : dict ): a_ : Tuple = BeautifulSoup(requests.get(__A , params=__A ).content , '''html.parser''' ) a_ : List[str] = soup.find('''div''' , attrs={'''class''': '''gs_ri'''} ) a_ : List[str] = div.find('''div''' , attrs={'''class''': '''gs_fl'''} ).find_all('''a''' ) return anchors[2].get_text() if __name__ == "__main__": __lowerCAmelCase = { 'title': ( 'Precisely geometry controlled microsupercapacitors for ultrahigh areal ' 'capacitance, volumetric capacitance, and energy density' ), 'journal': 'Chem. Mater.', 'volume': 30, 'pages': '3979-3990', 'year': 2_018, 'hl': 'en', } print(get_citation('https://scholar.google.com/scholar_lookup', params=params))
666
0
'''simple docstring''' import multiprocessing from typing import TYPE_CHECKING, Optional, Union from .. import Dataset, Features, config from ..formatting import query_table from ..packaged_modules.sql.sql import Sql from ..utils import logging from .abc import AbstractDatasetInputStream if TYPE_CHECKING: import sqlitea import sqlalchemy class SCREAMING_SNAKE_CASE ( _a ): def __init__( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Union[str, Any] = None , __SCREAMING_SNAKE_CASE : Tuple = None , __SCREAMING_SNAKE_CASE : int = False , **__SCREAMING_SNAKE_CASE : List[Any] , ) -> Tuple: super().__init__(features=_A , cache_dir=_A , keep_in_memory=_A , **_A ) a_ : Any = Sql( cache_dir=_A , features=_A , sql=_A , con=_A , **_A , ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> List[Any]: a_ : Any = None a_ : Optional[int] = None a_ : Optional[int] = None a_ : Union[str, Any] = None self.builder.download_and_prepare( download_config=_A , download_mode=_A , verification_mode=_A , base_path=_A , ) # Build dataset for splits a_ : Dict = self.builder.as_dataset( split='''train''' , verification_mode=_A , in_memory=self.keep_in_memory ) return dataset class SCREAMING_SNAKE_CASE : def __init__( self : Tuple , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Tuple = None , __SCREAMING_SNAKE_CASE : str = None , **__SCREAMING_SNAKE_CASE : Dict , ) -> Tuple: if num_proc is not None and num_proc <= 0: raise ValueError(f'num_proc {num_proc} must be an integer > 0.' ) a_ : Tuple = dataset a_ : Optional[Any] = name a_ : str = con a_ : Union[str, Any] = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE a_ : Dict = num_proc a_ : Union[str, Any] = to_sql_kwargs def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: a_ : List[str] = self.to_sql_kwargs.pop('''sql''' , _A ) a_ : Tuple = self.to_sql_kwargs.pop('''con''' , _A ) a_ : List[Any] = self.to_sql_kwargs.pop('''index''' , _A ) a_ : Optional[Any] = self._write(index=_A , **self.to_sql_kwargs ) return written def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> int: a_ , a_ , a_ : Union[str, Any] = args a_ : int = {**to_sql_kwargs, '''if_exists''': '''append'''} if offset > 0 else to_sql_kwargs a_ : Union[str, Any] = query_table( table=self.dataset.data , key=slice(_A , offset + self.batch_size ) , indices=self.dataset._indices , ) a_ : List[Any] = batch.to_pandas() a_ : Any = df.to_sql(self.name , self.con , index=_A , **_A ) return num_rows or len(_A ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : List[str] , **__SCREAMING_SNAKE_CASE : Dict ) -> int: a_ : Optional[int] = 0 if self.num_proc is None or self.num_proc == 1: for offset in logging.tqdm( range(0 , len(self.dataset ) , self.batch_size ) , unit='''ba''' , disable=not logging.is_progress_bar_enabled() , desc='''Creating SQL from Arrow format''' , ): written += self._batch_sql((offset, index, to_sql_kwargs) ) else: a_ , a_ : Dict = len(self.dataset ), self.batch_size with multiprocessing.Pool(self.num_proc ) as pool: for num_rows in logging.tqdm( pool.imap( self._batch_sql , [(offset, index, to_sql_kwargs) for offset in range(0 , _A , _A )] , ) , total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size , unit='''ba''' , disable=not logging.is_progress_bar_enabled() , desc='''Creating SQL from Arrow format''' , ): written += num_rows return written
714
'''simple docstring''' import argparse from torch import nn # transformers_old should correspond to branch `save_old_prophetnet_model_structure` here # original prophetnet_checkpoints are saved under `patrickvonplaten/..._old` respectively from transformers_old.modeling_prophetnet import ( ProphetNetForConditionalGeneration as ProphetNetForConditionalGenerationOld, ) from transformers_old.modeling_xlm_prophetnet import ( XLMProphetNetForConditionalGeneration as XLMProphetNetForConditionalGenerationOld, ) from transformers import ProphetNetForConditionalGeneration, XLMProphetNetForConditionalGeneration, logging __lowerCAmelCase = logging.get_logger(__name__) logging.set_verbosity_info() def _UpperCAmelCase ( __A : str , __A : str ): if "xprophetnet" in prophetnet_checkpoint_path: a_ : Tuple = XLMProphetNetForConditionalGenerationOld.from_pretrained(__A ) a_ , a_ : Optional[Any] = XLMProphetNetForConditionalGeneration.from_pretrained( __A , output_loading_info=__A ) else: a_ : List[Any] = ProphetNetForConditionalGenerationOld.from_pretrained(__A ) a_ , a_ : Any = ProphetNetForConditionalGeneration.from_pretrained( __A , output_loading_info=__A ) a_ : str = ['''key_proj''', '''value_proj''', '''query_proj'''] a_ : Tuple = { '''self_attn''': '''ngram_self_attn''', '''cross_attn''': '''encoder_attn''', '''cross_attn_layer_norm''': '''encoder_attn_layer_norm''', '''feed_forward_layer_norm''': '''final_layer_norm''', '''feed_forward''': '''''', '''intermediate''': '''fc1''', '''output''': '''fc2''', '''key_proj''': '''k_proj''', '''query_proj''': '''q_proj''', '''value_proj''': '''v_proj''', '''word_embeddings''': '''embed_tokens''', '''embeddings_layer_norm''': '''emb_layer_norm''', '''relative_pos_embeddings''': '''relative_linear''', '''ngram_embeddings''': '''ngram_input_embed''', '''position_embeddings''': '''embed_positions''', } for key in loading_info["missing_keys"]: a_ : List[str] = key.split('''.''' ) if attributes[0] == "lm_head": a_ : List[str] = prophet a_ : Dict = prophet_old else: a_ : str = prophet.prophetnet a_ : int = prophet_old.model a_ : str = False for attribute in attributes: if attribute in mapping: a_ : Dict = mapping[attribute] if not hasattr(__A , __A ) and len(__A ) > 0: a_ : List[str] = attribute elif hasattr(__A , __A ): a_ : Union[str, Any] = attribute if attribute == "weight": assert old_model.weight.shape == model.weight.shape, "Shapes have to match!" a_ : Tuple = old_model.weight logger.info(f'{attribute} is initialized.' ) a_ : Union[str, Any] = True break elif attribute == "bias": assert old_model.bias.shape == model.bias.shape, "Shapes have to match!" a_ : Union[str, Any] = old_model.bias logger.info(f'{attribute} is initialized' ) a_ : Dict = True break elif attribute in special_keys and hasattr(__A , '''in_proj_weight''' ): a_ : Tuple = old_model.in_proj_weight.shape[0] // 3 a_ : Any = getattr(__A , __A ) param.weight.shape == old_model.in_proj_weight[:embed_dim, :].shape, "Shapes have to match" param.bias.shape == old_model.in_proj_bias[:embed_dim].shape, "Shapes have to match" if attribute == "query_proj": a_ : Union[str, Any] = nn.Parameter(old_model.in_proj_weight[:embed_dim, :] ) a_ : Optional[Any] = nn.Parameter(old_model.in_proj_bias[:embed_dim] ) elif attribute == "key_proj": a_ : List[Any] = nn.Parameter(old_model.in_proj_weight[embed_dim : 2 * embed_dim, :] ) a_ : Optional[int] = nn.Parameter(old_model.in_proj_bias[embed_dim : 2 * embed_dim] ) elif attribute == "value_proj": a_ : Tuple = nn.Parameter(old_model.in_proj_weight[2 * embed_dim :, :] ) a_ : Any = nn.Parameter(old_model.in_proj_bias[2 * embed_dim :] ) a_ : Dict = True break elif attribute == "position_embeddings": assert ( model.position_embeddings.weight.shape[-1] == old_model.embed_positions.weight.shape[-1] ), "Hidden size has to match" assert model.position_embeddings.weight.shape[0] == 5_12, "We want 512 position_embeddings." a_ : Union[str, Any] = nn.Parameter(old_model.embed_positions.weight[:5_12, :] ) a_ : Optional[Any] = True break if attribute.isdigit(): a_ : Union[str, Any] = model[int(__A )] a_ : str = old_model[int(__A )] else: a_ : Tuple = getattr(__A , __A ) if old_attribute == "": a_ : List[str] = old_model else: if not hasattr(__A , __A ): raise ValueError(f'{old_model} does not have {old_attribute}' ) a_ : Optional[Any] = getattr(__A , __A ) if not is_key_init: raise ValueError(f'{key} was not correctly initialized!' ) print(f'Saving model to {pytorch_dump_folder_path}' ) prophet.save_pretrained(__A ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '--prophetnet_checkpoint_path', default=None, type=str, required=True, help='Path the official PyTorch dump.' ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) __lowerCAmelCase = parser.parse_args() convert_prophetnet_checkpoint_to_pytorch(args.prophetnet_checkpoint_path, args.pytorch_dump_folder_path)
666
0
import argparse import io import requests import torch from omegaconf import OmegaConf from diffusers import AutoencoderKL from diffusers.pipelines.stable_diffusion.convert_from_ckpt import ( assign_to_checkpoint, conv_attn_to_linear, create_vae_diffusers_config, renew_vae_attention_paths, renew_vae_resnet_paths, ) def _UpperCAmelCase ( __A : Optional[Any] , __A : Any ): a_ : Optional[Any] = checkpoint a_ : Optional[int] = {} a_ : Union[str, Any] = vae_state_dict['''encoder.conv_in.weight'''] a_ : Optional[int] = vae_state_dict['''encoder.conv_in.bias'''] a_ : Dict = vae_state_dict['''encoder.conv_out.weight'''] a_ : Optional[int] = vae_state_dict['''encoder.conv_out.bias'''] a_ : List[str] = vae_state_dict['''encoder.norm_out.weight'''] a_ : List[Any] = vae_state_dict['''encoder.norm_out.bias'''] a_ : Any = vae_state_dict['''decoder.conv_in.weight'''] a_ : Tuple = vae_state_dict['''decoder.conv_in.bias'''] a_ : List[str] = vae_state_dict['''decoder.conv_out.weight'''] a_ : List[str] = vae_state_dict['''decoder.conv_out.bias'''] a_ : Optional[int] = vae_state_dict['''decoder.norm_out.weight'''] a_ : Tuple = vae_state_dict['''decoder.norm_out.bias'''] a_ : Dict = vae_state_dict['''quant_conv.weight'''] a_ : str = vae_state_dict['''quant_conv.bias'''] a_ : List[Any] = vae_state_dict['''post_quant_conv.weight'''] a_ : List[str] = vae_state_dict['''post_quant_conv.bias'''] # Retrieves the keys for the encoder down blocks only a_ : Any = len({'''.'''.join(layer.split('''.''' )[:3] ) for layer in vae_state_dict if '''encoder.down''' in layer} ) a_ : Any = { layer_id: [key for key in vae_state_dict if f'down.{layer_id}' in key] for layer_id in range(UpperCAmelCase__ ) } # Retrieves the keys for the decoder up blocks only a_ : Tuple = len({'''.'''.join(layer.split('''.''' )[:3] ) for layer in vae_state_dict if '''decoder.up''' in layer} ) a_ : Optional[int] = { layer_id: [key for key in vae_state_dict if f'up.{layer_id}' in key] for layer_id in range(UpperCAmelCase__ ) } for i in range(UpperCAmelCase__ ): a_ : List[str] = [key for key in down_blocks[i] if f'down.{i}' in key and f'down.{i}.downsample' not in key] if f'encoder.down.{i}.downsample.conv.weight' in vae_state_dict: a_ : Optional[int] = vae_state_dict.pop( f'encoder.down.{i}.downsample.conv.weight' ) a_ : List[Any] = vae_state_dict.pop( f'encoder.down.{i}.downsample.conv.bias' ) a_ : Union[str, Any] = renew_vae_resnet_paths(UpperCAmelCase__ ) a_ : List[str] = {'''old''': f'down.{i}.block', '''new''': f'down_blocks.{i}.resnets'} assign_to_checkpoint(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , additional_replacements=[meta_path] , config=UpperCAmelCase__ ) a_ : Optional[Any] = [key for key in vae_state_dict if '''encoder.mid.block''' in key] a_ : str = 2 for i in range(1 , num_mid_res_blocks + 1 ): a_ : Union[str, Any] = [key for key in mid_resnets if f'encoder.mid.block_{i}' in key] a_ : List[Any] = renew_vae_resnet_paths(UpperCAmelCase__ ) a_ : List[str] = {'''old''': f'mid.block_{i}', '''new''': f'mid_block.resnets.{i - 1}'} assign_to_checkpoint(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , additional_replacements=[meta_path] , config=UpperCAmelCase__ ) a_ : Any = [key for key in vae_state_dict if '''encoder.mid.attn''' in key] a_ : int = renew_vae_attention_paths(UpperCAmelCase__ ) a_ : List[str] = {'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''} assign_to_checkpoint(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , additional_replacements=[meta_path] , config=UpperCAmelCase__ ) conv_attn_to_linear(UpperCAmelCase__ ) for i in range(UpperCAmelCase__ ): a_ : List[str] = num_up_blocks - 1 - i a_ : List[str] = [ key for key in up_blocks[block_id] if f'up.{block_id}' in key and f'up.{block_id}.upsample' not in key ] if f'decoder.up.{block_id}.upsample.conv.weight' in vae_state_dict: a_ : int = vae_state_dict[ f'decoder.up.{block_id}.upsample.conv.weight' ] a_ : Union[str, Any] = vae_state_dict[ f'decoder.up.{block_id}.upsample.conv.bias' ] a_ : Optional[Any] = renew_vae_resnet_paths(UpperCAmelCase__ ) a_ : Optional[Any] = {'''old''': f'up.{block_id}.block', '''new''': f'up_blocks.{i}.resnets'} assign_to_checkpoint(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , additional_replacements=[meta_path] , config=UpperCAmelCase__ ) a_ : int = [key for key in vae_state_dict if '''decoder.mid.block''' in key] a_ : int = 2 for i in range(1 , num_mid_res_blocks + 1 ): a_ : Tuple = [key for key in mid_resnets if f'decoder.mid.block_{i}' in key] a_ : int = renew_vae_resnet_paths(UpperCAmelCase__ ) a_ : int = {'''old''': f'mid.block_{i}', '''new''': f'mid_block.resnets.{i - 1}'} assign_to_checkpoint(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , additional_replacements=[meta_path] , config=UpperCAmelCase__ ) a_ : str = [key for key in vae_state_dict if '''decoder.mid.attn''' in key] a_ : Dict = renew_vae_attention_paths(UpperCAmelCase__ ) a_ : Tuple = {'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''} assign_to_checkpoint(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , additional_replacements=[meta_path] , config=UpperCAmelCase__ ) conv_attn_to_linear(UpperCAmelCase__ ) return new_checkpoint def _UpperCAmelCase ( __A : str , __A : str , ): # Only support V1 a_ : Any = requests.get( ''' https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml''' ) a_ : str = io.BytesIO(r.content ) a_ : str = OmegaConf.load(UpperCAmelCase__ ) a_ : Optional[Any] = 5_12 a_ : Union[str, Any] = '''cuda''' if torch.cuda.is_available() else '''cpu''' if checkpoint_path.endswith('''safetensors''' ): from safetensors import safe_open a_ : List[str] = {} with safe_open(UpperCAmelCase__ , framework='''pt''' , device='''cpu''' ) as f: for key in f.keys(): a_ : Optional[int] = f.get_tensor(UpperCAmelCase__ ) else: a_ : Dict = torch.load(UpperCAmelCase__ , map_location=UpperCAmelCase__ )['''state_dict'''] # Convert the VAE model. a_ : Union[str, Any] = create_vae_diffusers_config(UpperCAmelCase__ , image_size=UpperCAmelCase__ ) a_ : List[str] = custom_convert_ldm_vae_checkpoint(UpperCAmelCase__ , UpperCAmelCase__ ) a_ : int = AutoencoderKL(**UpperCAmelCase__ ) vae.load_state_dict(UpperCAmelCase__ ) vae.save_pretrained(UpperCAmelCase__ ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() parser.add_argument('--vae_pt_path', default=None, type=str, required=True, help='Path to the VAE.pt to convert.') parser.add_argument('--dump_path', default=None, type=str, required=True, help='Path to the VAE.pt to convert.') __lowerCAmelCase = parser.parse_args() vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)
715
'''simple docstring''' import re import string import numpy as np import datasets __lowerCAmelCase = '\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n' __lowerCAmelCase = '\nArgs:\n predictions: List of predicted texts.\n references: List of reference texts.\n regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n ignore when calculating the exact matches. Note: these regexes are removed\n from the input data before the changes based on the options below (e.g. ignore_case,\n ignore_punctuation, ignore_numbers) are applied.\n ignore_case: Boolean, defaults to False. If true, turns everything\n to lowercase so that capitalization differences are ignored.\n ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\n ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\nReturns:\n exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 25.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 50.0\n\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 75.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n >>> print(round(results["exact_match"], 1))\n 100.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["The cat sat on the mat.", "Theaters are great.", "It\'s like comparing oranges and apples."]\n >>> preds = ["The cat sat on the mat?", "Theaters are great.", "It\'s like comparing apples and oranges."]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 33.3\n\n' __lowerCAmelCase = '\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class SCREAMING_SNAKE_CASE ( datasets.Metric ): def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> int: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , reference_urls=[] , ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Dict=None , __SCREAMING_SNAKE_CASE : int=False , __SCREAMING_SNAKE_CASE : Optional[int]=False , __SCREAMING_SNAKE_CASE : Dict=False , ) -> str: if regexes_to_ignore is not None: for s in regexes_to_ignore: a_ : Optional[Any] = np.array([re.sub(__SCREAMING_SNAKE_CASE , '''''' , __SCREAMING_SNAKE_CASE ) for x in predictions] ) a_ : int = np.array([re.sub(__SCREAMING_SNAKE_CASE , '''''' , __SCREAMING_SNAKE_CASE ) for x in references] ) else: a_ : List[str] = np.asarray(__SCREAMING_SNAKE_CASE ) a_ : Any = np.asarray(__SCREAMING_SNAKE_CASE ) if ignore_case: a_ : List[str] = np.char.lower(__SCREAMING_SNAKE_CASE ) a_ : List[Any] = np.char.lower(__SCREAMING_SNAKE_CASE ) if ignore_punctuation: a_ : Any = string.punctuation.maketrans('''''' , '''''' , string.punctuation ) a_ : Union[str, Any] = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : int = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) if ignore_numbers: a_ : int = string.digits.maketrans('''''' , '''''' , string.digits ) a_ : Optional[int] = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : Dict = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = predictions == references return {"exact_match": np.mean(__SCREAMING_SNAKE_CASE ) * 100}
666
0
'''simple docstring''' 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 if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ChineseCLIPImageProcessor class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def __init__( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Optional[Any]=7 , __SCREAMING_SNAKE_CASE : str=3 , __SCREAMING_SNAKE_CASE : Union[str, Any]=18 , __SCREAMING_SNAKE_CASE : List[Any]=30 , __SCREAMING_SNAKE_CASE : int=400 , __SCREAMING_SNAKE_CASE : Union[str, Any]=True , __SCREAMING_SNAKE_CASE : str=None , __SCREAMING_SNAKE_CASE : Any=True , __SCREAMING_SNAKE_CASE : Optional[int]=None , __SCREAMING_SNAKE_CASE : Dict=True , __SCREAMING_SNAKE_CASE : Tuple=[0.4814_5466, 0.457_8275, 0.4082_1073] , __SCREAMING_SNAKE_CASE : str=[0.2686_2954, 0.2613_0258, 0.2757_7711] , __SCREAMING_SNAKE_CASE : Dict=True , ) -> int: a_ : Any = size if size is not None else {'''height''': 224, '''width''': 224} a_ : List[str] = crop_size if crop_size is not None else {'''height''': 18, '''width''': 18} a_ : Dict = parent a_ : str = batch_size a_ : int = num_channels a_ : Dict = image_size a_ : Tuple = min_resolution a_ : int = max_resolution a_ : List[Any] = do_resize a_ : Union[str, Any] = size a_ : Union[str, Any] = do_center_crop a_ : str = crop_size a_ : str = do_normalize a_ : Optional[int] = image_mean a_ : Dict = image_std a_ : int = do_convert_rgb def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Optional[Any]: return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_convert_rgb": self.do_convert_rgb, } def SCREAMING_SNAKE_CASE ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[int]=False , __SCREAMING_SNAKE_CASE : Optional[Any]=False , __SCREAMING_SNAKE_CASE : Optional[int]=False ) -> Any: assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time" if equal_resolution: a_ : Tuple = [] for i in range(self.batch_size ): image_inputs.append( np.random.randint( 255 , size=(self.num_channels, self.max_resolution, self.max_resolution) , dtype=np.uinta ) ) else: a_ : Union[str, Any] = [] for i in range(self.batch_size ): a_ , a_ : Tuple = np.random.choice(np.arange(self.min_resolution , self.max_resolution ) , 2 ) image_inputs.append(np.random.randint(255 , size=(self.num_channels, width, height) , dtype=np.uinta ) ) if not numpify and not torchify: # PIL expects the channel dimension as last dimension a_ : str = [Image.fromarray(np.moveaxis(__SCREAMING_SNAKE_CASE , 0 , -1 ) ) for x in image_inputs] if torchify: a_ : List[Any] = [torch.from_numpy(__SCREAMING_SNAKE_CASE ) for x in image_inputs] return image_inputs @require_torch @require_vision class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = ChineseCLIPImageProcessor if is_vision_available() else None def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> int: a_ : int = ChineseCLIPImageProcessingTester(self , do_center_crop=__SCREAMING_SNAKE_CASE ) @property def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> int: return self.image_processor_tester.prepare_image_processor_dict() def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Optional[Any]: a_ : Any = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_resize''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''size''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_center_crop''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''center_crop''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_normalize''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''image_mean''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''image_std''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_convert_rgb''' ) ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> List[Any]: a_ : Union[str, Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 224, '''width''': 224} ) self.assertEqual(image_processor.crop_size , {'''height''': 18, '''width''': 18} ) a_ : Tuple = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {'''shortest_edge''': 42} ) self.assertEqual(image_processor.crop_size , {'''height''': 84, '''width''': 84} ) def SCREAMING_SNAKE_CASE ( self : int ) -> Dict: pass def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Union[str, Any]: a_ : Any = self.image_processing_class(**self.image_processor_dict ) # create random PIL images a_ : Optional[Any] = self.image_processor_tester.prepare_inputs(equal_resolution=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , Image.Image ) # Test not batched input a_ : Union[str, 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.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched a_ : Optional[Any] = image_processing(__SCREAMING_SNAKE_CASE , 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.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> List[str]: a_ : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors a_ : int = self.image_processor_tester.prepare_inputs(equal_resolution=__SCREAMING_SNAKE_CASE , numpify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , np.ndarray ) # Test not batched input a_ : Dict = 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.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched a_ : Optional[Any] = image_processing(__SCREAMING_SNAKE_CASE , 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.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def SCREAMING_SNAKE_CASE ( self : str ) -> int: a_ : Dict = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors a_ : Any = self.image_processor_tester.prepare_inputs(equal_resolution=__SCREAMING_SNAKE_CASE , torchify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) # Test not batched input a_ : Optional[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.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched a_ : str = image_processing(__SCREAMING_SNAKE_CASE , 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.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) @require_torch @require_vision class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = ChineseCLIPImageProcessor if is_vision_available() else None def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Any: a_ : Dict = ChineseCLIPImageProcessingTester(self , num_channels=4 , do_center_crop=__SCREAMING_SNAKE_CASE ) a_ : str = 3 @property def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> List[Any]: return self.image_processor_tester.prepare_image_processor_dict() def SCREAMING_SNAKE_CASE ( self : str ) -> int: a_ : Union[str, Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_resize''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''size''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_center_crop''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''center_crop''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_normalize''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''image_mean''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''image_std''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_convert_rgb''' ) ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> List[Any]: pass def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Tuple: a_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images a_ : List[str] = self.image_processor_tester.prepare_inputs(equal_resolution=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , Image.Image ) # Test not batched input a_ : List[str] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.expected_encoded_image_num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched a_ : Tuple = image_processing(__SCREAMING_SNAKE_CASE , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.expected_encoded_image_num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , )
716
'''simple docstring''' import gc import unittest import numpy as np import torch from diffusers import ( AudioDiffusionPipeline, AutoencoderKL, DDIMScheduler, DDPMScheduler, DiffusionPipeline, Mel, UNetaDConditionModel, UNetaDModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : Dict ) -> List[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> List[Any]: torch.manual_seed(0 ) a_ : Tuple = UNetaDModel( sample_size=(32, 64) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''') , ) return model @property def SCREAMING_SNAKE_CASE ( self : int ) -> List[Any]: torch.manual_seed(0 ) a_ : Any = UNetaDConditionModel( sample_size=(64, 32) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''CrossAttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''CrossAttnUpBlock2D''') , cross_attention_dim=10 , ) return model @property def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: torch.manual_seed(0 ) a_ : List[Any] = AutoencoderKL( sample_size=(128, 64) , in_channels=1 , out_channels=1 , latent_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''DownEncoderBlock2D''', '''DownEncoderBlock2D''') , up_block_types=('''UpDecoderBlock2D''', '''UpDecoderBlock2D''') , ) a_ : List[Any] = UNetaDModel( sample_size=(64, 32) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''') , ) return vqvae, unet @slow def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union[str, Any]: a_ : Dict = '''cpu''' # ensure determinism for the device-dependent torch.Generator a_ : Union[str, Any] = Mel( x_res=self.dummy_unet.config.sample_size[1] , y_res=self.dummy_unet.config.sample_size[0] , ) a_ : Any = DDPMScheduler() a_ : str = AudioDiffusionPipeline(vqvae=__SCREAMING_SNAKE_CASE , unet=self.dummy_unet , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : List[Any] = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : List[str] = pipe(generator=__SCREAMING_SNAKE_CASE , steps=4 ) a_ : List[Any] = output.audios[0] a_ : Dict = output.images[0] a_ : Dict = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : Optional[Any] = pipe(generator=__SCREAMING_SNAKE_CASE , steps=4 , return_dict=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output[0][0] assert audio.shape == (1, (self.dummy_unet.config.sample_size[1] - 1) * mel.hop_length) assert ( image.height == self.dummy_unet.config.sample_size[0] and image.width == self.dummy_unet.config.sample_size[1] ) a_ : Dict = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : str = np.frombuffer(image_from_tuple.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([69, 255, 255, 255, 0, 0, 77, 181, 12, 127] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() == 0 a_ : str = Mel( x_res=self.dummy_vqvae_and_unet[0].config.sample_size[1] , y_res=self.dummy_vqvae_and_unet[0].config.sample_size[0] , ) a_ : int = DDIMScheduler() a_ : Dict = self.dummy_vqvae_and_unet a_ : List[str] = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0] , unet=dummy_vqvae_and_unet[1] , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : Any = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) np.random.seed(0 ) a_ : List[str] = np.random.uniform(-1 , 1 , ((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,) ) a_ : int = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : int = pipe(raw_audio=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , start_step=5 , steps=10 ) a_ : List[str] = output.images[0] assert ( image.height == self.dummy_vqvae_and_unet[0].config.sample_size[0] and image.width == self.dummy_vqvae_and_unet[0].config.sample_size[1] ) a_ : Optional[Any] = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([120, 117, 110, 109, 138, 167, 138, 148, 132, 121] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 a_ : List[str] = self.dummy_unet_condition a_ : Dict = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0] , unet=__SCREAMING_SNAKE_CASE , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : int = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) np.random.seed(0 ) a_ : Any = torch.rand((1, 1, 10) ) a_ : Tuple = pipe(generator=__SCREAMING_SNAKE_CASE , encoding=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output.images[0] a_ : Dict = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([107, 103, 120, 127, 142, 122, 113, 122, 97, 111] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 @slow @require_torch_gpu class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Any: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Any: a_ : Any = torch_device a_ : Optional[int] = DiffusionPipeline.from_pretrained('''teticio/audio-diffusion-ddim-256''' ) a_ : Dict = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : str = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : List[str] = pipe(generator=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output.audios[0] a_ : Tuple = output.images[0] assert audio.shape == (1, (pipe.unet.config.sample_size[1] - 1) * pipe.mel.hop_length) assert image.height == pipe.unet.config.sample_size[0] and image.width == pipe.unet.config.sample_size[1] a_ : str = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : Tuple = np.array([151, 167, 154, 144, 122, 134, 121, 105, 70, 26] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0
666
0
from __future__ import annotations from scipy.special import comb # type: ignore class SCREAMING_SNAKE_CASE : def __init__( self : Any , __SCREAMING_SNAKE_CASE : Union[str, Any] ) -> int: a_ : str = list_of_points # Degree determines the flexibility of the curve. # Degree = 1 will produce a straight line. a_ : Dict = len(__SCREAMING_SNAKE_CASE ) - 1 def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Optional[int] ) -> int: assert 0 <= t <= 1, "Time t must be between 0 and 1." a_ : Any = [] for i in range(len(self.list_of_points ) ): # basis function for each i output_values.append( comb(self.degree , __SCREAMING_SNAKE_CASE ) * ((1 - t) ** (self.degree - i)) * (t**i) ) # the basis must sum up to 1 for it to produce a valid Bezier curve. assert round(sum(__SCREAMING_SNAKE_CASE ) , 5 ) == 1 return output_values def SCREAMING_SNAKE_CASE ( self : int , __SCREAMING_SNAKE_CASE : Any ) -> Dict: assert 0 <= t <= 1, "Time t must be between 0 and 1." a_ : List[str] = self.basis_function(__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = 0.0 a_ : str = 0.0 for i in range(len(self.list_of_points ) ): # For all points, sum up the product of i-th basis function and i-th point. x += basis_function[i] * self.list_of_points[i][0] y += basis_function[i] * self.list_of_points[i][1] return (x, y) def SCREAMING_SNAKE_CASE ( self : Tuple , __SCREAMING_SNAKE_CASE : str = 0.01 ) -> Union[str, Any]: from matplotlib import pyplot as plt # type: ignore a_ : Tuple = [] # x coordinates of points to plot a_ : str = [] # y coordinates of points to plot a_ : List[Any] = 0.0 while t <= 1: a_ : Dict = self.bezier_curve_function(__SCREAMING_SNAKE_CASE ) to_plot_x.append(value[0] ) to_plot_y.append(value[1] ) t += step_size a_ : int = [i[0] for i in self.list_of_points] a_ : str = [i[1] for i in self.list_of_points] plt.plot( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , color='''blue''' , label='''Curve of Degree ''' + str(self.degree ) , ) plt.scatter(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , color='''red''' , label='''Control Points''' ) plt.legend() plt.show() if __name__ == "__main__": import doctest doctest.testmod() BezierCurve([(1, 2), (3, 5)]).plot_curve() # degree 1 BezierCurve([(0, 0), (5, 5), (5, 0)]).plot_curve() # degree 2 BezierCurve([(0, 0), (5, 5), (5, 0), (2.5, -2.5)]).plot_curve() # degree 3
717
'''simple docstring''' import argparse import json from pathlib import Path import requests import torch from huggingface_hub import cached_download, hf_hub_download, hf_hub_url from PIL import Image from transformers import DetaConfig, DetaForObjectDetection, DetaImageProcessor, SwinConfig from transformers.utils import logging logging.set_verbosity_info() __lowerCAmelCase = logging.get_logger(__name__) def _UpperCAmelCase ( __A : Union[str, Any] ): a_ : Tuple = SwinConfig( embed_dim=1_92 , depths=(2, 2, 18, 2) , num_heads=(6, 12, 24, 48) , window_size=12 , out_features=['''stage2''', '''stage3''', '''stage4'''] , ) a_ : List[Any] = DetaConfig( backbone_config=__A , num_queries=9_00 , encoder_ffn_dim=20_48 , decoder_ffn_dim=20_48 , num_feature_levels=5 , assign_first_stage=__A , with_box_refine=__A , two_stage=__A , ) # set labels a_ : Optional[Any] = '''huggingface/label-files''' if "o365" in model_name: a_ : Optional[Any] = 3_66 a_ : Tuple = '''object365-id2label.json''' else: a_ : Any = 91 a_ : Union[str, Any] = '''coco-detection-id2label.json''' a_ : Tuple = num_labels a_ : str = json.load(open(cached_download(hf_hub_url(__A , __A , repo_type='''dataset''' ) ) , '''r''' ) ) a_ : Optional[int] = {int(__A ): v for k, v in idalabel.items()} a_ : int = idalabel a_ : Dict = {v: k for k, v in idalabel.items()} return config def _UpperCAmelCase ( __A : List[str] ): a_ : Tuple = [] # stem # fmt: off rename_keys.append(('''backbone.0.body.patch_embed.proj.weight''', '''model.backbone.model.embeddings.patch_embeddings.projection.weight''') ) rename_keys.append(('''backbone.0.body.patch_embed.proj.bias''', '''model.backbone.model.embeddings.patch_embeddings.projection.bias''') ) rename_keys.append(('''backbone.0.body.patch_embed.norm.weight''', '''model.backbone.model.embeddings.norm.weight''') ) rename_keys.append(('''backbone.0.body.patch_embed.norm.bias''', '''model.backbone.model.embeddings.norm.bias''') ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm1.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm1.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_bias_table', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_index', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.proj.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.proj.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm2.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm2.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.bias') ) if i < 3: rename_keys.append((f'backbone.0.body.layers.{i}.downsample.reduction.weight', f'model.backbone.model.encoder.layers.{i}.downsample.reduction.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.downsample.norm.weight', f'model.backbone.model.encoder.layers.{i}.downsample.norm.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.downsample.norm.bias', f'model.backbone.model.encoder.layers.{i}.downsample.norm.bias') ) rename_keys.append(('''backbone.0.body.norm1.weight''', '''model.backbone.model.hidden_states_norms.stage2.weight''') ) rename_keys.append(('''backbone.0.body.norm1.bias''', '''model.backbone.model.hidden_states_norms.stage2.bias''') ) rename_keys.append(('''backbone.0.body.norm2.weight''', '''model.backbone.model.hidden_states_norms.stage3.weight''') ) rename_keys.append(('''backbone.0.body.norm2.bias''', '''model.backbone.model.hidden_states_norms.stage3.bias''') ) rename_keys.append(('''backbone.0.body.norm3.weight''', '''model.backbone.model.hidden_states_norms.stage4.weight''') ) rename_keys.append(('''backbone.0.body.norm3.bias''', '''model.backbone.model.hidden_states_norms.stage4.bias''') ) # transformer encoder for i in range(config.encoder_layers ): rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.sampling_offsets.weight', f'model.encoder.layers.{i}.self_attn.sampling_offsets.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.sampling_offsets.bias', f'model.encoder.layers.{i}.self_attn.sampling_offsets.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.attention_weights.weight', f'model.encoder.layers.{i}.self_attn.attention_weights.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.attention_weights.bias', f'model.encoder.layers.{i}.self_attn.attention_weights.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.value_proj.weight', f'model.encoder.layers.{i}.self_attn.value_proj.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.value_proj.bias', f'model.encoder.layers.{i}.self_attn.value_proj.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.output_proj.weight', f'model.encoder.layers.{i}.self_attn.output_proj.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.output_proj.bias', f'model.encoder.layers.{i}.self_attn.output_proj.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm1.weight', f'model.encoder.layers.{i}.self_attn_layer_norm.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm1.bias', f'model.encoder.layers.{i}.self_attn_layer_norm.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear1.weight', f'model.encoder.layers.{i}.fc1.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear1.bias', f'model.encoder.layers.{i}.fc1.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear2.weight', f'model.encoder.layers.{i}.fc2.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear2.bias', f'model.encoder.layers.{i}.fc2.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm2.weight', f'model.encoder.layers.{i}.final_layer_norm.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm2.bias', f'model.encoder.layers.{i}.final_layer_norm.bias') ) # transformer decoder for i in range(config.decoder_layers ): rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.sampling_offsets.weight', f'model.decoder.layers.{i}.encoder_attn.sampling_offsets.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.sampling_offsets.bias', f'model.decoder.layers.{i}.encoder_attn.sampling_offsets.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.attention_weights.weight', f'model.decoder.layers.{i}.encoder_attn.attention_weights.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.attention_weights.bias', f'model.decoder.layers.{i}.encoder_attn.attention_weights.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.value_proj.weight', f'model.decoder.layers.{i}.encoder_attn.value_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.value_proj.bias', f'model.decoder.layers.{i}.encoder_attn.value_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.output_proj.weight', f'model.decoder.layers.{i}.encoder_attn.output_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.output_proj.bias', f'model.decoder.layers.{i}.encoder_attn.output_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm1.weight', f'model.decoder.layers.{i}.encoder_attn_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm1.bias', f'model.decoder.layers.{i}.encoder_attn_layer_norm.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.self_attn.out_proj.weight', f'model.decoder.layers.{i}.self_attn.out_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.self_attn.out_proj.bias', f'model.decoder.layers.{i}.self_attn.out_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm2.weight', f'model.decoder.layers.{i}.self_attn_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm2.bias', f'model.decoder.layers.{i}.self_attn_layer_norm.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear1.weight', f'model.decoder.layers.{i}.fc1.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear1.bias', f'model.decoder.layers.{i}.fc1.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear2.weight', f'model.decoder.layers.{i}.fc2.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear2.bias', f'model.decoder.layers.{i}.fc2.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm3.weight', f'model.decoder.layers.{i}.final_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm3.bias', f'model.decoder.layers.{i}.final_layer_norm.bias') ) # fmt: on return rename_keys def _UpperCAmelCase ( __A : str , __A : int , __A : Tuple ): a_ : str = dct.pop(__A ) a_ : Dict = val def _UpperCAmelCase ( __A : List[str] , __A : Optional[int] ): a_ : str = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): a_ : Tuple = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) a_ : List[str] = state_dict.pop(f'backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.weight' ) a_ : str = state_dict.pop(f'backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.bias' ) # next, add query, keys and values (in that order) to the state dict a_ : Optional[Any] = in_proj_weight[:dim, :] a_ : List[Any] = in_proj_bias[: dim] a_ : Optional[Any] = in_proj_weight[ dim : dim * 2, : ] a_ : Union[str, Any] = in_proj_bias[ dim : dim * 2 ] a_ : Optional[int] = in_proj_weight[ -dim :, : ] a_ : int = in_proj_bias[-dim :] # fmt: on def _UpperCAmelCase ( __A : Dict , __A : Dict ): # transformer decoder self-attention layers a_ : Any = config.d_model for i in range(config.decoder_layers ): # read in weights + bias of input projection layer of self-attention a_ : int = state_dict.pop(f'transformer.decoder.layers.{i}.self_attn.in_proj_weight' ) a_ : Any = state_dict.pop(f'transformer.decoder.layers.{i}.self_attn.in_proj_bias' ) # next, add query, keys and values (in that order) to the state dict a_ : Dict = in_proj_weight[:hidden_size, :] a_ : Tuple = in_proj_bias[:hidden_size] a_ : Any = in_proj_weight[ hidden_size : hidden_size * 2, : ] a_ : Tuple = in_proj_bias[hidden_size : hidden_size * 2] a_ : Optional[int] = in_proj_weight[-hidden_size:, :] a_ : int = in_proj_bias[-hidden_size:] def _UpperCAmelCase ( ): a_ : Union[str, Any] = '''http://images.cocodataset.org/val2017/000000039769.jpg''' a_ : List[str] = Image.open(requests.get(__A , stream=__A ).raw ) return im @torch.no_grad() def _UpperCAmelCase ( __A : int , __A : int , __A : Any ): a_ : Union[str, Any] = get_deta_config(__A ) # load original state dict if model_name == "deta-swin-large": a_ : Optional[Any] = hf_hub_download(repo_id='''nielsr/deta-checkpoints''' , filename='''adet_swin_ft.pth''' ) elif model_name == "deta-swin-large-o365": a_ : List[str] = hf_hub_download(repo_id='''jozhang97/deta-swin-l-o365''' , filename='''deta_swin_pt_o365.pth''' ) else: raise ValueError(f'Model name {model_name} not supported' ) a_ : List[Any] = torch.load(__A , map_location='''cpu''' )['''model'''] # original state dict for name, param in state_dict.items(): print(__A , param.shape ) # rename keys a_ : Union[str, Any] = create_rename_keys(__A ) for src, dest in rename_keys: rename_key(__A , __A , __A ) read_in_swin_q_k_v(__A , config.backbone_config ) read_in_decoder_q_k_v(__A , __A ) # fix some prefixes for key in state_dict.copy().keys(): if "transformer.decoder.class_embed" in key or "transformer.decoder.bbox_embed" in key: a_ : Optional[Any] = state_dict.pop(__A ) a_ : int = val if "input_proj" in key: a_ : str = state_dict.pop(__A ) a_ : Optional[Any] = val if "level_embed" in key or "pos_trans" in key or "pix_trans" in key or "enc_output" in key: a_ : List[str] = state_dict.pop(__A ) a_ : List[Any] = val # finally, create HuggingFace model and load state dict a_ : Dict = DetaForObjectDetection(__A ) model.load_state_dict(__A ) model.eval() a_ : int = '''cuda''' if torch.cuda.is_available() else '''cpu''' model.to(__A ) # load image processor a_ : List[Any] = DetaImageProcessor(format='''coco_detection''' ) # verify our conversion on image a_ : Dict = prepare_img() a_ : Optional[int] = processor(images=__A , return_tensors='''pt''' ) a_ : Any = encoding['''pixel_values'''] a_ : int = model(pixel_values.to(__A ) ) # verify logits print('''Logits:''' , outputs.logits[0, :3, :3] ) print('''Boxes:''' , outputs.pred_boxes[0, :3, :3] ) if model_name == "deta-swin-large": a_ : Optional[int] = torch.tensor( [[-7.6308, -2.8485, -5.3737], [-7.2037, -4.5505, -4.8027], [-7.2943, -4.2611, -4.6617]] ) a_ : Tuple = torch.tensor([[0.4987, 0.4969, 0.9999], [0.2549, 0.5498, 0.4805], [0.5498, 0.2757, 0.0569]] ) elif model_name == "deta-swin-large-o365": a_ : Union[str, Any] = torch.tensor( [[-8.0122, -3.5720, -4.9717], [-8.1547, -3.6886, -4.6389], [-7.6610, -3.6194, -5.0134]] ) a_ : Any = torch.tensor([[0.2523, 0.5549, 0.4881], [0.7715, 0.4149, 0.4601], [0.5503, 0.2753, 0.0575]] ) assert torch.allclose(outputs.logits[0, :3, :3] , expected_logits.to(__A ) , atol=1E-4 ) assert torch.allclose(outputs.pred_boxes[0, :3, :3] , expected_boxes.to(__A ) , atol=1E-4 ) print('''Everything ok!''' ) if pytorch_dump_folder_path: # Save model and processor logger.info(f'Saving PyTorch model and processor to {pytorch_dump_folder_path}...' ) Path(__A ).mkdir(exist_ok=__A ) model.save_pretrained(__A ) processor.save_pretrained(__A ) # Push to hub if push_to_hub: print('''Pushing model and processor to hub...''' ) model.push_to_hub(f'jozhang97/{model_name}' ) processor.push_to_hub(f'jozhang97/{model_name}' ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() parser.add_argument( '--model_name', type=str, default='deta-swin-large', choices=['deta-swin-large', 'deta-swin-large-o365'], help='Name of the model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the folder to output PyTorch model.', ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.' ) __lowerCAmelCase = parser.parse_args() convert_deta_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
666
0
'''simple docstring''' import unittest from diffusers.pipelines.pipeline_utils import is_safetensors_compatible class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : Any ) -> List[Any]: a_ : List[str] = [ '''safety_checker/pytorch_model.bin''', '''safety_checker/model.safetensors''', '''vae/diffusion_pytorch_model.bin''', '''vae/diffusion_pytorch_model.safetensors''', '''text_encoder/pytorch_model.bin''', '''text_encoder/model.safetensors''', '''unet/diffusion_pytorch_model.bin''', '''unet/diffusion_pytorch_model.safetensors''', ] self.assertTrue(is_safetensors_compatible(lowerCamelCase__ ) ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> List[str]: a_ : str = [ '''unet/diffusion_pytorch_model.bin''', '''unet/diffusion_pytorch_model.safetensors''', ] self.assertTrue(is_safetensors_compatible(lowerCamelCase__ ) ) def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Tuple: a_ : str = [ '''safety_checker/pytorch_model.bin''', '''safety_checker/model.safetensors''', '''vae/diffusion_pytorch_model.bin''', '''vae/diffusion_pytorch_model.safetensors''', '''text_encoder/pytorch_model.bin''', '''text_encoder/model.safetensors''', '''unet/diffusion_pytorch_model.bin''', # Removed: 'unet/diffusion_pytorch_model.safetensors', ] self.assertFalse(is_safetensors_compatible(lowerCamelCase__ ) ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Optional[int]: a_ : Optional[Any] = [ '''text_encoder/pytorch_model.bin''', '''text_encoder/model.safetensors''', ] self.assertTrue(is_safetensors_compatible(lowerCamelCase__ ) ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Dict: a_ : int = [ '''safety_checker/pytorch_model.bin''', '''safety_checker/model.safetensors''', '''vae/diffusion_pytorch_model.bin''', '''vae/diffusion_pytorch_model.safetensors''', '''text_encoder/pytorch_model.bin''', # Removed: 'text_encoder/model.safetensors', '''unet/diffusion_pytorch_model.bin''', '''unet/diffusion_pytorch_model.safetensors''', ] self.assertFalse(is_safetensors_compatible(lowerCamelCase__ ) ) def SCREAMING_SNAKE_CASE ( self : int ) -> Any: a_ : Tuple = [ '''safety_checker/pytorch_model.fp16.bin''', '''safety_checker/model.fp16.safetensors''', '''vae/diffusion_pytorch_model.fp16.bin''', '''vae/diffusion_pytorch_model.fp16.safetensors''', '''text_encoder/pytorch_model.fp16.bin''', '''text_encoder/model.fp16.safetensors''', '''unet/diffusion_pytorch_model.fp16.bin''', '''unet/diffusion_pytorch_model.fp16.safetensors''', ] a_ : List[str] = '''fp16''' self.assertTrue(is_safetensors_compatible(lowerCamelCase__ , variant=lowerCamelCase__ ) ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> int: a_ : Optional[Any] = [ '''unet/diffusion_pytorch_model.fp16.bin''', '''unet/diffusion_pytorch_model.fp16.safetensors''', ] a_ : Union[str, Any] = '''fp16''' self.assertTrue(is_safetensors_compatible(lowerCamelCase__ , variant=lowerCamelCase__ ) ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Tuple: a_ : Any = [ '''unet/diffusion_pytorch_model.bin''', '''unet/diffusion_pytorch_model.safetensors''', ] a_ : Dict = '''fp16''' self.assertTrue(is_safetensors_compatible(lowerCamelCase__ , variant=lowerCamelCase__ ) ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Tuple: a_ : Any = [ '''safety_checker/pytorch_model.fp16.bin''', '''safety_checker/model.fp16.safetensors''', '''vae/diffusion_pytorch_model.fp16.bin''', '''vae/diffusion_pytorch_model.fp16.safetensors''', '''text_encoder/pytorch_model.fp16.bin''', '''text_encoder/model.fp16.safetensors''', '''unet/diffusion_pytorch_model.fp16.bin''', # Removed: 'unet/diffusion_pytorch_model.fp16.safetensors', ] a_ : int = '''fp16''' self.assertFalse(is_safetensors_compatible(lowerCamelCase__ , variant=lowerCamelCase__ ) ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Tuple: a_ : int = [ '''text_encoder/pytorch_model.fp16.bin''', '''text_encoder/model.fp16.safetensors''', ] a_ : Optional[Any] = '''fp16''' self.assertTrue(is_safetensors_compatible(lowerCamelCase__ , variant=lowerCamelCase__ ) ) def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[Any]: a_ : str = [ '''text_encoder/pytorch_model.bin''', '''text_encoder/model.safetensors''', ] a_ : Tuple = '''fp16''' self.assertTrue(is_safetensors_compatible(lowerCamelCase__ , variant=lowerCamelCase__ ) ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> int: a_ : List[Any] = [ '''safety_checker/pytorch_model.fp16.bin''', '''safety_checker/model.fp16.safetensors''', '''vae/diffusion_pytorch_model.fp16.bin''', '''vae/diffusion_pytorch_model.fp16.safetensors''', '''text_encoder/pytorch_model.fp16.bin''', # 'text_encoder/model.fp16.safetensors', '''unet/diffusion_pytorch_model.fp16.bin''', '''unet/diffusion_pytorch_model.fp16.safetensors''', ] a_ : Optional[Any] = '''fp16''' self.assertFalse(is_safetensors_compatible(lowerCamelCase__ , variant=lowerCamelCase__ ) )
718
'''simple docstring''' import unittest import numpy as np import torch from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = DDIMPipeline snake_case__ = UNCONDITIONAL_IMAGE_GENERATION_PARAMS snake_case__ = PipelineTesterMixin.required_optional_params - { "num_images_per_prompt", "latents", "callback", "callback_steps", } snake_case__ = UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS snake_case__ = False def SCREAMING_SNAKE_CASE ( self : Any ) -> Optional[Any]: torch.manual_seed(0 ) a_ : 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''') , ) a_ : str = DDIMScheduler() a_ : Union[str, Any] = {'''unet''': unet, '''scheduler''': scheduler} return components def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Tuple=0 ) -> str: if str(__SCREAMING_SNAKE_CASE ).startswith('''mps''' ): a_ : Dict = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: a_ : Union[str, Any] = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = { '''batch_size''': 1, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union[str, Any]: a_ : Dict = '''cpu''' a_ : List[Any] = self.get_dummy_components() a_ : List[str] = self.pipeline_class(**__SCREAMING_SNAKE_CASE ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = self.get_dummy_inputs(__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = pipe(**__SCREAMING_SNAKE_CASE ).images a_ : List[str] = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 32, 32, 3) ) a_ : int = np.array( [1.000e00, 5.717e-01, 4.717e-01, 1.000e00, 0.000e00, 1.000e00, 3.000e-04, 0.000e00, 9.000e-04] ) a_ : Union[str, Any] = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(__SCREAMING_SNAKE_CASE , 1e-3 ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> str: super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : str ) -> Optional[int]: super().test_save_load_local(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : Any ) -> List[str]: super().test_save_load_optional_components(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : str ) -> Optional[int]: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : str ) -> Any: a_ : Optional[Any] = '''google/ddpm-cifar10-32''' a_ : Optional[Any] = UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Dict = DDIMScheduler() a_ : List[str] = DDIMPipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) ddim.to(__SCREAMING_SNAKE_CASE ) ddim.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = torch.manual_seed(0 ) a_ : Tuple = ddim(generator=__SCREAMING_SNAKE_CASE , eta=0.0 , output_type='''numpy''' ).images a_ : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) a_ : List[str] = np.array([0.1723, 0.1617, 0.1600, 0.1626, 0.1497, 0.1513, 0.1505, 0.1442, 0.1453] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> str: a_ : int = '''google/ddpm-ema-bedroom-256''' a_ : str = UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Tuple = DDIMScheduler.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = DDIMPipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) ddpm.to(__SCREAMING_SNAKE_CASE ) ddpm.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = torch.manual_seed(0 ) a_ : List[Any] = ddpm(generator=__SCREAMING_SNAKE_CASE , output_type='''numpy''' ).images a_ : Optional[int] = image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) a_ : Optional[Any] = np.array([0.0060, 0.0201, 0.0344, 0.0024, 0.0018, 0.0002, 0.0022, 0.0000, 0.0069] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
666
0
'''simple docstring''' import colorsys from PIL import Image # type: ignore def _UpperCAmelCase ( __A : float , __A : float , __A : int ): a_ : List[Any] = x a_ : Dict = y for step in range(snake_case_ ): # noqa: B007 a_ : Optional[int] = a * a - b * b + x a_ : Tuple = 2 * a * b + y a_ : List[str] = a_new # divergence happens for all complex number with an absolute value # greater than 4 if a * a + b * b > 4: break return step / (max_step - 1) def _UpperCAmelCase ( __A : float ): if distance == 1: return (0, 0, 0) else: return (2_55, 2_55, 2_55) def _UpperCAmelCase ( __A : float ): if distance == 1: return (0, 0, 0) else: return tuple(round(i * 2_55 ) for i in colorsys.hsv_to_rgb(snake_case_ , 1 , 1 ) ) def _UpperCAmelCase ( __A : int = 8_00 , __A : int = 6_00 , __A : float = -0.6 , __A : float = 0 , __A : float = 3.2 , __A : int = 50 , __A : bool = True , ): a_ : List[Any] = Image.new('''RGB''' , (image_width, image_height) ) a_ : Tuple = img.load() # loop through the image-coordinates for image_x in range(snake_case_ ): for image_y in range(snake_case_ ): # determine the figure-coordinates based on the image-coordinates a_ : List[str] = figure_width / image_width * image_height a_ : str = figure_center_x + (image_x / image_width - 0.5) * figure_width a_ : str = figure_center_y + (image_y / image_height - 0.5) * figure_height a_ : str = get_distance(snake_case_ , snake_case_ , snake_case_ ) # color the corresponding pixel based on the selected coloring-function if use_distance_color_coding: a_ : int = get_color_coded_rgb(snake_case_ ) else: a_ : Tuple = get_black_and_white_rgb(snake_case_ ) return img if __name__ == "__main__": import doctest doctest.testmod() # colored version, full figure __lowerCAmelCase = get_image() # uncomment for colored version, different section, zoomed in # img = get_image(figure_center_x = -0.6, figure_center_y = -0.4, # figure_width = 0.8) # uncomment for black and white version, full figure # img = get_image(use_distance_color_coding = False) # uncomment to save the image # img.save("mandelbrot.png") img.show()
719
'''simple docstring''' from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import pyarrow as pa if TYPE_CHECKING: from .features import FeatureType @dataclass class SCREAMING_SNAKE_CASE : snake_case__ = 42 snake_case__ = None # Automatically constructed snake_case__ = "dict" snake_case__ = None snake_case__ = field(default="Translation" , init=SCREAMING_SNAKE_CASE_ , repr=SCREAMING_SNAKE_CASE_ ) def __call__( self : Dict ) -> Tuple: return pa.struct({lang: pa.string() for lang in sorted(self.languages )} ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Value return {k: Value('''string''' ) for k in sorted(self.languages )} @dataclass class SCREAMING_SNAKE_CASE : snake_case__ = None snake_case__ = None snake_case__ = None # Automatically constructed snake_case__ = "dict" snake_case__ = None snake_case__ = field(default="TranslationVariableLanguages" , init=SCREAMING_SNAKE_CASE_ , repr=SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : List[str] = sorted(set(self.languages ) ) if self.languages else None a_ : Optional[Any] = len(self.languages ) if self.languages else None def __call__( self : Any ) -> Optional[Any]: return pa.struct({'''language''': pa.list_(pa.string() ), '''translation''': pa.list_(pa.string() )} ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Optional[Any]: a_ : str = set(self.languages ) if self.languages and set(__SCREAMING_SNAKE_CASE ) - lang_set: raise ValueError( f'Some languages in example ({", ".join(sorted(set(__SCREAMING_SNAKE_CASE ) - lang_set ) )}) are not in valid set ({", ".join(__SCREAMING_SNAKE_CASE )}).' ) # Convert dictionary into tuples, splitting out cases where there are # multiple translations for a single language. a_ : int = [] for lang, text in translation_dict.items(): if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): translation_tuples.append((lang, text) ) else: translation_tuples.extend([(lang, el) for el in text] ) # Ensure translations are in ascending order by language code. a_ , a_ : List[Any] = zip(*sorted(__SCREAMING_SNAKE_CASE ) ) return {"language": languages, "translation": translations} def SCREAMING_SNAKE_CASE ( self : Any ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Sequence, Value return { "language": Sequence(Value('''string''' ) ), "translation": Sequence(Value('''string''' ) ), }
666
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) __lowerCAmelCase = {"""configuration_fnet""": ["""FNET_PRETRAINED_CONFIG_ARCHIVE_MAP""", """FNetConfig"""]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = ["""FNetTokenizer"""] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = ["""FNetTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ """FNET_PRETRAINED_MODEL_ARCHIVE_LIST""", """FNetForMaskedLM""", """FNetForMultipleChoice""", """FNetForNextSentencePrediction""", """FNetForPreTraining""", """FNetForQuestionAnswering""", """FNetForSequenceClassification""", """FNetForTokenClassification""", """FNetLayer""", """FNetModel""", """FNetPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_fnet import FNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FNetConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_fnet import FNetTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_fnet_fast import FNetTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_fnet import ( FNET_PRETRAINED_MODEL_ARCHIVE_LIST, FNetForMaskedLM, FNetForMultipleChoice, FNetForNextSentencePrediction, FNetForPreTraining, FNetForQuestionAnswering, FNetForSequenceClassification, FNetForTokenClassification, FNetLayer, FNetModel, FNetPreTrainedModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
720
'''simple docstring''' import json import os import pickle import shutil import tempfile from unittest import TestCase from unittest.mock import patch import numpy as np from datasets import Dataset from transformers import is_faiss_available from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bart.tokenization_bart import BartTokenizer from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.dpr.tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.retrieval_rag import CustomHFIndex, RagRetriever from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_sentencepiece, require_tokenizers, require_torch if is_faiss_available(): import faiss @require_faiss class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): def SCREAMING_SNAKE_CASE ( self : Dict ) -> Dict: a_ : Union[str, Any] = tempfile.mkdtemp() a_ : Union[str, Any] = 8 # DPR tok a_ : Dict = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] a_ : str = os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = os.path.join(__SCREAMING_SNAKE_CASE , DPR_VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) # BART tok a_ : Union[str, Any] = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] a_ : int = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) a_ : int = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] a_ : Optional[int] = {'''unk_token''': '''<unk>'''} a_ : List[str] = os.path.join(self.tmpdirname , '''bart_tokenizer''' ) os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) a_ : Tuple = os.path.join(__SCREAMING_SNAKE_CASE , BART_VOCAB_FILES_NAMES['''vocab_file'''] ) a_ : int = os.path.join(__SCREAMING_SNAKE_CASE , BART_VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> DPRQuestionEncoderTokenizer: return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : str ) -> DPRContextEncoderTokenizer: return DPRContextEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> BartTokenizer: return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''bart_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Union[str, Any]: shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> Union[str, Any]: a_ : str = Dataset.from_dict( { '''id''': ['''0''', '''1'''], '''text''': ['''foo''', '''bar'''], '''title''': ['''Foo''', '''Bar'''], '''embeddings''': [np.ones(self.retrieval_vector_size ), 2 * np.ones(self.retrieval_vector_size )], } ) dataset.add_faiss_index('''embeddings''' , string_factory='''Flat''' , metric_type=faiss.METRIC_INNER_PRODUCT ) return dataset def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Tuple: a_ : List[str] = self.get_dummy_dataset() a_ : Tuple = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , ) with patch('''transformers.models.rag.retrieval_rag.load_dataset''' ) as mock_load_dataset: a_ : Tuple = dataset a_ : Any = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) return retriever def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : bool ) -> Dict: a_ : Dict = self.get_dummy_dataset() a_ : Dict = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''custom''' , ) if from_disk: a_ : Optional[int] = os.path.join(self.tmpdirname , '''dataset''' ) a_ : str = os.path.join(self.tmpdirname , '''index.faiss''' ) dataset.get_index('''embeddings''' ).save(os.path.join(self.tmpdirname , '''index.faiss''' ) ) dataset.drop_index('''embeddings''' ) dataset.save_to_disk(os.path.join(self.tmpdirname , '''dataset''' ) ) del dataset a_ : int = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) else: a_ : Optional[Any] = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , index=CustomHFIndex(config.retrieval_vector_size , __SCREAMING_SNAKE_CASE ) , ) return retriever def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Optional[int]: a_ : str = Dataset.from_dict( { '''id''': ['''0''', '''1'''], '''text''': ['''foo''', '''bar'''], '''title''': ['''Foo''', '''Bar'''], '''embeddings''': [np.ones(self.retrieval_vector_size + 1 ), 2 * np.ones(self.retrieval_vector_size + 1 )], } ) dataset.add_faiss_index('''embeddings''' , string_factory='''Flat''' , metric_type=faiss.METRIC_INNER_PRODUCT ) a_ : Optional[int] = os.path.join(self.tmpdirname , '''hf_bert_base.hnswSQ8_correct_phi_128.c_index''' ) dataset.save_faiss_index('''embeddings''' , index_file_name + '''.index.dpr''' ) pickle.dump(dataset['''id'''] , open(index_file_name + '''.index_meta.dpr''' , '''wb''' ) ) a_ : Union[str, Any] = os.path.join(self.tmpdirname , '''psgs_w100.tsv.pkl''' ) a_ : Dict = {sample['''id''']: [sample['''text'''], sample['''title''']] for sample in dataset} pickle.dump(__SCREAMING_SNAKE_CASE , open(__SCREAMING_SNAKE_CASE , '''wb''' ) ) a_ : Optional[Any] = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''legacy''' , index_path=self.tmpdirname , ) a_ : int = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() ) return retriever def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : Optional[Any] = 1 a_ : Dict = self.get_dummy_canonical_hf_index_retriever() a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : str = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : str = self.get_dummy_canonical_hf_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: with patch('''transformers.models.rag.retrieval_rag.load_dataset''' ) as mock_load_dataset: a_ : List[str] = self.get_dummy_dataset() retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : List[str] = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> int: a_ : Union[str, Any] = 1 a_ : Optional[Any] = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) a_ : List[Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : Any = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : str ) -> List[str]: a_ : Dict = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : List[str] = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Dict = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Optional[int]: a_ : Union[str, Any] = 1 a_ : str = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : Tuple = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : int ) -> Dict: a_ : List[str] = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Dict = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Any: a_ : str = 1 a_ : Tuple = self.get_dummy_legacy_index_retriever() a_ : Union[str, Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : Any = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''text'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''text'''][0] , '''bar''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''text'''][0] , '''foo''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[int]: a_ : List[str] = self.get_dummy_legacy_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : List[str] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Optional[Any] = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) @require_torch @require_tokenizers @require_sentencepiece def SCREAMING_SNAKE_CASE ( self : int ) -> List[str]: import torch a_ : Any = 1 a_ : List[Any] = self.get_dummy_canonical_hf_index_retriever() a_ : Union[str, Any] = [[5, 7], [10, 11]] a_ : Optional[int] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : str = retriever(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE ) a_ , a_ , a_ : List[str] = ( out['''context_input_ids'''], out['''context_attention_mask'''], out['''retrieved_doc_embeds'''], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , np.ndarray ) a_ : Any = retriever( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE , return_tensors='''pt''' , ) a_ , a_ , a_ , a_ : str = ( # noqa: F841 out['''context_input_ids'''], out['''context_attention_mask'''], out['''retrieved_doc_embeds'''], out['''doc_ids'''], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) @require_torch @require_tokenizers @require_sentencepiece def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Any: a_ : str = self.get_dpr_ctx_encoder_tokenizer() a_ : Tuple = 1 a_ : Any = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) retriever.set_ctx_encoder_tokenizer(__SCREAMING_SNAKE_CASE ) a_ : Dict = [[5, 7], [10, 11]] a_ : List[str] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : List[Any] = retriever(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual( len(__SCREAMING_SNAKE_CASE ) , 6 ) # check whether the retriever output consist of 6 attributes including tokenized docs self.assertEqual( all(k in out for k in ('''tokenized_doc_ids''', '''tokenized_doc_attention_mask''') ) , __SCREAMING_SNAKE_CASE ) # check for doc token related keys in dictionary.
666
0
'''simple docstring''' __lowerCAmelCase = range(2, 20 + 1) __lowerCAmelCase = [10**k for k in range(ks[-1] + 1)] __lowerCAmelCase = {} def _UpperCAmelCase ( __A : Any , __A : int , __A : Optional[int] , __A : int ): a_ : Union[str, Any] = sum(a_i[j] for j in range(lowerCamelCase__ , len(lowerCamelCase__ ) ) ) a_ : Any = sum(a_i[j] * base[j] for j in range(min(len(lowerCamelCase__ ) , lowerCamelCase__ ) ) ) a_ , a_ : str = 0, 0 a_ : Dict = n - i a_ : Union[str, Any] = memo.get(lowerCamelCase__ ) if sub_memo is not None: a_ : Union[str, Any] = sub_memo.get(lowerCamelCase__ ) if jumps is not None and len(lowerCamelCase__ ) > 0: # find and make the largest jump without going over a_ : List[Any] = -1 for _k in range(len(lowerCamelCase__ ) - 1 , -1 , -1 ): if jumps[_k][2] <= k and jumps[_k][1] <= max_dn: a_ : Optional[int] = _k break if max_jump >= 0: a_ , a_ , a_ : Any = jumps[max_jump] # since the difference between jumps is cached, add c a_ : str = diff + c for j in range(min(lowerCamelCase__ , len(lowerCamelCase__ ) ) ): a_ , a_ : Optional[Any] = divmod(lowerCamelCase__ , 10 ) if new_c > 0: add(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) else: a_ : Dict = [] else: a_ : Union[str, Any] = {c: []} a_ : str = sub_memo if dn >= max_dn or c + diff >= base[k]: return diff, dn if k > ks[0]: while True: # keep doing smaller jumps a_ , a_ : Optional[Any] = next_term(lowerCamelCase__ , k - 1 , i + dn , lowerCamelCase__ ) diff += _diff dn += terms_jumped if dn >= max_dn or c + diff >= base[k]: break else: # would be too small a jump, just compute sequential terms instead a_ , a_ : str = compute(lowerCamelCase__ , lowerCamelCase__ , i + dn , lowerCamelCase__ ) diff += _diff dn += terms_jumped a_ : Optional[int] = sub_memo[c] # keep jumps sorted by # of terms skipped a_ : int = 0 while j < len(lowerCamelCase__ ): if jumps[j][1] > dn: break j += 1 # cache the jump for this value digitsum(b) and c sub_memo[c].insert(lowerCamelCase__ , (diff, dn, k) ) return (diff, dn) def _UpperCAmelCase ( __A : Optional[Any] , __A : str , __A : Dict , __A : Optional[Any] ): if i >= n: return 0, i if k > len(lowerCamelCase__ ): a_i.extend([0 for _ in range(k - len(lowerCamelCase__ ) )] ) # note: a_i -> b * 10^k + c # ds_b -> digitsum(b) # ds_c -> digitsum(c) a_ : str = i a_ , a_ , a_ : Optional[Any] = 0, 0, 0 for j in range(len(lowerCamelCase__ ) ): if j >= k: ds_b += a_i[j] else: ds_c += a_i[j] while i < n: i += 1 a_ : Optional[int] = ds_c + ds_b diff += addend a_ : int = 0 for j in range(lowerCamelCase__ ): a_ : Tuple = a_i[j] + addend a_ , a_ : Tuple = divmod(lowerCamelCase__ , 10 ) ds_c += a_i[j] if addend > 0: break if addend > 0: add(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) return diff, i - start_i def _UpperCAmelCase ( __A : Any , __A : List[str] , __A : str ): for j in range(lowerCamelCase__ , len(lowerCamelCase__ ) ): a_ : Any = digits[j] + addend if s >= 10: a_ , a_ : Any = divmod(lowerCamelCase__ , 10 ) a_ : Optional[int] = addend // 10 + quotient else: a_ : Union[str, Any] = s a_ : int = addend // 10 if addend == 0: break while addend > 0: a_ , a_ : Any = divmod(lowerCamelCase__ , 10 ) digits.append(lowerCamelCase__ ) def _UpperCAmelCase ( __A : Any = 10**15 ): a_ : List[str] = [1] a_ : List[Any] = 1 a_ : Optional[int] = 0 while True: a_ , a_ : Dict = next_term(lowerCamelCase__ , 20 , i + dn , lowerCamelCase__ ) dn += terms_jumped if dn == n - i: break a_ : Optional[Any] = 0 for j in range(len(lowerCamelCase__ ) ): a_n += digits[j] * 10**j return a_n if __name__ == "__main__": print(F"""{solution() = }""")
721
'''simple docstring''' from math import pi, sqrt, tan def _UpperCAmelCase ( __A : float ): if side_length < 0: raise ValueError('''surface_area_cube() only accepts non-negative values''' ) return 6 * side_length**2 def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if length < 0 or breadth < 0 or height < 0: raise ValueError('''surface_area_cuboid() only accepts non-negative values''' ) return 2 * ((length * breadth) + (breadth * height) + (length * height)) def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''surface_area_sphere() only accepts non-negative values''' ) return 4 * pi * radius**2 def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''surface_area_hemisphere() only accepts non-negative values''' ) return 3 * pi * radius**2 def _UpperCAmelCase ( __A : float , __A : float ): if radius < 0 or height < 0: raise ValueError('''surface_area_cone() only accepts non-negative values''' ) return pi * radius * (radius + (height**2 + radius**2) ** 0.5) def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if radius_a < 0 or radius_a < 0 or height < 0: raise ValueError( '''surface_area_conical_frustum() only accepts non-negative values''' ) a_ : Any = (height**2 + (radius_a - radius_a) ** 2) ** 0.5 return pi * ((slant_height * (radius_a + radius_a)) + radius_a**2 + radius_a**2) def _UpperCAmelCase ( __A : float , __A : float ): if radius < 0 or height < 0: raise ValueError('''surface_area_cylinder() only accepts non-negative values''' ) return 2 * pi * radius * (height + radius) def _UpperCAmelCase ( __A : float , __A : float ): if torus_radius < 0 or tube_radius < 0: raise ValueError('''surface_area_torus() only accepts non-negative values''' ) if torus_radius < tube_radius: raise ValueError( '''surface_area_torus() does not support spindle or self intersecting tori''' ) return 4 * pow(__A , 2 ) * torus_radius * tube_radius def _UpperCAmelCase ( __A : float , __A : float ): if length < 0 or width < 0: raise ValueError('''area_rectangle() only accepts non-negative values''' ) return length * width def _UpperCAmelCase ( __A : float ): if side_length < 0: raise ValueError('''area_square() only accepts non-negative values''' ) return side_length**2 def _UpperCAmelCase ( __A : float , __A : float ): if base < 0 or height < 0: raise ValueError('''area_triangle() only accepts non-negative values''' ) return (base * height) / 2 def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if sidea < 0 or sidea < 0 or sidea < 0: raise ValueError('''area_triangle_three_sides() only accepts non-negative values''' ) elif sidea + sidea < sidea or sidea + sidea < sidea or sidea + sidea < sidea: raise ValueError('''Given three sides do not form a triangle''' ) a_ : int = (sidea + sidea + sidea) / 2 a_ : Optional[Any] = sqrt( semi_perimeter * (semi_perimeter - sidea) * (semi_perimeter - sidea) * (semi_perimeter - sidea) ) return area def _UpperCAmelCase ( __A : float , __A : float ): if base < 0 or height < 0: raise ValueError('''area_parallelogram() only accepts non-negative values''' ) return base * height def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if basea < 0 or basea < 0 or height < 0: raise ValueError('''area_trapezium() only accepts non-negative values''' ) return 1 / 2 * (basea + basea) * height def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''area_circle() only accepts non-negative values''' ) return pi * radius**2 def _UpperCAmelCase ( __A : float , __A : float ): if radius_x < 0 or radius_y < 0: raise ValueError('''area_ellipse() only accepts non-negative values''' ) return pi * radius_x * radius_y def _UpperCAmelCase ( __A : float , __A : float ): if diagonal_a < 0 or diagonal_a < 0: raise ValueError('''area_rhombus() only accepts non-negative values''' ) return 1 / 2 * diagonal_a * diagonal_a def _UpperCAmelCase ( __A : int , __A : float ): if not isinstance(__A , __A ) or sides < 3: raise ValueError( '''area_reg_polygon() only accepts integers greater than or \ equal to three as number of sides''' ) elif length < 0: raise ValueError( '''area_reg_polygon() only accepts non-negative values as \ length of a side''' ) return (sides * length**2) / (4 * tan(pi / sides )) return (sides * length**2) / (4 * tan(pi / sides )) if __name__ == "__main__": import doctest doctest.testmod(verbose=True) # verbose so we can see methods missing tests print('[DEMO] Areas of various geometric shapes: \n') print(F"""Rectangle: {area_rectangle(10, 20) = }""") print(F"""Square: {area_square(10) = }""") print(F"""Triangle: {area_triangle(10, 10) = }""") print(F"""Triangle: {area_triangle_three_sides(5, 12, 13) = }""") print(F"""Parallelogram: {area_parallelogram(10, 20) = }""") print(F"""Rhombus: {area_rhombus(10, 20) = }""") print(F"""Trapezium: {area_trapezium(10, 20, 30) = }""") print(F"""Circle: {area_circle(20) = }""") print(F"""Ellipse: {area_ellipse(10, 20) = }""") print('\nSurface Areas of various geometric shapes: \n') print(F"""Cube: {surface_area_cube(20) = }""") print(F"""Cuboid: {surface_area_cuboid(10, 20, 30) = }""") print(F"""Sphere: {surface_area_sphere(20) = }""") print(F"""Hemisphere: {surface_area_hemisphere(20) = }""") print(F"""Cone: {surface_area_cone(10, 20) = }""") print(F"""Conical Frustum: {surface_area_conical_frustum(10, 20, 30) = }""") print(F"""Cylinder: {surface_area_cylinder(10, 20) = }""") print(F"""Torus: {surface_area_torus(20, 10) = }""") print(F"""Equilateral Triangle: {area_reg_polygon(3, 10) = }""") print(F"""Square: {area_reg_polygon(4, 10) = }""") print(F"""Reqular Pentagon: {area_reg_polygon(5, 10) = }""")
666
0
'''simple docstring''' import argparse import torch from torch import nn from transformers import MaMaaaConfig, MaMaaaForConditionalGeneration def _UpperCAmelCase ( __A : Any ): '''simple docstring''' a_ : Optional[Any] = [ '''encoder.version''', '''decoder.version''', '''model.encoder.version''', '''model.decoder.version''', '''decoder.output_projection.weight''', '''_float_tensor''', '''encoder.embed_positions._float_tensor''', '''decoder.embed_positions._float_tensor''', ] for k in ignore_keys: state_dict.pop(__A , __A ) def _UpperCAmelCase ( __A : Union[str, Any] ): '''simple docstring''' a_ : str = emb.weight.shape a_ : Any = nn.Linear(__A , __A , bias=__A ) a_ : List[Any] = emb.weight.data return lin_layer def _UpperCAmelCase ( __A : List[Any] ): '''simple docstring''' a_ : int = torch.load(__A , map_location='''cpu''' ) a_ : Tuple = mam_aaa['''args'''] or mam_aaa['''cfg''']['''model'''] a_ : List[str] = mam_aaa['''model'''] remove_ignore_keys_(__A ) a_ : Union[str, Any] = state_dict['''encoder.embed_tokens.weight'''].shape[0] a_ : List[str] = MaMaaaConfig( vocab_size=__A , max_position_embeddings=10_24 , encoder_layers=args.encoder_layers , decoder_layers=args.decoder_layers , encoder_attention_heads=args.encoder_attention_heads , decoder_attention_heads=args.decoder_attention_heads , encoder_ffn_dim=args.encoder_ffn_embed_dim , decoder_ffn_dim=args.decoder_ffn_embed_dim , d_model=args.encoder_embed_dim , encoder_layerdrop=args.encoder_layerdrop , decoder_layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function='''relu''' , ) a_ : List[Any] = state_dict['''decoder.embed_tokens.weight'''] a_ : List[str] = MaMaaaForConditionalGeneration(__A ) model.model.load_state_dict(__A , strict=__A ) a_ : Tuple = make_linear_from_emb(model.model.shared ) return model if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument('fairseq_path', type=str, help='path to a model.pt on local filesystem.') parser.add_argument('pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') __lowerCAmelCase = parser.parse_args() __lowerCAmelCase = convert_fairseq_mamaaa_checkpoint_from_disk(args.fairseq_pathß) model.save_pretrained(args.pytorch_dump_folder_path)
700
'''simple docstring''' import random import unittest import torch from diffusers import IFInpaintingSuperResolutionPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = IFInpaintingSuperResolutionPipeline snake_case__ = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"width", "height"} snake_case__ = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({"original_image"} ) snake_case__ = PipelineTesterMixin.required_optional_params - {"latents"} def SCREAMING_SNAKE_CASE ( self : str ) -> List[str]: return self._get_superresolution_dummy_components() def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Dict=0 ) -> List[Any]: if str(__SCREAMING_SNAKE_CASE ).startswith('''mps''' ): a_ : Optional[int] = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: a_ : str = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) a_ : Dict = floats_tensor((1, 3, 16, 16) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = floats_tensor((1, 3, 32, 32) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) a_ : Tuple = floats_tensor((1, 3, 32, 32) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': image, '''original_image''': original_image, '''mask_image''': mask_image, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) def SCREAMING_SNAKE_CASE ( self : int ) -> int: self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def SCREAMING_SNAKE_CASE ( self : str ) -> Tuple: # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder super().test_save_load_floataa(expected_max_diff=1e-1 ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Optional[int]: self._test_attention_slicing_forward_pass(expected_max_diff=1e-2 ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Optional[Any]: self._test_save_load_local() def SCREAMING_SNAKE_CASE ( self : Dict ) -> Optional[Any]: self._test_inference_batch_single_identical( expected_max_diff=1e-2 , )
666
0
from .integrations import ( is_optuna_available, is_ray_available, is_sigopt_available, is_wandb_available, run_hp_search_optuna, run_hp_search_ray, run_hp_search_sigopt, run_hp_search_wandb, ) from .trainer_utils import ( HPSearchBackend, default_hp_space_optuna, default_hp_space_ray, default_hp_space_sigopt, default_hp_space_wandb, ) from .utils import logging __lowerCAmelCase = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE : snake_case__ = 42 snake_case__ = None @staticmethod def SCREAMING_SNAKE_CASE ( ) -> Any: raise NotImplementedError def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : str , **__SCREAMING_SNAKE_CASE : Dict ) -> Tuple: raise NotImplementedError def SCREAMING_SNAKE_CASE ( self : List[Any] , __SCREAMING_SNAKE_CASE : int ) -> Union[str, Any]: raise NotImplementedError def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Optional[int]: if not self.is_available(): raise RuntimeError( f'You picked the {self.name} backend, but it is not installed. Run {self.pip_install()}.' ) @classmethod def SCREAMING_SNAKE_CASE ( cls : List[str] ) -> Any: return f'`pip install {cls.pip_package or cls.name}`' class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "optuna" @staticmethod def SCREAMING_SNAKE_CASE ( ) -> Tuple: return is_optuna_available() def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : str , **__SCREAMING_SNAKE_CASE : List[str] ) -> Optional[int]: return run_hp_search_optuna(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Any , __SCREAMING_SNAKE_CASE : int ) -> int: return default_hp_space_optuna(__SCREAMING_SNAKE_CASE ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "ray" snake_case__ = "'ray[tune]'" @staticmethod def SCREAMING_SNAKE_CASE ( ) -> List[str]: return is_ray_available() def SCREAMING_SNAKE_CASE ( self : Any , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : str , **__SCREAMING_SNAKE_CASE : List[str] ) -> Optional[int]: return run_hp_search_ray(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : int ) -> Optional[int]: return default_hp_space_ray(__SCREAMING_SNAKE_CASE ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "sigopt" @staticmethod def SCREAMING_SNAKE_CASE ( ) -> List[Any]: return is_sigopt_available() def SCREAMING_SNAKE_CASE ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : str , **__SCREAMING_SNAKE_CASE : Optional[int] ) -> int: return run_hp_search_sigopt(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : Any ) -> List[str]: return default_hp_space_sigopt(__SCREAMING_SNAKE_CASE ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "wandb" @staticmethod def SCREAMING_SNAKE_CASE ( ) -> List[Any]: return is_wandb_available() def SCREAMING_SNAKE_CASE ( self : List[Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : str , **__SCREAMING_SNAKE_CASE : Any ) -> Optional[int]: return run_hp_search_wandb(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Any , __SCREAMING_SNAKE_CASE : Tuple ) -> Dict: return default_hp_space_wandb(__SCREAMING_SNAKE_CASE ) __lowerCAmelCase = { HPSearchBackend(backend.name): backend for backend in [OptunaBackend, RayTuneBackend, SigOptBackend, WandbBackend] } def _UpperCAmelCase ( ): a_ : Optional[Any] = [backend for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() if backend.is_available()] if len(__A ) > 0: a_ : List[Any] = available_backends[0].name if len(__A ) > 1: logger.info( f'{len(__A )} hyperparameter search backends available. Using {name} as the default.' ) return name raise RuntimeError( '''No hyperparameter search backend available.\n''' + '''\n'''.join( f' - To install {backend.name} run {backend.pip_install()}' for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() ) )
701
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCAmelCase = { 'configuration_git': ['GIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GitConfig', 'GitVisionConfig'], 'processing_git': ['GitProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ 'GIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'GitForCausalLM', 'GitModel', 'GitPreTrainedModel', 'GitVisionModel', ] if TYPE_CHECKING: from .configuration_git import GIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GitConfig, GitVisionConfig from .processing_git import GitProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_git import ( GIT_PRETRAINED_MODEL_ARCHIVE_LIST, GitForCausalLM, GitModel, GitPreTrainedModel, GitVisionModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
666
0
'''simple docstring''' import argparse import os import gluonnlp as nlp import mxnet as mx import numpy as np import torch from gluonnlp.base import get_home_dir from gluonnlp.model.bert import BERTEncoder from gluonnlp.model.utils import _load_vocab from gluonnlp.vocab import Vocab from packaging import version from torch import nn from transformers import BertConfig, BertForMaskedLM, BertModel, RobertaTokenizer from transformers.models.bert.modeling_bert import ( BertIntermediate, BertLayer, BertOutput, BertSelfAttention, BertSelfOutput, ) from transformers.utils import logging if version.parse(nlp.__version__) != version.parse('0.8.3'): raise Exception('requires gluonnlp == 0.8.3') if version.parse(mx.__version__) != version.parse('1.5.0'): raise Exception('requires mxnet == 1.5.0') logging.set_verbosity_info() __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = 'The Nymphenburg Palace is a beautiful palace in Munich!' def _UpperCAmelCase ( __A : str , __A : str ): a_ : Dict = { '''attention_cell''': '''multi_head''', '''num_layers''': 4, '''units''': 10_24, '''hidden_size''': 7_68, '''max_length''': 5_12, '''num_heads''': 8, '''scaled''': True, '''dropout''': 0.1, '''use_residual''': True, '''embed_size''': 10_24, '''embed_dropout''': 0.1, '''word_embed''': None, '''layer_norm_eps''': 1E-5, '''token_type_vocab_size''': 2, } a_ : Tuple = bort_4_8_768_1024_hparams # Let's construct the original Bort model here # Taken from official BERT implementation, see: # https://github.com/alexa/bort/blob/master/bort/bort.py a_ : Optional[Any] = BERTEncoder( attention_cell=predefined_args['''attention_cell'''] , num_layers=predefined_args['''num_layers'''] , units=predefined_args['''units'''] , hidden_size=predefined_args['''hidden_size'''] , max_length=predefined_args['''max_length'''] , num_heads=predefined_args['''num_heads'''] , scaled=predefined_args['''scaled'''] , dropout=predefined_args['''dropout'''] , output_attention=__A , output_all_encodings=__A , use_residual=predefined_args['''use_residual'''] , activation=predefined_args.get('''activation''' , '''gelu''' ) , layer_norm_eps=predefined_args.get('''layer_norm_eps''' , __A ) , ) # Vocab information needs to be fetched first # It's the same as RoBERTa, so RobertaTokenizer can be used later a_ : Optional[Any] = '''openwebtext_ccnews_stories_books_cased''' # Specify download folder to Gluonnlp's vocab a_ : Dict = os.path.join(get_home_dir() , '''models''' ) a_ : List[Any] = _load_vocab(__A , __A , __A , cls=__A ) a_ : str = nlp.model.BERTModel( __A , len(__A ) , units=predefined_args['''units'''] , embed_size=predefined_args['''embed_size'''] , embed_dropout=predefined_args['''embed_dropout'''] , word_embed=predefined_args['''word_embed'''] , use_pooler=__A , use_token_type_embed=__A , token_type_vocab_size=predefined_args['''token_type_vocab_size'''] , use_classifier=__A , use_decoder=__A , ) original_bort.load_parameters(__A , cast_dtype=__A , ignore_extra=__A ) a_ : Union[str, Any] = original_bort._collect_params_with_prefix() # Build our config 🤗 a_ : List[Any] = { '''architectures''': ['''BertForMaskedLM'''], '''attention_probs_dropout_prob''': predefined_args['''dropout'''], '''hidden_act''': '''gelu''', '''hidden_dropout_prob''': predefined_args['''dropout'''], '''hidden_size''': predefined_args['''embed_size'''], '''initializer_range''': 0.02, '''intermediate_size''': predefined_args['''hidden_size'''], '''layer_norm_eps''': predefined_args['''layer_norm_eps'''], '''max_position_embeddings''': predefined_args['''max_length'''], '''model_type''': '''bort''', '''num_attention_heads''': predefined_args['''num_heads'''], '''num_hidden_layers''': predefined_args['''num_layers'''], '''pad_token_id''': 1, # 2 = BERT, 1 = RoBERTa '''type_vocab_size''': 1, # 2 = BERT, 1 = RoBERTa '''vocab_size''': len(__A ), } a_ : List[str] = BertConfig.from_dict(__A ) a_ : Dict = BertForMaskedLM(__A ) hf_bort_model.eval() # Parameter mapping table (Gluonnlp to Transformers) # * denotes layer index # # | Gluon Parameter | Transformers Parameter # | -------------------------------------------------------------- | ---------------------- # | `encoder.layer_norm.beta` | `bert.embeddings.LayerNorm.bias` # | `encoder.layer_norm.gamma` | `bert.embeddings.LayerNorm.weight` # | `encoder.position_weight` | `bert.embeddings.position_embeddings.weight` # | `word_embed.0.weight` | `bert.embeddings.word_embeddings.weight` # | `encoder.transformer_cells.*.attention_cell.proj_key.bias` | `bert.encoder.layer.*.attention.self.key.bias` # | `encoder.transformer_cells.*.attention_cell.proj_key.weight` | `bert.encoder.layer.*.attention.self.key.weight` # | `encoder.transformer_cells.*.attention_cell.proj_query.bias` | `bert.encoder.layer.*.attention.self.query.bias` # | `encoder.transformer_cells.*.attention_cell.proj_query.weight` | `bert.encoder.layer.*.attention.self.query.weight` # | `encoder.transformer_cells.*.attention_cell.proj_value.bias` | `bert.encoder.layer.*.attention.self.value.bias` # | `encoder.transformer_cells.*.attention_cell.proj_value.weight` | `bert.encoder.layer.*.attention.self.value.weight` # | `encoder.transformer_cells.*.ffn.ffn_2.bias` | `bert.encoder.layer.*.attention.output.dense.bias` # | `encoder.transformer_cells.*.ffn.ffn_2.weight` | `bert.encoder.layer.*.attention.output.dense.weight` # | `encoder.transformer_cells.*.layer_norm.beta` | `bert.encoder.layer.*.attention.output.LayerNorm.bias` # | `encoder.transformer_cells.*.layer_norm.gamma` | `bert.encoder.layer.*.attention.output.LayerNorm.weight` # | `encoder.transformer_cells.*.ffn.ffn_1.bias` | `bert.encoder.layer.*.intermediate.dense.bias` # | `encoder.transformer_cells.*.ffn.ffn_1.weight` | `bert.encoder.layer.*.intermediate.dense.weight` # | `encoder.transformer_cells.*.ffn.layer_norm.beta` | `bert.encoder.layer.*.output.LayerNorm.bias` # | `encoder.transformer_cells.*.ffn.layer_norm.gamma` | `bert.encoder.layer.*.output.LayerNorm.weight` # | `encoder.transformer_cells.*.proj.bias` | `bert.encoder.layer.*.output.dense.bias` # | `encoder.transformer_cells.*.proj.weight` | `bert.encoder.layer.*.output.dense.weight` # Helper function to convert MXNET Arrays to PyTorch def to_torch(__A : List[Any] ) -> nn.Parameter: return nn.Parameter(torch.FloatTensor(mx_array.data().asnumpy() ) ) # Check param shapes and map new HF param back def check_and_map_params(__A : str , __A : Tuple ): a_ : Tuple = hf_param.shape a_ : int = to_torch(params[gluon_param] ) a_ : Union[str, Any] = gluon_param.shape assert ( shape_hf == shape_gluon ), f'The gluon parameter {gluon_param} has shape {shape_gluon}, but expects shape {shape_hf} for Transformers' return gluon_param a_ : Any = check_and_map_params( hf_bort_model.bert.embeddings.word_embeddings.weight , '''word_embed.0.weight''' ) a_ : List[str] = check_and_map_params( hf_bort_model.bert.embeddings.position_embeddings.weight , '''encoder.position_weight''' ) a_ : Optional[int] = check_and_map_params( hf_bort_model.bert.embeddings.LayerNorm.bias , '''encoder.layer_norm.beta''' ) a_ : Union[str, Any] = check_and_map_params( hf_bort_model.bert.embeddings.LayerNorm.weight , '''encoder.layer_norm.gamma''' ) # Inspired by RoBERTa conversion script, we just zero them out (Bort does not use them) a_ : str = torch.zeros_like( hf_bort_model.bert.embeddings.token_type_embeddings.weight.data ) for i in range(hf_bort_config.num_hidden_layers ): a_ : BertLayer = hf_bort_model.bert.encoder.layer[i] # self attention a_ : BertSelfAttention = layer.attention.self a_ : Union[str, Any] = check_and_map_params( self_attn.key.bias.data , f'encoder.transformer_cells.{i}.attention_cell.proj_key.bias' ) a_ : Union[str, Any] = check_and_map_params( self_attn.key.weight.data , f'encoder.transformer_cells.{i}.attention_cell.proj_key.weight' ) a_ : Optional[int] = check_and_map_params( self_attn.query.bias.data , f'encoder.transformer_cells.{i}.attention_cell.proj_query.bias' ) a_ : Tuple = check_and_map_params( self_attn.query.weight.data , f'encoder.transformer_cells.{i}.attention_cell.proj_query.weight' ) a_ : Any = check_and_map_params( self_attn.value.bias.data , f'encoder.transformer_cells.{i}.attention_cell.proj_value.bias' ) a_ : Union[str, Any] = check_and_map_params( self_attn.value.weight.data , f'encoder.transformer_cells.{i}.attention_cell.proj_value.weight' ) # self attention output a_ : BertSelfOutput = layer.attention.output a_ : List[Any] = check_and_map_params( self_output.dense.bias , f'encoder.transformer_cells.{i}.proj.bias' ) a_ : List[Any] = check_and_map_params( self_output.dense.weight , f'encoder.transformer_cells.{i}.proj.weight' ) a_ : Optional[int] = check_and_map_params( self_output.LayerNorm.bias , f'encoder.transformer_cells.{i}.layer_norm.beta' ) a_ : Dict = check_and_map_params( self_output.LayerNorm.weight , f'encoder.transformer_cells.{i}.layer_norm.gamma' ) # intermediate a_ : BertIntermediate = layer.intermediate a_ : str = check_and_map_params( intermediate.dense.bias , f'encoder.transformer_cells.{i}.ffn.ffn_1.bias' ) a_ : Optional[Any] = check_and_map_params( intermediate.dense.weight , f'encoder.transformer_cells.{i}.ffn.ffn_1.weight' ) # output a_ : BertOutput = layer.output a_ : Union[str, Any] = check_and_map_params( bert_output.dense.bias , f'encoder.transformer_cells.{i}.ffn.ffn_2.bias' ) a_ : Union[str, Any] = check_and_map_params( bert_output.dense.weight , f'encoder.transformer_cells.{i}.ffn.ffn_2.weight' ) a_ : str = check_and_map_params( bert_output.LayerNorm.bias , f'encoder.transformer_cells.{i}.ffn.layer_norm.beta' ) a_ : str = check_and_map_params( bert_output.LayerNorm.weight , f'encoder.transformer_cells.{i}.ffn.layer_norm.gamma' ) # Save space and energy 🎄 hf_bort_model.half() # Compare output of both models a_ : int = RobertaTokenizer.from_pretrained('''roberta-base''' ) a_ : Union[str, Any] = tokenizer.encode_plus(__A )['''input_ids'''] # Get gluon output a_ : int = mx.nd.array([input_ids] ) a_ : List[str] = original_bort(inputs=__A , token_types=[] ) # Get Transformer output (save and reload model again) hf_bort_model.save_pretrained(__A ) a_ : Optional[Any] = BertModel.from_pretrained(__A ) hf_bort_model.eval() a_ : int = tokenizer.encode_plus(__A , return_tensors='''pt''' ) a_ : Union[str, Any] = hf_bort_model(**__A )[0] a_ : Tuple = output_gluon[0].asnumpy() a_ : Optional[Any] = output_hf[0].detach().numpy() a_ : Optional[Any] = np.max(np.abs(hf_layer - gluon_layer ) ).item() a_ : Dict = np.allclose(__A , __A , atol=1E-3 ) if success: print('''✔️ Both model do output the same tensors''' ) else: print('''❌ Both model do **NOT** output the same tensors''' ) print('''Absolute difference is:''' , __A ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '--bort_checkpoint_path', default=None, type=str, required=True, help='Path the official Bort params file.' ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) __lowerCAmelCase = parser.parse_args() convert_bort_checkpoint_to_pytorch(args.bort_checkpoint_path, args.pytorch_dump_folder_path)
702
'''simple docstring''' from unittest.mock import patch import pyspark from datasets.packaged_modules.spark.spark import ( Spark, SparkExamplesIterable, _generate_iterable_examples, ) from ..utils import ( require_dill_gt_0_3_2, require_not_windows, ) def _UpperCAmelCase ( __A : List[str] , __A : List[Any] ): a_ : Any = [] for part_id in partition_order: a_ : str = df.where(f'SPARK_PARTITION_ID() = {part_id}' ).collect() for row_idx, row in enumerate(__A ): expected_row_ids_and_row_dicts.append((f'{part_id}_{row_idx}', row.asDict()) ) return expected_row_ids_and_row_dicts @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : List[str] = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : Union[str, Any] = spark.range(1_00 ).repartition(1 ) a_ : Any = Spark(__A ) # The id ints will be converted to Pyarrow int64s, so each row will be 8 bytes. Setting a max_shard_size of 16 means # that each partition can hold 2 rows. spark_builder._repartition_df_if_needed(max_shard_size=16 ) # Given that the dataframe has 100 rows and each partition has 2 rows, we expect 50 partitions. assert spark_builder.df.rdd.getNumPartitions() == 50 @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : List[Any] = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : int = spark.range(10 ).repartition(2 ) a_ : Tuple = [1, 0] a_ : List[str] = _generate_iterable_examples(__A , __A ) # Reverse the partitions. a_ : int = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , __A ) for i, (row_id, row_dict) in enumerate(generate_fn() ): a_ , a_ : List[Any] = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : int = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : str = spark.range(10 ).repartition(1 ) a_ : Tuple = SparkExamplesIterable(__A ) assert it.n_shards == 1 for i, (row_id, row_dict) in enumerate(__A ): assert row_id == f'0_{i}' assert row_dict == {"id": i} @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : Tuple = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : str = spark.range(30 ).repartition(3 ) # Mock the generator so that shuffle reverses the partition indices. with patch('''numpy.random.Generator''' ) as generator_mock: a_ : Union[str, Any] = lambda __A : x.reverse() a_ : Any = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [2, 1, 0] ) a_ : str = SparkExamplesIterable(__A ).shuffle_data_sources(__A ) assert shuffled_it.n_shards == 3 for i, (row_id, row_dict) in enumerate(__A ): a_ , a_ : Optional[Any] = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : int = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : List[str] = spark.range(20 ).repartition(4 ) # Partitions 0 and 2 a_ : Dict = SparkExamplesIterable(__A ).shard_data_sources(worker_id=0 , num_workers=2 ) assert shard_it_a.n_shards == 2 a_ : Optional[Any] = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [0, 2] ) for i, (row_id, row_dict) in enumerate(__A ): a_ , a_ : Tuple = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict # Partitions 1 and 3 a_ : List[Any] = SparkExamplesIterable(__A ).shard_data_sources(worker_id=1 , num_workers=2 ) assert shard_it_a.n_shards == 2 a_ : Optional[int] = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [1, 3] ) for i, (row_id, row_dict) in enumerate(__A ): a_ , a_ : Any = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : Any = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : List[Any] = spark.range(1_00 ).repartition(1 ) a_ : Optional[Any] = Spark(__A ) # Choose a small max_shard_size for maximum partitioning. spark_builder._repartition_df_if_needed(max_shard_size=1 ) # The new number of partitions should not be greater than the number of rows. assert spark_builder.df.rdd.getNumPartitions() == 1_00
666
0
'''simple docstring''' from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, TensorType __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = { 'openai/imagegpt-small': '', 'openai/imagegpt-medium': '', 'openai/imagegpt-large': '', } class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case__ = "imagegpt" snake_case__ = ["past_key_values"] snake_case__ = { "hidden_size": "n_embd", "max_position_embeddings": "n_positions", "num_attention_heads": "n_head", "num_hidden_layers": "n_layer", } def __init__( self : Tuple , __SCREAMING_SNAKE_CASE : str=512 + 1 , __SCREAMING_SNAKE_CASE : int=32 * 32 , __SCREAMING_SNAKE_CASE : Dict=512 , __SCREAMING_SNAKE_CASE : List[str]=24 , __SCREAMING_SNAKE_CASE : Tuple=8 , __SCREAMING_SNAKE_CASE : Tuple=None , __SCREAMING_SNAKE_CASE : str="quick_gelu" , __SCREAMING_SNAKE_CASE : Optional[int]=0.1 , __SCREAMING_SNAKE_CASE : List[str]=0.1 , __SCREAMING_SNAKE_CASE : Optional[Any]=0.1 , __SCREAMING_SNAKE_CASE : int=1e-5 , __SCREAMING_SNAKE_CASE : Union[str, Any]=0.02 , __SCREAMING_SNAKE_CASE : List[Any]=True , __SCREAMING_SNAKE_CASE : Optional[Any]=True , __SCREAMING_SNAKE_CASE : str=False , __SCREAMING_SNAKE_CASE : List[Any]=False , __SCREAMING_SNAKE_CASE : Optional[Any]=False , **__SCREAMING_SNAKE_CASE : Tuple , ) -> Any: a_ : str = vocab_size a_ : Optional[int] = n_positions a_ : Any = n_embd a_ : Union[str, Any] = n_layer a_ : List[Any] = n_head a_ : str = n_inner a_ : int = activation_function a_ : Optional[int] = resid_pdrop a_ : Dict = embd_pdrop a_ : List[Any] = attn_pdrop a_ : Dict = layer_norm_epsilon a_ : List[str] = initializer_range a_ : Optional[Any] = scale_attn_weights a_ : str = use_cache a_ : Dict = scale_attn_by_inverse_layer_idx a_ : Optional[Any] = reorder_and_upcast_attn a_ : str = tie_word_embeddings super().__init__(tie_word_embeddings=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' @property def SCREAMING_SNAKE_CASE ( self : int ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ] ) def SCREAMING_SNAKE_CASE ( self : List[Any] , __SCREAMING_SNAKE_CASE : "FeatureExtractionMixin" , __SCREAMING_SNAKE_CASE : int = 1 , __SCREAMING_SNAKE_CASE : int = -1 , __SCREAMING_SNAKE_CASE : bool = False , __SCREAMING_SNAKE_CASE : Optional["TensorType"] = None , __SCREAMING_SNAKE_CASE : int = 3 , __SCREAMING_SNAKE_CASE : int = 32 , __SCREAMING_SNAKE_CASE : int = 32 , ) -> Mapping[str, Any]: a_ : Tuple = self._generate_dummy_images(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Tuple = dict(preprocessor(images=__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE ) ) return inputs
703
'''simple docstring''' from collections import OrderedDict from typing import TYPE_CHECKING, Any, List, Mapping, Optional from packaging import version if TYPE_CHECKING: from ... import PreTrainedTokenizer, TensorType from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import is_torch_available, logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = { 'bigscience/bloom': 'https://huggingface.co/bigscience/bloom/resolve/main/config.json', 'bigscience/bloom-560m': 'https://huggingface.co/bigscience/bloom-560m/blob/main/config.json', 'bigscience/bloom-1b1': 'https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json', 'bigscience/bloom-1b7': 'https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json', 'bigscience/bloom-3b': 'https://huggingface.co/bigscience/bloom-3b/blob/main/config.json', 'bigscience/bloom-7b1': 'https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json', } class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "bloom" snake_case__ = ["past_key_values"] snake_case__ = { "num_hidden_layers": "n_layer", "num_attention_heads": "n_head", } def __init__( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : int=25_0880 , __SCREAMING_SNAKE_CASE : Dict=64 , __SCREAMING_SNAKE_CASE : Tuple=2 , __SCREAMING_SNAKE_CASE : int=8 , __SCREAMING_SNAKE_CASE : Any=1e-5 , __SCREAMING_SNAKE_CASE : Optional[Any]=0.02 , __SCREAMING_SNAKE_CASE : Union[str, Any]=True , __SCREAMING_SNAKE_CASE : int=1 , __SCREAMING_SNAKE_CASE : Any=2 , __SCREAMING_SNAKE_CASE : Optional[Any]=False , __SCREAMING_SNAKE_CASE : Optional[Any]=0.0 , __SCREAMING_SNAKE_CASE : str=0.0 , __SCREAMING_SNAKE_CASE : List[Any]=1 , __SCREAMING_SNAKE_CASE : List[str]=False , **__SCREAMING_SNAKE_CASE : str , ) -> Any: a_ : Optional[int] = vocab_size # Backward compatibility with n_embed kwarg a_ : Any = kwargs.pop('''n_embed''' , __SCREAMING_SNAKE_CASE ) a_ : Optional[int] = hidden_size if n_embed is None else n_embed a_ : int = n_layer a_ : str = n_head a_ : Optional[int] = layer_norm_epsilon a_ : Dict = initializer_range a_ : List[str] = use_cache a_ : Dict = pretraining_tp a_ : Optional[Any] = apply_residual_connection_post_layernorm a_ : Optional[Any] = hidden_dropout a_ : List[str] = attention_dropout a_ : Dict = bos_token_id a_ : Optional[int] = eos_token_id a_ : Any = slow_but_exact super().__init__(bos_token_id=__SCREAMING_SNAKE_CASE , eos_token_id=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = version.parse("1.12" ) def __init__( self : Tuple , __SCREAMING_SNAKE_CASE : PretrainedConfig , __SCREAMING_SNAKE_CASE : str = "default" , __SCREAMING_SNAKE_CASE : List[PatchingSpec] = None , __SCREAMING_SNAKE_CASE : bool = False , ) -> Optional[Any]: super().__init__(__SCREAMING_SNAKE_CASE , task=__SCREAMING_SNAKE_CASE , patching_specs=__SCREAMING_SNAKE_CASE , use_past=__SCREAMING_SNAKE_CASE ) if not getattr(self._config , '''pad_token_id''' , __SCREAMING_SNAKE_CASE ): # TODO: how to do that better? a_ : Tuple = 0 @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Mapping[str, Mapping[int, str]]: a_ : Optional[Any] = OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}} ) if self.use_past: # BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344 self.fill_with_past_key_values_(__SCREAMING_SNAKE_CASE , direction='''inputs''' , inverted_values_shape=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = {0: '''batch''', 1: '''past_sequence + sequence'''} else: a_ : Union[str, Any] = {0: '''batch''', 1: '''sequence'''} return common_inputs @property def SCREAMING_SNAKE_CASE ( self : Any ) -> int: return self._config.n_layer @property def SCREAMING_SNAKE_CASE ( self : int ) -> int: return self._config.n_head @property def SCREAMING_SNAKE_CASE ( self : int ) -> float: return 1e-3 def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : "PreTrainedTokenizer" , __SCREAMING_SNAKE_CASE : int = -1 , __SCREAMING_SNAKE_CASE : int = -1 , __SCREAMING_SNAKE_CASE : bool = False , __SCREAMING_SNAKE_CASE : Optional["TensorType"] = None , ) -> Mapping[str, Any]: a_ : Dict = super(__SCREAMING_SNAKE_CASE , self ).generate_dummy_inputs( __SCREAMING_SNAKE_CASE , batch_size=__SCREAMING_SNAKE_CASE , seq_length=__SCREAMING_SNAKE_CASE , is_pair=__SCREAMING_SNAKE_CASE , framework=__SCREAMING_SNAKE_CASE ) # We need to order the input in the way they appears in the forward() a_ : Union[str, Any] = OrderedDict({'''input_ids''': common_inputs['''input_ids''']} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch a_ , a_ : Any = common_inputs['''input_ids'''].shape # Not using the same length for past_key_values a_ : str = seqlen + 2 a_ : Any = self._config.hidden_size // self.num_attention_heads a_ : Optional[int] = ( batch * self.num_attention_heads, head_dim, past_key_values_length, ) a_ : Any = ( batch * self.num_attention_heads, past_key_values_length, head_dim, ) a_ : List[str] = [ (torch.zeros(__SCREAMING_SNAKE_CASE ), torch.zeros(__SCREAMING_SNAKE_CASE )) for _ in range(self.num_layers ) ] a_ : Union[str, Any] = common_inputs['''attention_mask'''] if self.use_past: a_ : Optional[int] = ordered_inputs['''attention_mask'''].dtype a_ : List[Any] = torch.cat( [ordered_inputs['''attention_mask'''], torch.ones(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , dtype=__SCREAMING_SNAKE_CASE )] , dim=1 ) return ordered_inputs @property def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> int: return 13
666
0
'''simple docstring''' from argparse import ArgumentParser from . import BaseTransformersCLICommand def _UpperCAmelCase ( __A : Tuple ): return DownloadCommand(args.model , args.cache_dir , args.force , args.trust_remote_code ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): @staticmethod def SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE : ArgumentParser ) -> Dict: a_ : List[Any] = parser.add_parser('''download''' ) download_parser.add_argument( '''--cache-dir''' , type=__SCREAMING_SNAKE_CASE , default=__SCREAMING_SNAKE_CASE , help='''Path to location to store the models''' ) download_parser.add_argument( '''--force''' , action='''store_true''' , help='''Force the model to be download even if already in cache-dir''' ) download_parser.add_argument( '''--trust-remote-code''' , action='''store_true''' , help='''Whether or not to allow for custom models defined on the Hub in their own modeling files. Use only if you\'ve reviewed the code as it will execute on your local machine''' , ) download_parser.add_argument('''model''' , type=__SCREAMING_SNAKE_CASE , help='''Name of the model to download''' ) download_parser.set_defaults(func=__SCREAMING_SNAKE_CASE ) def __init__( self : List[Any] , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : bool , __SCREAMING_SNAKE_CASE : bool ) -> Tuple: a_ : int = model a_ : Any = cache a_ : List[Any] = force a_ : Optional[Any] = trust_remote_code def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> int: from ..models.auto import AutoModel, AutoTokenizer AutoModel.from_pretrained( self._model , cache_dir=self._cache , force_download=self._force , trust_remote_code=self._trust_remote_code ) AutoTokenizer.from_pretrained( self._model , cache_dir=self._cache , force_download=self._force , trust_remote_code=self._trust_remote_code )
704
'''simple docstring''' import sys __lowerCAmelCase = ( '73167176531330624919225119674426574742355349194934' '96983520312774506326239578318016984801869478851843' '85861560789112949495459501737958331952853208805511' '12540698747158523863050715693290963295227443043557' '66896648950445244523161731856403098711121722383113' '62229893423380308135336276614282806444486645238749' '30358907296290491560440772390713810515859307960866' '70172427121883998797908792274921901699720888093776' '65727333001053367881220235421809751254540594752243' '52584907711670556013604839586446706324415722155397' '53697817977846174064955149290862569321978468622482' '83972241375657056057490261407972968652414535100474' '82166370484403199890008895243450658541227588666881' '16427171479924442928230863465674813919123162824586' '17866458359124566529476545682848912883142607690042' '24219022671055626321111109370544217506941658960408' '07198403850962455444362981230987879927244284909188' '84580156166097919133875499200524063689912560717606' '05886116467109405077541002256983155200055935729725' '71636269561882670428252483600823257530420752963450' ) def _UpperCAmelCase ( __A : str ): a_ : Tuple = 1 for digit in s: product *= int(__A ) return product def _UpperCAmelCase ( __A : str = N ): a_ : Dict = -sys.maxsize - 1 a_ : Optional[int] = n[:13] a_ : str = 13 while cur_index < len(__A ) - 13: if int(n[cur_index] ) >= int(substr[0] ): a_ : Tuple = substr[1:] + n[cur_index] cur_index += 1 else: a_ : Dict = max(__A , str_eval(__A ) ) a_ : List[str] = n[cur_index : cur_index + 13] cur_index += 13 return largest_product if __name__ == "__main__": print(F"""{solution() = }""")
666
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = { 'uclanlp/visualbert-vqa': 'https://huggingface.co/uclanlp/visualbert-vqa/resolve/main/config.json', 'uclanlp/visualbert-vqa-pre': 'https://huggingface.co/uclanlp/visualbert-vqa-pre/resolve/main/config.json', 'uclanlp/visualbert-vqa-coco-pre': ( 'https://huggingface.co/uclanlp/visualbert-vqa-coco-pre/resolve/main/config.json' ), 'uclanlp/visualbert-vcr': 'https://huggingface.co/uclanlp/visualbert-vcr/resolve/main/config.json', 'uclanlp/visualbert-vcr-pre': 'https://huggingface.co/uclanlp/visualbert-vcr-pre/resolve/main/config.json', 'uclanlp/visualbert-vcr-coco-pre': ( 'https://huggingface.co/uclanlp/visualbert-vcr-coco-pre/resolve/main/config.json' ), 'uclanlp/visualbert-nlvr2': 'https://huggingface.co/uclanlp/visualbert-nlvr2/resolve/main/config.json', 'uclanlp/visualbert-nlvr2-pre': 'https://huggingface.co/uclanlp/visualbert-nlvr2-pre/resolve/main/config.json', 'uclanlp/visualbert-nlvr2-coco-pre': ( 'https://huggingface.co/uclanlp/visualbert-nlvr2-coco-pre/resolve/main/config.json' ) # See all VisualBERT models at https://huggingface.co/models?filter=visual_bert } class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "visual_bert" def __init__( self : List[str] , __SCREAMING_SNAKE_CASE : str=3_0522 , __SCREAMING_SNAKE_CASE : Union[str, Any]=768 , __SCREAMING_SNAKE_CASE : Optional[Any]=512 , __SCREAMING_SNAKE_CASE : Union[str, Any]=12 , __SCREAMING_SNAKE_CASE : Dict=12 , __SCREAMING_SNAKE_CASE : List[Any]=3072 , __SCREAMING_SNAKE_CASE : List[Any]="gelu" , __SCREAMING_SNAKE_CASE : Optional[int]=0.1 , __SCREAMING_SNAKE_CASE : Dict=0.1 , __SCREAMING_SNAKE_CASE : Optional[int]=512 , __SCREAMING_SNAKE_CASE : Union[str, Any]=2 , __SCREAMING_SNAKE_CASE : int=0.02 , __SCREAMING_SNAKE_CASE : Dict=1e-12 , __SCREAMING_SNAKE_CASE : int=False , __SCREAMING_SNAKE_CASE : Dict=True , __SCREAMING_SNAKE_CASE : Optional[Any]=1 , __SCREAMING_SNAKE_CASE : Dict=0 , __SCREAMING_SNAKE_CASE : List[Any]=2 , **__SCREAMING_SNAKE_CASE : int , ) -> str: super().__init__(pad_token_id=__SCREAMING_SNAKE_CASE , bos_token_id=__SCREAMING_SNAKE_CASE , eos_token_id=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) a_ : List[str] = vocab_size a_ : Dict = max_position_embeddings a_ : int = hidden_size a_ : List[str] = visual_embedding_dim a_ : str = num_hidden_layers a_ : Dict = num_attention_heads a_ : Dict = intermediate_size a_ : Optional[int] = hidden_act a_ : List[str] = hidden_dropout_prob a_ : Optional[int] = attention_probs_dropout_prob a_ : Optional[Any] = initializer_range a_ : int = type_vocab_size a_ : Union[str, Any] = layer_norm_eps a_ : List[Any] = bypass_transformer a_ : Union[str, Any] = special_visual_initialize
705
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( __A : list[int] ): a_ : int = len(__A ) // 2 # choose the middle 3 elements a_ : Dict = lst[m - 1 : m + 2] # if middle element is peak if three[1] > three[0] and three[1] > three[2]: return three[1] # if increasing, recurse on right elif three[0] < three[2]: if len(lst[:m] ) == 2: m -= 1 return peak(lst[m:] ) # decreasing else: if len(lst[:m] ) == 2: m += 1 return peak(lst[:m] ) if __name__ == "__main__": import doctest doctest.testmod()
666
0
'''simple docstring''' import inspect from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel, VQModel from ...schedulers import DDIMScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): def __init__( self : List[Any] , __SCREAMING_SNAKE_CASE : VQModel , __SCREAMING_SNAKE_CASE : UNetaDModel , __SCREAMING_SNAKE_CASE : DDIMScheduler ) -> Tuple: super().__init__() self.register_modules(vqvae=__SCREAMING_SNAKE_CASE , unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) @torch.no_grad() def __call__( self : List[Any] , __SCREAMING_SNAKE_CASE : int = 1 , __SCREAMING_SNAKE_CASE : Optional[Union[torch.Generator, List[torch.Generator]]] = None , __SCREAMING_SNAKE_CASE : float = 0.0 , __SCREAMING_SNAKE_CASE : int = 50 , __SCREAMING_SNAKE_CASE : Optional[str] = "pil" , __SCREAMING_SNAKE_CASE : bool = True , **__SCREAMING_SNAKE_CASE : List[Any] , ) -> Union[Tuple, ImagePipelineOutput]: a_ : List[str] = randn_tensor( (batch_size, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size) , generator=__SCREAMING_SNAKE_CASE , ) a_ : Tuple = latents.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler a_ : Dict = latents * self.scheduler.init_noise_sigma self.scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature a_ : Tuple = '''eta''' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) a_ : Tuple = {} if accepts_eta: a_ : int = eta for t in self.progress_bar(self.scheduler.timesteps ): a_ : Union[str, Any] = self.scheduler.scale_model_input(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # predict the noise residual a_ : Tuple = self.unet(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).sample # compute the previous noisy sample x_t -> x_t-1 a_ : str = self.scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample # decode the image latents with the VAE a_ : Tuple = self.vqvae.decode(__SCREAMING_SNAKE_CASE ).sample a_ : Dict = (image / 2 + 0.5).clamp(0 , 1 ) a_ : Tuple = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": a_ : List[str] = self.numpy_to_pil(__SCREAMING_SNAKE_CASE ) if not return_dict: return (image,) return ImagePipelineOutput(images=__SCREAMING_SNAKE_CASE )
706
'''simple docstring''' 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 SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = LongformerTokenizer snake_case__ = True snake_case__ = LongformerTokenizerFast snake_case__ = True def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt a_ : Tuple = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] a_ : Optional[Any] = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) a_ : Union[str, Any] = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] a_ : Any = {'''unk_token''': '''<unk>'''} a_ : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) a_ : Optional[int] = 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(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : Any , **__SCREAMING_SNAKE_CASE : Any ) -> int: kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , **__SCREAMING_SNAKE_CASE : List[Any] ) -> List[str]: kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : List[Any] ) -> Any: a_ : Union[str, Any] = '''lower newer''' a_ : List[Any] = '''lower newer''' return input_text, output_text def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: a_ : Optional[Any] = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) a_ : List[str] = '''lower newer''' a_ : str = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] a_ : Optional[int] = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) # , add_prefix_space=True) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Dict = tokens + [tokenizer.unk_token] a_ : Any = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Optional[Any]: a_ : Union[str, Any] = self.get_tokenizer() self.assertListEqual(tokenizer.encode('''Hello world!''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) , [0, 3_1414, 232, 328, 2] ) self.assertListEqual( tokenizer.encode('''Hello world! cécé herlolip 418''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) , [0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2] , ) @slow def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[Any]: a_ : Dict = self.tokenizer_class.from_pretrained('''allenai/longformer-base-4096''' ) a_ : Tuple = tokenizer.encode('''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer.encode('''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.encode( '''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.encode( '''sequence builders''' , '''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : List[str] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def SCREAMING_SNAKE_CASE ( self : str ) -> int: a_ : str = self.get_tokenizer() a_ : int = '''Encode this sequence.''' a_ : List[str] = tokenizer.byte_encoder[''' '''.encode('''utf-8''' )[0]] # Testing encoder arguments a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) tokenizer.add_special_tokens({'''bos_token''': '''<s>'''} ) a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Testing spaces after special tokens a_ : Optional[Any] = '''<mask>''' tokenizer.add_special_tokens( {'''mask_token''': AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE )} ) # mask token has a left space a_ : Optional[int] = tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) a_ : List[Any] = '''Encode <mask> sequence''' a_ : List[str] = '''Encode <mask>sequence''' a_ : int = tokenizer.encode(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = encoded.index(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer.encode(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = encoded.index(__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Union[str, Any]: pass def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Any: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): a_ : Any = self.rust_tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) a_ : Any = self.tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) a_ : str = '''A, <mask> AllenNLP sentence.''' a_ : List[Any] = tokenizer_r.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer_p.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE ) # 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'''] ) , ) a_ : str = tokenizer_r.convert_ids_to_tokens(tokens_r['''input_ids'''] ) a_ : Tuple = 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, 250, 6, 5_0264, 3823, 487, 2_1992, 3645, 4, 2] ) self.assertSequenceEqual(tokens_r['''input_ids'''] , [0, 250, 6, 5_0264, 3823, 487, 2_1992, 3645, 4, 2] ) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>'''] ) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>'''] ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[Any]: for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): a_ : Any = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) a_ : str = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state['''add_prefix_space'''] , __SCREAMING_SNAKE_CASE ) self.assertEqual(post_processor_state['''add_prefix_space'''] , __SCREAMING_SNAKE_CASE ) self.assertEqual(post_processor_state['''trim_offsets'''] , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[int]: # 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})' ): a_ : Dict = '''hello''' # `hello` is a token in the vocabulary of `pretrained_name` a_ : Union[str, Any] = f'{text_of_1_token} {text_of_1_token}' a_ : Any = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ) + 1, len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Any = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ) + 1, len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : int = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ), len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Tuple = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ), len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Union[str, Any] = 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)), # ) a_ : str = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : int = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ) + 1, 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : int = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ), 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Optional[int] = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : int = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ), 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , )
666
0
'''simple docstring''' from typing import List, Optional, Union import numpy as np import PIL.Image from ...image_processing_utils import BaseImageProcessor, BatchFeature from ...image_transforms import rescale, resize, to_channel_dimension_format from ...image_utils import ( ChannelDimension, PILImageResampling, get_image_size, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, logging __lowerCAmelCase = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = ["pixel_values"] def __init__( self : Dict , __SCREAMING_SNAKE_CASE : bool = True , __SCREAMING_SNAKE_CASE : int = 32 , __SCREAMING_SNAKE_CASE : str=PILImageResampling.BILINEAR , __SCREAMING_SNAKE_CASE : bool = True , **__SCREAMING_SNAKE_CASE : Optional[int] , ) -> None: a_ : Union[str, Any] = do_resize a_ : List[Any] = do_rescale a_ : Dict = size_divisor a_ : List[str] = resample super().__init__(**__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Tuple , __SCREAMING_SNAKE_CASE : np.ndarray , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Optional[ChannelDimension] = None , **__SCREAMING_SNAKE_CASE : int ) -> np.ndarray: a_ : List[Any] = get_image_size(__SCREAMING_SNAKE_CASE ) # Rounds the height and width down to the closest multiple of size_divisor a_ : Any = height // size_divisor * size_divisor a_ : List[str] = width // size_divisor * size_divisor a_ : Any = resize(__SCREAMING_SNAKE_CASE , (new_h, new_w) , resample=__SCREAMING_SNAKE_CASE , data_format=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) return image def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : np.ndarray , __SCREAMING_SNAKE_CASE : float , __SCREAMING_SNAKE_CASE : Optional[ChannelDimension] = None , **__SCREAMING_SNAKE_CASE : List[Any] ) -> np.ndarray: return rescale(image=__SCREAMING_SNAKE_CASE , scale=__SCREAMING_SNAKE_CASE , data_format=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : Union["PIL.Image.Image", TensorType, List["PIL.Image.Image"], List[TensorType]] , __SCREAMING_SNAKE_CASE : Optional[bool] = None , __SCREAMING_SNAKE_CASE : Optional[int] = None , __SCREAMING_SNAKE_CASE : Dict=None , __SCREAMING_SNAKE_CASE : Optional[bool] = None , __SCREAMING_SNAKE_CASE : Optional[Union[TensorType, str]] = None , __SCREAMING_SNAKE_CASE : ChannelDimension = ChannelDimension.FIRST , **__SCREAMING_SNAKE_CASE : Optional[Any] , ) -> BatchFeature: a_ : Union[str, Any] = do_resize if do_resize is not None else self.do_resize a_ : Optional[int] = do_rescale if do_rescale is not None else self.do_rescale a_ : Dict = size_divisor if size_divisor is not None else self.size_divisor a_ : Optional[Any] = resample if resample is not None else self.resample if do_resize and size_divisor is None: raise ValueError('''size_divisor is required for resizing''' ) a_ : Dict = make_list_of_images(__SCREAMING_SNAKE_CASE ) if not valid_images(__SCREAMING_SNAKE_CASE ): raise ValueError('''Invalid image(s)''' ) # All transformations expect numpy arrays. a_ : Optional[int] = [to_numpy_array(__SCREAMING_SNAKE_CASE ) for img in images] if do_resize: a_ : List[str] = [self.resize(__SCREAMING_SNAKE_CASE , size_divisor=__SCREAMING_SNAKE_CASE , resample=__SCREAMING_SNAKE_CASE ) for image in images] if do_rescale: a_ : int = [self.rescale(__SCREAMING_SNAKE_CASE , scale=1 / 255 ) for image in images] a_ : str = [to_channel_dimension_format(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) for image in images] a_ : List[Any] = {'''pixel_values''': images} return BatchFeature(data=__SCREAMING_SNAKE_CASE , tensor_type=__SCREAMING_SNAKE_CASE )
707
'''simple docstring''' import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = {'vocab_file': 'sentencepiece.bpe.model'} __lowerCAmelCase = { 'vocab_file': { 'moussaKam/mbarthez': 'https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model', 'moussaKam/barthez': 'https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model', 'moussaKam/barthez-orangesum-title': ( 'https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model' ), }, } __lowerCAmelCase = { 'moussaKam/mbarthez': 1_024, 'moussaKam/barthez': 1_024, 'moussaKam/barthez-orangesum-title': 1_024, } __lowerCAmelCase = '▁' class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = VOCAB_FILES_NAMES snake_case__ = PRETRAINED_VOCAB_FILES_MAP snake_case__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case__ = ["input_ids", "attention_mask"] def __init__( self : Dict , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Dict="<s>" , __SCREAMING_SNAKE_CASE : List[Any]="</s>" , __SCREAMING_SNAKE_CASE : List[str]="</s>" , __SCREAMING_SNAKE_CASE : List[str]="<s>" , __SCREAMING_SNAKE_CASE : Dict="<unk>" , __SCREAMING_SNAKE_CASE : int="<pad>" , __SCREAMING_SNAKE_CASE : Tuple="<mask>" , __SCREAMING_SNAKE_CASE : Optional[Dict[str, Any]] = None , **__SCREAMING_SNAKE_CASE : Optional[Any] , ) -> None: # Mask token behave like a normal word, i.e. include the space before it a_ : Tuple = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else mask_token a_ : List[str] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , sp_model_kwargs=self.sp_model_kwargs , **__SCREAMING_SNAKE_CASE , ) a_ : Tuple = vocab_file a_ : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(__SCREAMING_SNAKE_CASE ) ) a_ : Optional[Any] = {'''<s>''': 0, '''<pad>''': 1, '''</s>''': 2, '''<unk>''': 3} a_ : Any = len(self.sp_model ) - 1 a_ : Optional[Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None ) -> List[int]: if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] a_ : List[str] = [self.cls_token_id] a_ : Optional[Any] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None , __SCREAMING_SNAKE_CASE : bool = False ) -> List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__SCREAMING_SNAKE_CASE , token_ids_a=__SCREAMING_SNAKE_CASE , already_has_special_tokens=__SCREAMING_SNAKE_CASE ) if token_ids_a is None: return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1, 1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None ) -> List[int]: a_ : List[str] = [self.sep_token_id] a_ : Any = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def SCREAMING_SNAKE_CASE ( self : Any ) -> Union[str, Any]: return len(self.sp_model ) def SCREAMING_SNAKE_CASE ( self : str ) -> Tuple: a_ : int = {self.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def SCREAMING_SNAKE_CASE ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : str ) -> List[str]: return self.sp_model.encode(__SCREAMING_SNAKE_CASE , out_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : List[Any] ) -> Dict: if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] a_ : Optional[int] = self.sp_model.PieceToId(__SCREAMING_SNAKE_CASE ) return spm_id if spm_id else self.unk_token_id def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : int ) -> str: if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Optional[int]: a_ : Dict = [] a_ : List[Any] = '''''' a_ : Dict = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) + token a_ : Dict = True a_ : Optional[Any] = [] else: current_sub_tokens.append(__SCREAMING_SNAKE_CASE ) a_ : Tuple = False out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) return out_string.strip() def __getstate__( self : Dict ) -> int: a_ : Dict = self.__dict__.copy() a_ : List[str] = None return state def __setstate__( self : Dict , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Any: a_ : Optional[Any] = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): a_ : Union[str, Any] = {} a_ : int = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Optional[str] = None ) -> Tuple[str]: if not os.path.isdir(__SCREAMING_SNAKE_CASE ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return a_ : Union[str, Any] = os.path.join( __SCREAMING_SNAKE_CASE , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__SCREAMING_SNAKE_CASE ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __SCREAMING_SNAKE_CASE ) elif not os.path.isfile(self.vocab_file ): with open(__SCREAMING_SNAKE_CASE , '''wb''' ) as fi: a_ : Any = self.sp_model.serialized_model_proto() fi.write(__SCREAMING_SNAKE_CASE ) return (out_vocab_file,)
666
0
'''simple docstring''' 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 SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): 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[Any] , __SCREAMING_SNAKE_CASE : Optional[int]=4_0478 , __SCREAMING_SNAKE_CASE : List[str]=512 , __SCREAMING_SNAKE_CASE : Any=768 , __SCREAMING_SNAKE_CASE : Optional[int]=12 , __SCREAMING_SNAKE_CASE : Union[str, Any]=12 , __SCREAMING_SNAKE_CASE : Any="gelu" , __SCREAMING_SNAKE_CASE : int=0.1 , __SCREAMING_SNAKE_CASE : List[Any]=0.1 , __SCREAMING_SNAKE_CASE : List[str]=0.1 , __SCREAMING_SNAKE_CASE : List[Any]=1e-5 , __SCREAMING_SNAKE_CASE : str=0.02 , __SCREAMING_SNAKE_CASE : int="cls_index" , __SCREAMING_SNAKE_CASE : str=True , __SCREAMING_SNAKE_CASE : Optional[int]=None , __SCREAMING_SNAKE_CASE : List[Any]=True , __SCREAMING_SNAKE_CASE : List[str]=0.1 , **__SCREAMING_SNAKE_CASE : List[str] , ) -> int: a_ : Optional[int] = vocab_size a_ : str = n_positions a_ : Tuple = n_embd a_ : Dict = n_layer a_ : Optional[Any] = n_head a_ : List[str] = afn a_ : str = resid_pdrop a_ : Tuple = embd_pdrop a_ : List[Any] = attn_pdrop a_ : List[Any] = layer_norm_epsilon a_ : Tuple = initializer_range a_ : Optional[Any] = summary_type a_ : List[str] = summary_use_proj a_ : List[str] = summary_activation a_ : List[str] = summary_first_dropout a_ : Optional[Any] = summary_proj_to_labels super().__init__(**__SCREAMING_SNAKE_CASE )
708
'''simple docstring''' def _UpperCAmelCase ( __A : str , __A : str ): def get_matched_characters(__A : str , __A : str ) -> str: a_ : Union[str, Any] = [] a_ : int = min(len(_stra ) , len(_stra ) ) // 2 for i, l in enumerate(_stra ): a_ : Any = int(max(0 , i - limit ) ) a_ : Union[str, Any] = int(min(i + limit + 1 , len(_stra ) ) ) if l in _stra[left:right]: matched.append(__A ) a_ : Any = f'{_stra[0:_stra.index(__A )]} {_stra[_stra.index(__A ) + 1:]}' return "".join(__A ) # matching characters a_ : Optional[Any] = get_matched_characters(__A , __A ) a_ : int = get_matched_characters(__A , __A ) a_ : Any = len(__A ) # transposition a_ : List[Any] = ( len([(ca, ca) for ca, ca in zip(__A , __A ) if ca != ca] ) // 2 ) if not match_count: a_ : Dict = 0.0 else: a_ : Optional[int] = ( 1 / 3 * ( match_count / len(__A ) + match_count / len(__A ) + (match_count - transpositions) / match_count ) ) # common prefix up to 4 characters a_ : List[str] = 0 for ca, ca in zip(stra[:4] , stra[:4] ): if ca == ca: prefix_len += 1 else: break return jaro + 0.1 * prefix_len * (1 - jaro) if __name__ == "__main__": import doctest doctest.testmod() print(jaro_winkler('hello', 'world'))
666
0
'''simple docstring''' import inspect import unittest import numpy as np from transformers import ViTConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_configuration_common import ConfigTester from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor if is_flax_available(): import jax from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def __init__( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Any=13 , __SCREAMING_SNAKE_CASE : Union[str, Any]=30 , __SCREAMING_SNAKE_CASE : str=2 , __SCREAMING_SNAKE_CASE : Any=3 , __SCREAMING_SNAKE_CASE : Optional[Any]=True , __SCREAMING_SNAKE_CASE : int=True , __SCREAMING_SNAKE_CASE : List[str]=32 , __SCREAMING_SNAKE_CASE : Union[str, Any]=5 , __SCREAMING_SNAKE_CASE : Any=4 , __SCREAMING_SNAKE_CASE : Tuple=37 , __SCREAMING_SNAKE_CASE : str="gelu" , __SCREAMING_SNAKE_CASE : Optional[int]=0.1 , __SCREAMING_SNAKE_CASE : List[str]=0.1 , __SCREAMING_SNAKE_CASE : Optional[int]=10 , __SCREAMING_SNAKE_CASE : str=0.02 , ) -> Optional[Any]: a_ : Optional[Any] = parent a_ : Optional[int] = batch_size a_ : int = image_size a_ : int = patch_size a_ : Optional[Any] = num_channels a_ : Any = is_training a_ : Union[str, Any] = use_labels a_ : List[Any] = hidden_size a_ : Optional[int] = num_hidden_layers a_ : Union[str, Any] = num_attention_heads a_ : List[str] = intermediate_size a_ : Dict = hidden_act a_ : Optional[Any] = hidden_dropout_prob a_ : Optional[int] = attention_probs_dropout_prob a_ : List[str] = type_sequence_label_size a_ : Union[str, Any] = initializer_range # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) a_ : Tuple = (image_size // patch_size) ** 2 a_ : Optional[Any] = num_patches + 1 def SCREAMING_SNAKE_CASE ( self : str ) -> Union[str, Any]: a_ : List[str] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) a_ : Any = ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=__SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , ) return config, pixel_values def SCREAMING_SNAKE_CASE ( self : Tuple , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : Dict ) -> Optional[Any]: a_ : Tuple = FlaxViTModel(config=__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = model(__SCREAMING_SNAKE_CASE ) # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) a_ : str = (self.image_size, self.image_size) a_ : Union[str, Any] = (self.patch_size, self.patch_size) a_ : int = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, num_patches + 1, self.hidden_size) ) def SCREAMING_SNAKE_CASE ( self : int , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : int ) -> Union[str, Any]: a_ : Dict = self.type_sequence_label_size a_ : Tuple = FlaxViTForImageClassification(config=__SCREAMING_SNAKE_CASE ) a_ : List[str] = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images a_ : int = 1 a_ : Optional[Any] = FlaxViTForImageClassification(__SCREAMING_SNAKE_CASE ) a_ : List[Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) a_ : Optional[int] = model(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> int: a_ : str = self.prepare_config_and_inputs() ( a_ ) : Tuple = config_and_inputs a_ : Dict = {'''pixel_values''': pixel_values} return config, inputs_dict @require_flax class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = (FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else () def SCREAMING_SNAKE_CASE ( self : str ) -> None: a_ : List[Any] = FlaxViTModelTester(self ) a_ : List[str] = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , has_text_modality=__SCREAMING_SNAKE_CASE , hidden_size=37 ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> List[Any]: self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE ( self : str ) -> Any: a_ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> List[Any]: a_ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Any ) -> Union[str, Any]: a_ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a_ : Dict = model_class(__SCREAMING_SNAKE_CASE ) a_ : Any = inspect.signature(model.__call__ ) # signature.parameters is an OrderedDict => so arg_names order is deterministic a_ : Optional[int] = [*signature.parameters.keys()] a_ : Union[str, Any] = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Tuple: a_ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): a_ : str = self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : int = model_class(__SCREAMING_SNAKE_CASE ) @jax.jit def model_jitted(__SCREAMING_SNAKE_CASE : Tuple , **__SCREAMING_SNAKE_CASE : int ): return model(pixel_values=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) with self.subTest('''JIT Enabled''' ): a_ : int = model_jitted(**__SCREAMING_SNAKE_CASE ).to_tuple() with self.subTest('''JIT Disabled''' ): with jax.disable_jit(): a_ : Optional[int] = model_jitted(**__SCREAMING_SNAKE_CASE ).to_tuple() self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , len(__SCREAMING_SNAKE_CASE ) ) for jitted_output, output in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertEqual(jitted_output.shape , output.shape ) @slow def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> List[str]: for model_class_name in self.all_model_classes: a_ : Union[str, Any] = model_class_name.from_pretrained('''google/vit-base-patch16-224''' ) a_ : Optional[int] = model(np.ones((1, 3, 224, 224) ) ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE )
709
'''simple docstring''' import torch from transformers import AutoModel class SCREAMING_SNAKE_CASE ( torch.nn.Module ): def __init__( self : Optional[int] , __SCREAMING_SNAKE_CASE : int="sayef/fsner-bert-base-uncased" ) -> str: super(__SCREAMING_SNAKE_CASE , self ).__init__() a_ : str = AutoModel.from_pretrained(__SCREAMING_SNAKE_CASE , return_dict=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = torch.nn.CosineSimilarity(3 , 1e-08 ) a_ : Dict = torch.nn.Softmax(dim=1 ) def SCREAMING_SNAKE_CASE ( self : str , **__SCREAMING_SNAKE_CASE : int ) -> str: return self.bert(**__SCREAMING_SNAKE_CASE ).last_hidden_state def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : List[Any] ) -> Optional[int]: return token_embeddings.sum(2 , keepdim=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : int=1 ) -> Dict: return self.softmax(T * self.cos(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[int] ) -> Any: a_ : Dict = W_supports['''sizes'''].tolist() a_ : Tuple = W_supports['''start_token_id'''].item() a_ : List[Any] = W_supports['''end_token_id'''].item() del W_supports["sizes"] del W_supports["start_token_id"] del W_supports["end_token_id"] a_ : int = self.BERT(**__SCREAMING_SNAKE_CASE ) a_ : Any = self.BERT(**__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = None a_ : Tuple = None a_ : List[str] = W_supports['''input_ids'''] == start_token_id a_ : Dict = W_supports['''input_ids'''] == end_token_id for i, size in enumerate(__SCREAMING_SNAKE_CASE ): if i == 0: a_ : str = 0 else: a_ : str = support_sizes[i - 1] a_ : Union[str, Any] = S[s : s + size][start_token_masks[s : s + size]] a_ : Tuple = S[s : s + size][end_token_masks[s : s + size]] a_ : Tuple = torch.matmul(q[i] , s_start.T ).sum(1 ).softmax(0 ) a_ : Optional[Any] = torch.matmul(q[i] , s_end.T ).sum(1 ).softmax(0 ) if p_starts is not None: a_ : Any = torch.vstack((p_starts, p_start) ) a_ : Dict = torch.vstack((p_ends, p_end) ) else: a_ : Optional[int] = p_start a_ : List[Any] = p_end return p_starts, p_ends
666
0
'''simple docstring''' import io import math from typing import Dict, Optional, Union import numpy as np from huggingface_hub import hf_hub_download from ...image_processing_utils import BaseImageProcessor, BatchFeature from ...image_transforms import convert_to_rgb, normalize, to_channel_dimension_format, to_pil_image from ...image_utils import ( ChannelDimension, ImageInput, get_image_size, infer_channel_dimension_format, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_vision_available, logging from ...utils.import_utils import requires_backends if is_vision_available(): import textwrap from PIL import Image, ImageDraw, ImageFont if is_torch_available(): import torch from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11 else: __lowerCAmelCase = False __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = 'ybelkada/fonts' def _UpperCAmelCase ( ): if is_torch_available() and not is_torch_greater_or_equal_than_1_11: raise ImportError( f'You are using torch=={torch.__version__}, but torch>=1.11.0 is required to use ' '''Pix2StructImageProcessor. Please upgrade torch.''' ) def _UpperCAmelCase ( __A : Any , __A : Union[str, Any] , __A : int ): requires_backends(__A , ['''torch'''] ) _check_torch_version() a_ : Union[str, Any] = image_tensor.unsqueeze(0 ) a_ : List[str] = torch.nn.functional.unfold(__A , (patch_height, patch_width) , stride=(patch_height, patch_width) ) a_ : List[str] = patches.reshape(image_tensor.size(0 ) , image_tensor.size(1 ) , __A , __A , -1 ) a_ : Any = patches.permute(0 , 4 , 2 , 3 , 1 ).reshape( image_tensor.size(2 ) // patch_height , image_tensor.size(3 ) // patch_width , image_tensor.size(1 ) * patch_height * patch_width , ) return patches.unsqueeze(0 ) def _UpperCAmelCase ( __A : str , __A : int = 36 , __A : str = "black" , __A : str = "white" , __A : int = 5 , __A : int = 5 , __A : int = 5 , __A : int = 5 , __A : Optional[bytes] = None , __A : Optional[str] = None , ): requires_backends(__A , '''vision''' ) # Add new lines so that each line is no more than 80 characters. a_ : List[Any] = textwrap.TextWrapper(width=80 ) a_ : int = wrapper.wrap(text=__A ) a_ : Optional[Any] = '''\n'''.join(__A ) if font_bytes is not None and font_path is None: a_ : List[Any] = io.BytesIO(__A ) elif font_path is not None: a_ : Any = font_path else: a_ : int = hf_hub_download(__A , '''Arial.TTF''' ) a_ : Optional[Any] = ImageFont.truetype(__A , encoding='''UTF-8''' , size=__A ) # Use a temporary canvas to determine the width and height in pixels when # rendering the text. a_ : Tuple = ImageDraw.Draw(Image.new('''RGB''' , (1, 1) , __A ) ) a_ : Optional[Any] = temp_draw.textbbox((0, 0) , __A , __A ) # Create the actual image with a bit of padding around the text. a_ : Tuple = text_width + left_padding + right_padding a_ : int = text_height + top_padding + bottom_padding a_ : Union[str, Any] = Image.new('''RGB''' , (image_width, image_height) , __A ) a_ : Dict = ImageDraw.Draw(__A ) draw.text(xy=(left_padding, top_padding) , text=__A , fill=__A , font=__A ) return image def _UpperCAmelCase ( __A : np.ndarray , __A : str , **__A : int ): requires_backends(__A , '''vision''' ) # Convert to PIL image if necessary a_ : Dict = to_pil_image(__A ) a_ : int = render_text(__A , **__A ) a_ : List[Any] = max(header_image.width , image.width ) a_ : List[Any] = int(image.height * (new_width / image.width) ) a_ : Optional[int] = int(header_image.height * (new_width / header_image.width) ) a_ : Union[str, Any] = Image.new('''RGB''' , (new_width, new_height + new_header_height) , '''white''' ) new_image.paste(header_image.resize((new_width, new_header_height) ) , (0, 0) ) new_image.paste(image.resize((new_width, new_height) ) , (0, new_header_height) ) # Convert back to the original framework if necessary a_ : Optional[int] = to_numpy_array(__A ) if infer_channel_dimension_format(__A ) == ChannelDimension.LAST: a_ : Optional[Any] = to_channel_dimension_format(__A , ChannelDimension.LAST ) return new_image class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = ["flattened_patches"] def __init__( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : bool = True , __SCREAMING_SNAKE_CASE : bool = True , __SCREAMING_SNAKE_CASE : Dict[str, int] = None , __SCREAMING_SNAKE_CASE : int = 2048 , __SCREAMING_SNAKE_CASE : bool = False , **__SCREAMING_SNAKE_CASE : List[Any] , ) -> None: super().__init__(**__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = patch_size if patch_size is not None else {'''height''': 16, '''width''': 16} a_ : int = do_normalize a_ : Any = do_convert_rgb a_ : List[Any] = max_patches a_ : Any = is_vqa def SCREAMING_SNAKE_CASE ( self : int , __SCREAMING_SNAKE_CASE : np.ndarray , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : dict , **__SCREAMING_SNAKE_CASE : List[Any] ) -> np.ndarray: requires_backends(self.extract_flattened_patches , '''torch''' ) _check_torch_version() # convert to torch a_ : str = to_channel_dimension_format(__SCREAMING_SNAKE_CASE , ChannelDimension.FIRST ) a_ : Any = torch.from_numpy(__SCREAMING_SNAKE_CASE ) a_ : int = patch_size['''height'''], patch_size['''width'''] a_ : Dict = get_image_size(__SCREAMING_SNAKE_CASE ) # maximize scale s.t. a_ : Optional[Any] = math.sqrt(max_patches * (patch_height / image_height) * (patch_width / image_width) ) a_ : Optional[int] = max(min(math.floor(scale * image_height / patch_height ) , __SCREAMING_SNAKE_CASE ) , 1 ) a_ : List[Any] = max(min(math.floor(scale * image_width / patch_width ) , __SCREAMING_SNAKE_CASE ) , 1 ) a_ : List[Any] = max(num_feasible_rows * patch_height , 1 ) a_ : Dict = max(num_feasible_cols * patch_width , 1 ) a_ : int = torch.nn.functional.interpolate( image.unsqueeze(0 ) , size=(resized_height, resized_width) , mode='''bilinear''' , align_corners=__SCREAMING_SNAKE_CASE , antialias=__SCREAMING_SNAKE_CASE , ).squeeze(0 ) # [1, rows, columns, patch_height * patch_width * image_channels] a_ : Optional[int] = torch_extract_patches(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : int = patches.shape a_ : List[Any] = patches_shape[1] a_ : str = patches_shape[2] a_ : Any = patches_shape[3] # [rows * columns, patch_height * patch_width * image_channels] a_ : int = patches.reshape([rows * columns, depth] ) # [rows * columns, 1] a_ : List[Any] = torch.arange(__SCREAMING_SNAKE_CASE ).reshape([rows, 1] ).repeat(1 , __SCREAMING_SNAKE_CASE ).reshape([rows * columns, 1] ) a_ : List[Any] = torch.arange(__SCREAMING_SNAKE_CASE ).reshape([1, columns] ).repeat(__SCREAMING_SNAKE_CASE , 1 ).reshape([rows * columns, 1] ) # Offset by 1 so the ids do not contain zeros, which represent padding. row_ids += 1 col_ids += 1 # Prepare additional patch features. # [rows * columns, 1] a_ : Union[str, Any] = row_ids.to(torch.floataa ) a_ : Optional[int] = col_ids.to(torch.floataa ) # [rows * columns, 2 + patch_height * patch_width * image_channels] a_ : Dict = torch.cat([row_ids, col_ids, patches] , -1 ) # [max_patches, 2 + patch_height * patch_width * image_channels] a_ : Optional[Any] = torch.nn.functional.pad(__SCREAMING_SNAKE_CASE , [0, 0, 0, max_patches - (rows * columns)] ).float() a_ : Tuple = to_numpy_array(__SCREAMING_SNAKE_CASE ) return result def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : np.ndarray , __SCREAMING_SNAKE_CASE : Optional[Union[str, ChannelDimension]] = None , **__SCREAMING_SNAKE_CASE : Optional[int] ) -> np.ndarray: if image.dtype == np.uinta: a_ : Dict = image.astype(np.floataa ) # take mean across the whole `image` a_ : Any = np.mean(__SCREAMING_SNAKE_CASE ) a_ : Any = np.std(__SCREAMING_SNAKE_CASE ) a_ : Dict = max(__SCREAMING_SNAKE_CASE , 1.0 / math.sqrt(np.prod(image.shape ) ) ) return normalize(__SCREAMING_SNAKE_CASE , mean=__SCREAMING_SNAKE_CASE , std=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : ImageInput , __SCREAMING_SNAKE_CASE : Optional[str] = None , __SCREAMING_SNAKE_CASE : bool = None , __SCREAMING_SNAKE_CASE : Optional[bool] = None , __SCREAMING_SNAKE_CASE : Optional[int] = None , __SCREAMING_SNAKE_CASE : Optional[Dict[str, int]] = None , __SCREAMING_SNAKE_CASE : Optional[Union[str, TensorType]] = None , __SCREAMING_SNAKE_CASE : ChannelDimension = ChannelDimension.FIRST , **__SCREAMING_SNAKE_CASE : List[str] , ) -> ImageInput: a_ : Union[str, Any] = do_normalize if do_normalize is not None else self.do_normalize a_ : Optional[int] = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb a_ : Dict = patch_size if patch_size is not None else self.patch_size a_ : Union[str, Any] = max_patches if max_patches is not None else self.max_patches a_ : Union[str, Any] = self.is_vqa if kwargs.get('''data_format''' , __SCREAMING_SNAKE_CASE ) is not None: raise ValueError('''data_format is not an accepted input as the outputs are ''' ) a_ : int = make_list_of_images(__SCREAMING_SNAKE_CASE ) if not valid_images(__SCREAMING_SNAKE_CASE ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) # PIL RGBA images are converted to RGB if do_convert_rgb: a_ : Optional[Any] = [convert_to_rgb(__SCREAMING_SNAKE_CASE ) for image in images] # All transformations expect numpy arrays. a_ : str = [to_numpy_array(__SCREAMING_SNAKE_CASE ) for image in images] if is_vqa: if header_text is None: raise ValueError('''A header text must be provided for VQA models.''' ) a_ : int = kwargs.pop('''font_bytes''' , __SCREAMING_SNAKE_CASE ) a_ : Tuple = kwargs.pop('''font_path''' , __SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): a_ : str = [header_text] * len(__SCREAMING_SNAKE_CASE ) a_ : int = [ render_header(__SCREAMING_SNAKE_CASE , header_text[i] , font_bytes=__SCREAMING_SNAKE_CASE , font_path=__SCREAMING_SNAKE_CASE ) for i, image in enumerate(__SCREAMING_SNAKE_CASE ) ] if do_normalize: a_ : int = [self.normalize(image=__SCREAMING_SNAKE_CASE ) for image in images] # convert to torch tensor and permute a_ : Tuple = [ self.extract_flattened_patches(image=__SCREAMING_SNAKE_CASE , max_patches=__SCREAMING_SNAKE_CASE , patch_size=__SCREAMING_SNAKE_CASE ) for image in images ] # create attention mask in numpy a_ : int = [(image.sum(axis=-1 ) != 0).astype(np.floataa ) for image in images] a_ : Dict = BatchFeature( data={'''flattened_patches''': images, '''attention_mask''': attention_masks} , tensor_type=__SCREAMING_SNAKE_CASE ) return encoded_outputs
710
'''simple docstring''' import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import ClassLabel, Features, Image from .base import TaskTemplate @dataclass(frozen=SCREAMING_SNAKE_CASE_ ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = field(default="image-classification" , metadata={"include_in_asdict_even_if_is_default": True} ) snake_case__ = Features({"image": Image()} ) snake_case__ = Features({"labels": ClassLabel} ) snake_case__ = "image" snake_case__ = "labels" def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : List[Any] ) -> Any: if self.label_column not in features: raise ValueError(f'Column {self.label_column} is not present in features.' ) if not isinstance(features[self.label_column] , __SCREAMING_SNAKE_CASE ): raise ValueError(f'Column {self.label_column} is not a ClassLabel.' ) a_ : Optional[int] = copy.deepcopy(self ) a_ : int = self.label_schema.copy() a_ : Tuple = features[self.label_column] a_ : str = label_schema return task_template @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict[str, str]: return { self.image_column: "image", self.label_column: "labels", }
666
0
'''simple docstring''' from __future__ import annotations import math def _UpperCAmelCase ( __A : float , __A : int ): a_ : Any = u for i in range(1 , __A ): a_ : int = temp * (u - i) return temp def _UpperCAmelCase ( ): a_ : Tuple = int(input('''enter the numbers of values: ''' ) ) a_ : list[list[float]] = [] for _ in range(__A ): y.append([] ) for i in range(__A ): for j in range(__A ): y[i].append(__A ) a_ : List[str] = 0 print('''enter the values of parameters in a list: ''' ) a_ : List[Any] = list(map(__A , input().split() ) ) print('''enter the values of corresponding parameters: ''' ) for i in range(__A ): a_ : Optional[Any] = float(input() ) a_ : Union[str, Any] = int(input('''enter the value to interpolate: ''' ) ) a_ : Any = (value - x[0]) / (x[1] - x[0]) # for calculating forward difference table for i in range(1 , __A ): for j in range(n - i ): a_ : Optional[Any] = y[j + 1][i - 1] - y[j][i - 1] a_ : Optional[int] = y[0][0] for i in range(1 , __A ): summ += (ucal(__A , __A ) * y[0][i]) / math.factorial(__A ) print(f'the value at {value} is {summ}' ) if __name__ == "__main__": main()
711
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( __A : tuple[int, int] , __A : int ): a_ , a_ : List[str] = position a_ : Optional[int] = [ (y + 1, x + 2), (y - 1, x + 2), (y + 1, x - 2), (y - 1, x - 2), (y + 2, x + 1), (y + 2, x - 1), (y - 2, x + 1), (y - 2, x - 1), ] a_ : Any = [] for position in positions: a_ , a_ : Dict = position if 0 <= y_test < n and 0 <= x_test < n: permissible_positions.append(__A ) return permissible_positions def _UpperCAmelCase ( __A : list[list[int]] ): return not any(elem == 0 for row in board for elem in row ) def _UpperCAmelCase ( __A : list[list[int]] , __A : tuple[int, int] , __A : int ): if is_complete(__A ): return True for position in get_valid_pos(__A , len(__A ) ): a_ , a_ : Dict = position if board[y][x] == 0: a_ : Optional[Any] = curr + 1 if open_knight_tour_helper(__A , __A , curr + 1 ): return True a_ : Tuple = 0 return False def _UpperCAmelCase ( __A : int ): a_ : List[str] = [[0 for i in range(__A )] for j in range(__A )] for i in range(__A ): for j in range(__A ): a_ : Optional[Any] = 1 if open_knight_tour_helper(__A , (i, j) , 1 ): return board a_ : Union[str, Any] = 0 a_ : Dict = f'Open Kight Tour cannot be performed on a board of size {n}' raise ValueError(__A ) if __name__ == "__main__": import doctest doctest.testmod()
666
0
'''simple docstring''' import re import string import numpy as np import datasets __lowerCAmelCase = '\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n' __lowerCAmelCase = '\nArgs:\n predictions: List of predicted texts.\n references: List of reference texts.\n regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n ignore when calculating the exact matches. Note: these regexes are removed\n from the input data before the changes based on the options below (e.g. ignore_case,\n ignore_punctuation, ignore_numbers) are applied.\n ignore_case: Boolean, defaults to False. If true, turns everything\n to lowercase so that capitalization differences are ignored.\n ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\n ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\nReturns:\n exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 25.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 50.0\n\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 75.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n >>> print(round(results["exact_match"], 1))\n 100.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["The cat sat on the mat.", "Theaters are great.", "It\'s like comparing oranges and apples."]\n >>> preds = ["The cat sat on the mat?", "Theaters are great.", "It\'s like comparing apples and oranges."]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 33.3\n\n' __lowerCAmelCase = '\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class SCREAMING_SNAKE_CASE ( datasets.Metric ): def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> int: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , reference_urls=[] , ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Dict=None , __SCREAMING_SNAKE_CASE : int=False , __SCREAMING_SNAKE_CASE : Optional[int]=False , __SCREAMING_SNAKE_CASE : Dict=False , ) -> str: if regexes_to_ignore is not None: for s in regexes_to_ignore: a_ : Optional[Any] = np.array([re.sub(__SCREAMING_SNAKE_CASE , '''''' , __SCREAMING_SNAKE_CASE ) for x in predictions] ) a_ : int = np.array([re.sub(__SCREAMING_SNAKE_CASE , '''''' , __SCREAMING_SNAKE_CASE ) for x in references] ) else: a_ : List[str] = np.asarray(__SCREAMING_SNAKE_CASE ) a_ : Any = np.asarray(__SCREAMING_SNAKE_CASE ) if ignore_case: a_ : List[str] = np.char.lower(__SCREAMING_SNAKE_CASE ) a_ : List[Any] = np.char.lower(__SCREAMING_SNAKE_CASE ) if ignore_punctuation: a_ : Any = string.punctuation.maketrans('''''' , '''''' , string.punctuation ) a_ : Union[str, Any] = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : int = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) if ignore_numbers: a_ : int = string.digits.maketrans('''''' , '''''' , string.digits ) a_ : Optional[int] = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : Dict = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = predictions == references return {"exact_match": np.mean(__SCREAMING_SNAKE_CASE ) * 100}
712
'''simple docstring''' import warnings warnings.warn( 'memory_utils has been reorganized to utils.memory. Import `find_executable_batchsize` from the main `__init__`: ' '`from accelerate import find_executable_batch_size` to avoid this warning.', FutureWarning, )
666
0
'''simple docstring''' import shutil import tempfile import unittest import numpy as np from transformers.testing_utils import ( is_pt_tf_cross_test, require_tf, require_torch, require_torchvision, require_vision, ) from transformers.utils import is_tf_available, is_torch_available, is_vision_available if is_vision_available(): from PIL import Image from transformers import AutoProcessor, SamImageProcessor, SamProcessor if is_torch_available(): import torch if is_tf_available(): import tensorflow as tf @require_vision @require_torchvision class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : Dict ) -> Tuple: a_ : int = tempfile.mkdtemp() a_ : str = SamImageProcessor() a_ : Optional[int] = SamProcessor(__SCREAMING_SNAKE_CASE ) processor.save_pretrained(self.tmpdirname ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] , **__SCREAMING_SNAKE_CASE : List[str] ) -> Union[str, Any]: return AutoProcessor.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ).image_processor def SCREAMING_SNAKE_CASE ( self : str ) -> Optional[Any]: shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> str: a_ : Optional[Any] = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] a_ : List[str] = [Image.fromarray(np.moveaxis(__SCREAMING_SNAKE_CASE , 0 , -1 ) ) for x in image_inputs] return image_inputs def SCREAMING_SNAKE_CASE ( self : int ) -> str: a_ : List[Any] = SamProcessor(image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) a_ : Optional[int] = self.get_image_processor(do_normalize=__SCREAMING_SNAKE_CASE , padding_value=1.0 ) a_ : Dict = SamProcessor.from_pretrained(self.tmpdirname , do_normalize=__SCREAMING_SNAKE_CASE , padding_value=1.0 ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Union[str, Any]: a_ : List[Any] = self.get_image_processor() a_ : List[Any] = SamProcessor(image_processor=__SCREAMING_SNAKE_CASE ) a_ : Tuple = self.prepare_image_inputs() a_ : int = image_processor(__SCREAMING_SNAKE_CASE , return_tensors='''np''' ) a_ : Union[str, Any] = processor(images=__SCREAMING_SNAKE_CASE , return_tensors='''np''' ) input_feat_extract.pop('''original_sizes''' ) # pop original_sizes as it is popped in the processor input_feat_extract.pop('''reshaped_input_sizes''' ) # pop original_sizes as it is popped in the processor for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) @require_torch def SCREAMING_SNAKE_CASE ( self : Dict ) -> Union[str, Any]: a_ : Tuple = self.get_image_processor() a_ : List[str] = SamProcessor(image_processor=__SCREAMING_SNAKE_CASE ) a_ : Any = [torch.ones((1, 3, 5, 5) )] a_ : Tuple = [[1764, 2646]] a_ : List[str] = [[683, 1024]] a_ : Dict = processor.post_process_masks(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertEqual(masks[0].shape , (1, 3, 1764, 2646) ) a_ : int = processor.post_process_masks( __SCREAMING_SNAKE_CASE , torch.tensor(__SCREAMING_SNAKE_CASE ) , torch.tensor(__SCREAMING_SNAKE_CASE ) ) self.assertEqual(masks[0].shape , (1, 3, 1764, 2646) ) # should also work with np a_ : Dict = [np.ones((1, 3, 5, 5) )] a_ : int = processor.post_process_masks(__SCREAMING_SNAKE_CASE , np.array(__SCREAMING_SNAKE_CASE ) , np.array(__SCREAMING_SNAKE_CASE ) ) self.assertEqual(masks[0].shape , (1, 3, 1764, 2646) ) a_ : Union[str, Any] = [[1, 0], [0, 1]] with self.assertRaises(__SCREAMING_SNAKE_CASE ): a_ : Dict = processor.post_process_masks(__SCREAMING_SNAKE_CASE , np.array(__SCREAMING_SNAKE_CASE ) , np.array(__SCREAMING_SNAKE_CASE ) ) @require_vision @require_tf class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : int ) -> int: a_ : Any = tempfile.mkdtemp() a_ : str = SamImageProcessor() a_ : Optional[Any] = SamProcessor(__SCREAMING_SNAKE_CASE ) processor.save_pretrained(self.tmpdirname ) def SCREAMING_SNAKE_CASE ( self : Dict , **__SCREAMING_SNAKE_CASE : str ) -> Dict: return AutoProcessor.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ).image_processor def SCREAMING_SNAKE_CASE ( self : Tuple ) -> List[str]: shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE ( self : str ) -> Any: a_ : Union[str, Any] = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] a_ : List[str] = [Image.fromarray(np.moveaxis(__SCREAMING_SNAKE_CASE , 0 , -1 ) ) for x in image_inputs] return image_inputs def SCREAMING_SNAKE_CASE ( self : Dict ) -> Any: a_ : Optional[int] = SamProcessor(image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) a_ : Union[str, Any] = self.get_image_processor(do_normalize=__SCREAMING_SNAKE_CASE , padding_value=1.0 ) a_ : Any = SamProcessor.from_pretrained(self.tmpdirname , do_normalize=__SCREAMING_SNAKE_CASE , padding_value=1.0 ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : int ) -> int: a_ : str = self.get_image_processor() a_ : List[str] = SamProcessor(image_processor=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = self.prepare_image_inputs() a_ : Union[str, Any] = image_processor(__SCREAMING_SNAKE_CASE , return_tensors='''np''' ) a_ : str = processor(images=__SCREAMING_SNAKE_CASE , return_tensors='''np''' ) input_feat_extract.pop('''original_sizes''' ) # pop original_sizes as it is popped in the processor input_feat_extract.pop('''reshaped_input_sizes''' ) # pop reshaped_input_sizes as it is popped in the processor for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) @require_tf def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Tuple: a_ : Optional[int] = self.get_image_processor() a_ : str = SamProcessor(image_processor=__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = [tf.ones((1, 3, 5, 5) )] a_ : Optional[Any] = [[1764, 2646]] a_ : Union[str, Any] = [[683, 1024]] a_ : Optional[int] = processor.post_process_masks(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , return_tensors='''tf''' ) self.assertEqual(masks[0].shape , (1, 3, 1764, 2646) ) a_ : List[Any] = processor.post_process_masks( __SCREAMING_SNAKE_CASE , tf.convert_to_tensor(__SCREAMING_SNAKE_CASE ) , tf.convert_to_tensor(__SCREAMING_SNAKE_CASE ) , return_tensors='''tf''' , ) self.assertEqual(masks[0].shape , (1, 3, 1764, 2646) ) # should also work with np a_ : int = [np.ones((1, 3, 5, 5) )] a_ : int = processor.post_process_masks( __SCREAMING_SNAKE_CASE , np.array(__SCREAMING_SNAKE_CASE ) , np.array(__SCREAMING_SNAKE_CASE ) , return_tensors='''tf''' ) self.assertEqual(masks[0].shape , (1, 3, 1764, 2646) ) a_ : Tuple = [[1, 0], [0, 1]] with self.assertRaises(tf.errors.InvalidArgumentError ): a_ : Optional[Any] = processor.post_process_masks( __SCREAMING_SNAKE_CASE , np.array(__SCREAMING_SNAKE_CASE ) , np.array(__SCREAMING_SNAKE_CASE ) , return_tensors='''tf''' ) @require_vision @require_torchvision class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : Dict ) -> int: a_ : List[Any] = tempfile.mkdtemp() a_ : Dict = SamImageProcessor() a_ : List[str] = SamProcessor(__SCREAMING_SNAKE_CASE ) processor.save_pretrained(self.tmpdirname ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , **__SCREAMING_SNAKE_CASE : Tuple ) -> int: return AutoProcessor.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ).image_processor def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Any: shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union[str, Any]: a_ : str = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] a_ : List[str] = [Image.fromarray(np.moveaxis(__SCREAMING_SNAKE_CASE , 0 , -1 ) ) for x in image_inputs] return image_inputs @is_pt_tf_cross_test def SCREAMING_SNAKE_CASE ( self : int ) -> int: a_ : int = self.get_image_processor() a_ : Tuple = SamProcessor(image_processor=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = np.random.randint(0 , 2 , size=(1, 3, 5, 5) ).astype(np.floataa ) a_ : List[str] = [tf.convert_to_tensor(__SCREAMING_SNAKE_CASE )] a_ : Optional[Any] = [torch.tensor(__SCREAMING_SNAKE_CASE )] a_ : int = [[1764, 2646]] a_ : Any = [[683, 1024]] a_ : Dict = processor.post_process_masks( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , return_tensors='''tf''' ) a_ : Tuple = processor.post_process_masks( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , return_tensors='''pt''' ) self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy() ) ) @is_pt_tf_cross_test def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[Any]: a_ : Optional[Any] = self.get_image_processor() a_ : Optional[Any] = SamProcessor(image_processor=__SCREAMING_SNAKE_CASE ) a_ : str = self.prepare_image_inputs() a_ : Dict = image_processor(__SCREAMING_SNAKE_CASE , return_tensors='''pt''' )['''pixel_values'''].numpy() a_ : List[str] = processor(images=__SCREAMING_SNAKE_CASE , return_tensors='''pt''' )['''pixel_values'''].numpy() a_ : List[Any] = image_processor(__SCREAMING_SNAKE_CASE , return_tensors='''tf''' )['''pixel_values'''].numpy() a_ : Any = processor(images=__SCREAMING_SNAKE_CASE , return_tensors='''tf''' )['''pixel_values'''].numpy() self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) )
713
'''simple docstring''' import requests from bsa import BeautifulSoup def _UpperCAmelCase ( __A : str , __A : dict ): a_ : Tuple = BeautifulSoup(requests.get(__A , params=__A ).content , '''html.parser''' ) a_ : List[str] = soup.find('''div''' , attrs={'''class''': '''gs_ri'''} ) a_ : List[str] = div.find('''div''' , attrs={'''class''': '''gs_fl'''} ).find_all('''a''' ) return anchors[2].get_text() if __name__ == "__main__": __lowerCAmelCase = { 'title': ( 'Precisely geometry controlled microsupercapacitors for ultrahigh areal ' 'capacitance, volumetric capacitance, and energy density' ), 'journal': 'Chem. Mater.', 'volume': 30, 'pages': '3979-3990', 'year': 2_018, 'hl': 'en', } print(get_citation('https://scholar.google.com/scholar_lookup', params=params))
666
0
'''simple docstring''' import numpy as np import pandas as pd from sklearn.preprocessing import Normalizer from sklearn.svm import SVR from statsmodels.tsa.statespace.sarimax import SARIMAX def _UpperCAmelCase ( __A : list , __A : list , __A : list , __A : list , __A : list ): a_ : List[Any] = np.array([[1, item, train_mtch[i]] for i, item in enumerate(__A )] ) a_ : int = np.array(__A ) a_ : str = np.dot(np.dot(np.linalg.inv(np.dot(x.transpose() , __A ) ) , x.transpose() ) , __A ) return abs(beta[0] + test_dt[0] * beta[1] + test_mtch[0] + beta[2] ) def _UpperCAmelCase ( __A : list , __A : list , __A : list ): a_ : str = (1, 2, 1) a_ : int = (1, 1, 0, 7) a_ : Tuple = SARIMAX( __A , exog=__A , order=__A , seasonal_order=__A ) a_ : List[Any] = model.fit(disp=__A , maxiter=6_00 , method='''nm''' ) a_ : Union[str, Any] = model_fit.predict(1 , len(__A ) , exog=[test_match] ) return result[0] def _UpperCAmelCase ( __A : list , __A : list , __A : list ): a_ : str = SVR(kernel='''rbf''' , C=1 , gamma=0.1 , epsilon=0.1 ) regressor.fit(__A , __A ) a_ : Union[str, Any] = regressor.predict(__A ) return y_pred[0] def _UpperCAmelCase ( __A : list ): train_user.sort() a_ : Optional[Any] = np.percentile(__A , 25 ) a_ : Optional[Any] = np.percentile(__A , 75 ) a_ : int = qa - qa a_ : Optional[int] = qa - (iqr * 0.1) return low_lim def _UpperCAmelCase ( __A : list , __A : float ): a_ : Optional[int] = 0 a_ : List[str] = 0 for i in list_vote: if i > actual_result: a_ : Optional[int] = not_safe + 1 else: if abs(abs(__A ) - abs(__A ) ) <= 0.1: safe += 1 else: not_safe += 1 return safe > not_safe if __name__ == "__main__": # data_input_df = pd.read_csv("ex_data.csv", header=None) __lowerCAmelCase = [[18_231, 0.0, 1], [22_621, 1.0, 2], [15_675, 0.0, 3], [23_583, 1.0, 4]] __lowerCAmelCase = pd.DataFrame( data_input, columns=['total_user', 'total_even', 'days'] ) __lowerCAmelCase = Normalizer().fit_transform(data_input_df.values) # split data __lowerCAmelCase = normalize_df[:, 2].tolist() __lowerCAmelCase = normalize_df[:, 0].tolist() __lowerCAmelCase = normalize_df[:, 1].tolist() # for svr (input variable = total date and total match) __lowerCAmelCase = normalize_df[:, [1, 2]].tolist() __lowerCAmelCase = x[: len(x) - 1] __lowerCAmelCase = x[len(x) - 1 :] # for linear regression & sarimax __lowerCAmelCase = total_date[: len(total_date) - 1] __lowerCAmelCase = total_user[: len(total_user) - 1] __lowerCAmelCase = total_match[: len(total_match) - 1] __lowerCAmelCase = total_date[len(total_date) - 1 :] __lowerCAmelCase = total_user[len(total_user) - 1 :] __lowerCAmelCase = total_match[len(total_match) - 1 :] # voting system with forecasting __lowerCAmelCase = [ linear_regression_prediction( trn_date, trn_user, trn_match, tst_date, tst_match ), sarimax_predictor(trn_user, trn_match, tst_match), support_vector_regressor(x_train, x_test, trn_user), ] # check the safety of today's data __lowerCAmelCase = '' if data_safety_checker(res_vote, tst_user) else 'not ' print('Today\'s data is {not_str}safe.')
714
'''simple docstring''' import argparse from torch import nn # transformers_old should correspond to branch `save_old_prophetnet_model_structure` here # original prophetnet_checkpoints are saved under `patrickvonplaten/..._old` respectively from transformers_old.modeling_prophetnet import ( ProphetNetForConditionalGeneration as ProphetNetForConditionalGenerationOld, ) from transformers_old.modeling_xlm_prophetnet import ( XLMProphetNetForConditionalGeneration as XLMProphetNetForConditionalGenerationOld, ) from transformers import ProphetNetForConditionalGeneration, XLMProphetNetForConditionalGeneration, logging __lowerCAmelCase = logging.get_logger(__name__) logging.set_verbosity_info() def _UpperCAmelCase ( __A : str , __A : str ): if "xprophetnet" in prophetnet_checkpoint_path: a_ : Tuple = XLMProphetNetForConditionalGenerationOld.from_pretrained(__A ) a_ , a_ : Optional[Any] = XLMProphetNetForConditionalGeneration.from_pretrained( __A , output_loading_info=__A ) else: a_ : List[Any] = ProphetNetForConditionalGenerationOld.from_pretrained(__A ) a_ , a_ : Any = ProphetNetForConditionalGeneration.from_pretrained( __A , output_loading_info=__A ) a_ : str = ['''key_proj''', '''value_proj''', '''query_proj'''] a_ : Tuple = { '''self_attn''': '''ngram_self_attn''', '''cross_attn''': '''encoder_attn''', '''cross_attn_layer_norm''': '''encoder_attn_layer_norm''', '''feed_forward_layer_norm''': '''final_layer_norm''', '''feed_forward''': '''''', '''intermediate''': '''fc1''', '''output''': '''fc2''', '''key_proj''': '''k_proj''', '''query_proj''': '''q_proj''', '''value_proj''': '''v_proj''', '''word_embeddings''': '''embed_tokens''', '''embeddings_layer_norm''': '''emb_layer_norm''', '''relative_pos_embeddings''': '''relative_linear''', '''ngram_embeddings''': '''ngram_input_embed''', '''position_embeddings''': '''embed_positions''', } for key in loading_info["missing_keys"]: a_ : List[str] = key.split('''.''' ) if attributes[0] == "lm_head": a_ : List[str] = prophet a_ : Dict = prophet_old else: a_ : str = prophet.prophetnet a_ : int = prophet_old.model a_ : str = False for attribute in attributes: if attribute in mapping: a_ : Dict = mapping[attribute] if not hasattr(__A , __A ) and len(__A ) > 0: a_ : List[str] = attribute elif hasattr(__A , __A ): a_ : Union[str, Any] = attribute if attribute == "weight": assert old_model.weight.shape == model.weight.shape, "Shapes have to match!" a_ : Tuple = old_model.weight logger.info(f'{attribute} is initialized.' ) a_ : Union[str, Any] = True break elif attribute == "bias": assert old_model.bias.shape == model.bias.shape, "Shapes have to match!" a_ : Union[str, Any] = old_model.bias logger.info(f'{attribute} is initialized' ) a_ : Dict = True break elif attribute in special_keys and hasattr(__A , '''in_proj_weight''' ): a_ : Tuple = old_model.in_proj_weight.shape[0] // 3 a_ : Any = getattr(__A , __A ) param.weight.shape == old_model.in_proj_weight[:embed_dim, :].shape, "Shapes have to match" param.bias.shape == old_model.in_proj_bias[:embed_dim].shape, "Shapes have to match" if attribute == "query_proj": a_ : Union[str, Any] = nn.Parameter(old_model.in_proj_weight[:embed_dim, :] ) a_ : Optional[Any] = nn.Parameter(old_model.in_proj_bias[:embed_dim] ) elif attribute == "key_proj": a_ : List[Any] = nn.Parameter(old_model.in_proj_weight[embed_dim : 2 * embed_dim, :] ) a_ : Optional[int] = nn.Parameter(old_model.in_proj_bias[embed_dim : 2 * embed_dim] ) elif attribute == "value_proj": a_ : Tuple = nn.Parameter(old_model.in_proj_weight[2 * embed_dim :, :] ) a_ : Any = nn.Parameter(old_model.in_proj_bias[2 * embed_dim :] ) a_ : Dict = True break elif attribute == "position_embeddings": assert ( model.position_embeddings.weight.shape[-1] == old_model.embed_positions.weight.shape[-1] ), "Hidden size has to match" assert model.position_embeddings.weight.shape[0] == 5_12, "We want 512 position_embeddings." a_ : Union[str, Any] = nn.Parameter(old_model.embed_positions.weight[:5_12, :] ) a_ : Optional[Any] = True break if attribute.isdigit(): a_ : Union[str, Any] = model[int(__A )] a_ : str = old_model[int(__A )] else: a_ : Tuple = getattr(__A , __A ) if old_attribute == "": a_ : List[str] = old_model else: if not hasattr(__A , __A ): raise ValueError(f'{old_model} does not have {old_attribute}' ) a_ : Optional[Any] = getattr(__A , __A ) if not is_key_init: raise ValueError(f'{key} was not correctly initialized!' ) print(f'Saving model to {pytorch_dump_folder_path}' ) prophet.save_pretrained(__A ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '--prophetnet_checkpoint_path', default=None, type=str, required=True, help='Path the official PyTorch dump.' ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) __lowerCAmelCase = parser.parse_args() convert_prophetnet_checkpoint_to_pytorch(args.prophetnet_checkpoint_path, args.pytorch_dump_folder_path)
666
0
import json from typing import List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_bart import BartTokenizer __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'} # See all BART models at https://huggingface.co/models?filter=bart __lowerCAmelCase = { 'vocab_file': { 'facebook/bart-base': 'https://huggingface.co/facebook/bart-base/resolve/main/vocab.json', 'facebook/bart-large': 'https://huggingface.co/facebook/bart-large/resolve/main/vocab.json', 'facebook/bart-large-mnli': 'https://huggingface.co/facebook/bart-large-mnli/resolve/main/vocab.json', 'facebook/bart-large-cnn': 'https://huggingface.co/facebook/bart-large-cnn/resolve/main/vocab.json', 'facebook/bart-large-xsum': 'https://huggingface.co/facebook/bart-large-xsum/resolve/main/vocab.json', 'yjernite/bart_eli5': 'https://huggingface.co/yjernite/bart_eli5/resolve/main/vocab.json', }, 'merges_file': { 'facebook/bart-base': 'https://huggingface.co/facebook/bart-base/resolve/main/merges.txt', 'facebook/bart-large': 'https://huggingface.co/facebook/bart-large/resolve/main/merges.txt', 'facebook/bart-large-mnli': 'https://huggingface.co/facebook/bart-large-mnli/resolve/main/merges.txt', 'facebook/bart-large-cnn': 'https://huggingface.co/facebook/bart-large-cnn/resolve/main/merges.txt', 'facebook/bart-large-xsum': 'https://huggingface.co/facebook/bart-large-xsum/resolve/main/merges.txt', 'yjernite/bart_eli5': 'https://huggingface.co/yjernite/bart_eli5/resolve/main/merges.txt', }, 'tokenizer_file': { 'facebook/bart-base': 'https://huggingface.co/facebook/bart-base/resolve/main/tokenizer.json', 'facebook/bart-large': 'https://huggingface.co/facebook/bart-large/resolve/main/tokenizer.json', 'facebook/bart-large-mnli': 'https://huggingface.co/facebook/bart-large-mnli/resolve/main/tokenizer.json', 'facebook/bart-large-cnn': 'https://huggingface.co/facebook/bart-large-cnn/resolve/main/tokenizer.json', 'facebook/bart-large-xsum': 'https://huggingface.co/facebook/bart-large-xsum/resolve/main/tokenizer.json', 'yjernite/bart_eli5': 'https://huggingface.co/yjernite/bart_eli5/resolve/main/tokenizer.json', }, } __lowerCAmelCase = { 'facebook/bart-base': 1_024, 'facebook/bart-large': 1_024, 'facebook/bart-large-mnli': 1_024, 'facebook/bart-large-cnn': 1_024, 'facebook/bart-large-xsum': 1_024, 'yjernite/bart_eli5': 1_024, } class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = VOCAB_FILES_NAMES snake_case__ = PRETRAINED_VOCAB_FILES_MAP snake_case__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case__ = ["input_ids", "attention_mask"] snake_case__ = BartTokenizer def __init__( self : Dict , __SCREAMING_SNAKE_CASE : Optional[Any]=None , __SCREAMING_SNAKE_CASE : List[Any]=None , __SCREAMING_SNAKE_CASE : Any=None , __SCREAMING_SNAKE_CASE : Tuple="replace" , __SCREAMING_SNAKE_CASE : Dict="<s>" , __SCREAMING_SNAKE_CASE : int="</s>" , __SCREAMING_SNAKE_CASE : Tuple="</s>" , __SCREAMING_SNAKE_CASE : Optional[Any]="<s>" , __SCREAMING_SNAKE_CASE : Any="<unk>" , __SCREAMING_SNAKE_CASE : Optional[Any]="<pad>" , __SCREAMING_SNAKE_CASE : Optional[int]="<mask>" , __SCREAMING_SNAKE_CASE : Tuple=False , __SCREAMING_SNAKE_CASE : int=True , **__SCREAMING_SNAKE_CASE : Tuple , ) -> Tuple: super().__init__( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , errors=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) a_ : List[str] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , __SCREAMING_SNAKE_CASE ) != add_prefix_space: a_ : Any = getattr(__SCREAMING_SNAKE_CASE , pre_tok_state.pop('''type''' ) ) a_ : int = add_prefix_space a_ : Union[str, Any] = pre_tok_class(**__SCREAMING_SNAKE_CASE ) a_ : List[str] = add_prefix_space # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__` a_ : int = '''post_processor''' a_ : Optional[int] = getattr(self.backend_tokenizer , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if tokenizer_component_instance: a_ : Optional[int] = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: a_ : Optional[int] = tuple(state['''sep'''] ) if "cls" in state: a_ : List[str] = tuple(state['''cls'''] ) a_ : List[str] = False if state.get('''add_prefix_space''' , __SCREAMING_SNAKE_CASE ) != add_prefix_space: a_ : Optional[Any] = add_prefix_space a_ : str = True if state.get('''trim_offsets''' , __SCREAMING_SNAKE_CASE ) != trim_offsets: a_ : Dict = trim_offsets a_ : Any = True if changes_to_apply: a_ : str = getattr(__SCREAMING_SNAKE_CASE , state.pop('''type''' ) ) a_ : List[Any] = component_class(**__SCREAMING_SNAKE_CASE ) setattr(self.backend_tokenizer , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> str: if self._mask_token is None: if self.verbose: logger.error('''Using mask_token, but it is not set yet.''' ) return None return str(self._mask_token ) @mask_token.setter def SCREAMING_SNAKE_CASE ( self : Any , __SCREAMING_SNAKE_CASE : Any ) -> Dict: a_ : int = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else value a_ : Any = value def SCREAMING_SNAKE_CASE ( self : Any , *__SCREAMING_SNAKE_CASE : Tuple , **__SCREAMING_SNAKE_CASE : Optional[Any] ) -> BatchEncoding: a_ : Tuple = kwargs.get('''is_split_into_words''' , __SCREAMING_SNAKE_CASE ) if is_split_into_words and not self.add_prefix_space: raise ValueError( f'You need to instantiate {self.__class__.__name__} with add_prefix_space=True ' '''to use it with pretokenized inputs.''' ) return super()._batch_encode_plus(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[str] , *__SCREAMING_SNAKE_CASE : List[str] , **__SCREAMING_SNAKE_CASE : Dict ) -> BatchEncoding: a_ : int = kwargs.get('''is_split_into_words''' , __SCREAMING_SNAKE_CASE ) if is_split_into_words and not self.add_prefix_space: raise ValueError( f'You need to instantiate {self.__class__.__name__} with add_prefix_space=True ' '''to use it with pretokenized inputs.''' ) return super()._encode_plus(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Optional[str] = None ) -> Tuple[str]: a_ : List[Any] = self._tokenizer.model.save(__SCREAMING_SNAKE_CASE , name=__SCREAMING_SNAKE_CASE ) return tuple(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Optional[Any]=None ) -> Dict: a_ : int = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None ) -> List[int]: a_ : Optional[Any] = [self.sep_token_id] a_ : str = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0]
715
'''simple docstring''' import re import string import numpy as np import datasets __lowerCAmelCase = '\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n' __lowerCAmelCase = '\nArgs:\n predictions: List of predicted texts.\n references: List of reference texts.\n regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n ignore when calculating the exact matches. Note: these regexes are removed\n from the input data before the changes based on the options below (e.g. ignore_case,\n ignore_punctuation, ignore_numbers) are applied.\n ignore_case: Boolean, defaults to False. If true, turns everything\n to lowercase so that capitalization differences are ignored.\n ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\n ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\nReturns:\n exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 25.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 50.0\n\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 75.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n >>> print(round(results["exact_match"], 1))\n 100.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["The cat sat on the mat.", "Theaters are great.", "It\'s like comparing oranges and apples."]\n >>> preds = ["The cat sat on the mat?", "Theaters are great.", "It\'s like comparing apples and oranges."]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 33.3\n\n' __lowerCAmelCase = '\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class SCREAMING_SNAKE_CASE ( datasets.Metric ): def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> int: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , reference_urls=[] , ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Dict=None , __SCREAMING_SNAKE_CASE : int=False , __SCREAMING_SNAKE_CASE : Optional[int]=False , __SCREAMING_SNAKE_CASE : Dict=False , ) -> str: if regexes_to_ignore is not None: for s in regexes_to_ignore: a_ : Optional[Any] = np.array([re.sub(__SCREAMING_SNAKE_CASE , '''''' , __SCREAMING_SNAKE_CASE ) for x in predictions] ) a_ : int = np.array([re.sub(__SCREAMING_SNAKE_CASE , '''''' , __SCREAMING_SNAKE_CASE ) for x in references] ) else: a_ : List[str] = np.asarray(__SCREAMING_SNAKE_CASE ) a_ : Any = np.asarray(__SCREAMING_SNAKE_CASE ) if ignore_case: a_ : List[str] = np.char.lower(__SCREAMING_SNAKE_CASE ) a_ : List[Any] = np.char.lower(__SCREAMING_SNAKE_CASE ) if ignore_punctuation: a_ : Any = string.punctuation.maketrans('''''' , '''''' , string.punctuation ) a_ : Union[str, Any] = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : int = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) if ignore_numbers: a_ : int = string.digits.maketrans('''''' , '''''' , string.digits ) a_ : Optional[int] = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : Dict = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = predictions == references return {"exact_match": np.mean(__SCREAMING_SNAKE_CASE ) * 100}
666
0
'''simple docstring''' import argparse import numpy as np import torch from transformers import SpeechTaHifiGan, SpeechTaHifiGanConfig, logging logging.set_verbosity_info() __lowerCAmelCase = logging.get_logger('transformers.models.speecht5') def _UpperCAmelCase ( __A : Dict , __A : List[Any] , __A : List[Any] ): hf_model.apply_weight_norm() a_ : List[Any] = checkpoint['''input_conv.weight_g'''] a_ : Optional[Any] = checkpoint['''input_conv.weight_v'''] a_ : Dict = checkpoint['''input_conv.bias'''] for i in range(len(config.upsample_rates ) ): a_ : Optional[int] = checkpoint[f'upsamples.{i}.1.weight_g'] a_ : int = checkpoint[f'upsamples.{i}.1.weight_v'] a_ : Union[str, Any] = checkpoint[f'upsamples.{i}.1.bias'] for i in range(len(config.upsample_rates ) * len(config.resblock_kernel_sizes ) ): for j in range(len(config.resblock_dilation_sizes ) ): a_ : Union[str, Any] = checkpoint[f'blocks.{i}.convs1.{j}.1.weight_g'] a_ : int = checkpoint[f'blocks.{i}.convs1.{j}.1.weight_v'] a_ : int = checkpoint[f'blocks.{i}.convs1.{j}.1.bias'] a_ : Any = checkpoint[f'blocks.{i}.convs2.{j}.1.weight_g'] a_ : Optional[Any] = checkpoint[f'blocks.{i}.convs2.{j}.1.weight_v'] a_ : Any = checkpoint[f'blocks.{i}.convs2.{j}.1.bias'] a_ : Union[str, Any] = checkpoint['''output_conv.1.weight_g'''] a_ : Optional[int] = checkpoint['''output_conv.1.weight_v'''] a_ : str = checkpoint['''output_conv.1.bias'''] hf_model.remove_weight_norm() @torch.no_grad() def _UpperCAmelCase ( __A : Any , __A : List[Any] , __A : Tuple , __A : Optional[int]=None , __A : Union[str, Any]=None , ): if config_path is not None: a_ : Dict = SpeechTaHifiGanConfig.from_pretrained(__A ) else: a_ : int = SpeechTaHifiGanConfig() a_ : Any = SpeechTaHifiGan(__A ) a_ : Optional[Any] = torch.load(__A ) load_weights(orig_checkpoint['''model''']['''generator'''] , __A , __A ) a_ : Union[str, Any] = np.load(__A ) a_ : str = stats[0].reshape(-1 ) a_ : Optional[Any] = stats[1].reshape(-1 ) a_ : Any = torch.from_numpy(__A ).float() a_ : str = torch.from_numpy(__A ).float() model.save_pretrained(__A ) if repo_id: print('''Pushing to the hub...''' ) model.push_to_hub(__A ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() parser.add_argument('--checkpoint_path', required=True, default=None, type=str, help='Path to original checkpoint') parser.add_argument('--stats_path', required=True, default=None, type=str, help='Path to stats.npy file') parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert') parser.add_argument( '--pytorch_dump_folder_path', required=True, default=None, type=str, help='Path to the output PyTorch model.' ) parser.add_argument( '--push_to_hub', default=None, type=str, help='Where to upload the converted model on the 🤗 hub.' ) __lowerCAmelCase = parser.parse_args() convert_hifigan_checkpoint( args.checkpoint_path, args.stats_path, args.pytorch_dump_folder_path, args.config_path, args.push_to_hub, )
716
'''simple docstring''' import gc import unittest import numpy as np import torch from diffusers import ( AudioDiffusionPipeline, AutoencoderKL, DDIMScheduler, DDPMScheduler, DiffusionPipeline, Mel, UNetaDConditionModel, UNetaDModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : Dict ) -> List[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> List[Any]: torch.manual_seed(0 ) a_ : Tuple = UNetaDModel( sample_size=(32, 64) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''') , ) return model @property def SCREAMING_SNAKE_CASE ( self : int ) -> List[Any]: torch.manual_seed(0 ) a_ : Any = UNetaDConditionModel( sample_size=(64, 32) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''CrossAttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''CrossAttnUpBlock2D''') , cross_attention_dim=10 , ) return model @property def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: torch.manual_seed(0 ) a_ : List[Any] = AutoencoderKL( sample_size=(128, 64) , in_channels=1 , out_channels=1 , latent_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''DownEncoderBlock2D''', '''DownEncoderBlock2D''') , up_block_types=('''UpDecoderBlock2D''', '''UpDecoderBlock2D''') , ) a_ : List[Any] = UNetaDModel( sample_size=(64, 32) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''') , ) return vqvae, unet @slow def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union[str, Any]: a_ : Dict = '''cpu''' # ensure determinism for the device-dependent torch.Generator a_ : Union[str, Any] = Mel( x_res=self.dummy_unet.config.sample_size[1] , y_res=self.dummy_unet.config.sample_size[0] , ) a_ : Any = DDPMScheduler() a_ : str = AudioDiffusionPipeline(vqvae=__SCREAMING_SNAKE_CASE , unet=self.dummy_unet , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : List[Any] = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : List[str] = pipe(generator=__SCREAMING_SNAKE_CASE , steps=4 ) a_ : List[Any] = output.audios[0] a_ : Dict = output.images[0] a_ : Dict = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : Optional[Any] = pipe(generator=__SCREAMING_SNAKE_CASE , steps=4 , return_dict=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output[0][0] assert audio.shape == (1, (self.dummy_unet.config.sample_size[1] - 1) * mel.hop_length) assert ( image.height == self.dummy_unet.config.sample_size[0] and image.width == self.dummy_unet.config.sample_size[1] ) a_ : Dict = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : str = np.frombuffer(image_from_tuple.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([69, 255, 255, 255, 0, 0, 77, 181, 12, 127] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() == 0 a_ : str = Mel( x_res=self.dummy_vqvae_and_unet[0].config.sample_size[1] , y_res=self.dummy_vqvae_and_unet[0].config.sample_size[0] , ) a_ : int = DDIMScheduler() a_ : Dict = self.dummy_vqvae_and_unet a_ : List[str] = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0] , unet=dummy_vqvae_and_unet[1] , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : Any = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) np.random.seed(0 ) a_ : List[str] = np.random.uniform(-1 , 1 , ((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,) ) a_ : int = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : int = pipe(raw_audio=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , start_step=5 , steps=10 ) a_ : List[str] = output.images[0] assert ( image.height == self.dummy_vqvae_and_unet[0].config.sample_size[0] and image.width == self.dummy_vqvae_and_unet[0].config.sample_size[1] ) a_ : Optional[Any] = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([120, 117, 110, 109, 138, 167, 138, 148, 132, 121] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 a_ : List[str] = self.dummy_unet_condition a_ : Dict = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0] , unet=__SCREAMING_SNAKE_CASE , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : int = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) np.random.seed(0 ) a_ : Any = torch.rand((1, 1, 10) ) a_ : Tuple = pipe(generator=__SCREAMING_SNAKE_CASE , encoding=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output.images[0] a_ : Dict = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([107, 103, 120, 127, 142, 122, 113, 122, 97, 111] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 @slow @require_torch_gpu class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Any: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Any: a_ : Any = torch_device a_ : Optional[int] = DiffusionPipeline.from_pretrained('''teticio/audio-diffusion-ddim-256''' ) a_ : Dict = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : str = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : List[str] = pipe(generator=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output.audios[0] a_ : Tuple = output.images[0] assert audio.shape == (1, (pipe.unet.config.sample_size[1] - 1) * pipe.mel.hop_length) assert image.height == pipe.unet.config.sample_size[0] and image.width == pipe.unet.config.sample_size[1] a_ : str = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : Tuple = np.array([151, 167, 154, 144, 122, 134, 121, 105, 70, 26] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0
666
0
from ...configuration_utils import PretrainedConfig from ...utils import logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = { 'facebook/nllb-moe-54B': 'https://huggingface.co/facebook/nllb-moe-54b/resolve/main/config.json', } class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "nllb-moe" snake_case__ = ["past_key_values"] snake_case__ = {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"} def __init__( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : int=12_8112 , __SCREAMING_SNAKE_CASE : List[str]=1024 , __SCREAMING_SNAKE_CASE : Any=12 , __SCREAMING_SNAKE_CASE : Optional[Any]=4096 , __SCREAMING_SNAKE_CASE : List[Any]=16 , __SCREAMING_SNAKE_CASE : Optional[int]=12 , __SCREAMING_SNAKE_CASE : Dict=4096 , __SCREAMING_SNAKE_CASE : Dict=16 , __SCREAMING_SNAKE_CASE : Dict=0.05 , __SCREAMING_SNAKE_CASE : int=0.05 , __SCREAMING_SNAKE_CASE : Dict=True , __SCREAMING_SNAKE_CASE : Optional[int]=True , __SCREAMING_SNAKE_CASE : Optional[int]="relu" , __SCREAMING_SNAKE_CASE : int=1024 , __SCREAMING_SNAKE_CASE : Union[str, Any]=0.1 , __SCREAMING_SNAKE_CASE : str=0.1 , __SCREAMING_SNAKE_CASE : Optional[int]=0.0 , __SCREAMING_SNAKE_CASE : Dict=0.02 , __SCREAMING_SNAKE_CASE : int=2 , __SCREAMING_SNAKE_CASE : Any=True , __SCREAMING_SNAKE_CASE : List[Any]=False , __SCREAMING_SNAKE_CASE : Optional[int]="float32" , __SCREAMING_SNAKE_CASE : Dict=False , __SCREAMING_SNAKE_CASE : Union[str, Any]=128 , __SCREAMING_SNAKE_CASE : str=64 , __SCREAMING_SNAKE_CASE : Tuple=4 , __SCREAMING_SNAKE_CASE : str=4 , __SCREAMING_SNAKE_CASE : List[str]=0.001 , __SCREAMING_SNAKE_CASE : str=0.001 , __SCREAMING_SNAKE_CASE : str="all" , __SCREAMING_SNAKE_CASE : List[Any]=False , __SCREAMING_SNAKE_CASE : List[Any]=False , __SCREAMING_SNAKE_CASE : Optional[int]=1.0 , __SCREAMING_SNAKE_CASE : List[Any]=0.2 , __SCREAMING_SNAKE_CASE : str=1 , __SCREAMING_SNAKE_CASE : Any=0 , __SCREAMING_SNAKE_CASE : int=2 , __SCREAMING_SNAKE_CASE : Tuple=False , **__SCREAMING_SNAKE_CASE : Any , ) -> int: a_ : List[Any] = vocab_size a_ : List[str] = max_position_embeddings a_ : Any = d_model a_ : Optional[Any] = encoder_ffn_dim a_ : int = encoder_layers a_ : Optional[Any] = encoder_attention_heads a_ : int = decoder_ffn_dim a_ : Optional[int] = decoder_layers a_ : Optional[Any] = decoder_attention_heads a_ : Optional[Any] = dropout a_ : List[Any] = attention_dropout a_ : int = activation_dropout a_ : str = activation_function a_ : List[Any] = init_std a_ : Optional[int] = encoder_layerdrop a_ : Optional[int] = decoder_layerdrop a_ : Any = use_cache a_ : Any = encoder_layers a_ : int = scale_embedding # scale factor will be sqrt(d_model) if True a_ : Optional[Any] = router_z_loss_coef a_ : Tuple = router_aux_loss_coef a_ : List[str] = decoder_sparse_step a_ : List[Any] = encoder_sparse_step a_ : str = num_experts a_ : Union[str, Any] = expert_capacity a_ : Optional[int] = router_bias if router_dtype not in ["float32", "float16", "bfloat16"]: raise ValueError(f'`router_dtype` must be one of \'float32\', \'float16\' or \'bfloat16\', got {router_dtype}' ) a_ : List[Any] = router_dtype a_ : int = router_ignore_padding_tokens a_ : str = batch_prioritized_routing a_ : int = second_expert_policy a_ : List[Any] = normalize_router_prob_before_dropping a_ : List[str] = moe_eval_capacity_token_fraction a_ : Any = moe_token_dropout a_ : List[Any] = output_router_logits super().__init__( pad_token_id=__SCREAMING_SNAKE_CASE , bos_token_id=__SCREAMING_SNAKE_CASE , eos_token_id=__SCREAMING_SNAKE_CASE , is_encoder_decoder=__SCREAMING_SNAKE_CASE , decoder_start_token_id=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , )
717
'''simple docstring''' import argparse import json from pathlib import Path import requests import torch from huggingface_hub import cached_download, hf_hub_download, hf_hub_url from PIL import Image from transformers import DetaConfig, DetaForObjectDetection, DetaImageProcessor, SwinConfig from transformers.utils import logging logging.set_verbosity_info() __lowerCAmelCase = logging.get_logger(__name__) def _UpperCAmelCase ( __A : Union[str, Any] ): a_ : Tuple = SwinConfig( embed_dim=1_92 , depths=(2, 2, 18, 2) , num_heads=(6, 12, 24, 48) , window_size=12 , out_features=['''stage2''', '''stage3''', '''stage4'''] , ) a_ : List[Any] = DetaConfig( backbone_config=__A , num_queries=9_00 , encoder_ffn_dim=20_48 , decoder_ffn_dim=20_48 , num_feature_levels=5 , assign_first_stage=__A , with_box_refine=__A , two_stage=__A , ) # set labels a_ : Optional[Any] = '''huggingface/label-files''' if "o365" in model_name: a_ : Optional[Any] = 3_66 a_ : Tuple = '''object365-id2label.json''' else: a_ : Any = 91 a_ : Union[str, Any] = '''coco-detection-id2label.json''' a_ : Tuple = num_labels a_ : str = json.load(open(cached_download(hf_hub_url(__A , __A , repo_type='''dataset''' ) ) , '''r''' ) ) a_ : Optional[int] = {int(__A ): v for k, v in idalabel.items()} a_ : int = idalabel a_ : Dict = {v: k for k, v in idalabel.items()} return config def _UpperCAmelCase ( __A : List[str] ): a_ : Tuple = [] # stem # fmt: off rename_keys.append(('''backbone.0.body.patch_embed.proj.weight''', '''model.backbone.model.embeddings.patch_embeddings.projection.weight''') ) rename_keys.append(('''backbone.0.body.patch_embed.proj.bias''', '''model.backbone.model.embeddings.patch_embeddings.projection.bias''') ) rename_keys.append(('''backbone.0.body.patch_embed.norm.weight''', '''model.backbone.model.embeddings.norm.weight''') ) rename_keys.append(('''backbone.0.body.patch_embed.norm.bias''', '''model.backbone.model.embeddings.norm.bias''') ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm1.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm1.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_bias_table', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_index', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.proj.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.proj.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm2.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm2.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.bias') ) if i < 3: rename_keys.append((f'backbone.0.body.layers.{i}.downsample.reduction.weight', f'model.backbone.model.encoder.layers.{i}.downsample.reduction.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.downsample.norm.weight', f'model.backbone.model.encoder.layers.{i}.downsample.norm.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.downsample.norm.bias', f'model.backbone.model.encoder.layers.{i}.downsample.norm.bias') ) rename_keys.append(('''backbone.0.body.norm1.weight''', '''model.backbone.model.hidden_states_norms.stage2.weight''') ) rename_keys.append(('''backbone.0.body.norm1.bias''', '''model.backbone.model.hidden_states_norms.stage2.bias''') ) rename_keys.append(('''backbone.0.body.norm2.weight''', '''model.backbone.model.hidden_states_norms.stage3.weight''') ) rename_keys.append(('''backbone.0.body.norm2.bias''', '''model.backbone.model.hidden_states_norms.stage3.bias''') ) rename_keys.append(('''backbone.0.body.norm3.weight''', '''model.backbone.model.hidden_states_norms.stage4.weight''') ) rename_keys.append(('''backbone.0.body.norm3.bias''', '''model.backbone.model.hidden_states_norms.stage4.bias''') ) # transformer encoder for i in range(config.encoder_layers ): rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.sampling_offsets.weight', f'model.encoder.layers.{i}.self_attn.sampling_offsets.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.sampling_offsets.bias', f'model.encoder.layers.{i}.self_attn.sampling_offsets.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.attention_weights.weight', f'model.encoder.layers.{i}.self_attn.attention_weights.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.attention_weights.bias', f'model.encoder.layers.{i}.self_attn.attention_weights.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.value_proj.weight', f'model.encoder.layers.{i}.self_attn.value_proj.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.value_proj.bias', f'model.encoder.layers.{i}.self_attn.value_proj.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.output_proj.weight', f'model.encoder.layers.{i}.self_attn.output_proj.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.output_proj.bias', f'model.encoder.layers.{i}.self_attn.output_proj.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm1.weight', f'model.encoder.layers.{i}.self_attn_layer_norm.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm1.bias', f'model.encoder.layers.{i}.self_attn_layer_norm.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear1.weight', f'model.encoder.layers.{i}.fc1.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear1.bias', f'model.encoder.layers.{i}.fc1.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear2.weight', f'model.encoder.layers.{i}.fc2.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear2.bias', f'model.encoder.layers.{i}.fc2.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm2.weight', f'model.encoder.layers.{i}.final_layer_norm.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm2.bias', f'model.encoder.layers.{i}.final_layer_norm.bias') ) # transformer decoder for i in range(config.decoder_layers ): rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.sampling_offsets.weight', f'model.decoder.layers.{i}.encoder_attn.sampling_offsets.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.sampling_offsets.bias', f'model.decoder.layers.{i}.encoder_attn.sampling_offsets.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.attention_weights.weight', f'model.decoder.layers.{i}.encoder_attn.attention_weights.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.attention_weights.bias', f'model.decoder.layers.{i}.encoder_attn.attention_weights.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.value_proj.weight', f'model.decoder.layers.{i}.encoder_attn.value_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.value_proj.bias', f'model.decoder.layers.{i}.encoder_attn.value_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.output_proj.weight', f'model.decoder.layers.{i}.encoder_attn.output_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.output_proj.bias', f'model.decoder.layers.{i}.encoder_attn.output_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm1.weight', f'model.decoder.layers.{i}.encoder_attn_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm1.bias', f'model.decoder.layers.{i}.encoder_attn_layer_norm.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.self_attn.out_proj.weight', f'model.decoder.layers.{i}.self_attn.out_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.self_attn.out_proj.bias', f'model.decoder.layers.{i}.self_attn.out_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm2.weight', f'model.decoder.layers.{i}.self_attn_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm2.bias', f'model.decoder.layers.{i}.self_attn_layer_norm.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear1.weight', f'model.decoder.layers.{i}.fc1.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear1.bias', f'model.decoder.layers.{i}.fc1.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear2.weight', f'model.decoder.layers.{i}.fc2.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear2.bias', f'model.decoder.layers.{i}.fc2.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm3.weight', f'model.decoder.layers.{i}.final_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm3.bias', f'model.decoder.layers.{i}.final_layer_norm.bias') ) # fmt: on return rename_keys def _UpperCAmelCase ( __A : str , __A : int , __A : Tuple ): a_ : str = dct.pop(__A ) a_ : Dict = val def _UpperCAmelCase ( __A : List[str] , __A : Optional[int] ): a_ : str = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): a_ : Tuple = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) a_ : List[str] = state_dict.pop(f'backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.weight' ) a_ : str = state_dict.pop(f'backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.bias' ) # next, add query, keys and values (in that order) to the state dict a_ : Optional[Any] = in_proj_weight[:dim, :] a_ : List[Any] = in_proj_bias[: dim] a_ : Optional[Any] = in_proj_weight[ dim : dim * 2, : ] a_ : Union[str, Any] = in_proj_bias[ dim : dim * 2 ] a_ : Optional[int] = in_proj_weight[ -dim :, : ] a_ : int = in_proj_bias[-dim :] # fmt: on def _UpperCAmelCase ( __A : Dict , __A : Dict ): # transformer decoder self-attention layers a_ : Any = config.d_model for i in range(config.decoder_layers ): # read in weights + bias of input projection layer of self-attention a_ : int = state_dict.pop(f'transformer.decoder.layers.{i}.self_attn.in_proj_weight' ) a_ : Any = state_dict.pop(f'transformer.decoder.layers.{i}.self_attn.in_proj_bias' ) # next, add query, keys and values (in that order) to the state dict a_ : Dict = in_proj_weight[:hidden_size, :] a_ : Tuple = in_proj_bias[:hidden_size] a_ : Any = in_proj_weight[ hidden_size : hidden_size * 2, : ] a_ : Tuple = in_proj_bias[hidden_size : hidden_size * 2] a_ : Optional[int] = in_proj_weight[-hidden_size:, :] a_ : int = in_proj_bias[-hidden_size:] def _UpperCAmelCase ( ): a_ : Union[str, Any] = '''http://images.cocodataset.org/val2017/000000039769.jpg''' a_ : List[str] = Image.open(requests.get(__A , stream=__A ).raw ) return im @torch.no_grad() def _UpperCAmelCase ( __A : int , __A : int , __A : Any ): a_ : Union[str, Any] = get_deta_config(__A ) # load original state dict if model_name == "deta-swin-large": a_ : Optional[Any] = hf_hub_download(repo_id='''nielsr/deta-checkpoints''' , filename='''adet_swin_ft.pth''' ) elif model_name == "deta-swin-large-o365": a_ : List[str] = hf_hub_download(repo_id='''jozhang97/deta-swin-l-o365''' , filename='''deta_swin_pt_o365.pth''' ) else: raise ValueError(f'Model name {model_name} not supported' ) a_ : List[Any] = torch.load(__A , map_location='''cpu''' )['''model'''] # original state dict for name, param in state_dict.items(): print(__A , param.shape ) # rename keys a_ : Union[str, Any] = create_rename_keys(__A ) for src, dest in rename_keys: rename_key(__A , __A , __A ) read_in_swin_q_k_v(__A , config.backbone_config ) read_in_decoder_q_k_v(__A , __A ) # fix some prefixes for key in state_dict.copy().keys(): if "transformer.decoder.class_embed" in key or "transformer.decoder.bbox_embed" in key: a_ : Optional[Any] = state_dict.pop(__A ) a_ : int = val if "input_proj" in key: a_ : str = state_dict.pop(__A ) a_ : Optional[Any] = val if "level_embed" in key or "pos_trans" in key or "pix_trans" in key or "enc_output" in key: a_ : List[str] = state_dict.pop(__A ) a_ : List[Any] = val # finally, create HuggingFace model and load state dict a_ : Dict = DetaForObjectDetection(__A ) model.load_state_dict(__A ) model.eval() a_ : int = '''cuda''' if torch.cuda.is_available() else '''cpu''' model.to(__A ) # load image processor a_ : List[Any] = DetaImageProcessor(format='''coco_detection''' ) # verify our conversion on image a_ : Dict = prepare_img() a_ : Optional[int] = processor(images=__A , return_tensors='''pt''' ) a_ : Any = encoding['''pixel_values'''] a_ : int = model(pixel_values.to(__A ) ) # verify logits print('''Logits:''' , outputs.logits[0, :3, :3] ) print('''Boxes:''' , outputs.pred_boxes[0, :3, :3] ) if model_name == "deta-swin-large": a_ : Optional[int] = torch.tensor( [[-7.6308, -2.8485, -5.3737], [-7.2037, -4.5505, -4.8027], [-7.2943, -4.2611, -4.6617]] ) a_ : Tuple = torch.tensor([[0.4987, 0.4969, 0.9999], [0.2549, 0.5498, 0.4805], [0.5498, 0.2757, 0.0569]] ) elif model_name == "deta-swin-large-o365": a_ : Union[str, Any] = torch.tensor( [[-8.0122, -3.5720, -4.9717], [-8.1547, -3.6886, -4.6389], [-7.6610, -3.6194, -5.0134]] ) a_ : Any = torch.tensor([[0.2523, 0.5549, 0.4881], [0.7715, 0.4149, 0.4601], [0.5503, 0.2753, 0.0575]] ) assert torch.allclose(outputs.logits[0, :3, :3] , expected_logits.to(__A ) , atol=1E-4 ) assert torch.allclose(outputs.pred_boxes[0, :3, :3] , expected_boxes.to(__A ) , atol=1E-4 ) print('''Everything ok!''' ) if pytorch_dump_folder_path: # Save model and processor logger.info(f'Saving PyTorch model and processor to {pytorch_dump_folder_path}...' ) Path(__A ).mkdir(exist_ok=__A ) model.save_pretrained(__A ) processor.save_pretrained(__A ) # Push to hub if push_to_hub: print('''Pushing model and processor to hub...''' ) model.push_to_hub(f'jozhang97/{model_name}' ) processor.push_to_hub(f'jozhang97/{model_name}' ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() parser.add_argument( '--model_name', type=str, default='deta-swin-large', choices=['deta-swin-large', 'deta-swin-large-o365'], help='Name of the model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the folder to output PyTorch model.', ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.' ) __lowerCAmelCase = parser.parse_args() convert_deta_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
666
0
'''simple docstring''' import mpmath # for roots of unity import numpy as np class SCREAMING_SNAKE_CASE : def __init__( self : int , __SCREAMING_SNAKE_CASE : Union[str, Any]=None , __SCREAMING_SNAKE_CASE : Optional[int]=None ) -> List[str]: # Input as list a_ : Tuple = list(poly_a or [0] )[:] a_ : int = list(poly_b or [0] )[:] # Remove leading zero coefficients while self.polyA[-1] == 0: self.polyA.pop() a_ : str = len(self.polyA ) while self.polyB[-1] == 0: self.polyB.pop() a_ : Tuple = len(self.polyB ) # Add 0 to make lengths equal a power of 2 a_ : Tuple = int( 2 ** np.ceil(np.loga(len(self.polyA ) + len(self.polyB ) - 1 ) ) ) while len(self.polyA ) < self.c_max_length: self.polyA.append(0 ) while len(self.polyB ) < self.c_max_length: self.polyB.append(0 ) # A complex root used for the fourier transform a_ : Optional[Any] = complex(mpmath.root(x=1 , n=self.c_max_length , k=1 ) ) # The product a_ : int = self.__multiply() def SCREAMING_SNAKE_CASE ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : Dict ) -> List[str]: a_ : Tuple = [[x] for x in self.polyA] if which == '''A''' else [[x] for x in self.polyB] # Corner case if len(__SCREAMING_SNAKE_CASE ) <= 1: return dft[0] # a_ : Union[str, Any] = self.c_max_length // 2 while next_ncol > 0: a_ : List[str] = [[] for i in range(__SCREAMING_SNAKE_CASE )] a_ : Optional[Any] = self.root**next_ncol # First half of next step a_ : Optional[int] = 1 for j in range(self.c_max_length // (next_ncol * 2) ): for i in range(__SCREAMING_SNAKE_CASE ): new_dft[i].append(dft[i][j] + current_root * dft[i + next_ncol][j] ) current_root *= root # Second half of next step a_ : List[str] = 1 for j in range(self.c_max_length // (next_ncol * 2) ): for i in range(__SCREAMING_SNAKE_CASE ): new_dft[i].append(dft[i][j] - current_root * dft[i + next_ncol][j] ) current_root *= root # Update a_ : Any = new_dft a_ : str = next_ncol // 2 return dft[0] def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Tuple: a_ : List[Any] = self.__dft('''A''' ) a_ : Dict = self.__dft('''B''' ) a_ : Union[str, Any] = [[dft_a[i] * dft_b[i] for i in range(self.c_max_length )]] del dft_a del dft_b # Corner Case if len(inverce_c[0] ) <= 1: return inverce_c[0] # Inverse DFT a_ : List[Any] = 2 while next_ncol <= self.c_max_length: a_ : int = [[] for i in range(__SCREAMING_SNAKE_CASE )] a_ : Tuple = self.root ** (next_ncol // 2) a_ : Dict = 1 # First half of next step for j in range(self.c_max_length // next_ncol ): for i in range(next_ncol // 2 ): # Even positions new_inverse_c[i].append( ( inverce_c[i][j] + inverce_c[i][j + self.c_max_length // next_ncol] ) / 2 ) # Odd positions new_inverse_c[i + next_ncol // 2].append( ( inverce_c[i][j] - inverce_c[i][j + self.c_max_length // next_ncol] ) / (2 * current_root) ) current_root *= root # Update a_ : Optional[Any] = new_inverse_c next_ncol *= 2 # Unpack a_ : Optional[int] = [round(x[0].real , 8 ) + round(x[0].imag , 8 ) * 1J for x in inverce_c] # Remove leading 0's while inverce_c[-1] == 0: inverce_c.pop() return inverce_c def __str__( self : Optional[int] ) -> List[Any]: a_ : Any = '''A = ''' + ''' + '''.join( f'{coef}*x^{i}' for coef, i in enumerate(self.polyA[: self.len_A] ) ) a_ : Any = '''B = ''' + ''' + '''.join( f'{coef}*x^{i}' for coef, i in enumerate(self.polyB[: self.len_B] ) ) a_ : Optional[Any] = '''A*B = ''' + ''' + '''.join( f'{coef}*x^{i}' for coef, i in enumerate(self.product ) ) return f'{a}\n{b}\n{c}' # Unit tests if __name__ == "__main__": import doctest doctest.testmod()
718
'''simple docstring''' import unittest import numpy as np import torch from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = DDIMPipeline snake_case__ = UNCONDITIONAL_IMAGE_GENERATION_PARAMS snake_case__ = PipelineTesterMixin.required_optional_params - { "num_images_per_prompt", "latents", "callback", "callback_steps", } snake_case__ = UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS snake_case__ = False def SCREAMING_SNAKE_CASE ( self : Any ) -> Optional[Any]: torch.manual_seed(0 ) a_ : 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''') , ) a_ : str = DDIMScheduler() a_ : Union[str, Any] = {'''unet''': unet, '''scheduler''': scheduler} return components def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Tuple=0 ) -> str: if str(__SCREAMING_SNAKE_CASE ).startswith('''mps''' ): a_ : Dict = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: a_ : Union[str, Any] = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = { '''batch_size''': 1, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union[str, Any]: a_ : Dict = '''cpu''' a_ : List[Any] = self.get_dummy_components() a_ : List[str] = self.pipeline_class(**__SCREAMING_SNAKE_CASE ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = self.get_dummy_inputs(__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = pipe(**__SCREAMING_SNAKE_CASE ).images a_ : List[str] = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 32, 32, 3) ) a_ : int = np.array( [1.000e00, 5.717e-01, 4.717e-01, 1.000e00, 0.000e00, 1.000e00, 3.000e-04, 0.000e00, 9.000e-04] ) a_ : Union[str, Any] = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(__SCREAMING_SNAKE_CASE , 1e-3 ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> str: super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : str ) -> Optional[int]: super().test_save_load_local(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : Any ) -> List[str]: super().test_save_load_optional_components(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : str ) -> Optional[int]: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : str ) -> Any: a_ : Optional[Any] = '''google/ddpm-cifar10-32''' a_ : Optional[Any] = UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Dict = DDIMScheduler() a_ : List[str] = DDIMPipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) ddim.to(__SCREAMING_SNAKE_CASE ) ddim.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = torch.manual_seed(0 ) a_ : Tuple = ddim(generator=__SCREAMING_SNAKE_CASE , eta=0.0 , output_type='''numpy''' ).images a_ : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) a_ : List[str] = np.array([0.1723, 0.1617, 0.1600, 0.1626, 0.1497, 0.1513, 0.1505, 0.1442, 0.1453] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> str: a_ : int = '''google/ddpm-ema-bedroom-256''' a_ : str = UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Tuple = DDIMScheduler.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = DDIMPipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) ddpm.to(__SCREAMING_SNAKE_CASE ) ddpm.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = torch.manual_seed(0 ) a_ : List[Any] = ddpm(generator=__SCREAMING_SNAKE_CASE , output_type='''numpy''' ).images a_ : Optional[int] = image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) a_ : Optional[Any] = np.array([0.0060, 0.0201, 0.0344, 0.0024, 0.0018, 0.0002, 0.0022, 0.0000, 0.0069] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
666
0
'''simple docstring''' def _UpperCAmelCase ( __A : list ): if len(__A ) <= 1: return [tuple(__A )] a_ : Optional[int] = [] def generate(__A : int , __A : list ): a_ : Tuple = [0] * n res.append(tuple(__A ) ) a_ : int = 0 while i < n: if c[i] < i: if i % 2 == 0: a_ : Optional[Any] = arr[i], arr[0] else: a_ : int = arr[i], arr[c[i]] res.append(tuple(__A ) ) c[i] += 1 a_ : List[Any] = 0 else: a_ : List[Any] = 0 i += 1 generate(len(__A ) , __A ) return res if __name__ == "__main__": __lowerCAmelCase = input('Enter numbers separated by a comma:\n').strip() __lowerCAmelCase = [int(item) for item in user_input.split(',')] print(heaps(arr))
719
'''simple docstring''' from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import pyarrow as pa if TYPE_CHECKING: from .features import FeatureType @dataclass class SCREAMING_SNAKE_CASE : snake_case__ = 42 snake_case__ = None # Automatically constructed snake_case__ = "dict" snake_case__ = None snake_case__ = field(default="Translation" , init=SCREAMING_SNAKE_CASE_ , repr=SCREAMING_SNAKE_CASE_ ) def __call__( self : Dict ) -> Tuple: return pa.struct({lang: pa.string() for lang in sorted(self.languages )} ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Value return {k: Value('''string''' ) for k in sorted(self.languages )} @dataclass class SCREAMING_SNAKE_CASE : snake_case__ = None snake_case__ = None snake_case__ = None # Automatically constructed snake_case__ = "dict" snake_case__ = None snake_case__ = field(default="TranslationVariableLanguages" , init=SCREAMING_SNAKE_CASE_ , repr=SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : List[str] = sorted(set(self.languages ) ) if self.languages else None a_ : Optional[Any] = len(self.languages ) if self.languages else None def __call__( self : Any ) -> Optional[Any]: return pa.struct({'''language''': pa.list_(pa.string() ), '''translation''': pa.list_(pa.string() )} ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Optional[Any]: a_ : str = set(self.languages ) if self.languages and set(__SCREAMING_SNAKE_CASE ) - lang_set: raise ValueError( f'Some languages in example ({", ".join(sorted(set(__SCREAMING_SNAKE_CASE ) - lang_set ) )}) are not in valid set ({", ".join(__SCREAMING_SNAKE_CASE )}).' ) # Convert dictionary into tuples, splitting out cases where there are # multiple translations for a single language. a_ : int = [] for lang, text in translation_dict.items(): if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): translation_tuples.append((lang, text) ) else: translation_tuples.extend([(lang, el) for el in text] ) # Ensure translations are in ascending order by language code. a_ , a_ : List[Any] = zip(*sorted(__SCREAMING_SNAKE_CASE ) ) return {"language": languages, "translation": translations} def SCREAMING_SNAKE_CASE ( self : Any ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Sequence, Value return { "language": Sequence(Value('''string''' ) ), "translation": Sequence(Value('''string''' ) ), }
666
0
'''simple docstring''' import datasets import faiss import numpy as np import streamlit as st import torch from elasticsearch import Elasticsearch from elia_utils import ( embed_questions_for_retrieval, make_qa_sas_model, qa_sas_generate, query_es_index, query_qa_dense_index, ) import transformers from transformers import AutoModel, AutoModelForSeqaSeqLM, AutoTokenizer __lowerCAmelCase = 'bart' __lowerCAmelCase = True @st.cache(allow_output_mutation=__A ) def _UpperCAmelCase ( ): if LOAD_DENSE_INDEX: a_ : Tuple = AutoTokenizer.from_pretrained('''yjernite/retribert-base-uncased''' ) a_ : Tuple = AutoModel.from_pretrained('''yjernite/retribert-base-uncased''' ).to('''cuda:0''' ) a_ : List[str] = qar_model.eval() else: a_ : str = (None, None) if MODEL_TYPE == "bart": a_ : List[Any] = AutoTokenizer.from_pretrained('''yjernite/bart_eli5''' ) a_ : int = AutoModelForSeqaSeqLM.from_pretrained('''yjernite/bart_eli5''' ).to('''cuda:0''' ) a_ : List[Any] = torch.load('''seq2seq_models/eli5_bart_model_blm_2.pth''' ) sas_model.load_state_dict(save_dict['''model'''] ) a_ : str = sas_model.eval() else: a_ : Optional[int] = make_qa_sas_model( model_name='''t5-small''' , from_file='''seq2seq_models/eli5_t5_model_1024_4.pth''' , device='''cuda:0''' ) return (qar_tokenizer, qar_model, sas_tokenizer, sas_model) @st.cache(allow_output_mutation=__A ) def _UpperCAmelCase ( ): if LOAD_DENSE_INDEX: a_ : Optional[int] = faiss.StandardGpuResources() a_ : List[str] = datasets.load_dataset(path='''wiki_snippets''' , name='''wiki40b_en_100_0''' )['''train'''] a_ : Optional[int] = np.memmap( '''wiki40b_passages_reps_32_l-8_h-768_b-512-512.dat''' , dtype='''float32''' , mode='''r''' , shape=(wikiaab_passages.num_rows, 1_28) , ) a_ : Optional[int] = faiss.IndexFlatIP(1_28 ) a_ : Dict = faiss.index_cpu_to_gpu(__A , 1 , __A ) wikiaab_gpu_index_flat.add(__A ) # TODO fix for larger GPU else: a_ : List[Any] = (None, None) a_ : Union[str, Any] = Elasticsearch([{'''host''': '''localhost''', '''port''': '''9200'''}] ) return (wikiaab_passages, wikiaab_gpu_index_flat, es_client) @st.cache(allow_output_mutation=__A ) def _UpperCAmelCase ( ): a_ : List[str] = datasets.load_dataset('''eli5''' , name='''LFQA_reddit''' ) a_ : str = elia['''train_eli5'''] a_ : int = np.memmap( '''eli5_questions_reps.dat''' , dtype='''float32''' , mode='''r''' , shape=(elia_train.num_rows, 1_28) ) a_ : Union[str, Any] = faiss.IndexFlatIP(1_28 ) eli5_train_q_index.add(__A ) return (elia_train, eli5_train_q_index) __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = load_indexes() __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = load_models() __lowerCAmelCase , __lowerCAmelCase = load_train_data() def _UpperCAmelCase ( __A : Tuple , __A : List[str]=10 ): a_ : List[Any] = embed_questions_for_retrieval([question] , __A , __A ) a_ : Dict = eli5_train_q_index.search(__A , __A ) a_ : Tuple = [elia_train[int(__A )] for i in I[0]] return nn_examples def _UpperCAmelCase ( __A : Union[str, Any] , __A : Optional[Any]="wiki40b" , __A : List[Any]="dense" , __A : List[Any]=10 ): if source == "none": a_ : List[Any] = (''' <P> '''.join(['''''' for _ in range(11 )] ).strip(), []) else: if method == "dense": a_ : List[Any] = query_qa_dense_index( __A , __A , __A , __A , __A , __A ) else: a_ : Optional[Any] = query_es_index( __A , __A , index_name='''english_wiki40b_snippets_100w''' , n_results=__A , ) a_ : int = [ (res['''article_title'''], res['''section_title'''].strip(), res['''score'''], res['''passage_text''']) for res in hit_lst ] a_ : str = '''question: {} context: {}'''.format(__A , __A ) return question_doc, support_list @st.cache( hash_funcs={ torch.Tensor: (lambda __A : None), transformers.models.bart.tokenization_bart.BartTokenizer: (lambda __A : None), } ) def _UpperCAmelCase ( __A : Optional[int] , __A : Union[str, Any] , __A : Union[str, Any] , __A : List[str]=64 , __A : Optional[Any]=2_56 , __A : int=False , __A : Optional[Any]=2 , __A : int=0.95 , __A : Optional[Any]=0.8 ): with torch.no_grad(): a_ : Optional[Any] = qa_sas_generate( __A , __A , __A , num_answers=1 , num_beams=__A , min_len=__A , max_len=__A , do_sample=__A , temp=__A , top_p=__A , top_k=__A , max_input_length=10_24 , device='''cuda:0''' , )[0] return (answer, support_list) st.title('Long Form Question Answering with ELI5') # Start sidebar __lowerCAmelCase = '<img src=\'https://huggingface.co/front/assets/huggingface_logo.svg\'>' __lowerCAmelCase = '\n<html>\n <head>\n <style>\n .img-container {\n padding-left: 90px;\n padding-right: 90px;\n padding-top: 50px;\n padding-bottom: 50px;\n background-color: #f0f3f9;\n }\n </style>\n </head>\n <body>\n <span class="img-container"> <!-- Inline parent element -->\n %s\n </span>\n </body>\n</html>\n' % ( header_html, ) st.sidebar.markdown( header_full, unsafe_allow_html=True, ) # Long Form QA with ELI5 and Wikipedia __lowerCAmelCase = '\nThis demo presents a model trained to [provide long-form answers to open-domain questions](https://yjernite.github.io/lfqa.html).\nFirst, a document retriever fetches a set of relevant Wikipedia passages given the question from the [Wiki40b](https://research.google/pubs/pub49029/) dataset,\na pre-processed fixed snapshot of Wikipedia.\n' st.sidebar.markdown(description, unsafe_allow_html=True) __lowerCAmelCase = [ 'Answer the question', 'View the retrieved document only', 'View the most similar ELI5 question and answer', 'Show me everything, please!', ] __lowerCAmelCase = st.sidebar.checkbox('Demo options') if demo_options: __lowerCAmelCase = st.sidebar.selectbox( '', action_list, index=3, ) __lowerCAmelCase = action_list.index(action_st) __lowerCAmelCase = st.sidebar.selectbox( '', ['Show full text of passages', 'Show passage section titles'], index=0, ) __lowerCAmelCase = show_type == 'Show full text of passages' else: __lowerCAmelCase = 3 __lowerCAmelCase = True __lowerCAmelCase = st.sidebar.checkbox('Retrieval options') if retrieval_options: __lowerCAmelCase = '\n ### Information retriever options\n\n The **sparse** retriever uses ElasticSearch, while the **dense** retriever uses max-inner-product search between a question and passage embedding\n trained using the [ELI5](https://arxiv.org/abs/1907.09190) questions-answer pairs.\n The answer is then generated by sequence to sequence model which takes the question and retrieved document as input.\n ' st.sidebar.markdown(retriever_info) __lowerCAmelCase = st.sidebar.selectbox('Which Wikipedia format should the model use?', ['wiki40b', 'none']) __lowerCAmelCase = st.sidebar.selectbox('Which Wikipedia indexer should the model use?', ['dense', 'sparse', 'mixed']) else: __lowerCAmelCase = 'wiki40b' __lowerCAmelCase = 'dense' __lowerCAmelCase = 'beam' __lowerCAmelCase = 2 __lowerCAmelCase = 64 __lowerCAmelCase = 256 __lowerCAmelCase = None __lowerCAmelCase = None __lowerCAmelCase = st.sidebar.checkbox('Generation options') if generate_options: __lowerCAmelCase = '\n ### Answer generation options\n\n The sequence-to-sequence model was initialized with [BART](https://huggingface.co/facebook/bart-large)\n weights and fine-tuned on the ELI5 QA pairs and retrieved documents. You can use the model for greedy decoding with\n **beam** search, or **sample** from the decoder\'s output probabilities.\n ' st.sidebar.markdown(generate_info) __lowerCAmelCase = st.sidebar.selectbox('Would you like to use beam search or sample an answer?', ['beam', 'sampled']) __lowerCAmelCase = st.sidebar.slider( 'Minimum generation length', min_value=8, max_value=256, value=64, step=8, format=None, key=None ) __lowerCAmelCase = st.sidebar.slider( 'Maximum generation length', min_value=64, max_value=512, value=256, step=16, format=None, key=None ) if sampled == "beam": __lowerCAmelCase = st.sidebar.slider('Beam size', min_value=1, max_value=8, value=2, step=None, format=None, key=None) else: __lowerCAmelCase = st.sidebar.slider( 'Nucleus sampling p', min_value=0.1, max_value=1.0, value=0.95, step=0.01, format=None, key=None ) __lowerCAmelCase = st.sidebar.slider( 'Temperature', min_value=0.1, max_value=1.0, value=0.7, step=0.01, format=None, key=None ) __lowerCAmelCase = None # start main text __lowerCAmelCase = [ '<MY QUESTION>', 'How do people make chocolate?', 'Why do we get a fever when we are sick?', 'How can different animals perceive different colors?', 'What is natural language processing?', 'What\'s the best way to treat a sunburn?', 'What exactly are vitamins ?', 'How does nuclear energy provide electricity?', 'What\'s the difference between viruses and bacteria?', 'Why are flutes classified as woodwinds when most of them are made out of metal ?', 'Why do people like drinking coffee even though it tastes so bad?', 'What happens when wine ages? How does it make the wine taste better?', 'If an animal is an herbivore, where does it get the protein that it needs to survive if it only eats grass?', 'How can we set a date to the beginning or end of an artistic period? Doesn\'t the change happen gradually?', 'How does New Zealand have so many large bird predators?', ] __lowerCAmelCase = st.selectbox( 'What would you like to ask? ---- select <MY QUESTION> to enter a new query', questions_list, index=1, ) if question_s == "<MY QUESTION>": __lowerCAmelCase = st.text_input('Enter your question here:', '') else: __lowerCAmelCase = question_s if st.button('Show me!'): if action in [0, 1, 3]: if index_type == "mixed": __lowerCAmelCase , __lowerCAmelCase = make_support(question, source=wiki_source, method='dense', n_results=10) __lowerCAmelCase , __lowerCAmelCase = make_support(question, source=wiki_source, method='sparse', n_results=10) __lowerCAmelCase = [] for res_d, res_s in zip(support_list_dense, support_list_sparse): if tuple(res_d) not in support_list: support_list += [tuple(res_d)] if tuple(res_s) not in support_list: support_list += [tuple(res_s)] __lowerCAmelCase = support_list[:10] __lowerCAmelCase = '<P> ' + ' <P> '.join([res[-1] for res in support_list]) else: __lowerCAmelCase , __lowerCAmelCase = make_support(question, source=wiki_source, method=index_type, n_results=10) if action in [0, 3]: __lowerCAmelCase , __lowerCAmelCase = answer_question( question_doc, sas_model, sas_tokenizer, min_len=min_len, max_len=int(max_len), sampling=(sampled == 'sampled'), n_beams=n_beams, top_p=top_p, temp=temp, ) st.markdown('### The model generated answer is:') st.write(answer) if action in [0, 1, 3] and wiki_source != "none": st.markdown('--- \n ### The model is drawing information from the following Wikipedia passages:') for i, res in enumerate(support_list): __lowerCAmelCase = 'https://en.wikipedia.org/wiki/{}'.format(res[0].replace(' ', '_')) __lowerCAmelCase = res[1].strip() if sec_titles == "": __lowerCAmelCase = '[{}]({})'.format(res[0], wiki_url) else: __lowerCAmelCase = sec_titles.split(' & ') __lowerCAmelCase = ' & '.join( ['[{}]({}#{})'.format(sec.strip(), wiki_url, sec.strip().replace(' ', '_')) for sec in sec_list] ) st.markdown( '{0:02d} - **Article**: {1:<18} <br> _Section_: {2}'.format(i + 1, res[0], sections), unsafe_allow_html=True, ) if show_passages: st.write( '> <span style="font-family:arial; font-size:10pt;">' + res[-1] + '</span>', unsafe_allow_html=True ) if action in [2, 3]: __lowerCAmelCase = find_nearest_training(question) __lowerCAmelCase = nn_train_list[0] st.markdown( '--- \n ### The most similar question in the ELI5 training set was: \n\n {}'.format(train_exple['title']) ) __lowerCAmelCase = [ '{}. {}'.format(i + 1, ' \n'.join([line.strip() for line in ans.split('\n') if line.strip() != ''])) for i, (ans, sc) in enumerate(zip(train_exple['answers']['text'], train_exple['answers']['score'])) if i == 0 or sc > 2 ] st.markdown('##### Its answers were: \n\n {}'.format('\n'.join(answers_st))) __lowerCAmelCase = '\n---\n\n**Disclaimer**\n\n*The intent of this app is to provide some (hopefully entertaining) insights into the behavior of a current LFQA system.\nEvaluating biases of such a model and ensuring factual generations are still very much open research problems.\nTherefore, until some significant progress is achieved, we caution against using the generated answers for practical purposes.*\n' st.sidebar.markdown(disclaimer, unsafe_allow_html=True)
720
'''simple docstring''' import json import os import pickle import shutil import tempfile from unittest import TestCase from unittest.mock import patch import numpy as np from datasets import Dataset from transformers import is_faiss_available from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bart.tokenization_bart import BartTokenizer from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.dpr.tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.retrieval_rag import CustomHFIndex, RagRetriever from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_sentencepiece, require_tokenizers, require_torch if is_faiss_available(): import faiss @require_faiss class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): def SCREAMING_SNAKE_CASE ( self : Dict ) -> Dict: a_ : Union[str, Any] = tempfile.mkdtemp() a_ : Union[str, Any] = 8 # DPR tok a_ : Dict = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] a_ : str = os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = os.path.join(__SCREAMING_SNAKE_CASE , DPR_VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) # BART tok a_ : Union[str, Any] = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] a_ : int = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) a_ : int = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] a_ : Optional[int] = {'''unk_token''': '''<unk>'''} a_ : List[str] = os.path.join(self.tmpdirname , '''bart_tokenizer''' ) os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) a_ : Tuple = os.path.join(__SCREAMING_SNAKE_CASE , BART_VOCAB_FILES_NAMES['''vocab_file'''] ) a_ : int = os.path.join(__SCREAMING_SNAKE_CASE , BART_VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> DPRQuestionEncoderTokenizer: return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : str ) -> DPRContextEncoderTokenizer: return DPRContextEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> BartTokenizer: return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''bart_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Union[str, Any]: shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> Union[str, Any]: a_ : str = Dataset.from_dict( { '''id''': ['''0''', '''1'''], '''text''': ['''foo''', '''bar'''], '''title''': ['''Foo''', '''Bar'''], '''embeddings''': [np.ones(self.retrieval_vector_size ), 2 * np.ones(self.retrieval_vector_size )], } ) dataset.add_faiss_index('''embeddings''' , string_factory='''Flat''' , metric_type=faiss.METRIC_INNER_PRODUCT ) return dataset def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Tuple: a_ : List[str] = self.get_dummy_dataset() a_ : Tuple = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , ) with patch('''transformers.models.rag.retrieval_rag.load_dataset''' ) as mock_load_dataset: a_ : Tuple = dataset a_ : Any = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) return retriever def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : bool ) -> Dict: a_ : Dict = self.get_dummy_dataset() a_ : Dict = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''custom''' , ) if from_disk: a_ : Optional[int] = os.path.join(self.tmpdirname , '''dataset''' ) a_ : str = os.path.join(self.tmpdirname , '''index.faiss''' ) dataset.get_index('''embeddings''' ).save(os.path.join(self.tmpdirname , '''index.faiss''' ) ) dataset.drop_index('''embeddings''' ) dataset.save_to_disk(os.path.join(self.tmpdirname , '''dataset''' ) ) del dataset a_ : int = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) else: a_ : Optional[Any] = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , index=CustomHFIndex(config.retrieval_vector_size , __SCREAMING_SNAKE_CASE ) , ) return retriever def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Optional[int]: a_ : str = Dataset.from_dict( { '''id''': ['''0''', '''1'''], '''text''': ['''foo''', '''bar'''], '''title''': ['''Foo''', '''Bar'''], '''embeddings''': [np.ones(self.retrieval_vector_size + 1 ), 2 * np.ones(self.retrieval_vector_size + 1 )], } ) dataset.add_faiss_index('''embeddings''' , string_factory='''Flat''' , metric_type=faiss.METRIC_INNER_PRODUCT ) a_ : Optional[int] = os.path.join(self.tmpdirname , '''hf_bert_base.hnswSQ8_correct_phi_128.c_index''' ) dataset.save_faiss_index('''embeddings''' , index_file_name + '''.index.dpr''' ) pickle.dump(dataset['''id'''] , open(index_file_name + '''.index_meta.dpr''' , '''wb''' ) ) a_ : Union[str, Any] = os.path.join(self.tmpdirname , '''psgs_w100.tsv.pkl''' ) a_ : Dict = {sample['''id''']: [sample['''text'''], sample['''title''']] for sample in dataset} pickle.dump(__SCREAMING_SNAKE_CASE , open(__SCREAMING_SNAKE_CASE , '''wb''' ) ) a_ : Optional[Any] = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''legacy''' , index_path=self.tmpdirname , ) a_ : int = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() ) return retriever def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : Optional[Any] = 1 a_ : Dict = self.get_dummy_canonical_hf_index_retriever() a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : str = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : str = self.get_dummy_canonical_hf_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: with patch('''transformers.models.rag.retrieval_rag.load_dataset''' ) as mock_load_dataset: a_ : List[str] = self.get_dummy_dataset() retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : List[str] = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> int: a_ : Union[str, Any] = 1 a_ : Optional[Any] = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) a_ : List[Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : Any = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : str ) -> List[str]: a_ : Dict = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : List[str] = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Dict = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Optional[int]: a_ : Union[str, Any] = 1 a_ : str = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : Tuple = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : int ) -> Dict: a_ : List[str] = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Dict = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Any: a_ : str = 1 a_ : Tuple = self.get_dummy_legacy_index_retriever() a_ : Union[str, Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : Any = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''text'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''text'''][0] , '''bar''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''text'''][0] , '''foo''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[int]: a_ : List[str] = self.get_dummy_legacy_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : List[str] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Optional[Any] = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) @require_torch @require_tokenizers @require_sentencepiece def SCREAMING_SNAKE_CASE ( self : int ) -> List[str]: import torch a_ : Any = 1 a_ : List[Any] = self.get_dummy_canonical_hf_index_retriever() a_ : Union[str, Any] = [[5, 7], [10, 11]] a_ : Optional[int] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : str = retriever(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE ) a_ , a_ , a_ : List[str] = ( out['''context_input_ids'''], out['''context_attention_mask'''], out['''retrieved_doc_embeds'''], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , np.ndarray ) a_ : Any = retriever( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE , return_tensors='''pt''' , ) a_ , a_ , a_ , a_ : str = ( # noqa: F841 out['''context_input_ids'''], out['''context_attention_mask'''], out['''retrieved_doc_embeds'''], out['''doc_ids'''], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) @require_torch @require_tokenizers @require_sentencepiece def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Any: a_ : str = self.get_dpr_ctx_encoder_tokenizer() a_ : Tuple = 1 a_ : Any = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) retriever.set_ctx_encoder_tokenizer(__SCREAMING_SNAKE_CASE ) a_ : Dict = [[5, 7], [10, 11]] a_ : List[str] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : List[Any] = retriever(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual( len(__SCREAMING_SNAKE_CASE ) , 6 ) # check whether the retriever output consist of 6 attributes including tokenized docs self.assertEqual( all(k in out for k in ('''tokenized_doc_ids''', '''tokenized_doc_attention_mask''') ) , __SCREAMING_SNAKE_CASE ) # check for doc token related keys in dictionary.
666
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCAmelCase = { 'configuration_table_transformer': [ 'TABLE_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'TableTransformerConfig', 'TableTransformerOnnxConfig', ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ 'TABLE_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'TableTransformerForObjectDetection', 'TableTransformerModel', 'TableTransformerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_table_transformer import ( TABLE_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TableTransformerConfig, TableTransformerOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_table_transformer import ( TABLE_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TableTransformerForObjectDetection, TableTransformerModel, TableTransformerPreTrainedModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
721
'''simple docstring''' from math import pi, sqrt, tan def _UpperCAmelCase ( __A : float ): if side_length < 0: raise ValueError('''surface_area_cube() only accepts non-negative values''' ) return 6 * side_length**2 def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if length < 0 or breadth < 0 or height < 0: raise ValueError('''surface_area_cuboid() only accepts non-negative values''' ) return 2 * ((length * breadth) + (breadth * height) + (length * height)) def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''surface_area_sphere() only accepts non-negative values''' ) return 4 * pi * radius**2 def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''surface_area_hemisphere() only accepts non-negative values''' ) return 3 * pi * radius**2 def _UpperCAmelCase ( __A : float , __A : float ): if radius < 0 or height < 0: raise ValueError('''surface_area_cone() only accepts non-negative values''' ) return pi * radius * (radius + (height**2 + radius**2) ** 0.5) def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if radius_a < 0 or radius_a < 0 or height < 0: raise ValueError( '''surface_area_conical_frustum() only accepts non-negative values''' ) a_ : Any = (height**2 + (radius_a - radius_a) ** 2) ** 0.5 return pi * ((slant_height * (radius_a + radius_a)) + radius_a**2 + radius_a**2) def _UpperCAmelCase ( __A : float , __A : float ): if radius < 0 or height < 0: raise ValueError('''surface_area_cylinder() only accepts non-negative values''' ) return 2 * pi * radius * (height + radius) def _UpperCAmelCase ( __A : float , __A : float ): if torus_radius < 0 or tube_radius < 0: raise ValueError('''surface_area_torus() only accepts non-negative values''' ) if torus_radius < tube_radius: raise ValueError( '''surface_area_torus() does not support spindle or self intersecting tori''' ) return 4 * pow(__A , 2 ) * torus_radius * tube_radius def _UpperCAmelCase ( __A : float , __A : float ): if length < 0 or width < 0: raise ValueError('''area_rectangle() only accepts non-negative values''' ) return length * width def _UpperCAmelCase ( __A : float ): if side_length < 0: raise ValueError('''area_square() only accepts non-negative values''' ) return side_length**2 def _UpperCAmelCase ( __A : float , __A : float ): if base < 0 or height < 0: raise ValueError('''area_triangle() only accepts non-negative values''' ) return (base * height) / 2 def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if sidea < 0 or sidea < 0 or sidea < 0: raise ValueError('''area_triangle_three_sides() only accepts non-negative values''' ) elif sidea + sidea < sidea or sidea + sidea < sidea or sidea + sidea < sidea: raise ValueError('''Given three sides do not form a triangle''' ) a_ : int = (sidea + sidea + sidea) / 2 a_ : Optional[Any] = sqrt( semi_perimeter * (semi_perimeter - sidea) * (semi_perimeter - sidea) * (semi_perimeter - sidea) ) return area def _UpperCAmelCase ( __A : float , __A : float ): if base < 0 or height < 0: raise ValueError('''area_parallelogram() only accepts non-negative values''' ) return base * height def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if basea < 0 or basea < 0 or height < 0: raise ValueError('''area_trapezium() only accepts non-negative values''' ) return 1 / 2 * (basea + basea) * height def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''area_circle() only accepts non-negative values''' ) return pi * radius**2 def _UpperCAmelCase ( __A : float , __A : float ): if radius_x < 0 or radius_y < 0: raise ValueError('''area_ellipse() only accepts non-negative values''' ) return pi * radius_x * radius_y def _UpperCAmelCase ( __A : float , __A : float ): if diagonal_a < 0 or diagonal_a < 0: raise ValueError('''area_rhombus() only accepts non-negative values''' ) return 1 / 2 * diagonal_a * diagonal_a def _UpperCAmelCase ( __A : int , __A : float ): if not isinstance(__A , __A ) or sides < 3: raise ValueError( '''area_reg_polygon() only accepts integers greater than or \ equal to three as number of sides''' ) elif length < 0: raise ValueError( '''area_reg_polygon() only accepts non-negative values as \ length of a side''' ) return (sides * length**2) / (4 * tan(pi / sides )) return (sides * length**2) / (4 * tan(pi / sides )) if __name__ == "__main__": import doctest doctest.testmod(verbose=True) # verbose so we can see methods missing tests print('[DEMO] Areas of various geometric shapes: \n') print(F"""Rectangle: {area_rectangle(10, 20) = }""") print(F"""Square: {area_square(10) = }""") print(F"""Triangle: {area_triangle(10, 10) = }""") print(F"""Triangle: {area_triangle_three_sides(5, 12, 13) = }""") print(F"""Parallelogram: {area_parallelogram(10, 20) = }""") print(F"""Rhombus: {area_rhombus(10, 20) = }""") print(F"""Trapezium: {area_trapezium(10, 20, 30) = }""") print(F"""Circle: {area_circle(20) = }""") print(F"""Ellipse: {area_ellipse(10, 20) = }""") print('\nSurface Areas of various geometric shapes: \n') print(F"""Cube: {surface_area_cube(20) = }""") print(F"""Cuboid: {surface_area_cuboid(10, 20, 30) = }""") print(F"""Sphere: {surface_area_sphere(20) = }""") print(F"""Hemisphere: {surface_area_hemisphere(20) = }""") print(F"""Cone: {surface_area_cone(10, 20) = }""") print(F"""Conical Frustum: {surface_area_conical_frustum(10, 20, 30) = }""") print(F"""Cylinder: {surface_area_cylinder(10, 20) = }""") print(F"""Torus: {surface_area_torus(20, 10) = }""") print(F"""Equilateral Triangle: {area_reg_polygon(3, 10) = }""") print(F"""Square: {area_reg_polygon(4, 10) = }""") print(F"""Reqular Pentagon: {area_reg_polygon(5, 10) = }""")
666
0
'''simple docstring''' def _UpperCAmelCase ( __A : dict ): '''simple docstring''' a_ : set[int] = set() # To detect a back edge, keep track of vertices currently in the recursion stack a_ : set[int] = set() return any( node not in visited and depth_first_search(__A , __A , __A , __A ) for node in graph ) def _UpperCAmelCase ( __A : dict , __A : int , __A : set , __A : set ): '''simple docstring''' visited.add(__A ) rec_stk.add(__A ) for node in graph[vertex]: if node not in visited: if depth_first_search(__A , __A , __A , __A ): return True elif node in rec_stk: return True # The node needs to be removed from recursion stack before function ends rec_stk.remove(__A ) return False if __name__ == "__main__": from doctest import testmod testmod()
700
'''simple docstring''' import random import unittest import torch from diffusers import IFInpaintingSuperResolutionPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = IFInpaintingSuperResolutionPipeline snake_case__ = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"width", "height"} snake_case__ = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({"original_image"} ) snake_case__ = PipelineTesterMixin.required_optional_params - {"latents"} def SCREAMING_SNAKE_CASE ( self : str ) -> List[str]: return self._get_superresolution_dummy_components() def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Dict=0 ) -> List[Any]: if str(__SCREAMING_SNAKE_CASE ).startswith('''mps''' ): a_ : Optional[int] = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: a_ : str = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) a_ : Dict = floats_tensor((1, 3, 16, 16) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = floats_tensor((1, 3, 32, 32) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) a_ : Tuple = floats_tensor((1, 3, 32, 32) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': image, '''original_image''': original_image, '''mask_image''': mask_image, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) def SCREAMING_SNAKE_CASE ( self : int ) -> int: self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def SCREAMING_SNAKE_CASE ( self : str ) -> Tuple: # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder super().test_save_load_floataa(expected_max_diff=1e-1 ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Optional[int]: self._test_attention_slicing_forward_pass(expected_max_diff=1e-2 ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Optional[Any]: self._test_save_load_local() def SCREAMING_SNAKE_CASE ( self : Dict ) -> Optional[Any]: self._test_inference_batch_single_identical( expected_max_diff=1e-2 , )
666
0
import inspect import warnings from typing import Any, Dict, Optional, Union from packaging import version def _UpperCAmelCase ( *__A : List[Any] , __A : Optional[Union[Dict, Any]] = None , __A : List[str]=True , __A : List[Any]=2 ): from .. import __version__ a_ : Tuple = take_from a_ : Optional[int] = () if not isinstance(args[0] , __A ): a_ : Optional[int] = (args,) for attribute, version_name, message in args: if version.parse(version.parse(__A ).base_version ) >= version.parse(__A ): raise ValueError( f'The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers\'' f' version {__version__} is >= {version_name}' ) a_ : List[Any] = None if isinstance(__A , __A ) and attribute in deprecated_kwargs: values += (deprecated_kwargs.pop(__A ),) a_ : Any = f'The `{attribute}` argument is deprecated and will be removed in version {version_name}.' elif hasattr(__A , __A ): values += (getattr(__A , __A ),) a_ : List[Any] = f'The `{attribute}` attribute is deprecated and will be removed in version {version_name}.' elif deprecated_kwargs is None: a_ : str = f'`{attribute}` is deprecated and will be removed in version {version_name}.' if warning is not None: a_ : Dict = warning + ''' ''' if standard_warn else '''''' warnings.warn(warning + message , __A , stacklevel=__A ) if isinstance(__A , __A ) and len(__A ) > 0: a_ : Optional[int] = inspect.getouterframes(inspect.currentframe() )[1] a_ : Optional[int] = call_frame.filename a_ : List[str] = call_frame.lineno a_ : List[str] = call_frame.function a_ : str = next(iter(deprecated_kwargs.items() ) ) raise TypeError(f'{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`' ) if len(__A ) == 0: return elif len(__A ) == 1: return values[0] return values
701
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCAmelCase = { 'configuration_git': ['GIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GitConfig', 'GitVisionConfig'], 'processing_git': ['GitProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ 'GIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'GitForCausalLM', 'GitModel', 'GitPreTrainedModel', 'GitVisionModel', ] if TYPE_CHECKING: from .configuration_git import GIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GitConfig, GitVisionConfig from .processing_git import GitProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_git import ( GIT_PRETRAINED_MODEL_ARCHIVE_LIST, GitForCausalLM, GitModel, GitPreTrainedModel, GitVisionModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
666
0
'''simple docstring''' import json from typing import List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_roberta import RobertaTokenizer __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'} __lowerCAmelCase = { 'vocab_file': { 'roberta-base': 'https://huggingface.co/roberta-base/resolve/main/vocab.json', 'roberta-large': 'https://huggingface.co/roberta-large/resolve/main/vocab.json', 'roberta-large-mnli': 'https://huggingface.co/roberta-large-mnli/resolve/main/vocab.json', 'distilroberta-base': 'https://huggingface.co/distilroberta-base/resolve/main/vocab.json', 'roberta-base-openai-detector': 'https://huggingface.co/roberta-base-openai-detector/resolve/main/vocab.json', 'roberta-large-openai-detector': ( 'https://huggingface.co/roberta-large-openai-detector/resolve/main/vocab.json' ), }, 'merges_file': { 'roberta-base': 'https://huggingface.co/roberta-base/resolve/main/merges.txt', 'roberta-large': 'https://huggingface.co/roberta-large/resolve/main/merges.txt', 'roberta-large-mnli': 'https://huggingface.co/roberta-large-mnli/resolve/main/merges.txt', 'distilroberta-base': 'https://huggingface.co/distilroberta-base/resolve/main/merges.txt', 'roberta-base-openai-detector': 'https://huggingface.co/roberta-base-openai-detector/resolve/main/merges.txt', 'roberta-large-openai-detector': ( 'https://huggingface.co/roberta-large-openai-detector/resolve/main/merges.txt' ), }, 'tokenizer_file': { 'roberta-base': 'https://huggingface.co/roberta-base/resolve/main/tokenizer.json', 'roberta-large': 'https://huggingface.co/roberta-large/resolve/main/tokenizer.json', 'roberta-large-mnli': 'https://huggingface.co/roberta-large-mnli/resolve/main/tokenizer.json', 'distilroberta-base': 'https://huggingface.co/distilroberta-base/resolve/main/tokenizer.json', 'roberta-base-openai-detector': ( 'https://huggingface.co/roberta-base-openai-detector/resolve/main/tokenizer.json' ), 'roberta-large-openai-detector': ( 'https://huggingface.co/roberta-large-openai-detector/resolve/main/tokenizer.json' ), }, } __lowerCAmelCase = { 'roberta-base': 512, 'roberta-large': 512, 'roberta-large-mnli': 512, 'distilroberta-base': 512, 'roberta-base-openai-detector': 512, 'roberta-large-openai-detector': 512, } class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = VOCAB_FILES_NAMES snake_case__ = PRETRAINED_VOCAB_FILES_MAP snake_case__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case__ = ["input_ids", "attention_mask"] snake_case__ = RobertaTokenizer def __init__( self : Tuple , __SCREAMING_SNAKE_CASE : Dict=None , __SCREAMING_SNAKE_CASE : Optional[int]=None , __SCREAMING_SNAKE_CASE : Optional[int]=None , __SCREAMING_SNAKE_CASE : str="replace" , __SCREAMING_SNAKE_CASE : Optional[Any]="<s>" , __SCREAMING_SNAKE_CASE : Optional[int]="</s>" , __SCREAMING_SNAKE_CASE : List[Any]="</s>" , __SCREAMING_SNAKE_CASE : List[Any]="<s>" , __SCREAMING_SNAKE_CASE : int="<unk>" , __SCREAMING_SNAKE_CASE : Optional[int]="<pad>" , __SCREAMING_SNAKE_CASE : Optional[int]="<mask>" , __SCREAMING_SNAKE_CASE : Optional[int]=False , __SCREAMING_SNAKE_CASE : int=True , **__SCREAMING_SNAKE_CASE : str , ) -> Optional[Any]: super().__init__( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , errors=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) a_ : Optional[int] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , __SCREAMING_SNAKE_CASE ) != add_prefix_space: a_ : List[Any] = getattr(__SCREAMING_SNAKE_CASE , pre_tok_state.pop('''type''' ) ) a_ : str = add_prefix_space a_ : Dict = pre_tok_class(**__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = add_prefix_space a_ : str = '''post_processor''' a_ : Any = getattr(self.backend_tokenizer , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if tokenizer_component_instance: a_ : Dict = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: a_ : List[str] = tuple(state['''sep'''] ) if "cls" in state: a_ : List[str] = tuple(state['''cls'''] ) a_ : Any = False if state.get('''add_prefix_space''' , __SCREAMING_SNAKE_CASE ) != add_prefix_space: a_ : Union[str, Any] = add_prefix_space a_ : Optional[int] = True if state.get('''trim_offsets''' , __SCREAMING_SNAKE_CASE ) != trim_offsets: a_ : Optional[int] = trim_offsets a_ : List[Any] = True if changes_to_apply: a_ : Optional[Any] = getattr(__SCREAMING_SNAKE_CASE , state.pop('''type''' ) ) a_ : str = component_class(**__SCREAMING_SNAKE_CASE ) setattr(self.backend_tokenizer , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @property def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> str: if self._mask_token is None: if self.verbose: logger.error('''Using mask_token, but it is not set yet.''' ) return None return str(self._mask_token ) @mask_token.setter def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : int ) -> Tuple: a_ : Tuple = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else value a_ : List[str] = value def SCREAMING_SNAKE_CASE ( self : Optional[int] , *__SCREAMING_SNAKE_CASE : Any , **__SCREAMING_SNAKE_CASE : List[str] ) -> BatchEncoding: a_ : List[str] = kwargs.get('''is_split_into_words''' , __SCREAMING_SNAKE_CASE ) assert self.add_prefix_space or not is_split_into_words, ( f'You need to instantiate {self.__class__.__name__} with add_prefix_space=True ' "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , *__SCREAMING_SNAKE_CASE : Optional[Any] , **__SCREAMING_SNAKE_CASE : str ) -> BatchEncoding: a_ : str = kwargs.get('''is_split_into_words''' , __SCREAMING_SNAKE_CASE ) assert self.add_prefix_space or not is_split_into_words, ( f'You need to instantiate {self.__class__.__name__} with add_prefix_space=True ' "to use it with pretokenized inputs." ) return super()._encode_plus(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : int , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Optional[str] = None ) -> Tuple[str]: a_ : str = self._tokenizer.model.save(__SCREAMING_SNAKE_CASE , name=__SCREAMING_SNAKE_CASE ) return tuple(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : str=None ) -> List[Any]: a_ : Any = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None ) -> List[int]: a_ : int = [self.sep_token_id] a_ : 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 + sep + token_ids_a + sep ) * [0]
702
'''simple docstring''' from unittest.mock import patch import pyspark from datasets.packaged_modules.spark.spark import ( Spark, SparkExamplesIterable, _generate_iterable_examples, ) from ..utils import ( require_dill_gt_0_3_2, require_not_windows, ) def _UpperCAmelCase ( __A : List[str] , __A : List[Any] ): a_ : Any = [] for part_id in partition_order: a_ : str = df.where(f'SPARK_PARTITION_ID() = {part_id}' ).collect() for row_idx, row in enumerate(__A ): expected_row_ids_and_row_dicts.append((f'{part_id}_{row_idx}', row.asDict()) ) return expected_row_ids_and_row_dicts @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : List[str] = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : Union[str, Any] = spark.range(1_00 ).repartition(1 ) a_ : Any = Spark(__A ) # The id ints will be converted to Pyarrow int64s, so each row will be 8 bytes. Setting a max_shard_size of 16 means # that each partition can hold 2 rows. spark_builder._repartition_df_if_needed(max_shard_size=16 ) # Given that the dataframe has 100 rows and each partition has 2 rows, we expect 50 partitions. assert spark_builder.df.rdd.getNumPartitions() == 50 @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : List[Any] = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : int = spark.range(10 ).repartition(2 ) a_ : Tuple = [1, 0] a_ : List[str] = _generate_iterable_examples(__A , __A ) # Reverse the partitions. a_ : int = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , __A ) for i, (row_id, row_dict) in enumerate(generate_fn() ): a_ , a_ : List[Any] = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : int = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : str = spark.range(10 ).repartition(1 ) a_ : Tuple = SparkExamplesIterable(__A ) assert it.n_shards == 1 for i, (row_id, row_dict) in enumerate(__A ): assert row_id == f'0_{i}' assert row_dict == {"id": i} @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : Tuple = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : str = spark.range(30 ).repartition(3 ) # Mock the generator so that shuffle reverses the partition indices. with patch('''numpy.random.Generator''' ) as generator_mock: a_ : Union[str, Any] = lambda __A : x.reverse() a_ : Any = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [2, 1, 0] ) a_ : str = SparkExamplesIterable(__A ).shuffle_data_sources(__A ) assert shuffled_it.n_shards == 3 for i, (row_id, row_dict) in enumerate(__A ): a_ , a_ : Optional[Any] = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : int = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : List[str] = spark.range(20 ).repartition(4 ) # Partitions 0 and 2 a_ : Dict = SparkExamplesIterable(__A ).shard_data_sources(worker_id=0 , num_workers=2 ) assert shard_it_a.n_shards == 2 a_ : Optional[Any] = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [0, 2] ) for i, (row_id, row_dict) in enumerate(__A ): a_ , a_ : Tuple = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict # Partitions 1 and 3 a_ : List[Any] = SparkExamplesIterable(__A ).shard_data_sources(worker_id=1 , num_workers=2 ) assert shard_it_a.n_shards == 2 a_ : Optional[int] = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [1, 3] ) for i, (row_id, row_dict) in enumerate(__A ): a_ , a_ : Any = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : Any = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : List[Any] = spark.range(1_00 ).repartition(1 ) a_ : Optional[Any] = Spark(__A ) # Choose a small max_shard_size for maximum partitioning. spark_builder._repartition_df_if_needed(max_shard_size=1 ) # The new number of partitions should not be greater than the number of rows. assert spark_builder.df.rdd.getNumPartitions() == 1_00
666
0
'''simple docstring''' import gc import unittest import numpy as np import torch from diffusers import ( AudioDiffusionPipeline, AutoencoderKL, DDIMScheduler, DDPMScheduler, DiffusionPipeline, Mel, UNetaDConditionModel, UNetaDModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' def SCREAMING_SNAKE_CASE ( self : Dict ) -> List[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> List[Any]: torch.manual_seed(0 ) a_ : Tuple = UNetaDModel( sample_size=(32, 64) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''') , ) return model @property def SCREAMING_SNAKE_CASE ( self : int ) -> List[Any]: torch.manual_seed(0 ) a_ : Any = UNetaDConditionModel( sample_size=(64, 32) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''CrossAttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''CrossAttnUpBlock2D''') , cross_attention_dim=10 , ) return model @property def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: torch.manual_seed(0 ) a_ : List[Any] = AutoencoderKL( sample_size=(128, 64) , in_channels=1 , out_channels=1 , latent_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''DownEncoderBlock2D''', '''DownEncoderBlock2D''') , up_block_types=('''UpDecoderBlock2D''', '''UpDecoderBlock2D''') , ) a_ : List[Any] = UNetaDModel( sample_size=(64, 32) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''') , ) return vqvae, unet @slow def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union[str, Any]: a_ : Dict = '''cpu''' # ensure determinism for the device-dependent torch.Generator a_ : Union[str, Any] = Mel( x_res=self.dummy_unet.config.sample_size[1] , y_res=self.dummy_unet.config.sample_size[0] , ) a_ : Any = DDPMScheduler() a_ : str = AudioDiffusionPipeline(vqvae=__SCREAMING_SNAKE_CASE , unet=self.dummy_unet , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : List[Any] = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : List[str] = pipe(generator=__SCREAMING_SNAKE_CASE , steps=4 ) a_ : List[Any] = output.audios[0] a_ : Dict = output.images[0] a_ : Dict = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : Optional[Any] = pipe(generator=__SCREAMING_SNAKE_CASE , steps=4 , return_dict=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output[0][0] assert audio.shape == (1, (self.dummy_unet.config.sample_size[1] - 1) * mel.hop_length) assert ( image.height == self.dummy_unet.config.sample_size[0] and image.width == self.dummy_unet.config.sample_size[1] ) a_ : Dict = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : str = np.frombuffer(image_from_tuple.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([69, 255, 255, 255, 0, 0, 77, 181, 12, 127] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() == 0 a_ : str = Mel( x_res=self.dummy_vqvae_and_unet[0].config.sample_size[1] , y_res=self.dummy_vqvae_and_unet[0].config.sample_size[0] , ) a_ : int = DDIMScheduler() a_ : Dict = self.dummy_vqvae_and_unet a_ : List[str] = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0] , unet=dummy_vqvae_and_unet[1] , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : Any = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) np.random.seed(0 ) a_ : List[str] = np.random.uniform(-1 , 1 , ((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,) ) a_ : int = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : int = pipe(raw_audio=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , start_step=5 , steps=10 ) a_ : List[str] = output.images[0] assert ( image.height == self.dummy_vqvae_and_unet[0].config.sample_size[0] and image.width == self.dummy_vqvae_and_unet[0].config.sample_size[1] ) a_ : Optional[Any] = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([120, 117, 110, 109, 138, 167, 138, 148, 132, 121] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 a_ : List[str] = self.dummy_unet_condition a_ : Dict = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0] , unet=__SCREAMING_SNAKE_CASE , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : int = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) np.random.seed(0 ) a_ : Any = torch.rand((1, 1, 10) ) a_ : Tuple = pipe(generator=__SCREAMING_SNAKE_CASE , encoding=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output.images[0] a_ : Dict = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([107, 103, 120, 127, 142, 122, 113, 122, 97, 111] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 @slow @require_torch_gpu class SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Any: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Any: a_ : Any = torch_device a_ : Optional[int] = DiffusionPipeline.from_pretrained('''teticio/audio-diffusion-ddim-256''' ) a_ : Dict = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : str = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : List[str] = pipe(generator=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output.audios[0] a_ : Tuple = output.images[0] assert audio.shape == (1, (pipe.unet.config.sample_size[1] - 1) * pipe.mel.hop_length) assert image.height == pipe.unet.config.sample_size[0] and image.width == pipe.unet.config.sample_size[1] a_ : str = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : Tuple = np.array([151, 167, 154, 144, 122, 134, 121, 105, 70, 26] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0
703
'''simple docstring''' from collections import OrderedDict from typing import TYPE_CHECKING, Any, List, Mapping, Optional from packaging import version if TYPE_CHECKING: from ... import PreTrainedTokenizer, TensorType from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import is_torch_available, logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = { 'bigscience/bloom': 'https://huggingface.co/bigscience/bloom/resolve/main/config.json', 'bigscience/bloom-560m': 'https://huggingface.co/bigscience/bloom-560m/blob/main/config.json', 'bigscience/bloom-1b1': 'https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json', 'bigscience/bloom-1b7': 'https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json', 'bigscience/bloom-3b': 'https://huggingface.co/bigscience/bloom-3b/blob/main/config.json', 'bigscience/bloom-7b1': 'https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json', } class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "bloom" snake_case__ = ["past_key_values"] snake_case__ = { "num_hidden_layers": "n_layer", "num_attention_heads": "n_head", } def __init__( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : int=25_0880 , __SCREAMING_SNAKE_CASE : Dict=64 , __SCREAMING_SNAKE_CASE : Tuple=2 , __SCREAMING_SNAKE_CASE : int=8 , __SCREAMING_SNAKE_CASE : Any=1e-5 , __SCREAMING_SNAKE_CASE : Optional[Any]=0.02 , __SCREAMING_SNAKE_CASE : Union[str, Any]=True , __SCREAMING_SNAKE_CASE : int=1 , __SCREAMING_SNAKE_CASE : Any=2 , __SCREAMING_SNAKE_CASE : Optional[Any]=False , __SCREAMING_SNAKE_CASE : Optional[Any]=0.0 , __SCREAMING_SNAKE_CASE : str=0.0 , __SCREAMING_SNAKE_CASE : List[Any]=1 , __SCREAMING_SNAKE_CASE : List[str]=False , **__SCREAMING_SNAKE_CASE : str , ) -> Any: a_ : Optional[int] = vocab_size # Backward compatibility with n_embed kwarg a_ : Any = kwargs.pop('''n_embed''' , __SCREAMING_SNAKE_CASE ) a_ : Optional[int] = hidden_size if n_embed is None else n_embed a_ : int = n_layer a_ : str = n_head a_ : Optional[int] = layer_norm_epsilon a_ : Dict = initializer_range a_ : List[str] = use_cache a_ : Dict = pretraining_tp a_ : Optional[Any] = apply_residual_connection_post_layernorm a_ : Optional[Any] = hidden_dropout a_ : List[str] = attention_dropout a_ : Dict = bos_token_id a_ : Optional[int] = eos_token_id a_ : Any = slow_but_exact super().__init__(bos_token_id=__SCREAMING_SNAKE_CASE , eos_token_id=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = version.parse("1.12" ) def __init__( self : Tuple , __SCREAMING_SNAKE_CASE : PretrainedConfig , __SCREAMING_SNAKE_CASE : str = "default" , __SCREAMING_SNAKE_CASE : List[PatchingSpec] = None , __SCREAMING_SNAKE_CASE : bool = False , ) -> Optional[Any]: super().__init__(__SCREAMING_SNAKE_CASE , task=__SCREAMING_SNAKE_CASE , patching_specs=__SCREAMING_SNAKE_CASE , use_past=__SCREAMING_SNAKE_CASE ) if not getattr(self._config , '''pad_token_id''' , __SCREAMING_SNAKE_CASE ): # TODO: how to do that better? a_ : Tuple = 0 @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Mapping[str, Mapping[int, str]]: a_ : Optional[Any] = OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}} ) if self.use_past: # BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344 self.fill_with_past_key_values_(__SCREAMING_SNAKE_CASE , direction='''inputs''' , inverted_values_shape=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = {0: '''batch''', 1: '''past_sequence + sequence'''} else: a_ : Union[str, Any] = {0: '''batch''', 1: '''sequence'''} return common_inputs @property def SCREAMING_SNAKE_CASE ( self : Any ) -> int: return self._config.n_layer @property def SCREAMING_SNAKE_CASE ( self : int ) -> int: return self._config.n_head @property def SCREAMING_SNAKE_CASE ( self : int ) -> float: return 1e-3 def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : "PreTrainedTokenizer" , __SCREAMING_SNAKE_CASE : int = -1 , __SCREAMING_SNAKE_CASE : int = -1 , __SCREAMING_SNAKE_CASE : bool = False , __SCREAMING_SNAKE_CASE : Optional["TensorType"] = None , ) -> Mapping[str, Any]: a_ : Dict = super(__SCREAMING_SNAKE_CASE , self ).generate_dummy_inputs( __SCREAMING_SNAKE_CASE , batch_size=__SCREAMING_SNAKE_CASE , seq_length=__SCREAMING_SNAKE_CASE , is_pair=__SCREAMING_SNAKE_CASE , framework=__SCREAMING_SNAKE_CASE ) # We need to order the input in the way they appears in the forward() a_ : Union[str, Any] = OrderedDict({'''input_ids''': common_inputs['''input_ids''']} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch a_ , a_ : Any = common_inputs['''input_ids'''].shape # Not using the same length for past_key_values a_ : str = seqlen + 2 a_ : Any = self._config.hidden_size // self.num_attention_heads a_ : Optional[int] = ( batch * self.num_attention_heads, head_dim, past_key_values_length, ) a_ : Any = ( batch * self.num_attention_heads, past_key_values_length, head_dim, ) a_ : List[str] = [ (torch.zeros(__SCREAMING_SNAKE_CASE ), torch.zeros(__SCREAMING_SNAKE_CASE )) for _ in range(self.num_layers ) ] a_ : Union[str, Any] = common_inputs['''attention_mask'''] if self.use_past: a_ : Optional[int] = ordered_inputs['''attention_mask'''].dtype a_ : List[Any] = torch.cat( [ordered_inputs['''attention_mask'''], torch.ones(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , dtype=__SCREAMING_SNAKE_CASE )] , dim=1 ) return ordered_inputs @property def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> int: return 13
666
0
'''simple docstring''' import pytest from datasets import inspect_metric, list_metrics, load_metric @pytest.fixture def _UpperCAmelCase ( __A : Dict ): monkeypatch.setattr('''datasets.utils.deprecation_utils._emitted_deprecation_warnings''' , set() ) @pytest.fixture def _UpperCAmelCase ( __A : Optional[int] ): class SCREAMING_SNAKE_CASE : def __init__( self : Optional[Any] , __SCREAMING_SNAKE_CASE : List[str] ) -> Dict: a_ : List[Any] = metric_id class SCREAMING_SNAKE_CASE : snake_case__ = [MetricMock(SCREAMING_SNAKE_CASE_ ) for metric_id in ["accuracy", "mse", "precision", "codeparrot/apps_metric"]] def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Tuple: return self._metrics monkeypatch.setattr('''datasets.inspect.huggingface_hub''' , HfhMock() ) @pytest.mark.parametrize( '''func, args''' , [(load_metric, ('''metrics/mse''',)), (list_metrics, ()), (inspect_metric, ('''metrics/mse''', '''tmp_path'''))] ) def _UpperCAmelCase ( __A : Any , __A : str , __A : Optional[Any] , __A : Dict , __A : Dict ): if "tmp_path" in args: a_ : int = tuple(arg if arg != '''tmp_path''' else tmp_path for arg in args ) with pytest.warns(__A , match='''https://huggingface.co/docs/evaluate''' ): func(*__A )
704
'''simple docstring''' import sys __lowerCAmelCase = ( '73167176531330624919225119674426574742355349194934' '96983520312774506326239578318016984801869478851843' '85861560789112949495459501737958331952853208805511' '12540698747158523863050715693290963295227443043557' '66896648950445244523161731856403098711121722383113' '62229893423380308135336276614282806444486645238749' '30358907296290491560440772390713810515859307960866' '70172427121883998797908792274921901699720888093776' '65727333001053367881220235421809751254540594752243' '52584907711670556013604839586446706324415722155397' '53697817977846174064955149290862569321978468622482' '83972241375657056057490261407972968652414535100474' '82166370484403199890008895243450658541227588666881' '16427171479924442928230863465674813919123162824586' '17866458359124566529476545682848912883142607690042' '24219022671055626321111109370544217506941658960408' '07198403850962455444362981230987879927244284909188' '84580156166097919133875499200524063689912560717606' '05886116467109405077541002256983155200055935729725' '71636269561882670428252483600823257530420752963450' ) def _UpperCAmelCase ( __A : str ): a_ : Tuple = 1 for digit in s: product *= int(__A ) return product def _UpperCAmelCase ( __A : str = N ): a_ : Dict = -sys.maxsize - 1 a_ : Optional[int] = n[:13] a_ : str = 13 while cur_index < len(__A ) - 13: if int(n[cur_index] ) >= int(substr[0] ): a_ : Tuple = substr[1:] + n[cur_index] cur_index += 1 else: a_ : Dict = max(__A , str_eval(__A ) ) a_ : List[str] = n[cur_index : cur_index + 13] cur_index += 13 return largest_product if __name__ == "__main__": print(F"""{solution() = }""")
666
0
'''simple docstring''' import inspect import tempfile from collections import OrderedDict, UserDict from collections.abc import MutableMapping from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from typing import Any, ContextManager, List, Tuple import numpy as np from .import_utils import is_flax_available, is_tf_available, is_torch_available, is_torch_fx_proxy if is_flax_available(): import jax.numpy as jnp class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): def __get__( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Dict=None ) -> Dict: # See docs.python.org/3/howto/descriptor.html#properties if obj is None: return self if self.fget is None: raise AttributeError('''unreadable attribute''' ) a_ : str = '''__cached_''' + self.fget.__name__ a_ : Optional[Any] = getattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if cached is None: a_ : List[Any] = self.fget(__SCREAMING_SNAKE_CASE ) setattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) return cached def _UpperCAmelCase ( __A : Any ): a_ : Union[str, Any] = val.lower() if val in {"y", "yes", "t", "true", "on", "1"}: return 1 if val in {"n", "no", "f", "false", "off", "0"}: return 0 raise ValueError(f'invalid truth value {val!r}' ) def _UpperCAmelCase ( __A : List[Any] ): if is_torch_fx_proxy(__A ): return True if is_torch_available(): import torch if isinstance(__A , torch.Tensor ): return True if is_tf_available(): import tensorflow as tf if isinstance(__A , tf.Tensor ): return True if is_flax_available(): import jax.numpy as jnp from jax.core import Tracer if isinstance(__A , (jnp.ndarray, Tracer) ): return True return isinstance(__A , np.ndarray ) def _UpperCAmelCase ( __A : Any ): return isinstance(__A , np.ndarray ) def _UpperCAmelCase ( __A : Tuple ): return _is_numpy(__A ) def _UpperCAmelCase ( __A : Tuple ): import torch return isinstance(__A , torch.Tensor ) def _UpperCAmelCase ( __A : Tuple ): return False if not is_torch_available() else _is_torch(__A ) def _UpperCAmelCase ( __A : List[Any] ): import torch return isinstance(__A , torch.device ) def _UpperCAmelCase ( __A : Optional[int] ): return False if not is_torch_available() else _is_torch_device(__A ) def _UpperCAmelCase ( __A : int ): import torch if isinstance(__A , __A ): if hasattr(__A , __A ): a_ : Any = getattr(__A , __A ) else: return False return isinstance(__A , torch.dtype ) def _UpperCAmelCase ( __A : str ): return False if not is_torch_available() else _is_torch_dtype(__A ) def _UpperCAmelCase ( __A : List[Any] ): import tensorflow as tf return isinstance(__A , tf.Tensor ) def _UpperCAmelCase ( __A : Optional[Any] ): return False if not is_tf_available() else _is_tensorflow(__A ) def _UpperCAmelCase ( __A : int ): import tensorflow as tf # the `is_symbolic_tensor` predicate is only available starting with TF 2.14 if hasattr(__A , '''is_symbolic_tensor''' ): return tf.is_symbolic_tensor(__A ) return type(__A ) == tf.Tensor def _UpperCAmelCase ( __A : Dict ): return False if not is_tf_available() else _is_tf_symbolic_tensor(__A ) def _UpperCAmelCase ( __A : Optional[Any] ): import jax.numpy as jnp # noqa: F811 return isinstance(__A , jnp.ndarray ) def _UpperCAmelCase ( __A : Optional[int] ): return False if not is_flax_available() else _is_jax(__A ) def _UpperCAmelCase ( __A : List[str] ): if isinstance(__A , (dict, UserDict) ): return {k: to_py_obj(__A ) for k, v in obj.items()} elif isinstance(__A , (list, tuple) ): return [to_py_obj(__A ) for o in obj] elif is_tf_tensor(__A ): return obj.numpy().tolist() elif is_torch_tensor(__A ): return obj.detach().cpu().tolist() elif is_jax_tensor(__A ): return np.asarray(__A ).tolist() elif isinstance(__A , (np.ndarray, np.number) ): # tolist also works on 0d np arrays return obj.tolist() else: return obj def _UpperCAmelCase ( __A : Dict ): if isinstance(__A , (dict, UserDict) ): return {k: to_numpy(__A ) for k, v in obj.items()} elif isinstance(__A , (list, tuple) ): return np.array(__A ) elif is_tf_tensor(__A ): return obj.numpy() elif is_torch_tensor(__A ): return obj.detach().cpu().numpy() elif is_jax_tensor(__A ): return np.asarray(__A ) else: return obj class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Union[str, Any]: a_ : Dict = fields(self ) # Safety and consistency checks if not len(__SCREAMING_SNAKE_CASE ): raise ValueError(f'{self.__class__.__name__} has no fields.' ) if not all(field.default is None for field in class_fields[1:] ): raise ValueError(f'{self.__class__.__name__} should not have more than one required field.' ) a_ : List[str] = getattr(self , class_fields[0].name ) a_ : Union[str, Any] = all(getattr(self , field.name ) is None for field in class_fields[1:] ) if other_fields_are_none and not is_tensor(__SCREAMING_SNAKE_CASE ): if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): a_ : Any = first_field.items() a_ : int = True else: try: a_ : str = iter(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = True except TypeError: a_ : Optional[Any] = False # if we provided an iterator as first field and the iterator is a (key, value) iterator # set the associated fields if first_field_iterator: for idx, element in enumerate(__SCREAMING_SNAKE_CASE ): if ( not isinstance(__SCREAMING_SNAKE_CASE , (list, tuple) ) or not len(__SCREAMING_SNAKE_CASE ) == 2 or not isinstance(element[0] , __SCREAMING_SNAKE_CASE ) ): if idx == 0: # If we do not have an iterator of key/values, set it as attribute a_ : List[Any] = first_field else: # If we have a mixed iterator, raise an error raise ValueError( f'Cannot set key/value for {element}. It needs to be a tuple (key, value).' ) break setattr(self , element[0] , element[1] ) if element[1] is not None: a_ : Optional[int] = element[1] elif first_field is not None: a_ : Any = first_field else: for field in class_fields: a_ : Optional[int] = getattr(self , field.name ) if v is not None: a_ : Any = v def __delitem__( self : Any , *__SCREAMING_SNAKE_CASE : Optional[int] , **__SCREAMING_SNAKE_CASE : Union[str, Any] ) -> List[str]: raise Exception(f'You cannot use ``__delitem__`` on a {self.__class__.__name__} instance.' ) def SCREAMING_SNAKE_CASE ( self : List[Any] , *__SCREAMING_SNAKE_CASE : Optional[int] , **__SCREAMING_SNAKE_CASE : Any ) -> Optional[Any]: raise Exception(f'You cannot use ``setdefault`` on a {self.__class__.__name__} instance.' ) def SCREAMING_SNAKE_CASE ( self : List[str] , *__SCREAMING_SNAKE_CASE : Any , **__SCREAMING_SNAKE_CASE : List[Any] ) -> List[Any]: raise Exception(f'You cannot use ``pop`` on a {self.__class__.__name__} instance.' ) def SCREAMING_SNAKE_CASE ( self : Any , *__SCREAMING_SNAKE_CASE : Optional[int] , **__SCREAMING_SNAKE_CASE : Tuple ) -> Optional[int]: raise Exception(f'You cannot use ``update`` on a {self.__class__.__name__} instance.' ) def __getitem__( self : Tuple , __SCREAMING_SNAKE_CASE : Dict ) -> Optional[int]: if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): a_ : Optional[int] = dict(self.items() ) return inner_dict[k] else: return self.to_tuple()[k] def __setattr__( self : Optional[int] , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Tuple ) -> Optional[Any]: if name in self.keys() and value is not None: # Don't call self.__setitem__ to avoid recursion errors super().__setitem__(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) super().__setattr__(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def __setitem__( self : Any , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : Any ) -> str: # Will raise a KeyException if needed super().__setitem__(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Don't call self.__setattr__ to avoid recursion errors super().__setattr__(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Tuple[Any]: return tuple(self[k] for k in self.keys() ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): @classmethod def SCREAMING_SNAKE_CASE ( cls : Optional[int] , __SCREAMING_SNAKE_CASE : Dict ) -> Any: raise ValueError( f'{value} is not a valid {cls.__name__}, please select one of {list(cls._valueamember_map_.keys() )}' ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "longest" snake_case__ = "max_length" snake_case__ = "do_not_pad" class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "pt" snake_case__ = "tf" snake_case__ = "np" snake_case__ = "jax" class SCREAMING_SNAKE_CASE : def __init__( self : List[Any] , __SCREAMING_SNAKE_CASE : List[ContextManager] ) -> Any: a_ : List[Any] = context_managers a_ : str = ExitStack() def __enter__( self : str ) -> int: for context_manager in self.context_managers: self.stack.enter_context(__SCREAMING_SNAKE_CASE ) def __exit__( self : Union[str, Any] , *__SCREAMING_SNAKE_CASE : Tuple , **__SCREAMING_SNAKE_CASE : Tuple ) -> Any: self.stack.__exit__(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def _UpperCAmelCase ( __A : Optional[int] ): a_ : int = infer_framework(__A ) if framework == "tf": a_ : Optional[Any] = inspect.signature(model_class.call ) # TensorFlow models elif framework == "pt": a_ : List[str] = inspect.signature(model_class.forward ) # PyTorch models else: a_ : str = inspect.signature(model_class.__call__ ) # Flax models for p in signature.parameters: if p == "return_loss" and signature.parameters[p].default is True: return True return False def _UpperCAmelCase ( __A : Optional[int] ): a_ : List[str] = model_class.__name__ a_ : Optional[int] = infer_framework(__A ) if framework == "tf": a_ : List[Any] = inspect.signature(model_class.call ) # TensorFlow models elif framework == "pt": a_ : int = inspect.signature(model_class.forward ) # PyTorch models else: a_ : List[Any] = inspect.signature(model_class.__call__ ) # Flax models if "QuestionAnswering" in model_name: return [p for p in signature.parameters if "label" in p or p in ("start_positions", "end_positions")] else: return [p for p in signature.parameters if "label" in p] def _UpperCAmelCase ( __A : MutableMapping , __A : str = "" , __A : str = "." ): def _flatten_dict(__A : List[str] , __A : List[Any]="" , __A : List[str]="." ): for k, v in d.items(): a_ : Optional[Any] = str(__A ) + delimiter + str(__A ) if parent_key else k if v and isinstance(__A , __A ): yield from flatten_dict(__A , __A , delimiter=__A ).items() else: yield key, v return dict(_flatten_dict(__A , __A , __A ) ) @contextmanager def _UpperCAmelCase ( __A : Dict , __A : bool = False ): if use_temp_dir: with tempfile.TemporaryDirectory() as tmp_dir: yield tmp_dir else: yield working_dir def _UpperCAmelCase ( __A : Union[str, Any] , __A : Tuple=None ): if is_numpy_array(__A ): return np.transpose(__A , axes=__A ) elif is_torch_tensor(__A ): return array.T if axes is None else array.permute(*__A ) elif is_tf_tensor(__A ): import tensorflow as tf return tf.transpose(__A , perm=__A ) elif is_jax_tensor(__A ): return jnp.transpose(__A , axes=__A ) else: raise ValueError(f'Type not supported for transpose: {type(__A )}.' ) def _UpperCAmelCase ( __A : List[Any] , __A : Optional[Any] ): if is_numpy_array(__A ): return np.reshape(__A , __A ) elif is_torch_tensor(__A ): return array.reshape(*__A ) elif is_tf_tensor(__A ): import tensorflow as tf return tf.reshape(__A , __A ) elif is_jax_tensor(__A ): return jnp.reshape(__A , __A ) else: raise ValueError(f'Type not supported for reshape: {type(__A )}.' ) def _UpperCAmelCase ( __A : Optional[Any] , __A : Union[str, Any]=None ): if is_numpy_array(__A ): return np.squeeze(__A , axis=__A ) elif is_torch_tensor(__A ): return array.squeeze() if axis is None else array.squeeze(dim=__A ) elif is_tf_tensor(__A ): import tensorflow as tf return tf.squeeze(__A , axis=__A ) elif is_jax_tensor(__A ): return jnp.squeeze(__A , axis=__A ) else: raise ValueError(f'Type not supported for squeeze: {type(__A )}.' ) def _UpperCAmelCase ( __A : List[Any] , __A : List[str] ): if is_numpy_array(__A ): return np.expand_dims(__A , __A ) elif is_torch_tensor(__A ): return array.unsqueeze(dim=__A ) elif is_tf_tensor(__A ): import tensorflow as tf return tf.expand_dims(__A , axis=__A ) elif is_jax_tensor(__A ): return jnp.expand_dims(__A , axis=__A ) else: raise ValueError(f'Type not supported for expand_dims: {type(__A )}.' ) def _UpperCAmelCase ( __A : Tuple ): if is_numpy_array(__A ): return np.size(__A ) elif is_torch_tensor(__A ): return array.numel() elif is_tf_tensor(__A ): import tensorflow as tf return tf.size(__A ) elif is_jax_tensor(__A ): return array.size else: raise ValueError(f'Type not supported for expand_dims: {type(__A )}.' ) def _UpperCAmelCase ( __A : Tuple , __A : str ): for key, value in auto_map.items(): if isinstance(__A , (tuple, list) ): a_ : Union[str, Any] = [f'{repo_id}--{v}' if (v is not None and '''--''' not in v) else v for v in value] elif value is not None and "--" not in value: a_ : Any = f'{repo_id}--{value}' return auto_map def _UpperCAmelCase ( __A : Tuple ): for base_class in inspect.getmro(__A ): a_ : Union[str, Any] = base_class.__module__ a_ : List[Any] = base_class.__name__ if module.startswith('''tensorflow''' ) or module.startswith('''keras''' ) or name == "TFPreTrainedModel": return "tf" elif module.startswith('''torch''' ) or name == "PreTrainedModel": return "pt" elif module.startswith('''flax''' ) or module.startswith('''jax''' ) or name == "FlaxPreTrainedModel": return "flax" else: raise TypeError(f'Could not infer framework from class {model_class}.' )
705
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( __A : list[int] ): a_ : int = len(__A ) // 2 # choose the middle 3 elements a_ : Dict = lst[m - 1 : m + 2] # if middle element is peak if three[1] > three[0] and three[1] > three[2]: return three[1] # if increasing, recurse on right elif three[0] < three[2]: if len(lst[:m] ) == 2: m -= 1 return peak(lst[m:] ) # decreasing else: if len(lst[:m] ) == 2: m += 1 return peak(lst[:m] ) if __name__ == "__main__": import doctest doctest.testmod()
666
0
'''simple docstring''' from math import pi, sqrt, tan def _UpperCAmelCase ( __A : float ): if side_length < 0: raise ValueError('''surface_area_cube() only accepts non-negative values''' ) return 6 * side_length**2 def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if length < 0 or breadth < 0 or height < 0: raise ValueError('''surface_area_cuboid() only accepts non-negative values''' ) return 2 * ((length * breadth) + (breadth * height) + (length * height)) def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''surface_area_sphere() only accepts non-negative values''' ) return 4 * pi * radius**2 def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''surface_area_hemisphere() only accepts non-negative values''' ) return 3 * pi * radius**2 def _UpperCAmelCase ( __A : float , __A : float ): if radius < 0 or height < 0: raise ValueError('''surface_area_cone() only accepts non-negative values''' ) return pi * radius * (radius + (height**2 + radius**2) ** 0.5) def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if radius_a < 0 or radius_a < 0 or height < 0: raise ValueError( '''surface_area_conical_frustum() only accepts non-negative values''' ) a_ : Any = (height**2 + (radius_a - radius_a) ** 2) ** 0.5 return pi * ((slant_height * (radius_a + radius_a)) + radius_a**2 + radius_a**2) def _UpperCAmelCase ( __A : float , __A : float ): if radius < 0 or height < 0: raise ValueError('''surface_area_cylinder() only accepts non-negative values''' ) return 2 * pi * radius * (height + radius) def _UpperCAmelCase ( __A : float , __A : float ): if torus_radius < 0 or tube_radius < 0: raise ValueError('''surface_area_torus() only accepts non-negative values''' ) if torus_radius < tube_radius: raise ValueError( '''surface_area_torus() does not support spindle or self intersecting tori''' ) return 4 * pow(__A , 2 ) * torus_radius * tube_radius def _UpperCAmelCase ( __A : float , __A : float ): if length < 0 or width < 0: raise ValueError('''area_rectangle() only accepts non-negative values''' ) return length * width def _UpperCAmelCase ( __A : float ): if side_length < 0: raise ValueError('''area_square() only accepts non-negative values''' ) return side_length**2 def _UpperCAmelCase ( __A : float , __A : float ): if base < 0 or height < 0: raise ValueError('''area_triangle() only accepts non-negative values''' ) return (base * height) / 2 def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if sidea < 0 or sidea < 0 or sidea < 0: raise ValueError('''area_triangle_three_sides() only accepts non-negative values''' ) elif sidea + sidea < sidea or sidea + sidea < sidea or sidea + sidea < sidea: raise ValueError('''Given three sides do not form a triangle''' ) a_ : int = (sidea + sidea + sidea) / 2 a_ : Optional[Any] = sqrt( semi_perimeter * (semi_perimeter - sidea) * (semi_perimeter - sidea) * (semi_perimeter - sidea) ) return area def _UpperCAmelCase ( __A : float , __A : float ): if base < 0 or height < 0: raise ValueError('''area_parallelogram() only accepts non-negative values''' ) return base * height def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if basea < 0 or basea < 0 or height < 0: raise ValueError('''area_trapezium() only accepts non-negative values''' ) return 1 / 2 * (basea + basea) * height def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''area_circle() only accepts non-negative values''' ) return pi * radius**2 def _UpperCAmelCase ( __A : float , __A : float ): if radius_x < 0 or radius_y < 0: raise ValueError('''area_ellipse() only accepts non-negative values''' ) return pi * radius_x * radius_y def _UpperCAmelCase ( __A : float , __A : float ): if diagonal_a < 0 or diagonal_a < 0: raise ValueError('''area_rhombus() only accepts non-negative values''' ) return 1 / 2 * diagonal_a * diagonal_a def _UpperCAmelCase ( __A : int , __A : float ): if not isinstance(__A , __A ) or sides < 3: raise ValueError( '''area_reg_polygon() only accepts integers greater than or \ equal to three as number of sides''' ) elif length < 0: raise ValueError( '''area_reg_polygon() only accepts non-negative values as \ length of a side''' ) return (sides * length**2) / (4 * tan(pi / sides )) return (sides * length**2) / (4 * tan(pi / sides )) if __name__ == "__main__": import doctest doctest.testmod(verbose=True) # verbose so we can see methods missing tests print('''[DEMO] Areas of various geometric shapes: \n''') print(F"""Rectangle: {area_rectangle(10, 20) = }""") print(F"""Square: {area_square(10) = }""") print(F"""Triangle: {area_triangle(10, 10) = }""") print(F"""Triangle: {area_triangle_three_sides(5, 12, 13) = }""") print(F"""Parallelogram: {area_parallelogram(10, 20) = }""") print(F"""Rhombus: {area_rhombus(10, 20) = }""") print(F"""Trapezium: {area_trapezium(10, 20, 30) = }""") print(F"""Circle: {area_circle(20) = }""") print(F"""Ellipse: {area_ellipse(10, 20) = }""") print('''\nSurface Areas of various geometric shapes: \n''') print(F"""Cube: {surface_area_cube(20) = }""") print(F"""Cuboid: {surface_area_cuboid(10, 20, 30) = }""") print(F"""Sphere: {surface_area_sphere(20) = }""") print(F"""Hemisphere: {surface_area_hemisphere(20) = }""") print(F"""Cone: {surface_area_cone(10, 20) = }""") print(F"""Conical Frustum: {surface_area_conical_frustum(10, 20, 30) = }""") print(F"""Cylinder: {surface_area_cylinder(10, 20) = }""") print(F"""Torus: {surface_area_torus(20, 10) = }""") print(F"""Equilateral Triangle: {area_reg_polygon(3, 10) = }""") print(F"""Square: {area_reg_polygon(4, 10) = }""") print(F"""Reqular Pentagon: {area_reg_polygon(5, 10) = }""")
706
'''simple docstring''' 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 SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = LongformerTokenizer snake_case__ = True snake_case__ = LongformerTokenizerFast snake_case__ = True def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt a_ : Tuple = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] a_ : Optional[Any] = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) a_ : Union[str, Any] = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] a_ : Any = {'''unk_token''': '''<unk>'''} a_ : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) a_ : Optional[int] = 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(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : Any , **__SCREAMING_SNAKE_CASE : Any ) -> int: kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , **__SCREAMING_SNAKE_CASE : List[Any] ) -> List[str]: kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : List[Any] ) -> Any: a_ : Union[str, Any] = '''lower newer''' a_ : List[Any] = '''lower newer''' return input_text, output_text def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: a_ : Optional[Any] = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) a_ : List[str] = '''lower newer''' a_ : str = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] a_ : Optional[int] = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) # , add_prefix_space=True) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Dict = tokens + [tokenizer.unk_token] a_ : Any = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Optional[Any]: a_ : Union[str, Any] = self.get_tokenizer() self.assertListEqual(tokenizer.encode('''Hello world!''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) , [0, 3_1414, 232, 328, 2] ) self.assertListEqual( tokenizer.encode('''Hello world! cécé herlolip 418''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) , [0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2] , ) @slow def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[Any]: a_ : Dict = self.tokenizer_class.from_pretrained('''allenai/longformer-base-4096''' ) a_ : Tuple = tokenizer.encode('''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer.encode('''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.encode( '''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.encode( '''sequence builders''' , '''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : List[str] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def SCREAMING_SNAKE_CASE ( self : str ) -> int: a_ : str = self.get_tokenizer() a_ : int = '''Encode this sequence.''' a_ : List[str] = tokenizer.byte_encoder[''' '''.encode('''utf-8''' )[0]] # Testing encoder arguments a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) tokenizer.add_special_tokens({'''bos_token''': '''<s>'''} ) a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Testing spaces after special tokens a_ : Optional[Any] = '''<mask>''' tokenizer.add_special_tokens( {'''mask_token''': AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE )} ) # mask token has a left space a_ : Optional[int] = tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) a_ : List[Any] = '''Encode <mask> sequence''' a_ : List[str] = '''Encode <mask>sequence''' a_ : int = tokenizer.encode(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = encoded.index(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer.encode(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = encoded.index(__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Union[str, Any]: pass def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Any: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): a_ : Any = self.rust_tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) a_ : Any = self.tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) a_ : str = '''A, <mask> AllenNLP sentence.''' a_ : List[Any] = tokenizer_r.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer_p.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE ) # 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'''] ) , ) a_ : str = tokenizer_r.convert_ids_to_tokens(tokens_r['''input_ids'''] ) a_ : Tuple = 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, 250, 6, 5_0264, 3823, 487, 2_1992, 3645, 4, 2] ) self.assertSequenceEqual(tokens_r['''input_ids'''] , [0, 250, 6, 5_0264, 3823, 487, 2_1992, 3645, 4, 2] ) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>'''] ) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>'''] ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[Any]: for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): a_ : Any = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) a_ : str = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state['''add_prefix_space'''] , __SCREAMING_SNAKE_CASE ) self.assertEqual(post_processor_state['''add_prefix_space'''] , __SCREAMING_SNAKE_CASE ) self.assertEqual(post_processor_state['''trim_offsets'''] , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[int]: # 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})' ): a_ : Dict = '''hello''' # `hello` is a token in the vocabulary of `pretrained_name` a_ : Union[str, Any] = f'{text_of_1_token} {text_of_1_token}' a_ : Any = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ) + 1, len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Any = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ) + 1, len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : int = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ), len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Tuple = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ), len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Union[str, Any] = 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)), # ) a_ : str = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : int = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ) + 1, 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : int = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ), 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Optional[int] = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : int = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ), 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , )
666
0
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( __A : list[int] ): a_ : int = len(__A ) // 2 # choose the middle 3 elements a_ : Dict = lst[m - 1 : m + 2] # if middle element is peak if three[1] > three[0] and three[1] > three[2]: return three[1] # if increasing, recurse on right elif three[0] < three[2]: if len(lst[:m] ) == 2: m -= 1 return peak(lst[m:] ) # decreasing else: if len(lst[:m] ) == 2: m += 1 return peak(lst[:m] ) if __name__ == "__main__": import doctest doctest.testmod()
707
'''simple docstring''' import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = {'vocab_file': 'sentencepiece.bpe.model'} __lowerCAmelCase = { 'vocab_file': { 'moussaKam/mbarthez': 'https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model', 'moussaKam/barthez': 'https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model', 'moussaKam/barthez-orangesum-title': ( 'https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model' ), }, } __lowerCAmelCase = { 'moussaKam/mbarthez': 1_024, 'moussaKam/barthez': 1_024, 'moussaKam/barthez-orangesum-title': 1_024, } __lowerCAmelCase = '▁' class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = VOCAB_FILES_NAMES snake_case__ = PRETRAINED_VOCAB_FILES_MAP snake_case__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case__ = ["input_ids", "attention_mask"] def __init__( self : Dict , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Dict="<s>" , __SCREAMING_SNAKE_CASE : List[Any]="</s>" , __SCREAMING_SNAKE_CASE : List[str]="</s>" , __SCREAMING_SNAKE_CASE : List[str]="<s>" , __SCREAMING_SNAKE_CASE : Dict="<unk>" , __SCREAMING_SNAKE_CASE : int="<pad>" , __SCREAMING_SNAKE_CASE : Tuple="<mask>" , __SCREAMING_SNAKE_CASE : Optional[Dict[str, Any]] = None , **__SCREAMING_SNAKE_CASE : Optional[Any] , ) -> None: # Mask token behave like a normal word, i.e. include the space before it a_ : Tuple = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else mask_token a_ : List[str] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , sp_model_kwargs=self.sp_model_kwargs , **__SCREAMING_SNAKE_CASE , ) a_ : Tuple = vocab_file a_ : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(__SCREAMING_SNAKE_CASE ) ) a_ : Optional[Any] = {'''<s>''': 0, '''<pad>''': 1, '''</s>''': 2, '''<unk>''': 3} a_ : Any = len(self.sp_model ) - 1 a_ : Optional[Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None ) -> List[int]: if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] a_ : List[str] = [self.cls_token_id] a_ : Optional[Any] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None , __SCREAMING_SNAKE_CASE : bool = False ) -> List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__SCREAMING_SNAKE_CASE , token_ids_a=__SCREAMING_SNAKE_CASE , already_has_special_tokens=__SCREAMING_SNAKE_CASE ) if token_ids_a is None: return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1, 1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None ) -> List[int]: a_ : List[str] = [self.sep_token_id] a_ : Any = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def SCREAMING_SNAKE_CASE ( self : Any ) -> Union[str, Any]: return len(self.sp_model ) def SCREAMING_SNAKE_CASE ( self : str ) -> Tuple: a_ : int = {self.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def SCREAMING_SNAKE_CASE ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : str ) -> List[str]: return self.sp_model.encode(__SCREAMING_SNAKE_CASE , out_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : List[Any] ) -> Dict: if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] a_ : Optional[int] = self.sp_model.PieceToId(__SCREAMING_SNAKE_CASE ) return spm_id if spm_id else self.unk_token_id def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : int ) -> str: if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Optional[int]: a_ : Dict = [] a_ : List[Any] = '''''' a_ : Dict = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) + token a_ : Dict = True a_ : Optional[Any] = [] else: current_sub_tokens.append(__SCREAMING_SNAKE_CASE ) a_ : Tuple = False out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) return out_string.strip() def __getstate__( self : Dict ) -> int: a_ : Dict = self.__dict__.copy() a_ : List[str] = None return state def __setstate__( self : Dict , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Any: a_ : Optional[Any] = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): a_ : Union[str, Any] = {} a_ : int = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Optional[str] = None ) -> Tuple[str]: if not os.path.isdir(__SCREAMING_SNAKE_CASE ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return a_ : Union[str, Any] = os.path.join( __SCREAMING_SNAKE_CASE , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__SCREAMING_SNAKE_CASE ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __SCREAMING_SNAKE_CASE ) elif not os.path.isfile(self.vocab_file ): with open(__SCREAMING_SNAKE_CASE , '''wb''' ) as fi: a_ : Any = self.sp_model.serialized_model_proto() fi.write(__SCREAMING_SNAKE_CASE ) return (out_vocab_file,)
666
0
'''simple docstring''' import unittest import numpy as np import torch from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = DDIMPipeline snake_case__ = UNCONDITIONAL_IMAGE_GENERATION_PARAMS snake_case__ = PipelineTesterMixin.required_optional_params - { "num_images_per_prompt", "latents", "callback", "callback_steps", } snake_case__ = UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS snake_case__ = False def SCREAMING_SNAKE_CASE ( self : Any ) -> Optional[Any]: torch.manual_seed(0 ) a_ : 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''') , ) a_ : str = DDIMScheduler() a_ : Union[str, Any] = {'''unet''': unet, '''scheduler''': scheduler} return components def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Tuple=0 ) -> str: if str(__SCREAMING_SNAKE_CASE ).startswith('''mps''' ): a_ : Dict = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: a_ : Union[str, Any] = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = { '''batch_size''': 1, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union[str, Any]: a_ : Dict = '''cpu''' a_ : List[Any] = self.get_dummy_components() a_ : List[str] = self.pipeline_class(**__SCREAMING_SNAKE_CASE ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = self.get_dummy_inputs(__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = pipe(**__SCREAMING_SNAKE_CASE ).images a_ : List[str] = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 32, 32, 3) ) a_ : int = np.array( [1.000e00, 5.717e-01, 4.717e-01, 1.000e00, 0.000e00, 1.000e00, 3.000e-04, 0.000e00, 9.000e-04] ) a_ : Union[str, Any] = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(__SCREAMING_SNAKE_CASE , 1e-3 ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> str: super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : str ) -> Optional[int]: super().test_save_load_local(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : Any ) -> List[str]: super().test_save_load_optional_components(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : str ) -> Optional[int]: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : str ) -> Any: a_ : Optional[Any] = '''google/ddpm-cifar10-32''' a_ : Optional[Any] = UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Dict = DDIMScheduler() a_ : List[str] = DDIMPipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) ddim.to(__SCREAMING_SNAKE_CASE ) ddim.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = torch.manual_seed(0 ) a_ : Tuple = ddim(generator=__SCREAMING_SNAKE_CASE , eta=0.0 , output_type='''numpy''' ).images a_ : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) a_ : List[str] = np.array([0.1723, 0.1617, 0.1600, 0.1626, 0.1497, 0.1513, 0.1505, 0.1442, 0.1453] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> str: a_ : int = '''google/ddpm-ema-bedroom-256''' a_ : str = UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Tuple = DDIMScheduler.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = DDIMPipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) ddpm.to(__SCREAMING_SNAKE_CASE ) ddpm.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = torch.manual_seed(0 ) a_ : List[Any] = ddpm(generator=__SCREAMING_SNAKE_CASE , output_type='''numpy''' ).images a_ : Optional[int] = image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) a_ : Optional[Any] = np.array([0.0060, 0.0201, 0.0344, 0.0024, 0.0018, 0.0002, 0.0022, 0.0000, 0.0069] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
708
'''simple docstring''' def _UpperCAmelCase ( __A : str , __A : str ): def get_matched_characters(__A : str , __A : str ) -> str: a_ : Union[str, Any] = [] a_ : int = min(len(_stra ) , len(_stra ) ) // 2 for i, l in enumerate(_stra ): a_ : Any = int(max(0 , i - limit ) ) a_ : Union[str, Any] = int(min(i + limit + 1 , len(_stra ) ) ) if l in _stra[left:right]: matched.append(__A ) a_ : Any = f'{_stra[0:_stra.index(__A )]} {_stra[_stra.index(__A ) + 1:]}' return "".join(__A ) # matching characters a_ : Optional[Any] = get_matched_characters(__A , __A ) a_ : int = get_matched_characters(__A , __A ) a_ : Any = len(__A ) # transposition a_ : List[Any] = ( len([(ca, ca) for ca, ca in zip(__A , __A ) if ca != ca] ) // 2 ) if not match_count: a_ : Dict = 0.0 else: a_ : Optional[int] = ( 1 / 3 * ( match_count / len(__A ) + match_count / len(__A ) + (match_count - transpositions) / match_count ) ) # common prefix up to 4 characters a_ : List[str] = 0 for ca, ca in zip(stra[:4] , stra[:4] ): if ca == ca: prefix_len += 1 else: break return jaro + 0.1 * prefix_len * (1 - jaro) if __name__ == "__main__": import doctest doctest.testmod() print(jaro_winkler('hello', 'world'))
666
0
'''simple docstring''' from math import isqrt def _UpperCAmelCase ( __A : int ): a_ : Dict = [True] * max_number for i in range(2 , isqrt(max_number - 1 ) + 1 ): if is_prime[i]: for j in range(i**2 , __A , __A ): a_ : str = False return [i for i in range(2 , __A ) if is_prime[i]] def _UpperCAmelCase ( __A : int = 10**8 ): a_ : Tuple = calculate_prime_numbers(max_number // 2 ) a_ : Union[str, Any] = 0 a_ : Any = 0 a_ : Optional[Any] = len(__A ) - 1 while left <= right: while prime_numbers[left] * prime_numbers[right] >= max_number: right -= 1 semiprimes_count += right - left + 1 left += 1 return semiprimes_count if __name__ == "__main__": print(F"""{solution() = }""")
709
'''simple docstring''' import torch from transformers import AutoModel class SCREAMING_SNAKE_CASE ( torch.nn.Module ): def __init__( self : Optional[int] , __SCREAMING_SNAKE_CASE : int="sayef/fsner-bert-base-uncased" ) -> str: super(__SCREAMING_SNAKE_CASE , self ).__init__() a_ : str = AutoModel.from_pretrained(__SCREAMING_SNAKE_CASE , return_dict=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = torch.nn.CosineSimilarity(3 , 1e-08 ) a_ : Dict = torch.nn.Softmax(dim=1 ) def SCREAMING_SNAKE_CASE ( self : str , **__SCREAMING_SNAKE_CASE : int ) -> str: return self.bert(**__SCREAMING_SNAKE_CASE ).last_hidden_state def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : List[Any] ) -> Optional[int]: return token_embeddings.sum(2 , keepdim=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : int=1 ) -> Dict: return self.softmax(T * self.cos(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[int] ) -> Any: a_ : Dict = W_supports['''sizes'''].tolist() a_ : Tuple = W_supports['''start_token_id'''].item() a_ : List[Any] = W_supports['''end_token_id'''].item() del W_supports["sizes"] del W_supports["start_token_id"] del W_supports["end_token_id"] a_ : int = self.BERT(**__SCREAMING_SNAKE_CASE ) a_ : Any = self.BERT(**__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = None a_ : Tuple = None a_ : List[str] = W_supports['''input_ids'''] == start_token_id a_ : Dict = W_supports['''input_ids'''] == end_token_id for i, size in enumerate(__SCREAMING_SNAKE_CASE ): if i == 0: a_ : str = 0 else: a_ : str = support_sizes[i - 1] a_ : Union[str, Any] = S[s : s + size][start_token_masks[s : s + size]] a_ : Tuple = S[s : s + size][end_token_masks[s : s + size]] a_ : Tuple = torch.matmul(q[i] , s_start.T ).sum(1 ).softmax(0 ) a_ : Optional[Any] = torch.matmul(q[i] , s_end.T ).sum(1 ).softmax(0 ) if p_starts is not None: a_ : Any = torch.vstack((p_starts, p_start) ) a_ : Dict = torch.vstack((p_ends, p_end) ) else: a_ : Optional[int] = p_start a_ : List[Any] = p_end return p_starts, p_ends
666
0
'''simple docstring''' from typing import Any, Dict, List, Optional, Tuple, Union import torch from torch import nn from torch.utils.data import DistributedSampler, RandomSampler from transformers import PreTrainedModel, Trainer, logging from transformers.integrations import is_fairscale_available from transformers.models.fsmt.configuration_fsmt import FSMTConfig from transformers.optimization import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.trainer_pt_utils import get_tpu_sampler from transformers.training_args import ParallelMode from transformers.utils import is_torch_tpu_available if is_fairscale_available(): from fairscale.optim import OSS __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = { 'linear': get_linear_schedule_with_warmup, 'cosine': get_cosine_schedule_with_warmup, 'cosine_w_restarts': get_cosine_with_hard_restarts_schedule_with_warmup, 'polynomial': get_polynomial_decay_schedule_with_warmup, 'constant': get_constant_schedule, 'constant_w_warmup': get_constant_schedule_with_warmup, } class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): def __init__( self : Tuple , __SCREAMING_SNAKE_CASE : List[str]=None , __SCREAMING_SNAKE_CASE : Any=None , *__SCREAMING_SNAKE_CASE : int , **__SCREAMING_SNAKE_CASE : Any ) -> Optional[Any]: super().__init__(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if config is None: assert isinstance(self.model , __SCREAMING_SNAKE_CASE ), ( "If no `config` is passed the model to be trained has to be of type `PreTrainedModel`, but is" f' {self.model.__class__}' ) a_ : int = self.model.config else: a_ : Optional[int] = config a_ : Dict = data_args a_ : Optional[Any] = self.config.tgt_vocab_size if isinstance(self.config , __SCREAMING_SNAKE_CASE ) else self.config.vocab_size if self.args.label_smoothing != 0 or (self.data_args is not None and self.data_args.ignore_pad_token_for_loss): assert self.config.pad_token_id is not None, ( "Make sure that `config.pad_token_id` is correcly defined when ignoring `pad_token` for loss" " calculation or doing label smoothing." ) if self.config.pad_token_id is None and self.config.eos_token_id is not None: logger.warning( f'The `config.pad_token_id` is `None`. Using `config.eos_token_id` = {self.config.eos_token_id} for' ''' padding..''' ) if self.args.label_smoothing == 0: a_ : Optional[int] = torch.nn.CrossEntropyLoss(ignore_index=self.config.pad_token_id ) else: # dynamically import label_smoothed_nll_loss from utils import label_smoothed_nll_loss a_ : Dict = label_smoothed_nll_loss def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : int ) -> Union[str, Any]: if self.optimizer is None: a_ : Any = ['''bias''', '''LayerNorm.weight'''] a_ : Any = [ { '''params''': [p for n, p in self.model.named_parameters() if not any(nd in n for nd in no_decay )], '''weight_decay''': self.args.weight_decay, }, { '''params''': [p for n, p in self.model.named_parameters() if any(nd in n for nd in no_decay )], '''weight_decay''': 0.0, }, ] a_ : Tuple = Adafactor if self.args.adafactor else AdamW if self.args.adafactor: a_ : Union[str, Any] = Adafactor a_ : List[str] = {'''scale_parameter''': False, '''relative_step''': False} else: a_ : List[Any] = AdamW a_ : Optional[int] = { '''betas''': (self.args.adam_betaa, self.args.adam_betaa), '''eps''': self.args.adam_epsilon, } a_ : Any = self.args.learning_rate if self.sharded_ddp: a_ : Any = OSS( params=__SCREAMING_SNAKE_CASE , optim=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) else: a_ : int = optimizer_cls(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if self.lr_scheduler is None: a_ : Optional[int] = self._get_lr_scheduler(__SCREAMING_SNAKE_CASE ) else: # ignoring --lr_scheduler logger.warning('''scheduler is passed to `Seq2SeqTrainer`, `--lr_scheduler` arg is ignored.''' ) def SCREAMING_SNAKE_CASE ( self : List[Any] , __SCREAMING_SNAKE_CASE : int ) -> List[str]: a_ : str = arg_to_scheduler[self.args.lr_scheduler] if self.args.lr_scheduler == "constant": a_ : Tuple = schedule_func(self.optimizer ) elif self.args.lr_scheduler == "constant_w_warmup": a_ : Dict = schedule_func(self.optimizer , num_warmup_steps=self.args.warmup_steps ) else: a_ : Any = schedule_func( self.optimizer , num_warmup_steps=self.args.warmup_steps , num_training_steps=__SCREAMING_SNAKE_CASE ) return scheduler def SCREAMING_SNAKE_CASE ( self : Dict ) -> Optional[torch.utils.data.Sampler]: if isinstance(self.train_dataset , torch.utils.data.IterableDataset ): return None elif is_torch_tpu_available(): return get_tpu_sampler(self.train_dataset ) else: if self.args.sortish_sampler: self.train_dataset.make_sortish_sampler( self.args.per_device_train_batch_size , distributed=(self.args.parallel_mode == ParallelMode.DISTRIBUTED) , ) return ( RandomSampler(self.train_dataset ) if self.args.local_rank == -1 else DistributedSampler(self.train_dataset ) ) def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : List[Any] ) -> Dict: if self.args.label_smoothing == 0: if self.data_args is not None and self.data_args.ignore_pad_token_for_loss: # force training to ignore pad token a_ : Optional[Any] = model(**__SCREAMING_SNAKE_CASE , use_cache=__SCREAMING_SNAKE_CASE )[0] a_ : Optional[int] = self.loss_fn(logits.view(-1 , logits.shape[-1] ) , labels.view(-1 ) ) else: # compute usual loss via models a_ : List[Any] = model(**__SCREAMING_SNAKE_CASE , labels=__SCREAMING_SNAKE_CASE , use_cache=__SCREAMING_SNAKE_CASE )[:2] else: # compute label smoothed loss a_ : Optional[Any] = model(**__SCREAMING_SNAKE_CASE , use_cache=__SCREAMING_SNAKE_CASE )[0] a_ : Any = torch.nn.functional.log_softmax(__SCREAMING_SNAKE_CASE , dim=-1 ) a_ : Any = self.loss_fn(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , self.args.label_smoothing , ignore_index=self.config.pad_token_id ) return loss, logits def SCREAMING_SNAKE_CASE ( self : Any , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> int: a_ : Tuple = inputs.pop('''labels''' ) a_ : Union[str, Any] = self._compute_loss(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) return loss def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : nn.Module , __SCREAMING_SNAKE_CASE : Dict[str, Union[torch.Tensor, Any]] , __SCREAMING_SNAKE_CASE : bool , __SCREAMING_SNAKE_CASE : Optional[List[str]] = None , ) -> Tuple[Optional[float], Optional[torch.Tensor], Optional[torch.Tensor]]: a_ : Optional[int] = self._prepare_inputs(__SCREAMING_SNAKE_CASE ) a_ : List[Any] = { '''max_length''': self.data_args.val_max_target_length if self.data_args is not None else self.config.max_length, '''num_beams''': self.data_args.eval_beams if self.data_args is not None else self.config.num_beams, } if self.args.predict_with_generate and not self.args.prediction_loss_only: a_ : str = self.model.generate( inputs['''input_ids'''] , attention_mask=inputs['''attention_mask'''] , **__SCREAMING_SNAKE_CASE , ) # in case the batch is shorter than max length, the output should be padded if generated_tokens.shape[-1] < gen_kwargs["max_length"]: a_ : Dict = self._pad_tensors_to_max_len(__SCREAMING_SNAKE_CASE , gen_kwargs['''max_length'''] ) a_ : Tuple = inputs.pop('''labels''' ) with torch.no_grad(): # compute loss on predict data a_ : Union[str, Any] = self._compute_loss(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : str = loss.mean().detach() if self.args.prediction_loss_only: return (loss, None, None) a_ : Optional[int] = generated_tokens if self.args.predict_with_generate else logits if labels.shape[-1] < gen_kwargs["max_length"]: a_ : Union[str, Any] = self._pad_tensors_to_max_len(__SCREAMING_SNAKE_CASE , gen_kwargs['''max_length'''] ) return (loss, logits, labels) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> List[str]: # If PAD token is not defined at least EOS token has to be defined a_ : Optional[int] = self.config.pad_token_id if self.config.pad_token_id is not None else self.config.eos_token_id if pad_token_id is None: raise ValueError( '''Make sure that either `config.pad_token_id` or `config.eos_token_id` is defined if tensor has to be''' f' padded to `max_length`={max_length}' ) a_ : List[str] = pad_token_id * torch.ones( (tensor.shape[0], max_length) , dtype=tensor.dtype , device=tensor.device ) a_ : Any = tensor return padded_tensor
710
'''simple docstring''' import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import ClassLabel, Features, Image from .base import TaskTemplate @dataclass(frozen=SCREAMING_SNAKE_CASE_ ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = field(default="image-classification" , metadata={"include_in_asdict_even_if_is_default": True} ) snake_case__ = Features({"image": Image()} ) snake_case__ = Features({"labels": ClassLabel} ) snake_case__ = "image" snake_case__ = "labels" def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : List[Any] ) -> Any: if self.label_column not in features: raise ValueError(f'Column {self.label_column} is not present in features.' ) if not isinstance(features[self.label_column] , __SCREAMING_SNAKE_CASE ): raise ValueError(f'Column {self.label_column} is not a ClassLabel.' ) a_ : Optional[int] = copy.deepcopy(self ) a_ : int = self.label_schema.copy() a_ : Tuple = features[self.label_column] a_ : str = label_schema return task_template @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict[str, str]: return { self.image_column: "image", self.label_column: "labels", }
666
0
'''simple docstring''' from math import loga def _UpperCAmelCase ( __A : int ): if a < 0: raise ValueError('''Input value must be a positive integer''' ) elif isinstance(__A , __A ): raise TypeError('''Input value must be a \'int\' type''' ) return 0 if (a == 0) else int(loga(a & -a ) ) if __name__ == "__main__": import doctest doctest.testmod()
711
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( __A : tuple[int, int] , __A : int ): a_ , a_ : List[str] = position a_ : Optional[int] = [ (y + 1, x + 2), (y - 1, x + 2), (y + 1, x - 2), (y - 1, x - 2), (y + 2, x + 1), (y + 2, x - 1), (y - 2, x + 1), (y - 2, x - 1), ] a_ : Any = [] for position in positions: a_ , a_ : Dict = position if 0 <= y_test < n and 0 <= x_test < n: permissible_positions.append(__A ) return permissible_positions def _UpperCAmelCase ( __A : list[list[int]] ): return not any(elem == 0 for row in board for elem in row ) def _UpperCAmelCase ( __A : list[list[int]] , __A : tuple[int, int] , __A : int ): if is_complete(__A ): return True for position in get_valid_pos(__A , len(__A ) ): a_ , a_ : Dict = position if board[y][x] == 0: a_ : Optional[Any] = curr + 1 if open_knight_tour_helper(__A , __A , curr + 1 ): return True a_ : Tuple = 0 return False def _UpperCAmelCase ( __A : int ): a_ : List[str] = [[0 for i in range(__A )] for j in range(__A )] for i in range(__A ): for j in range(__A ): a_ : Optional[Any] = 1 if open_knight_tour_helper(__A , (i, j) , 1 ): return board a_ : Union[str, Any] = 0 a_ : Dict = f'Open Kight Tour cannot be performed on a board of size {n}' raise ValueError(__A ) if __name__ == "__main__": import doctest doctest.testmod()
666
0
'''simple docstring''' from .glue import GlueDataset, GlueDataTrainingArguments from .language_modeling import ( LineByLineTextDataset, LineByLineWithRefDataset, LineByLineWithSOPTextDataset, TextDataset, TextDatasetForNextSentencePrediction, ) from .squad import SquadDataset, SquadDataTrainingArguments
712
'''simple docstring''' import warnings warnings.warn( 'memory_utils has been reorganized to utils.memory. Import `find_executable_batchsize` from the main `__init__`: ' '`from accelerate import find_executable_batch_size` to avoid this warning.', FutureWarning, )
666
0
'''simple docstring''' import os import shutil import sys import tempfile import unittest from pathlib import Path import pytest import transformers from transformers import ( BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, AutoTokenizer, BertConfig, BertTokenizer, BertTokenizerFast, CTRLTokenizer, GPTaTokenizer, GPTaTokenizerFast, PreTrainedTokenizerFast, RobertaTokenizer, RobertaTokenizerFast, is_tokenizers_available, ) from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig from transformers.models.auto.tokenization_auto import ( TOKENIZER_MAPPING, get_tokenizer_config, tokenizer_class_from_name, ) from transformers.models.roberta.configuration_roberta import RobertaConfig from transformers.testing_utils import ( DUMMY_DIFF_TOKENIZER_IDENTIFIER, DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, RequestCounter, require_tokenizers, slow, ) sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils')) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_tokenization import CustomTokenizer # noqa E402 if is_tokenizers_available(): from test_module.custom_tokenization_fast import CustomTokenizerFast class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : str ) -> Tuple: a_ : Optional[Any] = 0 @slow def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: for model_name in (x for x in BERT_PRETRAINED_CONFIG_ARCHIVE_MAP.keys() if "japanese" not in x): a_ : Any = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , (BertTokenizer, BertTokenizerFast) ) self.assertGreater(len(__SCREAMING_SNAKE_CASE ) , 0 ) for model_name in GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP.keys(): a_ : Optional[int] = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , (GPTaTokenizer, GPTaTokenizerFast) ) self.assertGreater(len(__SCREAMING_SNAKE_CASE ) , 0 ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> str: a_ : Optional[Any] = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(tokenizer.vocab_size , 12 ) def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Union[str, Any]: a_ : List[Any] = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , (RobertaTokenizer, RobertaTokenizerFast) ) self.assertEqual(tokenizer.vocab_size , 20 ) def SCREAMING_SNAKE_CASE ( self : Any ) -> str: a_ : int = AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Check that tokenizer_type ≠ model_type a_ : Optional[Any] = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE , config=__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(tokenizer.vocab_size , 12 ) def SCREAMING_SNAKE_CASE ( self : int ) -> Any: with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.txt''' , os.path.join(__SCREAMING_SNAKE_CASE , '''vocab.txt''' ) ) a_ : Optional[int] = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE , tokenizer_type='''bert''' , use_fast=__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.json''' , os.path.join(__SCREAMING_SNAKE_CASE , '''vocab.json''' ) ) shutil.copy('''./tests/fixtures/merges.txt''' , os.path.join(__SCREAMING_SNAKE_CASE , '''merges.txt''' ) ) a_ : List[Any] = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE , tokenizer_type='''gpt2''' , use_fast=__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @require_tokenizers def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.txt''' , os.path.join(__SCREAMING_SNAKE_CASE , '''vocab.txt''' ) ) a_ : Union[str, Any] = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE , tokenizer_type='''bert''' ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.json''' , os.path.join(__SCREAMING_SNAKE_CASE , '''vocab.json''' ) ) shutil.copy('''./tests/fixtures/merges.txt''' , os.path.join(__SCREAMING_SNAKE_CASE , '''merges.txt''' ) ) a_ : int = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE , tokenizer_type='''gpt2''' ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> int: with pytest.raises(__SCREAMING_SNAKE_CASE ): AutoTokenizer.from_pretrained('''./''' , tokenizer_type='''xxx''' ) @require_tokenizers def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[Any]: for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]: a_ : List[str] = tokenizer_class.from_pretrained('''wietsedv/bert-base-dutch-cased''' ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , (BertTokenizer, BertTokenizerFast) ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertEqual(tokenizer.basic_tokenizer.do_lower_case , __SCREAMING_SNAKE_CASE ) else: self.assertEqual(tokenizer.do_lower_case , __SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.model_max_length , 512 ) @require_tokenizers def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> int: for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]: with self.assertRaisesRegex( __SCREAMING_SNAKE_CASE , '''julien-c/herlolip-not-exists is not a local folder and is not a valid model identifier''' , ): a_ : Tuple = tokenizer_class.from_pretrained('''julien-c/herlolip-not-exists''' ) def SCREAMING_SNAKE_CASE ( self : Any ) -> Optional[Any]: # tests: https://github.com/huggingface/transformers/pull/13251 # 1. models with `-`, e.g. xlm-roberta -> xlm_roberta # 2. models that don't remap 1-1 from model-name to model file, e.g., openai-gpt -> openai a_ : int = TOKENIZER_MAPPING.values() a_ : str = [] for slow_tok, fast_tok in tokenizers: if slow_tok is not None: tokenizer_names.append(slow_tok.__name__ ) if fast_tok is not None: tokenizer_names.append(fast_tok.__name__ ) for tokenizer_name in tokenizer_names: # must find the right class tokenizer_class_from_name(__SCREAMING_SNAKE_CASE ) @require_tokenizers def SCREAMING_SNAKE_CASE ( self : str ) -> int: self.assertIsInstance(AutoTokenizer.from_pretrained('''bert-base-cased''' , use_fast=__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) self.assertIsInstance(AutoTokenizer.from_pretrained('''bert-base-cased''' ) , __SCREAMING_SNAKE_CASE ) @require_tokenizers def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Union[str, Any]: a_ : Optional[int] = AutoTokenizer.from_pretrained('''distilbert-base-uncased''' , do_lower_case=__SCREAMING_SNAKE_CASE ) a_ : str = '''Hello, world. How are you?''' a_ : List[Any] = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) self.assertEqual('''[UNK]''' , tokens[0] ) a_ : List[str] = AutoTokenizer.from_pretrained('''microsoft/mpnet-base''' , do_lower_case=__SCREAMING_SNAKE_CASE ) a_ : List[Any] = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) self.assertEqual('''[UNK]''' , tokens[0] ) @require_tokenizers def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> List[str]: a_ : List[str] = AutoTokenizer.from_pretrained('''robot-test/dummy-tokenizer-fast-with-model-config''' ) self.assertEqual(type(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.model_max_length , 512 ) self.assertEqual(tokenizer.vocab_size , 3_0000 ) self.assertEqual(tokenizer.unk_token , '''[UNK]''' ) self.assertEqual(tokenizer.padding_side , '''right''' ) self.assertEqual(tokenizer.truncation_side , '''right''' ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> int: a_ : Optional[int] = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , (BertTokenizer, BertTokenizerFast) ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : List[Any] = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , tokenizer.__class__ ) self.assertEqual(tokenizera.vocab_size , 12 ) def SCREAMING_SNAKE_CASE ( self : int ) -> str: a_ : Dict = AutoTokenizer.from_pretrained('''ctrl''' ) # There is no fast CTRL so this always gives us a slow tokenizer. self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Optional[Any]: # Check we can load the tokenizer config of an online model. a_ : int = get_tokenizer_config('''bert-base-cased''' ) a_ : Union[str, Any] = config.pop('''_commit_hash''' , __SCREAMING_SNAKE_CASE ) # If we ever update bert-base-cased tokenizer config, this dict here will need to be updated. self.assertEqual(__SCREAMING_SNAKE_CASE , {'''do_lower_case''': False} ) # This model does not have a tokenizer_config so we get back an empty dict. a_ : Dict = get_tokenizer_config(__SCREAMING_SNAKE_CASE ) self.assertDictEqual(__SCREAMING_SNAKE_CASE , {} ) # A tokenizer saved with `save_pretrained` always creates a tokenizer config. a_ : Optional[Any] = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Dict = get_tokenizer_config(__SCREAMING_SNAKE_CASE ) # Check the class of the tokenizer was properly saved (note that it always saves the slow class). self.assertEqual(config['''tokenizer_class'''] , '''BertTokenizer''' ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> str: try: AutoConfig.register('''custom''' , __SCREAMING_SNAKE_CASE ) AutoTokenizer.register(__SCREAMING_SNAKE_CASE , slow_tokenizer_class=__SCREAMING_SNAKE_CASE ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(__SCREAMING_SNAKE_CASE ): AutoTokenizer.register(__SCREAMING_SNAKE_CASE , slow_tokenizer_class=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = CustomTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] @require_tokenizers def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Optional[int]: try: AutoConfig.register('''custom''' , __SCREAMING_SNAKE_CASE ) # Can register in two steps AutoTokenizer.register(__SCREAMING_SNAKE_CASE , slow_tokenizer_class=__SCREAMING_SNAKE_CASE ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] , (CustomTokenizer, None) ) AutoTokenizer.register(__SCREAMING_SNAKE_CASE , fast_tokenizer_class=__SCREAMING_SNAKE_CASE ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] , (CustomTokenizer, CustomTokenizerFast) ) del TOKENIZER_MAPPING._extra_content[CustomConfig] # Can register in one step AutoTokenizer.register( __SCREAMING_SNAKE_CASE , slow_tokenizer_class=__SCREAMING_SNAKE_CASE , fast_tokenizer_class=__SCREAMING_SNAKE_CASE ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] , (CustomTokenizer, CustomTokenizerFast) ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(__SCREAMING_SNAKE_CASE ): AutoTokenizer.register(__SCREAMING_SNAKE_CASE , fast_tokenizer_class=__SCREAMING_SNAKE_CASE ) # We pass through a bert tokenizer fast cause there is no converter slow to fast for our new toknizer # and that model does not have a tokenizer.json with tempfile.TemporaryDirectory() as tmp_dir: a_ : Optional[int] = BertTokenizerFast.from_pretrained(__SCREAMING_SNAKE_CASE ) bert_tokenizer.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Tuple = CustomTokenizerFast.from_pretrained(__SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Tuple = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> int: # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(__SCREAMING_SNAKE_CASE ): a_ : Tuple = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' ) # If remote code is disabled, we can't load this config. with self.assertRaises(__SCREAMING_SNAKE_CASE ): a_ : Optional[int] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=__SCREAMING_SNAKE_CASE ) a_ : List[Any] = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=__SCREAMING_SNAKE_CASE ) self.assertTrue(tokenizer.special_attribute_present ) # Test tokenizer can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : str = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE , trust_remote_code=__SCREAMING_SNAKE_CASE ) self.assertTrue(reloaded_tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) self.assertEqual(reloaded_tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) # Test we can also load the slow version a_ : Dict = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=__SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE ) self.assertTrue(tokenizer.special_attribute_present ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) # Test tokenizer can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : List[str] = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE , trust_remote_code=__SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE ) self.assertEqual(reloaded_tokenizer.__class__.__name__ , '''NewTokenizer''' ) self.assertTrue(reloaded_tokenizer.special_attribute_present ) else: self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) self.assertEqual(reloaded_tokenizer.__class__.__name__ , '''NewTokenizer''' ) @require_tokenizers def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Optional[int]: class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = False class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = NewTokenizer snake_case__ = False try: AutoConfig.register('''custom''' , __SCREAMING_SNAKE_CASE ) AutoTokenizer.register(__SCREAMING_SNAKE_CASE , slow_tokenizer_class=__SCREAMING_SNAKE_CASE ) AutoTokenizer.register(__SCREAMING_SNAKE_CASE , fast_tokenizer_class=__SCREAMING_SNAKE_CASE ) # If remote code is not set, the default is to use local a_ : List[Any] = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) self.assertFalse(tokenizer.special_attribute_present ) a_ : Dict = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' , use_fast=__SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) self.assertFalse(tokenizer.special_attribute_present ) # If remote code is disabled, we load the local one. a_ : Optional[int] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=__SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) self.assertFalse(tokenizer.special_attribute_present ) a_ : Any = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=__SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) self.assertFalse(tokenizer.special_attribute_present ) # If remote is enabled, we load from the Hub a_ : str = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=__SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) self.assertTrue(tokenizer.special_attribute_present ) a_ : Optional[Any] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=__SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) self.assertTrue(tokenizer.special_attribute_present ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] def SCREAMING_SNAKE_CASE ( self : Any ) -> List[str]: a_ : List[Any] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer_legacy''' , trust_remote_code=__SCREAMING_SNAKE_CASE ) self.assertTrue(tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) # Test we can also load the slow version a_ : Tuple = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer_legacy''' , trust_remote_code=__SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE ) self.assertTrue(tokenizer.special_attribute_present ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) else: self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) def SCREAMING_SNAKE_CASE ( self : Any ) -> List[str]: with self.assertRaisesRegex( __SCREAMING_SNAKE_CASE , '''bert-base is not a local folder and is not a valid model identifier''' ): a_ : Optional[Any] = AutoTokenizer.from_pretrained('''bert-base''' ) def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Tuple: with self.assertRaisesRegex( __SCREAMING_SNAKE_CASE , r'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): a_ : Optional[int] = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE , revision='''aaaaaa''' ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: # Make sure we have cached the tokenizer. a_ : str = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) with RequestCounter() as counter: a_ : str = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 )
713
'''simple docstring''' import requests from bsa import BeautifulSoup def _UpperCAmelCase ( __A : str , __A : dict ): a_ : Tuple = BeautifulSoup(requests.get(__A , params=__A ).content , '''html.parser''' ) a_ : List[str] = soup.find('''div''' , attrs={'''class''': '''gs_ri'''} ) a_ : List[str] = div.find('''div''' , attrs={'''class''': '''gs_fl'''} ).find_all('''a''' ) return anchors[2].get_text() if __name__ == "__main__": __lowerCAmelCase = { 'title': ( 'Precisely geometry controlled microsupercapacitors for ultrahigh areal ' 'capacitance, volumetric capacitance, and energy density' ), 'journal': 'Chem. Mater.', 'volume': 30, 'pages': '3979-3990', 'year': 2_018, 'hl': 'en', } print(get_citation('https://scholar.google.com/scholar_lookup', params=params))
666
0
'''simple docstring''' from __future__ import annotations import os from collections.abc import Mapping __lowerCAmelCase = tuple[int, int] class SCREAMING_SNAKE_CASE : def __init__( self : List[str] , __SCREAMING_SNAKE_CASE : set[int] , __SCREAMING_SNAKE_CASE : Mapping[EdgeT, int] ) -> None: a_ : set[int] = vertices a_ : dict[EdgeT, int] = { (min(__SCREAMING_SNAKE_CASE ), max(__SCREAMING_SNAKE_CASE )): weight for edge, weight in edges.items() } def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : EdgeT , __SCREAMING_SNAKE_CASE : int ) -> None: self.vertices.add(edge[0] ) self.vertices.add(edge[1] ) a_ : Optional[Any] = weight def SCREAMING_SNAKE_CASE ( self : Any ) -> Graph: a_ : Graph = Graph({min(self.vertices )} , {} ) a_ : EdgeT a_ : int a_ : EdgeT a_ : int while len(subgraph.vertices ) < len(self.vertices ): a_ : List[Any] = max(self.edges.values() ) + 1 for edge, weight in self.edges.items(): if (edge[0] in subgraph.vertices) ^ (edge[1] in subgraph.vertices): if weight < min_weight: a_ : Any = edge a_ : List[Any] = weight subgraph.add_edge(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) return subgraph def _UpperCAmelCase ( __A : str = "p107_network.txt" ): a_ : str = os.path.abspath(os.path.dirname(__A ) ) a_ : str = os.path.join(__A , __A ) a_ : dict[EdgeT, int] = {} a_ : list[str] a_ : int a_ : int with open(__A ) as f: a_ : str = f.read().strip().split('''\n''' ) a_ : List[Any] = [line.split(''',''' ) for line in data] for edgea in range(1 , len(__A ) ): for edgea in range(__A ): if adjaceny_matrix[edgea][edgea] != "-": a_ : Tuple = int(adjaceny_matrix[edgea][edgea] ) a_ : Graph = Graph(set(range(len(__A ) ) ) , __A ) a_ : Graph = graph.prims_algorithm() a_ : int = sum(graph.edges.values() ) a_ : int = sum(subgraph.edges.values() ) return initial_total - optimal_total if __name__ == "__main__": print(F"""{solution() = }""")
714
'''simple docstring''' import argparse from torch import nn # transformers_old should correspond to branch `save_old_prophetnet_model_structure` here # original prophetnet_checkpoints are saved under `patrickvonplaten/..._old` respectively from transformers_old.modeling_prophetnet import ( ProphetNetForConditionalGeneration as ProphetNetForConditionalGenerationOld, ) from transformers_old.modeling_xlm_prophetnet import ( XLMProphetNetForConditionalGeneration as XLMProphetNetForConditionalGenerationOld, ) from transformers import ProphetNetForConditionalGeneration, XLMProphetNetForConditionalGeneration, logging __lowerCAmelCase = logging.get_logger(__name__) logging.set_verbosity_info() def _UpperCAmelCase ( __A : str , __A : str ): if "xprophetnet" in prophetnet_checkpoint_path: a_ : Tuple = XLMProphetNetForConditionalGenerationOld.from_pretrained(__A ) a_ , a_ : Optional[Any] = XLMProphetNetForConditionalGeneration.from_pretrained( __A , output_loading_info=__A ) else: a_ : List[Any] = ProphetNetForConditionalGenerationOld.from_pretrained(__A ) a_ , a_ : Any = ProphetNetForConditionalGeneration.from_pretrained( __A , output_loading_info=__A ) a_ : str = ['''key_proj''', '''value_proj''', '''query_proj'''] a_ : Tuple = { '''self_attn''': '''ngram_self_attn''', '''cross_attn''': '''encoder_attn''', '''cross_attn_layer_norm''': '''encoder_attn_layer_norm''', '''feed_forward_layer_norm''': '''final_layer_norm''', '''feed_forward''': '''''', '''intermediate''': '''fc1''', '''output''': '''fc2''', '''key_proj''': '''k_proj''', '''query_proj''': '''q_proj''', '''value_proj''': '''v_proj''', '''word_embeddings''': '''embed_tokens''', '''embeddings_layer_norm''': '''emb_layer_norm''', '''relative_pos_embeddings''': '''relative_linear''', '''ngram_embeddings''': '''ngram_input_embed''', '''position_embeddings''': '''embed_positions''', } for key in loading_info["missing_keys"]: a_ : List[str] = key.split('''.''' ) if attributes[0] == "lm_head": a_ : List[str] = prophet a_ : Dict = prophet_old else: a_ : str = prophet.prophetnet a_ : int = prophet_old.model a_ : str = False for attribute in attributes: if attribute in mapping: a_ : Dict = mapping[attribute] if not hasattr(__A , __A ) and len(__A ) > 0: a_ : List[str] = attribute elif hasattr(__A , __A ): a_ : Union[str, Any] = attribute if attribute == "weight": assert old_model.weight.shape == model.weight.shape, "Shapes have to match!" a_ : Tuple = old_model.weight logger.info(f'{attribute} is initialized.' ) a_ : Union[str, Any] = True break elif attribute == "bias": assert old_model.bias.shape == model.bias.shape, "Shapes have to match!" a_ : Union[str, Any] = old_model.bias logger.info(f'{attribute} is initialized' ) a_ : Dict = True break elif attribute in special_keys and hasattr(__A , '''in_proj_weight''' ): a_ : Tuple = old_model.in_proj_weight.shape[0] // 3 a_ : Any = getattr(__A , __A ) param.weight.shape == old_model.in_proj_weight[:embed_dim, :].shape, "Shapes have to match" param.bias.shape == old_model.in_proj_bias[:embed_dim].shape, "Shapes have to match" if attribute == "query_proj": a_ : Union[str, Any] = nn.Parameter(old_model.in_proj_weight[:embed_dim, :] ) a_ : Optional[Any] = nn.Parameter(old_model.in_proj_bias[:embed_dim] ) elif attribute == "key_proj": a_ : List[Any] = nn.Parameter(old_model.in_proj_weight[embed_dim : 2 * embed_dim, :] ) a_ : Optional[int] = nn.Parameter(old_model.in_proj_bias[embed_dim : 2 * embed_dim] ) elif attribute == "value_proj": a_ : Tuple = nn.Parameter(old_model.in_proj_weight[2 * embed_dim :, :] ) a_ : Any = nn.Parameter(old_model.in_proj_bias[2 * embed_dim :] ) a_ : Dict = True break elif attribute == "position_embeddings": assert ( model.position_embeddings.weight.shape[-1] == old_model.embed_positions.weight.shape[-1] ), "Hidden size has to match" assert model.position_embeddings.weight.shape[0] == 5_12, "We want 512 position_embeddings." a_ : Union[str, Any] = nn.Parameter(old_model.embed_positions.weight[:5_12, :] ) a_ : Optional[Any] = True break if attribute.isdigit(): a_ : Union[str, Any] = model[int(__A )] a_ : str = old_model[int(__A )] else: a_ : Tuple = getattr(__A , __A ) if old_attribute == "": a_ : List[str] = old_model else: if not hasattr(__A , __A ): raise ValueError(f'{old_model} does not have {old_attribute}' ) a_ : Optional[Any] = getattr(__A , __A ) if not is_key_init: raise ValueError(f'{key} was not correctly initialized!' ) print(f'Saving model to {pytorch_dump_folder_path}' ) prophet.save_pretrained(__A ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '--prophetnet_checkpoint_path', default=None, type=str, required=True, help='Path the official PyTorch dump.' ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) __lowerCAmelCase = parser.parse_args() convert_prophetnet_checkpoint_to_pytorch(args.prophetnet_checkpoint_path, args.pytorch_dump_folder_path)
666
0
import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( WavaVecaConformerConfig, WavaVecaConformerForCTC, WavaVecaConformerForPreTraining, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaProcessor, logging, ) logging.set_verbosity_info() __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = { 'post_extract_proj': 'feature_projection.projection', 'encoder.pos_conv.0': 'encoder.pos_conv_embed.conv', 'self_attn.linear_k': 'encoder.layers.*.self_attn.linear_k', 'self_attn.linear_v': 'encoder.layers.*.self_attn.linear_v', 'self_attn.linear_q': 'encoder.layers.*.self_attn.linear_q', 'self_attn.pos_bias_u': 'encoder.layers.*.self_attn.pos_bias_u', 'self_attn.pos_bias_v': 'encoder.layers.*.self_attn.pos_bias_v', 'self_attn.linear_out': 'encoder.layers.*.self_attn.linear_out', 'self_attn.linear_pos': 'encoder.layers.*.self_attn.linear_pos', 'self_attn.rotary_emb': 'encoder.embed_positions', 'self_attn_layer_norm': 'encoder.layers.*.self_attn_layer_norm', 'conv_module.pointwise_conv1': 'encoder.layers.*.conv_module.pointwise_conv1', 'conv_module.pointwise_conv2': 'encoder.layers.*.conv_module.pointwise_conv2', 'conv_module.depthwise_conv': 'encoder.layers.*.conv_module.depthwise_conv', 'conv_module.batch_norm': 'encoder.layers.*.conv_module.batch_norm', 'conv_module.layer_norm': 'encoder.layers.*.conv_module.layer_norm', 'ffn1.w_1': 'encoder.layers.*.ffn1.intermediate_dense', 'ffn1.w_2': 'encoder.layers.*.ffn1.output_dense', 'ffn1.layer_norm': 'encoder.layers.*.ffn1_layer_norm', 'ffn2.w_1': 'encoder.layers.*.ffn2.intermediate_dense', 'ffn2.w_2': 'encoder.layers.*.ffn2.output_dense', 'ffn2.layer_norm': 'encoder.layers.*.ffn2_layer_norm', 'final_layer_norm': 'encoder.layers.*.final_layer_norm', 'encoder.layer_norm': 'encoder.layer_norm', 'w2v_model.layer_norm': 'feature_projection.layer_norm', 'quantizer.weight_proj': 'quantizer.weight_proj', 'quantizer.vars': 'quantizer.codevectors', 'project_q': 'project_q', 'final_proj': 'project_hid', 'w2v_encoder.proj': 'lm_head', 'mask_emb': 'masked_spec_embed', } __lowerCAmelCase = [ 'lm_head', 'quantizer.weight_proj', 'quantizer.codevectors', 'project_q', 'project_hid', ] def _UpperCAmelCase ( __A : Optional[int] , __A : str , __A : Dict , __A : List[Any] , __A : List[Any] ): for attribute in key.split('''.''' ): a_ : int = getattr(__A , __A ) if weight_type is not None: a_ : Tuple = getattr(__A , __A ).shape else: a_ : int = hf_pointer.shape if hf_shape != value.shape: raise ValueError( f'Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be' f' {value.shape} for {full_name}' ) if weight_type == "weight": a_ : Optional[int] = value elif weight_type == "weight_g": a_ : List[Any] = value elif weight_type == "weight_v": a_ : Any = value elif weight_type == "bias": a_ : List[Any] = value elif weight_type == "running_mean": a_ : str = value elif weight_type == "running_var": a_ : List[str] = value elif weight_type == "num_batches_tracked": a_ : Optional[Any] = value elif weight_type == "inv_freq": a_ : Union[str, Any] = value else: a_ : Tuple = value logger.info(f'{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.' ) def _UpperCAmelCase ( __A : str , __A : Union[str, Any] , __A : int ): a_ : List[str] = [] a_ : List[str] = fairseq_model.state_dict() a_ : Union[str, Any] = hf_model.wavaveca_conformer.feature_extractor for name, value in fairseq_dict.items(): a_ : Optional[int] = False if "conv_layers" in name: load_conv_layer( __A , __A , __A , __A , hf_model.config.feat_extract_norm == '''group''' , ) a_ : Optional[Any] = True else: for key, mapped_key in MAPPING.items(): a_ : Union[str, Any] = '''wav2vec2_conformer.''' + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if key in name or key.split('''w2v_model.''' )[-1] == name.split('''.''' )[0]: a_ : Tuple = True if "*" in mapped_key: a_ : Any = name.split(__A )[0].split('''.''' )[-2] a_ : Tuple = mapped_key.replace('''*''' , __A ) if "pos_bias_u" in name: a_ : List[Any] = None elif "pos_bias_v" in name: a_ : List[Any] = None elif "weight_g" in name: a_ : str = '''weight_g''' elif "weight_v" in name: a_ : List[Any] = '''weight_v''' elif "bias" in name: a_ : List[str] = '''bias''' elif "weight" in name: # TODO: don't match quantizer.weight_proj a_ : List[Any] = '''weight''' elif "running_mean" in name: a_ : Any = '''running_mean''' elif "inv_freq" in name: a_ : List[str] = '''inv_freq''' elif "running_var" in name: a_ : Optional[int] = '''running_var''' elif "num_batches_tracked" in name: a_ : Optional[Any] = '''num_batches_tracked''' else: a_ : Dict = None set_recursively(__A , __A , __A , __A , __A ) continue if not is_used: unused_weights.append(__A ) logger.warning(f'Unused weights: {unused_weights}' ) def _UpperCAmelCase ( __A : str , __A : int , __A : int , __A : List[str] , __A : int ): a_ : Optional[int] = full_name.split('''conv_layers.''' )[-1] a_ : Optional[Any] = name.split('''.''' ) a_ : List[str] = int(items[0] ) a_ : Tuple = int(items[1] ) if type_id == 0: if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.bias.data.shape: raise ValueError( f'{full_name} has size {value.shape}, but' f' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.' ) a_ : List[Any] = value logger.info(f'Feat extract conv layer {layer_id} was initialized from {full_name}.' ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.weight.data.shape: raise ValueError( f'{full_name} has size {value.shape}, but' f' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.' ) a_ : int = value logger.info(f'Feat extract conv layer {layer_id} was initialized from {full_name}.' ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape: raise ValueError( f'{full_name} has size {value.shape}, but' f' {feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape} was found.' ) a_ : List[str] = value logger.info(f'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.' ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape: raise ValueError( f'{full_name} has size {value.shape}, but' f' {feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape} was found.' ) a_ : Any = value logger.info(f'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.' ) else: unused_weights.append(__A ) @torch.no_grad() def _UpperCAmelCase ( __A : str , __A : int , __A : Dict=None , __A : Tuple=None , __A : int=True ): if config_path is not None: a_ : Dict = WavaVecaConformerConfig.from_pretrained(__A , hidden_act='''swish''' ) else: a_ : List[Any] = WavaVecaConformerConfig() if "rope" in checkpoint_path: a_ : Union[str, Any] = '''rotary''' if is_finetuned: if dict_path: a_ : Tuple = Dictionary.load(__A ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq a_ : Dict = target_dict.pad_index a_ : Union[str, Any] = target_dict.bos_index a_ : Dict = target_dict.eos_index a_ : Union[str, Any] = len(target_dict.symbols ) a_ : str = os.path.join(__A , '''vocab.json''' ) if not os.path.isdir(__A ): logger.error('''--pytorch_dump_folder_path ({}) should be a directory'''.format(__A ) ) return os.makedirs(__A , exist_ok=__A ) a_ : Dict = target_dict.indices # fairseq has the <pad> and <s> switched a_ : Union[str, Any] = 0 a_ : Dict = 1 with open(__A , '''w''' , encoding='''utf-8''' ) as vocab_handle: json.dump(__A , __A ) a_ : Any = WavaVecaCTCTokenizer( __A , unk_token=target_dict.unk_word , pad_token=target_dict.pad_word , bos_token=target_dict.bos_word , eos_token=target_dict.eos_word , word_delimiter_token='''|''' , do_lower_case=__A , ) a_ : Optional[Any] = True if config.feat_extract_norm == '''layer''' else False a_ : List[Any] = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_60_00 , padding_value=0 , do_normalize=__A , return_attention_mask=__A , ) a_ : Dict = WavaVecaProcessor(feature_extractor=__A , tokenizer=__A ) processor.save_pretrained(__A ) a_ : Dict = WavaVecaConformerForCTC(__A ) else: a_ : Any = WavaVecaConformerForPreTraining(__A ) if is_finetuned: a_ : Any = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={'''data''': '''/'''.join(dict_path.split('''/''' )[:-1] )} ) else: a_ : str = argparse.Namespace(task='''audio_pretraining''' ) a_ : str = fairseq.tasks.setup_task(__A ) a_ : Union[str, Any] = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] , task=__A ) a_ : Optional[int] = model[0].eval() recursively_load_weights(__A , __A , not is_finetuned ) hf_wavavec.save_pretrained(__A ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to fairseq checkpoint') parser.add_argument('--dict_path', default=None, type=str, help='Path to dict of fine-tuned model') parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert') parser.add_argument( '--not_finetuned', action='store_true', help='Whether the model to convert is a fine-tuned model or not' ) __lowerCAmelCase = parser.parse_args() convert_wavaveca_conformer_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned )
715
'''simple docstring''' import re import string import numpy as np import datasets __lowerCAmelCase = '\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n' __lowerCAmelCase = '\nArgs:\n predictions: List of predicted texts.\n references: List of reference texts.\n regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n ignore when calculating the exact matches. Note: these regexes are removed\n from the input data before the changes based on the options below (e.g. ignore_case,\n ignore_punctuation, ignore_numbers) are applied.\n ignore_case: Boolean, defaults to False. If true, turns everything\n to lowercase so that capitalization differences are ignored.\n ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\n ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\nReturns:\n exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 25.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 50.0\n\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 75.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n >>> print(round(results["exact_match"], 1))\n 100.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["The cat sat on the mat.", "Theaters are great.", "It\'s like comparing oranges and apples."]\n >>> preds = ["The cat sat on the mat?", "Theaters are great.", "It\'s like comparing apples and oranges."]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 33.3\n\n' __lowerCAmelCase = '\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class SCREAMING_SNAKE_CASE ( datasets.Metric ): def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> int: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , reference_urls=[] , ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Dict=None , __SCREAMING_SNAKE_CASE : int=False , __SCREAMING_SNAKE_CASE : Optional[int]=False , __SCREAMING_SNAKE_CASE : Dict=False , ) -> str: if regexes_to_ignore is not None: for s in regexes_to_ignore: a_ : Optional[Any] = np.array([re.sub(__SCREAMING_SNAKE_CASE , '''''' , __SCREAMING_SNAKE_CASE ) for x in predictions] ) a_ : int = np.array([re.sub(__SCREAMING_SNAKE_CASE , '''''' , __SCREAMING_SNAKE_CASE ) for x in references] ) else: a_ : List[str] = np.asarray(__SCREAMING_SNAKE_CASE ) a_ : Any = np.asarray(__SCREAMING_SNAKE_CASE ) if ignore_case: a_ : List[str] = np.char.lower(__SCREAMING_SNAKE_CASE ) a_ : List[Any] = np.char.lower(__SCREAMING_SNAKE_CASE ) if ignore_punctuation: a_ : Any = string.punctuation.maketrans('''''' , '''''' , string.punctuation ) a_ : Union[str, Any] = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : int = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) if ignore_numbers: a_ : int = string.digits.maketrans('''''' , '''''' , string.digits ) a_ : Optional[int] = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : Dict = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = predictions == references return {"exact_match": np.mean(__SCREAMING_SNAKE_CASE ) * 100}
666
0
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( __A : list[int] ): # This function is recursive a_ : Any = len(__A ) # If the array contains only one element, we return it (it's the stop condition of # recursion) if array_length <= 1: return array # Else a_ : Optional[int] = array[0] a_ : Union[str, Any] = False a_ : List[Any] = 1 a_ : list[int] = [] while not is_found and i < array_length: if array[i] < pivot: a_ : Optional[int] = True a_ : Dict = [element for element in array[i:] if element >= array[i]] a_ : Any = longest_subsequence(__A ) if len(__A ) > len(__A ): a_ : Tuple = temp_array else: i += 1 a_ : str = [element for element in array[1:] if element >= pivot] a_ : Optional[int] = [pivot, *longest_subsequence(__A )] if len(__A ) > len(__A ): return temp_array else: return longest_subseq if __name__ == "__main__": import doctest doctest.testmod()
716
'''simple docstring''' import gc import unittest import numpy as np import torch from diffusers import ( AudioDiffusionPipeline, AutoencoderKL, DDIMScheduler, DDPMScheduler, DiffusionPipeline, Mel, UNetaDConditionModel, UNetaDModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : Dict ) -> List[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> List[Any]: torch.manual_seed(0 ) a_ : Tuple = UNetaDModel( sample_size=(32, 64) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''') , ) return model @property def SCREAMING_SNAKE_CASE ( self : int ) -> List[Any]: torch.manual_seed(0 ) a_ : Any = UNetaDConditionModel( sample_size=(64, 32) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''CrossAttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''CrossAttnUpBlock2D''') , cross_attention_dim=10 , ) return model @property def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: torch.manual_seed(0 ) a_ : List[Any] = AutoencoderKL( sample_size=(128, 64) , in_channels=1 , out_channels=1 , latent_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''DownEncoderBlock2D''', '''DownEncoderBlock2D''') , up_block_types=('''UpDecoderBlock2D''', '''UpDecoderBlock2D''') , ) a_ : List[Any] = UNetaDModel( sample_size=(64, 32) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''') , ) return vqvae, unet @slow def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union[str, Any]: a_ : Dict = '''cpu''' # ensure determinism for the device-dependent torch.Generator a_ : Union[str, Any] = Mel( x_res=self.dummy_unet.config.sample_size[1] , y_res=self.dummy_unet.config.sample_size[0] , ) a_ : Any = DDPMScheduler() a_ : str = AudioDiffusionPipeline(vqvae=__SCREAMING_SNAKE_CASE , unet=self.dummy_unet , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : List[Any] = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : List[str] = pipe(generator=__SCREAMING_SNAKE_CASE , steps=4 ) a_ : List[Any] = output.audios[0] a_ : Dict = output.images[0] a_ : Dict = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : Optional[Any] = pipe(generator=__SCREAMING_SNAKE_CASE , steps=4 , return_dict=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output[0][0] assert audio.shape == (1, (self.dummy_unet.config.sample_size[1] - 1) * mel.hop_length) assert ( image.height == self.dummy_unet.config.sample_size[0] and image.width == self.dummy_unet.config.sample_size[1] ) a_ : Dict = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : str = np.frombuffer(image_from_tuple.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([69, 255, 255, 255, 0, 0, 77, 181, 12, 127] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() == 0 a_ : str = Mel( x_res=self.dummy_vqvae_and_unet[0].config.sample_size[1] , y_res=self.dummy_vqvae_and_unet[0].config.sample_size[0] , ) a_ : int = DDIMScheduler() a_ : Dict = self.dummy_vqvae_and_unet a_ : List[str] = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0] , unet=dummy_vqvae_and_unet[1] , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : Any = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) np.random.seed(0 ) a_ : List[str] = np.random.uniform(-1 , 1 , ((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,) ) a_ : int = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : int = pipe(raw_audio=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , start_step=5 , steps=10 ) a_ : List[str] = output.images[0] assert ( image.height == self.dummy_vqvae_and_unet[0].config.sample_size[0] and image.width == self.dummy_vqvae_and_unet[0].config.sample_size[1] ) a_ : Optional[Any] = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([120, 117, 110, 109, 138, 167, 138, 148, 132, 121] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 a_ : List[str] = self.dummy_unet_condition a_ : Dict = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0] , unet=__SCREAMING_SNAKE_CASE , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : int = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) np.random.seed(0 ) a_ : Any = torch.rand((1, 1, 10) ) a_ : Tuple = pipe(generator=__SCREAMING_SNAKE_CASE , encoding=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output.images[0] a_ : Dict = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([107, 103, 120, 127, 142, 122, 113, 122, 97, 111] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 @slow @require_torch_gpu class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Any: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Any: a_ : Any = torch_device a_ : Optional[int] = DiffusionPipeline.from_pretrained('''teticio/audio-diffusion-ddim-256''' ) a_ : Dict = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : str = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : List[str] = pipe(generator=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output.audios[0] a_ : Tuple = output.images[0] assert audio.shape == (1, (pipe.unet.config.sample_size[1] - 1) * pipe.mel.hop_length) assert image.height == pipe.unet.config.sample_size[0] and image.width == pipe.unet.config.sample_size[1] a_ : str = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : Tuple = np.array([151, 167, 154, 144, 122, 134, 121, 105, 70, 26] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0
666
0
import os import textwrap import pyarrow as pa import pytest from datasets import ClassLabel, Features, Image from datasets.packaged_modules.csv.csv import Csv from ..utils import require_pil @pytest.fixture def _UpperCAmelCase ( __A : List[Any] ): a_ : Optional[Any] = tmp_path / '''file.csv''' a_ : List[Any] = textwrap.dedent( '''\ header1,header2 1,2 10,20 ''' ) with open(__A , '''w''' ) as f: f.write(__A ) return str(__A ) @pytest.fixture def _UpperCAmelCase ( __A : int ): a_ : int = tmp_path / '''malformed_file.csv''' a_ : Union[str, Any] = textwrap.dedent( '''\ header1,header2 1,2 10,20, ''' ) with open(__A , '''w''' ) as f: f.write(__A ) return str(__A ) @pytest.fixture def _UpperCAmelCase ( __A : Optional[Any] , __A : Dict ): a_ : List[str] = tmp_path / '''csv_with_image.csv''' a_ : Dict = textwrap.dedent( f'\\n image\n {image_file}\n ' ) with open(__A , '''w''' ) as f: f.write(__A ) return str(__A ) @pytest.fixture def _UpperCAmelCase ( __A : Optional[Any] ): a_ : Dict = tmp_path / '''csv_with_label.csv''' a_ : Dict = textwrap.dedent( '''\ label good bad good ''' ) with open(__A , '''w''' ) as f: f.write(__A ) return str(__A ) @pytest.fixture def _UpperCAmelCase ( __A : int ): a_ : List[str] = tmp_path / '''csv_with_int_list.csv''' a_ : str = textwrap.dedent( '''\ int_list 1 2 3 4 5 6 7 8 9 ''' ) with open(__A , '''w''' ) as f: f.write(__A ) return str(__A ) def _UpperCAmelCase ( __A : Union[str, Any] , __A : Optional[int] , __A : List[Any] ): a_ : Optional[int] = Csv() a_ : Any = csv._generate_tables([[csv_file, malformed_csv_file]] ) with pytest.raises(__A , match='''Error tokenizing data''' ): for _ in generator: pass assert any( record.levelname == '''ERROR''' and '''Failed to read file''' in record.message and os.path.basename(__A ) in record.message for record in caplog.records ) @require_pil def _UpperCAmelCase ( __A : str ): with open(__A , encoding='''utf-8''' ) as f: a_ : Tuple = f.read().splitlines()[1] a_ : Union[str, Any] = Csv(encoding='''utf-8''' , features=Features({'''image''': Image()} ) ) a_ : Optional[int] = csv._generate_tables([[csv_file_with_image]] ) a_ : Union[str, Any] = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field('''image''' ).type == Image()() a_ : str = pa_table.to_pydict()['''image'''] assert generated_content == [{"path": image_file, "bytes": None}] def _UpperCAmelCase ( __A : List[str] ): with open(__A , encoding='''utf-8''' ) as f: a_ : int = f.read().splitlines()[1:] a_ : str = Csv(encoding='''utf-8''' , features=Features({'''label''': ClassLabel(names=['''good''', '''bad'''] )} ) ) a_ : Optional[Any] = csv._generate_tables([[csv_file_with_label]] ) a_ : str = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field('''label''' ).type == ClassLabel(names=['''good''', '''bad'''] )() a_ : Dict = pa_table.to_pydict()['''label'''] assert generated_content == [ClassLabel(names=['''good''', '''bad'''] ).straint(__A ) for label in labels] def _UpperCAmelCase ( __A : str ): a_ : Optional[int] = Csv(encoding='''utf-8''' , sep=''',''' , converters={'''int_list''': lambda __A : [int(__A ) for i in x.split()]} ) a_ : Union[str, Any] = csv._generate_tables([[csv_file_with_int_list]] ) a_ : Dict = pa.concat_tables([table for _, table in generator] ) assert pa.types.is_list(pa_table.schema.field('''int_list''' ).type ) a_ : List[Any] = pa_table.to_pydict()['''int_list'''] assert generated_content == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
717
'''simple docstring''' import argparse import json from pathlib import Path import requests import torch from huggingface_hub import cached_download, hf_hub_download, hf_hub_url from PIL import Image from transformers import DetaConfig, DetaForObjectDetection, DetaImageProcessor, SwinConfig from transformers.utils import logging logging.set_verbosity_info() __lowerCAmelCase = logging.get_logger(__name__) def _UpperCAmelCase ( __A : Union[str, Any] ): a_ : Tuple = SwinConfig( embed_dim=1_92 , depths=(2, 2, 18, 2) , num_heads=(6, 12, 24, 48) , window_size=12 , out_features=['''stage2''', '''stage3''', '''stage4'''] , ) a_ : List[Any] = DetaConfig( backbone_config=__A , num_queries=9_00 , encoder_ffn_dim=20_48 , decoder_ffn_dim=20_48 , num_feature_levels=5 , assign_first_stage=__A , with_box_refine=__A , two_stage=__A , ) # set labels a_ : Optional[Any] = '''huggingface/label-files''' if "o365" in model_name: a_ : Optional[Any] = 3_66 a_ : Tuple = '''object365-id2label.json''' else: a_ : Any = 91 a_ : Union[str, Any] = '''coco-detection-id2label.json''' a_ : Tuple = num_labels a_ : str = json.load(open(cached_download(hf_hub_url(__A , __A , repo_type='''dataset''' ) ) , '''r''' ) ) a_ : Optional[int] = {int(__A ): v for k, v in idalabel.items()} a_ : int = idalabel a_ : Dict = {v: k for k, v in idalabel.items()} return config def _UpperCAmelCase ( __A : List[str] ): a_ : Tuple = [] # stem # fmt: off rename_keys.append(('''backbone.0.body.patch_embed.proj.weight''', '''model.backbone.model.embeddings.patch_embeddings.projection.weight''') ) rename_keys.append(('''backbone.0.body.patch_embed.proj.bias''', '''model.backbone.model.embeddings.patch_embeddings.projection.bias''') ) rename_keys.append(('''backbone.0.body.patch_embed.norm.weight''', '''model.backbone.model.embeddings.norm.weight''') ) rename_keys.append(('''backbone.0.body.patch_embed.norm.bias''', '''model.backbone.model.embeddings.norm.bias''') ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm1.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm1.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_bias_table', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_index', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.proj.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.proj.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm2.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm2.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.bias') ) if i < 3: rename_keys.append((f'backbone.0.body.layers.{i}.downsample.reduction.weight', f'model.backbone.model.encoder.layers.{i}.downsample.reduction.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.downsample.norm.weight', f'model.backbone.model.encoder.layers.{i}.downsample.norm.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.downsample.norm.bias', f'model.backbone.model.encoder.layers.{i}.downsample.norm.bias') ) rename_keys.append(('''backbone.0.body.norm1.weight''', '''model.backbone.model.hidden_states_norms.stage2.weight''') ) rename_keys.append(('''backbone.0.body.norm1.bias''', '''model.backbone.model.hidden_states_norms.stage2.bias''') ) rename_keys.append(('''backbone.0.body.norm2.weight''', '''model.backbone.model.hidden_states_norms.stage3.weight''') ) rename_keys.append(('''backbone.0.body.norm2.bias''', '''model.backbone.model.hidden_states_norms.stage3.bias''') ) rename_keys.append(('''backbone.0.body.norm3.weight''', '''model.backbone.model.hidden_states_norms.stage4.weight''') ) rename_keys.append(('''backbone.0.body.norm3.bias''', '''model.backbone.model.hidden_states_norms.stage4.bias''') ) # transformer encoder for i in range(config.encoder_layers ): rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.sampling_offsets.weight', f'model.encoder.layers.{i}.self_attn.sampling_offsets.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.sampling_offsets.bias', f'model.encoder.layers.{i}.self_attn.sampling_offsets.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.attention_weights.weight', f'model.encoder.layers.{i}.self_attn.attention_weights.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.attention_weights.bias', f'model.encoder.layers.{i}.self_attn.attention_weights.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.value_proj.weight', f'model.encoder.layers.{i}.self_attn.value_proj.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.value_proj.bias', f'model.encoder.layers.{i}.self_attn.value_proj.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.output_proj.weight', f'model.encoder.layers.{i}.self_attn.output_proj.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.output_proj.bias', f'model.encoder.layers.{i}.self_attn.output_proj.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm1.weight', f'model.encoder.layers.{i}.self_attn_layer_norm.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm1.bias', f'model.encoder.layers.{i}.self_attn_layer_norm.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear1.weight', f'model.encoder.layers.{i}.fc1.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear1.bias', f'model.encoder.layers.{i}.fc1.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear2.weight', f'model.encoder.layers.{i}.fc2.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear2.bias', f'model.encoder.layers.{i}.fc2.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm2.weight', f'model.encoder.layers.{i}.final_layer_norm.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm2.bias', f'model.encoder.layers.{i}.final_layer_norm.bias') ) # transformer decoder for i in range(config.decoder_layers ): rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.sampling_offsets.weight', f'model.decoder.layers.{i}.encoder_attn.sampling_offsets.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.sampling_offsets.bias', f'model.decoder.layers.{i}.encoder_attn.sampling_offsets.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.attention_weights.weight', f'model.decoder.layers.{i}.encoder_attn.attention_weights.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.attention_weights.bias', f'model.decoder.layers.{i}.encoder_attn.attention_weights.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.value_proj.weight', f'model.decoder.layers.{i}.encoder_attn.value_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.value_proj.bias', f'model.decoder.layers.{i}.encoder_attn.value_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.output_proj.weight', f'model.decoder.layers.{i}.encoder_attn.output_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.output_proj.bias', f'model.decoder.layers.{i}.encoder_attn.output_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm1.weight', f'model.decoder.layers.{i}.encoder_attn_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm1.bias', f'model.decoder.layers.{i}.encoder_attn_layer_norm.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.self_attn.out_proj.weight', f'model.decoder.layers.{i}.self_attn.out_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.self_attn.out_proj.bias', f'model.decoder.layers.{i}.self_attn.out_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm2.weight', f'model.decoder.layers.{i}.self_attn_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm2.bias', f'model.decoder.layers.{i}.self_attn_layer_norm.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear1.weight', f'model.decoder.layers.{i}.fc1.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear1.bias', f'model.decoder.layers.{i}.fc1.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear2.weight', f'model.decoder.layers.{i}.fc2.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear2.bias', f'model.decoder.layers.{i}.fc2.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm3.weight', f'model.decoder.layers.{i}.final_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm3.bias', f'model.decoder.layers.{i}.final_layer_norm.bias') ) # fmt: on return rename_keys def _UpperCAmelCase ( __A : str , __A : int , __A : Tuple ): a_ : str = dct.pop(__A ) a_ : Dict = val def _UpperCAmelCase ( __A : List[str] , __A : Optional[int] ): a_ : str = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): a_ : Tuple = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) a_ : List[str] = state_dict.pop(f'backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.weight' ) a_ : str = state_dict.pop(f'backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.bias' ) # next, add query, keys and values (in that order) to the state dict a_ : Optional[Any] = in_proj_weight[:dim, :] a_ : List[Any] = in_proj_bias[: dim] a_ : Optional[Any] = in_proj_weight[ dim : dim * 2, : ] a_ : Union[str, Any] = in_proj_bias[ dim : dim * 2 ] a_ : Optional[int] = in_proj_weight[ -dim :, : ] a_ : int = in_proj_bias[-dim :] # fmt: on def _UpperCAmelCase ( __A : Dict , __A : Dict ): # transformer decoder self-attention layers a_ : Any = config.d_model for i in range(config.decoder_layers ): # read in weights + bias of input projection layer of self-attention a_ : int = state_dict.pop(f'transformer.decoder.layers.{i}.self_attn.in_proj_weight' ) a_ : Any = state_dict.pop(f'transformer.decoder.layers.{i}.self_attn.in_proj_bias' ) # next, add query, keys and values (in that order) to the state dict a_ : Dict = in_proj_weight[:hidden_size, :] a_ : Tuple = in_proj_bias[:hidden_size] a_ : Any = in_proj_weight[ hidden_size : hidden_size * 2, : ] a_ : Tuple = in_proj_bias[hidden_size : hidden_size * 2] a_ : Optional[int] = in_proj_weight[-hidden_size:, :] a_ : int = in_proj_bias[-hidden_size:] def _UpperCAmelCase ( ): a_ : Union[str, Any] = '''http://images.cocodataset.org/val2017/000000039769.jpg''' a_ : List[str] = Image.open(requests.get(__A , stream=__A ).raw ) return im @torch.no_grad() def _UpperCAmelCase ( __A : int , __A : int , __A : Any ): a_ : Union[str, Any] = get_deta_config(__A ) # load original state dict if model_name == "deta-swin-large": a_ : Optional[Any] = hf_hub_download(repo_id='''nielsr/deta-checkpoints''' , filename='''adet_swin_ft.pth''' ) elif model_name == "deta-swin-large-o365": a_ : List[str] = hf_hub_download(repo_id='''jozhang97/deta-swin-l-o365''' , filename='''deta_swin_pt_o365.pth''' ) else: raise ValueError(f'Model name {model_name} not supported' ) a_ : List[Any] = torch.load(__A , map_location='''cpu''' )['''model'''] # original state dict for name, param in state_dict.items(): print(__A , param.shape ) # rename keys a_ : Union[str, Any] = create_rename_keys(__A ) for src, dest in rename_keys: rename_key(__A , __A , __A ) read_in_swin_q_k_v(__A , config.backbone_config ) read_in_decoder_q_k_v(__A , __A ) # fix some prefixes for key in state_dict.copy().keys(): if "transformer.decoder.class_embed" in key or "transformer.decoder.bbox_embed" in key: a_ : Optional[Any] = state_dict.pop(__A ) a_ : int = val if "input_proj" in key: a_ : str = state_dict.pop(__A ) a_ : Optional[Any] = val if "level_embed" in key or "pos_trans" in key or "pix_trans" in key or "enc_output" in key: a_ : List[str] = state_dict.pop(__A ) a_ : List[Any] = val # finally, create HuggingFace model and load state dict a_ : Dict = DetaForObjectDetection(__A ) model.load_state_dict(__A ) model.eval() a_ : int = '''cuda''' if torch.cuda.is_available() else '''cpu''' model.to(__A ) # load image processor a_ : List[Any] = DetaImageProcessor(format='''coco_detection''' ) # verify our conversion on image a_ : Dict = prepare_img() a_ : Optional[int] = processor(images=__A , return_tensors='''pt''' ) a_ : Any = encoding['''pixel_values'''] a_ : int = model(pixel_values.to(__A ) ) # verify logits print('''Logits:''' , outputs.logits[0, :3, :3] ) print('''Boxes:''' , outputs.pred_boxes[0, :3, :3] ) if model_name == "deta-swin-large": a_ : Optional[int] = torch.tensor( [[-7.6308, -2.8485, -5.3737], [-7.2037, -4.5505, -4.8027], [-7.2943, -4.2611, -4.6617]] ) a_ : Tuple = torch.tensor([[0.4987, 0.4969, 0.9999], [0.2549, 0.5498, 0.4805], [0.5498, 0.2757, 0.0569]] ) elif model_name == "deta-swin-large-o365": a_ : Union[str, Any] = torch.tensor( [[-8.0122, -3.5720, -4.9717], [-8.1547, -3.6886, -4.6389], [-7.6610, -3.6194, -5.0134]] ) a_ : Any = torch.tensor([[0.2523, 0.5549, 0.4881], [0.7715, 0.4149, 0.4601], [0.5503, 0.2753, 0.0575]] ) assert torch.allclose(outputs.logits[0, :3, :3] , expected_logits.to(__A ) , atol=1E-4 ) assert torch.allclose(outputs.pred_boxes[0, :3, :3] , expected_boxes.to(__A ) , atol=1E-4 ) print('''Everything ok!''' ) if pytorch_dump_folder_path: # Save model and processor logger.info(f'Saving PyTorch model and processor to {pytorch_dump_folder_path}...' ) Path(__A ).mkdir(exist_ok=__A ) model.save_pretrained(__A ) processor.save_pretrained(__A ) # Push to hub if push_to_hub: print('''Pushing model and processor to hub...''' ) model.push_to_hub(f'jozhang97/{model_name}' ) processor.push_to_hub(f'jozhang97/{model_name}' ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() parser.add_argument( '--model_name', type=str, default='deta-swin-large', choices=['deta-swin-large', 'deta-swin-large-o365'], help='Name of the model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the folder to output PyTorch model.', ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.' ) __lowerCAmelCase = parser.parse_args() convert_deta_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
666
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCAmelCase = { 'configuration_x_clip': [ 'XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP', 'XCLIPConfig', 'XCLIPTextConfig', 'XCLIPVisionConfig', ], 'processing_x_clip': ['XCLIPProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ 'XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST', 'XCLIPModel', 'XCLIPPreTrainedModel', 'XCLIPTextModel', 'XCLIPVisionModel', ] if TYPE_CHECKING: from .configuration_x_clip import ( XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, XCLIPConfig, XCLIPTextConfig, XCLIPVisionConfig, ) from .processing_x_clip import XCLIPProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_x_clip import ( XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST, XCLIPModel, XCLIPPreTrainedModel, XCLIPTextModel, XCLIPVisionModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
718
'''simple docstring''' import unittest import numpy as np import torch from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = DDIMPipeline snake_case__ = UNCONDITIONAL_IMAGE_GENERATION_PARAMS snake_case__ = PipelineTesterMixin.required_optional_params - { "num_images_per_prompt", "latents", "callback", "callback_steps", } snake_case__ = UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS snake_case__ = False def SCREAMING_SNAKE_CASE ( self : Any ) -> Optional[Any]: torch.manual_seed(0 ) a_ : 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''') , ) a_ : str = DDIMScheduler() a_ : Union[str, Any] = {'''unet''': unet, '''scheduler''': scheduler} return components def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Tuple=0 ) -> str: if str(__SCREAMING_SNAKE_CASE ).startswith('''mps''' ): a_ : Dict = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: a_ : Union[str, Any] = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = { '''batch_size''': 1, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union[str, Any]: a_ : Dict = '''cpu''' a_ : List[Any] = self.get_dummy_components() a_ : List[str] = self.pipeline_class(**__SCREAMING_SNAKE_CASE ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = self.get_dummy_inputs(__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = pipe(**__SCREAMING_SNAKE_CASE ).images a_ : List[str] = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 32, 32, 3) ) a_ : int = np.array( [1.000e00, 5.717e-01, 4.717e-01, 1.000e00, 0.000e00, 1.000e00, 3.000e-04, 0.000e00, 9.000e-04] ) a_ : Union[str, Any] = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(__SCREAMING_SNAKE_CASE , 1e-3 ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> str: super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : str ) -> Optional[int]: super().test_save_load_local(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : Any ) -> List[str]: super().test_save_load_optional_components(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : str ) -> Optional[int]: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : str ) -> Any: a_ : Optional[Any] = '''google/ddpm-cifar10-32''' a_ : Optional[Any] = UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Dict = DDIMScheduler() a_ : List[str] = DDIMPipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) ddim.to(__SCREAMING_SNAKE_CASE ) ddim.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = torch.manual_seed(0 ) a_ : Tuple = ddim(generator=__SCREAMING_SNAKE_CASE , eta=0.0 , output_type='''numpy''' ).images a_ : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) a_ : List[str] = np.array([0.1723, 0.1617, 0.1600, 0.1626, 0.1497, 0.1513, 0.1505, 0.1442, 0.1453] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> str: a_ : int = '''google/ddpm-ema-bedroom-256''' a_ : str = UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Tuple = DDIMScheduler.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = DDIMPipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) ddpm.to(__SCREAMING_SNAKE_CASE ) ddpm.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = torch.manual_seed(0 ) a_ : List[Any] = ddpm(generator=__SCREAMING_SNAKE_CASE , output_type='''numpy''' ).images a_ : Optional[int] = image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) a_ : Optional[Any] = np.array([0.0060, 0.0201, 0.0344, 0.0024, 0.0018, 0.0002, 0.0022, 0.0000, 0.0069] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
666
0
'''simple docstring''' 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 SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = LongformerTokenizer snake_case__ = True snake_case__ = LongformerTokenizerFast snake_case__ = True def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt a_ : Tuple = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] a_ : Optional[Any] = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) a_ : Union[str, Any] = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] a_ : Any = {'''unk_token''': '''<unk>'''} a_ : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) a_ : Optional[int] = 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(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : Any , **__SCREAMING_SNAKE_CASE : Any ) -> int: kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , **__SCREAMING_SNAKE_CASE : List[Any] ) -> List[str]: kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : List[Any] ) -> Any: a_ : Union[str, Any] = '''lower newer''' a_ : List[Any] = '''lower newer''' return input_text, output_text def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: a_ : Optional[Any] = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) a_ : List[str] = '''lower newer''' a_ : str = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] a_ : Optional[int] = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) # , add_prefix_space=True) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Dict = tokens + [tokenizer.unk_token] a_ : Any = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Optional[Any]: a_ : Union[str, Any] = self.get_tokenizer() self.assertListEqual(tokenizer.encode('''Hello world!''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) , [0, 3_1414, 232, 328, 2] ) self.assertListEqual( tokenizer.encode('''Hello world! cécé herlolip 418''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) , [0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2] , ) @slow def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[Any]: a_ : Dict = self.tokenizer_class.from_pretrained('''allenai/longformer-base-4096''' ) a_ : Tuple = tokenizer.encode('''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer.encode('''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.encode( '''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.encode( '''sequence builders''' , '''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : List[str] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def SCREAMING_SNAKE_CASE ( self : str ) -> int: a_ : str = self.get_tokenizer() a_ : int = '''Encode this sequence.''' a_ : List[str] = tokenizer.byte_encoder[''' '''.encode('''utf-8''' )[0]] # Testing encoder arguments a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) tokenizer.add_special_tokens({'''bos_token''': '''<s>'''} ) a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Testing spaces after special tokens a_ : Optional[Any] = '''<mask>''' tokenizer.add_special_tokens( {'''mask_token''': AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE )} ) # mask token has a left space a_ : Optional[int] = tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) a_ : List[Any] = '''Encode <mask> sequence''' a_ : List[str] = '''Encode <mask>sequence''' a_ : int = tokenizer.encode(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = encoded.index(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer.encode(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = encoded.index(__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Union[str, Any]: pass def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Any: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): a_ : Any = self.rust_tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) a_ : Any = self.tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) a_ : str = '''A, <mask> AllenNLP sentence.''' a_ : List[Any] = tokenizer_r.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer_p.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE ) # 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'''] ) , ) a_ : str = tokenizer_r.convert_ids_to_tokens(tokens_r['''input_ids'''] ) a_ : Tuple = 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, 250, 6, 5_0264, 3823, 487, 2_1992, 3645, 4, 2] ) self.assertSequenceEqual(tokens_r['''input_ids'''] , [0, 250, 6, 5_0264, 3823, 487, 2_1992, 3645, 4, 2] ) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>'''] ) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>'''] ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[Any]: for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): a_ : Any = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) a_ : str = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state['''add_prefix_space'''] , __SCREAMING_SNAKE_CASE ) self.assertEqual(post_processor_state['''add_prefix_space'''] , __SCREAMING_SNAKE_CASE ) self.assertEqual(post_processor_state['''trim_offsets'''] , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[int]: # 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})' ): a_ : Dict = '''hello''' # `hello` is a token in the vocabulary of `pretrained_name` a_ : Union[str, Any] = f'{text_of_1_token} {text_of_1_token}' a_ : Any = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ) + 1, len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Any = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ) + 1, len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : int = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ), len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Tuple = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ), len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Union[str, Any] = 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)), # ) a_ : str = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : int = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ) + 1, 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : int = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ), 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Optional[int] = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : int = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ), 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , )
719
'''simple docstring''' from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import pyarrow as pa if TYPE_CHECKING: from .features import FeatureType @dataclass class SCREAMING_SNAKE_CASE : snake_case__ = 42 snake_case__ = None # Automatically constructed snake_case__ = "dict" snake_case__ = None snake_case__ = field(default="Translation" , init=SCREAMING_SNAKE_CASE_ , repr=SCREAMING_SNAKE_CASE_ ) def __call__( self : Dict ) -> Tuple: return pa.struct({lang: pa.string() for lang in sorted(self.languages )} ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Value return {k: Value('''string''' ) for k in sorted(self.languages )} @dataclass class SCREAMING_SNAKE_CASE : snake_case__ = None snake_case__ = None snake_case__ = None # Automatically constructed snake_case__ = "dict" snake_case__ = None snake_case__ = field(default="TranslationVariableLanguages" , init=SCREAMING_SNAKE_CASE_ , repr=SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : List[str] = sorted(set(self.languages ) ) if self.languages else None a_ : Optional[Any] = len(self.languages ) if self.languages else None def __call__( self : Any ) -> Optional[Any]: return pa.struct({'''language''': pa.list_(pa.string() ), '''translation''': pa.list_(pa.string() )} ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Optional[Any]: a_ : str = set(self.languages ) if self.languages and set(__SCREAMING_SNAKE_CASE ) - lang_set: raise ValueError( f'Some languages in example ({", ".join(sorted(set(__SCREAMING_SNAKE_CASE ) - lang_set ) )}) are not in valid set ({", ".join(__SCREAMING_SNAKE_CASE )}).' ) # Convert dictionary into tuples, splitting out cases where there are # multiple translations for a single language. a_ : int = [] for lang, text in translation_dict.items(): if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): translation_tuples.append((lang, text) ) else: translation_tuples.extend([(lang, el) for el in text] ) # Ensure translations are in ascending order by language code. a_ , a_ : List[Any] = zip(*sorted(__SCREAMING_SNAKE_CASE ) ) return {"language": languages, "translation": translations} def SCREAMING_SNAKE_CASE ( self : Any ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Sequence, Value return { "language": Sequence(Value('''string''' ) ), "translation": Sequence(Value('''string''' ) ), }
666
0