code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
"""simple docstring""" def __lowerCAmelCase (_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): return round(float(moles / volume ) * nfactor ) def __lowerCAmelCase (_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): return round(float((moles * 0.0821 * temperature) / (volume) ) ) def __lowerCAmelCase (_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): return round(float((moles * 0.0821 * temperature) / (pressure) ) ) def __lowerCAmelCase (_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): return round(float((pressure * volume) / (0.0821 * moles) ) ) if __name__ == "__main__": import doctest doctest.testmod()
86
"""simple docstring""" from __future__ import annotations lowerCamelCase__ = list[tuple[int, int]] lowerCamelCase__ = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] lowerCamelCase__ = ([-1, 0], [0, -1], [1, 0], [0, 1]) # up, left, down, right class A__ : def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , ): __lowerCAmelCase : int = pos_x __lowerCAmelCase : Optional[Any] = pos_y __lowerCAmelCase : Optional[int] = (pos_y, pos_x) __lowerCAmelCase : Union[str, Any] = goal_x __lowerCAmelCase : Any = goal_y __lowerCAmelCase : Optional[Any] = g_cost __lowerCAmelCase : Any = parent __lowerCAmelCase : Union[str, Any] = self.calculate_heuristic() def __lowerCamelCase ( self ): __lowerCAmelCase : str = abs(self.pos_x - self.goal_x ) __lowerCAmelCase : str = abs(self.pos_y - self.goal_y ) return dx + dy def __lt__( self , _SCREAMING_SNAKE_CASE ): return self.f_cost < other.f_cost class A__ : def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Optional[int] = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , _SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Any = Node(goal[1] , goal[0] , goal[1] , goal[0] , 9_99_99 , _SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Tuple = [self.start] __lowerCAmelCase : list[Node] = [] __lowerCAmelCase : str = False def __lowerCamelCase ( self ): while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() __lowerCAmelCase : Optional[int] = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: __lowerCAmelCase : Union[str, Any] = True return self.retrace_path(_SCREAMING_SNAKE_CASE ) self.closed_nodes.append(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Optional[Any] = self.get_successors(_SCREAMING_SNAKE_CASE ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(_SCREAMING_SNAKE_CASE ) else: # retrieve the best current path __lowerCAmelCase : Optional[Any] = self.open_nodes.pop(self.open_nodes.index(_SCREAMING_SNAKE_CASE ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(_SCREAMING_SNAKE_CASE ) else: self.open_nodes.append(_SCREAMING_SNAKE_CASE ) if not self.reached: return [self.start.pos] return None def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Dict = [] for action in delta: __lowerCAmelCase : Optional[int] = parent.pos_x + action[1] __lowerCAmelCase : Union[str, Any] = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(_SCREAMING_SNAKE_CASE ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , _SCREAMING_SNAKE_CASE , ) ) return successors def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Union[str, Any] = node __lowerCAmelCase : Optional[int] = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) __lowerCAmelCase : int = current_node.parent path.reverse() return path if __name__ == "__main__": lowerCamelCase__ = (0, 0) lowerCamelCase__ = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) print("""------""") lowerCamelCase__ = GreedyBestFirst(init, goal) lowerCamelCase__ = greedy_bf.search() if path: for pos_x, pos_y in path: lowerCamelCase__ = 2 for elem in grid: print(elem)
86
1
import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DPMSolverMultistepScheduler, TextToVideoSDPipeline, UNetaDConditionModel, ) from diffusers.utils import is_xformers_available, load_numpy, skip_mps, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() @skip_mps class _UpperCamelCase ( SCREAMING_SNAKE_CASE__ , unittest.TestCase ): _UpperCamelCase : Optional[Any] = TextToVideoSDPipeline _UpperCamelCase : str = TEXT_TO_IMAGE_PARAMS _UpperCamelCase : List[str] = TEXT_TO_IMAGE_BATCH_PARAMS # No `output_type`. _UpperCamelCase : List[Any] = frozenset( [ '''num_inference_steps''', '''generator''', '''latents''', '''return_dict''', '''callback''', '''callback_steps''', ] ) def lowercase ( self: str ) -> List[Any]: """simple docstring""" torch.manual_seed(0 ) UpperCamelCase_ = UNetaDConditionModel( block_out_channels=(32, 64, 64, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "DownBlock3D") , up_block_types=("UpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D") , cross_attention_dim=32 , attention_head_dim=4 , ) UpperCamelCase_ = DDIMScheduler( beta_start=0.0_00_85 , beta_end=0.0_12 , beta_schedule="scaled_linear" , clip_sample=_SCREAMING_SNAKE_CASE , set_alpha_to_one=_SCREAMING_SNAKE_CASE , ) torch.manual_seed(0 ) UpperCamelCase_ = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"] , up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0 ) UpperCamelCase_ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , hidden_act="gelu" , projection_dim=512 , ) UpperCamelCase_ = CLIPTextModel(_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) UpperCamelCase_ = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, } return components def lowercase ( self: Optional[int] , _SCREAMING_SNAKE_CASE: str , _SCREAMING_SNAKE_CASE: int=0 ) -> Optional[Any]: """simple docstring""" if str(_SCREAMING_SNAKE_CASE ).startswith("mps" ): UpperCamelCase_ = torch.manual_seed(_SCREAMING_SNAKE_CASE ) else: UpperCamelCase_ = torch.Generator(device=_SCREAMING_SNAKE_CASE ).manual_seed(_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = { "prompt": "A painting of a squirrel eating a burger", "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, "output_type": "pt", } return inputs def lowercase ( self: Optional[int] ) -> int: """simple docstring""" UpperCamelCase_ = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCamelCase_ = self.get_dummy_components() UpperCamelCase_ = TextToVideoSDPipeline(**_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = sd_pipe.to(_SCREAMING_SNAKE_CASE ) sd_pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = self.get_dummy_inputs(_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = "np" UpperCamelCase_ = sd_pipe(**_SCREAMING_SNAKE_CASE ).frames UpperCamelCase_ = frames[0][-3:, -3:, -1] assert frames[0].shape == (64, 64, 3) UpperCamelCase_ = np.array([1_58.0, 1_60.0, 1_53.0, 1_25.0, 1_00.0, 1_21.0, 1_11.0, 93.0, 1_13.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def lowercase ( self: Tuple ) -> Tuple: """simple docstring""" self._test_attention_slicing_forward_pass(test_mean_pixel_difference=_SCREAMING_SNAKE_CASE , expected_max_diff=3e-3 ) @unittest.skipIf( torch_device != "cuda" or not is_xformers_available() , reason="XFormers attention is only available with CUDA and `xformers` installed" , ) def lowercase ( self: Tuple ) -> int: """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=_SCREAMING_SNAKE_CASE , expected_max_diff=1e-2 ) @unittest.skip(reason="Batching needs to be properly figured out first for this pipeline." ) def lowercase ( self: str ) -> Optional[int]: """simple docstring""" pass @unittest.skip(reason="Batching needs to be properly figured out first for this pipeline." ) def lowercase ( self: List[str] ) -> Dict: """simple docstring""" pass @unittest.skip(reason="`num_images_per_prompt` argument is not supported for this pipeline." ) def lowercase ( self: Tuple ) -> List[str]: """simple docstring""" pass def lowercase ( self: Tuple ) -> Any: """simple docstring""" return super().test_progress_bar() @slow @skip_mps class _UpperCamelCase ( unittest.TestCase ): def lowercase ( self: int ) -> Optional[Any]: """simple docstring""" UpperCamelCase_ = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video.npy" ) UpperCamelCase_ = TextToVideoSDPipeline.from_pretrained("damo-vilab/text-to-video-ms-1.7b" ) UpperCamelCase_ = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) UpperCamelCase_ = pipe.to("cuda" ) UpperCamelCase_ = "Spiderman is surfing" UpperCamelCase_ = torch.Generator(device="cpu" ).manual_seed(0 ) UpperCamelCase_ = pipe(_SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE , num_inference_steps=25 , output_type="pt" ).frames UpperCamelCase_ = video_frames.cpu().numpy() assert np.abs(expected_video - video ).mean() < 5e-2 def lowercase ( self: str ) -> Optional[Any]: """simple docstring""" UpperCamelCase_ = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video_2step.npy" ) UpperCamelCase_ = TextToVideoSDPipeline.from_pretrained("damo-vilab/text-to-video-ms-1.7b" ) UpperCamelCase_ = pipe.to("cuda" ) UpperCamelCase_ = "Spiderman is surfing" UpperCamelCase_ = torch.Generator(device="cpu" ).manual_seed(0 ) UpperCamelCase_ = pipe(_SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE , num_inference_steps=2 , output_type="pt" ).frames UpperCamelCase_ = video_frames.cpu().numpy() assert np.abs(expected_video - video ).mean() < 5e-2
364
import torch from torch import nn from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin class _UpperCamelCase ( lowerCAmelCase_ , lowerCAmelCase_ ): @register_to_config def __init__( self: List[str] , *, _SCREAMING_SNAKE_CASE: int = 4 , _SCREAMING_SNAKE_CASE: int = 768 , _SCREAMING_SNAKE_CASE: int , _SCREAMING_SNAKE_CASE: str , ) -> Tuple: """simple docstring""" super().__init__() UpperCamelCase_ = nn.Parameter(torch.zeros(_SCREAMING_SNAKE_CASE ) ) # parameters for additional clip time embeddings UpperCamelCase_ = nn.Linear(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCamelCase_ = nn.Linear(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # parameters for encoder hidden states UpperCamelCase_ = clip_extra_context_tokens UpperCamelCase_ = nn.Linear( _SCREAMING_SNAKE_CASE , self.clip_extra_context_tokens * cross_attention_dim ) UpperCamelCase_ = nn.Linear(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCamelCase_ = nn.LayerNorm(_SCREAMING_SNAKE_CASE ) def lowercase ( self: Optional[int] , *, _SCREAMING_SNAKE_CASE: str , _SCREAMING_SNAKE_CASE: List[str] , _SCREAMING_SNAKE_CASE: Optional[Any] , _SCREAMING_SNAKE_CASE: Tuple ) -> str: """simple docstring""" if do_classifier_free_guidance: # Add the classifier free guidance embeddings to the image embeddings UpperCamelCase_ = image_embeddings.shape[0] UpperCamelCase_ = self.learned_classifier_free_guidance_embeddings.unsqueeze(0 ) UpperCamelCase_ = classifier_free_guidance_embeddings.expand( _SCREAMING_SNAKE_CASE , -1 ) UpperCamelCase_ = torch.cat([classifier_free_guidance_embeddings, image_embeddings] , dim=0 ) # The image embeddings batch size and the text embeddings batch size are equal assert image_embeddings.shape[0] == prompt_embeds.shape[0] UpperCamelCase_ = prompt_embeds.shape[0] # "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and # adding CLIP embeddings to the existing timestep embedding, ... UpperCamelCase_ = self.embedding_proj(_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = self.clip_image_embeddings_project_to_time_embeddings(_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = time_projected_image_embeddings + time_projected_prompt_embeds # ... and by projecting CLIP embeddings into four # extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder" UpperCamelCase_ = self.clip_extra_context_tokens_proj(_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = clip_extra_context_tokens.reshape(_SCREAMING_SNAKE_CASE , -1 , self.clip_extra_context_tokens ) UpperCamelCase_ = clip_extra_context_tokens.permute(0 , 2 , 1 ) UpperCamelCase_ = self.encoder_hidden_states_proj(_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = self.text_encoder_hidden_states_norm(_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = torch.cat([clip_extra_context_tokens, text_encoder_hidden_states] , dim=1 ) return text_encoder_hidden_states, additive_clip_time_embeddings
328
0
from itertools import product def UpperCamelCase__( UpperCamelCase__ : int , UpperCamelCase__ : int )->list[int]: A__ = sides_number A__ = max_face_number * dice_number A__ = [0] * (max_total + 1) A__ = 1 A__ = range(UpperCamelCase__ , max_face_number + 1 ) for dice_numbers in product(UpperCamelCase__ , repeat=UpperCamelCase__ ): A__ = sum(UpperCamelCase__ ) totals_frequencies[total] += 1 return totals_frequencies def UpperCamelCase__( )->float: A__ = total_frequency_distribution( sides_number=4 , dice_number=9 ) A__ = total_frequency_distribution( sides_number=6 , dice_number=6 ) A__ = 0 A__ = 9 A__ = 4 * 9 A__ = 6 for peter_total in range(UpperCamelCase__ , max_peter_total + 1 ): peter_wins_count += peter_totals_frequencies[peter_total] * sum( colin_totals_frequencies[min_colin_total:peter_total] ) A__ = (4**9) * (6**6) A__ = peter_wins_count / total_games_number A__ = round(UpperCamelCase__ , ndigits=7 ) return rounded_peter_win_probability if __name__ == "__main__": print(F"{solution() = }")
193
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_bert import BertTokenizer a__: Optional[int] = logging.get_logger(__name__) a__: int = {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'} a__: Optional[Any] = { 'vocab_file': { 'bert-base-uncased': 'https://huggingface.co/bert-base-uncased/resolve/main/vocab.txt', 'bert-large-uncased': 'https://huggingface.co/bert-large-uncased/resolve/main/vocab.txt', 'bert-base-cased': 'https://huggingface.co/bert-base-cased/resolve/main/vocab.txt', 'bert-large-cased': 'https://huggingface.co/bert-large-cased/resolve/main/vocab.txt', 'bert-base-multilingual-uncased': ( 'https://huggingface.co/bert-base-multilingual-uncased/resolve/main/vocab.txt' ), 'bert-base-multilingual-cased': 'https://huggingface.co/bert-base-multilingual-cased/resolve/main/vocab.txt', 'bert-base-chinese': 'https://huggingface.co/bert-base-chinese/resolve/main/vocab.txt', 'bert-base-german-cased': 'https://huggingface.co/bert-base-german-cased/resolve/main/vocab.txt', 'bert-large-uncased-whole-word-masking': ( 'https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/vocab.txt' ), 'bert-large-cased-whole-word-masking': ( 'https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/vocab.txt' ), 'bert-large-uncased-whole-word-masking-finetuned-squad': ( 'https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt' ), 'bert-large-cased-whole-word-masking-finetuned-squad': ( 'https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt' ), 'bert-base-cased-finetuned-mrpc': ( 'https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/vocab.txt' ), 'bert-base-german-dbmdz-cased': 'https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/vocab.txt', 'bert-base-german-dbmdz-uncased': ( 'https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/vocab.txt' ), 'TurkuNLP/bert-base-finnish-cased-v1': ( 'https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/vocab.txt' ), 'TurkuNLP/bert-base-finnish-uncased-v1': ( 'https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/vocab.txt' ), 'wietsedv/bert-base-dutch-cased': ( 'https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'bert-base-uncased': 'https://huggingface.co/bert-base-uncased/resolve/main/tokenizer.json', 'bert-large-uncased': 'https://huggingface.co/bert-large-uncased/resolve/main/tokenizer.json', 'bert-base-cased': 'https://huggingface.co/bert-base-cased/resolve/main/tokenizer.json', 'bert-large-cased': 'https://huggingface.co/bert-large-cased/resolve/main/tokenizer.json', 'bert-base-multilingual-uncased': ( 'https://huggingface.co/bert-base-multilingual-uncased/resolve/main/tokenizer.json' ), 'bert-base-multilingual-cased': ( 'https://huggingface.co/bert-base-multilingual-cased/resolve/main/tokenizer.json' ), 'bert-base-chinese': 'https://huggingface.co/bert-base-chinese/resolve/main/tokenizer.json', 'bert-base-german-cased': 'https://huggingface.co/bert-base-german-cased/resolve/main/tokenizer.json', 'bert-large-uncased-whole-word-masking': ( 'https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/tokenizer.json' ), 'bert-large-cased-whole-word-masking': ( 'https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/tokenizer.json' ), 'bert-large-uncased-whole-word-masking-finetuned-squad': ( 'https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json' ), 'bert-large-cased-whole-word-masking-finetuned-squad': ( 'https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json' ), 'bert-base-cased-finetuned-mrpc': ( 'https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/tokenizer.json' ), 'bert-base-german-dbmdz-cased': ( 'https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/tokenizer.json' ), 'bert-base-german-dbmdz-uncased': ( 'https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/tokenizer.json' ), 'TurkuNLP/bert-base-finnish-cased-v1': ( 'https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/tokenizer.json' ), 'TurkuNLP/bert-base-finnish-uncased-v1': ( 'https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/tokenizer.json' ), 'wietsedv/bert-base-dutch-cased': ( 'https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/tokenizer.json' ), }, } a__: List[str] = { 'bert-base-uncased': 512, 'bert-large-uncased': 512, 'bert-base-cased': 512, 'bert-large-cased': 512, 'bert-base-multilingual-uncased': 512, 'bert-base-multilingual-cased': 512, 'bert-base-chinese': 512, 'bert-base-german-cased': 512, 'bert-large-uncased-whole-word-masking': 512, 'bert-large-cased-whole-word-masking': 512, 'bert-large-uncased-whole-word-masking-finetuned-squad': 512, 'bert-large-cased-whole-word-masking-finetuned-squad': 512, 'bert-base-cased-finetuned-mrpc': 512, 'bert-base-german-dbmdz-cased': 512, 'bert-base-german-dbmdz-uncased': 512, 'TurkuNLP/bert-base-finnish-cased-v1': 512, 'TurkuNLP/bert-base-finnish-uncased-v1': 512, 'wietsedv/bert-base-dutch-cased': 512, } a__: Optional[Any] = { 'bert-base-uncased': {'do_lower_case': True}, 'bert-large-uncased': {'do_lower_case': True}, 'bert-base-cased': {'do_lower_case': False}, 'bert-large-cased': {'do_lower_case': False}, 'bert-base-multilingual-uncased': {'do_lower_case': True}, 'bert-base-multilingual-cased': {'do_lower_case': False}, 'bert-base-chinese': {'do_lower_case': False}, 'bert-base-german-cased': {'do_lower_case': False}, 'bert-large-uncased-whole-word-masking': {'do_lower_case': True}, 'bert-large-cased-whole-word-masking': {'do_lower_case': False}, 'bert-large-uncased-whole-word-masking-finetuned-squad': {'do_lower_case': True}, 'bert-large-cased-whole-word-masking-finetuned-squad': {'do_lower_case': False}, 'bert-base-cased-finetuned-mrpc': {'do_lower_case': False}, 'bert-base-german-dbmdz-cased': {'do_lower_case': False}, 'bert-base-german-dbmdz-uncased': {'do_lower_case': True}, 'TurkuNLP/bert-base-finnish-cased-v1': {'do_lower_case': False}, 'TurkuNLP/bert-base-finnish-uncased-v1': {'do_lower_case': True}, 'wietsedv/bert-base-dutch-cased': {'do_lower_case': False}, } class SCREAMING_SNAKE_CASE__ ( UpperCamelCase__ ): __SCREAMING_SNAKE_CASE = VOCAB_FILES_NAMES __SCREAMING_SNAKE_CASE = PRETRAINED_VOCAB_FILES_MAP __SCREAMING_SNAKE_CASE = PRETRAINED_INIT_CONFIGURATION __SCREAMING_SNAKE_CASE = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __SCREAMING_SNAKE_CASE = BertTokenizer def __init__( self,__lowerCamelCase=None,__lowerCamelCase=None,__lowerCamelCase=True,__lowerCamelCase="[UNK]",__lowerCamelCase="[SEP]",__lowerCamelCase="[PAD]",__lowerCamelCase="[CLS]",__lowerCamelCase="[MASK]",__lowerCamelCase=True,__lowerCamelCase=None,**__lowerCamelCase,): super().__init__( __lowerCamelCase,tokenizer_file=__lowerCamelCase,do_lower_case=__lowerCamelCase,unk_token=__lowerCamelCase,sep_token=__lowerCamelCase,pad_token=__lowerCamelCase,cls_token=__lowerCamelCase,mask_token=__lowerCamelCase,tokenize_chinese_chars=__lowerCamelCase,strip_accents=__lowerCamelCase,**__lowerCamelCase,) A__ = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('''lowercase''',__lowerCamelCase ) != do_lower_case or normalizer_state.get('''strip_accents''',__lowerCamelCase ) != strip_accents or normalizer_state.get('''handle_chinese_chars''',__lowerCamelCase ) != tokenize_chinese_chars ): A__ = getattr(__lowerCamelCase,normalizer_state.pop('''type''' ) ) A__ = do_lower_case A__ = strip_accents A__ = tokenize_chinese_chars A__ = normalizer_class(**__lowerCamelCase ) A__ = do_lower_case def UpperCamelCase ( self,__lowerCamelCase,__lowerCamelCase=None ): A__ = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def UpperCamelCase ( self,__lowerCamelCase,__lowerCamelCase = None ): A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def UpperCamelCase ( self,__lowerCamelCase,__lowerCamelCase = None ): A__ = self._tokenizer.model.save(__lowerCamelCase,name=__lowerCamelCase ) return tuple(__lowerCamelCase )
193
1
"""simple docstring""" from __future__ import annotations import matplotlib.pyplot as plt # type: ignore import numpy # initial triangle of Koch snowflake _lowerCAmelCase : Optional[int] = numpy.array([0, 0]) _lowerCAmelCase : List[Any] = numpy.array([0.5, 0.8_660_254]) _lowerCAmelCase : Any = numpy.array([1, 0]) _lowerCAmelCase : Optional[int] = [VECTOR_1, VECTOR_2, VECTOR_3, VECTOR_1] def lowerCamelCase_( _lowerCamelCase , _lowerCamelCase ) -> list[numpy.ndarray]: '''simple docstring''' _lowerCamelCase : Union[str, Any] = initial_vectors for _ in range(lowercase__ ): _lowerCamelCase : List[Any] = iteration_step(lowercase__ ) return vectors def lowerCamelCase_( _lowerCamelCase ) -> list[numpy.ndarray]: '''simple docstring''' _lowerCamelCase : Tuple = [] for i, start_vector in enumerate(vectors[:-1] ): _lowerCamelCase : List[Any] = vectors[i + 1] new_vectors.append(lowercase__ ) _lowerCamelCase : str = end_vector - start_vector new_vectors.append(start_vector + difference_vector / 3 ) new_vectors.append( start_vector + difference_vector / 3 + rotate(difference_vector / 3 , 60 ) ) new_vectors.append(start_vector + difference_vector * 2 / 3 ) new_vectors.append(vectors[-1] ) return new_vectors def lowerCamelCase_( _lowerCamelCase , _lowerCamelCase ) -> numpy.ndarray: '''simple docstring''' _lowerCamelCase : Union[str, Any] = numpy.radians(lowercase__ ) _lowerCamelCase : Tuple = numpy.cos(lowercase__ ), numpy.sin(lowercase__ ) _lowerCamelCase : Tuple = numpy.array(((c, -s), (s, c)) ) return numpy.dot(lowercase__ , lowercase__ ) def lowerCamelCase_( _lowerCamelCase ) -> None: '''simple docstring''' _lowerCamelCase : Tuple = plt.gca() axes.set_aspect("equal" ) # matplotlib.pyplot.plot takes a list of all x-coordinates and a list of all # y-coordinates as inputs, which are constructed from the vector-list using # zip() _lowerCamelCase : Any = zip(*lowercase__ ) plt.plot(lowercase__ , lowercase__ ) plt.show() if __name__ == "__main__": import doctest doctest.testmod() _lowerCAmelCase : Any = iterate(INITIAL_VECTORS, 5) plot(processed_vectors)
351
"""simple docstring""" import time from contextlib import contextmanager from pathlib import Path import pytest import requests from huggingface_hub.hf_api import HfApi, HfFolder _lowerCAmelCase : Optional[int] = '''__DUMMY_TRANSFORMERS_USER__''' _lowerCAmelCase : Dict = '''Dummy User''' _lowerCAmelCase : Optional[int] = '''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''' _lowerCAmelCase : Tuple = '''https://hub-ci.huggingface.co''' _lowerCAmelCase : Any = CI_HUB_ENDPOINT + '''/datasets/{repo_id}/resolve/{revision}/{path}''' _lowerCAmelCase : Tuple = CI_HUB_ENDPOINT + '''/{repo_id}/resolve/{revision}/{filename}''' _lowerCAmelCase : Dict = Path('''~/.huggingface/hub_ci_token''').expanduser() @pytest.fixture def lowerCamelCase_( _lowerCamelCase ) -> Optional[Any]: '''simple docstring''' monkeypatch.setattr( "huggingface_hub.file_download.HUGGINGFACE_CO_URL_TEMPLATE" , _lowerCamelCase ) @pytest.fixture def lowerCamelCase_( _lowerCamelCase ) -> Tuple: '''simple docstring''' monkeypatch.setattr("datasets.config.HF_ENDPOINT" , _lowerCamelCase ) monkeypatch.setattr("datasets.config.HUB_DATASETS_URL" , _lowerCamelCase ) @pytest.fixture def lowerCamelCase_( _lowerCamelCase ) -> Optional[Any]: '''simple docstring''' monkeypatch.setattr("huggingface_hub.hf_api.HfFolder.path_token" , _lowerCamelCase ) @pytest.fixture def lowerCamelCase_( _lowerCamelCase , _lowerCamelCase ) -> Dict: '''simple docstring''' HfFolder.save_token(_lowerCamelCase ) yield HfFolder.delete_token() @pytest.fixture(scope="session" ) def lowerCamelCase_( ) -> str: '''simple docstring''' return HfApi(endpoint=_lowerCamelCase ) @pytest.fixture(scope="session" ) def lowerCamelCase_( _lowerCamelCase ) -> int: '''simple docstring''' _lowerCamelCase : Dict = HfFolder.get_token() HfFolder.save_token(_lowerCamelCase ) yield CI_HUB_USER_TOKEN if previous_token is not None: HfFolder.save_token(_lowerCamelCase ) @pytest.fixture def lowerCamelCase_( _lowerCamelCase ) -> Optional[Any]: '''simple docstring''' def _cleanup_repo(_lowerCamelCase ): hf_api.delete_repo(_lowerCamelCase , token=_lowerCamelCase , repo_type="dataset" ) return _cleanup_repo @pytest.fixture def lowerCamelCase_( _lowerCamelCase ) -> List[str]: '''simple docstring''' @contextmanager def _temporary_repo(_lowerCamelCase ): try: yield repo_id finally: cleanup_repo(_lowerCamelCase ) return _temporary_repo @pytest.fixture(scope="session" ) def lowerCamelCase_( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> Optional[Any]: '''simple docstring''' _lowerCamelCase : Tuple = F"""repo_txt_data-{int(time.time() * 10e3 )}""" _lowerCamelCase : List[str] = F"""{CI_HUB_USER}/{repo_name}""" hf_api.create_repo(_lowerCamelCase , token=_lowerCamelCase , repo_type="dataset" , private=_lowerCamelCase ) hf_api.upload_file( token=_lowerCamelCase , path_or_fileobj=str(_lowerCamelCase ) , path_in_repo="data/text_data.txt" , repo_id=_lowerCamelCase , repo_type="dataset" , ) yield repo_id try: hf_api.delete_repo(_lowerCamelCase , token=_lowerCamelCase , repo_type="dataset" ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def lowerCamelCase_( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> str: '''simple docstring''' return hf_private_dataset_repo_txt_data_ @pytest.fixture(scope="session" ) def lowerCamelCase_( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> str: '''simple docstring''' _lowerCamelCase : List[Any] = F"""repo_zipped_txt_data-{int(time.time() * 10e3 )}""" _lowerCamelCase : Dict = F"""{CI_HUB_USER}/{repo_name}""" hf_api.create_repo(_lowerCamelCase , token=_lowerCamelCase , repo_type="dataset" , private=_lowerCamelCase ) hf_api.upload_file( token=_lowerCamelCase , path_or_fileobj=str(_lowerCamelCase ) , path_in_repo="data.zip" , repo_id=_lowerCamelCase , repo_type="dataset" , ) yield repo_id try: hf_api.delete_repo(_lowerCamelCase , token=_lowerCamelCase , repo_type="dataset" ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def lowerCamelCase_( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> Dict: '''simple docstring''' return hf_private_dataset_repo_zipped_txt_data_ @pytest.fixture(scope="session" ) def lowerCamelCase_( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> List[Any]: '''simple docstring''' _lowerCamelCase : Any = F"""repo_zipped_img_data-{int(time.time() * 10e3 )}""" _lowerCamelCase : List[Any] = F"""{CI_HUB_USER}/{repo_name}""" hf_api.create_repo(_lowerCamelCase , token=_lowerCamelCase , repo_type="dataset" , private=_lowerCamelCase ) hf_api.upload_file( token=_lowerCamelCase , path_or_fileobj=str(_lowerCamelCase ) , path_in_repo="data.zip" , repo_id=_lowerCamelCase , repo_type="dataset" , ) yield repo_id try: hf_api.delete_repo(_lowerCamelCase , token=_lowerCamelCase , repo_type="dataset" ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def lowerCamelCase_( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> Optional[Any]: '''simple docstring''' return hf_private_dataset_repo_zipped_img_data_
340
0
"""simple docstring""" from __future__ import annotations def lowercase_ ( _snake_case ,_snake_case ): if len(_snake_case ) == 0: return False SCREAMING_SNAKE_CASE__ : List[str] = len(_snake_case ) // 2 if a_list[midpoint] == item: return True if item < a_list[midpoint]: return binary_search(a_list[:midpoint] ,_snake_case ) else: return binary_search(a_list[midpoint + 1 :] ,_snake_case ) if __name__ == "__main__": UpperCAmelCase__ : Optional[int] = input('Enter numbers separated by comma:\n').strip() UpperCAmelCase__ : Union[str, Any] = [int(item.strip()) for item in user_input.split(',')] UpperCAmelCase__ : List[str] = int(input('Enter the number to be found in the list:\n').strip()) UpperCAmelCase__ : Optional[int] = '' if binary_search(sequence, target) else 'not ' print(f"""{target} was {not_str}found in {sequence}""")
25
from bisect import bisect from itertools import accumulate def __magic_name__ ( A : Optional[Any], A : List[str], A : Tuple, A : Optional[Any] ): '''simple docstring''' a = sorted(zip(A, A ), key=lambda A : x[0] / x[1], reverse=A ) a , a = [i[0] for i in r], [i[1] for i in r] a = list(accumulate(A ) ) a = bisect(A, A ) return ( 0 if k == 0 else sum(vl[:k] ) + (w - acc[k - 1]) * (vl[k]) / (wt[k]) if k != n else sum(vl[:k] ) ) if __name__ == "__main__": import doctest doctest.testmod()
107
0
'''simple docstring''' import json import os import unittest from transformers import BatchEncoding, MvpTokenizer, MvpTokenizerFast from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, require_torch from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin, filter_roberta_detectors @require_tokenizers class _SCREAMING_SNAKE_CASE ( __a ,unittest.TestCase ): __SCREAMING_SNAKE_CASE :List[str] = MvpTokenizer __SCREAMING_SNAKE_CASE :List[str] = MvpTokenizerFast __SCREAMING_SNAKE_CASE :Union[str, Any] = True __SCREAMING_SNAKE_CASE :List[Any] = filter_roberta_detectors def snake_case__ ( self : List[str] ): super().setUp() __magic_name__ = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] __magic_name__ = dict(zip(a__ , range(len(a__ ) ) ) ) __magic_name__ = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] __magic_name__ = {'''unk_token''': '''<unk>'''} __magic_name__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) __magic_name__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(a__ ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(a__ ) ) def snake_case__ ( self : int , **a__ : Tuple ): kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **a__ ) def snake_case__ ( self : Optional[Any] , **a__ : Optional[Any] ): kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **a__ ) def snake_case__ ( self : Dict , a__ : Tuple ): return "lower newer", "lower newer" @cached_property def snake_case__ ( self : Dict ): return MvpTokenizer.from_pretrained('''RUCAIBox/mvp''' ) @cached_property def snake_case__ ( self : Any ): return MvpTokenizerFast.from_pretrained('''RUCAIBox/mvp''' ) @require_torch def snake_case__ ( self : List[str] ): __magic_name__ = ['''A long paragraph for summarization.''', '''Another paragraph for summarization.'''] __magic_name__ = [0, 250, 251, 1_7818, 13, 3_9186, 1938, 4, 2] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: __magic_name__ = tokenizer(a__ , max_length=len(a__ ) , padding=a__ , return_tensors='''pt''' ) self.assertIsInstance(a__ , a__ ) self.assertEqual((2, 9) , batch.input_ids.shape ) self.assertEqual((2, 9) , batch.attention_mask.shape ) __magic_name__ = batch.input_ids.tolist()[0] self.assertListEqual(a__ , a__ ) # Test that special tokens are reset @require_torch def snake_case__ ( self : Any ): __magic_name__ = ['''A long paragraph for summarization.''', '''Another paragraph for summarization.'''] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: __magic_name__ = tokenizer(a__ , padding=a__ , return_tensors='''pt''' ) # check if input_ids are returned and no labels self.assertIn('''input_ids''' , a__ ) self.assertIn('''attention_mask''' , a__ ) self.assertNotIn('''labels''' , a__ ) self.assertNotIn('''decoder_attention_mask''' , a__ ) @require_torch def snake_case__ ( self : Optional[int] ): __magic_name__ = [ '''Summary of the text.''', '''Another summary.''', ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: __magic_name__ = tokenizer(text_target=a__ , max_length=32 , padding='''max_length''' , return_tensors='''pt''' ) self.assertEqual(32 , targets['''input_ids'''].shape[1] ) @require_torch def snake_case__ ( self : str ): for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: __magic_name__ = tokenizer( ['''I am a small frog''' * 1024, '''I am a small frog'''] , padding=a__ , truncation=a__ , return_tensors='''pt''' ) self.assertIsInstance(a__ , a__ ) self.assertEqual(batch.input_ids.shape , (2, 1024) ) @require_torch def snake_case__ ( self : Optional[Any] ): __magic_name__ = ['''A long paragraph for summarization.'''] __magic_name__ = [ '''Summary of the text.''', ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: __magic_name__ = tokenizer(a__ , text_target=a__ , return_tensors='''pt''' ) __magic_name__ = inputs['''input_ids'''] __magic_name__ = inputs['''labels'''] self.assertTrue((input_ids[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((labels[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((input_ids[:, -1] == tokenizer.eos_token_id).all().item() ) self.assertTrue((labels[:, -1] == tokenizer.eos_token_id).all().item() ) def snake_case__ ( self : Tuple ): pass def snake_case__ ( self : str ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): __magic_name__ = self.rust_tokenizer_class.from_pretrained(a__ , **a__ ) __magic_name__ = self.tokenizer_class.from_pretrained(a__ , **a__ ) __magic_name__ = '''A, <mask> AllenNLP sentence.''' __magic_name__ = tokenizer_r.encode_plus(a__ , add_special_tokens=a__ , return_token_type_ids=a__ ) __magic_name__ = tokenizer_p.encode_plus(a__ , add_special_tokens=a__ , return_token_type_ids=a__ ) # 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'''] ) , ) __magic_name__ = tokenizer_r.convert_ids_to_tokens(tokens_r['''input_ids'''] ) __magic_name__ = 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( a__ , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>'''] ) self.assertSequenceEqual( a__ , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>'''] )
98
'''simple docstring''' import importlib import inspect import json import os import re import shutil import sys from pathlib import Path from typing import Dict, Optional, Union from urllib import request from huggingface_hub import HfFolder, cached_download, hf_hub_download, model_info from packaging import version from .. import __version__ from . import DIFFUSERS_DYNAMIC_MODULE_NAME, HF_MODULES_CACHE, logging _lowerCAmelCase = ( "https://raw.githubusercontent.com/huggingface/diffusers/{revision}/examples/community/{pipeline}.py" ) _lowerCAmelCase = logging.get_logger(__name__) # pylint: disable=invalid-name def UpperCamelCase ( ) -> Optional[Any]: '''simple docstring''' __magic_name__ = '''https://pypi.org/pypi/diffusers/json''' __magic_name__ = json.loads(request.urlopen(a ).read() )['''releases'''].keys() return sorted(a , key=lambda a : version.Version(a ) ) def UpperCamelCase ( ) -> Any: '''simple docstring''' # This function has already been executed if HF_MODULES_CACHE already is in the Python path. if HF_MODULES_CACHE in sys.path: return sys.path.append(a ) os.makedirs(a , exist_ok=a ) __magic_name__ = Path(a ) / '''__init__.py''' if not init_path.exists(): init_path.touch() def UpperCamelCase ( a ) -> Tuple: '''simple docstring''' init_hf_modules() __magic_name__ = Path(a ) / name # If the parent module does not exist yet, recursively create it. if not dynamic_module_path.parent.exists(): create_dynamic_module(dynamic_module_path.parent ) os.makedirs(a , exist_ok=a ) __magic_name__ = dynamic_module_path / '''__init__.py''' if not init_path.exists(): init_path.touch() def UpperCamelCase ( a ) -> List[str]: '''simple docstring''' with open(a , '''r''' , encoding='''utf-8''' ) as f: __magic_name__ = f.read() # Imports of the form `import .xxx` __magic_name__ = re.findall('''^\s*import\s+\.(\S+)\s*$''' , a , flags=re.MULTILINE ) # Imports of the form `from .xxx import yyy` relative_imports += re.findall('''^\s*from\s+\.(\S+)\s+import''' , a , flags=re.MULTILINE ) # Unique-ify return list(set(a ) ) def UpperCamelCase ( a ) -> str: '''simple docstring''' __magic_name__ = False __magic_name__ = [module_file] __magic_name__ = [] # Let's recurse through all relative imports while not no_change: __magic_name__ = [] for f in files_to_check: new_imports.extend(get_relative_imports(a ) ) __magic_name__ = Path(a ).parent __magic_name__ = [str(module_path / m ) for m in new_imports] __magic_name__ = [f for f in new_import_files if f not in all_relative_imports] __magic_name__ = [F'''{f}.py''' for f in new_import_files] __magic_name__ = len(a ) == 0 all_relative_imports.extend(a ) return all_relative_imports def UpperCamelCase ( a ) -> List[str]: '''simple docstring''' with open(a , '''r''' , encoding='''utf-8''' ) as f: __magic_name__ = f.read() # Imports of the form `import xxx` __magic_name__ = re.findall('''^\s*import\s+(\S+)\s*$''' , a , flags=re.MULTILINE ) # Imports of the form `from xxx import yyy` imports += re.findall('''^\s*from\s+(\S+)\s+import''' , a , flags=re.MULTILINE ) # Only keep the top-level module __magic_name__ = [imp.split('''.''' )[0] for imp in imports if not imp.startswith('''.''' )] # Unique-ify and test we got them all __magic_name__ = list(set(a ) ) __magic_name__ = [] for imp in imports: try: importlib.import_module(a ) except ImportError: missing_packages.append(a ) if len(a ) > 0: raise ImportError( '''This modeling file requires the following packages that were not found in your environment: ''' F'''{", ".join(a )}. Run `pip install {" ".join(a )}`''' ) return get_relative_imports(a ) def UpperCamelCase ( a , a ) -> Optional[int]: '''simple docstring''' __magic_name__ = module_path.replace(os.path.sep , '''.''' ) __magic_name__ = importlib.import_module(a ) if class_name is None: return find_pipeline_class(a ) return getattr(a , a ) def UpperCamelCase ( a ) -> Tuple: '''simple docstring''' from ..pipelines import DiffusionPipeline __magic_name__ = dict(inspect.getmembers(a , inspect.isclass ) ) __magic_name__ = None for cls_name, cls in cls_members.items(): if ( cls_name != DiffusionPipeline.__name__ and issubclass(cls , a ) and cls.__module__.split('''.''' )[0] != "diffusers" ): if pipeline_class is not None: raise ValueError( F'''Multiple classes that inherit from {DiffusionPipeline.__name__} have been found:''' F''' {pipeline_class.__name__}, and {cls_name}. Please make sure to define only one in''' F''' {loaded_module}.''' ) __magic_name__ = cls return pipeline_class def UpperCamelCase ( a , a , a = None , a = False , a = False , a = None , a = None , a = None , a = False , ) -> List[Any]: '''simple docstring''' __magic_name__ = str(a ) __magic_name__ = os.path.join(a , a ) if os.path.isfile(a ): __magic_name__ = module_file_or_url __magic_name__ = '''local''' elif pretrained_model_name_or_path.count('''/''' ) == 0: __magic_name__ = get_diffusers_versions() # cut ".dev0" __magic_name__ = '''v''' + '''.'''.join(__version__.split('''.''' )[:3] ) # retrieve github version that matches if revision is None: __magic_name__ = latest_version if latest_version[1:] in available_versions else '''main''' logger.info(F'''Defaulting to latest_version: {revision}.''' ) elif revision in available_versions: __magic_name__ = F'''v{revision}''' elif revision == "main": __magic_name__ = revision else: raise ValueError( F'''`custom_revision`: {revision} does not exist. Please make sure to choose one of''' F''' {", ".join(available_versions + ["main"] )}.''' ) # community pipeline on GitHub __magic_name__ = COMMUNITY_PIPELINES_URL.format(revision=a , pipeline=a ) try: __magic_name__ = cached_download( a , cache_dir=a , force_download=a , proxies=a , resume_download=a , local_files_only=a , use_auth_token=a , ) __magic_name__ = '''git''' __magic_name__ = pretrained_model_name_or_path + '''.py''' except EnvironmentError: logger.error(F'''Could not locate the {module_file} inside {pretrained_model_name_or_path}.''' ) raise else: try: # Load from URL or cache if already cached __magic_name__ = hf_hub_download( a , a , cache_dir=a , force_download=a , proxies=a , resume_download=a , local_files_only=a , use_auth_token=a , ) __magic_name__ = os.path.join('''local''' , '''--'''.join(pretrained_model_name_or_path.split('''/''' ) ) ) except EnvironmentError: logger.error(F'''Could not locate the {module_file} inside {pretrained_model_name_or_path}.''' ) raise # Check we have all the requirements in our environment __magic_name__ = check_imports(a ) # Now we move the module inside our cached dynamic modules. __magic_name__ = DIFFUSERS_DYNAMIC_MODULE_NAME + os.path.sep + submodule create_dynamic_module(a ) __magic_name__ = Path(a ) / full_submodule if submodule == "local" or submodule == "git": # We always copy local files (we could hash the file to see if there was a change, and give them the name of # that hash, to only copy when there is a modification but it seems overkill for now). # The only reason we do the copy is to avoid putting too many folders in sys.path. shutil.copy(a , submodule_path / module_file ) for module_needed in modules_needed: __magic_name__ = F'''{module_needed}.py''' shutil.copy(os.path.join(a , a ) , submodule_path / module_needed ) else: # Get the commit hash # TODO: we will get this info in the etag soon, so retrieve it from there and not here. if isinstance(a , a ): __magic_name__ = use_auth_token elif use_auth_token is True: __magic_name__ = HfFolder.get_token() else: __magic_name__ = None __magic_name__ = model_info(a , revision=a , token=a ).sha # The module file will end up being placed in a subfolder with the git hash of the repo. This way we get the # benefit of versioning. __magic_name__ = submodule_path / commit_hash __magic_name__ = full_submodule + os.path.sep + commit_hash create_dynamic_module(a ) if not (submodule_path / module_file).exists(): shutil.copy(a , submodule_path / module_file ) # Make sure we also have every file with relative for module_needed in modules_needed: if not (submodule_path / module_needed).exists(): get_cached_module_file( a , F'''{module_needed}.py''' , cache_dir=a , force_download=a , resume_download=a , proxies=a , use_auth_token=a , revision=a , local_files_only=a , ) return os.path.join(a , a ) def UpperCamelCase ( a , a , a = None , a = None , a = False , a = False , a = None , a = None , a = None , a = False , **a , ) -> List[Any]: '''simple docstring''' __magic_name__ = get_cached_module_file( a , a , cache_dir=a , force_download=a , resume_download=a , proxies=a , use_auth_token=a , revision=a , local_files_only=a , ) return get_class_in_module(a , final_module.replace('''.py''' , '''''' ) )
98
1
import os import sys import unittest lowerCAmelCase_ = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, '''utils''')) import get_test_info # noqa: E402 from get_test_info import ( # noqa: E402 get_model_to_test_mapping, get_model_to_tester_mapping, get_test_to_tester_mapping, ) lowerCAmelCase_ = os.path.join('''tests''', '''models''', '''bert''', '''test_modeling_bert.py''') lowerCAmelCase_ = os.path.join('''tests''', '''models''', '''blip''', '''test_modeling_blip.py''') class __lowerCAmelCase ( unittest.TestCase ): def lowerCamelCase (self ) -> Optional[int]: '''simple docstring''' snake_case_ : str = get_test_to_tester_mapping(__magic_name__ ) snake_case_ : List[str] = get_test_to_tester_mapping(__magic_name__ ) snake_case_ : List[Any] = {'''BertModelTest''': '''BertModelTester'''} snake_case_ : List[str] = { '''BlipModelTest''': '''BlipModelTester''', '''BlipTextImageModelTest''': '''BlipTextImageModelsModelTester''', '''BlipTextModelTest''': '''BlipTextModelTester''', '''BlipTextRetrievalModelTest''': '''BlipTextRetrievalModelTester''', '''BlipVQAModelTest''': '''BlipVQAModelTester''', '''BlipVisionModelTest''': '''BlipVisionModelTester''', } self.assertEqual(get_test_info.to_json(__magic_name__ ) , __magic_name__ ) self.assertEqual(get_test_info.to_json(__magic_name__ ) , __magic_name__ ) def lowerCamelCase (self ) -> List[str]: '''simple docstring''' snake_case_ : Union[str, Any] = get_model_to_test_mapping(__magic_name__ ) snake_case_ : Any = get_model_to_test_mapping(__magic_name__ ) snake_case_ : Optional[int] = { '''BertForMaskedLM''': ['''BertModelTest'''], '''BertForMultipleChoice''': ['''BertModelTest'''], '''BertForNextSentencePrediction''': ['''BertModelTest'''], '''BertForPreTraining''': ['''BertModelTest'''], '''BertForQuestionAnswering''': ['''BertModelTest'''], '''BertForSequenceClassification''': ['''BertModelTest'''], '''BertForTokenClassification''': ['''BertModelTest'''], '''BertLMHeadModel''': ['''BertModelTest'''], '''BertModel''': ['''BertModelTest'''], } snake_case_ : Any = { '''BlipForConditionalGeneration''': ['''BlipTextImageModelTest'''], '''BlipForImageTextRetrieval''': ['''BlipTextRetrievalModelTest'''], '''BlipForQuestionAnswering''': ['''BlipVQAModelTest'''], '''BlipModel''': ['''BlipModelTest'''], '''BlipTextModel''': ['''BlipTextModelTest'''], '''BlipVisionModel''': ['''BlipVisionModelTest'''], } self.assertEqual(get_test_info.to_json(__magic_name__ ) , __magic_name__ ) self.assertEqual(get_test_info.to_json(__magic_name__ ) , __magic_name__ ) def lowerCamelCase (self ) -> Tuple: '''simple docstring''' snake_case_ : Tuple = get_model_to_tester_mapping(__magic_name__ ) snake_case_ : Dict = get_model_to_tester_mapping(__magic_name__ ) snake_case_ : Union[str, Any] = { '''BertForMaskedLM''': ['''BertModelTester'''], '''BertForMultipleChoice''': ['''BertModelTester'''], '''BertForNextSentencePrediction''': ['''BertModelTester'''], '''BertForPreTraining''': ['''BertModelTester'''], '''BertForQuestionAnswering''': ['''BertModelTester'''], '''BertForSequenceClassification''': ['''BertModelTester'''], '''BertForTokenClassification''': ['''BertModelTester'''], '''BertLMHeadModel''': ['''BertModelTester'''], '''BertModel''': ['''BertModelTester'''], } snake_case_ : List[Any] = { '''BlipForConditionalGeneration''': ['''BlipTextImageModelsModelTester'''], '''BlipForImageTextRetrieval''': ['''BlipTextRetrievalModelTester'''], '''BlipForQuestionAnswering''': ['''BlipVQAModelTester'''], '''BlipModel''': ['''BlipModelTester'''], '''BlipTextModel''': ['''BlipTextModelTester'''], '''BlipVisionModel''': ['''BlipVisionModelTester'''], } self.assertEqual(get_test_info.to_json(__magic_name__ ) , __magic_name__ ) self.assertEqual(get_test_info.to_json(__magic_name__ ) , __magic_name__ )
279
import argparse import hashlib import os import urllib import warnings import torch from torch import nn from tqdm import tqdm from transformers import WhisperConfig, WhisperForConditionalGeneration lowerCAmelCase_ = { '''tiny.en''': '''https://openaipublic.azureedge.net/main/whisper/models/d3dd57d32accea0b295c96e26691aa14d8822fac7d9d27d5dc00b4ca2826dd03/tiny.en.pt''', '''tiny''': '''https://openaipublic.azureedge.net/main/whisper/models/65147644a518d12f04e32d6f3b26facc3f8dd46e5390956a9424a650c0ce22b9/tiny.pt''', '''base.en''': '''https://openaipublic.azureedge.net/main/whisper/models/25a8566e1d0c1e2231d1c762132cd20e0f96a85d16145c3a00adf5d1ac670ead/base.en.pt''', '''base''': '''https://openaipublic.azureedge.net/main/whisper/models/ed3a0b6b1c0edf879ad9b11b1af5a0e6ab5db9205f891f668f8b0e6c6326e34e/base.pt''', '''small.en''': '''https://openaipublic.azureedge.net/main/whisper/models/f953ad0fd29cacd07d5a9eda5624af0f6bcf2258be67c92b79389873d91e0872/small.en.pt''', '''small''': '''https://openaipublic.azureedge.net/main/whisper/models/9ecf779972d90ba49c06d968637d720dd632c55bbf19d441fb42bf17a411e794/small.pt''', '''medium.en''': '''https://openaipublic.azureedge.net/main/whisper/models/d7440d1dc186f76616474e0ff0b3b6b879abc9d1a4926b7adfa41db2d497ab4f/medium.en.pt''', '''medium''': '''https://openaipublic.azureedge.net/main/whisper/models/345ae4da62f9b3d59415adc60127b97c714f32e89e936602e85993674d08dcb1/medium.pt''', '''large''': '''https://openaipublic.azureedge.net/main/whisper/models/e4b87e7e0bf463eb8e6956e646f1e277e901512310def2c24bf0e11bd3c28e9a/large.pt''', '''large-v2''': '''https://openaipublic.azureedge.net/main/whisper/models/81f7c96c852ee8fc832187b0132e569d6c3065a3252ed18e56effd0b6a73e524/large-v2.pt''', } def lowerCamelCase_ ( _UpperCamelCase ) -> List[Any]: """simple docstring""" snake_case_ : List[str] = ['''layers''', '''blocks'''] for k in ignore_keys: state_dict.pop(_UpperCamelCase , _UpperCamelCase ) lowerCAmelCase_ = { '''blocks''': '''layers''', '''mlp.0''': '''fc1''', '''mlp.2''': '''fc2''', '''mlp_ln''': '''final_layer_norm''', '''.attn.query''': '''.self_attn.q_proj''', '''.attn.key''': '''.self_attn.k_proj''', '''.attn.value''': '''.self_attn.v_proj''', '''.attn_ln''': '''.self_attn_layer_norm''', '''.attn.out''': '''.self_attn.out_proj''', '''.cross_attn.query''': '''.encoder_attn.q_proj''', '''.cross_attn.key''': '''.encoder_attn.k_proj''', '''.cross_attn.value''': '''.encoder_attn.v_proj''', '''.cross_attn_ln''': '''.encoder_attn_layer_norm''', '''.cross_attn.out''': '''.encoder_attn.out_proj''', '''decoder.ln.''': '''decoder.layer_norm.''', '''encoder.ln.''': '''encoder.layer_norm.''', '''token_embedding''': '''embed_tokens''', '''encoder.positional_embedding''': '''encoder.embed_positions.weight''', '''decoder.positional_embedding''': '''decoder.embed_positions.weight''', '''ln_post''': '''layer_norm''', } def lowerCamelCase_ ( _UpperCamelCase ) -> int: """simple docstring""" snake_case_ : str = list(s_dict.keys() ) for key in keys: snake_case_ : Optional[int] = key for k, v in WHISPER_MAPPING.items(): if k in key: snake_case_ : List[str] = new_key.replace(_UpperCamelCase , _UpperCamelCase ) print(f'''{key} -> {new_key}''' ) snake_case_ : Tuple = s_dict.pop(_UpperCamelCase ) return s_dict def lowerCamelCase_ ( _UpperCamelCase ) -> int: """simple docstring""" snake_case_ , snake_case_ : Dict = emb.weight.shape snake_case_ : Tuple = nn.Linear(_UpperCamelCase , _UpperCamelCase , bias=_UpperCamelCase ) snake_case_ : Any = emb.weight.data return lin_layer def lowerCamelCase_ ( _UpperCamelCase , _UpperCamelCase ) -> bytes: """simple docstring""" os.makedirs(_UpperCamelCase , exist_ok=_UpperCamelCase ) snake_case_ : List[Any] = os.path.basename(_UpperCamelCase ) snake_case_ : Any = url.split('''/''' )[-2] snake_case_ : str = os.path.join(_UpperCamelCase , _UpperCamelCase ) if os.path.exists(_UpperCamelCase ) and not os.path.isfile(_UpperCamelCase ): raise RuntimeError(f'''{download_target} exists and is not a regular file''' ) if os.path.isfile(_UpperCamelCase ): snake_case_ : Union[str, Any] = open(_UpperCamelCase , '''rb''' ).read() if hashlib.shaaaa(_UpperCamelCase ).hexdigest() == expected_shaaaa: return model_bytes else: warnings.warn(f'''{download_target} exists, but the SHA256 checksum does not match; re-downloading the file''' ) with urllib.request.urlopen(_UpperCamelCase ) as source, open(_UpperCamelCase , '''wb''' ) as output: with tqdm( total=int(source.info().get('''Content-Length''' ) ) , ncols=80 , unit='''iB''' , unit_scale=_UpperCamelCase , unit_divisor=1_024 ) as loop: while True: snake_case_ : Dict = source.read(8_192 ) if not buffer: break output.write(_UpperCamelCase ) loop.update(len(_UpperCamelCase ) ) snake_case_ : Any = open(_UpperCamelCase , '''rb''' ).read() if hashlib.shaaaa(_UpperCamelCase ).hexdigest() != expected_shaaaa: raise RuntimeError( '''Model has been downloaded but the SHA256 checksum does not not match. Please retry loading the model.''' ) return model_bytes def lowerCamelCase_ ( _UpperCamelCase , _UpperCamelCase ) -> int: """simple docstring""" if ".pt" not in checkpoint_path: snake_case_ : str = _download(_MODELS[checkpoint_path] ) else: snake_case_ : Union[str, Any] = torch.load(_UpperCamelCase , map_location='''cpu''' ) snake_case_ : int = original_checkpoint['''dims'''] snake_case_ : List[str] = original_checkpoint['''model_state_dict'''] snake_case_ : str = state_dict['''decoder.token_embedding.weight'''] remove_ignore_keys_(_UpperCamelCase ) rename_keys(_UpperCamelCase ) snake_case_ : Optional[int] = True snake_case_ : int = state_dict['''decoder.layers.0.fc1.weight'''].shape[0] snake_case_ : List[str] = WhisperConfig( vocab_size=dimensions['''n_vocab'''] , encoder_ffn_dim=_UpperCamelCase , decoder_ffn_dim=_UpperCamelCase , num_mel_bins=dimensions['''n_mels'''] , d_model=dimensions['''n_audio_state'''] , max_target_positions=dimensions['''n_text_ctx'''] , encoder_layers=dimensions['''n_audio_layer'''] , encoder_attention_heads=dimensions['''n_audio_head'''] , decoder_layers=dimensions['''n_text_layer'''] , decoder_attention_heads=dimensions['''n_text_state'''] , max_source_positions=dimensions['''n_audio_ctx'''] , ) snake_case_ : Union[str, Any] = WhisperForConditionalGeneration(_UpperCamelCase ) snake_case_ , snake_case_ : List[Any] = model.model.load_state_dict(_UpperCamelCase , strict=_UpperCamelCase ) if len(_UpperCamelCase ) > 0 and not set(_UpperCamelCase ) <= { "encoder.embed_positions.weights", "decoder.embed_positions.weights", }: raise ValueError( '''Only `encoder.embed_positions.weights` and `decoder.embed_positions.weights` are allowed to be missing,''' f''' but all the following weights are missing {missing}''' ) if tie_embeds: snake_case_ : List[str] = make_linear_from_emb(model.model.decoder.embed_tokens ) else: snake_case_ : Any = proj_out_weights model.save_pretrained(_UpperCamelCase ) if __name__ == "__main__": lowerCAmelCase_ = argparse.ArgumentParser() # # Required parameters parser.add_argument('''--checkpoint_path''', type=str, help='''Patht to the downloaded checkpoints''') parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') lowerCAmelCase_ = parser.parse_args() convert_openai_whisper_to_tfms(args.checkpoint_path, args.pytorch_dump_folder_path)
279
1
import os import tempfile import unittest from transformers import DistilBertConfig, is_torch_available from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST, DistilBertForMaskedLM, DistilBertForMultipleChoice, DistilBertForQuestionAnswering, DistilBertForSequenceClassification, DistilBertForTokenClassification, DistilBertModel, ) class _lowercase ( __snake_case ): def __init__( self : str , snake_case : List[str] , snake_case : Dict=1_3 , snake_case : str=7 , snake_case : Dict=True , snake_case : Any=True , snake_case : str=False , snake_case : Optional[int]=True , snake_case : int=9_9 , snake_case : Dict=3_2 , snake_case : Any=5 , snake_case : List[str]=4 , snake_case : Dict=3_7 , snake_case : List[Any]="gelu" , snake_case : Optional[int]=0.1 , snake_case : Union[str, Any]=0.1 , snake_case : Optional[Any]=5_1_2 , snake_case : int=1_6 , snake_case : List[Any]=2 , snake_case : Optional[Any]=0.02 , snake_case : List[Any]=3 , snake_case : str=4 , snake_case : Any=None , ) -> Tuple: """simple docstring""" UpperCamelCase_ : List[Any] = parent UpperCamelCase_ : List[Any] = batch_size UpperCamelCase_ : str = seq_length UpperCamelCase_ : Any = is_training UpperCamelCase_ : Any = use_input_mask UpperCamelCase_ : str = use_token_type_ids UpperCamelCase_ : Dict = use_labels UpperCamelCase_ : int = vocab_size UpperCamelCase_ : Union[str, Any] = hidden_size UpperCamelCase_ : List[str] = num_hidden_layers UpperCamelCase_ : str = num_attention_heads UpperCamelCase_ : Optional[int] = intermediate_size UpperCamelCase_ : str = hidden_act UpperCamelCase_ : Union[str, Any] = hidden_dropout_prob UpperCamelCase_ : Optional[Any] = attention_probs_dropout_prob UpperCamelCase_ : str = max_position_embeddings UpperCamelCase_ : Dict = type_vocab_size UpperCamelCase_ : List[Any] = type_sequence_label_size UpperCamelCase_ : Union[str, Any] = initializer_range UpperCamelCase_ : str = num_labels UpperCamelCase_ : Dict = num_choices UpperCamelCase_ : Optional[int] = scope def SCREAMING_SNAKE_CASE__ ( self : Tuple ) -> Dict: """simple docstring""" UpperCamelCase_ : Dict = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCamelCase_ : Dict = None if self.use_input_mask: UpperCamelCase_ : List[Any] = random_attention_mask([self.batch_size, self.seq_length] ) UpperCamelCase_ : Tuple = None UpperCamelCase_ : List[str] = None UpperCamelCase_ : Dict = None if self.use_labels: UpperCamelCase_ : List[str] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase_ : int = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCamelCase_ : Optional[Any] = ids_tensor([self.batch_size] , self.num_choices ) UpperCamelCase_ : List[Any] = self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def SCREAMING_SNAKE_CASE__ ( self : List[Any] ) -> Dict: """simple docstring""" return DistilBertConfig( vocab_size=self.vocab_size , dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , hidden_dim=self.intermediate_size , hidden_act=self.hidden_act , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , ) def SCREAMING_SNAKE_CASE__ ( self : List[str] , snake_case : Optional[int] , snake_case : int , snake_case : Any , snake_case : Dict , snake_case : Optional[Any] , snake_case : int ) -> Union[str, Any]: """simple docstring""" UpperCamelCase_ : List[str] = DistilBertModel(config=a_ ) model.to(a_ ) model.eval() UpperCamelCase_ : int = model(a_ , a_ ) UpperCamelCase_ : List[Any] = model(a_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE__ ( self : int , snake_case : Optional[int] , snake_case : Any , snake_case : Union[str, Any] , snake_case : Union[str, Any] , snake_case : int , snake_case : List[str] ) -> Dict: """simple docstring""" UpperCamelCase_ : Optional[Any] = DistilBertForMaskedLM(config=a_ ) model.to(a_ ) model.eval() UpperCamelCase_ : Union[str, Any] = model(a_ , attention_mask=a_ , labels=a_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE__ ( self : Tuple , snake_case : str , snake_case : str , snake_case : Tuple , snake_case : List[Any] , snake_case : Union[str, Any] , snake_case : Optional[int] ) -> Optional[int]: """simple docstring""" UpperCamelCase_ : Tuple = DistilBertForQuestionAnswering(config=a_ ) model.to(a_ ) model.eval() UpperCamelCase_ : Optional[Any] = model( a_ , attention_mask=a_ , start_positions=a_ , end_positions=a_ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def SCREAMING_SNAKE_CASE__ ( self : str , snake_case : Tuple , snake_case : Tuple , snake_case : List[Any] , snake_case : List[Any] , snake_case : Dict , snake_case : List[Any] ) -> Any: """simple docstring""" UpperCamelCase_ : Any = self.num_labels UpperCamelCase_ : Optional[int] = DistilBertForSequenceClassification(a_ ) model.to(a_ ) model.eval() UpperCamelCase_ : Union[str, Any] = model(a_ , attention_mask=a_ , labels=a_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def SCREAMING_SNAKE_CASE__ ( self : str , snake_case : List[str] , snake_case : str , snake_case : str , snake_case : Any , snake_case : Optional[Any] , snake_case : List[str] ) -> List[Any]: """simple docstring""" UpperCamelCase_ : Union[str, Any] = self.num_labels UpperCamelCase_ : Optional[int] = DistilBertForTokenClassification(config=a_ ) model.to(a_ ) model.eval() UpperCamelCase_ : Dict = model(a_ , attention_mask=a_ , labels=a_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def SCREAMING_SNAKE_CASE__ ( self : Dict , snake_case : int , snake_case : int , snake_case : Optional[Any] , snake_case : Tuple , snake_case : Union[str, Any] , snake_case : Union[str, Any] ) -> Optional[int]: """simple docstring""" UpperCamelCase_ : List[Any] = self.num_choices UpperCamelCase_ : Any = DistilBertForMultipleChoice(config=a_ ) model.to(a_ ) model.eval() UpperCamelCase_ : Union[str, Any] = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() UpperCamelCase_ : List[Any] = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() UpperCamelCase_ : Optional[int] = model( a_ , attention_mask=a_ , labels=a_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ) -> Any: """simple docstring""" UpperCamelCase_ : List[Any] = self.prepare_config_and_inputs() (UpperCamelCase_) : str = config_and_inputs UpperCamelCase_ : Optional[Any] = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class _lowercase ( __snake_case , __snake_case , unittest.TestCase ): lowercase = ( ( DistilBertModel, DistilBertForMaskedLM, DistilBertForMultipleChoice, DistilBertForQuestionAnswering, DistilBertForSequenceClassification, DistilBertForTokenClassification, ) if is_torch_available() else None ) lowercase = ( { 'feature-extraction': DistilBertModel, 'fill-mask': DistilBertForMaskedLM, 'question-answering': DistilBertForQuestionAnswering, 'text-classification': DistilBertForSequenceClassification, 'token-classification': DistilBertForTokenClassification, 'zero-shot': DistilBertForSequenceClassification, } if is_torch_available() else {} ) lowercase = True lowercase = True lowercase = True lowercase = True def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ) -> Optional[int]: """simple docstring""" UpperCamelCase_ : Any = DistilBertModelTester(self ) UpperCamelCase_ : List[str] = ConfigTester(self , config_class=a_ , dim=3_7 ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] ) -> Dict: """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE__ ( self : Any ) -> Tuple: """simple docstring""" UpperCamelCase_ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_model(*a_ ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] ) -> Dict: """simple docstring""" UpperCamelCase_ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_masked_lm(*a_ ) def SCREAMING_SNAKE_CASE__ ( self : Tuple ) -> List[Any]: """simple docstring""" UpperCamelCase_ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_question_answering(*a_ ) def SCREAMING_SNAKE_CASE__ ( self : Dict ) -> Union[str, Any]: """simple docstring""" UpperCamelCase_ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_sequence_classification(*a_ ) def SCREAMING_SNAKE_CASE__ ( self : str ) -> Union[str, Any]: """simple docstring""" UpperCamelCase_ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_token_classification(*a_ ) def SCREAMING_SNAKE_CASE__ ( self : str ) -> Optional[int]: """simple docstring""" UpperCamelCase_ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_multiple_choice(*a_ ) @slow def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ) -> Dict: """simple docstring""" for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase_ : Tuple = DistilBertModel.from_pretrained(a_ ) self.assertIsNotNone(a_ ) @slow @require_torch_gpu def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ) -> int: """simple docstring""" UpperCamelCase_ : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: # BertForMultipleChoice behaves incorrectly in JIT environments. if model_class == DistilBertForMultipleChoice: return UpperCamelCase_ : List[str] = True UpperCamelCase_ : Tuple = model_class(config=a_ ) UpperCamelCase_ : Any = self._prepare_for_class(a_ , a_ ) UpperCamelCase_ : Dict = torch.jit.trace( a_ , (inputs_dict['input_ids'].to('cpu' ), inputs_dict['attention_mask'].to('cpu' )) ) with tempfile.TemporaryDirectory() as tmp: torch.jit.save(a_ , os.path.join(a_ , 'traced_model.pt' ) ) UpperCamelCase_ : int = torch.jit.load(os.path.join(a_ , 'traced_model.pt' ) , map_location=a_ ) loaded(inputs_dict['input_ids'].to(a_ ) , inputs_dict['attention_mask'].to(a_ ) ) @require_torch class _lowercase ( unittest.TestCase ): @slow def SCREAMING_SNAKE_CASE__ ( self : Tuple ) -> str: """simple docstring""" UpperCamelCase_ : Optional[int] = DistilBertModel.from_pretrained('distilbert-base-uncased' ) UpperCamelCase_ : List[Any] = torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]] ) UpperCamelCase_ : Any = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): UpperCamelCase_ : List[Any] = model(a_ , attention_mask=a_ )[0] UpperCamelCase_ : Tuple = torch.Size((1, 1_1, 7_6_8) ) self.assertEqual(output.shape , a_ ) UpperCamelCase_ : Optional[int] = torch.tensor( [[[-0.1639, 0.3299, 0.1648], [-0.1746, 0.3289, 0.1710], [-0.1884, 0.3357, 0.1810]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , a_ , atol=1e-4 ) )
352
import argparse from collections import OrderedDict from pathlib import Path import requests import torch from PIL import Image from transformers import GLPNConfig, GLPNForDepthEstimation, GLPNImageProcessor from transformers.utils import logging logging.set_verbosity_info() a_ = logging.get_logger(__name__) def __lowercase ( lowerCamelCase : Optional[int] ): UpperCamelCase_ : Dict = OrderedDict() for key, value in state_dict.items(): if key.startswith('module.encoder' ): UpperCamelCase_ : Tuple = key.replace('module.encoder' , 'glpn.encoder' ) if key.startswith('module.decoder' ): UpperCamelCase_ : List[Any] = key.replace('module.decoder' , 'decoder.stages' ) if "patch_embed" in key: # replace for example patch_embed1 by patch_embeddings.0 UpperCamelCase_ : Optional[Any] = key[key.find('patch_embed' ) + len('patch_embed' )] UpperCamelCase_ : List[str] = key.replace(F"patch_embed{idx}" , F"patch_embeddings.{int(lowerCamelCase )-1}" ) if "norm" in key: UpperCamelCase_ : int = key.replace('norm' , 'layer_norm' ) if "glpn.encoder.layer_norm" in key: # replace for example layer_norm1 by layer_norm.0 UpperCamelCase_ : int = key[key.find('glpn.encoder.layer_norm' ) + len('glpn.encoder.layer_norm' )] UpperCamelCase_ : int = key.replace(F"layer_norm{idx}" , F"layer_norm.{int(lowerCamelCase )-1}" ) if "layer_norm1" in key: UpperCamelCase_ : Optional[int] = key.replace('layer_norm1' , 'layer_norm_1' ) if "layer_norm2" in key: UpperCamelCase_ : str = key.replace('layer_norm2' , 'layer_norm_2' ) if "block" in key: # replace for example block1 by block.0 UpperCamelCase_ : Union[str, Any] = key[key.find('block' ) + len('block' )] UpperCamelCase_ : Dict = key.replace(F"block{idx}" , F"block.{int(lowerCamelCase )-1}" ) if "attn.q" in key: UpperCamelCase_ : Optional[Any] = key.replace('attn.q' , 'attention.self.query' ) if "attn.proj" in key: UpperCamelCase_ : Any = key.replace('attn.proj' , 'attention.output.dense' ) if "attn" in key: UpperCamelCase_ : Optional[int] = key.replace('attn' , 'attention.self' ) if "fc1" in key: UpperCamelCase_ : int = key.replace('fc1' , 'dense1' ) if "fc2" in key: UpperCamelCase_ : Optional[int] = key.replace('fc2' , 'dense2' ) if "linear_pred" in key: UpperCamelCase_ : Tuple = key.replace('linear_pred' , 'classifier' ) if "linear_fuse" in key: UpperCamelCase_ : Union[str, Any] = key.replace('linear_fuse.conv' , 'linear_fuse' ) UpperCamelCase_ : Tuple = key.replace('linear_fuse.bn' , 'batch_norm' ) if "linear_c" in key: # replace for example linear_c4 by linear_c.3 UpperCamelCase_ : Optional[Any] = key[key.find('linear_c' ) + len('linear_c' )] UpperCamelCase_ : List[str] = key.replace(F"linear_c{idx}" , F"linear_c.{int(lowerCamelCase )-1}" ) if "bot_conv" in key: UpperCamelCase_ : Union[str, Any] = key.replace('bot_conv' , '0.convolution' ) if "skip_conv1" in key: UpperCamelCase_ : Optional[int] = key.replace('skip_conv1' , '1.convolution' ) if "skip_conv2" in key: UpperCamelCase_ : List[Any] = key.replace('skip_conv2' , '2.convolution' ) if "fusion1" in key: UpperCamelCase_ : Tuple = key.replace('fusion1' , '1.fusion' ) if "fusion2" in key: UpperCamelCase_ : Any = key.replace('fusion2' , '2.fusion' ) if "fusion3" in key: UpperCamelCase_ : int = key.replace('fusion3' , '3.fusion' ) if "fusion" in key and "conv" in key: UpperCamelCase_ : Optional[Any] = key.replace('conv' , 'convolutional_layer' ) if key.startswith('module.last_layer_depth' ): UpperCamelCase_ : str = key.replace('module.last_layer_depth' , 'head.head' ) UpperCamelCase_ : Optional[Any] = value return new_state_dict def __lowercase ( lowerCamelCase : Optional[Any] , lowerCamelCase : List[str] ): # for each of the encoder blocks: for i in range(config.num_encoder_blocks ): for j in range(config.depths[i] ): # read in weights + bias of keys and values (which is a single matrix in the original implementation) UpperCamelCase_ : Union[str, Any] = state_dict.pop(F"glpn.encoder.block.{i}.{j}.attention.self.kv.weight" ) UpperCamelCase_ : int = state_dict.pop(F"glpn.encoder.block.{i}.{j}.attention.self.kv.bias" ) # next, add keys and values (in that order) to the state dict UpperCamelCase_ : Union[str, Any] = kv_weight[ : config.hidden_sizes[i], : ] UpperCamelCase_ : int = kv_bias[: config.hidden_sizes[i]] UpperCamelCase_ : Union[str, Any] = kv_weight[ config.hidden_sizes[i] :, : ] UpperCamelCase_ : Optional[int] = kv_bias[config.hidden_sizes[i] :] def __lowercase ( ): UpperCamelCase_ : Optional[int] = 'http://images.cocodataset.org/val2017/000000039769.jpg' UpperCamelCase_ : Optional[Any] = Image.open(requests.get(lowerCamelCase , stream=lowerCamelCase ).raw ) return image @torch.no_grad() def __lowercase ( lowerCamelCase : Tuple , lowerCamelCase : List[str] , lowerCamelCase : Dict=False , lowerCamelCase : Optional[int]=None ): UpperCamelCase_ : List[Any] = GLPNConfig(hidden_sizes=[64, 128, 320, 512] , decoder_hidden_size=64 , depths=[3, 8, 27, 3] ) # load image processor (only resize + rescale) UpperCamelCase_ : Tuple = GLPNImageProcessor() # prepare image UpperCamelCase_ : List[Any] = prepare_img() UpperCamelCase_ : str = image_processor(images=lowerCamelCase , return_tensors='pt' ).pixel_values logger.info('Converting model...' ) # load original state dict UpperCamelCase_ : Any = torch.load(lowerCamelCase , map_location=torch.device('cpu' ) ) # rename keys UpperCamelCase_ : str = rename_keys(lowerCamelCase ) # key and value matrices need special treatment read_in_k_v(lowerCamelCase , lowerCamelCase ) # create HuggingFace model and load state dict UpperCamelCase_ : Dict = GLPNForDepthEstimation(lowerCamelCase ) model.load_state_dict(lowerCamelCase ) model.eval() # forward pass UpperCamelCase_ : Optional[Any] = model(lowerCamelCase ) UpperCamelCase_ : str = outputs.predicted_depth # verify output if model_name is not None: if "nyu" in model_name: UpperCamelCase_ : Tuple = torch.tensor( [[4.4_1_4_7, 4.0_8_7_3, 4.0_6_7_3], [3.7_8_9_0, 3.2_8_8_1, 3.1_5_2_5], [3.7_6_7_4, 3.5_4_2_3, 3.4_9_1_3]] ) elif "kitti" in model_name: UpperCamelCase_ : Any = torch.tensor( [[3.4_2_9_1, 2.7_8_6_5, 2.5_1_5_1], [3.2_8_4_1, 2.7_0_2_1, 2.3_5_0_2], [3.1_1_4_7, 2.4_6_2_5, 2.2_4_8_1]] ) else: raise ValueError(F"Unknown model name: {model_name}" ) UpperCamelCase_ : Tuple = torch.Size([1, 480, 640] ) assert predicted_depth.shape == expected_shape assert torch.allclose(predicted_depth[0, :3, :3] , lowerCamelCase , atol=1e-4 ) print('Looks ok!' ) # finally, push to hub if required if push_to_hub: logger.info('Pushing model and image processor to the hub...' ) model.push_to_hub( repo_path_or_name=Path(lowerCamelCase , lowerCamelCase ) , organization='nielsr' , commit_message='Add model' , use_temp_dir=lowerCamelCase , ) image_processor.push_to_hub( repo_path_or_name=Path(lowerCamelCase , lowerCamelCase ) , organization='nielsr' , commit_message='Add image processor' , use_temp_dir=lowerCamelCase , ) if __name__ == "__main__": a_ = argparse.ArgumentParser() parser.add_argument( '--checkpoint_path', default=None, type=str, help='Path to the original PyTorch checkpoint (.pth file).', ) 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 to upload the model to the HuggingFace hub.' ) parser.add_argument( '--model_name', default='glpn-kitti', type=str, help='Name of the model in case you\'re pushing to the hub.', ) a_ = parser.parse_args() convert_glpn_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub, args.model_name)
50
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available _SCREAMING_SNAKE_CASE : Optional[Any] = { "configuration_mask2former": [ "MASK2FORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "Mask2FormerConfig", ], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : str = ["Mask2FormerImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Dict = [ "MASK2FORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "Mask2FormerForUniversalSegmentation", "Mask2FormerModel", "Mask2FormerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_maskaformer import MASK2FORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, MaskaFormerConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_maskaformer import MaskaFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_maskaformer import ( MASK2FORMER_PRETRAINED_MODEL_ARCHIVE_LIST, MaskaFormerForUniversalSegmentation, MaskaFormerModel, MaskaFormerPreTrainedModel, ) else: import sys _SCREAMING_SNAKE_CASE : Dict = _LazyModule(__name__, globals()["__file__"], _import_structure)
85
'''simple docstring''' import unittest from transformers import ( MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING, TextGenerationPipeline, logging, pipeline, ) from transformers.testing_utils import ( CaptureLogger, is_pipeline_test, require_accelerate, require_tf, require_torch, require_torch_gpu, require_torch_or_tf, ) from .test_pipelines_common import ANY @is_pipeline_test @require_torch_or_tf class _snake_case ( unittest.TestCase ): lowerCAmelCase_ : Optional[Any] = MODEL_FOR_CAUSAL_LM_MAPPING lowerCAmelCase_ : Optional[Any] = TF_MODEL_FOR_CAUSAL_LM_MAPPING @require_torch def lowerCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' snake_case_ = pipeline(task="text-generation" , model="sshleifer/tiny-ctrl" , framework="pt" ) # Using `do_sample=False` to force deterministic output snake_case_ = text_generator("This is a test" , do_sample=a__ ) self.assertEqual( a__ , [ { "generated_text": ( "This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope." " oscope. FiliFili@@" ) } ] , ) snake_case_ = text_generator(["This is a test", "This is a second test"] ) self.assertEqual( a__ , [ [ { "generated_text": ( "This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope." " oscope. FiliFili@@" ) } ], [ { "generated_text": ( "This is a second test ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy" " oscope. oscope. FiliFili@@" ) } ], ] , ) snake_case_ = text_generator("This is a test" , do_sample=a__ , num_return_sequences=2 , return_tensors=a__ ) self.assertEqual( a__ , [ {"generated_token_ids": ANY(a__ )}, {"generated_token_ids": ANY(a__ )}, ] , ) snake_case_ = text_generator.model.config.eos_token_id snake_case_ = "<pad>" snake_case_ = text_generator( ["This is a test", "This is a second test"] , do_sample=a__ , num_return_sequences=2 , batch_size=2 , return_tensors=a__ , ) self.assertEqual( a__ , [ [ {"generated_token_ids": ANY(a__ )}, {"generated_token_ids": ANY(a__ )}, ], [ {"generated_token_ids": ANY(a__ )}, {"generated_token_ids": ANY(a__ )}, ], ] , ) @require_tf def lowerCAmelCase__ ( self ) -> Tuple: '''simple docstring''' snake_case_ = pipeline(task="text-generation" , model="sshleifer/tiny-ctrl" , framework="tf" ) # Using `do_sample=False` to force deterministic output snake_case_ = text_generator("This is a test" , do_sample=a__ ) self.assertEqual( a__ , [ { "generated_text": ( "This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵" " please," ) } ] , ) snake_case_ = text_generator(["This is a test", "This is a second test"] , do_sample=a__ ) self.assertEqual( a__ , [ [ { "generated_text": ( "This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵" " please," ) } ], [ { "generated_text": ( "This is a second test Chieftain Chieftain prefecture prefecture prefecture Cannes Cannes" " Cannes 閲閲Cannes Cannes Cannes 攵 please," ) } ], ] , ) def lowerCAmelCase__ ( self , a__ , a__ , a__ ) -> str: '''simple docstring''' snake_case_ = TextGenerationPipeline(model=a__ , tokenizer=a__ ) return text_generator, ["This is a test", "Another test"] def lowerCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' snake_case_ = "Hello I believe in" snake_case_ = pipeline("text-generation" , model="hf-internal-testing/tiny-random-gpt2" ) snake_case_ = text_generator(a__ ) self.assertEqual( a__ , [{"generated_text": "Hello I believe in fe fe fe fe fe fe fe fe fe fe fe fe"}] , ) snake_case_ = text_generator(a__ , stop_sequence=" fe" ) self.assertEqual(a__ , [{"generated_text": "Hello I believe in fe"}] ) def lowerCAmelCase__ ( self , a__ , a__ ) -> Tuple: '''simple docstring''' snake_case_ = text_generator.model snake_case_ = text_generator.tokenizer snake_case_ = text_generator("This is a test" ) self.assertEqual(a__ , [{"generated_text": ANY(a__ )}] ) self.assertTrue(outputs[0]["generated_text"].startswith("This is a test" ) ) snake_case_ = text_generator("This is a test" , return_full_text=a__ ) self.assertEqual(a__ , [{"generated_text": ANY(a__ )}] ) self.assertNotIn("This is a test" , outputs[0]["generated_text"] ) snake_case_ = pipeline(task="text-generation" , model=a__ , tokenizer=a__ , return_full_text=a__ ) snake_case_ = text_generator("This is a test" ) self.assertEqual(a__ , [{"generated_text": ANY(a__ )}] ) self.assertNotIn("This is a test" , outputs[0]["generated_text"] ) snake_case_ = text_generator("This is a test" , return_full_text=a__ ) self.assertEqual(a__ , [{"generated_text": ANY(a__ )}] ) self.assertTrue(outputs[0]["generated_text"].startswith("This is a test" ) ) snake_case_ = text_generator(["This is great !", "Something else"] , num_return_sequences=2 , do_sample=a__ ) self.assertEqual( a__ , [ [{"generated_text": ANY(a__ )}, {"generated_text": ANY(a__ )}], [{"generated_text": ANY(a__ )}, {"generated_text": ANY(a__ )}], ] , ) if text_generator.tokenizer.pad_token is not None: snake_case_ = text_generator( ["This is great !", "Something else"] , num_return_sequences=2 , batch_size=2 , do_sample=a__ ) self.assertEqual( a__ , [ [{"generated_text": ANY(a__ )}, {"generated_text": ANY(a__ )}], [{"generated_text": ANY(a__ )}, {"generated_text": ANY(a__ )}], ] , ) with self.assertRaises(a__ ): snake_case_ = text_generator("test" , return_full_text=a__ , return_text=a__ ) with self.assertRaises(a__ ): snake_case_ = text_generator("test" , return_full_text=a__ , return_tensors=a__ ) with self.assertRaises(a__ ): snake_case_ = text_generator("test" , return_text=a__ , return_tensors=a__ ) # Empty prompt is slighly special # it requires BOS token to exist. # Special case for Pegasus which will always append EOS so will # work even without BOS. if ( text_generator.tokenizer.bos_token_id is not None or "Pegasus" in tokenizer.__class__.__name__ or "Git" in model.__class__.__name__ ): snake_case_ = text_generator("" ) self.assertEqual(a__ , [{"generated_text": ANY(a__ )}] ) else: with self.assertRaises((ValueError, AssertionError) ): snake_case_ = text_generator("" ) if text_generator.framework == "tf": # TF generation does not support max_new_tokens, and it's impossible # to control long generation with only max_length without # fancy calculation, dismissing tests for now. return # We don't care about infinite range models. # They already work. # Skip this test for XGLM, since it uses sinusoidal positional embeddings which are resized on-the-fly. snake_case_ = ["RwkvForCausalLM", "XGLMForCausalLM", "GPTNeoXForCausalLM"] if ( tokenizer.model_max_length < 10_000 and text_generator.model.__class__.__name__ not in EXTRA_MODELS_CAN_HANDLE_LONG_INPUTS ): # Handling of large generations with self.assertRaises((RuntimeError, IndexError, ValueError, AssertionError) ): text_generator("This is a test" * 500 , max_new_tokens=20 ) snake_case_ = text_generator("This is a test" * 500 , handle_long_generation="hole" , max_new_tokens=20 ) # Hole strategy cannot work with self.assertRaises(a__ ): text_generator( "This is a test" * 500 , handle_long_generation="hole" , max_new_tokens=tokenizer.model_max_length + 10 , ) @require_torch @require_accelerate @require_torch_gpu def lowerCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' import torch # Classic `model_kwargs` snake_case_ = pipeline( model="hf-internal-testing/tiny-random-bloom" , model_kwargs={"device_map": "auto", "torch_dtype": torch.bfloataa} , ) self.assertEqual(pipe.model.device , torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype , torch.bfloataa ) snake_case_ = pipe("This is a test" ) self.assertEqual( a__ , [ { "generated_text": ( "This is a test test test test test test test test test test test test test test test test" " test" ) } ] , ) # Upgraded those two to real pipeline arguments (they just get sent for the model as they're unlikely to mean anything else.) snake_case_ = pipeline(model="hf-internal-testing/tiny-random-bloom" , device_map="auto" , torch_dtype=torch.bfloataa ) self.assertEqual(pipe.model.device , torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype , torch.bfloataa ) snake_case_ = pipe("This is a test" ) self.assertEqual( a__ , [ { "generated_text": ( "This is a test test test test test test test test test test test test test test test test" " test" ) } ] , ) # torch_dtype will be automatically set to float32 if not provided - check: https://github.com/huggingface/transformers/pull/20602 snake_case_ = pipeline(model="hf-internal-testing/tiny-random-bloom" , device_map="auto" ) self.assertEqual(pipe.model.device , torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype , torch.floataa ) snake_case_ = pipe("This is a test" ) self.assertEqual( a__ , [ { "generated_text": ( "This is a test test test test test test test test test test test test test test test test" " test" ) } ] , ) @require_torch @require_torch_gpu def lowerCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' import torch snake_case_ = pipeline(model="hf-internal-testing/tiny-random-bloom" , device=0 , torch_dtype=torch.floataa ) pipe("This is a test" ) @require_torch @require_accelerate @require_torch_gpu def lowerCAmelCase__ ( self ) -> Dict: '''simple docstring''' import torch snake_case_ = pipeline(model="hf-internal-testing/tiny-random-bloom" , device_map="auto" , torch_dtype=torch.floataa ) pipe("This is a test" , do_sample=a__ , top_p=0.5 ) def lowerCAmelCase__ ( self ) -> Tuple: '''simple docstring''' snake_case_ = "Hello world" snake_case_ = pipeline("text-generation" , model="hf-internal-testing/tiny-random-gpt2" ) if text_generator.model.framework == "tf": snake_case_ = logging.get_logger("transformers.generation.tf_utils" ) else: snake_case_ = logging.get_logger("transformers.generation.utils" ) snake_case_ = "Both `max_new_tokens`" # The beggining of the message to be checked in this test # Both are set by the user -> log warning with CaptureLogger(a__ ) as cl: snake_case_ = text_generator(a__ , max_length=10 , max_new_tokens=1 ) self.assertIn(a__ , cl.out ) # The user only sets one -> no warning with CaptureLogger(a__ ) as cl: snake_case_ = text_generator(a__ , max_new_tokens=1 ) self.assertNotIn(a__ , cl.out ) with CaptureLogger(a__ ) as cl: snake_case_ = text_generator(a__ , max_length=10 ) self.assertNotIn(a__ , cl.out )
85
1
"""simple docstring""" lowercase : Tuple = 9.80_665 def A_ ( A__ , A__ , A__ = g ) -> float: if fluid_density <= 0: raise ValueError('Impossible fluid density' ) if volume < 0: raise ValueError('Impossible Object volume' ) if gravity <= 0: raise ValueError('Impossible Gravity' ) return fluid_density * gravity * volume if __name__ == "__main__": import doctest # run doctest doctest.testmod()
367
import unittest from transformers import AutoTokenizer, NystromformerConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( NystromformerForMaskedLM, NystromformerForMultipleChoice, NystromformerForQuestionAnswering, NystromformerForSequenceClassification, NystromformerForTokenClassification, NystromformerModel, ) from transformers.models.nystromformer.modeling_nystromformer import NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST class A__ : """simple docstring""" def __init__( self , lowercase , lowercase=13 , lowercase=7 , lowercase=True , lowercase=True , lowercase=True , lowercase=True , lowercase=99 , lowercase=32 , lowercase=5 , lowercase=4 , lowercase=37 , lowercase="gelu" , lowercase=0.1 , lowercase=0.1 , lowercase=512 , lowercase=16 , lowercase=2 , lowercase=0.02 , lowercase=3 , lowercase=4 , lowercase=None , ) -> List[Any]: '''simple docstring''' a__ : Any = parent a__ : int = batch_size a__ : Dict = seq_length a__ : Tuple = is_training a__ : Any = use_input_mask a__ : Optional[Any] = use_token_type_ids a__ : Dict = use_labels a__ : Optional[int] = vocab_size a__ : List[Any] = hidden_size a__ : int = num_hidden_layers a__ : Optional[Any] = num_attention_heads a__ : str = intermediate_size a__ : Optional[int] = hidden_act a__ : Dict = hidden_dropout_prob a__ : Optional[int] = attention_probs_dropout_prob a__ : Tuple = max_position_embeddings a__ : Dict = type_vocab_size a__ : Any = type_sequence_label_size a__ : List[str] = initializer_range a__ : List[str] = num_labels a__ : Optional[Any] = num_choices a__ : str = scope def __lowercase ( self) -> Union[str, Any]: '''simple docstring''' a__ : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size) a__ : Tuple = None if self.use_input_mask: a__ : Optional[int] = random_attention_mask([self.batch_size, self.seq_length]) a__ : Any = None if self.use_token_type_ids: a__ : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size) a__ : str = None a__ : List[Any] = None a__ : List[str] = None if self.use_labels: a__ : Any = ids_tensor([self.batch_size] , self.type_sequence_label_size) a__ : str = ids_tensor([self.batch_size, self.seq_length] , self.num_labels) a__ : str = ids_tensor([self.batch_size] , self.num_choices) a__ : Union[str, Any] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def __lowercase ( self) -> Optional[int]: '''simple docstring''' return NystromformerConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=lowercase , initializer_range=self.initializer_range , ) def __lowercase ( self , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase) -> Optional[int]: '''simple docstring''' a__ : Union[str, Any] = NystromformerModel(config=lowercase) model.to(lowercase) model.eval() a__ : List[Any] = model(lowercase , attention_mask=lowercase , token_type_ids=lowercase) a__ : int = model(lowercase , token_type_ids=lowercase) a__ : Optional[Any] = model(lowercase) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def __lowercase ( self , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase) -> str: '''simple docstring''' a__ : List[str] = NystromformerForMaskedLM(config=lowercase) model.to(lowercase) model.eval() a__ : int = model(lowercase , attention_mask=lowercase , token_type_ids=lowercase , labels=lowercase) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size)) def __lowercase ( self , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase) -> Union[str, Any]: '''simple docstring''' a__ : Any = NystromformerForQuestionAnswering(config=lowercase) model.to(lowercase) model.eval() a__ : str = model( lowercase , attention_mask=lowercase , token_type_ids=lowercase , start_positions=lowercase , end_positions=lowercase , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length)) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length)) def __lowercase ( self , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase) -> Optional[Any]: '''simple docstring''' a__ : int = self.num_labels a__ : Optional[Any] = NystromformerForSequenceClassification(lowercase) model.to(lowercase) model.eval() a__ : Tuple = model(lowercase , attention_mask=lowercase , token_type_ids=lowercase , labels=lowercase) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels)) def __lowercase ( self , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase) -> List[str]: '''simple docstring''' a__ : Tuple = self.num_labels a__ : int = NystromformerForTokenClassification(config=lowercase) model.to(lowercase) model.eval() a__ : str = model(lowercase , attention_mask=lowercase , token_type_ids=lowercase , labels=lowercase) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels)) def __lowercase ( self , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase) -> Any: '''simple docstring''' a__ : Optional[int] = self.num_choices a__ : Tuple = NystromformerForMultipleChoice(config=lowercase) model.to(lowercase) model.eval() a__ : Optional[Any] = input_ids.unsqueeze(1).expand(-1 , self.num_choices , -1).contiguous() a__ : Tuple = token_type_ids.unsqueeze(1).expand(-1 , self.num_choices , -1).contiguous() a__ : str = input_mask.unsqueeze(1).expand(-1 , self.num_choices , -1).contiguous() a__ : Optional[int] = model( lowercase , attention_mask=lowercase , token_type_ids=lowercase , labels=lowercase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices)) def __lowercase ( self) -> Union[str, Any]: '''simple docstring''' a__ : List[Any] = self.prepare_config_and_inputs() ( ( a__ ) , ( a__ ) , ( a__ ) , ( a__ ) , ( a__ ) , ( a__ ) , ( a__ ) , ) : str = config_and_inputs a__ : Any = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class A__ ( __UpperCAmelCase , __UpperCAmelCase , unittest.TestCase ): """simple docstring""" __A : Any = ( ( NystromformerModel, NystromformerForMaskedLM, NystromformerForMultipleChoice, NystromformerForQuestionAnswering, NystromformerForSequenceClassification, NystromformerForTokenClassification, ) if is_torch_available() else () ) __A : str = ( { '''feature-extraction''': NystromformerModel, '''fill-mask''': NystromformerForMaskedLM, '''question-answering''': NystromformerForQuestionAnswering, '''text-classification''': NystromformerForSequenceClassification, '''token-classification''': NystromformerForTokenClassification, '''zero-shot''': NystromformerForSequenceClassification, } if is_torch_available() else {} ) __A : Optional[Any] = False __A : Tuple = False def __lowercase ( self) -> Optional[Any]: '''simple docstring''' a__ : int = NystromformerModelTester(self) a__ : Any = ConfigTester(self , config_class=lowercase , hidden_size=37) def __lowercase ( self) -> Any: '''simple docstring''' self.config_tester.run_common_tests() def __lowercase ( self) -> int: '''simple docstring''' a__ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowercase) def __lowercase ( self) -> Tuple: '''simple docstring''' a__ : Tuple = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: a__ : Optional[Any] = type self.model_tester.create_and_check_model(*lowercase) def __lowercase ( self) -> Tuple: '''simple docstring''' a__ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*lowercase) def __lowercase ( self) -> Any: '''simple docstring''' a__ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*lowercase) def __lowercase ( self) -> Any: '''simple docstring''' a__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*lowercase) def __lowercase ( self) -> Union[str, Any]: '''simple docstring''' a__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*lowercase) def __lowercase ( self) -> int: '''simple docstring''' a__ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*lowercase) @slow def __lowercase ( self) -> Optional[int]: '''simple docstring''' for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a__ : int = NystromformerModel.from_pretrained(lowercase) self.assertIsNotNone(lowercase) @require_torch class A__ ( unittest.TestCase ): """simple docstring""" @slow def __lowercase ( self) -> Optional[Any]: '''simple docstring''' a__ : List[str] = NystromformerModel.from_pretrained('uw-madison/nystromformer-512') a__ : Tuple = torch.tensor([[0, 1, 2, 3, 4, 5]]) with torch.no_grad(): a__ : List[Any] = model(lowercase)[0] a__ : str = torch.Size((1, 6, 768)) self.assertEqual(output.shape , lowercase) a__ : str = torch.tensor( [[[-0.45_32, -0.09_36, 0.51_37], [-0.26_76, 0.06_28, 0.61_86], [-0.36_29, -0.17_26, 0.47_16]]]) self.assertTrue(torch.allclose(output[:, :3, :3] , lowercase , atol=1e-4)) @slow def __lowercase ( self) -> Optional[int]: '''simple docstring''' a__ : Any = 'the [MASK] of Belgium is Brussels' a__ : List[str] = AutoTokenizer.from_pretrained('uw-madison/nystromformer-512') a__ : Optional[int] = NystromformerForMaskedLM.from_pretrained('uw-madison/nystromformer-512') a__ : List[Any] = tokenizer(lowercase , return_tensors='pt') with torch.no_grad(): a__ : Union[str, Any] = model(encoding.input_ids).logits a__ : str = token_logits[:, 2, :].argmax(-1)[0] self.assertEqual(tokenizer.decode(lowercase) , 'capital')
225
0
"""simple docstring""" class _snake_case : def __init__( self : Optional[int] , UpperCAmelCase : Any , UpperCAmelCase : Union[str, Any]=None , UpperCAmelCase : Any=None ): __lowerCamelCase : Optional[int] = data __lowerCamelCase : int = previous __lowerCamelCase : List[Any] = next_node def __str__( self : int ): return F"""{self.data}""" def lowerCamelCase__ ( self : List[str] ): return self.data def lowerCamelCase__ ( self : Optional[Any] ): return self.next def lowerCamelCase__ ( self : List[Any] ): return self.previous class _snake_case : def __init__( self : Tuple , UpperCAmelCase : List[Any] ): __lowerCamelCase : List[Any] = head def __iter__( self : Optional[Any] ): return self def lowerCamelCase__ ( self : List[str] ): if not self.current: raise StopIteration else: __lowerCamelCase : List[Any] = self.current.get_data() __lowerCamelCase : List[Any] = self.current.get_next() return value class _snake_case : def __init__( self : Any ): __lowerCamelCase : Optional[int] = None # First node in list __lowerCamelCase : Tuple = None # Last node in list def __str__( self : str ): __lowerCamelCase : List[str] = self.head __lowerCamelCase : Tuple = [] while current is not None: nodes.append(current.get_data() ) __lowerCamelCase : Any = current.get_next() return " ".join(str(UpperCAmelCase ) for node in nodes ) def __contains__( self : Optional[int] , UpperCAmelCase : int ): __lowerCamelCase : Union[str, Any] = self.head while current: if current.get_data() == value: return True __lowerCamelCase : Dict = current.get_next() return False def __iter__( self : Any ): return LinkedListIterator(self.head ) def lowerCamelCase__ ( self : Optional[Any] ): if self.head: return self.head.get_data() return None def lowerCamelCase__ ( self : Dict ): if self.tail: return self.tail.get_data() return None def lowerCamelCase__ ( self : int , UpperCAmelCase : Union[str, Any] ): if self.head is None: __lowerCamelCase : str = node __lowerCamelCase : int = node else: self.insert_before_node(self.head , UpperCAmelCase ) def lowerCamelCase__ ( self : List[str] , UpperCAmelCase : List[str] ): if self.head is None: self.set_head(UpperCAmelCase ) else: self.insert_after_node(self.tail , UpperCAmelCase ) def lowerCamelCase__ ( self : Optional[Any] , UpperCAmelCase : List[Any] ): __lowerCamelCase : Tuple = Node(UpperCAmelCase ) if self.head is None: self.set_head(UpperCAmelCase ) else: self.set_tail(UpperCAmelCase ) def lowerCamelCase__ ( self : Union[str, Any] , UpperCAmelCase : Optional[Any] , UpperCAmelCase : Union[str, Any] ): __lowerCamelCase : str = node __lowerCamelCase : str = node.previous if node.get_previous() is None: __lowerCamelCase : Any = node_to_insert else: __lowerCamelCase : Optional[Any] = node_to_insert __lowerCamelCase : Any = node_to_insert def lowerCamelCase__ ( self : List[str] , UpperCAmelCase : List[Any] , UpperCAmelCase : Union[str, Any] ): __lowerCamelCase : Optional[int] = node __lowerCamelCase : str = node.next if node.get_next() is None: __lowerCamelCase : int = node_to_insert else: __lowerCamelCase : Tuple = node_to_insert __lowerCamelCase : Optional[Any] = node_to_insert def lowerCamelCase__ ( self : str , UpperCAmelCase : Tuple , UpperCAmelCase : Any ): __lowerCamelCase : List[Any] = 1 __lowerCamelCase : Tuple = Node(UpperCAmelCase ) __lowerCamelCase : List[Any] = self.head while node: if current_position == position: self.insert_before_node(UpperCAmelCase , UpperCAmelCase ) return current_position += 1 __lowerCamelCase : int = node.next self.insert_after_node(self.tail , UpperCAmelCase ) def lowerCamelCase__ ( self : Union[str, Any] , UpperCAmelCase : int ): __lowerCamelCase : Dict = self.head while node: if node.get_data() == item: return node __lowerCamelCase : Dict = node.get_next() raise Exception("Node not found" ) def lowerCamelCase__ ( self : int , UpperCAmelCase : Dict ): if (node := self.get_node(UpperCAmelCase )) is not None: if node == self.head: __lowerCamelCase : Union[str, Any] = self.head.get_next() if node == self.tail: __lowerCamelCase : Union[str, Any] = self.tail.get_previous() self.remove_node_pointers(UpperCAmelCase ) @staticmethod def lowerCamelCase__ ( UpperCAmelCase : List[str] ): if node.get_next(): __lowerCamelCase : int = node.previous if node.get_previous(): __lowerCamelCase : Optional[Any] = node.next __lowerCamelCase : Optional[int] = None __lowerCamelCase : Optional[Any] = None def lowerCamelCase__ ( self : Optional[int] ): return self.head is None def lowercase_ ( ) -> None: '''simple docstring''' if __name__ == "__main__": import doctest doctest.testmod()
135
import os def a ( ) -> Any: """simple docstring""" with open(os.path.dirname(A__ ) + '/p022_names.txt' ) as file: _lowercase =str(file.readlines()[0] ) _lowercase =names.replace('"' , '' ).split(',' ) names.sort() _lowercase =0 _lowercase =0 for i, name in enumerate(A__ ): for letter in name: name_score += ord(A__ ) - 64 total_score += (i + 1) * name_score _lowercase =0 return total_score if __name__ == "__main__": print(solution())
205
0
'''simple docstring''' import argparse import json import re from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( MobileNetVaConfig, MobileNetVaForImageClassification, MobileNetVaImageProcessor, load_tf_weights_in_mobilenet_va, ) from transformers.utils import logging logging.set_verbosity_info() _lowerCAmelCase = logging.get_logger(__name__) def UpperCamelCase ( a ) -> List[Any]: '''simple docstring''' __magic_name__ = MobileNetVaConfig(layer_norm_eps=0.0_01 ) if "_quant" in model_name: raise ValueError('''Quantized models are not supported.''' ) __magic_name__ = re.match(R'''^mobilenet_v1_([^_]*)_([^_]*)$''' , a ) if matches: __magic_name__ = float(matches[1] ) __magic_name__ = int(matches[2] ) # The TensorFlow version of MobileNetV1 predicts 1001 classes instead of # the usual 1000. The first class (index 0) is "background". __magic_name__ = 1001 __magic_name__ = '''imagenet-1k-id2label.json''' __magic_name__ = '''huggingface/label-files''' __magic_name__ = json.load(open(hf_hub_download(a , a , repo_type='''dataset''' ) , '''r''' ) ) __magic_name__ = {int(a ) + 1: v for k, v in idalabel.items()} __magic_name__ = '''background''' __magic_name__ = idalabel __magic_name__ = {v: k for k, v in idalabel.items()} return config def UpperCamelCase ( ) -> Union[str, Any]: '''simple docstring''' __magic_name__ = '''http://images.cocodataset.org/val2017/000000039769.jpg''' __magic_name__ = Image.open(requests.get(a , stream=a ).raw ) return im @torch.no_grad() def UpperCamelCase ( a , a , a , a=False ) -> Dict: '''simple docstring''' __magic_name__ = get_mobilenet_va_config(a ) # Load 🤗 model __magic_name__ = MobileNetVaForImageClassification(a ).eval() # Load weights from TensorFlow checkpoint load_tf_weights_in_mobilenet_va(a , a , a ) # Check outputs on an image, prepared by MobileNetV1ImageProcessor __magic_name__ = MobileNetVaImageProcessor( crop_size={'''width''': config.image_size, '''height''': config.image_size} , size={'''shortest_edge''': config.image_size + 32} , ) __magic_name__ = image_processor(images=prepare_img() , return_tensors='''pt''' ) __magic_name__ = model(**a ) __magic_name__ = outputs.logits assert logits.shape == (1, 1001) if model_name == "mobilenet_v1_1.0_224": __magic_name__ = torch.tensor([-4.17_39, -1.12_33, 3.12_05] ) elif model_name == "mobilenet_v1_0.75_192": __magic_name__ = torch.tensor([-3.94_40, -2.31_41, -0.33_33] ) else: __magic_name__ = None if expected_logits is not None: assert torch.allclose(logits[0, :3] , a , atol=1e-4 ) Path(a ).mkdir(exist_ok=a ) print(F'''Saving model {model_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(a ) print(F'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(a ) if push_to_hub: print('''Pushing to the hub...''' ) __magic_name__ = '''google/''' + model_name image_processor.push_to_hub(a ) model.push_to_hub(a ) if __name__ == "__main__": _lowerCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default="mobilenet_v1_1.0_224", type=str, help="Name of the MobileNetV1 model you'd like to convert. Should in the form 'mobilenet_v1_<depth>_<size>'.", ) parser.add_argument( "--checkpoint_path", required=True, type=str, help="Path to the original TensorFlow checkpoint (.ckpt file)." ) parser.add_argument( "--pytorch_dump_folder_path", required=True, type=str, help="Path to the output PyTorch model directory." ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether or not to push the converted model to the 🤗 hub." ) _lowerCAmelCase = parser.parse_args() convert_movilevit_checkpoint( args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
98
'''simple docstring''' import argparse import json import pickle from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import MaskFormerConfig, MaskFormerForInstanceSegmentation, MaskFormerImageProcessor, SwinConfig from transformers.utils import logging logging.set_verbosity_info() _lowerCAmelCase = logging.get_logger(__name__) def UpperCamelCase ( a ) -> str: '''simple docstring''' __magic_name__ = SwinConfig.from_pretrained( '''microsoft/swin-tiny-patch4-window7-224''' , out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4'''] ) __magic_name__ = MaskFormerConfig(backbone_config=a ) __magic_name__ = '''huggingface/label-files''' if "ade20k-full" in model_name: # this should be ok __magic_name__ = 847 __magic_name__ = '''maskformer-ade20k-full-id2label.json''' elif "ade" in model_name: # this should be ok __magic_name__ = 150 __magic_name__ = '''ade20k-id2label.json''' elif "coco-stuff" in model_name: # this should be ok __magic_name__ = 171 __magic_name__ = '''maskformer-coco-stuff-id2label.json''' elif "coco" in model_name: # TODO __magic_name__ = 133 __magic_name__ = '''coco-panoptic-id2label.json''' elif "cityscapes" in model_name: # this should be ok __magic_name__ = 19 __magic_name__ = '''cityscapes-id2label.json''' elif "vistas" in model_name: # this should be ok __magic_name__ = 65 __magic_name__ = '''mapillary-vistas-id2label.json''' __magic_name__ = json.load(open(hf_hub_download(a , a , repo_type='''dataset''' ) , '''r''' ) ) __magic_name__ = {int(a ): v for k, v in idalabel.items()} return config def UpperCamelCase ( a ) -> Tuple: '''simple docstring''' __magic_name__ = [] # stem # fmt: off rename_keys.append(('''backbone.patch_embed.proj.weight''', '''model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.weight''') ) rename_keys.append(('''backbone.patch_embed.proj.bias''', '''model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.bias''') ) rename_keys.append(('''backbone.patch_embed.norm.weight''', '''model.pixel_level_module.encoder.model.embeddings.norm.weight''') ) rename_keys.append(('''backbone.patch_embed.norm.bias''', '''model.pixel_level_module.encoder.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.layers.{i}.blocks.{j}.norm1.weight''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.norm1.bias''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.attn.relative_position_bias_table''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.attn.relative_position_index''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.attn.proj.weight''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.attn.proj.bias''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.norm2.weight''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.norm2.bias''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.mlp.fc1.weight''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.mlp.fc1.bias''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.mlp.fc2.weight''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.weight''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.mlp.fc2.bias''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.bias''') ) if i < 3: rename_keys.append((F'''backbone.layers.{i}.downsample.reduction.weight''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.reduction.weight''') ) rename_keys.append((F'''backbone.layers.{i}.downsample.norm.weight''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.weight''') ) rename_keys.append((F'''backbone.layers.{i}.downsample.norm.bias''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.bias''') ) rename_keys.append((F'''backbone.norm{i}.weight''', F'''model.pixel_level_module.encoder.hidden_states_norms.{i}.weight''') ) rename_keys.append((F'''backbone.norm{i}.bias''', F'''model.pixel_level_module.encoder.hidden_states_norms.{i}.bias''') ) # FPN rename_keys.append(('''sem_seg_head.layer_4.weight''', '''model.pixel_level_module.decoder.fpn.stem.0.weight''') ) rename_keys.append(('''sem_seg_head.layer_4.norm.weight''', '''model.pixel_level_module.decoder.fpn.stem.1.weight''') ) rename_keys.append(('''sem_seg_head.layer_4.norm.bias''', '''model.pixel_level_module.decoder.fpn.stem.1.bias''') ) for source_index, target_index in zip(range(3 , 0 , -1 ) , range(0 , 3 ) ): rename_keys.append((F'''sem_seg_head.adapter_{source_index}.weight''', F'''model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.0.weight''') ) rename_keys.append((F'''sem_seg_head.adapter_{source_index}.norm.weight''', F'''model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.weight''') ) rename_keys.append((F'''sem_seg_head.adapter_{source_index}.norm.bias''', F'''model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.bias''') ) rename_keys.append((F'''sem_seg_head.layer_{source_index}.weight''', F'''model.pixel_level_module.decoder.fpn.layers.{target_index}.block.0.weight''') ) rename_keys.append((F'''sem_seg_head.layer_{source_index}.norm.weight''', F'''model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.weight''') ) rename_keys.append((F'''sem_seg_head.layer_{source_index}.norm.bias''', F'''model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.bias''') ) rename_keys.append(('''sem_seg_head.mask_features.weight''', '''model.pixel_level_module.decoder.mask_projection.weight''') ) rename_keys.append(('''sem_seg_head.mask_features.bias''', '''model.pixel_level_module.decoder.mask_projection.bias''') ) # Transformer decoder for idx in range(config.decoder_config.decoder_layers ): # self-attention out projection rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.weight''', F'''model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.weight''') ) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.bias''', F'''model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.bias''') ) # cross-attention out projection rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.weight''', F'''model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.weight''') ) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.bias''', F'''model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.bias''') ) # MLP 1 rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.weight''', F'''model.transformer_module.decoder.layers.{idx}.fc1.weight''') ) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.bias''', F'''model.transformer_module.decoder.layers.{idx}.fc1.bias''') ) # MLP 2 rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.weight''', F'''model.transformer_module.decoder.layers.{idx}.fc2.weight''') ) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.bias''', F'''model.transformer_module.decoder.layers.{idx}.fc2.bias''') ) # layernorm 1 (self-attention layernorm) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.weight''', F'''model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.weight''') ) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.bias''', F'''model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.bias''') ) # layernorm 2 (cross-attention layernorm) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.weight''', F'''model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.weight''') ) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.bias''', F'''model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.bias''') ) # layernorm 3 (final layernorm) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.weight''', F'''model.transformer_module.decoder.layers.{idx}.final_layer_norm.weight''') ) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.bias''', F'''model.transformer_module.decoder.layers.{idx}.final_layer_norm.bias''') ) rename_keys.append(('''sem_seg_head.predictor.transformer.decoder.norm.weight''', '''model.transformer_module.decoder.layernorm.weight''') ) rename_keys.append(('''sem_seg_head.predictor.transformer.decoder.norm.bias''', '''model.transformer_module.decoder.layernorm.bias''') ) # heads on top rename_keys.append(('''sem_seg_head.predictor.query_embed.weight''', '''model.transformer_module.queries_embedder.weight''') ) rename_keys.append(('''sem_seg_head.predictor.input_proj.weight''', '''model.transformer_module.input_projection.weight''') ) rename_keys.append(('''sem_seg_head.predictor.input_proj.bias''', '''model.transformer_module.input_projection.bias''') ) rename_keys.append(('''sem_seg_head.predictor.class_embed.weight''', '''class_predictor.weight''') ) rename_keys.append(('''sem_seg_head.predictor.class_embed.bias''', '''class_predictor.bias''') ) for i in range(3 ): rename_keys.append((F'''sem_seg_head.predictor.mask_embed.layers.{i}.weight''', F'''mask_embedder.{i}.0.weight''') ) rename_keys.append((F'''sem_seg_head.predictor.mask_embed.layers.{i}.bias''', F'''mask_embedder.{i}.0.bias''') ) # fmt: on return rename_keys def UpperCamelCase ( a , a , a ) -> str: '''simple docstring''' __magic_name__ = dct.pop(a ) __magic_name__ = val def UpperCamelCase ( a , a ) -> List[str]: '''simple docstring''' __magic_name__ = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): __magic_name__ = 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) __magic_name__ = state_dict.pop(F'''backbone.layers.{i}.blocks.{j}.attn.qkv.weight''' ) __magic_name__ = state_dict.pop(F'''backbone.layers.{i}.blocks.{j}.attn.qkv.bias''' ) # next, add query, keys and values (in that order) to the state dict __magic_name__ = in_proj_weight[:dim, :] __magic_name__ = in_proj_bias[: dim] __magic_name__ = in_proj_weight[ dim : dim * 2, : ] __magic_name__ = in_proj_bias[ dim : dim * 2 ] __magic_name__ = in_proj_weight[ -dim :, : ] __magic_name__ = in_proj_bias[-dim :] # fmt: on def UpperCamelCase ( a , a ) -> int: '''simple docstring''' # fmt: off __magic_name__ = config.decoder_config.hidden_size for idx in range(config.decoder_config.decoder_layers ): # read in weights + bias of self-attention input projection layer (in the original implementation, this is a single matrix + bias) __magic_name__ = state_dict.pop(F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_weight''' ) __magic_name__ = state_dict.pop(F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_bias''' ) # next, add query, keys and values (in that order) to the state dict __magic_name__ = in_proj_weight[: hidden_size, :] __magic_name__ = in_proj_bias[:config.hidden_size] __magic_name__ = in_proj_weight[hidden_size : hidden_size * 2, :] __magic_name__ = in_proj_bias[hidden_size : hidden_size * 2] __magic_name__ = in_proj_weight[-hidden_size :, :] __magic_name__ = in_proj_bias[-hidden_size :] # read in weights + bias of cross-attention input projection layer (in the original implementation, this is a single matrix + bias) __magic_name__ = state_dict.pop(F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_weight''' ) __magic_name__ = state_dict.pop(F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_bias''' ) # next, add query, keys and values (in that order) to the state dict __magic_name__ = in_proj_weight[: hidden_size, :] __magic_name__ = in_proj_bias[:config.hidden_size] __magic_name__ = in_proj_weight[hidden_size : hidden_size * 2, :] __magic_name__ = in_proj_bias[hidden_size : hidden_size * 2] __magic_name__ = in_proj_weight[-hidden_size :, :] __magic_name__ = in_proj_bias[-hidden_size :] # fmt: on def UpperCamelCase ( ) -> torch.Tensor: '''simple docstring''' __magic_name__ = '''http://images.cocodataset.org/val2017/000000039769.jpg''' __magic_name__ = Image.open(requests.get(a , stream=a ).raw ) return im @torch.no_grad() def UpperCamelCase ( a , a , a , a = False ) -> Dict: '''simple docstring''' __magic_name__ = get_maskformer_config(a ) # load original state_dict with open(a , '''rb''' ) as f: __magic_name__ = pickle.load(a ) __magic_name__ = data['''model'''] # for name, param in state_dict.items(): # print(name, param.shape) # rename keys __magic_name__ = 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 ) # update to torch tensors for key, value in state_dict.items(): __magic_name__ = torch.from_numpy(a ) # load 🤗 model __magic_name__ = MaskFormerForInstanceSegmentation(a ) model.eval() for name, param in model.named_parameters(): print(a , param.shape ) __magic_name__ , __magic_name__ = model.load_state_dict(a , strict=a ) assert missing_keys == [ "model.pixel_level_module.encoder.model.layernorm.weight", "model.pixel_level_module.encoder.model.layernorm.bias", ] assert len(a ) == 0, F'''Unexpected keys: {unexpected_keys}''' # verify results __magic_name__ = prepare_img() if "vistas" in model_name: __magic_name__ = 65 elif "cityscapes" in model_name: __magic_name__ = 6_5535 else: __magic_name__ = 255 __magic_name__ = True if '''ade''' in model_name else False __magic_name__ = MaskFormerImageProcessor(ignore_index=a , reduce_labels=a ) __magic_name__ = image_processor(a , return_tensors='''pt''' ) __magic_name__ = model(**a ) print('''Logits:''' , outputs.class_queries_logits[0, :3, :3] ) if model_name == "maskformer-swin-tiny-ade": __magic_name__ = torch.tensor( [[3.63_53, -4.47_70, -2.60_65], [0.50_81, -4.23_94, -3.53_43], [2.19_09, -5.03_53, -1.93_23]] ) assert torch.allclose(outputs.class_queries_logits[0, :3, :3] , a , atol=1e-4 ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: print(F'''Saving model and image processor to {pytorch_dump_folder_path}''' ) Path(a ).mkdir(exist_ok=a ) model.save_pretrained(a ) image_processor.save_pretrained(a ) if push_to_hub: print('''Pushing model and image processor to the hub...''' ) model.push_to_hub(F'''nielsr/{model_name}''' ) image_processor.push_to_hub(F'''nielsr/{model_name}''' ) if __name__ == "__main__": _lowerCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default="maskformer-swin-tiny-ade", type=str, help=("Name of the MaskFormer model you'd like to convert",), ) parser.add_argument( "--checkpoint_path", default="/Users/nielsrogge/Documents/MaskFormer_checkpoints/MaskFormer-Swin-tiny-ADE20k/model.pkl", type=str, help="Path to the original state dict (.pth file).", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether or not to push the converted model to the 🤗 hub." ) _lowerCAmelCase = parser.parse_args() convert_maskformer_checkpoint( args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
98
1
'''simple docstring''' from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_tf_available(): import tensorflow as tf from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) @add_end_docstrings(A__ ) class __UpperCAmelCase ( A__ ): '''simple docstring''' def __init__(self : Tuple , *_lowerCAmelCase : List[str] , **_lowerCAmelCase : List[str] ): super().__init__(*_lowerCAmelCase , **_lowerCAmelCase ) requires_backends(self , """vision""" ) self.check_model_type( TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING if self.framework == """tf""" else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING ) def A (self : Any , _lowerCAmelCase : str=None ): A = {} if top_k is not None: A = top_k return {}, {}, postprocess_params def __call__(self : str , _lowerCAmelCase : Union[str, List[str], "Image.Image", List["Image.Image"]] , **_lowerCAmelCase : int ): return super().__call__(_lowerCAmelCase , **_lowerCAmelCase ) def A (self : List[str] , _lowerCAmelCase : List[Any] ): A = load_image(_lowerCAmelCase ) A = self.image_processor(images=_lowerCAmelCase , return_tensors=self.framework ) return model_inputs def A (self : Union[str, Any] , _lowerCAmelCase : Optional[int] ): A = self.model(**_lowerCAmelCase ) return model_outputs def A (self : Optional[Any] , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : int=5 ): if top_k > self.model.config.num_labels: A = self.model.config.num_labels if self.framework == "pt": A = model_outputs.logits.softmax(-1 )[0] A , A = probs.topk(_lowerCAmelCase ) elif self.framework == "tf": A = stable_softmax(model_outputs.logits , axis=-1 )[0] A = tf.math.top_k(_lowerCAmelCase , k=_lowerCAmelCase ) A , A = topk.values.numpy(), topk.indices.numpy() else: raise ValueError(F"""Unsupported framework: {self.framework}""" ) A = scores.tolist() A = ids.tolist() return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(_lowerCAmelCase , _lowerCAmelCase )]
258
'''simple docstring''' import collections import inspect import unittest from transformers import SwinvaConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class __UpperCAmelCase : '''simple docstring''' def __init__(self : Tuple , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : int=13 , _lowerCAmelCase : Any=32 , _lowerCAmelCase : Dict=2 , _lowerCAmelCase : Any=3 , _lowerCAmelCase : Dict=16 , _lowerCAmelCase : str=[1, 2, 1] , _lowerCAmelCase : List[Any]=[2, 2, 4] , _lowerCAmelCase : List[Any]=2 , _lowerCAmelCase : Any=2.0 , _lowerCAmelCase : Any=True , _lowerCAmelCase : int=0.0 , _lowerCAmelCase : List[Any]=0.0 , _lowerCAmelCase : List[Any]=0.1 , _lowerCAmelCase : List[str]="gelu" , _lowerCAmelCase : Optional[Any]=False , _lowerCAmelCase : str=True , _lowerCAmelCase : List[str]=0.02 , _lowerCAmelCase : Dict=1e-5 , _lowerCAmelCase : Optional[int]=True , _lowerCAmelCase : Dict=None , _lowerCAmelCase : Optional[int]=True , _lowerCAmelCase : Dict=10 , _lowerCAmelCase : int=8 , ): A = parent A = batch_size A = image_size A = patch_size A = num_channels A = embed_dim A = depths A = num_heads A = window_size A = mlp_ratio A = qkv_bias A = hidden_dropout_prob A = attention_probs_dropout_prob A = drop_path_rate A = hidden_act A = use_absolute_embeddings A = patch_norm A = layer_norm_eps A = initializer_range A = is_training A = scope A = use_labels A = type_sequence_label_size A = encoder_stride def A (self : Dict ): A = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A = None if self.use_labels: A = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A = self.get_config() return config, pixel_values, labels def A (self : Optional[Any] ): return SwinvaConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , embed_dim=self.embed_dim , depths=self.depths , num_heads=self.num_heads , window_size=self.window_size , mlp_ratio=self.mlp_ratio , qkv_bias=self.qkv_bias , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , drop_path_rate=self.drop_path_rate , hidden_act=self.hidden_act , use_absolute_embeddings=self.use_absolute_embeddings , path_norm=self.patch_norm , layer_norm_eps=self.layer_norm_eps , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def A (self : List[Any] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Union[str, Any] ): A = SwinvaModel(config=_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.eval() A = model(_lowerCAmelCase ) A = ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths ) - 1)) A = int(config.embed_dim * 2 ** (len(config.depths ) - 1) ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, expected_seq_len, expected_dim) ) def A (self : Tuple , _lowerCAmelCase : Dict , _lowerCAmelCase : int , _lowerCAmelCase : Optional[int] ): A = SwinvaForMaskedImageModeling(config=_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.eval() A = model(_lowerCAmelCase ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images A = 1 A = SwinvaForMaskedImageModeling(_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.eval() A = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A = model(_lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def A (self : List[str] , _lowerCAmelCase : Any , _lowerCAmelCase : Dict , _lowerCAmelCase : Any ): A = self.type_sequence_label_size A = SwinvaForImageClassification(_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.eval() A = model(_lowerCAmelCase , labels=_lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def A (self : Union[str, Any] ): A = self.prepare_config_and_inputs() A , A , A = config_and_inputs A = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class __UpperCAmelCase ( A__ , A__ , unittest.TestCase ): '''simple docstring''' __lowerCAmelCase = ( (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else () ) __lowerCAmelCase = ( {'''feature-extraction''': SwinvaModel, '''image-classification''': SwinvaForImageClassification} if is_torch_available() else {} ) __lowerCAmelCase = False __lowerCAmelCase = False __lowerCAmelCase = False __lowerCAmelCase = False def A (self : Any ): A = SwinvaModelTester(self ) A = ConfigTester(self , config_class=_lowerCAmelCase , embed_dim=37 ) def A (self : Dict ): self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def A (self : int ): A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_lowerCAmelCase ) @unittest.skip(reason="""Got `CUDA error: misaligned address` with PyTorch 2.0.0.""" ) def A (self : Dict ): pass @unittest.skip(reason="""Swinv2 does not use inputs_embeds""" ) def A (self : Optional[int] ): pass def A (self : List[str] ): A , A = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A = model_class(_lowerCAmelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_lowerCAmelCase , nn.Linear ) ) def A (self : Optional[int] ): A , A = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A = model_class(_lowerCAmelCase ) A = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A = [*signature.parameters.keys()] A = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , _lowerCAmelCase ) def A (self : int ): A , A = self.model_tester.prepare_config_and_inputs_for_common() A = True for model_class in self.all_model_classes: A = True A = False A = True A = model_class(_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.eval() with torch.no_grad(): A = model(**self._prepare_for_class(_lowerCAmelCase , _lowerCAmelCase ) ) A = outputs.attentions A = len(self.model_tester.depths ) self.assertEqual(len(_lowerCAmelCase ) , _lowerCAmelCase ) # check that output_attentions also work using config del inputs_dict["output_attentions"] A = True A = config.window_size**2 A = model_class(_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.eval() with torch.no_grad(): A = model(**self._prepare_for_class(_lowerCAmelCase , _lowerCAmelCase ) ) A = outputs.attentions self.assertEqual(len(_lowerCAmelCase ) , _lowerCAmelCase ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_heads[0], window_size_squared, window_size_squared] , ) A = len(_lowerCAmelCase ) # Check attention is always last and order is fine A = True A = True A = model_class(_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.eval() with torch.no_grad(): A = model(**self._prepare_for_class(_lowerCAmelCase , _lowerCAmelCase ) ) if hasattr(self.model_tester , """num_hidden_states_types""" ): A = self.model_tester.num_hidden_states_types else: # also another +1 for reshaped_hidden_states A = 2 self.assertEqual(out_len + added_hidden_states , len(_lowerCAmelCase ) ) A = outputs.attentions self.assertEqual(len(_lowerCAmelCase ) , _lowerCAmelCase ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_heads[0], window_size_squared, window_size_squared] , ) def A (self : List[Any] , _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : Tuple , _lowerCAmelCase : List[Any] , _lowerCAmelCase : List[str] ): A = model_class(_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.eval() with torch.no_grad(): A = model(**self._prepare_for_class(_lowerCAmelCase , _lowerCAmelCase ) ) A = outputs.hidden_states A = getattr( self.model_tester , """expected_num_hidden_layers""" , len(self.model_tester.depths ) + 1 ) self.assertEqual(len(_lowerCAmelCase ) , _lowerCAmelCase ) # Swinv2 has a different seq_length A = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) A = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) A = outputs.reshaped_hidden_states self.assertEqual(len(_lowerCAmelCase ) , _lowerCAmelCase ) A , A , A , A = reshaped_hidden_states[0].shape A = ( reshaped_hidden_states[0].view(_lowerCAmelCase , _lowerCAmelCase , height * width ).permute(0 , 2 , 1 ) ) self.assertListEqual( list(reshaped_hidden_states.shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) def A (self : Tuple ): A , A = self.model_tester.prepare_config_and_inputs_for_common() A = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) for model_class in self.all_model_classes: A = True self.check_hidden_states_output(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A = True self.check_hidden_states_output(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) def A (self : List[str] ): A , A = self.model_tester.prepare_config_and_inputs_for_common() A = 3 A = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) A = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) A = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0]) A = image_size[1] + patch_size[1] - (image_size[1] % patch_size[1]) for model_class in self.all_model_classes: A = True self.check_hidden_states_output(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , (padded_height, padded_width) ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A = True self.check_hidden_states_output(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , (padded_height, padded_width) ) def A (self : Optional[int] ): A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*_lowerCAmelCase ) def A (self : Union[str, Any] ): A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase ) @slow def A (self : Optional[Any] ): for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A = SwinvaModel.from_pretrained(_lowerCAmelCase ) self.assertIsNotNone(_lowerCAmelCase ) def A (self : Optional[Any] ): A , A = self.model_tester.prepare_config_and_inputs_for_common() A = _config_zero_init(_lowerCAmelCase ) for model_class in self.all_model_classes: A = model_class(config=_lowerCAmelCase ) for name, param in model.named_parameters(): if "embeddings" not in name and "logit_scale" not in name and param.requires_grad: self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=F"""Parameter {name} of model {model_class} seems not properly initialized""" , ) @require_vision @require_torch class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @cached_property def A (self : List[str] ): return ( AutoImageProcessor.from_pretrained("""microsoft/swinv2-tiny-patch4-window8-256""" ) if is_vision_available() else None ) @slow def A (self : List[str] ): A = SwinvaForImageClassification.from_pretrained("""microsoft/swinv2-tiny-patch4-window8-256""" ).to( _lowerCAmelCase ) A = self.default_image_processor A = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) A = image_processor(images=_lowerCAmelCase , return_tensors="""pt""" ).to(_lowerCAmelCase ) # forward pass with torch.no_grad(): A = model(**_lowerCAmelCase ) # verify the logits A = torch.Size((1, 1000) ) self.assertEqual(outputs.logits.shape , _lowerCAmelCase ) A = torch.tensor([-0.3_947, -0.4_306, 0.0_026] ).to(_lowerCAmelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _lowerCAmelCase , atol=1e-4 ) )
258
1
"""simple docstring""" import os from typing import List, Optional, Union from ...tokenization_utils import PreTrainedTokenizer from ...tokenization_utils_base import AddedToken from ...utils import logging _lowercase : Optional[Any] = logging.get_logger(__name__) _lowercase : Union[str, Any] = {'vocab_file': 'vocab.txt'} _lowercase : List[Any] = { 'vocab_file': { 'facebook/esm2_t6_8M_UR50D': 'https://huggingface.co/facebook/esm2_t6_8M_UR50D/resolve/main/vocab.txt', 'facebook/esm2_t12_35M_UR50D': 'https://huggingface.co/facebook/esm2_t12_35M_UR50D/resolve/main/vocab.txt', }, } _lowercase : List[Any] = { 'facebook/esm2_t6_8M_UR50D': 10_24, 'facebook/esm2_t12_35M_UR50D': 10_24, } def lowercase__ ( snake_case_ :Optional[int] ): with open(snake_case_ , '''r''' ) as f: __UpperCAmelCase = f.read().splitlines() return [l.strip() for l in lines] class _UpperCAmelCase ( _lowerCAmelCase ): a__ : Optional[int] = VOCAB_FILES_NAMES a__ : str = PRETRAINED_VOCAB_FILES_MAP a__ : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES a__ : Tuple = ["input_ids", "attention_mask"] def __init__( self : str , _lowercase : str , _lowercase : List[Any]="<unk>" , _lowercase : List[str]="<cls>" , _lowercase : str="<pad>" , _lowercase : List[Any]="<mask>" , _lowercase : Optional[Any]="<eos>" , **_lowercase : List[Any] , ): super().__init__(**_lowercase ) __UpperCAmelCase = load_vocab_file(_lowercase ) __UpperCAmelCase = dict(enumerate(self.all_tokens ) ) __UpperCAmelCase = {tok: ind for ind, tok in enumerate(self.all_tokens )} __UpperCAmelCase = unk_token __UpperCAmelCase = cls_token __UpperCAmelCase = pad_token __UpperCAmelCase = mask_token __UpperCAmelCase = eos_token __UpperCAmelCase = self.all_tokens self._create_trie(self.unique_no_split_tokens ) def a ( self : Tuple , _lowercase : int ): return self._id_to_token.get(_lowercase , self.unk_token ) def a ( self : Any , _lowercase : str ): return self._token_to_id.get(_lowercase , self._token_to_id.get(self.unk_token ) ) def a ( self : Union[str, Any] , _lowercase : Any , **_lowercase : Union[str, Any] ): return text.split() def a ( self : Union[str, Any] , _lowercase : Dict=False ): return len(self._id_to_token ) def a ( self : List[str] ): return {token: i for i, token in enumerate(self.all_tokens )} def a ( self : Any , _lowercase : str ): return self._token_to_id.get(_lowercase , self._token_to_id.get(self.unk_token ) ) def a ( self : List[str] , _lowercase : int ): return self._id_to_token.get(_lowercase , self.unk_token ) def a ( self : int , _lowercase : List[int] , _lowercase : Optional[List[int]] = None ): __UpperCAmelCase = [self.cls_token_id] __UpperCAmelCase = [self.eos_token_id] # No sep token in ESM vocabulary if token_ids_a is None: if self.eos_token_id is None: return cls + token_ids_a else: return cls + token_ids_a + sep elif self.eos_token_id is None: raise ValueError('''Cannot tokenize multiple sequences when EOS token is not set!''' ) return cls + token_ids_a + sep + token_ids_a + sep # Multiple inputs always have an EOS token def a ( self : Union[str, Any] , _lowercase : List , _lowercase : Optional[List] = None , _lowercase : bool = False ): if already_has_special_tokens: if token_ids_a is not None: raise ValueError( '''You should not supply a second sequence if the provided sequence of ''' '''ids is already formatted with special tokens for the model.''' ) return [1 if token in self.all_special_ids else 0 for token in token_ids_a] __UpperCAmelCase = [1] + ([0] * len(_lowercase )) + [1] if token_ids_a is not None: mask += [0] * len(_lowercase ) + [1] return mask def a ( self : Optional[Any] , _lowercase : Dict , _lowercase : str ): __UpperCAmelCase = os.path.join(_lowercase , (filename_prefix + '''-''' if filename_prefix else '''''') + '''vocab.txt''' ) with open(_lowercase , '''w''' ) as f: f.write('''\n'''.join(self.all_tokens ) ) return (vocab_file,) @property def a ( self : Optional[Any] ): return self.get_vocab_size(with_added_tokens=_lowercase ) def a ( self : Optional[Any] , _lowercase : Union[List[str], List[AddedToken]] , _lowercase : bool = False ): return super()._add_tokens(_lowercase , special_tokens=_lowercase )
86
"""simple docstring""" from binascii import hexlify from hashlib import shaaaa from os import urandom # RFC 3526 - More Modular Exponential (MODP) Diffie-Hellman groups for # Internet Key Exchange (IKE) https://tools.ietf.org/html/rfc3526 _lowercase : str = { # 1536-bit 5: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF', base=16, ), 'generator': 2, }, # 2048-bit 14: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' + '15728E5A8AACAA68FFFFFFFFFFFFFFFF', base=16, ), 'generator': 2, }, # 3072-bit 15: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' + '15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64' + 'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7' + 'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B' + 'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C' + 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31' + '43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF', base=16, ), 'generator': 2, }, # 4096-bit 16: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' + '15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64' + 'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7' + 'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B' + 'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C' + 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31' + '43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7' + '88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA' + '2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6' + '287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED' + '1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9' + '93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199' + 'FFFFFFFFFFFFFFFF', base=16, ), 'generator': 2, }, # 6144-bit 17: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08' + '8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B' + '302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9' + 'A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6' + '49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8' + 'FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C' + '180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718' + '3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D' + '04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D' + 'B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226' + '1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C' + 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC' + 'E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26' + '99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB' + '04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2' + '233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127' + 'D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492' + '36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406' + 'AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918' + 'DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151' + '2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03' + 'F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F' + 'BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA' + 'CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B' + 'B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632' + '387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E' + '6DCC4024FFFFFFFFFFFFFFFF', base=16, ), 'generator': 2, }, # 8192-bit 18: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' + '15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64' + 'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7' + 'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B' + 'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C' + 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31' + '43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7' + '88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA' + '2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6' + '287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED' + '1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9' + '93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492' + '36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD' + 'F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831' + '179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B' + 'DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF' + '5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6' + 'D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3' + '23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA' + 'CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328' + '06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C' + 'DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE' + '12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4' + '38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300' + '741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568' + '3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9' + '22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B' + '4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A' + '062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36' + '4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1' + 'B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92' + '4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47' + '9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71' + '60C980DD98EDD3DFFFFFFFFFFFFFFFFF', base=16, ), 'generator': 2, }, } class _UpperCAmelCase : def __init__( self : List[Any] , _lowercase : int = 14 ): if group not in primes: raise ValueError('''Unsupported Group''' ) __UpperCAmelCase = primes[group]['''prime'''] __UpperCAmelCase = primes[group]['''generator'''] __UpperCAmelCase = int(hexlify(urandom(32 ) ) , base=16 ) def a ( self : int ): return hex(self.__private_key )[2:] def a ( self : Dict ): __UpperCAmelCase = pow(self.generator , self.__private_key , self.prime ) return hex(_lowercase )[2:] def a ( self : Union[str, Any] , _lowercase : int ): # check if the other public key is valid based on NIST SP800-56 return ( 2 <= key <= self.prime - 2 and pow(_lowercase , (self.prime - 1) // 2 , self.prime ) == 1 ) def a ( self : Optional[Any] , _lowercase : str ): __UpperCAmelCase = int(_lowercase , base=16 ) if not self.is_valid_public_key(_lowercase ): raise ValueError('''Invalid public key''' ) __UpperCAmelCase = pow(_lowercase , self.__private_key , self.prime ) return shaaaa(str(_lowercase ).encode() ).hexdigest() @staticmethod def a ( _lowercase : int , _lowercase : int ): # check if the other public key is valid based on NIST SP800-56 return ( 2 <= remote_public_key_str <= prime - 2 and pow(_lowercase , (prime - 1) // 2 , _lowercase ) == 1 ) @staticmethod def a ( _lowercase : str , _lowercase : str , _lowercase : int = 14 ): __UpperCAmelCase = int(_lowercase , base=16 ) __UpperCAmelCase = int(_lowercase , base=16 ) __UpperCAmelCase = primes[group]['''prime'''] if not DiffieHellman.is_valid_public_key_static(_lowercase , _lowercase ): raise ValueError('''Invalid public key''' ) __UpperCAmelCase = pow(_lowercase , _lowercase , _lowercase ) return shaaaa(str(_lowercase ).encode() ).hexdigest() if __name__ == "__main__": import doctest doctest.testmod()
86
1
from __future__ import annotations import numpy as np from numpy import floataa from numpy.typing import NDArray def lowerCAmelCase_ ( __a , __a , __a , __a , ) -> List[str]: """simple docstring""" lowerCamelCase__ , lowerCamelCase__: Optional[int] =coefficient_matrix.shape lowerCamelCase__ , lowerCamelCase__: List[str] =constant_matrix.shape if rowsa != colsa: lowerCamelCase__: Optional[Any] =F"""Coefficient matrix dimensions must be nxn but received {rowsa}x{colsa}""" raise ValueError(__a ) if colsa != 1: lowerCamelCase__: Dict =F"""Constant matrix must be nx1 but received {rowsa}x{colsa}""" raise ValueError(__a ) if rowsa != rowsa: lowerCamelCase__: int =( "Coefficient and constant matrices dimensions must be nxn and nx1 but " F"""received {rowsa}x{colsa} and {rowsa}x{colsa}""" ) raise ValueError(__a ) if len(__a ) != rowsa: lowerCamelCase__: str =( "Number of initial values must be equal to number of rows in coefficient " F"""matrix but received {len(__a )} and {rowsa}""" ) raise ValueError(__a ) if iterations <= 0: raise ValueError("Iterations must be at least 1" ) lowerCamelCase__: Any =np.concatenate( (coefficient_matrix, constant_matrix) , axis=1 ) lowerCamelCase__ , lowerCamelCase__: str =table.shape strictly_diagonally_dominant(__a ) # Iterates the whole matrix for given number of times for _ in range(__a ): lowerCamelCase__: Any =[] for row in range(__a ): lowerCamelCase__: int =0 for col in range(__a ): if col == row: lowerCamelCase__: str =table[row][col] elif col == cols - 1: lowerCamelCase__: Tuple =table[row][col] else: temp += (-1) * table[row][col] * init_val[col] lowerCamelCase__: Optional[Any] =(temp + val) / denom new_val.append(__a ) lowerCamelCase__: Any =new_val return [float(__a ) for i in new_val] def lowerCAmelCase_ ( __a ) -> str: """simple docstring""" lowerCamelCase__ , lowerCamelCase__: Union[str, Any] =table.shape lowerCamelCase__: Union[str, Any] =True for i in range(0 , __a ): lowerCamelCase__: Dict =0 for j in range(0 , cols - 1 ): if i == j: continue else: total += table[i][j] if table[i][i] <= total: raise ValueError("Coefficient matrix is not strictly diagonally dominant" ) return is_diagonally_dominant # Test Cases if __name__ == "__main__": import doctest doctest.testmod()
10
from typing import Optional, Union import torch from torch import nn from ...configuration_utils import ConfigMixin, register_to_config from ...models.modeling_utils import ModelMixin class __lowerCAmelCase ( lowerCamelCase__ , lowerCamelCase__ ): @register_to_config def __init__( self , _snake_case = 768 , ): """simple docstring""" super().__init__() _lowerCAmelCase = nn.Parameter(torch.zeros(1 , _snake_case ) ) _lowerCAmelCase = nn.Parameter(torch.ones(1 , _snake_case ) ) def snake_case ( self , _snake_case = None , _snake_case = None , ): """simple docstring""" _lowerCAmelCase = nn.Parameter(self.mean.to(_snake_case ).to(_snake_case ) ) _lowerCAmelCase = nn.Parameter(self.std.to(_snake_case ).to(_snake_case ) ) return self def snake_case ( self , _snake_case ): """simple docstring""" _lowerCAmelCase = (embeds - self.mean) * 1.0 / self.std return embeds def snake_case ( self , _snake_case ): """simple docstring""" _lowerCAmelCase = (embeds * self.std) + self.mean return embeds
82
0
from typing import Optional, Tuple import jax import jax.numpy as jnp from flax import linen as nn from flax.core.frozen_dict import FrozenDict from transformers import CLIPConfig, FlaxPreTrainedModel from transformers.models.clip.modeling_flax_clip import FlaxCLIPVisionModule def UpperCAmelCase_( a__ , a__ , a__=1e-12 ): """simple docstring""" SCREAMING_SNAKE_CASE : Union[str, Any] = jnp.divide(emb_a.T , jnp.clip(jnp.linalg.norm(__SCREAMING_SNAKE_CASE , axis=1 ) , a_min=__SCREAMING_SNAKE_CASE ) ).T SCREAMING_SNAKE_CASE : Union[str, Any] = jnp.divide(emb_a.T , jnp.clip(jnp.linalg.norm(__SCREAMING_SNAKE_CASE , axis=1 ) , a_min=__SCREAMING_SNAKE_CASE ) ).T return jnp.matmul(__SCREAMING_SNAKE_CASE , norm_emb_a.T ) class a_ ( nn.Module ): """simple docstring""" __SCREAMING_SNAKE_CASE : CLIPConfig __SCREAMING_SNAKE_CASE : jnp.dtype = jnp.floataa def __lowerCAmelCase ( self ) ->str: SCREAMING_SNAKE_CASE : List[Any] = FlaxCLIPVisionModule(self.config.vision_config ) SCREAMING_SNAKE_CASE : Union[str, Any] = nn.Dense(self.config.projection_dim , use_bias=_lowerCAmelCase , dtype=self.dtype ) SCREAMING_SNAKE_CASE : Optional[Any] = self.param('''concept_embeds''' , jax.nn.initializers.ones , (17, self.config.projection_dim) ) SCREAMING_SNAKE_CASE : Optional[int] = self.param( '''special_care_embeds''' , jax.nn.initializers.ones , (3, self.config.projection_dim) ) SCREAMING_SNAKE_CASE : Union[str, Any] = self.param('''concept_embeds_weights''' , jax.nn.initializers.ones , (17,) ) SCREAMING_SNAKE_CASE : List[str] = self.param('''special_care_embeds_weights''' , jax.nn.initializers.ones , (3,) ) def __call__( self , _lowerCamelCase ) ->Tuple: SCREAMING_SNAKE_CASE : Tuple = self.vision_model(_lowerCAmelCase )[1] SCREAMING_SNAKE_CASE : int = self.visual_projection(_lowerCAmelCase ) SCREAMING_SNAKE_CASE : List[Any] = jax_cosine_distance(_lowerCAmelCase , self.special_care_embeds ) SCREAMING_SNAKE_CASE : Optional[int] = jax_cosine_distance(_lowerCAmelCase , self.concept_embeds ) # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign image inputs SCREAMING_SNAKE_CASE : List[str] = 0.0 SCREAMING_SNAKE_CASE : List[str] = special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment SCREAMING_SNAKE_CASE : Tuple = jnp.round(_lowerCAmelCase , 3 ) SCREAMING_SNAKE_CASE : Any = jnp.any(special_scores > 0 , axis=1 , keepdims=_lowerCAmelCase ) # Use a lower threshold if an image has any special care concept SCREAMING_SNAKE_CASE : str = is_special_care * 0.0_1 SCREAMING_SNAKE_CASE : str = cos_dist - self.concept_embeds_weights[None, :] + special_adjustment SCREAMING_SNAKE_CASE : Any = jnp.round(_lowerCAmelCase , 3 ) SCREAMING_SNAKE_CASE : Optional[int] = jnp.any(concept_scores > 0 , axis=1 ) return has_nsfw_concepts class a_ ( __UpperCamelCase ): """simple docstring""" __SCREAMING_SNAKE_CASE : Union[str, Any] = CLIPConfig __SCREAMING_SNAKE_CASE : List[str] = "clip_input" __SCREAMING_SNAKE_CASE : str = FlaxStableDiffusionSafetyCheckerModule def __init__( self , _lowerCamelCase , _lowerCamelCase = None , _lowerCamelCase = 0 , _lowerCamelCase = jnp.floataa , _lowerCamelCase = True , **_lowerCamelCase , ) ->Optional[Any]: if input_shape is None: SCREAMING_SNAKE_CASE : List[str] = (1, 224, 224, 3) SCREAMING_SNAKE_CASE : Dict = self.module_class(config=_lowerCAmelCase , dtype=_lowerCAmelCase , **_lowerCAmelCase ) super().__init__(_lowerCAmelCase , _lowerCAmelCase , input_shape=_lowerCAmelCase , seed=_lowerCAmelCase , dtype=_lowerCAmelCase , _do_init=_do_init ) def __lowerCAmelCase ( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase = None ) ->str: # init input tensor SCREAMING_SNAKE_CASE : int = jax.random.normal(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE : Dict = jax.random.split(_lowerCAmelCase ) SCREAMING_SNAKE_CASE : Dict = {"""params""": params_rng, """dropout""": dropout_rng} SCREAMING_SNAKE_CASE : Union[str, Any] = self.module.init(_lowerCAmelCase , _lowerCAmelCase )["""params"""] return random_params def __call__( self , _lowerCamelCase , _lowerCamelCase = None , ) ->Optional[int]: SCREAMING_SNAKE_CASE : Optional[int] = jnp.transpose(_lowerCAmelCase , (0, 2, 3, 1) ) return self.module.apply( {'''params''': params or self.params} , jnp.array(_lowerCAmelCase , dtype=jnp.floataa ) , rngs={} , )
369
from math import pi, sqrt, tan def UpperCAmelCase_( a__ ): """simple docstring""" if side_length < 0: raise ValueError('''surface_area_cube() only accepts non-negative values''' ) return 6 * side_length**2 def UpperCAmelCase_( a__ , a__ , a__ ): """simple docstring""" 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__ ): """simple docstring""" if radius < 0: raise ValueError('''surface_area_sphere() only accepts non-negative values''' ) return 4 * pi * radius**2 def UpperCAmelCase_( a__ ): """simple docstring""" if radius < 0: raise ValueError('''surface_area_hemisphere() only accepts non-negative values''' ) return 3 * pi * radius**2 def UpperCAmelCase_( a__ , a__ ): """simple docstring""" 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__ , a__ , a__ ): """simple docstring""" if radius_a < 0 or radius_a < 0 or height < 0: raise ValueError( '''surface_area_conical_frustum() only accepts non-negative values''' ) SCREAMING_SNAKE_CASE : Optional[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__ , a__ ): """simple docstring""" 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__ , a__ ): """simple docstring""" 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__ , a__ ): """simple docstring""" if length < 0 or width < 0: raise ValueError('''area_rectangle() only accepts non-negative values''' ) return length * width def UpperCAmelCase_( a__ ): """simple docstring""" if side_length < 0: raise ValueError('''area_square() only accepts non-negative values''' ) return side_length**2 def UpperCAmelCase_( a__ , a__ ): """simple docstring""" if base < 0 or height < 0: raise ValueError('''area_triangle() only accepts non-negative values''' ) return (base * height) / 2 def UpperCAmelCase_( a__ , a__ , a__ ): """simple docstring""" 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''' ) SCREAMING_SNAKE_CASE : int = (sidea + sidea + sidea) / 2 SCREAMING_SNAKE_CASE : List[str] = sqrt( semi_perimeter * (semi_perimeter - sidea) * (semi_perimeter - sidea) * (semi_perimeter - sidea) ) return area def UpperCAmelCase_( a__ , a__ ): """simple docstring""" if base < 0 or height < 0: raise ValueError('''area_parallelogram() only accepts non-negative values''' ) return base * height def UpperCAmelCase_( a__ , a__ , a__ ): """simple docstring""" 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__ ): """simple docstring""" if radius < 0: raise ValueError('''area_circle() only accepts non-negative values''' ) return pi * radius**2 def UpperCAmelCase_( a__ , a__ ): """simple docstring""" 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__ , a__ ): """simple docstring""" 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__ , a__ ): """simple docstring""" 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) = }")
19
0
"""simple docstring""" import inspect import unittest from transformers import MobileNetVaConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation, MobileNetVaModel from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import MobileNetVaImageProcessor class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(_a , '''tf_padding''' ) ) self.parent.assertTrue(hasattr(_a , '''depth_multiplier''' ) ) class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , _a=13 , _a=3 , _a=32 , _a=0.25 , _a=8 , _a=8 , _a=6 , _a=32 , _a=True , _a=True , _a=True , _a="relu6" , _a=1_280 , _a=0.1 , _a=0.02 , _a=True , _a=True , _a=10 , _a=None , ): __a = parent __a = batch_size __a = num_channels __a = image_size __a = depth_multiplier __a = depth_divisible_by __a = min_depth __a = expand_ratio __a = tf_padding __a = output_stride __a = first_layer_is_expansion __a = finegrained_output __a = hidden_act __a = last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier ) __a = classifier_dropout_prob __a = use_labels __a = is_training __a = num_labels __a = initializer_range __a = scope def __UpperCAmelCase ( self ): __a = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __a = None __a = None if self.use_labels: __a = ids_tensor([self.batch_size] , self.num_labels ) __a = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) __a = self.get_config() return config, pixel_values, labels, pixel_labels def __UpperCAmelCase ( self ): return MobileNetVaConfig( num_channels=self.num_channels , image_size=self.image_size , depth_multiplier=self.depth_multiplier , depth_divisible_by=self.depth_divisible_by , min_depth=self.min_depth , expand_ratio=self.expand_ratio , output_stride=self.output_stride , first_layer_is_expansion=self.first_layer_is_expansion , finegrained_output=self.finegrained_output , hidden_act=self.hidden_act , tf_padding=self.tf_padding , classifier_dropout_prob=self.classifier_dropout_prob , initializer_range=self.initializer_range , ) def __UpperCAmelCase ( self , _a , _a , _a , _a ): __a = MobileNetVaModel(config=_a ) model.to(_a ) model.eval() __a = model(_a ) self.parent.assertEqual( result.last_hidden_state.shape , ( self.batch_size, self.last_hidden_size, self.image_size // self.output_stride, self.image_size // self.output_stride, ) , ) self.parent.assertEqual( result.pooler_output.shape , (self.batch_size, self.last_hidden_size) , ) def __UpperCAmelCase ( self , _a , _a , _a , _a ): __a = self.num_labels __a = MobileNetVaForImageClassification(_a ) model.to(_a ) model.eval() __a = model(_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __UpperCAmelCase ( self , _a , _a , _a , _a ): __a = self.num_labels __a = MobileNetVaForSemanticSegmentation(_a ) model.to(_a ) model.eval() __a = model(_a ) self.parent.assertEqual( result.logits.shape , ( self.batch_size, self.num_labels, self.image_size // self.output_stride, self.image_size // self.output_stride, ) , ) __a = model(_a , labels=_a ) self.parent.assertEqual( result.logits.shape , ( self.batch_size, self.num_labels, self.image_size // self.output_stride, self.image_size // self.output_stride, ) , ) def __UpperCAmelCase ( self ): __a = self.prepare_config_and_inputs() __a , __a , __a , __a = config_and_inputs __a = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : int = ( (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation) if is_torch_available() else () ) __UpperCAmelCase : str = ( { 'feature-extraction': MobileNetVaModel, 'image-classification': MobileNetVaForImageClassification, 'image-segmentation': MobileNetVaForSemanticSegmentation, } if is_torch_available() else {} ) __UpperCAmelCase : List[str] = False __UpperCAmelCase : Optional[Any] = False __UpperCAmelCase : Optional[Any] = False __UpperCAmelCase : str = False def __UpperCAmelCase ( self ): __a = MobileNetVaModelTester(self ) __a = MobileNetVaConfigTester(self , config_class=_a , has_text_modality=_a ) def __UpperCAmelCase ( self ): self.config_tester.run_common_tests() @unittest.skip(reason='''MobileNetV2 does not use inputs_embeds''' ) def __UpperCAmelCase ( self ): pass @unittest.skip(reason='''MobileNetV2 does not support input and output embeddings''' ) def __UpperCAmelCase ( self ): pass @unittest.skip(reason='''MobileNetV2 does not output attentions''' ) def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): __a , __a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __a = model_class(_a ) __a = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic __a = [*signature.parameters.keys()] __a = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def __UpperCAmelCase ( self ): def check_hidden_states_output(_a , _a , _a ): __a = model_class(_a ) model.to(_a ) model.eval() with torch.no_grad(): __a = model(**self._prepare_for_class(_a , _a ) ) __a = outputs.hidden_states __a = 16 self.assertEqual(len(_a ) , _a ) __a , __a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __a = True check_hidden_states_output(_a , _a , _a ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] __a = True check_hidden_states_output(_a , _a , _a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*_a ) @slow def __UpperCAmelCase ( self ): for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __a = MobileNetVaModel.from_pretrained(_a ) self.assertIsNotNone(_a ) def lowercase ( ) -> Dict: __a = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' @cached_property def __UpperCAmelCase ( self ): return ( MobileNetVaImageProcessor.from_pretrained('''google/mobilenet_v2_1.0_224''' ) if is_vision_available() else None ) @slow def __UpperCAmelCase ( self ): __a = MobileNetVaForImageClassification.from_pretrained('''google/mobilenet_v2_1.0_224''' ).to(_a ) __a = self.default_image_processor __a = prepare_img() __a = image_processor(images=_a , return_tensors='''pt''' ).to(_a ) # forward pass with torch.no_grad(): __a = model(**_a ) # verify the logits __a = torch.Size((1, 1_001) ) self.assertEqual(outputs.logits.shape , _a ) __a = torch.tensor([0.2445, -1.1993, 0.1905] ).to(_a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _a , atol=1E-4 ) ) @slow def __UpperCAmelCase ( self ): __a = MobileNetVaForSemanticSegmentation.from_pretrained('''google/deeplabv3_mobilenet_v2_1.0_513''' ) __a = model.to(_a ) __a = MobileNetVaImageProcessor.from_pretrained('''google/deeplabv3_mobilenet_v2_1.0_513''' ) __a = prepare_img() __a = image_processor(images=_a , return_tensors='''pt''' ).to(_a ) # forward pass with torch.no_grad(): __a = model(**_a ) __a = outputs.logits # verify the logits __a = torch.Size((1, 21, 65, 65) ) self.assertEqual(logits.shape , _a ) __a = torch.tensor( [ [[17.5790, 17.7581, 18.3355], [18.3257, 18.4230, 18.8973], [18.6169, 18.8650, 19.2187]], [[-2.1595, -2.0977, -2.3741], [-2.4226, -2.3028, -2.6835], [-2.7819, -2.5991, -2.7706]], [[4.2058, 4.8317, 4.7638], [4.4136, 5.0361, 4.9383], [4.5028, 4.9644, 4.8734]], ] , device=_a , ) self.assertTrue(torch.allclose(logits[0, :3, :3, :3] , _a , atol=1E-4 ) )
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : int ) -> str: if number > 0: raise ValueError('''input must be a negative integer''' ) __a = len(bin(lowerCAmelCase__ )[3:] ) __a = bin(abs(lowerCAmelCase__ ) - (1 << binary_number_length) )[3:] __a = ( ( '''1''' + '''0''' * (binary_number_length - len(lowerCAmelCase__ )) + twos_complement_number ) if number < 0 else '''0''' ) return "0b" + twos_complement_number if __name__ == "__main__": import doctest doctest.testmod()
45
1
import numpy as np import torch from torch.utils.data import Dataset from utils import logger class _UpperCamelCase ( _A ): '''simple docstring''' def __init__( self : List[str] , snake_case_ : Tuple , snake_case_ : Any ): UpperCamelCase_: str = params UpperCamelCase_: Union[str, Any] = np.array(_A ) UpperCamelCase_: List[str] = np.array([len(_A ) for t in data] ) self.check() self.remove_long_sequences() self.remove_empty_sequences() self.remove_unknown_sequences() self.check() self.print_statistics() def __getitem__( self : Optional[Any] , snake_case_ : Union[str, Any] ): return (self.token_ids[index], self.lengths[index]) def __len__( self : List[str] ): return len(self.lengths ) def lowerCAmelCase__ ( self : Optional[Any] ): assert len(self.token_ids ) == len(self.lengths ) assert all(self.lengths[i] == len(self.token_ids[i] ) for i in range(len(self.lengths ) ) ) def lowerCAmelCase__ ( self : Dict ): UpperCamelCase_: Tuple = self.params.max_model_input_size UpperCamelCase_: Union[str, Any] = self.lengths > max_len logger.info(f'''Splitting {sum(_A )} too long sequences.''' ) def divide_chunks(snake_case_ : str , snake_case_ : List[Any] ): return [l[i : i + n] for i in range(0 , len(_A ) , _A )] UpperCamelCase_: Optional[int] = [] UpperCamelCase_: int = [] if self.params.mlm: UpperCamelCase_: int = self.params.special_tok_ids['cls_token'], self.params.special_tok_ids['sep_token'] else: UpperCamelCase_: Optional[Any] = self.params.special_tok_ids['bos_token'], self.params.special_tok_ids['eos_token'] for seq_, len_ in zip(self.token_ids , self.lengths ): assert (seq_[0] == cls_id) and (seq_[-1] == sep_id), seq_ if len_ <= max_len: new_tok_ids.append(seq_ ) new_lengths.append(len_ ) else: UpperCamelCase_: Optional[Any] = [] for sub_s in divide_chunks(seq_ , max_len - 2 ): if sub_s[0] != cls_id: UpperCamelCase_: Optional[Any] = np.insert(_A , 0 , _A ) if sub_s[-1] != sep_id: UpperCamelCase_: List[Any] = np.insert(_A , len(_A ) , _A ) assert len(_A ) <= max_len assert (sub_s[0] == cls_id) and (sub_s[-1] == sep_id), sub_s sub_seqs.append(_A ) new_tok_ids.extend(_A ) new_lengths.extend([len(_A ) for l in sub_seqs] ) UpperCamelCase_: Union[str, Any] = np.array(_A ) UpperCamelCase_: Union[str, Any] = np.array(_A ) def lowerCAmelCase__ ( self : Union[str, Any] ): UpperCamelCase_: List[str] = len(self ) UpperCamelCase_: Dict = self.lengths > 11 UpperCamelCase_: Union[str, Any] = self.token_ids[indices] UpperCamelCase_: str = self.lengths[indices] UpperCamelCase_: Any = len(self ) logger.info(f'''Remove {init_size - new_size} too short (<=11 tokens) sequences.''' ) def lowerCAmelCase__ ( self : int ): if "unk_token" not in self.params.special_tok_ids: return else: UpperCamelCase_: Optional[Any] = self.params.special_tok_ids['unk_token'] UpperCamelCase_: Optional[Any] = len(self ) UpperCamelCase_: Tuple = np.array([np.count_nonzero(a == unk_token_id ) for a in self.token_ids] ) UpperCamelCase_: Any = (unk_occs / self.lengths) < 0.5 UpperCamelCase_: Any = self.token_ids[indices] UpperCamelCase_: str = self.lengths[indices] UpperCamelCase_: Optional[Any] = len(self ) logger.info(f'''Remove {init_size - new_size} sequences with a high level of unknown tokens (50%).''' ) def lowerCAmelCase__ ( self : Any ): if not self.params.is_master: return logger.info(f'''{len(self )} sequences''' ) # data_len = sum(self.lengths) # nb_unique_tokens = len(Counter(list(chain(*self.token_ids)))) # logger.info(f'{data_len} tokens ({nb_unique_tokens} unique)') # unk_idx = self.params.special_tok_ids['unk_token'] # nb_unknown = sum([(t==unk_idx).sum() for t in self.token_ids]) # logger.info(f'{nb_unknown} unknown tokens (covering {100*nb_unknown/data_len:.2f}% of the data)') def lowerCAmelCase__ ( self : Dict , snake_case_ : Optional[Any] ): UpperCamelCase_: int = [t[0] for t in batch] UpperCamelCase_: Any = [t[1] for t in batch] assert len(_A ) == len(_A ) # Max for paddings UpperCamelCase_: Any = max(_A ) # Pad token ids if self.params.mlm: UpperCamelCase_: Optional[int] = self.params.special_tok_ids['pad_token'] else: UpperCamelCase_: Tuple = self.params.special_tok_ids['unk_token'] UpperCamelCase_: List[str] = [list(t.astype(_A ) ) + [pad_idx] * (max_seq_len_ - len(_A )) for t in token_ids] assert len(tk_ ) == len(_A ) assert all(len(_A ) == max_seq_len_ for t in tk_ ) UpperCamelCase_: Tuple = torch.tensor(tk_ ) # (bs, max_seq_len_) UpperCamelCase_: Any = torch.tensor(_A ) # (bs) return tk_t, lg_t
357
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_ : List[str] = False lowerCamelCase_ : int = logging.get_logger(__name__) lowerCamelCase_ : Optional[int] = """ybelkada/fonts""" def A__ ( ) -> Dict: 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 A__ ( lowerCamelCase , lowerCamelCase , lowerCamelCase ) -> Optional[int]: requires_backends(lowerCamelCase , ["""torch"""] ) _check_torch_version() UpperCamelCase_: Tuple = image_tensor.unsqueeze(0 ) UpperCamelCase_: Any = torch.nn.functional.unfold(lowerCamelCase , (patch_height, patch_width) , stride=(patch_height, patch_width) ) UpperCamelCase_: int = patches.reshape(image_tensor.size(0 ) , image_tensor.size(1 ) , lowerCamelCase , lowerCamelCase , -1 ) UpperCamelCase_: 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 A__ ( lowerCamelCase , lowerCamelCase = 36 , lowerCamelCase = "black" , lowerCamelCase = "white" , lowerCamelCase = 5 , lowerCamelCase = 5 , lowerCamelCase = 5 , lowerCamelCase = 5 , lowerCamelCase = None , lowerCamelCase = None , ) -> Image.Image: requires_backends(lowerCamelCase , """vision""" ) # Add new lines so that each line is no more than 80 characters. UpperCamelCase_: List[str] = textwrap.TextWrapper(width=80 ) UpperCamelCase_: Optional[int] = wrapper.wrap(text=lowerCamelCase ) UpperCamelCase_: List[str] = """\n""".join(lowerCamelCase ) if font_bytes is not None and font_path is None: UpperCamelCase_: List[Any] = io.BytesIO(lowerCamelCase ) elif font_path is not None: UpperCamelCase_: List[Any] = font_path else: UpperCamelCase_: Tuple = hf_hub_download(lowerCamelCase , """Arial.TTF""" ) UpperCamelCase_: Optional[Any] = ImageFont.truetype(lowerCamelCase , encoding="""UTF-8""" , size=lowerCamelCase ) # Use a temporary canvas to determine the width and height in pixels when # rendering the text. UpperCamelCase_: str = ImageDraw.Draw(Image.new("""RGB""" , (1, 1) , lowerCamelCase ) ) UpperCamelCase_, UpperCamelCase_, UpperCamelCase_, UpperCamelCase_: Optional[int] = temp_draw.textbbox((0, 0) , lowerCamelCase , lowerCamelCase ) # Create the actual image with a bit of padding around the text. UpperCamelCase_: Optional[int] = text_width + left_padding + right_padding UpperCamelCase_: List[str] = text_height + top_padding + bottom_padding UpperCamelCase_: Union[str, Any] = Image.new("""RGB""" , (image_width, image_height) , lowerCamelCase ) UpperCamelCase_: Optional[Any] = ImageDraw.Draw(lowerCamelCase ) draw.text(xy=(left_padding, top_padding) , text=lowerCamelCase , fill=lowerCamelCase , font=lowerCamelCase ) return image def A__ ( lowerCamelCase , lowerCamelCase , **lowerCamelCase ) -> List[str]: requires_backends(lowerCamelCase , """vision""" ) # Convert to PIL image if necessary UpperCamelCase_: List[str] = to_pil_image(lowerCamelCase ) UpperCamelCase_: Union[str, Any] = render_text(lowerCamelCase , **lowerCamelCase ) UpperCamelCase_: Tuple = max(header_image.width , image.width ) UpperCamelCase_: Tuple = int(image.height * (new_width / image.width) ) UpperCamelCase_: Dict = int(header_image.height * (new_width / header_image.width) ) UpperCamelCase_: str = 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 UpperCamelCase_: Optional[Any] = to_numpy_array(lowerCamelCase ) if infer_channel_dimension_format(lowerCamelCase ) == ChannelDimension.LAST: UpperCamelCase_: Tuple = to_channel_dimension_format(lowerCamelCase , ChannelDimension.LAST ) return new_image class _UpperCamelCase ( _A ): '''simple docstring''' __UpperCamelCase : Optional[int] = ["""flattened_patches"""] def __init__( self : int , snake_case_ : bool = True , snake_case_ : bool = True , snake_case_ : Dict[str, int] = None , snake_case_ : int = 2048 , snake_case_ : bool = False , **snake_case_ : Any , ): super().__init__(**snake_case_ ) UpperCamelCase_: int = patch_size if patch_size is not None else {"""height""": 16, """width""": 16} UpperCamelCase_: Tuple = do_normalize UpperCamelCase_: List[Any] = do_convert_rgb UpperCamelCase_: Tuple = max_patches UpperCamelCase_: Tuple = is_vqa def lowerCAmelCase__ ( self : int , snake_case_ : np.ndarray , snake_case_ : int , snake_case_ : dict , **snake_case_ : Tuple ): requires_backends(self.extract_flattened_patches , """torch""" ) _check_torch_version() # convert to torch UpperCamelCase_: int = to_channel_dimension_format(snake_case_ , ChannelDimension.FIRST ) UpperCamelCase_: List[str] = torch.from_numpy(snake_case_ ) UpperCamelCase_, UpperCamelCase_: List[Any] = patch_size["""height"""], patch_size["""width"""] UpperCamelCase_, UpperCamelCase_: Tuple = get_image_size(snake_case_ ) # maximize scale s.t. UpperCamelCase_: List[Any] = math.sqrt(max_patches * (patch_height / image_height) * (patch_width / image_width) ) UpperCamelCase_: Any = max(min(math.floor(scale * image_height / patch_height ) , snake_case_ ) , 1 ) UpperCamelCase_: List[str] = max(min(math.floor(scale * image_width / patch_width ) , snake_case_ ) , 1 ) UpperCamelCase_: int = max(num_feasible_rows * patch_height , 1 ) UpperCamelCase_: Optional[Any] = max(num_feasible_cols * patch_width , 1 ) UpperCamelCase_: str = torch.nn.functional.interpolate( image.unsqueeze(0 ) , size=(resized_height, resized_width) , mode="""bilinear""" , align_corners=snake_case_ , antialias=snake_case_ , ).squeeze(0 ) # [1, rows, columns, patch_height * patch_width * image_channels] UpperCamelCase_: List[str] = torch_extract_patches(snake_case_ , snake_case_ , snake_case_ ) UpperCamelCase_: List[Any] = patches.shape UpperCamelCase_: List[str] = patches_shape[1] UpperCamelCase_: Optional[Any] = patches_shape[2] UpperCamelCase_: List[str] = patches_shape[3] # [rows * columns, patch_height * patch_width * image_channels] UpperCamelCase_: Union[str, Any] = patches.reshape([rows * columns, depth] ) # [rows * columns, 1] UpperCamelCase_: Optional[Any] = torch.arange(snake_case_ ).reshape([rows, 1] ).repeat(1 , snake_case_ ).reshape([rows * columns, 1] ) UpperCamelCase_: Optional[int] = torch.arange(snake_case_ ).reshape([1, columns] ).repeat(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] UpperCamelCase_: Union[str, Any] = row_ids.to(torch.floataa ) UpperCamelCase_: str = col_ids.to(torch.floataa ) # [rows * columns, 2 + patch_height * patch_width * image_channels] UpperCamelCase_: Optional[Any] = torch.cat([row_ids, col_ids, patches] , -1 ) # [max_patches, 2 + patch_height * patch_width * image_channels] UpperCamelCase_: Tuple = torch.nn.functional.pad(snake_case_ , [0, 0, 0, max_patches - (rows * columns)] ).float() UpperCamelCase_: List[Any] = to_numpy_array(snake_case_ ) return result def lowerCAmelCase__ ( self : List[Any] , snake_case_ : np.ndarray , snake_case_ : Optional[Union[str, ChannelDimension]] = None , **snake_case_ : Tuple ): if image.dtype == np.uinta: UpperCamelCase_: List[str] = image.astype(np.floataa ) # take mean across the whole `image` UpperCamelCase_: str = np.mean(snake_case_ ) UpperCamelCase_: str = np.std(snake_case_ ) UpperCamelCase_: str = max(snake_case_ , 1.0 / math.sqrt(np.prod(image.shape ) ) ) return normalize(snake_case_ , mean=snake_case_ , std=snake_case_ , **snake_case_ ) def lowerCAmelCase__ ( self : str , snake_case_ : ImageInput , snake_case_ : Optional[str] = None , snake_case_ : bool = None , snake_case_ : Optional[bool] = None , snake_case_ : Optional[int] = None , snake_case_ : Optional[Dict[str, int]] = None , snake_case_ : Optional[Union[str, TensorType]] = None , snake_case_ : ChannelDimension = ChannelDimension.FIRST , **snake_case_ : Union[str, Any] , ): UpperCamelCase_: Tuple = do_normalize if do_normalize is not None else self.do_normalize UpperCamelCase_: Tuple = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb UpperCamelCase_: Optional[Any] = patch_size if patch_size is not None else self.patch_size UpperCamelCase_: Optional[int] = max_patches if max_patches is not None else self.max_patches UpperCamelCase_: Tuple = self.is_vqa if kwargs.get("""data_format""" , snake_case_ ) is not None: raise ValueError("""data_format is not an accepted input as the outputs are """ ) UpperCamelCase_: Dict = make_list_of_images(snake_case_ ) if not valid_images(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: UpperCamelCase_: str = [convert_to_rgb(snake_case_ ) for image in images] # All transformations expect numpy arrays. UpperCamelCase_: Union[str, Any] = [to_numpy_array(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.""" ) UpperCamelCase_: List[Any] = kwargs.pop("""font_bytes""" , snake_case_ ) UpperCamelCase_: List[Any] = kwargs.pop("""font_path""" , snake_case_ ) if isinstance(snake_case_ , snake_case_ ): UpperCamelCase_: str = [header_text] * len(snake_case_ ) UpperCamelCase_: str = [ render_header(snake_case_ , header_text[i] , font_bytes=snake_case_ , font_path=snake_case_ ) for i, image in enumerate(snake_case_ ) ] if do_normalize: UpperCamelCase_: Union[str, Any] = [self.normalize(image=snake_case_ ) for image in images] # convert to torch tensor and permute UpperCamelCase_: str = [ self.extract_flattened_patches(image=snake_case_ , max_patches=snake_case_ , patch_size=snake_case_ ) for image in images ] # create attention mask in numpy UpperCamelCase_: List[Any] = [(image.sum(axis=-1 ) != 0).astype(np.floataa ) for image in images] UpperCamelCase_: Optional[Any] = BatchFeature( data={"""flattened_patches""": images, """attention_mask""": attention_masks} , tensor_type=snake_case_ ) return encoded_outputs
223
0
import math from typing import Dict, Iterable, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, get_image_size, is_torch_available, is_torch_tensor, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_torch_available(): import torch if is_vision_available(): import PIL _snake_case = logging.get_logger(__name__) def A ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): '''simple docstring''' def constraint_to_multiple_of(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase=0 , _lowerCamelCase=None ): _lowerCAmelCase : Tuple = round(val / multiple ) * multiple if max_val is not None and x > max_val: _lowerCAmelCase : Optional[int] = math.floor(val / multiple ) * multiple if x < min_val: _lowerCAmelCase : List[str] = math.ceil(val / multiple ) * multiple return x _lowerCAmelCase : Union[str, Any] = (output_size, output_size) if isinstance(_lowerCamelCase , _lowerCamelCase ) else output_size _lowerCAmelCase , _lowerCAmelCase : Optional[Any] = get_image_size(_lowerCamelCase ) _lowerCAmelCase , _lowerCAmelCase : Any = output_size # determine new height and width _lowerCAmelCase : List[Any] = output_height / input_height _lowerCAmelCase : Any = output_width / input_width if keep_aspect_ratio: # scale as little as possible if abs(1 - scale_width ) < abs(1 - scale_height ): # fit width _lowerCAmelCase : Union[str, Any] = scale_width else: # fit height _lowerCAmelCase : Union[str, Any] = scale_height _lowerCAmelCase : List[str] = constraint_to_multiple_of(scale_height * input_height , multiple=_lowerCamelCase ) _lowerCAmelCase : Dict = constraint_to_multiple_of(scale_width * input_width , multiple=_lowerCamelCase ) return (new_height, new_width) class UpperCAmelCase_ ( a): lowerCamelCase__ = ['pixel_values'] def __init__( self, __a = True, __a = None, __a = PILImageResampling.BILINEAR, __a = False, __a = 1, __a = True, __a = 1 / 255, __a = True, __a = None, __a = None, **__a, ): '''simple docstring''' super().__init__(**__a) _lowerCAmelCase : Any = size if size is not None else {"height": 384, "width": 384} _lowerCAmelCase : Optional[int] = get_size_dict(__a) _lowerCAmelCase : Optional[Any] = do_resize _lowerCAmelCase : Dict = size _lowerCAmelCase : Any = keep_aspect_ratio _lowerCAmelCase : str = ensure_multiple_of _lowerCAmelCase : str = resample _lowerCAmelCase : Dict = do_rescale _lowerCAmelCase : Optional[int] = rescale_factor _lowerCAmelCase : Dict = do_normalize _lowerCAmelCase : Dict = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN _lowerCAmelCase : int = image_std if image_std is not None else IMAGENET_STANDARD_STD def snake_case__ ( self, __a, __a, __a = False, __a = 1, __a = PILImageResampling.BICUBIC, __a = None, **__a, ): '''simple docstring''' _lowerCAmelCase : List[Any] = get_size_dict(__a) if "height" not in size or "width" not in size: raise ValueError(f"The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}") _lowerCAmelCase : List[Any] = get_resize_output_image_size( __a, output_size=(size["height"], size["width"]), keep_aspect_ratio=__a, multiple=__a, ) return resize(__a, size=__a, resample=__a, data_format=__a, **__a) def snake_case__ ( self, __a, __a, __a = None, **__a, ): '''simple docstring''' return rescale(__a, scale=__a, data_format=__a, **__a) def snake_case__ ( self, __a, __a, __a, __a = None, **__a, ): '''simple docstring''' return normalize(__a, mean=__a, std=__a, data_format=__a, **__a) def snake_case__ ( self, __a, __a = None, __a = None, __a = None, __a = None, __a = None, __a = None, __a = None, __a = None, __a = None, __a = None, __a = None, __a = ChannelDimension.FIRST, **__a, ): '''simple docstring''' _lowerCAmelCase : int = do_resize if do_resize is not None else self.do_resize _lowerCAmelCase : List[Any] = size if size is not None else self.size _lowerCAmelCase : str = get_size_dict(__a) _lowerCAmelCase : Dict = keep_aspect_ratio if keep_aspect_ratio is not None else self.keep_aspect_ratio _lowerCAmelCase : Any = ensure_multiple_of if ensure_multiple_of is not None else self.ensure_multiple_of _lowerCAmelCase : int = resample if resample is not None else self.resample _lowerCAmelCase : Union[str, Any] = do_rescale if do_rescale is not None else self.do_rescale _lowerCAmelCase : Tuple = rescale_factor if rescale_factor is not None else self.rescale_factor _lowerCAmelCase : List[str] = do_normalize if do_normalize is not None else self.do_normalize _lowerCAmelCase : Dict = image_mean if image_mean is not None else self.image_mean _lowerCAmelCase : List[str] = image_std if image_std is not None else self.image_std _lowerCAmelCase : Optional[Any] = make_list_of_images(__a) if not valid_images(__a): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray.") if do_resize and size is None or resample is None: raise ValueError("Size and resample must be specified if do_resize is True.") if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True.") if do_normalize and (image_mean is None or image_std is None): raise ValueError("Image mean and std must be specified if do_normalize is True.") # All transformations expect numpy arrays. _lowerCAmelCase : List[Any] = [to_numpy_array(__a) for image in images] if do_resize: _lowerCAmelCase : Any = [self.resize(image=__a, size=__a, resample=__a) for image in images] if do_rescale: _lowerCAmelCase : List[str] = [self.rescale(image=__a, scale=__a) for image in images] if do_normalize: _lowerCAmelCase : Dict = [self.normalize(image=__a, mean=__a, std=__a) for image in images] _lowerCAmelCase : List[str] = [to_channel_dimension_format(__a, __a) for image in images] _lowerCAmelCase : Optional[Any] = {"pixel_values": images} return BatchFeature(data=__a, tensor_type=__a) def snake_case__ ( self, __a, __a = None): '''simple docstring''' _lowerCAmelCase : Optional[Any] = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(__a) != len(__a): raise ValueError( "Make sure that you pass in as many target sizes as the batch dimension of the logits") if is_torch_tensor(__a): _lowerCAmelCase : List[Any] = target_sizes.numpy() _lowerCAmelCase : Dict = [] for idx in range(len(__a)): _lowerCAmelCase : int = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0), size=target_sizes[idx], mode="bilinear", align_corners=__a) _lowerCAmelCase : int = resized_logits[0].argmax(dim=0) semantic_segmentation.append(__a) else: _lowerCAmelCase : Dict = logits.argmax(dim=1) _lowerCAmelCase : str = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0])] return semantic_segmentation
36
from __future__ import annotations from collections import deque class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , SCREAMING_SNAKE_CASE_ )-> int: '''simple docstring''' __UpperCamelCase = [] self.adlist.append( {'''value''': '''''', '''next_states''': [], '''fail_state''': 0, '''output''': []} ) for keyword in keywords: self.add_keyword(SCREAMING_SNAKE_CASE_ ) self.set_fail_transitions() def A__ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )-> int | None: '''simple docstring''' for state in self.adlist[current_state]["next_states"]: if char == self.adlist[state]["value"]: return state return None def A__ ( self , SCREAMING_SNAKE_CASE_ )-> None: '''simple docstring''' __UpperCamelCase = 0 for character in keyword: __UpperCamelCase = self.find_next_state(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if next_state is None: self.adlist.append( { '''value''': character, '''next_states''': [], '''fail_state''': 0, '''output''': [], } ) self.adlist[current_state]["next_states"].append(len(self.adlist ) - 1 ) __UpperCamelCase = len(self.adlist ) - 1 else: __UpperCamelCase = next_state self.adlist[current_state]["output"].append(SCREAMING_SNAKE_CASE_ ) def A__ ( self )-> None: '''simple docstring''' __UpperCamelCase = deque() for node in self.adlist[0]["next_states"]: q.append(SCREAMING_SNAKE_CASE_ ) __UpperCamelCase = 0 while q: __UpperCamelCase = q.popleft() for child in self.adlist[r]["next_states"]: q.append(SCREAMING_SNAKE_CASE_ ) __UpperCamelCase = self.adlist[r]['''fail_state'''] while ( self.find_next_state(SCREAMING_SNAKE_CASE_ , self.adlist[child]['''value'''] ) is None and state != 0 ): __UpperCamelCase = self.adlist[state]['''fail_state'''] __UpperCamelCase = self.find_next_state( SCREAMING_SNAKE_CASE_ , self.adlist[child]['''value'''] ) if self.adlist[child]["fail_state"] is None: __UpperCamelCase = 0 __UpperCamelCase = ( self.adlist[child]['''output'''] + self.adlist[self.adlist[child]['''fail_state''']]['''output'''] ) def A__ ( self , SCREAMING_SNAKE_CASE_ )-> dict[str, list[int]]: '''simple docstring''' __UpperCamelCase = {} # returns a dict with keywords and list of its occurrences __UpperCamelCase = 0 for i in range(len(SCREAMING_SNAKE_CASE_ ) ): while ( self.find_next_state(SCREAMING_SNAKE_CASE_ , string[i] ) is None and current_state != 0 ): __UpperCamelCase = self.adlist[current_state]['''fail_state'''] __UpperCamelCase = self.find_next_state(SCREAMING_SNAKE_CASE_ , string[i] ) if next_state is None: __UpperCamelCase = 0 else: __UpperCamelCase = next_state for key in self.adlist[current_state]["output"]: if key not in result: __UpperCamelCase = [] result[key].append(i - len(SCREAMING_SNAKE_CASE_ ) + 1 ) return result if __name__ == "__main__": import doctest doctest.testmod()
328
0
"""simple docstring""" from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import center_crop, normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL __A = logging.get_logger(__name__) class lowerCamelCase__ ( __magic_name__ ): '''simple docstring''' lowerCamelCase = ['''pixel_values'''] def __init__( self , __UpperCAmelCase = True , __UpperCAmelCase = None , __UpperCAmelCase = PIL.Image.BICUBIC , __UpperCAmelCase = True , __UpperCAmelCase = None , __UpperCAmelCase = 1 / 2_55 , __UpperCAmelCase = True , __UpperCAmelCase = True , __UpperCAmelCase = None , __UpperCAmelCase = None , **__UpperCAmelCase , ) -> None: super().__init__(**__UpperCAmelCase ) _lowerCAmelCase =size if size is not None else {"""height""": 2_56, """width""": 2_56} _lowerCAmelCase =get_size_dict(__UpperCAmelCase ) _lowerCAmelCase =crop_size if crop_size is not None else {"""height""": 2_24, """width""": 2_24} _lowerCAmelCase =get_size_dict(__UpperCAmelCase , param_name="""crop_size""" ) _lowerCAmelCase =do_resize _lowerCAmelCase =size _lowerCAmelCase =resample _lowerCAmelCase =do_center_crop _lowerCAmelCase =crop_size _lowerCAmelCase =do_rescale _lowerCAmelCase =rescale_factor _lowerCAmelCase =do_normalize _lowerCAmelCase =image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN _lowerCAmelCase =image_std if image_std is not None else IMAGENET_STANDARD_STD def _lowerCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = PIL.Image.BICUBIC , __UpperCAmelCase = None , **__UpperCAmelCase , ) -> np.ndarray: _lowerCAmelCase =get_size_dict(__UpperCAmelCase ) if "height" not in size or "width" not in size: raise ValueError(f'''The size dictionary must have keys \'height\' and \'width\'. Got {size.keys()}''' ) return resize( __UpperCAmelCase , size=(size["""height"""], size["""width"""]) , resample=__UpperCAmelCase , data_format=__UpperCAmelCase , **__UpperCAmelCase ) def _lowerCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = None , **__UpperCAmelCase , ) -> np.ndarray: _lowerCAmelCase =get_size_dict(__UpperCAmelCase ) if "height" not in size or "width" not in size: raise ValueError(f'''The size dictionary must have keys \'height\' and \'width\'. Got {size.keys()}''' ) return center_crop(__UpperCAmelCase , size=(size["""height"""], size["""width"""]) , data_format=__UpperCAmelCase , **__UpperCAmelCase ) def _lowerCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = None , **__UpperCAmelCase , ) -> Optional[Any]: return rescale(__UpperCAmelCase , scale=__UpperCAmelCase , data_format=__UpperCAmelCase , **__UpperCAmelCase ) def _lowerCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = None , **__UpperCAmelCase , ) -> np.ndarray: return normalize(__UpperCAmelCase , mean=__UpperCAmelCase , std=__UpperCAmelCase , data_format=__UpperCAmelCase , **__UpperCAmelCase ) def _lowerCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase = None , __UpperCAmelCase = None , __UpperCAmelCase=None , __UpperCAmelCase = None , __UpperCAmelCase = None , __UpperCAmelCase = None , __UpperCAmelCase = None , __UpperCAmelCase = None , __UpperCAmelCase = None , __UpperCAmelCase = None , __UpperCAmelCase = None , __UpperCAmelCase = ChannelDimension.FIRST , **__UpperCAmelCase , ) -> PIL.Image.Image: _lowerCAmelCase =do_resize if do_resize is not None else self.do_resize _lowerCAmelCase =resample if resample is not None else self.resample _lowerCAmelCase =do_center_crop if do_center_crop is not None else self.do_center_crop _lowerCAmelCase =do_rescale if do_rescale is not None else self.do_rescale _lowerCAmelCase =rescale_factor if rescale_factor is not None else self.rescale_factor _lowerCAmelCase =do_normalize if do_normalize is not None else self.do_normalize _lowerCAmelCase =image_mean if image_mean is not None else self.image_mean _lowerCAmelCase =image_std if image_std is not None else self.image_std _lowerCAmelCase =size if size is not None else self.size _lowerCAmelCase =get_size_dict(__UpperCAmelCase ) _lowerCAmelCase =crop_size if crop_size is not None else self.crop_size _lowerCAmelCase =get_size_dict(__UpperCAmelCase , param_name="""crop_size""" ) _lowerCAmelCase =make_list_of_images(__UpperCAmelCase ) if not valid_images(__UpperCAmelCase ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None or resample is None: raise ValueError("""Size and resample must be specified if do_resize is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("""Image mean and std must be specified if do_normalize is True.""" ) # All transformations expect numpy arrays. _lowerCAmelCase =[to_numpy_array(__UpperCAmelCase ) for image in images] if do_resize: _lowerCAmelCase =[self.resize(image=__UpperCAmelCase , size=__UpperCAmelCase , resample=__UpperCAmelCase ) for image in images] if do_center_crop: _lowerCAmelCase =[self.center_crop(image=__UpperCAmelCase , size=__UpperCAmelCase ) for image in images] if do_rescale: _lowerCAmelCase =[self.rescale(image=__UpperCAmelCase , scale=__UpperCAmelCase ) for image in images] if do_normalize: _lowerCAmelCase =[self.normalize(image=__UpperCAmelCase , mean=__UpperCAmelCase , std=__UpperCAmelCase ) for image in images] _lowerCAmelCase =[to_channel_dimension_format(__UpperCAmelCase , __UpperCAmelCase ) for image in images] _lowerCAmelCase ={"""pixel_values""": images} return BatchFeature(data=__UpperCAmelCase , tensor_type=__UpperCAmelCase )
341
"""simple docstring""" import unittest from transformers import TrOCRConfig from transformers.testing_utils import is_torch_available, require_torch, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers.models.trocr.modeling_trocr import TrOCRDecoder, TrOCRForCausalLM @require_torch class lowerCamelCase__ : '''simple docstring''' def __init__( self , __UpperCAmelCase , __UpperCAmelCase=99 , __UpperCAmelCase=13 , __UpperCAmelCase=16 , __UpperCAmelCase=7 , __UpperCAmelCase=True , __UpperCAmelCase=True , __UpperCAmelCase=True , __UpperCAmelCase=False , __UpperCAmelCase=True , __UpperCAmelCase=2 , __UpperCAmelCase=32 , __UpperCAmelCase=4 , __UpperCAmelCase=4 , __UpperCAmelCase=30 , __UpperCAmelCase=0 , __UpperCAmelCase=1 , __UpperCAmelCase=2 , __UpperCAmelCase=None , ) -> Any: _lowerCAmelCase =parent _lowerCAmelCase =batch_size _lowerCAmelCase =decoder_seq_length # For common tests _lowerCAmelCase =self.decoder_seq_length _lowerCAmelCase =is_training _lowerCAmelCase =use_attention_mask _lowerCAmelCase =use_labels _lowerCAmelCase =vocab_size _lowerCAmelCase =d_model _lowerCAmelCase =d_model _lowerCAmelCase =decoder_layers _lowerCAmelCase =decoder_layers _lowerCAmelCase =decoder_ffn_dim _lowerCAmelCase =decoder_attention_heads _lowerCAmelCase =decoder_attention_heads _lowerCAmelCase =eos_token_id _lowerCAmelCase =bos_token_id _lowerCAmelCase =pad_token_id _lowerCAmelCase =decoder_start_token_id _lowerCAmelCase =use_cache _lowerCAmelCase =max_position_embeddings _lowerCAmelCase =None _lowerCAmelCase =decoder_seq_length _lowerCAmelCase =2 _lowerCAmelCase =1 def _lowerCAmelCase ( self ) -> Tuple: _lowerCAmelCase =ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) _lowerCAmelCase =None if self.use_attention_mask: _lowerCAmelCase =ids_tensor([self.batch_size, self.decoder_seq_length] , vocab_size=2 ) _lowerCAmelCase =None if self.use_labels: _lowerCAmelCase =ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) _lowerCAmelCase =TrOCRConfig( vocab_size=self.vocab_size , d_model=self.d_model , decoder_layers=self.decoder_layers , decoder_ffn_dim=self.decoder_ffn_dim , decoder_attention_heads=self.decoder_attention_heads , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , use_cache=self.use_cache , pad_token_id=self.pad_token_id , decoder_start_token_id=self.decoder_start_token_id , max_position_embeddings=self.max_position_embeddings , ) return (config, input_ids, attention_mask, lm_labels) def _lowerCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , ) -> List[Any]: _lowerCAmelCase =True _lowerCAmelCase =TrOCRDecoder(config=__UpperCAmelCase ).to(__UpperCAmelCase ).eval() _lowerCAmelCase =input_ids[:2] input_ids[input_ids == 0] += 1 # first forward pass _lowerCAmelCase =model(__UpperCAmelCase , use_cache=__UpperCAmelCase ) _lowerCAmelCase =model(__UpperCAmelCase ) _lowerCAmelCase =model(__UpperCAmelCase , use_cache=__UpperCAmelCase ) self.parent.assertTrue(len(__UpperCAmelCase ) == len(__UpperCAmelCase ) ) self.parent.assertTrue(len(__UpperCAmelCase ) == len(__UpperCAmelCase ) + 1 ) _lowerCAmelCase =outputs["""past_key_values"""] # create hypothetical next token and extent to next_input_ids _lowerCAmelCase =ids_tensor((2, 1) , config.vocab_size - 1 ) + 1 # append to next input_ids and _lowerCAmelCase =torch.cat([input_ids, next_tokens] , dim=-1 ) _lowerCAmelCase =model(__UpperCAmelCase )["""last_hidden_state"""] _lowerCAmelCase =model(__UpperCAmelCase , past_key_values=__UpperCAmelCase )["""last_hidden_state"""] # select random slice _lowerCAmelCase =ids_tensor((1,) , output_from_past.shape[-1] ).item() _lowerCAmelCase =output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach() _lowerCAmelCase =output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice assert torch.allclose(__UpperCAmelCase , __UpperCAmelCase , atol=1e-3 ) def _lowerCAmelCase ( self ) -> List[str]: _lowerCAmelCase =self.prepare_config_and_inputs() _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase =config_and_inputs _lowerCAmelCase ={"""input_ids""": input_ids, """attention_mask""": attention_mask} return config, inputs_dict @require_torch class lowerCamelCase__ ( __magic_name__ , __magic_name__ , __magic_name__ , unittest.TestCase ): '''simple docstring''' lowerCamelCase = (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else () lowerCamelCase = (TrOCRForCausalLM,) if is_torch_available() else () lowerCamelCase = {'''text-generation''': TrOCRForCausalLM} if is_torch_available() else {} lowerCamelCase = True lowerCamelCase = False def _lowerCAmelCase ( self ) -> int: _lowerCAmelCase =TrOCRStandaloneDecoderModelTester(self , is_training=__UpperCAmelCase ) _lowerCAmelCase =ConfigTester(self , config_class=__UpperCAmelCase ) def _lowerCAmelCase ( self ) -> List[str]: pass def _lowerCAmelCase ( self ) -> List[Any]: pass def _lowerCAmelCase ( self ) -> Any: pass def _lowerCAmelCase ( self ) -> Optional[Any]: self.config_tester.run_common_tests() def _lowerCAmelCase ( self ) -> Any: _lowerCAmelCase =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past(*__UpperCAmelCase ) def _lowerCAmelCase ( self ) -> Tuple: return @unittest.skip("""The model doesn't support left padding""" ) # and it's not used enough to be worth fixing :) def _lowerCAmelCase ( self ) -> str: pass
341
1
'''simple docstring''' def lowerCAmelCase_ ( snake_case_ : str ) -> bool: '''simple docstring''' return credit_card_number.startswith(("34", "35", "37", "4", "5", "6") ) def lowerCAmelCase_ ( snake_case_ : str ) -> bool: '''simple docstring''' UpperCAmelCase_ = credit_card_number UpperCAmelCase_ = 0 UpperCAmelCase_ = len(snake_case_ ) - 2 for i in range(snake_case_ , -1 , -2 ): # double the value of every second digit UpperCAmelCase_ = int(cc_number[i] ) digit *= 2 # If doubling of a number results in a two digit number # i.e greater than 9(e.g., 6 × 2 = 12), # then add the digits of the product (e.g., 12: 1 + 2 = 3, 15: 1 + 5 = 6), # to get a single digit number. if digit > 9: digit %= 10 digit += 1 UpperCAmelCase_ = cc_number[:i] + str(snake_case_ ) + cc_number[i + 1 :] total += digit # Sum up the remaining digits for i in range(len(snake_case_ ) - 1 , -1 , -2 ): total += int(cc_number[i] ) return total % 10 == 0 def lowerCAmelCase_ ( snake_case_ : str ) -> bool: '''simple docstring''' UpperCAmelCase_ = f"""{credit_card_number} is an invalid credit card number because""" if not credit_card_number.isdigit(): print(f"""{error_message} it has nonnumerical characters.""" ) return False if not 13 <= len(snake_case_ ) <= 16: print(f"""{error_message} of its length.""" ) return False if not validate_initial_digits(snake_case_ ): print(f"""{error_message} of its first two digits.""" ) return False if not luhn_validation(snake_case_ ): print(f"""{error_message} it fails the Luhn check.""" ) return False print(f"""{credit_card_number} is a valid credit card number.""" ) return True if __name__ == "__main__": import doctest doctest.testmod() validate_credit_card_number('4111111111111111') validate_credit_card_number('32323')
1
import os import unittest from transformers.models.bartpho.tokenization_bartpho import VOCAB_FILES_NAMES, BartphoTokenizer from transformers.testing_utils import get_tests_dir from ...test_tokenization_common import TokenizerTesterMixin a_ = get_tests_dir('''fixtures/test_sentencepiece_bpe.model''') class lowercase__ ( _UpperCAmelCase, unittest.TestCase ): a_ =BartphoTokenizer a_ =False a_ =True def UpperCAmelCase ( self )-> Dict: '''simple docstring''' super().setUp() lowerCAmelCase__ = ["▁This", "▁is", "▁a", "▁t", "est"] lowerCAmelCase__ = dict(zip(__UpperCAmelCase , range(len(__UpperCAmelCase ) ) ) ) lowerCAmelCase__ = {"unk_token": "<unk>"} lowerCAmelCase__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["monolingual_vocab_file"] ) with open(self.monolingual_vocab_file , "w" , encoding="utf-8" ) as fp: for token in vocab_tokens: fp.write(F"{token} {vocab_tokens[token]}\n" ) lowerCAmelCase__ = BartphoTokenizer(__UpperCAmelCase , self.monolingual_vocab_file , **self.special_tokens_map ) tokenizer.save_pretrained(self.tmpdirname ) def UpperCAmelCase ( self , **__UpperCAmelCase )-> Union[str, Any]: '''simple docstring''' kwargs.update(self.special_tokens_map ) return BartphoTokenizer.from_pretrained(self.tmpdirname , **__UpperCAmelCase ) def UpperCAmelCase ( self , __UpperCAmelCase )-> Union[str, Any]: '''simple docstring''' lowerCAmelCase__ = "This is a là test" lowerCAmelCase__ = "This is a<unk><unk> test" return input_text, output_text def UpperCAmelCase ( self )-> Optional[Any]: '''simple docstring''' lowerCAmelCase__ = BartphoTokenizer(__UpperCAmelCase , self.monolingual_vocab_file , **self.special_tokens_map ) lowerCAmelCase__ = "This is a là test" lowerCAmelCase__ = "▁This ▁is ▁a ▁l à ▁t est".split() lowerCAmelCase__ = tokenizer.tokenize(__UpperCAmelCase ) self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase ) lowerCAmelCase__ = tokens + [tokenizer.unk_token] lowerCAmelCase__ = [4, 5, 6, 3, 3, 7, 8, 3] self.assertListEqual(tokenizer.convert_tokens_to_ids(__UpperCAmelCase ) , __UpperCAmelCase )
340
0
"""simple docstring""" def __A ( a_ :Any) -> int: if collection == []: return [] # get some information about the collection __a : List[str] = len(lowercase__) __a : List[str] = max(lowercase__) __a : int = min(lowercase__) # create the counting array __a : Dict = coll_max + 1 - coll_min __a : Optional[Any] = [0] * counting_arr_length # count how much a number appears in the collection for number in collection: counting_arr[number - coll_min] += 1 # sum each position with it's predecessors. now, counting_arr[i] tells # us how many elements <= i has in the collection for i in range(1 , lowercase__): __a : Tuple = counting_arr[i] + counting_arr[i - 1] # create the output collection __a : Tuple = [0] * coll_len # place the elements in the output, respecting the original order (stable # sort) from end to begin, updating counting_arr for i in reversed(range(0 , lowercase__)): __a : List[Any] = collection[i] counting_arr[collection[i] - coll_min] -= 1 return ordered def __A ( a_ :Any) -> int: return "".join([chr(lowercase__) for i in counting_sort([ord(lowercase__) for c in string])]) if __name__ == "__main__": # Test string sort assert counting_sort_string('''thisisthestring''') == "eghhiiinrsssttt" A = input('''Enter numbers separated by a comma:\n''').strip() A = [int(item) for item in user_input.split(''',''')] print(counting_sort(unsorted))
351
"""simple docstring""" import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging A = logging.get_logger(__name__) A = {'''vocab_file''': '''spiece.model'''} A = { '''vocab_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/spiece.model''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/spiece.model''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/spiece.model''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/spiece.model''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model''', } } A = { '''albert-base-v1''': 512, '''albert-large-v1''': 512, '''albert-xlarge-v1''': 512, '''albert-xxlarge-v1''': 512, '''albert-base-v2''': 512, '''albert-large-v2''': 512, '''albert-xlarge-v2''': 512, '''albert-xxlarge-v2''': 512, } A = '''▁''' class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = VOCAB_FILES_NAMES __lowerCAmelCase = PRETRAINED_VOCAB_FILES_MAP __lowerCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self , _UpperCAmelCase , _UpperCAmelCase=True , _UpperCAmelCase=True , _UpperCAmelCase=False , _UpperCAmelCase="[CLS]" , _UpperCAmelCase="[SEP]" , _UpperCAmelCase="<unk>" , _UpperCAmelCase="[SEP]" , _UpperCAmelCase="<pad>" , _UpperCAmelCase="[CLS]" , _UpperCAmelCase="[MASK]" , _UpperCAmelCase = None , **_UpperCAmelCase , ): # Mask token behave like a normal word, i.e. include the space before it and # is included in the raw text, there should be a match in a non-normalized sentence. __a : int = ( AddedToken(_UpperCAmelCase , lstrip=_UpperCAmelCase , rstrip=_UpperCAmelCase , normalized=_UpperCAmelCase ) if isinstance(_UpperCAmelCase , _UpperCAmelCase ) else mask_token ) __a : Any = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=_UpperCAmelCase , remove_space=_UpperCAmelCase , keep_accents=_UpperCAmelCase , bos_token=_UpperCAmelCase , eos_token=_UpperCAmelCase , unk_token=_UpperCAmelCase , sep_token=_UpperCAmelCase , pad_token=_UpperCAmelCase , cls_token=_UpperCAmelCase , mask_token=_UpperCAmelCase , sp_model_kwargs=self.sp_model_kwargs , **_UpperCAmelCase , ) __a : Tuple = do_lower_case __a : Optional[Any] = remove_space __a : Optional[Any] = keep_accents __a : Union[str, Any] = vocab_file __a : List[str] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(_UpperCAmelCase ) @property def _lowerCamelCase ( self ): return len(self.sp_model ) def _lowerCamelCase ( self ): __a : Any = {self.convert_ids_to_tokens(_UpperCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ): __a : str = self.__dict__.copy() __a : Tuple = None return state def __setstate__( self , _UpperCAmelCase ): __a : Any = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): __a : Optional[Any] = {} __a : Dict = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def _lowerCamelCase ( self , _UpperCAmelCase ): if self.remove_space: __a : Any = ''' '''.join(inputs.strip().split() ) else: __a : Tuple = inputs __a : Union[str, Any] = outputs.replace('''``''' , '''"''' ).replace('''\'\'''' , '''"''' ) if not self.keep_accents: __a : List[str] = unicodedata.normalize('''NFKD''' , _UpperCAmelCase ) __a : Optional[int] = ''''''.join([c for c in outputs if not unicodedata.combining(_UpperCAmelCase )] ) if self.do_lower_case: __a : Optional[Any] = outputs.lower() return outputs def _lowerCamelCase ( self , _UpperCAmelCase ): __a : int = self.preprocess_text(_UpperCAmelCase ) __a : Tuple = self.sp_model.encode(_UpperCAmelCase , out_type=_UpperCAmelCase ) __a : int = [] for piece in pieces: if len(_UpperCAmelCase ) > 1 and piece[-1] == str(''',''' ) and piece[-2].isdigit(): __a : List[str] = self.sp_model.EncodeAsPieces(piece[:-1].replace(_UpperCAmelCase , '''''' ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: __a : Tuple = cur_pieces[1:] else: __a : Optional[Any] = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(_UpperCAmelCase ) else: new_pieces.append(_UpperCAmelCase ) return new_pieces def _lowerCamelCase ( self , _UpperCAmelCase ): return self.sp_model.PieceToId(_UpperCAmelCase ) def _lowerCamelCase ( self , _UpperCAmelCase ): return self.sp_model.IdToPiece(_UpperCAmelCase ) def _lowerCamelCase ( self , _UpperCAmelCase ): __a : List[str] = [] __a : str = '''''' __a : Any = 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(_UpperCAmelCase ) + token __a : Tuple = True __a : Tuple = [] else: current_sub_tokens.append(_UpperCAmelCase ) __a : Optional[int] = False out_string += self.sp_model.decode(_UpperCAmelCase ) return out_string.strip() def _lowerCamelCase ( self , _UpperCAmelCase , _UpperCAmelCase = None ): __a : int = [self.sep_token_id] __a : Union[str, Any] = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def _lowerCamelCase ( self , _UpperCAmelCase , _UpperCAmelCase = None , _UpperCAmelCase = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_UpperCAmelCase , token_ids_a=_UpperCAmelCase , already_has_special_tokens=_UpperCAmelCase ) if token_ids_a is not None: return [1] + ([0] * len(_UpperCAmelCase )) + [1] + ([0] * len(_UpperCAmelCase )) + [1] return [1] + ([0] * len(_UpperCAmelCase )) + [1] def _lowerCamelCase ( self , _UpperCAmelCase , _UpperCAmelCase = None ): __a : Union[str, Any] = [self.sep_token_id] __a : Tuple = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def _lowerCamelCase ( self , _UpperCAmelCase , _UpperCAmelCase = None ): if not os.path.isdir(_UpperCAmelCase ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return __a : List[str] = os.path.join( _UpperCAmelCase , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_UpperCAmelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , _UpperCAmelCase ) elif not os.path.isfile(self.vocab_file ): with open(_UpperCAmelCase , '''wb''' ) as fi: __a : Any = self.sp_model.serialized_model_proto() fi.write(_UpperCAmelCase ) return (out_vocab_file,)
188
0
"""simple docstring""" from typing import Dict, List, Optional, Union import numpy as np from transformers.utils import is_vision_available from transformers.utils.generic import TensorType from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, is_valid_image, to_numpy_array, valid_images, ) from ...utils import logging if is_vision_available(): import PIL lowerCAmelCase__ : Optional[Any] = logging.get_logger(__name__) def a_ ( lowerCamelCase ): if isinstance(lowerCamelCase , (list, tuple) ) and isinstance(videos[0] , (list, tuple) ) and is_valid_image(videos[0][0] ): return videos elif isinstance(lowerCamelCase , (list, tuple) ) and is_valid_image(videos[0] ): return [videos] elif is_valid_image(lowerCamelCase ): return [[videos]] raise ValueError(f'''Could not make batched video from {videos}''' ) class snake_case ( __UpperCAmelCase ): """simple docstring""" snake_case__ = ["pixel_values"] def __init__( self : Dict ,lowerCamelCase__ : bool = True ,lowerCamelCase__ : Dict[str, int] = None ,lowerCamelCase__ : PILImageResampling = PILImageResampling.BILINEAR ,lowerCamelCase__ : bool = True ,lowerCamelCase__ : Dict[str, int] = None ,lowerCamelCase__ : bool = True ,lowerCamelCase__ : Union[int, float] = 1 / 255 ,lowerCamelCase__ : bool = True ,lowerCamelCase__ : bool = True ,lowerCamelCase__ : Optional[Union[float, List[float]]] = None ,lowerCamelCase__ : Optional[Union[float, List[float]]] = None ,**lowerCamelCase__ : List[Any] ,): super().__init__(**lowerCamelCase__ ) UpperCAmelCase__ = size if size is not None else {'shortest_edge': 256} UpperCAmelCase__ = get_size_dict(lowerCamelCase__ ,default_to_square=lowerCamelCase__ ) UpperCAmelCase__ = crop_size if crop_size is not None else {'height': 224, 'width': 224} UpperCAmelCase__ = get_size_dict(lowerCamelCase__ ,param_name='crop_size' ) UpperCAmelCase__ = do_resize UpperCAmelCase__ = size UpperCAmelCase__ = do_center_crop UpperCAmelCase__ = crop_size UpperCAmelCase__ = resample UpperCAmelCase__ = do_rescale UpperCAmelCase__ = rescale_factor UpperCAmelCase__ = offset UpperCAmelCase__ = do_normalize UpperCAmelCase__ = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN UpperCAmelCase__ = image_std if image_std is not None else IMAGENET_STANDARD_STD def __lowerCAmelCase ( self : int ,lowerCamelCase__ : np.ndarray ,lowerCamelCase__ : Dict[str, int] ,lowerCamelCase__ : PILImageResampling = PILImageResampling.BILINEAR ,lowerCamelCase__ : Optional[Union[str, ChannelDimension]] = None ,**lowerCamelCase__ : List[str] ,): UpperCAmelCase__ = get_size_dict(lowerCamelCase__ ,default_to_square=lowerCamelCase__ ) if "shortest_edge" in size: UpperCAmelCase__ = get_resize_output_image_size(lowerCamelCase__ ,size['shortest_edge'] ,default_to_square=lowerCamelCase__ ) elif "height" in size and "width" in size: UpperCAmelCase__ = (size['height'], size['width']) else: raise ValueError(f'''Size must have \'height\' and \'width\' or \'shortest_edge\' as keys. Got {size.keys()}''' ) return resize(lowerCamelCase__ ,size=lowerCamelCase__ ,resample=lowerCamelCase__ ,data_format=lowerCamelCase__ ,**lowerCamelCase__ ) def __lowerCAmelCase ( self : Tuple ,lowerCamelCase__ : np.ndarray ,lowerCamelCase__ : Dict[str, int] ,lowerCamelCase__ : Optional[Union[str, ChannelDimension]] = None ,**lowerCamelCase__ : List[Any] ,): UpperCAmelCase__ = get_size_dict(lowerCamelCase__ ) if "height" not in size or "width" not in size: raise ValueError(f'''Size must have \'height\' and \'width\' as keys. Got {size.keys()}''' ) return center_crop(lowerCamelCase__ ,size=(size['height'], size['width']) ,data_format=lowerCamelCase__ ,**lowerCamelCase__ ) def __lowerCAmelCase ( self : Union[str, Any] ,lowerCamelCase__ : np.ndarray ,lowerCamelCase__ : Union[int, float] ,lowerCamelCase__ : bool = True ,lowerCamelCase__ : Optional[Union[str, ChannelDimension]] = None ,**lowerCamelCase__ : Union[str, Any] ,): UpperCAmelCase__ = image.astype(np.floataa ) if offset: UpperCAmelCase__ = image - (scale / 2) return rescale(lowerCamelCase__ ,scale=lowerCamelCase__ ,data_format=lowerCamelCase__ ,**lowerCamelCase__ ) def __lowerCAmelCase ( self : Optional[int] ,lowerCamelCase__ : np.ndarray ,lowerCamelCase__ : Union[float, List[float]] ,lowerCamelCase__ : Union[float, List[float]] ,lowerCamelCase__ : Optional[Union[str, ChannelDimension]] = None ,**lowerCamelCase__ : Dict ,): return normalize(lowerCamelCase__ ,mean=lowerCamelCase__ ,std=lowerCamelCase__ ,data_format=lowerCamelCase__ ,**lowerCamelCase__ ) def __lowerCAmelCase ( self : Union[str, Any] ,lowerCamelCase__ : ImageInput ,lowerCamelCase__ : bool = None ,lowerCamelCase__ : Dict[str, int] = None ,lowerCamelCase__ : PILImageResampling = None ,lowerCamelCase__ : bool = None ,lowerCamelCase__ : Dict[str, int] = None ,lowerCamelCase__ : bool = None ,lowerCamelCase__ : float = None ,lowerCamelCase__ : bool = None ,lowerCamelCase__ : bool = None ,lowerCamelCase__ : Optional[Union[float, List[float]]] = None ,lowerCamelCase__ : Optional[Union[float, List[float]]] = None ,lowerCamelCase__ : Optional[ChannelDimension] = ChannelDimension.FIRST ,): if do_resize and size is None or resample is None: raise ValueError('Size and resample must be specified if do_resize is True.' ) if do_center_crop and crop_size is None: raise ValueError('Crop size must be specified if do_center_crop is True.' ) if do_rescale and rescale_factor is None: raise ValueError('Rescale factor must be specified if do_rescale is True.' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('Image mean and std must be specified if do_normalize is True.' ) if offset and not do_rescale: raise ValueError('For offset, do_rescale must also be set to True.' ) # All transformations expect numpy arrays. UpperCAmelCase__ = to_numpy_array(lowerCamelCase__ ) if do_resize: UpperCAmelCase__ = self.resize(image=lowerCamelCase__ ,size=lowerCamelCase__ ,resample=lowerCamelCase__ ) if do_center_crop: UpperCAmelCase__ = self.center_crop(lowerCamelCase__ ,size=lowerCamelCase__ ) if do_rescale: UpperCAmelCase__ = self.rescale(image=lowerCamelCase__ ,scale=lowerCamelCase__ ,offset=lowerCamelCase__ ) if do_normalize: UpperCAmelCase__ = self.normalize(image=lowerCamelCase__ ,mean=lowerCamelCase__ ,std=lowerCamelCase__ ) UpperCAmelCase__ = to_channel_dimension_format(lowerCamelCase__ ,lowerCamelCase__ ) return image def __lowerCAmelCase ( self : Tuple ,lowerCamelCase__ : ImageInput ,lowerCamelCase__ : bool = None ,lowerCamelCase__ : Dict[str, int] = None ,lowerCamelCase__ : PILImageResampling = None ,lowerCamelCase__ : bool = None ,lowerCamelCase__ : Dict[str, int] = None ,lowerCamelCase__ : bool = None ,lowerCamelCase__ : float = None ,lowerCamelCase__ : bool = None ,lowerCamelCase__ : bool = None ,lowerCamelCase__ : Optional[Union[float, List[float]]] = None ,lowerCamelCase__ : Optional[Union[float, List[float]]] = None ,lowerCamelCase__ : Optional[Union[str, TensorType]] = None ,lowerCamelCase__ : ChannelDimension = ChannelDimension.FIRST ,**lowerCamelCase__ : Dict ,): UpperCAmelCase__ = do_resize if do_resize is not None else self.do_resize UpperCAmelCase__ = resample if resample is not None else self.resample UpperCAmelCase__ = do_center_crop if do_center_crop is not None else self.do_center_crop UpperCAmelCase__ = do_rescale if do_rescale is not None else self.do_rescale UpperCAmelCase__ = rescale_factor if rescale_factor is not None else self.rescale_factor UpperCAmelCase__ = offset if offset is not None else self.offset UpperCAmelCase__ = do_normalize if do_normalize is not None else self.do_normalize UpperCAmelCase__ = image_mean if image_mean is not None else self.image_mean UpperCAmelCase__ = image_std if image_std is not None else self.image_std UpperCAmelCase__ = size if size is not None else self.size UpperCAmelCase__ = get_size_dict(lowerCamelCase__ ,default_to_square=lowerCamelCase__ ) UpperCAmelCase__ = crop_size if crop_size is not None else self.crop_size UpperCAmelCase__ = get_size_dict(lowerCamelCase__ ,param_name='crop_size' ) if not valid_images(lowerCamelCase__ ): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.' ) UpperCAmelCase__ = make_batched(lowerCamelCase__ ) UpperCAmelCase__ = [ [ self._preprocess_image( image=lowerCamelCase__ ,do_resize=lowerCamelCase__ ,size=lowerCamelCase__ ,resample=lowerCamelCase__ ,do_center_crop=lowerCamelCase__ ,crop_size=lowerCamelCase__ ,do_rescale=lowerCamelCase__ ,rescale_factor=lowerCamelCase__ ,offset=lowerCamelCase__ ,do_normalize=lowerCamelCase__ ,image_mean=lowerCamelCase__ ,image_std=lowerCamelCase__ ,data_format=lowerCamelCase__ ,) for img in video ] for video in videos ] UpperCAmelCase__ = {'pixel_values': videos} return BatchFeature(data=lowerCamelCase__ ,tensor_type=lowerCamelCase__ )
98
"""simple docstring""" from __future__ import annotations from collections import namedtuple from dataclasses import dataclass @dataclass class snake_case : """simple docstring""" snake_case__ = 42 snake_case__ = None snake_case__ = None lowerCAmelCase__ : Union[str, Any] = namedtuple('CoinsDistribResult', 'moves excess') def a_ ( lowerCamelCase ): if root is None: return 0 # Validation def count_nodes(lowerCamelCase ) -> int: if node is None: return 0 return count_nodes(node.left ) + count_nodes(node.right ) + 1 def count_coins(lowerCamelCase ) -> int: if node is None: return 0 return count_coins(node.left ) + count_coins(node.right ) + node.data if count_nodes(lowerCamelCase ) != count_coins(lowerCamelCase ): raise ValueError('The nodes number should be same as the number of coins' ) # Main calculation def get_distrib(lowerCamelCase ) -> CoinsDistribResult: if node is None: return CoinsDistribResult(0 , 1 ) UpperCAmelCase__ , UpperCAmelCase__ = get_distrib(node.left ) UpperCAmelCase__ , UpperCAmelCase__ = get_distrib(node.right ) UpperCAmelCase__ = 1 - left_distrib_excess UpperCAmelCase__ = 1 - right_distrib_excess UpperCAmelCase__ = ( left_distrib_moves + right_distrib_moves + abs(lowerCamelCase ) + abs(lowerCamelCase ) ) UpperCAmelCase__ = node.data - coins_to_left - coins_to_right return CoinsDistribResult(lowerCamelCase , lowerCamelCase ) return get_distrib(lowerCamelCase )[0] if __name__ == "__main__": import doctest doctest.testmod()
98
1
"""simple docstring""" def _lowerCAmelCase ( UpperCAmelCase__ : float ) ->float: return 1_0 - x * x def _lowerCAmelCase ( UpperCAmelCase__ : float, UpperCAmelCase__ : float ) ->float: # Bolzano theory in order to find if there is a root between a and b if equation(UpperCAmelCase__ ) * equation(UpperCAmelCase__ ) >= 0: raise ValueError("""Wrong space!""" ) A__ : Dict = a while (b - a) >= 0.01: # Find middle point A__ : Union[str, Any] = (a + b) / 2 # Check if middle point is root if equation(UpperCAmelCase__ ) == 0.0: break # Decide the side to repeat the steps if equation(UpperCAmelCase__ ) * equation(UpperCAmelCase__ ) < 0: A__ : int = c else: A__ : List[Any] = c return c if __name__ == "__main__": import doctest doctest.testmod() print(bisection(-2, 5)) print(bisection(0, 6))
296
"""simple docstring""" import os import unittest from tempfile import TemporaryDirectory import torch import torch.nn as nn from accelerate.utils import ( OffloadedWeightsLoader, extract_submodules_state_dict, load_offloaded_weight, offload_state_dict, offload_weight, ) class __SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : List[Any] ): '''simple docstring''' super().__init__() A__ : int = nn.Linear(3 , 4 ) A__ : Union[str, Any] = nn.BatchNormad(4 ) A__ : Union[str, Any] = nn.Linear(4 , 5 ) def _UpperCamelCase ( self : str , snake_case : List[str] ): '''simple docstring''' return self.lineara(self.batchnorm(self.lineara(snake_case ) ) ) class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): def _UpperCamelCase ( self : str ): '''simple docstring''' A__ : int = ModelForTest() with TemporaryDirectory() as tmp_dir: offload_state_dict(snake_case , model.state_dict() ) A__ : List[str] = os.path.join(snake_case , """index.json""" ) self.assertTrue(os.path.isfile(snake_case ) ) # TODO: add tests on what is inside the index for key in ["linear1.weight", "linear1.bias", "linear2.weight", "linear2.bias"]: A__ : List[str] = os.path.join(snake_case , F'{key}.dat' ) self.assertTrue(os.path.isfile(snake_case ) ) # TODO: add tests on the fact weights are properly loaded def _UpperCamelCase ( self : int ): '''simple docstring''' A__ : Union[str, Any] = [torch.floataa, torch.floataa, torch.bfloataa] for dtype in dtypes: A__ : str = torch.randn(2 , 3 , dtype=snake_case ) with TemporaryDirectory() as tmp_dir: A__ : List[str] = offload_weight(snake_case , """weight""" , snake_case , {} ) A__ : Union[str, Any] = os.path.join(snake_case , """weight.dat""" ) self.assertTrue(os.path.isfile(snake_case ) ) self.assertDictEqual(snake_case , {"""weight""": {"""shape""": [2, 3], """dtype""": str(snake_case ).split(""".""" )[1]}} ) A__ : str = load_offloaded_weight(snake_case , index["""weight"""] ) self.assertTrue(torch.equal(snake_case , snake_case ) ) def _UpperCamelCase ( self : List[str] ): '''simple docstring''' A__ : str = ModelForTest() A__ : Union[str, Any] = model.state_dict() A__ : Optional[int] = {k: v for k, v in state_dict.items() if """linear2""" not in k} A__ : List[Any] = {k: v for k, v in state_dict.items() if """linear2""" in k} with TemporaryDirectory() as tmp_dir: offload_state_dict(snake_case , snake_case ) A__ : Dict = OffloadedWeightsLoader(state_dict=snake_case , save_folder=snake_case ) # Every key is there with the right value self.assertEqual(sorted(snake_case ) , sorted(state_dict.keys() ) ) for key, param in state_dict.items(): self.assertTrue(torch.allclose(snake_case , weight_map[key] ) ) A__ : int = {k: v for k, v in state_dict.items() if """weight""" in k} A__ : Tuple = {k: v for k, v in state_dict.items() if """weight""" not in k} with TemporaryDirectory() as tmp_dir: offload_state_dict(snake_case , snake_case ) A__ : Optional[Any] = OffloadedWeightsLoader(state_dict=snake_case , save_folder=snake_case ) # Every key is there with the right value self.assertEqual(sorted(snake_case ) , sorted(state_dict.keys() ) ) for key, param in state_dict.items(): self.assertTrue(torch.allclose(snake_case , weight_map[key] ) ) with TemporaryDirectory() as tmp_dir: offload_state_dict(snake_case , snake_case ) # Duplicates are removed A__ : int = OffloadedWeightsLoader(state_dict=snake_case , save_folder=snake_case ) # Every key is there with the right value self.assertEqual(sorted(snake_case ) , sorted(state_dict.keys() ) ) for key, param in state_dict.items(): self.assertTrue(torch.allclose(snake_case , weight_map[key] ) ) def _UpperCamelCase ( self : Tuple ): '''simple docstring''' A__ : List[str] = {"""a.1""": 0, """a.10""": 1, """a.2""": 2} A__ : str = extract_submodules_state_dict(snake_case , ["""a.1""", """a.2"""] ) self.assertDictEqual(snake_case , {"""a.1""": 0, """a.2""": 2} ) A__ : Dict = {"""a.1.a""": 0, """a.10.a""": 1, """a.2.a""": 2} A__ : int = extract_submodules_state_dict(snake_case , ["""a.1""", """a.2"""] ) self.assertDictEqual(snake_case , {"""a.1.a""": 0, """a.2.a""": 2} )
296
1
'''simple docstring''' import argparse import json import logging import os import sys from unittest.mock import patch from transformers.testing_utils import TestCasePlus, get_gpu_count, slow __SCREAMING_SNAKE_CASE :Union[str, Any] = [ os.path.join(os.path.dirname(__file__), dirname) for dirname in [ '''text-classification''', '''language-modeling''', '''summarization''', '''token-classification''', '''question-answering''', ] ] sys.path.extend(SRC_DIRS) if SRC_DIRS is not None: import run_clm_flax import run_flax_glue import run_flax_ner import run_mlm_flax import run_qa import run_summarization_flax import run_ta_mlm_flax logging.basicConfig(level=logging.DEBUG) __SCREAMING_SNAKE_CASE :Union[str, Any] = logging.getLogger() def UpperCAmelCase_ ( ) -> List[str]: '''simple docstring''' _UpperCAmelCase = argparse.ArgumentParser() parser.add_argument("-f" ) _UpperCAmelCase = parser.parse_args() return args.f def UpperCAmelCase_ ( __lowercase : Tuple , __lowercase : int="eval" ) -> Tuple: '''simple docstring''' _UpperCAmelCase = os.path.join(__lowercase , f'{split}_results.json' ) if os.path.exists(__lowercase ): with open(__lowercase , "r" ) as f: return json.load(__lowercase ) raise ValueError(f'can\'t find {path}' ) __SCREAMING_SNAKE_CASE :List[str] = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class A_ ( lowerCAmelCase_ ): def lowercase ( self : List[str] ): _UpperCAmelCase = self.get_auto_remove_tmp_dir() _UpperCAmelCase = f'\n run_glue.py\n --model_name_or_path distilbert-base-uncased\n --output_dir {tmp_dir}\n --train_file ./tests/fixtures/tests_samples/MRPC/train.csv\n --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --learning_rate=1e-4\n --eval_steps=2\n --warmup_steps=2\n --seed=42\n --max_seq_length=128\n '.split() with patch.object(snake_case_ , "argv" , snake_case_ ): run_flax_glue.main() _UpperCAmelCase = get_results(snake_case_ ) self.assertGreaterEqual(result["eval_accuracy"] , 0.7_5 ) @slow def lowercase ( self : int ): _UpperCAmelCase = self.get_auto_remove_tmp_dir() _UpperCAmelCase = f'\n run_clm_flax.py\n --model_name_or_path distilgpt2\n --train_file ./tests/fixtures/sample_text.txt\n --validation_file ./tests/fixtures/sample_text.txt\n --do_train\n --do_eval\n --block_size 128\n --per_device_train_batch_size 4\n --per_device_eval_batch_size 4\n --num_train_epochs 2\n --logging_steps 2 --eval_steps 2\n --output_dir {tmp_dir}\n --overwrite_output_dir\n '.split() with patch.object(snake_case_ , "argv" , snake_case_ ): run_clm_flax.main() _UpperCAmelCase = get_results(snake_case_ ) self.assertLess(result["eval_perplexity"] , 1_0_0 ) @slow def lowercase ( self : str ): _UpperCAmelCase = self.get_auto_remove_tmp_dir() _UpperCAmelCase = f'\n run_summarization.py\n --model_name_or_path t5-small\n --train_file tests/fixtures/tests_samples/xsum/sample.json\n --validation_file tests/fixtures/tests_samples/xsum/sample.json\n --test_file tests/fixtures/tests_samples/xsum/sample.json\n --output_dir {tmp_dir}\n --overwrite_output_dir\n --num_train_epochs=3\n --warmup_steps=8\n --do_train\n --do_eval\n --do_predict\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --predict_with_generate\n '.split() with patch.object(snake_case_ , "argv" , snake_case_ ): run_summarization_flax.main() _UpperCAmelCase = get_results(snake_case_ , split="test" ) self.assertGreaterEqual(result["test_rouge1"] , 1_0 ) self.assertGreaterEqual(result["test_rouge2"] , 2 ) self.assertGreaterEqual(result["test_rougeL"] , 7 ) self.assertGreaterEqual(result["test_rougeLsum"] , 7 ) @slow def lowercase ( self : List[Any] ): _UpperCAmelCase = self.get_auto_remove_tmp_dir() _UpperCAmelCase = f'\n run_mlm.py\n --model_name_or_path distilroberta-base\n --train_file ./tests/fixtures/sample_text.txt\n --validation_file ./tests/fixtures/sample_text.txt\n --output_dir {tmp_dir}\n --overwrite_output_dir\n --max_seq_length 128\n --per_device_train_batch_size 4\n --per_device_eval_batch_size 4\n --logging_steps 2 --eval_steps 2\n --do_train\n --do_eval\n --num_train_epochs=1\n '.split() with patch.object(snake_case_ , "argv" , snake_case_ ): run_mlm_flax.main() _UpperCAmelCase = get_results(snake_case_ ) self.assertLess(result["eval_perplexity"] , 4_2 ) @slow def lowercase ( self : Any ): _UpperCAmelCase = self.get_auto_remove_tmp_dir() _UpperCAmelCase = f'\n run_t5_mlm_flax.py\n --model_name_or_path t5-small\n --train_file ./tests/fixtures/sample_text.txt\n --validation_file ./tests/fixtures/sample_text.txt\n --do_train\n --do_eval\n --max_seq_length 128\n --per_device_train_batch_size 4\n --per_device_eval_batch_size 4\n --num_train_epochs 2\n --logging_steps 2 --eval_steps 2\n --output_dir {tmp_dir}\n --overwrite_output_dir\n '.split() with patch.object(snake_case_ , "argv" , snake_case_ ): run_ta_mlm_flax.main() _UpperCAmelCase = get_results(snake_case_ ) self.assertGreaterEqual(result["eval_accuracy"] , 0.4_2 ) @slow def lowercase ( self : Tuple ): # with so little data distributed training needs more epochs to get the score on par with 0/1 gpu _UpperCAmelCase = 7 if get_gpu_count() > 1 else 2 _UpperCAmelCase = self.get_auto_remove_tmp_dir() _UpperCAmelCase = f'\n run_flax_ner.py\n --model_name_or_path bert-base-uncased\n --train_file tests/fixtures/tests_samples/conll/sample.json\n --validation_file tests/fixtures/tests_samples/conll/sample.json\n --output_dir {tmp_dir}\n --overwrite_output_dir\n --do_train\n --do_eval\n --warmup_steps=2\n --learning_rate=2e-4\n --logging_steps 2 --eval_steps 2\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=2\n --num_train_epochs={epochs}\n --seed 7\n '.split() with patch.object(snake_case_ , "argv" , snake_case_ ): run_flax_ner.main() _UpperCAmelCase = get_results(snake_case_ ) self.assertGreaterEqual(result["eval_accuracy"] , 0.7_5 ) self.assertGreaterEqual(result["eval_f1"] , 0.3 ) @slow def lowercase ( self : Any ): _UpperCAmelCase = self.get_auto_remove_tmp_dir() _UpperCAmelCase = f'\n run_qa.py\n --model_name_or_path bert-base-uncased\n --version_2_with_negative\n --train_file tests/fixtures/tests_samples/SQUAD/sample.json\n --validation_file tests/fixtures/tests_samples/SQUAD/sample.json\n --output_dir {tmp_dir}\n --overwrite_output_dir\n --num_train_epochs=3\n --warmup_steps=2\n --do_train\n --do_eval\n --logging_steps 2 --eval_steps 2\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n '.split() with patch.object(snake_case_ , "argv" , snake_case_ ): run_qa.main() _UpperCAmelCase = get_results(snake_case_ ) self.assertGreaterEqual(result["eval_f1"] , 3_0 ) self.assertGreaterEqual(result["eval_exact"] , 3_0 )
22
import argparse import json import os import time import zipfile from get_ci_error_statistics import download_artifact, get_artifacts_links from transformers import logging _UpperCAmelCase : Optional[int] = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE ( _UpperCAmelCase , _UpperCAmelCase ) -> Dict: lowerCamelCase__ : str = set() lowerCamelCase__ : Any = [] def parse_line(_UpperCAmelCase ): for line in fp: if isinstance(_UpperCAmelCase , _UpperCAmelCase ): lowerCamelCase__ : Any = line.decode('UTF-8' ) if "warnings summary (final)" in line: continue # This means we are outside the body of a warning elif not line.startswith(' ' ): # process a single warning and move it to `selected_warnings`. if len(_UpperCAmelCase ) > 0: lowerCamelCase__ : str = '\n'.join(_UpperCAmelCase ) # Only keep the warnings specified in `targets` if any(F""": {x}: """ in warning for x in targets ): selected_warnings.add(_UpperCAmelCase ) buffer.clear() continue else: lowerCamelCase__ : List[str] = line.strip() buffer.append(_UpperCAmelCase ) if from_gh: for filename in os.listdir(_UpperCAmelCase ): lowerCamelCase__ : Dict = os.path.join(_UpperCAmelCase , _UpperCAmelCase ) if not os.path.isdir(_UpperCAmelCase ): # read the file if filename != "warnings.txt": continue with open(_UpperCAmelCase ) as fp: parse_line(_UpperCAmelCase ) else: try: with zipfile.ZipFile(_UpperCAmelCase ) as z: for filename in z.namelist(): if not os.path.isdir(_UpperCAmelCase ): # read the file if filename != "warnings.txt": continue with z.open(_UpperCAmelCase ) as fp: parse_line(_UpperCAmelCase ) except Exception: logger.warning( F"""{artifact_path} is either an invalid zip file or something else wrong. This file is skipped.""" ) return selected_warnings def SCREAMING_SNAKE_CASE ( _UpperCAmelCase , _UpperCAmelCase ) -> Union[str, Any]: lowerCamelCase__ : Tuple = set() lowerCamelCase__ : Optional[int] = [os.path.join(_UpperCAmelCase , _UpperCAmelCase ) for p in os.listdir(_UpperCAmelCase ) if (p.endswith('.zip' ) or from_gh)] for p in paths: selected_warnings.update(extract_warnings_from_single_artifact(_UpperCAmelCase , _UpperCAmelCase ) ) return selected_warnings if __name__ == "__main__": def SCREAMING_SNAKE_CASE ( _UpperCAmelCase ) -> Tuple: return values.split(',' ) _UpperCAmelCase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument("""--workflow_run_id""", type=str, required=True, help="""A GitHub Actions workflow run id.""") parser.add_argument( """--output_dir""", type=str, required=True, help="""Where to store the downloaded artifacts and other result files.""", ) parser.add_argument("""--token""", default=None, type=str, help="""A token that has actions:read permission.""") # optional parameters parser.add_argument( """--targets""", default="""DeprecationWarning,UserWarning,FutureWarning""", type=list_str, help="""Comma-separated list of target warning(s) which we want to extract.""", ) parser.add_argument( """--from_gh""", action="""store_true""", help="""If running from a GitHub action workflow and collecting warnings from its artifacts.""", ) _UpperCAmelCase : Union[str, Any] = parser.parse_args() _UpperCAmelCase : Dict = args.from_gh if from_gh: # The artifacts have to be downloaded using `actions/download-artifact@v3` pass else: os.makedirs(args.output_dir, exist_ok=True) # get download links _UpperCAmelCase : Union[str, Any] = get_artifacts_links(args.workflow_run_id, token=args.token) with open(os.path.join(args.output_dir, """artifacts.json"""), """w""", encoding="""UTF-8""") as fp: json.dump(artifacts, fp, ensure_ascii=False, indent=4) # download artifacts for idx, (name, url) in enumerate(artifacts.items()): print(name) print(url) print("""=""" * 80) download_artifact(name, url, args.output_dir, args.token) # Be gentle to GitHub time.sleep(1) # extract warnings from artifacts _UpperCAmelCase : Dict = extract_warnings(args.output_dir, args.targets) _UpperCAmelCase : Optional[Any] = sorted(selected_warnings) with open(os.path.join(args.output_dir, """selected_warnings.json"""), """w""", encoding="""UTF-8""") as fp: json.dump(selected_warnings, fp, ensure_ascii=False, indent=4)
50
0
def lowerCamelCase__ (_UpperCAmelCase): if not isinstance(_UpperCAmelCase , _UpperCAmelCase): SCREAMING_SNAKE_CASE = F'''Input value of [number={number}] must be an integer''' raise TypeError(_UpperCAmelCase) if number < 0: return False SCREAMING_SNAKE_CASE = number * number while number > 0: if number % 10 != number_square % 10: return False number //= 10 number_square //= 10 return True if __name__ == "__main__": import doctest doctest.testmod()
327
import unittest from transformers import ( MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TextaTextGenerationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, require_tf, require_torch from transformers.utils import is_torch_available from .test_pipelines_common import ANY if is_torch_available(): import torch @is_pipeline_test class _snake_case ( unittest.TestCase ): _lowercase : List[Any] = MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING _lowercase : int = TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING def SCREAMING_SNAKE_CASE__ ( self , a , a , a) -> Any: SCREAMING_SNAKE_CASE = TextaTextGenerationPipeline(model=a , tokenizer=a) return generator, ["Something to write", "Something else"] def SCREAMING_SNAKE_CASE__ ( self , a , a) -> Any: SCREAMING_SNAKE_CASE = generator('Something there') self.assertEqual(a , [{'generated_text': ANY(a)}]) # These are encoder decoder, they don't just append to incoming string self.assertFalse(outputs[0]['generated_text'].startswith('Something there')) SCREAMING_SNAKE_CASE = generator(['This is great !', 'Something else'] , num_return_sequences=2 , do_sample=a) self.assertEqual( a , [ [{'generated_text': ANY(a)}, {'generated_text': ANY(a)}], [{'generated_text': ANY(a)}, {'generated_text': ANY(a)}], ] , ) SCREAMING_SNAKE_CASE = generator( ['This is great !', 'Something else'] , num_return_sequences=2 , batch_size=2 , do_sample=a) self.assertEqual( a , [ [{'generated_text': ANY(a)}, {'generated_text': ANY(a)}], [{'generated_text': ANY(a)}, {'generated_text': ANY(a)}], ] , ) with self.assertRaises(a): generator(4) @require_torch def SCREAMING_SNAKE_CASE__ ( self) -> Any: SCREAMING_SNAKE_CASE = pipeline('text2text-generation' , model='patrickvonplaten/t5-tiny-random' , framework='pt') # do_sample=False necessary for reproducibility SCREAMING_SNAKE_CASE = generator('Something there' , do_sample=a) self.assertEqual(a , [{'generated_text': ''}]) SCREAMING_SNAKE_CASE = 3 SCREAMING_SNAKE_CASE = generator( 'Something there' , num_return_sequences=a , num_beams=a , ) SCREAMING_SNAKE_CASE = [ {'generated_text': 'Beide Beide Beide Beide Beide Beide Beide Beide Beide'}, {'generated_text': 'Beide Beide Beide Beide Beide Beide Beide Beide'}, {'generated_text': ''}, ] self.assertEqual(a , a) SCREAMING_SNAKE_CASE = generator('This is a test' , do_sample=a , num_return_sequences=2 , return_tensors=a) self.assertEqual( a , [ {'generated_token_ids': ANY(torch.Tensor)}, {'generated_token_ids': ANY(torch.Tensor)}, ] , ) SCREAMING_SNAKE_CASE = generator.model.config.eos_token_id SCREAMING_SNAKE_CASE = '<pad>' SCREAMING_SNAKE_CASE = generator( ['This is a test', 'This is a second test'] , do_sample=a , num_return_sequences=2 , batch_size=2 , return_tensors=a , ) self.assertEqual( a , [ [ {'generated_token_ids': ANY(torch.Tensor)}, {'generated_token_ids': ANY(torch.Tensor)}, ], [ {'generated_token_ids': ANY(torch.Tensor)}, {'generated_token_ids': ANY(torch.Tensor)}, ], ] , ) @require_tf def SCREAMING_SNAKE_CASE__ ( self) -> Optional[Any]: SCREAMING_SNAKE_CASE = pipeline('text2text-generation' , model='patrickvonplaten/t5-tiny-random' , framework='tf') # do_sample=False necessary for reproducibility SCREAMING_SNAKE_CASE = generator('Something there' , do_sample=a) self.assertEqual(a , [{'generated_text': ''}])
327
1
"""simple docstring""" import inspect import unittest from transformers import BitConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import BitBackbone, BitForImageClassification, BitImageProcessor, BitModel from transformers.models.bit.modeling_bit import BIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class A_ : """simple docstring""" def __init__( self :Tuple , lowercase_ :str , lowercase_ :Optional[Any]=3 , lowercase_ :List[str]=32 , lowercase_ :str=3 , lowercase_ :Optional[int]=10 , lowercase_ :Union[str, Any]=[8, 16, 32, 64] , lowercase_ :Any=[1, 1, 2, 1] , lowercase_ :Dict=True , lowercase_ :Optional[int]=True , lowercase_ :List[Any]="relu" , lowercase_ :List[str]=3 , lowercase_ :Optional[Any]=None , lowercase_ :Dict=["stage2", "stage3", "stage4"] , lowercase_ :str=[2, 3, 4] , lowercase_ :Union[str, Any]=1 , ) -> Union[str, Any]: UpperCAmelCase = parent UpperCAmelCase = batch_size UpperCAmelCase = image_size UpperCAmelCase = num_channels UpperCAmelCase = embeddings_size UpperCAmelCase = hidden_sizes UpperCAmelCase = depths UpperCAmelCase = is_training UpperCAmelCase = use_labels UpperCAmelCase = hidden_act UpperCAmelCase = num_labels UpperCAmelCase = scope UpperCAmelCase = len(lowercase_ ) UpperCAmelCase = out_features UpperCAmelCase = out_indices UpperCAmelCase = num_groups def UpperCAmelCase__ ( self :Optional[int] ) -> List[str]: UpperCAmelCase = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCAmelCase = None if self.use_labels: UpperCAmelCase = ids_tensor([self.batch_size] , self.num_labels ) UpperCAmelCase = self.get_config() return config, pixel_values, labels def UpperCAmelCase__ ( self :Union[str, Any] ) -> List[str]: return BitConfig( num_channels=self.num_channels , embeddings_size=self.embeddings_size , hidden_sizes=self.hidden_sizes , depths=self.depths , hidden_act=self.hidden_act , num_labels=self.num_labels , out_features=self.out_features , out_indices=self.out_indices , num_groups=self.num_groups , ) def UpperCAmelCase__ ( self :List[str] , lowercase_ :str , lowercase_ :List[str] , lowercase_ :Dict ) -> List[Any]: UpperCAmelCase = BitModel(config=lowercase_ ) model.to(lowercase_ ) model.eval() UpperCAmelCase = model(lowercase_ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def UpperCAmelCase__ ( self :Tuple , lowercase_ :List[str] , lowercase_ :Optional[int] , lowercase_ :Tuple ) -> List[str]: UpperCAmelCase = self.num_labels UpperCAmelCase = BitForImageClassification(lowercase_ ) model.to(lowercase_ ) model.eval() UpperCAmelCase = model(lowercase_ , labels=lowercase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCAmelCase__ ( self :Union[str, Any] , lowercase_ :Optional[int] , lowercase_ :Tuple , lowercase_ :Optional[Any] ) -> List[str]: UpperCAmelCase = BitBackbone(config=lowercase_ ) model.to(lowercase_ ) model.eval() UpperCAmelCase = model(lowercase_ ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , len(config.out_features ) ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [self.batch_size, self.hidden_sizes[1], 4, 4] ) # verify channels self.parent.assertEqual(len(model.channels ) , len(config.out_features ) ) self.parent.assertListEqual(model.channels , config.hidden_sizes[1:] ) # verify backbone works with out_features=None UpperCAmelCase = None UpperCAmelCase = BitBackbone(config=lowercase_ ) model.to(lowercase_ ) model.eval() UpperCAmelCase = model(lowercase_ ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , 1 ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [self.batch_size, self.hidden_sizes[-1], 1, 1] ) # verify channels self.parent.assertEqual(len(model.channels ) , 1 ) self.parent.assertListEqual(model.channels , [config.hidden_sizes[-1]] ) def UpperCAmelCase__ ( self :int ) -> int: UpperCAmelCase = self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = config_and_inputs UpperCAmelCase = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class A_ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = (BitModel, BitForImageClassification, BitBackbone) if is_torch_available() else () __UpperCamelCase = ( {"""feature-extraction""": BitModel, """image-classification""": BitForImageClassification} if is_torch_available() else {} ) __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False def UpperCAmelCase__ ( self :Tuple ) -> Union[str, Any]: UpperCAmelCase = BitModelTester(self ) UpperCAmelCase = ConfigTester(self , config_class=lowercase_ , has_text_modality=lowercase_ ) def UpperCAmelCase__ ( self :Optional[int] ) -> str: self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def UpperCAmelCase__ ( self :int ) -> Tuple: return @unittest.skip(reason='Bit does not output attentions' ) def UpperCAmelCase__ ( self :Dict ) -> int: pass @unittest.skip(reason='Bit does not use inputs_embeds' ) def UpperCAmelCase__ ( self :str ) -> Dict: pass @unittest.skip(reason='Bit does not support input and output embeddings' ) def UpperCAmelCase__ ( self :str ) -> Optional[Any]: pass def UpperCAmelCase__ ( self :List[Any] ) -> Union[str, Any]: UpperCAmelCase , UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase = model_class(lowercase_ ) UpperCAmelCase = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCAmelCase = [*signature.parameters.keys()] UpperCAmelCase = ['pixel_values'] self.assertListEqual(arg_names[:1] , lowercase_ ) def UpperCAmelCase__ ( self :int ) -> List[Any]: UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowercase_ ) def UpperCAmelCase__ ( self :int ) -> List[Any]: UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*lowercase_ ) def UpperCAmelCase__ ( self :int ) -> Optional[Any]: UpperCAmelCase , UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase = model_class(config=lowercase_ ) for name, module in model.named_modules(): if isinstance(lowercase_ , (nn.BatchNormad, nn.GroupNorm) ): self.assertTrue( torch.all(module.weight == 1 ) , msg=f"""Parameter {name} of model {model_class} seems not properly initialized""" , ) self.assertTrue( torch.all(module.bias == 0 ) , msg=f"""Parameter {name} of model {model_class} seems not properly initialized""" , ) def UpperCAmelCase__ ( self :List[Any] ) -> str: def check_hidden_states_output(lowercase_ :int , lowercase_ :Optional[Any] , lowercase_ :List[str] ): UpperCAmelCase = model_class(lowercase_ ) model.to(lowercase_ ) model.eval() with torch.no_grad(): UpperCAmelCase = model(**self._prepare_for_class(lowercase_ , lowercase_ ) ) UpperCAmelCase = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states UpperCAmelCase = self.model_tester.num_stages self.assertEqual(len(lowercase_ ) , expected_num_stages + 1 ) # Bit's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) UpperCAmelCase , UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase = ['preactivation', 'bottleneck'] for model_class in self.all_model_classes: for layer_type in layers_type: UpperCAmelCase = layer_type UpperCAmelCase = True check_hidden_states_output(lowercase_ , lowercase_ , lowercase_ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] UpperCAmelCase = True check_hidden_states_output(lowercase_ , lowercase_ , lowercase_ ) @unittest.skip(reason='Bit does not use feedforward chunking' ) def UpperCAmelCase__ ( self :Optional[int] ) -> List[Any]: pass def UpperCAmelCase__ ( self :Union[str, Any] ) -> Union[str, Any]: UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowercase_ ) @slow def UpperCAmelCase__ ( self :Optional[int] ) -> Union[str, Any]: for model_name in BIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase = BitModel.from_pretrained(lowercase_ ) self.assertIsNotNone(lowercase_ ) def _lowerCAmelCase ( ): UpperCAmelCase = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class A_ ( unittest.TestCase ): """simple docstring""" @cached_property def UpperCAmelCase__ ( self :Tuple ) -> Tuple: return ( BitImageProcessor.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def UpperCAmelCase__ ( self :List[str] ) -> List[Any]: UpperCAmelCase = BitForImageClassification.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to(lowercase_ ) UpperCAmelCase = self.default_image_processor UpperCAmelCase = prepare_img() UpperCAmelCase = image_processor(images=lowercase_ , return_tensors='pt' ).to(lowercase_ ) # forward pass with torch.no_grad(): UpperCAmelCase = model(**lowercase_ ) # verify the logits UpperCAmelCase = torch.Size((1, 10_00) ) self.assertEqual(outputs.logits.shape , lowercase_ ) UpperCAmelCase = torch.tensor([[-0.6526, -0.5263, -1.4398]] ).to(lowercase_ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , lowercase_ , atol=1E-4 ) ) @require_torch class A_ ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = (BitBackbone,) if is_torch_available() else () __UpperCamelCase = BitConfig __UpperCamelCase = False def UpperCAmelCase__ ( self :Optional[int] ) -> List[str]: UpperCAmelCase = BitModelTester(self )
78
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : List[str] = logging.get_logger(__name__) lowerCamelCase__ : List[str] = { 'facebook/s2t-small-librispeech-asr': ( 'https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/config.json' ), # See all Speech2Text models at https://huggingface.co/models?filter=speech_to_text } class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "speech_to_text" lowercase_ = ["past_key_values"] lowercase_ = {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"} def __init__( self : Tuple , _lowerCAmelCase : List[Any]=10_000 , _lowerCAmelCase : List[Any]=12 , _lowerCAmelCase : Union[str, Any]=2_048 , _lowerCAmelCase : Optional[int]=4 , _lowerCAmelCase : Union[str, Any]=6 , _lowerCAmelCase : Optional[int]=2_048 , _lowerCAmelCase : Optional[Any]=4 , _lowerCAmelCase : Any=0.0 , _lowerCAmelCase : Optional[Any]=0.0 , _lowerCAmelCase : List[str]=True , _lowerCAmelCase : List[Any]=True , _lowerCAmelCase : int="relu" , _lowerCAmelCase : Union[str, Any]=256 , _lowerCAmelCase : List[str]=0.1 , _lowerCAmelCase : str=0.0 , _lowerCAmelCase : Tuple=0.0 , _lowerCAmelCase : List[str]=0.02 , _lowerCAmelCase : List[str]=2 , _lowerCAmelCase : Tuple=True , _lowerCAmelCase : List[str]=1 , _lowerCAmelCase : str=0 , _lowerCAmelCase : str=2 , _lowerCAmelCase : Union[str, Any]=6_000 , _lowerCAmelCase : List[str]=1_024 , _lowerCAmelCase : str=2 , _lowerCAmelCase : Optional[Any]=(5, 5) , _lowerCAmelCase : str=1_024 , _lowerCAmelCase : str=80 , _lowerCAmelCase : Tuple=1 , **_lowerCAmelCase : Any , ): SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = d_model SCREAMING_SNAKE_CASE_ = encoder_ffn_dim SCREAMING_SNAKE_CASE_ = encoder_layers SCREAMING_SNAKE_CASE_ = encoder_attention_heads SCREAMING_SNAKE_CASE_ = decoder_ffn_dim SCREAMING_SNAKE_CASE_ = decoder_layers SCREAMING_SNAKE_CASE_ = decoder_attention_heads SCREAMING_SNAKE_CASE_ = dropout SCREAMING_SNAKE_CASE_ = attention_dropout SCREAMING_SNAKE_CASE_ = activation_dropout SCREAMING_SNAKE_CASE_ = activation_function SCREAMING_SNAKE_CASE_ = init_std SCREAMING_SNAKE_CASE_ = encoder_layerdrop SCREAMING_SNAKE_CASE_ = decoder_layerdrop SCREAMING_SNAKE_CASE_ = use_cache SCREAMING_SNAKE_CASE_ = encoder_layers SCREAMING_SNAKE_CASE_ = scale_embedding # scale factor will be sqrt(d_model) if True SCREAMING_SNAKE_CASE_ = max_source_positions SCREAMING_SNAKE_CASE_ = max_target_positions SCREAMING_SNAKE_CASE_ = num_conv_layers SCREAMING_SNAKE_CASE_ = list(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = conv_channels SCREAMING_SNAKE_CASE_ = input_feat_per_channel SCREAMING_SNAKE_CASE_ = input_channels if len(self.conv_kernel_sizes ) != self.num_conv_layers: raise ValueError( 'Configuration for convolutional module is incorrect. ' 'It is required that `len(config.conv_kernel_sizes)` == `config.num_conv_layers` ' F"but is `len(config.conv_kernel_sizes) = {len(self.conv_kernel_sizes )}`, " F"`config.num_conv_layers = {self.num_conv_layers}`." ) super().__init__( pad_token_id=_lowerCAmelCase , bos_token_id=_lowerCAmelCase , eos_token_id=_lowerCAmelCase , is_encoder_decoder=_lowerCAmelCase , decoder_start_token_id=_lowerCAmelCase , **_lowerCAmelCase , )
225
0
'''simple docstring''' def __UpperCamelCase ( UpperCAmelCase ): if not isinstance(UpperCAmelCase , UpperCAmelCase ): raise ValueError('''multiplicative_persistence() only accepts integral values''' ) if num < 0: raise ValueError('''multiplicative_persistence() does not accept negative values''' ) lowercase__ : Tuple = 0 lowercase__ : int = str(UpperCAmelCase ) while len(UpperCAmelCase ) != 1: lowercase__ : List[Any] = [int(UpperCAmelCase ) for i in num_string] lowercase__ : Any = 1 for i in range(0 , len(UpperCAmelCase ) ): total *= numbers[i] lowercase__ : List[Any] = str(UpperCAmelCase ) steps += 1 return steps def __UpperCamelCase ( UpperCAmelCase ): if not isinstance(UpperCAmelCase , UpperCAmelCase ): raise ValueError('''additive_persistence() only accepts integral values''' ) if num < 0: raise ValueError('''additive_persistence() does not accept negative values''' ) lowercase__ : List[str] = 0 lowercase__ : Tuple = str(UpperCAmelCase ) while len(UpperCAmelCase ) != 1: lowercase__ : Optional[Any] = [int(UpperCAmelCase ) for i in num_string] lowercase__ : Dict = 0 for i in range(0 , len(UpperCAmelCase ) ): total += numbers[i] lowercase__ : Any = str(UpperCAmelCase ) steps += 1 return steps if __name__ == "__main__": import doctest doctest.testmod()
214
'''simple docstring''' import numpy as np def __UpperCamelCase ( UpperCAmelCase ): return 1 / (1 + np.exp(-vector )) def __UpperCamelCase ( UpperCAmelCase ): return vector * sigmoid(UpperCAmelCase ) if __name__ == "__main__": import doctest doctest.testmod()
214
1
"""simple docstring""" from typing import Dict from transformers import EvalPrediction, HfArgumentParser, TrainingArguments, is_torch_available from transformers.testing_utils import ( TestCasePlus, execute_subprocess_async, get_torch_dist_unique_port, require_torch_multi_gpu, require_torch_neuroncore, ) from transformers.training_args import ParallelMode from transformers.utils import logging lowerCAmelCase__ : List[Any] = logging.get_logger(__name__) if is_torch_available(): import torch from torch import nn from torch.utils.data import Dataset from transformers import Trainer class snake_case ( __UpperCAmelCase ): """simple docstring""" def __init__( self : Optional[int] ,lowerCamelCase__ : int = 101 ): UpperCAmelCase__ = length def __len__( self : Union[str, Any] ): return self.length def __getitem__( self : str ,lowerCamelCase__ : Tuple ): return i class snake_case : """simple docstring""" def __call__( self : int ,lowerCamelCase__ : Tuple ): return {"input_ids": torch.tensor(lowerCamelCase__ ), "labels": torch.tensor(lowerCamelCase__ )} class snake_case ( nn.Module ): """simple docstring""" def __init__( self : Any ): super().__init__() # Add some (unused) params otherwise DDP will complain. UpperCAmelCase__ = nn.Linear(120 ,80 ) def __lowerCAmelCase ( self : Optional[Any] ,lowerCamelCase__ : List[str] ,lowerCamelCase__ : Union[str, Any]=None ): if labels is not None: return torch.tensor(0.0 ,device=input_ids.device ), input_ids else: return input_ids class snake_case ( __UpperCAmelCase ): """simple docstring""" @require_torch_neuroncore def __lowerCAmelCase ( self : Optional[Any] ): UpperCAmelCase__ = f'''--nproc_per_node=2 --master_port={get_torch_dist_unique_port()} {self.test_file_dir}/test_trainer_distributed.py '''.split() UpperCAmelCase__ = self.get_auto_remove_tmp_dir() UpperCAmelCase__ = f'''--output_dir {output_dir}'''.split() UpperCAmelCase__ = ['torchrun'] + distributed_args + args execute_subprocess_async(lowerCamelCase__ ,env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call class snake_case ( __UpperCAmelCase ): """simple docstring""" @require_torch_multi_gpu def __lowerCAmelCase ( self : List[str] ): UpperCAmelCase__ = f'''--nproc_per_node={torch.cuda.device_count()} --master_port={get_torch_dist_unique_port()} {self.test_file_dir}/test_trainer_distributed.py '''.split() UpperCAmelCase__ = self.get_auto_remove_tmp_dir() UpperCAmelCase__ = f'''--output_dir {output_dir}'''.split() UpperCAmelCase__ = ['torchrun'] + distributed_args + args execute_subprocess_async(lowerCamelCase__ ,env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call if __name__ == "__main__": # The script below is meant to be run under torch.distributed, on a machine with multiple GPUs: # # PYTHONPATH="src" python -m torch.distributed.run --nproc_per_node 2 --output_dir output_dir ./tests/test_trainer_distributed.py lowerCAmelCase__ : List[Any] = HfArgumentParser((TrainingArguments,)) lowerCAmelCase__ : Any = parser.parse_args_into_dataclasses()[0] logger.warning( F"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}, """ F"""distributed training: {training_args.parallel_mode != ParallelMode.NOT_DISTRIBUTED}""" ) # Essentially, what we want to verify in the distributed case is that we get all samples back, # in the right order. (this is crucial for prediction for instance) for dataset_length in [101, 40, 7]: lowerCAmelCase__ : str = DummyDataset(dataset_length) def a_ ( lowerCamelCase ): UpperCAmelCase__ = list(range(len(lowerCamelCase ) ) ) UpperCAmelCase__ = p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential if not success and training_args.local_rank == 0: logger.warning( 'Predictions and/or labels do not match expected results:\n - predictions: ' f'''{p.predictions.tolist()}\n - labels: {p.label_ids.tolist()}\n - expected: {sequential}''' ) return {"success": success} lowerCAmelCase__ : Union[str, Any] = Trainer( model=DummyModel(), args=training_args, data_collator=DummyDataCollator(), eval_dataset=dataset, compute_metrics=compute_metrics, ) lowerCAmelCase__ : str = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) lowerCAmelCase__ : Optional[Any] = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) lowerCAmelCase__ : List[Any] = 2 lowerCAmelCase__ : Optional[Any] = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) lowerCAmelCase__ : List[str] = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) lowerCAmelCase__ : List[str] = None
98
"""simple docstring""" import tempfile import torch from diffusers import PNDMScheduler from .test_schedulers import SchedulerCommonTest class snake_case ( __UpperCAmelCase ): """simple docstring""" snake_case__ = (PNDMScheduler,) snake_case__ = (("num_inference_steps", 50),) def __lowerCAmelCase ( self : List[str] ,**lowerCamelCase__ : str ): UpperCAmelCase__ = { 'num_train_timesteps': 1_000, 'beta_start': 0.0_0_0_1, 'beta_end': 0.0_2, 'beta_schedule': 'linear', } config.update(**lowerCamelCase__ ) return config def __lowerCAmelCase ( self : str ,lowerCamelCase__ : Optional[Any]=0 ,**lowerCamelCase__ : List[str] ): UpperCAmelCase__ = dict(self.forward_default_kwargs ) UpperCAmelCase__ = kwargs.pop('num_inference_steps' ,lowerCamelCase__ ) UpperCAmelCase__ = self.dummy_sample UpperCAmelCase__ = 0.1 * sample UpperCAmelCase__ = [residual + 0.2, residual + 0.1_5, residual + 0.1, residual + 0.0_5] for scheduler_class in self.scheduler_classes: UpperCAmelCase__ = self.get_scheduler_config(**lowerCamelCase__ ) UpperCAmelCase__ = scheduler_class(**lowerCamelCase__ ) scheduler.set_timesteps(lowerCamelCase__ ) # copy over dummy past residuals UpperCAmelCase__ = dummy_past_residuals[:] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(lowerCamelCase__ ) UpperCAmelCase__ = scheduler_class.from_pretrained(lowerCamelCase__ ) new_scheduler.set_timesteps(lowerCamelCase__ ) # copy over dummy past residuals UpperCAmelCase__ = dummy_past_residuals[:] UpperCAmelCase__ = scheduler.step_prk(lowerCamelCase__ ,lowerCamelCase__ ,lowerCamelCase__ ,**lowerCamelCase__ ).prev_sample UpperCAmelCase__ = new_scheduler.step_prk(lowerCamelCase__ ,lowerCamelCase__ ,lowerCamelCase__ ,**lowerCamelCase__ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" UpperCAmelCase__ = scheduler.step_plms(lowerCamelCase__ ,lowerCamelCase__ ,lowerCamelCase__ ,**lowerCamelCase__ ).prev_sample UpperCAmelCase__ = new_scheduler.step_plms(lowerCamelCase__ ,lowerCamelCase__ ,lowerCamelCase__ ,**lowerCamelCase__ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def __lowerCAmelCase ( self : Tuple ): pass def __lowerCAmelCase ( self : Dict ,lowerCamelCase__ : List[str]=0 ,**lowerCamelCase__ : Tuple ): UpperCAmelCase__ = dict(self.forward_default_kwargs ) UpperCAmelCase__ = kwargs.pop('num_inference_steps' ,lowerCamelCase__ ) UpperCAmelCase__ = self.dummy_sample UpperCAmelCase__ = 0.1 * sample UpperCAmelCase__ = [residual + 0.2, residual + 0.1_5, residual + 0.1, residual + 0.0_5] for scheduler_class in self.scheduler_classes: UpperCAmelCase__ = self.get_scheduler_config() UpperCAmelCase__ = scheduler_class(**lowerCamelCase__ ) scheduler.set_timesteps(lowerCamelCase__ ) # copy over dummy past residuals (must be after setting timesteps) UpperCAmelCase__ = dummy_past_residuals[:] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(lowerCamelCase__ ) UpperCAmelCase__ = scheduler_class.from_pretrained(lowerCamelCase__ ) # copy over dummy past residuals new_scheduler.set_timesteps(lowerCamelCase__ ) # copy over dummy past residual (must be after setting timesteps) UpperCAmelCase__ = dummy_past_residuals[:] UpperCAmelCase__ = scheduler.step_prk(lowerCamelCase__ ,lowerCamelCase__ ,lowerCamelCase__ ,**lowerCamelCase__ ).prev_sample UpperCAmelCase__ = new_scheduler.step_prk(lowerCamelCase__ ,lowerCamelCase__ ,lowerCamelCase__ ,**lowerCamelCase__ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" UpperCAmelCase__ = scheduler.step_plms(lowerCamelCase__ ,lowerCamelCase__ ,lowerCamelCase__ ,**lowerCamelCase__ ).prev_sample UpperCAmelCase__ = new_scheduler.step_plms(lowerCamelCase__ ,lowerCamelCase__ ,lowerCamelCase__ ,**lowerCamelCase__ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def __lowerCAmelCase ( self : List[Any] ,**lowerCamelCase__ : int ): UpperCAmelCase__ = self.scheduler_classes[0] UpperCAmelCase__ = self.get_scheduler_config(**lowerCamelCase__ ) UpperCAmelCase__ = scheduler_class(**lowerCamelCase__ ) UpperCAmelCase__ = 10 UpperCAmelCase__ = self.dummy_model() UpperCAmelCase__ = self.dummy_sample_deter scheduler.set_timesteps(lowerCamelCase__ ) for i, t in enumerate(scheduler.prk_timesteps ): UpperCAmelCase__ = model(lowerCamelCase__ ,lowerCamelCase__ ) UpperCAmelCase__ = scheduler.step_prk(lowerCamelCase__ ,lowerCamelCase__ ,lowerCamelCase__ ).prev_sample for i, t in enumerate(scheduler.plms_timesteps ): UpperCAmelCase__ = model(lowerCamelCase__ ,lowerCamelCase__ ) UpperCAmelCase__ = scheduler.step_plms(lowerCamelCase__ ,lowerCamelCase__ ,lowerCamelCase__ ).prev_sample return sample def __lowerCAmelCase ( self : int ): UpperCAmelCase__ = dict(self.forward_default_kwargs ) UpperCAmelCase__ = kwargs.pop('num_inference_steps' ,lowerCamelCase__ ) for scheduler_class in self.scheduler_classes: UpperCAmelCase__ = self.get_scheduler_config() UpperCAmelCase__ = scheduler_class(**lowerCamelCase__ ) UpperCAmelCase__ = self.dummy_sample UpperCAmelCase__ = 0.1 * sample if num_inference_steps is not None and hasattr(lowerCamelCase__ ,'set_timesteps' ): scheduler.set_timesteps(lowerCamelCase__ ) elif num_inference_steps is not None and not hasattr(lowerCamelCase__ ,'set_timesteps' ): UpperCAmelCase__ = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) UpperCAmelCase__ = [residual + 0.2, residual + 0.1_5, residual + 0.1, residual + 0.0_5] UpperCAmelCase__ = dummy_past_residuals[:] UpperCAmelCase__ = scheduler.step_prk(lowerCamelCase__ ,0 ,lowerCamelCase__ ,**lowerCamelCase__ ).prev_sample UpperCAmelCase__ = scheduler.step_prk(lowerCamelCase__ ,1 ,lowerCamelCase__ ,**lowerCamelCase__ ).prev_sample self.assertEqual(output_a.shape ,sample.shape ) self.assertEqual(output_a.shape ,output_a.shape ) UpperCAmelCase__ = scheduler.step_plms(lowerCamelCase__ ,0 ,lowerCamelCase__ ,**lowerCamelCase__ ).prev_sample UpperCAmelCase__ = scheduler.step_plms(lowerCamelCase__ ,1 ,lowerCamelCase__ ,**lowerCamelCase__ ).prev_sample self.assertEqual(output_a.shape ,sample.shape ) self.assertEqual(output_a.shape ,output_a.shape ) def __lowerCAmelCase ( self : List[Any] ): for timesteps in [100, 1_000]: self.check_over_configs(num_train_timesteps=lowerCamelCase__ ) def __lowerCAmelCase ( self : Optional[int] ): for steps_offset in [0, 1]: self.check_over_configs(steps_offset=lowerCamelCase__ ) UpperCAmelCase__ = self.scheduler_classes[0] UpperCAmelCase__ = self.get_scheduler_config(steps_offset=1 ) UpperCAmelCase__ = scheduler_class(**lowerCamelCase__ ) scheduler.set_timesteps(10 ) assert torch.equal( scheduler.timesteps ,torch.LongTensor( [901, 851, 851, 801, 801, 751, 751, 701, 701, 651, 651, 601, 601, 501, 401, 301, 201, 101, 1] ) ,) def __lowerCAmelCase ( self : Dict ): for beta_start, beta_end in zip([0.0_0_0_1, 0.0_0_1] ,[0.0_0_2, 0.0_2] ): self.check_over_configs(beta_start=lowerCamelCase__ ,beta_end=lowerCamelCase__ ) def __lowerCAmelCase ( self : Union[str, Any] ): for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=lowerCamelCase__ ) def __lowerCAmelCase ( self : List[Any] ): for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=lowerCamelCase__ ) def __lowerCAmelCase ( self : Optional[Any] ): for t in [1, 5, 10]: self.check_over_forward(time_step=lowerCamelCase__ ) def __lowerCAmelCase ( self : List[Any] ): for t, num_inference_steps in zip([1, 5, 10] ,[10, 50, 100] ): self.check_over_forward(num_inference_steps=lowerCamelCase__ ) def __lowerCAmelCase ( self : int ): # earlier version of set_timesteps() caused an error indexing alpha's with inference steps as power of 3 UpperCAmelCase__ = 27 for scheduler_class in self.scheduler_classes: UpperCAmelCase__ = self.dummy_sample UpperCAmelCase__ = 0.1 * sample UpperCAmelCase__ = self.get_scheduler_config() UpperCAmelCase__ = scheduler_class(**lowerCamelCase__ ) scheduler.set_timesteps(lowerCamelCase__ ) # before power of 3 fix, would error on first step, so we only need to do two for i, t in enumerate(scheduler.prk_timesteps[:2] ): UpperCAmelCase__ = scheduler.step_prk(lowerCamelCase__ ,lowerCamelCase__ ,lowerCamelCase__ ).prev_sample def __lowerCAmelCase ( self : int ): with self.assertRaises(lowerCamelCase__ ): UpperCAmelCase__ = self.scheduler_classes[0] UpperCAmelCase__ = self.get_scheduler_config() UpperCAmelCase__ = scheduler_class(**lowerCamelCase__ ) scheduler.step_plms(self.dummy_sample ,1 ,self.dummy_sample ).prev_sample def __lowerCAmelCase ( self : Tuple ): UpperCAmelCase__ = self.full_loop() UpperCAmelCase__ = torch.sum(torch.abs(lowerCamelCase__ ) ) UpperCAmelCase__ = torch.mean(torch.abs(lowerCamelCase__ ) ) assert abs(result_sum.item() - 1_9_8.1_3_1_8 ) < 1e-2 assert abs(result_mean.item() - 0.2_5_8_0 ) < 1e-3 def __lowerCAmelCase ( self : Tuple ): UpperCAmelCase__ = self.full_loop(prediction_type='v_prediction' ) UpperCAmelCase__ = torch.sum(torch.abs(lowerCamelCase__ ) ) UpperCAmelCase__ = torch.mean(torch.abs(lowerCamelCase__ ) ) assert abs(result_sum.item() - 6_7.3_9_8_6 ) < 1e-2 assert abs(result_mean.item() - 0.0_8_7_8 ) < 1e-3 def __lowerCAmelCase ( self : Union[str, Any] ): # We specify different beta, so that the first alpha is 0.99 UpperCAmelCase__ = self.full_loop(set_alpha_to_one=lowerCamelCase__ ,beta_start=0.0_1 ) UpperCAmelCase__ = torch.sum(torch.abs(lowerCamelCase__ ) ) UpperCAmelCase__ = torch.mean(torch.abs(lowerCamelCase__ ) ) assert abs(result_sum.item() - 2_3_0.0_3_9_9 ) < 1e-2 assert abs(result_mean.item() - 0.2_9_9_5 ) < 1e-3 def __lowerCAmelCase ( self : Tuple ): # We specify different beta, so that the first alpha is 0.99 UpperCAmelCase__ = self.full_loop(set_alpha_to_one=lowerCamelCase__ ,beta_start=0.0_1 ) UpperCAmelCase__ = torch.sum(torch.abs(lowerCamelCase__ ) ) UpperCAmelCase__ = torch.mean(torch.abs(lowerCamelCase__ ) ) assert abs(result_sum.item() - 1_8_6.9_4_8_2 ) < 1e-2 assert abs(result_mean.item() - 0.2_4_3_4 ) < 1e-3
98
1
import numpy as np from transformers import Pipeline def lowerCAmelCase( __lowerCamelCase ): __a = np.max(__lowerCamelCase , axis=-1 , keepdims=__lowerCamelCase ) __a = np.exp(outputs - maxes ) return shifted_exp / shifted_exp.sum(axis=-1 , keepdims=__lowerCamelCase ) class a__ ( __snake_case ): def __SCREAMING_SNAKE_CASE ( self , **UpperCAmelCase ) -> int: __a = {} if "second_text" in kwargs: __a = kwargs['second_text'] return preprocess_kwargs, {}, {} def __SCREAMING_SNAKE_CASE ( self , UpperCAmelCase , UpperCAmelCase=None ) -> Dict: return self.tokenizer(UpperCAmelCase , text_pair=UpperCAmelCase , return_tensors=self.framework ) def __SCREAMING_SNAKE_CASE ( self , UpperCAmelCase ) -> Union[str, Any]: return self.model(**UpperCAmelCase ) def __SCREAMING_SNAKE_CASE ( self , UpperCAmelCase ) -> List[str]: __a = model_outputs.logits[0].numpy() __a = softmax(UpperCAmelCase ) __a = np.argmax(UpperCAmelCase ) __a = self.model.config.idalabel[best_class] __a = probabilities[best_class].item() __a = logits.tolist() return {"label": label, "score": score, "logits": logits}
368
from datetime import datetime import matplotlib.pyplot as plt import torch def lowerCAmelCase( __lowerCamelCase ): for param in module.parameters(): __a = False def lowerCAmelCase( ): __a = 'cuda' if torch.cuda.is_available() else 'cpu' if torch.backends.mps.is_available() and torch.backends.mps.is_built(): __a = 'mps' if device == "mps": print( 'WARNING: MPS currently doesn\'t seem to work, and messes up backpropagation without any visible torch' ' errors. I recommend using CUDA on a colab notebook or CPU instead if you\'re facing inexplicable issues' ' with generations.' ) return device def lowerCAmelCase( __lowerCamelCase ): __a = plt.imshow(__lowerCamelCase ) fig.axes.get_xaxis().set_visible(__lowerCamelCase ) fig.axes.get_yaxis().set_visible(__lowerCamelCase ) plt.show() def lowerCAmelCase( ): __a = datetime.now() __a = current_time.strftime('%H:%M:%S' ) return timestamp
197
0
"""simple docstring""" import gc import random import unittest import numpy as np import torch from PIL import Image from diffusers import ( DDIMScheduler, KandinskyVaaInpaintPipeline, KandinskyVaaPriorPipeline, UNetaDConditionModel, VQModel, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class A__ ( _lowerCamelCase , unittest.TestCase): A_ : Any = KandinskyVaaInpaintPipeline A_ : Tuple = ['image_embeds', 'negative_image_embeds', 'image', 'mask_image'] A_ : List[str] = [ 'image_embeds', 'negative_image_embeds', 'image', 'mask_image', ] A_ : List[str] = [ 'generator', 'height', 'width', 'latents', 'guidance_scale', 'num_inference_steps', 'return_dict', 'guidance_scale', 'num_images_per_prompt', 'output_type', 'return_dict', ] A_ : Tuple = False @property def __lowerCamelCase ( self ): return 32 @property def __lowerCamelCase ( self ): return 32 @property def __lowerCamelCase ( self ): return self.time_input_dim @property def __lowerCamelCase ( self ): return self.time_input_dim * 4 @property def __lowerCamelCase ( self ): return 1_00 @property def __lowerCamelCase ( self ): torch.manual_seed(0 ) __lowerCAmelCase : Optional[Any] = { 'in_channels': 9, # Out channels is double in channels because predicts mean and variance 'out_channels': 8, 'addition_embed_type': 'image', 'down_block_types': ('ResnetDownsampleBlock2D', 'SimpleCrossAttnDownBlock2D'), 'up_block_types': ('SimpleCrossAttnUpBlock2D', 'ResnetUpsampleBlock2D'), 'mid_block_type': 'UNetMidBlock2DSimpleCrossAttn', 'block_out_channels': (self.block_out_channels_a, self.block_out_channels_a * 2), 'layers_per_block': 1, 'encoder_hid_dim': self.text_embedder_hidden_size, 'encoder_hid_dim_type': 'image_proj', 'cross_attention_dim': self.cross_attention_dim, 'attention_head_dim': 4, 'resnet_time_scale_shift': 'scale_shift', 'class_embed_type': None, } __lowerCAmelCase : Optional[Any] = UNetaDConditionModel(**_SCREAMING_SNAKE_CASE ) return model @property def __lowerCamelCase ( self ): return { "block_out_channels": [32, 64], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def __lowerCamelCase ( self ): torch.manual_seed(0 ) __lowerCAmelCase : str = VQModel(**self.dummy_movq_kwargs ) return model def __lowerCamelCase ( self ): __lowerCAmelCase : Dict = self.dummy_unet __lowerCAmelCase : List[str] = self.dummy_movq __lowerCAmelCase : Union[str, Any] = DDIMScheduler( num_train_timesteps=10_00 , beta_schedule='linear' , beta_start=0.0_0085 , beta_end=0.012 , clip_sample=_SCREAMING_SNAKE_CASE , set_alpha_to_one=_SCREAMING_SNAKE_CASE , steps_offset=1 , prediction_type='epsilon' , thresholding=_SCREAMING_SNAKE_CASE , ) __lowerCAmelCase : str = { 'unet': unet, 'scheduler': scheduler, 'movq': movq, } return components def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=0 ): __lowerCAmelCase : List[Any] = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(_SCREAMING_SNAKE_CASE ) ).to(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Optional[Any] = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(seed + 1 ) ).to( _SCREAMING_SNAKE_CASE ) # create init_image __lowerCAmelCase : Tuple = floats_tensor((1, 3, 64, 64) , rng=random.Random(_SCREAMING_SNAKE_CASE ) ).to(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : str = image.cpu().permute(0 , 2 , 3 , 1 )[0] __lowerCAmelCase : List[Any] = Image.fromarray(np.uinta(_SCREAMING_SNAKE_CASE ) ).convert('RGB' ).resize((2_56, 2_56) ) # create mask __lowerCAmelCase : Optional[Any] = np.ones((64, 64) , dtype=np.floataa ) __lowerCAmelCase : Dict = 0 if str(_SCREAMING_SNAKE_CASE ).startswith('mps' ): __lowerCAmelCase : Dict = torch.manual_seed(_SCREAMING_SNAKE_CASE ) else: __lowerCAmelCase : Optional[int] = torch.Generator(device=_SCREAMING_SNAKE_CASE ).manual_seed(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : int = { 'image': init_image, 'mask_image': mask, 'image_embeds': image_embeds, 'negative_image_embeds': negative_image_embeds, 'generator': generator, 'height': 64, 'width': 64, 'num_inference_steps': 2, 'guidance_scale': 4.0, 'output_type': 'np', } return inputs def __lowerCamelCase ( self ): __lowerCAmelCase : Dict = 'cpu' __lowerCAmelCase : Union[str, Any] = self.get_dummy_components() __lowerCAmelCase : List[Any] = self.pipeline_class(**_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : str = pipe.to(_SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : List[str] = pipe(**self.get_dummy_inputs(_SCREAMING_SNAKE_CASE ) ) __lowerCAmelCase : Union[str, Any] = output.images __lowerCAmelCase : Optional[Any] = pipe( **self.get_dummy_inputs(_SCREAMING_SNAKE_CASE ) , return_dict=_SCREAMING_SNAKE_CASE , )[0] __lowerCAmelCase : str = image[0, -3:, -3:, -1] __lowerCAmelCase : int = image_from_tuple[0, -3:, -3:, -1] print(f"image.shape {image.shape}" ) assert image.shape == (1, 64, 64, 3) __lowerCAmelCase : List[Any] = np.array( [0.5077_5903, 0.4952_7195, 0.4882_4543, 0.5019_2237, 0.4864_4906, 0.4937_3814, 0.478_0598, 0.4723_4827, 0.4832_7848] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 ), f" expected_slice {expected_slice}, but got {image_slice.flatten()}" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 ), f" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}" def __lowerCamelCase ( self ): super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class A__ ( unittest.TestCase): def __lowerCamelCase ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __lowerCamelCase ( self ): __lowerCAmelCase : Optional[int] = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinskyv22/kandinskyv22_inpaint_cat_with_hat_fp16.npy' ) __lowerCAmelCase : Optional[int] = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinsky/cat.png' ) __lowerCAmelCase : Union[str, Any] = np.ones((7_68, 7_68) , dtype=np.floataa ) __lowerCAmelCase : List[str] = 0 __lowerCAmelCase : Dict = 'a hat' __lowerCAmelCase : Optional[int] = KandinskyVaaPriorPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-2-prior' , torch_dtype=torch.floataa ) pipe_prior.to(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : str = KandinskyVaaInpaintPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-2-decoder-inpaint' , torch_dtype=torch.floataa ) __lowerCAmelCase : str = pipeline.to(_SCREAMING_SNAKE_CASE ) pipeline.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : int = torch.Generator(device='cpu' ).manual_seed(0 ) __lowerCAmelCase , __lowerCAmelCase : Union[str, Any] = pipe_prior( _SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE , num_inference_steps=5 , negative_prompt='' , ).to_tuple() __lowerCAmelCase : int = pipeline( image=_SCREAMING_SNAKE_CASE , mask_image=_SCREAMING_SNAKE_CASE , image_embeds=_SCREAMING_SNAKE_CASE , negative_image_embeds=_SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE , num_inference_steps=1_00 , height=7_68 , width=7_68 , output_type='np' , ) __lowerCAmelCase : Optional[int] = output.images[0] assert image.shape == (7_68, 7_68, 3) assert_mean_pixel_difference(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE )
86
"""simple docstring""" def __lowerCAmelCase (_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): return round(float(moles / volume ) * nfactor ) def __lowerCAmelCase (_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): return round(float((moles * 0.0821 * temperature) / (volume) ) ) def __lowerCAmelCase (_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): return round(float((moles * 0.0821 * temperature) / (pressure) ) ) def __lowerCAmelCase (_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): return round(float((pressure * volume) / (0.0821 * moles) ) ) if __name__ == "__main__": import doctest doctest.testmod()
86
1
"""simple docstring""" import warnings from typing import List, Optional, Tuple, Union import numpy as np import PIL import torch from ...models import UNetaDModel from ...schedulers import RePaintScheduler from ...utils import PIL_INTERPOLATION, logging, randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput a_ = logging.get_logger(__name__) # pylint: disable=invalid-name def a__ ( __lowercase ): warnings.warn( "The preprocess method is deprecated and will be removed in a future version. Please" " use VaeImageProcessor.preprocess instead" , __lowercase , ) if isinstance(__lowercase , torch.Tensor ): return image elif isinstance(__lowercase , PIL.Image.Image ): _A = [image] if isinstance(image[0] , PIL.Image.Image ): _A , _A = image[0].size _A , _A = (x - x % 8 for x in (w, h)) # resize to integer multiple of 8 _A = [np.array(i.resize((w, h) , resample=PIL_INTERPOLATION["lanczos"] ) )[None, :] for i in image] _A = np.concatenate(__lowercase , axis=0 ) _A = np.array(__lowercase ).astype(np.floataa ) / 255.0 _A = image.transpose(0 , 3 , 1 , 2 ) _A = 2.0 * image - 1.0 _A = torch.from_numpy(__lowercase ) elif isinstance(image[0] , torch.Tensor ): _A = torch.cat(__lowercase , dim=0 ) return image def a__ ( __lowercase ): if isinstance(__lowercase , torch.Tensor ): return mask elif isinstance(__lowercase , PIL.Image.Image ): _A = [mask] if isinstance(mask[0] , PIL.Image.Image ): _A , _A = mask[0].size _A , _A = (x - x % 32 for x in (w, h)) # resize to integer multiple of 32 _A = [np.array(m.convert("L" ).resize((w, h) , resample=PIL_INTERPOLATION["nearest"] ) )[None, :] for m in mask] _A = np.concatenate(__lowercase , axis=0 ) _A = mask.astype(np.floataa ) / 255.0 _A = 0 _A = 1 _A = torch.from_numpy(__lowercase ) elif isinstance(mask[0] , torch.Tensor ): _A = torch.cat(__lowercase , dim=0 ) return mask class snake_case ( _UpperCamelCase): __UpperCamelCase = 42 __UpperCamelCase = 42 def __init__( self : str , a__ : str , a__ : Tuple ) -> str: '''simple docstring''' super().__init__() self.register_modules(unet=a__ , scheduler=a__ ) @torch.no_grad() def __call__( self : List[Any] , a__ : Union[torch.Tensor, PIL.Image.Image] , a__ : Union[torch.Tensor, PIL.Image.Image] , a__ : int = 2_50 , a__ : float = 0.0 , a__ : int = 10 , a__ : int = 10 , a__ : Optional[Union[torch.Generator, List[torch.Generator]]] = None , a__ : Optional[str] = "pil" , a__ : bool = True , ) -> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' _A = image _A = _preprocess_image(a__ ) _A = original_image.to(device=self.device , dtype=self.unet.dtype ) _A = _preprocess_mask(a__ ) _A = mask_image.to(device=self.device , dtype=self.unet.dtype ) _A = original_image.shape[0] # sample gaussian noise to begin the loop if isinstance(a__ , a__ ) and len(a__ ) != batch_size: raise ValueError( F"""You have passed a list of generators of length {len(a__ )}, but requested an effective batch""" F""" size of {batch_size}. Make sure the batch size matches the length of the generators.""" ) _A = original_image.shape _A = randn_tensor(a__ , generator=a__ , device=self.device , dtype=self.unet.dtype ) # set step values self.scheduler.set_timesteps(a__ , a__ , a__ , self.device ) _A = eta _A = self.scheduler.timesteps[0] + 1 _A = generator[0] if isinstance(a__ , a__ ) else generator for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): if t < t_last: # predict the noise residual _A = self.unet(a__ , a__ ).sample # compute previous image: x_t -> x_t-1 _A = self.scheduler.step(a__ , a__ , a__ , a__ , a__ , a__ ).prev_sample else: # compute the reverse: x_t-1 -> x_t _A = self.scheduler.undo_step(a__ , a__ , a__ ) _A = t _A = (image / 2 + 0.5).clamp(0 , 1 ) _A = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": _A = self.numpy_to_pil(a__ ) if not return_dict: return (image,) return ImagePipelineOutput(images=a__ )
359
"""simple docstring""" import collections.abc from typing import Optional, Tuple, Union import torch import torch.utils.checkpoint from torch import nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import BaseModelOutputWithNoAttention, ImageClassifierOutputWithNoAttention from ...modeling_utils import PreTrainedModel from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging from .configuration_poolformer import PoolFormerConfig a_ = logging.get_logger(__name__) # General docstring a_ = "PoolFormerConfig" # Base docstring a_ = "sail/poolformer_s12" a_ = [1, 5_12, 7, 7] # Image classification docstring a_ = "sail/poolformer_s12" a_ = "tabby, tabby cat" a_ = [ "sail/poolformer_s12", # See all PoolFormer models at https://huggingface.co/models?filter=poolformer ] def a__ ( __lowercase , __lowercase = 0.0 , __lowercase = False ) -> Dict: if drop_prob == 0.0 or not training: return input _A = 1 - drop_prob _A = (input.shape[0],) + (1,) * (input.ndim - 1) # work with diff dim tensors, not just 2D ConvNets _A = keep_prob + torch.rand(__lowercase , dtype=input.dtype , device=input.device ) random_tensor.floor_() # binarize _A = input.div(__lowercase ) * random_tensor return output class snake_case ( nn.Module): def __init__( self : Any , a__ : Optional[float] = None ) -> None: '''simple docstring''' super().__init__() _A = drop_prob def a_ ( self : Optional[Any] , a__ : torch.Tensor ) -> torch.Tensor: '''simple docstring''' return drop_path(a__ , self.drop_prob , self.training ) def a_ ( self : List[str] ) -> str: '''simple docstring''' return "p={}".format(self.drop_prob ) class snake_case ( nn.Module): def __init__( self : Union[str, Any] , a__ : List[Any] , a__ : Any , a__ : List[Any] , a__ : Optional[int] , a__ : Dict , a__ : str=None ) -> Optional[Any]: '''simple docstring''' super().__init__() _A = patch_size if isinstance(a__ , collections.abc.Iterable ) else (patch_size, patch_size) _A = stride if isinstance(a__ , collections.abc.Iterable ) else (stride, stride) _A = padding if isinstance(a__ , collections.abc.Iterable ) else (padding, padding) _A = nn.Convad(a__ , a__ , kernel_size=a__ , stride=a__ , padding=a__ ) _A = norm_layer(a__ ) if norm_layer else nn.Identity() def a_ ( self : Dict , a__ : Any ) -> List[str]: '''simple docstring''' _A = self.projection(a__ ) _A = self.norm(a__ ) return embeddings class snake_case ( nn.GroupNorm): def __init__( self : Dict , a__ : Optional[int] , **a__ : Dict ) -> Optional[Any]: '''simple docstring''' super().__init__(1 , a__ , **a__ ) class snake_case ( nn.Module): def __init__( self : int , a__ : List[Any] ) -> Union[str, Any]: '''simple docstring''' super().__init__() _A = nn.AvgPoolad(a__ , stride=1 , padding=pool_size // 2 , count_include_pad=a__ ) def a_ ( self : List[str] , a__ : int ) -> str: '''simple docstring''' return self.pool(a__ ) - hidden_states class snake_case ( nn.Module): def __init__( self : Tuple , a__ : Optional[int] , a__ : Optional[Any] , a__ : List[str] , a__ : Optional[int] ) -> Any: '''simple docstring''' super().__init__() _A = nn.Convad(a__ , a__ , 1 ) _A = nn.Convad(a__ , a__ , 1 ) _A = PoolFormerDropPath(a__ ) if isinstance(config.hidden_act , a__ ): _A = ACTaFN[config.hidden_act] else: _A = config.hidden_act def a_ ( self : List[Any] , a__ : int ) -> Dict: '''simple docstring''' _A = self.conva(a__ ) _A = self.act_fn(a__ ) _A = self.drop(a__ ) _A = self.conva(a__ ) _A = self.drop(a__ ) return hidden_states class snake_case ( nn.Module): def __init__( self : Union[str, Any] , a__ : str , a__ : List[str] , a__ : List[Any] , a__ : List[str] , a__ : Optional[Any] , a__ : Tuple ) -> Dict: '''simple docstring''' super().__init__() _A = PoolFormerPooling(a__ ) _A = PoolFormerOutput(a__ , a__ , a__ , a__ ) _A = PoolFormerGroupNorm(a__ ) _A = PoolFormerGroupNorm(a__ ) # Useful for training neural nets _A = PoolFormerDropPath(a__ ) if drop_path > 0.0 else nn.Identity() _A = config.use_layer_scale if config.use_layer_scale: _A = nn.Parameter( config.layer_scale_init_value * torch.ones((a__) ) , requires_grad=a__ ) _A = nn.Parameter( config.layer_scale_init_value * torch.ones((a__) ) , requires_grad=a__ ) def a_ ( self : Union[str, Any] , a__ : Optional[int] ) -> Tuple: '''simple docstring''' if self.use_layer_scale: _A = self.pooling(self.before_norm(a__ ) ) _A = self.layer_scale_a.unsqueeze(-1 ).unsqueeze(-1 ) * pooling_output # First residual connection _A = hidden_states + self.drop_path(a__ ) _A = () _A = self.output(self.after_norm(a__ ) ) _A = self.layer_scale_a.unsqueeze(-1 ).unsqueeze(-1 ) * layer_output # Second residual connection _A = hidden_states + self.drop_path(a__ ) _A = (output,) + outputs return outputs else: _A = self.drop_path(self.pooling(self.before_norm(a__ ) ) ) # First residual connection _A = pooling_output + hidden_states _A = () # Second residual connection inside the PoolFormerOutput block _A = self.drop_path(self.output(self.after_norm(a__ ) ) ) _A = hidden_states + layer_output _A = (output,) + outputs return outputs class snake_case ( nn.Module): def __init__( self : str , a__ : int ) -> Any: '''simple docstring''' super().__init__() _A = config # stochastic depth decay rule _A = [x.item() for x in torch.linspace(0 , config.drop_path_rate , sum(config.depths ) )] # patch embeddings _A = [] for i in range(config.num_encoder_blocks ): embeddings.append( PoolFormerEmbeddings( patch_size=config.patch_sizes[i] , stride=config.strides[i] , padding=config.padding[i] , num_channels=config.num_channels if i == 0 else config.hidden_sizes[i - 1] , hidden_size=config.hidden_sizes[i] , ) ) _A = nn.ModuleList(a__ ) # Transformer blocks _A = [] _A = 0 for i in range(config.num_encoder_blocks ): # each block consists of layers _A = [] if i != 0: cur += config.depths[i - 1] for j in range(config.depths[i] ): layers.append( PoolFormerLayer( a__ , num_channels=config.hidden_sizes[i] , pool_size=config.pool_size , hidden_size=config.hidden_sizes[i] , intermediate_size=int(config.hidden_sizes[i] * config.mlp_ratio ) , drop_path=dpr[cur + j] , ) ) blocks.append(nn.ModuleList(a__ ) ) _A = nn.ModuleList(a__ ) def a_ ( self : Tuple , a__ : Union[str, Any] , a__ : Tuple=False , a__ : List[str]=True ) -> List[Any]: '''simple docstring''' _A = () if output_hidden_states else None _A = pixel_values for idx, layers in enumerate(zip(self.patch_embeddings , self.block ) ): _A , _A = layers # Get patch embeddings from hidden_states _A = embedding_layer(a__ ) # Send the embeddings through the blocks for _, blk in enumerate(a__ ): _A = blk(a__ ) _A = layer_outputs[0] if output_hidden_states: _A = all_hidden_states + (hidden_states,) if not return_dict: return tuple(v for v in [hidden_states, all_hidden_states] if v is not None ) return BaseModelOutputWithNoAttention(last_hidden_state=a__ , hidden_states=a__ ) class snake_case ( _UpperCamelCase): __UpperCamelCase = PoolFormerConfig __UpperCamelCase = 'poolformer' __UpperCamelCase = 'pixel_values' __UpperCamelCase = True def a_ ( self : Tuple , a__ : Dict ) -> Any: '''simple docstring''' if isinstance(a__ , (nn.Linear, nn.Convad) ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() elif isinstance(a__ , nn.LayerNorm ): module.bias.data.zero_() module.weight.data.fill_(1.0 ) def a_ ( self : int , a__ : Dict , a__ : int=False ) -> str: '''simple docstring''' if isinstance(a__ , a__ ): _A = value a_ = r"\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n it 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 ([`PoolFormerConfig`]): 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" a_ = 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 [`PoolFormerImageProcessor.__call__`] for details.\n" @add_start_docstrings( 'The bare PoolFormer Model transformer outputting raw hidden-states without any specific head on top.' , _UpperCamelCase , ) class snake_case ( _UpperCamelCase): def __init__( self : int , a__ : Dict ) -> str: '''simple docstring''' super().__init__(a__ ) _A = config _A = PoolFormerEncoder(a__ ) # Initialize weights and apply final processing self.post_init() def a_ ( self : Union[str, Any] ) -> str: '''simple docstring''' return self.embeddings.patch_embeddings @add_start_docstrings_to_model_forward(a__ ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=a__ , config_class=_CONFIG_FOR_DOC , modality="vision" , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def a_ ( self : Tuple , a__ : Optional[torch.FloatTensor] = None , a__ : Optional[bool] = None , a__ : Optional[bool] = None , ) -> Union[Tuple, BaseModelOutputWithNoAttention]: '''simple docstring''' _A = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) _A = return_dict if return_dict is not None else self.config.use_return_dict if pixel_values is None: raise ValueError("You have to specify pixel_values" ) _A = self.encoder( a__ , output_hidden_states=a__ , return_dict=a__ , ) _A = encoder_outputs[0] if not return_dict: return (sequence_output, None) + encoder_outputs[1:] return BaseModelOutputWithNoAttention( last_hidden_state=a__ , hidden_states=encoder_outputs.hidden_states , ) class snake_case ( nn.Module): def __init__( self : List[str] , a__ : Dict ) -> Optional[Any]: '''simple docstring''' super().__init__() _A = nn.Linear(config.hidden_size , config.hidden_size ) def a_ ( self : int , a__ : Tuple ) -> str: '''simple docstring''' _A = self.dense(a__ ) return output @add_start_docstrings( '\n PoolFormer Model transformer with an image classification head on top\n ' , _UpperCamelCase , ) class snake_case ( _UpperCamelCase): def __init__( self : Tuple , a__ : str ) -> Optional[int]: '''simple docstring''' super().__init__(a__ ) _A = config.num_labels _A = PoolFormerModel(a__ ) # Final norm _A = PoolFormerGroupNorm(config.hidden_sizes[-1] ) # Classifier head _A = ( 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(a__ ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=a__ , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def a_ ( self : int , a__ : Optional[torch.FloatTensor] = None , a__ : Optional[torch.LongTensor] = None , a__ : Optional[bool] = None , a__ : Optional[bool] = None , ) -> Union[Tuple, ImageClassifierOutputWithNoAttention]: '''simple docstring''' _A = return_dict if return_dict is not None else self.config.use_return_dict _A = self.poolformer( a__ , output_hidden_states=a__ , return_dict=a__ , ) _A = outputs[0] _A = self.classifier(self.norm(a__ ).mean([-2, -1] ) ) _A = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: _A = "regression" elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): _A = "single_label_classification" else: _A = "multi_label_classification" if self.config.problem_type == "regression": _A = MSELoss() if self.num_labels == 1: _A = loss_fct(logits.squeeze() , labels.squeeze() ) else: _A = loss_fct(a__ , a__ ) elif self.config.problem_type == "single_label_classification": _A = CrossEntropyLoss() _A = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": _A = BCEWithLogitsLoss() _A = loss_fct(a__ , a__ ) if not return_dict: _A = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return ImageClassifierOutputWithNoAttention(loss=a__ , logits=a__ , hidden_states=outputs.hidden_states )
163
0
import argparse import json import os import pickle import shutil import numpy as np import torch from distiller import Distiller from lm_seqs_dataset import LmSeqsDataset from transformers import ( BertConfig, BertForMaskedLM, BertTokenizer, DistilBertConfig, DistilBertForMaskedLM, DistilBertTokenizer, GPTaConfig, GPTaLMHeadModel, GPTaTokenizer, RobertaConfig, RobertaForMaskedLM, RobertaTokenizer, ) from utils import git_log, init_gpu_params, logger, set_seed __lowerCamelCase : Optional[int] = { '''distilbert''': (DistilBertConfig, DistilBertForMaskedLM, DistilBertTokenizer), '''roberta''': (RobertaConfig, RobertaForMaskedLM, RobertaTokenizer), '''bert''': (BertConfig, BertForMaskedLM, BertTokenizer), '''gpt2''': (GPTaConfig, GPTaLMHeadModel, GPTaTokenizer), } def _snake_case ( lowerCAmelCase : Optional[Any] ): """simple docstring""" assert (args.mlm and args.alpha_mlm > 0.0) or (not args.mlm and args.alpha_mlm == 0.0) assert (args.alpha_mlm > 0.0 and args.alpha_clm == 0.0) or (args.alpha_mlm == 0.0 and args.alpha_clm > 0.0) if args.mlm: assert os.path.isfile(args.token_counts ) assert (args.student_type in ["roberta", "distilbert"]) and (args.teacher_type in ["roberta", "bert"]) else: assert (args.student_type in ["gpt2"]) and (args.teacher_type in ["gpt2"]) assert args.teacher_type == args.student_type or ( args.student_type == "distilbert" and args.teacher_type == "bert" ) assert os.path.isfile(args.student_config ) if args.student_pretrained_weights is not None: assert os.path.isfile(args.student_pretrained_weights ) if args.freeze_token_type_embds: assert args.student_type in ["roberta"] assert args.alpha_ce >= 0.0 assert args.alpha_mlm >= 0.0 assert args.alpha_clm >= 0.0 assert args.alpha_mse >= 0.0 assert args.alpha_cos >= 0.0 assert args.alpha_ce + args.alpha_mlm + args.alpha_clm + args.alpha_mse + args.alpha_cos > 0.0 def _snake_case ( lowerCAmelCase : str , lowerCAmelCase : Optional[int] ): """simple docstring""" if args.student_type == "roberta": SCREAMING_SNAKE_CASE_ : Tuple = False elif args.student_type == "gpt2": SCREAMING_SNAKE_CASE_ : int = False def _snake_case ( lowerCAmelCase : str , lowerCAmelCase : int ): """simple docstring""" if args.student_type == "roberta": SCREAMING_SNAKE_CASE_ : str = False def _snake_case ( ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = argparse.ArgumentParser(description="Training" ) parser.add_argument("--force" , action="store_true" , help="Overwrite dump_path if it already exists." ) parser.add_argument( "--dump_path" , type=lowerCAmelCase , required=lowerCAmelCase , help="The output directory (log, checkpoints, parameters, etc.)" ) parser.add_argument( "--data_file" , type=lowerCAmelCase , required=lowerCAmelCase , help="The binarized file (tokenized + tokens_to_ids) and grouped by sequence." , ) parser.add_argument( "--student_type" , type=lowerCAmelCase , choices=["distilbert", "roberta", "gpt2"] , required=lowerCAmelCase , help="The student type (DistilBERT, RoBERTa)." , ) parser.add_argument("--student_config" , type=lowerCAmelCase , required=lowerCAmelCase , help="Path to the student configuration." ) parser.add_argument( "--student_pretrained_weights" , default=lowerCAmelCase , type=lowerCAmelCase , help="Load student initialization checkpoint." ) parser.add_argument( "--teacher_type" , choices=["bert", "roberta", "gpt2"] , required=lowerCAmelCase , help="Teacher type (BERT, RoBERTa)." ) parser.add_argument("--teacher_name" , type=lowerCAmelCase , required=lowerCAmelCase , help="The teacher model." ) parser.add_argument("--temperature" , default=2.0 , type=lowerCAmelCase , help="Temperature for the softmax temperature." ) parser.add_argument( "--alpha_ce" , default=0.5 , type=lowerCAmelCase , help="Linear weight for the distillation loss. Must be >=0." ) parser.add_argument( "--alpha_mlm" , default=0.0 , type=lowerCAmelCase , help="Linear weight for the MLM loss. Must be >=0. Should be used in conjunction with `mlm` flag." , ) parser.add_argument("--alpha_clm" , default=0.5 , type=lowerCAmelCase , help="Linear weight for the CLM loss. Must be >=0." ) parser.add_argument("--alpha_mse" , default=0.0 , type=lowerCAmelCase , help="Linear weight of the MSE loss. Must be >=0." ) parser.add_argument( "--alpha_cos" , default=0.0 , type=lowerCAmelCase , help="Linear weight of the cosine embedding loss. Must be >=0." ) parser.add_argument( "--mlm" , action="store_true" , help="The LM step: MLM or CLM. If `mlm` is True, the MLM is used over CLM." ) parser.add_argument( "--mlm_mask_prop" , default=0.15 , type=lowerCAmelCase , help="Proportion of tokens for which we need to make a prediction." , ) parser.add_argument("--word_mask" , default=0.8 , type=lowerCAmelCase , help="Proportion of tokens to mask out." ) parser.add_argument("--word_keep" , default=0.1 , type=lowerCAmelCase , help="Proportion of tokens to keep." ) parser.add_argument("--word_rand" , default=0.1 , type=lowerCAmelCase , help="Proportion of tokens to randomly replace." ) parser.add_argument( "--mlm_smoothing" , default=0.7 , type=lowerCAmelCase , help="Smoothing parameter to emphasize more rare tokens (see XLM, similar to word2vec)." , ) parser.add_argument("--token_counts" , type=lowerCAmelCase , help="The token counts in the data_file for MLM." ) parser.add_argument( "--restrict_ce_to_mask" , action="store_true" , help="If true, compute the distillation loss only the [MLM] prediction distribution." , ) parser.add_argument( "--freeze_pos_embs" , action="store_true" , help="Freeze positional embeddings during distillation. For student_type in ['roberta', 'gpt2'] only." , ) parser.add_argument( "--freeze_token_type_embds" , action="store_true" , help="Freeze token type embeddings during distillation if existent. For student_type in ['roberta'] only." , ) parser.add_argument("--n_epoch" , type=lowerCAmelCase , default=3 , help="Number of pass on the whole dataset." ) parser.add_argument("--batch_size" , type=lowerCAmelCase , default=5 , help="Batch size (for each process)." ) parser.add_argument( "--group_by_size" , action="store_false" , help="If true, group sequences that have similar length into the same batch. Default is true." , ) parser.add_argument( "--gradient_accumulation_steps" , type=lowerCAmelCase , default=5_0 , help="Gradient accumulation for larger training batches." , ) parser.add_argument("--warmup_prop" , default=0.05 , type=lowerCAmelCase , help="Linear warmup proportion." ) parser.add_argument("--weight_decay" , default=0.0 , type=lowerCAmelCase , help="Weight decay if we apply some." ) parser.add_argument("--learning_rate" , default=5E-4 , type=lowerCAmelCase , help="The initial learning rate for Adam." ) parser.add_argument("--adam_epsilon" , default=1E-6 , type=lowerCAmelCase , help="Epsilon for Adam optimizer." ) parser.add_argument("--max_grad_norm" , default=5.0 , type=lowerCAmelCase , help="Max gradient norm." ) parser.add_argument("--initializer_range" , default=0.02 , type=lowerCAmelCase , help="Random initialization range." ) parser.add_argument( "--fp16" , action="store_true" , help="Whether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bit" , ) parser.add_argument( "--fp16_opt_level" , type=lowerCAmelCase , default="O1" , help=( "For fp16: Apex AMP optimization level selected in ['O0', 'O1', 'O2', and 'O3']." "See details at https://nvidia.github.io/apex/amp.html" ) , ) parser.add_argument("--n_gpu" , type=lowerCAmelCase , default=1 , help="Number of GPUs in the node." ) parser.add_argument("--local_rank" , type=lowerCAmelCase , default=-1 , help="Distributed training - Local rank" ) parser.add_argument("--seed" , type=lowerCAmelCase , default=5_6 , help="Random seed" ) parser.add_argument("--log_interval" , type=lowerCAmelCase , default=5_0_0 , help="Tensorboard logging interval." ) parser.add_argument("--checkpoint_interval" , type=lowerCAmelCase , default=4_0_0_0 , help="Checkpoint interval." ) SCREAMING_SNAKE_CASE_ : Tuple = parser.parse_args() sanity_checks(lowerCAmelCase ) # ARGS # init_gpu_params(lowerCAmelCase ) set_seed(lowerCAmelCase ) if args.is_master: if os.path.exists(args.dump_path ): if not args.force: raise ValueError( f'Serialization dir {args.dump_path} already exists, but you have not precised wheter to overwrite' " itUse `--force` if you want to overwrite it" ) else: shutil.rmtree(args.dump_path ) if not os.path.exists(args.dump_path ): os.makedirs(args.dump_path ) logger.info(f'Experiment will be dumped and logged in {args.dump_path}' ) # SAVE PARAMS # logger.info(f'Param: {args}' ) with open(os.path.join(args.dump_path , "parameters.json" ) , "w" ) as f: json.dump(vars(lowerCAmelCase ) , lowerCAmelCase , indent=4 ) git_log(args.dump_path ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : List[Any] = MODEL_CLASSES[args.student_type] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : str = MODEL_CLASSES[args.teacher_type] # TOKENIZER # SCREAMING_SNAKE_CASE_ : Dict = teacher_tokenizer_class.from_pretrained(args.teacher_name ) SCREAMING_SNAKE_CASE_ : Optional[Any] = {} for tok_name, tok_symbol in tokenizer.special_tokens_map.items(): SCREAMING_SNAKE_CASE_ : List[str] = tokenizer.all_special_tokens.index(lowerCAmelCase ) SCREAMING_SNAKE_CASE_ : Tuple = tokenizer.all_special_ids[idx] logger.info(f'Special tokens {special_tok_ids}' ) SCREAMING_SNAKE_CASE_ : Tuple = special_tok_ids SCREAMING_SNAKE_CASE_ : Tuple = tokenizer.max_model_input_sizes[args.teacher_name] # DATA LOADER # logger.info(f'Loading data from {args.data_file}' ) with open(args.data_file , "rb" ) as fp: SCREAMING_SNAKE_CASE_ : Optional[int] = pickle.load(lowerCAmelCase ) if args.mlm: logger.info(f'Loading token counts from {args.token_counts} (already pre-computed)' ) with open(args.token_counts , "rb" ) as fp: SCREAMING_SNAKE_CASE_ : str = pickle.load(lowerCAmelCase ) SCREAMING_SNAKE_CASE_ : int = np.maximum(lowerCAmelCase , 1 ) ** -args.mlm_smoothing for idx in special_tok_ids.values(): SCREAMING_SNAKE_CASE_ : str = 0.0 # do not predict special tokens SCREAMING_SNAKE_CASE_ : Union[str, Any] = torch.from_numpy(lowerCAmelCase ) else: SCREAMING_SNAKE_CASE_ : Tuple = None SCREAMING_SNAKE_CASE_ : List[str] = LmSeqsDataset(params=lowerCAmelCase , data=lowerCAmelCase ) logger.info("Data loader created." ) # STUDENT # logger.info(f'Loading student config from {args.student_config}' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = student_config_class.from_pretrained(args.student_config ) SCREAMING_SNAKE_CASE_ : List[Any] = True if args.student_pretrained_weights is not None: logger.info(f'Loading pretrained weights from {args.student_pretrained_weights}' ) SCREAMING_SNAKE_CASE_ : Optional[int] = student_model_class.from_pretrained(args.student_pretrained_weights , config=lowerCAmelCase ) else: SCREAMING_SNAKE_CASE_ : List[str] = student_model_class(lowerCAmelCase ) if args.n_gpu > 0: student.to(f'cuda:{args.local_rank}' ) logger.info("Student loaded." ) # TEACHER # SCREAMING_SNAKE_CASE_ : List[Any] = teacher_model_class.from_pretrained(args.teacher_name , output_hidden_states=lowerCAmelCase ) if args.n_gpu > 0: teacher.to(f'cuda:{args.local_rank}' ) logger.info(f'Teacher loaded from {args.teacher_name}.' ) # FREEZING # if args.freeze_pos_embs: freeze_pos_embeddings(lowerCAmelCase , lowerCAmelCase ) if args.freeze_token_type_embds: freeze_token_type_embeddings(lowerCAmelCase , lowerCAmelCase ) # SANITY CHECKS # assert student.config.vocab_size == teacher.config.vocab_size assert student.config.hidden_size == teacher.config.hidden_size assert student.config.max_position_embeddings == teacher.config.max_position_embeddings if args.mlm: assert token_probs.size(0 ) == stu_architecture_config.vocab_size # DISTILLER # torch.cuda.empty_cache() SCREAMING_SNAKE_CASE_ : int = Distiller( params=lowerCAmelCase , dataset=lowerCAmelCase , token_probs=lowerCAmelCase , student=lowerCAmelCase , teacher=lowerCAmelCase ) distiller.train() logger.info("Let's go get some drinks." ) if __name__ == "__main__": main()
18
from dataclasses import dataclass from typing import List, Optional, Union import numpy as np import PIL import torch from transformers import CLIPImageProcessor, CLIPVisionModel from ...models import PriorTransformer from ...pipelines import DiffusionPipeline from ...schedulers import HeunDiscreteScheduler from ...utils import ( BaseOutput, is_accelerate_available, logging, randn_tensor, replace_example_docstring, ) from .renderer import ShapERenderer __A =logging.get_logger(__name__) # pylint: disable=invalid-name __A =''' Examples: ```py >>> from PIL import Image >>> import torch >>> from diffusers import DiffusionPipeline >>> from diffusers.utils import export_to_gif, load_image >>> device = torch.device("cuda" if torch.cuda.is_available() else "cpu") >>> repo = "openai/shap-e-img2img" >>> pipe = DiffusionPipeline.from_pretrained(repo, torch_dtype=torch.float16) >>> pipe = pipe.to(device) >>> guidance_scale = 3.0 >>> image_url = "https://hf.co/datasets/diffusers/docs-images/resolve/main/shap-e/corgi.png" >>> image = load_image(image_url).convert("RGB") >>> images = pipe( ... image, ... guidance_scale=guidance_scale, ... num_inference_steps=64, ... frame_size=256, ... ).images >>> gif_path = export_to_gif(images[0], "corgi_3d.gif") ``` ''' @dataclass class _SCREAMING_SNAKE_CASE ( snake_case_ ): lowerCAmelCase__ = 42 class _SCREAMING_SNAKE_CASE ( snake_case_ ): def __init__( self , lowercase , lowercase , lowercase , lowercase , lowercase , ) -> List[str]: super().__init__() self.register_modules( prior=lowercase , image_encoder=lowercase , image_processor=lowercase , scheduler=lowercase , renderer=lowercase , ) def SCREAMING_SNAKE_CASE_( self , lowercase , lowercase , lowercase , lowercase , lowercase , lowercase ) -> int: if latents is None: lowerCamelCase_ = randn_tensor(lowercase , generator=lowercase , device=lowercase , dtype=lowercase ) else: if latents.shape != shape: raise ValueError(f'Unexpected latents shape, got {latents.shape}, expected {shape}' ) lowerCamelCase_ = latents.to(lowercase ) lowerCamelCase_ = latents * scheduler.init_noise_sigma return latents def SCREAMING_SNAKE_CASE_( self , lowercase=0 ) -> int: if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError("Please install accelerate via `pip install accelerate`" ) lowerCamelCase_ = torch.device(f'cuda:{gpu_id}' ) lowerCamelCase_ = [self.image_encoder, self.prior] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(lowercase , lowercase ) @property def SCREAMING_SNAKE_CASE_( self ) -> List[str]: if self.device != torch.device("meta" ) or not hasattr(self.image_encoder , "_hf_hook" ): return self.device for module in self.image_encoder.modules(): if ( hasattr(lowercase , "_hf_hook" ) and hasattr(module._hf_hook , "execution_device" ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device def SCREAMING_SNAKE_CASE_( self , lowercase , lowercase , lowercase , lowercase , ) -> List[str]: if isinstance(lowercase , lowercase ) and isinstance(image[0] , torch.Tensor ): lowerCamelCase_ = torch.cat(lowercase , axis=0 ) if image[0].ndim == 4 else torch.stack(lowercase , axis=0 ) if not isinstance(lowercase , torch.Tensor ): lowerCamelCase_ = self.image_processor(lowercase , return_tensors="pt" ).pixel_values[0].unsqueeze(0 ) lowerCamelCase_ = image.to(dtype=self.image_encoder.dtype , device=lowercase ) lowerCamelCase_ = self.image_encoder(lowercase )["last_hidden_state"] lowerCamelCase_ = image_embeds[:, 1:, :].contiguous() # batch_size, dim, 256 lowerCamelCase_ = image_embeds.repeat_interleave(lowercase , dim=0 ) if do_classifier_free_guidance: lowerCamelCase_ = torch.zeros_like(lowercase ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes lowerCamelCase_ = torch.cat([negative_image_embeds, image_embeds] ) return image_embeds @torch.no_grad() @replace_example_docstring(lowercase ) def __call__( self , lowercase , lowercase = 1 , lowercase = 25 , lowercase = None , lowercase = None , lowercase = 4.0 , lowercase = 64 , lowercase = "pil" , lowercase = True , ) -> Union[str, Any]: if isinstance(lowercase , PIL.Image.Image ): lowerCamelCase_ = 1 elif isinstance(lowercase , torch.Tensor ): lowerCamelCase_ = image.shape[0] elif isinstance(lowercase , lowercase ) and isinstance(image[0] , (torch.Tensor, PIL.Image.Image) ): lowerCamelCase_ = len(lowercase ) else: raise ValueError( f'`image` has to be of type `PIL.Image.Image`, `torch.Tensor`, `List[PIL.Image.Image]` or `List[torch.Tensor]` but is {type(lowercase )}' ) lowerCamelCase_ = self._execution_device lowerCamelCase_ = batch_size * num_images_per_prompt lowerCamelCase_ = guidance_scale > 1.0 lowerCamelCase_ = self._encode_image(lowercase , lowercase , lowercase , lowercase ) # prior self.scheduler.set_timesteps(lowercase , device=lowercase ) lowerCamelCase_ = self.scheduler.timesteps lowerCamelCase_ = self.prior.config.num_embeddings lowerCamelCase_ = self.prior.config.embedding_dim lowerCamelCase_ = self.prepare_latents( (batch_size, num_embeddings * embedding_dim) , image_embeds.dtype , lowercase , lowercase , lowercase , self.scheduler , ) # YiYi notes: for testing only to match ldm, we can directly create a latents with desired shape: batch_size, num_embeddings, embedding_dim lowerCamelCase_ = latents.reshape(latents.shape[0] , lowercase , lowercase ) for i, t in enumerate(self.progress_bar(lowercase ) ): # expand the latents if we are doing classifier free guidance lowerCamelCase_ = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents lowerCamelCase_ = self.scheduler.scale_model_input(lowercase , lowercase ) lowerCamelCase_ = self.prior( lowercase , timestep=lowercase , proj_embedding=lowercase , ).predicted_image_embedding # remove the variance lowerCamelCase_ , lowerCamelCase_ = noise_pred.split( scaled_model_input.shape[2] , dim=2 ) # batch_size, num_embeddings, embedding_dim if do_classifier_free_guidance is not None: lowerCamelCase_ , lowerCamelCase_ = noise_pred.chunk(2 ) lowerCamelCase_ = noise_pred_uncond + guidance_scale * (noise_pred - noise_pred_uncond) lowerCamelCase_ = self.scheduler.step( lowercase , timestep=lowercase , sample=lowercase , ).prev_sample if output_type == "latent": return ShapEPipelineOutput(images=lowercase ) lowerCamelCase_ = [] for i, latent in enumerate(lowercase ): print() lowerCamelCase_ = self.renderer.decode( latent[None, :] , lowercase , size=lowercase , ray_batch_size=4096 , n_coarse_samples=64 , n_fine_samples=128 , ) images.append(lowercase ) lowerCamelCase_ = torch.stack(lowercase ) if output_type not in ["np", "pil"]: raise ValueError(f'Only the output types `pil` and `np` are supported not output_type={output_type}' ) lowerCamelCase_ = images.cpu().numpy() if output_type == "pil": lowerCamelCase_ = [self.numpy_to_pil(lowercase ) for image in images] # Offload last model to CPU if hasattr(self , "final_offload_hook" ) and self.final_offload_hook is not None: self.final_offload_hook.offload() if not return_dict: return (images,) return ShapEPipelineOutput(images=lowercase )
19
0
import argparse import os import re _SCREAMING_SNAKE_CASE : str = '''src/transformers/models/auto''' # re pattern that matches mapping introductions: # SUPER_MODEL_MAPPING_NAMES = OrderedDict or SUPER_MODEL_MAPPING = OrderedDict _SCREAMING_SNAKE_CASE : str = re.compile(r'''[A-Z_]+_MAPPING(\s+|_[A-Z_]+\s+)=\s+OrderedDict''') # re pattern that matches identifiers in mappings _SCREAMING_SNAKE_CASE : Tuple = re.compile(r'''\s*\(\s*"(\S[^"]+)"''') def UpperCAmelCase_ ( _A , _A = False ): '''simple docstring''' with open(_A , '''r''' , encoding='''utf-8''' ) as f: SCREAMING_SNAKE_CASE__ = f.read() SCREAMING_SNAKE_CASE__ = content.split('''\n''' ) SCREAMING_SNAKE_CASE__ = [] SCREAMING_SNAKE_CASE__ = 0 while line_idx < len(_A ): if _re_intro_mapping.search(lines[line_idx] ) is not None: SCREAMING_SNAKE_CASE__ = len(re.search(R'''^(\s*)\S''' , lines[line_idx] ).groups()[0] ) + 8 # Start of a new mapping! while not lines[line_idx].startswith(''' ''' * indent + '''(''' ): new_lines.append(lines[line_idx] ) line_idx += 1 SCREAMING_SNAKE_CASE__ = [] while lines[line_idx].strip() != "]": # Blocks either fit in one line or not if lines[line_idx].strip() == "(": SCREAMING_SNAKE_CASE__ = line_idx while not lines[line_idx].startswith(''' ''' * indent + ''')''' ): line_idx += 1 blocks.append('''\n'''.join(lines[start_idx : line_idx + 1] ) ) else: blocks.append(lines[line_idx] ) line_idx += 1 # Sort blocks by their identifiers SCREAMING_SNAKE_CASE__ = sorted(_A , key=lambda _A : _re_identifier.search(_A ).groups()[0] ) new_lines += blocks else: new_lines.append(lines[line_idx] ) line_idx += 1 if overwrite: with open(_A , '''w''' , encoding='''utf-8''' ) as f: f.write('''\n'''.join(_A ) ) elif "\n".join(_A ) != content: return True def UpperCAmelCase_ ( _A = False ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = [os.path.join(_A , _A ) for f in os.listdir(_A ) if f.endswith('''.py''' )] SCREAMING_SNAKE_CASE__ = [sort_auto_mapping(_A , overwrite=_A ) for fname in fnames] if not overwrite and any(_A ): SCREAMING_SNAKE_CASE__ = [f for f, d in zip(_A , _A ) if d] raise ValueError( F'''The following files have auto mappings that need sorting: {", ".join(_A )}. Run `make style` to fix''' ''' this.''' ) if __name__ == "__main__": _SCREAMING_SNAKE_CASE : int = argparse.ArgumentParser() parser.add_argument('''--check_only''', action='''store_true''', help='''Whether to only check or fix style.''') _SCREAMING_SNAKE_CASE : Tuple = parser.parse_args() sort_all_auto_mappings(not args.check_only)
218
import datasets from .evaluate import evaluate _SCREAMING_SNAKE_CASE : Union[str, Any] = '''\ @inproceedings{Rajpurkar2016SQuAD10, title={SQuAD: 100, 000+ Questions for Machine Comprehension of Text}, author={Pranav Rajpurkar and Jian Zhang and Konstantin Lopyrev and Percy Liang}, booktitle={EMNLP}, year={2016} } ''' _SCREAMING_SNAKE_CASE : Dict = ''' This metric wrap the official scoring script for version 1 of the Stanford Question Answering Dataset (SQuAD). Stanford Question Answering Dataset (SQuAD) is a reading comprehension dataset, consisting of questions posed by crowdworkers on a set of Wikipedia articles, where the answer to every question is a segment of text, or span, from the corresponding reading passage, or the question might be unanswerable. ''' _SCREAMING_SNAKE_CASE : str = ''' Computes SQuAD scores (F1 and EM). Args: predictions: List of question-answers dictionaries with the following key-values: - \'id\': id of the question-answer pair as given in the references (see below) - \'prediction_text\': the text of the answer references: List of question-answers dictionaries with the following key-values: - \'id\': id of the question-answer pair (see above), - \'answers\': a Dict in the SQuAD dataset format { \'text\': list of possible texts for the answer, as a list of strings \'answer_start\': list of start positions for the answer, as a list of ints } Note that answer_start values are not taken into account to compute the metric. Returns: \'exact_match\': Exact match (the normalized answer exactly match the gold answer) \'f1\': The F-score of predicted tokens versus the gold answer Examples: >>> predictions = [{\'prediction_text\': \'1976\', \'id\': \'56e10a3be3433e1400422b22\'}] >>> references = [{\'answers\': {\'answer_start\': [97], \'text\': [\'1976\']}, \'id\': \'56e10a3be3433e1400422b22\'}] >>> squad_metric = datasets.load_metric("squad") >>> results = squad_metric.compute(predictions=predictions, references=references) >>> print(results) {\'exact_match\': 100.0, \'f1\': 100.0} ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase__ ( datasets.Metric ): """simple docstring""" def lowercase_ ( self : List[Any] ) -> Optional[int]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': {'''id''': datasets.Value('''string''' ), '''prediction_text''': datasets.Value('''string''' )}, '''references''': { '''id''': datasets.Value('''string''' ), '''answers''': datasets.features.Sequence( { '''text''': datasets.Value('''string''' ), '''answer_start''': datasets.Value('''int32''' ), } ), }, } ) , codebase_urls=['''https://rajpurkar.github.io/SQuAD-explorer/'''] , reference_urls=['''https://rajpurkar.github.io/SQuAD-explorer/'''] , ) def lowercase_ ( self : Optional[Any] , __lowerCamelCase : Dict , __lowerCamelCase : List[Any] ) -> Optional[int]: SCREAMING_SNAKE_CASE__ = {prediction['''id''']: prediction['''prediction_text'''] for prediction in predictions} SCREAMING_SNAKE_CASE__ = [ { '''paragraphs''': [ { '''qas''': [ { '''answers''': [{'''text''': answer_text} for answer_text in ref['''answers''']['''text''']], '''id''': ref['''id'''], } for ref in references ] } ] } ] SCREAMING_SNAKE_CASE__ = evaluate(dataset=__lowerCamelCase , predictions=__lowerCamelCase ) return score
218
1
import pickle import unittest import torch from accelerate import Accelerator from accelerate.state import AcceleratorState from accelerate.test_utils import require_cpu @require_cpu class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def lowercase_ ( self ) -> Dict: '''simple docstring''' __lowerCamelCase = torch.nn.Linear(10 , 10 ) __lowerCamelCase = torch.optim.SGD(model.parameters() , 0.1 ) __lowerCamelCase = Accelerator() __lowerCamelCase = accelerator.prepare(lowerCamelCase__ ) try: pickle.loads(pickle.dumps(lowerCamelCase__ ) ) except Exception as e: self.fail(f"""Accelerated optimizer pickling failed with {e}""" ) AcceleratorState._reset_state()
90
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase : Optional[Any] ={ '''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 : Tuple =[ '''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 : str =_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
223
0
'''simple docstring''' import datasets from .evaluate import evaluate _UpperCAmelCase : List[Any] = """\ @article{hendrycks2021cuad, title={CUAD: An Expert-Annotated NLP Dataset for Legal Contract Review}, author={Dan Hendrycks and Collin Burns and Anya Chen and Spencer Ball}, journal={arXiv preprint arXiv:2103.06268}, year={2021} } """ _UpperCAmelCase : Any = """ This metric wrap the official scoring script for version 1 of the Contract Understanding Atticus Dataset (CUAD). Contract Understanding Atticus Dataset (CUAD) v1 is a corpus of more than 13,000 labels in 510 commercial legal contracts that have been manually labeled to identify 41 categories of important clauses that lawyers look for when reviewing contracts in connection with corporate transactions. """ _UpperCAmelCase : int = """ Computes CUAD scores (EM, F1, AUPR, Precision@80%Recall, and Precision@90%Recall). Args: predictions: List of question-answers dictionaries with the following key-values: - 'id': id of the question-answer pair as given in the references (see below) - 'prediction_text': list of possible texts for the answer, as a list of strings depending on a threshold on the confidence probability of each prediction. references: List of question-answers dictionaries with the following key-values: - 'id': id of the question-answer pair (see above), - 'answers': a Dict in the CUAD dataset format { 'text': list of possible texts for the answer, as a list of strings 'answer_start': list of start positions for the answer, as a list of ints } Note that answer_start values are not taken into account to compute the metric. Returns: 'exact_match': Exact match (the normalized answer exactly match the gold answer) 'f1': The F-score of predicted tokens versus the gold answer 'aupr': Area Under the Precision-Recall curve 'prec_at_80_recall': Precision at 80% recall 'prec_at_90_recall': Precision at 90% recall Examples: >>> predictions = [{'prediction_text': ['The seller:', 'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.'], 'id': 'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties'}] >>> references = [{'answers': {'answer_start': [143, 49], 'text': ['The seller:', 'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.']}, 'id': 'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties'}] >>> cuad_metric = datasets.load_metric(\"cuad\") >>> results = cuad_metric.compute(predictions=predictions, references=references) >>> print(results) {'exact_match': 100.0, 'f1': 100.0, 'aupr': 0.0, 'prec_at_80_recall': 1.0, 'prec_at_90_recall': 1.0} """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class a__ ( datasets.Metric ): """simple docstring""" def _snake_case (self ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': { '''id''': datasets.Value('''string''' ), '''prediction_text''': datasets.features.Sequence(datasets.Value('''string''' ) ), }, '''references''': { '''id''': datasets.Value('''string''' ), '''answers''': datasets.features.Sequence( { '''text''': datasets.Value('''string''' ), '''answer_start''': datasets.Value('''int32''' ), } ), }, } ) , codebase_urls=['''https://www.atticusprojectai.org/cuad'''] , reference_urls=['''https://www.atticusprojectai.org/cuad'''] , ) def _snake_case (self , __lowercase , __lowercase ): __lowerCAmelCase = {prediction['''id''']: prediction['''prediction_text'''] for prediction in predictions} __lowerCAmelCase = [ { '''paragraphs''': [ { '''qas''': [ { '''answers''': [{'''text''': answer_text} for answer_text in ref['''answers''']['''text''']], '''id''': ref['''id'''], } for ref in references ] } ] } ] __lowerCAmelCase = evaluate(dataset=__lowercase , predictions=__lowercase ) return score
356
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from transformers.generation import DisjunctiveConstraint @require_torch class a__ ( unittest.TestCase ): """simple docstring""" def _snake_case (self ): # For consistency across different places the DisjunctiveConstraint is called, # dc.token_ids is a list of integers. It is also initialized only by integers. __lowerCAmelCase = [[1, 2, 4], [1, 2, 3, 4]] __lowerCAmelCase = DisjunctiveConstraint(__lowercase ) self.assertTrue(isinstance(dc.token_ids , __lowercase ) ) with self.assertRaises(__lowercase ): DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]] ) ) with self.assertRaises(__lowercase ): DisjunctiveConstraint([torch.LongTensor([1, 2, 4] ), torch.LongTensor([1, 2, 3, 4, 5] )] ) def _snake_case (self ): # We can't have constraints that are complete subsets of another. This leads to a preverse # interpretation of "constraint fulfillment": does generating [1,2,3] fulfill the constraint? # It would mean that it generated [1,2] which fulfills it, but it's in the middle of potentially # fulfilling [1,2,3,4]. If we believe that [1,2,3] does fulfill the constraint, then the algorithm # will necessarily never reach [1,2,3,4], giving users a false sense of control (better to just not allow it). __lowerCAmelCase = [[1, 2], [1, 2, 3, 4]] with self.assertRaises(__lowercase ): DisjunctiveConstraint(__lowercase ) # fails here def _snake_case (self ): __lowerCAmelCase = [[1, 2, 3], [1, 2, 4]] __lowerCAmelCase = DisjunctiveConstraint(__lowercase ) __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = dc.update(1 ) __lowerCAmelCase = stepped is True and completed is False and reset is False self.assertTrue(__lowercase ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1] ) __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = dc.update(2 ) __lowerCAmelCase = stepped is True and completed is False and reset is False self.assertTrue(__lowercase ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2] ) __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = dc.update(3 ) __lowerCAmelCase = stepped is True and completed is True and reset is False self.assertTrue(__lowercase ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.current_seq == [1, 2, 3] ) def _snake_case (self ): __lowerCAmelCase = [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]] __lowerCAmelCase = DisjunctiveConstraint(__lowercase ) __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = dc.update(1 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1] ) __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = dc.update(2 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2] ) __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = dc.update(4 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2, 4] ) __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = dc.update(5 ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.current_seq == [1, 2, 4, 5] ) dc.reset() __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = dc.update(1 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.remaining() == 3 ) self.assertTrue(dc.current_seq == [1] ) __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = dc.update(2 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.remaining() == 2 ) self.assertTrue(dc.current_seq == [1, 2] ) __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = dc.update(5 ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.remaining() == 0 ) self.assertTrue(dc.current_seq == [1, 2, 5] )
9
0
'''simple docstring''' from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import center_crop, normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL __lowerCAmelCase = logging.get_logger(__name__) class _lowerCAmelCase ( __snake_case ): '''simple docstring''' lowerCAmelCase_ = ["pixel_values"] def __init__(self , UpperCAmelCase = True , UpperCAmelCase = None , UpperCAmelCase = PIL.Image.BICUBIC , UpperCAmelCase = True , UpperCAmelCase = None , UpperCAmelCase = 1 / 255 , UpperCAmelCase = True , UpperCAmelCase = True , UpperCAmelCase = None , UpperCAmelCase = None , **UpperCAmelCase , ) -> None: super().__init__(**UpperCAmelCase ) _snake_case = size if size is not None else {"""height""": 256, """width""": 256} _snake_case = get_size_dict(UpperCAmelCase ) _snake_case = crop_size if crop_size is not None else {"""height""": 224, """width""": 224} _snake_case = get_size_dict(UpperCAmelCase , param_name="""crop_size""" ) _snake_case = do_resize _snake_case = size _snake_case = resample _snake_case = do_center_crop _snake_case = crop_size _snake_case = do_rescale _snake_case = rescale_factor _snake_case = do_normalize _snake_case = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN _snake_case = image_std if image_std is not None else IMAGENET_STANDARD_STD def lowercase (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = PIL.Image.BICUBIC , UpperCAmelCase = None , **UpperCAmelCase , ) -> np.ndarray: _snake_case = get_size_dict(UpperCAmelCase ) if "height" not in size or "width" not in size: raise ValueError(f"""The size dictionary must have keys 'height' and 'width'. Got {size.keys()}""" ) return resize( UpperCAmelCase , size=(size["""height"""], size["""width"""]) , resample=UpperCAmelCase , data_format=UpperCAmelCase , **UpperCAmelCase ) def lowercase (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = None , **UpperCAmelCase , ) -> np.ndarray: _snake_case = get_size_dict(UpperCAmelCase ) if "height" not in size or "width" not in size: raise ValueError(f"""The size dictionary must have keys 'height' and 'width'. Got {size.keys()}""" ) return center_crop(UpperCAmelCase , size=(size["""height"""], size["""width"""]) , data_format=UpperCAmelCase , **UpperCAmelCase ) def lowercase (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = None , **UpperCAmelCase , ) -> List[Any]: return rescale(UpperCAmelCase , scale=UpperCAmelCase , data_format=UpperCAmelCase , **UpperCAmelCase ) def lowercase (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = None , **UpperCAmelCase , ) -> np.ndarray: return normalize(UpperCAmelCase , mean=UpperCAmelCase , std=UpperCAmelCase , data_format=UpperCAmelCase , **UpperCAmelCase ) def lowercase (self , UpperCAmelCase , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase=None , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = ChannelDimension.FIRST , **UpperCAmelCase , ) -> PIL.Image.Image: _snake_case = do_resize if do_resize is not None else self.do_resize _snake_case = resample if resample is not None else self.resample _snake_case = do_center_crop if do_center_crop is not None else self.do_center_crop _snake_case = do_rescale if do_rescale is not None else self.do_rescale _snake_case = rescale_factor if rescale_factor is not None else self.rescale_factor _snake_case = do_normalize if do_normalize is not None else self.do_normalize _snake_case = image_mean if image_mean is not None else self.image_mean _snake_case = image_std if image_std is not None else self.image_std _snake_case = size if size is not None else self.size _snake_case = get_size_dict(UpperCAmelCase ) _snake_case = crop_size if crop_size is not None else self.crop_size _snake_case = get_size_dict(UpperCAmelCase , param_name="""crop_size""" ) _snake_case = make_list_of_images(UpperCAmelCase ) if not valid_images(UpperCAmelCase ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None or resample is None: raise ValueError("""Size and resample must be specified if do_resize is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("""Image mean and std must be specified if do_normalize is True.""" ) # All transformations expect numpy arrays. _snake_case = [to_numpy_array(UpperCAmelCase ) for image in images] if do_resize: _snake_case = [self.resize(image=UpperCAmelCase , size=UpperCAmelCase , resample=UpperCAmelCase ) for image in images] if do_center_crop: _snake_case = [self.center_crop(image=UpperCAmelCase , size=UpperCAmelCase ) for image in images] if do_rescale: _snake_case = [self.rescale(image=UpperCAmelCase , scale=UpperCAmelCase ) for image in images] if do_normalize: _snake_case = [self.normalize(image=UpperCAmelCase , mean=UpperCAmelCase , std=UpperCAmelCase ) for image in images] _snake_case = [to_channel_dimension_format(UpperCAmelCase , UpperCAmelCase ) for image in images] _snake_case = {"""pixel_values""": images} return BatchFeature(data=UpperCAmelCase , tensor_type=UpperCAmelCase )
341
'''simple docstring''' import inspect import unittest from huggingface_hub import hf_hub_download from transformers import ConvNextConfig, UperNetConfig from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import UperNetForSemanticSegmentation from transformers.models.upernet.modeling_upernet import UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class _lowerCAmelCase : '''simple docstring''' def __init__(self , UpperCAmelCase , UpperCAmelCase=13 , UpperCAmelCase=32 , UpperCAmelCase=3 , UpperCAmelCase=4 , UpperCAmelCase=[10, 20, 30, 40] , UpperCAmelCase=[2, 2, 3, 2] , UpperCAmelCase=True , UpperCAmelCase=True , UpperCAmelCase=37 , UpperCAmelCase="gelu" , UpperCAmelCase=10 , UpperCAmelCase=0.02 , UpperCAmelCase=["stage2", "stage3", "stage4"] , UpperCAmelCase=3 , UpperCAmelCase=None , ) -> List[Any]: _snake_case = parent _snake_case = batch_size _snake_case = image_size _snake_case = num_channels _snake_case = num_stages _snake_case = hidden_sizes _snake_case = depths _snake_case = is_training _snake_case = use_labels _snake_case = intermediate_size _snake_case = hidden_act _snake_case = type_sequence_label_size _snake_case = initializer_range _snake_case = out_features _snake_case = num_labels _snake_case = scope _snake_case = num_stages def lowercase (self ) -> List[Any]: _snake_case = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _snake_case = None if self.use_labels: _snake_case = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _snake_case = self.get_config() return config, pixel_values, labels def lowercase (self ) -> Tuple: return ConvNextConfig( num_channels=self.num_channels , num_stages=self.num_stages , hidden_sizes=self.hidden_sizes , depths=self.depths , is_training=self.is_training , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , out_features=self.out_features , ) def lowercase (self ) -> Any: return UperNetConfig( backbone_config=self.get_backbone_config() , hidden_size=512 , pool_scales=[1, 2, 3, 6] , use_auxiliary_head=UpperCAmelCase , auxiliary_loss_weight=0.4 , auxiliary_in_channels=40 , auxiliary_channels=256 , auxiliary_num_convs=1 , auxiliary_concat_input=UpperCAmelCase , loss_ignore_index=255 , num_labels=self.num_labels , ) def lowercase (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> str: _snake_case = UperNetForSemanticSegmentation(config=UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() _snake_case = model(UpperCAmelCase ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size, self.image_size) ) def lowercase (self ) -> Tuple: _snake_case = self.prepare_config_and_inputs() ( ( _snake_case ), ( _snake_case ), ( _snake_case ), ) = config_and_inputs _snake_case = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class _lowerCAmelCase ( __snake_case , __snake_case , unittest.TestCase ): '''simple docstring''' lowerCAmelCase_ = (UperNetForSemanticSegmentation,) if is_torch_available() else () lowerCAmelCase_ = {"image-segmentation": UperNetForSemanticSegmentation} if is_torch_available() else {} lowerCAmelCase_ = False lowerCAmelCase_ = False lowerCAmelCase_ = False lowerCAmelCase_ = False lowerCAmelCase_ = False lowerCAmelCase_ = False def lowercase (self ) -> Optional[Any]: _snake_case = UperNetModelTester(self ) _snake_case = ConfigTester(self , config_class=UpperCAmelCase , has_text_modality=UpperCAmelCase , hidden_size=37 ) def lowercase (self ) -> str: self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def lowercase (self ) -> Union[str, Any]: return def lowercase (self ) -> Union[str, Any]: _snake_case, _snake_case = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _snake_case = model_class(UpperCAmelCase ) _snake_case = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _snake_case = [*signature.parameters.keys()] _snake_case = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , UpperCAmelCase ) def lowercase (self ) -> int: _snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*UpperCAmelCase ) @unittest.skip(reason="""UperNet does not use inputs_embeds""" ) def lowercase (self ) -> int: pass @unittest.skip(reason="""UperNet does not support input and output embeddings""" ) def lowercase (self ) -> List[str]: pass @unittest.skip(reason="""UperNet does not have a base model""" ) def lowercase (self ) -> Union[str, Any]: pass @unittest.skip(reason="""UperNet does not have a base model""" ) def lowercase (self ) -> Union[str, Any]: pass @require_torch_multi_gpu @unittest.skip(reason="""UperNet has some layers using `add_module` which doesn't work well with `nn.DataParallel`""" ) def lowercase (self ) -> str: pass @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" ) def lowercase (self ) -> int: pass def lowercase (self ) -> List[str]: def check_hidden_states_output(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ): _snake_case = model_class(UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() with torch.no_grad(): _snake_case = model(**self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) ) _snake_case = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states _snake_case = self.model_tester.num_stages self.assertEqual(len(UpperCAmelCase ) , expected_num_stages + 1 ) # ConvNext's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) _snake_case, _snake_case = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _snake_case = True check_hidden_states_output(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _snake_case = True check_hidden_states_output(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) def lowercase (self ) -> List[str]: _snake_case, _snake_case = self.model_tester.prepare_config_and_inputs_for_common() _snake_case = _config_zero_init(UpperCAmelCase ) _snake_case = _config_zero_init(configs_no_init.backbone_config ) for model_class in self.all_model_classes: _snake_case = model_class(config=UpperCAmelCase ) for name, param in model.named_parameters(): if param.requires_grad: self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=f"""Parameter {name} of model {model_class} seems not properly initialized""" , ) @unittest.skip(reason="""UperNet does not have tied weights""" ) def lowercase (self ) -> Optional[Any]: pass @slow def lowercase (self ) -> Tuple: for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _snake_case = UperNetForSemanticSegmentation.from_pretrained(UpperCAmelCase ) self.assertIsNotNone(UpperCAmelCase ) def __SCREAMING_SNAKE_CASE ( ): _snake_case = hf_hub_download( repo_id="""hf-internal-testing/fixtures_ade20k""" , repo_type="""dataset""" , filename="""ADE_val_00000001.jpg""" ) _snake_case = Image.open(_SCREAMING_SNAKE_CASE ).convert("""RGB""" ) return image @require_torch @require_vision @slow class _lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def lowercase (self ) -> Any: _snake_case = AutoImageProcessor.from_pretrained("""openmmlab/upernet-swin-tiny""" ) _snake_case = UperNetForSemanticSegmentation.from_pretrained("""openmmlab/upernet-swin-tiny""" ).to(UpperCAmelCase ) _snake_case = prepare_img() _snake_case = processor(images=UpperCAmelCase , return_tensors="""pt""" ).to(UpperCAmelCase ) with torch.no_grad(): _snake_case = model(**UpperCAmelCase ) _snake_case = torch.Size((1, model.config.num_labels, 512, 512) ) self.assertEqual(outputs.logits.shape , UpperCAmelCase ) _snake_case = torch.tensor( [[-7.5958, -7.5958, -7.4302], [-7.5958, -7.5958, -7.4302], [-7.4797, -7.4797, -7.3068]] ).to(UpperCAmelCase ) self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3] , UpperCAmelCase , atol=1e-4 ) ) def lowercase (self ) -> Any: _snake_case = AutoImageProcessor.from_pretrained("""openmmlab/upernet-convnext-tiny""" ) _snake_case = UperNetForSemanticSegmentation.from_pretrained("""openmmlab/upernet-convnext-tiny""" ).to(UpperCAmelCase ) _snake_case = prepare_img() _snake_case = processor(images=UpperCAmelCase , return_tensors="""pt""" ).to(UpperCAmelCase ) with torch.no_grad(): _snake_case = model(**UpperCAmelCase ) _snake_case = torch.Size((1, model.config.num_labels, 512, 512) ) self.assertEqual(outputs.logits.shape , UpperCAmelCase ) _snake_case = torch.tensor( [[-8.8110, -8.8110, -8.6521], [-8.8110, -8.8110, -8.6521], [-8.7746, -8.7746, -8.6130]] ).to(UpperCAmelCase ) self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3] , UpperCAmelCase , atol=1e-4 ) )
341
1
"""simple docstring""" import warnings from ...utils import logging from .image_processing_donut import DonutImageProcessor _lowerCAmelCase : Dict = logging.get_logger(__name__) class A_ ( _a ): def __init__( self: str ,*__lowerCAmelCase: Tuple ,**__lowerCAmelCase: int ): '''simple docstring''' warnings.warn( "The class DonutFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use DonutImageProcessor instead." ,lowerCamelCase_ ,) super().__init__(*lowerCamelCase_ ,**lowerCamelCase_ )
361
"""simple docstring""" def lowerCamelCase_( _lowerCamelCase , _lowerCamelCase ) -> float: '''simple docstring''' _validate_point(_lowerCamelCase ) _validate_point(_lowerCamelCase ) if len(_lowerCamelCase ) != len(_lowerCamelCase ): raise ValueError("Both points must be in the same n-dimensional space" ) return float(sum(abs(a - b ) for a, b in zip(_lowerCamelCase , _lowerCamelCase ) ) ) def lowerCamelCase_( _lowerCamelCase ) -> None: '''simple docstring''' if point: if isinstance(_lowerCamelCase , _lowerCamelCase ): for item in point: if not isinstance(_lowerCamelCase , (int, float) ): _lowerCamelCase : Dict = ( "Expected a list of numbers as input, found " F"""{type(_lowerCamelCase ).__name__}""" ) raise TypeError(_lowerCamelCase ) else: _lowerCamelCase : Optional[int] = F"""Expected a list of numbers as input, found {type(_lowerCamelCase ).__name__}""" raise TypeError(_lowerCamelCase ) else: raise ValueError("Missing an input" ) def lowerCamelCase_( _lowerCamelCase , _lowerCamelCase ) -> float: '''simple docstring''' _validate_point(_lowerCamelCase ) _validate_point(_lowerCamelCase ) if len(_lowerCamelCase ) != len(_lowerCamelCase ): raise ValueError("Both points must be in the same n-dimensional space" ) return float(sum(abs(x - y ) for x, y in zip(_lowerCamelCase , _lowerCamelCase ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
340
0
from argparse import ArgumentParser from .env import EnvironmentCommand def UpperCAmelCase__ ( ): '''simple docstring''' a__ =ArgumentParser('''Diffusers CLI tool''' , usage='''diffusers-cli <command> [<args>]''' ) a__ =parser.add_subparsers(help='''diffusers-cli command helpers''' ) # Register commands EnvironmentCommand.register_subcommand(_A ) # Let's go a__ =parser.parse_args() if not hasattr(_A , '''func''' ): parser.print_help() exit(1 ) # Run a__ =args.func(_A ) service.run() if __name__ == "__main__": main()
188
from __future__ import annotations def UpperCAmelCase__ ( _A : float , _A : float , _A : float , ): '''simple docstring''' if (stress, tangential_force, area).count(0 ) != 1: raise ValueError('''You cannot supply more or less than 2 values''' ) elif stress < 0: raise ValueError('''Stress cannot be negative''' ) elif tangential_force < 0: raise ValueError('''Tangential Force cannot be negative''' ) elif area < 0: raise ValueError('''Area cannot be negative''' ) elif stress == 0: return ( "stress", tangential_force / area, ) elif tangential_force == 0: return ( "tangential_force", stress * area, ) else: return ( "area", tangential_force / stress, ) if __name__ == "__main__": import doctest doctest.testmod()
188
1
from jiwer import compute_measures import datasets _snake_case = "\\n@inproceedings{inproceedings,\n author = {Morris, Andrew and Maier, Viktoria and Green, Phil},\n year = {2004},\n month = {01},\n pages = {},\n title = {From WER and RIL to MER and WIL: improved evaluation measures for connected speech recognition.}\n}\n" _snake_case = "\\nWord error rate (WER) is a common metric of the performance of an automatic speech recognition system.\n\nThe general difficulty of measuring performance lies in the fact that the recognized word sequence can have a different length from the reference word sequence (supposedly the correct one). The WER is derived from the Levenshtein distance, working at the word level instead of the phoneme level. The WER is a valuable tool for comparing different systems as well as for evaluating improvements within one system. This kind of measurement, however, provides no details on the nature of translation errors and further work is therefore required to identify the main source(s) of error and to focus any research effort.\n\nThis problem is solved by first aligning the recognized word sequence with the reference (spoken) word sequence using dynamic string alignment. Examination of this issue is seen through a theory called the power law that states the correlation between perplexity and word error rate.\n\nWord error rate can then be computed as:\n\nWER = (S + D + I) / N = (S + D + I) / (S + D + C)\n\nwhere\n\nS is the number of substitutions,\nD is the number of deletions,\nI is the number of insertions,\nC is the number of correct words,\nN is the number of words in the reference (N=S+D+C).\n\nThis value indicates the average number of errors per reference word. The lower the value, the better the\nperformance of the ASR system with a WER of 0 being a perfect score.\n" _snake_case = "\nCompute WER score of transcribed segments against references.\n\nArgs:\n references: List of references for each speech input.\n predictions: List of transcriptions to score.\n concatenate_texts (bool, default=False): Whether to concatenate all input texts or compute WER iteratively.\n\nReturns:\n (float): the word error rate\n\nExamples:\n\n >>> predictions = [\"this is the prediction\", \"there is an other sample\"]\n >>> references = [\"this is the reference\", \"there is another one\"]\n >>> wer = datasets.load_metric(\"wer\")\n >>> wer_score = wer.compute(predictions=predictions, references=references)\n >>> print(wer_score)\n 0.5\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION,_KWARGS_DESCRIPTION ) class lowercase ( datasets.Metric ): def a__ ( self ) -> Tuple: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""string""" , id="""sequence""" ), """references""": datasets.Value("""string""" , id="""sequence""" ), } ) , codebase_urls=["""https://github.com/jitsi/jiwer/"""] , reference_urls=[ """https://en.wikipedia.org/wiki/Word_error_rate""", ] , ) def a__ ( self , _a=None , _a=None , _a=False ) -> Tuple: if concatenate_texts: return compute_measures(_a , _a )["wer"] else: _A : Tuple = 0 _A : int = 0 for prediction, reference in zip(_a , _a ): _A : List[str] = compute_measures(_a , _a ) incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"] total += measures["substitutions"] + measures["deletions"] + measures["hits"] return incorrect / total
354
from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { "MIT/ast-finetuned-audioset-10-10-0.4593": ( "https://huggingface.co/MIT/ast-finetuned-audioset-10-10-0.4593/resolve/main/config.json" ), } class lowercase ( UpperCamelCase__ ): _a = "audio-spectrogram-transformer" def __init__( self , _a=768 , _a=12 , _a=12 , _a=3072 , _a="gelu" , _a=0.0 , _a=0.0 , _a=0.02 , _a=1e-12 , _a=16 , _a=True , _a=10 , _a=10 , _a=1024 , _a=128 , **_a , ) -> List[Any]: super().__init__(**_a ) _A : Any = hidden_size _A : Tuple = num_hidden_layers _A : List[str] = num_attention_heads _A : Any = intermediate_size _A : Optional[Any] = hidden_act _A : Optional[Any] = hidden_dropout_prob _A : Any = attention_probs_dropout_prob _A : Optional[Any] = initializer_range _A : Optional[Any] = layer_norm_eps _A : str = patch_size _A : Tuple = qkv_bias _A : Dict = frequency_stride _A : Union[str, Any] = time_stride _A : Any = max_length _A : Tuple = num_mel_bins
343
0
from collections import defaultdict from math import gcd def __lowercase ( _SCREAMING_SNAKE_CASE = 1_50_00_00 ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = defaultdict(_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE = 2 while 2 * euclid_m * (euclid_m + 1) <= limit: for euclid_n in range((euclid_m % 2) + 1 , _SCREAMING_SNAKE_CASE , 2 ): if gcd(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) > 1: continue SCREAMING_SNAKE_CASE = 2 * euclid_m * (euclid_m + euclid_n) for perimeter in range(_SCREAMING_SNAKE_CASE , limit + 1 , _SCREAMING_SNAKE_CASE ): frequencies[perimeter] += 1 euclid_m += 1 return sum(1 for frequency in frequencies.values() if frequency == 1 ) if __name__ == "__main__": print(F'''{solution() = }''')
296
def __lowercase ( _SCREAMING_SNAKE_CASE = 10 ) -> str: '''simple docstring''' if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) or n < 0: raise ValueError("""Invalid input""" ) SCREAMING_SNAKE_CASE = 10**n SCREAMING_SNAKE_CASE = 2_84_33 * (pow(2 , 7_83_04_57 , _SCREAMING_SNAKE_CASE )) + 1 return str(number % modulus ) if __name__ == "__main__": from doctest import testmod testmod() print(F'''{solution(1_0) = }''')
296
1
"""simple docstring""" from torch import nn class __snake_case ( nn.Module): def __init__( self : Optional[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : str ): """simple docstring""" super().__init__() _lowerCamelCase : List[str] = class_size _lowerCamelCase : List[str] = embed_size # self.mlp1 = nn.Linear(embed_size, embed_size) # self.mlp2 = (nn.Linear(embed_size, class_size)) _lowerCamelCase : Optional[int] = nn.Linear(__lowerCAmelCase , __lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : List[Any] , __lowerCAmelCase : Tuple ): """simple docstring""" _lowerCamelCase : Optional[Any] = self.mlp(__lowerCAmelCase ) return logits
353
"""simple docstring""" import os import tempfile import unittest from transformers import FlaubertConfig, is_torch_available from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( FlaubertForMultipleChoice, FlaubertForQuestionAnswering, FlaubertForQuestionAnsweringSimple, FlaubertForSequenceClassification, FlaubertForTokenClassification, FlaubertModel, FlaubertWithLMHeadModel, ) from transformers.models.flaubert.modeling_flaubert import FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST class __snake_case ( _lowercase): def __init__( self : List[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : List[Any]=1_3 , __lowerCAmelCase : Any=7 , __lowerCAmelCase : Any=True , __lowerCAmelCase : Any=True , __lowerCAmelCase : Union[str, Any]=True , __lowerCAmelCase : List[Any]=True , __lowerCAmelCase : Any=True , __lowerCAmelCase : Union[str, Any]=False , __lowerCAmelCase : List[Any]=False , __lowerCAmelCase : int=False , __lowerCAmelCase : Tuple=2 , __lowerCAmelCase : Dict=9_9 , __lowerCAmelCase : str=0 , __lowerCAmelCase : Optional[Any]=3_2 , __lowerCAmelCase : Tuple=5 , __lowerCAmelCase : Tuple=4 , __lowerCAmelCase : List[str]=0.1 , __lowerCAmelCase : Union[str, Any]=0.1 , __lowerCAmelCase : Optional[int]=5_1_2 , __lowerCAmelCase : Any=1_2 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : Any=0.02 , __lowerCAmelCase : str=3 , __lowerCAmelCase : Optional[int]=4 , __lowerCAmelCase : Optional[int]="last" , __lowerCAmelCase : str=None , __lowerCAmelCase : int=None , ): """simple docstring""" _lowerCamelCase : Dict = parent _lowerCamelCase : List[str] = batch_size _lowerCamelCase : Dict = seq_length _lowerCamelCase : List[Any] = is_training _lowerCamelCase : Dict = use_input_lengths _lowerCamelCase : Tuple = use_token_type_ids _lowerCamelCase : Any = use_labels _lowerCamelCase : Optional[Any] = gelu_activation _lowerCamelCase : Optional[Any] = sinusoidal_embeddings _lowerCamelCase : Dict = causal _lowerCamelCase : Dict = asm _lowerCamelCase : str = n_langs _lowerCamelCase : str = vocab_size _lowerCamelCase : Optional[int] = n_special _lowerCamelCase : Dict = hidden_size _lowerCamelCase : int = num_hidden_layers _lowerCamelCase : str = num_attention_heads _lowerCamelCase : Dict = hidden_dropout_prob _lowerCamelCase : int = attention_probs_dropout_prob _lowerCamelCase : Any = max_position_embeddings _lowerCamelCase : Any = type_vocab_size _lowerCamelCase : Optional[int] = type_sequence_label_size _lowerCamelCase : List[str] = initializer_range _lowerCamelCase : List[Any] = num_labels _lowerCamelCase : Dict = num_choices _lowerCamelCase : str = summary_type _lowerCamelCase : List[str] = use_proj _lowerCamelCase : int = scope def SCREAMING_SNAKE_CASE ( self : Tuple ): """simple docstring""" _lowerCamelCase : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _lowerCamelCase : Optional[Any] = random_attention_mask([self.batch_size, self.seq_length] ) _lowerCamelCase : Optional[int] = None if self.use_input_lengths: _lowerCamelCase : int = ( ids_tensor([self.batch_size] , vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length _lowerCamelCase : Union[str, Any] = None if self.use_token_type_ids: _lowerCamelCase : str = ids_tensor([self.batch_size, self.seq_length] , self.n_langs ) _lowerCamelCase : Union[str, Any] = None _lowerCamelCase : List[str] = None _lowerCamelCase : Optional[Any] = None if self.use_labels: _lowerCamelCase : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _lowerCamelCase : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _lowerCamelCase : str = ids_tensor([self.batch_size] , 2 ).float() _lowerCamelCase : Optional[int] = ids_tensor([self.batch_size] , self.num_choices ) _lowerCamelCase : Tuple = self.get_config() return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ): """simple docstring""" return FlaubertConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , ) def SCREAMING_SNAKE_CASE ( self : str , __lowerCAmelCase : List[str] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : str , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Tuple , __lowerCAmelCase : Any , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : List[str] , __lowerCAmelCase : Tuple , ): """simple docstring""" _lowerCamelCase : Optional[Any] = FlaubertModel(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() _lowerCamelCase : Union[str, Any] = model(__lowerCAmelCase , lengths=__lowerCAmelCase , langs=__lowerCAmelCase ) _lowerCamelCase : str = model(__lowerCAmelCase , langs=__lowerCAmelCase ) _lowerCamelCase : List[str] = model(__lowerCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE ( self : Dict , __lowerCAmelCase : str , __lowerCAmelCase : Dict , __lowerCAmelCase : Dict , __lowerCAmelCase : List[str] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Tuple , __lowerCAmelCase : int , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : str , ): """simple docstring""" _lowerCamelCase : Tuple = FlaubertWithLMHeadModel(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() _lowerCamelCase : str = model(__lowerCAmelCase , token_type_ids=__lowerCAmelCase , labels=__lowerCAmelCase ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE ( self : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : Tuple , __lowerCAmelCase : Any , __lowerCAmelCase : Any , __lowerCAmelCase : str , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Dict , __lowerCAmelCase : Union[str, Any] , ): """simple docstring""" _lowerCamelCase : Union[str, Any] = FlaubertForQuestionAnsweringSimple(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() _lowerCamelCase : List[str] = model(__lowerCAmelCase ) _lowerCamelCase : Union[str, Any] = model(__lowerCAmelCase , start_positions=__lowerCAmelCase , end_positions=__lowerCAmelCase ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Any , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : int , ): """simple docstring""" _lowerCamelCase : str = FlaubertForQuestionAnswering(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() _lowerCamelCase : Optional[Any] = model(__lowerCAmelCase ) _lowerCamelCase : Optional[Any] = model( __lowerCAmelCase , start_positions=__lowerCAmelCase , end_positions=__lowerCAmelCase , cls_index=__lowerCAmelCase , is_impossible=__lowerCAmelCase , p_mask=__lowerCAmelCase , ) _lowerCamelCase : List[str] = model( __lowerCAmelCase , start_positions=__lowerCAmelCase , end_positions=__lowerCAmelCase , cls_index=__lowerCAmelCase , is_impossible=__lowerCAmelCase , ) ((_lowerCamelCase) , ) : str = result_with_labels.to_tuple() _lowerCamelCase : Optional[Any] = model(__lowerCAmelCase , start_positions=__lowerCAmelCase , end_positions=__lowerCAmelCase ) ((_lowerCamelCase) , ) : Union[str, Any] = result_with_labels.to_tuple() self.parent.assertEqual(result_with_labels.loss.shape , () ) self.parent.assertEqual(result.start_top_log_probs.shape , (self.batch_size, model.config.start_n_top) ) self.parent.assertEqual(result.start_top_index.shape , (self.batch_size, model.config.start_n_top) ) self.parent.assertEqual( result.end_top_log_probs.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual( result.end_top_index.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual(result.cls_logits.shape , (self.batch_size,) ) def SCREAMING_SNAKE_CASE ( self : int , __lowerCAmelCase : str , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : int , __lowerCAmelCase : List[str] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Any , __lowerCAmelCase : Dict , __lowerCAmelCase : str , ): """simple docstring""" _lowerCamelCase : Dict = FlaubertForSequenceClassification(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() _lowerCamelCase : str = model(__lowerCAmelCase ) _lowerCamelCase : Tuple = model(__lowerCAmelCase , labels=__lowerCAmelCase ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __lowerCAmelCase : Any , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : List[str] , __lowerCAmelCase : List[str] , __lowerCAmelCase : Dict , __lowerCAmelCase : List[str] , __lowerCAmelCase : Tuple , __lowerCAmelCase : Union[str, Any] , ): """simple docstring""" _lowerCamelCase : Any = self.num_labels _lowerCamelCase : List[str] = FlaubertForTokenClassification(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() _lowerCamelCase : Union[str, Any] = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase , labels=__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __lowerCAmelCase : Any , __lowerCAmelCase : List[str] , __lowerCAmelCase : Dict , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Dict , __lowerCAmelCase : Optional[Any] , ): """simple docstring""" _lowerCamelCase : List[str] = self.num_choices _lowerCamelCase : Any = FlaubertForMultipleChoice(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() _lowerCamelCase : List[str] = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _lowerCamelCase : List[str] = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _lowerCamelCase : List[Any] = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _lowerCamelCase : int = model( __lowerCAmelCase , attention_mask=__lowerCAmelCase , token_type_ids=__lowerCAmelCase , labels=__lowerCAmelCase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ): """simple docstring""" _lowerCamelCase : Any = self.prepare_config_and_inputs() ( ( _lowerCamelCase ) , ( _lowerCamelCase ) , ( _lowerCamelCase ) , ( _lowerCamelCase ) , ( _lowerCamelCase ) , ( _lowerCamelCase ) , ( _lowerCamelCase ) , ( _lowerCamelCase ) , ( _lowerCamelCase ) , ) : Optional[Any] = config_and_inputs _lowerCamelCase : int = { '''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''lengths''': input_lengths, '''attention_mask''': input_mask, } return config, inputs_dict @require_torch class __snake_case ( _lowercase , _lowercase , unittest.TestCase): snake_case__ : List[str] = ( ( FlaubertModel, FlaubertWithLMHeadModel, FlaubertForQuestionAnswering, FlaubertForQuestionAnsweringSimple, FlaubertForSequenceClassification, FlaubertForTokenClassification, FlaubertForMultipleChoice, ) if is_torch_available() else () ) snake_case__ : List[Any] = ( { "feature-extraction": FlaubertModel, "fill-mask": FlaubertWithLMHeadModel, "question-answering": FlaubertForQuestionAnsweringSimple, "text-classification": FlaubertForSequenceClassification, "token-classification": FlaubertForTokenClassification, "zero-shot": FlaubertForSequenceClassification, } if is_torch_available() else {} ) def SCREAMING_SNAKE_CASE ( self : str , __lowerCAmelCase : List[str] , __lowerCAmelCase : int , __lowerCAmelCase : Dict , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Tuple ): """simple docstring""" if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith('''Fast''' ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def SCREAMING_SNAKE_CASE ( self : Dict , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : Optional[int]=False ): """simple docstring""" _lowerCamelCase : Dict = super()._prepare_for_class(__lowerCAmelCase , __lowerCAmelCase , return_labels=__lowerCAmelCase ) if return_labels: if model_class.__name__ == "FlaubertForQuestionAnswering": _lowerCamelCase : Dict = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=__lowerCAmelCase ) _lowerCamelCase : Any = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=__lowerCAmelCase ) return inputs_dict def SCREAMING_SNAKE_CASE ( self : List[Any] ): """simple docstring""" _lowerCamelCase : int = FlaubertModelTester(self ) _lowerCamelCase : str = ConfigTester(self , config_class=__lowerCAmelCase , emb_dim=3_7 ) def SCREAMING_SNAKE_CASE ( self : List[Any] ): """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE ( self : int ): """simple docstring""" _lowerCamelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : List[str] ): """simple docstring""" _lowerCamelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : Tuple ): """simple docstring""" _lowerCamelCase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_simple_qa(*__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : Tuple ): """simple docstring""" _lowerCamelCase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ): """simple docstring""" _lowerCamelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : Dict ): """simple docstring""" _lowerCamelCase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_token_classif(*__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : List[Any] ): """simple docstring""" _lowerCamelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_multiple_choice(*__lowerCAmelCase ) @slow def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ): """simple docstring""" for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _lowerCamelCase : Dict = FlaubertModel.from_pretrained(__lowerCAmelCase ) self.assertIsNotNone(__lowerCAmelCase ) @slow @require_torch_gpu def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ): """simple docstring""" _lowerCamelCase , _lowerCamelCase : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: # FlauBertForMultipleChoice behaves incorrectly in JIT environments. if model_class == FlaubertForMultipleChoice: return _lowerCamelCase : Any = True _lowerCamelCase : int = model_class(config=__lowerCAmelCase ) _lowerCamelCase : List[str] = self._prepare_for_class(__lowerCAmelCase , __lowerCAmelCase ) _lowerCamelCase : int = torch.jit.trace( __lowerCAmelCase , (inputs_dict['''input_ids'''].to('''cpu''' ), inputs_dict['''attention_mask'''].to('''cpu''' )) ) with tempfile.TemporaryDirectory() as tmp: torch.jit.save(__lowerCAmelCase , os.path.join(__lowerCAmelCase , '''traced_model.pt''' ) ) _lowerCamelCase : Union[str, Any] = torch.jit.load(os.path.join(__lowerCAmelCase , '''traced_model.pt''' ) , map_location=__lowerCAmelCase ) loaded(inputs_dict['''input_ids'''].to(__lowerCAmelCase ) , inputs_dict['''attention_mask'''].to(__lowerCAmelCase ) ) @require_torch class __snake_case ( unittest.TestCase): @slow def SCREAMING_SNAKE_CASE ( self : Optional[int] ): """simple docstring""" _lowerCamelCase : Union[str, Any] = FlaubertModel.from_pretrained('''flaubert/flaubert_base_cased''' ) _lowerCamelCase : Any = torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]] ) with torch.no_grad(): _lowerCamelCase : Any = model(__lowerCAmelCase )[0] _lowerCamelCase : Optional[Any] = torch.Size((1, 1_1, 7_6_8) ) self.assertEqual(output.shape , __lowerCAmelCase ) _lowerCamelCase : Optional[int] = torch.tensor( [[[-2.62_51, -1.42_98, -0.02_27], [-2.85_10, -1.63_87, 0.22_58], [-2.81_14, -1.18_32, -0.30_66]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , __lowerCAmelCase , atol=1E-4 ) )
175
0
'''simple docstring''' import argparse import json from pathlib import Path import torch import torchaudio from datasets import load_dataset from huggingface_hub import hf_hub_download from transformers import ASTConfig, ASTFeatureExtractor, ASTForAudioClassification from transformers.utils import logging logging.set_verbosity_info() a : Optional[int] = logging.get_logger(__name__) def __lowerCamelCase ( _lowercase ) -> int: UpperCAmelCase : List[Any] = ASTConfig() if "10-10" in model_name: pass elif "speech-commands" in model_name: UpperCAmelCase : List[str] = 1_2_8 elif "12-12" in model_name: UpperCAmelCase : Union[str, Any] = 1_2 UpperCAmelCase : Optional[int] = 1_2 elif "14-14" in model_name: UpperCAmelCase : Any = 1_4 UpperCAmelCase : Any = 1_4 elif "16-16" in model_name: UpperCAmelCase : Tuple = 1_6 UpperCAmelCase : Dict = 1_6 else: raise ValueError("""Model not supported""" ) UpperCAmelCase : Dict = """huggingface/label-files""" if "speech-commands" in model_name: UpperCAmelCase : List[str] = 3_5 UpperCAmelCase : Optional[Any] = """speech-commands-v2-id2label.json""" else: UpperCAmelCase : List[str] = 5_2_7 UpperCAmelCase : Tuple = """audioset-id2label.json""" UpperCAmelCase : Union[str, Any] = json.load(open(hf_hub_download(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , repo_type="""dataset""" ) , """r""" ) ) UpperCAmelCase : str = {int(_SCREAMING_SNAKE_CASE ): v for k, v in idalabel.items()} UpperCAmelCase : Union[str, Any] = idalabel UpperCAmelCase : Dict = {v: k for k, v in idalabel.items()} return config def __lowerCamelCase ( _lowercase ) -> str: if "module.v" in name: UpperCAmelCase : int = name.replace("""module.v""" , """audio_spectrogram_transformer""" ) if "cls_token" in name: UpperCAmelCase : str = name.replace("""cls_token""" , """embeddings.cls_token""" ) if "dist_token" in name: UpperCAmelCase : str = name.replace("""dist_token""" , """embeddings.distillation_token""" ) if "pos_embed" in name: UpperCAmelCase : List[Any] = name.replace("""pos_embed""" , """embeddings.position_embeddings""" ) if "patch_embed.proj" in name: UpperCAmelCase : List[Any] = name.replace("""patch_embed.proj""" , """embeddings.patch_embeddings.projection""" ) # transformer blocks if "blocks" in name: UpperCAmelCase : Tuple = name.replace("""blocks""" , """encoder.layer""" ) if "attn.proj" in name: UpperCAmelCase : Optional[int] = name.replace("""attn.proj""" , """attention.output.dense""" ) if "attn" in name: UpperCAmelCase : Dict = name.replace("""attn""" , """attention.self""" ) if "norm1" in name: UpperCAmelCase : Tuple = name.replace("""norm1""" , """layernorm_before""" ) if "norm2" in name: UpperCAmelCase : Tuple = name.replace("""norm2""" , """layernorm_after""" ) if "mlp.fc1" in name: UpperCAmelCase : List[str] = name.replace("""mlp.fc1""" , """intermediate.dense""" ) if "mlp.fc2" in name: UpperCAmelCase : Optional[int] = name.replace("""mlp.fc2""" , """output.dense""" ) # final layernorm if "audio_spectrogram_transformer.norm" in name: UpperCAmelCase : Union[str, Any] = name.replace("""audio_spectrogram_transformer.norm""" , """audio_spectrogram_transformer.layernorm""" ) # classifier head if "module.mlp_head.0" in name: UpperCAmelCase : Dict = name.replace("""module.mlp_head.0""" , """classifier.layernorm""" ) if "module.mlp_head.1" in name: UpperCAmelCase : Dict = name.replace("""module.mlp_head.1""" , """classifier.dense""" ) return name def __lowerCamelCase ( _lowercase , _lowercase ) -> str: for key in orig_state_dict.copy().keys(): UpperCAmelCase : Optional[Any] = orig_state_dict.pop(_SCREAMING_SNAKE_CASE ) if "qkv" in key: UpperCAmelCase : int = key.split(""".""" ) UpperCAmelCase : Tuple = int(key_split[3] ) UpperCAmelCase : int = config.hidden_size if "weight" in key: UpperCAmelCase : List[Any] = val[:dim, :] UpperCAmelCase : Dict = val[dim : dim * 2, :] UpperCAmelCase : List[Any] = val[-dim:, :] else: UpperCAmelCase : Tuple = val[:dim] UpperCAmelCase : Optional[Any] = val[dim : dim * 2] UpperCAmelCase : int = val[-dim:] else: UpperCAmelCase : Optional[int] = val return orig_state_dict def __lowerCamelCase ( _lowercase ) -> Tuple: UpperCAmelCase : Optional[Any] = [ """module.v.head.weight""", """module.v.head.bias""", """module.v.head_dist.weight""", """module.v.head_dist.bias""", ] for k in ignore_keys: state_dict.pop(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) @torch.no_grad() def __lowerCamelCase ( _lowercase , _lowercase , _lowercase=False ) -> Union[str, Any]: UpperCAmelCase : Union[str, Any] = get_audio_spectrogram_transformer_config(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : str = { """ast-finetuned-audioset-10-10-0.4593""": ( """https://www.dropbox.com/s/ca0b1v2nlxzyeb4/audioset_10_10_0.4593.pth?dl=1""" ), """ast-finetuned-audioset-10-10-0.450""": ( """https://www.dropbox.com/s/1tv0hovue1bxupk/audioset_10_10_0.4495.pth?dl=1""" ), """ast-finetuned-audioset-10-10-0.448""": ( """https://www.dropbox.com/s/6u5sikl4b9wo4u5/audioset_10_10_0.4483.pth?dl=1""" ), """ast-finetuned-audioset-10-10-0.448-v2""": ( """https://www.dropbox.com/s/kt6i0v9fvfm1mbq/audioset_10_10_0.4475.pth?dl=1""" ), """ast-finetuned-audioset-12-12-0.447""": ( """https://www.dropbox.com/s/snfhx3tizr4nuc8/audioset_12_12_0.4467.pth?dl=1""" ), """ast-finetuned-audioset-14-14-0.443""": ( """https://www.dropbox.com/s/z18s6pemtnxm4k7/audioset_14_14_0.4431.pth?dl=1""" ), """ast-finetuned-audioset-16-16-0.442""": ( """https://www.dropbox.com/s/mdsa4t1xmcimia6/audioset_16_16_0.4422.pth?dl=1""" ), """ast-finetuned-speech-commands-v2""": ( """https://www.dropbox.com/s/q0tbqpwv44pquwy/speechcommands_10_10_0.9812.pth?dl=1""" ), } # load original state_dict UpperCAmelCase : Any = model_name_to_url[model_name] UpperCAmelCase : Optional[int] = torch.hub.load_state_dict_from_url(_SCREAMING_SNAKE_CASE , map_location="""cpu""" ) # remove some keys remove_keys(_SCREAMING_SNAKE_CASE ) # rename some keys UpperCAmelCase : Union[str, Any] = convert_state_dict(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # load 🤗 model UpperCAmelCase : str = ASTForAudioClassification(_SCREAMING_SNAKE_CASE ) model.eval() model.load_state_dict(_SCREAMING_SNAKE_CASE ) # verify outputs on dummy input # source: https://github.com/YuanGongND/ast/blob/79e873b8a54d0a3b330dd522584ff2b9926cd581/src/run.py#L62 UpperCAmelCase : Union[str, Any] = -4.267_7393 if """speech-commands""" not in model_name else -6.84_5978 UpperCAmelCase : Dict = 4.568_9974 if """speech-commands""" not in model_name else 5.565_4526 UpperCAmelCase : List[Any] = 1_0_2_4 if """speech-commands""" not in model_name else 1_2_8 UpperCAmelCase : Union[str, Any] = ASTFeatureExtractor(mean=_SCREAMING_SNAKE_CASE , std=_SCREAMING_SNAKE_CASE , max_length=_SCREAMING_SNAKE_CASE ) if "speech-commands" in model_name: UpperCAmelCase : Dict = load_dataset("""speech_commands""" , """v0.02""" , split="""validation""" ) UpperCAmelCase : List[Any] = dataset[0]["""audio"""]["""array"""] else: UpperCAmelCase : Tuple = hf_hub_download( repo_id="""nielsr/audio-spectogram-transformer-checkpoint""" , filename="""sample_audio.flac""" , repo_type="""dataset""" , ) UpperCAmelCase , UpperCAmelCase : Dict = torchaudio.load(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : Tuple = waveform.squeeze().numpy() UpperCAmelCase : Optional[int] = feature_extractor(_SCREAMING_SNAKE_CASE , sampling_rate=1_6_0_0_0 , return_tensors="""pt""" ) # forward pass UpperCAmelCase : Optional[int] = model(**_SCREAMING_SNAKE_CASE ) UpperCAmelCase : str = outputs.logits if model_name == "ast-finetuned-audioset-10-10-0.4593": UpperCAmelCase : Any = torch.tensor([-0.8760, -7.0042, -8.6602] ) elif model_name == "ast-finetuned-audioset-10-10-0.450": UpperCAmelCase : List[Any] = torch.tensor([-1.1986, -7.0903, -8.2718] ) elif model_name == "ast-finetuned-audioset-10-10-0.448": UpperCAmelCase : List[str] = torch.tensor([-2.6128, -8.0080, -9.4344] ) elif model_name == "ast-finetuned-audioset-10-10-0.448-v2": UpperCAmelCase : Union[str, Any] = torch.tensor([-1.5080, -7.4534, -8.8917] ) elif model_name == "ast-finetuned-audioset-12-12-0.447": UpperCAmelCase : Tuple = torch.tensor([-0.5050, -6.5833, -8.0843] ) elif model_name == "ast-finetuned-audioset-14-14-0.443": UpperCAmelCase : Dict = torch.tensor([-0.3826, -7.0336, -8.2413] ) elif model_name == "ast-finetuned-audioset-16-16-0.442": UpperCAmelCase : List[str] = torch.tensor([-1.2113, -6.9101, -8.3470] ) elif model_name == "ast-finetuned-speech-commands-v2": UpperCAmelCase : int = torch.tensor([6.1589, -8.0566, -8.7984] ) else: raise ValueError("""Unknown model name""" ) if not torch.allclose(logits[0, :3] , _SCREAMING_SNAKE_CASE , atol=1e-4 ): raise ValueError("""Logits don't match""" ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: Path(_SCREAMING_SNAKE_CASE ).mkdir(exist_ok=_SCREAMING_SNAKE_CASE ) print(F'''Saving model {model_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(_SCREAMING_SNAKE_CASE ) print(F'''Saving feature extractor to {pytorch_dump_folder_path}''' ) feature_extractor.save_pretrained(_SCREAMING_SNAKE_CASE ) if push_to_hub: print("""Pushing model and feature extractor to the hub...""" ) model.push_to_hub(F'''MIT/{model_name}''' ) feature_extractor.push_to_hub(F'''MIT/{model_name}''' ) if __name__ == "__main__": a : str = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default="""ast-finetuned-audioset-10-10-0.4593""", type=str, help="""Name of the Audio Spectrogram Transformer model you\'d like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether or not to push the converted model to the 🤗 hub.""" ) a : int = parser.parse_args() convert_audio_spectrogram_transformer_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
265
"""simple docstring""" import logging import os from typing import Dict, List, Optional, Union import torch import torch.nn as nn from accelerate.utils.imports import ( is_abit_bnb_available, is_abit_bnb_available, is_bnb_available, ) from ..big_modeling import dispatch_model, init_empty_weights from .dataclasses import BnbQuantizationConfig from .modeling import ( find_tied_parameters, get_balanced_memory, infer_auto_device_map, load_checkpoint_in_model, offload_weight, set_module_tensor_to_device, ) if is_bnb_available(): import bitsandbytes as bnb from copy import deepcopy __SCREAMING_SNAKE_CASE : Any = logging.getLogger(__name__) def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = False , ) -> Optional[Any]: snake_case_ = bnb_quantization_config.load_in_abit snake_case_ = bnb_quantization_config.load_in_abit if load_in_abit and not is_abit_bnb_available(): raise ImportError( """You have a version of `bitsandbytes` that is not compatible with 8bit quantization,""" """ make sure you have the latest version of `bitsandbytes` installed.""" ) if load_in_abit and not is_abit_bnb_available(): raise ValueError( """You have a version of `bitsandbytes` that is not compatible with 4bit quantization,""" """make sure you have the latest version of `bitsandbytes` installed.""" ) snake_case_ = [] # custom device map if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) and len(device_map.keys() ) > 1: snake_case_ = [key for key, value in device_map.items() if value in ["""disk""", """cpu"""]] # We keep some modules such as the lm_head in their original dtype for numerical stability reasons if bnb_quantization_config.skip_modules is None: snake_case_ = get_keys_to_not_convert(_SCREAMING_SNAKE_CASE ) # add cpu modules to skip modules only for 4-bit modules if load_in_abit: bnb_quantization_config.skip_modules.extend(_SCREAMING_SNAKE_CASE ) snake_case_ = bnb_quantization_config.skip_modules # We add the modules we want to keep in full precision if bnb_quantization_config.keep_in_fpaa_modules is None: snake_case_ = [] snake_case_ = bnb_quantization_config.keep_in_fpaa_modules modules_to_not_convert.extend(_SCREAMING_SNAKE_CASE ) # compatibility with peft snake_case_ = load_in_abit snake_case_ = load_in_abit snake_case_ = get_parameter_device(_SCREAMING_SNAKE_CASE ) if model_device.type != "meta": # quantization of an already loaded model logger.warning( """It is not recommended to quantize a loaded model. """ """The model should be instantiated under the `init_empty_weights` context manager.""" ) snake_case_ = replace_with_bnb_layers(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , modules_to_not_convert=_SCREAMING_SNAKE_CASE ) # convert param to the right dtype snake_case_ = bnb_quantization_config.torch_dtype for name, param in model.state_dict().items(): if any(module_to_keep_in_fpaa in name for module_to_keep_in_fpaa in keep_in_fpaa_modules ): param.to(torch.floataa ) if param.dtype != torch.floataa: snake_case_ = name.replace(""".weight""" , """""" ).replace(""".bias""" , """""" ) snake_case_ = getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if param is not None: param.to(torch.floataa ) elif torch.is_floating_point(_SCREAMING_SNAKE_CASE ): param.to(_SCREAMING_SNAKE_CASE ) if model_device.type == "cuda": # move everything to cpu in the first place because we can't do quantization if the weights are already on cuda model.cuda(torch.cuda.current_device() ) torch.cuda.empty_cache() elif torch.cuda.is_available(): model.to(torch.cuda.current_device() ) else: raise RuntimeError("""No GPU found. A GPU is needed for quantization.""" ) logger.info( f"""The model device type is {model_device.type}. However, cuda is needed for quantization.""" """We move the model to cuda.""" ) return model elif weights_location is None: raise RuntimeError( f"""`weights_location` needs to be the folder path containing the weights of the model, but we found {weights_location} """ ) else: with init_empty_weights(): snake_case_ = replace_with_bnb_layers( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , modules_to_not_convert=_SCREAMING_SNAKE_CASE ) snake_case_ = get_quantized_model_device_map( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , max_memory=_SCREAMING_SNAKE_CASE , no_split_module_classes=_SCREAMING_SNAKE_CASE , ) if offload_state_dict is None and device_map is not None and "disk" in device_map.values(): snake_case_ = True snake_case_ = any(x in list(device_map.values() ) for x in ["""cpu""", """disk"""] ) load_checkpoint_in_model( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , dtype=bnb_quantization_config.torch_dtype , offload_folder=_SCREAMING_SNAKE_CASE , offload_state_dict=_SCREAMING_SNAKE_CASE , keep_in_fpaa_modules=bnb_quantization_config.keep_in_fpaa_modules , offload_abit_bnb=load_in_abit and offload , ) return dispatch_model(_SCREAMING_SNAKE_CASE , device_map=_SCREAMING_SNAKE_CASE , offload_dir=_SCREAMING_SNAKE_CASE ) def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None ) -> Tuple: if device_map is None: if torch.cuda.is_available(): snake_case_ = {"""""": torch.cuda.current_device()} else: raise RuntimeError("""No GPU found. A GPU is needed for quantization.""" ) logger.info("""The device_map was not initialized.""" """Setting device_map to `{'':torch.cuda.current_device()}`.""" ) if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): if device_map not in ["auto", "balanced", "balanced_low_0", "sequential"]: raise ValueError( """If passing a string for `device_map`, please choose 'auto', 'balanced', 'balanced_low_0' or """ """'sequential'.""" ) snake_case_ = {} special_dtypes.update( { name: bnb_quantization_config.torch_dtype for name, _ in model.named_parameters() if any(m in name for m in bnb_quantization_config.skip_modules ) } ) special_dtypes.update( { name: torch.floataa for name, _ in model.named_parameters() if any(m in name for m in bnb_quantization_config.keep_in_fpaa_modules ) } ) snake_case_ = {} snake_case_ = special_dtypes snake_case_ = no_split_module_classes snake_case_ = bnb_quantization_config.target_dtype # get max_memory for each device. if device_map != "sequential": snake_case_ = get_balanced_memory( _SCREAMING_SNAKE_CASE , low_zero=(device_map == """balanced_low_0""") , max_memory=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) snake_case_ = max_memory snake_case_ = infer_auto_device_map(_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): # check if don't have any quantized module on the cpu snake_case_ = bnb_quantization_config.skip_modules + bnb_quantization_config.keep_in_fpaa_modules snake_case_ = { key: device_map[key] for key in device_map.keys() if key not in modules_not_to_convert } for device in ["cpu", "disk"]: if device in device_map_without_some_modules.values(): if bnb_quantization_config.load_in_abit: raise ValueError( """ Some modules are dispatched on the CPU or the disk. Make sure you have enough GPU RAM to fit the quantized model. If you want to dispatch the model on the CPU or the disk while keeping these modules in `torch_dtype`, you need to pass a custom `device_map` to `load_and_quantize_model`. Check https://huggingface.co/docs/accelerate/main/en/usage_guides/quantization#offload-modules-to-cpu-and-disk for more details. """ ) else: logger.info( """Some modules are are offloaded to the CPU or the disk. Note that these modules will be converted to 8-bit""" ) del device_map_without_some_modules return device_map def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None ) -> Tuple: if modules_to_not_convert is None: snake_case_ = [] snake_case_ , snake_case_ = _replace_with_bnb_layers( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if not has_been_replaced: logger.warning( """You are loading your model in 8bit or 4bit but no linear modules were found in your model.""" """ this can happen for some architectures such as gpt2 that uses Conv1D instead of Linear layers.""" """ Please double check your model architecture, or submit an issue on github if you think this is""" """ a bug.""" ) return model def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , ) -> List[Any]: snake_case_ = False for name, module in model.named_children(): if current_key_name is None: snake_case_ = [] current_key_name.append(_SCREAMING_SNAKE_CASE ) if isinstance(_SCREAMING_SNAKE_CASE , nn.Linear ) and name not in modules_to_not_convert: # Check if the current key is not in the `modules_to_not_convert` snake_case_ = """.""".join(_SCREAMING_SNAKE_CASE ) snake_case_ = True for key in modules_to_not_convert: if ( (key in current_key_name_str) and (key + "." in current_key_name_str) ) or key == current_key_name_str: snake_case_ = False break if proceed: # Load bnb module with empty weight and replace ``nn.Linear` module if bnb_quantization_config.load_in_abit: snake_case_ = bnb.nn.LinearabitLt( module.in_features , module.out_features , module.bias is not None , has_fpaa_weights=_SCREAMING_SNAKE_CASE , threshold=bnb_quantization_config.llm_inta_threshold , ) elif bnb_quantization_config.load_in_abit: snake_case_ = bnb.nn.Linearabit( module.in_features , module.out_features , module.bias is not None , bnb_quantization_config.bnb_abit_compute_dtype , compress_statistics=bnb_quantization_config.bnb_abit_use_double_quant , quant_type=bnb_quantization_config.bnb_abit_quant_type , ) else: raise ValueError("""load_in_8bit and load_in_4bit can't be both False""" ) snake_case_ = module.weight.data if module.bias is not None: snake_case_ = module.bias.data bnb_module.requires_grad_(_SCREAMING_SNAKE_CASE ) setattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) snake_case_ = True if len(list(module.children() ) ) > 0: snake_case_ , snake_case_ = _replace_with_bnb_layers( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) snake_case_ = has_been_replaced | _has_been_replaced # Remove the last key for recursion current_key_name.pop(-1 ) return model, has_been_replaced def _a ( _SCREAMING_SNAKE_CASE ) -> Any: # Create a copy of the model with init_empty_weights(): snake_case_ = deepcopy(_SCREAMING_SNAKE_CASE ) # this has 0 cost since it is done inside `init_empty_weights` context manager` snake_case_ = find_tied_parameters(_SCREAMING_SNAKE_CASE ) # For compatibility with Accelerate < 0.18 if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): snake_case_ = sum(list(tied_params.values() ) , [] ) + list(tied_params.keys() ) else: snake_case_ = sum(_SCREAMING_SNAKE_CASE , [] ) snake_case_ = len(_SCREAMING_SNAKE_CASE ) > 0 # Check if it is a base model snake_case_ = False if hasattr(_SCREAMING_SNAKE_CASE , """base_model_prefix""" ): snake_case_ = not hasattr(_SCREAMING_SNAKE_CASE , model.base_model_prefix ) # Ignore this for base models (BertModel, GPT2Model, etc.) if (not has_tied_params) and is_base_model: return [] # otherwise they have an attached head snake_case_ = list(model.named_children() ) snake_case_ = [list_modules[-1][0]] # add last module together with tied weights snake_case_ = set(_SCREAMING_SNAKE_CASE ) - set(_SCREAMING_SNAKE_CASE ) snake_case_ = list(set(_SCREAMING_SNAKE_CASE ) ) + list(_SCREAMING_SNAKE_CASE ) # remove ".weight" from the keys snake_case_ = [""".weight""", """.bias"""] snake_case_ = [] for name in list_untouched: for name_to_remove in names_to_remove: if name_to_remove in name: snake_case_ = name.replace(_SCREAMING_SNAKE_CASE , """""" ) filtered_module_names.append(_SCREAMING_SNAKE_CASE ) return filtered_module_names def _a ( _SCREAMING_SNAKE_CASE ) -> Union[str, Any]: for m in model.modules(): if isinstance(_SCREAMING_SNAKE_CASE , bnb.nn.Linearabit ): return True return False def _a ( _SCREAMING_SNAKE_CASE ) -> Optional[int]: return next(parameter.parameters() ).device def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: # if it is not quantized, we quantize and offload the quantized weights and the SCB stats if fpaa_statistics is None: set_module_tensor_to_device(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , 0 , dtype=_SCREAMING_SNAKE_CASE , value=_SCREAMING_SNAKE_CASE ) snake_case_ = param_name snake_case_ = model if "." in tensor_name: snake_case_ = tensor_name.split(""".""" ) for split in splits[:-1]: snake_case_ = getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if new_module is None: raise ValueError(f"""{module} has no attribute {split}.""" ) snake_case_ = new_module snake_case_ = splits[-1] # offload weights snake_case_ = False offload_weight(module._parameters[tensor_name] , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , index=_SCREAMING_SNAKE_CASE ) if hasattr(module._parameters[tensor_name] , """SCB""" ): offload_weight( module._parameters[tensor_name].SCB , param_name.replace("""weight""" , """SCB""" ) , _SCREAMING_SNAKE_CASE , index=_SCREAMING_SNAKE_CASE , ) else: offload_weight(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , index=_SCREAMING_SNAKE_CASE ) offload_weight(_SCREAMING_SNAKE_CASE , param_name.replace("""weight""" , """SCB""" ) , _SCREAMING_SNAKE_CASE , index=_SCREAMING_SNAKE_CASE ) set_module_tensor_to_device(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , """meta""" , dtype=_SCREAMING_SNAKE_CASE , value=torch.empty(*param.size() ) )
347
0
'''simple docstring''' def _A ( snake_case , snake_case ) -> Optional[int]: assert x is not None assert y is not None _lowercase : Tuple = len(snake_case ) _lowercase : Optional[Any] = len(snake_case ) # declaring the array for storing the dp values _lowercase : Optional[int] = [[0] * (n + 1) for _ in range(m + 1 )] # noqa: E741 for i in range(1 , m + 1 ): for j in range(1 , n + 1 ): _lowercase : Optional[int] = 1 if x[i - 1] == y[j - 1] else 0 _lowercase : List[str] = max(l[i - 1][j] , l[i][j - 1] , l[i - 1][j - 1] + match ) _lowercase : List[Any] = "" _lowercase , _lowercase : Tuple = m, n while i > 0 and j > 0: _lowercase : Any = 1 if x[i - 1] == y[j - 1] else 0 if l[i][j] == l[i - 1][j - 1] + match: if match == 1: _lowercase : List[Any] = x[i - 1] + seq i -= 1 j -= 1 elif l[i][j] == l[i - 1][j]: i -= 1 else: j -= 1 return l[m][n], seq if __name__ == "__main__": _snake_case = 'AGGTAB' _snake_case = 'GXTXAYB' _snake_case = 4 _snake_case = 'GTAB' _snake_case , _snake_case = longest_common_subsequence(a, b) print('len =', ln, ', sub-sequence =', subseq) import doctest doctest.testmod()
199
'''simple docstring''' import os import sys import unittest _snake_case = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, 'utils')) import check_dummies # noqa: E402 from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init # noqa: E402 # Align TRANSFORMERS_PATH in check_dummies with the current path _snake_case = os.path.join(git_repo_path, 'src', 'transformers') _snake_case = '\n{0} = None\n' _snake_case = '\nclass {0}(metaclass=DummyObject):\n _backends = {1}\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, {1})\n' _snake_case = '\ndef {0}(*args, **kwargs):\n requires_backends({0}, {1})\n' class a__ ( unittest.TestCase ): def _lowerCamelCase ( self ): """simple docstring""" _lowercase : Optional[int] = find_backend(" _import_structure[\"models.albert\"].append(\"AlbertTokenizerFast\")" ) self.assertIsNone(_UpperCamelCase ) _lowercase : Dict = find_backend(" if not is_tokenizers_available():" ) self.assertEqual(_UpperCamelCase , "tokenizers" ) _lowercase : str = find_backend(" if not is_tensorflow_text_available():" ) self.assertEqual(_UpperCamelCase , "tensorflow_text" ) _lowercase : Optional[Any] = find_backend(" if not (is_sentencepiece_available() and is_tokenizers_available()):" ) self.assertEqual(_UpperCamelCase , "sentencepiece_and_tokenizers" ) _lowercase : List[str] = find_backend( " if not (is_sentencepiece_available() and is_tensorflow_text_available()):" ) self.assertEqual(_UpperCamelCase , "sentencepiece_and_tensorflow_text" ) _lowercase : Optional[Any] = find_backend( " if not (is_sentencepiece_available() and is_tokenizers_available() and is_vision_available()):" ) self.assertEqual(_UpperCamelCase , "sentencepiece_and_tokenizers_and_vision" ) def _lowerCamelCase ( self ): """simple docstring""" _lowercase : List[Any] = read_init() # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects self.assertIn("torch" , _UpperCamelCase ) self.assertIn("tensorflow_text" , _UpperCamelCase ) self.assertIn("sentencepiece_and_tokenizers" , _UpperCamelCase ) # Likewise, we can't assert on the exact content of a key self.assertIn("BertModel" , objects["torch"] ) self.assertIn("TFBertModel" , objects["tf"] ) self.assertIn("FlaxBertModel" , objects["flax"] ) self.assertIn("BertModel" , objects["torch"] ) self.assertIn("TFBertTokenizer" , objects["tensorflow_text"] ) self.assertIn("convert_slow_tokenizer" , objects["sentencepiece_and_tokenizers"] ) def _lowerCamelCase ( self ): """simple docstring""" _lowercase : int = create_dummy_object("CONSTANT" , "'torch'" ) self.assertEqual(_UpperCamelCase , "\nCONSTANT = None\n" ) _lowercase : Optional[Any] = create_dummy_object("function" , "'torch'" ) self.assertEqual( _UpperCamelCase , "\ndef function(*args, **kwargs):\n requires_backends(function, 'torch')\n" ) _lowercase : Union[str, Any] = "\nclass FakeClass(metaclass=DummyObject):\n _backends = 'torch'\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, 'torch')\n" _lowercase : List[str] = create_dummy_object("FakeClass" , "'torch'" ) self.assertEqual(_UpperCamelCase , _UpperCamelCase ) def _lowerCamelCase ( self ): """simple docstring""" _lowercase : Optional[int] = "# This file is autogenerated by the command `make fix-copies`, do not edit.\nfrom ..utils import DummyObject, requires_backends\n\n\nCONSTANT = None\n\n\ndef function(*args, **kwargs):\n requires_backends(function, [\"torch\"])\n\n\nclass FakeClass(metaclass=DummyObject):\n _backends = [\"torch\"]\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, [\"torch\"])\n" _lowercase : str = create_dummy_files({"torch": ["CONSTANT", "function", "FakeClass"]} ) self.assertEqual(dummy_files["torch"] , _UpperCamelCase )
199
1
class SCREAMING_SNAKE_CASE__ : def __init__( self): lowercase__ : str = {} def snake_case_ ( self): print(self.vertex) for i in self.vertex: print(a , ' -> ' , ' -> '.join([str(a) for j in self.vertex[i]])) def snake_case_ ( self , a , a): # check if vertex is already present, if from_vertex in self.vertex: self.vertex[from_vertex].append(a) else: # else make a new vertex lowercase__ : str = [to_vertex] def snake_case_ ( self): # visited array for storing already visited nodes lowercase__ : List[str] = [False] * len(self.vertex) # call the recursive helper function for i in range(len(self.vertex)): if not visited[i]: self.dfs_recursive(a , a) def snake_case_ ( self , a , a): # mark start vertex as visited lowercase__ : Optional[int] = True print(a , end=' ') # Recur for all the vertices that are adjacent to this node for i in self.vertex: if not visited[i]: self.dfs_recursive(a , a) if __name__ == "__main__": snake_case_ = Graph() g.add_edge(0, 1) g.add_edge(0, 2) g.add_edge(1, 2) g.add_edge(2, 0) g.add_edge(2, 3) g.add_edge(3, 3) g.print_graph() print('''DFS:''') g.dfs() # OUTPUT: # 0 -> 1 -> 2 # 1 -> 2 # 2 -> 0 -> 3 # 3 -> 3 # DFS: # 0 1 2 3
214
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging snake_case_ = logging.get_logger(__name__) snake_case_ = { '''distilbert-base-uncased''': '''https://huggingface.co/distilbert-base-uncased/resolve/main/config.json''', '''distilbert-base-uncased-distilled-squad''': ( '''https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json''' ), '''distilbert-base-cased''': '''https://huggingface.co/distilbert-base-cased/resolve/main/config.json''', '''distilbert-base-cased-distilled-squad''': ( '''https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json''' ), '''distilbert-base-german-cased''': '''https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json''', '''distilbert-base-multilingual-cased''': ( '''https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json''' ), '''distilbert-base-uncased-finetuned-sst-2-english''': ( '''https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json''' ), } class SCREAMING_SNAKE_CASE__ (__snake_case ): __lowerCamelCase : Any = """distilbert""" __lowerCamelCase : List[Any] = { """hidden_size""": """dim""", """num_attention_heads""": """n_heads""", """num_hidden_layers""": """n_layers""", } def __init__( self , a=3_0522 , a=512 , a=False , a=6 , a=12 , a=768 , a=4 * 768 , a=0.1 , a=0.1 , a="gelu" , a=0.02 , a=0.1 , a=0.2 , a=0 , **a , ): lowercase__ : int = vocab_size lowercase__ : int = max_position_embeddings lowercase__ : List[str] = sinusoidal_pos_embds lowercase__ : Tuple = n_layers lowercase__ : Optional[int] = n_heads lowercase__ : List[Any] = dim lowercase__ : Any = hidden_dim lowercase__ : Union[str, Any] = dropout lowercase__ : Optional[int] = attention_dropout lowercase__ : List[Any] = activation lowercase__ : Union[str, Any] = initializer_range lowercase__ : Dict = qa_dropout lowercase__ : Tuple = seq_classif_dropout super().__init__(**a , pad_token_id=a) class SCREAMING_SNAKE_CASE__ (__snake_case ): @property def snake_case_ ( self): if self.task == "multiple-choice": lowercase__ : List[str] = {0: 'batch', 1: 'choice', 2: 'sequence'} else: lowercase__ : Tuple = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ])
214
1
"""simple docstring""" import logging from dataclasses import dataclass, field from pathlib import Path from typing import Optional, Union from .generation.configuration_utils import GenerationConfig from .training_args import TrainingArguments from .utils import add_start_docstrings _lowerCAmelCase : Union[str, Any] = logging.getLogger(__name__) @dataclass @add_start_docstrings(TrainingArguments.__doc__ ) class A_ ( _a ): lowerCAmelCase__ = field(default=_a , metadata={'help': 'Whether to use SortishSampler or not.'} ) lowerCAmelCase__ = field( default=_a , metadata={'help': 'Whether to use generate to calculate generative metrics (ROUGE, BLEU).'} ) lowerCAmelCase__ = field( default=_a , metadata={ 'help': ( 'The `max_length` to use on each evaluation loop when `predict_with_generate=True`. Will default ' 'to the `max_length` value of the model configuration.' ) } , ) lowerCAmelCase__ = field( default=_a , metadata={ 'help': ( 'The `num_beams` to use on each evaluation loop when `predict_with_generate=True`. Will default ' 'to the `num_beams` value of the model configuration.' ) } , ) lowerCAmelCase__ = field( default=_a , metadata={ 'help': 'Model id, file path or url pointing to a GenerationConfig json file, to use during prediction.' } , ) def _lowercase ( self: Optional[int] ): '''simple docstring''' _lowerCamelCase : List[Any] = super().to_dict() for k, v in d.items(): if isinstance(__lowerCAmelCase ,__lowerCAmelCase ): _lowerCamelCase : Optional[int] = v.to_dict() return d
340
"""simple docstring""" _lowerCAmelCase : dict[tuple[int, int, int], int] = {} def lowerCamelCase_( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> int: '''simple docstring''' if late == 3 or absent == 2: return 0 # if we have no days left, and have not failed any other rules, # we have a prize string if days == 0: return 1 # No easy solution, so now we need to do the recursive calculation # First, check if the combination is already in the cache, and # if yes, return the stored value from there since we already # know the number of possible prize strings from this point on _lowerCamelCase : Optional[int] = (days, absent, late) if key in cache: return cache[key] # now we calculate the three possible ways that can unfold from # this point on, depending on our attendance today # 1) if we are late (but not absent), the "absent" counter stays as # it is, but the "late" counter increases by one _lowerCamelCase : int = _calculate(days - 1 , _lowerCamelCase , late + 1 ) # 2) if we are absent, the "absent" counter increases by 1, and the # "late" counter resets to 0 _lowerCamelCase : Tuple = _calculate(days - 1 , absent + 1 , 0 ) # 3) if we are on time, this resets the "late" counter and keeps the # absent counter _lowerCamelCase : str = _calculate(days - 1 , _lowerCamelCase , 0 ) _lowerCamelCase : List[Any] = state_late + state_absent + state_ontime _lowerCamelCase : int = prizestrings return prizestrings def lowerCamelCase_( _lowerCamelCase = 30 ) -> int: '''simple docstring''' return _calculate(_lowerCamelCase , absent=0 , late=0 ) if __name__ == "__main__": print(solution())
340
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available _SCREAMING_SNAKE_CASE = { """configuration_roc_bert""": ["""ROC_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """RoCBertConfig"""], """tokenization_roc_bert""": ["""RoCBertTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: pass try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE = [ """ROC_BERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """RoCBertForCausalLM""", """RoCBertForMaskedLM""", """RoCBertForMultipleChoice""", """RoCBertForPreTraining""", """RoCBertForQuestionAnswering""", """RoCBertForSequenceClassification""", """RoCBertForTokenClassification""", """RoCBertLayer""", """RoCBertModel""", """RoCBertPreTrainedModel""", """load_tf_weights_in_roc_bert""", ] if TYPE_CHECKING: from .configuration_roc_bert import ROC_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RoCBertConfig from .tokenization_roc_bert import RoCBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: raise OptionalDependencyNotAvailable() try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roc_bert import ( ROC_BERT_PRETRAINED_MODEL_ARCHIVE_LIST, RoCBertForCausalLM, RoCBertForMaskedLM, RoCBertForMultipleChoice, RoCBertForPreTraining, RoCBertForQuestionAnswering, RoCBertForSequenceClassification, RoCBertForTokenClassification, RoCBertLayer, RoCBertModel, RoCBertPreTrainedModel, load_tf_weights_in_roc_bert, ) else: import sys _SCREAMING_SNAKE_CASE = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
327
"""simple docstring""" def UpperCAmelCase__ ( lowerCAmelCase__ :str , lowerCAmelCase__ :Optional[Any] ) -> int: '''simple docstring''' lowercase = [0 for i in range(r + 1 )] # nc0 = 1 lowercase = 1 for i in range(1 , n + 1 ): # to compute current row from previous row. lowercase = min(lowerCAmelCase__ , lowerCAmelCase__ ) while j > 0: c[j] += c[j - 1] j -= 1 return c[r] print(binomial_coefficient(n=1_0, r=5))
197
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase__ = { '''configuration_whisper''': ['''WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''WhisperConfig''', '''WhisperOnnxConfig'''], '''feature_extraction_whisper''': ['''WhisperFeatureExtractor'''], '''processing_whisper''': ['''WhisperProcessor'''], '''tokenization_whisper''': ['''WhisperTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ['''WhisperTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''WhisperForConditionalGeneration''', '''WhisperModel''', '''WhisperPreTrainedModel''', '''WhisperForAudioClassification''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFWhisperForConditionalGeneration''', '''TFWhisperModel''', '''TFWhisperPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''FlaxWhisperForConditionalGeneration''', '''FlaxWhisperModel''', '''FlaxWhisperPreTrainedModel''', '''FlaxWhisperForAudioClassification''', ] if TYPE_CHECKING: from .configuration_whisper import WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP, WhisperConfig, WhisperOnnxConfig from .feature_extraction_whisper import WhisperFeatureExtractor from .processing_whisper import WhisperProcessor from .tokenization_whisper import WhisperTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_whisper_fast import WhisperTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_whisper import ( WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST, WhisperForAudioClassification, WhisperForConditionalGeneration, WhisperModel, WhisperPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_whisper import ( TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST, TFWhisperForConditionalGeneration, TFWhisperModel, TFWhisperPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_whisper import ( FlaxWhisperForAudioClassification, FlaxWhisperForConditionalGeneration, FlaxWhisperModel, FlaxWhisperPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
133
"""simple docstring""" import argparse import glob import importlib.util import os import re import black from doc_builder.style_doc import style_docstrings_in_code # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_copies.py lowerCAmelCase__ = '''src/diffusers''' lowerCAmelCase__ = '''.''' # This is to make sure the diffusers module imported is the one in the repo. lowerCAmelCase__ = importlib.util.spec_from_file_location( '''diffusers''', os.path.join(DIFFUSERS_PATH, '''__init__.py'''), submodule_search_locations=[DIFFUSERS_PATH], ) lowerCAmelCase__ = spec.loader.load_module() def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int ): '''simple docstring''' return line.startswith(SCREAMING_SNAKE_CASE ) or len(SCREAMING_SNAKE_CASE ) <= 1 or re.search(r"^\s*\)(\s*->.*:|:)\s*$" , SCREAMING_SNAKE_CASE ) is not None def a__ ( SCREAMING_SNAKE_CASE : Tuple ): '''simple docstring''' lowerCAmelCase : Dict = object_name.split("." ) lowerCAmelCase : Optional[int] = 0 # First let's find the module where our object lives. lowerCAmelCase : Any = parts[i] while i < len(SCREAMING_SNAKE_CASE ) and not os.path.isfile(os.path.join(SCREAMING_SNAKE_CASE , f"""{module}.py""" ) ): i += 1 if i < len(SCREAMING_SNAKE_CASE ): lowerCAmelCase : Optional[Any] = os.path.join(SCREAMING_SNAKE_CASE , parts[i] ) if i >= len(SCREAMING_SNAKE_CASE ): raise ValueError(f"""`object_name` should begin with the name of a module of diffusers but got {object_name}.""" ) with open(os.path.join(SCREAMING_SNAKE_CASE , f"""{module}.py""" ) , "r" , encoding="utf-8" , newline="\n" ) as f: lowerCAmelCase : List[Any] = f.readlines() # Now let's find the class / func in the code! lowerCAmelCase : List[str] = "" lowerCAmelCase : int = 0 for name in parts[i + 1 :]: while ( line_index < len(SCREAMING_SNAKE_CASE ) and re.search(rf"""^{indent}(class|def)\s+{name}(\(|\:)""" , lines[line_index] ) is None ): line_index += 1 indent += " " line_index += 1 if line_index >= len(SCREAMING_SNAKE_CASE ): raise ValueError(f""" {object_name} does not match any function or class in {module}.""" ) # We found the beginning of the class / func, now let's find the end (when the indent diminishes). lowerCAmelCase : List[str] = line_index while line_index < len(SCREAMING_SNAKE_CASE ) and _should_continue(lines[line_index] , SCREAMING_SNAKE_CASE ): line_index += 1 # Clean up empty lines at the end (if any). while len(lines[line_index - 1] ) <= 1: line_index -= 1 lowerCAmelCase : List[Any] = lines[start_index:line_index] return "".join(SCREAMING_SNAKE_CASE ) lowerCAmelCase__ = re.compile(r'''^(\s*)#\s*Copied from\s+diffusers\.(\S+\.\S+)\s*($|\S.*$)''') lowerCAmelCase__ = re.compile(r'''^\s*(\S+)->(\S+)(\s+.*|$)''') lowerCAmelCase__ = re.compile(r'''<FILL\s+[^>]*>''') def a__ ( SCREAMING_SNAKE_CASE : str ): '''simple docstring''' lowerCAmelCase : int = code.split("\n" ) lowerCAmelCase : List[str] = 0 while idx < len(SCREAMING_SNAKE_CASE ) and len(lines[idx] ) == 0: idx += 1 if idx < len(SCREAMING_SNAKE_CASE ): return re.search(r"^(\s*)\S" , lines[idx] ).groups()[0] return "" def a__ ( SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' lowerCAmelCase : List[Any] = len(get_indent(SCREAMING_SNAKE_CASE ) ) > 0 if has_indent: lowerCAmelCase : Tuple = f"""class Bla:\n{code}""" lowerCAmelCase : Optional[Any] = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=1_1_9 , preview=SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = black.format_str(SCREAMING_SNAKE_CASE , mode=SCREAMING_SNAKE_CASE ) lowerCAmelCase , lowerCAmelCase : List[Any] = style_docstrings_in_code(SCREAMING_SNAKE_CASE ) return result[len("class Bla:\n" ) :] if has_indent else result def a__ ( SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : int=False ): '''simple docstring''' with open(SCREAMING_SNAKE_CASE , "r" , encoding="utf-8" , newline="\n" ) as f: lowerCAmelCase : int = f.readlines() lowerCAmelCase : List[str] = [] lowerCAmelCase : str = 0 # Not a for loop cause `lines` is going to change (if `overwrite=True`). while line_index < len(SCREAMING_SNAKE_CASE ): lowerCAmelCase : List[Any] = _re_copy_warning.search(lines[line_index] ) if search is None: line_index += 1 continue # There is some copied code here, let's retrieve the original. lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : List[Any] = search.groups() lowerCAmelCase : List[str] = find_code_in_diffusers(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Optional[Any] = get_indent(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = line_index + 1 if indent == theoretical_indent else line_index + 2 lowerCAmelCase : Optional[int] = theoretical_indent lowerCAmelCase : List[str] = start_index # Loop to check the observed code, stop when indentation diminishes or if we see a End copy comment. lowerCAmelCase : str = True while line_index < len(SCREAMING_SNAKE_CASE ) and should_continue: line_index += 1 if line_index >= len(SCREAMING_SNAKE_CASE ): break lowerCAmelCase : Tuple = lines[line_index] lowerCAmelCase : str = _should_continue(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and re.search(f"""^{indent}# End copy""" , SCREAMING_SNAKE_CASE ) is None # Clean up empty lines at the end (if any). while len(lines[line_index - 1] ) <= 1: line_index -= 1 lowerCAmelCase : Tuple = lines[start_index:line_index] lowerCAmelCase : List[str] = "".join(SCREAMING_SNAKE_CASE ) # Remove any nested `Copied from` comments to avoid circular copies lowerCAmelCase : List[str] = [line for line in theoretical_code.split("\n" ) if _re_copy_warning.search(SCREAMING_SNAKE_CASE ) is None] lowerCAmelCase : Union[str, Any] = "\n".join(SCREAMING_SNAKE_CASE ) # Before comparing, use the `replace_pattern` on the original code. if len(SCREAMING_SNAKE_CASE ) > 0: lowerCAmelCase : str = replace_pattern.replace("with" , "" ).split("," ) lowerCAmelCase : List[str] = [_re_replace_pattern.search(SCREAMING_SNAKE_CASE ) for p in patterns] for pattern in patterns: if pattern is None: continue lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : List[str] = pattern.groups() lowerCAmelCase : List[Any] = re.sub(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) if option.strip() == "all-casing": lowerCAmelCase : Optional[Any] = re.sub(obja.lower() , obja.lower() , SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = re.sub(obja.upper() , obja.upper() , SCREAMING_SNAKE_CASE ) # Blackify after replacement. To be able to do that, we need the header (class or function definition) # from the previous line lowerCAmelCase : Union[str, Any] = blackify(lines[start_index - 1] + theoretical_code ) lowerCAmelCase : List[str] = theoretical_code[len(lines[start_index - 1] ) :] # Test for a diff and act accordingly. if observed_code != theoretical_code: diffs.append([object_name, start_index] ) if overwrite: lowerCAmelCase : Tuple = lines[:start_index] + [theoretical_code] + lines[line_index:] lowerCAmelCase : int = start_index + 1 if overwrite and len(SCREAMING_SNAKE_CASE ) > 0: # Warn the user a file has been modified. print(f"""Detected changes, rewriting {filename}.""" ) with open(SCREAMING_SNAKE_CASE , "w" , encoding="utf-8" , newline="\n" ) as f: f.writelines(SCREAMING_SNAKE_CASE ) return diffs def a__ ( SCREAMING_SNAKE_CASE : bool = False ): '''simple docstring''' lowerCAmelCase : List[Any] = glob.glob(os.path.join(SCREAMING_SNAKE_CASE , "**/*.py" ) , recursive=SCREAMING_SNAKE_CASE ) lowerCAmelCase : str = [] for filename in all_files: lowerCAmelCase : List[Any] = is_copy_consistent(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) diffs += [f"""- {filename}: copy does not match {d[0]} at line {d[1]}""" for d in new_diffs] if not overwrite and len(SCREAMING_SNAKE_CASE ) > 0: lowerCAmelCase : List[Any] = "\n".join(SCREAMING_SNAKE_CASE ) raise Exception( "Found the following copy inconsistencies:\n" + diff + "\nRun `make fix-copies` or `python utils/check_copies.py --fix_and_overwrite` to fix them." ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') lowerCAmelCase__ = parser.parse_args() check_copies(args.fix_and_overwrite)
133
1
__A : Any = [sum(int(c, 10) ** 2 for c in i.__str__()) for i in range(10_00_00)] def __UpperCamelCase ( _A : str ) ->List[str]: """simple docstring""" lowerCamelCase_ =0 while number: # Increased Speed Slightly by checking every 5 digits together. sum_of_digits_squared += DIGITS_SQUARED[number % 100000] number //= 100000 return sum_of_digits_squared # There are 2 Chains made, # One ends with 89 with the chain member 58 being the one which when declared first, # there will be the least number of iterations for all the members to be checked. # The other one ends with 1 and has only one element 1. # So 58 and 1 are chosen to be declared at the starting. # Changed dictionary to an array to quicken the solution __A : int = [None] * 10_00_00_00 __A : Dict = True __A : List[Any] = False def __UpperCamelCase ( _A : Optional[Any] ) ->Optional[Any]: """simple docstring""" if CHAINS[number - 1] is not None: return CHAINS[number - 1] # type: ignore lowerCamelCase_ =chain(next_number(UpperCamelCase__ ) ) lowerCamelCase_ =number_chain while number < 10000000: lowerCamelCase_ =number_chain number *= 10 return number_chain def __UpperCamelCase ( _A : Optional[int] = 10000000 ) ->Any: """simple docstring""" for i in range(1 , UpperCamelCase__ ): if CHAINS[i] is None: chain(i + 1 ) return CHAINS[:number].count(UpperCamelCase__ ) if __name__ == "__main__": import doctest doctest.testmod() print(F"""{solution() = }""")
154
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __A =logging.get_logger(__name__) __A ={ 'google/pegasus-large': 'https://huggingface.co/google/pegasus-large/resolve/main/config.json', # See all PEGASUS models at https://huggingface.co/models?filter=pegasus } class _snake_case ( a__ ): lowerCAmelCase :Optional[int] = '''pegasus''' lowerCAmelCase :Optional[int] = ['''past_key_values'''] lowerCAmelCase :str = {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''} def __init__( self , _lowerCamelCase=5_0265 , _lowerCamelCase=1024 , _lowerCamelCase=12 , _lowerCamelCase=4096 , _lowerCamelCase=16 , _lowerCamelCase=12 , _lowerCamelCase=4096 , _lowerCamelCase=16 , _lowerCamelCase=0.0 , _lowerCamelCase=0.0 , _lowerCamelCase=True , _lowerCamelCase=True , _lowerCamelCase="gelu" , _lowerCamelCase=1024 , _lowerCamelCase=0.1 , _lowerCamelCase=0.0 , _lowerCamelCase=0.0 , _lowerCamelCase=0.02 , _lowerCamelCase=0 , _lowerCamelCase=False , _lowerCamelCase=0 , _lowerCamelCase=1 , _lowerCamelCase=1 , **_lowerCamelCase , ): UpperCAmelCase__ : Union[str, Any] = vocab_size UpperCAmelCase__ : Union[str, Any] = max_position_embeddings UpperCAmelCase__ : List[Any] = d_model UpperCAmelCase__ : Union[str, Any] = encoder_ffn_dim UpperCAmelCase__ : Any = encoder_layers UpperCAmelCase__ : List[str] = encoder_attention_heads UpperCAmelCase__ : int = decoder_ffn_dim UpperCAmelCase__ : Any = decoder_layers UpperCAmelCase__ : Tuple = decoder_attention_heads UpperCAmelCase__ : Optional[int] = dropout UpperCAmelCase__ : Dict = attention_dropout UpperCAmelCase__ : Optional[int] = activation_dropout UpperCAmelCase__ : Dict = activation_function UpperCAmelCase__ : Optional[Any] = init_std UpperCAmelCase__ : int = encoder_layerdrop UpperCAmelCase__ : Tuple = decoder_layerdrop UpperCAmelCase__ : str = use_cache UpperCAmelCase__ : Any = encoder_layers UpperCAmelCase__ : Tuple = scale_embedding # scale factor will be sqrt(d_model) if True super().__init__( pad_token_id=_lowerCamelCase , eos_token_id=_lowerCamelCase , is_encoder_decoder=_lowerCamelCase , decoder_start_token_id=_lowerCamelCase , forced_eos_token_id=_lowerCamelCase , **_lowerCamelCase , ) @property def snake_case__ ( self): return self.encoder_attention_heads @property def snake_case__ ( self): return self.d_model
163
0
'''simple docstring''' from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available from ...utils import OptionalDependencyNotAvailable __lowerCAmelCase = {'''configuration_dpt''': ['''DPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''DPTConfig''']} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = ['''DPTFeatureExtractor'''] __lowerCAmelCase = ['''DPTImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ '''DPT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''DPTForDepthEstimation''', '''DPTForSemanticSegmentation''', '''DPTModel''', '''DPTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_dpt import DPT_PRETRAINED_CONFIG_ARCHIVE_MAP, DPTConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_dpt import DPTFeatureExtractor from .image_processing_dpt import DPTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_dpt import ( DPT_PRETRAINED_MODEL_ARCHIVE_LIST, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel, DPTPreTrainedModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
107
'''simple docstring''' import enum import warnings from ..tokenization_utils import TruncationStrategy from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, Pipeline if is_tf_available(): import tensorflow as tf from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING __lowerCAmelCase = logging.get_logger(__name__) class __magic_name__ ( enum.Enum ): lowerCAmelCase : Union[str, Any] = 0 lowerCAmelCase : Dict = 1 @add_end_docstrings(_UpperCamelCase ) class __magic_name__ ( _UpperCamelCase ): lowerCAmelCase : int = 'generated' def __init__( self : List[Any] ,*_UpperCAmelCase : Any ,**_UpperCAmelCase : Optional[int] ): super().__init__(*_UpperCAmelCase ,**_UpperCAmelCase ) self.check_model_type( TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING if self.framework == 'tf' else MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING ) def __lowercase ( self : Optional[int] ,_UpperCAmelCase : List[Any]=None ,_UpperCAmelCase : Tuple=None ,_UpperCAmelCase : List[Any]=None ,_UpperCAmelCase : Union[str, Any]=None ,_UpperCAmelCase : Dict=None ,_UpperCAmelCase : Optional[int]=None ,**_UpperCAmelCase : List[Any] ,): _a : Tuple = {} if truncation is not None: _a : Union[str, Any] = truncation _a : List[str] = generate_kwargs _a : Optional[Any] = {} if return_tensors is not None and return_type is None: _a : Optional[int] = ReturnType.TENSORS if return_tensors else ReturnType.TEXT if return_type is not None: _a : List[Any] = return_type if clean_up_tokenization_spaces is not None: _a : Optional[int] = clean_up_tokenization_spaces if stop_sequence is not None: _a : List[str] = self.tokenizer.encode(_UpperCAmelCase ,add_special_tokens=_UpperCAmelCase ) if len(_UpperCAmelCase ) > 1: warnings.warn( 'Stopping on a multiple token sequence is not yet supported on transformers. The first token of' ' the stop sequence will be used as the stop sequence string in the interim.' ) _a : List[Any] = stop_sequence_ids[0] return preprocess_params, forward_params, postprocess_params def __lowercase ( self : Any ,_UpperCAmelCase : int ,_UpperCAmelCase : int ,_UpperCAmelCase : int ): return True def __lowercase ( self : str ,*_UpperCAmelCase : Any ,_UpperCAmelCase : Union[str, Any] ): _a : Optional[Any] = self.model.config.prefix if self.model.config.prefix is not None else '' if isinstance(args[0] ,_UpperCAmelCase ): if self.tokenizer.pad_token_id is None: raise ValueError('Please make sure that the tokenizer has a pad_token_id when using a batch input' ) _a : Optional[Any] = ([prefix + arg for arg in args[0]],) _a : Any = True elif isinstance(args[0] ,_UpperCAmelCase ): _a : List[str] = (prefix + args[0],) _a : str = False else: raise ValueError( F""" `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`""" ) _a : Optional[Any] = self.tokenizer(*_UpperCAmelCase ,padding=_UpperCAmelCase ,truncation=_UpperCAmelCase ,return_tensors=self.framework ) # This is produced by tokenizers but is an invalid generate kwargs if "token_type_ids" in inputs: del inputs["token_type_ids"] return inputs def __call__( self : Optional[int] ,*_UpperCAmelCase : List[Any] ,**_UpperCAmelCase : Any ): _a : Tuple = super().__call__(*_UpperCAmelCase ,**_UpperCAmelCase ) if ( isinstance(args[0] ,_UpperCAmelCase ) and all(isinstance(_UpperCAmelCase ,_UpperCAmelCase ) for el in args[0] ) and all(len(_UpperCAmelCase ) == 1 for res in result ) ): return [res[0] for res in result] return result def __lowercase ( self : Tuple ,_UpperCAmelCase : Union[str, Any] ,_UpperCAmelCase : List[str]=TruncationStrategy.DO_NOT_TRUNCATE ,**_UpperCAmelCase : int ): _a : Dict = self._parse_and_tokenize(_UpperCAmelCase ,truncation=_UpperCAmelCase ,**_UpperCAmelCase ) return inputs def __lowercase ( self : Any ,_UpperCAmelCase : str ,**_UpperCAmelCase : Tuple ): if self.framework == "pt": _a , _a : int = model_inputs['input_ids'].shape elif self.framework == "tf": _a , _a : Dict = tf.shape(model_inputs['input_ids'] ).numpy() _a : Optional[Any] = generate_kwargs.get('min_length' ,self.model.config.min_length ) _a : Optional[int] = generate_kwargs.get('max_length' ,self.model.config.max_length ) self.check_inputs(_UpperCAmelCase ,generate_kwargs['min_length'] ,generate_kwargs['max_length'] ) _a : List[str] = self.model.generate(**_UpperCAmelCase ,**_UpperCAmelCase ) _a : int = output_ids.shape[0] if self.framework == "pt": _a : int = output_ids.reshape(_UpperCAmelCase ,out_b // in_b ,*output_ids.shape[1:] ) elif self.framework == "tf": _a : Any = tf.reshape(_UpperCAmelCase ,(in_b, out_b // in_b, *output_ids.shape[1:]) ) return {"output_ids": output_ids} def __lowercase ( self : Dict ,_UpperCAmelCase : Tuple ,_UpperCAmelCase : Any=ReturnType.TEXT ,_UpperCAmelCase : Dict=False ): _a : Union[str, Any] = [] for output_ids in model_outputs["output_ids"][0]: if return_type == ReturnType.TENSORS: _a : int = {F"""{self.return_name}_token_ids""": output_ids} elif return_type == ReturnType.TEXT: _a : str = { F"""{self.return_name}_text""": self.tokenizer.decode( _UpperCAmelCase ,skip_special_tokens=_UpperCAmelCase ,clean_up_tokenization_spaces=_UpperCAmelCase ,) } records.append(_UpperCAmelCase ) return records @add_end_docstrings(_UpperCamelCase ) class __magic_name__ ( _UpperCamelCase ): lowerCAmelCase : Any = 'summary' def __call__( self : Any ,*_UpperCAmelCase : Union[str, Any] ,**_UpperCAmelCase : List[str] ): return super().__call__(*_UpperCAmelCase ,**_UpperCAmelCase ) def __lowercase ( self : Optional[Any] ,_UpperCAmelCase : int ,_UpperCAmelCase : int ,_UpperCAmelCase : int ): if max_length < min_length: logger.warning(F"""Your min_length={min_length} must be inferior than your max_length={max_length}.""" ) if input_length < max_length: logger.warning( F"""Your max_length is set to {max_length}, but your input_length is only {input_length}. Since this is """ 'a summarization task, where outputs shorter than the input are typically wanted, you might ' F"""consider decreasing max_length manually, e.g. summarizer('...', max_length={input_length//2})""" ) @add_end_docstrings(_UpperCamelCase ) class __magic_name__ ( _UpperCamelCase ): lowerCAmelCase : List[Any] = 'translation' def __lowercase ( self : Optional[int] ,_UpperCAmelCase : int ,_UpperCAmelCase : int ,_UpperCAmelCase : int ): if input_length > 0.9 * max_length: logger.warning( F"""Your input_length: {input_length} is bigger than 0.9 * max_length: {max_length}. You might consider """ 'increasing your max_length manually, e.g. translator(\'...\', max_length=400)' ) return True def __lowercase ( self : Any ,*_UpperCAmelCase : Any ,_UpperCAmelCase : List[str]=TruncationStrategy.DO_NOT_TRUNCATE ,_UpperCAmelCase : Optional[int]=None ,_UpperCAmelCase : Any=None ): if getattr(self.tokenizer ,'_build_translation_inputs' ,_UpperCAmelCase ): return self.tokenizer._build_translation_inputs( *_UpperCAmelCase ,return_tensors=self.framework ,truncation=_UpperCAmelCase ,src_lang=_UpperCAmelCase ,tgt_lang=_UpperCAmelCase ) else: return super()._parse_and_tokenize(*_UpperCAmelCase ,truncation=_UpperCAmelCase ) def __lowercase ( self : Tuple ,_UpperCAmelCase : List[Any]=None ,_UpperCAmelCase : List[str]=None ,**_UpperCAmelCase : Dict ): _a , _a , _a : str = super()._sanitize_parameters(**_UpperCAmelCase ) if src_lang is not None: _a : Optional[int] = src_lang if tgt_lang is not None: _a : List[Any] = tgt_lang if src_lang is None and tgt_lang is None: # Backward compatibility, direct arguments use is preferred. _a : int = kwargs.get('task' ,self.task ) _a : int = task.split('_' ) if task and len(_UpperCAmelCase ) == 4: # translation, XX, to YY _a : List[Any] = items[1] _a : Any = items[3] return preprocess_params, forward_params, postprocess_params def __call__( self : Optional[Any] ,*_UpperCAmelCase : Tuple ,**_UpperCAmelCase : Optional[int] ): return super().__call__(*_UpperCAmelCase ,**_UpperCAmelCase )
107
1
import tempfile import unittest from make_student import create_student_by_copying_alternating_layers from transformers import AutoConfig from transformers.file_utils import cached_property from transformers.testing_utils import require_torch _lowerCAmelCase : Any = "sshleifer/bart-tiny-random" _lowerCAmelCase : str = "patrickvonplaten/t5-tiny-random" @require_torch class __magic_name__ ( unittest.TestCase ): @cached_property def __magic_name__ ( self ) -> Optional[Any]: '''simple docstring''' return AutoConfig.from_pretrained(__snake_case ) def __magic_name__ ( self ) -> Tuple: '''simple docstring''' __a , *__a =create_student_by_copying_alternating_layers(__snake_case , tempfile.mkdtemp() , e=1 , d=1 ) self.assertEqual(student.config.num_hidden_layers , 1 ) def __magic_name__ ( self ) -> str: '''simple docstring''' __a , *__a =create_student_by_copying_alternating_layers(__snake_case , tempfile.mkdtemp() , e=1 , d=__snake_case ) def __magic_name__ ( self ) -> str: '''simple docstring''' __a , *__a =create_student_by_copying_alternating_layers(__snake_case , tempfile.mkdtemp() , e=1 , d=__snake_case ) self.assertEqual(student.config.encoder_layers , 1 ) self.assertEqual(student.config.decoder_layers , self.teacher_config.encoder_layers ) def __magic_name__ ( self ) -> List[Any]: '''simple docstring''' __a , *__a =create_student_by_copying_alternating_layers(__snake_case , tempfile.mkdtemp() , e=1 , d=1 ) self.assertEqual(student.config.encoder_layers , 1 ) self.assertEqual(student.config.decoder_layers , 1 ) def __magic_name__ ( self ) -> int: '''simple docstring''' with self.assertRaises(__snake_case ): create_student_by_copying_alternating_layers(__snake_case , tempfile.mkdtemp() , e=__snake_case , d=__snake_case )
218
from ... import PretrainedConfig _lowerCAmelCase : Any = { "sijunhe/nezha-cn-base": "https://huggingface.co/sijunhe/nezha-cn-base/resolve/main/config.json", } class __magic_name__ ( lowerCAmelCase_ ): SCREAMING_SNAKE_CASE = NEZHA_PRETRAINED_CONFIG_ARCHIVE_MAP SCREAMING_SNAKE_CASE = 'nezha' def __init__( self , __snake_case=2_1128 , __snake_case=768 , __snake_case=12 , __snake_case=12 , __snake_case=3072 , __snake_case="gelu" , __snake_case=0.1 , __snake_case=0.1 , __snake_case=512 , __snake_case=64 , __snake_case=2 , __snake_case=0.02 , __snake_case=1e-12 , __snake_case=0.1 , __snake_case=0 , __snake_case=2 , __snake_case=3 , __snake_case=True , **__snake_case , ) -> str: '''simple docstring''' super().__init__(pad_token_id=__snake_case , bos_token_id=__snake_case , eos_token_id=__snake_case , **__snake_case ) __a =vocab_size __a =hidden_size __a =num_hidden_layers __a =num_attention_heads __a =hidden_act __a =intermediate_size __a =hidden_dropout_prob __a =attention_probs_dropout_prob __a =max_position_embeddings __a =max_relative_position __a =type_vocab_size __a =initializer_range __a =layer_norm_eps __a =classifier_dropout __a =use_cache
218
1
import unittest from transformers import BigBirdTokenizer, BigBirdTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin UpperCamelCase__ = "▁" UpperCamelCase__ = get_tests_dir("fixtures/test_sentencepiece.model") @require_sentencepiece @require_tokenizers class __SCREAMING_SNAKE_CASE ( _a , unittest.TestCase ): snake_case : List[Any] = BigBirdTokenizer snake_case : Union[str, Any] = BigBirdTokenizerFast snake_case : Tuple = True snake_case : List[str] = True def _lowerCamelCase ( self ): super().setUp() UpperCamelCase__ = self.tokenizer_class(__lowerCAmelCase , keep_accents=__lowerCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) def _lowerCamelCase ( self ): UpperCamelCase__ = """<s>""" UpperCamelCase__ = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__lowerCAmelCase ) , __lowerCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__lowerCAmelCase ) , __lowerCAmelCase ) def _lowerCamelCase ( self ): UpperCamelCase__ = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , """<unk>""" ) self.assertEqual(vocab_keys[1] , """<s>""" ) self.assertEqual(vocab_keys[-1] , """[MASK]""" ) self.assertEqual(len(__lowerCAmelCase ) , 1004 ) def _lowerCamelCase ( self ): self.assertEqual(self.get_tokenizer().vocab_size , 1000 ) def _lowerCamelCase ( self ): if not self.test_rust_tokenizer: return UpperCamelCase__ = self.get_tokenizer() UpperCamelCase__ = self.get_rust_tokenizer() UpperCamelCase__ = """I was born in 92000, and this is falsé.""" UpperCamelCase__ = tokenizer.tokenize(__lowerCAmelCase ) UpperCamelCase__ = rust_tokenizer.tokenize(__lowerCAmelCase ) self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase ) UpperCamelCase__ = tokenizer.encode(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase ) UpperCamelCase__ = rust_tokenizer.encode(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase ) self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase ) UpperCamelCase__ = self.get_rust_tokenizer() UpperCamelCase__ = tokenizer.encode(__lowerCAmelCase ) UpperCamelCase__ = rust_tokenizer.encode(__lowerCAmelCase ) self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase ) def _lowerCamelCase ( self ): UpperCamelCase__ = BigBirdTokenizer(__lowerCAmelCase , keep_accents=__lowerCAmelCase ) UpperCamelCase__ = tokenizer.tokenize("""This is a test""" ) self.assertListEqual(__lowerCAmelCase , ["""▁This""", """▁is""", """▁a""", """▁t""", """est"""] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__lowerCAmelCase ) , [285, 46, 10, 170, 382] , ) UpperCamelCase__ = tokenizer.tokenize("""I was born in 92000, and this is falsé.""" ) self.assertListEqual( __lowerCAmelCase , [ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """9""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """é""", """.""", ] , ) UpperCamelCase__ = tokenizer.convert_tokens_to_ids(__lowerCAmelCase ) self.assertListEqual( __lowerCAmelCase , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) UpperCamelCase__ = tokenizer.convert_ids_to_tokens(__lowerCAmelCase ) self.assertListEqual( __lowerCAmelCase , [ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """<unk>""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """<unk>""", """.""", ] , ) @cached_property def _lowerCamelCase ( self ): return BigBirdTokenizer.from_pretrained("""google/bigbird-roberta-base""" ) @slow def _lowerCamelCase ( self ): UpperCamelCase__ = """Hello World!""" UpperCamelCase__ = [65, 18536, 2260, 101, 66] self.assertListEqual(__lowerCAmelCase , self.big_tokenizer.encode(__lowerCAmelCase ) ) @slow def _lowerCamelCase ( self ): UpperCamelCase__ = ( """This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will""" """ add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth""" ) # fmt: off UpperCamelCase__ = [65, 871, 419, 358, 946, 991, 2521, 452, 358, 1357, 387, 7751, 3536, 112, 985, 456, 126, 865, 938, 5400, 5734, 458, 1368, 467, 786, 2462, 5246, 1159, 633, 865, 4519, 457, 582, 852, 2557, 427, 916, 508, 405, 34324, 497, 391, 408, 11342, 1244, 385, 100, 938, 985, 456, 574, 362, 12597, 3200, 3129, 1172, 66] # noqa: E231 # fmt: on self.assertListEqual(__lowerCAmelCase , self.big_tokenizer.encode(__lowerCAmelCase ) ) @require_torch @slow def _lowerCamelCase ( self ): import torch from transformers import BigBirdConfig, BigBirdModel # Build sequence UpperCamelCase__ = list(self.big_tokenizer.get_vocab().keys() )[:10] UpperCamelCase__ = """ """.join(__lowerCAmelCase ) UpperCamelCase__ = self.big_tokenizer.encode_plus(__lowerCAmelCase , return_tensors="""pt""" , return_token_type_ids=__lowerCAmelCase ) UpperCamelCase__ = self.big_tokenizer.batch_encode_plus( [sequence + """ """ + sequence] , return_tensors="""pt""" , return_token_type_ids=__lowerCAmelCase ) UpperCamelCase__ = BigBirdConfig(attention_type="""original_full""" ) UpperCamelCase__ = BigBirdModel(__lowerCAmelCase ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**__lowerCAmelCase ) model(**__lowerCAmelCase ) @slow def _lowerCamelCase ( self ): UpperCamelCase__ = BigBirdTokenizer.from_pretrained("""google/bigbird-roberta-base""" ) UpperCamelCase__ = tokenizer.decode(tokenizer("""Paris is the [MASK].""" ).input_ids ) self.assertTrue(decoded_text == """[CLS] Paris is the[MASK].[SEP]""" ) @slow def _lowerCamelCase ( self ): # fmt: off UpperCamelCase__ = {"""input_ids""": [[65, 39286, 458, 36335, 2001, 456, 13073, 13266, 455, 113, 7746, 1741, 11157, 391, 13073, 13266, 455, 113, 3967, 35412, 113, 4936, 109, 3870, 2377, 113, 30084, 45720, 458, 134, 17496, 112, 503, 11672, 113, 118, 112, 5665, 13347, 38687, 112, 1496, 31389, 112, 3268, 47264, 134, 962, 112, 16377, 8035, 23130, 430, 12169, 15518, 28592, 458, 146, 41697, 109, 391, 12169, 15518, 16689, 458, 146, 41358, 109, 452, 726, 4034, 111, 763, 35412, 5082, 388, 1903, 111, 9051, 391, 2870, 48918, 1900, 1123, 550, 998, 112, 9586, 15985, 455, 391, 410, 22955, 37636, 114, 66], [65, 448, 17496, 419, 3663, 385, 763, 113, 27533, 2870, 3283, 13043, 1639, 24713, 523, 656, 24013, 18550, 2521, 517, 27014, 21244, 420, 1212, 1465, 391, 927, 4833, 388, 578, 11786, 114, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [65, 484, 2169, 7687, 21932, 18146, 726, 363, 17032, 3391, 114, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=__lowerCAmelCase , model_name="""google/bigbird-roberta-base""" , revision="""215c99f1600e06f83acce68422f2035b2b5c3510""" , )
87
from datetime import datetime as dt import os from github import Github UpperCamelCase__ = [ "good first issue", "good second issue", "good difficult issue", "feature request", "new model", "wip", ] def _UpperCamelCase (): """simple docstring""" UpperCamelCase__ = Github(os.environ["""GITHUB_TOKEN"""] ) UpperCamelCase__ = g.get_repo("""huggingface/transformers""" ) UpperCamelCase__ = repo.get_issues(state="""open""" ) for issue in open_issues: UpperCamelCase__ = sorted([comment for comment in issue.get_comments()] , key=lambda a__ : i.created_at , reverse=a__ ) UpperCamelCase__ = comments[0] if len(a__ ) > 0 else None if ( last_comment is not None and last_comment.user.login == "github-actions[bot]" and (dt.utcnow() - issue.updated_at).days > 7 and (dt.utcnow() - issue.created_at).days >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # print(f"Would close issue {issue.number} since it has been 7 days of inactivity since bot mention.") issue.edit(state="""closed""" ) elif ( (dt.utcnow() - issue.updated_at).days > 23 and (dt.utcnow() - issue.created_at).days >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # print(f"Would add stale comment to {issue.number}") issue.create_comment( """This issue has been automatically marked as stale because it has not had """ """recent activity. If you think this still needs to be addressed """ """please comment on this thread.\n\nPlease note that issues that do not follow the """ """[contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) """ """are likely to be ignored.""" ) if __name__ == "__main__": main()
87
1
"""simple docstring""" class lowerCAmelCase__ : def __init__( self : Optional[int] , _lowerCamelCase : Union[str, Any] , _lowerCamelCase : Union[str, Any] , _lowerCamelCase : Optional[Any] ): _snake_case = name _snake_case = value _snake_case = weight def __repr__( self : List[Any] ): return f'''{self.__class__.__name__}({self.name}, {self.value}, {self.weight})''' def lowercase ( self : Union[str, Any] ): return self.value def lowercase ( self : Any ): return self.name def lowercase ( self : str ): return self.weight def lowercase ( self : Optional[int] ): return self.value / self.weight def _UpperCAmelCase ( __lowerCamelCase : Optional[int] , __lowerCamelCase : str , __lowerCamelCase : str ) -> Optional[Any]: _snake_case = [] for i in range(len(lowercase__ ) ): menu.append(Things(name[i] , value[i] , weight[i] ) ) return menu def _UpperCAmelCase ( __lowerCamelCase : Any , __lowerCamelCase : List[Any] , __lowerCamelCase : List[str] ) -> List[Any]: _snake_case = sorted(lowercase__ , key=lowercase__ , reverse=lowercase__ ) _snake_case = [] _snake_case = 0.0, 0.0 for i in range(len(lowercase__ ) ): if (total_cost + items_copy[i].get_weight()) <= max_cost: result.append(items_copy[i] ) total_cost += items_copy[i].get_weight() total_value += items_copy[i].get_value() return (result, total_value) def _UpperCAmelCase ( ) -> Optional[Any]: pass if __name__ == "__main__": import doctest doctest.testmod()
288
from collections import Counter import numpy as np from sklearn import datasets from sklearn.model_selection import train_test_split __lowerCAmelCase : List[Any] =datasets.load_iris() __lowerCAmelCase : Tuple =np.array(data['data']) __lowerCAmelCase : Dict =np.array(data['target']) __lowerCAmelCase : List[str] =data['target_names'] __lowerCAmelCase ,__lowerCAmelCase ,__lowerCAmelCase ,__lowerCAmelCase : str =train_test_split(X, y) def _UpperCamelCase ( lowercase__ , lowercase__ ): return np.linalg.norm(np.array(lowercase__ ) - np.array(lowercase__ ) ) def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__=5 ): __SCREAMING_SNAKE_CASE : Optional[int] = zip(lowercase__ , lowercase__ ) # List of distances of all points from the point to be classified __SCREAMING_SNAKE_CASE : Dict = [] for data_point in data: __SCREAMING_SNAKE_CASE : Tuple = euclidean_distance(data_point[0] , lowercase__ ) distances.append((distance, data_point[1]) ) # Choosing 'k' points with the least distances. __SCREAMING_SNAKE_CASE : int = [i[1] for i in sorted(lowercase__ )[:k]] # Most commonly occurring class among them # is the class into which the point is classified __SCREAMING_SNAKE_CASE : Any = Counter(lowercase__ ).most_common(1 )[0][0] return classes[result] if __name__ == "__main__": print(classifier(X_train, y_train, classes, [4.4, 3.1, 1.3, 1.4]))
9
0
import shutil import tempfile import unittest from unittest.mock import patch from transformers import ( DefaultFlowCallback, IntervalStrategy, PrinterCallback, ProgressCallback, Trainer, TrainerCallback, TrainingArguments, is_torch_available, ) from transformers.testing_utils import require_torch if is_torch_available(): from transformers.trainer import DEFAULT_CALLBACKS from .test_trainer import RegressionDataset, RegressionModelConfig, RegressionPreTrainedModel class UpperCAmelCase_ ( SCREAMING_SNAKE_CASE__ ): '''simple docstring''' def __init__( self ) -> Dict: snake_case_ : int = [] def _lowerCAmelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> Any: self.events.append("on_init_end" ) def _lowerCAmelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> Dict: self.events.append("on_train_begin" ) def _lowerCAmelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> Tuple: self.events.append("on_train_end" ) def _lowerCAmelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> List[Any]: self.events.append("on_epoch_begin" ) def _lowerCAmelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> Union[str, Any]: self.events.append("on_epoch_end" ) def _lowerCAmelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> List[str]: self.events.append("on_step_begin" ) def _lowerCAmelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> Tuple: self.events.append("on_step_end" ) def _lowerCAmelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> List[str]: self.events.append("on_evaluate" ) def _lowerCAmelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> Tuple: self.events.append("on_predict" ) def _lowerCAmelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> Union[str, Any]: self.events.append("on_save" ) def _lowerCAmelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> List[str]: self.events.append("on_log" ) def _lowerCAmelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> Optional[int]: self.events.append("on_prediction_step" ) @require_torch class UpperCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def _lowerCAmelCase ( self ) -> Any: snake_case_ : Optional[int] = tempfile.mkdtemp() def _lowerCAmelCase ( self ) -> Optional[Any]: shutil.rmtree(self.output_dir ) def _lowerCAmelCase ( self , _SCREAMING_SNAKE_CASE=0 , _SCREAMING_SNAKE_CASE=0 , _SCREAMING_SNAKE_CASE=64 , _SCREAMING_SNAKE_CASE=64 , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=False , **_SCREAMING_SNAKE_CASE ) -> Dict: # disable_tqdm in TrainingArguments has a flaky default since it depends on the level of logging. We make sure # its set to False since the tests later on depend on its value. snake_case_ : Any = RegressionDataset(length=_SCREAMING_SNAKE_CASE ) snake_case_ : Dict = RegressionDataset(length=_SCREAMING_SNAKE_CASE ) snake_case_ : List[Any] = RegressionModelConfig(a=_SCREAMING_SNAKE_CASE , b=_SCREAMING_SNAKE_CASE ) snake_case_ : List[str] = RegressionPreTrainedModel(_SCREAMING_SNAKE_CASE ) snake_case_ : Tuple = TrainingArguments(self.output_dir , disable_tqdm=_SCREAMING_SNAKE_CASE , report_to=[] , **_SCREAMING_SNAKE_CASE ) return Trainer( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , train_dataset=_SCREAMING_SNAKE_CASE , eval_dataset=_SCREAMING_SNAKE_CASE , callbacks=_SCREAMING_SNAKE_CASE , ) def _lowerCAmelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[Any]: self.assertEqual(len(_SCREAMING_SNAKE_CASE ) , len(_SCREAMING_SNAKE_CASE ) ) # Order doesn't matter snake_case_ : List[str] = sorted(_SCREAMING_SNAKE_CASE , key=lambda _SCREAMING_SNAKE_CASE : cb.__name__ if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else cb.__class__.__name__ ) snake_case_ : List[str] = sorted(_SCREAMING_SNAKE_CASE , key=lambda _SCREAMING_SNAKE_CASE : cb.__name__ if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else cb.__class__.__name__ ) for cba, cba in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) and isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): self.assertEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) elif isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) and not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): self.assertEqual(_SCREAMING_SNAKE_CASE , cba.__class__ ) elif not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) and isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): self.assertEqual(cba.__class__ , _SCREAMING_SNAKE_CASE ) else: self.assertEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def _lowerCAmelCase ( self , _SCREAMING_SNAKE_CASE ) -> int: snake_case_ : int = ["on_init_end", "on_train_begin"] snake_case_ : Any = 0 snake_case_ : Dict = len(trainer.get_eval_dataloader() ) snake_case_ : Tuple = ["on_prediction_step"] * len(trainer.get_eval_dataloader() ) + ["on_log", "on_evaluate"] for _ in range(trainer.state.num_train_epochs ): expected_events.append("on_epoch_begin" ) for _ in range(_SCREAMING_SNAKE_CASE ): step += 1 expected_events += ["on_step_begin", "on_step_end"] if step % trainer.args.logging_steps == 0: expected_events.append("on_log" ) if trainer.args.evaluation_strategy == IntervalStrategy.STEPS and step % trainer.args.eval_steps == 0: expected_events += evaluation_events.copy() if step % trainer.args.save_steps == 0: expected_events.append("on_save" ) expected_events.append("on_epoch_end" ) if trainer.args.evaluation_strategy == IntervalStrategy.EPOCH: expected_events += evaluation_events.copy() expected_events += ["on_log", "on_train_end"] return expected_events def _lowerCAmelCase ( self ) -> int: snake_case_ : Dict = self.get_trainer() snake_case_ : Any = DEFAULT_CALLBACKS.copy() + [ProgressCallback] self.check_callbacks_equality(trainer.callback_handler.callbacks , _SCREAMING_SNAKE_CASE ) # Callbacks passed at init are added to the default callbacks snake_case_ : int = self.get_trainer(callbacks=[MyTestTrainerCallback] ) expected_callbacks.append(_SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , _SCREAMING_SNAKE_CASE ) # TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback snake_case_ : Optional[int] = self.get_trainer(disable_tqdm=_SCREAMING_SNAKE_CASE ) snake_case_ : Dict = DEFAULT_CALLBACKS.copy() + [PrinterCallback] self.check_callbacks_equality(trainer.callback_handler.callbacks , _SCREAMING_SNAKE_CASE ) def _lowerCAmelCase ( self ) -> Optional[int]: snake_case_ : Tuple = DEFAULT_CALLBACKS.copy() + [ProgressCallback] snake_case_ : List[str] = self.get_trainer() # We can add, pop, or remove by class name trainer.remove_callback(_SCREAMING_SNAKE_CASE ) expected_callbacks.remove(_SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , _SCREAMING_SNAKE_CASE ) snake_case_ : Union[str, Any] = self.get_trainer() snake_case_ : List[Any] = trainer.pop_callback(_SCREAMING_SNAKE_CASE ) self.assertEqual(cb.__class__ , _SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , _SCREAMING_SNAKE_CASE ) trainer.add_callback(_SCREAMING_SNAKE_CASE ) expected_callbacks.insert(0 , _SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , _SCREAMING_SNAKE_CASE ) # We can also add, pop, or remove by instance snake_case_ : str = self.get_trainer() snake_case_ : Tuple = trainer.callback_handler.callbacks[0] trainer.remove_callback(_SCREAMING_SNAKE_CASE ) expected_callbacks.remove(_SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , _SCREAMING_SNAKE_CASE ) snake_case_ : str = self.get_trainer() snake_case_ : List[Any] = trainer.callback_handler.callbacks[0] snake_case_ : List[str] = trainer.pop_callback(_SCREAMING_SNAKE_CASE ) self.assertEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , _SCREAMING_SNAKE_CASE ) trainer.add_callback(_SCREAMING_SNAKE_CASE ) expected_callbacks.insert(0 , _SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , _SCREAMING_SNAKE_CASE ) def _lowerCAmelCase ( self ) -> Union[str, Any]: import warnings # XXX: for now ignore scatter_gather warnings in this test since it's not relevant to what's being tested warnings.simplefilter(action="ignore" , category=_SCREAMING_SNAKE_CASE ) snake_case_ : List[str] = self.get_trainer(callbacks=[MyTestTrainerCallback] ) trainer.train() snake_case_ : Optional[Any] = trainer.callback_handler.callbacks[-2].events self.assertEqual(_SCREAMING_SNAKE_CASE , self.get_expected_events(_SCREAMING_SNAKE_CASE ) ) # Independent log/save/eval snake_case_ : int = self.get_trainer(callbacks=[MyTestTrainerCallback] , logging_steps=5 ) trainer.train() snake_case_ : Tuple = trainer.callback_handler.callbacks[-2].events self.assertEqual(_SCREAMING_SNAKE_CASE , self.get_expected_events(_SCREAMING_SNAKE_CASE ) ) snake_case_ : Any = self.get_trainer(callbacks=[MyTestTrainerCallback] , save_steps=5 ) trainer.train() snake_case_ : Tuple = trainer.callback_handler.callbacks[-2].events self.assertEqual(_SCREAMING_SNAKE_CASE , self.get_expected_events(_SCREAMING_SNAKE_CASE ) ) snake_case_ : Dict = self.get_trainer(callbacks=[MyTestTrainerCallback] , eval_steps=5 , evaluation_strategy="steps" ) trainer.train() snake_case_ : List[Any] = trainer.callback_handler.callbacks[-2].events self.assertEqual(_SCREAMING_SNAKE_CASE , self.get_expected_events(_SCREAMING_SNAKE_CASE ) ) snake_case_ : List[str] = self.get_trainer(callbacks=[MyTestTrainerCallback] , evaluation_strategy="epoch" ) trainer.train() snake_case_ : Union[str, Any] = trainer.callback_handler.callbacks[-2].events self.assertEqual(_SCREAMING_SNAKE_CASE , self.get_expected_events(_SCREAMING_SNAKE_CASE ) ) # A bit of everything snake_case_ : Any = self.get_trainer( callbacks=[MyTestTrainerCallback] , logging_steps=3 , save_steps=10 , eval_steps=5 , evaluation_strategy="steps" , ) trainer.train() snake_case_ : List[str] = trainer.callback_handler.callbacks[-2].events self.assertEqual(_SCREAMING_SNAKE_CASE , self.get_expected_events(_SCREAMING_SNAKE_CASE ) ) # warning should be emitted for duplicated callbacks with patch("transformers.trainer_callback.logger.warning" ) as warn_mock: snake_case_ : int = self.get_trainer( callbacks=[MyTestTrainerCallback, MyTestTrainerCallback] , ) assert str(_SCREAMING_SNAKE_CASE ) in warn_mock.call_args[0][0]
355
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import center_crop, normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL lowercase : Union[str, Any] = logging.get_logger(__name__) class UpperCAmelCase_ ( SCREAMING_SNAKE_CASE__ ): '''simple docstring''' A : List[Any] = ['pixel_values'] def __init__( self , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = PIL.Image.BICUBIC , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = 1 / 255 , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , ) -> None: super().__init__(**_SCREAMING_SNAKE_CASE ) snake_case_ : Optional[Any] = size if size is not None else {"height": 256, "width": 256} snake_case_ : int = get_size_dict(_SCREAMING_SNAKE_CASE ) snake_case_ : Tuple = crop_size if crop_size is not None else {"height": 224, "width": 224} snake_case_ : Dict = get_size_dict(_SCREAMING_SNAKE_CASE , param_name="crop_size" ) snake_case_ : str = do_resize snake_case_ : Tuple = size snake_case_ : Tuple = resample snake_case_ : Dict = do_center_crop snake_case_ : Any = crop_size snake_case_ : int = do_rescale snake_case_ : Union[str, Any] = rescale_factor snake_case_ : Optional[int] = do_normalize snake_case_ : Union[str, Any] = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN snake_case_ : Optional[Any] = image_std if image_std is not None else IMAGENET_STANDARD_STD def _lowerCAmelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = PIL.Image.BICUBIC , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , ) -> np.ndarray: snake_case_ : List[Any] = get_size_dict(_SCREAMING_SNAKE_CASE ) if "height" not in size or "width" not in size: raise ValueError(f'''The size dictionary must have keys \'height\' and \'width\'. Got {size.keys()}''' ) return resize( _SCREAMING_SNAKE_CASE , size=(size["height"], size["width"]) , resample=_SCREAMING_SNAKE_CASE , data_format=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) def _lowerCAmelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , ) -> np.ndarray: snake_case_ : str = get_size_dict(_SCREAMING_SNAKE_CASE ) if "height" not in size or "width" not in size: raise ValueError(f'''The size dictionary must have keys \'height\' and \'width\'. Got {size.keys()}''' ) return center_crop(_SCREAMING_SNAKE_CASE , size=(size["height"], size["width"]) , data_format=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) def _lowerCAmelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , ) -> Optional[int]: return rescale(_SCREAMING_SNAKE_CASE , scale=_SCREAMING_SNAKE_CASE , data_format=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) def _lowerCAmelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , ) -> np.ndarray: return normalize(_SCREAMING_SNAKE_CASE , mean=_SCREAMING_SNAKE_CASE , std=_SCREAMING_SNAKE_CASE , data_format=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) def _lowerCAmelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = ChannelDimension.FIRST , **_SCREAMING_SNAKE_CASE , ) -> PIL.Image.Image: snake_case_ : Optional[Any] = do_resize if do_resize is not None else self.do_resize snake_case_ : Tuple = resample if resample is not None else self.resample snake_case_ : Optional[int] = do_center_crop if do_center_crop is not None else self.do_center_crop snake_case_ : Union[str, Any] = do_rescale if do_rescale is not None else self.do_rescale snake_case_ : Optional[Any] = rescale_factor if rescale_factor is not None else self.rescale_factor snake_case_ : Tuple = do_normalize if do_normalize is not None else self.do_normalize snake_case_ : Union[str, Any] = image_mean if image_mean is not None else self.image_mean snake_case_ : Optional[int] = image_std if image_std is not None else self.image_std snake_case_ : Optional[Any] = size if size is not None else self.size snake_case_ : int = get_size_dict(_SCREAMING_SNAKE_CASE ) snake_case_ : str = crop_size if crop_size is not None else self.crop_size snake_case_ : Optional[Any] = get_size_dict(_SCREAMING_SNAKE_CASE , param_name="crop_size" ) snake_case_ : 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." ) if do_resize and size is None or resample is None: raise ValueError("Size and resample must be specified if do_resize is True." ) if do_center_crop and crop_size is None: raise ValueError("Crop size must be specified if do_center_crop is True." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("Image mean and std must be specified if do_normalize is True." ) # All transformations expect numpy arrays. snake_case_ : Optional[int] = [to_numpy_array(_SCREAMING_SNAKE_CASE ) for image in images] if do_resize: snake_case_ : Optional[Any] = [self.resize(image=_SCREAMING_SNAKE_CASE , size=_SCREAMING_SNAKE_CASE , resample=_SCREAMING_SNAKE_CASE ) for image in images] if do_center_crop: snake_case_ : List[Any] = [self.center_crop(image=_SCREAMING_SNAKE_CASE , size=_SCREAMING_SNAKE_CASE ) for image in images] if do_rescale: snake_case_ : Optional[int] = [self.rescale(image=_SCREAMING_SNAKE_CASE , scale=_SCREAMING_SNAKE_CASE ) for image in images] if do_normalize: snake_case_ : List[str] = [self.normalize(image=_SCREAMING_SNAKE_CASE , mean=_SCREAMING_SNAKE_CASE , std=_SCREAMING_SNAKE_CASE ) for image in images] snake_case_ : int = [to_channel_dimension_format(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) for image in images] snake_case_ : List[str] = {"pixel_values": images} return BatchFeature(data=_SCREAMING_SNAKE_CASE , tensor_type=_SCREAMING_SNAKE_CASE )
36
0
"""simple docstring""" from math import loga def a_ ( _lowerCAmelCase : int ): '''simple docstring''' if a < 0: raise ValueError('Input value must be a positive integer' ) elif isinstance(_lowerCAmelCase , _lowerCAmelCase ): 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()
77
import warnings from transformers import AutoTokenizer from transformers.utils import is_torch_available from transformers.utils.generic import ExplicitEnum from ...processing_utils import ProcessorMixin if is_torch_available(): import torch class lowercase__ ( _UpperCAmelCase ): a_ ="""char""" a_ ="""bpe""" a_ ="""wp""" a_ = (DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE) class lowercase__ ( _UpperCAmelCase ): a_ =["""image_processor""", """char_tokenizer"""] a_ ="""ViTImageProcessor""" a_ ="""MgpstrTokenizer""" def __init__( self , __UpperCAmelCase=None , __UpperCAmelCase=None , **__UpperCAmelCase )-> str: '''simple docstring''' lowerCAmelCase__ = None if "feature_extractor" in kwargs: warnings.warn( "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`" " instead." , __UpperCAmelCase , ) lowerCAmelCase__ = kwargs.pop("feature_extractor" ) lowerCAmelCase__ = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("You need to specify an `image_processor`." ) if tokenizer is None: raise ValueError("You need to specify a `tokenizer`." ) lowerCAmelCase__ = tokenizer lowerCAmelCase__ = AutoTokenizer.from_pretrained("gpt2" ) lowerCAmelCase__ = AutoTokenizer.from_pretrained("bert-base-uncased" ) super().__init__(__UpperCAmelCase , __UpperCAmelCase ) def __call__( self , __UpperCAmelCase=None , __UpperCAmelCase=None , __UpperCAmelCase=None , **__UpperCAmelCase )-> List[Any]: '''simple docstring''' if images is None and text is None: raise ValueError("You need to specify either an `images` or `text` input to process." ) if images is not None: lowerCAmelCase__ = self.image_processor(__UpperCAmelCase , return_tensors=__UpperCAmelCase , **__UpperCAmelCase ) if text is not None: lowerCAmelCase__ = self.char_tokenizer(__UpperCAmelCase , return_tensors=__UpperCAmelCase , **__UpperCAmelCase ) if text is None: return inputs elif images is None: return encodings else: lowerCAmelCase__ = encodings["input_ids"] return inputs def UpperCAmelCase ( self , __UpperCAmelCase )-> Optional[int]: '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ = sequences lowerCAmelCase__ = char_preds.size(0 ) lowerCAmelCase__ , lowerCAmelCase__ = self._decode_helper(__UpperCAmelCase , "char" ) lowerCAmelCase__ , lowerCAmelCase__ = self._decode_helper(__UpperCAmelCase , "bpe" ) lowerCAmelCase__ , lowerCAmelCase__ = self._decode_helper(__UpperCAmelCase , "wp" ) lowerCAmelCase__ = [] lowerCAmelCase__ = [] for i in range(__UpperCAmelCase ): lowerCAmelCase__ = [char_scores[i], bpe_scores[i], wp_scores[i]] lowerCAmelCase__ = [char_strs[i], bpe_strs[i], wp_strs[i]] lowerCAmelCase__ = scores.index(max(__UpperCAmelCase ) ) final_strs.append(strs[max_score_index] ) final_scores.append(scores[max_score_index] ) lowerCAmelCase__ = {} lowerCAmelCase__ = final_strs lowerCAmelCase__ = final_scores lowerCAmelCase__ = char_strs lowerCAmelCase__ = bpe_strs lowerCAmelCase__ = wp_strs return out def UpperCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase )-> Optional[int]: '''simple docstring''' if format == DecodeType.CHARACTER: lowerCAmelCase__ = self.char_decode lowerCAmelCase__ = 1 lowerCAmelCase__ = "[s]" elif format == DecodeType.BPE: lowerCAmelCase__ = self.bpe_decode lowerCAmelCase__ = 2 lowerCAmelCase__ = "#" elif format == DecodeType.WORDPIECE: lowerCAmelCase__ = self.wp_decode lowerCAmelCase__ = 102 lowerCAmelCase__ = "[SEP]" else: raise ValueError(F"Format {format} is not supported." ) lowerCAmelCase__ , lowerCAmelCase__ = [], [] lowerCAmelCase__ = pred_logits.size(0 ) lowerCAmelCase__ = pred_logits.size(1 ) lowerCAmelCase__ , lowerCAmelCase__ = pred_logits.topk(1 , dim=-1 , largest=__UpperCAmelCase , sorted=__UpperCAmelCase ) lowerCAmelCase__ = preds_index.view(-1 , __UpperCAmelCase )[:, 1:] lowerCAmelCase__ = decoder(__UpperCAmelCase ) lowerCAmelCase__ , lowerCAmelCase__ = torch.nn.functional.softmax(__UpperCAmelCase , dim=2 ).max(dim=2 ) lowerCAmelCase__ = preds_max_prob[:, 1:] for index in range(__UpperCAmelCase ): lowerCAmelCase__ = preds_str[index].find(__UpperCAmelCase ) lowerCAmelCase__ = preds_str[index][:pred_eos] lowerCAmelCase__ = preds_index[index].cpu().tolist() lowerCAmelCase__ = pred_index.index(__UpperCAmelCase ) if eos_token in pred_index else -1 lowerCAmelCase__ = preds_max_prob[index][: pred_eos_index + 1] lowerCAmelCase__ = pred_max_prob.cumprod(dim=0 )[-1] if pred_max_prob.nelement() != 0 else 0.0 dec_strs.append(__UpperCAmelCase ) conf_scores.append(__UpperCAmelCase ) return dec_strs, conf_scores def UpperCAmelCase ( self , __UpperCAmelCase )-> Optional[Any]: '''simple docstring''' lowerCAmelCase__ = [seq.replace(" " , "" ) for seq in self.char_tokenizer.batch_decode(__UpperCAmelCase )] return decode_strs def UpperCAmelCase ( self , __UpperCAmelCase )-> Optional[Any]: '''simple docstring''' return self.bpe_tokenizer.batch_decode(__UpperCAmelCase ) def UpperCAmelCase ( self , __UpperCAmelCase )-> Optional[int]: '''simple docstring''' lowerCAmelCase__ = [seq.replace(" " , "" ) for seq in self.wp_tokenizer.batch_decode(__UpperCAmelCase )] return decode_strs
340
0
'''simple docstring''' from typing import Callable, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging snake_case_ : int = logging.get_logger(__name__) snake_case_ : Optional[Any] = { 'microsoft/xprophetnet-large-wiki100-cased': ( 'https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/config.json' ), } class lowercase__ ( lowercase ): lowercase__ = """xlm-prophetnet""" lowercase__ = ["""past_key_values"""] lowercase__ = { """num_attention_heads""": """num_encoder_attention_heads""", } def __init__( self : str ,lowerCamelCase__ : Optional[float] = 0.1 ,lowerCamelCase__ : Optional[Union[str, Callable]] = "gelu" ,lowerCamelCase__ : Optional[int] = 30522 ,lowerCamelCase__ : Optional[int] = 1024 ,lowerCamelCase__ : Optional[int] = 4096 ,lowerCamelCase__ : Optional[int] = 12 ,lowerCamelCase__ : Optional[int] = 16 ,lowerCamelCase__ : Optional[int] = 4096 ,lowerCamelCase__ : Optional[int] = 12 ,lowerCamelCase__ : Optional[int] = 16 ,lowerCamelCase__ : Optional[float] = 0.1 ,lowerCamelCase__ : Optional[float] = 0.1 ,lowerCamelCase__ : Optional[int] = 512 ,lowerCamelCase__ : Optional[float] = 0.0_2 ,lowerCamelCase__ : Optional[bool] = True ,lowerCamelCase__ : Optional[bool] = True ,lowerCamelCase__ : Optional[int] = 0 ,lowerCamelCase__ : Optional[int] = 2 ,lowerCamelCase__ : Optional[int] = 32 ,lowerCamelCase__ : Optional[int] = 128 ,lowerCamelCase__ : Optional[bool] = False ,lowerCamelCase__ : Optional[float] = 0.0 ,lowerCamelCase__ : Optional[bool] = True ,lowerCamelCase__ : Optional[int] = 0 ,lowerCamelCase__ : Optional[int] = 1 ,lowerCamelCase__ : Optional[int] = 2 ,**lowerCamelCase__ : Optional[int] ,): '''simple docstring''' _UpperCamelCase : Tuple = vocab_size _UpperCamelCase : Tuple = hidden_size _UpperCamelCase : Tuple = encoder_ffn_dim _UpperCamelCase : int = num_encoder_layers _UpperCamelCase : int = num_encoder_attention_heads _UpperCamelCase : List[str] = decoder_ffn_dim _UpperCamelCase : Union[str, Any] = num_decoder_layers _UpperCamelCase : List[Any] = num_decoder_attention_heads _UpperCamelCase : Union[str, Any] = max_position_embeddings _UpperCamelCase : Any = init_std # Normal(0, this parameter) _UpperCamelCase : str = activation_function # parameters for xlmprophetnet _UpperCamelCase : int = ngram _UpperCamelCase : str = num_buckets _UpperCamelCase : Tuple = relative_max_distance _UpperCamelCase : int = disable_ngram_loss _UpperCamelCase : Union[str, Any] = eps # 3 Types of Dropout _UpperCamelCase : Optional[int] = attention_dropout _UpperCamelCase : Dict = activation_dropout _UpperCamelCase : Any = dropout _UpperCamelCase : Dict = use_cache super().__init__( pad_token_id=lowerCamelCase__ ,bos_token_id=lowerCamelCase__ ,eos_token_id=lowerCamelCase__ ,is_encoder_decoder=lowerCamelCase__ ,add_cross_attention=lowerCamelCase__ ,decoder_start_token_id=lowerCamelCase__ ,**lowerCamelCase__ ,) @property def UpperCamelCase_ ( self : Optional[Any] ): '''simple docstring''' return self.num_encoder_layers + self.num_decoder_layers @num_hidden_layers.setter def UpperCamelCase_ ( self : Optional[int] ,lowerCamelCase__ : Optional[Any] ): '''simple docstring''' raise NotImplementedError( 'This model does not support the setting of `num_hidden_layers`. Please set `num_encoder_layers` and' ' `num_decoder_layers`.' )
359
'''simple docstring''' from abc import ABC, abstractmethod from argparse import ArgumentParser class lowercase__ ( lowercase ): @staticmethod @abstractmethod def UpperCamelCase_ ( lowerCamelCase__ : ArgumentParser ): '''simple docstring''' raise NotImplementedError() @abstractmethod def UpperCamelCase_ ( self : Any ): '''simple docstring''' raise NotImplementedError()
236
0
"""simple docstring""" def lowercase ( _snake_case : Optional[int] , _snake_case : List[str] ) ->int: """simple docstring""" if len(UpperCamelCase_ ) != len(UpperCamelCase_ ): raise ValueError('''String lengths must match!''' ) __snake_case : Union[str, Any] = 0 for chara, chara in zip(UpperCamelCase_ , UpperCamelCase_ ): if chara != chara: count += 1 return count if __name__ == "__main__": import doctest doctest.testmod()
102
from __future__ import annotations import inspect import unittest import numpy as np from transformers import ResNetConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFResNetForImageClassification, TFResNetModel from transformers.models.resnet.modeling_tf_resnet import TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class SCREAMING_SNAKE_CASE_ : def __init__( self : Optional[int] , lowerCamelCase_ : Optional[Any] , lowerCamelCase_ : str=3 , lowerCamelCase_ : Tuple=32 , lowerCamelCase_ : List[str]=3 , lowerCamelCase_ : Optional[int]=10 , lowerCamelCase_ : List[str]=[10, 20, 30, 40] , lowerCamelCase_ : Tuple=[1, 1, 2, 1] , lowerCamelCase_ : Dict=True , lowerCamelCase_ : str=True , lowerCamelCase_ : Tuple="relu" , lowerCamelCase_ : List[str]=3 , lowerCamelCase_ : Dict=None , ): """simple docstring""" UpperCamelCase = parent UpperCamelCase = batch_size UpperCamelCase = image_size UpperCamelCase = num_channels UpperCamelCase = embeddings_size UpperCamelCase = hidden_sizes UpperCamelCase = depths UpperCamelCase = is_training UpperCamelCase = use_labels UpperCamelCase = hidden_act UpperCamelCase = num_labels UpperCamelCase = scope UpperCamelCase = len(lowerCamelCase_ ) def lowerCamelCase_ ( self : Dict ): """simple docstring""" UpperCamelCase = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCamelCase = None if self.use_labels: UpperCamelCase = ids_tensor([self.batch_size] , self.num_labels ) UpperCamelCase = self.get_config() return config, pixel_values, labels def lowerCamelCase_ ( self : Optional[int] ): """simple docstring""" return ResNetConfig( num_channels=self.num_channels , embeddings_size=self.embeddings_size , hidden_sizes=self.hidden_sizes , depths=self.depths , hidden_act=self.hidden_act , num_labels=self.num_labels , image_size=self.image_size , ) def lowerCamelCase_ ( self : Any , lowerCamelCase_ : List[Any] , lowerCamelCase_ : Optional[int] , lowerCamelCase_ : Tuple ): """simple docstring""" UpperCamelCase = TFResNetModel(config=lowerCamelCase_ ) UpperCamelCase = model(lowerCamelCase_ ) # expected last hidden states: B, C, H // 32, W // 32 self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def lowerCamelCase_ ( self : List[Any] , lowerCamelCase_ : List[str] , lowerCamelCase_ : str , lowerCamelCase_ : Optional[int] ): """simple docstring""" UpperCamelCase = self.num_labels UpperCamelCase = TFResNetForImageClassification(lowerCamelCase_ ) UpperCamelCase = model(lowerCamelCase_ , labels=lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowerCamelCase_ ( self : int ): """simple docstring""" UpperCamelCase = self.prepare_config_and_inputs() UpperCamelCase , UpperCamelCase , UpperCamelCase = config_and_inputs UpperCamelCase = {"""pixel_values""": pixel_values} return config, inputs_dict @require_tf class SCREAMING_SNAKE_CASE_ ( __lowerCAmelCase , __lowerCAmelCase , unittest.TestCase ): __lowerCAmelCase = (TFResNetModel, TFResNetForImageClassification) if is_tf_available() else () __lowerCAmelCase = ( {"""feature-extraction""": TFResNetModel, """image-classification""": TFResNetForImageClassification} if is_tf_available() else {} ) __lowerCAmelCase = False __lowerCAmelCase = False __lowerCAmelCase = False __lowerCAmelCase = False __lowerCAmelCase = False def lowerCamelCase_ ( self : Any ): """simple docstring""" UpperCamelCase = TFResNetModelTester(self ) UpperCamelCase = ConfigTester(self , config_class=lowerCamelCase_ , has_text_modality=lowerCamelCase_ ) def lowerCamelCase_ ( self : List[Any] ): """simple docstring""" self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def lowerCamelCase_ ( self : Union[str, Any] ): """simple docstring""" return @unittest.skip(reason="""ResNet does not use inputs_embeds""" ) def lowerCamelCase_ ( self : Dict ): """simple docstring""" pass @unittest.skip(reason="""ResNet does not support input and output embeddings""" ) def lowerCamelCase_ ( self : Optional[Any] ): """simple docstring""" pass def lowerCamelCase_ ( self : Optional[int] ): """simple docstring""" UpperCamelCase , UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase = model_class(lowerCamelCase_ ) UpperCamelCase = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCamelCase = [*signature.parameters.keys()] UpperCamelCase = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , lowerCamelCase_ ) def lowerCamelCase_ ( self : List[str] ): """simple docstring""" UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase_ ) def lowerCamelCase_ ( self : Union[str, Any] ): """simple docstring""" def check_hidden_states_output(lowerCamelCase_ : Tuple , lowerCamelCase_ : int , lowerCamelCase_ : str ): UpperCamelCase = model_class(lowerCamelCase_ ) UpperCamelCase = model(**self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) UpperCamelCase = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states UpperCamelCase = self.model_tester.num_stages self.assertEqual(len(lowerCamelCase_ ) , expected_num_stages + 1 ) # ResNet's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) UpperCamelCase , UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() UpperCamelCase = ["""basic""", """bottleneck"""] for model_class in self.all_model_classes: for layer_type in layers_type: UpperCamelCase = layer_type UpperCamelCase = True check_hidden_states_output(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] UpperCamelCase = True check_hidden_states_output(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) def lowerCamelCase_ ( self : List[Any] ): """simple docstring""" UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_ ) @slow def lowerCamelCase_ ( self : Any ): """simple docstring""" for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase = TFResNetModel.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) def lowercase( ) -> Any: '''simple docstring''' UpperCamelCase = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_tf @require_vision class SCREAMING_SNAKE_CASE_ ( unittest.TestCase ): @cached_property def lowerCamelCase_ ( self : Dict ): """simple docstring""" return ( AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def lowerCamelCase_ ( self : Dict ): """simple docstring""" UpperCamelCase = TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) UpperCamelCase = self.default_image_processor UpperCamelCase = prepare_img() UpperCamelCase = image_processor(images=lowerCamelCase_ , return_tensors="""tf""" ) # forward pass UpperCamelCase = model(**lowerCamelCase_ ) # verify the logits UpperCamelCase = tf.TensorShape((1, 1000) ) self.assertEqual(outputs.logits.shape , lowerCamelCase_ ) UpperCamelCase = tf.constant([-1_1.1_0_6_9, -9.7_8_7_7, -8.3_7_7_7] ) self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() , lowerCamelCase_ , atol=1E-4 ) )
343
0
"""simple docstring""" from typing import List, Optional, Tuple, Union import torch from ...schedulers import DDIMScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class _snake_case ( a__ ): def __init__( self : Optional[Any] , UpperCAmelCase : Optional[Any] , UpperCAmelCase : str ): super().__init__() # make sure scheduler can always be converted to DDIM __lowerCamelCase : Dict = DDIMScheduler.from_config(scheduler.config ) self.register_modules(unet=UpperCAmelCase , scheduler=UpperCAmelCase ) @torch.no_grad() def __call__( self : str , UpperCAmelCase : int = 1 , UpperCAmelCase : Optional[Union[torch.Generator, List[torch.Generator]]] = None , UpperCAmelCase : float = 0.0 , UpperCAmelCase : int = 50 , UpperCAmelCase : Optional[bool] = None , UpperCAmelCase : Optional[str] = "pil" , UpperCAmelCase : bool = True , ): # Sample gaussian noise to begin loop if isinstance(self.unet.config.sample_size , UpperCAmelCase ): __lowerCamelCase : Any = ( batch_size, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size, ) else: __lowerCamelCase : Dict = (batch_size, self.unet.config.in_channels, *self.unet.config.sample_size) if isinstance(UpperCAmelCase , UpperCAmelCase ) and len(UpperCAmelCase ) != batch_size: raise ValueError( F"""You have passed a list of generators of length {len(UpperCAmelCase )}, but requested an effective batch""" F""" size of {batch_size}. Make sure the batch size matches the length of the generators.""" ) __lowerCamelCase : str = randn_tensor(UpperCAmelCase , generator=UpperCAmelCase , device=self.device , dtype=self.unet.dtype ) # set step values self.scheduler.set_timesteps(UpperCAmelCase ) for t in self.progress_bar(self.scheduler.timesteps ): # 1. predict noise model_output __lowerCamelCase : Any = self.unet(UpperCAmelCase , UpperCAmelCase ).sample # 2. predict previous mean of image x_t-1 and add variance depending on eta # eta corresponds to η in paper and should be between [0, 1] # do x_t -> x_t-1 __lowerCamelCase : Union[str, Any] = self.scheduler.step( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , eta=UpperCAmelCase , use_clipped_model_output=UpperCAmelCase , generator=UpperCAmelCase ).prev_sample __lowerCamelCase : Any = (image / 2 + 0.5).clamp(0 , 1 ) __lowerCamelCase : Optional[Any] = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": __lowerCamelCase : str = self.numpy_to_pil(UpperCAmelCase ) if not return_dict: return (image,) return ImagePipelineOutput(images=UpperCAmelCase )
64
"""simple docstring""" from ...configuration_utils import PretrainedConfig class _snake_case ( a__ ): snake_case__ = "bert-generation" def __init__( self : Optional[int] , UpperCAmelCase : Dict=50358 , UpperCAmelCase : int=1024 , UpperCAmelCase : Optional[int]=24 , UpperCAmelCase : str=16 , UpperCAmelCase : str=4096 , UpperCAmelCase : List[Any]="gelu" , UpperCAmelCase : str=0.1 , UpperCAmelCase : Tuple=0.1 , UpperCAmelCase : Union[str, Any]=512 , UpperCAmelCase : Optional[Any]=0.0_2 , UpperCAmelCase : int=1E-12 , UpperCAmelCase : Tuple=0 , UpperCAmelCase : int=2 , UpperCAmelCase : Optional[int]=1 , UpperCAmelCase : Union[str, Any]="absolute" , UpperCAmelCase : Tuple=True , **UpperCAmelCase : Optional[Any] , ): super().__init__(pad_token_id=UpperCAmelCase , bos_token_id=UpperCAmelCase , eos_token_id=UpperCAmelCase , **UpperCAmelCase ) __lowerCamelCase : Union[str, Any] = vocab_size __lowerCamelCase : List[Any] = hidden_size __lowerCamelCase : Any = num_hidden_layers __lowerCamelCase : List[Any] = num_attention_heads __lowerCamelCase : int = hidden_act __lowerCamelCase : List[str] = intermediate_size __lowerCamelCase : Tuple = hidden_dropout_prob __lowerCamelCase : List[str] = attention_probs_dropout_prob __lowerCamelCase : Optional[Any] = max_position_embeddings __lowerCamelCase : List[Any] = initializer_range __lowerCamelCase : Union[str, Any] = layer_norm_eps __lowerCamelCase : List[str] = position_embedding_type __lowerCamelCase : Optional[Any] = use_cache
64
1
def lowerCAmelCase__ ( a__: int ) -> List[str]: '''simple docstring''' return 1 if digit in (0, 1) else (digit * factorial(digit - 1 )) def lowerCAmelCase__ ( a__: int ) -> str: '''simple docstring''' _UpperCAmelCase = 0 _UpperCAmelCase = number while duplicate > 0: _UpperCAmelCase = divmod(a__ , 1_0 ) fact_sum += factorial(a__ ) return fact_sum == number if __name__ == "__main__": print('''Program to check whether a number is a Krisnamurthy Number or not.''') lowerCAmelCase__ :List[str] = int(input('''Enter number: ''').strip()) print( f'''{number} is {'' if krishnamurthy(number) else 'not '}a Krishnamurthy Number.''' )
329
from maths.prime_check import is_prime def __lowercase ( lowerCamelCase : int ): if not isinstance(lowerCamelCase , lowerCamelCase ): UpperCamelCase_ : List[str] = F"Input value of [number={number}] must be an integer" raise TypeError(lowerCamelCase ) if is_prime(lowerCamelCase ) and is_prime(number + 2 ): return number + 2 else: return -1 if __name__ == "__main__": import doctest doctest.testmod()
175
0
def lowerCAmelCase( __lowerCamelCase ): __a = len(__lowerCamelCase ) __a = sum(__lowerCamelCase ) __a = [[False for x in range(s + 1 )] for y in range(n + 1 )] for i in range(1 , n + 1 ): __a = True for i in range(1 , s + 1 ): __a = False for i in range(1 , n + 1 ): for j in range(1 , s + 1 ): __a = dp[i][j - 1] if arr[i - 1] <= j: __a = dp[i][j] or dp[i - 1][j - arr[i - 1]] for j in range(int(s / 2 ) , -1 , -1 ): if dp[n][j] is True: __a = s - 2 * j break return diff
197
from __future__ import annotations import numpy as np def lowerCAmelCase( __lowerCamelCase ): return np.maximum(0 , __lowerCamelCase ) if __name__ == "__main__": print(np.array(relu([-1, 0, 5]))) # --> [0, 0, 5]
197
1
import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class A ( UpperCamelCase_ ): UpperCamelCase__ : Optional[int] =['image_processor', 'tokenizer'] UpperCamelCase__ : Optional[Any] ='CLIPImageProcessor' UpperCamelCase__ : Any =('CLIPTokenizer', 'CLIPTokenizerFast') def __init__( self : str , lowercase_ : int=None , lowercase_ : Optional[Any]=None , **lowercase_ : Tuple ) -> Dict: """simple docstring""" _lowerCamelCase : str =None if "feature_extractor" in kwargs: warnings.warn( 'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`' ' instead.' , lowercase_ , ) _lowerCamelCase : int =kwargs.pop('feature_extractor' ) _lowerCamelCase : str =image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('You need to specify an `image_processor`.' ) if tokenizer is None: raise ValueError('You need to specify a `tokenizer`.' ) super().__init__(lowercase_ , lowercase_ ) def __call__( self : Tuple , lowercase_ : Optional[Any]=None , lowercase_ : List[str]=None , lowercase_ : Optional[Any]=None , **lowercase_ : str ) -> str: """simple docstring""" if text is None and images is None: raise ValueError('You have to specify either text or images. Both cannot be none.' ) if text is not None: _lowerCamelCase : int =self.tokenizer(lowercase_ , return_tensors=lowercase_ , **lowercase_ ) if images is not None: _lowerCamelCase : Tuple =self.image_processor(lowercase_ , return_tensors=lowercase_ , **lowercase_ ) if text is not None and images is not None: _lowerCamelCase : List[str] =image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**lowercase_ ) , tensor_type=lowercase_ ) def lowerCamelCase ( self : str , *lowercase_ : Optional[int] , **lowercase_ : Any ) -> Dict: """simple docstring""" return self.tokenizer.batch_decode(*lowercase_ , **lowercase_ ) def lowerCamelCase ( self : List[str] , *lowercase_ : Union[str, Any] , **lowercase_ : int ) -> Tuple: """simple docstring""" return self.tokenizer.decode(*lowercase_ , **lowercase_ ) @property def lowerCamelCase ( self : Optional[int] ) -> Any: """simple docstring""" _lowerCamelCase : str =self.tokenizer.model_input_names _lowerCamelCase : Tuple =self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def lowerCamelCase ( self : Tuple ) -> Tuple: """simple docstring""" warnings.warn( '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.' , lowercase_ , ) return self.image_processor_class @property def lowerCamelCase ( self : Tuple ) -> int: """simple docstring""" warnings.warn( '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.' , lowercase_ , ) return self.image_processor
199
import unittest from transformers.models.xlm_prophetnet.tokenization_xlm_prophetnet import SPIECE_UNDERLINE, XLMProphetNetTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin lowerCamelCase = get_tests_dir('fixtures/test_sentencepiece.model') @require_sentencepiece class A ( UpperCamelCase_ , unittest.TestCase ): UpperCamelCase__ : str =XLMProphetNetTokenizer UpperCamelCase__ : Any =False UpperCamelCase__ : Optional[Any] =True def lowerCamelCase ( self : Tuple ) -> Optional[Any]: """simple docstring""" super().setUp() # We have a SentencePiece fixture for testing _lowerCamelCase : Union[str, Any] =XLMProphetNetTokenizer(lowercase_ , keep_accents=lowercase_ ) tokenizer.save_pretrained(self.tmpdirname ) def lowerCamelCase ( self : int ) -> List[str]: """simple docstring""" _lowerCamelCase : Tuple ='[PAD]' _lowerCamelCase : Dict =0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowercase_ ) , lowercase_ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowercase_ ) , lowercase_ ) def lowerCamelCase ( self : Optional[Any] ) -> int: """simple docstring""" _lowerCamelCase : int =list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '[PAD]' ) self.assertEqual(vocab_keys[1] , '[CLS]' ) self.assertEqual(vocab_keys[-1] , 'j' ) self.assertEqual(len(lowercase_ ) , 1012 ) def lowerCamelCase ( self : Optional[int] ) -> Any: """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 1012 ) def lowerCamelCase ( self : Dict ) -> Union[str, Any]: """simple docstring""" _lowerCamelCase : List[Any] =XLMProphetNetTokenizer(lowercase_ , keep_accents=lowercase_ ) _lowerCamelCase : Any =tokenizer.tokenize('This is a test' ) self.assertListEqual(lowercase_ , ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(lowercase_ ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) _lowerCamelCase : Dict =tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( lowercase_ , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '9', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', 'é', '.', ] , ) _lowerCamelCase : Union[str, Any] =tokenizer.convert_tokens_to_ids(lowercase_ ) self.assertListEqual( lowercase_ , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, -9, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, -9, 4] ] , ) _lowerCamelCase : int =tokenizer.convert_ids_to_tokens(lowercase_ ) self.assertListEqual( lowercase_ , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '[UNK]', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', '[UNK]', '.', ] , ) @cached_property def lowerCamelCase ( self : Any ) -> Optional[Any]: """simple docstring""" return XLMProphetNetTokenizer.from_pretrained('microsoft/xprophetnet-large-wiki100-cased' ) @slow def lowerCamelCase ( self : List[str] ) -> Any: """simple docstring""" _lowerCamelCase : Optional[int] ='Hello World!' _lowerCamelCase : Optional[int] =[3_5389, 6672, 49, 2] self.assertListEqual(lowercase_ , self.big_tokenizer.encode(lowercase_ ) ) @slow def lowerCamelCase ( self : Optional[int] ) -> Dict: """simple docstring""" _lowerCamelCase : Dict ={'input_ids': [[1_1073, 8_2783, 18, 26, 8_2783, 549, 5_1540, 248, 1_7209, 1301, 217, 20, 21_5186, 1325, 147, 1_7209, 1301, 217, 20, 5_6370, 53, 12_2020, 20, 1_6477, 27, 8_7355, 4548, 20, 4728, 7_8392, 17, 15_9969, 18, 26, 2_4491, 629, 15, 538, 2_2704, 5439, 15, 2788, 2_4491, 9885, 15, 4_3534, 605, 15, 814, 1_8403, 3_3200, 29, 15, 4_3534, 2_4458, 1_2410, 111, 2_4966, 8_3669, 9637, 14_4068, 26, 850, 2_2346, 27, 147, 2_4966, 8_3669, 8_3490, 26, 3_9113, 735, 27, 689, 656, 2800, 1339, 4600, 53, 12_2020, 11_5785, 34, 816, 1339, 4_6887, 18, 147, 5_3905, 1951, 4_2238, 4_1170, 1_7732, 834, 436, 15, 2_7523, 9_8733, 217, 147, 5542, 4981, 930, 1_7347, 16, 2], [2_0091, 629, 94, 8_2786, 58, 490, 20, 1528, 84, 5_3905, 344, 8_0592, 11_0128, 1_8822, 5267, 1306, 62, 15_2537, 308, 7997, 401, 12_4427, 549, 3_5442, 225, 109, 1_5055, 2_5748, 147, 7119, 4_3712, 34, 767, 13_5366, 18, 16, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [592, 6_3784, 11_9466, 17, 14_7808, 8_8214, 18, 656, 81, 32, 3296, 1_0280, 16, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=lowercase_ , model_name='microsoft/xprophetnet-large-wiki100-cased' , revision='1acad1643ddd54a44df6a1b797ada8373685d90e' , )
199
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) lowercase__ = { "configuration_wav2vec2": ["WAV_2_VEC_2_PRETRAINED_CONFIG_ARCHIVE_MAP", "Wav2Vec2Config"], "feature_extraction_wav2vec2": ["Wav2Vec2FeatureExtractor"], "processing_wav2vec2": ["Wav2Vec2Processor"], "tokenization_wav2vec2": ["Wav2Vec2CTCTokenizer", "Wav2Vec2Tokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ = [ "WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST", "Wav2Vec2ForAudioFrameClassification", "Wav2Vec2ForCTC", "Wav2Vec2ForMaskedLM", "Wav2Vec2ForPreTraining", "Wav2Vec2ForSequenceClassification", "Wav2Vec2ForXVector", "Wav2Vec2Model", "Wav2Vec2PreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ = [ "TF_WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST", "TFWav2Vec2ForCTC", "TFWav2Vec2Model", "TFWav2Vec2PreTrainedModel", "TFWav2Vec2ForSequenceClassification", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ = [ "FlaxWav2Vec2ForCTC", "FlaxWav2Vec2ForPreTraining", "FlaxWav2Vec2Model", "FlaxWav2Vec2PreTrainedModel", ] if TYPE_CHECKING: from .configuration_wavaveca import WAV_2_VEC_2_PRETRAINED_CONFIG_ARCHIVE_MAP, WavaVecaConfig from .feature_extraction_wavaveca import WavaVecaFeatureExtractor from .processing_wavaveca import WavaVecaProcessor from .tokenization_wavaveca import WavaVecaCTCTokenizer, WavaVecaTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_wavaveca import ( WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST, WavaVecaForAudioFrameClassification, WavaVecaForCTC, WavaVecaForMaskedLM, WavaVecaForPreTraining, WavaVecaForSequenceClassification, WavaVecaForXVector, WavaVecaModel, WavaVecaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_wavaveca import ( TF_WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST, TFWavaVecaForCTC, TFWavaVecaForSequenceClassification, TFWavaVecaModel, TFWavaVecaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_wavaveca import ( FlaxWavaVecaForCTC, FlaxWavaVecaForPreTraining, FlaxWavaVecaModel, FlaxWavaVecaPreTrainedModel, ) else: import sys lowercase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
352
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, PNDMScheduler, StableDiffusionLDMaDPipeline, UNetaDConditionModel, ) from diffusers.utils import nightly, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS enable_full_determinism() class snake_case__ ( unittest.TestCase ): """simple docstring""" lowerCamelCase = StableDiffusionLDMaDPipeline lowerCamelCase = TEXT_TO_IMAGE_PARAMS lowerCamelCase = TEXT_TO_IMAGE_BATCH_PARAMS lowerCamelCase = TEXT_TO_IMAGE_IMAGE_PARAMS def lowerCAmelCase ( self : Optional[Any] ) -> Union[str, Any]: """simple docstring""" torch.manual_seed(0 ) snake_case : str = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , ) snake_case : Dict = DDIMScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=UpperCamelCase__ , set_alpha_to_one=UpperCamelCase__ , ) torch.manual_seed(0 ) snake_case : int = AutoencoderKL( block_out_channels=[32, 64] , in_channels=6 , out_channels=6 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , ) torch.manual_seed(0 ) snake_case : Tuple = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1e-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) snake_case : int = CLIPTextModel(UpperCamelCase__ ) snake_case : Tuple = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) snake_case : str = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def lowerCAmelCase ( self : Optional[int] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : str=0 ) -> Optional[int]: """simple docstring""" if str(UpperCamelCase__ ).startswith('''mps''' ): snake_case : Optional[int] = torch.manual_seed(UpperCamelCase__ ) else: snake_case : Optional[int] = torch.Generator(device=UpperCamelCase__ ).manual_seed(UpperCamelCase__ ) snake_case : Tuple = { '''prompt''': '''A painting of a squirrel eating a burger''', '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def lowerCAmelCase ( self : Any ) -> str: """simple docstring""" snake_case : int = '''cpu''' # ensure determinism for the device-dependent torch.Generator snake_case : List[Any] = self.get_dummy_components() snake_case : str = StableDiffusionLDMaDPipeline(**UpperCamelCase__ ) snake_case : Union[str, Any] = ldmad_pipe.to(UpperCamelCase__ ) ldmad_pipe.set_progress_bar_config(disable=UpperCamelCase__ ) snake_case : List[str] = self.get_dummy_inputs(UpperCamelCase__ ) snake_case : Tuple = ldmad_pipe(**UpperCamelCase__ ) snake_case ,snake_case : int = output.rgb, output.depth snake_case : str = rgb[0, -3:, -3:, -1] snake_case : Union[str, Any] = depth[0, -3:, -1] assert rgb.shape == (1, 64, 64, 3) assert depth.shape == (1, 64, 64) snake_case : int = np.array( [0.37_338_176, 0.70_247, 0.74_203_193, 0.51_643_604, 0.58_256_793, 0.60_932_136, 0.4_181_095, 0.48_355_877, 0.46_535_262] ) snake_case : str = np.array([103.46_727, 85.812_004, 87.849_236] ) assert np.abs(image_slice_rgb.flatten() - expected_slice_rgb ).max() < 1e-2 assert np.abs(image_slice_depth.flatten() - expected_slice_depth ).max() < 1e-2 def lowerCAmelCase ( self : Dict ) -> int: """simple docstring""" snake_case : int = self.get_dummy_components() snake_case : Any = StableDiffusionLDMaDPipeline(**UpperCamelCase__ ) snake_case : Optional[Any] = ldmad_pipe.to(UpperCamelCase__ ) ldmad_pipe.set_progress_bar_config(disable=UpperCamelCase__ ) snake_case : int = self.get_dummy_inputs(UpperCamelCase__ ) snake_case : str = 3 * [inputs['''prompt''']] # forward snake_case : Union[str, Any] = ldmad_pipe(**UpperCamelCase__ ) snake_case ,snake_case : Union[str, Any] = output.rgb, output.depth snake_case : Tuple = rgb_slice_a[0, -3:, -3:, -1] snake_case : List[str] = depth_slice_a[0, -3:, -1] snake_case : int = self.get_dummy_inputs(UpperCamelCase__ ) snake_case : Optional[int] = 3 * [inputs.pop('''prompt''' )] snake_case : Dict = ldmad_pipe.tokenizer( UpperCamelCase__ , padding='''max_length''' , max_length=ldmad_pipe.tokenizer.model_max_length , truncation=UpperCamelCase__ , return_tensors='''pt''' , ) snake_case : Optional[Any] = text_inputs['''input_ids'''].to(UpperCamelCase__ ) snake_case : Any = ldmad_pipe.text_encoder(UpperCamelCase__ )[0] snake_case : Tuple = prompt_embeds # forward snake_case : List[Any] = ldmad_pipe(**UpperCamelCase__ ) snake_case ,snake_case : Dict = output.rgb, output.depth snake_case : Any = rgb_slice_a[0, -3:, -3:, -1] snake_case : Dict = depth_slice_a[0, -3:, -1] assert np.abs(rgb_slice_a.flatten() - rgb_slice_a.flatten() ).max() < 1e-4 assert np.abs(depth_slice_a.flatten() - depth_slice_a.flatten() ).max() < 1e-4 def lowerCAmelCase ( self : str ) -> List[Any]: """simple docstring""" snake_case : str = '''cpu''' # ensure determinism for the device-dependent torch.Generator snake_case : Dict = self.get_dummy_components() snake_case : List[str] = PNDMScheduler(skip_prk_steps=UpperCamelCase__ ) snake_case : Optional[Any] = StableDiffusionLDMaDPipeline(**UpperCamelCase__ ) snake_case : Dict = ldmad_pipe.to(UpperCamelCase__ ) ldmad_pipe.set_progress_bar_config(disable=UpperCamelCase__ ) snake_case : Dict = self.get_dummy_inputs(UpperCamelCase__ ) snake_case : str = '''french fries''' snake_case : List[str] = ldmad_pipe(**UpperCamelCase__ , negative_prompt=UpperCamelCase__ ) snake_case ,snake_case : Union[str, Any] = output.rgb, output.depth snake_case : Union[str, Any] = rgb[0, -3:, -3:, -1] snake_case : int = depth[0, -3:, -1] assert rgb.shape == (1, 64, 64, 3) assert depth.shape == (1, 64, 64) snake_case : Dict = np.array( [0.37_044, 0.71_811_503, 0.7_223_251, 0.48_603_675, 0.5_638_391, 0.6_364_948, 0.42_833_704, 0.4_901_315, 0.47_926_217] ) snake_case : Any = np.array([107.84_738, 84.62_802, 89.962_135] ) assert np.abs(rgb_slice.flatten() - expected_slice_rgb ).max() < 1e-2 assert np.abs(depth_slice.flatten() - expected_slice_depth ).max() < 1e-2 @slow @require_torch_gpu class snake_case__ ( unittest.TestCase ): """simple docstring""" def lowerCAmelCase ( self : Optional[int] ) -> List[str]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def lowerCAmelCase ( self : Union[str, Any] , UpperCamelCase__ : Any , UpperCamelCase__ : Union[str, Any]="cpu" , UpperCamelCase__ : Optional[int]=torch.floataa , UpperCamelCase__ : Union[str, Any]=0 ) -> Any: """simple docstring""" snake_case : Any = torch.Generator(device=UpperCamelCase__ ).manual_seed(UpperCamelCase__ ) snake_case : Optional[Any] = np.random.RandomState(UpperCamelCase__ ).standard_normal((1, 4, 64, 64) ) snake_case : Any = torch.from_numpy(UpperCamelCase__ ).to(device=UpperCamelCase__ , dtype=UpperCamelCase__ ) snake_case : List[Any] = { '''prompt''': '''a photograph of an astronaut riding a horse''', '''latents''': latents, '''generator''': generator, '''num_inference_steps''': 3, '''guidance_scale''': 7.5, '''output_type''': '''numpy''', } return inputs def lowerCAmelCase ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" snake_case : Dict = StableDiffusionLDMaDPipeline.from_pretrained('''Intel/ldm3d''' ) snake_case : Optional[Any] = ldmad_pipe.to(UpperCamelCase__ ) ldmad_pipe.set_progress_bar_config(disable=UpperCamelCase__ ) snake_case : Any = self.get_inputs(UpperCamelCase__ ) snake_case : str = ldmad_pipe(**UpperCamelCase__ ) snake_case ,snake_case : List[Any] = output.rgb, output.depth snake_case : Optional[Any] = rgb[0, -3:, -3:, -1].flatten() snake_case : Tuple = rgb[0, -3:, -1].flatten() assert rgb.shape == (1, 512, 512, 3) assert depth.shape == (1, 512, 512) snake_case : Optional[Any] = np.array( [0.53_805_465, 0.56_707_305, 0.5_486_515, 0.57_012_236, 0.5_814_511, 0.56_253_487, 0.54_843_014, 0.55_092_263, 0.6_459_706] ) snake_case : str = np.array( [0.9_263_781, 0.6_678_672, 0.5_486_515, 0.92_202_145, 0.67_831_135, 0.56_253_487, 0.9_241_694, 0.7_551_478, 0.6_459_706] ) assert np.abs(rgb_slice - expected_slice_rgb ).max() < 3e-3 assert np.abs(depth_slice - expected_slice_depth ).max() < 3e-3 @nightly @require_torch_gpu class snake_case__ ( unittest.TestCase ): """simple docstring""" def lowerCAmelCase ( self : Union[str, Any] ) -> Optional[int]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def lowerCAmelCase ( self : Optional[Any] , UpperCamelCase__ : Any , UpperCamelCase__ : Any="cpu" , UpperCamelCase__ : Optional[int]=torch.floataa , UpperCamelCase__ : Optional[int]=0 ) -> str: """simple docstring""" snake_case : Dict = torch.Generator(device=UpperCamelCase__ ).manual_seed(UpperCamelCase__ ) snake_case : Optional[Any] = np.random.RandomState(UpperCamelCase__ ).standard_normal((1, 4, 64, 64) ) snake_case : int = torch.from_numpy(UpperCamelCase__ ).to(device=UpperCamelCase__ , dtype=UpperCamelCase__ ) snake_case : List[str] = { '''prompt''': '''a photograph of an astronaut riding a horse''', '''latents''': latents, '''generator''': generator, '''num_inference_steps''': 50, '''guidance_scale''': 7.5, '''output_type''': '''numpy''', } return inputs def lowerCAmelCase ( self : Optional[Any] ) -> Tuple: """simple docstring""" snake_case : Optional[Any] = StableDiffusionLDMaDPipeline.from_pretrained('''Intel/ldm3d''' ).to(UpperCamelCase__ ) ldmad_pipe.set_progress_bar_config(disable=UpperCamelCase__ ) snake_case : Dict = self.get_inputs(UpperCamelCase__ ) snake_case : str = ldmad_pipe(**UpperCamelCase__ ) snake_case ,snake_case : Union[str, Any] = output.rgb, output.depth snake_case : Union[str, Any] = 0.495_586 snake_case : Tuple = 0.33_795_515 snake_case : Dict = 112.48_518 snake_case : Optional[int] = 98.489_746 assert np.abs(expected_rgb_mean - rgb.mean() ) < 1e-3 assert np.abs(expected_rgb_std - rgb.std() ) < 1e-3 assert np.abs(expected_depth_mean - depth.mean() ) < 1e-3 assert np.abs(expected_depth_std - depth.std() ) < 1e-3 def lowerCAmelCase ( self : Dict ) -> Dict: """simple docstring""" snake_case : Optional[int] = StableDiffusionLDMaDPipeline.from_pretrained('''Intel/ldm3d-4c''' ).to(UpperCamelCase__ ) ldmad_pipe.set_progress_bar_config(disable=UpperCamelCase__ ) snake_case : int = self.get_inputs(UpperCamelCase__ ) snake_case : List[Any] = ldmad_pipe(**UpperCamelCase__ ) snake_case ,snake_case : Union[str, Any] = output.rgb, output.depth snake_case : Tuple = 0.4_194_127 snake_case : Optional[Any] = 0.35_375_586 snake_case : Any = 0.5_638_502 snake_case : int = 0.34_686_103 assert rgb.shape == (1, 512, 512, 3) assert depth.shape == (1, 512, 512, 1) assert np.abs(expected_rgb_mean - rgb.mean() ) < 1e-3 assert np.abs(expected_rgb_std - rgb.std() ) < 1e-3 assert np.abs(expected_depth_mean - depth.mean() ) < 1e-3 assert np.abs(expected_depth_std - depth.std() ) < 1e-3
83
0
import logging from dataclasses import dataclass, field from pathlib import Path from typing import Optional, Union from .generation.configuration_utils import GenerationConfig from .training_args import TrainingArguments from .utils import add_start_docstrings a_ = logging.getLogger(__name__) @dataclass @add_start_docstrings(TrainingArguments.__doc__ ) class lowercase__ ( _UpperCAmelCase ): a_ =field(default=_UpperCAmelCase, metadata={"""help""": """Whether to use SortishSampler or not."""} ) a_ =field( default=_UpperCAmelCase, metadata={"""help""": """Whether to use generate to calculate generative metrics (ROUGE, BLEU)."""} ) a_ =field( default=_UpperCAmelCase, metadata={ """help""": ( """The `max_length` to use on each evaluation loop when `predict_with_generate=True`. Will default """ """to the `max_length` value of the model configuration.""" ) }, ) a_ =field( default=_UpperCAmelCase, metadata={ """help""": ( """The `num_beams` to use on each evaluation loop when `predict_with_generate=True`. Will default """ """to the `num_beams` value of the model configuration.""" ) }, ) a_ =field( default=_UpperCAmelCase, metadata={ """help""": """Model id, file path or url pointing to a GenerationConfig json file, to use during prediction.""" }, ) def UpperCAmelCase ( self )-> Tuple: '''simple docstring''' lowerCAmelCase__ = super().to_dict() for k, v in d.items(): if isinstance(__UpperCAmelCase , __UpperCAmelCase ): lowerCAmelCase__ = v.to_dict() return d
340
import collections import importlib.util import os import re from pathlib import Path a_ = '''src/transformers''' # Matches is_xxx_available() a_ = re.compile(r'''is\_([a-z_]*)_available()''') # Catches a one-line _import_struct = {xxx} a_ = re.compile(r'''^_import_structure\s+=\s+\{([^\}]+)\}''') # Catches a line with a key-values pattern: "bla": ["foo", "bar"] a_ = re.compile(r'''\s+"\S*":\s+\[([^\]]*)\]''') # Catches a line if not is_foo_available a_ = re.compile(r'''^\s*if\s+not\s+is\_[a-z_]*\_available\(\)''') # Catches a line _import_struct["bla"].append("foo") a_ = re.compile(r'''^\s*_import_structure\["\S*"\]\.append\("(\S*)"\)''') # Catches a line _import_struct["bla"].extend(["foo", "bar"]) or _import_struct["bla"] = ["foo", "bar"] a_ = re.compile(r'''^\s*_import_structure\[\S*\](?:\.extend\(|\s*=\s+)\[([^\]]*)\]''') # Catches a line with an object between quotes and a comma: "MyModel", a_ = re.compile('''^\s+"([^"]+)",''') # Catches a line with objects between brackets only: ["foo", "bar"], a_ = re.compile('''^\s+\[([^\]]+)\]''') # Catches a line with from foo import bar, bla, boo a_ = re.compile(r'''\s+from\s+\S*\s+import\s+([^\(\s].*)\n''') # Catches a line with try: a_ = re.compile(r'''^\s*try:''') # Catches a line with else: a_ = re.compile(r'''^\s*else:''') def _a ( UpperCamelCase_ : Union[str, Any] ) -> List[str]: """simple docstring""" if _re_test_backend.search(UpperCamelCase_ ) is None: return None lowerCAmelCase__ = [b[0] for b in _re_backend.findall(UpperCamelCase_ )] backends.sort() return "_and_".join(UpperCamelCase_ ) def _a ( UpperCamelCase_ : Optional[int] ) -> Tuple: """simple docstring""" with open(UpperCamelCase_ , "r" , encoding="utf-8" , newline="\n" ) as f: lowerCAmelCase__ = f.readlines() lowerCAmelCase__ = 0 while line_index < len(UpperCamelCase_ ) and not lines[line_index].startswith("_import_structure = {" ): line_index += 1 # If this is a traditional init, just return. if line_index >= len(UpperCamelCase_ ): return None # First grab the objects without a specific backend in _import_structure lowerCAmelCase__ = [] while not lines[line_index].startswith("if TYPE_CHECKING" ) and find_backend(lines[line_index] ) is None: lowerCAmelCase__ = lines[line_index] # If we have everything on a single line, let's deal with it. if _re_one_line_import_struct.search(UpperCamelCase_ ): lowerCAmelCase__ = _re_one_line_import_struct.search(UpperCamelCase_ ).groups()[0] lowerCAmelCase__ = re.findall("\[([^\]]+)\]" , UpperCamelCase_ ) for imp in imports: objects.extend([obj[1:-1] for obj in imp.split(", " )] ) line_index += 1 continue lowerCAmelCase__ = _re_import_struct_key_value.search(UpperCamelCase_ ) if single_line_import_search is not None: lowerCAmelCase__ = [obj[1:-1] for obj in single_line_import_search.groups()[0].split(", " ) if len(UpperCamelCase_ ) > 0] objects.extend(UpperCamelCase_ ) elif line.startswith(" " * 8 + "\"" ): objects.append(line[9:-3] ) line_index += 1 lowerCAmelCase__ = {"none": objects} # Let's continue with backend-specific objects in _import_structure while not lines[line_index].startswith("if TYPE_CHECKING" ): # If the line is an if not is_backend_available, we grab all objects associated. lowerCAmelCase__ = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: lowerCAmelCase__ = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 lowerCAmelCase__ = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(" " * 4 ): lowerCAmelCase__ = lines[line_index] if _re_import_struct_add_one.search(UpperCamelCase_ ) is not None: objects.append(_re_import_struct_add_one.search(UpperCamelCase_ ).groups()[0] ) elif _re_import_struct_add_many.search(UpperCamelCase_ ) is not None: lowerCAmelCase__ = _re_import_struct_add_many.search(UpperCamelCase_ ).groups()[0].split(", " ) lowerCAmelCase__ = [obj[1:-1] for obj in imports if len(UpperCamelCase_ ) > 0] objects.extend(UpperCamelCase_ ) elif _re_between_brackets.search(UpperCamelCase_ ) is not None: lowerCAmelCase__ = _re_between_brackets.search(UpperCamelCase_ ).groups()[0].split(", " ) lowerCAmelCase__ = [obj[1:-1] for obj in imports if len(UpperCamelCase_ ) > 0] objects.extend(UpperCamelCase_ ) elif _re_quote_object.search(UpperCamelCase_ ) is not None: objects.append(_re_quote_object.search(UpperCamelCase_ ).groups()[0] ) elif line.startswith(" " * 8 + "\"" ): objects.append(line[9:-3] ) elif line.startswith(" " * 12 + "\"" ): objects.append(line[13:-3] ) line_index += 1 lowerCAmelCase__ = objects else: line_index += 1 # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend lowerCAmelCase__ = [] while ( line_index < len(UpperCamelCase_ ) and find_backend(lines[line_index] ) is None and not lines[line_index].startswith("else" ) ): lowerCAmelCase__ = lines[line_index] lowerCAmelCase__ = _re_import.search(UpperCamelCase_ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(", " ) ) elif line.startswith(" " * 8 ): objects.append(line[8:-2] ) line_index += 1 lowerCAmelCase__ = {"none": objects} # Let's continue with backend-specific objects while line_index < len(UpperCamelCase_ ): # If the line is an if is_backend_available, we grab all objects associated. lowerCAmelCase__ = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: lowerCAmelCase__ = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 lowerCAmelCase__ = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(" " * 8 ): lowerCAmelCase__ = lines[line_index] lowerCAmelCase__ = _re_import.search(UpperCamelCase_ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(", " ) ) elif line.startswith(" " * 12 ): objects.append(line[12:-2] ) line_index += 1 lowerCAmelCase__ = objects else: line_index += 1 return import_dict_objects, type_hint_objects def _a ( UpperCamelCase_ : int , UpperCamelCase_ : Optional[Any] ) -> str: """simple docstring""" def find_duplicates(UpperCamelCase_ : str ): return [k for k, v in collections.Counter(UpperCamelCase_ ).items() if v > 1] if list(import_dict_objects.keys() ) != list(type_hint_objects.keys() ): return ["Both sides of the init do not have the same backends!"] lowerCAmelCase__ = [] for key in import_dict_objects.keys(): lowerCAmelCase__ = find_duplicates(import_dict_objects[key] ) if duplicate_imports: errors.append(F"Duplicate _import_structure definitions for: {duplicate_imports}" ) lowerCAmelCase__ = find_duplicates(type_hint_objects[key] ) if duplicate_type_hints: errors.append(F"Duplicate TYPE_CHECKING objects for: {duplicate_type_hints}" ) if sorted(set(import_dict_objects[key] ) ) != sorted(set(type_hint_objects[key] ) ): lowerCAmelCase__ = "base imports" if key == "none" else F"{key} backend" errors.append(F"Differences for {name}:" ) for a in type_hint_objects[key]: if a not in import_dict_objects[key]: errors.append(F" {a} in TYPE_HINT but not in _import_structure." ) for a in import_dict_objects[key]: if a not in type_hint_objects[key]: errors.append(F" {a} in _import_structure but not in TYPE_HINT." ) return errors def _a ( ) -> List[Any]: """simple docstring""" lowerCAmelCase__ = [] for root, _, files in os.walk(UpperCamelCase_ ): if "__init__.py" in files: lowerCAmelCase__ = os.path.join(UpperCamelCase_ , "__init__.py" ) lowerCAmelCase__ = parse_init(UpperCamelCase_ ) if objects is not None: lowerCAmelCase__ = analyze_results(*UpperCamelCase_ ) if len(UpperCamelCase_ ) > 0: lowerCAmelCase__ = F"Problem in {fname}, both halves do not define the same objects.\n{errors[0]}" failures.append("\n".join(UpperCamelCase_ ) ) if len(UpperCamelCase_ ) > 0: raise ValueError("\n\n".join(UpperCamelCase_ ) ) def _a ( ) -> str: """simple docstring""" lowerCAmelCase__ = [] for path, directories, files in os.walk(UpperCamelCase_ ): for folder in directories: # Ignore private modules if folder.startswith("_" ): directories.remove(UpperCamelCase_ ) continue # Ignore leftovers from branches (empty folders apart from pycache) if len(list((Path(UpperCamelCase_ ) / folder).glob("*.py" ) ) ) == 0: continue lowerCAmelCase__ = str((Path(UpperCamelCase_ ) / folder).relative_to(UpperCamelCase_ ) ) lowerCAmelCase__ = short_path.replace(os.path.sep , "." ) submodules.append(UpperCamelCase_ ) for fname in files: if fname == "__init__.py": continue lowerCAmelCase__ = str((Path(UpperCamelCase_ ) / fname).relative_to(UpperCamelCase_ ) ) lowerCAmelCase__ = short_path.replace(".py" , "" ).replace(os.path.sep , "." ) if len(submodule.split("." ) ) == 1: submodules.append(UpperCamelCase_ ) return submodules a_ = [ '''convert_pytorch_checkpoint_to_tf2''', '''modeling_flax_pytorch_utils''', ] def _a ( ) -> int: """simple docstring""" lowerCAmelCase__ = importlib.util.spec_from_file_location( "transformers" , os.path.join(UpperCamelCase_ , "__init__.py" ) , submodule_search_locations=[PATH_TO_TRANSFORMERS] , ) lowerCAmelCase__ = spec.loader.load_module() lowerCAmelCase__ = [ module for module in get_transformers_submodules() if module not in IGNORE_SUBMODULES and module not in transformers._import_structure.keys() ] if len(UpperCamelCase_ ) > 0: lowerCAmelCase__ = "\n".join(F"- {module}" for module in module_not_registered ) raise ValueError( "The following submodules are not properly registered in the main init of Transformers:\n" F"{list_of_modules}\n" "Make sure they appear somewhere in the keys of `_import_structure` with an empty list as value." ) if __name__ == "__main__": check_all_inits() check_submodules()
340
1
"""simple docstring""" # A Bipartite Graph is a graph whose vertices can be divided into two independent sets, # U and V such that every edge (u, v) either connects a vertex from U to V or a vertex # from V to U. In other words, for every edge (u, v), either u belongs to U and v to V, # or u belongs to V and v to U. We can also say that there is no edge that connects # vertices of same set. def lowerCAmelCase__ ( _UpperCamelCase : int ) -> Union[str, Any]: """simple docstring""" snake_case = [False] * len(_UpperCamelCase ) snake_case = [-1] * len(_UpperCamelCase ) def dfs(_UpperCamelCase : Optional[Any] , _UpperCamelCase : str ): snake_case = True snake_case = c for u in graph[v]: if not visited[u]: dfs(_UpperCamelCase , 1 - c ) for i in range(len(_UpperCamelCase ) ): if not visited[i]: dfs(_UpperCamelCase , 0 ) for i in range(len(_UpperCamelCase ) ): for j in graph[i]: if color[i] == color[j]: return False return True # Adjacency list of graph SCREAMING_SNAKE_CASE__ = {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: []} print(check_bipartite_dfs(graph))
149
"""simple docstring""" def lowerCAmelCase__ ( _UpperCamelCase : str ) -> bool: """simple docstring""" if not all(x.isalpha() for x in string ): raise ValueError('String must only contain alphabetic characters.' ) snake_case = sorted(string.lower() ) return len(_UpperCamelCase ) == len(set(_UpperCamelCase ) ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = input("Enter a string ").strip() SCREAMING_SNAKE_CASE__ = is_isogram(input_str) print(f"""{input_str} is {'an' if isogram else 'not an'} isogram.""")
149
1
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowercase_ : List[str] = logging.get_logger(__name__) lowercase_ : Optional[Any] = { 'facebook/convnextv2-tiny-1k-224': 'https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json', } class __lowerCAmelCase ( UpperCAmelCase__ , UpperCAmelCase__ ): snake_case_ : Optional[int] = "convnextv2" def __init__( self : Optional[Any] , snake_case__ : Union[str, Any]=3 , snake_case__ : Dict=4 , snake_case__ : List[str]=4 , snake_case__ : List[str]=None , snake_case__ : str=None , snake_case__ : Optional[int]="gelu" , snake_case__ : List[Any]=0.02 , snake_case__ : List[str]=1e-12 , snake_case__ : Union[str, Any]=0.0 , snake_case__ : Dict=224 , snake_case__ : Optional[int]=None , snake_case__ : Optional[Any]=None , **snake_case__ : Union[str, Any] , ): """simple docstring""" super().__init__(**snake_case__ ) _UpperCAmelCase = num_channels _UpperCAmelCase = patch_size _UpperCAmelCase = num_stages _UpperCAmelCase = [96, 192, 384, 768] if hidden_sizes is None else hidden_sizes _UpperCAmelCase = [3, 3, 9, 3] if depths is None else depths _UpperCAmelCase = hidden_act _UpperCAmelCase = initializer_range _UpperCAmelCase = layer_norm_eps _UpperCAmelCase = drop_path_rate _UpperCAmelCase = image_size _UpperCAmelCase = ["stem"] + [F"""stage{idx}""" for idx in range(1 , len(self.depths ) + 1 )] _UpperCAmelCase , _UpperCAmelCase = get_aligned_output_features_output_indices( out_features=snake_case__ , out_indices=snake_case__ , stage_names=self.stage_names )
133
from typing import Any import numpy as np def __SCREAMING_SNAKE_CASE ( snake_case_ ): '''simple docstring''' return np.array_equal(snake_case_ , matrix.conjugate().T ) def __SCREAMING_SNAKE_CASE ( snake_case_ , snake_case_ ): '''simple docstring''' _UpperCAmelCase = v.conjugate().T _UpperCAmelCase = v_star.dot(snake_case_ ) assert isinstance(snake_case_ , np.ndarray ) return (v_star_dot.dot(snake_case_ )) / (v_star.dot(snake_case_ )) def __SCREAMING_SNAKE_CASE ( ): '''simple docstring''' _UpperCAmelCase = np.array([[2, 2 + 1j, 4], [2 - 1j, 3, 1j], [4, -1j, 1]] ) _UpperCAmelCase = np.array([[1], [2], [3]] ) assert is_hermitian(snake_case_ ), f"""{a} is not hermitian.""" print(rayleigh_quotient(snake_case_ , snake_case_ ) ) _UpperCAmelCase = np.array([[1, 2, 4], [2, 3, -1], [4, -1, 1]] ) assert is_hermitian(snake_case_ ), f"""{a} is not hermitian.""" assert rayleigh_quotient(snake_case_ , snake_case_ ) == float(3 ) if __name__ == "__main__": import doctest doctest.testmod() tests()
133
1
import shutil import tempfile import unittest from transformers import ( SPIECE_UNDERLINE, AddedToken, BatchEncoding, NllbTokenizer, NllbTokenizerFast, is_torch_available, ) from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, ) from ...test_tokenization_common import TokenizerTesterMixin _lowerCamelCase : List[str] = get_tests_dir("""fixtures/test_sentencepiece.model""") if is_torch_available(): from transformers.models.mam_aaa.modeling_mam_aaa import shift_tokens_right _lowerCamelCase : Tuple = 256047 _lowerCamelCase : Any = 256145 @require_sentencepiece @require_tokenizers class UpperCamelCase_ ( UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' UpperCAmelCase__ = NllbTokenizer UpperCAmelCase__ = NllbTokenizerFast UpperCAmelCase__ = True UpperCAmelCase__ = True UpperCAmelCase__ = {} def SCREAMING_SNAKE_CASE ( self : int) ->int: '''simple docstring''' super().setUp() # We have a SentencePiece fixture for testing A__ = NllbTokenizer(UpperCAmelCase__ , keep_accents=UpperCAmelCase__) tokenizer.save_pretrained(self.tmpdirname) def SCREAMING_SNAKE_CASE ( self : Optional[Any]) ->List[Any]: '''simple docstring''' A__ = NllbTokenizer(UpperCAmelCase__ , keep_accents=UpperCAmelCase__) A__ = tokenizer.tokenize('''This is a test''') self.assertListEqual(UpperCAmelCase__ , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est''']) self.assertListEqual( tokenizer.convert_tokens_to_ids(UpperCAmelCase__) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) A__ = tokenizer.tokenize('''I was born in 92000, and this is falsé.''') self.assertListEqual( UpperCAmelCase__ , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.''', ] , ) A__ = tokenizer.convert_tokens_to_ids(UpperCAmelCase__) self.assertListEqual( UpperCAmelCase__ , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) A__ = tokenizer.convert_ids_to_tokens(UpperCAmelCase__) self.assertListEqual( UpperCAmelCase__ , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''<unk>''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''<unk>''', '''.''', ] , ) def SCREAMING_SNAKE_CASE ( self : Optional[Any]) ->str: '''simple docstring''' A__ = (self.rust_tokenizer_class, '''hf-internal-testing/tiny-random-nllb''', {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})"""): A__ = self.rust_tokenizer_class.from_pretrained(UpperCAmelCase__ , **UpperCAmelCase__) A__ = self.tokenizer_class.from_pretrained(UpperCAmelCase__ , **UpperCAmelCase__) A__ = tempfile.mkdtemp() A__ = tokenizer_r.save_pretrained(UpperCAmelCase__) A__ = tokenizer_p.save_pretrained(UpperCAmelCase__) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any('''tokenizer.json''' in f for f in tokenizer_r_files)) A__ = tuple(f for f in tokenizer_r_files if '''tokenizer.json''' not in f) self.assertSequenceEqual(UpperCAmelCase__ , UpperCAmelCase__) # Checks everything loads correctly in the same way A__ = tokenizer_r.from_pretrained(UpperCAmelCase__) A__ = tokenizer_p.from_pretrained(UpperCAmelCase__) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(UpperCAmelCase__ , UpperCAmelCase__)) shutil.rmtree(UpperCAmelCase__) # Save tokenizer rust, legacy_format=True A__ = tempfile.mkdtemp() A__ = tokenizer_r.save_pretrained(UpperCAmelCase__ , legacy_format=UpperCAmelCase__) A__ = tokenizer_p.save_pretrained(UpperCAmelCase__) # Checks it save with the same files self.assertSequenceEqual(UpperCAmelCase__ , UpperCAmelCase__) # Checks everything loads correctly in the same way A__ = tokenizer_r.from_pretrained(UpperCAmelCase__) A__ = tokenizer_p.from_pretrained(UpperCAmelCase__) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(UpperCAmelCase__ , UpperCAmelCase__)) shutil.rmtree(UpperCAmelCase__) # Save tokenizer rust, legacy_format=False A__ = tempfile.mkdtemp() A__ = tokenizer_r.save_pretrained(UpperCAmelCase__ , legacy_format=UpperCAmelCase__) A__ = tokenizer_p.save_pretrained(UpperCAmelCase__) # Checks it saved the tokenizer.json file self.assertTrue(any('''tokenizer.json''' in f for f in tokenizer_r_files)) # Checks everything loads correctly in the same way A__ = tokenizer_r.from_pretrained(UpperCAmelCase__) A__ = tokenizer_p.from_pretrained(UpperCAmelCase__) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(UpperCAmelCase__ , UpperCAmelCase__)) shutil.rmtree(UpperCAmelCase__) @require_torch def SCREAMING_SNAKE_CASE ( self : List[str]) ->str: '''simple docstring''' if not self.test_seqaseq: return A__ = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}"""): # Longer text that will definitely require truncation. A__ = [ ''' UN Chief Says There Is No Military Solution in Syria''', ''' Secretary-General Ban Ki-moon says his response to Russia\'s stepped up military support for''' ''' Syria is that \'there is no military solution\' to the nearly five-year conflict and more weapons''' ''' will only worsen the violence and misery for millions of people.''', ] A__ = [ '''Şeful ONU declară că nu există o soluţie militară în Siria''', '''Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al''' ''' Rusiei pentru Siria este că "nu există o soluţie militară" la conflictul de aproape cinci ani şi''' ''' că noi arme nu vor face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.''', ] try: A__ = tokenizer.prepare_seqaseq_batch( src_texts=UpperCAmelCase__ , tgt_texts=UpperCAmelCase__ , max_length=3 , max_target_length=10 , return_tensors='''pt''' , src_lang='''eng_Latn''' , tgt_lang='''ron_Latn''' , ) except NotImplementedError: return self.assertEqual(batch.input_ids.shape[1] , 3) self.assertEqual(batch.labels.shape[1] , 10) # max_target_length will default to max_length if not specified A__ = tokenizer.prepare_seqaseq_batch( UpperCAmelCase__ , tgt_texts=UpperCAmelCase__ , max_length=3 , return_tensors='''pt''') self.assertEqual(batch.input_ids.shape[1] , 3) self.assertEqual(batch.labels.shape[1] , 3) A__ = tokenizer.prepare_seqaseq_batch( src_texts=UpperCAmelCase__ , max_length=3 , max_target_length=10 , return_tensors='''pt''') self.assertEqual(batch_encoder_only.input_ids.shape[1] , 3) self.assertEqual(batch_encoder_only.attention_mask.shape[1] , 3) self.assertNotIn('''decoder_input_ids''' , UpperCAmelCase__) @unittest.skip('''Unfortunately way too slow to build a BPE with SentencePiece.''') def SCREAMING_SNAKE_CASE ( self : List[Any]) ->Union[str, Any]: '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( self : Tuple) ->Any: '''simple docstring''' for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})"""): A__ = [AddedToken('''<special>''' , lstrip=UpperCAmelCase__)] A__ = self.rust_tokenizer_class.from_pretrained( UpperCAmelCase__ , additional_special_tokens=UpperCAmelCase__ , **UpperCAmelCase__) A__ = tokenizer_r.encode('''Hey this is a <special> token''') A__ = tokenizer_r.encode('''<special>''' , add_special_tokens=UpperCAmelCase__)[0] self.assertTrue(special_token_id in r_output) if self.test_slow_tokenizer: A__ = self.rust_tokenizer_class.from_pretrained( UpperCAmelCase__ , additional_special_tokens=UpperCAmelCase__ , **UpperCAmelCase__ , ) A__ = self.tokenizer_class.from_pretrained( UpperCAmelCase__ , additional_special_tokens=UpperCAmelCase__ , **UpperCAmelCase__) A__ = tokenizer_p.encode('''Hey this is a <special> token''') A__ = tokenizer_cr.encode('''Hey this is a <special> token''') self.assertEqual(UpperCAmelCase__ , UpperCAmelCase__) self.assertEqual(UpperCAmelCase__ , UpperCAmelCase__) self.assertTrue(special_token_id in p_output) self.assertTrue(special_token_id in cr_output) @require_torch @require_sentencepiece @require_tokenizers class UpperCamelCase_ ( unittest.TestCase ): '''simple docstring''' UpperCAmelCase__ = '''facebook/nllb-200-distilled-600M''' UpperCAmelCase__ = [ ''' UN Chief Says There Is No Military Solution in Syria''', ''' Secretary-General Ban Ki-moon says his response to Russia\'s stepped up military support for Syria is that "there is no military solution" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.''', ] UpperCAmelCase__ = [ '''Şeful ONU declară că nu există o soluţie militară în Siria''', '''Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei''' ''' pentru Siria este că "nu există o soluţie militară" la conflictul de aproape cinci ani şi că noi arme nu vor''' ''' face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.''', ] UpperCAmelCase__ = [ 25_6047, 1_6297, 13_4408, 8165, 24_8066, 1_4734, 950, 1135, 10_5721, 3573, 83, 2_7352, 108, 4_9486, 2, ] @classmethod def SCREAMING_SNAKE_CASE ( cls : Optional[int]) ->List[str]: '''simple docstring''' A__ = NllbTokenizer.from_pretrained( cls.checkpoint_name , src_lang='''eng_Latn''' , tgt_lang='''ron_Latn''') A__ = 1 return cls def SCREAMING_SNAKE_CASE ( self : Optional[int]) ->Any: '''simple docstring''' self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''ace_Arab'''] , 256_001) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''ace_Latn'''] , 256_002) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''fra_Latn'''] , 256_057) def SCREAMING_SNAKE_CASE ( self : str) ->Tuple: '''simple docstring''' A__ = self.tokenizer.batch_encode_plus(self.src_text).input_ids[0] self.assertListEqual(self.expected_src_tokens , UpperCAmelCase__) def SCREAMING_SNAKE_CASE ( self : Optional[Any]) ->List[str]: '''simple docstring''' self.assertIn(UpperCAmelCase__ , self.tokenizer.all_special_ids) # fmt: off A__ = [RO_CODE, 4_254, 98_068, 112_923, 39_072, 3_909, 713, 102_767, 26, 17_314, 35_642, 14_683, 33_118, 2_022, 66_987, 2, 256_047] # fmt: on A__ = self.tokenizer.decode(UpperCAmelCase__ , skip_special_tokens=UpperCAmelCase__) A__ = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=UpperCAmelCase__) self.assertEqual(UpperCAmelCase__ , UpperCAmelCase__) self.assertNotIn(self.tokenizer.eos_token , UpperCAmelCase__) def SCREAMING_SNAKE_CASE ( self : Dict) ->str: '''simple docstring''' A__ = ['''this is gunna be a long sentence ''' * 20] assert isinstance(src_text[0] , UpperCAmelCase__) A__ = 10 A__ = self.tokenizer(UpperCAmelCase__ , max_length=UpperCAmelCase__ , truncation=UpperCAmelCase__).input_ids[0] self.assertEqual(ids[-1] , 2) self.assertEqual(ids[0] , UpperCAmelCase__) self.assertEqual(len(UpperCAmelCase__) , UpperCAmelCase__) def SCREAMING_SNAKE_CASE ( self : List[str]) ->int: '''simple docstring''' self.assertListEqual(self.tokenizer.convert_tokens_to_ids(['''<mask>''', '''ar_AR''']) , [256_203, 3]) def SCREAMING_SNAKE_CASE ( self : int) ->Union[str, Any]: '''simple docstring''' A__ = tempfile.mkdtemp() A__ = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(UpperCAmelCase__) A__ = NllbTokenizer.from_pretrained(UpperCAmelCase__) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , UpperCAmelCase__) @require_torch def SCREAMING_SNAKE_CASE ( self : Dict) ->Tuple: '''simple docstring''' A__ = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=UpperCAmelCase__ , truncation=UpperCAmelCase__ , max_length=len(self.expected_src_tokens) , return_tensors='''pt''' , ) A__ = shift_tokens_right( batch['''labels'''] , self.tokenizer.pad_token_id , self.tokenizer.lang_code_to_id['''ron_Latn''']) self.assertIsInstance(UpperCAmelCase__ , UpperCAmelCase__) self.assertEqual((2, 15) , batch.input_ids.shape) self.assertEqual((2, 15) , batch.attention_mask.shape) A__ = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , UpperCAmelCase__) self.assertEqual(UpperCAmelCase__ , batch.decoder_input_ids[0, 0]) # EOS # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [EN_CODE]) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id]) def SCREAMING_SNAKE_CASE ( self : str) ->List[Any]: '''simple docstring''' A__ = self.tokenizer(self.src_text , padding=UpperCAmelCase__ , truncation=UpperCAmelCase__ , max_length=3 , return_tensors='''pt''') A__ = self.tokenizer( text_target=self.tgt_text , padding=UpperCAmelCase__ , truncation=UpperCAmelCase__ , max_length=10 , return_tensors='''pt''') A__ = targets['''input_ids'''] A__ = shift_tokens_right( UpperCAmelCase__ , self.tokenizer.pad_token_id , decoder_start_token_id=self.tokenizer.lang_code_to_id[self.tokenizer.tgt_lang] , ) self.assertEqual(batch.input_ids.shape[1] , 3) self.assertEqual(batch.decoder_input_ids.shape[1] , 10) @require_torch def SCREAMING_SNAKE_CASE ( self : List[Any]) ->Optional[int]: '''simple docstring''' A__ = self.tokenizer._build_translation_inputs( '''A test''' , return_tensors='''pt''' , src_lang='''eng_Latn''' , tgt_lang='''fra_Latn''') self.assertEqual( nested_simplify(UpperCAmelCase__) , { # A, test, EOS, en_XX '''input_ids''': [[256_047, 70, 7_356, 2]], '''attention_mask''': [[1, 1, 1, 1]], # ar_AR '''forced_bos_token_id''': 256_057, } , ) @require_torch def SCREAMING_SNAKE_CASE ( self : int) ->Tuple: '''simple docstring''' A__ = True A__ = self.tokenizer( '''UN Chief says there is no military solution in Syria''' , src_lang='''eng_Latn''' , tgt_lang='''fra_Latn''') self.assertEqual( inputs.input_ids , [16_297, 134_408, 25_653, 6_370, 248, 254, 103_929, 94_995, 108, 49_486, 2, 256_047]) A__ = False A__ = self.tokenizer( '''UN Chief says there is no military solution in Syria''' , src_lang='''eng_Latn''' , tgt_lang='''fra_Latn''') self.assertEqual( inputs.input_ids , [256_047, 16_297, 134_408, 25_653, 6_370, 248, 254, 103_929, 94_995, 108, 49_486, 2])
231
class UpperCamelCase_ : '''simple docstring''' def __init__( self : List[str] , UpperCAmelCase__ : int) ->Union[str, Any]: '''simple docstring''' A__ = n A__ = [None] * self.n A__ = 0 # index of the first element A__ = 0 A__ = 0 def __len__( self : List[Any]) ->int: '''simple docstring''' return self.size def SCREAMING_SNAKE_CASE ( self : List[Any]) ->bool: '''simple docstring''' return self.size == 0 def SCREAMING_SNAKE_CASE ( self : str) ->List[Any]: '''simple docstring''' return False if self.is_empty() else self.array[self.front] def SCREAMING_SNAKE_CASE ( self : List[str] , UpperCAmelCase__ : str) ->Any: '''simple docstring''' if self.size >= self.n: raise Exception('''QUEUE IS FULL''') A__ = data A__ = (self.rear + 1) % self.n self.size += 1 return self def SCREAMING_SNAKE_CASE ( self : Union[str, Any]) ->Any: '''simple docstring''' if self.size == 0: raise Exception('''UNDERFLOW''') A__ = self.array[self.front] A__ = None A__ = (self.front + 1) % self.n self.size -= 1 return temp
231
1
import unittest from parameterized import parameterized from transformers import AutoTokenizer, GPTNeoXConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( GPTNeoXForCausalLM, GPTNeoXForQuestionAnswering, GPTNeoXForSequenceClassification, GPTNeoXForTokenClassification, GPTNeoXModel, ) class snake_case__ : """simple docstring""" def __init__( self : Union[str, Any] , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : Optional[Any]=13 , __lowerCamelCase : Union[str, Any]=7 , __lowerCamelCase : List[str]=True , __lowerCamelCase : List[Any]=True , __lowerCamelCase : Tuple=True , __lowerCamelCase : Optional[int]=True , __lowerCamelCase : str=99 , __lowerCamelCase : Optional[int]=64 , __lowerCamelCase : Optional[int]=5 , __lowerCamelCase : List[Any]=4 , __lowerCamelCase : Optional[int]=37 , __lowerCamelCase : List[str]="gelu" , __lowerCamelCase : Dict=0.1 , __lowerCamelCase : List[Any]=0.1 , __lowerCamelCase : List[str]=5_12 , __lowerCamelCase : Any=16 , __lowerCamelCase : Tuple=2 , __lowerCamelCase : Optional[Any]=0.02 , __lowerCamelCase : List[Any]=3 , __lowerCamelCase : Optional[Any]=4 , __lowerCamelCase : str=None , ) -> int: a = parent a = batch_size a = seq_length a = is_training a = use_input_mask a = use_token_type_ids a = use_labels a = vocab_size a = hidden_size a = num_hidden_layers a = num_attention_heads a = intermediate_size a = hidden_act a = hidden_dropout_prob a = attention_probs_dropout_prob a = max_position_embeddings a = type_vocab_size a = type_sequence_label_size a = initializer_range a = num_labels a = num_choices a = scope a = vocab_size - 1 def __UpperCAmelCase ( self : List[str] ) -> Optional[Any]: a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) a = None if self.use_input_mask: a = random_attention_mask([self.batch_size, self.seq_length] ) a = None if self.use_labels: a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) a = self.get_config() return config, input_ids, input_mask, token_labels def __UpperCAmelCase ( self : Union[str, Any] ) -> str: return GPTNeoXConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=__lowerCamelCase , initializer_range=self.initializer_range , pad_token_id=self.pad_token_id , ) def __UpperCAmelCase ( self : str ) -> Any: a , a , a , a = self.prepare_config_and_inputs() a = True return config, input_ids, input_mask, token_labels def __UpperCAmelCase ( self : Union[str, Any] , __lowerCamelCase : Any , __lowerCamelCase : List[Any] , __lowerCamelCase : Optional[Any] ) -> Optional[int]: a = GPTNeoXModel(config=__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() a = model(__lowerCamelCase , attention_mask=__lowerCamelCase ) a = model(__lowerCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __UpperCAmelCase ( self : Optional[Any] , __lowerCamelCase : str , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : str ) -> Dict: a = True a = GPTNeoXModel(__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() a = model(__lowerCamelCase , attention_mask=__lowerCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __UpperCAmelCase ( self : Optional[int] , __lowerCamelCase : Tuple , __lowerCamelCase : Optional[Any] , __lowerCamelCase : List[str] , __lowerCamelCase : Dict ) -> str: a = GPTNeoXForCausalLM(config=__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() a = model(__lowerCamelCase , attention_mask=__lowerCamelCase , labels=__lowerCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __UpperCAmelCase ( self : Optional[int] , __lowerCamelCase : Dict , __lowerCamelCase : Any , __lowerCamelCase : Optional[int] , __lowerCamelCase : Union[str, Any] ) -> str: a = self.num_labels a = GPTNeoXForQuestionAnswering(__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() a = model(__lowerCamelCase , attention_mask=__lowerCamelCase ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __UpperCAmelCase ( self : Optional[int] , __lowerCamelCase : List[str] , __lowerCamelCase : Optional[int] , __lowerCamelCase : List[Any] , __lowerCamelCase : Optional[Any] ) -> Union[str, Any]: a = self.num_labels a = GPTNeoXForSequenceClassification(__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) a = model(__lowerCamelCase , attention_mask=__lowerCamelCase , labels=__lowerCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __UpperCAmelCase ( self : Union[str, Any] , __lowerCamelCase : List[str] , __lowerCamelCase : Any , __lowerCamelCase : Tuple , __lowerCamelCase : Dict ) -> Dict: a = self.num_labels a = GPTNeoXForTokenClassification(__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() a = model(__lowerCamelCase , attention_mask=__lowerCamelCase , labels=__lowerCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __UpperCAmelCase ( self : int , __lowerCamelCase : Optional[int] , __lowerCamelCase : Tuple , __lowerCamelCase : Dict ) -> List[Any]: a = True a = GPTNeoXForCausalLM(config=__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() # first forward pass a = model(__lowerCamelCase , attention_mask=__lowerCamelCase , use_cache=__lowerCamelCase ) a = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids a = ids_tensor((self.batch_size, 3) , config.vocab_size ) a = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and a = torch.cat([input_ids, next_tokens] , dim=-1 ) a = torch.cat([input_mask, next_mask] , dim=-1 ) a = model(__lowerCamelCase , attention_mask=__lowerCamelCase , output_hidden_states=__lowerCamelCase ) a = output_from_no_past["hidden_states"][0] a = model( __lowerCamelCase , attention_mask=__lowerCamelCase , past_key_values=__lowerCamelCase , output_hidden_states=__lowerCamelCase , )["hidden_states"][0] # select random slice a = ids_tensor((1,) , output_from_past.shape[-1] ).item() a = output_from_no_past[:, -3:, random_slice_idx].detach() a = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(__lowerCamelCase , __lowerCamelCase , atol=1e-3 ) ) def __UpperCAmelCase ( self : Dict ) -> List[Any]: a = self.prepare_config_and_inputs() a , a , a , a = config_and_inputs a = {"input_ids": input_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class snake_case__ (_UpperCamelCase , _UpperCamelCase , _UpperCamelCase , unittest.TestCase ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = ( ( GPTNeoXModel, GPTNeoXForCausalLM, GPTNeoXForQuestionAnswering, GPTNeoXForSequenceClassification, GPTNeoXForTokenClassification, ) if is_torch_available() else () ) SCREAMING_SNAKE_CASE_ : List[Any] = (GPTNeoXForCausalLM,) if is_torch_available() else () SCREAMING_SNAKE_CASE_ : List[str] = ( { """feature-extraction""": GPTNeoXModel, """question-answering""": GPTNeoXForQuestionAnswering, """text-classification""": GPTNeoXForSequenceClassification, """text-generation""": GPTNeoXForCausalLM, """token-classification""": GPTNeoXForTokenClassification, """zero-shot""": GPTNeoXForSequenceClassification, } if is_torch_available() else {} ) SCREAMING_SNAKE_CASE_ : int = False SCREAMING_SNAKE_CASE_ : Optional[Any] = False SCREAMING_SNAKE_CASE_ : int = False SCREAMING_SNAKE_CASE_ : List[Any] = False def __UpperCAmelCase ( self : List[str] ) -> Optional[int]: a = GPTNeoXModelTester(self ) a = ConfigTester(self , config_class=__lowerCamelCase , hidden_size=64 , num_attention_heads=8 ) def __UpperCAmelCase ( self : List[Any] ) -> Union[str, Any]: self.config_tester.run_common_tests() def __UpperCAmelCase ( self : Dict ) -> Dict: a , a , a , a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) def __UpperCAmelCase ( self : Tuple ) -> Tuple: a , a , a , a = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) def __UpperCAmelCase ( self : Optional[Any] ) -> int: # This regression test was failing with PyTorch < 1.3 a , a , a , a = self.model_tester.prepare_config_and_inputs_for_decoder() a = None self.model_tester.create_and_check_model_as_decoder(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) def __UpperCAmelCase ( self : List[Any] ) -> List[Any]: a , a , a , a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) def __UpperCAmelCase ( self : str ) -> List[Any]: a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_causal_lm(*__lowerCamelCase ) def __UpperCAmelCase ( self : Optional[Any] ) -> Optional[Any]: a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*__lowerCamelCase ) def __UpperCAmelCase ( self : str ) -> Union[str, Any]: a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*__lowerCamelCase ) def __UpperCAmelCase ( self : int ) -> str: a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*__lowerCamelCase ) @unittest.skip(reason="Feed forward chunking is not implemented" ) def __UpperCAmelCase ( self : List[Any] ) -> Any: pass @parameterized.expand([("linear",), ("dynamic",)] ) def __UpperCAmelCase ( self : Union[str, Any] , __lowerCamelCase : Union[str, Any] ) -> str: a , a = self.model_tester.prepare_config_and_inputs_for_common() a = ids_tensor([1, 10] , config.vocab_size ) a = ids_tensor([1, int(config.max_position_embeddings * 1.5 )] , config.vocab_size ) set_seed(42 ) # Fixed seed at init time so the two models get the same random weights a = GPTNeoXModel(__lowerCamelCase ) original_model.to(__lowerCamelCase ) original_model.eval() a = original_model(__lowerCamelCase ).last_hidden_state a = original_model(__lowerCamelCase ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights a = {"type": scaling_type, "factor": 10.0} a = GPTNeoXModel(__lowerCamelCase ) scaled_model.to(__lowerCamelCase ) scaled_model.eval() a = scaled_model(__lowerCamelCase ).last_hidden_state a = scaled_model(__lowerCamelCase ).last_hidden_state # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original # maximum sequence length, so the outputs for the short input should match. if scaling_type == "dynamic": self.assertTrue(torch.allclose(__lowerCamelCase , __lowerCamelCase , atol=1e-5 ) ) else: self.assertFalse(torch.allclose(__lowerCamelCase , __lowerCamelCase , atol=1e-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(__lowerCamelCase , __lowerCamelCase , atol=1e-5 ) ) @require_torch class snake_case__ (unittest.TestCase ): """simple docstring""" @slow def __UpperCAmelCase ( self : Any ) -> List[Any]: a = AutoTokenizer.from_pretrained("EleutherAI/pythia-410m-deduped" ) for checkpointing in [True, False]: a = GPTNeoXForCausalLM.from_pretrained("EleutherAI/pythia-410m-deduped" ) if checkpointing: model.gradient_checkpointing_enable() else: model.gradient_checkpointing_disable() model.to(__lowerCamelCase ) a = tokenizer("My favorite food is" , return_tensors="pt" ).to(__lowerCamelCase ) # The hub repo. is updated on 2023-04-04, resulting in poor outputs. # See: https://github.com/huggingface/transformers/pull/24193 a = "My favorite food is a good old-fashioned, old-fashioned, old-fashioned.\n\nI'm not sure" a = model.generate(**__lowerCamelCase , do_sample=__lowerCamelCase , max_new_tokens=20 ) a = tokenizer.batch_decode(__lowerCamelCase )[0] self.assertEqual(__lowerCamelCase , __lowerCamelCase )
107
from copy import deepcopy import torch import torch.nn.functional as F from torch.optim import AdamW from torch.optim.lr_scheduler import LambdaLR from torch.utils.data import DataLoader from accelerate.accelerator import Accelerator from accelerate.state import GradientState from accelerate.test_utils import RegressionDataset, RegressionModel from accelerate.utils import DistributedType, is_torch_version, set_seed def __magic_name__ ( A : Tuple, A : List[Any], A : List[Any], A : Dict ): '''simple docstring''' for param, grad_param in zip(model_a.parameters(), model_b.parameters() ): if not param.requires_grad: continue if not did_step: # Grads should not be in sync assert ( torch.allclose(param.grad, grad_param.grad ) is False ), F"""Gradients in sync when they should not be at iteration {iteration}:\nmodel_a grad ({param.grad}) == model_b grad ({grad_param.grad})""" else: # Grads should be in sync assert ( torch.allclose(param.grad, grad_param.grad ) is True ), F"""Gradients not in sync when they should be at iteration {iteration}:\nmodel_a grad ({param.grad}) != model_b grad ({grad_param.grad})""" def __magic_name__ ( A : List[Any], A : int, A : Optional[Any], A : Optional[int], A : Any=True ): '''simple docstring''' model.train() a = model(A ) a = F.mse_loss(A, target.to(output.device ) ) if not do_backward: loss /= accelerator.gradient_accumulation_steps loss.backward() else: accelerator.backward(A ) def __magic_name__ ( A : Any, A : Any=False ): '''simple docstring''' set_seed(42 ) a = RegressionModel() a = deepcopy(A ) a = RegressionDataset(length=80 ) a = DataLoader(A, batch_size=16 ) model.to(accelerator.device ) if sched: a = AdamW(params=model.parameters(), lr=1E-3 ) a = AdamW(params=ddp_model.parameters(), lr=1E-3 ) a = LambdaLR(A, lr_lambda=lambda A : epoch**0.65 ) a = LambdaLR(A, lr_lambda=lambda A : epoch**0.65 ) # Make a copy of `model` if sched: a , a , a , a = accelerator.prepare(A, A, A, A ) else: a , a = accelerator.prepare(A, A ) if sched: return (model, opt, sched, dataloader, ddp_model, ddp_opt, ddp_sched) return model, ddp_model, dataloader def __magic_name__ ( A : List[Any] ): '''simple docstring''' a , a , a = get_training_setup(A ) # Use a single batch a , a = next(iter(A ) ).values() for iteration in range(3 ): # Gather the distributed inputs and targs for the base model a , a = accelerator.gather((ddp_input, ddp_target) ) a , a = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" step_model(A, A, A, A ) # Do "gradient accumulation" (noop) if iteration % 2 == 0: # Accumulate grads locally with accelerator.no_sync(A ): step_model(A, A, A, A ) else: # Sync grads step_model(A, A, A, A ) # Since `no_sync` is a noop, `ddp_model` and `model` grads should always be in sync check_model_parameters(A, A, A, A ) for param, ddp_param in zip(model.parameters(), ddp_model.parameters() ): if not param.requires_grad: continue assert torch.allclose( param.grad, ddp_param.grad ), F"""Gradients not in sync when they should be:\nModel grad ({param.grad}) != DDP grad ({ddp_param.grad})""" # Shuffle ddp_input on each iteration torch.manual_seed(1337 + iteration ) a = ddp_input[torch.randperm(len(A ) )] def __magic_name__ ( A : Optional[int] ): '''simple docstring''' a , a , a = get_training_setup(A ) # Use a single batch a , a = next(iter(A ) ).values() for iteration in range(3 ): # Gather the distributed inputs and targs for the base model a , a = accelerator.gather((ddp_input, ddp_target) ) a , a = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" step_model(A, A, A, A ) # Do "gradient accumulation" (noop) if iteration % 2 == 0: # Accumulate grads locally with accelerator.no_sync(A ): step_model(A, A, A, A ) else: # Sync grads step_model(A, A, A, A ) # DDP model and model should only be in sync when not (iteration % 2 == 0) for param, ddp_param in zip(model.parameters(), ddp_model.parameters() ): if not param.requires_grad: continue if iteration % 2 == 0: # Grads should not be in sync assert ( torch.allclose(param.grad, ddp_param.grad ) is False ), F"""Gradients in sync when they should not be:\nModel grad ({param.grad}) == DDP grad ({ddp_param.grad})""" else: # Grads should be in sync assert ( torch.allclose(param.grad, ddp_param.grad ) is True ), F"""Gradients not in sync when they should be:\nModel grad ({param.grad}) != DDP grad ({ddp_param.grad})""" # Shuffle ddp_input on each iteration torch.manual_seed(1337 + iteration ) a = ddp_input[torch.randperm(len(A ) )] def __magic_name__ ( A : List[Any]=False, A : List[Any]=False ): '''simple docstring''' a = Accelerator( split_batches=A, dispatch_batches=A, gradient_accumulation_steps=2 ) # Test that context manager behaves properly a , a , a = get_training_setup(A ) for iteration, batch in enumerate(A ): a , a = batch.values() # Gather the distributed inputs and targs for the base model a , a = accelerator.gather((ddp_input, ddp_target) ) a , a = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" step_model(A, A, A, A, A ) # Do "gradient accumulation" (noop) with accelerator.accumulate(A ): step_model(A, A, A, A ) # DDP model and model should only be in sync when not (iteration % 2 == 0) for param, ddp_param in zip(model.parameters(), ddp_model.parameters() ): if not param.requires_grad: continue if ((iteration + 1) % 2 == 0) or (iteration == len(A ) - 1): # Grads should be in sync assert ( torch.allclose(param.grad, ddp_param.grad ) is True ), F"""Gradients not in sync when they should be at iteration {iteration}:\nModel grad ({param.grad}) != DDP grad ({ddp_param.grad})""" else: # Grads should not be in sync assert ( torch.allclose(param.grad, ddp_param.grad ) is False ), F"""Gradients in sync when they should not be at iteration {iteration}:\nModel grad ({param.grad}) == DDP grad ({ddp_param.grad})""" # Shuffle ddp_input on each iteration torch.manual_seed(1337 + iteration ) a = ddp_input[torch.randperm(len(A ) )] GradientState._reset_state() def __magic_name__ ( A : List[Any]=False, A : Any=False ): '''simple docstring''' a = Accelerator( split_batches=A, dispatch_batches=A, gradient_accumulation_steps=2 ) # Test that context manager behaves properly a , a , a , a , a , a , a = get_training_setup(A, A ) for iteration, batch in enumerate(A ): a , a = batch.values() # Gather the distributed inputs and targs for the base model a , a = accelerator.gather((ddp_input, ddp_target) ) a , a = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" model.train() ddp_model.train() step_model(A, A, A, A, A ) opt.step() if ((iteration + 1) % 2 == 0) or ((iteration + 1) == len(A )): if split_batches: sched.step() else: for _ in range(accelerator.num_processes ): sched.step() opt.zero_grad() # Perform gradient accumulation under wrapper with accelerator.accumulate(A ): step_model(A, A, A, A ) ddp_opt.step() ddp_sched.step() ddp_opt.zero_grad() # Learning rates should be the same assert ( opt.param_groups[0]["lr"] == ddp_opt.param_groups[0]["lr"] ), F"""Learning rates found in each optimizer did not align\nopt: {opt.param_groups[0]["lr"]}\nDDP opt: {ddp_opt.param_groups[0]["lr"]}\n""" a = (((iteration + 1) % 2) == 0) or ((iteration + 1) == len(A )) if accelerator.num_processes > 1: check_model_parameters(A, A, A, A ) # Shuffle ddp_input on each iteration torch.manual_seed(1337 + iteration ) GradientState._reset_state() def __magic_name__ ( ): '''simple docstring''' a = Accelerator() a = RegressionDataset(length=80 ) a = DataLoader(A, batch_size=16 ) a = RegressionDataset(length=96 ) a = DataLoader(A, batch_size=16 ) a , a = accelerator.prepare(A, A ) assert accelerator.gradient_state.active_dataloader is None for iteration, _ in enumerate(A ): assert id(accelerator.gradient_state.active_dataloader ) == id(A ) if iteration < len(A ) - 1: assert not accelerator.gradient_state.end_of_dataloader if iteration == 1: for batch_num, _ in enumerate(A ): assert id(accelerator.gradient_state.active_dataloader ) == id(A ) if batch_num < len(A ) - 1: assert not accelerator.gradient_state.end_of_dataloader else: assert accelerator.gradient_state.end_of_dataloader else: assert accelerator.gradient_state.end_of_dataloader assert accelerator.gradient_state.active_dataloader is None def __magic_name__ ( ): '''simple docstring''' a = Accelerator() a = accelerator.state if state.local_process_index == 0: print("**Test `accumulate` gradient accumulation with dataloader break**" ) test_dataloader_break() if state.distributed_type == DistributedType.NO: if state.local_process_index == 0: print("**Test NOOP `no_sync` context manager**" ) test_noop_sync(A ) if state.distributed_type in (DistributedType.MULTI_GPU, DistributedType.MULTI_CPU): if state.local_process_index == 0: print("**Test Distributed `no_sync` context manager**" ) test_distributed_sync(A ) if state.distributed_type == DistributedType.MULTI_GPU: for split_batch in [True, False]: for dispatch_batches in [True, False]: if state.local_process_index == 0: print( "**Test `accumulate` gradient accumulation, ", F"""`split_batches={split_batch}` and `dispatch_batches={dispatch_batches}`**""", ) test_gradient_accumulation(A, A ) # Currently will break on torch 2.0 +, need to investigate why if is_torch_version("<", "2.0" ) or state.distributed_type == DistributedType.NO: if state.local_process_index == 0: print( "**Test `accumulate` gradient accumulation with optimizer and scheduler, ", "`split_batches=False`, `dispatch_batches=False`**", ) test_gradient_accumulation_with_opt_and_scheduler() if state.distributed_type == DistributedType.MULTI_GPU: for split_batch in [True, False]: for dispatch_batches in [True, False]: if not split_batch and not dispatch_batches: continue if state.local_process_index == 0: print( "**Test `accumulate` gradient accumulation with optimizer and scheduler, ", F"""`split_batches={split_batch}` and `dispatch_batches={dispatch_batches}`**""", ) test_gradient_accumulation_with_opt_and_scheduler(A, A ) def __magic_name__ ( A : Optional[int] ): '''simple docstring''' main() if __name__ == "__main__": main()
107
1
"""simple docstring""" from argparse import ArgumentParser from datasets.commands.convert import ConvertCommand from datasets.commands.dummy_data import DummyDataCommand from datasets.commands.env import EnvironmentCommand from datasets.commands.run_beam import RunBeamCommand from datasets.commands.test import TestCommand from datasets.utils.logging import set_verbosity_info def __SCREAMING_SNAKE_CASE ( __UpperCAmelCase ): return {key.lstrip("""-""" ): value for key, value in zip(unknown_args[::2] , unknown_args[1::2] )} def __SCREAMING_SNAKE_CASE ( ): _lowercase : Optional[Any] = ArgumentParser( """HuggingFace Datasets CLI tool""" , usage="""datasets-cli <command> [<args>]""" , allow_abbrev=__UpperCAmelCase ) _lowercase : Dict = parser.add_subparsers(help="""datasets-cli command helpers""" ) set_verbosity_info() # Register commands ConvertCommand.register_subcommand(__UpperCAmelCase ) EnvironmentCommand.register_subcommand(__UpperCAmelCase ) TestCommand.register_subcommand(__UpperCAmelCase ) RunBeamCommand.register_subcommand(__UpperCAmelCase ) DummyDataCommand.register_subcommand(__UpperCAmelCase ) # Parse args _lowercase : Optional[int] = parser.parse_known_args() if not hasattr(__UpperCAmelCase , """func""" ): parser.print_help() exit(1 ) _lowercase : List[str] = parse_unknown_args(__UpperCAmelCase ) # Run _lowercase : Optional[int] = args.func(__UpperCAmelCase , **__UpperCAmelCase ) service.run() if __name__ == "__main__": main()
359
"""simple docstring""" import cva import numpy as np class UpperCamelCase : """simple docstring""" def __init__( self ,UpperCAmelCase_ ,UpperCAmelCase_ ): if k in (0.04, 0.06): _lowercase : Optional[Any] = k _lowercase : Optional[Any] = window_size else: raise ValueError("""invalid k value""" ) def __str__( self ): return str(self.k ) def lowerCamelCase__ ( self ,UpperCAmelCase_ ): _lowercase : List[str] = cva.imread(UpperCAmelCase_ ,0 ) _lowercase , _lowercase : Dict = img.shape _lowercase : list[list[int]] = [] _lowercase : int = img.copy() _lowercase : List[str] = cva.cvtColor(UpperCAmelCase_ ,cva.COLOR_GRAY2RGB ) _lowercase , _lowercase : Optional[Any] = np.gradient(UpperCAmelCase_ ) _lowercase : Optional[int] = dx**2 _lowercase : Optional[Any] = dy**2 _lowercase : Optional[Any] = dx * dy _lowercase : List[str] = 0.04 _lowercase : Optional[Any] = self.window_size // 2 for y in range(UpperCAmelCase_ ,h - offset ): for x in range(UpperCAmelCase_ ,w - offset ): _lowercase : Optional[Any] = ixx[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() _lowercase : Dict = iyy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() _lowercase : Union[str, Any] = ixy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() _lowercase : int = (wxx * wyy) - (wxy**2) _lowercase : Union[str, Any] = wxx + wyy _lowercase : Union[str, Any] = det - k * (trace**2) # Can change the value if r > 0.5: corner_list.append([x, y, r] ) color_img.itemset((y, x, 0) ,0 ) color_img.itemset((y, x, 1) ,0 ) color_img.itemset((y, x, 2) ,2_55 ) return color_img, corner_list if __name__ == "__main__": UpperCAmelCase: Optional[int] = HarrisCorner(0.04, 3) UpperCAmelCase , UpperCAmelCase: List[Any] = edge_detect.detect("""path_to_image""") cva.imwrite("""detect.png""", color_img)
336
0
import gc import unittest from diffusers import FlaxDPMSolverMultistepScheduler, FlaxStableDiffusionPipeline from diffusers.utils import is_flax_available, slow from diffusers.utils.testing_utils import require_flax if is_flax_available(): import jax import jax.numpy as jnp from flax.jax_utils import replicate from flax.training.common_utils import shard @slow @require_flax class snake_case_ ( unittest.TestCase ): def __UpperCamelCase ( self : List[Any] ) -> Tuple: # clean up the VRAM after each test super().tearDown() gc.collect() def __UpperCamelCase ( self : Tuple ) -> Dict: lowercase__ , lowercase__ : int = FlaxStableDiffusionPipeline.from_pretrained( "stabilityai/stable-diffusion-2" , revision="bf16" , dtype=jnp.bfloataa , ) lowercase__ : Union[str, Any] = "A painting of a squirrel eating a burger" lowercase__ : Any = jax.device_count() lowercase__ : str = num_samples * [prompt] lowercase__ : int = sd_pipe.prepare_inputs(lowercase_ ) lowercase__ : Optional[int] = replicate(lowercase_ ) lowercase__ : Tuple = shard(lowercase_ ) lowercase__ : Tuple = jax.random.PRNGKey(0 ) lowercase__ : str = jax.random.split(lowercase_ , jax.device_count() ) lowercase__ : int = sd_pipe(lowercase_ , lowercase_ , lowercase_ , num_inference_steps=25 , jit=lowercase_ )[0] assert images.shape == (jax.device_count(), 1, 7_68, 7_68, 3) lowercase__ : Dict = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] ) lowercase__ : List[Any] = images[0, 2_53:2_56, 2_53:2_56, -1] lowercase__ : Optional[Any] = jnp.asarray(jax.device_get(image_slice.flatten() ) ) lowercase__ : Dict = jnp.array([0.42_38, 0.44_14, 0.43_95, 0.44_53, 0.46_29, 0.45_90, 0.45_31, 0.4_55_08, 0.45_12] ) print(F'''output_slice: {output_slice}''' ) assert jnp.abs(output_slice - expected_slice ).max() < 1E-2 def __UpperCamelCase ( self : int ) -> str: lowercase__ : List[str] = "stabilityai/stable-diffusion-2" lowercase__ , lowercase__ : Optional[Any] = FlaxDPMSolverMultistepScheduler.from_pretrained(lowercase_ , subfolder="scheduler" ) lowercase__ , lowercase__ : int = FlaxStableDiffusionPipeline.from_pretrained( lowercase_ , scheduler=lowercase_ , revision="bf16" , dtype=jnp.bfloataa , ) lowercase__ : Tuple = scheduler_params lowercase__ : int = "A painting of a squirrel eating a burger" lowercase__ : Optional[int] = jax.device_count() lowercase__ : Union[str, Any] = num_samples * [prompt] lowercase__ : Any = sd_pipe.prepare_inputs(lowercase_ ) lowercase__ : Union[str, Any] = replicate(lowercase_ ) lowercase__ : Any = shard(lowercase_ ) lowercase__ : Optional[int] = jax.random.PRNGKey(0 ) lowercase__ : Tuple = jax.random.split(lowercase_ , jax.device_count() ) lowercase__ : Tuple = sd_pipe(lowercase_ , lowercase_ , lowercase_ , num_inference_steps=25 , jit=lowercase_ )[0] assert images.shape == (jax.device_count(), 1, 7_68, 7_68, 3) lowercase__ : Any = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] ) lowercase__ : Union[str, Any] = images[0, 2_53:2_56, 2_53:2_56, -1] lowercase__ : int = jnp.asarray(jax.device_get(image_slice.flatten() ) ) lowercase__ : List[str] = jnp.array([0.43_36, 0.4_29_69, 0.44_53, 0.41_99, 0.42_97, 0.45_31, 0.44_34, 0.44_34, 0.42_97] ) print(F'''output_slice: {output_slice}''' ) assert jnp.abs(output_slice - expected_slice ).max() < 1E-2
87
import operator def lowercase_ ( _lowerCamelCase : list , _lowerCamelCase : bool = False , _lowerCamelCase : list | None = None): lowercase__ : int = operator.lt if reverse else operator.gt lowercase__ : str = solution or [] if not arr: return solution lowercase__ : List[str] = [arr.pop(0)] for i, item in enumerate(_lowerCamelCase): if _operator(_lowerCamelCase , sublist[-1]): sublist.append(_lowerCamelCase) arr.pop(_lowerCamelCase) # merging sublist into solution list if not solution: solution.extend(_lowerCamelCase) else: while sublist: lowercase__ : str = sublist.pop(0) for i, xx in enumerate(_lowerCamelCase): if not _operator(_lowerCamelCase , _lowerCamelCase): solution.insert(_lowerCamelCase , _lowerCamelCase) break else: solution.append(_lowerCamelCase) strand_sort(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase) return solution if __name__ == "__main__": assert strand_sort([4, 3, 5, 1, 2]) == [1, 2, 3, 4, 5] assert strand_sort([4, 3, 5, 1, 2], reverse=True) == [5, 4, 3, 2, 1]
87
1
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, StableDiffusionSAGPipeline, UNetaDConditionModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class _snake_case (__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase): __A : Union[str, Any] =StableDiffusionSAGPipeline __A : Optional[int] =TEXT_TO_IMAGE_PARAMS __A : str =TEXT_TO_IMAGE_BATCH_PARAMS __A : int =TEXT_TO_IMAGE_IMAGE_PARAMS __A : Union[str, Any] =TEXT_TO_IMAGE_IMAGE_PARAMS __A : Union[str, Any] =False def UpperCamelCase__ ( self ): torch.manual_seed(0 ) UpperCAmelCase_ : Union[str, Any] = UNetaDConditionModel( block_out_channels=(32, 64) ,layers_per_block=2 ,sample_size=32 ,in_channels=4 ,out_channels=4 ,down_block_types=("DownBlock2D", "CrossAttnDownBlock2D") ,up_block_types=("CrossAttnUpBlock2D", "UpBlock2D") ,cross_attention_dim=32 ,) UpperCAmelCase_ : Tuple = DDIMScheduler( beta_start=0.00085 ,beta_end=0.012 ,beta_schedule="scaled_linear" ,clip_sample=_snake_case ,set_alpha_to_one=_snake_case ,) torch.manual_seed(0 ) UpperCAmelCase_ : Any = AutoencoderKL( block_out_channels=[32, 64] ,in_channels=3 ,out_channels=3 ,down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"] ,up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"] ,latent_channels=4 ,) torch.manual_seed(0 ) UpperCAmelCase_ : Union[str, Any] = CLIPTextConfig( bos_token_id=0 ,eos_token_id=2 ,hidden_size=32 ,intermediate_size=37 ,layer_norm_eps=1E-05 ,num_attention_heads=4 ,num_hidden_layers=5 ,pad_token_id=1 ,vocab_size=10_00 ,) UpperCAmelCase_ : Union[str, Any] = CLIPTextModel(_snake_case ) UpperCAmelCase_ : Dict = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) UpperCAmelCase_ : Optional[int] = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, "safety_checker": None, "feature_extractor": None, } return components def UpperCamelCase__ ( self ,_snake_case ,_snake_case=0 ): if str(_snake_case ).startswith("mps" ): UpperCAmelCase_ : Dict = torch.manual_seed(_snake_case ) else: UpperCAmelCase_ : List[Any] = torch.Generator(device=_snake_case ).manual_seed(_snake_case ) UpperCAmelCase_ : Dict = { "prompt": ".", "generator": generator, "num_inference_steps": 2, "guidance_scale": 1.0, "sag_scale": 1.0, "output_type": "numpy", } return inputs def UpperCamelCase__ ( self ): super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class _snake_case (unittest.TestCase): def UpperCamelCase__ ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = StableDiffusionSAGPipeline.from_pretrained("CompVis/stable-diffusion-v1-4" ) UpperCAmelCase_ : Tuple = sag_pipe.to(_snake_case ) sag_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Optional[int] = "." UpperCAmelCase_ : Optional[int] = torch.manual_seed(0 ) UpperCAmelCase_ : List[str] = sag_pipe( [prompt] ,generator=_snake_case ,guidance_scale=7.5 ,sag_scale=1.0 ,num_inference_steps=20 ,output_type="np" ) UpperCAmelCase_ : Tuple = output.images UpperCAmelCase_ : int = image[0, -3:, -3:, -1] assert image.shape == (1, 5_12, 5_12, 3) UpperCAmelCase_ : List[Any] = np.array([0.1568, 0.1738, 0.1695, 0.1693, 0.1507, 0.1705, 0.1547, 0.1751, 0.1949] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[int] = StableDiffusionSAGPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base" ) UpperCAmelCase_ : Optional[Any] = sag_pipe.to(_snake_case ) sag_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : Union[str, Any] = "." UpperCAmelCase_ : Dict = torch.manual_seed(0 ) UpperCAmelCase_ : Union[str, Any] = sag_pipe( [prompt] ,generator=_snake_case ,guidance_scale=7.5 ,sag_scale=1.0 ,num_inference_steps=20 ,output_type="np" ) UpperCAmelCase_ : Optional[Any] = output.images UpperCAmelCase_ : Optional[int] = image[0, -3:, -3:, -1] assert image.shape == (1, 5_12, 5_12, 3) UpperCAmelCase_ : Union[str, Any] = np.array([0.3459, 0.2876, 0.2537, 0.3002, 0.2671, 0.2160, 0.3026, 0.2262, 0.2371] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = StableDiffusionSAGPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base" ) UpperCAmelCase_ : str = sag_pipe.to(_snake_case ) sag_pipe.set_progress_bar_config(disable=_snake_case ) UpperCAmelCase_ : str = "." UpperCAmelCase_ : Union[str, Any] = torch.manual_seed(0 ) UpperCAmelCase_ : int = sag_pipe( [prompt] ,width=7_68 ,height=5_12 ,generator=_snake_case ,guidance_scale=7.5 ,sag_scale=1.0 ,num_inference_steps=20 ,output_type="np" ,) UpperCAmelCase_ : Union[str, Any] = output.images assert image.shape == (1, 5_12, 7_68, 3)
67
'''simple docstring''' import re def a__ ( _SCREAMING_SNAKE_CASE : str ) -> str: """simple docstring""" if len(re.findall("[ATCG]" , _SCREAMING_SNAKE_CASE ) ) != len(_SCREAMING_SNAKE_CASE ): raise ValueError("Invalid Strand" ) return dna.translate(dna.maketrans("ATCG" , "TAGC" ) ) if __name__ == "__main__": import doctest doctest.testmod()
67
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available __lowerCamelCase = {"configuration_speech_encoder_decoder": ["SpeechEncoderDecoderConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = ["SpeechEncoderDecoderModel"] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = ["FlaxSpeechEncoderDecoderModel"] if TYPE_CHECKING: from .configuration_speech_encoder_decoder import SpeechEncoderDecoderConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speech_encoder_decoder import SpeechEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_speech_encoder_decoder import FlaxSpeechEncoderDecoderModel else: import sys __lowerCamelCase = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
221
def A ( _lowerCamelCase ): '''simple docstring''' if bit_count < 0: raise ValueError("The given input must be positive" ) # get the generated string sequence _lowerCAmelCase : List[str] = gray_code_sequence_string(_lowerCamelCase ) # # convert them to integers for i in range(len(_lowerCamelCase ) ): _lowerCAmelCase : List[str] = int(sequence[i] , 2 ) return sequence def A ( _lowerCamelCase ): '''simple docstring''' if bit_count == 0: return ["0"] if bit_count == 1: return ["0", "1"] _lowerCAmelCase : List[Any] = 1 << bit_count # defines the length of the sequence # 1<< n is equivalent to 2^n # recursive answer will generate answer for n-1 bits _lowerCAmelCase : Optional[int] = gray_code_sequence_string(bit_count - 1 ) _lowerCAmelCase : str = [] # append 0 to first half of the smaller sequence generated for i in range(seq_len // 2 ): _lowerCAmelCase : Dict = "0" + smaller_sequence[i] sequence.append(_lowerCamelCase ) # append 1 to second half ... start from the end of the list for i in reversed(range(seq_len // 2 ) ): _lowerCAmelCase : Optional[Any] = "1" + smaller_sequence[i] sequence.append(_lowerCamelCase ) return sequence if __name__ == "__main__": import doctest doctest.testmod()
36
0
"""simple docstring""" import json import os from functools import lru_cache from typing import Dict, List, Optional, Tuple, Union import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...tokenization_utils_base import BatchEncoding, EncodedInput from ...utils import PaddingStrategy, logging __A = logging.get_logger(__name__) __A = {"vocab_file": "vocab.json", "merges_file": "merges.txt"} # See all LED models at https://huggingface.co/models?filter=LED __A = { "vocab_file": { "allenai/led-base-16384": "https://huggingface.co/allenai/led-base-16384/resolve/main/vocab.json", }, "merges_file": { "allenai/led-base-16384": "https://huggingface.co/allenai/led-base-16384/resolve/main/merges.txt", }, "tokenizer_file": { "allenai/led-base-16384": "https://huggingface.co/allenai/led-base-16384/resolve/main/tokenizer.json", }, } __A = { "allenai/led-base-16384": 1_6_3_8_4, } @lru_cache() # Copied from transformers.models.bart.tokenization_bart.bytes_to_unicode def SCREAMING_SNAKE_CASE__ ( ) -> Optional[Any]: lowercase__: List[str] = ( list(range(ord('''!''' ) , ord('''~''' ) + 1 ) ) + list(range(ord('''¡''' ) , ord('''¬''' ) + 1 ) ) + list(range(ord('''®''' ) , ord('''ÿ''' ) + 1 ) ) ) lowercase__: str = bs[:] lowercase__: Optional[int] = 0 for b in range(2**8 ): if b not in bs: bs.append(lowerCAmelCase__ ) cs.append(2**8 + n ) n += 1 lowercase__: List[Any] = [chr(lowerCAmelCase__ ) for n in cs] return dict(zip(lowerCAmelCase__ , lowerCAmelCase__ ) ) def SCREAMING_SNAKE_CASE__ ( __UpperCAmelCase ) -> Any: lowercase__: Union[str, Any] = set() lowercase__: Optional[int] = word[0] for char in word[1:]: pairs.add((prev_char, char) ) lowercase__: Tuple = char return pairs class UpperCAmelCase (_SCREAMING_SNAKE_CASE ): """simple docstring""" _UpperCAmelCase :str = VOCAB_FILES_NAMES _UpperCAmelCase :str = PRETRAINED_VOCAB_FILES_MAP _UpperCAmelCase :Tuple = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _UpperCAmelCase :List[Any] = ["input_ids", "attention_mask"] def __init__( self , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase="replace" , _UpperCAmelCase="<s>" , _UpperCAmelCase="</s>" , _UpperCAmelCase="</s>" , _UpperCAmelCase="<s>" , _UpperCAmelCase="<unk>" , _UpperCAmelCase="<pad>" , _UpperCAmelCase="<mask>" , _UpperCAmelCase=False , **_UpperCAmelCase , ): lowercase__: Any = AddedToken(_UpperCAmelCase , lstrip=_UpperCAmelCase , rstrip=_UpperCAmelCase ) if isinstance(_UpperCAmelCase , _UpperCAmelCase ) else bos_token lowercase__: List[Any] = AddedToken(_UpperCAmelCase , lstrip=_UpperCAmelCase , rstrip=_UpperCAmelCase ) if isinstance(_UpperCAmelCase , _UpperCAmelCase ) else eos_token lowercase__: Tuple = AddedToken(_UpperCAmelCase , lstrip=_UpperCAmelCase , rstrip=_UpperCAmelCase ) if isinstance(_UpperCAmelCase , _UpperCAmelCase ) else sep_token lowercase__: Union[str, Any] = AddedToken(_UpperCAmelCase , lstrip=_UpperCAmelCase , rstrip=_UpperCAmelCase ) if isinstance(_UpperCAmelCase , _UpperCAmelCase ) else cls_token lowercase__: List[Any] = AddedToken(_UpperCAmelCase , lstrip=_UpperCAmelCase , rstrip=_UpperCAmelCase ) if isinstance(_UpperCAmelCase , _UpperCAmelCase ) else unk_token lowercase__: 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 lowercase__: List[str] = AddedToken(_UpperCAmelCase , lstrip=_UpperCAmelCase , rstrip=_UpperCAmelCase ) if isinstance(_UpperCAmelCase , _UpperCAmelCase ) else mask_token super().__init__( errors=_UpperCAmelCase , bos_token=_UpperCAmelCase , eos_token=_UpperCAmelCase , unk_token=_UpperCAmelCase , sep_token=_UpperCAmelCase , cls_token=_UpperCAmelCase , pad_token=_UpperCAmelCase , mask_token=_UpperCAmelCase , add_prefix_space=_UpperCAmelCase , **_UpperCAmelCase , ) with open(_UpperCAmelCase , encoding='''utf-8''' ) as vocab_handle: lowercase__: Any = json.load(_UpperCAmelCase ) lowercase__: Tuple = {v: k for k, v in self.encoder.items()} lowercase__: Any = errors # how to handle errors in decoding lowercase__: int = bytes_to_unicode() lowercase__: List[str] = {v: k for k, v in self.byte_encoder.items()} with open(_UpperCAmelCase , encoding='''utf-8''' ) as merges_handle: lowercase__: Optional[Any] = merges_handle.read().split('''\n''' )[1:-1] lowercase__: Union[str, Any] = [tuple(merge.split() ) for merge in bpe_merges] lowercase__: Union[str, Any] = dict(zip(_UpperCAmelCase , range(len(_UpperCAmelCase ) ) ) ) lowercase__: List[str] = {} lowercase__: List[str] = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions lowercase__: Optional[int] = re.compile(r'''\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+''' ) @property # Copied from transformers.models.bart.tokenization_bart.BartTokenizer.vocab_size def _snake_case ( self ): return len(self.encoder ) def _snake_case ( self ): return dict(self.encoder , **self.added_tokens_encoder ) def _snake_case ( self , _UpperCAmelCase ): if token in self.cache: return self.cache[token] lowercase__: Tuple = tuple(_UpperCAmelCase ) lowercase__: int = get_pairs(_UpperCAmelCase ) if not pairs: return token while True: lowercase__: List[Any] = min(_UpperCAmelCase , key=lambda _UpperCAmelCase : self.bpe_ranks.get(_UpperCAmelCase , float('''inf''' ) ) ) if bigram not in self.bpe_ranks: break lowercase__, lowercase__: Dict = bigram lowercase__: Optional[int] = [] lowercase__: List[Any] = 0 while i < len(_UpperCAmelCase ): try: lowercase__: Optional[Any] = word.index(_UpperCAmelCase , _UpperCAmelCase ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) lowercase__: Optional[int] = j if word[i] == first and i < len(_UpperCAmelCase ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 lowercase__: Any = tuple(_UpperCAmelCase ) lowercase__: Any = new_word if len(_UpperCAmelCase ) == 1: break else: lowercase__: List[Any] = get_pairs(_UpperCAmelCase ) lowercase__: str = ''' '''.join(_UpperCAmelCase ) lowercase__: List[str] = word return word def _snake_case ( self , _UpperCAmelCase ): lowercase__: Optional[Any] = [] for token in re.findall(self.pat , _UpperCAmelCase ): lowercase__: Any = ''''''.join( self.byte_encoder[b] for b in token.encode('''utf-8''' ) ) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(_UpperCAmelCase ).split(''' ''' ) ) return bpe_tokens def _snake_case ( self , _UpperCAmelCase ): return self.encoder.get(_UpperCAmelCase , self.encoder.get(self.unk_token ) ) def _snake_case ( self , _UpperCAmelCase ): return self.decoder.get(_UpperCAmelCase ) def _snake_case ( self , _UpperCAmelCase ): lowercase__: Optional[int] = ''''''.join(_UpperCAmelCase ) lowercase__: Union[str, Any] = bytearray([self.byte_decoder[c] for c in text] ).decode('''utf-8''' , errors=self.errors ) return text def _snake_case ( self , _UpperCAmelCase , _UpperCAmelCase = None ): if not os.path.isdir(_UpperCAmelCase ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return lowercase__: str = os.path.join( _UpperCAmelCase , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) lowercase__: str = os.path.join( _UpperCAmelCase , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''merges_file'''] ) with open(_UpperCAmelCase , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=_UpperCAmelCase , ensure_ascii=_UpperCAmelCase ) + '''\n''' ) lowercase__: str = 0 with open(_UpperCAmelCase , '''w''' , encoding='''utf-8''' ) as writer: writer.write('''#version: 0.2\n''' ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda _UpperCAmelCase : kv[1] ): if index != token_index: logger.warning( F"""Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.""" ''' Please check that the tokenizer is not corrupted!''' ) lowercase__: List[str] = token_index writer.write(''' '''.join(_UpperCAmelCase ) + '''\n''' ) index += 1 return vocab_file, merge_file def _snake_case ( self , _UpperCAmelCase , _UpperCAmelCase = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] lowercase__: Tuple = [self.cls_token_id] lowercase__: Optional[int] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def _snake_case ( self , _UpperCAmelCase , _UpperCAmelCase = None , _UpperCAmelCase = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_UpperCAmelCase , token_ids_a=_UpperCAmelCase , already_has_special_tokens=_UpperCAmelCase ) if token_ids_a is None: return [1] + ([0] * len(_UpperCAmelCase )) + [1] return [1] + ([0] * len(_UpperCAmelCase )) + [1, 1] + ([0] * len(_UpperCAmelCase )) + [1] def _snake_case ( self , _UpperCAmelCase , _UpperCAmelCase = None ): lowercase__: Union[str, Any] = [self.sep_token_id] lowercase__: Optional[int] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def _snake_case ( self , _UpperCAmelCase , _UpperCAmelCase=False , **_UpperCAmelCase ): lowercase__: str = kwargs.pop('''add_prefix_space''' , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(_UpperCAmelCase ) > 0 and not text[0].isspace()): lowercase__: Union[str, Any] = ''' ''' + text return (text, kwargs) def _snake_case ( self , _UpperCAmelCase , _UpperCAmelCase = None , _UpperCAmelCase = PaddingStrategy.DO_NOT_PAD , _UpperCAmelCase = None , _UpperCAmelCase = None , ): lowercase__: List[str] = super()._pad( encoded_inputs=_UpperCAmelCase , max_length=_UpperCAmelCase , padding_strategy=_UpperCAmelCase , pad_to_multiple_of=_UpperCAmelCase , return_attention_mask=_UpperCAmelCase , ) # Load from model defaults if return_attention_mask is None: lowercase__: str = '''attention_mask''' in self.model_input_names if return_attention_mask and "global_attention_mask" in encoded_inputs: lowercase__: Optional[int] = encoded_inputs[self.model_input_names[0]] # `global_attention_mask` need to have the same length as other (sequential) inputs. lowercase__: Dict = len(encoded_inputs['''global_attention_mask'''] ) != len(_UpperCAmelCase ) if needs_to_be_padded: lowercase__: Any = len(_UpperCAmelCase ) - len(encoded_inputs['''global_attention_mask'''] ) if self.padding_side == "right": # Use `-1` since `0` in `global_attention_mask` means `local attention` instead of `not to attend` lowercase__: Tuple = ( encoded_inputs['''global_attention_mask'''] + [-1] * difference ) elif self.padding_side == "left": lowercase__: List[str] = [-1] * difference + encoded_inputs[ '''global_attention_mask''' ] else: raise ValueError('''Invalid padding strategy:''' + str(self.padding_side ) ) return encoded_inputs
365
"""simple docstring""" import unittest from transformers import DonutProcessor __A = "naver-clova-ix/donut-base" class UpperCAmelCase (unittest.TestCase ): """simple docstring""" def _snake_case ( self ): lowercase__: int = DonutProcessor.from_pretrained(_UpperCAmelCase ) def _snake_case ( self ): lowercase__: Tuple = { '''name''': '''John Doe''', '''age''': '''99''', '''city''': '''Atlanta''', '''state''': '''GA''', '''zip''': '''30301''', '''phone''': '''123-4567''', '''nicknames''': [{'''nickname''': '''Johnny'''}, {'''nickname''': '''JD'''}], } lowercase__: Union[str, Any] = ( '''<s_name>John Doe</s_name><s_age>99</s_age><s_city>Atlanta</s_city>''' '''<s_state>GA</s_state><s_zip>30301</s_zip><s_phone>123-4567</s_phone>''' '''<s_nicknames><s_nickname>Johnny</s_nickname>''' '''<sep/><s_nickname>JD</s_nickname></s_nicknames>''' ) lowercase__: str = self.processor.tokenajson(_UpperCAmelCase ) self.assertDictEqual(_UpperCAmelCase , _UpperCAmelCase )
2
0
'''simple docstring''' import argparse import torch from safetensors.torch import load_file from diffusers import StableDiffusionPipeline def lowerCamelCase_ ( SCREAMING_SNAKE_CASE__ : int, SCREAMING_SNAKE_CASE__ : Tuple, SCREAMING_SNAKE_CASE__ : Optional[Any], SCREAMING_SNAKE_CASE__ : List[str], SCREAMING_SNAKE_CASE__ : Any ) -> Tuple: # load base model UpperCAmelCase_ : int = StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE__, torch_dtype=torch.floataa ) # load LoRA weight from .safetensors UpperCAmelCase_ : Any = load_file(SCREAMING_SNAKE_CASE__ ) UpperCAmelCase_ : Dict = [] # directly update weight in diffusers model for key in state_dict: # it is suggested to print out the key, it usually will be something like below # "lora_te_text_model_encoder_layers_0_self_attn_k_proj.lora_down.weight" # as we have set the alpha beforehand, so just skip if ".alpha" in key or key in visited: continue if "text" in key: UpperCAmelCase_ : Optional[Any] = key.split('''.''' )[0].split(LORA_PREFIX_TEXT_ENCODER + '''_''' )[-1].split('''_''' ) UpperCAmelCase_ : Optional[int] = pipeline.text_encoder else: UpperCAmelCase_ : Optional[int] = key.split('''.''' )[0].split(LORA_PREFIX_UNET + '''_''' )[-1].split('''_''' ) UpperCAmelCase_ : int = pipeline.unet # find the target layer UpperCAmelCase_ : Union[str, Any] = layer_infos.pop(0 ) while len(SCREAMING_SNAKE_CASE__ ) > -1: try: UpperCAmelCase_ : Union[str, Any] = curr_layer.__getattr__(SCREAMING_SNAKE_CASE__ ) if len(SCREAMING_SNAKE_CASE__ ) > 0: UpperCAmelCase_ : Optional[int] = layer_infos.pop(0 ) elif len(SCREAMING_SNAKE_CASE__ ) == 0: break except Exception: if len(SCREAMING_SNAKE_CASE__ ) > 0: temp_name += "_" + layer_infos.pop(0 ) else: UpperCAmelCase_ : str = layer_infos.pop(0 ) UpperCAmelCase_ : Optional[int] = [] if "lora_down" in key: pair_keys.append(key.replace('''lora_down''', '''lora_up''' ) ) pair_keys.append(SCREAMING_SNAKE_CASE__ ) else: pair_keys.append(SCREAMING_SNAKE_CASE__ ) pair_keys.append(key.replace('''lora_up''', '''lora_down''' ) ) # update weight if len(state_dict[pair_keys[0]].shape ) == 4: UpperCAmelCase_ : Optional[Any] = state_dict[pair_keys[0]].squeeze(3 ).squeeze(2 ).to(torch.floataa ) UpperCAmelCase_ : Any = state_dict[pair_keys[1]].squeeze(3 ).squeeze(2 ).to(torch.floataa ) curr_layer.weight.data += alpha * torch.mm(SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__ ).unsqueeze(2 ).unsqueeze(3 ) else: UpperCAmelCase_ : Optional[int] = state_dict[pair_keys[0]].to(torch.floataa ) UpperCAmelCase_ : Optional[Any] = state_dict[pair_keys[1]].to(torch.floataa ) curr_layer.weight.data += alpha * torch.mm(SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__ ) # update visited list for item in pair_keys: visited.append(SCREAMING_SNAKE_CASE__ ) return pipeline if __name__ == "__main__": snake_case_ : List[Any] = argparse.ArgumentParser() parser.add_argument( "--base_model_path", default=None, type=str, required=True, help="Path to the base model in diffusers format." ) parser.add_argument( "--checkpoint_path", default=None, type=str, required=True, help="Path to the checkpoint to convert." ) parser.add_argument("--dump_path", default=None, type=str, required=True, help="Path to the output model.") parser.add_argument( "--lora_prefix_unet", default="lora_unet", type=str, help="The prefix of UNet weight in safetensors" ) parser.add_argument( "--lora_prefix_text_encoder", default="lora_te", type=str, help="The prefix of text encoder weight in safetensors", ) parser.add_argument("--alpha", default=0.75, type=float, help="The merging ratio in W = W0 + alpha * deltaW") parser.add_argument( "--to_safetensors", action="store_true", help="Whether to store pipeline in safetensors format or not." ) parser.add_argument("--device", type=str, help="Device to use (e.g. cpu, cuda:0, cuda:1, etc.)") snake_case_ : Tuple = parser.parse_args() snake_case_ : int = args.base_model_path snake_case_ : Tuple = args.checkpoint_path snake_case_ : Optional[int] = args.dump_path snake_case_ : Dict = args.lora_prefix_unet snake_case_ : Dict = args.lora_prefix_text_encoder snake_case_ : Optional[Any] = args.alpha snake_case_ : Optional[Any] = convert(base_model_path, checkpoint_path, lora_prefix_unet, lora_prefix_text_encoder, alpha) snake_case_ : Any = pipe.to(args.device) pipe.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)
125
import math import sys def UpperCAmelCase__ ( lowerCamelCase ): if number != int(lowerCamelCase ): raise ValueError("the value of input must be a natural number" ) if number < 0: raise ValueError("the value of input must not be a negative number" ) if number == 0: return 1 lowercase :Dict = [-1] * (number + 1) lowercase :Optional[Any] = 0 for i in range(1, number + 1 ): lowercase :Optional[int] = sys.maxsize lowercase :Any = int(math.sqrt(lowerCamelCase ) ) for j in range(1, root + 1 ): lowercase :Any = 1 + answers[i - (j**2)] lowercase :Dict = min(lowerCamelCase, lowerCamelCase ) lowercase :Union[str, Any] = answer return answers[number] if __name__ == "__main__": import doctest doctest.testmod()
236
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available __SCREAMING_SNAKE_CASE :Optional[Any] = { '''configuration_tapas''': ['''TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TapasConfig'''], '''tokenization_tapas''': ['''TapasTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE :Dict = [ '''TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TapasForMaskedLM''', '''TapasForQuestionAnswering''', '''TapasForSequenceClassification''', '''TapasModel''', '''TapasPreTrainedModel''', '''load_tf_weights_in_tapas''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE :str = [ '''TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFTapasForMaskedLM''', '''TFTapasForQuestionAnswering''', '''TFTapasForSequenceClassification''', '''TFTapasModel''', '''TFTapasPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_tapas import TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP, TapasConfig from .tokenization_tapas import TapasTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tapas import ( TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST, TapasForMaskedLM, TapasForQuestionAnswering, TapasForSequenceClassification, TapasModel, TapasPreTrainedModel, load_tf_weights_in_tapas, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_tapas import ( TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST, TFTapasForMaskedLM, TFTapasForQuestionAnswering, TFTapasForSequenceClassification, TFTapasModel, TFTapasPreTrainedModel, ) else: import sys __SCREAMING_SNAKE_CASE :Union[str, Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
156
'''simple docstring''' import numpy as np import torch from torch.nn import CrossEntropyLoss from transformers import AutoModelForCausalLM, AutoTokenizer import datasets from datasets import logging __SCREAMING_SNAKE_CASE :Tuple = '''\ ''' __SCREAMING_SNAKE_CASE :Union[str, Any] = ''' Perplexity (PPL) is one of the most common metrics for evaluating language models. It is defined as the exponentiated average negative log-likelihood of a sequence. For more information, see https://huggingface.co/docs/transformers/perplexity ''' __SCREAMING_SNAKE_CASE :List[Any] = ''' Args: model_id (str): model used for calculating Perplexity NOTE: Perplexity can only be calculated for causal language models. This includes models such as gpt2, causal variations of bert, causal versions of t5, and more (the full list can be found in the AutoModelForCausalLM documentation here: https://huggingface.co/docs/transformers/master/en/model_doc/auto#transformers.AutoModelForCausalLM ) input_texts (list of str): input text, each separate text snippet is one list entry. batch_size (int): the batch size to run texts through the model. Defaults to 16. add_start_token (bool): whether to add the start token to the texts, so the perplexity can include the probability of the first word. Defaults to True. device (str): device to run on, defaults to \'cuda\' when available Returns: perplexity: dictionary containing the perplexity scores for the texts in the input list, as well as the mean perplexity. If one of the input texts is longer than the max input length of the model, then it is truncated to the max length for the perplexity computation. Examples: Example 1: >>> perplexity = datasets.load_metric("perplexity") >>> input_texts = ["lorem ipsum", "Happy Birthday!", "Bienvenue"] >>> results = perplexity.compute(model_id=\'gpt2\', ... add_start_token=False, ... input_texts=input_texts) # doctest:+ELLIPSIS >>> print(list(results.keys())) [\'perplexities\', \'mean_perplexity\'] >>> print(round(results["mean_perplexity"], 2)) 78.22 >>> print(round(results["perplexities"][0], 2)) 11.11 Example 2: >>> perplexity = datasets.load_metric("perplexity") >>> input_texts = datasets.load_dataset("wikitext", ... "wikitext-2-raw-v1", ... split="test")["text"][:50] # doctest:+ELLIPSIS [...] >>> input_texts = [s for s in input_texts if s!=\'\'] >>> results = perplexity.compute(model_id=\'gpt2\', ... input_texts=input_texts) # doctest:+ELLIPSIS >>> print(list(results.keys())) [\'perplexities\', \'mean_perplexity\'] >>> print(round(results["mean_perplexity"], 2)) 60.35 >>> print(round(results["perplexities"][0], 2)) 81.12 ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A_ ( datasets.Metric ): def lowercase ( self : Optional[int] ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "input_texts": datasets.Value("string" ), } ) , reference_urls=["https://huggingface.co/docs/transformers/perplexity"] , ) def lowercase ( self : List[Any] , snake_case_ : Optional[Any] , snake_case_ : List[Any] , snake_case_ : int = 1_6 , snake_case_ : bool = True , snake_case_ : int=None ): if device is not None: assert device in ["gpu", "cpu", "cuda"], "device should be either gpu or cpu." if device == "gpu": _UpperCAmelCase = "cuda" else: _UpperCAmelCase = "cuda" if torch.cuda.is_available() else "cpu" _UpperCAmelCase = AutoModelForCausalLM.from_pretrained(snake_case_ ) _UpperCAmelCase = model.to(snake_case_ ) _UpperCAmelCase = AutoTokenizer.from_pretrained(snake_case_ ) # if batch_size > 1 (which generally leads to padding being required), and # if there is not an already assigned pad_token, assign an existing # special token to also be the padding token if tokenizer.pad_token is None and batch_size > 1: _UpperCAmelCase = list(tokenizer.special_tokens_map_extended.values() ) # check that the model already has at least one special token defined assert ( len(snake_case_ ) > 0 ), "If batch_size > 1, model must have at least one special token to use for padding. Please use a different model or set batch_size=1." # assign one of the special tokens to also be the pad token tokenizer.add_special_tokens({"pad_token": existing_special_tokens[0]} ) if add_start_token: # leave room for <BOS> token to be added: assert ( tokenizer.bos_token is not None ), "Input model must already have a BOS token if using add_start_token=True. Please use a different model, or set add_start_token=False" _UpperCAmelCase = model.config.max_length - 1 else: _UpperCAmelCase = model.config.max_length _UpperCAmelCase = tokenizer( snake_case_ , add_special_tokens=snake_case_ , padding=snake_case_ , truncation=snake_case_ , max_length=snake_case_ , return_tensors="pt" , return_attention_mask=snake_case_ , ).to(snake_case_ ) _UpperCAmelCase = encodings["input_ids"] _UpperCAmelCase = encodings["attention_mask"] # check that each input is long enough: if add_start_token: assert torch.all(torch.ge(attn_masks.sum(1 ) , 1 ) ), "Each input text must be at least one token long." else: assert torch.all( torch.ge(attn_masks.sum(1 ) , 2 ) ), "When add_start_token=False, each input text must be at least two tokens long. Run with add_start_token=True if inputting strings of only one token, and remove all empty input strings." _UpperCAmelCase = [] _UpperCAmelCase = CrossEntropyLoss(reduction="none" ) for start_index in logging.tqdm(range(0 , len(snake_case_ ) , snake_case_ ) ): _UpperCAmelCase = min(start_index + batch_size , len(snake_case_ ) ) _UpperCAmelCase = encoded_texts[start_index:end_index] _UpperCAmelCase = attn_masks[start_index:end_index] if add_start_token: _UpperCAmelCase = torch.tensor([[tokenizer.bos_token_id]] * encoded_batch.size(dim=0 ) ).to(snake_case_ ) _UpperCAmelCase = torch.cat([bos_tokens_tensor, encoded_batch] , dim=1 ) _UpperCAmelCase = torch.cat( [torch.ones(bos_tokens_tensor.size() , dtype=torch.intaa ).to(snake_case_ ), attn_mask] , dim=1 ) _UpperCAmelCase = encoded_batch with torch.no_grad(): _UpperCAmelCase = model(snake_case_ , attention_mask=snake_case_ ).logits _UpperCAmelCase = out_logits[..., :-1, :].contiguous() _UpperCAmelCase = labels[..., 1:].contiguous() _UpperCAmelCase = attn_mask[..., 1:].contiguous() _UpperCAmelCase = torch.expa( (loss_fct(shift_logits.transpose(1 , 2 ) , snake_case_ ) * shift_attention_mask_batch).sum(1 ) / shift_attention_mask_batch.sum(1 ) ) ppls += perplexity_batch.tolist() return {"perplexities": ppls, "mean_perplexity": np.mean(snake_case_ )}
156
1
"""simple docstring""" import gzip import hashlib import json import multiprocessing import os import re import shutil import time from pathlib import Path import numpy as np from arguments import PreprocessingArguments from datasets import load_dataset from minhash_deduplication import deduplicate_dataset from transformers import AutoTokenizer, HfArgumentParser A_ = re.compile(r'''\s+''') def UpperCAmelCase__ (snake_case__ : Optional[int] ): """simple docstring""" return {"hash": hashlib.mda(re.sub(snake_case__ , """""" , example["""content"""] ).encode("""utf-8""" ) ).hexdigest()} def UpperCAmelCase__ (snake_case__ : Dict ): """simple docstring""" _snake_case : Any = [len(snake_case__ ) for line in example["""content"""].splitlines()] return {"line_mean": np.mean(snake_case__ ), "line_max": max(snake_case__ )} def UpperCAmelCase__ (snake_case__ : List[Any] ): """simple docstring""" _snake_case : Tuple = np.mean([c.isalnum() for c in example["""content"""]] ) return {"alpha_frac": alpha_frac} def UpperCAmelCase__ (snake_case__ : List[Any] , snake_case__ : List[Any] ): """simple docstring""" if example["hash"] in uniques: uniques.remove(example["""hash"""] ) return True else: return False def UpperCAmelCase__ (snake_case__ : Optional[Any] , snake_case__ : List[str]=5 ): """simple docstring""" _snake_case : Any = ["""auto-generated""", """autogenerated""", """automatically generated"""] _snake_case : Tuple = example["""content"""].splitlines() for _, line in zip(range(snake_case__ ) , snake_case__ ): for keyword in keywords: if keyword in line.lower(): return {"autogenerated": True} else: return {"autogenerated": False} def UpperCAmelCase__ (snake_case__ : Any , snake_case__ : Union[str, Any]=5 , snake_case__ : Any=0.05 ): """simple docstring""" _snake_case : Optional[Any] = ["""unit tests""", """test file""", """configuration file"""] _snake_case : List[Any] = example["""content"""].splitlines() _snake_case : Dict = 0 _snake_case : str = 0 # first test for _, line in zip(range(snake_case__ ) , snake_case__ ): for keyword in keywords: if keyword in line.lower(): return {"config_or_test": True} # second test _snake_case : Optional[int] = example["""content"""].count("""\n""" ) _snake_case : Tuple = int(coeff * nlines ) for line in lines: count_config += line.lower().count("""config""" ) count_test += line.lower().count("""test""" ) if count_config > threshold or count_test > threshold: return {"config_or_test": True} return {"config_or_test": False} def UpperCAmelCase__ (snake_case__ : str ): """simple docstring""" _snake_case : Optional[int] = ["""def """, """class """, """for """, """while """] _snake_case : str = example["""content"""].splitlines() for line in lines: for keyword in keywords: if keyword in line.lower(): return {"has_no_keywords": False} return {"has_no_keywords": True} def UpperCAmelCase__ (snake_case__ : List[str] , snake_case__ : List[str]=4 ): """simple docstring""" _snake_case : List[Any] = example["""content"""].splitlines() _snake_case : str = 0 for line in lines: counter += line.lower().count("""=""" ) if counter > minimum: return {"has_few_assignments": False} return {"has_few_assignments": True} def UpperCAmelCase__ (snake_case__ : List[str] ): """simple docstring""" _snake_case : Optional[Any] = tokenizer(example["""content"""] , truncation=snake_case__ )["""input_ids"""] _snake_case : Optional[Any] = len(example["""content"""] ) / len(snake_case__ ) return {"ratio": ratio} def UpperCAmelCase__ (snake_case__ : Optional[int] ): """simple docstring""" _snake_case : Optional[int] = {} results.update(get_hash(snake_case__ ) ) results.update(line_stats(snake_case__ ) ) results.update(alpha_stats(snake_case__ ) ) results.update(char_token_ratio(snake_case__ ) ) results.update(is_autogenerated(snake_case__ ) ) results.update(is_config_or_test(snake_case__ ) ) results.update(has_no_keywords(snake_case__ ) ) results.update(has_few_assignments(snake_case__ ) ) return results def UpperCAmelCase__ (snake_case__ : Tuple , snake_case__ : List[Any] , snake_case__ : List[str] ): """simple docstring""" if not check_uniques(snake_case__ , snake_case__ ): return False elif example["autogenerated"]: return False elif example["line_max"] > args.line_max: return False elif example["line_mean"] > args.line_mean: return False elif example["alpha_frac"] < args.alpha_frac: return False elif example["ratio"] < args.min_token_ratio: return False elif example["config_or_test"] and np.random.rand() <= args.filter_proba: return False elif example["has_no_keywords"] and np.random.rand() <= args.filter_proba: return False elif example["has_few_assignments"]: return False else: return True def UpperCAmelCase__ (snake_case__ : Optional[Any] ): """simple docstring""" with open(snake_case__ , """rb""" ) as f_in: with gzip.open(str(snake_case__ ) + """.gz""" , """wb""" , compresslevel=6 ) as f_out: shutil.copyfileobj(snake_case__ , snake_case__ ) os.unlink(snake_case__ ) # Settings A_ = HfArgumentParser(PreprocessingArguments) A_ = parser.parse_args() if args.num_workers is None: A_ = multiprocessing.cpu_count() A_ = AutoTokenizer.from_pretrained(args.tokenizer_dir) # Load dataset A_ = time.time() A_ = load_dataset(args.dataset_name, split='''train''') print(F'''Time to load dataset: {time.time()-t_start:.2f}''') # Run preprocessing A_ = time.time() A_ = ds.map(preprocess, num_proc=args.num_workers) print(F'''Time to preprocess dataset: {time.time()-t_start:.2f}''') # Deduplicate hashes A_ = set(ds.unique('''hash''')) A_ = len(uniques) / len(ds) print(F'''Fraction of duplicates: {1-frac:.2%}''') # Deduplicate data and apply heuristics A_ = time.time() A_ = ds.filter(filter, fn_kwargs={'''uniques''': uniques, '''args''': args}) print(F'''Time to filter dataset: {time.time()-t_start:.2f}''') print(F'''Size of filtered dataset: {len(ds_filter)}''') # Deduplicate with minhash and jaccard similarity if args.near_deduplication: A_ = time.time() A_ , A_ = deduplicate_dataset(ds_filter, args.jaccard_threshold) print(F'''Time to deduplicate dataset: {time.time()-t_start:.2f}''') print(F'''Size of deduplicate dataset: {len(ds_filter)}''') # Save data in batches of samples_per_file A_ = Path(args.output_dir) output_dir.mkdir(exist_ok=True) # save duplicate_clusters in the output_dir as artifacts # not sure it is the right place the save it if args.near_deduplication: with open(output_dir / '''duplicate_clusters.json''', '''w''') as f: json.dump(duplicate_clusters, f) A_ = output_dir / '''data''' data_dir.mkdir(exist_ok=True) A_ = time.time() for file_number, index in enumerate(range(0, len(ds_filter), args.samples_per_file)): A_ = str(data_dir / F'''file-{file_number+1:012}.json''') A_ = min(len(ds_filter), index + args.samples_per_file) ds_filter.select(list(range(index, end_index))).to_json(file_path) compress_file(file_path) print(F'''Time to save dataset: {time.time()-t_start:.2f}''')
64
"""simple docstring""" import unittest import numpy as np from diffusers import OnnxStableDiffusionInpaintPipelineLegacy from diffusers.utils.testing_utils import ( is_onnx_available, load_image, load_numpy, nightly, require_onnxruntime, require_torch_gpu, ) if is_onnx_available(): import onnxruntime as ort @nightly @require_onnxruntime @require_torch_gpu class lowercase( unittest.TestCase ): '''simple docstring''' @property def UpperCamelCase_ ( self: Optional[Any] ): '''simple docstring''' return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def UpperCamelCase_ ( self: int ): '''simple docstring''' _snake_case : Any = ort.SessionOptions() _snake_case : Union[str, Any] = False return options def UpperCamelCase_ ( self: List[Any] ): '''simple docstring''' _snake_case : Any = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/in_paint/overture-creations-5sI6fQgYIuo.png""" ) _snake_case : Union[str, Any] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/in_paint/overture-creations-5sI6fQgYIuo_mask.png""" ) _snake_case : Union[str, Any] = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/in_paint/red_cat_sitting_on_a_park_bench_onnx.npy""" ) # using the PNDM scheduler by default _snake_case : Optional[Any] = OnnxStableDiffusionInpaintPipelineLegacy.from_pretrained( """CompVis/stable-diffusion-v1-4""", revision="""onnx""", safety_checker=a_, feature_extractor=a_, provider=self.gpu_provider, sess_options=self.gpu_options, ) pipe.set_progress_bar_config(disable=a_ ) _snake_case : Optional[Any] = """A red cat sitting on a park bench""" _snake_case : Optional[int] = np.random.RandomState(0 ) _snake_case : Any = pipe( prompt=a_, image=a_, mask_image=a_, strength=0.75, guidance_scale=7.5, num_inference_steps=15, generator=a_, output_type="""np""", ) _snake_case : Dict = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 1E-2
64
1
import warnings from ...utils import logging from .image_processing_glpn import GLPNImageProcessor UpperCAmelCase__ = logging.get_logger(__name__) class __lowerCAmelCase ( A ): def __init__( self : List[str] , *A : List[Any] , **A : Union[str, Any]) -> None: """simple docstring""" warnings.warn( 'The class GLPNFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please' ' use GLPNImageProcessor instead.' , A , ) super().__init__(*A , **A)
367
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, is_vision_available, ) UpperCAmelCase__ = { "configuration_clip": [ "CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP", "CLIPConfig", "CLIPOnnxConfig", "CLIPTextConfig", "CLIPVisionConfig", ], "processing_clip": ["CLIPProcessor"], "tokenization_clip": ["CLIPTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = ["CLIPTokenizerFast"] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = ["CLIPFeatureExtractor"] UpperCAmelCase__ = ["CLIPImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "CLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "CLIPModel", "CLIPPreTrainedModel", "CLIPTextModel", "CLIPTextModelWithProjection", "CLIPVisionModel", "CLIPVisionModelWithProjection", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "TFCLIPModel", "TFCLIPPreTrainedModel", "TFCLIPTextModel", "TFCLIPVisionModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "FlaxCLIPModel", "FlaxCLIPPreTrainedModel", "FlaxCLIPTextModel", "FlaxCLIPTextPreTrainedModel", "FlaxCLIPVisionModel", "FlaxCLIPVisionPreTrainedModel", ] if TYPE_CHECKING: from .configuration_clip import ( CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, CLIPConfig, CLIPOnnxConfig, CLIPTextConfig, CLIPVisionConfig, ) from .processing_clip import CLIPProcessor from .tokenization_clip import CLIPTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_clip_fast import CLIPTokenizerFast try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_clip import CLIPFeatureExtractor from .image_processing_clip import CLIPImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_clip import ( CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, CLIPModel, CLIPPreTrainedModel, CLIPTextModel, CLIPTextModelWithProjection, CLIPVisionModel, CLIPVisionModelWithProjection, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_clip import ( TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, TFCLIPModel, TFCLIPPreTrainedModel, TFCLIPTextModel, TFCLIPVisionModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_clip import ( FlaxCLIPModel, FlaxCLIPPreTrainedModel, FlaxCLIPTextModel, FlaxCLIPTextPreTrainedModel, FlaxCLIPVisionModel, FlaxCLIPVisionPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
290
0
"""simple docstring""" import tempfile import numpy as np import torch from transformers import AutoTokenizer, TaEncoderModel from diffusers import DDPMScheduler, UNetaDConditionModel from diffusers.models.attention_processor import AttnAddedKVProcessor from diffusers.pipelines.deepfloyd_if import IFWatermarker from diffusers.utils.testing_utils import torch_device from ..test_pipelines_common import to_np class _A : def A__ ( self ): """simple docstring""" torch.manual_seed(0 ) lowercase = TaEncoderModel.from_pretrained("""hf-internal-testing/tiny-random-t5""" ) torch.manual_seed(0 ) lowercase = AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-random-t5""" ) torch.manual_seed(0 ) lowercase = UNetaDConditionModel( sample_size=32 , layers_per_block=1 , block_out_channels=[32, 64] , down_block_types=[ """ResnetDownsampleBlock2D""", """SimpleCrossAttnDownBlock2D""", ] , mid_block_type="""UNetMidBlock2DSimpleCrossAttn""" , up_block_types=["""SimpleCrossAttnUpBlock2D""", """ResnetUpsampleBlock2D"""] , in_channels=3 , out_channels=6 , cross_attention_dim=32 , encoder_hid_dim=32 , attention_head_dim=8 , addition_embed_type="""text""" , addition_embed_type_num_heads=2 , cross_attention_norm="""group_norm""" , resnet_time_scale_shift="""scale_shift""" , act_fn="""gelu""" , ) unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests torch.manual_seed(0 ) lowercase = DDPMScheduler( num_train_timesteps=1000 , beta_schedule="""squaredcos_cap_v2""" , beta_start=0.0_0_0_1 , beta_end=0.0_2 , thresholding=__lowerCAmelCase , dynamic_thresholding_ratio=0.9_5 , sample_max_value=1.0 , prediction_type="""epsilon""" , variance_type="""learned_range""" , ) torch.manual_seed(0 ) lowercase = IFWatermarker() return { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "watermarker": watermarker, "safety_checker": None, "feature_extractor": None, } def A__ ( self ): """simple docstring""" torch.manual_seed(0 ) lowercase = TaEncoderModel.from_pretrained("""hf-internal-testing/tiny-random-t5""" ) torch.manual_seed(0 ) lowercase = AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-random-t5""" ) torch.manual_seed(0 ) lowercase = UNetaDConditionModel( sample_size=32 , layers_per_block=[1, 2] , block_out_channels=[32, 64] , down_block_types=[ """ResnetDownsampleBlock2D""", """SimpleCrossAttnDownBlock2D""", ] , mid_block_type="""UNetMidBlock2DSimpleCrossAttn""" , up_block_types=["""SimpleCrossAttnUpBlock2D""", """ResnetUpsampleBlock2D"""] , in_channels=6 , out_channels=6 , cross_attention_dim=32 , encoder_hid_dim=32 , attention_head_dim=8 , addition_embed_type="""text""" , addition_embed_type_num_heads=2 , cross_attention_norm="""group_norm""" , resnet_time_scale_shift="""scale_shift""" , act_fn="""gelu""" , class_embed_type="""timestep""" , mid_block_scale_factor=1.4_1_4 , time_embedding_act_fn="""gelu""" , time_embedding_dim=32 , ) unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests torch.manual_seed(0 ) lowercase = DDPMScheduler( num_train_timesteps=1000 , beta_schedule="""squaredcos_cap_v2""" , beta_start=0.0_0_0_1 , beta_end=0.0_2 , thresholding=__lowerCAmelCase , dynamic_thresholding_ratio=0.9_5 , sample_max_value=1.0 , prediction_type="""epsilon""" , variance_type="""learned_range""" , ) torch.manual_seed(0 ) lowercase = DDPMScheduler( num_train_timesteps=1000 , beta_schedule="""squaredcos_cap_v2""" , beta_start=0.0_0_0_1 , beta_end=0.0_2 , ) torch.manual_seed(0 ) lowercase = IFWatermarker() return { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "image_noising_scheduler": image_noising_scheduler, "watermarker": watermarker, "safety_checker": None, "feature_extractor": None, } def A__ ( self ): """simple docstring""" lowercase = self.get_dummy_components() lowercase = self.pipeline_class(**__lowerCAmelCase ) pipe.to(__lowerCAmelCase ) pipe.set_progress_bar_config(disable=__lowerCAmelCase ) lowercase = self.get_dummy_inputs(__lowerCAmelCase ) lowercase = inputs["""prompt"""] lowercase = inputs["""generator"""] lowercase = inputs["""num_inference_steps"""] lowercase = inputs["""output_type"""] if "image" in inputs: lowercase = inputs["""image"""] else: lowercase = None if "mask_image" in inputs: lowercase = inputs["""mask_image"""] else: lowercase = None if "original_image" in inputs: lowercase = inputs["""original_image"""] else: lowercase = None lowercase , lowercase = pipe.encode_prompt(__lowerCAmelCase ) # inputs with prompt converted to embeddings lowercase = { """prompt_embeds""": prompt_embeds, """negative_prompt_embeds""": negative_prompt_embeds, """generator""": generator, """num_inference_steps""": num_inference_steps, """output_type""": output_type, } if image is not None: lowercase = image if mask_image is not None: lowercase = mask_image if original_image is not None: lowercase = original_image # set all optional components to None for optional_component in pipe._optional_components: setattr(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) lowercase = pipe(**__lowerCAmelCase )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(__lowerCAmelCase ) lowercase = self.pipeline_class.from_pretrained(__lowerCAmelCase ) pipe_loaded.to(__lowerCAmelCase ) pipe_loaded.set_progress_bar_config(disable=__lowerCAmelCase ) pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests for optional_component in pipe._optional_components: self.assertTrue( getattr(__lowerCAmelCase , __lowerCAmelCase ) is None , f'`{optional_component}` did not stay set to None after loading.' , ) lowercase = self.get_dummy_inputs(__lowerCAmelCase ) lowercase = inputs["""generator"""] lowercase = inputs["""num_inference_steps"""] lowercase = inputs["""output_type"""] # inputs with prompt converted to embeddings lowercase = { """prompt_embeds""": prompt_embeds, """negative_prompt_embeds""": negative_prompt_embeds, """generator""": generator, """num_inference_steps""": num_inference_steps, """output_type""": output_type, } if image is not None: lowercase = image if mask_image is not None: lowercase = mask_image if original_image is not None: lowercase = original_image lowercase = pipe_loaded(**__lowerCAmelCase )[0] lowercase = np.abs(to_np(__lowerCAmelCase ) - to_np(__lowerCAmelCase ) ).max() self.assertLess(__lowerCAmelCase , 1E-4 ) def A__ ( self ): """simple docstring""" lowercase = self.get_dummy_components() lowercase = self.pipeline_class(**__lowerCAmelCase ) pipe.to(__lowerCAmelCase ) pipe.set_progress_bar_config(disable=__lowerCAmelCase ) lowercase = self.get_dummy_inputs(__lowerCAmelCase ) lowercase = pipe(**__lowerCAmelCase )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(__lowerCAmelCase ) lowercase = self.pipeline_class.from_pretrained(__lowerCAmelCase ) pipe_loaded.to(__lowerCAmelCase ) pipe_loaded.set_progress_bar_config(disable=__lowerCAmelCase ) pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests lowercase = self.get_dummy_inputs(__lowerCAmelCase ) lowercase = pipe_loaded(**__lowerCAmelCase )[0] lowercase = np.abs(to_np(__lowerCAmelCase ) - to_np(__lowerCAmelCase ) ).max() self.assertLess(__lowerCAmelCase , 1E-4 )
197
"""simple docstring""" from __future__ import annotations from typing import Any class _A : def __init__( self , __lowerCAmelCase = 6 ): """simple docstring""" lowercase = None lowercase = None self.create_linked_list(__lowerCAmelCase ) def A__ ( self , __lowerCAmelCase ): """simple docstring""" lowercase = Node() lowercase = current_node lowercase = current_node lowercase = current_node for _ in range(1 , __lowerCAmelCase ): lowercase = Node() lowercase = current_node lowercase = previous_node lowercase = current_node lowercase = self.front lowercase = previous_node def A__ ( self ): """simple docstring""" return ( self.front == self.rear and self.front is not None and self.front.data is None ) def A__ ( self ): """simple docstring""" self.check_can_perform_operation() return self.front.data if self.front else None def A__ ( self , __lowerCAmelCase ): """simple docstring""" if self.rear is None: return self.check_is_full() if not self.is_empty(): lowercase = self.rear.next if self.rear: lowercase = data def A__ ( self ): """simple docstring""" self.check_can_perform_operation() if self.rear is None or self.front is None: return None if self.front == self.rear: lowercase = self.front.data lowercase = None return data lowercase = self.front lowercase = old_front.next lowercase = old_front.data lowercase = None return data def A__ ( self ): """simple docstring""" if self.is_empty(): raise Exception("""Empty Queue""" ) def A__ ( self ): """simple docstring""" if self.rear and self.rear.next == self.front: raise Exception("""Full Queue""" ) class _A : def __init__( self ): """simple docstring""" lowercase = None lowercase = None lowercase = None if __name__ == "__main__": import doctest doctest.testmod()
197
1
from typing import Callable, Dict, Optional, Tuple import torch from torch import nn from torch.distributions import ( AffineTransform, Distribution, Independent, NegativeBinomial, Normal, StudentT, TransformedDistribution, ) class __lowerCamelCase ( snake_case_ ): """simple docstring""" def __init__( self , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=0 ) -> Optional[int]: '''simple docstring''' lowercase_ = 1.0 if scale is None else scale lowercase_ = 0.0 if loc is None else loc super().__init__(UpperCAmelCase , [AffineTransform(loc=self.loc , scale=self.scale , event_dim=UpperCAmelCase )] ) @property def A__ ( self ) -> int: '''simple docstring''' return self.base_dist.mean * self.scale + self.loc @property def A__ ( self ) -> str: '''simple docstring''' return self.base_dist.variance * self.scale**2 @property def A__ ( self ) -> List[str]: '''simple docstring''' return self.variance.sqrt() class __lowerCamelCase ( nn.Module ): """simple docstring""" def __init__( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) -> None: '''simple docstring''' super().__init__(**UpperCAmelCase ) lowercase_ = args_dim lowercase_ = nn.ModuleList([nn.Linear(UpperCAmelCase , UpperCAmelCase ) for dim in args_dim.values()] ) lowercase_ = domain_map def A__ ( self , UpperCAmelCase ) -> Tuple[torch.Tensor]: '''simple docstring''' lowercase_ = [proj(UpperCAmelCase ) for proj in self.proj] return self.domain_map(*UpperCAmelCase ) class __lowerCamelCase ( nn.Module ): """simple docstring""" def __init__( self , UpperCAmelCase ) -> Dict: '''simple docstring''' super().__init__() lowercase_ = function def A__ ( self , UpperCAmelCase , *UpperCAmelCase ) -> Union[str, Any]: '''simple docstring''' return self.function(UpperCAmelCase , *UpperCAmelCase ) class __lowerCamelCase : """simple docstring""" lowerCAmelCase__ = 42 lowerCAmelCase__ = 42 lowerCAmelCase__ = 42 def __init__( self , UpperCAmelCase = 1 ) -> None: '''simple docstring''' lowercase_ = dim lowercase_ = {k: dim * self.args_dim[k] for k in self.args_dim} def A__ ( self , UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' if self.dim == 1: return self.distribution_class(*UpperCAmelCase ) else: return Independent(self.distribution_class(*UpperCAmelCase ) , 1 ) def A__ ( self , UpperCAmelCase , UpperCAmelCase = None , UpperCAmelCase = None , ) -> Distribution: '''simple docstring''' lowercase_ = self._base_distribution(UpperCAmelCase ) if loc is None and scale is None: return distr else: return AffineTransformed(UpperCAmelCase , loc=UpperCAmelCase , scale=UpperCAmelCase , event_dim=self.event_dim ) @property def A__ ( self ) -> Tuple: '''simple docstring''' return () if self.dim == 1 else (self.dim,) @property def A__ ( self ) -> int: '''simple docstring''' return len(self.event_shape ) @property def A__ ( self ) -> float: '''simple docstring''' return 0.0 def A__ ( self , UpperCAmelCase ) -> nn.Module: '''simple docstring''' return ParameterProjection( in_features=UpperCAmelCase , args_dim=self.args_dim , domain_map=LambdaLayer(self.domain_map ) , ) def A__ ( self , *UpperCAmelCase ) -> Any: '''simple docstring''' raise NotImplementedError() @staticmethod def A__ ( UpperCAmelCase ) -> torch.Tensor: '''simple docstring''' return (x + torch.sqrt(torch.square(UpperCAmelCase ) + 4.0 )) / 2.0 class __lowerCamelCase ( snake_case_ ): """simple docstring""" lowerCAmelCase__ = {"df": 1, "loc": 1, "scale": 1} lowerCAmelCase__ = StudentT @classmethod def A__ ( cls , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> Dict: '''simple docstring''' lowercase_ = cls.squareplus(UpperCAmelCase ).clamp_min(torch.finfo(scale.dtype ).eps ) lowercase_ = 2.0 + cls.squareplus(UpperCAmelCase ) return df.squeeze(-1 ), loc.squeeze(-1 ), scale.squeeze(-1 ) class __lowerCamelCase ( snake_case_ ): """simple docstring""" lowerCAmelCase__ = {"loc": 1, "scale": 1} lowerCAmelCase__ = Normal @classmethod def A__ ( cls , UpperCAmelCase , UpperCAmelCase ) -> int: '''simple docstring''' lowercase_ = cls.squareplus(UpperCAmelCase ).clamp_min(torch.finfo(scale.dtype ).eps ) return loc.squeeze(-1 ), scale.squeeze(-1 ) class __lowerCamelCase ( snake_case_ ): """simple docstring""" lowerCAmelCase__ = {"total_count": 1, "logits": 1} lowerCAmelCase__ = NegativeBinomial @classmethod def A__ ( cls , UpperCAmelCase , UpperCAmelCase ) -> Optional[int]: '''simple docstring''' lowercase_ = cls.squareplus(UpperCAmelCase ) return total_count.squeeze(-1 ), logits.squeeze(-1 ) def A__ ( self , UpperCAmelCase ) -> Distribution: '''simple docstring''' lowercase_ , lowercase_ = distr_args if self.dim == 1: return self.distribution_class(total_count=UpperCAmelCase , logits=UpperCAmelCase ) else: return Independent(self.distribution_class(total_count=UpperCAmelCase , logits=UpperCAmelCase ) , 1 ) def A__ ( self , UpperCAmelCase , UpperCAmelCase = None , UpperCAmelCase = None ) -> Distribution: '''simple docstring''' lowercase_ , lowercase_ = distr_args if scale is not None: # See scaling property of Gamma. logits += scale.log() return self._base_distribution((total_count, logits) )
371
import argparse import collections import numpy as np import torch from flax import traverse_util from tax import checkpoints from transformers import MTaConfig, UMTaEncoderModel, UMTaForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() def SCREAMING_SNAKE_CASE_ ( __lowerCamelCase: Any , __lowerCamelCase: List[str] , __lowerCamelCase: List[Any] ): '''simple docstring''' return params[F'{prefix}/{prefix}/relpos_bias/rel_embedding'][:, i, :] def SCREAMING_SNAKE_CASE_ ( __lowerCamelCase: List[Any] , __lowerCamelCase: Union[str, Any] , __lowerCamelCase: int , __lowerCamelCase: Any="attention" ): '''simple docstring''' lowercase_ = lowercase_ = np.ascontiguousarray(params[F'{prefix}/{prefix}/{layer_name}/key/kernel'][:, i, :, :] ) lowercase_ = k_tmp.reshape(k_tmp.shape[0] , k_tmp.shape[1] * k_tmp.shape[2] ) lowercase_ = np.ascontiguousarray(params[F'{prefix}/{prefix}/{layer_name}/out/kernel'][:, i, :, :] ) lowercase_ = o_tmp.reshape(o_tmp.shape[0] * o_tmp.shape[1] , o_tmp.shape[2] ) lowercase_ = np.ascontiguousarray(params[F'{prefix}/{prefix}/{layer_name}/query/kernel'][:, i, :, :] ) lowercase_ = q_tmp.reshape(q_tmp.shape[0] , q_tmp.shape[1] * q_tmp.shape[2] ) lowercase_ = np.ascontiguousarray(params[F'{prefix}/{prefix}/{layer_name}/value/kernel'][:, i, :, :] ) lowercase_ = v_tmp.reshape(v_tmp.shape[0] , v_tmp.shape[1] * v_tmp.shape[2] ) return k, o, q, v def SCREAMING_SNAKE_CASE_ ( __lowerCamelCase: Optional[Any] , __lowerCamelCase: str , __lowerCamelCase: Optional[Any] , __lowerCamelCase: Optional[Any]=False ): '''simple docstring''' if split_mlp_wi: lowercase_ = params[F'{prefix}/{prefix}/mlp/wi_0/kernel'][:, i, :] lowercase_ = params[F'{prefix}/{prefix}/mlp/wi_1/kernel'][:, i, :] lowercase_ = (wi_a, wi_a) else: lowercase_ = params[F'{prefix}/{prefix}/mlp/wi/kernel'][:, i, :] lowercase_ = params[F'{prefix}/{prefix}/mlp/wo/kernel'][:, i, :] return wi, wo def SCREAMING_SNAKE_CASE_ ( __lowerCamelCase: Optional[int] , __lowerCamelCase: Dict , __lowerCamelCase: int , __lowerCamelCase: Optional[Any] ): '''simple docstring''' return params[F'{prefix}/{prefix}/{layer_name}/scale'][:, i] def SCREAMING_SNAKE_CASE_ ( __lowerCamelCase: dict , *, __lowerCamelCase: int , __lowerCamelCase: bool , __lowerCamelCase: bool = False ): '''simple docstring''' lowercase_ = traverse_util.flatten_dict(variables["target"] ) lowercase_ = {"/".join(__lowerCamelCase ): v for k, v in old.items()} # v1.1 models have a gated GeLU with wi_0 and wi_1 instead of wi lowercase_ = "encoder/encoder/mlp/wi_0/kernel" in old print("Split MLP:" , __lowerCamelCase ) lowercase_ = collections.OrderedDict() # Shared embeddings. lowercase_ = old["token_embedder/embedding"] # Encoder. for i in range(__lowerCamelCase ): # Block i, layer 0 (Self Attention). lowercase_ = tax_layer_norm_lookup(__lowerCamelCase , __lowerCamelCase , "encoder" , "pre_attention_layer_norm" ) lowercase_ , lowercase_ , lowercase_ , lowercase_ = tax_attention_lookup(__lowerCamelCase , __lowerCamelCase , "encoder" , "attention" ) lowercase_ = layer_norm lowercase_ = k.T lowercase_ = o.T lowercase_ = q.T lowercase_ = v.T # Block i, layer 1 (MLP). lowercase_ = tax_layer_norm_lookup(__lowerCamelCase , __lowerCamelCase , "encoder" , "pre_mlp_layer_norm" ) lowercase_ , lowercase_ = tax_mlp_lookup(__lowerCamelCase , __lowerCamelCase , "encoder" , __lowerCamelCase ) lowercase_ = layer_norm if split_mlp_wi: lowercase_ = wi[0].T lowercase_ = wi[1].T else: lowercase_ = wi.T lowercase_ = wo.T if scalable_attention: # convert the rel_embedding of each layer lowercase_ = tax_relpos_bias_lookup( __lowerCamelCase , __lowerCamelCase , "encoder" ).T lowercase_ = old["encoder/encoder_norm/scale"] if not scalable_attention: lowercase_ = tax_relpos_bias_lookup( __lowerCamelCase , 0 , "encoder" ).T lowercase_ = tax_relpos_bias_lookup( __lowerCamelCase , 0 , "decoder" ).T if not is_encoder_only: # Decoder. for i in range(__lowerCamelCase ): # Block i, layer 0 (Self Attention). lowercase_ = tax_layer_norm_lookup(__lowerCamelCase , __lowerCamelCase , "decoder" , "pre_self_attention_layer_norm" ) lowercase_ , lowercase_ , lowercase_ , lowercase_ = tax_attention_lookup(__lowerCamelCase , __lowerCamelCase , "decoder" , "self_attention" ) lowercase_ = layer_norm lowercase_ = k.T lowercase_ = o.T lowercase_ = q.T lowercase_ = v.T # Block i, layer 1 (Cross Attention). lowercase_ = tax_layer_norm_lookup(__lowerCamelCase , __lowerCamelCase , "decoder" , "pre_cross_attention_layer_norm" ) lowercase_ , lowercase_ , lowercase_ , lowercase_ = tax_attention_lookup(__lowerCamelCase , __lowerCamelCase , "decoder" , "encoder_decoder_attention" ) lowercase_ = layer_norm lowercase_ = k.T lowercase_ = o.T lowercase_ = q.T lowercase_ = v.T # Block i, layer 2 (MLP). lowercase_ = tax_layer_norm_lookup(__lowerCamelCase , __lowerCamelCase , "decoder" , "pre_mlp_layer_norm" ) lowercase_ , lowercase_ = tax_mlp_lookup(__lowerCamelCase , __lowerCamelCase , "decoder" , __lowerCamelCase ) lowercase_ = layer_norm if split_mlp_wi: lowercase_ = wi[0].T lowercase_ = wi[1].T else: lowercase_ = wi.T lowercase_ = wo.T if scalable_attention: # convert the rel_embedding of each layer lowercase_ = tax_relpos_bias_lookup(__lowerCamelCase , __lowerCamelCase , "decoder" ).T lowercase_ = old["decoder/decoder_norm/scale"] # LM Head (only in v1.1 checkpoints, in v1.0 embeddings are used instead) if "decoder/logits_dense/kernel" in old: lowercase_ = old["decoder/logits_dense/kernel"].T return new def SCREAMING_SNAKE_CASE_ ( __lowerCamelCase: Dict , __lowerCamelCase: bool ): '''simple docstring''' lowercase_ = collections.OrderedDict([(k, torch.from_numpy(v.copy() )) for (k, v) in converted_params.items()] ) # Add what is missing. if "encoder.embed_tokens.weight" not in state_dict: lowercase_ = state_dict["shared.weight"] if not is_encoder_only: if "decoder.embed_tokens.weight" not in state_dict: lowercase_ = state_dict["shared.weight"] if "lm_head.weight" not in state_dict: # For old 1.0 models. print("Using shared word embeddings as lm_head." ) lowercase_ = state_dict["shared.weight"] return state_dict def SCREAMING_SNAKE_CASE_ ( __lowerCamelCase: Dict , __lowerCamelCase: Union[str, Any] , __lowerCamelCase: Union[str, Any] , __lowerCamelCase: List[Any] , __lowerCamelCase: Any ): '''simple docstring''' lowercase_ = checkpoints.load_tax_checkpoint(__lowerCamelCase ) lowercase_ = convert_tax_to_pytorch( __lowerCamelCase , num_layers=config.num_layers , is_encoder_only=__lowerCamelCase , scalable_attention=__lowerCamelCase ) lowercase_ = make_state_dict(__lowerCamelCase , __lowerCamelCase ) model.load_state_dict(__lowerCamelCase , strict=__lowerCamelCase ) def SCREAMING_SNAKE_CASE_ ( __lowerCamelCase: Dict , __lowerCamelCase: Optional[Any] , __lowerCamelCase: List[str] , __lowerCamelCase: bool = False , __lowerCamelCase: bool = False , ): '''simple docstring''' lowercase_ = MTaConfig.from_json_file(__lowerCamelCase ) print(F'Building PyTorch model from configuration: {config}' ) # Non-v1.1 checkpoints could also use T5Model, but this works for all. # The v1.0 checkpoints will simply have an LM head that is the word embeddings. if is_encoder_only: lowercase_ = UMTaEncoderModel(__lowerCamelCase ) else: lowercase_ = UMTaForConditionalGeneration(__lowerCamelCase ) # Load weights from tf checkpoint load_tax_weights_in_ta(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) # Save pytorch-model print(F'Save PyTorch model to {pytorch_dump_path}' ) model.save_pretrained(__lowerCamelCase ) # Verify that we can load the checkpoint. model.from_pretrained(__lowerCamelCase ) print("Done" ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = argparse.ArgumentParser(description="""Converts a native T5X checkpoint into a PyTorch checkpoint.""") # Required parameters parser.add_argument( """--t5x_checkpoint_path""", default=None, type=str, required=True, help="""Path to the T5X checkpoint.""" ) parser.add_argument( """--config_file""", default=None, type=str, required=True, help="""The config json file corresponding to the pre-trained T5 model.\nThis specifies the model architecture.""", ) parser.add_argument( """--pytorch_dump_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model.""" ) parser.add_argument( """--is_encoder_only""", action="""store_true""", help="""Check if the model is encoder-decoder model""", default=False ) parser.add_argument( """--scalable_attention""", action="""store_true""", help="""Whether the model uses scaled attention (umt5 model)""", default=False, ) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_tax_checkpoint_to_pytorch( args.tax_checkpoint_path, args.config_file, args.pytorch_dump_path, args.is_encoder_only, args.scalable_attention, )
297
0
from math import sqrt def _a ( a :int ) -> bool: assert isinstance(a , a ) and ( number >= 0 ), "'number' must been an int and positive" a = True # 0 and 1 are none primes. if number <= 1: a = False for divisor in range(2 , int(round(sqrt(a ) ) ) + 1 ): # if 'number' divisible by 'divisor' then sets 'status' # of false and break up the loop. if number % divisor == 0: a = False break # precondition assert isinstance(a , a ), "'status' must been from type bool" return status def _a ( a :Tuple ) -> str: assert isinstance(a , a ) and (n > 2), "'N' must been an int and > 2" # beginList: contains all natural numbers from 2 up to N a = list(range(2 , n + 1 ) ) a = [] # this list will be returns. # actual sieve of erathostenes for i in range(len(a ) ): for j in range(i + 1 , len(a ) ): if (begin_list[i] != 0) and (begin_list[j] % begin_list[i] == 0): a = 0 # filters actual prime numbers. a = [x for x in begin_list if x != 0] # precondition assert isinstance(a , a ), "'ans' must been from type list" return ans def _a ( a :Optional[Any] ) -> Optional[Any]: assert isinstance(a , a ) and (n > 2), "'N' must been an int and > 2" a = [] # iterates over all numbers between 2 up to N+1 # if a number is prime then appends to list 'ans' for number in range(2 , n + 1 ): if is_prime(a ): ans.append(a ) # precondition assert isinstance(a , a ), "'ans' must been from type list" return ans def _a ( a :Dict ) -> str: assert isinstance(a , a ) and number >= 0, "'number' must been an int and >= 0" a = [] # this list will be returns of the function. # potential prime number factors. a = 2 a = number if number == 0 or number == 1: ans.append(a ) # if 'number' not prime then builds the prime factorization of 'number' elif not is_prime(a ): while quotient != 1: if is_prime(a ) and (quotient % factor == 0): ans.append(a ) quotient /= factor else: factor += 1 else: ans.append(a ) # precondition assert isinstance(a , a ), "'ans' must been from type list" return ans def _a ( a :List[str] ) -> Union[str, Any]: assert isinstance(a , a ) and ( number >= 0 ), "'number' bust been an int and >= 0" a = 0 # prime factorization of 'number' a = prime_factorization(a ) a = max(a ) # precondition assert isinstance(a , a ), "'ans' must been from type int" return ans def _a ( a :Optional[int] ) -> List[str]: assert isinstance(a , a ) and ( number >= 0 ), "'number' bust been an int and >= 0" a = 0 # prime factorization of 'number' a = prime_factorization(a ) a = min(a ) # precondition assert isinstance(a , a ), "'ans' must been from type int" return ans def _a ( a :Any ) -> Any: assert isinstance(a , a ), "'number' must been an int" assert isinstance(number % 2 == 0 , a ), "compare bust been from type bool" return number % 2 == 0 def _a ( a :Any ) -> int: assert isinstance(a , a ), "'number' must been an int" assert isinstance(number % 2 != 0 , a ), "compare bust been from type bool" return number % 2 != 0 def _a ( a :Union[str, Any] ) -> Tuple: assert ( isinstance(a , a ) and (number > 2) and is_even(a ) ), "'number' must been an int, even and > 2" a = [] # this list will returned # creates a list of prime numbers between 2 up to 'number' a = get_prime_numbers(a ) a = len(a ) # run variable for while-loops. a = 0 a = None # exit variable. for break up the loops a = True while i < len_pn and loop: a = i + 1 while j < len_pn and loop: if prime_numbers[i] + prime_numbers[j] == number: a = False ans.append(prime_numbers[i] ) ans.append(prime_numbers[j] ) j += 1 i += 1 # precondition assert ( isinstance(a , a ) and (len(a ) == 2) and (ans[0] + ans[1] == number) and is_prime(ans[0] ) and is_prime(ans[1] ) ), "'ans' must contains two primes. And sum of elements must been eq 'number'" return ans def _a ( a :Dict , a :int ) -> Dict: assert ( isinstance(a , a ) and isinstance(a , a ) and (numbera >= 0) and (numbera >= 0) ), "'number1' and 'number2' must been positive integer." a = 0 while numbera != 0: a = numbera % numbera a = numbera a = rest # precondition assert isinstance(a , a ) and ( numbera >= 0 ), "'number' must been from type int and positive" return numbera def _a ( a :str , a :str ) -> str: assert ( isinstance(a , a ) and isinstance(a , a ) and (numbera >= 1) and (numbera >= 1) ), "'number1' and 'number2' must been positive integer." a = 1 # actual answer that will be return. # for kgV (x,1) if numbera > 1 and numbera > 1: # builds the prime factorization of 'number1' and 'number2' a = prime_factorization(a ) a = prime_factorization(a ) elif numbera == 1 or numbera == 1: a = [] a = [] a = max(a , a ) a = 0 a = 0 a = [] # captured numbers int both 'primeFac1' and 'primeFac2' # iterates through primeFac1 for n in prime_fac_a: if n not in done: if n in prime_fac_a: a = prime_fac_a.count(a ) a = prime_fac_a.count(a ) for _ in range(max(a , a ) ): ans *= n else: a = prime_fac_a.count(a ) for _ in range(a ): ans *= n done.append(a ) # iterates through primeFac2 for n in prime_fac_a: if n not in done: a = prime_fac_a.count(a ) for _ in range(a ): ans *= n done.append(a ) # precondition assert isinstance(a , a ) and ( ans >= 0 ), "'ans' must been from type int and positive" return ans def _a ( a :int ) -> Any: assert isinstance(a , a ) and (n >= 0), "'number' must been a positive int" a = 0 a = 2 # this variable holds the answer while index < n: index += 1 ans += 1 # counts to the next number # if ans not prime then # runs to the next prime number. while not is_prime(a ): ans += 1 # precondition assert isinstance(a , a ) and is_prime( a ), "'ans' must been a prime number and from type int" return ans def _a ( a :Optional[Any] , a :Optional[int] ) -> List[Any]: assert ( is_prime(a ) and is_prime(a ) and (p_number_a < p_number_a) ), "The arguments must been prime numbers and 'pNumber1' < 'pNumber2'" a = p_number_a + 1 # jump to the next number a = [] # this list will be returns. # if number is not prime then # fetch the next prime number. while not is_prime(a ): number += 1 while number < p_number_a: ans.append(a ) number += 1 # fetch the next prime number. while not is_prime(a ): number += 1 # precondition assert ( isinstance(a , a ) and ans[0] != p_number_a and ans[len(a ) - 1] != p_number_a ), "'ans' must been a list without the arguments" # 'ans' contains not 'pNumber1' and 'pNumber2' ! return ans def _a ( a :Optional[Any] ) -> Optional[Any]: assert isinstance(a , a ) and (n >= 1), "'n' must been int and >= 1" a = [] # will be returned. for divisor in range(1 , n + 1 ): if n % divisor == 0: ans.append(a ) # precondition assert ans[0] == 1 and ans[len(a ) - 1] == n, "Error in function getDivisiors(...)" return ans def _a ( a :List[Any] ) -> Optional[Any]: assert isinstance(a , a ) and ( number > 1 ), "'number' must been an int and >= 1" a = get_divisors(a ) # precondition assert ( isinstance(a , a ) and (divisors[0] == 1) and (divisors[len(a ) - 1] == number) ), "Error in help-function getDivisiors(...)" # summed all divisors up to 'number' (exclusive), hence [:-1] return sum(divisors[:-1] ) == number def _a ( a :Optional[int] , a :Optional[int] ) -> Dict: assert ( isinstance(a , a ) and isinstance(a , a ) and (denominator != 0) ), "The arguments must been from type int and 'denominator' != 0" # build the greatest common divisor of numerator and denominator. a = gcd(abs(a ) , abs(a ) ) # precondition assert ( isinstance(a , a ) and (numerator % gcd_of_fraction == 0) and (denominator % gcd_of_fraction == 0) ), "Error in function gcd(...,...)" return (numerator // gcd_of_fraction, denominator // gcd_of_fraction) def _a ( a :str ) -> str: assert isinstance(a , a ) and (n >= 0), "'n' must been a int and >= 0" a = 1 # this will be return. for factor in range(1 , n + 1 ): ans *= factor return ans def _a ( a :List[str] ) -> Optional[Any]: assert isinstance(a , a ) and (n >= 0), "'n' must been an int and >= 0" a = 0 a = 1 a = 1 # this will be return for _ in range(n - 1 ): a = ans ans += fiba a = tmp return ans
0
'''simple docstring''' import subprocess import sys from transformers import BertConfig, BertModel, BertTokenizer, pipeline from transformers.testing_utils import TestCasePlus, require_torch class lowercase__ ( lowercase ): @require_torch def UpperCamelCase_ ( self : Dict ): '''simple docstring''' # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before # `transformers` is loaded, and it's too late for inside pytest - so we are changing it # while running an external program # python one-liner segments # this must be loaded before socket.socket is monkey-patched _UpperCamelCase : Any = '\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n ' _UpperCamelCase : Dict = '\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n ' _UpperCamelCase : Optional[Any] = '\nimport socket\ndef offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet")\nsocket.socket = offline_socket\n ' # Force fetching the files so that we can use the cache _UpperCamelCase : Optional[Any] = 'hf-internal-testing/tiny-random-bert' BertConfig.from_pretrained(lowerCamelCase__ ) BertModel.from_pretrained(lowerCamelCase__ ) BertTokenizer.from_pretrained(lowerCamelCase__ ) pipeline(task='fill-mask' ,model=lowerCamelCase__ ) # baseline - just load from_pretrained with normal network _UpperCamelCase : Optional[int] = [sys.executable, '-c', '\n'.join([load, run, mock] )] # should succeed _UpperCamelCase : Dict = self.get_env() # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files _UpperCamelCase : str = '1' _UpperCamelCase : Union[str, Any] = subprocess.run(lowerCamelCase__ ,env=lowerCamelCase__ ,check=lowerCamelCase__ ,capture_output=lowerCamelCase__ ) self.assertEqual(result.returncode ,0 ,result.stderr ) self.assertIn('success' ,result.stdout.decode() ) @require_torch def UpperCamelCase_ ( self : Tuple ): '''simple docstring''' # python one-liner segments # this must be loaded before socket.socket is monkey-patched _UpperCamelCase : Any = '\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n ' _UpperCamelCase : Any = '\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n ' _UpperCamelCase : Any = '\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")\nsocket.socket = offline_socket\n ' # Force fetching the files so that we can use the cache _UpperCamelCase : List[Any] = 'hf-internal-testing/tiny-random-bert' BertConfig.from_pretrained(lowerCamelCase__ ) BertModel.from_pretrained(lowerCamelCase__ ) BertTokenizer.from_pretrained(lowerCamelCase__ ) pipeline(task='fill-mask' ,model=lowerCamelCase__ ) # baseline - just load from_pretrained with normal network _UpperCamelCase : Union[str, Any] = [sys.executable, '-c', '\n'.join([load, run, mock] )] # should succeed _UpperCamelCase : List[Any] = self.get_env() _UpperCamelCase : Dict = subprocess.run(lowerCamelCase__ ,env=lowerCamelCase__ ,check=lowerCamelCase__ ,capture_output=lowerCamelCase__ ) self.assertEqual(result.returncode ,0 ,result.stderr ) self.assertIn('success' ,result.stdout.decode() ) @require_torch def UpperCamelCase_ ( self : Union[str, Any] ): '''simple docstring''' # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before # `transformers` is loaded, and it's too late for inside pytest - so we are changing it # while running an external program # python one-liner segments # this must be loaded before socket.socket is monkey-patched _UpperCamelCase : Optional[Any] = '\nfrom transformers import BertConfig, BertModel, BertTokenizer\n ' _UpperCamelCase : str = '\nmname = "hf-internal-testing/tiny-random-bert-sharded"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nprint("success")\n ' _UpperCamelCase : Any = '\nimport socket\ndef offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")\nsocket.socket = offline_socket\n ' # baseline - just load from_pretrained with normal network _UpperCamelCase : Optional[int] = [sys.executable, '-c', '\n'.join([load, run] )] # should succeed _UpperCamelCase : Optional[Any] = self.get_env() _UpperCamelCase : int = subprocess.run(lowerCamelCase__ ,env=lowerCamelCase__ ,check=lowerCamelCase__ ,capture_output=lowerCamelCase__ ) self.assertEqual(result.returncode ,0 ,result.stderr ) self.assertIn('success' ,result.stdout.decode() ) # next emulate no network _UpperCamelCase : Dict = [sys.executable, '-c', '\n'.join([load, mock, run] )] # Doesn't fail anymore since the model is in the cache due to other tests, so commenting this. # env["TRANSFORMERS_OFFLINE"] = "0" # result = subprocess.run(cmd, env=env, check=False, capture_output=True) # self.assertEqual(result.returncode, 1, result.stderr) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files _UpperCamelCase : Dict = '1' _UpperCamelCase : Dict = subprocess.run(lowerCamelCase__ ,env=lowerCamelCase__ ,check=lowerCamelCase__ ,capture_output=lowerCamelCase__ ) self.assertEqual(result.returncode ,0 ,result.stderr ) self.assertIn('success' ,result.stdout.decode() ) @require_torch def UpperCamelCase_ ( self : str ): '''simple docstring''' _UpperCamelCase : int = '\nfrom transformers import pipeline\n ' _UpperCamelCase : str = '\nmname = "hf-internal-testing/tiny-random-bert"\npipe = pipeline(model=mname)\n ' _UpperCamelCase : Optional[Any] = '\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")\nsocket.socket = offline_socket\n ' _UpperCamelCase : Union[str, Any] = self.get_env() _UpperCamelCase : List[Any] = '1' _UpperCamelCase : Tuple = [sys.executable, '-c', '\n'.join([load, mock, run] )] _UpperCamelCase : int = subprocess.run(lowerCamelCase__ ,env=lowerCamelCase__ ,check=lowerCamelCase__ ,capture_output=lowerCamelCase__ ) self.assertEqual(result.returncode ,1 ,result.stderr ) self.assertIn( 'You cannot infer task automatically within `pipeline` when using offline mode' ,result.stderr.decode().replace('\n' ,'' ) ,) @require_torch def UpperCamelCase_ ( self : Dict ): '''simple docstring''' _UpperCamelCase : Optional[int] = '\nfrom transformers import AutoModel\n ' _UpperCamelCase : int = '\nmname = "hf-internal-testing/test_dynamic_model"\nAutoModel.from_pretrained(mname, trust_remote_code=True)\nprint("success")\n ' # baseline - just load from_pretrained with normal network _UpperCamelCase : Any = [sys.executable, '-c', '\n'.join([load, run] )] # should succeed _UpperCamelCase : Optional[Any] = self.get_env() _UpperCamelCase : Optional[int] = subprocess.run(lowerCamelCase__ ,env=lowerCamelCase__ ,check=lowerCamelCase__ ,capture_output=lowerCamelCase__ ) self.assertEqual(result.returncode ,0 ,result.stderr ) self.assertIn('success' ,result.stdout.decode() ) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files _UpperCamelCase : List[Any] = '1' _UpperCamelCase : Dict = subprocess.run(lowerCamelCase__ ,env=lowerCamelCase__ ,check=lowerCamelCase__ ,capture_output=lowerCamelCase__ ) self.assertEqual(result.returncode ,0 ,result.stderr ) self.assertIn('success' ,result.stdout.decode() )
83
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _SCREAMING_SNAKE_CASE : int = { "configuration_timesformer": ["TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "TimesformerConfig"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Dict = [ "TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TimesformerModel", "TimesformerForVideoClassification", "TimesformerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_timesformer import ( TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimesformerForVideoClassification, TimesformerModel, TimesformerPreTrainedModel, ) else: import sys _SCREAMING_SNAKE_CASE : int = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
213
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyImgaImgPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class A__ ( snake_case__ , unittest.TestCase ): """simple docstring""" __magic_name__ = KandinskyImgaImgPipeline __magic_name__ = ['prompt', 'image_embeds', 'negative_image_embeds', 'image'] __magic_name__ = [ 'prompt', 'negative_prompt', 'image_embeds', 'negative_image_embeds', 'image', ] __magic_name__ = [ 'generator', 'height', 'width', 'strength', 'guidance_scale', 'negative_prompt', 'num_inference_steps', 'return_dict', 'guidance_scale', 'num_images_per_prompt', 'output_type', 'return_dict', ] __magic_name__ = False @property def a_ ( self ): return 3_2 @property def a_ ( self ): return 3_2 @property def a_ ( self ): return self.time_input_dim @property def a_ ( self ): return self.time_input_dim * 4 @property def a_ ( self ): return 1_0_0 @property def a_ ( self ): snake_case = XLMRobertaTokenizerFast.from_pretrained('''YiYiXu/tiny-random-mclip-base''' ) return tokenizer @property def a_ ( self ): torch.manual_seed(0 ) snake_case = MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=3_7 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=1_0_0_5 , ) snake_case = MultilingualCLIP(__snake_case ) snake_case = text_encoder.eval() return text_encoder @property def a_ ( self ): torch.manual_seed(0 ) snake_case = { '''in_channels''': 4, # Out channels is double in channels because predicts mean and variance '''out_channels''': 8, '''addition_embed_type''': '''text_image''', '''down_block_types''': ('''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D'''), '''up_block_types''': ('''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''), '''mid_block_type''': '''UNetMidBlock2DSimpleCrossAttn''', '''block_out_channels''': (self.block_out_channels_a, self.block_out_channels_a * 2), '''layers_per_block''': 1, '''encoder_hid_dim''': self.text_embedder_hidden_size, '''encoder_hid_dim_type''': '''text_image_proj''', '''cross_attention_dim''': self.cross_attention_dim, '''attention_head_dim''': 4, '''resnet_time_scale_shift''': '''scale_shift''', '''class_embed_type''': None, } snake_case = UNetaDConditionModel(**__snake_case ) return model @property def a_ ( self ): return { "block_out_channels": [3_2, 6_4], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 1_2, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def a_ ( self ): torch.manual_seed(0 ) snake_case = VQModel(**self.dummy_movq_kwargs ) return model def a_ ( self ): snake_case = self.dummy_text_encoder snake_case = self.dummy_tokenizer snake_case = self.dummy_unet snake_case = self.dummy_movq snake_case = { '''num_train_timesteps''': 1_0_0_0, '''beta_schedule''': '''linear''', '''beta_start''': 0.0_0085, '''beta_end''': 0.012, '''clip_sample''': False, '''set_alpha_to_one''': False, '''steps_offset''': 0, '''prediction_type''': '''epsilon''', '''thresholding''': False, } snake_case = DDIMScheduler(**__snake_case ) snake_case = { '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''unet''': unet, '''scheduler''': scheduler, '''movq''': movq, } return components def a_ ( self , __snake_case , __snake_case=0 ): snake_case = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(__snake_case ) ).to(__snake_case ) snake_case = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(__snake_case ) # create init_image snake_case = floats_tensor((1, 3, 6_4, 6_4) , rng=random.Random(__snake_case ) ).to(__snake_case ) snake_case = image.cpu().permute(0 , 2 , 3 , 1 )[0] snake_case = Image.fromarray(np.uinta(__snake_case ) ).convert('''RGB''' ).resize((2_5_6, 2_5_6) ) if str(__snake_case ).startswith('''mps''' ): snake_case = torch.manual_seed(__snake_case ) else: snake_case = torch.Generator(device=__snake_case ).manual_seed(__snake_case ) snake_case = { '''prompt''': '''horse''', '''image''': init_image, '''image_embeds''': image_embeds, '''negative_image_embeds''': negative_image_embeds, '''generator''': generator, '''height''': 6_4, '''width''': 6_4, '''num_inference_steps''': 1_0, '''guidance_scale''': 7.0, '''strength''': 0.2, '''output_type''': '''np''', } return inputs def a_ ( self ): snake_case = '''cpu''' snake_case = self.get_dummy_components() snake_case = self.pipeline_class(**__snake_case ) snake_case = pipe.to(__snake_case ) pipe.set_progress_bar_config(disable=__snake_case ) snake_case = pipe(**self.get_dummy_inputs(__snake_case ) ) snake_case = output.images snake_case = pipe( **self.get_dummy_inputs(__snake_case ) , return_dict=__snake_case , )[0] snake_case = image[0, -3:, -3:, -1] snake_case = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 6_4, 6_4, 3) snake_case = np.array( [0.6147_4943, 0.607_3539, 0.4330_8544, 0.592_8269, 0.4749_3595, 0.4675_5973, 0.461_3838, 0.4536_8797, 0.5011_9233] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 ), F''' expected_slice {expected_slice}, but got {image_slice.flatten()}''' assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 ), F''' expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}''' @slow @require_torch_gpu class A__ ( unittest.TestCase ): """simple docstring""" def a_ ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def a_ ( self ): snake_case = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/kandinsky_img2img_frog.npy''' ) snake_case = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/cat.png''' ) snake_case = '''A red cartoon frog, 4k''' snake_case = KandinskyPriorPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-1-prior''' , torch_dtype=torch.floataa ) pipe_prior.to(__snake_case ) snake_case = KandinskyImgaImgPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-1''' , torch_dtype=torch.floataa ) snake_case = pipeline.to(__snake_case ) pipeline.set_progress_bar_config(disable=__snake_case ) snake_case = torch.Generator(device='''cpu''' ).manual_seed(0 ) snake_case , snake_case = pipe_prior( __snake_case , generator=__snake_case , num_inference_steps=5 , negative_prompt='''''' , ).to_tuple() snake_case = pipeline( __snake_case , image=__snake_case , image_embeds=__snake_case , negative_image_embeds=__snake_case , generator=__snake_case , num_inference_steps=1_0_0 , height=7_6_8 , width=7_6_8 , strength=0.2 , output_type='''np''' , ) snake_case = output.images[0] assert image.shape == (7_6_8, 7_6_8, 3) assert_mean_pixel_difference(__snake_case , __snake_case )
213
1
import unittest from transformers import LiltConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( LiltForQuestionAnswering, LiltForSequenceClassification, LiltForTokenClassification, LiltModel, ) from transformers.models.lilt.modeling_lilt import LILT_PRETRAINED_MODEL_ARCHIVE_LIST class _a : """simple docstring""" def __init__( self: List[str] , __lowerCamelCase: Optional[Any] , __lowerCamelCase: Dict=13 , __lowerCamelCase: List[str]=7 , __lowerCamelCase: Union[str, Any]=True , __lowerCamelCase: List[Any]=True , __lowerCamelCase: Any=True , __lowerCamelCase: Any=True , __lowerCamelCase: int=99 , __lowerCamelCase: Dict=24 , __lowerCamelCase: str=2 , __lowerCamelCase: List[str]=6 , __lowerCamelCase: Tuple=37 , __lowerCamelCase: Optional[int]="gelu" , __lowerCamelCase: int=0.1 , __lowerCamelCase: List[str]=0.1 , __lowerCamelCase: Optional[Any]=512 , __lowerCamelCase: List[Any]=16 , __lowerCamelCase: Tuple=2 , __lowerCamelCase: Optional[int]=0.02 , __lowerCamelCase: Any=3 , __lowerCamelCase: Tuple=None , __lowerCamelCase: Any=1000 , ): '''simple docstring''' UpperCamelCase__: List[str] = parent UpperCamelCase__: Union[str, Any] = batch_size UpperCamelCase__: int = seq_length UpperCamelCase__: Dict = is_training UpperCamelCase__: List[str] = use_input_mask UpperCamelCase__: List[Any] = use_token_type_ids UpperCamelCase__: List[str] = use_labels UpperCamelCase__: Optional[Any] = vocab_size UpperCamelCase__: Any = hidden_size UpperCamelCase__: Union[str, Any] = num_hidden_layers UpperCamelCase__: Dict = num_attention_heads UpperCamelCase__: int = intermediate_size UpperCamelCase__: Union[str, Any] = hidden_act UpperCamelCase__: Dict = hidden_dropout_prob UpperCamelCase__: Optional[Any] = attention_probs_dropout_prob UpperCamelCase__: Dict = max_position_embeddings UpperCamelCase__: Any = type_vocab_size UpperCamelCase__: str = type_sequence_label_size UpperCamelCase__: Union[str, Any] = initializer_range UpperCamelCase__: List[str] = num_labels UpperCamelCase__: List[Any] = scope UpperCamelCase__: Tuple = range_bbox def UpperCAmelCase_ ( self: Union[str, Any] ): '''simple docstring''' UpperCamelCase__: Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCamelCase__: List[str] = ids_tensor([self.batch_size, self.seq_length, 4] , self.range_bbox ) # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: UpperCamelCase__: int = bbox[i, j, 3] UpperCamelCase__: Tuple = bbox[i, j, 1] UpperCamelCase__: List[Any] = t if bbox[i, j, 2] < bbox[i, j, 0]: UpperCamelCase__: Optional[int] = bbox[i, j, 2] UpperCamelCase__: Union[str, Any] = bbox[i, j, 0] UpperCamelCase__: List[Any] = t UpperCamelCase__: int = None if self.use_input_mask: UpperCamelCase__: Optional[int] = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) UpperCamelCase__: Tuple = None if self.use_token_type_ids: UpperCamelCase__: Tuple = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) UpperCamelCase__: Optional[int] = None UpperCamelCase__: Union[str, Any] = None if self.use_labels: UpperCamelCase__: Tuple = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase__: Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCamelCase__: Dict = self.get_config() return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels def UpperCAmelCase_ ( self: List[Any] ): '''simple docstring''' return LiltConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , ) def UpperCAmelCase_ ( self: Optional[int] , __lowerCamelCase: List[str] , __lowerCamelCase: Tuple , __lowerCamelCase: Optional[int] , __lowerCamelCase: Optional[int] , __lowerCamelCase: Any , __lowerCamelCase: Dict , __lowerCamelCase: Optional[int] , ): '''simple docstring''' UpperCamelCase__: List[Any] = LiltModel(config=__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() UpperCamelCase__: str = model(__lowerCamelCase , bbox=__lowerCamelCase , attention_mask=__lowerCamelCase , token_type_ids=__lowerCamelCase ) UpperCamelCase__: str = model(__lowerCamelCase , bbox=__lowerCamelCase , token_type_ids=__lowerCamelCase ) UpperCamelCase__: Optional[Any] = model(__lowerCamelCase , bbox=__lowerCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def UpperCAmelCase_ ( self: Union[str, Any] , __lowerCamelCase: str , __lowerCamelCase: List[str] , __lowerCamelCase: int , __lowerCamelCase: Optional[Any] , __lowerCamelCase: str , __lowerCamelCase: int , __lowerCamelCase: List[Any] , ): '''simple docstring''' UpperCamelCase__: Optional[int] = self.num_labels UpperCamelCase__: str = LiltForTokenClassification(config=__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() UpperCamelCase__: Any = model( __lowerCamelCase , bbox=__lowerCamelCase , attention_mask=__lowerCamelCase , token_type_ids=__lowerCamelCase , labels=__lowerCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def UpperCAmelCase_ ( self: Tuple , __lowerCamelCase: str , __lowerCamelCase: Any , __lowerCamelCase: str , __lowerCamelCase: int , __lowerCamelCase: str , __lowerCamelCase: Any , __lowerCamelCase: List[str] , ): '''simple docstring''' UpperCamelCase__: Dict = LiltForQuestionAnswering(config=__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() UpperCamelCase__: List[str] = model( __lowerCamelCase , bbox=__lowerCamelCase , attention_mask=__lowerCamelCase , token_type_ids=__lowerCamelCase , start_positions=__lowerCamelCase , end_positions=__lowerCamelCase , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def UpperCAmelCase_ ( self: Any ): '''simple docstring''' UpperCamelCase__: Optional[Any] = self.prepare_config_and_inputs() ( ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ( UpperCamelCase__ ) , ): List[str] = config_and_inputs UpperCamelCase__: Union[str, Any] = { "input_ids": input_ids, "bbox": bbox, "token_type_ids": token_type_ids, "attention_mask": input_mask, } return config, inputs_dict @require_torch class _a ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , unittest.TestCase): """simple docstring""" UpperCamelCase__ = ( ( LiltModel, LiltForSequenceClassification, LiltForTokenClassification, LiltForQuestionAnswering, ) if is_torch_available() else () ) UpperCamelCase__ = ( { """feature-extraction""": LiltModel, """question-answering""": LiltForQuestionAnswering, """text-classification""": LiltForSequenceClassification, """token-classification""": LiltForTokenClassification, """zero-shot""": LiltForSequenceClassification, } if is_torch_available() else {} ) UpperCamelCase__ = False UpperCamelCase__ = False def UpperCAmelCase_ ( self: List[str] , __lowerCamelCase: Optional[int] , __lowerCamelCase: Optional[int] , __lowerCamelCase: List[Any] , __lowerCamelCase: Optional[int] , __lowerCamelCase: Optional[int] ): '''simple docstring''' return True def UpperCAmelCase_ ( self: int ): '''simple docstring''' UpperCamelCase__: str = LiltModelTester(self ) UpperCamelCase__: Tuple = ConfigTester(self , config_class=__lowerCamelCase , hidden_size=37 ) def UpperCAmelCase_ ( self: Dict ): '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase_ ( self: List[str] ): '''simple docstring''' UpperCamelCase__: Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__lowerCamelCase ) def UpperCAmelCase_ ( self: Optional[Any] ): '''simple docstring''' UpperCamelCase__: Optional[int] = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: UpperCamelCase__: List[str] = type self.model_tester.create_and_check_model(*__lowerCamelCase ) def UpperCAmelCase_ ( self: List[Any] ): '''simple docstring''' UpperCamelCase__: Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*__lowerCamelCase ) def UpperCAmelCase_ ( self: List[Any] ): '''simple docstring''' UpperCamelCase__: Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*__lowerCamelCase ) @slow def UpperCAmelCase_ ( self: Any ): '''simple docstring''' for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase__: int = LiltModel.from_pretrained(__lowerCamelCase ) self.assertIsNotNone(__lowerCamelCase ) @require_torch @slow class _a ( unittest.TestCase): """simple docstring""" def UpperCAmelCase_ ( self: Optional[Any] ): '''simple docstring''' UpperCamelCase__: Tuple = LiltModel.from_pretrained("SCUT-DLVCLab/lilt-roberta-en-base" ).to(__lowerCamelCase ) UpperCamelCase__: int = torch.tensor([[1, 2]] , device=__lowerCamelCase ) UpperCamelCase__: Any = torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]] , device=__lowerCamelCase ) # forward pass with torch.no_grad(): UpperCamelCase__: Union[str, Any] = model(input_ids=__lowerCamelCase , bbox=__lowerCamelCase ) UpperCamelCase__: Dict = torch.Size([1, 2, 768] ) UpperCamelCase__: Optional[int] = torch.tensor( [[-0.0_653, 0.0_950, -0.0_061], [-0.0_545, 0.0_926, -0.0_324]] , device=__lowerCamelCase , ) self.assertTrue(outputs.last_hidden_state.shape , __lowerCamelCase ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :, :3] , __lowerCamelCase , atol=1e-3 ) )
149
class _a : """simple docstring""" def __init__( self: Union[str, Any] , __lowerCamelCase: int , __lowerCamelCase: Tuple=None , __lowerCamelCase: Optional[Any]=None ): '''simple docstring''' UpperCamelCase__: Any = data UpperCamelCase__: Tuple = previous UpperCamelCase__: Any = next_node def __str__( self: str ): '''simple docstring''' return F"{self.data}" def UpperCAmelCase_ ( self: Optional[Any] ): '''simple docstring''' return self.data def UpperCAmelCase_ ( self: str ): '''simple docstring''' return self.next def UpperCAmelCase_ ( self: Optional[int] ): '''simple docstring''' return self.previous class _a : """simple docstring""" def __init__( self: List[str] , __lowerCamelCase: str ): '''simple docstring''' UpperCamelCase__: Optional[Any] = head def __iter__( self: Optional[int] ): '''simple docstring''' return self def UpperCAmelCase_ ( self: Dict ): '''simple docstring''' if not self.current: raise StopIteration else: UpperCamelCase__: Tuple = self.current.get_data() UpperCamelCase__: str = self.current.get_next() return value class _a : """simple docstring""" def __init__( self: List[Any] ): '''simple docstring''' UpperCamelCase__: List[str] = None # First node in list UpperCamelCase__: str = None # Last node in list def __str__( self: List[Any] ): '''simple docstring''' UpperCamelCase__: Dict = self.head UpperCamelCase__: int = [] while current is not None: nodes.append(current.get_data() ) UpperCamelCase__: Optional[Any] = current.get_next() return " ".join(str(__lowerCamelCase ) for node in nodes ) def __contains__( self: List[str] , __lowerCamelCase: int ): '''simple docstring''' UpperCamelCase__: Any = self.head while current: if current.get_data() == value: return True UpperCamelCase__: int = current.get_next() return False def __iter__( self: List[Any] ): '''simple docstring''' return LinkedListIterator(self.head ) def UpperCAmelCase_ ( self: List[str] ): '''simple docstring''' if self.head: return self.head.get_data() return None def UpperCAmelCase_ ( self: Optional[int] ): '''simple docstring''' if self.tail: return self.tail.get_data() return None def UpperCAmelCase_ ( self: List[str] , __lowerCamelCase: Node ): '''simple docstring''' if self.head is None: UpperCamelCase__: List[str] = node UpperCamelCase__: List[str] = node else: self.insert_before_node(self.head , __lowerCamelCase ) def UpperCAmelCase_ ( self: Any , __lowerCamelCase: Node ): '''simple docstring''' if self.head is None: self.set_head(__lowerCamelCase ) else: self.insert_after_node(self.tail , __lowerCamelCase ) def UpperCAmelCase_ ( self: Dict , __lowerCamelCase: int ): '''simple docstring''' UpperCamelCase__: Optional[int] = Node(__lowerCamelCase ) if self.head is None: self.set_head(__lowerCamelCase ) else: self.set_tail(__lowerCamelCase ) def UpperCAmelCase_ ( self: Tuple , __lowerCamelCase: Node , __lowerCamelCase: Node ): '''simple docstring''' UpperCamelCase__: Tuple = node UpperCamelCase__: int = node.previous if node.get_previous() is None: UpperCamelCase__: List[str] = node_to_insert else: UpperCamelCase__: Union[str, Any] = node_to_insert UpperCamelCase__: Dict = node_to_insert def UpperCAmelCase_ ( self: Dict , __lowerCamelCase: Node , __lowerCamelCase: Node ): '''simple docstring''' UpperCamelCase__: List[Any] = node UpperCamelCase__: Dict = node.next if node.get_next() is None: UpperCamelCase__: Optional[int] = node_to_insert else: UpperCamelCase__: Optional[int] = node_to_insert UpperCamelCase__: Any = node_to_insert def UpperCAmelCase_ ( self: List[Any] , __lowerCamelCase: int , __lowerCamelCase: int ): '''simple docstring''' UpperCamelCase__: Optional[int] = 1 UpperCamelCase__: Dict = Node(__lowerCamelCase ) UpperCamelCase__: Dict = self.head while node: if current_position == position: self.insert_before_node(__lowerCamelCase , __lowerCamelCase ) return current_position += 1 UpperCamelCase__: Dict = node.next self.insert_after_node(self.tail , __lowerCamelCase ) def UpperCAmelCase_ ( self: List[Any] , __lowerCamelCase: int ): '''simple docstring''' UpperCamelCase__: Any = self.head while node: if node.get_data() == item: return node UpperCamelCase__: str = node.get_next() raise Exception("Node not found" ) def UpperCAmelCase_ ( self: Union[str, Any] , __lowerCamelCase: Any ): '''simple docstring''' if (node := self.get_node(__lowerCamelCase )) is not None: if node == self.head: UpperCamelCase__: List[Any] = self.head.get_next() if node == self.tail: UpperCamelCase__: Union[str, Any] = self.tail.get_previous() self.remove_node_pointers(__lowerCamelCase ) @staticmethod def UpperCAmelCase_ ( __lowerCamelCase: Node ): '''simple docstring''' if node.get_next(): UpperCamelCase__: List[str] = node.previous if node.get_previous(): UpperCamelCase__: Union[str, Any] = node.next UpperCamelCase__: Union[str, Any] = None UpperCamelCase__: int = None def UpperCAmelCase_ ( self: List[Any] ): '''simple docstring''' return self.head is None def lowerCAmelCase_ ( ): pass if __name__ == "__main__": import doctest doctest.testmod()
149
1
import os from typing import Optional import fsspec from fsspec.archive import AbstractArchiveFileSystem from fsspec.utils import DEFAULT_BLOCK_SIZE class _a ( _lowerCAmelCase ): A = '''''' A = ( None # protocol passed in prefix to the url. ex: "gzip", for gzip://file.txt::http://foo.bar/file.txt.gz ) A = None # compression type in fsspec. ex: "gzip" A = None # extension of the filename to strip. ex: "".gz" to get file.txt from file.txt.gz def __init__(self, SCREAMING_SNAKE_CASE_ = "", SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, **SCREAMING_SNAKE_CASE_ ) -> Dict: super().__init__(self, **_SCREAMING_SNAKE_CASE ) # always open as "rb" since fsspec can then use the TextIOWrapper to make it work for "r" mode UpperCAmelCase_: List[Any] = fsspec.open( _SCREAMING_SNAKE_CASE, mode="""rb""", protocol=_SCREAMING_SNAKE_CASE, compression=self.compression, client_kwargs={ """requote_redirect_url""": False, # see https://github.com/huggingface/datasets/pull/5459 """trust_env""": True, # Enable reading proxy env variables. **(target_options or {}).pop("""client_kwargs""", {} ), # To avoid issues if it was already passed. }, **(target_options or {}), ) UpperCAmelCase_: Tuple = os.path.basename(self.file.path.split("""::""" )[0] ) UpperCAmelCase_: List[str] = ( self.compressed_name[: self.compressed_name.rindex(""".""" )] if '''.''' in self.compressed_name else self.compressed_name ) UpperCAmelCase_: List[str] = None @classmethod def __snake_case (cls, SCREAMING_SNAKE_CASE_ ) -> Tuple: return super()._strip_protocol(_SCREAMING_SNAKE_CASE ).lstrip("""/""" ) def __snake_case (self ) -> Optional[Any]: if self.dir_cache is None: UpperCAmelCase_: str = {**self.file.fs.info(self.file.path ), '''name''': self.uncompressed_name} UpperCAmelCase_: Any = {f['''name''']: f} def __snake_case (self, SCREAMING_SNAKE_CASE_ ) -> Tuple: return self.file.open().read() def __snake_case (self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ = "rb", SCREAMING_SNAKE_CASE_=None, SCREAMING_SNAKE_CASE_=True, SCREAMING_SNAKE_CASE_=None, **SCREAMING_SNAKE_CASE_, ) -> List[Any]: UpperCAmelCase_: Any = self._strip_protocol(_SCREAMING_SNAKE_CASE ) if mode != "rb": raise ValueError(f'Tried to read with mode {mode} on file {self.file.path} opened with mode \'rb\'' ) return self.file.open() class _a ( _lowerCAmelCase ): A = '''bz2''' A = '''bz2''' A = '''.bz2''' class _a ( _lowerCAmelCase ): A = '''gzip''' A = '''gzip''' A = '''.gz''' class _a ( _lowerCAmelCase ): A = '''lz4''' A = '''lz4''' A = '''.lz4''' class _a ( _lowerCAmelCase ): A = '''xz''' A = '''xz''' A = '''.xz''' class _a ( _lowerCAmelCase ): A = '''zstd''' A = '''zstd''' A = '''.zst''' def __init__(self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ = "rb", SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = DEFAULT_BLOCK_SIZE, **SCREAMING_SNAKE_CASE_, ) -> str: super().__init__( fo=_SCREAMING_SNAKE_CASE, mode=_SCREAMING_SNAKE_CASE, target_protocol=_SCREAMING_SNAKE_CASE, target_options=_SCREAMING_SNAKE_CASE, block_size=_SCREAMING_SNAKE_CASE, **_SCREAMING_SNAKE_CASE, ) # We need to wrap the zstd decompressor to avoid this error in fsspec==2021.7.0 and zstandard==0.15.2: # # File "/Users/user/.virtualenvs/hf-datasets/lib/python3.7/site-packages/fsspec/core.py", line 145, in open # out.close = close # AttributeError: 'zstd.ZstdDecompressionReader' object attribute 'close' is read-only # # see https://github.com/intake/filesystem_spec/issues/725 UpperCAmelCase_: Tuple = self.file.__enter__ class _a : def __init__(self, SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: UpperCAmelCase_: Any = file_ def __enter__(self ) -> Any: self._file.__enter__() return self def __exit__(self, *SCREAMING_SNAKE_CASE_, **SCREAMING_SNAKE_CASE_ ) -> List[str]: self._file.__exit__(*_SCREAMING_SNAKE_CASE, **_SCREAMING_SNAKE_CASE ) def __iter__(self ) -> Any: return iter(self._file ) def __snake_case (self ) -> Optional[Any]: return next(self._file ) def __getattr__(self, SCREAMING_SNAKE_CASE_ ) -> Any: return getattr(self._file, _SCREAMING_SNAKE_CASE ) def fixed_enter(*SCREAMING_SNAKE_CASE_, **SCREAMING_SNAKE_CASE_ ): return WrappedFile(_enter(*_SCREAMING_SNAKE_CASE, **_SCREAMING_SNAKE_CASE ) ) UpperCAmelCase_: Optional[Any] = fixed_enter
371
import unittest from transformers import load_tool from .test_tools_common import ToolTesterMixin class _a ( unittest.TestCase , _lowerCAmelCase ): def __snake_case (self ) -> Optional[int]: UpperCAmelCase_: Optional[int] = load_tool("""text-classification""" ) self.tool.setup() UpperCAmelCase_: str = load_tool("""text-classification""", remote=SCREAMING_SNAKE_CASE_ ) def __snake_case (self ) -> Optional[Any]: UpperCAmelCase_: Any = self.tool("""That's quite cool""", ["""positive""", """negative"""] ) self.assertEqual(SCREAMING_SNAKE_CASE_, """positive""" ) def __snake_case (self ) -> Optional[int]: UpperCAmelCase_: List[str] = self.remote_tool("""That's quite cool""", ["""positive""", """negative"""] ) self.assertEqual(SCREAMING_SNAKE_CASE_, """positive""" ) def __snake_case (self ) -> Any: UpperCAmelCase_: Tuple = self.tool(text="""That's quite cool""", labels=["""positive""", """negative"""] ) self.assertEqual(SCREAMING_SNAKE_CASE_, """positive""" ) def __snake_case (self ) -> int: UpperCAmelCase_: Dict = self.remote_tool(text="""That's quite cool""", labels=["""positive""", """negative"""] ) self.assertEqual(SCREAMING_SNAKE_CASE_, """positive""" )
82
0
from __future__ import annotations def lowerCamelCase__ ( __lowerCAmelCase : list[int] , __lowerCAmelCase : int ): """simple docstring""" lowerCAmelCase_ = 0 lowerCAmelCase_ = len(__lowerCAmelCase ) - 1 while i < j: if nums[i] + nums[j] == target: return [i, j] elif nums[i] + nums[j] < target: lowerCAmelCase_ = i + 1 else: lowerCAmelCase_ = j - 1 return [] if __name__ == "__main__": import doctest doctest.testmod() print(f"""{two_pointer([2, 7, 11, 15], 9) = }""")
231
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _A = {"configuration_xglm": ["XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP", "XGLMConfig"]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A = ["XGLMTokenizer"] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A = ["XGLMTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A = [ "XGLM_PRETRAINED_MODEL_ARCHIVE_LIST", "XGLMForCausalLM", "XGLMModel", "XGLMPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A = [ "FlaxXGLMForCausalLM", "FlaxXGLMModel", "FlaxXGLMPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A = [ "TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST", "TFXGLMForCausalLM", "TFXGLMModel", "TFXGLMPreTrainedModel", ] if TYPE_CHECKING: from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm import XGLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm_fast import XGLMTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, TFXGLMPreTrainedModel, ) else: import sys _A = _LazyModule(__name__, globals()["__file__"], _import_structure)
231
1
'''simple docstring''' import unittest import numpy as np from transformers import AlbertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.albert.modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, ) class _lowerCamelCase ( unittest.TestCase ): '''simple docstring''' def __init__( self : Tuple , _A : Dict , _A : List[str]=13 , _A : Union[str, Any]=7 , _A : Union[str, Any]=True , _A : Union[str, Any]=True , _A : Dict=True , _A : Optional[int]=True , _A : Dict=99 , _A : Tuple=32 , _A : Dict=5 , _A : str=4 , _A : Tuple=37 , _A : List[Any]="gelu" , _A : str=0.1 , _A : Tuple=0.1 , _A : List[str]=512 , _A : Tuple=16 , _A : Optional[int]=2 , _A : Dict=0.02 , _A : Any=4 , ) -> Union[str, Any]: __magic_name__ : Optional[int] = parent __magic_name__ : Dict = batch_size __magic_name__ : Optional[int] = seq_length __magic_name__ : Dict = is_training __magic_name__ : List[Any] = use_attention_mask __magic_name__ : List[Any] = use_token_type_ids __magic_name__ : Optional[int] = use_labels __magic_name__ : int = vocab_size __magic_name__ : Tuple = hidden_size __magic_name__ : str = num_hidden_layers __magic_name__ : str = num_attention_heads __magic_name__ : int = intermediate_size __magic_name__ : Optional[int] = hidden_act __magic_name__ : int = hidden_dropout_prob __magic_name__ : Union[str, Any] = attention_probs_dropout_prob __magic_name__ : Optional[Any] = max_position_embeddings __magic_name__ : List[Any] = type_vocab_size __magic_name__ : Dict = type_sequence_label_size __magic_name__ : Any = initializer_range __magic_name__ : List[Any] = num_choices def __lowerCAmelCase ( self : str ) -> Optional[Any]: __magic_name__ : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __magic_name__ : List[Any] = None if self.use_attention_mask: __magic_name__ : str = random_attention_mask([self.batch_size, self.seq_length] ) __magic_name__ : int = None if self.use_token_type_ids: __magic_name__ : Dict = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __magic_name__ : Any = AlbertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=_lowercase , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def __lowerCAmelCase ( self : List[str] ) -> str: __magic_name__ : List[str] = self.prepare_config_and_inputs() __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ : int = config_and_inputs __magic_name__ : Any = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': attention_mask} return config, inputs_dict @require_flax class _lowerCamelCase ( lowercase__ , unittest.TestCase ): '''simple docstring''' A_ : Optional[Any] = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def __lowerCAmelCase ( self : Dict ) -> Optional[int]: __magic_name__ : List[str] = FlaxAlbertModelTester(self ) @slow def __lowerCAmelCase ( self : Union[str, Any] ) -> str: for model_class_name in self.all_model_classes: __magic_name__ : Optional[int] = model_class_name.from_pretrained('albert-base-v2' ) __magic_name__ : Union[str, Any] = model(np.ones((1, 1) ) ) self.assertIsNotNone(_lowercase ) @require_flax class _lowerCamelCase ( unittest.TestCase ): '''simple docstring''' @slow def __lowerCAmelCase ( self : Optional[int] ) -> List[str]: __magic_name__ : List[str] = FlaxAlbertModel.from_pretrained('albert-base-v2' ) __magic_name__ : List[str] = np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]] ) __magic_name__ : Union[str, Any] = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) __magic_name__ : int = model(_lowercase , attention_mask=_lowercase )[0] __magic_name__ : str = (1, 11, 768) self.assertEqual(output.shape , _lowercase ) __magic_name__ : Optional[int] = np.array( [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]] ) self.assertTrue(jnp.allclose(output[:, 1:4, 1:4] , _lowercase , atol=1E-4 ) )
366
'''simple docstring''' from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, logging lowerCAmelCase :Tuple = logging.get_logger(__name__) class _lowerCamelCase ( lowercase__ ): '''simple docstring''' A_ : int = ["""pixel_values"""] def __init__( self : Any , _A : bool = True , _A : Optional[Dict[str, int]] = None , _A : PILImageResampling = PILImageResampling.BILINEAR , _A : bool = True , _A : Dict[str, int] = None , _A : bool = True , _A : Union[int, float] = 1 / 255 , _A : bool = True , _A : Optional[Union[float, List[float]]] = None , _A : Optional[Union[float, List[float]]] = None , **_A : Optional[Any] , ) -> None: super().__init__(**_A ) __magic_name__ : List[str] = size if size is not None else {'shortest_edge': 256} __magic_name__ : str = get_size_dict(_A , default_to_square=_A ) __magic_name__ : List[str] = crop_size if crop_size is not None else {'height': 224, 'width': 224} __magic_name__ : Optional[int] = get_size_dict(_A ) __magic_name__ : Union[str, Any] = do_resize __magic_name__ : List[Any] = size __magic_name__ : List[str] = resample __magic_name__ : Dict = do_center_crop __magic_name__ : List[str] = crop_size __magic_name__ : int = do_rescale __magic_name__ : Tuple = rescale_factor __magic_name__ : List[str] = do_normalize __magic_name__ : Union[str, Any] = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN __magic_name__ : Dict = image_std if image_std is not None else IMAGENET_STANDARD_STD def __lowerCAmelCase ( self : Optional[Any] , _A : np.ndarray , _A : Dict[str, int] , _A : PILImageResampling = PILImageResampling.BICUBIC , _A : Optional[Union[str, ChannelDimension]] = None , **_A : List[str] , ) -> np.ndarray: __magic_name__ : Optional[Any] = get_size_dict(_A , default_to_square=_A ) if "shortest_edge" not in size: raise ValueError(F'The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}' ) __magic_name__ : Dict = get_resize_output_image_size(_A , size=size['shortest_edge'] , default_to_square=_A ) return resize(_A , size=_A , resample=_A , data_format=_A , **_A ) def __lowerCAmelCase ( self : Dict , _A : np.ndarray , _A : Dict[str, int] , _A : Optional[Union[str, ChannelDimension]] = None , **_A : Optional[int] , ) -> np.ndarray: __magic_name__ : int = get_size_dict(_A ) return center_crop(_A , size=(size['height'], size['width']) , data_format=_A , **_A ) def __lowerCAmelCase ( self : List[str] , _A : np.ndarray , _A : float , _A : Optional[Union[str, ChannelDimension]] = None , **_A : Tuple ) -> np.ndarray: return rescale(_A , scale=_A , data_format=_A , **_A ) def __lowerCAmelCase ( self : str , _A : np.ndarray , _A : Union[float, List[float]] , _A : Union[float, List[float]] , _A : Optional[Union[str, ChannelDimension]] = None , **_A : int , ) -> np.ndarray: return normalize(_A , mean=_A , std=_A , data_format=_A , **_A ) def __lowerCAmelCase ( self : List[str] , _A : ImageInput , _A : Optional[bool] = None , _A : Dict[str, int] = None , _A : PILImageResampling = None , _A : bool = None , _A : Dict[str, int] = None , _A : Optional[bool] = None , _A : Optional[float] = None , _A : Optional[bool] = None , _A : Optional[Union[float, List[float]]] = None , _A : Optional[Union[float, List[float]]] = None , _A : Optional[Union[str, TensorType]] = None , _A : Union[str, ChannelDimension] = ChannelDimension.FIRST , **_A : List[Any] , ) -> List[str]: __magic_name__ : int = do_resize if do_resize is not None else self.do_resize __magic_name__ : Tuple = size if size is not None else self.size __magic_name__ : Optional[Any] = get_size_dict(_A , default_to_square=_A ) __magic_name__ : Dict = resample if resample is not None else self.resample __magic_name__ : Optional[Any] = do_center_crop if do_center_crop is not None else self.do_center_crop __magic_name__ : Dict = crop_size if crop_size is not None else self.crop_size __magic_name__ : List[str] = get_size_dict(_A ) __magic_name__ : Union[str, Any] = do_rescale if do_rescale is not None else self.do_rescale __magic_name__ : str = rescale_factor if rescale_factor is not None else self.rescale_factor __magic_name__ : Any = do_normalize if do_normalize is not None else self.do_normalize __magic_name__ : Tuple = image_mean if image_mean is not None else self.image_mean __magic_name__ : Union[str, Any] = image_std if image_std is not None else self.image_std __magic_name__ : int = make_list_of_images(_A ) if not valid_images(_A ): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.' ) if do_resize and size is None: raise ValueError('Size must be specified if do_resize is True.' ) if do_center_crop and crop_size is None: raise ValueError('Crop size must be specified if do_center_crop is True.' ) if do_rescale and rescale_factor is None: raise ValueError('Rescale factor must be specified if do_rescale is True.' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('Image mean and std must be specified if do_normalize is True.' ) # All transformations expect numpy arrays. __magic_name__ : List[Any] = [to_numpy_array(_A ) for image in images] if do_resize: __magic_name__ : Union[str, Any] = [self.resize(image=_A , size=_A , resample=_A ) for image in images] if do_center_crop: __magic_name__ : Union[str, Any] = [self.center_crop(image=_A , size=_A ) for image in images] if do_rescale: __magic_name__ : List[Any] = [self.rescale(image=_A , scale=_A ) for image in images] if do_normalize: __magic_name__ : Optional[Any] = [self.normalize(image=_A , mean=_A , std=_A ) for image in images] __magic_name__ : Union[str, Any] = [to_channel_dimension_format(_A , _A ) for image in images] __magic_name__ : List[str] = {'pixel_values': images} return BatchFeature(data=_A , tensor_type=_A )
275
0
"""simple docstring""" def _A ( UpperCamelCase_ : list[int], UpperCamelCase_ : list[int]) -> None: '''simple docstring''' __lowercase = len(UpperCamelCase_) print("The following activities are selected:") # The first activity is always selected __lowercase = 0 print(UpperCamelCase_, end=",") # Consider rest of the activities for j in range(UpperCamelCase_): # If this activity has start time greater than # or equal to the finish time of previously # selected activity, then select it if start[j] >= finish[i]: print(UpperCamelCase_, end=",") __lowercase = j if __name__ == "__main__": import doctest doctest.testmod() _a = [1, 3, 0, 5, 8, 5] _a = [2, 4, 6, 7, 9, 9] print_max_activities(start, finish)
17
from __future__ import annotations def a__ ( UpperCAmelCase : list[list[int]] ) -> bool: UpperCAmelCase : Union[str, Any] = len(UpperCAmelCase ) # We need to create solution object to save path. UpperCAmelCase : int = [[0 for _ in range(UpperCAmelCase )] for _ in range(UpperCAmelCase )] UpperCAmelCase : Union[str, Any] = run_maze(UpperCAmelCase , 0 , 0 , UpperCAmelCase ) if solved: print('''\n'''.join(str(UpperCAmelCase ) for row in solutions ) ) else: print('''No solution exists!''' ) return solved def a__ ( UpperCAmelCase : list[list[int]] , UpperCAmelCase : int , UpperCAmelCase : int , UpperCAmelCase : list[list[int]] ) -> bool: UpperCAmelCase : Dict = len(UpperCAmelCase ) # Final check point. if i == j == (size - 1): UpperCAmelCase : Dict = 1 return True UpperCAmelCase : Union[str, Any] = (not i < 0) and (not j < 0) # Check lower bounds UpperCAmelCase : List[Any] = (i < size) and (j < size) # Check upper bounds if lower_flag and upper_flag: # check for already visited and block points. UpperCAmelCase : Any = (not solutions[i][j]) and (not maze[i][j]) if block_flag: # check visited UpperCAmelCase : str = 1 # check for directions if ( run_maze(UpperCAmelCase , i + 1 , UpperCAmelCase , UpperCAmelCase ) or run_maze(UpperCAmelCase , UpperCAmelCase , j + 1 , UpperCAmelCase ) or run_maze(UpperCAmelCase , i - 1 , UpperCAmelCase , UpperCAmelCase ) or run_maze(UpperCAmelCase , UpperCAmelCase , j - 1 , UpperCAmelCase ) ): return True UpperCAmelCase : Any = 0 return False return False if __name__ == "__main__": import doctest doctest.testmod()
336
0
import argparse import re import requests import torch # git clone https://github.com/salesforce/BLIP.git from models.blip import blip_decoder from models.blip_itm import blip_itm from models.blip_vqa import blip_vqa from PIL import Image from torchvision import transforms from torchvision.transforms.functional import InterpolationMode from transformers import ( BertTokenizer, BlipConfig, BlipForConditionalGeneration, BlipForImageTextRetrieval, BlipForQuestionAnswering, ) def __lowerCAmelCase ( __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' __snake_case : Any = """https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg""" __snake_case : str = Image.open(requests.get(__SCREAMING_SNAKE_CASE , stream=__SCREAMING_SNAKE_CASE ).raw ).convert("""RGB""" ) __snake_case : str = transforms.Compose( [ transforms.Resize((image_size, image_size) , interpolation=InterpolationMode.BICUBIC ), transforms.ToTensor(), transforms.Normalize((0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73) , (0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11) ), ] ) __snake_case : Any = transform(__SCREAMING_SNAKE_CASE ).unsqueeze(0 ).to(__SCREAMING_SNAKE_CASE ) return image def __lowerCAmelCase ( __SCREAMING_SNAKE_CASE : Optional[Any] ): '''simple docstring''' if "visual_encoder" in key: __snake_case : Tuple = re.sub("""visual_encoder*""" , """vision_model.encoder""" , __SCREAMING_SNAKE_CASE ) if "blocks" in key: __snake_case : int = re.sub(R"""blocks""" , """layers""" , __SCREAMING_SNAKE_CASE ) if "attn" in key: __snake_case : Tuple = re.sub(R"""attn""" , """self_attn""" , __SCREAMING_SNAKE_CASE ) if "norm1" in key: __snake_case : Tuple = re.sub(R"""norm1""" , """layer_norm1""" , __SCREAMING_SNAKE_CASE ) if "norm2" in key: __snake_case : Tuple = re.sub(R"""norm2""" , """layer_norm2""" , __SCREAMING_SNAKE_CASE ) if "encoder.norm" in key: __snake_case : Dict = re.sub(R"""encoder.norm""" , """post_layernorm""" , __SCREAMING_SNAKE_CASE ) if "encoder.patch_embed.proj" in key: __snake_case : List[Any] = re.sub(R"""encoder.patch_embed.proj""" , """embeddings.patch_embedding""" , __SCREAMING_SNAKE_CASE ) if "encoder.pos_embed" in key: __snake_case : Tuple = re.sub(R"""encoder.pos_embed""" , """embeddings.position_embedding""" , __SCREAMING_SNAKE_CASE ) if "encoder.cls_token" in key: __snake_case : int = re.sub(R"""encoder.cls_token""" , """embeddings.class_embedding""" , __SCREAMING_SNAKE_CASE ) if "self_attn" in key: __snake_case : List[str] = re.sub(R"""self_attn.proj""" , """self_attn.projection""" , __SCREAMING_SNAKE_CASE ) return key @torch.no_grad() def __lowerCAmelCase ( __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : Optional[Any]=None ): '''simple docstring''' if config_path is not None: __snake_case : Any = BlipConfig.from_pretrained(__SCREAMING_SNAKE_CASE ) else: __snake_case : Tuple = BlipConfig(projection_dim=5_1_2 , text_config={} , vision_config={} ) __snake_case : Tuple = BlipForConditionalGeneration(__SCREAMING_SNAKE_CASE ).eval() __snake_case : Tuple = """https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_capfilt_large.pth""" __snake_case : str = blip_decoder(pretrained=__SCREAMING_SNAKE_CASE , image_size=3_8_4 , vit="""base""" ) __snake_case : List[str] = pt_model.eval() __snake_case : Tuple = pt_model.state_dict() for key in modified_state_dict.copy(): __snake_case : List[str] = modified_state_dict.pop(__SCREAMING_SNAKE_CASE ) __snake_case : List[Any] = rename_key(__SCREAMING_SNAKE_CASE ) __snake_case : Any = value hf_model.load_state_dict(__SCREAMING_SNAKE_CASE ) __snake_case : int = 3_8_4 __snake_case : Optional[int] = load_demo_image(image_size=__SCREAMING_SNAKE_CASE , device="""cpu""" ) __snake_case : Optional[Any] = BertTokenizer.from_pretrained("""bert-base-uncased""" ) __snake_case : Optional[int] = tokenizer(["""a picture of"""] ).input_ids __snake_case : Any = hf_model.generate(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert out[0].tolist() == [3_0_5_2_2, 1_0_3_7, 3_8_6_1, 1_9_9_7, 1_0_3_7, 2_4_5_0, 3_5_6_4, 2_0_0_6, 1_9_9_6, 3_5_0_9, 2_0_0_7, 2_0_1_4, 3_8_9_9, 1_0_2] __snake_case : Union[str, Any] = hf_model.generate(__SCREAMING_SNAKE_CASE ) assert out[0].tolist() == [3_0_5_2_2, 1_0_3_7, 2_4_5_0, 3_5_6_4, 2_0_0_6, 1_9_9_6, 3_5_0_9, 2_0_0_7, 2_0_1_4, 3_8_9_9, 1_0_2] if pytorch_dump_folder_path is not None: hf_model.save_pretrained(__SCREAMING_SNAKE_CASE ) # model_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_vqa.pth' __snake_case : List[str] = ( """https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_vqa_capfilt_large.pth""" ) __snake_case : Dict = blip_vqa(pretrained=__SCREAMING_SNAKE_CASE , image_size=__SCREAMING_SNAKE_CASE , vit="""base""" ) vqa_model.eval() __snake_case : int = vqa_model.state_dict() for key in modified_state_dict.copy(): __snake_case : int = modified_state_dict.pop(__SCREAMING_SNAKE_CASE ) __snake_case : List[Any] = rename_key(__SCREAMING_SNAKE_CASE ) __snake_case : Optional[Any] = value __snake_case : Any = BlipForQuestionAnswering(__SCREAMING_SNAKE_CASE ) hf_vqa_model.load_state_dict(__SCREAMING_SNAKE_CASE ) __snake_case : Optional[Any] = ["""How many dogs are in this image?"""] __snake_case : List[Any] = tokenizer(__SCREAMING_SNAKE_CASE , return_tensors="""pt""" ).input_ids __snake_case : List[str] = hf_vqa_model.generate(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) print(tokenizer.decode(answer[0] ) ) assert tokenizer.decode(answer[0] ) == "[UNK] 1 [SEP]" if pytorch_dump_folder_path is not None: hf_vqa_model.save_pretrained(pytorch_dump_folder_path + """_vqa""" ) __snake_case : int = """https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_retrieval_coco.pth""" __snake_case : int = blip_itm(pretrained=__SCREAMING_SNAKE_CASE , image_size=__SCREAMING_SNAKE_CASE , vit="""base""" ) itm_model.eval() __snake_case : List[Any] = itm_model.state_dict() for key in modified_state_dict.copy(): __snake_case : Optional[Any] = modified_state_dict.pop(__SCREAMING_SNAKE_CASE ) __snake_case : List[str] = rename_key(__SCREAMING_SNAKE_CASE ) __snake_case : Union[str, Any] = value __snake_case : Union[str, Any] = BlipForImageTextRetrieval(__SCREAMING_SNAKE_CASE ) __snake_case : Optional[Any] = ["""A picture of a woman with a dog sitting in a beach"""] __snake_case : str = tokenizer( __SCREAMING_SNAKE_CASE , return_tensors="""pt""" , padding="""max_length""" , truncation=__SCREAMING_SNAKE_CASE , max_length=3_5 , ).input_ids hf_itm_model.load_state_dict(__SCREAMING_SNAKE_CASE ) hf_itm_model.eval() __snake_case : Tuple = hf_itm_model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , use_itm_head=__SCREAMING_SNAKE_CASE ) __snake_case : Dict = hf_itm_model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , use_itm_head=__SCREAMING_SNAKE_CASE ) assert out[0].item() == 0.21_10_68_74_94_27_79_54 assert torch.nn.functional.softmax(out_itm[0] , dim=1 )[:, 1].item() == 0.4_56_98_84_53_86_50_51_27 if pytorch_dump_folder_path is not None: hf_itm_model.save_pretrained(pytorch_dump_folder_path + """_itm""" ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert") lowercase_ = parser.parse_args() convert_blip_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
20
from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_VISION_2_SEQ_MAPPING if is_torch_available(): import torch from ..models.auto.modeling_auto import MODEL_FOR_VISION_2_SEQ_MAPPING lowercase_ = logging.get_logger(__name__) @add_end_docstrings(__UpperCamelCase ) class SCREAMING_SNAKE_CASE__ ( __UpperCamelCase ): def __init__( self : Dict , *_lowerCAmelCase : Dict , **_lowerCAmelCase : int ): super().__init__(*_lowerCAmelCase , **_lowerCAmelCase ) requires_backends(self , """vision""" ) self.check_model_type( TF_MODEL_FOR_VISION_2_SEQ_MAPPING if self.framework == """tf""" else MODEL_FOR_VISION_2_SEQ_MAPPING ) def snake_case__ ( self : List[Any] , _lowerCAmelCase : List[str]=None , _lowerCAmelCase : Any=None , _lowerCAmelCase : List[str]=None ): __snake_case : Optional[Any] = {} __snake_case : int = {} if prompt is not None: __snake_case : Dict = prompt if generate_kwargs is not None: __snake_case : List[Any] = generate_kwargs if max_new_tokens is not None: if "generate_kwargs" not in forward_kwargs: __snake_case : Optional[int] = {} if "max_new_tokens" in forward_kwargs["generate_kwargs"]: raise ValueError( """'max_new_tokens' is defined twice, once in 'generate_kwargs' and once as a direct parameter,""" """ please use only one""" ) __snake_case : Any = max_new_tokens return preprocess_params, forward_kwargs, {} def __call__( self : Optional[Any] , _lowerCAmelCase : Union[str, List[str], "Image.Image", List["Image.Image"]] , **_lowerCAmelCase : Union[str, Any] ): return super().__call__(_lowerCAmelCase , **_lowerCAmelCase ) def snake_case__ ( self : Optional[Any] , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : List[str]=None ): __snake_case : Optional[Any] = load_image(_lowerCAmelCase ) if prompt is not None: if not isinstance(_lowerCAmelCase , _lowerCAmelCase ): raise ValueError( f'''Received an invalid text input, got - {type(_lowerCAmelCase )} - but expected a single string. ''' """Note also that one single text can be provided for conditional image to text generation.""" ) __snake_case : Tuple = self.model.config.model_type if model_type == "git": __snake_case : Tuple = self.image_processor(images=_lowerCAmelCase , return_tensors=self.framework ) __snake_case : Any = self.tokenizer(text=_lowerCAmelCase , add_special_tokens=_lowerCAmelCase ).input_ids __snake_case : Tuple = [self.tokenizer.cls_token_id] + input_ids __snake_case : int = torch.tensor(_lowerCAmelCase ).unsqueeze(0 ) model_inputs.update({"""input_ids""": input_ids} ) elif model_type == "pix2struct": __snake_case : Dict = self.image_processor(images=_lowerCAmelCase , header_text=_lowerCAmelCase , return_tensors=self.framework ) elif model_type != "vision-encoder-decoder": # vision-encoder-decoder does not support conditional generation __snake_case : int = self.image_processor(images=_lowerCAmelCase , return_tensors=self.framework ) __snake_case : Optional[Any] = self.tokenizer(_lowerCAmelCase , return_tensors=self.framework ) model_inputs.update(_lowerCAmelCase ) else: raise ValueError(f'''Model type {model_type} does not support conditional text generation''' ) else: __snake_case : Tuple = self.image_processor(images=_lowerCAmelCase , return_tensors=self.framework ) if self.model.config.model_type == "git" and prompt is None: __snake_case : int = None return model_inputs def snake_case__ ( self : Union[str, Any] , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : str=None ): # Git model sets `model_inputs["input_ids"] = None` in `preprocess` (when `prompt=None`). In batch model, the # pipeline will group them into a list of `None`, which fail `_forward`. Avoid this by checking it first. if ( "input_ids" in model_inputs and isinstance(model_inputs["""input_ids"""] , _lowerCAmelCase ) and all(x is None for x in model_inputs["""input_ids"""] ) ): __snake_case : List[Any] = None if generate_kwargs is None: __snake_case : Dict = {} # FIXME: We need to pop here due to a difference in how `generation.py` and `generation.tf_utils.py` # parse inputs. In the Tensorflow version, `generate` raises an error if we don't use `input_ids` whereas # the PyTorch version matches it with `self.model.main_input_name` or `self.model.encoder.main_input_name` # in the `_prepare_model_inputs` method. __snake_case : Dict = model_inputs.pop(self.model.main_input_name ) __snake_case : Optional[int] = self.model.generate(_lowerCAmelCase , **_lowerCAmelCase , **_lowerCAmelCase ) return model_outputs def snake_case__ ( self : List[Any] , _lowerCAmelCase : str ): __snake_case : Union[str, Any] = [] for output_ids in model_outputs: __snake_case : Union[str, Any] = { """generated_text""": self.tokenizer.decode( _lowerCAmelCase , skip_special_tokens=_lowerCAmelCase , ) } records.append(_lowerCAmelCase ) return records
20
1
'''simple docstring''' import itertools import math def __lowerCAmelCase ( UpperCamelCase__ ) -> bool: if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(UpperCamelCase__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def __lowerCAmelCase ( ) -> List[Any]: __lowerCamelCase = 2 while True: if is_prime(UpperCamelCase__ ): yield num num += 1 def __lowerCAmelCase ( UpperCamelCase__ = 1_00_01 ) -> int: return next(itertools.islice(prime_generator() , nth - 1 , UpperCamelCase__ ) ) if __name__ == "__main__": print(f'{solution() = }')
67
'''simple docstring''' import os import shutil import tempfile import unittest import numpy as np from transformers import AutoTokenizer, BarkProcessor from transformers.testing_utils import require_torch, slow @require_torch class a__ ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = '''ylacombe/bark-small''' __lowerCamelCase = tempfile.mkdtemp() __lowerCamelCase = '''en_speaker_1''' __lowerCamelCase = '''This is a test string''' __lowerCamelCase = '''speaker_embeddings_path.json''' __lowerCamelCase = '''speaker_embeddings''' def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , **a : Dict ): """simple docstring""" return AutoTokenizer.from_pretrained(self.checkpoint , **a ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" __lowerCamelCase = self.get_tokenizer() __lowerCamelCase = BarkProcessor(tokenizer=a ) processor.save_pretrained(self.tmpdirname ) __lowerCamelCase = BarkProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) @slow def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" __lowerCamelCase = BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) processor.save_pretrained( self.tmpdirname , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , speaker_embeddings_directory=self.speaker_embeddings_directory , ) __lowerCamelCase = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) __lowerCamelCase = BarkProcessor.from_pretrained( self.tmpdirname , self.speaker_embeddings_dict_path , bos_token='''(BOS)''' , eos_token='''(EOS)''' , ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) __lowerCamelCase = 35 __lowerCamelCase = 2 __lowerCamelCase = 8 __lowerCamelCase = { '''semantic_prompt''': np.ones(a ), '''coarse_prompt''': np.ones((nb_codebooks_coarse, seq_len) ), '''fine_prompt''': np.ones((nb_codebooks_total, seq_len) ), } # test providing already loaded voice_preset __lowerCamelCase = processor(text=self.input_string , voice_preset=a ) __lowerCamelCase = inputs['''history_prompt'''] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(a , np.array([] ) ).tolist() ) # test loading voice preset from npz file __lowerCamelCase = os.path.join(self.tmpdirname , '''file.npz''' ) np.savez(a , **a ) __lowerCamelCase = processor(text=self.input_string , voice_preset=a ) __lowerCamelCase = inputs['''history_prompt'''] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(a , np.array([] ) ).tolist() ) # test loading voice preset from the hub __lowerCamelCase = processor(text=self.input_string , voice_preset=self.voice_preset ) def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" __lowerCamelCase = self.get_tokenizer() __lowerCamelCase = BarkProcessor(tokenizer=a ) __lowerCamelCase = processor(text=self.input_string ) __lowerCamelCase = tokenizer( self.input_string , padding='''max_length''' , max_length=2_56 , add_special_tokens=a , return_attention_mask=a , return_token_type_ids=a , ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key].squeeze().tolist() )
67
1
import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = ['''image_processor''', '''tokenizer'''] __snake_case = '''CLIPImageProcessor''' __snake_case = ('''CLIPTokenizer''', '''CLIPTokenizerFast''') def __init__( self : Dict , __UpperCAmelCase : str=None , __UpperCAmelCase : Optional[Any]=None , **__UpperCAmelCase : Optional[Any] ) ->List[str]: """simple docstring""" a = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , __UpperCAmelCase , ) a = kwargs.pop('''feature_extractor''' ) a = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(__UpperCAmelCase , __UpperCAmelCase ) def __call__( self : List[str] , __UpperCAmelCase : Any=None , __UpperCAmelCase : Dict=None , __UpperCAmelCase : Any=None , **__UpperCAmelCase : str ) ->Optional[Any]: """simple docstring""" if text is None and images is None: raise ValueError('''You have to specify either text or images. Both cannot be none.''' ) if text is not None: a = self.tokenizer(__UpperCAmelCase , return_tensors=__UpperCAmelCase , **__UpperCAmelCase ) if images is not None: a = self.image_processor(__UpperCAmelCase , return_tensors=__UpperCAmelCase , **__UpperCAmelCase ) if text is not None and images is not None: a = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**__UpperCAmelCase ) , tensor_type=__UpperCAmelCase ) def __lowerCAmelCase ( self : Tuple , *__UpperCAmelCase : List[str] , **__UpperCAmelCase : Union[str, Any] ) ->Union[str, Any]: """simple docstring""" return self.tokenizer.batch_decode(*__UpperCAmelCase , **__UpperCAmelCase ) def __lowerCAmelCase ( self : Tuple , *__UpperCAmelCase : str , **__UpperCAmelCase : Tuple ) ->Any: """simple docstring""" return self.tokenizer.decode(*__UpperCAmelCase , **__UpperCAmelCase ) @property def __lowerCAmelCase ( self : int ) ->List[str]: """simple docstring""" a = self.tokenizer.model_input_names a = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def __lowerCAmelCase ( self : Optional[int] ) ->List[str]: """simple docstring""" warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , __UpperCAmelCase , ) return self.image_processor_class @property def __lowerCAmelCase ( self : Union[str, Any] ) ->Optional[int]: """simple docstring""" warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , __UpperCAmelCase , ) return self.image_processor
26
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_distilbert import DistilBertTokenizer UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} UpperCAmelCase__ = { "vocab_file": { "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/vocab.txt", "distilbert-base-uncased-distilled-squad": ( "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/vocab.txt" ), "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/vocab.txt", "distilbert-base-cased-distilled-squad": ( "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/vocab.txt" ), "distilbert-base-german-cased": "https://huggingface.co/distilbert-base-german-cased/resolve/main/vocab.txt", "distilbert-base-multilingual-cased": ( "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/vocab.txt" ), }, "tokenizer_file": { "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/tokenizer.json", "distilbert-base-uncased-distilled-squad": ( "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/tokenizer.json" ), "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/tokenizer.json", "distilbert-base-cased-distilled-squad": ( "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/tokenizer.json" ), "distilbert-base-german-cased": ( "https://huggingface.co/distilbert-base-german-cased/resolve/main/tokenizer.json" ), "distilbert-base-multilingual-cased": ( "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/tokenizer.json" ), }, } UpperCAmelCase__ = { "distilbert-base-uncased": 512, "distilbert-base-uncased-distilled-squad": 512, "distilbert-base-cased": 512, "distilbert-base-cased-distilled-squad": 512, "distilbert-base-german-cased": 512, "distilbert-base-multilingual-cased": 512, } UpperCAmelCase__ = { "distilbert-base-uncased": {"do_lower_case": True}, "distilbert-base-uncased-distilled-squad": {"do_lower_case": True}, "distilbert-base-cased": {"do_lower_case": False}, "distilbert-base-cased-distilled-squad": {"do_lower_case": False}, "distilbert-base-german-cased": {"do_lower_case": False}, "distilbert-base-multilingual-cased": {"do_lower_case": False}, } class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = VOCAB_FILES_NAMES __snake_case = PRETRAINED_VOCAB_FILES_MAP __snake_case = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __snake_case = PRETRAINED_INIT_CONFIGURATION __snake_case = ['''input_ids''', '''attention_mask'''] __snake_case = DistilBertTokenizer def __init__( self : Dict , __UpperCAmelCase : List[Any]=None , __UpperCAmelCase : Optional[int]=None , __UpperCAmelCase : Optional[int]=True , __UpperCAmelCase : Optional[int]="[UNK]" , __UpperCAmelCase : str="[SEP]" , __UpperCAmelCase : Tuple="[PAD]" , __UpperCAmelCase : Any="[CLS]" , __UpperCAmelCase : int="[MASK]" , __UpperCAmelCase : Optional[int]=True , __UpperCAmelCase : Optional[Any]=None , **__UpperCAmelCase : str , ) ->Optional[int]: """simple docstring""" super().__init__( __UpperCAmelCase , tokenizer_file=__UpperCAmelCase , do_lower_case=__UpperCAmelCase , unk_token=__UpperCAmelCase , sep_token=__UpperCAmelCase , pad_token=__UpperCAmelCase , cls_token=__UpperCAmelCase , mask_token=__UpperCAmelCase , tokenize_chinese_chars=__UpperCAmelCase , strip_accents=__UpperCAmelCase , **__UpperCAmelCase , ) a = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('''lowercase''' , __UpperCAmelCase ) != do_lower_case or normalizer_state.get('''strip_accents''' , __UpperCAmelCase ) != strip_accents or normalizer_state.get('''handle_chinese_chars''' , __UpperCAmelCase ) != tokenize_chinese_chars ): a = getattr(__UpperCAmelCase , normalizer_state.pop('''type''' ) ) a = do_lower_case a = strip_accents a = tokenize_chinese_chars a = normalizer_class(**__UpperCAmelCase ) a = do_lower_case def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Optional[int]=None ) ->Optional[Any]: """simple docstring""" a = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None ) ->List[int]: """simple docstring""" a = [self.sep_token_id] a = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : str , __UpperCAmelCase : Optional[str] = None ) ->Tuple[str]: """simple docstring""" a = self._tokenizer.model.save(__UpperCAmelCase , name=__UpperCAmelCase ) return tuple(__UpperCAmelCase )
26
1
'''simple docstring''' import copy import inspect import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers import VideoMAEConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VideoMAEForPreTraining, VideoMAEForVideoClassification, VideoMAEModel, ) from transformers.models.videomae.modeling_videomae import VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from transformers import VideoMAEImageProcessor class lowercase_ : """simple docstring""" def __init__( self : str ,lowercase__ : int ,lowercase__ : List[str]=1_3 ,lowercase__ : List[str]=1_0 ,lowercase__ : int=3 ,lowercase__ : Tuple=2 ,lowercase__ : Union[str, Any]=2 ,lowercase__ : List[str]=2 ,lowercase__ : List[Any]=True ,lowercase__ : Any=True ,lowercase__ : Optional[int]=3_2 ,lowercase__ : List[str]=5 ,lowercase__ : Tuple=4 ,lowercase__ : str=3_7 ,lowercase__ : List[Any]="gelu" ,lowercase__ : Dict=0.1 ,lowercase__ : Any=0.1 ,lowercase__ : str=1_0 ,lowercase__ : Any=0.0_2 ,lowercase__ : Tuple=0.9 ,lowercase__ : Tuple=None ,): __lowercase = parent __lowercase = batch_size __lowercase = image_size __lowercase = num_channels __lowercase = patch_size __lowercase = tubelet_size __lowercase = num_frames __lowercase = is_training __lowercase = use_labels __lowercase = hidden_size __lowercase = num_hidden_layers __lowercase = num_attention_heads __lowercase = intermediate_size __lowercase = hidden_act __lowercase = hidden_dropout_prob __lowercase = attention_probs_dropout_prob __lowercase = type_sequence_label_size __lowercase = initializer_range __lowercase = mask_ratio __lowercase = scope # in VideoMAE, the number of tokens equals num_frames/tubelet_size * num_patches per frame __lowercase = (image_size // patch_size) ** 2 __lowercase = (num_frames // tubelet_size) * self.num_patches_per_frame # use this variable to define bool_masked_pos __lowercase = int(mask_ratio * self.seq_length ) def SCREAMING_SNAKE_CASE ( self : Any ): __lowercase = floats_tensor( [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size] ) __lowercase = None if self.use_labels: __lowercase = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) __lowercase = self.get_config() return config, pixel_values, labels def SCREAMING_SNAKE_CASE ( self : List[str] ): return VideoMAEConfig( image_size=self.image_size ,patch_size=self.patch_size ,num_channels=self.num_channels ,num_frames=self.num_frames ,tubelet_size=self.tubelet_size ,hidden_size=self.hidden_size ,num_hidden_layers=self.num_hidden_layers ,num_attention_heads=self.num_attention_heads ,intermediate_size=self.intermediate_size ,hidden_act=self.hidden_act ,hidden_dropout_prob=self.hidden_dropout_prob ,attention_probs_dropout_prob=self.attention_probs_dropout_prob ,is_decoder=lowercase__ ,initializer_range=self.initializer_range ,) def SCREAMING_SNAKE_CASE ( self : int ,lowercase__ : int ,lowercase__ : Any ,lowercase__ : Optional[Any] ): __lowercase = VideoMAEModel(config=lowercase__ ) model.to(lowercase__ ) model.eval() __lowercase = model(lowercase__ ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE ( self : Dict ,lowercase__ : Optional[Any] ,lowercase__ : Any ,lowercase__ : Any ): __lowercase = VideoMAEForPreTraining(lowercase__ ) model.to(lowercase__ ) model.eval() # important: each video needs to have the same number of masked patches # hence we define a single mask, which we then repeat for each example in the batch __lowercase = torch.ones((self.num_masks,) ) __lowercase = torch.cat([mask, torch.zeros(self.seq_length - mask.size(0 ) )] ) __lowercase = mask.expand(self.batch_size ,-1 ).bool() __lowercase = model(lowercase__ ,lowercase__ ) # model only returns predictions for masked patches __lowercase = mask.sum().item() __lowercase = 3 * self.tubelet_size * self.patch_size**2 self.parent.assertEqual(result.logits.shape ,(self.batch_size, num_masked_patches, decoder_num_labels) ) def SCREAMING_SNAKE_CASE ( self : Tuple ): __lowercase = self.prepare_config_and_inputs() __lowercase , __lowercase , __lowercase = config_and_inputs __lowercase = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class lowercase_ (lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): """simple docstring""" SCREAMING_SNAKE_CASE : Optional[Any] = ( (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else () ) SCREAMING_SNAKE_CASE : int = ( {'feature-extraction': VideoMAEModel, 'video-classification': VideoMAEForVideoClassification} if is_torch_available() else {} ) SCREAMING_SNAKE_CASE : str = False SCREAMING_SNAKE_CASE : Any = False SCREAMING_SNAKE_CASE : str = False SCREAMING_SNAKE_CASE : Union[str, Any] = False def SCREAMING_SNAKE_CASE ( self : Any ): __lowercase = VideoMAEModelTester(self ) __lowercase = ConfigTester(self ,config_class=lowercase__ ,has_text_modality=lowercase__ ,hidden_size=3_7 ) def SCREAMING_SNAKE_CASE ( self : int ,lowercase__ : Tuple ,lowercase__ : List[str] ,lowercase__ : List[str]=False ): __lowercase = copy.deepcopy(lowercase__ ) if model_class == VideoMAEForPreTraining: # important: each video needs to have the same number of masked patches # hence we define a single mask, which we then repeat for each example in the batch __lowercase = torch.ones((self.model_tester.num_masks,) ) __lowercase = torch.cat([mask, torch.zeros(self.model_tester.seq_length - mask.size(0 ) )] ) __lowercase = mask.expand(self.model_tester.batch_size ,-1 ).bool() __lowercase = bool_masked_pos.to(lowercase__ ) if return_labels: if model_class in [ *get_values(lowercase__ ), ]: __lowercase = torch.zeros( self.model_tester.batch_size ,dtype=torch.long ,device=lowercase__ ) return inputs_dict def SCREAMING_SNAKE_CASE ( self : str ): self.config_tester.run_common_tests() @unittest.skip(reason='''VideoMAE does not use inputs_embeds''' ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ): pass def SCREAMING_SNAKE_CASE ( self : str ): __lowercase , __lowercase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __lowercase = model_class(lowercase__ ) self.assertIsInstance(model.get_input_embeddings() ,(nn.Module) ) __lowercase = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowercase__ ,nn.Linear ) ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ): __lowercase , __lowercase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __lowercase = model_class(lowercase__ ) __lowercase = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic __lowercase = [*signature.parameters.keys()] __lowercase = ['''pixel_values'''] self.assertListEqual(arg_names[:1] ,lowercase__ ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ): __lowercase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowercase__ ) def SCREAMING_SNAKE_CASE ( self : str ): __lowercase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*lowercase__ ) @slow def SCREAMING_SNAKE_CASE ( self : Any ): for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __lowercase = VideoMAEModel.from_pretrained(lowercase__ ) self.assertIsNotNone(lowercase__ ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ): if not self.has_attentions: pass else: __lowercase , __lowercase = self.model_tester.prepare_config_and_inputs_for_common() __lowercase = True for model_class in self.all_model_classes: __lowercase = self.model_tester.seq_length - self.model_tester.num_masks __lowercase = ( num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length ) __lowercase = True __lowercase = False __lowercase = True __lowercase = model_class(lowercase__ ) model.to(lowercase__ ) model.eval() with torch.no_grad(): __lowercase = model(**self._prepare_for_class(lowercase__ ,lowercase__ ) ) __lowercase = outputs.attentions self.assertEqual(len(lowercase__ ) ,self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] __lowercase = True __lowercase = model_class(lowercase__ ) model.to(lowercase__ ) model.eval() with torch.no_grad(): __lowercase = model(**self._prepare_for_class(lowercase__ ,lowercase__ ) ) __lowercase = outputs.attentions self.assertEqual(len(lowercase__ ) ,self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) ,[self.model_tester.num_attention_heads, seq_len, seq_len] ,) __lowercase = len(lowercase__ ) # Check attention is always last and order is fine __lowercase = True __lowercase = True __lowercase = model_class(lowercase__ ) model.to(lowercase__ ) model.eval() with torch.no_grad(): __lowercase = model(**self._prepare_for_class(lowercase__ ,lowercase__ ) ) self.assertEqual(out_len + 1 ,len(lowercase__ ) ) __lowercase = outputs.attentions self.assertEqual(len(lowercase__ ) ,self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) ,[self.model_tester.num_attention_heads, seq_len, seq_len] ,) def SCREAMING_SNAKE_CASE ( self : Dict ): def check_hidden_states_output(lowercase__ : List[str] ,lowercase__ : int ,lowercase__ : int ): __lowercase = model_class(lowercase__ ) model.to(lowercase__ ) model.eval() with torch.no_grad(): __lowercase = model(**self._prepare_for_class(lowercase__ ,lowercase__ ) ) __lowercase = outputs.hidden_states __lowercase = self.model_tester.num_hidden_layers + 1 self.assertEqual(len(lowercase__ ) ,lowercase__ ) __lowercase = self.model_tester.seq_length - self.model_tester.num_masks __lowercase = num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length self.assertListEqual( list(hidden_states[0].shape[-2:] ) ,[seq_length, self.model_tester.hidden_size] ,) __lowercase , __lowercase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __lowercase = True check_hidden_states_output(lowercase__ ,lowercase__ ,lowercase__ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] __lowercase = True check_hidden_states_output(lowercase__ ,lowercase__ ,lowercase__ ) @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def SCREAMING_SNAKE_CASE ( self : Tuple ): pass def _A ( ): """simple docstring""" __lowercase = hf_hub_download( repo_id='''hf-internal-testing/spaghetti-video''' , filename='''eating_spaghetti.npy''' , repo_type='''dataset''' ) __lowercase = np.load(A__ ) return list(A__ ) @require_torch @require_vision class lowercase_ (unittest.TestCase ): """simple docstring""" @cached_property def SCREAMING_SNAKE_CASE ( self : List[str] ): # logits were tested with a different mean and std, so we use the same here return ( VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5] ,image_std=[0.5, 0.5, 0.5] ) if is_vision_available() else None ) @slow def SCREAMING_SNAKE_CASE ( self : Optional[Any] ): __lowercase = VideoMAEForVideoClassification.from_pretrained('''MCG-NJU/videomae-base-finetuned-kinetics''' ).to( lowercase__ ) __lowercase = self.default_image_processor __lowercase = prepare_video() __lowercase = image_processor(lowercase__ ,return_tensors='''pt''' ).to(lowercase__ ) # forward pass with torch.no_grad(): __lowercase = model(**lowercase__ ) # verify the logits __lowercase = torch.Size((1, 4_0_0) ) self.assertEqual(outputs.logits.shape ,lowercase__ ) __lowercase = torch.tensor([0.3_6_6_9, -0.0_6_8_8, -0.2_4_2_1] ).to(lowercase__ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] ,lowercase__ ,atol=1e-4 ) ) @slow def SCREAMING_SNAKE_CASE ( self : Tuple ): __lowercase = VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short''' ).to(lowercase__ ) __lowercase = self.default_image_processor __lowercase = prepare_video() __lowercase = image_processor(lowercase__ ,return_tensors='''pt''' ).to(lowercase__ ) # add boolean mask, indicating which patches to mask __lowercase = hf_hub_download(repo_id='''hf-internal-testing/bool-masked-pos''' ,filename='''bool_masked_pos.pt''' ) __lowercase = torch.load(lowercase__ ) # forward pass with torch.no_grad(): __lowercase = model(**lowercase__ ) # verify the logits __lowercase = torch.Size([1, 1_4_0_8, 1_5_3_6] ) __lowercase = torch.tensor( [[0.7_9_9_4, 0.9_6_1_2, 0.8_5_0_8], [0.7_4_0_1, 0.8_9_5_8, 0.8_3_0_2], [0.5_8_6_2, 0.7_4_6_8, 0.7_3_2_5]] ,device=lowercase__ ) self.assertEqual(outputs.logits.shape ,lowercase__ ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] ,lowercase__ ,atol=1e-4 ) ) # verify the loss (`config.norm_pix_loss` = `True`) __lowercase = torch.tensor([0.5_1_4_2] ,device=lowercase__ ) self.assertTrue(torch.allclose(outputs.loss ,lowercase__ ,atol=1e-4 ) ) # verify the loss (`config.norm_pix_loss` = `False`) __lowercase = VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short''' ,norm_pix_loss=lowercase__ ).to( lowercase__ ) with torch.no_grad(): __lowercase = model(**lowercase__ ) __lowercase = torch.tensor(torch.tensor([0.6_4_6_9] ) ,device=lowercase__ ) self.assertTrue(torch.allclose(outputs.loss ,lowercase__ ,atol=1e-4 ) )
104
'''simple docstring''' import numpy as np # Importing the Keras libraries and packages import tensorflow as tf from tensorflow.keras import layers, models if __name__ == "__main__": # Initialising the CNN # (Sequential- Building the model layer by layer) lowerCamelCase : Any = models.Sequential() # Step 1 - Convolution # Here 64,64 is the length & breadth of dataset images and 3 is for the RGB channel # (3,3) is the kernel size (filter matrix) classifier.add( layers.ConvaD(32, (3, 3), input_shape=(64, 64, 3), activation='relu') ) # Step 2 - Pooling classifier.add(layers.MaxPoolingaD(pool_size=(2, 2))) # Adding a second convolutional layer classifier.add(layers.ConvaD(32, (3, 3), activation='relu')) classifier.add(layers.MaxPoolingaD(pool_size=(2, 2))) # Step 3 - Flattening classifier.add(layers.Flatten()) # Step 4 - Full connection classifier.add(layers.Dense(units=128, activation='relu')) classifier.add(layers.Dense(units=1, activation='sigmoid')) # Compiling the CNN classifier.compile( optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'] ) # Part 2 - Fitting the CNN to the images # Load Trained model weights # from keras.models import load_model # regressor=load_model('cnn.h5') lowerCamelCase : Optional[Any] = tf.keras.preprocessing.image.ImageDataGenerator( rescale=1.0 / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True ) lowerCamelCase : Any = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1.0 / 255) lowerCamelCase : List[Any] = train_datagen.flow_from_directory( 'dataset/training_set', target_size=(64, 64), batch_size=32, class_mode='binary' ) lowerCamelCase : List[str] = test_datagen.flow_from_directory( 'dataset/test_set', target_size=(64, 64), batch_size=32, class_mode='binary' ) classifier.fit_generator( training_set, steps_per_epoch=5, epochs=30, validation_data=test_set ) classifier.save('cnn.h5') # Part 3 - Making new predictions lowerCamelCase : List[str] = tf.keras.preprocessing.image.load_img( 'dataset/single_prediction/image.png', target_size=(64, 64) ) lowerCamelCase : Optional[int] = tf.keras.preprocessing.image.img_to_array(test_image) lowerCamelCase : str = np.expand_dims(test_image, axis=0) lowerCamelCase : List[str] = classifier.predict(test_image) # training_set.class_indices if result[0][0] == 0: lowerCamelCase : Any = 'Normal' if result[0][0] == 1: lowerCamelCase : Any = 'Abnormality detected'
2
0
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( SwiftFormerConfig, SwiftFormerForImageClassification, ViTImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() lowercase_ = logging.get_logger(__name__) lowercase_ = torch.device('cpu') def UpperCamelCase__ ( ): __lowerCamelCase : List[str] = 'http://images.cocodataset.org/val2017/000000039769.jpg' __lowerCamelCase : str = Image.open(requests.get(SCREAMING_SNAKE_CASE__ , stream=SCREAMING_SNAKE_CASE__ ).raw ) return im def UpperCamelCase__ ( SCREAMING_SNAKE_CASE__ ): if swiftformer_name == "swiftformer_xs": return torch.tensor([-2.1_703e00, 2.1_107e00, -2.0_811e00, 8.8_685e-01, 2.4_360e-01] ) elif swiftformer_name == "swiftformer_s": return torch.tensor([3.9_636e-01, 2.3_478e-01, -1.6_963e00, -1.7_381e00, -8.6_337e-01] ) elif swiftformer_name == "swiftformer_l1": return torch.tensor([-4.2_768e-01, -4.7_429e-01, -1.0_897e00, -1.0_248e00, 3.5_523e-02] ) elif swiftformer_name == "swiftformer_l3": return torch.tensor([-2.5_330e-01, 2.4_211e-01, -6.0_185e-01, -8.2_789e-01, -6.0_446e-02] ) def UpperCamelCase__ ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): __lowerCamelCase : Optional[Any] = dct.pop(SCREAMING_SNAKE_CASE__ ) __lowerCamelCase : str = val def UpperCamelCase__ ( SCREAMING_SNAKE_CASE__ ): __lowerCamelCase : Union[str, Any] = [] for k in state_dict.keys(): __lowerCamelCase : Optional[Any] = k if ".pwconv" in k: __lowerCamelCase : Optional[int] = k_new.replace('.pwconv' , '.point_wise_conv' ) if ".dwconv" in k: __lowerCamelCase : int = k_new.replace('.dwconv' , '.depth_wise_conv' ) if ".Proj." in k: __lowerCamelCase : Tuple = k_new.replace('.Proj.' , '.proj.' ) if "patch_embed" in k_new: __lowerCamelCase : Tuple = k_new.replace('patch_embed' , 'swiftformer.patch_embed.patch_embedding' ) if "network" in k_new: __lowerCamelCase : Optional[int] = k_new.split('.' ) if ls[2].isdigit(): __lowerCamelCase : List[str] = 'swiftformer.encoder.network.' + ls[1] + '.blocks.' + ls[2] + '.' + '.'.join(ls[3:] ) else: __lowerCamelCase : List[str] = k_new.replace('network' , 'swiftformer.encoder.network' ) rename_keys.append((k, k_new) ) return rename_keys @torch.no_grad() def UpperCamelCase__ ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): __lowerCamelCase : Tuple = SwiftFormerConfig() # dataset (ImageNet-21k only or also fine-tuned on ImageNet 2012), patch_size and image_size __lowerCamelCase : List[str] = 1_000 __lowerCamelCase : Optional[Any] = 'huggingface/label-files' __lowerCamelCase : str = 'imagenet-1k-id2label.json' __lowerCamelCase : Union[str, Any] = json.load(open(hf_hub_download(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , repo_type='dataset' ) , 'r' ) ) __lowerCamelCase : Union[str, Any] = {int(SCREAMING_SNAKE_CASE__ ): v for k, v in idalabel.items()} __lowerCamelCase : int = idalabel __lowerCamelCase : int = {v: k for k, v in idalabel.items()} # size of the architecture if swiftformer_name == "swiftformer_xs": __lowerCamelCase : int = [3, 3, 6, 4] __lowerCamelCase : str = [48, 56, 112, 220] elif swiftformer_name == "swiftformer_s": __lowerCamelCase : Any = [3, 3, 9, 6] __lowerCamelCase : str = [48, 64, 168, 224] elif swiftformer_name == "swiftformer_l1": __lowerCamelCase : int = [4, 3, 10, 5] __lowerCamelCase : Any = [48, 96, 192, 384] elif swiftformer_name == "swiftformer_l3": __lowerCamelCase : Union[str, Any] = [4, 4, 12, 6] __lowerCamelCase : List[str] = [64, 128, 320, 512] # load state_dict of original model, remove and rename some keys if original_ckpt: if original_ckpt.startswith('https' ): __lowerCamelCase : Any = torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE__ , map_location='cpu' , check_hash=SCREAMING_SNAKE_CASE__ ) else: __lowerCamelCase : str = torch.load(SCREAMING_SNAKE_CASE__ , map_location='cpu' ) __lowerCamelCase : Tuple = checkpoint __lowerCamelCase : List[str] = create_rename_keys(SCREAMING_SNAKE_CASE__ ) for rename_key_src, rename_key_dest in rename_keys: rename_key(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # load HuggingFace model __lowerCamelCase : Union[str, Any] = SwiftFormerForImageClassification(SCREAMING_SNAKE_CASE__ ).eval() hf_model.load_state_dict(SCREAMING_SNAKE_CASE__ ) # prepare test inputs __lowerCamelCase : Optional[Any] = prepare_img() __lowerCamelCase : Optional[int] = ViTImageProcessor.from_pretrained('preprocessor_config' ) __lowerCamelCase : Optional[Any] = processor(images=SCREAMING_SNAKE_CASE__ , return_tensors='pt' ) # compare outputs from both models __lowerCamelCase : Tuple = get_expected_output(SCREAMING_SNAKE_CASE__ ) __lowerCamelCase : int = hf_model(inputs['pixel_values'] ).logits assert hf_logits.shape == torch.Size([1, 1_000] ) assert torch.allclose(hf_logits[0, 0:5] , SCREAMING_SNAKE_CASE__ , atol=1e-3 ) Path(SCREAMING_SNAKE_CASE__ ).mkdir(exist_ok=SCREAMING_SNAKE_CASE__ ) print(f'Saving model {swiftformer_name} to {pytorch_dump_folder_path}' ) hf_model.save_pretrained(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( '--swiftformer_name', default='swiftformer_xs', choices=['swiftformer_xs', 'swiftformer_s', 'swiftformer_l1', 'swiftformer_l3'], type=str, help='Name of the SwiftFormer model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default='./converted_outputs/', type=str, help='Path to the output PyTorch model directory.', ) parser.add_argument('--original_ckpt', default=None, type=str, help='Path to the original model checkpoint.') lowercase_ = parser.parse_args() convert_swiftformer_checkpoint(args.swiftformer_name, args.pytorch_dump_folder_path, args.original_ckpt)
194
import darl # noqa import gym import tqdm from diffusers.experimental import ValueGuidedRLPipeline lowercase_ = { 'n_samples': 6_4, 'horizon': 3_2, 'num_inference_steps': 2_0, 'n_guide_steps': 2, # can set to 0 for faster sampling, does not use value network 'scale_grad_by_std': True, 'scale': 0.1, 'eta': 0.0, 't_grad_cutoff': 2, 'device': 'cpu', } if __name__ == "__main__": lowercase_ = 'hopper-medium-v2' lowercase_ = gym.make(env_name) lowercase_ = ValueGuidedRLPipeline.from_pretrained( 'bglick13/hopper-medium-v2-value-function-hor32', env=env, ) env.seed(0) lowercase_ = env.reset() lowercase_ = 0 lowercase_ = 0 lowercase_ = 1_0_0_0 lowercase_ = [obs.copy()] try: for t in tqdm.tqdm(range(T)): # call the policy lowercase_ = pipeline(obs, planning_horizon=3_2) # execute action in environment lowercase_ ,lowercase_ ,lowercase_ ,lowercase_ = env.step(denorm_actions) lowercase_ = env.get_normalized_score(total_reward) # update return total_reward += reward total_score += score print( F"""Step: {t}, Reward: {reward}, Total Reward: {total_reward}, Score: {score}, Total Score:""" F""" {total_score}""" ) # save observations for rendering rollout.append(next_observation.copy()) lowercase_ = next_observation except KeyboardInterrupt: pass print(F"""Total reward: {total_reward}""")
194
1
def UpperCAmelCase_ ( __lowerCAmelCase ) -> int: if not isinstance(__lowerCAmelCase , __lowerCAmelCase ): __lowercase : Optional[int] = F'Input value of [number={number}] must be an integer' raise TypeError(__lowerCAmelCase ) if number < 1: __lowercase : List[str] = F'Input value of [number={number}] must be > 0' raise ValueError(__lowerCAmelCase ) __lowercase : List[Any] = 1 for i in range(1 , __lowerCAmelCase ): current_number *= 4 * i - 2 current_number //= i + 1 return current_number if __name__ == "__main__": import doctest doctest.testmod()
156
from __future__ import annotations from collections.abc import Iterable, Iterator from dataclasses import dataclass __lowerCAmelCase : Any = (3, 9, -11, 0, 7, 5, 1, -1) __lowerCAmelCase : Tuple = (4, 6, 2, 0, 8, 10, 3, -2) @dataclass class __lowerCAmelCase : """simple docstring""" A__ : int A__ : Node | None class __lowerCAmelCase : """simple docstring""" def __init__( self : Optional[Any] , _snake_case : Iterable[int] ): __lowercase : Node | None = None for i in sorted(_snake_case , reverse=_snake_case ): __lowercase : List[Any] = Node(_snake_case , self.head ) def __iter__( self : str ): __lowercase : Union[str, Any] = self.head while node: yield node.data __lowercase : List[Any] = node.next_node def __len__( self : str ): return sum(1 for _ in self ) def __str__( self : List[str] ): return " -> ".join([str(_snake_case ) for node in self] ) def UpperCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase ) -> SortedLinkedList: return SortedLinkedList(list(__lowerCAmelCase ) + list(__lowerCAmelCase ) ) if __name__ == "__main__": import doctest doctest.testmod() __lowerCAmelCase : Dict = SortedLinkedList print(merge_lists(SSL(test_data_odd), SSL(test_data_even)))
156
1
'''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__) class _UpperCAmelCase ( snake_case_ , snake_case_ ): """simple docstring""" snake_case = '''maskformer-swin''' snake_case = { '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers''', } def __init__( self : Any , __UpperCAmelCase : Dict=224 , __UpperCAmelCase : List[Any]=4 , __UpperCAmelCase : Dict=3 , __UpperCAmelCase : Optional[Any]=96 , __UpperCAmelCase : Any=[2, 2, 6, 2] , __UpperCAmelCase : List[str]=[3, 6, 12, 24] , __UpperCAmelCase : List[Any]=7 , __UpperCAmelCase : List[Any]=4.0 , __UpperCAmelCase : List[Any]=True , __UpperCAmelCase : Tuple=0.0 , __UpperCAmelCase : Optional[int]=0.0 , __UpperCAmelCase : Any=0.1 , __UpperCAmelCase : Optional[int]="gelu" , __UpperCAmelCase : Optional[Any]=False , __UpperCAmelCase : int=0.02 , __UpperCAmelCase : List[str]=1E-5 , __UpperCAmelCase : str=None , __UpperCAmelCase : Any=None , **__UpperCAmelCase : Dict , ): '''simple docstring''' super().__init__(**__UpperCAmelCase ) _A = image_size _A = patch_size _A = num_channels _A = embed_dim _A = depths _A = len(__UpperCAmelCase ) _A = num_heads _A = window_size _A = mlp_ratio _A = qkv_bias _A = hidden_dropout_prob _A = attention_probs_dropout_prob _A = drop_path_rate _A = hidden_act _A = use_absolute_embeddings _A = layer_norm_eps _A = initializer_range # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model _A = int(embed_dim * 2 ** (len(__UpperCAmelCase ) - 1) ) _A = ["stem"] + [f'''stage{idx}''' for idx in range(1 , len(__UpperCAmelCase ) + 1 )] _A , _A = get_aligned_output_features_output_indices( out_features=__UpperCAmelCase , out_indices=__UpperCAmelCase , stage_names=self.stage_names )
366
'''simple docstring''' from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCamelCase_ = logging.get_logger(__name__) lowerCamelCase_ = { '''facebook/levit-128S''': '''https://huggingface.co/facebook/levit-128S/resolve/main/config.json''', # See all LeViT models at https://huggingface.co/models?filter=levit } class _UpperCAmelCase ( snake_case_ ): """simple docstring""" snake_case = '''levit''' def __init__( self : str , __UpperCAmelCase : int=224 , __UpperCAmelCase : Optional[Any]=3 , __UpperCAmelCase : Union[str, Any]=3 , __UpperCAmelCase : Optional[int]=2 , __UpperCAmelCase : Any=1 , __UpperCAmelCase : int=16 , __UpperCAmelCase : Any=[128, 256, 384] , __UpperCAmelCase : Optional[Any]=[4, 8, 12] , __UpperCAmelCase : Dict=[4, 4, 4] , __UpperCAmelCase : Union[str, Any]=[16, 16, 16] , __UpperCAmelCase : List[Any]=0 , __UpperCAmelCase : str=[2, 2, 2] , __UpperCAmelCase : Optional[Any]=[2, 2, 2] , __UpperCAmelCase : int=0.02 , **__UpperCAmelCase : Dict , ): '''simple docstring''' super().__init__(**__UpperCAmelCase ) _A = image_size _A = num_channels _A = kernel_size _A = stride _A = padding _A = hidden_sizes _A = num_attention_heads _A = depths _A = key_dim _A = drop_path_rate _A = patch_size _A = attention_ratio _A = mlp_ratio _A = initializer_range _A = [ ["Subsample", key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2], ["Subsample", key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2], ] class _UpperCAmelCase ( snake_case_ ): """simple docstring""" snake_case = version.parse('''1.11''' ) @property def lowerCAmelCase ( self : Union[str, Any] ): '''simple docstring''' return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def lowerCAmelCase ( self : Any ): '''simple docstring''' return 1E-4
174
0
'''simple docstring''' from math import pow, sqrt def __snake_case ( *UpperCAmelCase_ : Tuple ): lowerCamelCase_ = len(_SCREAMING_SNAKE_CASE ) > 0 and all(value > 0.0 for value in values ) return result def __snake_case ( UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Union[str, Any] ): return ( round(sqrt(molar_mass_a / molar_mass_a ) , 6 ) if validate(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else ValueError("Input Error: Molar mass values must greater than 0." ) ) def __snake_case ( UpperCAmelCase_ : Any , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Optional[Any] ): return ( round(effusion_rate * sqrt(molar_mass_a / molar_mass_a ) , 6 ) if validate(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else ValueError( "Input Error: Molar mass and effusion rate values must greater than 0." ) ) def __snake_case ( UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : List[Any] ): return ( round(effusion_rate / sqrt(molar_mass_a / molar_mass_a ) , 6 ) if validate(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else ValueError( "Input Error: Molar mass and effusion rate values must greater than 0." ) ) def __snake_case ( UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : str ): return ( round(molar_mass / pow(effusion_rate_a / effusion_rate_a , 2 ) , 6 ) if validate(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else ValueError( "Input Error: Molar mass and effusion rate values must greater than 0." ) ) def __snake_case ( UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : str , UpperCAmelCase_ : Union[str, Any] ): return ( round(pow(effusion_rate_a / effusion_rate_a , 2 ) / molar_mass , 6 ) if validate(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else ValueError( "Input Error: Molar mass and effusion rate values must greater than 0." ) )
55
"""simple docstring""" import os from typing import Dict, List, Tuple, TypeVar, Union lowercase__ = TypeVar('T') lowercase__ = Union[List[T], Tuple[T, ...]] lowercase__ = Union[T, List[T], Dict[str, T]] lowercase__ = Union[str, bytes, os.PathLike]
290
0
"""simple docstring""" import argparse from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import BigBirdPegasusConfig, BigBirdPegasusForConditionalGeneration __snake_case = [ # tf -> hf ("""/""", """."""), ("""layer_""", """layers."""), ("""kernel""", """weight"""), ("""beta""", """bias"""), ("""gamma""", """weight"""), ("""pegasus""", """model"""), ] __snake_case = [ (""".output.dense""", """.fc2"""), ("""intermediate.LayerNorm""", """final_layer_norm"""), ("""intermediate.dense""", """fc1"""), ] __snake_case = ( INIT_COMMON + [ ("""attention.self.LayerNorm""", """self_attn_layer_norm"""), ("""attention.output.dense""", """self_attn.out_proj"""), ("""attention.self""", """self_attn"""), ("""attention.encdec.LayerNorm""", """encoder_attn_layer_norm"""), ("""attention.encdec_output.dense""", """encoder_attn.out_proj"""), ("""attention.encdec""", """encoder_attn"""), ("""key""", """k_proj"""), ("""value""", """v_proj"""), ("""query""", """q_proj"""), ("""decoder.LayerNorm""", """decoder.layernorm_embedding"""), ] + END_COMMON ) __snake_case = ( INIT_COMMON + [ ("""embeddings.word_embeddings""", """shared.weight"""), ("""embeddings.position_embeddings""", """embed_positions.weight"""), ("""attention.self.LayerNorm""", """self_attn_layer_norm"""), ("""attention.output.dense""", """self_attn.output"""), ("""attention.self""", """self_attn.self"""), ("""encoder.LayerNorm""", """encoder.layernorm_embedding"""), ] + END_COMMON ) __snake_case = [ """encdec/key/bias""", """encdec/query/bias""", """encdec/value/bias""", """self/key/bias""", """self/query/bias""", """self/value/bias""", """encdec_output/dense/bias""", """attention/output/dense/bias""", ] def __lowerCAmelCase ( lowercase : int , lowercase : int ) -> Optional[Any]: """simple docstring""" for tf_name, hf_name in patterns: snake_case : int = k.replace(lowercase , lowercase ) return k def __lowerCAmelCase ( lowercase : dict , lowercase : dict ) -> BigBirdPegasusForConditionalGeneration: """simple docstring""" snake_case : Tuple = BigBirdPegasusConfig(**lowercase ) snake_case : Union[str, Any] = BigBirdPegasusForConditionalGeneration(lowercase ) snake_case : Optional[int] = torch_model.state_dict() snake_case : Optional[int] = {} # separating decoder weights snake_case : str = {k: tf_weights[k] for k in tf_weights if k.startswith("pegasus/decoder" )} snake_case : Dict = {k: tf_weights[k] for k in tf_weights if not k.startswith("pegasus/decoder" )} for k, v in tqdm(decoder_weights.items() , "tf -> hf conversion" ): snake_case : Any = [k.endswith(lowercase ) for ending in KEYS_TO_IGNORE] if any(lowercase ): continue snake_case : Tuple = DECODER_PATTERNS snake_case : List[Any] = rename_state_dict_key(lowercase , lowercase ) if new_k not in state_dict: raise ValueError(F'could not find new key {new_k} in state dict. (converted from {k})' ) if any(True if i in k else False for i in ["dense", "query", "key", "value"] ): snake_case : List[Any] = v.T snake_case : Dict = torch.from_numpy(lowercase ) assert v.shape == state_dict[new_k].shape, F'{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}' for k, v in tqdm(remaining_weights.items() , "tf -> hf conversion" ): snake_case : Tuple = [k.endswith(lowercase ) for ending in KEYS_TO_IGNORE] if any(lowercase ): continue snake_case : Any = REMAINING_PATTERNS snake_case : Dict = rename_state_dict_key(lowercase , lowercase ) if new_k not in state_dict and k != "pegasus/embeddings/position_embeddings": raise ValueError(F'could not find new key {new_k} in state dict. (converted from {k})' ) if any(True if i in k else False for i in ["dense", "query", "key", "value"] ): snake_case : Tuple = v.T snake_case : int = torch.from_numpy(lowercase ) if k != "pegasus/embeddings/position_embeddings": assert v.shape == state_dict[new_k].shape, F'{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}' snake_case : Optional[Any] = mapping["model.embed_positions.weight"] snake_case : Any = mapping.pop("model.embed_positions.weight" ) snake_case ,snake_case : List[str] = torch_model.load_state_dict(lowercase , strict=lowercase ) snake_case : List[str] = [ k for k in missing if k not in [ "final_logits_bias", "model.encoder.embed_tokens.weight", "model.decoder.embed_tokens.weight", "lm_head.weight", ] ] assert unexpected_missing == [], F'no matches found for the following torch keys {unexpected_missing}' assert extra == [], F'no matches found for the following tf keys {extra}' return torch_model def __lowerCAmelCase ( lowercase : Optional[Any] ) -> Dict: """simple docstring""" snake_case : Dict = tf.train.list_variables(lowercase ) snake_case : Any = {} snake_case : Dict = ["global_step"] for name, shape in tqdm(lowercase , desc="converting tf checkpoint to dict" ): snake_case : Optional[int] = any(pat in name for pat in ignore_name ) if skip_key: continue snake_case : List[Any] = tf.train.load_variable(lowercase , lowercase ) snake_case : List[Any] = array return tf_weights def __lowerCAmelCase ( lowercase : str , lowercase : str , lowercase : dict ) -> Optional[int]: """simple docstring""" snake_case : Dict = get_tf_weights_as_numpy(lowercase ) snake_case : List[str] = convert_bigbird_pegasus(lowercase , lowercase ) torch_model.save_pretrained(lowercase ) if __name__ == "__main__": __snake_case = argparse.ArgumentParser() parser.add_argument("""--tf_ckpt_path""", type=str, help="""passed to tf.train.list_variables""") parser.add_argument("""--save_dir""", default=None, type=str, help="""Path to the output PyTorch model.""") __snake_case = parser.parse_args() __snake_case = {} convert_bigbird_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir, config_update=config_update)
112
"""simple docstring""" import math import sys def __lowerCAmelCase ( lowercase : int ) -> int: """simple docstring""" if number != int(lowercase ): raise ValueError("the value of input must be a natural number" ) if number < 0: raise ValueError("the value of input must not be a negative number" ) if number == 0: return 1 snake_case : Optional[Any] = [-1] * (number + 1) snake_case : str = 0 for i in range(1 , number + 1 ): snake_case : List[Any] = sys.maxsize snake_case : Union[str, Any] = int(math.sqrt(lowercase ) ) for j in range(1 , root + 1 ): snake_case : List[str] = 1 + answers[i - (j**2)] snake_case : Optional[Any] = min(lowercase , lowercase ) snake_case : Any = answer return answers[number] if __name__ == "__main__": import doctest doctest.testmod()
112
1
from dataclasses import dataclass, field from typing import Tuple from ..utils import cached_property, is_torch_available, is_torch_tpu_available, logging, requires_backends from .benchmark_args_utils import BenchmarkArguments if is_torch_available(): import torch if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm snake_case_ = logging.get_logger(__name__) @dataclass class SCREAMING_SNAKE_CASE__ ( _UpperCAmelCase ): A_ : List[str] = [ 'no_inference', 'no_cuda', 'no_tpu', 'no_speed', 'no_memory', 'no_env_print', 'no_multi_process', ] def __init__(self : List[Any] , **a__ : Dict ): """simple docstring""" for deprecated_arg in self.deprecated_args: if deprecated_arg in kwargs: __snake_case = deprecated_arg[3:] setattr(self , a__ , not kwargs.pop(a__ ) ) logger.warning( f"""{deprecated_arg} is depreciated. Please use --no_{positive_arg} or""" f""" {positive_arg}={kwargs[positive_arg]}""" ) __snake_case = kwargs.pop('''torchscript''' , self.torchscript ) __snake_case = kwargs.pop('''torch_xla_tpu_print_metrics''' , self.torch_xla_tpu_print_metrics ) __snake_case = kwargs.pop('''fp16_opt_level''' , self.fpaa_opt_level ) super().__init__(**a__ ) A_ : bool = field(default=_UpperCAmelCase , metadata={'help': 'Trace the models using torchscript'} ) A_ : bool = field(default=_UpperCAmelCase , metadata={'help': 'Print Xla/PyTorch tpu metrics'} ) A_ : str = field( default='O1' , metadata={ 'help': ( 'For fp16: Apex AMP optimization level selected in [\'O0\', \'O1\', \'O2\', and \'O3\']. ' 'See details at https://nvidia.github.io/apex/amp.html' ) } , ) @cached_property def a (self : Tuple ): """simple docstring""" requires_backends(self , ['''torch'''] ) logger.info('''PyTorch: setting up devices''' ) if not self.cuda: __snake_case = torch.device('''cpu''' ) __snake_case = 0 elif is_torch_tpu_available(): __snake_case = xm.xla_device() __snake_case = 0 else: __snake_case = torch.device('''cuda''' if torch.cuda.is_available() else '''cpu''' ) __snake_case = torch.cuda.device_count() return device, n_gpu @property def a (self : Tuple ): """simple docstring""" return is_torch_tpu_available() and self.tpu @property def a (self : Tuple ): """simple docstring""" requires_backends(self , ['''torch'''] ) # TODO(PVP): currently only single GPU is supported return torch.cuda.current_device() @property def a (self : Any ): """simple docstring""" requires_backends(self , ['''torch'''] ) return self._setup_devices[0] @property def a (self : Optional[Any] ): """simple docstring""" requires_backends(self , ['''torch'''] ) return self._setup_devices[1] @property def a (self : List[str] ): """simple docstring""" return self.n_gpu > 0
24
import sacrebleu as scb from packaging import version from sacrebleu import TER import datasets _lowerCAmelCase : List[Any] = '''\ @inproceedings{snover-etal-2006-study, title = "A Study of Translation Edit Rate with Targeted Human Annotation", author = "Snover, Matthew and Dorr, Bonnie and Schwartz, Rich and Micciulla, Linnea and Makhoul, John", booktitle = "Proceedings of the 7th Conference of the Association for Machine Translation in the Americas: Technical Papers", month = aug # " 8-12", year = "2006", address = "Cambridge, Massachusetts, USA", publisher = "Association for Machine Translation in the Americas", url = "https://aclanthology.org/2006.amta-papers.25", pages = "223--231", } @inproceedings{post-2018-call, title = "A Call for Clarity in Reporting {BLEU} Scores", author = "Post, Matt", booktitle = "Proceedings of the Third Conference on Machine Translation: Research Papers", month = oct, year = "2018", address = "Belgium, Brussels", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/W18-6319", pages = "186--191", } ''' _lowerCAmelCase : Union[str, Any] = '''\ TER (Translation Edit Rate, also called Translation Error Rate) is a metric to quantify the edit operations that a hypothesis requires to match a reference translation. We use the implementation that is already present in sacrebleu (https://github.com/mjpost/sacreBLEU#ter), which in turn is inspired by the TERCOM implementation, which can be found here: https://github.com/jhclark/tercom. The implementation here is slightly different from sacrebleu in terms of the required input format. The length of the references and hypotheses lists need to be the same, so you may need to transpose your references compared to sacrebleu\'s required input format. See https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534 See the README.md file at https://github.com/mjpost/sacreBLEU#ter for more information. ''' _lowerCAmelCase : Optional[Any] = ''' Produces TER scores alongside the number of edits and reference length. Args: predictions (list of str): The system stream (a sequence of segments). references (list of list of str): A list of one or more reference streams (each a sequence of segments). normalized (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`. ignore_punct (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`. support_zh_ja_chars (boolean): If `True`, tokenization/normalization supports processing of Chinese characters, as well as Japanese Kanji, Hiragana, Katakana, and Phonetic Extensions of Katakana. Only applies if `normalized = True`. Defaults to `False`. case_sensitive (boolean): If `False`, makes all predictions and references lowercase to ignore differences in case. Defaults to `False`. Returns: \'score\' (float): TER score (num_edits / sum_ref_lengths * 100) \'num_edits\' (int): The cumulative number of edits \'ref_length\' (float): The cumulative average reference length Examples: Example 1: >>> predictions = ["does this sentence match??", ... "what about this sentence?", ... "What did the TER metric user say to the developer?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"], ... ["Your jokes are...", "...TERrible"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... case_sensitive=True) >>> print(results) {\'score\': 150.0, \'num_edits\': 15, \'ref_length\': 10.0} Example 2: >>> predictions = ["does this sentence match??", ... "what about this sentence?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... case_sensitive=True) >>> print(results) {\'score\': 62.5, \'num_edits\': 5, \'ref_length\': 8.0} Example 3: >>> predictions = ["does this sentence match??", ... "what about this sentence?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... normalized=True, ... case_sensitive=True) >>> print(results) {\'score\': 57.14285714285714, \'num_edits\': 6, \'ref_length\': 10.5} Example 4: >>> predictions = ["does this sentence match??", ... "what about this sentence?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... ignore_punct=True, ... case_sensitive=False) >>> print(results) {\'score\': 0.0, \'num_edits\': 0, \'ref_length\': 8.0} Example 5: >>> predictions = ["does this sentence match??", ... "what about this sentence?", ... "What did the TER metric user say to the developer?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"], ... ["Your jokes are...", "...TERrible"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... ignore_punct=True, ... case_sensitive=False) >>> print(results) {\'score\': 100.0, \'num_edits\': 10, \'ref_length\': 10.0} ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): """simple docstring""" def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' if version.parse(scb.__version__ ) < version.parse("1.4.12" ): raise ImportWarning( "To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn't match this condition.\n" "You can install it with `pip install \"sacrebleu>=1.4.12\"`." ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage="http://www.cs.umd.edu/~snover/tercom/" , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Sequence(datasets.Value("string" , id="sequence" ) , id="references" ), } ) , codebase_urls=["https://github.com/mjpost/sacreBLEU#ter"] , reference_urls=[ "https://github.com/jhclark/tercom", ] , ) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :Optional[int] , snake_case :List[Any] , snake_case :bool = False , snake_case :bool = False , snake_case :bool = False , snake_case :bool = False , ): '''simple docstring''' A_ : List[str] = len(references[0] ) if any(len(snake_case ) != references_per_prediction for refs in references ): raise ValueError("Sacrebleu requires the same number of references for each prediction" ) A_ : int = [[refs[i] for refs in references] for i in range(snake_case )] A_ : Optional[Any] = TER( normalized=snake_case , no_punct=snake_case , asian_support=snake_case , case_sensitive=snake_case , ) A_ : List[Any] = sb_ter.corpus_score(snake_case , snake_case ) return {"score": output.score, "num_edits": output.num_edits, "ref_length": output.ref_length}
300
0
from __future__ import annotations from collections.abc import Generator def UpperCamelCase ( ): """simple docstring""" __magic_name__ : Tuple = {} __magic_name__ : Any = 2 while True: __magic_name__ : Dict = factor_map.pop(lowerCAmelCase__, lowerCAmelCase__ ) if factor: __magic_name__ : Union[str, Any] = factor + prime while x in factor_map: x += factor __magic_name__ : Tuple = factor else: __magic_name__ : Dict = prime yield prime prime += 1 def UpperCamelCase ( _A = 1e10 ): """simple docstring""" __magic_name__ : int = sieve() __magic_name__ : str = 1 while True: __magic_name__ : Dict = next(lowerCAmelCase__ ) if (2 * prime * n) > limit: return n # Ignore the next prime as the reminder will be 2. next(lowerCAmelCase__ ) n += 2 if __name__ == "__main__": print(solution())
360
import math from dataclasses import dataclass from typing import Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, randn_tensor from .scheduling_utils import SchedulerMixin @dataclass # Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->UnCLIP class snake_case__ ( _lowerCAmelCase ): lowercase__ : torch.FloatTensor lowercase__ : Optional[torch.FloatTensor] = None def UpperCamelCase ( _A, _A=0.999, _A="cosine", ): """simple docstring""" if alpha_transform_type == "cosine": def alpha_bar_fn(_A ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(_A ): return math.exp(t * -12.0 ) else: raise ValueError(f'Unsupported alpha_tranform_type: {alpha_transform_type}' ) __magic_name__ : Optional[Any] = [] for i in range(_A ): __magic_name__ : Dict = i / num_diffusion_timesteps __magic_name__ : Any = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(_A ) / alpha_bar_fn(_A ), _A ) ) return torch.tensor(_A, dtype=torch.floataa ) class snake_case__ ( _lowerCAmelCase , _lowerCAmelCase ): @register_to_config def __init__( self , lowerCAmelCase__ = 10_00 , lowerCAmelCase__ = "fixed_small_log" , lowerCAmelCase__ = True , lowerCAmelCase__ = 1.0 , lowerCAmelCase__ = "epsilon" , lowerCAmelCase__ = "squaredcos_cap_v2" , ) -> Union[str, Any]: if beta_schedule != "squaredcos_cap_v2": raise ValueError("""UnCLIPScheduler only supports `beta_schedule`: 'squaredcos_cap_v2'""" ) __magic_name__ : Tuple = betas_for_alpha_bar(lowerCAmelCase__ ) __magic_name__ : Union[str, Any] = 1.0 - self.betas __magic_name__ : str = torch.cumprod(self.alphas , dim=0 ) __magic_name__ : Any = torch.tensor(1.0 ) # standard deviation of the initial noise distribution __magic_name__ : Tuple = 1.0 # setable values __magic_name__ : List[Any] = None __magic_name__ : int = torch.from_numpy(np.arange(0 , lowerCAmelCase__ )[::-1].copy() ) __magic_name__ : List[Any] = variance_type def __magic_name__ ( self , lowerCAmelCase__ , lowerCAmelCase__ = None ) -> torch.FloatTensor: return sample def __magic_name__ ( self , lowerCAmelCase__ , lowerCAmelCase__ = None ) -> str: __magic_name__ : List[Any] = num_inference_steps __magic_name__ : Union[str, Any] = (self.config.num_train_timesteps - 1) / (self.num_inference_steps - 1) __magic_name__ : List[Any] = (np.arange(0 , lowerCAmelCase__ ) * step_ratio).round()[::-1].copy().astype(np.intaa ) __magic_name__ : Dict = torch.from_numpy(lowerCAmelCase__ ).to(lowerCAmelCase__ ) def __magic_name__ ( self , lowerCAmelCase__ , lowerCAmelCase__=None , lowerCAmelCase__=None , lowerCAmelCase__=None ) -> Tuple: if prev_timestep is None: __magic_name__ : int = t - 1 __magic_name__ : Optional[Any] = self.alphas_cumprod[t] __magic_name__ : Any = self.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.one __magic_name__ : Tuple = 1 - alpha_prod_t __magic_name__ : int = 1 - alpha_prod_t_prev if prev_timestep == t - 1: __magic_name__ : List[str] = self.betas[t] else: __magic_name__ : List[Any] = 1 - alpha_prod_t / alpha_prod_t_prev # For t > 0, compute predicted variance βt (see formula (6) and (7) from https://arxiv.org/pdf/2006.11239.pdf) # and sample from it to get previous sample # x_{t-1} ~ N(pred_prev_sample, variance) == add variance to pred_sample __magic_name__ : Dict = beta_prod_t_prev / beta_prod_t * beta if variance_type is None: __magic_name__ : str = self.config.variance_type # hacks - were probably added for training stability if variance_type == "fixed_small_log": __magic_name__ : str = torch.log(torch.clamp(lowerCAmelCase__ , min=1e-2_0 ) ) __magic_name__ : Optional[Any] = torch.exp(0.5 * variance ) elif variance_type == "learned_range": # NOTE difference with DDPM scheduler __magic_name__ : List[str] = variance.log() __magic_name__ : Optional[int] = beta.log() __magic_name__ : Any = (predicted_variance + 1) / 2 __magic_name__ : Any = frac * max_log + (1 - frac) * min_log return variance def __magic_name__ ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ = None , lowerCAmelCase__=None , lowerCAmelCase__ = True , ) -> Union[UnCLIPSchedulerOutput, Tuple]: __magic_name__ : List[Any] = timestep if model_output.shape[1] == sample.shape[1] * 2 and self.variance_type == "learned_range": __magic_name__ ,__magic_name__ : List[Any] = torch.split(lowerCAmelCase__ , sample.shape[1] , dim=1 ) else: __magic_name__ : List[str] = None # 1. compute alphas, betas if prev_timestep is None: __magic_name__ : Union[str, Any] = t - 1 __magic_name__ : List[str] = self.alphas_cumprod[t] __magic_name__ : Dict = self.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.one __magic_name__ : Any = 1 - alpha_prod_t __magic_name__ : Dict = 1 - alpha_prod_t_prev if prev_timestep == t - 1: __magic_name__ : Union[str, Any] = self.betas[t] __magic_name__ : int = self.alphas[t] else: __magic_name__ : Tuple = 1 - alpha_prod_t / alpha_prod_t_prev __magic_name__ : Tuple = 1 - beta # 2. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (15) from https://arxiv.org/pdf/2006.11239.pdf if self.config.prediction_type == "epsilon": __magic_name__ : Optional[int] = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 elif self.config.prediction_type == "sample": __magic_name__ : Tuple = model_output else: raise ValueError( F'prediction_type given as {self.config.prediction_type} must be one of `epsilon` or `sample`' """ for the UnCLIPScheduler.""" ) # 3. Clip "predicted x_0" if self.config.clip_sample: __magic_name__ : Tuple = torch.clamp( lowerCAmelCase__ , -self.config.clip_sample_range , self.config.clip_sample_range ) # 4. Compute coefficients for pred_original_sample x_0 and current sample x_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf __magic_name__ : List[Any] = (alpha_prod_t_prev ** 0.5 * beta) / beta_prod_t __magic_name__ : Dict = alpha ** 0.5 * beta_prod_t_prev / beta_prod_t # 5. Compute predicted previous sample µ_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf __magic_name__ : str = pred_original_sample_coeff * pred_original_sample + current_sample_coeff * sample # 6. Add noise __magic_name__ : Tuple = 0 if t > 0: __magic_name__ : Any = randn_tensor( model_output.shape , dtype=model_output.dtype , generator=lowerCAmelCase__ , device=model_output.device ) __magic_name__ : Tuple = self._get_variance( lowerCAmelCase__ , predicted_variance=lowerCAmelCase__ , prev_timestep=lowerCAmelCase__ , ) if self.variance_type == "fixed_small_log": __magic_name__ : Tuple = variance elif self.variance_type == "learned_range": __magic_name__ : int = (0.5 * variance).exp() else: raise ValueError( F'variance_type given as {self.variance_type} must be one of `fixed_small_log` or `learned_range`' """ for the UnCLIPScheduler.""" ) __magic_name__ : Tuple = variance * variance_noise __magic_name__ : List[str] = pred_prev_sample + variance if not return_dict: return (pred_prev_sample,) return UnCLIPSchedulerOutput(prev_sample=lowerCAmelCase__ , pred_original_sample=lowerCAmelCase__ ) def __magic_name__ ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , ) -> torch.FloatTensor: # Make sure alphas_cumprod and timestep have same device and dtype as original_samples __magic_name__ : List[str] = self.alphas_cumprod.to(device=original_samples.device , dtype=original_samples.dtype ) __magic_name__ : Any = timesteps.to(original_samples.device ) __magic_name__ : int = alphas_cumprod[timesteps] ** 0.5 __magic_name__ : Union[str, Any] = sqrt_alpha_prod.flatten() while len(sqrt_alpha_prod.shape ) < len(original_samples.shape ): __magic_name__ : int = sqrt_alpha_prod.unsqueeze(-1 ) __magic_name__ : Any = (1 - alphas_cumprod[timesteps]) ** 0.5 __magic_name__ : str = sqrt_one_minus_alpha_prod.flatten() while len(sqrt_one_minus_alpha_prod.shape ) < len(original_samples.shape ): __magic_name__ : Any = sqrt_one_minus_alpha_prod.unsqueeze(-1 ) __magic_name__ : Any = sqrt_alpha_prod * original_samples + sqrt_one_minus_alpha_prod * noise return noisy_samples
138
0
"""simple docstring""" import argparse import struct import unittest class UpperCamelCase : def __init__( self ,__UpperCamelCase ) -> None: '''simple docstring''' lowercase_ : str = data # Initialize hash values lowercase_ : Optional[int] = [ 0X6_A_0_9_E_6_6_7, 0XB_B_6_7_A_E_8_5, 0X3_C_6_E_F_3_7_2, 0XA_5_4_F_F_5_3_A, 0X5_1_0_E_5_2_7_F, 0X9_B_0_5_6_8_8_C, 0X1_F_8_3_D_9_A_B, 0X5_B_E_0_C_D_1_9, ] # Initialize round constants lowercase_ : Tuple = [ 0X4_2_8_A_2_F_9_8, 0X7_1_3_7_4_4_9_1, 0XB_5_C_0_F_B_C_F, 0XE_9_B_5_D_B_A_5, 0X3_9_5_6_C_2_5_B, 0X5_9_F_1_1_1_F_1, 0X9_2_3_F_8_2_A_4, 0XA_B_1_C_5_E_D_5, 0XD_8_0_7_A_A_9_8, 0X1_2_8_3_5_B_0_1, 0X2_4_3_1_8_5_B_E, 0X5_5_0_C_7_D_C_3, 0X7_2_B_E_5_D_7_4, 0X8_0_D_E_B_1_F_E, 0X9_B_D_C_0_6_A_7, 0XC_1_9_B_F_1_7_4, 0XE_4_9_B_6_9_C_1, 0XE_F_B_E_4_7_8_6, 0X0_F_C_1_9_D_C_6, 0X2_4_0_C_A_1_C_C, 0X2_D_E_9_2_C_6_F, 0X4_A_7_4_8_4_A_A, 0X5_C_B_0_A_9_D_C, 0X7_6_F_9_8_8_D_A, 0X9_8_3_E_5_1_5_2, 0XA_8_3_1_C_6_6_D, 0XB_0_0_3_2_7_C_8, 0XB_F_5_9_7_F_C_7, 0XC_6_E_0_0_B_F_3, 0XD_5_A_7_9_1_4_7, 0X0_6_C_A_6_3_5_1, 0X1_4_2_9_2_9_6_7, 0X2_7_B_7_0_A_8_5, 0X2_E_1_B_2_1_3_8, 0X4_D_2_C_6_D_F_C, 0X5_3_3_8_0_D_1_3, 0X6_5_0_A_7_3_5_4, 0X7_6_6_A_0_A_B_B, 0X8_1_C_2_C_9_2_E, 0X9_2_7_2_2_C_8_5, 0XA_2_B_F_E_8_A_1, 0XA_8_1_A_6_6_4_B, 0XC_2_4_B_8_B_7_0, 0XC_7_6_C_5_1_A_3, 0XD_1_9_2_E_8_1_9, 0XD_6_9_9_0_6_2_4, 0XF_4_0_E_3_5_8_5, 0X1_0_6_A_A_0_7_0, 0X1_9_A_4_C_1_1_6, 0X1_E_3_7_6_C_0_8, 0X2_7_4_8_7_7_4_C, 0X3_4_B_0_B_C_B_5, 0X3_9_1_C_0_C_B_3, 0X4_E_D_8_A_A_4_A, 0X5_B_9_C_C_A_4_F, 0X6_8_2_E_6_F_F_3, 0X7_4_8_F_8_2_E_E, 0X7_8_A_5_6_3_6_F, 0X8_4_C_8_7_8_1_4, 0X8_C_C_7_0_2_0_8, 0X9_0_B_E_F_F_F_A, 0XA_4_5_0_6_C_E_B, 0XB_E_F_9_A_3_F_7, 0XC_6_7_1_7_8_F_2, ] lowercase_ : Tuple = self.preprocessing(self.data ) self.final_hash() @staticmethod def _UpperCAmelCase ( __UpperCamelCase ) -> bytes: '''simple docstring''' lowercase_ : str = B'\x80' + (B'\x00' * (63 - (len(__UpperCamelCase ) + 8) % 64)) lowercase_ : str = struct.pack('>Q' ,(len(__UpperCamelCase ) * 8) ) return data + padding + big_endian_integer def _UpperCAmelCase ( self ) -> None: '''simple docstring''' lowercase_ : Optional[Any] = [ self.preprocessed_data[x : x + 64] for x in range(0 ,len(self.preprocessed_data ) ,64 ) ] for block in self.blocks: # Convert the given block into a list of 4 byte integers lowercase_ : Any = list(struct.unpack('>16L' ,__UpperCamelCase ) ) # add 48 0-ed integers words += [0] * 48 lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ : Optional[int] = self.hashes for index in range(0 ,64 ): if index > 15: # modify the zero-ed indexes at the end of the array lowercase_ : str = ( self.ror(words[index - 15] ,7 ) ^ self.ror(words[index - 15] ,18 ) ^ (words[index - 15] >> 3) ) lowercase_ : int = ( self.ror(words[index - 2] ,17 ) ^ self.ror(words[index - 2] ,19 ) ^ (words[index - 2] >> 10) ) lowercase_ : Optional[Any] = ( words[index - 16] + sa + words[index - 7] + sa ) % 0X1_0_0_0_0_0_0_0_0 # Compression lowercase_ : Tuple = self.ror(__UpperCamelCase ,6 ) ^ self.ror(__UpperCamelCase ,11 ) ^ self.ror(__UpperCamelCase ,25 ) lowercase_ : Union[str, Any] = (e & f) ^ ((~e & 0XF_F_F_F_F_F_F_F) & g) lowercase_ : str = ( h + sa + ch + self.round_constants[index] + words[index] ) % 0X1_0_0_0_0_0_0_0_0 lowercase_ : Optional[int] = self.ror(__UpperCamelCase ,2 ) ^ self.ror(__UpperCamelCase ,13 ) ^ self.ror(__UpperCamelCase ,22 ) lowercase_ : Optional[Any] = (a & b) ^ (a & c) ^ (b & c) lowercase_ : Any = (sa + maj) % 0X1_0_0_0_0_0_0_0_0 lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ : Tuple = ( g, f, e, ((d + tempa) % 0X1_0_0_0_0_0_0_0_0), c, b, a, ((tempa + tempa) % 0X1_0_0_0_0_0_0_0_0), ) lowercase_ : str = [a, b, c, d, e, f, g, h] # Modify final values lowercase_ : Dict = [ ((element + mutated_hash_values[index]) % 0X1_0_0_0_0_0_0_0_0) for index, element in enumerate(self.hashes ) ] lowercase_ : Any = ''.join([hex(__UpperCamelCase )[2:].zfill(8 ) for value in self.hashes] ) def _UpperCAmelCase ( self ,__UpperCamelCase ,__UpperCamelCase ) -> int: '''simple docstring''' return 0XF_F_F_F_F_F_F_F & (value << (32 - rotations)) | (value >> rotations) class UpperCamelCase ( unittest.TestCase ): def _UpperCAmelCase ( self ) -> None: '''simple docstring''' import hashlib lowercase_ : Union[str, Any] = bytes('Test String' ,'utf-8' ) self.assertEqual(SHAaaa(__UpperCamelCase ).hash ,hashlib.shaaaa(__UpperCamelCase ).hexdigest() ) def lowercase__( ): import doctest doctest.testmod() lowercase_ : Tuple = argparse.ArgumentParser() parser.add_argument( '-s' , '--string' , dest='input_string' , default='Hello World!! Welcome to Cryptography' , help='Hash the string' , ) parser.add_argument( '-f' , '--file' , dest='input_file' , help='Hash contents of a file' ) lowercase_ : Any = parser.parse_args() lowercase_ : int = args.input_string # hash input should be a bytestring if args.input_file: with open(args.input_file , 'rb' ) as f: lowercase_ : str = f.read() else: lowercase_ : Optional[int] = bytes(__SCREAMING_SNAKE_CASE , 'utf-8' ) print(SHAaaa(__SCREAMING_SNAKE_CASE ).hash ) if __name__ == "__main__": main()
213
"""simple docstring""" __SCREAMING_SNAKE_CASE =[ "DownloadConfig", "DownloadManager", "DownloadMode", "StreamingDownloadManager", ] from .download_config import DownloadConfig from .download_manager import DownloadManager, DownloadMode from .streaming_download_manager import StreamingDownloadManager
213
1
import doctest import logging import os import unittest from pathlib import Path from typing import List, Union import transformers from transformers.testing_utils import require_tf, require_torch, slow _lowerCamelCase : Union[str, Any] = logging.getLogger() @unittest.skip("""Temporarily disable the doc tests.""" ) @require_torch @require_tf @slow class __UpperCAmelCase ( unittest.TestCase ): def __magic_name__ ( self : List[Any], __A : Path, __A : Union[str, None] = None, __A : Union[List[str], None] = None, __A : Union[str, List[str], None] = None, __A : bool = True, ): UpperCAmelCase : List[Any] = [file for file in os.listdir(__A ) if os.path.isfile(os.path.join(__A, __A ) )] if identifier is not None: UpperCAmelCase : Optional[Any] = [file for file in files if identifier in file] if n_identifier is not None: if isinstance(__A, __A ): for n_ in n_identifier: UpperCAmelCase : Dict = [file for file in files if n_ not in file] else: UpperCAmelCase : Union[str, Any] = [file for file in files if n_identifier not in file] UpperCAmelCase : Any = ignore_files or [] ignore_files.append('''__init__.py''' ) UpperCAmelCase : int = [file for file in files if file not in ignore_files] for file in files: # Open all files print('''Testing''', __A ) if only_modules: UpperCAmelCase : Union[str, Any] = file.split('''.''' )[0] try: UpperCAmelCase : Tuple = getattr(__A, __A ) UpperCAmelCase : Tuple = doctest.DocTestSuite(__A ) UpperCAmelCase : List[Any] = unittest.TextTestRunner().run(__A ) self.assertIs(len(result.failures ), 0 ) except AttributeError: logger.info(F'''{module_identifier} is not a module.''' ) else: UpperCAmelCase : List[str] = doctest.testfile(str('''..''' / directory / file ), optionflags=doctest.ELLIPSIS ) self.assertIs(result.failed, 0 ) def __magic_name__ ( self : Any ): UpperCAmelCase : str = Path('''src/transformers''' ) UpperCAmelCase : List[Any] = '''modeling''' UpperCAmelCase : Any = [ '''modeling_ctrl.py''', '''modeling_tf_ctrl.py''', ] self.analyze_directory(__A, identifier=__A, ignore_files=__A ) def __magic_name__ ( self : Dict ): UpperCAmelCase : Optional[Any] = Path('''src/transformers''' ) UpperCAmelCase : Any = '''tokenization''' self.analyze_directory(__A, identifier=__A ) def __magic_name__ ( self : List[Any] ): UpperCAmelCase : Optional[Any] = Path('''src/transformers''' ) UpperCAmelCase : str = '''configuration''' self.analyze_directory(__A, identifier=__A ) def __magic_name__ ( self : Any ): UpperCAmelCase : Dict = Path('''src/transformers''' ) UpperCAmelCase : List[str] = ['''configuration''', '''modeling''', '''tokenization'''] self.analyze_directory(__A, n_identifier=__A ) def __magic_name__ ( self : int ): UpperCAmelCase : str = Path('''docs/source''' ) UpperCAmelCase : Optional[int] = ['''favicon.ico'''] self.analyze_directory(__A, ignore_files=__A, only_modules=__A )
353
import os from shutil import copyfile from typing import List, Optional, Tuple from tokenizers import processors from ...tokenization_utils import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_nllb import NllbTokenizer else: _lowerCamelCase : List[Any] = None _lowerCamelCase : str = logging.get_logger(__name__) _lowerCamelCase : str = {"vocab_file": "sentencepiece.bpe.model", "tokenizer_file": "tokenizer.json"} _lowerCamelCase : str = { "vocab_file": { "facebook/nllb-200-distilled-600M": ( "https://huggingface.co/facebook/nllb-200-distilled-600M/resolve/main/sentencepiece.bpe.model" ), }, "tokenizer_file": { "facebook/nllb-200-distilled-600M": ( "https://huggingface.co/facebook/nllb-200-distilled-600M/resolve/main/tokenizer.json" ), }, } _lowerCamelCase : Any = { "facebook/nllb-large-en-ro": 1_0_2_4, "facebook/nllb-200-distilled-600M": 1_0_2_4, } # fmt: off _lowerCamelCase : int = ["ace_Arab", "ace_Latn", "acm_Arab", "acq_Arab", "aeb_Arab", "afr_Latn", "ajp_Arab", "aka_Latn", "amh_Ethi", "apc_Arab", "arb_Arab", "ars_Arab", "ary_Arab", "arz_Arab", "asm_Beng", "ast_Latn", "awa_Deva", "ayr_Latn", "azb_Arab", "azj_Latn", "bak_Cyrl", "bam_Latn", "ban_Latn", "bel_Cyrl", "bem_Latn", "ben_Beng", "bho_Deva", "bjn_Arab", "bjn_Latn", "bod_Tibt", "bos_Latn", "bug_Latn", "bul_Cyrl", "cat_Latn", "ceb_Latn", "ces_Latn", "cjk_Latn", "ckb_Arab", "crh_Latn", "cym_Latn", "dan_Latn", "deu_Latn", "dik_Latn", "dyu_Latn", "dzo_Tibt", "ell_Grek", "eng_Latn", "epo_Latn", "est_Latn", "eus_Latn", "ewe_Latn", "fao_Latn", "pes_Arab", "fij_Latn", "fin_Latn", "fon_Latn", "fra_Latn", "fur_Latn", "fuv_Latn", "gla_Latn", "gle_Latn", "glg_Latn", "grn_Latn", "guj_Gujr", "hat_Latn", "hau_Latn", "heb_Hebr", "hin_Deva", "hne_Deva", "hrv_Latn", "hun_Latn", "hye_Armn", "ibo_Latn", "ilo_Latn", "ind_Latn", "isl_Latn", "ita_Latn", "jav_Latn", "jpn_Jpan", "kab_Latn", "kac_Latn", "kam_Latn", "kan_Knda", "kas_Arab", "kas_Deva", "kat_Geor", "knc_Arab", "knc_Latn", "kaz_Cyrl", "kbp_Latn", "kea_Latn", "khm_Khmr", "kik_Latn", "kin_Latn", "kir_Cyrl", "kmb_Latn", "kon_Latn", "kor_Hang", "kmr_Latn", "lao_Laoo", "lvs_Latn", "lij_Latn", "lim_Latn", "lin_Latn", "lit_Latn", "lmo_Latn", "ltg_Latn", "ltz_Latn", "lua_Latn", "lug_Latn", "luo_Latn", "lus_Latn", "mag_Deva", "mai_Deva", "mal_Mlym", "mar_Deva", "min_Latn", "mkd_Cyrl", "plt_Latn", "mlt_Latn", "mni_Beng", "khk_Cyrl", "mos_Latn", "mri_Latn", "zsm_Latn", "mya_Mymr", "nld_Latn", "nno_Latn", "nob_Latn", "npi_Deva", "nso_Latn", "nus_Latn", "nya_Latn", "oci_Latn", "gaz_Latn", "ory_Orya", "pag_Latn", "pan_Guru", "pap_Latn", "pol_Latn", "por_Latn", "prs_Arab", "pbt_Arab", "quy_Latn", "ron_Latn", "run_Latn", "rus_Cyrl", "sag_Latn", "san_Deva", "sat_Beng", "scn_Latn", "shn_Mymr", "sin_Sinh", "slk_Latn", "slv_Latn", "smo_Latn", "sna_Latn", "snd_Arab", "som_Latn", "sot_Latn", "spa_Latn", "als_Latn", "srd_Latn", "srp_Cyrl", "ssw_Latn", "sun_Latn", "swe_Latn", "swh_Latn", "szl_Latn", "tam_Taml", "tat_Cyrl", "tel_Telu", "tgk_Cyrl", "tgl_Latn", "tha_Thai", "tir_Ethi", "taq_Latn", "taq_Tfng", "tpi_Latn", "tsn_Latn", "tso_Latn", "tuk_Latn", "tum_Latn", "tur_Latn", "twi_Latn", "tzm_Tfng", "uig_Arab", "ukr_Cyrl", "umb_Latn", "urd_Arab", "uzn_Latn", "vec_Latn", "vie_Latn", "war_Latn", "wol_Latn", "xho_Latn", "ydd_Hebr", "yor_Latn", "yue_Hant", "zho_Hans", "zho_Hant", "zul_Latn"] class __UpperCAmelCase ( lowerCamelCase__ ): UpperCamelCase = VOCAB_FILES_NAMES UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase = ["""input_ids""", """attention_mask"""] UpperCamelCase = NllbTokenizer UpperCamelCase = [] UpperCamelCase = [] def __init__( self : Optional[Any], __A : Tuple=None, __A : int=None, __A : List[Any]="<s>", __A : Tuple="</s>", __A : Any="</s>", __A : Optional[Any]="<s>", __A : Tuple="<unk>", __A : str="<pad>", __A : Dict="<mask>", __A : Optional[Any]=None, __A : List[Any]=None, __A : List[Any]=None, __A : str=False, **__A : Tuple, ): # Mask token behave like a normal word, i.e. include the space before it UpperCAmelCase : int = AddedToken(__A, lstrip=__A, rstrip=__A ) if isinstance(__A, __A ) else mask_token UpperCAmelCase : str = legacy_behaviour super().__init__( vocab_file=__A, tokenizer_file=__A, bos_token=__A, eos_token=__A, sep_token=__A, cls_token=__A, unk_token=__A, pad_token=__A, mask_token=__A, src_lang=__A, tgt_lang=__A, additional_special_tokens=__A, legacy_behaviour=__A, **__A, ) UpperCAmelCase : Optional[int] = vocab_file UpperCAmelCase : Optional[Any] = False if not self.vocab_file else True UpperCAmelCase : Optional[int] = FAIRSEQ_LANGUAGE_CODES.copy() if additional_special_tokens is not None: # Only add those special tokens if they are not already there. _additional_special_tokens.extend( [t for t in additional_special_tokens if t not in _additional_special_tokens] ) self.add_special_tokens({'''additional_special_tokens''': _additional_special_tokens} ) UpperCAmelCase : List[Any] = { lang_code: self.convert_tokens_to_ids(__A ) for lang_code in FAIRSEQ_LANGUAGE_CODES } UpperCAmelCase : List[Any] = src_lang if src_lang is not None else '''eng_Latn''' UpperCAmelCase : List[Any] = self.convert_tokens_to_ids(self._src_lang ) UpperCAmelCase : List[str] = tgt_lang self.set_src_lang_special_tokens(self._src_lang ) @property def __magic_name__ ( self : Optional[int] ): return self._src_lang @src_lang.setter def __magic_name__ ( self : Union[str, Any], __A : str ): UpperCAmelCase : Tuple = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def __magic_name__ ( self : Any, __A : List[int], __A : Optional[List[int]] = None ): if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def __magic_name__ ( self : Tuple, __A : List[int], __A : Optional[List[int]] = None ): UpperCAmelCase : Union[str, Any] = [self.sep_token_id] UpperCAmelCase : 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] def __magic_name__ ( self : str, __A : Optional[int], __A : str, __A : Optional[str], __A : Optional[str], **__A : Union[str, Any] ): if src_lang is None or tgt_lang is None: raise ValueError('''Translation requires a `src_lang` and a `tgt_lang` for this model''' ) UpperCAmelCase : Optional[Any] = src_lang UpperCAmelCase : Optional[Any] = self(__A, add_special_tokens=__A, return_tensors=__A, **__A ) UpperCAmelCase : Union[str, Any] = self.convert_tokens_to_ids(__A ) UpperCAmelCase : int = tgt_lang_id return inputs def __magic_name__ ( self : List[str], __A : List[str], __A : str = "eng_Latn", __A : Optional[List[str]] = None, __A : str = "fra_Latn", **__A : Tuple, ): UpperCAmelCase : Any = src_lang UpperCAmelCase : Tuple = tgt_lang return super().prepare_seqaseq_batch(__A, __A, **__A ) def __magic_name__ ( self : List[str] ): return self.set_src_lang_special_tokens(self.src_lang ) def __magic_name__ ( self : Optional[Any] ): return self.set_tgt_lang_special_tokens(self.tgt_lang ) def __magic_name__ ( self : Union[str, Any], __A : List[str] ): UpperCAmelCase : int = self.convert_tokens_to_ids(__A ) if self.legacy_behaviour: UpperCAmelCase : Tuple = [] UpperCAmelCase : Optional[int] = [self.eos_token_id, self.cur_lang_code] else: UpperCAmelCase : Any = [self.cur_lang_code] UpperCAmelCase : Optional[Any] = [self.eos_token_id] UpperCAmelCase : Dict = self.convert_ids_to_tokens(self.prefix_tokens ) UpperCAmelCase : Union[str, Any] = self.convert_ids_to_tokens(self.suffix_tokens ) UpperCAmelCase : int = processors.TemplateProcessing( single=prefix_tokens_str + ['''$A'''] + suffix_tokens_str, pair=prefix_tokens_str + ['''$A''', '''$B'''] + suffix_tokens_str, special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str, self.prefix_tokens + self.suffix_tokens ) ), ) def __magic_name__ ( self : Dict, __A : str ): UpperCAmelCase : Optional[int] = self.convert_tokens_to_ids(__A ) if self.legacy_behaviour: UpperCAmelCase : List[Any] = [] UpperCAmelCase : Any = [self.eos_token_id, self.cur_lang_code] else: UpperCAmelCase : int = [self.cur_lang_code] UpperCAmelCase : List[Any] = [self.eos_token_id] UpperCAmelCase : str = self.convert_ids_to_tokens(self.prefix_tokens ) UpperCAmelCase : List[Any] = self.convert_ids_to_tokens(self.suffix_tokens ) UpperCAmelCase : List[str] = processors.TemplateProcessing( single=prefix_tokens_str + ['''$A'''] + suffix_tokens_str, pair=prefix_tokens_str + ['''$A''', '''$B'''] + suffix_tokens_str, special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str, self.prefix_tokens + self.suffix_tokens ) ), ) def __magic_name__ ( self : Dict, __A : str, __A : Optional[str] = None ): if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(__A ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory.''' ) return UpperCAmelCase : Tuple = os.path.join( __A, (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__A ): copyfile(self.vocab_file, __A ) return (out_vocab_file,)
99
0
'''simple docstring''' from dataclasses import dataclass from typing import List, Optional, Union import numpy as np import torch from ...utils import BaseOutput, OptionalDependencyNotAvailable, is_torch_available, is_transformers_available @dataclass class __SCREAMING_SNAKE_CASE (lowerCamelCase_ ): """simple docstring""" __a =42 try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .pipeline_text_to_video_synth import TextToVideoSDPipeline from .pipeline_text_to_video_synth_imgaimg import VideoToVideoSDPipeline # noqa: F401 from .pipeline_text_to_video_zero import TextToVideoZeroPipeline
63
import argparse import glob import logging import os import sys import time from collections import defaultdict from pathlib import Path from typing import Dict, List, Tuple import numpy as np import pytorch_lightning as pl import torch from callbacks import SeqaSeqLoggingCallback, get_checkpoint_callback, get_early_stopping_callback from torch import nn from torch.utils.data import DataLoader from transformers import MBartTokenizer, TaForConditionalGeneration from transformers.models.bart.modeling_bart import shift_tokens_right from utils import ( ROUGE_KEYS, LegacySeqaSeqDataset, SeqaSeqDataset, assert_all_frozen, calculate_bleu, calculate_rouge, check_output_dir, flatten_list, freeze_embeds, freeze_params, get_git_info, label_smoothed_nll_loss, lmap, pickle_save, save_git_info, save_json, use_task_specific_params, ) # need the parent dir module sys.path.insert(2, str(Path(__file__).resolve().parents[1])) from lightning_base import BaseTransformer, add_generic_args, generic_train # noqa A__ = logging.getLogger(__name__) class __lowerCAmelCase ( lowerCamelCase__ ): __lowerCamelCase = '''summarization''' __lowerCamelCase = ['''loss'''] __lowerCamelCase = ROUGE_KEYS __lowerCamelCase = '''rouge2''' def __init__( self , _snake_case , **_snake_case ): """simple docstring""" if hparams.sortish_sampler and hparams.gpus > 1: _lowerCAmelCase = False elif hparams.max_tokens_per_batch is not None: if hparams.gpus > 1: raise NotImplementedError("""Dynamic Batch size does not work for multi-gpu training""" ) if hparams.sortish_sampler: raise ValueError("""--sortish_sampler and --max_tokens_per_batch may not be used simultaneously""" ) super().__init__(_snake_case , num_labels=_snake_case , mode=self.mode , **_snake_case ) use_task_specific_params(self.model , """summarization""" ) save_git_info(self.hparams.output_dir ) _lowerCAmelCase = Path(self.output_dir ) / """metrics.json""" _lowerCAmelCase = Path(self.output_dir ) / """hparams.pkl""" pickle_save(self.hparams , self.hparams_save_path ) _lowerCAmelCase = 0 _lowerCAmelCase = defaultdict(_snake_case ) _lowerCAmelCase = self.config.model_type _lowerCAmelCase = self.config.tgt_vocab_size if self.model_type == """fsmt""" else self.config.vocab_size _lowerCAmelCase = { "data_dir": self.hparams.data_dir, "max_source_length": self.hparams.max_source_length, "prefix": self.model.config.prefix or "", } _lowerCAmelCase = { """train""": self.hparams.n_train, """val""": self.hparams.n_val, """test""": self.hparams.n_test, } _lowerCAmelCase = {k: v if v >= 0 else None for k, v in n_observations_per_split.items()} _lowerCAmelCase = { """train""": self.hparams.max_target_length, """val""": self.hparams.val_max_target_length, """test""": self.hparams.test_max_target_length, } assert self.target_lens["train"] <= self.target_lens["val"], F'target_lens: {self.target_lens}' assert self.target_lens["train"] <= self.target_lens["test"], F'target_lens: {self.target_lens}' if self.hparams.freeze_embeds: freeze_embeds(self.model ) if self.hparams.freeze_encoder: freeze_params(self.model.get_encoder() ) assert_all_frozen(self.model.get_encoder() ) _lowerCAmelCase = get_git_info()["""repo_sha"""] _lowerCAmelCase = hparams.num_workers _lowerCAmelCase = None # default to config if self.model.config.decoder_start_token_id is None and isinstance(self.tokenizer , _snake_case ): _lowerCAmelCase = self.tokenizer.lang_code_to_id[hparams.tgt_lang] _lowerCAmelCase = self.decoder_start_token_id _lowerCAmelCase = ( SeqaSeqDataset if hasattr(self.tokenizer , """prepare_seq2seq_batch""" ) else LegacySeqaSeqDataset ) _lowerCAmelCase = False _lowerCAmelCase = self.model.config.num_beams if self.hparams.eval_beams is None else self.hparams.eval_beams if self.hparams.eval_max_gen_length is not None: _lowerCAmelCase = self.hparams.eval_max_gen_length else: _lowerCAmelCase = self.model.config.max_length _lowerCAmelCase = self.default_val_metric if self.hparams.val_metric is None else self.hparams.val_metric def snake_case ( self , _snake_case ): """simple docstring""" _lowerCAmelCase = { k: self.tokenizer.batch_decode(v.tolist() ) if """mask""" not in k else v.shape for k, v in batch.items() } save_json(_snake_case , Path(self.output_dir ) / """text_batch.json""" ) save_json({k: v.tolist() for k, v in batch.items()} , Path(self.output_dir ) / """tok_batch.json""" ) _lowerCAmelCase = True return readable_batch def snake_case ( self , _snake_case , **_snake_case ): """simple docstring""" return self.model(_snake_case , **_snake_case ) def snake_case ( self , _snake_case ): """simple docstring""" _lowerCAmelCase = self.tokenizer.batch_decode( _snake_case , skip_special_tokens=_snake_case , clean_up_tokenization_spaces=_snake_case ) return lmap(str.strip , _snake_case ) def snake_case ( self , _snake_case ): """simple docstring""" _lowerCAmelCase = self.tokenizer.pad_token_id _lowerCAmelCase , _lowerCAmelCase = batch["""input_ids"""], batch["""attention_mask"""] _lowerCAmelCase = batch["""labels"""] if isinstance(self.model , _snake_case ): _lowerCAmelCase = self.model._shift_right(_snake_case ) else: _lowerCAmelCase = shift_tokens_right(_snake_case , _snake_case ) if not self.already_saved_batch: # This would be slightly better if it only happened on rank zero _lowerCAmelCase = decoder_input_ids self.save_readable_batch(_snake_case ) _lowerCAmelCase = self(_snake_case , attention_mask=_snake_case , decoder_input_ids=_snake_case , use_cache=_snake_case ) _lowerCAmelCase = outputs["""logits"""] if self.hparams.label_smoothing == 0: # Same behavior as modeling_bart.py, besides ignoring pad_token_id _lowerCAmelCase = nn.CrossEntropyLoss(ignore_index=_snake_case ) assert lm_logits.shape[-1] == self.vocab_size _lowerCAmelCase = ce_loss_fct(lm_logits.view(-1 , lm_logits.shape[-1] ) , tgt_ids.view(-1 ) ) else: _lowerCAmelCase = nn.functional.log_softmax(_snake_case , dim=-1 ) _lowerCAmelCase , _lowerCAmelCase = label_smoothed_nll_loss( _snake_case , _snake_case , self.hparams.label_smoothing , ignore_index=_snake_case ) return (loss,) @property def snake_case ( self ): """simple docstring""" return self.tokenizer.pad_token_id def snake_case ( self , _snake_case , _snake_case ): """simple docstring""" _lowerCAmelCase = self._step(_snake_case ) _lowerCAmelCase = dict(zip(self.loss_names , _snake_case ) ) # tokens per batch _lowerCAmelCase = batch["""input_ids"""].ne(self.pad ).sum() + batch["""labels"""].ne(self.pad ).sum() _lowerCAmelCase = batch["""input_ids"""].shape[0] _lowerCAmelCase = batch["""input_ids"""].eq(self.pad ).sum() _lowerCAmelCase = batch["""input_ids"""].eq(self.pad ).float().mean() # TODO(SS): make a wandb summary metric for this return {"loss": loss_tensors[0], "log": logs} def snake_case ( self , _snake_case , _snake_case ): """simple docstring""" return self._generative_step(_snake_case ) def snake_case ( self , _snake_case , _snake_case="val" ): """simple docstring""" self.step_count += 1 _lowerCAmelCase = {k: torch.stack([x[k] for x in outputs] ).mean() for k in self.loss_names} _lowerCAmelCase = losses["""loss"""] _lowerCAmelCase = { k: np.array([x[k] for x in outputs] ).mean() for k in self.metric_names + ["""gen_time""", """gen_len"""] } _lowerCAmelCase = ( generative_metrics[self.val_metric] if self.val_metric in generative_metrics else losses[self.val_metric] ) _lowerCAmelCase = torch.tensor(_snake_case ).type_as(_snake_case ) generative_metrics.update({k: v.item() for k, v in losses.items()} ) losses.update(_snake_case ) _lowerCAmelCase = {F'{prefix}_avg_{k}': x for k, x in losses.items()} _lowerCAmelCase = self.step_count self.metrics[prefix].append(_snake_case ) # callback writes this to self.metrics_save_path _lowerCAmelCase = flatten_list([x["""preds"""] for x in outputs] ) return { "log": all_metrics, "preds": preds, F'{prefix}_loss': loss, F'{prefix}_{self.val_metric}': metric_tensor, } def snake_case ( self , _snake_case , _snake_case ): """simple docstring""" return calculate_rouge(_snake_case , _snake_case ) def snake_case ( self , _snake_case ): """simple docstring""" _lowerCAmelCase = time.time() # parser.add_argument('--eval_max_gen_length', type=int, default=None, help='never generate more than n tokens') _lowerCAmelCase = self.model.generate( batch["""input_ids"""] , attention_mask=batch["""attention_mask"""] , use_cache=_snake_case , decoder_start_token_id=self.decoder_start_token_id , num_beams=self.eval_beams , max_length=self.eval_max_length , ) _lowerCAmelCase = (time.time() - ta) / batch["""input_ids"""].shape[0] _lowerCAmelCase = self.ids_to_clean_text(_snake_case ) _lowerCAmelCase = self.ids_to_clean_text(batch["""labels"""] ) _lowerCAmelCase = self._step(_snake_case ) _lowerCAmelCase = dict(zip(self.loss_names , _snake_case ) ) _lowerCAmelCase = self.calc_generative_metrics(_snake_case , _snake_case ) _lowerCAmelCase = np.mean(lmap(_snake_case , _snake_case ) ) base_metrics.update(gen_time=_snake_case , gen_len=_snake_case , preds=_snake_case , target=_snake_case , **_snake_case ) return base_metrics def snake_case ( self , _snake_case , _snake_case ): """simple docstring""" return self._generative_step(_snake_case ) def snake_case ( self , _snake_case ): """simple docstring""" return self.validation_epoch_end(_snake_case , prefix="""test""" ) def snake_case ( self , _snake_case ): """simple docstring""" _lowerCAmelCase = self.n_obs[type_path] _lowerCAmelCase = self.target_lens[type_path] _lowerCAmelCase = self.dataset_class( self.tokenizer , type_path=_snake_case , n_obs=_snake_case , max_target_length=_snake_case , **self.dataset_kwargs , ) return dataset def snake_case ( self , _snake_case , _snake_case , _snake_case = False ): """simple docstring""" _lowerCAmelCase = self.get_dataset(_snake_case ) if self.hparams.sortish_sampler and type_path != "test" and type_path != "val": _lowerCAmelCase = dataset.make_sortish_sampler(_snake_case , distributed=self.hparams.gpus > 1 ) return DataLoader( _snake_case , batch_size=_snake_case , collate_fn=dataset.collate_fn , shuffle=_snake_case , num_workers=self.num_workers , sampler=_snake_case , ) elif self.hparams.max_tokens_per_batch is not None and type_path != "test" and type_path != "val": _lowerCAmelCase = dataset.make_dynamic_sampler( self.hparams.max_tokens_per_batch , distributed=self.hparams.gpus > 1 ) return DataLoader( _snake_case , batch_sampler=_snake_case , collate_fn=dataset.collate_fn , num_workers=self.num_workers , ) else: return DataLoader( _snake_case , batch_size=_snake_case , collate_fn=dataset.collate_fn , shuffle=_snake_case , num_workers=self.num_workers , sampler=_snake_case , ) def snake_case ( self ): """simple docstring""" _lowerCAmelCase = self.get_dataloader("""train""" , batch_size=self.hparams.train_batch_size , shuffle=_snake_case ) return dataloader def snake_case ( self ): """simple docstring""" return self.get_dataloader("""val""" , batch_size=self.hparams.eval_batch_size ) def snake_case ( self ): """simple docstring""" return self.get_dataloader("""test""" , batch_size=self.hparams.eval_batch_size ) @staticmethod def snake_case ( _snake_case , _snake_case ): """simple docstring""" BaseTransformer.add_model_specific_args(_snake_case , _snake_case ) add_generic_args(_snake_case , _snake_case ) parser.add_argument( """--max_source_length""" , default=1024 , type=_snake_case , help=( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) , ) parser.add_argument( """--max_target_length""" , default=56 , type=_snake_case , help=( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) , ) parser.add_argument( """--val_max_target_length""" , default=142 , type=_snake_case , help=( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) , ) parser.add_argument( """--test_max_target_length""" , default=142 , type=_snake_case , help=( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) , ) parser.add_argument("""--freeze_encoder""" , action="""store_true""" ) parser.add_argument("""--freeze_embeds""" , action="""store_true""" ) parser.add_argument("""--sortish_sampler""" , action="""store_true""" , default=_snake_case ) parser.add_argument("""--overwrite_output_dir""" , action="""store_true""" , default=_snake_case ) parser.add_argument("""--max_tokens_per_batch""" , type=_snake_case , default=_snake_case ) parser.add_argument("""--logger_name""" , type=_snake_case , choices=["""default""", """wandb""", """wandb_shared"""] , default="""default""" ) parser.add_argument("""--n_train""" , type=_snake_case , default=-1 , required=_snake_case , help="""# examples. -1 means use all.""" ) parser.add_argument("""--n_val""" , type=_snake_case , default=500 , required=_snake_case , help="""# examples. -1 means use all.""" ) parser.add_argument("""--n_test""" , type=_snake_case , default=-1 , required=_snake_case , help="""# examples. -1 means use all.""" ) parser.add_argument( """--task""" , type=_snake_case , default="""summarization""" , required=_snake_case , help="""# examples. -1 means use all.""" ) parser.add_argument("""--label_smoothing""" , type=_snake_case , default=0.0 , required=_snake_case ) parser.add_argument("""--src_lang""" , type=_snake_case , default="""""" , required=_snake_case ) parser.add_argument("""--tgt_lang""" , type=_snake_case , default="""""" , required=_snake_case ) parser.add_argument("""--eval_beams""" , type=_snake_case , default=_snake_case , required=_snake_case ) parser.add_argument( """--val_metric""" , type=_snake_case , default=_snake_case , required=_snake_case , choices=["""bleu""", """rouge2""", """loss""", None] ) parser.add_argument("""--eval_max_gen_length""" , type=_snake_case , default=_snake_case , help="""never generate more than n tokens""" ) parser.add_argument("""--save_top_k""" , type=_snake_case , default=1 , required=_snake_case , help="""How many checkpoints to save""" ) parser.add_argument( """--early_stopping_patience""" , type=_snake_case , default=-1 , required=_snake_case , help=( """-1 means never early stop. early_stopping_patience is measured in validation checks, not epochs. So""" """ val_check_interval will effect it.""" ) , ) return parser class __lowerCAmelCase ( lowerCamelCase__ ): __lowerCamelCase = '''translation''' __lowerCamelCase = ['''loss'''] __lowerCamelCase = ['''bleu'''] __lowerCamelCase = '''bleu''' def __init__( self , _snake_case , **_snake_case ): """simple docstring""" super().__init__(_snake_case , **_snake_case ) _lowerCAmelCase = hparams.src_lang _lowerCAmelCase = hparams.tgt_lang def snake_case ( self , _snake_case , _snake_case ): """simple docstring""" return calculate_bleu(_snake_case , _snake_case ) def _UpperCAmelCase ( snake_case , snake_case=None ): """simple docstring""" Path(args.output_dir ).mkdir(exist_ok=snake_case ) check_output_dir(snake_case , expected_items=3 ) if model is None: if "summarization" in args.task: _lowerCAmelCase = SummarizationModule(snake_case ) else: _lowerCAmelCase = TranslationModule(snake_case ) _lowerCAmelCase = Path(args.data_dir ).name if ( args.logger_name == "default" or args.fast_dev_run or str(args.output_dir ).startswith("""/tmp""" ) or str(args.output_dir ).startswith("""/var""" ) ): _lowerCAmelCase = True # don't pollute wandb logs unnecessarily elif args.logger_name == "wandb": from pytorch_lightning.loggers import WandbLogger _lowerCAmelCase = os.environ.get("""WANDB_PROJECT""" , snake_case ) _lowerCAmelCase = WandbLogger(name=model.output_dir.name , project=snake_case ) elif args.logger_name == "wandb_shared": from pytorch_lightning.loggers import WandbLogger _lowerCAmelCase = WandbLogger(name=model.output_dir.name , project=F'hf_{dataset}' ) if args.early_stopping_patience >= 0: _lowerCAmelCase = get_early_stopping_callback(model.val_metric , args.early_stopping_patience ) else: _lowerCAmelCase = False _lowerCAmelCase = args.val_metric == """loss""" _lowerCAmelCase = generic_train( snake_case , snake_case , logging_callback=SeqaSeqLoggingCallback() , checkpoint_callback=get_checkpoint_callback( args.output_dir , model.val_metric , args.save_top_k , snake_case ) , early_stopping_callback=snake_case , logger=snake_case , ) pickle_save(model.hparams , model.output_dir / """hparams.pkl""" ) if not args.do_predict: return model _lowerCAmelCase = """""" _lowerCAmelCase = sorted(glob.glob(os.path.join(args.output_dir , """*.ckpt""" ) , recursive=snake_case ) ) if checkpoints: _lowerCAmelCase = checkpoints[-1] _lowerCAmelCase = checkpoints[-1] trainer.logger.log_hyperparams(model.hparams ) # test() without a model tests using the best checkpoint automatically trainer.test() return model if __name__ == "__main__": A__ = argparse.ArgumentParser() A__ = pl.Trainer.add_argparse_args(parser) A__ = SummarizationModule.add_model_specific_args(parser, os.getcwd()) A__ = parser.parse_args() main(args)
82
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, is_vision_available, ) UpperCamelCase_ = { 'configuration_owlvit': [ 'OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'OwlViTConfig', 'OwlViTOnnxConfig', 'OwlViTTextConfig', 'OwlViTVisionConfig', ], 'processing_owlvit': ['OwlViTProcessor'], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase_ = ['OwlViTFeatureExtractor'] UpperCamelCase_ = ['OwlViTImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase_ = [ 'OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'OwlViTModel', 'OwlViTPreTrainedModel', 'OwlViTTextModel', 'OwlViTVisionModel', 'OwlViTForObjectDetection', ] if TYPE_CHECKING: from .configuration_owlvit import ( OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, OwlViTConfig, OwlViTOnnxConfig, OwlViTTextConfig, OwlViTVisionConfig, ) from .processing_owlvit import OwlViTProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_owlvit import OwlViTFeatureExtractor from .image_processing_owlvit import OwlViTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_owlvit import ( OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST, OwlViTForObjectDetection, OwlViTModel, OwlViTPreTrainedModel, OwlViTTextModel, OwlViTVisionModel, ) else: import sys UpperCamelCase_ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
303
"""simple docstring""" def UpperCamelCase ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) ->int: """simple docstring""" if n == 0: return 1 elif n % 2 == 1: return (binary_exponentiation(UpperCAmelCase , n - 1 , UpperCAmelCase ) * a) % mod else: a_ = binary_exponentiation(UpperCAmelCase , n / 2 , UpperCAmelCase ) return (b * b) % mod # a prime number UpperCamelCase_ = 701 UpperCamelCase_ = 1000000000 UpperCamelCase_ = 10 # using binary exponentiation function, O(log(p)): print((a / b) % p == (a * binary_exponentiation(b, p - 2, p)) % p) print((a / b) % p == (a * b ** (p - 2)) % p)
303
1