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
import gc import random import unittest import numpy as np import torch from transformers import CLIPImageProcessor, CLIPVisionConfig, CLIPVisionModel from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEImgaImgPipeline from diffusers.pipelines.shap_e import ShapERenderer from diffusers.utils import floats_tensor, load_image, load_numpy, slow from diffusers.utils.testing_utils import require_torch_gpu, torch_device from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference class UpperCAmelCase_ ( _a, unittest.TestCase): '''simple docstring''' __UpperCamelCase : int = ShapEImgaImgPipeline __UpperCamelCase : List[Any] = ["image"] __UpperCamelCase : Dict = ["image"] __UpperCamelCase : Any = [ "num_images_per_prompt", "num_inference_steps", "generator", "latents", "guidance_scale", "frame_size", "output_type", "return_dict", ] __UpperCamelCase : Dict = False @property def _lowercase ( self ): """simple docstring""" return 32 @property def _lowercase ( self ): """simple docstring""" return 32 @property def _lowercase ( self ): """simple docstring""" return self.time_input_dim * 4 @property def _lowercase ( self ): """simple docstring""" return 8 @property def _lowercase ( self ): """simple docstring""" torch.manual_seed(0 ) UpperCamelCase : Union[str, Any] = CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size , image_size=64 , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , num_attention_heads=4 , num_channels=3 , num_hidden_layers=5 , patch_size=1 , ) UpperCamelCase : List[Any] = CLIPVisionModel(__SCREAMING_SNAKE_CASE ) return model @property def _lowercase ( self ): """simple docstring""" UpperCamelCase : Union[str, Any] = CLIPImageProcessor( crop_size=224 , do_center_crop=__SCREAMING_SNAKE_CASE , do_normalize=__SCREAMING_SNAKE_CASE , do_resize=__SCREAMING_SNAKE_CASE , image_mean=[0.48_145_466, 0.4_578_275, 0.40_821_073] , image_std=[0.26_862_954, 0.26_130_258, 0.27_577_711] , resample=3 , size=224 , ) return image_processor @property def _lowercase ( self ): """simple docstring""" torch.manual_seed(0 ) UpperCamelCase : Union[str, Any] = { '''num_attention_heads''': 2, '''attention_head_dim''': 16, '''embedding_dim''': self.time_input_dim, '''num_embeddings''': 32, '''embedding_proj_dim''': self.text_embedder_hidden_size, '''time_embed_dim''': self.time_embed_dim, '''num_layers''': 1, '''clip_embed_dim''': self.time_input_dim * 2, '''additional_embeddings''': 0, '''time_embed_act_fn''': '''gelu''', '''norm_in_type''': '''layer''', '''embedding_proj_norm_type''': '''layer''', '''encoder_hid_proj_type''': None, '''added_emb_type''': None, } UpperCamelCase : List[str] = PriorTransformer(**__SCREAMING_SNAKE_CASE ) return model @property def _lowercase ( self ): """simple docstring""" torch.manual_seed(0 ) UpperCamelCase : str = { '''param_shapes''': ( (self.renderer_dim, 93), (self.renderer_dim, 8), (self.renderer_dim, 8), (self.renderer_dim, 8), ), '''d_latent''': self.time_input_dim, '''d_hidden''': self.renderer_dim, '''n_output''': 12, '''background''': ( 0.1, 0.1, 0.1, ), } UpperCamelCase : Dict = ShapERenderer(**__SCREAMING_SNAKE_CASE ) return model def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = self.dummy_prior UpperCamelCase : Optional[Any] = self.dummy_image_encoder UpperCamelCase : Any = self.dummy_image_processor UpperCamelCase : List[str] = self.dummy_renderer UpperCamelCase : int = HeunDiscreteScheduler( beta_schedule='''exp''' , num_train_timesteps=1_024 , prediction_type='''sample''' , use_karras_sigmas=__SCREAMING_SNAKE_CASE , clip_sample=__SCREAMING_SNAKE_CASE , clip_sample_range=1.0 , ) UpperCamelCase : Any = { '''prior''': prior, '''image_encoder''': image_encoder, '''image_processor''': image_processor, '''renderer''': renderer, '''scheduler''': scheduler, } return components def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=0 ): """simple docstring""" UpperCamelCase : List[str] = floats_tensor((1, 3, 64, 64) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) if str(__SCREAMING_SNAKE_CASE ).startswith('''mps''' ): UpperCamelCase : Dict = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: UpperCamelCase : List[Any] = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = { '''image''': input_image, '''generator''': generator, '''num_inference_steps''': 1, '''frame_size''': 32, '''output_type''': '''np''', } return inputs def _lowercase ( self ): """simple docstring""" UpperCamelCase : Any = '''cpu''' UpperCamelCase : Dict = self.get_dummy_components() UpperCamelCase : List[Any] = self.pipeline_class(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = pipe(**self.get_dummy_inputs(__SCREAMING_SNAKE_CASE ) ) UpperCamelCase : Tuple = output.images[0] UpperCamelCase : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (20, 32, 32, 3) UpperCamelCase : str = np.array( [ 0.00_039_216, 0.00_039_216, 0.00_039_216, 0.00_039_216, 0.00_039_216, 0.00_039_216, 0.00_039_216, 0.00_039_216, 0.00_039_216, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def _lowercase ( self ): """simple docstring""" self._test_inference_batch_consistent(batch_sizes=[1, 2] ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[int] = torch_device == '''cpu''' UpperCamelCase : int = True self._test_inference_batch_single_identical( batch_size=2 , test_max_difference=__SCREAMING_SNAKE_CASE , relax_max_difference=__SCREAMING_SNAKE_CASE , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[Any] = self.get_dummy_components() UpperCamelCase : Union[str, Any] = self.pipeline_class(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = 1 UpperCamelCase : int = 2 UpperCamelCase : Union[str, Any] = self.get_dummy_inputs(__SCREAMING_SNAKE_CASE ) for key in inputs.keys(): if key in self.batch_params: UpperCamelCase : int = batch_size * [inputs[key]] UpperCamelCase : Tuple = pipe(**__SCREAMING_SNAKE_CASE , num_images_per_prompt=__SCREAMING_SNAKE_CASE )[0] assert images.shape[0] == batch_size * num_images_per_prompt @slow @require_torch_gpu class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' def _lowercase ( self ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowercase ( self ): """simple docstring""" UpperCamelCase : Tuple = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/shap_e/corgi.png''' ) UpperCamelCase : Union[str, Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/shap_e/test_shap_e_img2img_out.npy''' ) UpperCamelCase : str = ShapEImgaImgPipeline.from_pretrained('''openai/shap-e-img2img''' ) UpperCamelCase : Optional[int] = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(0 ) UpperCamelCase : Union[str, Any] = pipe( __SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , guidance_scale=3.0 , num_inference_steps=64 , frame_size=64 , output_type='''np''' , ).images[0] assert images.shape == (20, 64, 64, 3) assert_mean_pixel_difference(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )
363
import collections import os import re from pathlib import Path __UpperCAmelCase : List[str] = "src/transformers" # Matches is_xxx_available() __UpperCAmelCase : int = re.compile(r"is\_([a-z_]*)_available()") # Catches a one-line _import_struct = {xxx} __UpperCAmelCase : Optional[int] = re.compile(r"^_import_structure\s+=\s+\{([^\}]+)\}") # Catches a line with a key-values pattern: "bla": ["foo", "bar"] __UpperCAmelCase : List[Any] = re.compile(r"\s+\"\S*\":\s+\[([^\]]*)\]") # Catches a line if not is_foo_available __UpperCAmelCase : List[Any] = re.compile(r"^\s*if\s+not\s+is\_[a-z_]*\_available\(\)") # Catches a line _import_struct["bla"].append("foo") __UpperCAmelCase : str = re.compile(r"^\s*_import_structure\[\"\S*\"\]\.append\(\"(\S*)\"\)") # Catches a line _import_struct["bla"].extend(["foo", "bar"]) or _import_struct["bla"] = ["foo", "bar"] __UpperCAmelCase : Union[str, Any] = re.compile(r"^\s*_import_structure\[\S*\](?:\.extend\(|\s*=\s+)\[([^\]]*)\]") # Catches a line with an object between quotes and a comma: "MyModel", __UpperCAmelCase : Dict = re.compile(r"^\s+\"([^\"]+)\",") # Catches a line with objects between brackets only: ["foo", "bar"], __UpperCAmelCase : str = re.compile(r"^\s+\[([^\]]+)\]") # Catches a line with from foo import bar, bla, boo __UpperCAmelCase : str = re.compile(r"\s+from\s+\S*\s+import\s+([^\(\s].*)\n") # Catches a line with try: __UpperCAmelCase : Any = re.compile(r"^\s*try:") # Catches a line with else: __UpperCAmelCase : List[Any] = re.compile(r"^\s*else:") def a ( SCREAMING_SNAKE_CASE_ : Dict ): """simple docstring""" if _re_test_backend.search(SCREAMING_SNAKE_CASE_ ) is None: return None UpperCamelCase : Union[str, Any] = [b[0] for b in _re_backend.findall(SCREAMING_SNAKE_CASE_ )] backends.sort() return "_and_".join(SCREAMING_SNAKE_CASE_ ) def a ( SCREAMING_SNAKE_CASE_ : Tuple ): """simple docstring""" with open(SCREAMING_SNAKE_CASE_ , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: UpperCamelCase : Tuple = f.readlines() UpperCamelCase : Tuple = 0 while line_index < len(SCREAMING_SNAKE_CASE_ ) and not lines[line_index].startswith('''_import_structure = {''' ): line_index += 1 # If this is a traditional init, just return. if line_index >= len(SCREAMING_SNAKE_CASE_ ): return None # First grab the objects without a specific backend in _import_structure UpperCamelCase : List[Any] = [] while not lines[line_index].startswith('''if TYPE_CHECKING''' ) and find_backend(lines[line_index] ) is None: UpperCamelCase : Optional[int] = lines[line_index] # If we have everything on a single line, let's deal with it. if _re_one_line_import_struct.search(SCREAMING_SNAKE_CASE_ ): UpperCamelCase : Optional[int] = _re_one_line_import_struct.search(SCREAMING_SNAKE_CASE_ ).groups()[0] UpperCamelCase : str = re.findall(R'''\[([^\]]+)\]''' , SCREAMING_SNAKE_CASE_ ) for imp in imports: objects.extend([obj[1:-1] for obj in imp.split(''', ''' )] ) line_index += 1 continue UpperCamelCase : List[Any] = _re_import_struct_key_value.search(SCREAMING_SNAKE_CASE_ ) if single_line_import_search is not None: UpperCamelCase : List[str] = [obj[1:-1] for obj in single_line_import_search.groups()[0].split(''', ''' ) if len(SCREAMING_SNAKE_CASE_ ) > 0] objects.extend(SCREAMING_SNAKE_CASE_ ) elif line.startswith(''' ''' * 8 + '''"''' ): objects.append(line[9:-3] ) line_index += 1 UpperCamelCase : Dict = {'''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. UpperCamelCase : Dict = 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: UpperCamelCase : Optional[Any] = 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 UpperCamelCase : str = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(''' ''' * 4 ): UpperCamelCase : str = lines[line_index] if _re_import_struct_add_one.search(SCREAMING_SNAKE_CASE_ ) is not None: objects.append(_re_import_struct_add_one.search(SCREAMING_SNAKE_CASE_ ).groups()[0] ) elif _re_import_struct_add_many.search(SCREAMING_SNAKE_CASE_ ) is not None: UpperCamelCase : Union[str, Any] = _re_import_struct_add_many.search(SCREAMING_SNAKE_CASE_ ).groups()[0].split(''', ''' ) UpperCamelCase : List[Any] = [obj[1:-1] for obj in imports if len(SCREAMING_SNAKE_CASE_ ) > 0] objects.extend(SCREAMING_SNAKE_CASE_ ) elif _re_between_brackets.search(SCREAMING_SNAKE_CASE_ ) is not None: UpperCamelCase : str = _re_between_brackets.search(SCREAMING_SNAKE_CASE_ ).groups()[0].split(''', ''' ) UpperCamelCase : Dict = [obj[1:-1] for obj in imports if len(SCREAMING_SNAKE_CASE_ ) > 0] objects.extend(SCREAMING_SNAKE_CASE_ ) elif _re_quote_object.search(SCREAMING_SNAKE_CASE_ ) is not None: objects.append(_re_quote_object.search(SCREAMING_SNAKE_CASE_ ).groups()[0] ) elif line.startswith(''' ''' * 8 + '''"''' ): objects.append(line[9:-3] ) elif line.startswith(''' ''' * 1_2 + '''"''' ): objects.append(line[1_3:-3] ) line_index += 1 UpperCamelCase : Tuple = objects else: line_index += 1 # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend UpperCamelCase : int = [] while ( line_index < len(SCREAMING_SNAKE_CASE_ ) and find_backend(lines[line_index] ) is None and not lines[line_index].startswith('''else''' ) ): UpperCamelCase : Tuple = lines[line_index] UpperCamelCase : Any = _re_import.search(SCREAMING_SNAKE_CASE_ ) 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 UpperCamelCase : Any = {'''none''': objects} # Let's continue with backend-specific objects while line_index < len(SCREAMING_SNAKE_CASE_ ): # If the line is an if is_backend_available, we grab all objects associated. UpperCamelCase : Optional[Any] = 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: UpperCamelCase : Dict = 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 UpperCamelCase : Dict = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(''' ''' * 8 ): UpperCamelCase : Optional[Any] = lines[line_index] UpperCamelCase : str = _re_import.search(SCREAMING_SNAKE_CASE_ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(''', ''' ) ) elif line.startswith(''' ''' * 1_2 ): objects.append(line[1_2:-2] ) line_index += 1 UpperCamelCase : str = objects else: line_index += 1 return import_dict_objects, type_hint_objects def a ( SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : List[Any] ): """simple docstring""" def find_duplicates(SCREAMING_SNAKE_CASE_ : Any ): return [k for k, v in collections.Counter(SCREAMING_SNAKE_CASE_ ).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!"] UpperCamelCase : Dict = [] for key in import_dict_objects.keys(): UpperCamelCase : Union[str, Any] = find_duplicates(import_dict_objects[key] ) if duplicate_imports: errors.append(F"""Duplicate _import_structure definitions for: {duplicate_imports}""" ) UpperCamelCase : Dict = 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] ) ): UpperCamelCase : List[str] = '''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 ( ): """simple docstring""" UpperCamelCase : Any = [] for root, _, files in os.walk(SCREAMING_SNAKE_CASE_ ): if "__init__.py" in files: UpperCamelCase : int = os.path.join(SCREAMING_SNAKE_CASE_ , '''__init__.py''' ) UpperCamelCase : Optional[int] = parse_init(SCREAMING_SNAKE_CASE_ ) if objects is not None: UpperCamelCase : str = analyze_results(*SCREAMING_SNAKE_CASE_ ) if len(SCREAMING_SNAKE_CASE_ ) > 0: UpperCamelCase : List[Any] = F"""Problem in {fname}, both halves do not define the same objects.\n{errors[0]}""" failures.append('''\n'''.join(SCREAMING_SNAKE_CASE_ ) ) if len(SCREAMING_SNAKE_CASE_ ) > 0: raise ValueError('''\n\n'''.join(SCREAMING_SNAKE_CASE_ ) ) def a ( ): """simple docstring""" UpperCamelCase : Dict = [] for path, directories, files in os.walk(SCREAMING_SNAKE_CASE_ ): for folder in directories: # Ignore private modules if folder.startswith('''_''' ): directories.remove(SCREAMING_SNAKE_CASE_ ) continue # Ignore leftovers from branches (empty folders apart from pycache) if len(list((Path(SCREAMING_SNAKE_CASE_ ) / folder).glob('''*.py''' ) ) ) == 0: continue UpperCamelCase : List[str] = str((Path(SCREAMING_SNAKE_CASE_ ) / folder).relative_to(SCREAMING_SNAKE_CASE_ ) ) UpperCamelCase : str = short_path.replace(os.path.sep , '''.''' ) submodules.append(SCREAMING_SNAKE_CASE_ ) for fname in files: if fname == "__init__.py": continue UpperCamelCase : Tuple = str((Path(SCREAMING_SNAKE_CASE_ ) / fname).relative_to(SCREAMING_SNAKE_CASE_ ) ) UpperCamelCase : int = short_path.replace('''.py''' , '''''' ).replace(os.path.sep , '''.''' ) if len(submodule.split('''.''' ) ) == 1: submodules.append(SCREAMING_SNAKE_CASE_ ) return submodules __UpperCAmelCase : Optional[int] = [ "convert_pytorch_checkpoint_to_tf2", "modeling_flax_pytorch_utils", "models.esm.openfold_utils", ] def a ( ): """simple docstring""" from transformers.utils import direct_transformers_import UpperCamelCase : Tuple = direct_transformers_import(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : str = set(transformers._import_structure.keys() ) # This contains all the base keys of the _import_structure object defined in the init, but if the user is missing # some optional dependencies, they may not have all of them. Thus we read the init to read all additions and # (potentiall re-) add them. with open(os.path.join(SCREAMING_SNAKE_CASE_ , '''__init__.py''' ) , '''r''' ) as f: UpperCamelCase : List[Any] = f.read() import_structure_keys.update(set(re.findall(R'''import_structure\[\"([^\"]*)\"\]''' , SCREAMING_SNAKE_CASE_ ) ) ) UpperCamelCase : Union[str, Any] = [ module for module in get_transformers_submodules() if module not in IGNORE_SUBMODULES and module not in import_structure_keys ] if len(SCREAMING_SNAKE_CASE_ ) > 0: UpperCamelCase : str = '''\n'''.join(F"""- {module}""" for module in module_not_registered ) raise ValueError( '''The following submodules are not properly registed 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()
315
0
import os from shutil import copyfile from typing import List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging __UpperCAmelCase : List[str] = logging.get_logger(__name__) __UpperCAmelCase : List[Any] = {"vocab_file": "sentencepiece.model"} __UpperCAmelCase : int = { "vocab_file": { "google/rembert": "https://huggingface.co/google/rembert/resolve/main/sentencepiece.model", }, } __UpperCAmelCase : int = { "google/rembert": 256, } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Optional[int] = VOCAB_FILES_NAMES __UpperCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE="[CLS]" , __SCREAMING_SNAKE_CASE="[SEP]" , __SCREAMING_SNAKE_CASE="[UNK]" , __SCREAMING_SNAKE_CASE="[SEP]" , __SCREAMING_SNAKE_CASE="[PAD]" , __SCREAMING_SNAKE_CASE="[CLS]" , __SCREAMING_SNAKE_CASE="[MASK]" , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" super().__init__( do_lower_case=__SCREAMING_SNAKE_CASE , remove_space=__SCREAMING_SNAKE_CASE , keep_accents=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) UpperCamelCase : Optional[int] = do_lower_case UpperCamelCase : List[str] = remove_space UpperCamelCase : int = keep_accents UpperCamelCase : int = vocab_file UpperCamelCase : List[str] = spm.SentencePieceProcessor() self.sp_model.Load(__SCREAMING_SNAKE_CASE ) @property def _lowercase ( self ): """simple docstring""" return len(self.sp_model ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[int] = {self.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ): """simple docstring""" UpperCamelCase : List[str] = self.__dict__.copy() UpperCamelCase : List[Any] = None return state def __setstate__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[int] = d UpperCamelCase : Union[str, Any] = spm.SentencePieceProcessor() self.sp_model.Load(self.vocab_file ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=False ): """simple docstring""" UpperCamelCase : Tuple = self.sp_model.EncodeAsPieces(__SCREAMING_SNAKE_CASE ) return pieces def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return self.sp_model.PieceToId(__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return self.sp_model.IdToPiece(__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Dict = self.sp_model.decode_pieces(__SCREAMING_SNAKE_CASE ) return out_string def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : List[str] = [self.sep_token_id] UpperCamelCase : Optional[int] = [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 _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = False ): """simple docstring""" 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 x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : Optional[Any] = [self.sep_token_id] UpperCamelCase : Union[str, Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" if not os.path.isdir(__SCREAMING_SNAKE_CASE ): logger.error('''Vocabulary path ({}) should be a directory'''.format(__SCREAMING_SNAKE_CASE ) ) return UpperCamelCase : str = os.path.join( __SCREAMING_SNAKE_CASE , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__SCREAMING_SNAKE_CASE ): copyfile(self.vocab_file , __SCREAMING_SNAKE_CASE ) return (out_vocab_file,)
364
def a ( SCREAMING_SNAKE_CASE_ : str = "The quick brown fox jumps over the lazy dog" , ): """simple docstring""" UpperCamelCase : Any = set() # Replace all the whitespace in our sentence UpperCamelCase : Union[str, Any] = input_str.replace(''' ''' , '''''' ) for alpha in input_str: if "a" <= alpha.lower() <= "z": frequency.add(alpha.lower() ) return len(SCREAMING_SNAKE_CASE_ ) == 2_6 def a ( SCREAMING_SNAKE_CASE_ : str = "The quick brown fox jumps over the lazy dog" , ): """simple docstring""" UpperCamelCase : str = [False] * 2_6 for char in input_str: if char.islower(): UpperCamelCase : List[Any] = True elif char.isupper(): UpperCamelCase : List[Any] = True return all(SCREAMING_SNAKE_CASE_ ) def a ( SCREAMING_SNAKE_CASE_ : str = "The quick brown fox jumps over the lazy dog" , ): """simple docstring""" return len({char for char in input_str.lower() if char.isalpha()} ) == 2_6 def a ( ): """simple docstring""" from timeit import timeit UpperCamelCase : int = '''from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest''' print(timeit('''is_pangram()''' , setup=SCREAMING_SNAKE_CASE_ ) ) print(timeit('''is_pangram_faster()''' , setup=SCREAMING_SNAKE_CASE_ ) ) print(timeit('''is_pangram_fastest()''' , setup=SCREAMING_SNAKE_CASE_ ) ) # 5.348480500048026, 2.6477354579837993, 1.8470395830227062 # 5.036091582966037, 2.644472333951853, 1.8869528750656173 if __name__ == "__main__": import doctest doctest.testmod() benchmark()
315
0
from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCAmelCase : int = logging.get_logger(__name__) __UpperCAmelCase : Dict = { "sayakpaul/vit-msn-base": "https://huggingface.co/sayakpaul/vit-msn-base/resolve/main/config.json", # See all ViT MSN models at https://huggingface.co/models?filter=vit_msn } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Tuple = "vit_msn" def __init__( self , __SCREAMING_SNAKE_CASE=768 , __SCREAMING_SNAKE_CASE=12 , __SCREAMING_SNAKE_CASE=12 , __SCREAMING_SNAKE_CASE=3_072 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=1e-06 , __SCREAMING_SNAKE_CASE=224 , __SCREAMING_SNAKE_CASE=16 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=True , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" super().__init__(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = hidden_size UpperCamelCase : List[Any] = num_hidden_layers UpperCamelCase : Tuple = num_attention_heads UpperCamelCase : Tuple = intermediate_size UpperCamelCase : List[Any] = hidden_act UpperCamelCase : Optional[Any] = hidden_dropout_prob UpperCamelCase : Dict = attention_probs_dropout_prob UpperCamelCase : str = initializer_range UpperCamelCase : List[str] = layer_norm_eps UpperCamelCase : Dict = image_size UpperCamelCase : Any = patch_size UpperCamelCase : Optional[Any] = num_channels UpperCamelCase : Dict = qkv_bias
365
import argparse import logging import sys from unittest.mock import patch import run_glue_deebert from transformers.testing_utils import TestCasePlus, get_gpu_count, require_torch_non_multi_gpu, slow logging.basicConfig(level=logging.DEBUG) __UpperCAmelCase : Union[str, Any] = logging.getLogger() def a ( ): """simple docstring""" UpperCamelCase : List[Any] = argparse.ArgumentParser() parser.add_argument('''-f''' ) UpperCamelCase : List[str] = parser.parse_args() return args.f class UpperCAmelCase_ ( _a): '''simple docstring''' def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = logging.StreamHandler(sys.stdout ) logger.addHandler(__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Dict = get_gpu_count() if n_gpu > 1: pass # XXX: doesn't quite work with n_gpu > 1 https://github.com/huggingface/transformers/issues/10560 # script = f"{self.examples_dir_str}/research_projects/deebert/run_glue_deebert.py" # distributed_args = f"-m torch.distributed.launch --nproc_per_node={n_gpu} {script}".split() # cmd = [sys.executable] + distributed_args + args # execute_subprocess_async(cmd, env=self.get_env()) # XXX: test the results - need to save them first into .json file else: args.insert(0 , '''run_glue_deebert.py''' ) with patch.object(__SCREAMING_SNAKE_CASE , '''argv''' , __SCREAMING_SNAKE_CASE ): UpperCamelCase : int = run_glue_deebert.main() for value in result.values(): self.assertGreaterEqual(__SCREAMING_SNAKE_CASE , 0.666 ) @slow @require_torch_non_multi_gpu def _lowercase ( self ): """simple docstring""" UpperCamelCase : Any = ''' --model_type roberta --model_name_or_path roberta-base --task_name MRPC --do_train --do_eval --do_lower_case --data_dir ./tests/fixtures/tests_samples/MRPC/ --max_seq_length 128 --per_gpu_eval_batch_size=1 --per_gpu_train_batch_size=8 --learning_rate 2e-4 --num_train_epochs 3 --overwrite_output_dir --seed 42 --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --plot_data_dir ./examples/deebert/results/ --save_steps 0 --overwrite_cache --eval_after_first_stage '''.split() self.run_and_check(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = ''' --model_type roberta --model_name_or_path ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --task_name MRPC --do_eval --do_lower_case --data_dir ./tests/fixtures/tests_samples/MRPC/ --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --plot_data_dir ./examples/deebert/results/ --max_seq_length 128 --eval_each_highway --eval_highway --overwrite_cache --per_gpu_eval_batch_size=1 '''.split() self.run_and_check(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = ''' --model_type roberta --model_name_or_path ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --task_name MRPC --do_eval --do_lower_case --data_dir ./tests/fixtures/tests_samples/MRPC/ --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --plot_data_dir ./examples/deebert/results/ --max_seq_length 128 --early_exit_entropy 0.1 --eval_highway --overwrite_cache --per_gpu_eval_batch_size=1 '''.split() self.run_and_check(__SCREAMING_SNAKE_CASE )
315
0
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 __UpperCAmelCase : 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) __UpperCAmelCase : str = logging.getLogger() def a ( ): """simple docstring""" UpperCamelCase : Optional[int] = argparse.ArgumentParser() parser.add_argument('''-f''' ) UpperCamelCase : int = parser.parse_args() return args.f def a ( SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : str="eval" ): """simple docstring""" UpperCamelCase : List[Any] = os.path.join(SCREAMING_SNAKE_CASE_ , F"""{split}_results.json""" ) if os.path.exists(SCREAMING_SNAKE_CASE_ ): with open(SCREAMING_SNAKE_CASE_ , '''r''' ) as f: return json.load(SCREAMING_SNAKE_CASE_ ) raise ValueError(F"""can't find {path}""" ) __UpperCAmelCase : str = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class UpperCAmelCase_ ( _a): '''simple docstring''' def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[Any] = self.get_auto_remove_tmp_dir() UpperCamelCase : Any = f""" run_glue.py --model_name_or_path distilbert-base-uncased --output_dir {tmp_dir} --train_file ./tests/fixtures/tests_samples/MRPC/train.csv --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --learning_rate=1e-4 --eval_steps=2 --warmup_steps=2 --seed=42 --max_seq_length=128 """.split() with patch.object(__SCREAMING_SNAKE_CASE , '''argv''' , __SCREAMING_SNAKE_CASE ): run_flax_glue.main() UpperCamelCase : str = get_results(__SCREAMING_SNAKE_CASE ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.75 ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = self.get_auto_remove_tmp_dir() UpperCamelCase : str = f""" run_clm_flax.py --model_name_or_path distilgpt2 --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --do_train --do_eval --block_size 128 --per_device_train_batch_size 4 --per_device_eval_batch_size 4 --num_train_epochs 2 --logging_steps 2 --eval_steps 2 --output_dir {tmp_dir} --overwrite_output_dir """.split() with patch.object(__SCREAMING_SNAKE_CASE , '''argv''' , __SCREAMING_SNAKE_CASE ): run_clm_flax.main() UpperCamelCase : Optional[int] = get_results(__SCREAMING_SNAKE_CASE ) self.assertLess(result['''eval_perplexity'''] , 100 ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = self.get_auto_remove_tmp_dir() UpperCamelCase : List[str] = f""" run_summarization.py --model_name_or_path t5-small --train_file tests/fixtures/tests_samples/xsum/sample.json --validation_file tests/fixtures/tests_samples/xsum/sample.json --test_file tests/fixtures/tests_samples/xsum/sample.json --output_dir {tmp_dir} --overwrite_output_dir --num_train_epochs=3 --warmup_steps=8 --do_train --do_eval --do_predict --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --predict_with_generate """.split() with patch.object(__SCREAMING_SNAKE_CASE , '''argv''' , __SCREAMING_SNAKE_CASE ): run_summarization_flax.main() UpperCamelCase : Dict = get_results(__SCREAMING_SNAKE_CASE , split='''test''' ) self.assertGreaterEqual(result['''test_rouge1'''] , 10 ) self.assertGreaterEqual(result['''test_rouge2'''] , 2 ) self.assertGreaterEqual(result['''test_rougeL'''] , 7 ) self.assertGreaterEqual(result['''test_rougeLsum'''] , 7 ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[int] = self.get_auto_remove_tmp_dir() UpperCamelCase : Tuple = f""" run_mlm.py --model_name_or_path distilroberta-base --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --output_dir {tmp_dir} --overwrite_output_dir --max_seq_length 128 --per_device_train_batch_size 4 --per_device_eval_batch_size 4 --logging_steps 2 --eval_steps 2 --do_train --do_eval --num_train_epochs=1 """.split() with patch.object(__SCREAMING_SNAKE_CASE , '''argv''' , __SCREAMING_SNAKE_CASE ): run_mlm_flax.main() UpperCamelCase : Dict = get_results(__SCREAMING_SNAKE_CASE ) self.assertLess(result['''eval_perplexity'''] , 42 ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = self.get_auto_remove_tmp_dir() UpperCamelCase : int = f""" run_t5_mlm_flax.py --model_name_or_path t5-small --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --do_train --do_eval --max_seq_length 128 --per_device_train_batch_size 4 --per_device_eval_batch_size 4 --num_train_epochs 2 --logging_steps 2 --eval_steps 2 --output_dir {tmp_dir} --overwrite_output_dir """.split() with patch.object(__SCREAMING_SNAKE_CASE , '''argv''' , __SCREAMING_SNAKE_CASE ): run_ta_mlm_flax.main() UpperCamelCase : str = get_results(__SCREAMING_SNAKE_CASE ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.42 ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : Any = 7 if get_gpu_count() > 1 else 2 UpperCamelCase : Optional[Any] = self.get_auto_remove_tmp_dir() UpperCamelCase : Tuple = f""" run_flax_ner.py --model_name_or_path bert-base-uncased --train_file tests/fixtures/tests_samples/conll/sample.json --validation_file tests/fixtures/tests_samples/conll/sample.json --output_dir {tmp_dir} --overwrite_output_dir --do_train --do_eval --warmup_steps=2 --learning_rate=2e-4 --logging_steps 2 --eval_steps 2 --per_device_train_batch_size=2 --per_device_eval_batch_size=2 --num_train_epochs={epochs} --seed 7 """.split() with patch.object(__SCREAMING_SNAKE_CASE , '''argv''' , __SCREAMING_SNAKE_CASE ): run_flax_ner.main() UpperCamelCase : int = get_results(__SCREAMING_SNAKE_CASE ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.75 ) self.assertGreaterEqual(result['''eval_f1'''] , 0.3 ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = self.get_auto_remove_tmp_dir() UpperCamelCase : List[str] = f""" run_qa.py --model_name_or_path bert-base-uncased --version_2_with_negative --train_file tests/fixtures/tests_samples/SQUAD/sample.json --validation_file tests/fixtures/tests_samples/SQUAD/sample.json --output_dir {tmp_dir} --overwrite_output_dir --num_train_epochs=3 --warmup_steps=2 --do_train --do_eval --logging_steps 2 --eval_steps 2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 """.split() with patch.object(__SCREAMING_SNAKE_CASE , '''argv''' , __SCREAMING_SNAKE_CASE ): run_qa.main() UpperCamelCase : List[str] = get_results(__SCREAMING_SNAKE_CASE ) self.assertGreaterEqual(result['''eval_f1'''] , 30 ) self.assertGreaterEqual(result['''eval_exact'''] , 30 )
366
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __UpperCAmelCase : Tuple = logging.get_logger(__name__) __UpperCAmelCase : Union[str, Any] = { "kssteven/ibert-roberta-base": "https://huggingface.co/kssteven/ibert-roberta-base/resolve/main/config.json", "kssteven/ibert-roberta-large": "https://huggingface.co/kssteven/ibert-roberta-large/resolve/main/config.json", "kssteven/ibert-roberta-large-mnli": ( "https://huggingface.co/kssteven/ibert-roberta-large-mnli/resolve/main/config.json" ), } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : List[Any] = "ibert" def __init__( self , __SCREAMING_SNAKE_CASE=30_522 , __SCREAMING_SNAKE_CASE=768 , __SCREAMING_SNAKE_CASE=12 , __SCREAMING_SNAKE_CASE=12 , __SCREAMING_SNAKE_CASE=3_072 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=512 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=1e-12 , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=0 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE="absolute" , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE="none" , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" super().__init__(pad_token_id=__SCREAMING_SNAKE_CASE , bos_token_id=__SCREAMING_SNAKE_CASE , eos_token_id=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = vocab_size UpperCamelCase : Optional[int] = hidden_size UpperCamelCase : Tuple = num_hidden_layers UpperCamelCase : Optional[Any] = num_attention_heads UpperCamelCase : Dict = hidden_act UpperCamelCase : Union[str, Any] = intermediate_size UpperCamelCase : str = hidden_dropout_prob UpperCamelCase : Any = attention_probs_dropout_prob UpperCamelCase : Dict = max_position_embeddings UpperCamelCase : Union[str, Any] = type_vocab_size UpperCamelCase : Optional[Any] = initializer_range UpperCamelCase : Union[str, Any] = layer_norm_eps UpperCamelCase : Dict = position_embedding_type UpperCamelCase : int = quant_mode UpperCamelCase : Any = force_dequant class UpperCAmelCase_ ( _a): '''simple docstring''' @property def _lowercase ( self ): """simple docstring""" if self.task == "multiple-choice": UpperCamelCase : int = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: UpperCamelCase : Optional[int] = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ] )
315
0
import itertools from dataclasses import dataclass from typing import Optional import pandas as pd import pyarrow as pa import datasets from datasets.table import table_cast @dataclass class UpperCAmelCase_ ( datasets.BuilderConfig): '''simple docstring''' __UpperCamelCase : Optional[datasets.Features] = None class UpperCAmelCase_ ( datasets.ArrowBasedBuilder): '''simple docstring''' __UpperCamelCase : Tuple = PandasConfig def _lowercase ( self ): """simple docstring""" return datasets.DatasetInfo(features=self.config.features ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if not self.config.data_files: raise ValueError(f"""At least one data file must be specified, but got data_files={self.config.data_files}""" ) UpperCamelCase : Dict = dl_manager.download_and_extract(self.config.data_files ) if isinstance(__SCREAMING_SNAKE_CASE , (str, list, tuple) ): UpperCamelCase : List[Any] = data_files if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): UpperCamelCase : Tuple = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive UpperCamelCase : List[Any] = [dl_manager.iter_files(__SCREAMING_SNAKE_CASE ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'''files''': files} )] UpperCamelCase : Dict = [] for split_name, files in data_files.items(): if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): UpperCamelCase : List[str] = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive UpperCamelCase : Dict = [dl_manager.iter_files(__SCREAMING_SNAKE_CASE ) for file in files] splits.append(datasets.SplitGenerator(name=__SCREAMING_SNAKE_CASE , gen_kwargs={'''files''': files} ) ) return splits def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if self.config.features is not None: # more expensive cast to support nested features with keys in a different order # allows str <-> int/float or str to Audio for example UpperCamelCase : Optional[Any] = table_cast(__SCREAMING_SNAKE_CASE , self.config.features.arrow_schema ) return pa_table def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" for i, file in enumerate(itertools.chain.from_iterable(__SCREAMING_SNAKE_CASE ) ): with open(__SCREAMING_SNAKE_CASE , '''rb''' ) as f: UpperCamelCase : List[Any] = pa.Table.from_pandas(pd.read_pickle(__SCREAMING_SNAKE_CASE ) ) yield i, self._cast_table(__SCREAMING_SNAKE_CASE )
367
import html from ...feature_extraction_utils import BatchFeature, FeatureExtractionMixin from ...utils import is_bsa_available, logging, requires_backends if is_bsa_available(): import bsa from bsa import BeautifulSoup __UpperCAmelCase : int = logging.get_logger(__name__) class UpperCAmelCase_ ( _a): '''simple docstring''' def __init__( self , **__SCREAMING_SNAKE_CASE ): """simple docstring""" requires_backends(self , ['''bs4'''] ) super().__init__(**__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[Any] = [] UpperCamelCase : int = [] UpperCamelCase : List[Any] = element if element.name else element.parent for parent in child.parents: # type: bs4.element.Tag UpperCamelCase : Tuple = parent.find_all(child.name , recursive=__SCREAMING_SNAKE_CASE ) xpath_tags.append(child.name ) xpath_subscripts.append( 0 if 1 == len(__SCREAMING_SNAKE_CASE ) else next(i for i, s in enumerate(__SCREAMING_SNAKE_CASE , 1 ) if s is child ) ) UpperCamelCase : Optional[Any] = parent xpath_tags.reverse() xpath_subscripts.reverse() return xpath_tags, xpath_subscripts def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[Any] = BeautifulSoup(__SCREAMING_SNAKE_CASE , '''html.parser''' ) UpperCamelCase : Union[str, Any] = [] UpperCamelCase : List[str] = [] UpperCamelCase : str = [] for element in html_code.descendants: if type(__SCREAMING_SNAKE_CASE ) == bsa.element.NavigableString: if type(element.parent ) != bsa.element.Tag: continue UpperCamelCase : Any = html.unescape(__SCREAMING_SNAKE_CASE ).strip() if not text_in_this_tag: continue all_doc_strings.append(__SCREAMING_SNAKE_CASE ) UpperCamelCase , UpperCamelCase : int = self.xpath_soup(__SCREAMING_SNAKE_CASE ) stringaxtag_seq.append(__SCREAMING_SNAKE_CASE ) stringaxsubs_seq.append(__SCREAMING_SNAKE_CASE ) if len(__SCREAMING_SNAKE_CASE ) != len(__SCREAMING_SNAKE_CASE ): raise ValueError('''Number of doc strings and xtags does not correspond''' ) if len(__SCREAMING_SNAKE_CASE ) != len(__SCREAMING_SNAKE_CASE ): raise ValueError('''Number of doc strings and xsubs does not correspond''' ) return all_doc_strings, stringaxtag_seq, stringaxsubs_seq def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[Any] = '''''' for tagname, subs in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): xpath += f"""/{tagname}""" if subs != 0: xpath += f"""[{subs}]""" return xpath def __call__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = False # Check that strings has a valid type if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): UpperCamelCase : Optional[Any] = True elif isinstance(__SCREAMING_SNAKE_CASE , (list, tuple) ): if len(__SCREAMING_SNAKE_CASE ) == 0 or isinstance(html_strings[0] , __SCREAMING_SNAKE_CASE ): UpperCamelCase : List[str] = True if not valid_strings: raise ValueError( '''HTML strings must of type `str`, `List[str]` (batch of examples), ''' f"""but is of type {type(__SCREAMING_SNAKE_CASE )}.""" ) UpperCamelCase : int = bool(isinstance(__SCREAMING_SNAKE_CASE , (list, tuple) ) and (isinstance(html_strings[0] , __SCREAMING_SNAKE_CASE )) ) if not is_batched: UpperCamelCase : Union[str, Any] = [html_strings] # Get nodes + xpaths UpperCamelCase : str = [] UpperCamelCase : int = [] for html_string in html_strings: UpperCamelCase , UpperCamelCase , UpperCamelCase : Dict = self.get_three_from_single(__SCREAMING_SNAKE_CASE ) nodes.append(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = [] for node, tag_list, sub_list in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): UpperCamelCase : str = self.construct_xpath(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) xpath_strings.append(__SCREAMING_SNAKE_CASE ) xpaths.append(__SCREAMING_SNAKE_CASE ) # return as Dict UpperCamelCase : List[str] = {'''nodes''': nodes, '''xpaths''': xpaths} UpperCamelCase : List[Any] = BatchFeature(data=__SCREAMING_SNAKE_CASE , tensor_type=__SCREAMING_SNAKE_CASE ) return encoded_inputs
315
0
def a ( SCREAMING_SNAKE_CASE_ : int = 1_0_0_0 ): """simple docstring""" return sum(2 * a * ((a - 1) // 2) for a in range(3 , n + 1 ) ) if __name__ == "__main__": print(solution())
368
import argparse import datetime import json import time import warnings from logging import getLogger from pathlib import Path from typing import Dict, List import torch from tqdm import tqdm from transformers import AutoModelForSeqaSeqLM, AutoTokenizer from utils import calculate_bleu, calculate_rouge, chunks, parse_numeric_n_bool_cl_kwargs, use_task_specific_params __UpperCAmelCase : List[str] = getLogger(__name__) __UpperCAmelCase : Optional[Any] = "cuda" if torch.cuda.is_available() else "cpu" def a ( SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : int = 8 , SCREAMING_SNAKE_CASE_ : str = DEFAULT_DEVICE , SCREAMING_SNAKE_CASE_ : List[Any]=False , SCREAMING_SNAKE_CASE_ : int="summarization" , SCREAMING_SNAKE_CASE_ : int=None , **SCREAMING_SNAKE_CASE_ : Any , ): """simple docstring""" UpperCamelCase : Dict = Path(SCREAMING_SNAKE_CASE_ ).open('''w''' , encoding='''utf-8''' ) UpperCamelCase : List[Any] = str(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[Any] = AutoModelForSeqaSeqLM.from_pretrained(SCREAMING_SNAKE_CASE_ ).to(SCREAMING_SNAKE_CASE_ ) if fpaa: UpperCamelCase : List[Any] = model.half() UpperCamelCase : str = AutoTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_ ) logger.info(F"""Inferred tokenizer type: {tokenizer.__class__}""" ) # if this is wrong, check config.model_type. UpperCamelCase : int = time.time() # update config with task specific params use_task_specific_params(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if prefix is None: UpperCamelCase : Union[str, Any] = prefix or getattr(model.config , '''prefix''' , '''''' ) or '''''' for examples_chunk in tqdm(list(chunks(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) ): UpperCamelCase : Optional[int] = [prefix + text for text in examples_chunk] UpperCamelCase : Union[str, Any] = tokenizer(SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' , truncation=SCREAMING_SNAKE_CASE_ , padding='''longest''' ).to(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Any = model.generate( input_ids=batch.input_ids , attention_mask=batch.attention_mask , **SCREAMING_SNAKE_CASE_ , ) UpperCamelCase : List[str] = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ , skip_special_tokens=SCREAMING_SNAKE_CASE_ , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ) for hypothesis in dec: fout.write(hypothesis + '''\n''' ) fout.flush() fout.close() UpperCamelCase : str = int(time.time() - start_time ) # seconds UpperCamelCase : Any = len(SCREAMING_SNAKE_CASE_ ) return {"n_obs": n_obs, "runtime": runtime, "seconds_per_sample": round(runtime / n_obs , 4 )} def a ( ): """simple docstring""" return datetime.datetime.now().strftime('''%Y-%m-%d %H:%M:%S''' ) def a ( SCREAMING_SNAKE_CASE_ : Union[str, Any]=True ): """simple docstring""" UpperCamelCase : int = argparse.ArgumentParser() parser.add_argument('''model_name''' , type=SCREAMING_SNAKE_CASE_ , help='''like facebook/bart-large-cnn,t5-base, etc.''' ) parser.add_argument('''input_path''' , type=SCREAMING_SNAKE_CASE_ , help='''like cnn_dm/test.source''' ) parser.add_argument('''save_path''' , type=SCREAMING_SNAKE_CASE_ , help='''where to save summaries''' ) parser.add_argument('''--reference_path''' , type=SCREAMING_SNAKE_CASE_ , required=SCREAMING_SNAKE_CASE_ , help='''like cnn_dm/test.target''' ) parser.add_argument('''--score_path''' , type=SCREAMING_SNAKE_CASE_ , required=SCREAMING_SNAKE_CASE_ , default='''metrics.json''' , help='''where to save metrics''' ) parser.add_argument('''--device''' , type=SCREAMING_SNAKE_CASE_ , required=SCREAMING_SNAKE_CASE_ , default=SCREAMING_SNAKE_CASE_ , help='''cuda, cuda:1, cpu etc.''' ) parser.add_argument( '''--prefix''' , type=SCREAMING_SNAKE_CASE_ , required=SCREAMING_SNAKE_CASE_ , default=SCREAMING_SNAKE_CASE_ , help='''will be added to the begininng of src examples''' ) parser.add_argument('''--task''' , type=SCREAMING_SNAKE_CASE_ , default='''summarization''' , help='''used for task_specific_params + metrics''' ) parser.add_argument('''--bs''' , type=SCREAMING_SNAKE_CASE_ , default=8 , required=SCREAMING_SNAKE_CASE_ , help='''batch size''' ) parser.add_argument( '''--n_obs''' , type=SCREAMING_SNAKE_CASE_ , default=-1 , required=SCREAMING_SNAKE_CASE_ , help='''How many observations. Defaults to all.''' ) parser.add_argument('''--fp16''' , action='''store_true''' ) parser.add_argument('''--dump-args''' , action='''store_true''' , help='''print the custom hparams with the results''' ) parser.add_argument( '''--info''' , nargs='''?''' , type=SCREAMING_SNAKE_CASE_ , const=datetime_now() , help=( '''use in conjunction w/ --dump-args to print with the results whatever other info you\'d like, e.g.''' ''' lang=en-ru. If no value is passed, the current datetime string will be used.''' ) , ) # Unspecified args like --num_beams=2 --decoder_start_token_id=4 are passed to model.generate UpperCamelCase , UpperCamelCase : int = parser.parse_known_args() UpperCamelCase : str = parse_numeric_n_bool_cl_kwargs(SCREAMING_SNAKE_CASE_ ) if parsed_args and verbose: print(F"""parsed the following generate kwargs: {parsed_args}""" ) UpperCamelCase : str = [''' ''' + x.rstrip() if '''t5''' in args.model_name else x.rstrip() for x in open(args.input_path ).readlines()] if args.n_obs > 0: UpperCamelCase : Tuple = examples[: args.n_obs] Path(args.save_path ).parent.mkdir(exist_ok=SCREAMING_SNAKE_CASE_ ) if args.reference_path is None and Path(args.score_path ).exists(): warnings.warn(F"""score_path {args.score_path} will be overwritten unless you type ctrl-c.""" ) if args.device == "cpu" and args.fpaa: # this mix leads to RuntimeError: "threshold_cpu" not implemented for 'Half' raise ValueError('''Can\'t mix --fp16 and --device cpu''' ) UpperCamelCase : str = generate_summaries_or_translations( SCREAMING_SNAKE_CASE_ , args.save_path , args.model_name , batch_size=args.bs , device=args.device , fpaa=args.fpaa , task=args.task , prefix=args.prefix , **SCREAMING_SNAKE_CASE_ , ) if args.reference_path is None: return {} # Compute scores UpperCamelCase : Tuple = calculate_bleu if '''translation''' in args.task else calculate_rouge UpperCamelCase : Dict = [x.rstrip() for x in open(args.save_path ).readlines()] UpperCamelCase : Optional[int] = [x.rstrip() for x in open(args.reference_path ).readlines()][: len(SCREAMING_SNAKE_CASE_ )] UpperCamelCase : dict = score_fn(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) scores.update(SCREAMING_SNAKE_CASE_ ) if args.dump_args: scores.update(SCREAMING_SNAKE_CASE_ ) if args.info: UpperCamelCase : Optional[Any] = args.info if verbose: print(SCREAMING_SNAKE_CASE_ ) if args.score_path is not None: json.dump(SCREAMING_SNAKE_CASE_ , open(args.score_path , '''w''' ) ) return scores if __name__ == "__main__": # Usage for MT: # python run_eval.py MODEL_NAME $DATA_DIR/test.source $save_dir/test_translations.txt --reference_path $DATA_DIR/test.target --score_path $save_dir/test_bleu.json --task translation $@ run_generate(verbose=True)
315
0
import json import os from functools import lru_cache from typing import List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging __UpperCAmelCase : Optional[Any] = logging.get_logger(__name__) __UpperCAmelCase : str = {"vocab_file": "vocab.json", "merges_file": "merges.txt"} # See all BART models at https://huggingface.co/models?filter=bart __UpperCAmelCase : Optional[int] = { "vocab_file": { "facebook/bart-base": "https://huggingface.co/facebook/bart-base/resolve/main/vocab.json", "facebook/bart-large": "https://huggingface.co/facebook/bart-large/resolve/main/vocab.json", "facebook/bart-large-mnli": "https://huggingface.co/facebook/bart-large-mnli/resolve/main/vocab.json", "facebook/bart-large-cnn": "https://huggingface.co/facebook/bart-large-cnn/resolve/main/vocab.json", "facebook/bart-large-xsum": "https://huggingface.co/facebook/bart-large-xsum/resolve/main/vocab.json", "yjernite/bart_eli5": "https://huggingface.co/yjernite/bart_eli5/resolve/main/vocab.json", }, "merges_file": { "facebook/bart-base": "https://huggingface.co/facebook/bart-base/resolve/main/merges.txt", "facebook/bart-large": "https://huggingface.co/facebook/bart-large/resolve/main/merges.txt", "facebook/bart-large-mnli": "https://huggingface.co/facebook/bart-large-mnli/resolve/main/merges.txt", "facebook/bart-large-cnn": "https://huggingface.co/facebook/bart-large-cnn/resolve/main/merges.txt", "facebook/bart-large-xsum": "https://huggingface.co/facebook/bart-large-xsum/resolve/main/merges.txt", "yjernite/bart_eli5": "https://huggingface.co/yjernite/bart_eli5/resolve/main/merges.txt", }, } __UpperCAmelCase : Optional[int] = { "facebook/bart-base": 1024, "facebook/bart-large": 1024, "facebook/bart-large-mnli": 1024, "facebook/bart-large-cnn": 1024, "facebook/bart-large-xsum": 1024, "yjernite/bart_eli5": 1024, } @lru_cache() def a ( ): """simple docstring""" UpperCamelCase : Dict = ( list(range(ord('''!''' ) , ord('''~''' ) + 1 ) ) + list(range(ord('''¡''' ) , ord('''¬''' ) + 1 ) ) + list(range(ord('''®''' ) , ord('''ÿ''' ) + 1 ) ) ) UpperCamelCase : str = bs[:] UpperCamelCase : List[Any] = 0 for b in range(2**8 ): if b not in bs: bs.append(SCREAMING_SNAKE_CASE_ ) cs.append(2**8 + n ) n += 1 UpperCamelCase : List[str] = [chr(SCREAMING_SNAKE_CASE_ ) for n in cs] return dict(zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) def a ( SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" UpperCamelCase : List[str] = set() UpperCamelCase : Dict = word[0] for char in word[1:]: pairs.add((prev_char, char) ) UpperCamelCase : Tuple = char return pairs class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : List[str] = VOCAB_FILES_NAMES __UpperCamelCase : Dict = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : Any = ["input_ids", "attention_mask"] def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE="replace" , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="<unk>" , __SCREAMING_SNAKE_CASE="<pad>" , __SCREAMING_SNAKE_CASE="<mask>" , __SCREAMING_SNAKE_CASE=False , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : Union[str, Any] = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else bos_token UpperCamelCase : Any = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else eos_token UpperCamelCase : str = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else sep_token UpperCamelCase : int = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else cls_token UpperCamelCase : int = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else unk_token UpperCamelCase : List[Any] = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else pad_token # Mask token behave like a normal word, i.e. include the space before it UpperCamelCase : List[str] = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else mask_token super().__init__( errors=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) with open(__SCREAMING_SNAKE_CASE , encoding='''utf-8''' ) as vocab_handle: UpperCamelCase : List[Any] = json.load(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = {v: k for k, v in self.encoder.items()} UpperCamelCase : Dict = errors # how to handle errors in decoding UpperCamelCase : Optional[int] = bytes_to_unicode() UpperCamelCase : Any = {v: k for k, v in self.byte_encoder.items()} with open(__SCREAMING_SNAKE_CASE , encoding='''utf-8''' ) as merges_handle: UpperCamelCase : str = merges_handle.read().split('''\n''' )[1:-1] UpperCamelCase : Union[str, Any] = [tuple(merge.split() ) for merge in bpe_merges] UpperCamelCase : str = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) UpperCamelCase : int = {} UpperCamelCase : Any = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions UpperCamelCase : Union[str, Any] = re.compile(R'''\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+''' ) @property def _lowercase ( self ): """simple docstring""" return len(self.encoder ) def _lowercase ( self ): """simple docstring""" return dict(self.encoder , **self.added_tokens_encoder ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if token in self.cache: return self.cache[token] UpperCamelCase : Optional[Any] = tuple(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = get_pairs(__SCREAMING_SNAKE_CASE ) if not pairs: return token while True: UpperCamelCase : int = min(__SCREAMING_SNAKE_CASE , key=lambda __SCREAMING_SNAKE_CASE : self.bpe_ranks.get(__SCREAMING_SNAKE_CASE , float('''inf''' ) ) ) if bigram not in self.bpe_ranks: break UpperCamelCase : List[Any] = bigram UpperCamelCase : Optional[Any] = [] UpperCamelCase : Dict = 0 while i < len(__SCREAMING_SNAKE_CASE ): try: UpperCamelCase : Any = word.index(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) UpperCamelCase : Any = j if word[i] == first and i < len(__SCREAMING_SNAKE_CASE ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 UpperCamelCase : int = tuple(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = new_word if len(__SCREAMING_SNAKE_CASE ) == 1: break else: UpperCamelCase : Optional[int] = get_pairs(__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = ''' '''.join(__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = word return word def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = [] for token in re.findall(self.pat , __SCREAMING_SNAKE_CASE ): UpperCamelCase : Dict = ''''''.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(__SCREAMING_SNAKE_CASE ).split(''' ''' ) ) return bpe_tokens def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return self.encoder.get(__SCREAMING_SNAKE_CASE , self.encoder.get(self.unk_token ) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return self.decoder.get(__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[Any] = ''''''.join(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = bytearray([self.byte_decoder[c] for c in text] ).decode('''utf-8''' , errors=self.errors ) return text def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" if not os.path.isdir(__SCREAMING_SNAKE_CASE ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return UpperCamelCase : List[str] = os.path.join( __SCREAMING_SNAKE_CASE , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) UpperCamelCase : Optional[Any] = os.path.join( __SCREAMING_SNAKE_CASE , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''merges_file'''] ) with open(__SCREAMING_SNAKE_CASE , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=__SCREAMING_SNAKE_CASE , ensure_ascii=__SCREAMING_SNAKE_CASE ) + '''\n''' ) UpperCamelCase : Tuple = 0 with open(__SCREAMING_SNAKE_CASE , '''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 __SCREAMING_SNAKE_CASE : kv[1] ): if index != token_index: logger.warning( f"""Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.""" ''' Please check that the tokenizer is not corrupted!''' ) UpperCamelCase : Optional[Any] = token_index writer.write(''' '''.join(__SCREAMING_SNAKE_CASE ) + '''\n''' ) index += 1 return vocab_file, merge_file def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] UpperCamelCase : str = [self.cls_token_id] UpperCamelCase : Tuple = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = False ): """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__SCREAMING_SNAKE_CASE , token_ids_a=__SCREAMING_SNAKE_CASE , already_has_special_tokens=__SCREAMING_SNAKE_CASE ) if token_ids_a is None: return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1, 1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : Optional[Any] = [self.sep_token_id] UpperCamelCase : Union[str, 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 _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=False , **__SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[Any] = kwargs.pop('''add_prefix_space''' , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(__SCREAMING_SNAKE_CASE ) > 0 and not text[0].isspace()): UpperCamelCase : Union[str, Any] = ''' ''' + text return (text, kwargs)
369
import re import warnings from contextlib import contextmanager from ...processing_utils import ProcessorMixin class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : int = ["image_processor", "tokenizer"] __UpperCamelCase : List[str] = "AutoImageProcessor" __UpperCamelCase : Optional[Any] = "AutoTokenizer" def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[int] = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , __SCREAMING_SNAKE_CASE , ) UpperCamelCase : Any = kwargs.pop('''feature_extractor''' ) UpperCamelCase : 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__(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = self.image_processor UpperCamelCase : int = False def __call__( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): """simple docstring""" if self._in_target_context_manager: return self.current_processor(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = kwargs.pop('''images''' , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = kwargs.pop('''text''' , __SCREAMING_SNAKE_CASE ) if len(__SCREAMING_SNAKE_CASE ) > 0: UpperCamelCase : Union[str, Any] = args[0] UpperCamelCase : str = args[1:] if images is None and text is None: raise ValueError('''You need to specify either an `images` or `text` input to process.''' ) if images is not None: UpperCamelCase : List[str] = self.image_processor(__SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if text is not None: UpperCamelCase : Optional[Any] = self.tokenizer(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if text is None: return inputs elif images is None: return encodings else: UpperCamelCase : List[str] = encodings['''input_ids'''] return inputs def _lowercase ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): """simple docstring""" return self.tokenizer.batch_decode(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def _lowercase ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): """simple docstring""" return self.tokenizer.decode(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) @contextmanager def _lowercase ( self ): """simple docstring""" warnings.warn( '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your ''' '''labels by using the argument `text` of the regular `__call__` method (either in the same call as ''' '''your images inputs, or in a separate call.''' ) UpperCamelCase : Any = True UpperCamelCase : int = self.tokenizer yield UpperCamelCase : List[Any] = self.image_processor UpperCamelCase : Tuple = False def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" if added_vocab is None: UpperCamelCase : str = self.tokenizer.get_added_vocab() UpperCamelCase : int = {} while tokens: UpperCamelCase : Dict = re.search(R'''<s_(.*?)>''' , __SCREAMING_SNAKE_CASE , re.IGNORECASE ) if start_token is None: break UpperCamelCase : List[str] = start_token.group(1 ) UpperCamelCase : Dict = re.search(Rf"""</s_{key}>""" , __SCREAMING_SNAKE_CASE , re.IGNORECASE ) UpperCamelCase : Any = start_token.group() if end_token is None: UpperCamelCase : Optional[int] = tokens.replace(__SCREAMING_SNAKE_CASE , '''''' ) else: UpperCamelCase : Dict = end_token.group() UpperCamelCase : int = re.escape(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = re.escape(__SCREAMING_SNAKE_CASE ) UpperCamelCase : str = re.search(f"""{start_token_escaped}(.*?){end_token_escaped}""" , __SCREAMING_SNAKE_CASE , re.IGNORECASE ) if content is not None: UpperCamelCase : Dict = content.group(1 ).strip() if r"<s_" in content and r"</s_" in content: # non-leaf node UpperCamelCase : Tuple = self.tokenajson(__SCREAMING_SNAKE_CASE , is_inner_value=__SCREAMING_SNAKE_CASE , added_vocab=__SCREAMING_SNAKE_CASE ) if value: if len(__SCREAMING_SNAKE_CASE ) == 1: UpperCamelCase : str = value[0] UpperCamelCase : str = value else: # leaf nodes UpperCamelCase : Optional[int] = [] for leaf in content.split(R'''<sep/>''' ): UpperCamelCase : Optional[int] = leaf.strip() if leaf in added_vocab and leaf[0] == "<" and leaf[-2:] == "/>": UpperCamelCase : int = leaf[1:-2] # for categorical special tokens output[key].append(__SCREAMING_SNAKE_CASE ) if len(output[key] ) == 1: UpperCamelCase : Tuple = output[key][0] UpperCamelCase : List[Any] = tokens[tokens.find(__SCREAMING_SNAKE_CASE ) + len(__SCREAMING_SNAKE_CASE ) :].strip() if tokens[:6] == r"<sep/>": # non-leaf nodes return [output] + self.tokenajson(tokens[6:] , is_inner_value=__SCREAMING_SNAKE_CASE , added_vocab=__SCREAMING_SNAKE_CASE ) if len(__SCREAMING_SNAKE_CASE ): return [output] if is_inner_value else output else: return [] if is_inner_value else {"text_sequence": tokens} @property def _lowercase ( self ): """simple docstring""" warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , __SCREAMING_SNAKE_CASE , ) return self.image_processor_class @property def _lowercase ( self ): """simple docstring""" warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , __SCREAMING_SNAKE_CASE , ) return self.image_processor
315
0
"""simple docstring""" from arguments import InitializationArguments from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer, HfArgumentParser # Configuration __UpperCAmelCase : Dict = HfArgumentParser(InitializationArguments) __UpperCAmelCase : List[str] = parser.parse_args() # Load codeparrot tokenizer trained for Python code tokenization __UpperCAmelCase : List[str] = AutoTokenizer.from_pretrained(args.tokenizer_name) # Config: "scale_attn_by_layer_idx" and "reorder_and_upcast_attn" are Mistral stability tweaks __UpperCAmelCase : Union[str, Any] = { "vocab_size": len(tokenizer), "scale_attn_by_inverse_layer_idx": True, "reorder_and_upcast_attn": True, } # Load model config (GPT-2 large in this case) __UpperCAmelCase : Optional[int] = AutoConfig.from_pretrained(args.config_name, **config_kwargs) # Initialize new model with config __UpperCAmelCase : str = AutoModelForCausalLM.from_config(config) # Save model to the hub model.save_pretrained(args.model_name, push_to_hub=args.push_to_hub)
370
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __UpperCAmelCase : Union[str, Any] = { "configuration_mgp_str": ["MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP", "MgpstrConfig"], "processing_mgp_str": ["MgpstrProcessor"], "tokenization_mgp_str": ["MgpstrTokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : Union[str, Any] = [ "MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST", "MgpstrModel", "MgpstrPreTrainedModel", "MgpstrForSceneTextRecognition", ] if TYPE_CHECKING: from .configuration_mgp_str import MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP, MgpstrConfig from .processing_mgp_str import MgpstrProcessor from .tokenization_mgp_str import MgpstrTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mgp_str import ( MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST, MgpstrForSceneTextRecognition, MgpstrModel, MgpstrPreTrainedModel, ) else: import sys __UpperCAmelCase : Any = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
315
0
import re import time from typing import Optional import IPython.display as disp from ..trainer_callback import TrainerCallback from ..trainer_utils import IntervalStrategy, has_length def a ( SCREAMING_SNAKE_CASE_ : Optional[Any] ): """simple docstring""" UpperCamelCase : Union[str, Any] = int(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Union[str, Any] = t // 3_6_0_0, (t // 6_0) % 6_0, t % 6_0 return F"""{h}:{m:02d}:{s:02d}""" if h != 0 else F"""{m:02d}:{s:02d}""" def a ( SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Any=3_0_0 ): """simple docstring""" return F""" <div> {prefix} <progress value='{value}' max='{total}' style='width:{width}px; height:20px; vertical-align: middle;'></progress> {label} </div> """ def a ( SCREAMING_SNAKE_CASE_ : List[Any] ): """simple docstring""" UpperCamelCase : Dict = '''<table border="1" class="dataframe">\n''' html_code += """ <thead>\n <tr style="text-align: left;">\n""" for i in items[0]: html_code += F""" <th>{i}</th>\n""" html_code += " </tr>\n </thead>\n <tbody>\n" for line in items[1:]: html_code += " <tr>\n" for elt in line: UpperCamelCase : Tuple = F"""{elt:.6f}""" if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else str(SCREAMING_SNAKE_CASE_ ) html_code += F""" <td>{elt}</td>\n""" html_code += " </tr>\n" html_code += " </tbody>\n</table><p>" return html_code class UpperCAmelCase_ : '''simple docstring''' __UpperCamelCase : str = 5 __UpperCamelCase : int = 0.2 def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = 300 , ): """simple docstring""" UpperCamelCase : List[Any] = total UpperCamelCase : str = '''''' if prefix is None else prefix UpperCamelCase : int = leave UpperCamelCase : int = parent UpperCamelCase : List[Any] = width UpperCamelCase : Any = None UpperCamelCase : Any = None UpperCamelCase : Optional[int] = None def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = False , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : Dict = value if comment is not None: UpperCamelCase : Union[str, Any] = comment if self.last_value is None: UpperCamelCase : Any = time.time() UpperCamelCase : Union[str, Any] = value UpperCamelCase : List[str] = None UpperCamelCase : Dict = self.warmup UpperCamelCase : Union[str, Any] = 1 self.update_bar(__SCREAMING_SNAKE_CASE ) elif value <= self.last_value and not force_update: return elif force_update or self.first_calls > 0 or value >= min(self.last_value + self.wait_for , self.total ): if self.first_calls > 0: self.first_calls -= 1 UpperCamelCase : Any = time.time() UpperCamelCase : List[str] = current_time - self.start_time # We could have value = self.start_value if the update is called twixe with the same start value. if value > self.start_value: UpperCamelCase : str = self.elapsed_time / (value - self.start_value) else: UpperCamelCase : Tuple = None if value >= self.total: UpperCamelCase : Dict = self.total UpperCamelCase : Any = None if not self.leave: self.close() elif self.average_time_per_item is not None: UpperCamelCase : List[str] = self.average_time_per_item * (self.total - value) self.update_bar(__SCREAMING_SNAKE_CASE ) UpperCamelCase : str = value UpperCamelCase : Any = current_time if self.average_time_per_item is None: UpperCamelCase : str = 1 else: UpperCamelCase : List[Any] = max(int(self.update_every / self.average_time_per_item ) , 1 ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" UpperCamelCase : Dict = ''' ''' * (len(str(self.total ) ) - len(str(__SCREAMING_SNAKE_CASE ) )) + str(__SCREAMING_SNAKE_CASE ) if self.elapsed_time is None: UpperCamelCase : Any = f"""[{spaced_value}/{self.total} : < :""" elif self.predicted_remaining is None: UpperCamelCase : int = f"""[{spaced_value}/{self.total} {format_time(self.elapsed_time )}""" else: UpperCamelCase : List[Any] = ( f"""[{spaced_value}/{self.total} {format_time(self.elapsed_time )} <""" f""" {format_time(self.predicted_remaining )}""" ) self.label += f""", {1/self.average_time_per_item:.2f} it/s""" self.label += "]" if self.comment is None or len(self.comment ) == 0 else f""", {self.comment}]""" self.display() def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = html_progress_bar(self.value , self.total , self.prefix , self.label , self.width ) if self.parent is not None: # If this is a child bar, the parent will take care of the display. self.parent.display() return if self.output is None: UpperCamelCase : List[str] = disp.display(disp.HTML(self.html_code ) , display_id=__SCREAMING_SNAKE_CASE ) else: self.output.update(disp.HTML(self.html_code ) ) def _lowercase ( self ): """simple docstring""" if self.parent is None and self.output is not None: self.output.update(disp.HTML('''''' ) ) class UpperCAmelCase_ ( _a): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" super().__init__(__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = None if column_names is None else [column_names] UpperCamelCase : Dict = None def _lowercase ( self ): """simple docstring""" UpperCamelCase : Tuple = html_progress_bar(self.value , self.total , self.prefix , self.label , self.width ) if self.inner_table is not None: self.html_code += text_to_html_table(self.inner_table ) if self.child_bar is not None: self.html_code += self.child_bar.html_code if self.output is None: UpperCamelCase : str = disp.display(disp.HTML(self.html_code ) , display_id=__SCREAMING_SNAKE_CASE ) else: self.output.update(disp.HTML(self.html_code ) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if self.inner_table is None: UpperCamelCase : List[str] = [list(values.keys() ), list(values.values() )] else: UpperCamelCase : Any = self.inner_table[0] if len(self.inner_table ) == 1: # We give a chance to update the column names at the first iteration for key in values.keys(): if key not in columns: columns.append(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = columns self.inner_table.append([values[c] for c in columns] ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=300 ): """simple docstring""" UpperCamelCase : str = NotebookProgressBar(__SCREAMING_SNAKE_CASE , prefix=__SCREAMING_SNAKE_CASE , parent=self , width=__SCREAMING_SNAKE_CASE ) return self.child_bar def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = None self.display() class UpperCAmelCase_ ( _a): '''simple docstring''' def __init__( self ): """simple docstring""" UpperCamelCase : Dict = None UpperCamelCase : int = None UpperCamelCase : Tuple = False def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = '''Epoch''' if args.evaluation_strategy == IntervalStrategy.EPOCH else '''Step''' UpperCamelCase : List[str] = 0 UpperCamelCase : Optional[Any] = 0 UpperCamelCase : Tuple = [self.first_column] + ['''Training Loss'''] if args.evaluation_strategy != IntervalStrategy.NO: column_names.append('''Validation Loss''' ) UpperCamelCase : Dict = NotebookTrainingTracker(state.max_steps , __SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = int(state.epoch ) if int(state.epoch ) == state.epoch else f"""{state.epoch:.2f}""" self.training_tracker.update( state.global_step + 1 , comment=f"""Epoch {epoch}/{state.num_train_epochs}""" , force_update=self._force_next_update , ) UpperCamelCase : str = False def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ): """simple docstring""" if not has_length(__SCREAMING_SNAKE_CASE ): return if self.prediction_bar is None: if self.training_tracker is not None: UpperCamelCase : str = self.training_tracker.add_child(len(__SCREAMING_SNAKE_CASE ) ) else: UpperCamelCase : Any = NotebookProgressBar(len(__SCREAMING_SNAKE_CASE ) ) self.prediction_bar.update(1 ) else: self.prediction_bar.update(self.prediction_bar.value + 1 ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): """simple docstring""" if self.prediction_bar is not None: self.prediction_bar.close() UpperCamelCase : Optional[int] = None def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ): """simple docstring""" if args.evaluation_strategy == IntervalStrategy.NO and "loss" in logs: UpperCamelCase : str = {'''Training Loss''': logs['''loss''']} # First column is necessarily Step sine we're not in epoch eval strategy UpperCamelCase : Any = state.global_step self.training_tracker.write_line(__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ): """simple docstring""" if self.training_tracker is not None: UpperCamelCase : str = {'''Training Loss''': '''No log''', '''Validation Loss''': '''No log'''} for log in reversed(state.log_history ): if "loss" in log: UpperCamelCase : Union[str, Any] = log['''loss'''] break if self.first_column == "Epoch": UpperCamelCase : int = int(state.epoch ) else: UpperCamelCase : Tuple = state.global_step UpperCamelCase : Any = '''eval''' for k in metrics: if k.endswith('''_loss''' ): UpperCamelCase : int = re.sub(R'''\_loss$''' , '''''' , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = metrics.pop('''total_flos''' , __SCREAMING_SNAKE_CASE ) UpperCamelCase : int = metrics.pop('''epoch''' , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = metrics.pop(f"""{metric_key_prefix}_runtime""" , __SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = metrics.pop(f"""{metric_key_prefix}_samples_per_second""" , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = metrics.pop(f"""{metric_key_prefix}_steps_per_second""" , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = metrics.pop(f"""{metric_key_prefix}_jit_compilation_time""" , __SCREAMING_SNAKE_CASE ) for k, v in metrics.items(): if k == f"""{metric_key_prefix}_loss""": UpperCamelCase : List[str] = v else: UpperCamelCase : Dict = k.split('''_''' ) UpperCamelCase : Union[str, Any] = ''' '''.join([part.capitalize() for part in splits[1:]] ) UpperCamelCase : Optional[Any] = v self.training_tracker.write_line(__SCREAMING_SNAKE_CASE ) self.training_tracker.remove_child() UpperCamelCase : Optional[int] = None # Evaluation takes a long time so we should force the next update. UpperCamelCase : Optional[Any] = True def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): """simple docstring""" self.training_tracker.update( state.global_step , comment=f"""Epoch {int(state.epoch )}/{state.num_train_epochs}""" , force_update=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = None
371
def a ( SCREAMING_SNAKE_CASE_ : int = 5_0 ): """simple docstring""" UpperCamelCase : List[str] = [1] * (length + 1) for row_length in range(3 , length + 1 ): for block_length in range(3 , row_length + 1 ): for block_start in range(row_length - block_length ): ways_number[row_length] += ways_number[ row_length - block_start - block_length - 1 ] ways_number[row_length] += 1 return ways_number[length] if __name__ == "__main__": print(f'''{solution() = }''')
315
0
from __future__ import annotations from itertools import permutations from random import randint from timeit import repeat def a ( ): """simple docstring""" UpperCamelCase : Dict = [randint(-1_0_0_0 , 1_0_0_0 ) for i in range(1_0 )] UpperCamelCase : Any = randint(-5_0_0_0 , 5_0_0_0 ) return (arr, r) __UpperCAmelCase : List[Any] = make_dataset() def a ( SCREAMING_SNAKE_CASE_ : list[int] , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" for triplet in permutations(SCREAMING_SNAKE_CASE_ , 3 ): if sum(SCREAMING_SNAKE_CASE_ ) == target: return tuple(sorted(SCREAMING_SNAKE_CASE_ ) ) return (0, 0, 0) def a ( SCREAMING_SNAKE_CASE_ : list[int] , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" arr.sort() UpperCamelCase : Dict = len(SCREAMING_SNAKE_CASE_ ) for i in range(n - 1 ): UpperCamelCase : Optional[Any] = i + 1, n - 1 while left < right: if arr[i] + arr[left] + arr[right] == target: return (arr[i], arr[left], arr[right]) elif arr[i] + arr[left] + arr[right] < target: left += 1 elif arr[i] + arr[left] + arr[right] > target: right -= 1 return (0, 0, 0) def a ( ): """simple docstring""" UpperCamelCase : Any = ''' from __main__ import dataset, triplet_sum1, triplet_sum2 ''' UpperCamelCase : Optional[Any] = ''' triplet_sum1(*dataset) ''' UpperCamelCase : Optional[int] = ''' triplet_sum2(*dataset) ''' UpperCamelCase : List[Any] = repeat(setup=SCREAMING_SNAKE_CASE_ , stmt=SCREAMING_SNAKE_CASE_ , repeat=5 , number=1_0_0_0_0 ) UpperCamelCase : Dict = repeat(setup=SCREAMING_SNAKE_CASE_ , stmt=SCREAMING_SNAKE_CASE_ , repeat=5 , number=1_0_0_0_0 ) return (min(SCREAMING_SNAKE_CASE_ ), min(SCREAMING_SNAKE_CASE_ )) if __name__ == "__main__": from doctest import testmod testmod() __UpperCAmelCase : Union[str, Any] = solution_times() print(f'''The time for naive implementation is {times[0]}.''') print(f'''The time for optimized implementation is {times[1]}.''')
350
import math import time from transformers import Trainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput, speed_metrics if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class UpperCAmelCase_ ( _a): '''simple docstring''' def __init__( self , *__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = eval_examples UpperCamelCase : Optional[Any] = post_process_function def _lowercase ( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE = "eval" ): """simple docstring""" UpperCamelCase : int = self.eval_dataset if eval_dataset is None else eval_dataset UpperCamelCase : int = self.get_eval_dataloader(__SCREAMING_SNAKE_CASE ) UpperCamelCase : str = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. UpperCamelCase : Any = self.compute_metrics UpperCamelCase : List[Any] = None UpperCamelCase : str = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop UpperCamelCase : Dict = time.time() try: UpperCamelCase : str = eval_loop( __SCREAMING_SNAKE_CASE , description='''Evaluation''' , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=__SCREAMING_SNAKE_CASE , metric_key_prefix=__SCREAMING_SNAKE_CASE , ) finally: UpperCamelCase : Union[str, Any] = compute_metrics UpperCamelCase : Any = self.args.eval_batch_size * self.args.world_size if f"""{metric_key_prefix}_jit_compilation_time""" in output.metrics: start_time += output.metrics[f"""{metric_key_prefix}_jit_compilation_time"""] output.metrics.update( speed_metrics( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save: # Only the main node write the results by default UpperCamelCase : Dict = self.post_process_function(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , output.predictions ) UpperCamelCase : Optional[Any] = self.compute_metrics(__SCREAMING_SNAKE_CASE ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f"""{metric_key_prefix}_""" ): UpperCamelCase : Dict = metrics.pop(__SCREAMING_SNAKE_CASE ) metrics.update(output.metrics ) else: UpperCamelCase : List[Any] = output.metrics if self.args.should_log: # Only the main node log the results by default self.log(__SCREAMING_SNAKE_CASE ) if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report() ) UpperCamelCase : Any = self.callback_handler.on_evaluate(self.args , self.state , self.control , __SCREAMING_SNAKE_CASE ) return metrics def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE = "test" ): """simple docstring""" UpperCamelCase : Tuple = self.get_test_dataloader(__SCREAMING_SNAKE_CASE ) # Temporarily disable metric computation, we will do it in the loop here. UpperCamelCase : Union[str, Any] = self.compute_metrics UpperCamelCase : Tuple = None UpperCamelCase : str = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop UpperCamelCase : Optional[int] = time.time() try: UpperCamelCase : int = eval_loop( __SCREAMING_SNAKE_CASE , description='''Prediction''' , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=__SCREAMING_SNAKE_CASE , metric_key_prefix=__SCREAMING_SNAKE_CASE , ) finally: UpperCamelCase : int = compute_metrics UpperCamelCase : Dict = self.args.eval_batch_size * self.args.world_size if f"""{metric_key_prefix}_jit_compilation_time""" in output.metrics: start_time += output.metrics[f"""{metric_key_prefix}_jit_compilation_time"""] output.metrics.update( speed_metrics( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is None or self.compute_metrics is None: return output UpperCamelCase : Dict = self.post_process_function(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , output.predictions , '''predict''' ) UpperCamelCase : Union[str, Any] = self.compute_metrics(__SCREAMING_SNAKE_CASE ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f"""{metric_key_prefix}_""" ): UpperCamelCase : Any = metrics.pop(__SCREAMING_SNAKE_CASE ) metrics.update(output.metrics ) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=__SCREAMING_SNAKE_CASE )
315
0
import argparse import logging import sys from unittest.mock import patch import run_glue_deebert from transformers.testing_utils import TestCasePlus, get_gpu_count, require_torch_non_multi_gpu, slow logging.basicConfig(level=logging.DEBUG) __UpperCAmelCase : Union[str, Any] = logging.getLogger() def a ( ): """simple docstring""" UpperCamelCase : List[Any] = argparse.ArgumentParser() parser.add_argument('''-f''' ) UpperCamelCase : List[str] = parser.parse_args() return args.f class UpperCAmelCase_ ( _a): '''simple docstring''' def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = logging.StreamHandler(sys.stdout ) logger.addHandler(__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Dict = get_gpu_count() if n_gpu > 1: pass # XXX: doesn't quite work with n_gpu > 1 https://github.com/huggingface/transformers/issues/10560 # script = f"{self.examples_dir_str}/research_projects/deebert/run_glue_deebert.py" # distributed_args = f"-m torch.distributed.launch --nproc_per_node={n_gpu} {script}".split() # cmd = [sys.executable] + distributed_args + args # execute_subprocess_async(cmd, env=self.get_env()) # XXX: test the results - need to save them first into .json file else: args.insert(0 , '''run_glue_deebert.py''' ) with patch.object(__SCREAMING_SNAKE_CASE , '''argv''' , __SCREAMING_SNAKE_CASE ): UpperCamelCase : int = run_glue_deebert.main() for value in result.values(): self.assertGreaterEqual(__SCREAMING_SNAKE_CASE , 0.666 ) @slow @require_torch_non_multi_gpu def _lowercase ( self ): """simple docstring""" UpperCamelCase : Any = ''' --model_type roberta --model_name_or_path roberta-base --task_name MRPC --do_train --do_eval --do_lower_case --data_dir ./tests/fixtures/tests_samples/MRPC/ --max_seq_length 128 --per_gpu_eval_batch_size=1 --per_gpu_train_batch_size=8 --learning_rate 2e-4 --num_train_epochs 3 --overwrite_output_dir --seed 42 --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --plot_data_dir ./examples/deebert/results/ --save_steps 0 --overwrite_cache --eval_after_first_stage '''.split() self.run_and_check(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = ''' --model_type roberta --model_name_or_path ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --task_name MRPC --do_eval --do_lower_case --data_dir ./tests/fixtures/tests_samples/MRPC/ --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --plot_data_dir ./examples/deebert/results/ --max_seq_length 128 --eval_each_highway --eval_highway --overwrite_cache --per_gpu_eval_batch_size=1 '''.split() self.run_and_check(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = ''' --model_type roberta --model_name_or_path ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --task_name MRPC --do_eval --do_lower_case --data_dir ./tests/fixtures/tests_samples/MRPC/ --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --plot_data_dir ./examples/deebert/results/ --max_seq_length 128 --early_exit_entropy 0.1 --eval_highway --overwrite_cache --per_gpu_eval_batch_size=1 '''.split() self.run_and_check(__SCREAMING_SNAKE_CASE )
351
from __future__ import annotations import collections import pprint from pathlib import Path def a ( SCREAMING_SNAKE_CASE_ : str ): """simple docstring""" return "".join(sorted(SCREAMING_SNAKE_CASE_ ) ) def a ( SCREAMING_SNAKE_CASE_ : str ): """simple docstring""" return word_by_signature[signature(SCREAMING_SNAKE_CASE_ )] __UpperCAmelCase : str = Path(__file__).parent.joinpath("words.txt").read_text(encoding="utf-8") __UpperCAmelCase : Tuple = sorted({word.strip().lower() for word in data.splitlines()}) __UpperCAmelCase : Union[str, Any] = collections.defaultdict(list) for word in word_list: word_by_signature[signature(word)].append(word) if __name__ == "__main__": __UpperCAmelCase : int = {word: anagram(word) for word in word_list if len(anagram(word)) > 1} with open("anagrams.txt", "w") as file: file.write("all_anagrams = \n ") file.write(pprint.pformat(all_anagrams))
315
0
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import KandinskyPipeline, KandinskyPriorPipeline else: from .pipeline_kandinsky import KandinskyPipeline from .pipeline_kandinsky_imgaimg import KandinskyImgaImgPipeline from .pipeline_kandinsky_inpaint import KandinskyInpaintPipeline from .pipeline_kandinsky_prior import KandinskyPriorPipeline, KandinskyPriorPipelineOutput from .text_encoder import MultilingualCLIP
352
def a ( SCREAMING_SNAKE_CASE_ : list[list[float]] ): """simple docstring""" UpperCamelCase : list[list[float]] = [] for data in source_data: for i, el in enumerate(SCREAMING_SNAKE_CASE_ ): if len(SCREAMING_SNAKE_CASE_ ) < i + 1: data_lists.append([] ) data_lists[i].append(float(SCREAMING_SNAKE_CASE_ ) ) return data_lists def a ( SCREAMING_SNAKE_CASE_ : list[list[float]] , SCREAMING_SNAKE_CASE_ : list[int] ): """simple docstring""" UpperCamelCase : list[list[float]] = [] for dlist, weight in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): UpperCamelCase : Union[str, Any] = min(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Tuple = max(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : list[float] = [] # for weight 0 score is 1 - actual score if weight == 0: for item in dlist: try: score.append(1 - ((item - mind) / (maxd - mind)) ) except ZeroDivisionError: score.append(1 ) elif weight == 1: for item in dlist: try: score.append((item - mind) / (maxd - mind) ) except ZeroDivisionError: score.append(0 ) # weight not 0 or 1 else: UpperCamelCase : Dict = F"""Invalid weight of {weight:f} provided""" raise ValueError(SCREAMING_SNAKE_CASE_ ) score_lists.append(SCREAMING_SNAKE_CASE_ ) return score_lists def a ( SCREAMING_SNAKE_CASE_ : list[list[float]] ): """simple docstring""" UpperCamelCase : list[float] = [0 for i in range(len(score_lists[0] ) )] for slist in score_lists: for j, ele in enumerate(SCREAMING_SNAKE_CASE_ ): UpperCamelCase : str = final_scores[j] + ele return final_scores def a ( SCREAMING_SNAKE_CASE_ : list[list[float]] , SCREAMING_SNAKE_CASE_ : list[int] ): """simple docstring""" UpperCamelCase : str = get_data(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[str] = calculate_each_score(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[str] = generate_final_scores(SCREAMING_SNAKE_CASE_ ) # append scores to source data for i, ele in enumerate(SCREAMING_SNAKE_CASE_ ): source_data[i].append(SCREAMING_SNAKE_CASE_ ) return source_data
315
0
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_mobilebert import MobileBertTokenizer __UpperCAmelCase : List[Any] = logging.get_logger(__name__) __UpperCAmelCase : Optional[int] = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} __UpperCAmelCase : Tuple = { "vocab_file": {"mobilebert-uncased": "https://huggingface.co/google/mobilebert-uncased/resolve/main/vocab.txt"}, "tokenizer_file": { "mobilebert-uncased": "https://huggingface.co/google/mobilebert-uncased/resolve/main/tokenizer.json" }, } __UpperCAmelCase : Optional[Any] = {"mobilebert-uncased": 512} __UpperCAmelCase : str = {} class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : List[str] = VOCAB_FILES_NAMES __UpperCamelCase : Tuple = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : List[str] = PRETRAINED_INIT_CONFIGURATION __UpperCamelCase : Any = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : List[str] = MobileBertTokenizer def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE="[UNK]" , __SCREAMING_SNAKE_CASE="[SEP]" , __SCREAMING_SNAKE_CASE="[PAD]" , __SCREAMING_SNAKE_CASE="[CLS]" , __SCREAMING_SNAKE_CASE="[MASK]" , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" super().__init__( __SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , do_lower_case=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , tokenize_chinese_chars=__SCREAMING_SNAKE_CASE , strip_accents=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) UpperCamelCase : Dict = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('''lowercase''' , __SCREAMING_SNAKE_CASE ) != do_lower_case or normalizer_state.get('''strip_accents''' , __SCREAMING_SNAKE_CASE ) != strip_accents or normalizer_state.get('''handle_chinese_chars''' , __SCREAMING_SNAKE_CASE ) != tokenize_chinese_chars ): UpperCamelCase : Optional[Any] = getattr(__SCREAMING_SNAKE_CASE , normalizer_state.pop('''type''' ) ) UpperCamelCase : List[str] = do_lower_case UpperCamelCase : Tuple = strip_accents UpperCamelCase : Any = tokenize_chinese_chars UpperCamelCase : int = normalizer_class(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = do_lower_case def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" UpperCamelCase : Optional[Any] = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : Any = [self.sep_token_id] UpperCamelCase : Union[str, Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : List[Any] = self._tokenizer.model.save(__SCREAMING_SNAKE_CASE , name=__SCREAMING_SNAKE_CASE ) return tuple(__SCREAMING_SNAKE_CASE )
353
import glob import os import random from string import ascii_lowercase, digits import cva __UpperCAmelCase : Optional[int] = "" __UpperCAmelCase : Union[str, Any] = "" __UpperCAmelCase : Optional[int] = "" __UpperCAmelCase : Any = 1 # (0 is vertical, 1 is horizontal) def a ( ): """simple docstring""" UpperCamelCase , UpperCamelCase : List[Any] = get_dataset(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) print('''Processing...''' ) UpperCamelCase , UpperCamelCase , UpperCamelCase : Any = update_image_and_anno(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) for index, image in enumerate(SCREAMING_SNAKE_CASE_ ): # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' UpperCamelCase : Optional[int] = random_chars(3_2 ) UpperCamelCase : List[Any] = paths[index].split(os.sep )[-1].rsplit('''.''' , 1 )[0] UpperCamelCase : int = F"""{OUTPUT_DIR}/{file_name}_FLIP_{letter_code}""" cva.imwrite(F"""/{file_root}.jpg""" , SCREAMING_SNAKE_CASE_ , [cva.IMWRITE_JPEG_QUALITY, 8_5] ) print(F"""Success {index+1}/{len(SCREAMING_SNAKE_CASE_ )} with {file_name}""" ) UpperCamelCase : Any = [] for anno in new_annos[index]: UpperCamelCase : Tuple = F"""{anno[0]} {anno[1]} {anno[2]} {anno[3]} {anno[4]}""" annos_list.append(SCREAMING_SNAKE_CASE_ ) with open(F"""/{file_root}.txt""" , '''w''' ) as outfile: outfile.write('''\n'''.join(line for line in annos_list ) ) def a ( SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : str ): """simple docstring""" UpperCamelCase : Any = [] UpperCamelCase : Union[str, Any] = [] for label_file in glob.glob(os.path.join(SCREAMING_SNAKE_CASE_ , '''*.txt''' ) ): UpperCamelCase : Optional[Any] = label_file.split(os.sep )[-1].rsplit('''.''' , 1 )[0] with open(SCREAMING_SNAKE_CASE_ ) as in_file: UpperCamelCase : List[str] = in_file.readlines() UpperCamelCase : Tuple = os.path.join(SCREAMING_SNAKE_CASE_ , F"""{label_name}.jpg""" ) UpperCamelCase : Union[str, Any] = [] for obj_list in obj_lists: UpperCamelCase : str = obj_list.rstrip('''\n''' ).split(''' ''' ) boxes.append( [ int(obj[0] ), float(obj[1] ), float(obj[2] ), float(obj[3] ), float(obj[4] ), ] ) if not boxes: continue img_paths.append(SCREAMING_SNAKE_CASE_ ) labels.append(SCREAMING_SNAKE_CASE_ ) return img_paths, labels def a ( SCREAMING_SNAKE_CASE_ : list , SCREAMING_SNAKE_CASE_ : list , SCREAMING_SNAKE_CASE_ : int = 1 ): """simple docstring""" UpperCamelCase : List[Any] = [] UpperCamelCase : str = [] UpperCamelCase : int = [] for idx in range(len(SCREAMING_SNAKE_CASE_ ) ): UpperCamelCase : Tuple = [] UpperCamelCase : Optional[int] = img_list[idx] path_list.append(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Optional[int] = anno_list[idx] UpperCamelCase : Optional[Any] = cva.imread(SCREAMING_SNAKE_CASE_ ) if flip_type == 1: UpperCamelCase : Optional[Any] = cva.flip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) for bbox in img_annos: UpperCamelCase : Optional[Any] = 1 - bbox[1] new_annos.append([bbox[0], x_center_new, bbox[2], bbox[3], bbox[4]] ) elif flip_type == 0: UpperCamelCase : List[str] = cva.flip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) for bbox in img_annos: UpperCamelCase : Union[str, Any] = 1 - bbox[2] new_annos.append([bbox[0], bbox[1], y_center_new, bbox[3], bbox[4]] ) new_annos_lists.append(SCREAMING_SNAKE_CASE_ ) new_imgs_list.append(SCREAMING_SNAKE_CASE_ ) return new_imgs_list, new_annos_lists, path_list def a ( SCREAMING_SNAKE_CASE_ : int = 3_2 ): """simple docstring""" assert number_char > 1, "The number of character should greater than 1" UpperCamelCase : Any = ascii_lowercase + digits return "".join(random.choice(SCREAMING_SNAKE_CASE_ ) for _ in range(SCREAMING_SNAKE_CASE_ ) ) if __name__ == "__main__": main() print("DONE ✅")
315
0
import inspect import os import unittest from pathlib import Path import torch import accelerate from accelerate.test_utils import execute_subprocess_async from accelerate.test_utils.testing import run_command class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' __UpperCamelCase : Optional[int] = inspect.getfile(accelerate.test_utils) __UpperCamelCase : List[Any] = os.path.sep.join(mod_file.split(os.path.sep)[:-1] + ["scripts", "test_cli.py"]) __UpperCamelCase : Optional[Any] = ["accelerate", "launch"] __UpperCamelCase : int = Path.home() / ".cache/huggingface/accelerate" __UpperCamelCase : Tuple = "default_config.yaml" __UpperCamelCase : int = config_folder / config_file __UpperCamelCase : Optional[Any] = config_folder / "_default_config.yaml" __UpperCamelCase : Tuple = Path("tests/test_configs") @classmethod def _lowercase ( cls ): """simple docstring""" if cls.config_path.is_file(): cls.config_path.rename(cls.changed_path ) @classmethod def _lowercase ( cls ): """simple docstring""" if cls.changed_path.is_file(): cls.changed_path.rename(cls.config_path ) def _lowercase ( self ): """simple docstring""" UpperCamelCase = self.base_cmd if torch.cuda.is_available() and (torch.cuda.device_count() > 1): cmd += ["--multi_gpu"] execute_subprocess_async(cmd + [self.test_file_path] , env=os.environ.copy() ) def _lowercase ( self ): """simple docstring""" for config in sorted(self.test_config_path.glob('''**/*.yaml''' ) ): with self.subTest(config_file=__SCREAMING_SNAKE_CASE ): execute_subprocess_async( self.base_cmd + ['''--config_file''', str(__SCREAMING_SNAKE_CASE ), self.test_file_path] , env=os.environ.copy() ) def _lowercase ( self ): """simple docstring""" execute_subprocess_async(['''accelerate''', '''test'''] , env=os.environ.copy() ) class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' __UpperCamelCase : Optional[int] = "test-tpu" __UpperCamelCase : List[str] = "us-central1-a" __UpperCamelCase : Union[str, Any] = "ls" __UpperCamelCase : Tuple = ["accelerate", "tpu-config"] __UpperCamelCase : Tuple = "cd /usr/share" __UpperCamelCase : Tuple = "tests/test_samples/test_command_file.sh" __UpperCamelCase : Optional[Any] = "Running gcloud compute tpus tpu-vm ssh" def _lowercase ( self ): """simple docstring""" UpperCamelCase = run_command( self.cmd + ['''--command''', self.command, '''--tpu_zone''', self.tpu_zone, '''--tpu_name''', self.tpu_name, '''--debug'''] , return_stdout=__SCREAMING_SNAKE_CASE , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all""" , __SCREAMING_SNAKE_CASE , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase = run_command( self.cmd + [ '''--config_file''', '''tests/test_configs/0_12_0.yaml''', '''--command''', self.command, '''--tpu_zone''', self.tpu_zone, '''--tpu_name''', self.tpu_name, '''--debug''', ] , return_stdout=__SCREAMING_SNAKE_CASE , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all""" , __SCREAMING_SNAKE_CASE , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase = run_command( self.cmd + ['''--config_file''', '''tests/test_configs/latest.yaml''', '''--debug'''] , return_stdout=__SCREAMING_SNAKE_CASE ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all""" , __SCREAMING_SNAKE_CASE , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase = run_command( self.cmd + ['''--config_file''', '''tests/test_configs/latest.yaml''', '''--command''', self.command, '''--debug'''] , return_stdout=__SCREAMING_SNAKE_CASE , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all""" , __SCREAMING_SNAKE_CASE , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase = run_command( self.cmd + [ '''--config_file''', '''tests/test_configs/latest.yaml''', '''--command''', self.command, '''--command''', '''echo "Hello World"''', '''--debug''', ] , return_stdout=__SCREAMING_SNAKE_CASE , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls; echo \"Hello World\" --worker all""" , __SCREAMING_SNAKE_CASE , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase = run_command( self.cmd + ['''--config_file''', '''tests/test_configs/latest.yaml''', '''--command_file''', self.command_file, '''--debug'''] , return_stdout=__SCREAMING_SNAKE_CASE , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all""" , __SCREAMING_SNAKE_CASE , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase = run_command( self.cmd + [ '''--config_file''', '''tests/test_configs/0_12_0.yaml''', '''--command_file''', self.command_file, '''--tpu_zone''', self.tpu_zone, '''--tpu_name''', self.tpu_name, '''--debug''', ] , return_stdout=__SCREAMING_SNAKE_CASE , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all""" , __SCREAMING_SNAKE_CASE , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase = run_command( self.cmd + ['''--config_file''', '''tests/test_configs/latest.yaml''', '''--install_accelerate''', '''--debug'''] , return_stdout=__SCREAMING_SNAKE_CASE , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate -U; echo \"hello world\"; echo \"this is a second command\" --worker all""" , __SCREAMING_SNAKE_CASE , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase = run_command( self.cmd + [ '''--config_file''', '''tests/test_configs/latest.yaml''', '''--install_accelerate''', '''--accelerate_version''', '''12.0.0''', '''--debug''', ] , return_stdout=__SCREAMING_SNAKE_CASE , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate==12.0.0; echo \"hello world\"; echo \"this is a second command\" --worker all""" , __SCREAMING_SNAKE_CASE , )
354
import qiskit def a ( SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" UpperCamelCase : List[str] = qiskit.Aer.get_backend('''aer_simulator''' ) UpperCamelCase : Any = qiskit.QuantumCircuit(4 , 2 ) # encode inputs in qubits 0 and 1 if bita == 1: qc_ha.x(0 ) if bita == 1: qc_ha.x(1 ) qc_ha.barrier() # use cnots to write XOR of the inputs on qubit2 qc_ha.cx(0 , 2 ) qc_ha.cx(1 , 2 ) # use ccx / toffoli gate to write AND of the inputs on qubit3 qc_ha.ccx(0 , 1 , 3 ) qc_ha.barrier() # extract outputs qc_ha.measure(2 , 0 ) # extract XOR value qc_ha.measure(3 , 1 ) # extract AND value # Execute the circuit on the qasm simulator UpperCamelCase : Any = qiskit.execute(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , shots=1_0_0_0 ) # Return the histogram data of the results of the experiment return job.result().get_counts(SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": __UpperCAmelCase : int = half_adder(1, 1) print(f'''Half Adder Output Qubit Counts: {counts}''')
315
0
import functools from typing import Any def a ( SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : list[str] ): if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) or len(SCREAMING_SNAKE_CASE_ ) == 0: raise ValueError('''the string should be not empty string''' ) if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) or not all( isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) and len(SCREAMING_SNAKE_CASE_ ) > 0 for item in words ): raise ValueError('''the words should be a list of non-empty strings''' ) # Build trie UpperCamelCase : dict[str, Any] = {} UpperCamelCase : Dict = '''WORD_KEEPER''' for word in words: UpperCamelCase : Union[str, Any] = trie for c in word: if c not in trie_node: UpperCamelCase : Tuple = {} UpperCamelCase : int = trie_node[c] UpperCamelCase : List[Any] = True UpperCamelCase : Optional[Any] = len(SCREAMING_SNAKE_CASE_ ) # Dynamic programming method @functools.cache def is_breakable(SCREAMING_SNAKE_CASE_ : int ) -> bool: if index == len_string: return True UpperCamelCase : str = trie for i in range(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): UpperCamelCase : List[str] = trie_node.get(string[i] , SCREAMING_SNAKE_CASE_ ) if trie_node is None: return False if trie_node.get(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) and is_breakable(i + 1 ): return True return False return is_breakable(0 ) if __name__ == "__main__": import doctest doctest.testmod()
355
import torch import torch.nn as nn from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel from ...utils import logging __UpperCAmelCase : str = logging.get_logger(__name__) def a ( SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[int] ): """simple docstring""" UpperCamelCase : Union[str, Any] = nn.functional.normalize(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Any = nn.functional.normalize(SCREAMING_SNAKE_CASE_ ) return torch.mm(SCREAMING_SNAKE_CASE_ , normalized_text_embeds.t() ) class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : List[str] = CLIPConfig __UpperCamelCase : Optional[int] = ["CLIPEncoderLayer"] def __init__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = CLIPVisionModel(config.vision_config ) UpperCamelCase : List[str] = nn.Linear(config.vision_config.hidden_size , config.projection_dim , bias=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = nn.Parameter(torch.ones(17 , config.projection_dim ) , requires_grad=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = nn.Parameter(torch.ones(3 , config.projection_dim ) , requires_grad=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = nn.Parameter(torch.ones(17 ) , requires_grad=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = nn.Parameter(torch.ones(3 ) , requires_grad=__SCREAMING_SNAKE_CASE ) @torch.no_grad() def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Tuple = self.vision_model(__SCREAMING_SNAKE_CASE )[1] # pooled_output UpperCamelCase : Union[str, Any] = self.visual_projection(__SCREAMING_SNAKE_CASE ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 UpperCamelCase : Optional[int] = cosine_distance(__SCREAMING_SNAKE_CASE , self.special_care_embeds ).cpu().float().numpy() UpperCamelCase : List[Any] = cosine_distance(__SCREAMING_SNAKE_CASE , self.concept_embeds ).cpu().float().numpy() UpperCamelCase : Dict = [] UpperCamelCase : List[str] = image_embeds.shape[0] for i in range(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Optional[Any] = {'''special_scores''': {}, '''special_care''': [], '''concept_scores''': {}, '''bad_concepts''': []} # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign images UpperCamelCase : Optional[int] = 0.0 for concept_idx in range(len(special_cos_dist[0] ) ): UpperCamelCase : List[str] = special_cos_dist[i][concept_idx] UpperCamelCase : Optional[Any] = self.special_care_embeds_weights[concept_idx].item() UpperCamelCase : Any = round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["special_scores"][concept_idx] > 0: result_img["special_care"].append({concept_idx, result_img['''special_scores'''][concept_idx]} ) UpperCamelCase : Optional[int] = 0.01 for concept_idx in range(len(cos_dist[0] ) ): UpperCamelCase : Optional[int] = cos_dist[i][concept_idx] UpperCamelCase : List[str] = self.concept_embeds_weights[concept_idx].item() UpperCamelCase : Any = round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["concept_scores"][concept_idx] > 0: result_img["bad_concepts"].append(__SCREAMING_SNAKE_CASE ) result.append(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = [len(res['''bad_concepts'''] ) > 0 for res in result] return images, has_nsfw_concepts @torch.no_grad() def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = self.vision_model(__SCREAMING_SNAKE_CASE )[1] # pooled_output UpperCamelCase : int = self.visual_projection(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = cosine_distance(__SCREAMING_SNAKE_CASE , self.special_care_embeds ) UpperCamelCase : str = cosine_distance(__SCREAMING_SNAKE_CASE , self.concept_embeds ) # increase this value to create a stronger `nsfw` filter # at the cost of increasing the possibility of filtering benign images UpperCamelCase : Union[str, Any] = 0.0 UpperCamelCase : Optional[Any] = special_cos_dist - self.special_care_embeds_weights + adjustment # special_scores = special_scores.round(decimals=3) UpperCamelCase : Optional[Any] = torch.any(special_scores > 0 , dim=1 ) UpperCamelCase : int = special_care * 0.01 UpperCamelCase : Tuple = special_adjustment.unsqueeze(1 ).expand(-1 , cos_dist.shape[1] ) UpperCamelCase : Optional[int] = (cos_dist - self.concept_embeds_weights) + special_adjustment # concept_scores = concept_scores.round(decimals=3) UpperCamelCase : List[str] = torch.any(concept_scores > 0 , dim=1 ) return images, has_nsfw_concepts
315
0
import numpy as np from transformers import Pipeline def a ( SCREAMING_SNAKE_CASE_ : Optional[Any] ): """simple docstring""" UpperCamelCase : List[str] = np.max(SCREAMING_SNAKE_CASE_ , axis=-1 , keepdims=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Tuple = np.exp(outputs - maxes ) return shifted_exp / shifted_exp.sum(axis=-1 , keepdims=SCREAMING_SNAKE_CASE_ ) class UpperCAmelCase_ ( _a): '''simple docstring''' def _lowercase ( self , **__SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : str = {} if "second_text" in kwargs: UpperCamelCase : Union[str, Any] = kwargs['''second_text'''] return preprocess_kwargs, {}, {} def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" return self.tokenizer(__SCREAMING_SNAKE_CASE , text_pair=__SCREAMING_SNAKE_CASE , return_tensors=self.framework ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return self.model(**__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Tuple = model_outputs.logits[0].numpy() UpperCamelCase : Optional[int] = softmax(__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = np.argmax(__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = self.model.config.idalabel[best_class] UpperCamelCase : str = probabilities[best_class].item() UpperCamelCase : Optional[Any] = logits.tolist() return {"label": label, "score": score, "logits": logits}
356
from argparse import ArgumentParser from .env import EnvironmentCommand def a ( ): """simple docstring""" UpperCamelCase : Any = ArgumentParser('''Diffusers CLI tool''' , usage='''diffusers-cli <command> [<args>]''' ) UpperCamelCase : Tuple = parser.add_subparsers(help='''diffusers-cli command helpers''' ) # Register commands EnvironmentCommand.register_subcommand(SCREAMING_SNAKE_CASE_ ) # Let's go UpperCamelCase : List[Any] = parser.parse_args() if not hasattr(SCREAMING_SNAKE_CASE_ , '''func''' ): parser.print_help() exit(1 ) # Run UpperCamelCase : str = args.func(SCREAMING_SNAKE_CASE_ ) service.run() if __name__ == "__main__": main()
315
0
import logging import os import sys from dataclasses import dataclass, field from typing import Optional import torch from datasets import load_dataset from torchvision.transforms import Compose, Lambda, Normalize, RandomHorizontalFlip, RandomResizedCrop, ToTensor from torchvision.transforms.functional import InterpolationMode import transformers from transformers import ( HfArgumentParser, Trainer, TrainingArguments, ViTImageProcessor, ViTMAEConfig, ViTMAEForPreTraining, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version __UpperCAmelCase : Optional[int] = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.31.0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/image-pretraining/requirements.txt") @dataclass class UpperCAmelCase_ : '''simple docstring''' __UpperCamelCase : Optional[str] = field( default="cifar10", metadata={"help": "Name of a dataset from the datasets package"}) __UpperCamelCase : Optional[str] = field( default=_a, metadata={"help": "The configuration name of the dataset to use (via the datasets library)."}) __UpperCamelCase : Optional[str] = field( default=_a, metadata={"help": "The column name of the images in the files."}) __UpperCamelCase : Optional[str] = field(default=_a, metadata={"help": "A folder containing the training data."}) __UpperCamelCase : Optional[str] = field(default=_a, metadata={"help": "A folder containing the validation data."}) __UpperCamelCase : Optional[float] = field( default=0.1_5, metadata={"help": "Percent to split off of train for validation."}) __UpperCamelCase : Optional[int] = field( default=_a, metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of training examples to this " "value if set." ) }, ) __UpperCamelCase : Optional[int] = field( default=_a, metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of evaluation examples to this " "value if set." ) }, ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[Any] = {} if self.train_dir is not None: UpperCamelCase : Union[str, Any] = self.train_dir if self.validation_dir is not None: UpperCamelCase : Any = self.validation_dir UpperCamelCase : List[Any] = data_files if data_files else None @dataclass class UpperCAmelCase_ : '''simple docstring''' __UpperCamelCase : str = field( default=_a, metadata={ "help": ( "The model checkpoint for weights initialization.Don't set if you want to train a model from scratch." ) }, ) __UpperCamelCase : Optional[str] = field( default=_a, metadata={"help": "Pretrained config name or path if not the same as model_name_or_path"}) __UpperCamelCase : Optional[str] = field( default=_a, metadata={ "help": ( "Override some existing default config settings when a model is trained from scratch. Example: " "n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index" ) }, ) __UpperCamelCase : Optional[str] = field( default=_a, metadata={"help": "Where do you want to store the pretrained models downloaded from s3"}) __UpperCamelCase : str = field( default="main", metadata={"help": "The specific model version to use (can be a branch name, tag name or commit id)."}, ) __UpperCamelCase : str = field(default=_a, metadata={"help": "Name or path of preprocessor config."}) __UpperCamelCase : bool = field( default=_a, metadata={ "help": ( "Will use the token generated when running `huggingface-cli login` (necessary to use this script " "with private models)." ) }, ) __UpperCamelCase : float = field( default=0.7_5, metadata={"help": "The ratio of the number of masked tokens in the input sequence."}) __UpperCamelCase : bool = field( default=_a, metadata={"help": "Whether or not to train with normalized pixel values as target."}) @dataclass class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : float = field( default=1E-3, metadata={"help": "Base learning rate: absolute_lr = base_lr * total_batch_size / 256."}) def a ( SCREAMING_SNAKE_CASE_ : List[Any] ): """simple docstring""" UpperCamelCase : Any = torch.stack([example['''pixel_values'''] for example in examples] ) return {"pixel_values": pixel_values} def a ( ): """simple docstring""" UpperCamelCase : int = HfArgumentParser((ModelArguments, DataTrainingArguments, CustomTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('''.json''' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. UpperCamelCase : Dict = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCamelCase : List[Any] = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry('''run_mae''' , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' , datefmt='''%m/%d/%Y %H:%M:%S''' , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() UpperCamelCase : List[str] = training_args.get_process_log_level() logger.setLevel(SCREAMING_SNAKE_CASE_ ) transformers.utils.logging.set_verbosity(SCREAMING_SNAKE_CASE_ ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}""" + F"""distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}""" ) logger.info(F"""Training/evaluation parameters {training_args}""" ) # Detecting last checkpoint. UpperCamelCase : int = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: UpperCamelCase : Optional[int] = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F"""Output directory ({training_args.output_dir}) already exists and is not empty. """ '''Use --overwrite_output_dir to overcome.''' ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F"""Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change """ '''the `--output_dir` or add `--overwrite_output_dir` to train from scratch.''' ) # Initialize our dataset. UpperCamelCase : int = load_dataset( data_args.dataset_name , data_args.dataset_config_name , data_files=data_args.data_files , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) # If we don't have a validation split, split off a percentage of train as validation. UpperCamelCase : List[str] = None if '''validation''' in ds.keys() else data_args.train_val_split if isinstance(data_args.train_val_split , SCREAMING_SNAKE_CASE_ ) and data_args.train_val_split > 0.0: UpperCamelCase : Optional[int] = ds['''train'''].train_test_split(data_args.train_val_split ) UpperCamelCase : List[str] = split['''train'''] UpperCamelCase : Dict = split['''test'''] # Load pretrained model and image processor # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. UpperCamelCase : int = { '''cache_dir''': model_args.cache_dir, '''revision''': model_args.model_revision, '''use_auth_token''': True if model_args.use_auth_token else None, } if model_args.config_name: UpperCamelCase : List[Any] = ViTMAEConfig.from_pretrained(model_args.config_name , **SCREAMING_SNAKE_CASE_ ) elif model_args.model_name_or_path: UpperCamelCase : Union[str, Any] = ViTMAEConfig.from_pretrained(model_args.model_name_or_path , **SCREAMING_SNAKE_CASE_ ) else: UpperCamelCase : List[str] = ViTMAEConfig() logger.warning('''You are instantiating a new config instance from scratch.''' ) if model_args.config_overrides is not None: logger.info(F"""Overriding config: {model_args.config_overrides}""" ) config.update_from_string(model_args.config_overrides ) logger.info(F"""New config: {config}""" ) # adapt config config.update( { '''mask_ratio''': model_args.mask_ratio, '''norm_pix_loss''': model_args.norm_pix_loss, } ) # create image processor if model_args.image_processor_name: UpperCamelCase : List[Any] = ViTImageProcessor.from_pretrained(model_args.image_processor_name , **SCREAMING_SNAKE_CASE_ ) elif model_args.model_name_or_path: UpperCamelCase : Optional[Any] = ViTImageProcessor.from_pretrained(model_args.model_name_or_path , **SCREAMING_SNAKE_CASE_ ) else: UpperCamelCase : Dict = ViTImageProcessor() # create model if model_args.model_name_or_path: UpperCamelCase : Optional[int] = ViTMAEForPreTraining.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=SCREAMING_SNAKE_CASE_ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info('''Training new model from scratch''' ) UpperCamelCase : int = ViTMAEForPreTraining(SCREAMING_SNAKE_CASE_ ) if training_args.do_train: UpperCamelCase : Optional[Any] = ds['''train'''].column_names else: UpperCamelCase : Dict = ds['''validation'''].column_names if data_args.image_column_name is not None: UpperCamelCase : Any = data_args.image_column_name elif "image" in column_names: UpperCamelCase : Tuple = '''image''' elif "img" in column_names: UpperCamelCase : Any = '''img''' else: UpperCamelCase : Any = column_names[0] # transformations as done in original MAE paper # source: https://github.com/facebookresearch/mae/blob/main/main_pretrain.py if "shortest_edge" in image_processor.size: UpperCamelCase : Tuple = image_processor.size['''shortest_edge'''] else: UpperCamelCase : Tuple = (image_processor.size['''height'''], image_processor.size['''width''']) UpperCamelCase : List[str] = Compose( [ Lambda(lambda SCREAMING_SNAKE_CASE_ : img.convert('''RGB''' ) if img.mode != "RGB" else img ), RandomResizedCrop(SCREAMING_SNAKE_CASE_ , scale=(0.2, 1.0) , interpolation=InterpolationMode.BICUBIC ), RandomHorizontalFlip(), ToTensor(), Normalize(mean=image_processor.image_mean , std=image_processor.image_std ), ] ) def preprocess_images(SCREAMING_SNAKE_CASE_ : Optional[int] ): UpperCamelCase : str = [transforms(SCREAMING_SNAKE_CASE_ ) for image in examples[image_column_name]] return examples if training_args.do_train: if "train" not in ds: raise ValueError('''--do_train requires a train dataset''' ) if data_args.max_train_samples is not None: UpperCamelCase : List[Any] = ds['''train'''].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) # Set the training transforms ds["train"].set_transform(SCREAMING_SNAKE_CASE_ ) if training_args.do_eval: if "validation" not in ds: raise ValueError('''--do_eval requires a validation dataset''' ) if data_args.max_eval_samples is not None: UpperCamelCase : List[str] = ( ds['''validation'''].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms ds["validation"].set_transform(SCREAMING_SNAKE_CASE_ ) # Compute absolute learning rate UpperCamelCase : Optional[Any] = ( training_args.train_batch_size * training_args.gradient_accumulation_steps * training_args.world_size ) if training_args.base_learning_rate is not None: UpperCamelCase : Tuple = training_args.base_learning_rate * total_train_batch_size / 2_5_6 # Initialize our trainer UpperCamelCase : List[str] = Trainer( model=SCREAMING_SNAKE_CASE_ , args=SCREAMING_SNAKE_CASE_ , train_dataset=ds['''train'''] if training_args.do_train else None , eval_dataset=ds['''validation'''] if training_args.do_eval else None , tokenizer=SCREAMING_SNAKE_CASE_ , data_collator=SCREAMING_SNAKE_CASE_ , ) # Training if training_args.do_train: UpperCamelCase : str = None if training_args.resume_from_checkpoint is not None: UpperCamelCase : Dict = training_args.resume_from_checkpoint elif last_checkpoint is not None: UpperCamelCase : Optional[Any] = last_checkpoint UpperCamelCase : Dict = trainer.train(resume_from_checkpoint=SCREAMING_SNAKE_CASE_ ) trainer.save_model() trainer.log_metrics('''train''' , train_result.metrics ) trainer.save_metrics('''train''' , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: UpperCamelCase : int = trainer.evaluate() trainer.log_metrics('''eval''' , SCREAMING_SNAKE_CASE_ ) trainer.save_metrics('''eval''' , SCREAMING_SNAKE_CASE_ ) # Write model card and (optionally) push to hub UpperCamelCase : List[str] = { '''tasks''': '''masked-auto-encoding''', '''dataset''': data_args.dataset_name, '''tags''': ['''masked-auto-encoding'''], } if training_args.push_to_hub: trainer.push_to_hub(**SCREAMING_SNAKE_CASE_ ) else: trainer.create_model_card(**SCREAMING_SNAKE_CASE_ ) def a ( SCREAMING_SNAKE_CASE_ : List[Any] ): """simple docstring""" main() if __name__ == "__main__": main()
357
def a ( SCREAMING_SNAKE_CASE_ : str ): """simple docstring""" return "".join(chr(ord(SCREAMING_SNAKE_CASE_ ) - 3_2 ) if '''a''' <= char <= '''z''' else char for char in word ) if __name__ == "__main__": from doctest import testmod testmod()
315
0
"""simple docstring""" import argparse import re import numpy as np import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( SamConfig, SamImageProcessor, SamModel, SamProcessor, SamVisionConfig, ) __UpperCAmelCase : Any = { "iou_prediction_head.layers.0": "iou_prediction_head.proj_in", "iou_prediction_head.layers.1": "iou_prediction_head.layers.0", "iou_prediction_head.layers.2": "iou_prediction_head.proj_out", "mask_decoder.output_upscaling.0": "mask_decoder.upscale_conv1", "mask_decoder.output_upscaling.1": "mask_decoder.upscale_layer_norm", "mask_decoder.output_upscaling.3": "mask_decoder.upscale_conv2", "mask_downscaling.0": "mask_embed.conv1", "mask_downscaling.1": "mask_embed.layer_norm1", "mask_downscaling.3": "mask_embed.conv2", "mask_downscaling.4": "mask_embed.layer_norm2", "mask_downscaling.6": "mask_embed.conv3", "point_embeddings": "point_embed", "pe_layer.positional_encoding_gaussian_matrix": "shared_embedding.positional_embedding", "image_encoder": "vision_encoder", "neck.0": "neck.conv1", "neck.1": "neck.layer_norm1", "neck.2": "neck.conv2", "neck.3": "neck.layer_norm2", "patch_embed.proj": "patch_embed.projection", ".norm": ".layer_norm", "blocks": "layers", } def a ( SCREAMING_SNAKE_CASE_ : Optional[int] ): """simple docstring""" UpperCamelCase : List[Any] = {} state_dict.pop('''pixel_mean''' , SCREAMING_SNAKE_CASE_ ) state_dict.pop('''pixel_std''' , SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Any = R'''.*.output_hypernetworks_mlps.(\d+).layers.(\d+).*''' for key, value in state_dict.items(): for key_to_modify, new_key in KEYS_TO_MODIFY_MAPPING.items(): if key_to_modify in key: UpperCamelCase : List[str] = key.replace(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if re.match(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): UpperCamelCase : Dict = int(re.match(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).group(2 ) ) if layer_nb == 0: UpperCamelCase : Optional[Any] = key.replace('''layers.0''' , '''proj_in''' ) elif layer_nb == 1: UpperCamelCase : Any = key.replace('''layers.1''' , '''layers.0''' ) elif layer_nb == 2: UpperCamelCase : Optional[int] = key.replace('''layers.2''' , '''proj_out''' ) UpperCamelCase : str = value UpperCamelCase : Optional[int] = model_state_dict[ '''prompt_encoder.shared_embedding.positional_embedding''' ] return model_state_dict def a ( SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Dict="ybelkada/segment-anything" ): """simple docstring""" UpperCamelCase : Dict = hf_hub_download(SCREAMING_SNAKE_CASE_ , F"""checkpoints/{model_name}.pth""" ) if "sam_vit_b" in model_name: UpperCamelCase : Any = SamConfig() elif "sam_vit_l" in model_name: UpperCamelCase : Union[str, Any] = SamVisionConfig( hidden_size=1_0_2_4 , num_hidden_layers=2_4 , num_attention_heads=1_6 , global_attn_indexes=[5, 1_1, 1_7, 2_3] , ) UpperCamelCase : Optional[int] = SamConfig( vision_config=SCREAMING_SNAKE_CASE_ , ) elif "sam_vit_h" in model_name: UpperCamelCase : List[Any] = SamVisionConfig( hidden_size=1_2_8_0 , num_hidden_layers=3_2 , num_attention_heads=1_6 , global_attn_indexes=[7, 1_5, 2_3, 3_1] , ) UpperCamelCase : Optional[Any] = SamConfig( vision_config=SCREAMING_SNAKE_CASE_ , ) UpperCamelCase : Any = torch.load(SCREAMING_SNAKE_CASE_ , map_location='''cpu''' ) UpperCamelCase : Tuple = replace_keys(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Union[str, Any] = SamImageProcessor() UpperCamelCase : Dict = SamProcessor(image_processor=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Dict = SamModel(SCREAMING_SNAKE_CASE_ ) hf_model.load_state_dict(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Any = hf_model.to('''cuda''' ) UpperCamelCase : List[str] = '''https://huggingface.co/ybelkada/segment-anything/resolve/main/assets/car.png''' UpperCamelCase : Optional[int] = Image.open(requests.get(SCREAMING_SNAKE_CASE_ , stream=SCREAMING_SNAKE_CASE_ ).raw ).convert('''RGB''' ) UpperCamelCase : List[str] = [[[4_0_0, 6_5_0]]] UpperCamelCase : List[Any] = [[1]] UpperCamelCase : str = processor(images=np.array(SCREAMING_SNAKE_CASE_ ) , return_tensors='''pt''' ).to('''cuda''' ) with torch.no_grad(): UpperCamelCase : Dict = hf_model(**SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Tuple = output.iou_scores.squeeze() if model_name == "sam_vit_h_4b8939": assert scores[-1].item() == 0.579890251159668 UpperCamelCase : List[str] = processor( images=np.array(SCREAMING_SNAKE_CASE_ ) , input_points=SCREAMING_SNAKE_CASE_ , input_labels=SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ).to('''cuda''' ) with torch.no_grad(): UpperCamelCase : str = hf_model(**SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Dict = output.iou_scores.squeeze() assert scores[-1].item() == 0.9712603092193604 UpperCamelCase : List[Any] = ((7_5, 2_7_5, 1_7_2_5, 8_5_0),) UpperCamelCase : str = processor(images=np.array(SCREAMING_SNAKE_CASE_ ) , input_boxes=SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ).to('''cuda''' ) with torch.no_grad(): UpperCamelCase : Optional[Any] = hf_model(**SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Dict = output.iou_scores.squeeze() assert scores[-1].item() == 0.8686015605926514 # Test with 2 points and 1 image. UpperCamelCase : Union[str, Any] = [[[4_0_0, 6_5_0], [8_0_0, 6_5_0]]] UpperCamelCase : List[Any] = [[1, 1]] UpperCamelCase : int = processor( images=np.array(SCREAMING_SNAKE_CASE_ ) , input_points=SCREAMING_SNAKE_CASE_ , input_labels=SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ).to('''cuda''' ) with torch.no_grad(): UpperCamelCase : Optional[Any] = hf_model(**SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[str] = output.iou_scores.squeeze() assert scores[-1].item() == 0.9936047792434692 if __name__ == "__main__": __UpperCAmelCase : List[str] = argparse.ArgumentParser() __UpperCAmelCase : Any = ["sam_vit_b_01ec64", "sam_vit_h_4b8939", "sam_vit_l_0b3195"] parser.add_argument( "--model_name", default="sam_vit_h_4b8939", choices=choices, type=str, help="Path to hf config.json of model to convert", ) parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument( "--push_to_hub", action="store_true", help="Whether to push the model and processor to the hub after converting", ) parser.add_argument( "--model_hub_id", default="ybelkada/segment-anything", choices=choices, type=str, help="Path to hf config.json of model to convert", ) __UpperCAmelCase : str = parser.parse_args() convert_sam_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub, args.model_hub_id)
358
import math def a ( SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" assert isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) and ( number >= 0 ), "'number' must been an int and positive" if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or not number % 2: # Negatives, 0, 1 and all even numbers are not primes return False UpperCamelCase : Union[str, Any] = range(3 , int(math.sqrt(SCREAMING_SNAKE_CASE_ ) + 1 ) , 2 ) return not any(not number % i for i in odd_numbers ) def a ( SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Tuple=1 , **SCREAMING_SNAKE_CASE_ : Tuple ): """simple docstring""" UpperCamelCase : Tuple = factor * value UpperCamelCase : Optional[int] = value while not is_prime(SCREAMING_SNAKE_CASE_ ): value += 1 if not ("desc" in kwargs and kwargs["desc"] is True) else -1 if value == first_value_val: return next_prime(value + 1 , **SCREAMING_SNAKE_CASE_ ) return value
315
0
class UpperCAmelCase_ : '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[Any] = n UpperCamelCase : Tuple = [None] * self.n UpperCamelCase : Union[str, Any] = 0 # index of the first element UpperCamelCase : Union[str, Any] = 0 UpperCamelCase : Any = 0 def __len__( self ): """simple docstring""" return self.size def _lowercase ( self ): """simple docstring""" return self.size == 0 def _lowercase ( self ): """simple docstring""" return False if self.is_empty() else self.array[self.front] def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if self.size >= self.n: raise Exception('''QUEUE IS FULL''' ) UpperCamelCase : Dict = data UpperCamelCase : Dict = (self.rear + 1) % self.n self.size += 1 return self def _lowercase ( self ): """simple docstring""" if self.size == 0: raise Exception('''UNDERFLOW''' ) UpperCamelCase : int = self.array[self.front] UpperCamelCase : str = None UpperCamelCase : Union[str, Any] = (self.front + 1) % self.n self.size -= 1 return temp
359
import warnings from ...utils import logging from .image_processing_imagegpt import ImageGPTImageProcessor __UpperCAmelCase : Optional[int] = logging.get_logger(__name__) class UpperCAmelCase_ ( _a): '''simple docstring''' def __init__( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): """simple docstring""" warnings.warn( '''The class ImageGPTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.''' ''' Please use ImageGPTImageProcessor instead.''' , __SCREAMING_SNAKE_CASE , ) super().__init__(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE )
315
0
import os import tempfile import unittest from transformers.models.marian.convert_marian_tatoeba_to_pytorch import DEFAULT_REPO, TatoebaConverter from transformers.testing_utils import slow from transformers.utils import cached_property @unittest.skipUnless(os.path.exists(_a), "Tatoeba directory does not exist.") class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' @cached_property def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = tempfile.mkdtemp() return TatoebaConverter(save_dir=__SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" self.resolver.convert_models(['''heb-eng'''] ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = self.resolver.write_model_card('''opus-mt-he-en''' , dry_run=__SCREAMING_SNAKE_CASE ) assert mmeta["long_pair"] == "heb-eng"
360
import unittest import numpy as np from transformers.testing_utils import require_pytesseract, require_torch from transformers.utils import is_pytesseract_available, is_torch_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_pytesseract_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=18 , __SCREAMING_SNAKE_CASE=30 , __SCREAMING_SNAKE_CASE=400 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=True , ): """simple docstring""" UpperCamelCase : List[str] = size if size is not None else {'''height''': 18, '''width''': 18} UpperCamelCase : int = parent UpperCamelCase : List[Any] = batch_size UpperCamelCase : Optional[int] = num_channels UpperCamelCase : Union[str, Any] = image_size UpperCamelCase : Union[str, Any] = min_resolution UpperCamelCase : Tuple = max_resolution UpperCamelCase : List[str] = do_resize UpperCamelCase : List[str] = size UpperCamelCase : int = apply_ocr def _lowercase ( self ): """simple docstring""" return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class UpperCAmelCase_ ( _a, unittest.TestCase): '''simple docstring''' __UpperCamelCase : List[str] = LayoutLMvaImageProcessor if is_pytesseract_available() else None def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = LayoutLMvaImageProcessingTester(self ) @property def _lowercase ( self ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[int] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_resize''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''size''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''apply_ocr''' ) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 18, '''width''': 18} ) UpperCamelCase : Dict = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'''height''': 42, '''width''': 42} ) def _lowercase ( self ): """simple docstring""" pass def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images UpperCamelCase : str = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , Image.Image ) # Test not batched input UpperCamelCase : Optional[Any] = image_processing(image_inputs[0] , return_tensors='''pt''' ) self.assertEqual( encoding.pixel_values.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) self.assertIsInstance(encoding.words , __SCREAMING_SNAKE_CASE ) self.assertIsInstance(encoding.boxes , __SCREAMING_SNAKE_CASE ) # Test batched UpperCamelCase : int = image_processing(__SCREAMING_SNAKE_CASE , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Any = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors UpperCamelCase : int = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE , numpify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , np.ndarray ) # Test not batched input UpperCamelCase : Dict = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched UpperCamelCase : List[str] = image_processing(__SCREAMING_SNAKE_CASE , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors UpperCamelCase : Tuple = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE , torchify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) # Test not batched input UpperCamelCase : List[Any] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched UpperCamelCase : Optional[int] = image_processing(__SCREAMING_SNAKE_CASE , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = LayoutLMvaImageProcessor() from datasets import load_dataset UpperCamelCase : Dict = load_dataset('''hf-internal-testing/fixtures_docvqa''' , split='''test''' ) UpperCamelCase : List[Any] = Image.open(ds[0]['''file'''] ).convert('''RGB''' ) UpperCamelCase : int = image_processing(__SCREAMING_SNAKE_CASE , return_tensors='''pt''' ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 224, 224) ) self.assertEqual(len(encoding.words ) , len(encoding.boxes ) ) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 UpperCamelCase : Union[str, Any] = [['''11:14''', '''to''', '''11:39''', '''a.m''', '''11:39''', '''to''', '''11:44''', '''a.m.''', '''11:44''', '''a.m.''', '''to''', '''12:25''', '''p.m.''', '''12:25''', '''to''', '''12:58''', '''p.m.''', '''12:58''', '''to''', '''4:00''', '''p.m.''', '''2:00''', '''to''', '''5:00''', '''p.m.''', '''Coffee''', '''Break''', '''Coffee''', '''will''', '''be''', '''served''', '''for''', '''men''', '''and''', '''women''', '''in''', '''the''', '''lobby''', '''adjacent''', '''to''', '''exhibit''', '''area.''', '''Please''', '''move''', '''into''', '''exhibit''', '''area.''', '''(Exhibits''', '''Open)''', '''TRRF''', '''GENERAL''', '''SESSION''', '''(PART''', '''|)''', '''Presiding:''', '''Lee''', '''A.''', '''Waller''', '''TRRF''', '''Vice''', '''President''', '''“Introductory''', '''Remarks”''', '''Lee''', '''A.''', '''Waller,''', '''TRRF''', '''Vice''', '''Presi-''', '''dent''', '''Individual''', '''Interviews''', '''with''', '''TRRF''', '''Public''', '''Board''', '''Members''', '''and''', '''Sci-''', '''entific''', '''Advisory''', '''Council''', '''Mem-''', '''bers''', '''Conducted''', '''by''', '''TRRF''', '''Treasurer''', '''Philip''', '''G.''', '''Kuehn''', '''to''', '''get''', '''answers''', '''which''', '''the''', '''public''', '''refrigerated''', '''warehousing''', '''industry''', '''is''', '''looking''', '''for.''', '''Plus''', '''questions''', '''from''', '''the''', '''floor.''', '''Dr.''', '''Emil''', '''M.''', '''Mrak,''', '''University''', '''of''', '''Cal-''', '''ifornia,''', '''Chairman,''', '''TRRF''', '''Board;''', '''Sam''', '''R.''', '''Cecil,''', '''University''', '''of''', '''Georgia''', '''College''', '''of''', '''Agriculture;''', '''Dr.''', '''Stanley''', '''Charm,''', '''Tufts''', '''University''', '''School''', '''of''', '''Medicine;''', '''Dr.''', '''Robert''', '''H.''', '''Cotton,''', '''ITT''', '''Continental''', '''Baking''', '''Company;''', '''Dr.''', '''Owen''', '''Fennema,''', '''University''', '''of''', '''Wis-''', '''consin;''', '''Dr.''', '''Robert''', '''E.''', '''Hardenburg,''', '''USDA.''', '''Questions''', '''and''', '''Answers''', '''Exhibits''', '''Open''', '''Capt.''', '''Jack''', '''Stoney''', '''Room''', '''TRRF''', '''Scientific''', '''Advisory''', '''Council''', '''Meeting''', '''Ballroom''', '''Foyer''']] # noqa: E231 UpperCamelCase : str = [[[141, 57, 214, 69], [228, 58, 252, 69], [141, 75, 216, 88], [230, 79, 280, 88], [142, 260, 218, 273], [230, 261, 255, 273], [143, 279, 218, 290], [231, 282, 290, 291], [143, 342, 218, 354], [231, 345, 289, 355], [202, 362, 227, 373], [143, 379, 220, 392], [231, 382, 291, 394], [144, 714, 220, 726], [231, 715, 256, 726], [144, 732, 220, 745], [232, 736, 291, 747], [144, 769, 218, 782], [231, 770, 256, 782], [141, 788, 202, 801], [215, 791, 274, 804], [143, 826, 204, 838], [215, 826, 240, 838], [142, 844, 202, 857], [215, 847, 274, 859], [334, 57, 427, 69], [440, 57, 522, 69], [369, 75, 461, 88], [469, 75, 516, 88], [528, 76, 562, 88], [570, 76, 667, 88], [675, 75, 711, 87], [721, 79, 778, 88], [789, 75, 840, 88], [369, 97, 470, 107], [484, 94, 507, 106], [518, 94, 562, 107], [576, 94, 655, 110], [668, 94, 792, 109], [804, 95, 829, 107], [369, 113, 465, 125], [477, 116, 547, 125], [562, 113, 658, 125], [671, 116, 748, 125], [761, 113, 811, 125], [369, 131, 465, 143], [477, 133, 548, 143], [563, 130, 698, 145], [710, 130, 802, 146], [336, 171, 412, 183], [423, 171, 572, 183], [582, 170, 716, 184], [728, 171, 817, 187], [829, 171, 844, 186], [338, 197, 482, 212], [507, 196, 557, 209], [569, 196, 595, 208], [610, 196, 702, 209], [505, 214, 583, 226], [595, 214, 656, 227], [670, 215, 807, 227], [335, 259, 543, 274], [556, 259, 708, 272], [372, 279, 422, 291], [435, 279, 460, 291], [474, 279, 574, 292], [587, 278, 664, 291], [676, 278, 738, 291], [751, 279, 834, 291], [372, 298, 434, 310], [335, 341, 483, 354], [497, 341, 655, 354], [667, 341, 728, 354], [740, 341, 825, 354], [335, 360, 430, 372], [442, 360, 534, 372], [545, 359, 687, 372], [697, 360, 754, 372], [765, 360, 823, 373], [334, 378, 428, 391], [440, 378, 577, 394], [590, 378, 705, 391], [720, 378, 801, 391], [334, 397, 400, 409], [370, 416, 529, 429], [544, 416, 576, 432], [587, 416, 665, 428], [677, 416, 814, 429], [372, 435, 452, 450], [465, 434, 495, 447], [511, 434, 600, 447], [611, 436, 637, 447], [649, 436, 694, 451], [705, 438, 824, 447], [369, 453, 452, 466], [464, 454, 509, 466], [522, 453, 611, 469], [625, 453, 792, 469], [370, 472, 556, 488], [570, 472, 684, 487], [697, 472, 718, 485], [732, 472, 835, 488], [369, 490, 411, 503], [425, 490, 484, 503], [496, 490, 635, 506], [645, 490, 707, 503], [718, 491, 761, 503], [771, 490, 840, 503], [336, 510, 374, 521], [388, 510, 447, 522], [460, 510, 489, 521], [503, 510, 580, 522], [592, 509, 736, 525], [745, 509, 770, 522], [781, 509, 840, 522], [338, 528, 434, 541], [448, 528, 596, 541], [609, 527, 687, 540], [700, 528, 792, 541], [336, 546, 397, 559], [407, 546, 431, 559], [443, 546, 525, 560], [537, 546, 680, 562], [688, 546, 714, 559], [722, 546, 837, 562], [336, 565, 449, 581], [461, 565, 485, 577], [497, 565, 665, 581], [681, 565, 718, 577], [732, 565, 837, 580], [337, 584, 438, 597], [452, 583, 521, 596], [535, 584, 677, 599], [690, 583, 787, 596], [801, 583, 825, 596], [338, 602, 478, 615], [492, 602, 530, 614], [543, 602, 638, 615], [650, 602, 676, 614], [688, 602, 788, 615], [802, 602, 843, 614], [337, 621, 502, 633], [516, 621, 615, 637], [629, 621, 774, 636], [789, 621, 827, 633], [337, 639, 418, 652], [432, 640, 571, 653], [587, 639, 731, 655], [743, 639, 769, 652], [780, 639, 841, 652], [338, 658, 440, 673], [455, 658, 491, 670], [508, 658, 602, 671], [616, 658, 638, 670], [654, 658, 835, 674], [337, 677, 429, 689], [337, 714, 482, 726], [495, 714, 548, 726], [561, 714, 683, 726], [338, 770, 461, 782], [474, 769, 554, 785], [489, 788, 562, 803], [576, 788, 643, 801], [656, 787, 751, 804], [764, 788, 844, 801], [334, 825, 421, 838], [430, 824, 574, 838], [584, 824, 723, 841], [335, 844, 450, 857], [464, 843, 583, 860], [628, 862, 755, 875], [769, 861, 848, 878]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words , __SCREAMING_SNAKE_CASE ) self.assertListEqual(encoding.boxes , __SCREAMING_SNAKE_CASE ) # with apply_OCR = False UpperCamelCase : Optional[Any] = LayoutLMvaImageProcessor(apply_ocr=__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = image_processing(__SCREAMING_SNAKE_CASE , return_tensors='''pt''' ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 224, 224) )
315
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __UpperCAmelCase : Union[str, Any] = { "configuration_blenderbot": [ "BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP", "BlenderbotConfig", "BlenderbotOnnxConfig", ], "tokenization_blenderbot": ["BlenderbotTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : List[Any] = ["BlenderbotTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : List[Any] = [ "BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST", "BlenderbotForCausalLM", "BlenderbotForConditionalGeneration", "BlenderbotModel", "BlenderbotPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : List[str] = [ "TFBlenderbotForConditionalGeneration", "TFBlenderbotModel", "TFBlenderbotPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : List[Any] = [ "FlaxBlenderbotForConditionalGeneration", "FlaxBlenderbotModel", "FlaxBlenderbotPreTrainedModel", ] if TYPE_CHECKING: from .configuration_blenderbot import ( BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotConfig, BlenderbotOnnxConfig, ) from .tokenization_blenderbot import BlenderbotTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_blenderbot_fast import BlenderbotTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blenderbot import ( BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST, BlenderbotForCausalLM, BlenderbotForConditionalGeneration, BlenderbotModel, BlenderbotPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blenderbot import ( TFBlenderbotForConditionalGeneration, TFBlenderbotModel, TFBlenderbotPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, FlaxBlenderbotPreTrainedModel, ) else: import sys __UpperCAmelCase : Optional[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
361
import numpy as np from sklearn.datasets import fetch_california_housing from sklearn.metrics import mean_absolute_error, mean_squared_error from sklearn.model_selection import train_test_split from xgboost import XGBRegressor def a ( SCREAMING_SNAKE_CASE_ : dict ): """simple docstring""" return (data["data"], data["target"]) def a ( SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : np.ndarray ): """simple docstring""" UpperCamelCase : Optional[Any] = XGBRegressor(verbosity=0 , random_state=4_2 ) xgb.fit(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # Predict target for test data UpperCamelCase : Any = xgb.predict(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[str] = predictions.reshape(len(SCREAMING_SNAKE_CASE_ ) , 1 ) return predictions def a ( ): """simple docstring""" UpperCamelCase : Tuple = fetch_california_housing() UpperCamelCase , UpperCamelCase : Tuple = data_handling(SCREAMING_SNAKE_CASE_ ) UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase : List[str] = train_test_split( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , test_size=0.25 , random_state=1 ) UpperCamelCase : Optional[Any] = xgboost(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # Error printing print(F"""Mean Absolute Error : {mean_absolute_error(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )}""" ) print(F"""Mean Square Error : {mean_squared_error(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )}""" ) if __name__ == "__main__": import doctest doctest.testmod(verbose=True) main()
315
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available __UpperCAmelCase : Any = {"tokenization_herbert": ["HerbertTokenizer"]} try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : Union[str, Any] = ["HerbertTokenizerFast"] if TYPE_CHECKING: from .tokenization_herbert import HerbertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_herbert_fast import HerbertTokenizerFast else: import sys __UpperCAmelCase : List[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
362
__UpperCAmelCase : str = "\n# Transformers installation\n! pip install transformers datasets\n# To install from source instead of the last release, comment the command above and uncomment the following one.\n# ! pip install git+https://github.com/huggingface/transformers.git\n" __UpperCAmelCase : Dict = [{"type": "code", "content": INSTALL_CONTENT}] __UpperCAmelCase : Union[str, Any] = { "{processor_class}": "FakeProcessorClass", "{model_class}": "FakeModelClass", "{object_class}": "FakeObjectClass", }
315
0
from __future__ import annotations import random # Maximum size of the population. Bigger could be faster but is more memory expensive. __UpperCAmelCase : List[Any] = 200 # Number of elements selected in every generation of evolution. The selection takes # place from best to worst of that generation and must be smaller than N_POPULATION. __UpperCAmelCase : List[Any] = 50 # Probability that an element of a generation can mutate, changing one of its genes. # This will guarantee that all genes will be used during evolution. __UpperCAmelCase : Tuple = 0.4 # Just a seed to improve randomness required by the algorithm. random.seed(random.randint(0, 1000)) def a ( SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : str ): """simple docstring""" UpperCamelCase : List[str] = len([g for position, g in enumerate(SCREAMING_SNAKE_CASE_ ) if g == main_target[position]] ) return (item, float(SCREAMING_SNAKE_CASE_ )) def a ( SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : str ): """simple docstring""" UpperCamelCase : Tuple = random.randint(0 , len(SCREAMING_SNAKE_CASE_ ) - 1 ) UpperCamelCase : Optional[int] = parent_a[:random_slice] + parent_a[random_slice:] UpperCamelCase : Optional[int] = parent_a[:random_slice] + parent_a[random_slice:] return (child_a, child_a) def a ( SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : list[str] ): """simple docstring""" UpperCamelCase : List[str] = list(SCREAMING_SNAKE_CASE_ ) if random.uniform(0 , 1 ) < MUTATION_PROBABILITY: UpperCamelCase : Union[str, Any] = random.choice(SCREAMING_SNAKE_CASE_ ) return "".join(SCREAMING_SNAKE_CASE_ ) def a ( SCREAMING_SNAKE_CASE_ : tuple[str, float] , SCREAMING_SNAKE_CASE_ : list[tuple[str, float]] , SCREAMING_SNAKE_CASE_ : list[str] , ): """simple docstring""" UpperCamelCase : Union[str, Any] = [] # Generate more children proportionally to the fitness score. UpperCamelCase : Any = int(parent_a[1] * 1_0_0 ) + 1 UpperCamelCase : Any = 1_0 if child_n >= 1_0 else child_n for _ in range(SCREAMING_SNAKE_CASE_ ): UpperCamelCase : int = population_score[random.randint(0 , SCREAMING_SNAKE_CASE_ )][0] UpperCamelCase : Union[str, Any] = crossover(parent_a[0] , SCREAMING_SNAKE_CASE_ ) # Append new string to the population list. pop.append(mutate(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) pop.append(mutate(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) return pop def a ( SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : list[str] , SCREAMING_SNAKE_CASE_ : bool = True ): """simple docstring""" if N_POPULATION < N_SELECTED: UpperCamelCase : Dict = F"""{N_POPULATION} must be bigger than {N_SELECTED}""" raise ValueError(SCREAMING_SNAKE_CASE_ ) # Verify that the target contains no genes besides the ones inside genes variable. UpperCamelCase : Any = sorted({c for c in target if c not in genes} ) if not_in_genes_list: UpperCamelCase : Tuple = F"""{not_in_genes_list} is not in genes list, evolution cannot converge""" raise ValueError(SCREAMING_SNAKE_CASE_ ) # Generate random starting population. UpperCamelCase : Any = [] for _ in range(SCREAMING_SNAKE_CASE_ ): population.append(''''''.join([random.choice(SCREAMING_SNAKE_CASE_ ) for i in range(len(SCREAMING_SNAKE_CASE_ ) )] ) ) # Just some logs to know what the algorithms is doing. UpperCamelCase : Tuple = 0, 0 # This loop will end when we find a perfect match for our target. while True: generation += 1 total_population += len(SCREAMING_SNAKE_CASE_ ) # Random population created. Now it's time to evaluate. # Adding a bit of concurrency can make everything faster, # # import concurrent.futures # population_score: list[tuple[str, float]] = [] # with concurrent.futures.ThreadPoolExecutor( # max_workers=NUM_WORKERS) as executor: # futures = {executor.submit(evaluate, item) for item in population} # concurrent.futures.wait(futures) # population_score = [item.result() for item in futures] # # but with a simple algorithm like this, it will probably be slower. # We just need to call evaluate for every item inside the population. UpperCamelCase : Dict = [evaluate(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) for item in population] # Check if there is a matching evolution. UpperCamelCase : List[Any] = sorted(SCREAMING_SNAKE_CASE_ , key=lambda SCREAMING_SNAKE_CASE_ : x[1] , reverse=SCREAMING_SNAKE_CASE_ ) if population_score[0][0] == target: return (generation, total_population, population_score[0][0]) # Print the best result every 10 generation. # Just to know that the algorithm is working. if debug and generation % 1_0 == 0: print( F"""\nGeneration: {generation}""" F"""\nTotal Population:{total_population}""" F"""\nBest score: {population_score[0][1]}""" F"""\nBest string: {population_score[0][0]}""" ) # Flush the old population, keeping some of the best evolutions. # Keeping this avoid regression of evolution. UpperCamelCase : List[Any] = population[: int(N_POPULATION / 3 )] population.clear() population.extend(SCREAMING_SNAKE_CASE_ ) # Normalize population score to be between 0 and 1. UpperCamelCase : Any = [ (item, score / len(SCREAMING_SNAKE_CASE_ )) for item, score in population_score ] # This is selection for i in range(SCREAMING_SNAKE_CASE_ ): population.extend(select(population_score[int(SCREAMING_SNAKE_CASE_ )] , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) # Check if the population has already reached the maximum value and if so, # break the cycle. If this check is disabled, the algorithm will take # forever to compute large strings, but will also calculate small strings in # a far fewer generations. if len(SCREAMING_SNAKE_CASE_ ) > N_POPULATION: break if __name__ == "__main__": __UpperCAmelCase : Optional[int] = ( "This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!" ) __UpperCAmelCase : str = list( " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm" "nopqrstuvwxyz.,;!?+-*#@^'èéòà€ù=)(&%$£/\\" ) __UpperCAmelCase : int = basic(target_str, genes_list) print( f'''\nGeneration: {generation}\nTotal Population: {population}\nTarget: {target}''' )
363
import collections import os import re from pathlib import Path __UpperCAmelCase : List[str] = "src/transformers" # Matches is_xxx_available() __UpperCAmelCase : int = re.compile(r"is\_([a-z_]*)_available()") # Catches a one-line _import_struct = {xxx} __UpperCAmelCase : Optional[int] = re.compile(r"^_import_structure\s+=\s+\{([^\}]+)\}") # Catches a line with a key-values pattern: "bla": ["foo", "bar"] __UpperCAmelCase : List[Any] = re.compile(r"\s+\"\S*\":\s+\[([^\]]*)\]") # Catches a line if not is_foo_available __UpperCAmelCase : List[Any] = re.compile(r"^\s*if\s+not\s+is\_[a-z_]*\_available\(\)") # Catches a line _import_struct["bla"].append("foo") __UpperCAmelCase : str = re.compile(r"^\s*_import_structure\[\"\S*\"\]\.append\(\"(\S*)\"\)") # Catches a line _import_struct["bla"].extend(["foo", "bar"]) or _import_struct["bla"] = ["foo", "bar"] __UpperCAmelCase : Union[str, Any] = re.compile(r"^\s*_import_structure\[\S*\](?:\.extend\(|\s*=\s+)\[([^\]]*)\]") # Catches a line with an object between quotes and a comma: "MyModel", __UpperCAmelCase : Dict = re.compile(r"^\s+\"([^\"]+)\",") # Catches a line with objects between brackets only: ["foo", "bar"], __UpperCAmelCase : str = re.compile(r"^\s+\[([^\]]+)\]") # Catches a line with from foo import bar, bla, boo __UpperCAmelCase : str = re.compile(r"\s+from\s+\S*\s+import\s+([^\(\s].*)\n") # Catches a line with try: __UpperCAmelCase : Any = re.compile(r"^\s*try:") # Catches a line with else: __UpperCAmelCase : List[Any] = re.compile(r"^\s*else:") def a ( SCREAMING_SNAKE_CASE_ : Dict ): """simple docstring""" if _re_test_backend.search(SCREAMING_SNAKE_CASE_ ) is None: return None UpperCamelCase : Union[str, Any] = [b[0] for b in _re_backend.findall(SCREAMING_SNAKE_CASE_ )] backends.sort() return "_and_".join(SCREAMING_SNAKE_CASE_ ) def a ( SCREAMING_SNAKE_CASE_ : Tuple ): """simple docstring""" with open(SCREAMING_SNAKE_CASE_ , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: UpperCamelCase : Tuple = f.readlines() UpperCamelCase : Tuple = 0 while line_index < len(SCREAMING_SNAKE_CASE_ ) and not lines[line_index].startswith('''_import_structure = {''' ): line_index += 1 # If this is a traditional init, just return. if line_index >= len(SCREAMING_SNAKE_CASE_ ): return None # First grab the objects without a specific backend in _import_structure UpperCamelCase : List[Any] = [] while not lines[line_index].startswith('''if TYPE_CHECKING''' ) and find_backend(lines[line_index] ) is None: UpperCamelCase : Optional[int] = lines[line_index] # If we have everything on a single line, let's deal with it. if _re_one_line_import_struct.search(SCREAMING_SNAKE_CASE_ ): UpperCamelCase : Optional[int] = _re_one_line_import_struct.search(SCREAMING_SNAKE_CASE_ ).groups()[0] UpperCamelCase : str = re.findall(R'''\[([^\]]+)\]''' , SCREAMING_SNAKE_CASE_ ) for imp in imports: objects.extend([obj[1:-1] for obj in imp.split(''', ''' )] ) line_index += 1 continue UpperCamelCase : List[Any] = _re_import_struct_key_value.search(SCREAMING_SNAKE_CASE_ ) if single_line_import_search is not None: UpperCamelCase : List[str] = [obj[1:-1] for obj in single_line_import_search.groups()[0].split(''', ''' ) if len(SCREAMING_SNAKE_CASE_ ) > 0] objects.extend(SCREAMING_SNAKE_CASE_ ) elif line.startswith(''' ''' * 8 + '''"''' ): objects.append(line[9:-3] ) line_index += 1 UpperCamelCase : Dict = {'''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. UpperCamelCase : Dict = 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: UpperCamelCase : Optional[Any] = 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 UpperCamelCase : str = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(''' ''' * 4 ): UpperCamelCase : str = lines[line_index] if _re_import_struct_add_one.search(SCREAMING_SNAKE_CASE_ ) is not None: objects.append(_re_import_struct_add_one.search(SCREAMING_SNAKE_CASE_ ).groups()[0] ) elif _re_import_struct_add_many.search(SCREAMING_SNAKE_CASE_ ) is not None: UpperCamelCase : Union[str, Any] = _re_import_struct_add_many.search(SCREAMING_SNAKE_CASE_ ).groups()[0].split(''', ''' ) UpperCamelCase : List[Any] = [obj[1:-1] for obj in imports if len(SCREAMING_SNAKE_CASE_ ) > 0] objects.extend(SCREAMING_SNAKE_CASE_ ) elif _re_between_brackets.search(SCREAMING_SNAKE_CASE_ ) is not None: UpperCamelCase : str = _re_between_brackets.search(SCREAMING_SNAKE_CASE_ ).groups()[0].split(''', ''' ) UpperCamelCase : Dict = [obj[1:-1] for obj in imports if len(SCREAMING_SNAKE_CASE_ ) > 0] objects.extend(SCREAMING_SNAKE_CASE_ ) elif _re_quote_object.search(SCREAMING_SNAKE_CASE_ ) is not None: objects.append(_re_quote_object.search(SCREAMING_SNAKE_CASE_ ).groups()[0] ) elif line.startswith(''' ''' * 8 + '''"''' ): objects.append(line[9:-3] ) elif line.startswith(''' ''' * 1_2 + '''"''' ): objects.append(line[1_3:-3] ) line_index += 1 UpperCamelCase : Tuple = objects else: line_index += 1 # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend UpperCamelCase : int = [] while ( line_index < len(SCREAMING_SNAKE_CASE_ ) and find_backend(lines[line_index] ) is None and not lines[line_index].startswith('''else''' ) ): UpperCamelCase : Tuple = lines[line_index] UpperCamelCase : Any = _re_import.search(SCREAMING_SNAKE_CASE_ ) 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 UpperCamelCase : Any = {'''none''': objects} # Let's continue with backend-specific objects while line_index < len(SCREAMING_SNAKE_CASE_ ): # If the line is an if is_backend_available, we grab all objects associated. UpperCamelCase : Optional[Any] = 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: UpperCamelCase : Dict = 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 UpperCamelCase : Dict = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(''' ''' * 8 ): UpperCamelCase : Optional[Any] = lines[line_index] UpperCamelCase : str = _re_import.search(SCREAMING_SNAKE_CASE_ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(''', ''' ) ) elif line.startswith(''' ''' * 1_2 ): objects.append(line[1_2:-2] ) line_index += 1 UpperCamelCase : str = objects else: line_index += 1 return import_dict_objects, type_hint_objects def a ( SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : List[Any] ): """simple docstring""" def find_duplicates(SCREAMING_SNAKE_CASE_ : Any ): return [k for k, v in collections.Counter(SCREAMING_SNAKE_CASE_ ).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!"] UpperCamelCase : Dict = [] for key in import_dict_objects.keys(): UpperCamelCase : Union[str, Any] = find_duplicates(import_dict_objects[key] ) if duplicate_imports: errors.append(F"""Duplicate _import_structure definitions for: {duplicate_imports}""" ) UpperCamelCase : Dict = 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] ) ): UpperCamelCase : List[str] = '''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 ( ): """simple docstring""" UpperCamelCase : Any = [] for root, _, files in os.walk(SCREAMING_SNAKE_CASE_ ): if "__init__.py" in files: UpperCamelCase : int = os.path.join(SCREAMING_SNAKE_CASE_ , '''__init__.py''' ) UpperCamelCase : Optional[int] = parse_init(SCREAMING_SNAKE_CASE_ ) if objects is not None: UpperCamelCase : str = analyze_results(*SCREAMING_SNAKE_CASE_ ) if len(SCREAMING_SNAKE_CASE_ ) > 0: UpperCamelCase : List[Any] = F"""Problem in {fname}, both halves do not define the same objects.\n{errors[0]}""" failures.append('''\n'''.join(SCREAMING_SNAKE_CASE_ ) ) if len(SCREAMING_SNAKE_CASE_ ) > 0: raise ValueError('''\n\n'''.join(SCREAMING_SNAKE_CASE_ ) ) def a ( ): """simple docstring""" UpperCamelCase : Dict = [] for path, directories, files in os.walk(SCREAMING_SNAKE_CASE_ ): for folder in directories: # Ignore private modules if folder.startswith('''_''' ): directories.remove(SCREAMING_SNAKE_CASE_ ) continue # Ignore leftovers from branches (empty folders apart from pycache) if len(list((Path(SCREAMING_SNAKE_CASE_ ) / folder).glob('''*.py''' ) ) ) == 0: continue UpperCamelCase : List[str] = str((Path(SCREAMING_SNAKE_CASE_ ) / folder).relative_to(SCREAMING_SNAKE_CASE_ ) ) UpperCamelCase : str = short_path.replace(os.path.sep , '''.''' ) submodules.append(SCREAMING_SNAKE_CASE_ ) for fname in files: if fname == "__init__.py": continue UpperCamelCase : Tuple = str((Path(SCREAMING_SNAKE_CASE_ ) / fname).relative_to(SCREAMING_SNAKE_CASE_ ) ) UpperCamelCase : int = short_path.replace('''.py''' , '''''' ).replace(os.path.sep , '''.''' ) if len(submodule.split('''.''' ) ) == 1: submodules.append(SCREAMING_SNAKE_CASE_ ) return submodules __UpperCAmelCase : Optional[int] = [ "convert_pytorch_checkpoint_to_tf2", "modeling_flax_pytorch_utils", "models.esm.openfold_utils", ] def a ( ): """simple docstring""" from transformers.utils import direct_transformers_import UpperCamelCase : Tuple = direct_transformers_import(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : str = set(transformers._import_structure.keys() ) # This contains all the base keys of the _import_structure object defined in the init, but if the user is missing # some optional dependencies, they may not have all of them. Thus we read the init to read all additions and # (potentiall re-) add them. with open(os.path.join(SCREAMING_SNAKE_CASE_ , '''__init__.py''' ) , '''r''' ) as f: UpperCamelCase : List[Any] = f.read() import_structure_keys.update(set(re.findall(R'''import_structure\[\"([^\"]*)\"\]''' , SCREAMING_SNAKE_CASE_ ) ) ) UpperCamelCase : Union[str, Any] = [ module for module in get_transformers_submodules() if module not in IGNORE_SUBMODULES and module not in import_structure_keys ] if len(SCREAMING_SNAKE_CASE_ ) > 0: UpperCamelCase : str = '''\n'''.join(F"""- {module}""" for module in module_not_registered ) raise ValueError( '''The following submodules are not properly registed 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()
315
0
import inspect import math import tempfile import unittest import numpy as np from transformers import ViTMAEConfig 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 ViTMAEForPreTraining, ViTMAEModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class UpperCAmelCase_ : '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=13 , __SCREAMING_SNAKE_CASE=30 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=32 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=37 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=10 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=0.6 , __SCREAMING_SNAKE_CASE=None , ): """simple docstring""" UpperCamelCase : Tuple = parent UpperCamelCase : Dict = batch_size UpperCamelCase : List[str] = image_size UpperCamelCase : Any = patch_size UpperCamelCase : Optional[Any] = num_channels UpperCamelCase : Union[str, Any] = is_training UpperCamelCase : Optional[int] = use_labels UpperCamelCase : str = hidden_size UpperCamelCase : Any = num_hidden_layers UpperCamelCase : int = num_attention_heads UpperCamelCase : Union[str, Any] = intermediate_size UpperCamelCase : int = hidden_act UpperCamelCase : Union[str, Any] = hidden_dropout_prob UpperCamelCase : Optional[Any] = attention_probs_dropout_prob UpperCamelCase : int = type_sequence_label_size UpperCamelCase : Tuple = initializer_range UpperCamelCase : List[Any] = mask_ratio UpperCamelCase : Dict = scope # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above # (we add 1 for the [CLS] token) UpperCamelCase : Any = (image_size // patch_size) ** 2 UpperCamelCase : Optional[Any] = int(math.ceil((1 - mask_ratio) * (num_patches + 1) ) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCamelCase : Any = None if self.use_labels: UpperCamelCase : int = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase : Optional[int] = self.get_config() return config, pixel_values, labels def _lowercase ( self ): """simple docstring""" return ViTMAEConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=__SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , mask_ratio=self.mask_ratio , ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = ViTMAEModel(config=__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.eval() UpperCamelCase : Dict = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[str] = ViTMAEForPreTraining(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.eval() UpperCamelCase : Dict = model(__SCREAMING_SNAKE_CASE ) UpperCamelCase : str = (self.image_size // self.patch_size) ** 2 UpperCamelCase : Dict = self.patch_size**2 * self.num_channels self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) # test greyscale images UpperCamelCase : str = 1 UpperCamelCase : int = ViTMAEForPreTraining(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.eval() UpperCamelCase : str = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) UpperCamelCase : str = model(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = self.prepare_config_and_inputs() UpperCamelCase : List[Any] = config_and_inputs UpperCamelCase : Dict = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class UpperCAmelCase_ ( _a, _a, unittest.TestCase): '''simple docstring''' __UpperCamelCase : int = (ViTMAEModel, ViTMAEForPreTraining) if is_torch_available() else () __UpperCamelCase : Optional[int] = {"feature-extraction": ViTMAEModel} if is_torch_available() else {} __UpperCamelCase : Tuple = False __UpperCamelCase : Any = False __UpperCamelCase : Union[str, Any] = False __UpperCamelCase : Tuple = False def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = ViTMAEModelTester(self ) UpperCamelCase : str = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , has_text_modality=__SCREAMING_SNAKE_CASE , hidden_size=37 ) def _lowercase ( self ): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='''ViTMAE does not use inputs_embeds''' ) def _lowercase ( self ): """simple docstring""" pass def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase : Optional[Any] = model_class(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) UpperCamelCase : Union[str, Any] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(__SCREAMING_SNAKE_CASE , nn.Linear ) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase : Tuple = model_class(__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCamelCase : Tuple = [*signature.parameters.keys()] UpperCamelCase : Dict = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" np.random.seed(2 ) UpperCamelCase : Optional[Any] = int((pt_model.config.image_size // pt_model.config.patch_size) ** 2 ) UpperCamelCase : Optional[Any] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) UpperCamelCase : Any = torch.from_numpy(__SCREAMING_SNAKE_CASE ) # Add `noise` argument. # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument UpperCamelCase : Tuple = pt_noise super().check_pt_tf_models(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase : Union[str, Any] = model_class(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.eval() # make random mask reproducible torch.manual_seed(2 ) with torch.no_grad(): UpperCamelCase : int = model(**self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) UpperCamelCase : Dict = outputs[0].cpu().numpy() UpperCamelCase : Optional[Any] = 0 with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = model_class.from_pretrained(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) # make random mask reproducible torch.manual_seed(2 ) with torch.no_grad(): UpperCamelCase : Optional[Any] = model(**self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) # Make sure we don't have nans UpperCamelCase : str = after_outputs[0].cpu().numpy() UpperCamelCase : List[str] = 0 UpperCamelCase : Dict = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(__SCREAMING_SNAKE_CASE , 1e-5 ) @unittest.skip( reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.''' ) def _lowercase ( self ): """simple docstring""" pass @unittest.skip( reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.''' ) def _lowercase ( self ): """simple docstring""" pass @unittest.skip( reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.''' ) def _lowercase ( self ): """simple docstring""" pass @unittest.skip(reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load''' ) def _lowercase ( self ): """simple docstring""" pass @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def _lowercase ( self ): """simple docstring""" pass @slow def _lowercase ( self ): """simple docstring""" for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase : Any = ViTMAEModel.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) def a ( ): """simple docstring""" UpperCamelCase : int = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' @cached_property def _lowercase ( self ): """simple docstring""" return ViTImageProcessor.from_pretrained('''facebook/vit-mae-base''' ) if is_vision_available() else None @slow def _lowercase ( self ): """simple docstring""" np.random.seed(2 ) UpperCamelCase : int = ViTMAEForPreTraining.from_pretrained('''facebook/vit-mae-base''' ).to(__SCREAMING_SNAKE_CASE ) UpperCamelCase : str = self.default_image_processor UpperCamelCase : str = prepare_img() UpperCamelCase : Optional[int] = image_processor(images=__SCREAMING_SNAKE_CASE , return_tensors='''pt''' ).to(__SCREAMING_SNAKE_CASE ) # prepare a noise vector that will be also used for testing the TF model # (this way we can ensure that the PT and TF models operate on the same inputs) UpperCamelCase : int = ViTMAEConfig() UpperCamelCase : Any = int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2 ) UpperCamelCase : Union[str, Any] = np.random.uniform(size=(1, num_patches) ) # forward pass with torch.no_grad(): UpperCamelCase : Optional[Any] = model(**__SCREAMING_SNAKE_CASE , noise=torch.from_numpy(__SCREAMING_SNAKE_CASE ).to(device=__SCREAMING_SNAKE_CASE ) ) # verify the logits UpperCamelCase : Dict = torch.Size((1, 196, 768) ) self.assertEqual(outputs.logits.shape , __SCREAMING_SNAKE_CASE ) UpperCamelCase : int = torch.tensor( [[-0.0_548, -1.7_023, -0.9_325], [0.3_721, -0.5_670, -0.2_233], [0.8_235, -1.3_878, -0.3_524]] ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , expected_slice.to(__SCREAMING_SNAKE_CASE ) , atol=1e-4 ) )
364
def a ( SCREAMING_SNAKE_CASE_ : str = "The quick brown fox jumps over the lazy dog" , ): """simple docstring""" UpperCamelCase : Any = set() # Replace all the whitespace in our sentence UpperCamelCase : Union[str, Any] = input_str.replace(''' ''' , '''''' ) for alpha in input_str: if "a" <= alpha.lower() <= "z": frequency.add(alpha.lower() ) return len(SCREAMING_SNAKE_CASE_ ) == 2_6 def a ( SCREAMING_SNAKE_CASE_ : str = "The quick brown fox jumps over the lazy dog" , ): """simple docstring""" UpperCamelCase : str = [False] * 2_6 for char in input_str: if char.islower(): UpperCamelCase : List[Any] = True elif char.isupper(): UpperCamelCase : List[Any] = True return all(SCREAMING_SNAKE_CASE_ ) def a ( SCREAMING_SNAKE_CASE_ : str = "The quick brown fox jumps over the lazy dog" , ): """simple docstring""" return len({char for char in input_str.lower() if char.isalpha()} ) == 2_6 def a ( ): """simple docstring""" from timeit import timeit UpperCamelCase : int = '''from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest''' print(timeit('''is_pangram()''' , setup=SCREAMING_SNAKE_CASE_ ) ) print(timeit('''is_pangram_faster()''' , setup=SCREAMING_SNAKE_CASE_ ) ) print(timeit('''is_pangram_fastest()''' , setup=SCREAMING_SNAKE_CASE_ ) ) # 5.348480500048026, 2.6477354579837993, 1.8470395830227062 # 5.036091582966037, 2.644472333951853, 1.8869528750656173 if __name__ == "__main__": import doctest doctest.testmod() benchmark()
315
0
from typing import TYPE_CHECKING from ..utils import _LazyModule __UpperCAmelCase : Any = { "config": [ "EXTERNAL_DATA_FORMAT_SIZE_LIMIT", "OnnxConfig", "OnnxConfigWithPast", "OnnxSeq2SeqConfigWithPast", "PatchingSpec", ], "convert": ["export", "validate_model_outputs"], "features": ["FeaturesManager"], "utils": ["ParameterFormat", "compute_serialized_parameters_size"], } if TYPE_CHECKING: from .config import ( EXTERNAL_DATA_FORMAT_SIZE_LIMIT, OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast, PatchingSpec, ) from .convert import export, validate_model_outputs from .features import FeaturesManager from .utils import ParameterFormat, compute_serialized_parameters_size else: import sys __UpperCAmelCase : Optional[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
365
import argparse import logging import sys from unittest.mock import patch import run_glue_deebert from transformers.testing_utils import TestCasePlus, get_gpu_count, require_torch_non_multi_gpu, slow logging.basicConfig(level=logging.DEBUG) __UpperCAmelCase : Union[str, Any] = logging.getLogger() def a ( ): """simple docstring""" UpperCamelCase : List[Any] = argparse.ArgumentParser() parser.add_argument('''-f''' ) UpperCamelCase : List[str] = parser.parse_args() return args.f class UpperCAmelCase_ ( _a): '''simple docstring''' def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = logging.StreamHandler(sys.stdout ) logger.addHandler(__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Dict = get_gpu_count() if n_gpu > 1: pass # XXX: doesn't quite work with n_gpu > 1 https://github.com/huggingface/transformers/issues/10560 # script = f"{self.examples_dir_str}/research_projects/deebert/run_glue_deebert.py" # distributed_args = f"-m torch.distributed.launch --nproc_per_node={n_gpu} {script}".split() # cmd = [sys.executable] + distributed_args + args # execute_subprocess_async(cmd, env=self.get_env()) # XXX: test the results - need to save them first into .json file else: args.insert(0 , '''run_glue_deebert.py''' ) with patch.object(__SCREAMING_SNAKE_CASE , '''argv''' , __SCREAMING_SNAKE_CASE ): UpperCamelCase : int = run_glue_deebert.main() for value in result.values(): self.assertGreaterEqual(__SCREAMING_SNAKE_CASE , 0.666 ) @slow @require_torch_non_multi_gpu def _lowercase ( self ): """simple docstring""" UpperCamelCase : Any = ''' --model_type roberta --model_name_or_path roberta-base --task_name MRPC --do_train --do_eval --do_lower_case --data_dir ./tests/fixtures/tests_samples/MRPC/ --max_seq_length 128 --per_gpu_eval_batch_size=1 --per_gpu_train_batch_size=8 --learning_rate 2e-4 --num_train_epochs 3 --overwrite_output_dir --seed 42 --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --plot_data_dir ./examples/deebert/results/ --save_steps 0 --overwrite_cache --eval_after_first_stage '''.split() self.run_and_check(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = ''' --model_type roberta --model_name_or_path ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --task_name MRPC --do_eval --do_lower_case --data_dir ./tests/fixtures/tests_samples/MRPC/ --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --plot_data_dir ./examples/deebert/results/ --max_seq_length 128 --eval_each_highway --eval_highway --overwrite_cache --per_gpu_eval_batch_size=1 '''.split() self.run_and_check(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = ''' --model_type roberta --model_name_or_path ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --task_name MRPC --do_eval --do_lower_case --data_dir ./tests/fixtures/tests_samples/MRPC/ --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --plot_data_dir ./examples/deebert/results/ --max_seq_length 128 --early_exit_entropy 0.1 --eval_highway --overwrite_cache --per_gpu_eval_batch_size=1 '''.split() self.run_and_check(__SCREAMING_SNAKE_CASE )
315
0
import argparse import requests import torch # pip3 install salesforce-lavis # I'm actually installing a slightly modified version: pip3 install git+https://github.com/nielsrogge/LAVIS.git@fix_lavis_float32 (there's also the fix_lavis branch) # also note: to convert Vicuna checkpoints, we had to include /home/niels/python_projects/checkpoints/FastChat/vicuna-7b in lavis/configs/models/blip2/blip2_instruct_vicuna7b.yaml # same for Vicuna-13b from lavis.models import load_model_and_preprocess from PIL import Image from transformers import ( AutoTokenizer, BlipImageProcessor, InstructBlipConfig, InstructBlipForConditionalGeneration, InstructBlipProcessor, InstructBlipQFormerConfig, InstructBlipVisionConfig, LlamaConfig, LlamaTokenizerFast, TaConfig, TaTokenizerFast, ) from transformers.utils.constants import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD def a ( ): """simple docstring""" UpperCamelCase : str = '''https://raw.githubusercontent.com/salesforce/LAVIS/main/docs/_static/Confusing-Pictures.jpg''' UpperCamelCase : List[Any] = Image.open(requests.get(SCREAMING_SNAKE_CASE_ , stream=SCREAMING_SNAKE_CASE_ ).raw ).convert('''RGB''' ) return image def a ( SCREAMING_SNAKE_CASE_ : Tuple ): """simple docstring""" UpperCamelCase : Dict = [] # fmt: off # vision encoder rename_keys.append(('''visual_encoder.cls_token''', '''vision_model.embeddings.class_embedding''') ) rename_keys.append(('''visual_encoder.pos_embed''', '''vision_model.embeddings.position_embedding''') ) rename_keys.append(('''visual_encoder.patch_embed.proj.weight''', '''vision_model.embeddings.patch_embedding.weight''') ) rename_keys.append(('''visual_encoder.patch_embed.proj.bias''', '''vision_model.embeddings.patch_embedding.bias''') ) rename_keys.append(('''ln_vision.weight''', '''vision_model.post_layernorm.weight''') ) rename_keys.append(('''ln_vision.bias''', '''vision_model.post_layernorm.bias''') ) for i in range(config.vision_config.num_hidden_layers ): rename_keys.append((F"""visual_encoder.blocks.{i}.norm1.weight""", F"""vision_model.encoder.layers.{i}.layer_norm1.weight""") ) rename_keys.append((F"""visual_encoder.blocks.{i}.norm1.bias""", F"""vision_model.encoder.layers.{i}.layer_norm1.bias""") ) rename_keys.append((F"""visual_encoder.blocks.{i}.norm2.weight""", F"""vision_model.encoder.layers.{i}.layer_norm2.weight""") ) rename_keys.append((F"""visual_encoder.blocks.{i}.norm2.bias""", F"""vision_model.encoder.layers.{i}.layer_norm2.bias""") ) rename_keys.append((F"""visual_encoder.blocks.{i}.attn.qkv.weight""", F"""vision_model.encoder.layers.{i}.self_attn.qkv.weight""") ) rename_keys.append((F"""visual_encoder.blocks.{i}.attn.proj.weight""", F"""vision_model.encoder.layers.{i}.self_attn.projection.weight""",) ) rename_keys.append((F"""visual_encoder.blocks.{i}.attn.proj.bias""", F"""vision_model.encoder.layers.{i}.self_attn.projection.bias""") ) rename_keys.append((F"""visual_encoder.blocks.{i}.mlp.fc1.weight""", F"""vision_model.encoder.layers.{i}.mlp.fc1.weight""") ) rename_keys.append((F"""visual_encoder.blocks.{i}.mlp.fc1.bias""", F"""vision_model.encoder.layers.{i}.mlp.fc1.bias""") ) rename_keys.append((F"""visual_encoder.blocks.{i}.mlp.fc2.weight""", F"""vision_model.encoder.layers.{i}.mlp.fc2.weight""") ) rename_keys.append((F"""visual_encoder.blocks.{i}.mlp.fc2.bias""", F"""vision_model.encoder.layers.{i}.mlp.fc2.bias""") ) # QFormer rename_keys.append(('''Qformer.bert.embeddings.LayerNorm.weight''', '''qformer.embeddings.layernorm.weight''') ) rename_keys.append(('''Qformer.bert.embeddings.LayerNorm.bias''', '''qformer.embeddings.layernorm.bias''') ) # fmt: on return rename_keys def a ( SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Optional[int] ): """simple docstring""" UpperCamelCase : Dict = dct.pop(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Optional[int] = val def a ( SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases UpperCamelCase : Dict = state_dict.pop(F"""visual_encoder.blocks.{i}.attn.q_bias""" ) UpperCamelCase : Dict = state_dict.pop(F"""visual_encoder.blocks.{i}.attn.v_bias""" ) # next, set bias in the state dict UpperCamelCase : int = torch.cat((q_bias, torch.zeros_like(SCREAMING_SNAKE_CASE_ , requires_grad=SCREAMING_SNAKE_CASE_ ), v_bias) ) UpperCamelCase : Any = qkv_bias def a ( SCREAMING_SNAKE_CASE_ : Optional[Any] ): """simple docstring""" UpperCamelCase : Optional[Any] = 3_6_4 if '''coco''' in model_name else 2_2_4 UpperCamelCase : Dict = InstructBlipVisionConfig(image_size=SCREAMING_SNAKE_CASE_ ).to_dict() # make sure the models have proper bos_token_id and eos_token_id set (important for generation) # seems like flan-T5 models don't have bos_token_id properly set? if "t5-xl" in model_name: UpperCamelCase : List[Any] = TaConfig.from_pretrained('''google/flan-t5-xl''' , dense_act_fn='''gelu''' , bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: UpperCamelCase : Union[str, Any] = TaConfig.from_pretrained('''google/flan-t5-xxl''' , dense_act_fn='''gelu''' , bos_token_id=1 ).to_dict() elif "vicuna-7b" in model_name: UpperCamelCase : List[Any] = LlamaConfig.from_pretrained('''decapoda-research/llama-7b-hf''' , vocab_size=3_2_0_0_1 ).to_dict() elif "vicuna-13b" in model_name: UpperCamelCase : Tuple = LlamaConfig.from_pretrained('''decapoda-research/llama-13b-hf''' , vocab_size=3_2_0_0_1 ).to_dict() else: raise ValueError('''Model name not supported''' ) # the authors add one special "[DEC]" token to the vocab of Q-Former, hence vocab size = 30522 + 1 UpperCamelCase : Dict = InstructBlipQFormerConfig(vocab_size=3_0_5_2_3 ).to_dict() UpperCamelCase : Tuple = InstructBlipConfig(vision_config=SCREAMING_SNAKE_CASE_ , text_config=SCREAMING_SNAKE_CASE_ , qformer_config=SCREAMING_SNAKE_CASE_ ) return config, image_size @torch.no_grad() def a ( SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : int=None , SCREAMING_SNAKE_CASE_ : Tuple=False ): """simple docstring""" UpperCamelCase : List[str] = AutoTokenizer.from_pretrained('''bert-base-uncased''' , truncation_side='''left''' ) qformer_tokenizer.add_special_tokens({'''bos_token''': '''[DEC]'''} ) if "t5" in model_name: UpperCamelCase : Optional[Any] = TaTokenizerFast.from_pretrained('''google/flan-t5-xl''' , truncation_side='''left''' ) elif "vicuna" in model_name: # the following was used in the original implementation: # tokenizer = LlamaTokenizer.from_pretrained("huggyllama/llama-7b", use_fast=False, truncation_side="left") # tokenizer.add_special_tokens({"pad_token": "[PAD]"}) # tokenizer.add_special_tokens({"bos_token": "</s>"}) # tokenizer.add_special_tokens({"eos_token": "</s>"}) # tokenizer.add_special_tokens({"unk_token": "</s>"}) UpperCamelCase : Optional[Any] = LlamaTokenizerFast.from_pretrained( '''huggyllama/llama-7b''' , truncation_side='''left''' , bos_token='''</s>''' , unk_token='''</s>''' ) tokenizer.add_special_tokens({'''pad_token''': '''[PAD]'''} ) UpperCamelCase : Dict = get_blipa_config(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Tuple = InstructBlipForConditionalGeneration(SCREAMING_SNAKE_CASE_ ).eval() UpperCamelCase : Optional[Any] = { '''instructblip-vicuna-7b''': ('''blip2_vicuna_instruct''', '''vicuna7b'''), '''instructblip-vicuna-13b''': ('''blip2_vicuna_instruct''', '''vicuna13b'''), '''instructblip-flan-t5-xl''': ('''blip2_t5_instruct''', '''flant5xl'''), '''instructblip-flan-t5-xxl''': ('''blip2_t5_instruct''', '''flant5xxl'''), } UpperCamelCase : Union[str, Any] = model_name_to_original[model_name] # load original model print('''Loading original model...''' ) UpperCamelCase : List[str] = '''cuda:1''' if torch.cuda.is_available() else '''cpu''' UpperCamelCase : List[Any] = '''cuda:2''' if torch.cuda.is_available() else '''cpu''' UpperCamelCase : Optional[int] = load_model_and_preprocess( name=SCREAMING_SNAKE_CASE_ , model_type=SCREAMING_SNAKE_CASE_ , is_eval=SCREAMING_SNAKE_CASE_ , device=SCREAMING_SNAKE_CASE_ ) original_model.eval() print('''Done!''' ) # update state dict keys UpperCamelCase : List[Any] = original_model.state_dict() UpperCamelCase : Optional[Any] = create_rename_keys(SCREAMING_SNAKE_CASE_ ) for src, dest in rename_keys: rename_key(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): UpperCamelCase : Dict = state_dict.pop(SCREAMING_SNAKE_CASE_ ) if key.startswith('''Qformer.bert''' ): UpperCamelCase : Dict = key.replace('''Qformer.bert''' , '''qformer''' ) if "attention.self" in key: UpperCamelCase : Dict = key.replace('''self''' , '''attention''' ) if "llm_proj" in key: UpperCamelCase : Optional[int] = key.replace('''llm_proj''' , '''language_projection''' ) if "t5_proj" in key: UpperCamelCase : Optional[int] = key.replace('''t5_proj''' , '''language_projection''' ) if key.startswith('''llm_model''' ): UpperCamelCase : Optional[Any] = key.replace('''llm_model''' , '''language_model''' ) if key.startswith('''t5''' ): UpperCamelCase : str = key.replace('''t5''' , '''language''' ) UpperCamelCase : str = val # read in qv biases read_in_q_v_bias(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # note: weights get loaded in torch.float32 by default hf_model.load_state_dict(SCREAMING_SNAKE_CASE_ , strict=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : str = load_demo_image() UpperCamelCase : str = '''What is unusual about this image?''' # create processor UpperCamelCase : Optional[Any] = BlipImageProcessor( size={'''height''': image_size, '''width''': image_size} , image_mean=SCREAMING_SNAKE_CASE_ , image_std=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Any = InstructBlipProcessor( image_processor=SCREAMING_SNAKE_CASE_ , tokenizer=SCREAMING_SNAKE_CASE_ , qformer_tokenizer=SCREAMING_SNAKE_CASE_ , ) UpperCamelCase : Union[str, Any] = processor(images=SCREAMING_SNAKE_CASE_ , text=SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ).to(SCREAMING_SNAKE_CASE_ ) # make sure processor creates exact same pixel values UpperCamelCase : Optional[int] = vis_processors['''eval'''](SCREAMING_SNAKE_CASE_ ).unsqueeze(0 ).to(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Optional[Any] = inputs.pixel_values assert torch.allclose(original_pixel_values.to(pixel_values.device ) , SCREAMING_SNAKE_CASE_ ) original_model.to(SCREAMING_SNAKE_CASE_ ) hf_model.to(SCREAMING_SNAKE_CASE_ ) with torch.no_grad(): if "vicuna" in model_name: UpperCamelCase : List[str] = original_model({'''image''': original_pixel_values, '''text_input''': [prompt]} ).logits UpperCamelCase : Dict = hf_model(**SCREAMING_SNAKE_CASE_ ).logits else: UpperCamelCase : Optional[Any] = original_model( {'''image''': original_pixel_values, '''text_input''': [prompt], '''text_output''': ['''\n''']} ).logits UpperCamelCase : List[str] = tokenizer('''\n''' , return_tensors='''pt''' ).input_ids.to(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[Any] = label_input_ids.masked_fill(label_input_ids == tokenizer.pad_token_id , -1_0_0 ) UpperCamelCase : Tuple = hf_model(**SCREAMING_SNAKE_CASE_ , labels=SCREAMING_SNAKE_CASE_ ).logits print('''First values of original logits:''' , original_logits[0, :3, :3] ) print('''First values of HF logits:''' , logits[0, :3, :3] ) # assert values assert original_logits.shape == logits.shape UpperCamelCase : Optional[int] = 1E-4 if '''vicuna''' in model_name else 1E-5 assert torch.allclose(original_logits.to(logits.device ) , SCREAMING_SNAKE_CASE_ , atol=SCREAMING_SNAKE_CASE_ ) print('''Looks ok!''' ) print('''Generating with original model...''' ) UpperCamelCase : Tuple = original_model.generate({'''image''': original_pixel_values, '''prompt''': prompt} , num_beams=5 ) # important: we need to cast the weights of the HF model to the appropriate type print('''Generating with HF model...''' ) UpperCamelCase : Optional[Any] = hf_model.generate( **SCREAMING_SNAKE_CASE_ , do_sample=SCREAMING_SNAKE_CASE_ , num_beams=5 , max_length=2_5_6 , min_length=1 , top_p=0.9 , repetition_penalty=1.5 , length_penalty=1.0 , temperature=1 , ) if "vicuna" in model_name: # convert output id 0 to 2 (eos_token_id) # TODO add this in the generate method? UpperCamelCase : Union[str, Any] = 2 print('''Original generation:''' , SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[str] = processor.batch_decode(SCREAMING_SNAKE_CASE_ , skip_special_tokens=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Any = [text.strip() for text in output_text] print('''HF generation:''' , SCREAMING_SNAKE_CASE_ ) if pytorch_dump_folder_path is not None: processor.save_pretrained(SCREAMING_SNAKE_CASE_ ) hf_model.save_pretrained(SCREAMING_SNAKE_CASE_ ) if push_to_hub: processor.push_to_hub(F"""Salesforce/{model_name}""" ) hf_model.push_to_hub(F"""Salesforce/{model_name}""" ) if __name__ == "__main__": __UpperCAmelCase : List[str] = argparse.ArgumentParser() __UpperCAmelCase : Tuple = [ "instructblip-vicuna-7b", "instructblip-vicuna-13b", "instructblip-flan-t5-xl", "instructblip-flan-t5-xxl", ] parser.add_argument( "--model_name", default="instructblip-flan-t5-xl", choices=choices, type=str, help="Path to hf config.json of model to convert", ) parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument( "--push_to_hub", action="store_true", help="Whether to push the model and processor to the hub after converting", ) __UpperCAmelCase : str = parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
366
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __UpperCAmelCase : Tuple = logging.get_logger(__name__) __UpperCAmelCase : Union[str, Any] = { "kssteven/ibert-roberta-base": "https://huggingface.co/kssteven/ibert-roberta-base/resolve/main/config.json", "kssteven/ibert-roberta-large": "https://huggingface.co/kssteven/ibert-roberta-large/resolve/main/config.json", "kssteven/ibert-roberta-large-mnli": ( "https://huggingface.co/kssteven/ibert-roberta-large-mnli/resolve/main/config.json" ), } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : List[Any] = "ibert" def __init__( self , __SCREAMING_SNAKE_CASE=30_522 , __SCREAMING_SNAKE_CASE=768 , __SCREAMING_SNAKE_CASE=12 , __SCREAMING_SNAKE_CASE=12 , __SCREAMING_SNAKE_CASE=3_072 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=512 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=1e-12 , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=0 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE="absolute" , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE="none" , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" super().__init__(pad_token_id=__SCREAMING_SNAKE_CASE , bos_token_id=__SCREAMING_SNAKE_CASE , eos_token_id=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = vocab_size UpperCamelCase : Optional[int] = hidden_size UpperCamelCase : Tuple = num_hidden_layers UpperCamelCase : Optional[Any] = num_attention_heads UpperCamelCase : Dict = hidden_act UpperCamelCase : Union[str, Any] = intermediate_size UpperCamelCase : str = hidden_dropout_prob UpperCamelCase : Any = attention_probs_dropout_prob UpperCamelCase : Dict = max_position_embeddings UpperCamelCase : Union[str, Any] = type_vocab_size UpperCamelCase : Optional[Any] = initializer_range UpperCamelCase : Union[str, Any] = layer_norm_eps UpperCamelCase : Dict = position_embedding_type UpperCamelCase : int = quant_mode UpperCamelCase : Any = force_dequant class UpperCAmelCase_ ( _a): '''simple docstring''' @property def _lowercase ( self ): """simple docstring""" if self.task == "multiple-choice": UpperCamelCase : int = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: UpperCamelCase : Optional[int] = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ] )
315
0
__UpperCAmelCase : int = [ "Audio", "Array2D", "Array3D", "Array4D", "Array5D", "ClassLabel", "Features", "Sequence", "Value", "Image", "Translation", "TranslationVariableLanguages", ] from .audio import Audio from .features import ArrayaD, ArrayaD, ArrayaD, ArrayaD, ClassLabel, Features, Sequence, Value from .image import Image from .translation import Translation, TranslationVariableLanguages
367
import html from ...feature_extraction_utils import BatchFeature, FeatureExtractionMixin from ...utils import is_bsa_available, logging, requires_backends if is_bsa_available(): import bsa from bsa import BeautifulSoup __UpperCAmelCase : int = logging.get_logger(__name__) class UpperCAmelCase_ ( _a): '''simple docstring''' def __init__( self , **__SCREAMING_SNAKE_CASE ): """simple docstring""" requires_backends(self , ['''bs4'''] ) super().__init__(**__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[Any] = [] UpperCamelCase : int = [] UpperCamelCase : List[Any] = element if element.name else element.parent for parent in child.parents: # type: bs4.element.Tag UpperCamelCase : Tuple = parent.find_all(child.name , recursive=__SCREAMING_SNAKE_CASE ) xpath_tags.append(child.name ) xpath_subscripts.append( 0 if 1 == len(__SCREAMING_SNAKE_CASE ) else next(i for i, s in enumerate(__SCREAMING_SNAKE_CASE , 1 ) if s is child ) ) UpperCamelCase : Optional[Any] = parent xpath_tags.reverse() xpath_subscripts.reverse() return xpath_tags, xpath_subscripts def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[Any] = BeautifulSoup(__SCREAMING_SNAKE_CASE , '''html.parser''' ) UpperCamelCase : Union[str, Any] = [] UpperCamelCase : List[str] = [] UpperCamelCase : str = [] for element in html_code.descendants: if type(__SCREAMING_SNAKE_CASE ) == bsa.element.NavigableString: if type(element.parent ) != bsa.element.Tag: continue UpperCamelCase : Any = html.unescape(__SCREAMING_SNAKE_CASE ).strip() if not text_in_this_tag: continue all_doc_strings.append(__SCREAMING_SNAKE_CASE ) UpperCamelCase , UpperCamelCase : int = self.xpath_soup(__SCREAMING_SNAKE_CASE ) stringaxtag_seq.append(__SCREAMING_SNAKE_CASE ) stringaxsubs_seq.append(__SCREAMING_SNAKE_CASE ) if len(__SCREAMING_SNAKE_CASE ) != len(__SCREAMING_SNAKE_CASE ): raise ValueError('''Number of doc strings and xtags does not correspond''' ) if len(__SCREAMING_SNAKE_CASE ) != len(__SCREAMING_SNAKE_CASE ): raise ValueError('''Number of doc strings and xsubs does not correspond''' ) return all_doc_strings, stringaxtag_seq, stringaxsubs_seq def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[Any] = '''''' for tagname, subs in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): xpath += f"""/{tagname}""" if subs != 0: xpath += f"""[{subs}]""" return xpath def __call__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = False # Check that strings has a valid type if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): UpperCamelCase : Optional[Any] = True elif isinstance(__SCREAMING_SNAKE_CASE , (list, tuple) ): if len(__SCREAMING_SNAKE_CASE ) == 0 or isinstance(html_strings[0] , __SCREAMING_SNAKE_CASE ): UpperCamelCase : List[str] = True if not valid_strings: raise ValueError( '''HTML strings must of type `str`, `List[str]` (batch of examples), ''' f"""but is of type {type(__SCREAMING_SNAKE_CASE )}.""" ) UpperCamelCase : int = bool(isinstance(__SCREAMING_SNAKE_CASE , (list, tuple) ) and (isinstance(html_strings[0] , __SCREAMING_SNAKE_CASE )) ) if not is_batched: UpperCamelCase : Union[str, Any] = [html_strings] # Get nodes + xpaths UpperCamelCase : str = [] UpperCamelCase : int = [] for html_string in html_strings: UpperCamelCase , UpperCamelCase , UpperCamelCase : Dict = self.get_three_from_single(__SCREAMING_SNAKE_CASE ) nodes.append(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = [] for node, tag_list, sub_list in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): UpperCamelCase : str = self.construct_xpath(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) xpath_strings.append(__SCREAMING_SNAKE_CASE ) xpaths.append(__SCREAMING_SNAKE_CASE ) # return as Dict UpperCamelCase : List[str] = {'''nodes''': nodes, '''xpaths''': xpaths} UpperCamelCase : List[Any] = BatchFeature(data=__SCREAMING_SNAKE_CASE , tensor_type=__SCREAMING_SNAKE_CASE ) return encoded_inputs
315
0
import math class UpperCAmelCase_ : '''simple docstring''' def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Tuple = 0.0 UpperCamelCase : Any = 0.0 for i in range(len(__SCREAMING_SNAKE_CASE ) ): da += math.pow((sample[i] - weights[0][i]) , 2 ) da += math.pow((sample[i] - weights[1][i]) , 2 ) return 0 if da > da else 1 return 0 def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" for i in range(len(__SCREAMING_SNAKE_CASE ) ): weights[j][i] += alpha * (sample[i] - weights[j][i]) return weights def a ( ): """simple docstring""" UpperCamelCase : Tuple = [[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]] # weight initialization ( n, C ) UpperCamelCase : List[str] = [[0.2, 0.6, 0.5, 0.9], [0.8, 0.4, 0.7, 0.3]] # training UpperCamelCase : int = SelfOrganizingMap() UpperCamelCase : List[Any] = 3 UpperCamelCase : str = 0.5 for _ in range(SCREAMING_SNAKE_CASE_ ): for j in range(len(SCREAMING_SNAKE_CASE_ ) ): # training sample UpperCamelCase : Optional[int] = training_samples[j] # Compute the winning vector UpperCamelCase : int = self_organizing_map.get_winner(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # Update the winning vector UpperCamelCase : Union[str, Any] = self_organizing_map.update(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # classify test sample UpperCamelCase : Union[str, Any] = [0, 0, 0, 1] UpperCamelCase : Optional[Any] = self_organizing_map.get_winner(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # results print(F"""Clusters that the test sample belongs to : {winner}""" ) print(F"""Weights that have been trained : {weights}""" ) # running the main() function if __name__ == "__main__": main()
368
import argparse import datetime import json import time import warnings from logging import getLogger from pathlib import Path from typing import Dict, List import torch from tqdm import tqdm from transformers import AutoModelForSeqaSeqLM, AutoTokenizer from utils import calculate_bleu, calculate_rouge, chunks, parse_numeric_n_bool_cl_kwargs, use_task_specific_params __UpperCAmelCase : List[str] = getLogger(__name__) __UpperCAmelCase : Optional[Any] = "cuda" if torch.cuda.is_available() else "cpu" def a ( SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : int = 8 , SCREAMING_SNAKE_CASE_ : str = DEFAULT_DEVICE , SCREAMING_SNAKE_CASE_ : List[Any]=False , SCREAMING_SNAKE_CASE_ : int="summarization" , SCREAMING_SNAKE_CASE_ : int=None , **SCREAMING_SNAKE_CASE_ : Any , ): """simple docstring""" UpperCamelCase : Dict = Path(SCREAMING_SNAKE_CASE_ ).open('''w''' , encoding='''utf-8''' ) UpperCamelCase : List[Any] = str(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[Any] = AutoModelForSeqaSeqLM.from_pretrained(SCREAMING_SNAKE_CASE_ ).to(SCREAMING_SNAKE_CASE_ ) if fpaa: UpperCamelCase : List[Any] = model.half() UpperCamelCase : str = AutoTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_ ) logger.info(F"""Inferred tokenizer type: {tokenizer.__class__}""" ) # if this is wrong, check config.model_type. UpperCamelCase : int = time.time() # update config with task specific params use_task_specific_params(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if prefix is None: UpperCamelCase : Union[str, Any] = prefix or getattr(model.config , '''prefix''' , '''''' ) or '''''' for examples_chunk in tqdm(list(chunks(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) ): UpperCamelCase : Optional[int] = [prefix + text for text in examples_chunk] UpperCamelCase : Union[str, Any] = tokenizer(SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' , truncation=SCREAMING_SNAKE_CASE_ , padding='''longest''' ).to(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Any = model.generate( input_ids=batch.input_ids , attention_mask=batch.attention_mask , **SCREAMING_SNAKE_CASE_ , ) UpperCamelCase : List[str] = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ , skip_special_tokens=SCREAMING_SNAKE_CASE_ , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ ) for hypothesis in dec: fout.write(hypothesis + '''\n''' ) fout.flush() fout.close() UpperCamelCase : str = int(time.time() - start_time ) # seconds UpperCamelCase : Any = len(SCREAMING_SNAKE_CASE_ ) return {"n_obs": n_obs, "runtime": runtime, "seconds_per_sample": round(runtime / n_obs , 4 )} def a ( ): """simple docstring""" return datetime.datetime.now().strftime('''%Y-%m-%d %H:%M:%S''' ) def a ( SCREAMING_SNAKE_CASE_ : Union[str, Any]=True ): """simple docstring""" UpperCamelCase : int = argparse.ArgumentParser() parser.add_argument('''model_name''' , type=SCREAMING_SNAKE_CASE_ , help='''like facebook/bart-large-cnn,t5-base, etc.''' ) parser.add_argument('''input_path''' , type=SCREAMING_SNAKE_CASE_ , help='''like cnn_dm/test.source''' ) parser.add_argument('''save_path''' , type=SCREAMING_SNAKE_CASE_ , help='''where to save summaries''' ) parser.add_argument('''--reference_path''' , type=SCREAMING_SNAKE_CASE_ , required=SCREAMING_SNAKE_CASE_ , help='''like cnn_dm/test.target''' ) parser.add_argument('''--score_path''' , type=SCREAMING_SNAKE_CASE_ , required=SCREAMING_SNAKE_CASE_ , default='''metrics.json''' , help='''where to save metrics''' ) parser.add_argument('''--device''' , type=SCREAMING_SNAKE_CASE_ , required=SCREAMING_SNAKE_CASE_ , default=SCREAMING_SNAKE_CASE_ , help='''cuda, cuda:1, cpu etc.''' ) parser.add_argument( '''--prefix''' , type=SCREAMING_SNAKE_CASE_ , required=SCREAMING_SNAKE_CASE_ , default=SCREAMING_SNAKE_CASE_ , help='''will be added to the begininng of src examples''' ) parser.add_argument('''--task''' , type=SCREAMING_SNAKE_CASE_ , default='''summarization''' , help='''used for task_specific_params + metrics''' ) parser.add_argument('''--bs''' , type=SCREAMING_SNAKE_CASE_ , default=8 , required=SCREAMING_SNAKE_CASE_ , help='''batch size''' ) parser.add_argument( '''--n_obs''' , type=SCREAMING_SNAKE_CASE_ , default=-1 , required=SCREAMING_SNAKE_CASE_ , help='''How many observations. Defaults to all.''' ) parser.add_argument('''--fp16''' , action='''store_true''' ) parser.add_argument('''--dump-args''' , action='''store_true''' , help='''print the custom hparams with the results''' ) parser.add_argument( '''--info''' , nargs='''?''' , type=SCREAMING_SNAKE_CASE_ , const=datetime_now() , help=( '''use in conjunction w/ --dump-args to print with the results whatever other info you\'d like, e.g.''' ''' lang=en-ru. If no value is passed, the current datetime string will be used.''' ) , ) # Unspecified args like --num_beams=2 --decoder_start_token_id=4 are passed to model.generate UpperCamelCase , UpperCamelCase : int = parser.parse_known_args() UpperCamelCase : str = parse_numeric_n_bool_cl_kwargs(SCREAMING_SNAKE_CASE_ ) if parsed_args and verbose: print(F"""parsed the following generate kwargs: {parsed_args}""" ) UpperCamelCase : str = [''' ''' + x.rstrip() if '''t5''' in args.model_name else x.rstrip() for x in open(args.input_path ).readlines()] if args.n_obs > 0: UpperCamelCase : Tuple = examples[: args.n_obs] Path(args.save_path ).parent.mkdir(exist_ok=SCREAMING_SNAKE_CASE_ ) if args.reference_path is None and Path(args.score_path ).exists(): warnings.warn(F"""score_path {args.score_path} will be overwritten unless you type ctrl-c.""" ) if args.device == "cpu" and args.fpaa: # this mix leads to RuntimeError: "threshold_cpu" not implemented for 'Half' raise ValueError('''Can\'t mix --fp16 and --device cpu''' ) UpperCamelCase : str = generate_summaries_or_translations( SCREAMING_SNAKE_CASE_ , args.save_path , args.model_name , batch_size=args.bs , device=args.device , fpaa=args.fpaa , task=args.task , prefix=args.prefix , **SCREAMING_SNAKE_CASE_ , ) if args.reference_path is None: return {} # Compute scores UpperCamelCase : Tuple = calculate_bleu if '''translation''' in args.task else calculate_rouge UpperCamelCase : Dict = [x.rstrip() for x in open(args.save_path ).readlines()] UpperCamelCase : Optional[int] = [x.rstrip() for x in open(args.reference_path ).readlines()][: len(SCREAMING_SNAKE_CASE_ )] UpperCamelCase : dict = score_fn(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) scores.update(SCREAMING_SNAKE_CASE_ ) if args.dump_args: scores.update(SCREAMING_SNAKE_CASE_ ) if args.info: UpperCamelCase : Optional[Any] = args.info if verbose: print(SCREAMING_SNAKE_CASE_ ) if args.score_path is not None: json.dump(SCREAMING_SNAKE_CASE_ , open(args.score_path , '''w''' ) ) return scores if __name__ == "__main__": # Usage for MT: # python run_eval.py MODEL_NAME $DATA_DIR/test.source $save_dir/test_translations.txt --reference_path $DATA_DIR/test.target --score_path $save_dir/test_bleu.json --task translation $@ run_generate(verbose=True)
315
0
import itertools import random import unittest import numpy as np from transformers import is_speech_available from transformers.testing_utils import require_torch, require_torchaudio from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_speech_available(): from transformers import SpeechaTextFeatureExtractor __UpperCAmelCase : Optional[int] = random.Random() def a ( SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : List[str]=1.0 , SCREAMING_SNAKE_CASE_ : List[Any]=None , SCREAMING_SNAKE_CASE_ : Tuple=None ): """simple docstring""" if rng is None: UpperCamelCase : Any = global_rng UpperCamelCase : Union[str, Any] = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch @require_torchaudio class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=400 , __SCREAMING_SNAKE_CASE=2_000 , __SCREAMING_SNAKE_CASE=24 , __SCREAMING_SNAKE_CASE=24 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=16_000 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , ): """simple docstring""" UpperCamelCase : Optional[Any] = parent UpperCamelCase : Union[str, Any] = batch_size UpperCamelCase : List[Any] = min_seq_length UpperCamelCase : str = max_seq_length UpperCamelCase : List[str] = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) UpperCamelCase : Optional[Any] = feature_size UpperCamelCase : Dict = num_mel_bins UpperCamelCase : List[str] = padding_value UpperCamelCase : str = sampling_rate UpperCamelCase : List[str] = return_attention_mask UpperCamelCase : Optional[Any] = do_normalize def _lowercase ( self ): """simple docstring""" return { "feature_size": self.feature_size, "num_mel_bins": self.num_mel_bins, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "return_attention_mask": self.return_attention_mask, "do_normalize": self.do_normalize, } def _lowercase ( self , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=False ): """simple docstring""" def _flatten(__SCREAMING_SNAKE_CASE ): return list(itertools.chain(*__SCREAMING_SNAKE_CASE ) ) if equal_length: UpperCamelCase : Optional[int] = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size UpperCamelCase : Dict = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: UpperCamelCase : List[Any] = [np.asarray(__SCREAMING_SNAKE_CASE ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class UpperCAmelCase_ ( _a, unittest.TestCase): '''simple docstring''' __UpperCamelCase : Tuple = SpeechaTextFeatureExtractor if is_speech_available() else None def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = SpeechaTextFeatureExtractionTester(self ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" self.assertTrue(np.all(np.mean(__SCREAMING_SNAKE_CASE , axis=0 ) < 1e-3 ) ) self.assertTrue(np.all(np.abs(np.var(__SCREAMING_SNAKE_CASE , axis=0 ) - 1 ) < 1e-3 ) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Any = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 UpperCamelCase : Union[str, Any] = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] UpperCamelCase : Tuple = [np.asarray(__SCREAMING_SNAKE_CASE ) for speech_input in speech_inputs] # Test feature size UpperCamelCase : Tuple = feature_extractor(__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , return_tensors='''np''' ).input_features self.assertTrue(input_features.ndim == 3 ) self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size ) # Test not batched input UpperCamelCase : Any = feature_extractor(speech_inputs[0] , return_tensors='''np''' ).input_features UpperCamelCase : Union[str, Any] = feature_extractor(np_speech_inputs[0] , return_tensors='''np''' ).input_features self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , atol=1e-3 ) ) # Test batched UpperCamelCase : int = feature_extractor(__SCREAMING_SNAKE_CASE , return_tensors='''np''' ).input_features UpperCamelCase : Optional[int] = feature_extractor(__SCREAMING_SNAKE_CASE , return_tensors='''np''' ).input_features for enc_seq_a, enc_seq_a in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , atol=1e-3 ) ) # Test 2-D numpy arrays are batched. UpperCamelCase : List[Any] = [floats_list((1, x) )[0] for x in (800, 800, 800)] UpperCamelCase : Optional[Any] = np.asarray(__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = feature_extractor(__SCREAMING_SNAKE_CASE , return_tensors='''np''' ).input_features UpperCamelCase : Optional[Any] = feature_extractor(__SCREAMING_SNAKE_CASE , return_tensors='''np''' ).input_features for enc_seq_a, enc_seq_a in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , atol=1e-3 ) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase : Tuple = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] UpperCamelCase : int = ['''longest''', '''max_length''', '''do_not_pad'''] UpperCamelCase : str = [None, 16, None] for max_length, padding in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): UpperCamelCase : Dict = feature_extractor( __SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , max_length=__SCREAMING_SNAKE_CASE , return_attention_mask=__SCREAMING_SNAKE_CASE ) UpperCamelCase : str = inputs.input_features UpperCamelCase : Any = inputs.attention_mask UpperCamelCase : Optional[Any] = [np.sum(__SCREAMING_SNAKE_CASE ) for x in attention_mask] self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]] ) self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]] ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase : List[str] = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] UpperCamelCase : Optional[Any] = ['''longest''', '''max_length''', '''do_not_pad'''] UpperCamelCase : Dict = [None, 16, None] for max_length, padding in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): UpperCamelCase : str = feature_extractor( __SCREAMING_SNAKE_CASE , max_length=__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , return_tensors='''np''' , return_attention_mask=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = inputs.input_features UpperCamelCase : str = inputs.attention_mask UpperCamelCase : Union[str, Any] = [np.sum(__SCREAMING_SNAKE_CASE ) for x in attention_mask] self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]] ) self.assertTrue(input_features[0][fbank_feat_lengths[0] :].sum() < 1e-6 ) self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]] ) self.assertTrue(input_features[0][fbank_feat_lengths[1] :].sum() < 1e-6 ) self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]] ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase : List[Any] = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] UpperCamelCase : List[Any] = feature_extractor( __SCREAMING_SNAKE_CASE , padding='''max_length''' , max_length=4 , truncation=__SCREAMING_SNAKE_CASE , return_tensors='''np''' , return_attention_mask=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : Union[str, Any] = inputs.input_features UpperCamelCase : Dict = inputs.attention_mask UpperCamelCase : List[str] = np.sum(attention_mask == 1 , axis=1 ) self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1] ) self._check_zero_mean_unit_variance(input_features[2] ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase : Union[str, Any] = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] UpperCamelCase : Optional[Any] = feature_extractor( __SCREAMING_SNAKE_CASE , padding='''longest''' , max_length=4 , truncation=__SCREAMING_SNAKE_CASE , return_tensors='''np''' , return_attention_mask=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : Optional[int] = inputs.input_features UpperCamelCase : Any = inputs.attention_mask UpperCamelCase : Optional[int] = np.sum(attention_mask == 1 , axis=1 ) self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]] ) self._check_zero_mean_unit_variance(input_features[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertEqual(input_features.shape , (3, 4, 24) ) UpperCamelCase : List[str] = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] UpperCamelCase : Any = feature_extractor( __SCREAMING_SNAKE_CASE , padding='''longest''' , max_length=16 , truncation=__SCREAMING_SNAKE_CASE , return_tensors='''np''' , return_attention_mask=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : List[str] = inputs.input_features UpperCamelCase : Union[str, Any] = inputs.attention_mask UpperCamelCase : Dict = np.sum(attention_mask == 1 , axis=1 ) self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]] ) self._check_zero_mean_unit_variance(input_features[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertEqual(input_features.shape , (3, 6, 24) ) def _lowercase ( self ): """simple docstring""" import torch UpperCamelCase : Tuple = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase : Any = np.random.rand(100 , 32 ).astype(np.floataa ) UpperCamelCase : Tuple = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: UpperCamelCase : Optional[Any] = feature_extractor.pad([{'''input_features''': inputs}] , return_tensors='''np''' ) self.assertTrue(np_processed.input_features.dtype == np.floataa ) UpperCamelCase : List[Any] = feature_extractor.pad([{'''input_features''': inputs}] , return_tensors='''pt''' ) self.assertTrue(pt_processed.input_features.dtype == torch.floataa ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" from datasets import load_dataset UpperCamelCase : Tuple = load_dataset('''hf-internal-testing/librispeech_asr_dummy''' , '''clean''' , split='''validation''' ) # automatic decoding with librispeech UpperCamelCase : Optional[Any] = ds.sort('''id''' ).select(range(__SCREAMING_SNAKE_CASE ) )[:num_samples]['''audio'''] return [x["array"] for x in speech_samples] def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = np.array([ -1.5_745, -1.7_713, -1.7_020, -1.6_069, -1.2_250, -1.1_105, -0.9_072, -0.8_241, -1.2_310, -0.8_098, -0.3_320, -0.4_101, -0.7_985, -0.4_996, -0.8_213, -0.9_128, -1.0_420, -1.1_286, -1.0_440, -0.7_999, -0.8_405, -1.2_275, -1.5_443, -1.4_625, ] ) # fmt: on UpperCamelCase : Dict = self._load_datasamples(1 ) UpperCamelCase : List[str] = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase : List[str] = feature_extractor(__SCREAMING_SNAKE_CASE , return_tensors='''pt''' ).input_features self.assertEquals(input_features.shape , (1, 584, 24) ) self.assertTrue(np.allclose(input_features[0, 0, :30] , __SCREAMING_SNAKE_CASE , atol=1e-4 ) )
369
import re import warnings from contextlib import contextmanager from ...processing_utils import ProcessorMixin class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : int = ["image_processor", "tokenizer"] __UpperCamelCase : List[str] = "AutoImageProcessor" __UpperCamelCase : Optional[Any] = "AutoTokenizer" def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[int] = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , __SCREAMING_SNAKE_CASE , ) UpperCamelCase : Any = kwargs.pop('''feature_extractor''' ) UpperCamelCase : 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__(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = self.image_processor UpperCamelCase : int = False def __call__( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): """simple docstring""" if self._in_target_context_manager: return self.current_processor(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = kwargs.pop('''images''' , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = kwargs.pop('''text''' , __SCREAMING_SNAKE_CASE ) if len(__SCREAMING_SNAKE_CASE ) > 0: UpperCamelCase : Union[str, Any] = args[0] UpperCamelCase : str = args[1:] if images is None and text is None: raise ValueError('''You need to specify either an `images` or `text` input to process.''' ) if images is not None: UpperCamelCase : List[str] = self.image_processor(__SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if text is not None: UpperCamelCase : Optional[Any] = self.tokenizer(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if text is None: return inputs elif images is None: return encodings else: UpperCamelCase : List[str] = encodings['''input_ids'''] return inputs def _lowercase ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): """simple docstring""" return self.tokenizer.batch_decode(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def _lowercase ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): """simple docstring""" return self.tokenizer.decode(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) @contextmanager def _lowercase ( self ): """simple docstring""" warnings.warn( '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your ''' '''labels by using the argument `text` of the regular `__call__` method (either in the same call as ''' '''your images inputs, or in a separate call.''' ) UpperCamelCase : Any = True UpperCamelCase : int = self.tokenizer yield UpperCamelCase : List[Any] = self.image_processor UpperCamelCase : Tuple = False def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" if added_vocab is None: UpperCamelCase : str = self.tokenizer.get_added_vocab() UpperCamelCase : int = {} while tokens: UpperCamelCase : Dict = re.search(R'''<s_(.*?)>''' , __SCREAMING_SNAKE_CASE , re.IGNORECASE ) if start_token is None: break UpperCamelCase : List[str] = start_token.group(1 ) UpperCamelCase : Dict = re.search(Rf"""</s_{key}>""" , __SCREAMING_SNAKE_CASE , re.IGNORECASE ) UpperCamelCase : Any = start_token.group() if end_token is None: UpperCamelCase : Optional[int] = tokens.replace(__SCREAMING_SNAKE_CASE , '''''' ) else: UpperCamelCase : Dict = end_token.group() UpperCamelCase : int = re.escape(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = re.escape(__SCREAMING_SNAKE_CASE ) UpperCamelCase : str = re.search(f"""{start_token_escaped}(.*?){end_token_escaped}""" , __SCREAMING_SNAKE_CASE , re.IGNORECASE ) if content is not None: UpperCamelCase : Dict = content.group(1 ).strip() if r"<s_" in content and r"</s_" in content: # non-leaf node UpperCamelCase : Tuple = self.tokenajson(__SCREAMING_SNAKE_CASE , is_inner_value=__SCREAMING_SNAKE_CASE , added_vocab=__SCREAMING_SNAKE_CASE ) if value: if len(__SCREAMING_SNAKE_CASE ) == 1: UpperCamelCase : str = value[0] UpperCamelCase : str = value else: # leaf nodes UpperCamelCase : Optional[int] = [] for leaf in content.split(R'''<sep/>''' ): UpperCamelCase : Optional[int] = leaf.strip() if leaf in added_vocab and leaf[0] == "<" and leaf[-2:] == "/>": UpperCamelCase : int = leaf[1:-2] # for categorical special tokens output[key].append(__SCREAMING_SNAKE_CASE ) if len(output[key] ) == 1: UpperCamelCase : Tuple = output[key][0] UpperCamelCase : List[Any] = tokens[tokens.find(__SCREAMING_SNAKE_CASE ) + len(__SCREAMING_SNAKE_CASE ) :].strip() if tokens[:6] == r"<sep/>": # non-leaf nodes return [output] + self.tokenajson(tokens[6:] , is_inner_value=__SCREAMING_SNAKE_CASE , added_vocab=__SCREAMING_SNAKE_CASE ) if len(__SCREAMING_SNAKE_CASE ): return [output] if is_inner_value else output else: return [] if is_inner_value else {"text_sequence": tokens} @property def _lowercase ( self ): """simple docstring""" warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , __SCREAMING_SNAKE_CASE , ) return self.image_processor_class @property def _lowercase ( self ): """simple docstring""" warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , __SCREAMING_SNAKE_CASE , ) return self.image_processor
315
0
"""simple docstring""" 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 ViTConfig, ViTForImageClassification, ViTImageProcessor, ViTModel from transformers.utils import logging logging.set_verbosity_info() __UpperCAmelCase : Tuple = logging.get_logger(__name__) def a ( SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any]=False ): """simple docstring""" UpperCamelCase : Optional[Any] = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"""blocks.{i}.norm1.weight""", F"""vit.encoder.layer.{i}.layernorm_before.weight""") ) rename_keys.append((F"""blocks.{i}.norm1.bias""", F"""vit.encoder.layer.{i}.layernorm_before.bias""") ) rename_keys.append((F"""blocks.{i}.attn.proj.weight""", F"""vit.encoder.layer.{i}.attention.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.attn.proj.bias""", F"""vit.encoder.layer.{i}.attention.output.dense.bias""") ) rename_keys.append((F"""blocks.{i}.norm2.weight""", F"""vit.encoder.layer.{i}.layernorm_after.weight""") ) rename_keys.append((F"""blocks.{i}.norm2.bias""", F"""vit.encoder.layer.{i}.layernorm_after.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.weight""", F"""vit.encoder.layer.{i}.intermediate.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.bias""", F"""vit.encoder.layer.{i}.intermediate.dense.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.weight""", F"""vit.encoder.layer.{i}.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.bias""", F"""vit.encoder.layer.{i}.output.dense.bias""") ) # projection layer + position embeddings rename_keys.extend( [ ('''cls_token''', '''vit.embeddings.cls_token'''), ('''patch_embed.proj.weight''', '''vit.embeddings.patch_embeddings.projection.weight'''), ('''patch_embed.proj.bias''', '''vit.embeddings.patch_embeddings.projection.bias'''), ('''pos_embed''', '''vit.embeddings.position_embeddings'''), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ('''norm.weight''', '''layernorm.weight'''), ('''norm.bias''', '''layernorm.bias'''), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" UpperCamelCase : Union[str, Any] = [(pair[0], pair[1][4:]) if pair[1].startswith('''vit''' ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ('''norm.weight''', '''vit.layernorm.weight'''), ('''norm.bias''', '''vit.layernorm.bias'''), ('''head.weight''', '''classifier.weight'''), ('''head.bias''', '''classifier.bias'''), ] ) return rename_keys def a ( SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Optional[Any]=False ): """simple docstring""" for i in range(config.num_hidden_layers ): if base_model: UpperCamelCase : Tuple = '''''' else: UpperCamelCase : Any = '''vit.''' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) UpperCamelCase : List[str] = state_dict.pop(F"""blocks.{i}.attn.qkv.weight""" ) UpperCamelCase : Any = state_dict.pop(F"""blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict UpperCamelCase : int = in_proj_weight[ : config.hidden_size, : ] UpperCamelCase : str = in_proj_bias[: config.hidden_size] UpperCamelCase : List[Any] = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] UpperCamelCase : Tuple = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] UpperCamelCase : List[Any] = in_proj_weight[ -config.hidden_size :, : ] UpperCamelCase : List[Any] = in_proj_bias[-config.hidden_size :] def a ( SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" UpperCamelCase : int = ['''head.weight''', '''head.bias'''] for k in ignore_keys: state_dict.pop(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def a ( SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Union[str, Any] ): """simple docstring""" UpperCamelCase : Union[str, Any] = dct.pop(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Optional[Any] = val def a ( ): """simple docstring""" UpperCamelCase : List[str] = '''http://images.cocodataset.org/val2017/000000039769.jpg''' UpperCamelCase : Optional[int] = Image.open(requests.get(SCREAMING_SNAKE_CASE_ , stream=SCREAMING_SNAKE_CASE_ ).raw ) return im @torch.no_grad() def a ( SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Optional[Any]=True ): """simple docstring""" UpperCamelCase : Tuple = ViTConfig() # patch_size if model_name[-1] == "8": UpperCamelCase : Any = 8 # set labels if required if not base_model: UpperCamelCase : List[Any] = 1_0_0_0 UpperCamelCase : str = '''huggingface/label-files''' UpperCamelCase : Any = '''imagenet-1k-id2label.json''' UpperCamelCase : Any = json.load(open(hf_hub_download(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , repo_type='''dataset''' ) , '''r''' ) ) UpperCamelCase : int = {int(SCREAMING_SNAKE_CASE_ ): v for k, v in idalabel.items()} UpperCamelCase : Any = idalabel UpperCamelCase : int = {v: k for k, v in idalabel.items()} # size of the architecture if model_name in ["dino_vits8", "dino_vits16"]: UpperCamelCase : Tuple = 3_8_4 UpperCamelCase : Dict = 1_5_3_6 UpperCamelCase : int = 1_2 UpperCamelCase : Optional[Any] = 6 # load original model from torch hub UpperCamelCase : List[str] = torch.hub.load('''facebookresearch/dino:main''' , SCREAMING_SNAKE_CASE_ ) original_model.eval() # load state_dict of original model, remove and rename some keys UpperCamelCase : Tuple = original_model.state_dict() if base_model: remove_classification_head_(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Any = create_rename_keys(SCREAMING_SNAKE_CASE_ , base_model=SCREAMING_SNAKE_CASE_ ) for src, dest in rename_keys: rename_key(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) read_in_q_k_v(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # load HuggingFace model if base_model: UpperCamelCase : Optional[Any] = ViTModel(SCREAMING_SNAKE_CASE_ , add_pooling_layer=SCREAMING_SNAKE_CASE_ ).eval() else: UpperCamelCase : List[Any] = ViTForImageClassification(SCREAMING_SNAKE_CASE_ ).eval() model.load_state_dict(SCREAMING_SNAKE_CASE_ ) # Check outputs on an image, prepared by ViTImageProcessor UpperCamelCase : str = ViTImageProcessor() UpperCamelCase : Optional[Any] = image_processor(images=prepare_img() , return_tensors='''pt''' ) UpperCamelCase : Union[str, Any] = encoding['''pixel_values'''] UpperCamelCase : Union[str, Any] = model(SCREAMING_SNAKE_CASE_ ) if base_model: UpperCamelCase : Optional[Any] = original_model(SCREAMING_SNAKE_CASE_ ) assert torch.allclose(SCREAMING_SNAKE_CASE_ , outputs.last_hidden_state[:, 0, :] , atol=1E-1 ) else: UpperCamelCase : str = original_model(SCREAMING_SNAKE_CASE_ ) assert logits.shape == outputs.logits.shape assert torch.allclose(SCREAMING_SNAKE_CASE_ , outputs.logits , atol=1E-3 ) 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 image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": __UpperCAmelCase : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default="dino_vitb16", type=str, help="Name of the model trained with DINO 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( "--base_model", action="store_true", help="Whether to only convert the base model (no projection head weights).", ) parser.set_defaults(base_model=True) __UpperCAmelCase : List[str] = parser.parse_args() convert_vit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.base_model)
370
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __UpperCAmelCase : Union[str, Any] = { "configuration_mgp_str": ["MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP", "MgpstrConfig"], "processing_mgp_str": ["MgpstrProcessor"], "tokenization_mgp_str": ["MgpstrTokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : Union[str, Any] = [ "MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST", "MgpstrModel", "MgpstrPreTrainedModel", "MgpstrForSceneTextRecognition", ] if TYPE_CHECKING: from .configuration_mgp_str import MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP, MgpstrConfig from .processing_mgp_str import MgpstrProcessor from .tokenization_mgp_str import MgpstrTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mgp_str import ( MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST, MgpstrForSceneTextRecognition, MgpstrModel, MgpstrPreTrainedModel, ) else: import sys __UpperCAmelCase : Any = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
315
0
# using dfs for finding eulerian path traversal def a ( SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Tuple=None ): """simple docstring""" UpperCamelCase : Union[str, Any] = (path or []) + [u] for v in graph[u]: if visited_edge[u][v] is False: UpperCamelCase : Dict = True, True UpperCamelCase : Union[str, Any] = dfs(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return path def a ( SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Dict ): """simple docstring""" UpperCamelCase : Union[str, Any] = 0 UpperCamelCase : Optional[int] = -1 for i in range(SCREAMING_SNAKE_CASE_ ): if i not in graph.keys(): continue if len(graph[i] ) % 2 == 1: odd_degree_nodes += 1 UpperCamelCase : Any = i if odd_degree_nodes == 0: return 1, odd_node if odd_degree_nodes == 2: return 2, odd_node return 3, odd_node def a ( SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" UpperCamelCase : List[str] = [[False for _ in range(max_node + 1 )] for _ in range(max_node + 1 )] UpperCamelCase : Union[str, Any] = check_circuit_or_path(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if check == 3: print('''graph is not Eulerian''' ) print('''no path''' ) return UpperCamelCase : Optional[int] = 1 if check == 2: UpperCamelCase : Tuple = odd_node print('''graph has a Euler path''' ) if check == 1: print('''graph has a Euler cycle''' ) UpperCamelCase : List[Any] = dfs(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) print(SCREAMING_SNAKE_CASE_ ) def a ( ): """simple docstring""" UpperCamelCase : Optional[Any] = {1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]} UpperCamelCase : Optional[int] = {1: [2, 3, 4, 5], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [1, 4]} UpperCamelCase : Dict = {1: [2, 3, 4], 2: [1, 3, 4], 3: [1, 2], 4: [1, 2, 5], 5: [4]} UpperCamelCase : str = {1: [2, 3], 2: [1, 3], 3: [1, 2]} UpperCamelCase : Any = { 1: [], 2: [] # all degree is zero } UpperCamelCase : Any = 1_0 check_euler(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) check_euler(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) check_euler(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) check_euler(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) check_euler(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": main()
371
def a ( SCREAMING_SNAKE_CASE_ : int = 5_0 ): """simple docstring""" UpperCamelCase : List[str] = [1] * (length + 1) for row_length in range(3 , length + 1 ): for block_length in range(3 , row_length + 1 ): for block_start in range(row_length - block_length ): ways_number[row_length] += ways_number[ row_length - block_start - block_length - 1 ] ways_number[row_length] += 1 return ways_number[length] if __name__ == "__main__": print(f'''{solution() = }''')
315
0
"""simple docstring""" from math import ceil, sqrt def lowerCamelCase_ ( _lowerCamelCase = 100_0000 ): lowerCamelCase__ : Optional[Any] = 0 for outer_width in range(3 , (limit // 4) + 2 ): if outer_width**2 > limit: lowerCamelCase__ : Optional[Any] = max(ceil(sqrt(outer_width**2 - limit ) ) , 1 ) else: lowerCamelCase__ : Optional[int] = 1 if (outer_width - hole_width_lower_bound) % 2: hole_width_lower_bound += 1 answer += (outer_width - hole_width_lower_bound - 2) // 2 + 1 return answer if __name__ == "__main__": print(f"{solution() = }")
316
"""simple docstring""" print((lambda quine: quine % quine)("print((lambda quine: quine %% quine)(%r))"))
316
1
"""simple docstring""" import torch from transformers import AutoModel class a_ ( torch.nn.Module ): '''simple docstring''' def __init__(self, lowerCamelCase_="sayef/fsner-bert-base-uncased" ): '''simple docstring''' super(lowerCamelCase_, self ).__init__() lowerCamelCase__ : List[str] = AutoModel.from_pretrained(lowerCamelCase_, return_dict=lowerCamelCase_ ) lowerCamelCase__ : str = torch.nn.CosineSimilarity(3, 1e-08 ) lowerCamelCase__ : Dict = torch.nn.Softmax(dim=1 ) def a__ (self, **lowerCamelCase_ ): '''simple docstring''' return self.bert(**lowerCamelCase_ ).last_hidden_state def a__ (self, lowerCamelCase_ ): '''simple docstring''' return token_embeddings.sum(2, keepdim=lowerCamelCase_ ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_=1 ): '''simple docstring''' return self.softmax(T * self.cos(lowerCamelCase_, lowerCamelCase_ ) ) def a__ (self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : List[Any] = W_supports['sizes'].tolist() lowerCamelCase__ : Dict = W_supports['start_token_id'].item() lowerCamelCase__ : str = W_supports['end_token_id'].item() del W_supports["sizes"] del W_supports["start_token_id"] del W_supports["end_token_id"] lowerCamelCase__ : List[Any] = self.BERT(**lowerCamelCase_ ) lowerCamelCase__ : Any = self.BERT(**lowerCamelCase_ ) lowerCamelCase__ : Union[str, Any] = None lowerCamelCase__ : int = None lowerCamelCase__ : List[str] = W_supports['input_ids'] == start_token_id lowerCamelCase__ : Any = W_supports['input_ids'] == end_token_id for i, size in enumerate(lowerCamelCase_ ): if i == 0: lowerCamelCase__ : int = 0 else: lowerCamelCase__ : int = support_sizes[i - 1] lowerCamelCase__ : str = S[s : s + size][start_token_masks[s : s + size]] lowerCamelCase__ : List[Any] = S[s : s + size][end_token_masks[s : s + size]] lowerCamelCase__ : Tuple = torch.matmul(q[i], s_start.T ).sum(1 ).softmax(0 ) lowerCamelCase__ : int = torch.matmul(q[i], s_end.T ).sum(1 ).softmax(0 ) if p_starts is not None: lowerCamelCase__ : Union[str, Any] = torch.vstack((p_starts, p_start) ) lowerCamelCase__ : List[Any] = torch.vstack((p_ends, p_end) ) else: lowerCamelCase__ : int = p_start lowerCamelCase__ : Any = p_end return p_starts, p_ends
316
"""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, ) A_ : int = { "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: A_ : Union[str, Any] = ["CLIPTokenizerFast"] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : int = ["CLIPFeatureExtractor"] A_ : Any = ["CLIPImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : List[Any] = [ "CLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "CLIPModel", "CLIPPreTrainedModel", "CLIPTextModel", "CLIPTextModelWithProjection", "CLIPVisionModel", "CLIPVisionModelWithProjection", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Any = [ "TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "TFCLIPModel", "TFCLIPPreTrainedModel", "TFCLIPTextModel", "TFCLIPVisionModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Any = [ "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 A_ : Optional[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
316
1
"""simple docstring""" import functools from typing import Any def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): # Validation if not isinstance(_lowerCamelCase , _lowerCamelCase ) or len(_lowerCamelCase ) == 0: raise ValueError('the string should be not empty string' ) if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not all( isinstance(_lowerCamelCase , _lowerCamelCase ) and len(_lowerCamelCase ) > 0 for item in words ): raise ValueError('the words should be a list of non-empty strings' ) # Build trie lowerCamelCase__ : dict[str, Any] = {} lowerCamelCase__ : str = 'WORD_KEEPER' for word in words: lowerCamelCase__ : Tuple = trie for c in word: if c not in trie_node: lowerCamelCase__ : Optional[int] = {} lowerCamelCase__ : int = trie_node[c] lowerCamelCase__ : Optional[Any] = True lowerCamelCase__ : Any = len(_lowerCamelCase ) # Dynamic programming method @functools.cache def is_breakable(_lowerCamelCase ) -> bool: if index == len_string: return True lowerCamelCase__ : Dict = trie for i in range(_lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : Tuple = trie_node.get(string[i] , _lowerCamelCase ) if trie_node is None: return False if trie_node.get(_lowerCamelCase , _lowerCamelCase ) and is_breakable(i + 1 ): return True return False return is_breakable(0 ) if __name__ == "__main__": import doctest doctest.testmod()
316
"""simple docstring""" import os import posixpath import uuid from dataclasses import dataclass from typing import TYPE_CHECKING, Iterable, List, Optional, Tuple, Union import numpy as np import pyarrow as pa import datasets from datasets.arrow_writer import ArrowWriter, ParquetWriter from datasets.config import MAX_SHARD_SIZE from datasets.filesystems import ( is_remote_filesystem, rename, ) from datasets.iterable_dataset import _BaseExamplesIterable from datasets.utils.py_utils import convert_file_size_to_int A_ : List[Any] = datasets.utils.logging.get_logger(__name__) if TYPE_CHECKING: import pyspark @dataclass class a_ ( datasets.BuilderConfig ): '''simple docstring''' lowerCamelCase__ : Optional[datasets.Features] = None def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , ): import pyspark def generate_fn(): lowerCamelCase__ : Optional[Any] = df.select('*' , pyspark.sql.functions.spark_partition_id().alias('part_id' ) ) for partition_id in partition_order: lowerCamelCase__ : Dict = df_with_partition_id.select('*' ).where(f'''part_id = {partition_id}''' ).drop('part_id' ) lowerCamelCase__ : Dict = partition_df.collect() lowerCamelCase__ : int = 0 for row in rows: yield f'''{partition_id}_{row_id}''', row.asDict() row_id += 1 return generate_fn class a_ ( _BaseExamplesIterable ): '''simple docstring''' def __init__(self, lowerCamelCase_, lowerCamelCase_=None, ): '''simple docstring''' lowerCamelCase__ : Tuple = df lowerCamelCase__ : Any = partition_order or range(self.df.rdd.getNumPartitions() ) lowerCamelCase__ : List[Any] = _generate_iterable_examples(self.df, self.partition_order ) def __iter__(self ): '''simple docstring''' yield from self.generate_examples_fn() def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = list(range(self.df.rdd.getNumPartitions() ) ) generator.shuffle(lowerCamelCase_ ) return SparkExamplesIterable(self.df, partition_order=lowerCamelCase_ ) def a__ (self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = self.split_shard_indices_by_worker(lowerCamelCase_, lowerCamelCase_ ) return SparkExamplesIterable(self.df, partition_order=lowerCamelCase_ ) @property def a__ (self ): '''simple docstring''' return len(self.partition_order ) class a_ ( datasets.DatasetBuilder ): '''simple docstring''' lowerCamelCase__ : Optional[int] = SparkConfig def __init__(self, lowerCamelCase_, lowerCamelCase_ = None, lowerCamelCase_ = None, **lowerCamelCase_, ): '''simple docstring''' import pyspark lowerCamelCase__ : str = pyspark.sql.SparkSession.builder.getOrCreate() lowerCamelCase__ : Optional[Any] = df lowerCamelCase__ : Dict = working_dir super().__init__( cache_dir=lowerCamelCase_, config_name=str(self.df.semanticHash() ), **lowerCamelCase_, ) def a__ (self ): '''simple docstring''' def create_cache_and_write_probe(lowerCamelCase_ ): # makedirs with exist_ok will recursively create the directory. It will not throw an error if directories # already exist. os.makedirs(self._cache_dir, exist_ok=lowerCamelCase_ ) lowerCamelCase__ : str = os.path.join(self._cache_dir, 'fs_test' + uuid.uuida().hex ) # Opening the file in append mode will create a new file unless it already exists, in which case it will not # change the file contents. open(lowerCamelCase_, 'a' ) return [probe_file] if self._spark.conf.get('spark.master', '' ).startswith('local' ): return # If the cluster is multi-node, make sure that the user provided a cache_dir and that it is on an NFS # accessible to the driver. # TODO: Stream batches to the driver using ArrowCollectSerializer instead of throwing an error. if self._cache_dir: lowerCamelCase__ : Tuple = ( self._spark.sparkContext.parallelize(range(1 ), 1 ).mapPartitions(lowerCamelCase_ ).collect() ) if os.path.isfile(probe[0] ): return raise ValueError( 'When using Dataset.from_spark on a multi-node cluster, the driver and all workers should be able to access cache_dir' ) def a__ (self ): '''simple docstring''' return datasets.DatasetInfo(features=self.config.features ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' return [datasets.SplitGenerator(name=datasets.Split.TRAIN )] def a__ (self, lowerCamelCase_ ): '''simple docstring''' import pyspark def get_arrow_batch_size(lowerCamelCase_ ): for batch in it: yield pa.RecordBatch.from_pydict({'batch_bytes': [batch.nbytes]} ) lowerCamelCase__ : List[Any] = self.df.count() lowerCamelCase__ : List[Any] = df_num_rows if df_num_rows <= 1_0_0 else 1_0_0 # Approximate the size of each row (in Arrow format) by averaging over a max-100-row sample. lowerCamelCase__ : List[Any] = ( self.df.limit(lowerCamelCase_ ) .repartition(1 ) .mapInArrow(lowerCamelCase_, 'batch_bytes: long' ) .agg(pyspark.sql.functions.sum('batch_bytes' ).alias('sample_bytes' ) ) .collect()[0] .sample_bytes / sample_num_rows ) lowerCamelCase__ : Dict = approx_bytes_per_row * df_num_rows if approx_total_size > max_shard_size: # Make sure there is at least one row per partition. lowerCamelCase__ : str = min(lowerCamelCase_, int(approx_total_size / max_shard_size ) ) lowerCamelCase__ : List[Any] = self.df.repartition(lowerCamelCase_ ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, ): '''simple docstring''' import pyspark lowerCamelCase__ : List[str] = ParquetWriter if file_format == 'parquet' else ArrowWriter lowerCamelCase__ : List[str] = os.path.join(self._working_dir, os.path.basename(lowerCamelCase_ ) ) if self._working_dir else fpath lowerCamelCase__ : Optional[int] = file_format == 'parquet' # Define these so that we don't reference self in write_arrow, which will result in a pickling error due to # pickling the SparkContext. lowerCamelCase__ : int = self.config.features lowerCamelCase__ : Dict = self._writer_batch_size lowerCamelCase__ : Optional[Any] = self._fs.storage_options def write_arrow(lowerCamelCase_ ): # Within the same SparkContext, no two task attempts will share the same attempt ID. lowerCamelCase__ : Any = pyspark.TaskContext().taskAttemptId() lowerCamelCase__ : str = next(lowerCamelCase_, lowerCamelCase_ ) if first_batch is None: # Some partitions might not receive any data. return pa.RecordBatch.from_arrays( [[task_id], [0], [0]], names=['task_id', 'num_examples', 'num_bytes'], ) lowerCamelCase__ : Tuple = 0 lowerCamelCase__ : Any = writer_class( features=lowerCamelCase_, path=working_fpath.replace('SSSSS', f'''{shard_id:05d}''' ).replace('TTTTT', f'''{task_id:05d}''' ), writer_batch_size=lowerCamelCase_, storage_options=lowerCamelCase_, embed_local_files=lowerCamelCase_, ) lowerCamelCase__ : List[str] = pa.Table.from_batches([first_batch] ) writer.write_table(lowerCamelCase_ ) for batch in it: if max_shard_size is not None and writer._num_bytes >= max_shard_size: lowerCamelCase__ , lowerCamelCase__ : Tuple = writer.finalize() writer.close() yield pa.RecordBatch.from_arrays( [[task_id], [num_examples], [num_bytes]], names=['task_id', 'num_examples', 'num_bytes'], ) shard_id += 1 lowerCamelCase__ : Dict = writer_class( features=writer._features, path=working_fpath.replace('SSSSS', f'''{shard_id:05d}''' ).replace('TTTTT', f'''{task_id:05d}''' ), writer_batch_size=lowerCamelCase_, storage_options=lowerCamelCase_, embed_local_files=lowerCamelCase_, ) lowerCamelCase__ : Tuple = pa.Table.from_batches([batch] ) writer.write_table(lowerCamelCase_ ) if writer._num_bytes > 0: lowerCamelCase__ , lowerCamelCase__ : Tuple = writer.finalize() writer.close() yield pa.RecordBatch.from_arrays( [[task_id], [num_examples], [num_bytes]], names=['task_id', 'num_examples', 'num_bytes'], ) if working_fpath != fpath: for file in os.listdir(os.path.dirname(lowerCamelCase_ ) ): lowerCamelCase__ : Optional[int] = os.path.join(os.path.dirname(lowerCamelCase_ ), os.path.basename(lowerCamelCase_ ) ) shutil.move(lowerCamelCase_, lowerCamelCase_ ) lowerCamelCase__ : List[str] = ( self.df.mapInArrow(lowerCamelCase_, 'task_id: long, num_examples: long, num_bytes: long' ) .groupBy('task_id' ) .agg( pyspark.sql.functions.sum('num_examples' ).alias('total_num_examples' ), pyspark.sql.functions.sum('num_bytes' ).alias('total_num_bytes' ), pyspark.sql.functions.count('num_bytes' ).alias('num_shards' ), pyspark.sql.functions.collect_list('num_examples' ).alias('shard_lengths' ), ) .collect() ) for row in stats: yield row.task_id, (row.total_num_examples, row.total_num_bytes, row.num_shards, row.shard_lengths) def a__ (self, lowerCamelCase_, lowerCamelCase_ = "arrow", lowerCamelCase_ = None, lowerCamelCase_ = None, **lowerCamelCase_, ): '''simple docstring''' self._validate_cache_dir() lowerCamelCase__ : Union[str, Any] = convert_file_size_to_int(max_shard_size or MAX_SHARD_SIZE ) self._repartition_df_if_needed(lowerCamelCase_ ) lowerCamelCase__ : str = not is_remote_filesystem(self._fs ) lowerCamelCase__ : Any = os.path.join if is_local else posixpath.join lowerCamelCase__ : Any = '-TTTTT-SSSSS-of-NNNNN' lowerCamelCase__ : Tuple = f'''{self.name}-{split_generator.name}{SUFFIX}.{file_format}''' lowerCamelCase__ : Union[str, Any] = path_join(self._output_dir, lowerCamelCase_ ) lowerCamelCase__ : List[str] = 0 lowerCamelCase__ : Dict = 0 lowerCamelCase__ : List[Any] = 0 lowerCamelCase__ : Optional[Any] = [] lowerCamelCase__ : List[str] = [] for task_id, content in self._prepare_split_single(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): ( ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ) : int = content if num_bytes > 0: total_num_examples += num_examples total_num_bytes += num_bytes total_shards += num_shards task_id_and_num_shards.append((task_id, num_shards) ) all_shard_lengths.extend(lowerCamelCase_ ) lowerCamelCase__ : str = total_num_examples lowerCamelCase__ : int = total_num_bytes # should rename everything at the end logger.debug(f'''Renaming {total_shards} shards.''' ) if total_shards > 1: lowerCamelCase__ : Union[str, Any] = all_shard_lengths # Define fs outside of _rename_shard so that we don't reference self in the function, which will result in a # pickling error due to pickling the SparkContext. lowerCamelCase__ : Optional[Any] = self._fs # use the -SSSSS-of-NNNNN pattern def _rename_shard( lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, ): rename( lowerCamelCase_, fpath.replace('SSSSS', f'''{shard_id:05d}''' ).replace('TTTTT', f'''{task_id:05d}''' ), fpath.replace('TTTTT-SSSSS', f'''{global_shard_id:05d}''' ).replace('NNNNN', f'''{total_shards:05d}''' ), ) lowerCamelCase__ : List[str] = [] lowerCamelCase__ : List[str] = 0 for i in range(len(lowerCamelCase_ ) ): lowerCamelCase__ , lowerCamelCase__ : Any = task_id_and_num_shards[i] for shard_id in range(lowerCamelCase_ ): args.append([task_id, shard_id, global_shard_id] ) global_shard_id += 1 self._spark.sparkContext.parallelize(lowerCamelCase_, len(lowerCamelCase_ ) ).map(lambda lowerCamelCase_ : _rename_shard(*lowerCamelCase_ ) ).collect() else: # don't use any pattern lowerCamelCase__ : List[Any] = 0 lowerCamelCase__ : Dict = task_id_and_num_shards[0][0] self._rename( fpath.replace('SSSSS', f'''{shard_id:05d}''' ).replace('TTTTT', f'''{task_id:05d}''' ), fpath.replace(lowerCamelCase_, '' ), ) def a__ (self, lowerCamelCase_, ): '''simple docstring''' return SparkExamplesIterable(self.df )
316
1
"""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 A_ : Any = logging.get_logger(__name__) class a_ ( enum.Enum ): '''simple docstring''' lowerCamelCase__ : Dict = 0 lowerCamelCase__ : List[str] = 1 @add_end_docstrings(snake_case_ ) class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = 'generated' def __init__(self, *lowerCamelCase_, **lowerCamelCase_ ): '''simple docstring''' super().__init__(*lowerCamelCase_, **lowerCamelCase_ ) 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 a__ (self, lowerCamelCase_=None, lowerCamelCase_=None, lowerCamelCase_=None, lowerCamelCase_=None, lowerCamelCase_=None, lowerCamelCase_=None, **lowerCamelCase_, ): '''simple docstring''' lowerCamelCase__ : str = {} if truncation is not None: lowerCamelCase__ : int = truncation lowerCamelCase__ : List[Any] = generate_kwargs lowerCamelCase__ : List[Any] = {} if return_tensors is not None and return_type is None: lowerCamelCase__ : List[str] = ReturnType.TENSORS if return_tensors else ReturnType.TEXT if return_type is not None: lowerCamelCase__ : Tuple = return_type if clean_up_tokenization_spaces is not None: lowerCamelCase__ : Dict = clean_up_tokenization_spaces if stop_sequence is not None: lowerCamelCase__ : int = self.tokenizer.encode(lowerCamelCase_, add_special_tokens=lowerCamelCase_ ) if len(lowerCamelCase_ ) > 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.' ) lowerCamelCase__ : int = stop_sequence_ids[0] return preprocess_params, forward_params, postprocess_params def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' return True def a__ (self, *lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : List[Any] = self.model.config.prefix if self.model.config.prefix is not None else '' if isinstance(args[0], lowerCamelCase_ ): 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' ) lowerCamelCase__ : Optional[int] = ([prefix + arg for arg in args[0]],) lowerCamelCase__ : Dict = True elif isinstance(args[0], lowerCamelCase_ ): lowerCamelCase__ : Any = (prefix + args[0],) lowerCamelCase__ : Optional[int] = False else: raise ValueError( f''' `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`''' ) lowerCamelCase__ : Dict = self.tokenizer(*lowerCamelCase_, padding=lowerCamelCase_, truncation=lowerCamelCase_, 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, *lowerCamelCase_, **lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Tuple = super().__call__(*lowerCamelCase_, **lowerCamelCase_ ) if ( isinstance(args[0], lowerCamelCase_ ) and all(isinstance(lowerCamelCase_, lowerCamelCase_ ) for el in args[0] ) and all(len(lowerCamelCase_ ) == 1 for res in result ) ): return [res[0] for res in result] return result def a__ (self, lowerCamelCase_, lowerCamelCase_=TruncationStrategy.DO_NOT_TRUNCATE, **lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Any = self._parse_and_tokenize(lowerCamelCase_, truncation=lowerCamelCase_, **lowerCamelCase_ ) return inputs def a__ (self, lowerCamelCase_, **lowerCamelCase_ ): '''simple docstring''' if self.framework == "pt": lowerCamelCase__ , lowerCamelCase__ : Dict = model_inputs['input_ids'].shape elif self.framework == "tf": lowerCamelCase__ , lowerCamelCase__ : str = tf.shape(model_inputs['input_ids'] ).numpy() lowerCamelCase__ : List[str] = generate_kwargs.get('min_length', self.model.config.min_length ) lowerCamelCase__ : Optional[int] = generate_kwargs.get('max_length', self.model.config.max_length ) self.check_inputs(lowerCamelCase_, generate_kwargs['min_length'], generate_kwargs['max_length'] ) lowerCamelCase__ : Optional[Any] = self.model.generate(**lowerCamelCase_, **lowerCamelCase_ ) lowerCamelCase__ : Union[str, Any] = output_ids.shape[0] if self.framework == "pt": lowerCamelCase__ : List[Any] = output_ids.reshape(lowerCamelCase_, out_b // in_b, *output_ids.shape[1:] ) elif self.framework == "tf": lowerCamelCase__ : str = tf.reshape(lowerCamelCase_, (in_b, out_b // in_b, *output_ids.shape[1:]) ) return {"output_ids": output_ids} def a__ (self, lowerCamelCase_, lowerCamelCase_=ReturnType.TEXT, lowerCamelCase_=False ): '''simple docstring''' lowerCamelCase__ : str = [] for output_ids in model_outputs["output_ids"][0]: if return_type == ReturnType.TENSORS: lowerCamelCase__ : Optional[int] = {f'''{self.return_name}_token_ids''': output_ids} elif return_type == ReturnType.TEXT: lowerCamelCase__ : List[str] = { f'''{self.return_name}_text''': self.tokenizer.decode( lowerCamelCase_, skip_special_tokens=lowerCamelCase_, clean_up_tokenization_spaces=lowerCamelCase_, ) } records.append(lowerCamelCase_ ) return records @add_end_docstrings(snake_case_ ) class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : List[Any] = 'summary' def __call__(self, *lowerCamelCase_, **lowerCamelCase_ ): '''simple docstring''' return super().__call__(*lowerCamelCase_, **lowerCamelCase_ ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' 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(snake_case_ ) class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : List[str] = 'translation' def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' 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 a__ (self, *lowerCamelCase_, lowerCamelCase_=TruncationStrategy.DO_NOT_TRUNCATE, lowerCamelCase_=None, lowerCamelCase_=None ): '''simple docstring''' if getattr(self.tokenizer, '_build_translation_inputs', lowerCamelCase_ ): return self.tokenizer._build_translation_inputs( *lowerCamelCase_, return_tensors=self.framework, truncation=lowerCamelCase_, src_lang=lowerCamelCase_, tgt_lang=lowerCamelCase_ ) else: return super()._parse_and_tokenize(*lowerCamelCase_, truncation=lowerCamelCase_ ) def a__ (self, lowerCamelCase_=None, lowerCamelCase_=None, **lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Tuple = super()._sanitize_parameters(**lowerCamelCase_ ) if src_lang is not None: lowerCamelCase__ : Dict = src_lang if tgt_lang is not None: lowerCamelCase__ : Union[str, Any] = tgt_lang if src_lang is None and tgt_lang is None: # Backward compatibility, direct arguments use is preferred. lowerCamelCase__ : List[Any] = kwargs.get('task', self.task ) lowerCamelCase__ : int = task.split('_' ) if task and len(lowerCamelCase_ ) == 4: # translation, XX, to YY lowerCamelCase__ : int = items[1] lowerCamelCase__ : Optional[int] = items[3] return preprocess_params, forward_params, postprocess_params def __call__(self, *lowerCamelCase_, **lowerCamelCase_ ): '''simple docstring''' return super().__call__(*lowerCamelCase_, **lowerCamelCase_ )
316
"""simple docstring""" class a_ : '''simple docstring''' def __init__(self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Tuple = len(lowerCamelCase_ ) lowerCamelCase__ : Any = [0] * len_array if len_array > 0: lowerCamelCase__ : Union[str, Any] = array[0] for i in range(1, lowerCamelCase_ ): lowerCamelCase__ : Optional[int] = self.prefix_sum[i - 1] + array[i] def a__ (self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Dict = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(lowerCamelCase_ ) return False if __name__ == "__main__": import doctest doctest.testmod()
316
1
"""simple docstring""" import tempfile import torch from diffusers import ( DEISMultistepScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) from .test_schedulers import SchedulerCommonTest class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = (UniPCMultistepScheduler,) lowerCamelCase__ : int = (('num_inference_steps', 25),) def a__ (self, **lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = { 'num_train_timesteps': 1_0_0_0, 'beta_start': 0.0_001, 'beta_end': 0.02, 'beta_schedule': 'linear', 'solver_order': 2, 'solver_type': 'bh2', } config.update(**lowerCamelCase_ ) return config def a__ (self, lowerCamelCase_=0, **lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : int = dict(self.forward_default_kwargs ) lowerCamelCase__ : List[Any] = kwargs.pop('num_inference_steps', lowerCamelCase_ ) lowerCamelCase__ : Tuple = self.dummy_sample lowerCamelCase__ : Union[str, Any] = 0.1 * sample lowerCamelCase__ : List[str] = [residual + 0.2, residual + 0.15, residual + 0.10] for scheduler_class in self.scheduler_classes: lowerCamelCase__ : Any = self.get_scheduler_config(**lowerCamelCase_ ) lowerCamelCase__ : Union[str, Any] = scheduler_class(**lowerCamelCase_ ) scheduler.set_timesteps(lowerCamelCase_ ) # copy over dummy past residuals lowerCamelCase__ : List[Any] = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(lowerCamelCase_ ) lowerCamelCase__ : Optional[Any] = scheduler_class.from_pretrained(lowerCamelCase_ ) new_scheduler.set_timesteps(lowerCamelCase_ ) # copy over dummy past residuals lowerCamelCase__ : Any = dummy_past_residuals[: new_scheduler.config.solver_order] lowerCamelCase__ , lowerCamelCase__ : Optional[int] = sample, sample for t in range(lowerCamelCase_, time_step + scheduler.config.solver_order + 1 ): lowerCamelCase__ : Any = scheduler.step(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, **lowerCamelCase_ ).prev_sample lowerCamelCase__ : Any = new_scheduler.step(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, **lowerCamelCase_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def a__ (self, lowerCamelCase_=0, **lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : List[str] = dict(self.forward_default_kwargs ) lowerCamelCase__ : Union[str, Any] = kwargs.pop('num_inference_steps', lowerCamelCase_ ) lowerCamelCase__ : List[Any] = self.dummy_sample lowerCamelCase__ : Dict = 0.1 * sample lowerCamelCase__ : Dict = [residual + 0.2, residual + 0.15, residual + 0.10] for scheduler_class in self.scheduler_classes: lowerCamelCase__ : List[Any] = self.get_scheduler_config() lowerCamelCase__ : int = scheduler_class(**lowerCamelCase_ ) scheduler.set_timesteps(lowerCamelCase_ ) # copy over dummy past residuals (must be after setting timesteps) lowerCamelCase__ : int = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(lowerCamelCase_ ) lowerCamelCase__ : List[str] = 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) lowerCamelCase__ : Dict = dummy_past_residuals[: new_scheduler.config.solver_order] lowerCamelCase__ : Dict = scheduler.step(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, **lowerCamelCase_ ).prev_sample lowerCamelCase__ : str = new_scheduler.step(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, **lowerCamelCase_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def a__ (self, lowerCamelCase_=None, **lowerCamelCase_ ): '''simple docstring''' if scheduler is None: lowerCamelCase__ : List[Any] = self.scheduler_classes[0] lowerCamelCase__ : str = self.get_scheduler_config(**lowerCamelCase_ ) lowerCamelCase__ : List[Any] = scheduler_class(**lowerCamelCase_ ) lowerCamelCase__ : Optional[int] = self.scheduler_classes[0] lowerCamelCase__ : Tuple = self.get_scheduler_config(**lowerCamelCase_ ) lowerCamelCase__ : Union[str, Any] = scheduler_class(**lowerCamelCase_ ) lowerCamelCase__ : Tuple = 1_0 lowerCamelCase__ : Optional[int] = self.dummy_model() lowerCamelCase__ : str = self.dummy_sample_deter scheduler.set_timesteps(lowerCamelCase_ ) for i, t in enumerate(scheduler.timesteps ): lowerCamelCase__ : Tuple = model(lowerCamelCase_, lowerCamelCase_ ) lowerCamelCase__ : Union[str, Any] = scheduler.step(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ).prev_sample return sample def a__ (self ): '''simple docstring''' lowerCamelCase__ : int = dict(self.forward_default_kwargs ) lowerCamelCase__ : Optional[Any] = kwargs.pop('num_inference_steps', lowerCamelCase_ ) for scheduler_class in self.scheduler_classes: lowerCamelCase__ : Dict = self.get_scheduler_config() lowerCamelCase__ : List[str] = scheduler_class(**lowerCamelCase_ ) lowerCamelCase__ : Any = self.dummy_sample lowerCamelCase__ : Optional[int] = 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' ): lowerCamelCase__ : List[str] = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) lowerCamelCase__ : List[Any] = [residual + 0.2, residual + 0.15, residual + 0.10] lowerCamelCase__ : Any = dummy_past_residuals[: scheduler.config.solver_order] lowerCamelCase__ : List[Any] = scheduler.timesteps[5] lowerCamelCase__ : Union[str, Any] = scheduler.timesteps[6] lowerCamelCase__ : List[Any] = scheduler.step(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, **lowerCamelCase_ ).prev_sample lowerCamelCase__ : Dict = scheduler.step(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, **lowerCamelCase_ ).prev_sample self.assertEqual(output_a.shape, sample.shape ) self.assertEqual(output_a.shape, output_a.shape ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Tuple = UniPCMultistepScheduler(**self.get_scheduler_config() ) lowerCamelCase__ : str = self.full_loop(scheduler=lowerCamelCase_ ) lowerCamelCase__ : Optional[int] = torch.mean(torch.abs(lowerCamelCase_ ) ) assert abs(result_mean.item() - 0.2_464 ) < 1e-3 lowerCamelCase__ : List[Any] = DPMSolverSinglestepScheduler.from_config(scheduler.config ) lowerCamelCase__ : Tuple = DEISMultistepScheduler.from_config(scheduler.config ) lowerCamelCase__ : List[Any] = DPMSolverMultistepScheduler.from_config(scheduler.config ) lowerCamelCase__ : Optional[Any] = UniPCMultistepScheduler.from_config(scheduler.config ) lowerCamelCase__ : int = self.full_loop(scheduler=lowerCamelCase_ ) lowerCamelCase__ : Optional[Any] = torch.mean(torch.abs(lowerCamelCase_ ) ) assert abs(result_mean.item() - 0.2_464 ) < 1e-3 def a__ (self ): '''simple docstring''' for timesteps in [2_5, 5_0, 1_0_0, 9_9_9, 1_0_0_0]: self.check_over_configs(num_train_timesteps=lowerCamelCase_ ) def a__ (self ): '''simple docstring''' self.check_over_configs(thresholding=lowerCamelCase_ ) for order in [1, 2, 3]: for solver_type in ["bh1", "bh2"]: for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( thresholding=lowerCamelCase_, prediction_type=lowerCamelCase_, sample_max_value=lowerCamelCase_, solver_order=lowerCamelCase_, solver_type=lowerCamelCase_, ) def a__ (self ): '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=lowerCamelCase_ ) def a__ (self ): '''simple docstring''' for solver_type in ["bh1", "bh2"]: for order in [1, 2, 3]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( solver_order=lowerCamelCase_, solver_type=lowerCamelCase_, prediction_type=lowerCamelCase_, ) lowerCamelCase__ : str = self.full_loop( solver_order=lowerCamelCase_, solver_type=lowerCamelCase_, prediction_type=lowerCamelCase_, ) assert not torch.isnan(lowerCamelCase_ ).any(), "Samples have nan numbers" def a__ (self ): '''simple docstring''' self.check_over_configs(lower_order_final=lowerCamelCase_ ) self.check_over_configs(lower_order_final=lowerCamelCase_ ) def a__ (self ): '''simple docstring''' for num_inference_steps in [1, 2, 3, 5, 1_0, 5_0, 1_0_0, 9_9_9, 1_0_0_0]: self.check_over_forward(num_inference_steps=lowerCamelCase_, time_step=0 ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = self.full_loop() lowerCamelCase__ : int = torch.mean(torch.abs(lowerCamelCase_ ) ) assert abs(result_mean.item() - 0.2_464 ) < 1e-3 def a__ (self ): '''simple docstring''' lowerCamelCase__ : Dict = self.full_loop(prediction_type='v_prediction' ) lowerCamelCase__ : Union[str, Any] = torch.mean(torch.abs(lowerCamelCase_ ) ) assert abs(result_mean.item() - 0.1_014 ) < 1e-3 def a__ (self ): '''simple docstring''' lowerCamelCase__ : Optional[int] = self.scheduler_classes[0] lowerCamelCase__ : Optional[int] = self.get_scheduler_config(thresholding=lowerCamelCase_, dynamic_thresholding_ratio=0 ) lowerCamelCase__ : Optional[Any] = scheduler_class(**lowerCamelCase_ ) lowerCamelCase__ : Union[str, Any] = 1_0 lowerCamelCase__ : List[str] = self.dummy_model() lowerCamelCase__ : int = self.dummy_sample_deter.half() scheduler.set_timesteps(lowerCamelCase_ ) for i, t in enumerate(scheduler.timesteps ): lowerCamelCase__ : Optional[int] = model(lowerCamelCase_, lowerCamelCase_ ) lowerCamelCase__ : List[str] = scheduler.step(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ).prev_sample assert sample.dtype == torch.floataa def a__ (self, **lowerCamelCase_ ): '''simple docstring''' for scheduler_class in self.scheduler_classes: lowerCamelCase__ : Tuple = self.get_scheduler_config(**lowerCamelCase_ ) lowerCamelCase__ : Optional[Any] = scheduler_class(**lowerCamelCase_ ) scheduler.set_timesteps(scheduler.config.num_train_timesteps ) assert len(scheduler.timesteps.unique() ) == scheduler.num_inference_steps
316
"""simple docstring""" import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : Dict = ['image_processor', 'tokenizer'] lowerCamelCase__ : Optional[int] = 'CLIPImageProcessor' lowerCamelCase__ : List[str] = ('XLMRobertaTokenizer', 'XLMRobertaTokenizerFast') def __init__(self, lowerCamelCase_=None, lowerCamelCase_=None, **lowerCamelCase_ ): '''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.', lowerCamelCase_, ) 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__(lowerCamelCase_, lowerCamelCase_ ) def __call__(self, lowerCamelCase_=None, lowerCamelCase_=None, lowerCamelCase_=None, **lowerCamelCase_ ): '''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__ : Any = self.tokenizer(lowerCamelCase_, return_tensors=lowerCamelCase_, **lowerCamelCase_ ) if images is not None: lowerCamelCase__ : List[Any] = self.image_processor(lowerCamelCase_, return_tensors=lowerCamelCase_, **lowerCamelCase_ ) if text is not None and images is not None: lowerCamelCase__ : str = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**lowerCamelCase_ ), tensor_type=lowerCamelCase_ ) def a__ (self, *lowerCamelCase_, **lowerCamelCase_ ): '''simple docstring''' return self.tokenizer.batch_decode(*lowerCamelCase_, **lowerCamelCase_ ) def a__ (self, *lowerCamelCase_, **lowerCamelCase_ ): '''simple docstring''' return self.tokenizer.decode(*lowerCamelCase_, **lowerCamelCase_ ) @property def a__ (self ): '''simple docstring''' lowerCamelCase__ : str = self.tokenizer.model_input_names lowerCamelCase__ : List[str] = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
316
1
"""simple docstring""" class a_ : '''simple docstring''' def __init__(self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Tuple = len(lowerCamelCase_ ) lowerCamelCase__ : Any = [0] * len_array if len_array > 0: lowerCamelCase__ : Union[str, Any] = array[0] for i in range(1, lowerCamelCase_ ): lowerCamelCase__ : Optional[int] = self.prefix_sum[i - 1] + array[i] def a__ (self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Dict = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(lowerCamelCase_ ) return False if __name__ == "__main__": import doctest doctest.testmod()
316
"""simple docstring""" import cva import numpy as np class a_ : '''simple docstring''' def __init__(self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' if k in (0.04, 0.06): lowerCamelCase__ : Tuple = k lowerCamelCase__ : Optional[Any] = window_size else: raise ValueError('invalid k value' ) def __str__(self ): '''simple docstring''' return str(self.k ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = cva.imread(lowerCamelCase_, 0 ) lowerCamelCase__ , lowerCamelCase__ : Union[str, Any] = img.shape lowerCamelCase__ : list[list[int]] = [] lowerCamelCase__ : Optional[Any] = img.copy() lowerCamelCase__ : Optional[Any] = cva.cvtColor(lowerCamelCase_, cva.COLOR_GRAY2RGB ) lowerCamelCase__ , lowerCamelCase__ : Any = np.gradient(lowerCamelCase_ ) lowerCamelCase__ : Optional[Any] = dx**2 lowerCamelCase__ : List[Any] = dy**2 lowerCamelCase__ : List[str] = dx * dy lowerCamelCase__ : Tuple = 0.04 lowerCamelCase__ : List[Any] = self.window_size // 2 for y in range(lowerCamelCase_, h - offset ): for x in range(lowerCamelCase_, w - offset ): lowerCamelCase__ : Union[str, Any] = ixx[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() lowerCamelCase__ : Optional[Any] = iyy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() lowerCamelCase__ : List[Any] = ixy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() lowerCamelCase__ : str = (wxx * wyy) - (wxy**2) lowerCamelCase__ : Dict = wxx + wyy lowerCamelCase__ : 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_5_5 ) return color_img, corner_list if __name__ == "__main__": A_ : Optional[Any] = HarrisCorner(0.04, 3) A_, A_ : List[Any] = edge_detect.detect("path_to_image") cva.imwrite("detect.png", color_img)
316
1
"""simple docstring""" import argparse import json from collections import OrderedDict from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import PoolFormerConfig, PoolFormerForImageClassification, PoolFormerImageProcessor from transformers.utils import logging logging.set_verbosity_info() A_ : List[Any] = logging.get_logger(__name__) def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : str = original_name.split('.' )[0] lowerCamelCase__ : List[str] = key.split('.' ) lowerCamelCase__ : Tuple = int(key_list[key_list.index(_lowerCamelCase ) - 2] ) lowerCamelCase__ : Any = int(key_list[key_list.index(_lowerCamelCase ) - 1] ) lowerCamelCase__ : str = orig_block_num - offset lowerCamelCase__ : Union[str, Any] = key.replace(f'''{orig_block_num}.{layer_num}.{original_name}''' , f'''block.{new_block_num}.{layer_num}.{new_name}''' ) return key def lowerCamelCase_ ( _lowerCamelCase ): lowerCamelCase__ : List[str] = OrderedDict() lowerCamelCase__ , lowerCamelCase__ : List[str] = 0, 0 for key, value in state_dict.items(): if key.startswith('network' ): lowerCamelCase__ : Tuple = key.replace('network' , 'poolformer.encoder' ) if "proj" in key: # Works for the first embedding as well as the internal embedding layers if key.endswith('bias' ) and "patch_embed" not in key: patch_emb_offset += 1 lowerCamelCase__ : Union[str, Any] = key[: key.find('proj' )] lowerCamelCase__ : Any = key.replace(_lowerCamelCase , f'''patch_embeddings.{total_embed_found}.''' ) lowerCamelCase__ : List[Any] = key.replace('proj' , 'projection' ) if key.endswith('bias' ): total_embed_found += 1 if "patch_embeddings" in key: lowerCamelCase__ : Union[str, Any] = 'poolformer.encoder.' + key if "mlp.fc1" in key: lowerCamelCase__ : List[Any] = replace_key_with_offset(_lowerCamelCase , _lowerCamelCase , 'mlp.fc1' , 'output.conv1' ) if "mlp.fc2" in key: lowerCamelCase__ : List[str] = replace_key_with_offset(_lowerCamelCase , _lowerCamelCase , 'mlp.fc2' , 'output.conv2' ) if "norm1" in key: lowerCamelCase__ : Optional[Any] = replace_key_with_offset(_lowerCamelCase , _lowerCamelCase , 'norm1' , 'before_norm' ) if "norm2" in key: lowerCamelCase__ : Union[str, Any] = replace_key_with_offset(_lowerCamelCase , _lowerCamelCase , 'norm2' , 'after_norm' ) if "layer_scale_1" in key: lowerCamelCase__ : int = replace_key_with_offset(_lowerCamelCase , _lowerCamelCase , 'layer_scale_1' , 'layer_scale_1' ) if "layer_scale_2" in key: lowerCamelCase__ : Tuple = replace_key_with_offset(_lowerCamelCase , _lowerCamelCase , 'layer_scale_2' , 'layer_scale_2' ) if "head" in key: lowerCamelCase__ : Optional[int] = key.replace('head' , 'classifier' ) lowerCamelCase__ : Optional[Any] = value return new_state_dict def lowerCamelCase_ ( ): lowerCamelCase__ : Any = 'http://images.cocodataset.org/val2017/000000039769.jpg' lowerCamelCase__ : str = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ) return image @torch.no_grad() def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : Tuple = PoolFormerConfig() # set attributes based on model_name lowerCamelCase__ : Optional[int] = 'huggingface/label-files' lowerCamelCase__ : Any = model_name[-3:] lowerCamelCase__ : Dict = 1000 lowerCamelCase__ : Union[str, Any] = 'imagenet-1k-id2label.json' lowerCamelCase__ : int = (1, 1000) # set config attributes lowerCamelCase__ : Any = json.load(open(hf_hub_download(_lowerCamelCase , _lowerCamelCase , repo_type='dataset' ) , 'r' ) ) lowerCamelCase__ : List[str] = {int(_lowerCamelCase ): v for k, v in idalabel.items()} lowerCamelCase__ : List[str] = idalabel lowerCamelCase__ : List[str] = {v: k for k, v in idalabel.items()} if size == "s12": lowerCamelCase__ : Any = [2, 2, 6, 2] lowerCamelCase__ : Tuple = [64, 128, 320, 512] lowerCamelCase__ : Tuple = 4.0 lowerCamelCase__ : List[str] = 0.9 elif size == "s24": lowerCamelCase__ : Dict = [4, 4, 12, 4] lowerCamelCase__ : List[Any] = [64, 128, 320, 512] lowerCamelCase__ : Any = 4.0 lowerCamelCase__ : Optional[Any] = 0.9 elif size == "s36": lowerCamelCase__ : Dict = [6, 6, 18, 6] lowerCamelCase__ : Tuple = [64, 128, 320, 512] lowerCamelCase__ : Optional[int] = 4.0 lowerCamelCase__ : Dict = 1e-6 lowerCamelCase__ : Tuple = 0.9 elif size == "m36": lowerCamelCase__ : List[str] = [6, 6, 18, 6] lowerCamelCase__ : int = [96, 192, 384, 768] lowerCamelCase__ : int = 4.0 lowerCamelCase__ : Union[str, Any] = 1e-6 lowerCamelCase__ : str = 0.95 elif size == "m48": lowerCamelCase__ : Optional[Any] = [8, 8, 24, 8] lowerCamelCase__ : int = [96, 192, 384, 768] lowerCamelCase__ : Tuple = 4.0 lowerCamelCase__ : Optional[int] = 1e-6 lowerCamelCase__ : int = 0.95 else: raise ValueError(f'''Size {size} not supported''' ) # load image processor lowerCamelCase__ : List[Any] = PoolFormerImageProcessor(crop_pct=_lowerCamelCase ) # Prepare image lowerCamelCase__ : Dict = prepare_img() lowerCamelCase__ : List[str] = image_processor(images=_lowerCamelCase , return_tensors='pt' ).pixel_values logger.info(f'''Converting model {model_name}...''' ) # load original state dict lowerCamelCase__ : Tuple = torch.load(_lowerCamelCase , map_location=torch.device('cpu' ) ) # rename keys lowerCamelCase__ : int = rename_keys(_lowerCamelCase ) # create HuggingFace model and load state dict lowerCamelCase__ : Any = PoolFormerForImageClassification(_lowerCamelCase ) model.load_state_dict(_lowerCamelCase ) model.eval() # Define image processor lowerCamelCase__ : Dict = PoolFormerImageProcessor(crop_pct=_lowerCamelCase ) lowerCamelCase__ : int = image_processor(images=prepare_img() , return_tensors='pt' ).pixel_values # forward pass lowerCamelCase__ : List[str] = model(_lowerCamelCase ) lowerCamelCase__ : Union[str, Any] = outputs.logits # define expected logit slices for different models if size == "s12": lowerCamelCase__ : str = torch.tensor([-0.3_045, -0.6_758, -0.4_869] ) elif size == "s24": lowerCamelCase__ : Union[str, Any] = torch.tensor([0.4_402, -0.1_374, -0.8_045] ) elif size == "s36": lowerCamelCase__ : str = torch.tensor([-0.6_080, -0.5_133, -0.5_898] ) elif size == "m36": lowerCamelCase__ : List[str] = torch.tensor([0.3_952, 0.2_263, -1.2_668] ) elif size == "m48": lowerCamelCase__ : Tuple = torch.tensor([0.1_167, -0.0_656, -0.3_423] ) else: raise ValueError(f'''Size {size} not supported''' ) # verify logits assert logits.shape == expected_shape assert torch.allclose(logits[0, :3] , _lowerCamelCase , atol=1e-2 ) # finally, save model and image processor logger.info(f'''Saving PyTorch model and image processor to {pytorch_dump_folder_path}...''' ) Path(_lowerCamelCase ).mkdir(exist_ok=_lowerCamelCase ) model.save_pretrained(_lowerCamelCase ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(_lowerCamelCase ) if __name__ == "__main__": A_ : Dict = argparse.ArgumentParser() parser.add_argument( "--model_name", default="poolformer_s12", type=str, help="Name of the model you'd like to convert.", ) 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." ) A_ : Any = parser.parse_args() convert_poolformer_checkpoint(args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path)
316
"""simple docstring""" from collections.abc import Iterator, MutableMapping from dataclasses import dataclass from typing import Generic, TypeVar A_ : str = TypeVar("KEY") A_ : List[Any] = TypeVar("VAL") @dataclass(frozen=snake_case_ , slots=snake_case_ ) class a_ ( Generic[KEY, VAL] ): '''simple docstring''' lowerCamelCase__ : KEY lowerCamelCase__ : VAL class a_ ( _Item ): '''simple docstring''' def __init__(self ): '''simple docstring''' super().__init__(lowerCamelCase_, lowerCamelCase_ ) def __bool__(self ): '''simple docstring''' return False A_ : List[Any] = _DeletedItem() class a_ ( MutableMapping[KEY, VAL] ): '''simple docstring''' def __init__(self, lowerCamelCase_ = 8, lowerCamelCase_ = 0.75 ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = initial_block_size lowerCamelCase__ : list[_Item | None] = [None] * initial_block_size assert 0.0 < capacity_factor < 1.0 lowerCamelCase__ : List[Any] = capacity_factor lowerCamelCase__ : Optional[int] = 0 def a__ (self, lowerCamelCase_ ): '''simple docstring''' return hash(lowerCamelCase_ ) % len(self._buckets ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' return (ind + 1) % len(self._buckets ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Optional[int] = self._buckets[ind] if not stored: lowerCamelCase__ : Tuple = _Item(lowerCamelCase_, lowerCamelCase_ ) self._len += 1 return True elif stored.key == key: lowerCamelCase__ : Optional[int] = _Item(lowerCamelCase_, lowerCamelCase_ ) return True else: return False def a__ (self ): '''simple docstring''' lowerCamelCase__ : int = len(self._buckets ) * self._capacity_factor return len(self ) >= int(lowerCamelCase_ ) def a__ (self ): '''simple docstring''' if len(self._buckets ) <= self._initial_block_size: return False lowerCamelCase__ : Any = len(self._buckets ) * self._capacity_factor / 2 return len(self ) < limit def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Any = self._buckets lowerCamelCase__ : Dict = [None] * new_size lowerCamelCase__ : Tuple = 0 for item in old_buckets: if item: self._add_item(item.key, item.val ) def a__ (self ): '''simple docstring''' self._resize(len(self._buckets ) * 2 ) def a__ (self ): '''simple docstring''' self._resize(len(self._buckets ) // 2 ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = self._get_bucket_index(lowerCamelCase_ ) for _ in range(len(self._buckets ) ): yield ind lowerCamelCase__ : Tuple = self._get_next_ind(lowerCamelCase_ ) def a__ (self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' for ind in self._iterate_buckets(lowerCamelCase_ ): if self._try_set(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): break def __setitem__(self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' if self._is_full(): self._size_up() self._add_item(lowerCamelCase_, lowerCamelCase_ ) def __delitem__(self, lowerCamelCase_ ): '''simple docstring''' for ind in self._iterate_buckets(lowerCamelCase_ ): lowerCamelCase__ : List[str] = self._buckets[ind] if item is None: raise KeyError(lowerCamelCase_ ) if item is _deleted: continue if item.key == key: lowerCamelCase__ : Optional[int] = _deleted self._len -= 1 break if self._is_sparse(): self._size_down() def __getitem__(self, lowerCamelCase_ ): '''simple docstring''' for ind in self._iterate_buckets(lowerCamelCase_ ): lowerCamelCase__ : List[Any] = self._buckets[ind] if item is None: break if item is _deleted: continue if item.key == key: return item.val raise KeyError(lowerCamelCase_ ) def __len__(self ): '''simple docstring''' return self._len def __iter__(self ): '''simple docstring''' yield from (item.key for item in self._buckets if item) def __repr__(self ): '''simple docstring''' lowerCamelCase__ : List[str] = ' ,'.join( f'''{item.key}: {item.val}''' for item in self._buckets if item ) return f'''HashMap({val_string})'''
316
1
"""simple docstring""" import argparse import pathlib import fairseq import torch from fairseq.models.roberta import RobertaModel as FairseqRobertaModel from fairseq.modules import TransformerSentenceEncoderLayer from packaging import version from transformers import XLMRobertaConfig, XLMRobertaXLForMaskedLM, XLMRobertaXLForSequenceClassification from transformers.models.bert.modeling_bert import ( BertIntermediate, BertLayer, BertOutput, BertSelfAttention, BertSelfOutput, ) from transformers.models.roberta.modeling_roberta import RobertaAttention from transformers.utils import logging if version.parse(fairseq.__version__) < version.parse("1.0.0a"): raise Exception("requires fairseq >= 1.0.0a") logging.set_verbosity_info() A_ : List[str] = logging.get_logger(__name__) A_ : Any = "Hello world! cécé herlolip" def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : Union[str, Any] = FairseqRobertaModel.from_pretrained(_lowerCamelCase ) roberta.eval() # disable dropout lowerCamelCase__ : Tuple = roberta.model.encoder.sentence_encoder lowerCamelCase__ : Tuple = XLMRobertaConfig( vocab_size=roberta_sent_encoder.embed_tokens.num_embeddings , hidden_size=roberta.cfg.model.encoder_embed_dim , num_hidden_layers=roberta.cfg.model.encoder_layers , num_attention_heads=roberta.cfg.model.encoder_attention_heads , intermediate_size=roberta.cfg.model.encoder_ffn_embed_dim , max_position_embeddings=514 , type_vocab_size=1 , layer_norm_eps=1e-5 , ) if classification_head: lowerCamelCase__ : int = roberta.model.classification_heads['mnli'].out_proj.weight.shape[0] print('Our RoBERTa config:' , _lowerCamelCase ) lowerCamelCase__ : List[str] = XLMRobertaXLForSequenceClassification(_lowerCamelCase ) if classification_head else XLMRobertaXLForMaskedLM(_lowerCamelCase ) model.eval() # Now let's copy all the weights. # Embeddings lowerCamelCase__ : Dict = roberta_sent_encoder.embed_tokens.weight lowerCamelCase__ : List[Any] = roberta_sent_encoder.embed_positions.weight lowerCamelCase__ : Tuple = torch.zeros_like( model.roberta.embeddings.token_type_embeddings.weight ) # just zero them out b/c RoBERTa doesn't use them. lowerCamelCase__ : Optional[Any] = roberta_sent_encoder.layer_norm.weight lowerCamelCase__ : Dict = roberta_sent_encoder.layer_norm.bias for i in range(config.num_hidden_layers ): # Encoder: start of layer lowerCamelCase__ : BertLayer = model.roberta.encoder.layer[i] lowerCamelCase__ : TransformerSentenceEncoderLayer = roberta_sent_encoder.layers[i] lowerCamelCase__ : RobertaAttention = layer.attention lowerCamelCase__ : Any = roberta_layer.self_attn_layer_norm.weight lowerCamelCase__ : List[str] = roberta_layer.self_attn_layer_norm.bias # self attention lowerCamelCase__ : BertSelfAttention = layer.attention.self assert ( roberta_layer.self_attn.k_proj.weight.data.shape == roberta_layer.self_attn.q_proj.weight.data.shape == roberta_layer.self_attn.v_proj.weight.data.shape == torch.Size((config.hidden_size, config.hidden_size) ) ) lowerCamelCase__ : Optional[Any] = roberta_layer.self_attn.q_proj.weight lowerCamelCase__ : List[Any] = roberta_layer.self_attn.q_proj.bias lowerCamelCase__ : Union[str, Any] = roberta_layer.self_attn.k_proj.weight lowerCamelCase__ : Optional[Any] = roberta_layer.self_attn.k_proj.bias lowerCamelCase__ : List[str] = roberta_layer.self_attn.v_proj.weight lowerCamelCase__ : Any = roberta_layer.self_attn.v_proj.bias # self-attention output lowerCamelCase__ : BertSelfOutput = layer.attention.output assert self_output.dense.weight.shape == roberta_layer.self_attn.out_proj.weight.shape lowerCamelCase__ : Optional[int] = roberta_layer.self_attn.out_proj.weight lowerCamelCase__ : Dict = roberta_layer.self_attn.out_proj.bias # this one is final layer norm lowerCamelCase__ : Dict = roberta_layer.final_layer_norm.weight lowerCamelCase__ : Tuple = roberta_layer.final_layer_norm.bias # intermediate lowerCamelCase__ : BertIntermediate = layer.intermediate assert intermediate.dense.weight.shape == roberta_layer.fca.weight.shape lowerCamelCase__ : Optional[Any] = roberta_layer.fca.weight lowerCamelCase__ : int = roberta_layer.fca.bias # output lowerCamelCase__ : BertOutput = layer.output assert bert_output.dense.weight.shape == roberta_layer.fca.weight.shape lowerCamelCase__ : Any = roberta_layer.fca.weight lowerCamelCase__ : Optional[Any] = roberta_layer.fca.bias # end of layer if classification_head: lowerCamelCase__ : Optional[int] = roberta.model.classification_heads['mnli'].dense.weight lowerCamelCase__ : List[str] = roberta.model.classification_heads['mnli'].dense.bias lowerCamelCase__ : Union[str, Any] = roberta.model.classification_heads['mnli'].out_proj.weight lowerCamelCase__ : Optional[int] = roberta.model.classification_heads['mnli'].out_proj.bias else: # LM Head lowerCamelCase__ : Tuple = roberta.model.encoder.lm_head.dense.weight lowerCamelCase__ : List[str] = roberta.model.encoder.lm_head.dense.bias lowerCamelCase__ : Any = roberta.model.encoder.lm_head.layer_norm.weight lowerCamelCase__ : Any = roberta.model.encoder.lm_head.layer_norm.bias lowerCamelCase__ : Union[str, Any] = roberta.model.encoder.lm_head.weight lowerCamelCase__ : Optional[Any] = roberta.model.encoder.lm_head.bias # Let's check that we get the same results. lowerCamelCase__ : torch.Tensor = roberta.encode(_lowerCamelCase ).unsqueeze(0 ) # batch of size 1 lowerCamelCase__ : Dict = model(_lowerCamelCase )[0] if classification_head: lowerCamelCase__ : Dict = roberta.model.classification_heads['mnli'](roberta.extract_features(_lowerCamelCase ) ) else: lowerCamelCase__ : Union[str, Any] = roberta.model(_lowerCamelCase )[0] print(our_output.shape , their_output.shape ) lowerCamelCase__ : Optional[Any] = torch.max(torch.abs(our_output - their_output ) ).item() print(f'''max_absolute_diff = {max_absolute_diff}''' ) # ~ 1e-7 lowerCamelCase__ : Optional[Any] = torch.allclose(_lowerCamelCase , _lowerCamelCase , atol=1e-3 ) print('Do both models output the same tensors?' , '🔥' if success else '💩' ) if not success: raise Exception('Something went wRoNg' ) pathlib.Path(_lowerCamelCase ).mkdir(parents=_lowerCamelCase , exist_ok=_lowerCamelCase ) print(f'''Saving model to {pytorch_dump_folder_path}''' ) model.save_pretrained(_lowerCamelCase ) if __name__ == "__main__": A_ : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--roberta_checkpoint_path", default=None, type=str, required=True, help="Path the official PyTorch dump." ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) parser.add_argument( "--classification_head", action="store_true", help="Whether to convert a final classification head." ) A_ : Tuple = parser.parse_args() convert_xlm_roberta_xl_checkpoint_to_pytorch( args.roberta_checkpoint_path, args.pytorch_dump_folder_path, args.classification_head )
316
"""simple docstring""" def lowerCamelCase_ ( ): lowerCamelCase__ : Optional[Any] = [] lowerCamelCase__ : List[Any] = 1 while len(_lowerCamelCase ) < 1e6: constant.append(str(_lowerCamelCase ) ) i += 1 lowerCamelCase__ : str = ''.join(_lowerCamelCase ) return ( int(constant[0] ) * int(constant[9] ) * int(constant[99] ) * int(constant[999] ) * int(constant[9999] ) * int(constant[9_9999] ) * int(constant[99_9999] ) ) if __name__ == "__main__": print(solution())
316
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging A_ : Optional[int] = logging.get_logger(__name__) A_ : Union[str, Any] = { "alibaba-damo/mgp-str-base": "https://huggingface.co/alibaba-damo/mgp-str-base/resolve/main/config.json", } class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : Optional[int] = 'mgp-str' def __init__(self, lowerCamelCase_=[3_2, 1_2_8], lowerCamelCase_=4, lowerCamelCase_=3, lowerCamelCase_=2_7, lowerCamelCase_=3_8, lowerCamelCase_=5_0_2_5_7, lowerCamelCase_=3_0_5_2_2, lowerCamelCase_=7_6_8, lowerCamelCase_=1_2, lowerCamelCase_=1_2, lowerCamelCase_=4.0, lowerCamelCase_=True, lowerCamelCase_=False, lowerCamelCase_=1e-5, lowerCamelCase_=0.0, lowerCamelCase_=0.0, lowerCamelCase_=0.0, lowerCamelCase_=False, lowerCamelCase_=0.02, **lowerCamelCase_, ): '''simple docstring''' super().__init__(**lowerCamelCase_ ) lowerCamelCase__ : Optional[Any] = image_size lowerCamelCase__ : Tuple = patch_size lowerCamelCase__ : str = num_channels lowerCamelCase__ : Tuple = max_token_length lowerCamelCase__ : Dict = num_character_labels lowerCamelCase__ : Tuple = num_bpe_labels lowerCamelCase__ : Tuple = num_wordpiece_labels lowerCamelCase__ : str = hidden_size lowerCamelCase__ : str = num_hidden_layers lowerCamelCase__ : str = num_attention_heads lowerCamelCase__ : List[str] = mlp_ratio lowerCamelCase__ : Optional[Any] = distilled lowerCamelCase__ : List[str] = layer_norm_eps lowerCamelCase__ : Dict = drop_rate lowerCamelCase__ : str = qkv_bias lowerCamelCase__ : Dict = attn_drop_rate lowerCamelCase__ : List[str] = drop_path_rate lowerCamelCase__ : Tuple = output_aa_attentions lowerCamelCase__ : Tuple = initializer_range
316
"""simple docstring""" # Lint as: python3 # pylint: enable=line-too-long # pylint: disable=g-import-not-at-top,g-bad-import-order,wrong-import-position A_ : Union[str, Any] = "2.13.1" import platform import pyarrow from packaging import version if version.parse(platform.python_version()) < version.parse("3.7"): raise ImportWarning( "To use `datasets`, Python>=3.7 is required, and the current version of Python doesn't match this condition." ) if version.parse(pyarrow.__version__).major < 8: raise ImportWarning( "To use `datasets`, the module `pyarrow>=8.0.0` is required, and the current version of `pyarrow` doesn't match this condition.\n" "If you are running this in a Google Colab, you should probably just restart the runtime to use the right version of `pyarrow`." ) del platform del pyarrow del version from .arrow_dataset import Dataset from .arrow_reader import ReadInstruction from .builder import ArrowBasedBuilder, BeamBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder from .combine import concatenate_datasets, interleave_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .download import * from .features import * from .fingerprint import disable_caching, enable_caching, is_caching_enabled, set_caching_enabled from .info import DatasetInfo, MetricInfo from .inspect import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, list_datasets, list_metrics, ) from .iterable_dataset import IterableDataset from .load import load_dataset, load_dataset_builder, load_from_disk, load_metric from .metric import Metric from .splits import ( NamedSplit, NamedSplitAll, Split, SplitBase, SplitDict, SplitGenerator, SplitInfo, SubSplitInfo, percent, ) from .tasks import * from .utils import * from .utils import logging # deprecated modules from datasets import arrow_dataset as _arrow_dataset # isort:skip from datasets import utils as _utils # isort:skip from datasets.utils import download_manager as _deprecated_download_manager # isort:skip A_ : int = concatenate_datasets A_ : Any = DownloadConfig A_ : List[Any] = DownloadManager A_ : Optional[Any] = DownloadMode A_ : List[str] = DownloadConfig A_ : Optional[int] = DownloadMode A_ : Dict = DownloadManager del _arrow_dataset, _utils, _deprecated_download_manager
316
1
"""simple docstring""" import numpy as np def lowerCamelCase_ ( _lowerCamelCase ): return (2 / (1 + np.exp(-2 * vector ))) - 1 if __name__ == "__main__": import doctest doctest.testmod()
316
"""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 A_ : str = logging.getLogger(__name__) @dataclass @add_start_docstrings(TrainingArguments.__doc__ ) class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : bool = field(default=snake_case_ , metadata={'help': 'Whether to use SortishSampler or not.'} ) lowerCamelCase__ : bool = field( default=snake_case_ , metadata={'help': 'Whether to use generate to calculate generative metrics (ROUGE, BLEU).'} ) lowerCamelCase__ : Optional[int] = field( default=snake_case_ , 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__ : Optional[int] = field( default=snake_case_ , 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__ : Optional[Union[str, Path, GenerationConfig]] = field( default=snake_case_ , metadata={ 'help': 'Model id, file path or url pointing to a GenerationConfig json file, to use during prediction.' } , ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Any = super().to_dict() for k, v in d.items(): if isinstance(lowerCamelCase_, lowerCamelCase_ ): lowerCamelCase__ : Any = v.to_dict() return d
316
1
"""simple docstring""" def lowerCamelCase_ ( _lowerCamelCase ): if num < 0: return False lowerCamelCase__ : int = num lowerCamelCase__ : int = 0 while num > 0: lowerCamelCase__ : Optional[int] = rev_num * 10 + (num % 10) num //= 10 return num_copy == rev_num if __name__ == "__main__": import doctest doctest.testmod()
316
"""simple docstring""" def lowerCamelCase_ ( _lowerCamelCase ): lowerCamelCase__ : Union[str, Any] = [] lowerCamelCase__ : List[str] = [] lowerCamelCase__ : Tuple = { '^': 3, '*': 2, '/': 2, '%': 2, '+': 1, '-': 1, } # Priority of each operator lowerCamelCase__ : List[str] = len(_lowerCamelCase ) if (len(_lowerCamelCase ) > 7) else 7 # Print table header for output print( 'Symbol'.center(8 ) , 'Stack'.center(_lowerCamelCase ) , 'Postfix'.center(_lowerCamelCase ) , sep=' | ' , ) print('-' * (print_width * 3 + 7) ) for x in infix: if x.isalpha() or x.isdigit(): post_fix.append(_lowerCamelCase ) # if x is Alphabet / Digit, add it to Postfix elif x == "(": stack.append(_lowerCamelCase ) # if x is "(" push to Stack elif x == ")": # if x is ")" pop stack until "(" is encountered while stack[-1] != "(": post_fix.append(stack.pop() ) # Pop stack & add the content to Postfix stack.pop() else: if len(_lowerCamelCase ) == 0: stack.append(_lowerCamelCase ) # If stack is empty, push x to stack else: # while priority of x is not > priority of element in the stack while len(_lowerCamelCase ) > 0 and priority[x] <= priority[stack[-1]]: post_fix.append(stack.pop() ) # pop stack & add to Postfix stack.append(_lowerCamelCase ) # push x to stack print( x.center(8 ) , (''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , (''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , sep=' | ' , ) # Output in tabular format while len(_lowerCamelCase ) > 0: # while stack is not empty post_fix.append(stack.pop() ) # pop stack & add to Postfix print( ' '.center(8 ) , (''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , (''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , sep=' | ' , ) # Output in tabular format return "".join(_lowerCamelCase ) # return Postfix as str def lowerCamelCase_ ( _lowerCamelCase ): lowerCamelCase__ : Union[str, Any] = list(infix[::-1] ) # reverse the infix equation for i in range(len(_lowerCamelCase ) ): if infix[i] == "(": lowerCamelCase__ : List[Any] = ')' # change "(" to ")" elif infix[i] == ")": lowerCamelCase__ : Tuple = '(' # change ")" to "(" return (infix_2_postfix(''.join(_lowerCamelCase ) ))[ ::-1 ] # call infix_2_postfix on Infix, return reverse of Postfix if __name__ == "__main__": A_ : Tuple = input("\nEnter an Infix Equation = ") # Input an Infix equation A_ : List[str] = "".join(Infix.split()) # Remove spaces from the input print("\n\t", Infix, "(Infix) -> ", infix_2_prefix(Infix), "(Prefix)")
316
1
"""simple docstring""" import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging A_ : str = logging.get_logger(__name__) A_ : Optional[int] = "▁" A_ : Dict = {"vocab_file": "sentencepiece.bpe.model", "monolingual_vocab_file": "dict.txt"} A_ : List[str] = { "vocab_file": { "vinai/bartpho-syllable": "https://huggingface.co/vinai/bartpho-syllable/resolve/main/sentencepiece.bpe.model", }, "monolingual_vocab_file": { "vinai/bartpho-syllable": "https://huggingface.co/vinai/bartpho-syllable/resolve/main/dict.txt", }, } A_ : Tuple = {"vinai/bartpho-syllable": 10_24} class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : Tuple = VOCAB_FILES_NAMES lowerCamelCase__ : Dict = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase__ : Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase__ : Any = ['input_ids', 'attention_mask'] def __init__(self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_="<s>", lowerCamelCase_="</s>", lowerCamelCase_="</s>", lowerCamelCase_="<s>", lowerCamelCase_="<unk>", lowerCamelCase_="<pad>", lowerCamelCase_="<mask>", lowerCamelCase_ = None, **lowerCamelCase_, ): '''simple docstring''' lowerCamelCase__ : List[Any] = AddedToken(lowerCamelCase_, lstrip=lowerCamelCase_, rstrip=lowerCamelCase_ ) if isinstance(lowerCamelCase_, lowerCamelCase_ ) else mask_token lowerCamelCase__ : Tuple = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=lowerCamelCase_, eos_token=lowerCamelCase_, unk_token=lowerCamelCase_, sep_token=lowerCamelCase_, cls_token=lowerCamelCase_, pad_token=lowerCamelCase_, mask_token=lowerCamelCase_, sp_model_kwargs=self.sp_model_kwargs, **lowerCamelCase_, ) lowerCamelCase__ : int = vocab_file lowerCamelCase__ : List[Any] = monolingual_vocab_file lowerCamelCase__ : List[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(lowerCamelCase_ ) ) # Load the reduced vocab # Keep order of special tokens for backward compatibility lowerCamelCase__ : Optional[int] = {} lowerCamelCase__ : List[Any] = 0 for token in [bos_token, pad_token, eos_token, unk_token, sep_token, cls_token]: if str(lowerCamelCase_ ) not in self.fairseq_tokens_to_ids: lowerCamelCase__ : List[Any] = cnt cnt += 1 with open(lowerCamelCase_, 'r', encoding='utf-8' ) as f: for line in f.readlines(): lowerCamelCase__ : Tuple = line.strip().split()[0] lowerCamelCase__ : Any = len(self.fairseq_tokens_to_ids ) if str(lowerCamelCase_ ) not in self.fairseq_tokens_to_ids: lowerCamelCase__ : Optional[Any] = len(self.fairseq_tokens_to_ids ) lowerCamelCase__ : List[Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def __getstate__(self ): '''simple docstring''' lowerCamelCase__ : List[Any] = self.__dict__.copy() lowerCamelCase__ : Union[str, Any] = None lowerCamelCase__ : Optional[Any] = self.sp_model.serialized_model_proto() return state def __setstate__(self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Optional[int] = d # for backward compatibility if not hasattr(self, 'sp_model_kwargs' ): lowerCamelCase__ : Optional[int] = {} lowerCamelCase__ : Tuple = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.LoadFromSerializedProto(self.sp_model_proto ) def a__ (self, lowerCamelCase_, lowerCamelCase_ = None ): '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] lowerCamelCase__ : Optional[int] = [self.cls_token_id] lowerCamelCase__ : int = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def a__ (self, lowerCamelCase_, lowerCamelCase_ = None, lowerCamelCase_ = False ): '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=lowerCamelCase_, token_ids_a=lowerCamelCase_, already_has_special_tokens=lowerCamelCase_ ) if token_ids_a is None: return [1] + ([0] * len(lowerCamelCase_ )) + [1] return [1] + ([0] * len(lowerCamelCase_ )) + [1, 1] + ([0] * len(lowerCamelCase_ )) + [1] def a__ (self, lowerCamelCase_, lowerCamelCase_ = None ): '''simple docstring''' lowerCamelCase__ : Optional[int] = [self.sep_token_id] lowerCamelCase__ : Union[str, Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def a__ (self ): '''simple docstring''' return len(self.fairseq_ids_to_tokens ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : int = {self.convert_ids_to_tokens(lowerCamelCase_ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def a__ (self, lowerCamelCase_ ): '''simple docstring''' return self.sp_model.encode(lowerCamelCase_, out_type=lowerCamelCase_ ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] else: return self.unk_token_id def a__ (self, lowerCamelCase_ ): '''simple docstring''' return self.fairseq_ids_to_tokens[index] def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : str = ''.join(lowerCamelCase_ ).replace(lowerCamelCase_, ' ' ).strip() return out_string def a__ (self, lowerCamelCase_, lowerCamelCase_ = None ): '''simple docstring''' if not os.path.isdir(lowerCamelCase_ ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return lowerCamelCase__ : List[str] = os.path.join( lowerCamelCase_, (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) lowerCamelCase__ : str = os.path.join( lowerCamelCase_, (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['monolingual_vocab_file'], ) if os.path.abspath(self.vocab_file ) != os.path.abspath(lowerCamelCase_ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file, lowerCamelCase_ ) elif not os.path.isfile(self.vocab_file ): with open(lowerCamelCase_, 'wb' ) as fi: lowerCamelCase__ : List[Any] = self.sp_model.serialized_model_proto() fi.write(lowerCamelCase_ ) if os.path.abspath(self.monolingual_vocab_file ) != os.path.abspath( lowerCamelCase_ ) and os.path.isfile(self.monolingual_vocab_file ): copyfile(self.monolingual_vocab_file, lowerCamelCase_ ) elif not os.path.isfile(self.monolingual_vocab_file ): with open(lowerCamelCase_, 'w', encoding='utf-8' ) as fp: for token in self.fairseq_tokens_to_ids: if token not in self.all_special_tokens: fp.write(f'''{str(lowerCamelCase_ )} \n''' ) return out_vocab_file, out_monolingual_vocab_file
316
"""simple docstring""" import json import os import shutil import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoConfig, BertConfig, GPTaConfig from transformers.configuration_utils import PretrainedConfig from transformers.testing_utils import TOKEN, USER, is_staging_test sys.path.append(str(Path(__file__).parent.parent / "utils")) from test_module.custom_configuration import CustomConfig # noqa E402 A_ : Any = { "return_dict": False, "output_hidden_states": True, "output_attentions": True, "torchscript": True, "torch_dtype": "float16", "use_bfloat16": True, "tf_legacy_loss": True, "pruned_heads": {"a": 1}, "tie_word_embeddings": False, "is_decoder": True, "cross_attention_hidden_size": 1_28, "add_cross_attention": True, "tie_encoder_decoder": True, "max_length": 50, "min_length": 3, "do_sample": True, "early_stopping": True, "num_beams": 3, "num_beam_groups": 3, "diversity_penalty": 0.5, "temperature": 2.0, "top_k": 10, "top_p": 0.7, "typical_p": 0.2, "repetition_penalty": 0.8, "length_penalty": 0.8, "no_repeat_ngram_size": 5, "encoder_no_repeat_ngram_size": 5, "bad_words_ids": [1, 2, 3], "num_return_sequences": 3, "chunk_size_feed_forward": 5, "output_scores": True, "return_dict_in_generate": True, "forced_bos_token_id": 2, "forced_eos_token_id": 3, "remove_invalid_values": True, "architectures": ["BertModel"], "finetuning_task": "translation", "id2label": {0: "label"}, "label2id": {"label": "0"}, "tokenizer_class": "BertTokenizerFast", "prefix": "prefix", "bos_token_id": 6, "pad_token_id": 7, "eos_token_id": 8, "sep_token_id": 9, "decoder_start_token_id": 10, "exponential_decay_length_penalty": (5, 1.01), "suppress_tokens": [0, 1], "begin_suppress_tokens": 2, "task_specific_params": {"translation": "some_params"}, "problem_type": "regression", } @is_staging_test class a_ ( unittest.TestCase ): '''simple docstring''' @classmethod def a__ (cls ): '''simple docstring''' lowerCamelCase__ : Tuple = TOKEN HfFolder.save_token(lowerCamelCase_ ) @classmethod def a__ (cls ): '''simple docstring''' try: delete_repo(token=cls._token, repo_id='test-config' ) except HTTPError: pass try: delete_repo(token=cls._token, repo_id='valid_org/test-config-org' ) except HTTPError: pass try: delete_repo(token=cls._token, repo_id='test-dynamic-config' ) except HTTPError: pass def a__ (self ): '''simple docstring''' lowerCamelCase__ : Optional[int] = BertConfig( vocab_size=9_9, hidden_size=3_2, num_hidden_layers=5, num_attention_heads=4, intermediate_size=3_7 ) config.push_to_hub('test-config', use_auth_token=self._token ) lowerCamelCase__ : List[str] = BertConfig.from_pretrained(f'''{USER}/test-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(lowerCamelCase_, getattr(lowerCamelCase_, lowerCamelCase_ ) ) # Reset repo delete_repo(token=self._token, repo_id='test-config' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(lowerCamelCase_, repo_id='test-config', push_to_hub=lowerCamelCase_, use_auth_token=self._token ) lowerCamelCase__ : List[Any] = BertConfig.from_pretrained(f'''{USER}/test-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(lowerCamelCase_, getattr(lowerCamelCase_, lowerCamelCase_ ) ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : List[Any] = BertConfig( vocab_size=9_9, hidden_size=3_2, num_hidden_layers=5, num_attention_heads=4, intermediate_size=3_7 ) config.push_to_hub('valid_org/test-config-org', use_auth_token=self._token ) lowerCamelCase__ : int = BertConfig.from_pretrained('valid_org/test-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(lowerCamelCase_, getattr(lowerCamelCase_, lowerCamelCase_ ) ) # Reset repo delete_repo(token=self._token, repo_id='valid_org/test-config-org' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( lowerCamelCase_, repo_id='valid_org/test-config-org', push_to_hub=lowerCamelCase_, use_auth_token=self._token ) lowerCamelCase__ : Tuple = BertConfig.from_pretrained('valid_org/test-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(lowerCamelCase_, getattr(lowerCamelCase_, lowerCamelCase_ ) ) def a__ (self ): '''simple docstring''' CustomConfig.register_for_auto_class() lowerCamelCase__ : str = CustomConfig(attribute=4_2 ) config.push_to_hub('test-dynamic-config', use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual(config.auto_map, {'AutoConfig': 'custom_configuration.CustomConfig'} ) lowerCamelCase__ : List[str] = AutoConfig.from_pretrained(f'''{USER}/test-dynamic-config''', trust_remote_code=lowerCamelCase_ ) # Can't make an isinstance check because the new_config is from the FakeConfig class of a dynamic module self.assertEqual(new_config.__class__.__name__, 'CustomConfig' ) self.assertEqual(new_config.attribute, 4_2 ) class a_ ( unittest.TestCase ): '''simple docstring''' def a__ (self ): '''simple docstring''' lowerCamelCase__ : Tuple = GPTaConfig() # attempt to modify each of int/float/bool/str config records and verify they were updated lowerCamelCase__ : Union[str, Any] = c.n_embd + 1 # int lowerCamelCase__ : Optional[Any] = c.resid_pdrop + 1.0 # float lowerCamelCase__ : str = not c.scale_attn_weights # bool lowerCamelCase__ : Any = c.summary_type + 'foo' # str c.update_from_string( f'''n_embd={n_embd},resid_pdrop={resid_pdrop},scale_attn_weights={scale_attn_weights},summary_type={summary_type}''' ) self.assertEqual(lowerCamelCase_, c.n_embd, 'mismatch for key: n_embd' ) self.assertEqual(lowerCamelCase_, c.resid_pdrop, 'mismatch for key: resid_pdrop' ) self.assertEqual(lowerCamelCase_, c.scale_attn_weights, 'mismatch for key: scale_attn_weights' ) self.assertEqual(lowerCamelCase_, c.summary_type, 'mismatch for key: summary_type' ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Any = PretrainedConfig() lowerCamelCase__ : Union[str, Any] = [key for key in base_config.__dict__ if key not in config_common_kwargs] # If this part of the test fails, you have arguments to addin config_common_kwargs above. self.assertListEqual( lowerCamelCase_, ['is_encoder_decoder', '_name_or_path', '_commit_hash', 'transformers_version'] ) lowerCamelCase__ : str = [key for key, value in config_common_kwargs.items() if value == getattr(lowerCamelCase_, lowerCamelCase_ )] if len(lowerCamelCase_ ) > 0: raise ValueError( 'The following keys are set with the default values in' ' `test_configuration_common.config_common_kwargs` pick another value for them:' f''' {', '.join(lowerCamelCase_ )}.''' ) def a__ (self ): '''simple docstring''' with self.assertRaises(lowerCamelCase_ ): # config is in subfolder, the following should not work without specifying the subfolder lowerCamelCase__ : Union[str, Any] = BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert-subfolder' ) lowerCamelCase__ : Any = BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert-subfolder', subfolder='bert' ) self.assertIsNotNone(lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : int = mock.Mock() lowerCamelCase__ : str = 5_0_0 lowerCamelCase__ : Union[str, Any] = {} lowerCamelCase__ : Any = HTTPError lowerCamelCase__ : str = {} # Download this model to make sure it's in the cache. lowerCamelCase__ : Dict = BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert' ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch('requests.Session.request', return_value=lowerCamelCase_ ) as mock_head: lowerCamelCase__ : Union[str, Any] = BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert' ) # This check we did call the fake head request mock_head.assert_called() def a__ (self ): '''simple docstring''' lowerCamelCase__ : int = BertConfig.from_pretrained( 'https://huggingface.co/hf-internal-testing/tiny-random-bert/resolve/main/config.json' ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : str = AutoConfig.from_pretrained('bert-base-cased' ) lowerCamelCase__ : Optional[Any] = ['config.4.0.0.json'] with tempfile.TemporaryDirectory() as tmp_dir: configuration.save_pretrained(lowerCamelCase_ ) lowerCamelCase__ : Tuple = 2 json.dump(configuration.to_dict(), open(os.path.join(lowerCamelCase_, 'config.4.0.0.json' ), 'w' ) ) # This should pick the new configuration file as the version of Transformers is > 4.0.0 lowerCamelCase__ : List[str] = AutoConfig.from_pretrained(lowerCamelCase_ ) self.assertEqual(new_configuration.hidden_size, 2 ) # Will need to be adjusted if we reach v42 and this test is still here. # Should pick the old configuration file as the version of Transformers is < 4.42.0 lowerCamelCase__ : Optional[Any] = ['config.42.0.0.json'] lowerCamelCase__ : List[Any] = 7_6_8 configuration.save_pretrained(lowerCamelCase_ ) shutil.move(os.path.join(lowerCamelCase_, 'config.4.0.0.json' ), os.path.join(lowerCamelCase_, 'config.42.0.0.json' ) ) lowerCamelCase__ : str = AutoConfig.from_pretrained(lowerCamelCase_ ) self.assertEqual(new_configuration.hidden_size, 7_6_8 ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : int = 'hf-internal-testing/test-two-configs' import transformers as new_transformers lowerCamelCase__ : Dict = 'v4.0.0' lowerCamelCase__ , lowerCamelCase__ : str = new_transformers.models.auto.AutoConfig.from_pretrained( lowerCamelCase_, return_unused_kwargs=lowerCamelCase_ ) self.assertEqual(new_configuration.hidden_size, 2 ) # This checks `_configuration_file` ia not kept in the kwargs by mistake. self.assertDictEqual(lowerCamelCase_, {} ) # Testing an older version by monkey-patching the version in the module it's used. import transformers as old_transformers lowerCamelCase__ : Optional[Any] = 'v3.0.0' lowerCamelCase__ : Optional[int] = old_transformers.models.auto.AutoConfig.from_pretrained(lowerCamelCase_ ) self.assertEqual(old_configuration.hidden_size, 7_6_8 )
316
1
"""simple docstring""" import argparse import json from tqdm import tqdm def lowerCamelCase_ ( ): lowerCamelCase__ : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument( '--src_path' , type=_lowerCamelCase , default='biencoder-nq-dev.json' , help='Path to raw DPR training data' , ) parser.add_argument( '--evaluation_set' , type=_lowerCamelCase , help='where to store parsed evaluation_set file' , ) parser.add_argument( '--gold_data_path' , type=_lowerCamelCase , help='where to store parsed gold_data_path file' , ) lowerCamelCase__ : int = parser.parse_args() with open(args.src_path , 'r' ) as src_file, open(args.evaluation_set , 'w' ) as eval_file, open( args.gold_data_path , 'w' ) as gold_file: lowerCamelCase__ : List[Any] = json.load(_lowerCamelCase ) for dpr_record in tqdm(_lowerCamelCase ): lowerCamelCase__ : Union[str, Any] = dpr_record['question'] lowerCamelCase__ : int = [context['title'] for context in dpr_record['positive_ctxs']] eval_file.write(question + '\n' ) gold_file.write('\t'.join(_lowerCamelCase ) + '\n' ) if __name__ == "__main__": main()
316
"""simple docstring""" from __future__ import annotations def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : list[list[int]] = [] lowerCamelCase__ : list[int] = [] lowerCamelCase__ : List[str] = 0 lowerCamelCase__ : List[Any] = sum(_lowerCamelCase ) create_state_space_tree(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) return result def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , ): if sum(_lowerCamelCase ) > max_sum or (remaining_nums_sum + sum(_lowerCamelCase )) < max_sum: return if sum(_lowerCamelCase ) == max_sum: result.append(_lowerCamelCase ) return for index in range(_lowerCamelCase , len(_lowerCamelCase ) ): create_state_space_tree( _lowerCamelCase , _lowerCamelCase , index + 1 , [*path, nums[index]] , _lowerCamelCase , remaining_nums_sum - nums[index] , ) A_ : Optional[Any] = [3, 34, 4, 12, 5, 2] A_ : List[str] = 9 A_ : List[Any] = generate_sum_of_subsets_soln(nums, max_sum) print(*result)
316
1
"""simple docstring""" import gc import unittest import numpy as np import torch from torch.backends.cuda import sdp_kernel from diffusers import ( CMStochasticIterativeScheduler, ConsistencyModelPipeline, UNetaDModel, ) from diffusers.utils import randn_tensor, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_a, require_torch_gpu from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class a_ ( snake_case_ , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ : str = ConsistencyModelPipeline lowerCamelCase__ : Optional[int] = UNCONDITIONAL_IMAGE_GENERATION_PARAMS lowerCamelCase__ : str = UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS # Override required_optional_params to remove num_images_per_prompt lowerCamelCase__ : Tuple = frozenset( [ 'num_inference_steps', 'generator', 'latents', 'output_type', 'return_dict', 'callback', 'callback_steps', ] ) @property def a__ (self ): '''simple docstring''' lowerCamelCase__ : List[Any] = UNetaDModel.from_pretrained( 'diffusers/consistency-models-test', subfolder='test_unet', ) return unet @property def a__ (self ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = UNetaDModel.from_pretrained( 'diffusers/consistency-models-test', subfolder='test_unet_class_cond', ) return unet def a__ (self, lowerCamelCase_=False ): '''simple docstring''' if class_cond: lowerCamelCase__ : Any = self.dummy_cond_unet else: lowerCamelCase__ : Optional[Any] = self.dummy_uncond_unet # Default to CM multistep sampler lowerCamelCase__ : Dict = CMStochasticIterativeScheduler( num_train_timesteps=4_0, sigma_min=0.002, sigma_max=80.0, ) lowerCamelCase__ : Optional[int] = { 'unet': unet, 'scheduler': scheduler, } return components def a__ (self, lowerCamelCase_, lowerCamelCase_=0 ): '''simple docstring''' if str(lowerCamelCase_ ).startswith('mps' ): lowerCamelCase__ : int = torch.manual_seed(lowerCamelCase_ ) else: lowerCamelCase__ : str = torch.Generator(device=lowerCamelCase_ ).manual_seed(lowerCamelCase_ ) lowerCamelCase__ : int = { 'batch_size': 1, 'num_inference_steps': None, 'timesteps': [2_2, 0], 'generator': generator, 'output_type': 'np', } return inputs def a__ (self ): '''simple docstring''' lowerCamelCase__ : Optional[int] = 'cpu' # ensure determinism for the device-dependent torch.Generator lowerCamelCase__ : Dict = self.get_dummy_components() lowerCamelCase__ : List[str] = ConsistencyModelPipeline(**lowerCamelCase_ ) lowerCamelCase__ : List[Any] = pipe.to(lowerCamelCase_ ) pipe.set_progress_bar_config(disable=lowerCamelCase_ ) lowerCamelCase__ : Tuple = self.get_dummy_inputs(lowerCamelCase_ ) lowerCamelCase__ : str = pipe(**lowerCamelCase_ ).images assert image.shape == (1, 3_2, 3_2, 3) lowerCamelCase__ : str = image[0, -3:, -3:, -1] lowerCamelCase__ : Dict = np.array([0.3_572, 0.6_273, 0.4_031, 0.3_961, 0.4_321, 0.5_730, 0.5_266, 0.4_780, 0.5_004] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def a__ (self ): '''simple docstring''' lowerCamelCase__ : Any = 'cpu' # ensure determinism for the device-dependent torch.Generator lowerCamelCase__ : Any = self.get_dummy_components(class_cond=lowerCamelCase_ ) lowerCamelCase__ : Dict = ConsistencyModelPipeline(**lowerCamelCase_ ) lowerCamelCase__ : Union[str, Any] = pipe.to(lowerCamelCase_ ) pipe.set_progress_bar_config(disable=lowerCamelCase_ ) lowerCamelCase__ : List[str] = self.get_dummy_inputs(lowerCamelCase_ ) lowerCamelCase__ : List[Any] = 0 lowerCamelCase__ : Dict = pipe(**lowerCamelCase_ ).images assert image.shape == (1, 3_2, 3_2, 3) lowerCamelCase__ : str = image[0, -3:, -3:, -1] lowerCamelCase__ : List[Any] = np.array([0.3_572, 0.6_273, 0.4_031, 0.3_961, 0.4_321, 0.5_730, 0.5_266, 0.4_780, 0.5_004] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def a__ (self ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = 'cpu' # ensure determinism for the device-dependent torch.Generator lowerCamelCase__ : Union[str, Any] = self.get_dummy_components() lowerCamelCase__ : int = ConsistencyModelPipeline(**lowerCamelCase_ ) lowerCamelCase__ : List[Any] = pipe.to(lowerCamelCase_ ) pipe.set_progress_bar_config(disable=lowerCamelCase_ ) lowerCamelCase__ : Optional[int] = self.get_dummy_inputs(lowerCamelCase_ ) lowerCamelCase__ : Tuple = 1 lowerCamelCase__ : Union[str, Any] = None lowerCamelCase__ : Optional[Any] = pipe(**lowerCamelCase_ ).images assert image.shape == (1, 3_2, 3_2, 3) lowerCamelCase__ : Optional[int] = image[0, -3:, -3:, -1] lowerCamelCase__ : int = np.array([0.5_004, 0.5_004, 0.4_994, 0.5_008, 0.4_976, 0.5_018, 0.4_990, 0.4_982, 0.4_987] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def a__ (self ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = 'cpu' # ensure determinism for the device-dependent torch.Generator lowerCamelCase__ : Optional[int] = self.get_dummy_components(class_cond=lowerCamelCase_ ) lowerCamelCase__ : Tuple = ConsistencyModelPipeline(**lowerCamelCase_ ) lowerCamelCase__ : str = pipe.to(lowerCamelCase_ ) pipe.set_progress_bar_config(disable=lowerCamelCase_ ) lowerCamelCase__ : Dict = self.get_dummy_inputs(lowerCamelCase_ ) lowerCamelCase__ : Optional[Any] = 1 lowerCamelCase__ : Optional[Any] = None lowerCamelCase__ : int = 0 lowerCamelCase__ : List[str] = pipe(**lowerCamelCase_ ).images assert image.shape == (1, 3_2, 3_2, 3) lowerCamelCase__ : Optional[Any] = image[0, -3:, -3:, -1] lowerCamelCase__ : Any = np.array([0.5_004, 0.5_004, 0.4_994, 0.5_008, 0.4_976, 0.5_018, 0.4_990, 0.4_982, 0.4_987] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 @slow @require_torch_gpu class a_ ( unittest.TestCase ): '''simple docstring''' def a__ (self ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def a__ (self, lowerCamelCase_=0, lowerCamelCase_=False, lowerCamelCase_="cpu", lowerCamelCase_=torch.floataa, lowerCamelCase_=(1, 3, 6_4, 6_4) ): '''simple docstring''' lowerCamelCase__ : int = torch.manual_seed(lowerCamelCase_ ) lowerCamelCase__ : Tuple = { 'num_inference_steps': None, 'timesteps': [2_2, 0], 'class_labels': 0, 'generator': generator, 'output_type': 'np', } if get_fixed_latents: lowerCamelCase__ : Tuple = self.get_fixed_latents(seed=lowerCamelCase_, device=lowerCamelCase_, dtype=lowerCamelCase_, shape=lowerCamelCase_ ) lowerCamelCase__ : List[str] = latents return inputs def a__ (self, lowerCamelCase_=0, lowerCamelCase_="cpu", lowerCamelCase_=torch.floataa, lowerCamelCase_=(1, 3, 6_4, 6_4) ): '''simple docstring''' if type(lowerCamelCase_ ) == str: lowerCamelCase__ : int = torch.device(lowerCamelCase_ ) lowerCamelCase__ : List[Any] = torch.Generator(device=lowerCamelCase_ ).manual_seed(lowerCamelCase_ ) lowerCamelCase__ : List[Any] = randn_tensor(lowerCamelCase_, generator=lowerCamelCase_, device=lowerCamelCase_, dtype=lowerCamelCase_ ) return latents def a__ (self ): '''simple docstring''' lowerCamelCase__ : List[Any] = UNetaDModel.from_pretrained('diffusers/consistency_models', subfolder='diffusers_cd_imagenet64_l2' ) lowerCamelCase__ : Tuple = CMStochasticIterativeScheduler( num_train_timesteps=4_0, sigma_min=0.002, sigma_max=80.0, ) lowerCamelCase__ : int = ConsistencyModelPipeline(unet=lowerCamelCase_, scheduler=lowerCamelCase_ ) pipe.to(torch_device=lowerCamelCase_ ) pipe.set_progress_bar_config(disable=lowerCamelCase_ ) lowerCamelCase__ : Union[str, Any] = self.get_inputs() lowerCamelCase__ : str = pipe(**lowerCamelCase_ ).images assert image.shape == (1, 6_4, 6_4, 3) lowerCamelCase__ : str = image[0, -3:, -3:, -1] lowerCamelCase__ : Tuple = np.array([0.0_888, 0.0_881, 0.0_666, 0.0_479, 0.0_292, 0.0_195, 0.0_201, 0.0_163, 0.0_254] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 2e-2 def a__ (self ): '''simple docstring''' lowerCamelCase__ : List[Any] = UNetaDModel.from_pretrained('diffusers/consistency_models', subfolder='diffusers_cd_imagenet64_l2' ) lowerCamelCase__ : int = CMStochasticIterativeScheduler( num_train_timesteps=4_0, sigma_min=0.002, sigma_max=80.0, ) lowerCamelCase__ : List[Any] = ConsistencyModelPipeline(unet=lowerCamelCase_, scheduler=lowerCamelCase_ ) pipe.to(torch_device=lowerCamelCase_ ) pipe.set_progress_bar_config(disable=lowerCamelCase_ ) lowerCamelCase__ : Any = self.get_inputs() lowerCamelCase__ : Union[str, Any] = 1 lowerCamelCase__ : Any = None lowerCamelCase__ : Any = pipe(**lowerCamelCase_ ).images assert image.shape == (1, 6_4, 6_4, 3) lowerCamelCase__ : Any = image[0, -3:, -3:, -1] lowerCamelCase__ : Dict = np.array([0.0_340, 0.0_152, 0.0_063, 0.0_267, 0.0_221, 0.0_107, 0.0_416, 0.0_186, 0.0_217] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 2e-2 @require_torch_a def a__ (self ): '''simple docstring''' lowerCamelCase__ : Tuple = UNetaDModel.from_pretrained('diffusers/consistency_models', subfolder='diffusers_cd_imagenet64_l2' ) lowerCamelCase__ : Tuple = CMStochasticIterativeScheduler( num_train_timesteps=4_0, sigma_min=0.002, sigma_max=80.0, ) lowerCamelCase__ : Dict = ConsistencyModelPipeline(unet=lowerCamelCase_, scheduler=lowerCamelCase_ ) pipe.to(torch_device=lowerCamelCase_, torch_dtype=torch.floataa ) pipe.set_progress_bar_config(disable=lowerCamelCase_ ) lowerCamelCase__ : Dict = self.get_inputs(get_fixed_latents=lowerCamelCase_, device=lowerCamelCase_ ) # Ensure usage of flash attention in torch 2.0 with sdp_kernel(enable_flash=lowerCamelCase_, enable_math=lowerCamelCase_, enable_mem_efficient=lowerCamelCase_ ): lowerCamelCase__ : List[Any] = pipe(**lowerCamelCase_ ).images assert image.shape == (1, 6_4, 6_4, 3) lowerCamelCase__ : int = image[0, -3:, -3:, -1] lowerCamelCase__ : str = np.array([0.1_875, 0.1_428, 0.1_289, 0.2_151, 0.2_092, 0.1_477, 0.1_877, 0.1_641, 0.1_353] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 @require_torch_a def a__ (self ): '''simple docstring''' lowerCamelCase__ : Any = UNetaDModel.from_pretrained('diffusers/consistency_models', subfolder='diffusers_cd_imagenet64_l2' ) lowerCamelCase__ : Optional[Any] = CMStochasticIterativeScheduler( num_train_timesteps=4_0, sigma_min=0.002, sigma_max=80.0, ) lowerCamelCase__ : int = ConsistencyModelPipeline(unet=lowerCamelCase_, scheduler=lowerCamelCase_ ) pipe.to(torch_device=lowerCamelCase_, torch_dtype=torch.floataa ) pipe.set_progress_bar_config(disable=lowerCamelCase_ ) lowerCamelCase__ : Any = self.get_inputs(get_fixed_latents=lowerCamelCase_, device=lowerCamelCase_ ) lowerCamelCase__ : Tuple = 1 lowerCamelCase__ : str = None # Ensure usage of flash attention in torch 2.0 with sdp_kernel(enable_flash=lowerCamelCase_, enable_math=lowerCamelCase_, enable_mem_efficient=lowerCamelCase_ ): lowerCamelCase__ : List[str] = pipe(**lowerCamelCase_ ).images assert image.shape == (1, 6_4, 6_4, 3) lowerCamelCase__ : Optional[Any] = image[0, -3:, -3:, -1] lowerCamelCase__ : List[str] = np.array([0.1_663, 0.1_948, 0.2_275, 0.1_680, 0.1_204, 0.1_245, 0.1_858, 0.1_338, 0.2_095] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3
316
"""simple docstring""" from __future__ import annotations import queue class a_ : '''simple docstring''' def __init__(self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = data lowerCamelCase__ : Optional[int] = None lowerCamelCase__ : List[Any] = None def lowerCamelCase_ ( ): print('\n********Press N to stop entering at any point of time********\n' ) lowerCamelCase__ : str = input('Enter the value of the root node: ' ).strip().lower() lowerCamelCase__ : queue.Queue = queue.Queue() lowerCamelCase__ : Optional[Any] = TreeNode(int(_lowerCamelCase ) ) q.put(_lowerCamelCase ) while not q.empty(): lowerCamelCase__ : List[Any] = q.get() lowerCamelCase__ : str = f'''Enter the left node of {node_found.data}: ''' lowerCamelCase__ : Dict = input(_lowerCamelCase ).strip().lower() or 'n' if check == "n": return tree_node lowerCamelCase__ : str = TreeNode(int(_lowerCamelCase ) ) lowerCamelCase__ : Dict = left_node q.put(_lowerCamelCase ) lowerCamelCase__ : List[str] = f'''Enter the right node of {node_found.data}: ''' lowerCamelCase__ : List[str] = input(_lowerCamelCase ).strip().lower() or 'n' if check == "n": return tree_node lowerCamelCase__ : Optional[int] = TreeNode(int(_lowerCamelCase ) ) lowerCamelCase__ : Any = right_node q.put(_lowerCamelCase ) raise def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return print(node.data , end=',' ) pre_order(node.left ) pre_order(node.right ) def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return in_order(node.left ) print(node.data , end=',' ) in_order(node.right ) def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return post_order(node.left ) post_order(node.right ) print(node.data , end=',' ) def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return lowerCamelCase__ : queue.Queue = queue.Queue() q.put(_lowerCamelCase ) while not q.empty(): lowerCamelCase__ : Any = q.get() print(node_dequeued.data , end=',' ) if node_dequeued.left: q.put(node_dequeued.left ) if node_dequeued.right: q.put(node_dequeued.right ) def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return lowerCamelCase__ : queue.Queue = queue.Queue() q.put(_lowerCamelCase ) while not q.empty(): lowerCamelCase__ : List[Any] = [] while not q.empty(): lowerCamelCase__ : str = q.get() print(node_dequeued.data , end=',' ) if node_dequeued.left: list_.append(node_dequeued.left ) if node_dequeued.right: list_.append(node_dequeued.right ) print() for node in list_: q.put(_lowerCamelCase ) def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return lowerCamelCase__ : list[TreeNode] = [] lowerCamelCase__ : int = node while n or stack: while n: # start from root node, find its left child print(n.data , end=',' ) stack.append(_lowerCamelCase ) lowerCamelCase__ : Union[str, Any] = n.left # end of while means current node doesn't have left child lowerCamelCase__ : List[Any] = stack.pop() # start to traverse its right child lowerCamelCase__ : Optional[Any] = n.right def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return lowerCamelCase__ : list[TreeNode] = [] lowerCamelCase__ : int = node while n or stack: while n: stack.append(_lowerCamelCase ) lowerCamelCase__ : List[str] = n.left lowerCamelCase__ : Tuple = stack.pop() print(n.data , end=',' ) lowerCamelCase__ : Union[str, Any] = n.right def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return lowerCamelCase__ , lowerCamelCase__ : Any = [], [] lowerCamelCase__ : int = node stacka.append(_lowerCamelCase ) while stacka: # to find the reversed order of post order, store it in stack2 lowerCamelCase__ : List[str] = stacka.pop() if n.left: stacka.append(n.left ) if n.right: stacka.append(n.right ) stacka.append(_lowerCamelCase ) while stacka: # pop up from stack2 will be the post order print(stacka.pop().data , end=',' ) def lowerCamelCase_ ( _lowerCamelCase = "" , _lowerCamelCase=50 , _lowerCamelCase="*" ): if not s: return "\n" + width * char lowerCamelCase__ , lowerCamelCase__ : Dict = divmod(width - len(_lowerCamelCase ) - 2 , 2 ) return f'''{left * char} {s} {(left + extra) * char}''' if __name__ == "__main__": import doctest doctest.testmod() print(prompt("Binary Tree Traversals")) A_ : TreeNode = build_tree() print(prompt("Pre Order Traversal")) pre_order(node) print(prompt() + "\n") print(prompt("In Order Traversal")) in_order(node) print(prompt() + "\n") print(prompt("Post Order Traversal")) post_order(node) print(prompt() + "\n") print(prompt("Level Order Traversal")) level_order(node) print(prompt() + "\n") print(prompt("Actual Level Order Traversal")) level_order_actual(node) print("*" * 50 + "\n") print(prompt("Pre Order Traversal - Iteration Version")) pre_order_iter(node) print(prompt() + "\n") print(prompt("In Order Traversal - Iteration Version")) in_order_iter(node) print(prompt() + "\n") print(prompt("Post Order Traversal - Iteration Version")) post_order_iter(node) print(prompt())
316
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging A_ : Any = logging.get_logger(__name__) A_ : List[Any] = { # See all MEGATRON_BERT models at https://huggingface.co/models?filter=bert } class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : Optional[int] = 'megatron-bert' def __init__(self, lowerCamelCase_=2_9_0_5_6, lowerCamelCase_=1_0_2_4, lowerCamelCase_=2_4, lowerCamelCase_=1_6, lowerCamelCase_=4_0_9_6, lowerCamelCase_="gelu", lowerCamelCase_=0.1, lowerCamelCase_=0.1, lowerCamelCase_=5_1_2, lowerCamelCase_=2, lowerCamelCase_=0.02, lowerCamelCase_=1e-12, lowerCamelCase_=0, lowerCamelCase_="absolute", lowerCamelCase_=True, **lowerCamelCase_, ): '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase_, **lowerCamelCase_ ) lowerCamelCase__ : Optional[Any] = vocab_size lowerCamelCase__ : Optional[int] = hidden_size lowerCamelCase__ : Union[str, Any] = num_hidden_layers lowerCamelCase__ : Union[str, Any] = num_attention_heads lowerCamelCase__ : Any = hidden_act lowerCamelCase__ : int = intermediate_size lowerCamelCase__ : Any = hidden_dropout_prob lowerCamelCase__ : Dict = attention_probs_dropout_prob lowerCamelCase__ : Union[str, Any] = max_position_embeddings lowerCamelCase__ : Tuple = type_vocab_size lowerCamelCase__ : List[str] = initializer_range lowerCamelCase__ : List[str] = layer_norm_eps lowerCamelCase__ : Union[str, Any] = position_embedding_type lowerCamelCase__ : List[Any] = use_cache
316
"""simple docstring""" # Note: if you intend to run this script make sure you look under scripts/fsmt/ # to locate the appropriate script to do the work correctly. There is a set of scripts to: # - download and prepare data and run the conversion script # - perform eval to get the best hparam into the config # - generate model_cards - useful if you have multiple models from the same paper import argparse import json import os import re from collections import OrderedDict from os.path import basename, dirname import fairseq import torch from fairseq import hub_utils from fairseq.data.dictionary import Dictionary from transformers import FSMTConfig, FSMTForConditionalGeneration from transformers.models.fsmt.tokenization_fsmt import VOCAB_FILES_NAMES from transformers.tokenization_utils_base import TOKENIZER_CONFIG_FILE from transformers.utils import WEIGHTS_NAME, logging logging.set_verbosity_warning() A_ : Optional[Any] = 2 # based on the results of a search on a range of `num_beams`, `length_penalty` and `early_stopping` # values against wmt19 test data to obtain the best BLEU scores, we will use the following defaults: # # * `num_beams`: 5 (higher scores better, but requires more memory/is slower, can be adjusted by users) # * `early_stopping`: `False` consistently scored better # * `length_penalty` varied, so will assign the best one depending on the model A_ : List[Any] = { # fairseq: "wmt19-ru-en": {"length_penalty": 1.1}, "wmt19-en-ru": {"length_penalty": 1.15}, "wmt19-en-de": {"length_penalty": 1.0}, "wmt19-de-en": {"length_penalty": 1.1}, # allenai: "wmt16-en-de-dist-12-1": {"length_penalty": 0.6}, "wmt16-en-de-dist-6-1": {"length_penalty": 0.6}, "wmt16-en-de-12-1": {"length_penalty": 0.8}, "wmt19-de-en-6-6-base": {"length_penalty": 0.6}, "wmt19-de-en-6-6-big": {"length_penalty": 0.6}, } # this remaps the different models to their organization names A_ : str = {} for m in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: A_ : str = "facebook" for m in [ "wmt16-en-de-dist-12-1", "wmt16-en-de-dist-6-1", "wmt16-en-de-12-1", "wmt19-de-en-6-6-base", "wmt19-de-en-6-6-big", ]: A_ : Optional[Any] = "allenai" def lowerCamelCase_ ( _lowerCamelCase ): # (1) remove word breaking symbol, (2) add word ending symbol where the word is not broken up, # e.g.: d = {'le@@': 5, 'tt@@': 6, 'er': 7} => {'le': 5, 'tt': 6, 'er</w>': 7} lowerCamelCase__ : List[Any] = dict((re.sub(r'@@$' , '' , _lowerCamelCase ), v) if k.endswith('@@' ) else (re.sub(r'$' , '</w>' , _lowerCamelCase ), v) for k, v in d.items() ) lowerCamelCase__ : int = '<s> <pad> </s> <unk>'.split() # restore the special tokens for k in keep_keys: del da[f'''{k}</w>'''] lowerCamelCase__ : List[str] = d[k] # restore return da def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): # prep assert os.path.exists(_lowerCamelCase ) os.makedirs(_lowerCamelCase , exist_ok=_lowerCamelCase ) print(f'''Writing results to {pytorch_dump_folder_path}''' ) # handle various types of models lowerCamelCase__ : Optional[int] = basename(_lowerCamelCase ) lowerCamelCase__ : str = dirname(_lowerCamelCase ) lowerCamelCase__ : Any = fairseq.model_parallel.models.transformer.ModelParallelTransformerModel lowerCamelCase__ : int = cls.hub_models() lowerCamelCase__ : str = {'bpe': 'fastbpe', 'tokenizer': 'moses'} lowerCamelCase__ : Optional[Any] = '.' # note: since the model dump is old, fairseq has upgraded its model some # time later, and it does a whole lot of rewrites and splits on the saved # weights, therefore we can't use torch.load() directly on the model file. # see: upgrade_state_dict(state_dict) in fairseq_model.py print(f'''using checkpoint {checkpoint_file}''' ) lowerCamelCase__ : Any = hub_utils.from_pretrained( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , archive_map=_lowerCamelCase , **_lowerCamelCase ) lowerCamelCase__ : List[str] = vars(chkpt['args']['model'] ) lowerCamelCase__ : Optional[Any] = args['source_lang'] lowerCamelCase__ : List[str] = args['target_lang'] lowerCamelCase__ : List[str] = dirname(_lowerCamelCase ) lowerCamelCase__ : Union[str, Any] = basename(_lowerCamelCase ) # dicts lowerCamelCase__ : Optional[Any] = os.path.join(_lowerCamelCase , f'''dict.{src_lang}.txt''' ) lowerCamelCase__ : Optional[Any] = os.path.join(_lowerCamelCase , f'''dict.{tgt_lang}.txt''' ) lowerCamelCase__ : Dict = Dictionary.load(_lowerCamelCase ) lowerCamelCase__ : List[Any] = rewrite_dict_keys(src_dict.indices ) lowerCamelCase__ : int = len(_lowerCamelCase ) lowerCamelCase__ : List[Any] = os.path.join(_lowerCamelCase , 'vocab-src.json' ) print(f'''Generating {src_vocab_file} of {src_vocab_size} of {src_lang} records''' ) with open(_lowerCamelCase , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(_lowerCamelCase , ensure_ascii=_lowerCamelCase , indent=_lowerCamelCase ) ) # detect whether this is a do_lower_case situation, which can be derived by checking whether we # have at least one uppercase letter in the source vocab lowerCamelCase__ : Optional[int] = True for k in src_vocab.keys(): if not k.islower(): lowerCamelCase__ : int = False break lowerCamelCase__ : str = Dictionary.load(_lowerCamelCase ) lowerCamelCase__ : Union[str, Any] = rewrite_dict_keys(tgt_dict.indices ) lowerCamelCase__ : Optional[Any] = len(_lowerCamelCase ) lowerCamelCase__ : Dict = os.path.join(_lowerCamelCase , 'vocab-tgt.json' ) print(f'''Generating {tgt_vocab_file} of {tgt_vocab_size} of {tgt_lang} records''' ) with open(_lowerCamelCase , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(_lowerCamelCase , ensure_ascii=_lowerCamelCase , indent=_lowerCamelCase ) ) # merges_file (bpecodes) lowerCamelCase__ : List[Any] = os.path.join(_lowerCamelCase , VOCAB_FILES_NAMES['merges_file'] ) for fn in ["bpecodes", "code"]: # older fairseq called the merges file "code" lowerCamelCase__ : Optional[int] = os.path.join(_lowerCamelCase , _lowerCamelCase ) if os.path.exists(_lowerCamelCase ): break with open(_lowerCamelCase , encoding='utf-8' ) as fin: lowerCamelCase__ : Union[str, Any] = fin.read() lowerCamelCase__ : Any = re.sub(r' \d+$' , '' , _lowerCamelCase , 0 , re.M ) # remove frequency number print(f'''Generating {merges_file}''' ) with open(_lowerCamelCase , 'w' , encoding='utf-8' ) as fout: fout.write(_lowerCamelCase ) # model config lowerCamelCase__ : Dict = os.path.join(_lowerCamelCase , 'config.json' ) # validate bpe/tokenizer config, as currently it's hardcoded to moses+fastbpe - # may have to modify the tokenizer if a different type is used by a future model assert args["bpe"] == "fastbpe", f'''need to extend tokenizer to support bpe={args['bpe']}''' assert args["tokenizer"] == "moses", f'''need to extend tokenizer to support bpe={args['tokenizer']}''' lowerCamelCase__ : Optional[int] = { 'architectures': ['FSMTForConditionalGeneration'], 'model_type': 'fsmt', 'activation_dropout': args['activation_dropout'], 'activation_function': 'relu', 'attention_dropout': args['attention_dropout'], 'd_model': args['decoder_embed_dim'], 'dropout': args['dropout'], 'init_std': 0.02, 'max_position_embeddings': args['max_source_positions'], 'num_hidden_layers': args['encoder_layers'], 'src_vocab_size': src_vocab_size, 'tgt_vocab_size': tgt_vocab_size, 'langs': [src_lang, tgt_lang], 'encoder_attention_heads': args['encoder_attention_heads'], 'encoder_ffn_dim': args['encoder_ffn_embed_dim'], 'encoder_layerdrop': args['encoder_layerdrop'], 'encoder_layers': args['encoder_layers'], 'decoder_attention_heads': args['decoder_attention_heads'], 'decoder_ffn_dim': args['decoder_ffn_embed_dim'], 'decoder_layerdrop': args['decoder_layerdrop'], 'decoder_layers': args['decoder_layers'], 'bos_token_id': 0, 'pad_token_id': 1, 'eos_token_id': 2, 'is_encoder_decoder': True, 'scale_embedding': not args['no_scale_embedding'], 'tie_word_embeddings': args['share_all_embeddings'], } # good hparam defaults to start with lowerCamelCase__ : str = 5 lowerCamelCase__ : Tuple = False if model_dir in best_score_hparams and "length_penalty" in best_score_hparams[model_dir]: lowerCamelCase__ : List[str] = best_score_hparams[model_dir]['length_penalty'] else: lowerCamelCase__ : List[Any] = 1.0 print(f'''Generating {fsmt_model_config_file}''' ) with open(_lowerCamelCase , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(_lowerCamelCase , ensure_ascii=_lowerCamelCase , indent=_lowerCamelCase ) ) # tokenizer config lowerCamelCase__ : Dict = os.path.join(_lowerCamelCase , _lowerCamelCase ) lowerCamelCase__ : int = { 'langs': [src_lang, tgt_lang], 'model_max_length': 1024, 'do_lower_case': do_lower_case, } print(f'''Generating {fsmt_tokenizer_config_file}''' ) with open(_lowerCamelCase , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(_lowerCamelCase , ensure_ascii=_lowerCamelCase , indent=_lowerCamelCase ) ) # model lowerCamelCase__ : List[str] = chkpt['models'][0] lowerCamelCase__ : Optional[Any] = model.state_dict() # rename keys to start with 'model.' lowerCamelCase__ : str = OrderedDict(('model.' + k, v) for k, v in model_state_dict.items() ) # remove unneeded keys lowerCamelCase__ : int = [ 'model.model', 'model.encoder.version', 'model.decoder.version', 'model.encoder_embed_tokens.weight', 'model.decoder_embed_tokens.weight', 'model.encoder.embed_positions._float_tensor', 'model.decoder.embed_positions._float_tensor', ] for k in ignore_keys: model_state_dict.pop(_lowerCamelCase , _lowerCamelCase ) lowerCamelCase__ : Any = FSMTConfig.from_pretrained(_lowerCamelCase ) lowerCamelCase__ : Union[str, Any] = FSMTForConditionalGeneration(_lowerCamelCase ) # check that it loads ok model_new.load_state_dict(_lowerCamelCase , strict=_lowerCamelCase ) # save lowerCamelCase__ : List[Any] = os.path.join(_lowerCamelCase , _lowerCamelCase ) print(f'''Generating {pytorch_weights_dump_path}''' ) torch.save(_lowerCamelCase , _lowerCamelCase ) print('Conversion is done!' ) print('\nLast step is to upload the files to s3' ) print(f'''cd {data_root}''' ) print(f'''transformers-cli upload {model_dir}''' ) if __name__ == "__main__": A_ : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--fsmt_checkpoint_path", default=None, type=str, required=True, help=( "Path to the official PyTorch checkpoint file which is expected to reside in the dump dir with dicts," " bpecodes, etc." ), ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) A_ : Dict = parser.parse_args() convert_fsmt_checkpoint_to_pytorch(args.fsmt_checkpoint_path, args.pytorch_dump_folder_path)
316
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging A_ : List[Any] = logging.get_logger(__name__) A_ : Union[str, Any] = { "tanreinama/GPTSAN-2.8B-spout_is_uniform": ( "https://huggingface.co/tanreinama/GPTSAN-2.8B-spout_is_uniform/resolve/main/config.json" ), } class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = 'gptsan-japanese' lowerCamelCase__ : Dict = [ 'past_key_values', ] lowerCamelCase__ : List[str] = { 'hidden_size': 'd_model', 'num_attention_heads': 'num_heads', 'num_hidden_layers': 'num_layers', } def __init__(self, lowerCamelCase_=3_6_0_0_0, lowerCamelCase_=1_2_8_0, lowerCamelCase_=1_0_2_4, lowerCamelCase_=8_1_9_2, lowerCamelCase_=4_0_9_6, lowerCamelCase_=1_2_8, lowerCamelCase_=1_0, lowerCamelCase_=0, lowerCamelCase_=1_6, lowerCamelCase_=1_6, lowerCamelCase_=1_2_8, lowerCamelCase_=0.0, lowerCamelCase_=1e-5, lowerCamelCase_=False, lowerCamelCase_=0.0, lowerCamelCase_="float32", lowerCamelCase_=False, lowerCamelCase_=False, lowerCamelCase_=False, lowerCamelCase_=0.002, lowerCamelCase_=False, lowerCamelCase_=True, lowerCamelCase_=3_5_9_9_8, lowerCamelCase_=3_5_9_9_5, lowerCamelCase_=3_5_9_9_9, **lowerCamelCase_, ): '''simple docstring''' lowerCamelCase__ : Tuple = vocab_size lowerCamelCase__ : List[Any] = max_position_embeddings lowerCamelCase__ : int = d_model lowerCamelCase__ : List[Any] = d_ff lowerCamelCase__ : str = d_ext lowerCamelCase__ : Optional[Any] = d_spout lowerCamelCase__ : Tuple = num_switch_layers lowerCamelCase__ : List[Any] = num_ext_layers lowerCamelCase__ : List[str] = num_switch_layers + num_ext_layers lowerCamelCase__ : Union[str, Any] = num_heads lowerCamelCase__ : List[Any] = num_experts lowerCamelCase__ : List[Any] = expert_capacity lowerCamelCase__ : Union[str, Any] = dropout_rate lowerCamelCase__ : Optional[int] = layer_norm_epsilon lowerCamelCase__ : List[Any] = router_bias lowerCamelCase__ : Optional[Any] = router_jitter_noise lowerCamelCase__ : Dict = router_dtype lowerCamelCase__ : List[Any] = router_ignore_padding_tokens lowerCamelCase__ : Optional[Any] = output_hidden_states lowerCamelCase__ : List[str] = output_attentions lowerCamelCase__ : Any = initializer_factor lowerCamelCase__ : List[str] = output_router_logits lowerCamelCase__ : List[str] = use_cache super().__init__( separator_token_id=lowerCamelCase_, pad_token_id=lowerCamelCase_, eos_token_id=lowerCamelCase_, **lowerCamelCase_, )
316
"""simple docstring""" from abc import ABC, abstractmethod from argparse import ArgumentParser class a_ ( snake_case_ ): '''simple docstring''' @staticmethod @abstractmethod def a__ (lowerCamelCase_ ): '''simple docstring''' raise NotImplementedError() @abstractmethod def a__ (self ): '''simple docstring''' raise NotImplementedError()
316
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) A_ : Optional[Any] = { "configuration_swiftformer": [ "SWIFTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "SwiftFormerConfig", "SwiftFormerOnnxConfig", ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Tuple = [ "SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "SwiftFormerForImageClassification", "SwiftFormerModel", "SwiftFormerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_swiftformer import ( SWIFTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, SwiftFormerConfig, SwiftFormerOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swiftformer import ( SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, SwiftFormerForImageClassification, SwiftFormerModel, SwiftFormerPreTrainedModel, ) else: import sys A_ : List[str] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
316
"""simple docstring""" import re def lowerCamelCase_ ( _lowerCamelCase ): if len(re.findall('[ATCG]' , _lowerCamelCase ) ) != len(_lowerCamelCase ): raise ValueError('Invalid Strand' ) return dna.translate(dna.maketrans('ATCG' , 'TAGC' ) ) if __name__ == "__main__": import doctest doctest.testmod()
316
1
"""simple docstring""" from __future__ import annotations import copy import inspect import unittest import numpy as np from transformers import is_tf_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST, TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, TFLayoutLMvaModel, ) if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class a_ : '''simple docstring''' def __init__(self, lowerCamelCase_, lowerCamelCase_=2, lowerCamelCase_=3, lowerCamelCase_=4, lowerCamelCase_=2, lowerCamelCase_=7, lowerCamelCase_=True, lowerCamelCase_=True, lowerCamelCase_=True, lowerCamelCase_=True, lowerCamelCase_=9_9, lowerCamelCase_=3_6, lowerCamelCase_=2, lowerCamelCase_=4, lowerCamelCase_=3_7, lowerCamelCase_="gelu", lowerCamelCase_=0.1, lowerCamelCase_=0.1, lowerCamelCase_=5_1_2, lowerCamelCase_=1_6, lowerCamelCase_=2, lowerCamelCase_=0.02, lowerCamelCase_=6, lowerCamelCase_=6, lowerCamelCase_=3, lowerCamelCase_=4, lowerCamelCase_=None, lowerCamelCase_=1_0_0_0, ): '''simple docstring''' lowerCamelCase__ : Dict = parent lowerCamelCase__ : Optional[int] = batch_size lowerCamelCase__ : Dict = num_channels lowerCamelCase__ : int = image_size lowerCamelCase__ : Dict = patch_size lowerCamelCase__ : Optional[int] = is_training lowerCamelCase__ : Any = use_input_mask lowerCamelCase__ : Dict = use_token_type_ids lowerCamelCase__ : List[str] = use_labels lowerCamelCase__ : List[str] = vocab_size lowerCamelCase__ : List[Any] = hidden_size lowerCamelCase__ : int = num_hidden_layers lowerCamelCase__ : Optional[int] = num_attention_heads lowerCamelCase__ : Union[str, Any] = intermediate_size lowerCamelCase__ : List[Any] = hidden_act lowerCamelCase__ : Tuple = hidden_dropout_prob lowerCamelCase__ : int = attention_probs_dropout_prob lowerCamelCase__ : List[str] = max_position_embeddings lowerCamelCase__ : Union[str, Any] = type_vocab_size lowerCamelCase__ : Optional[int] = type_sequence_label_size lowerCamelCase__ : str = initializer_range lowerCamelCase__ : str = coordinate_size lowerCamelCase__ : Optional[Any] = shape_size lowerCamelCase__ : Union[str, Any] = num_labels lowerCamelCase__ : Optional[Any] = num_choices lowerCamelCase__ : Dict = scope lowerCamelCase__ : List[str] = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) lowerCamelCase__ : List[Any] = text_seq_length lowerCamelCase__ : Optional[Any] = (image_size // patch_size) ** 2 + 1 lowerCamelCase__ : Optional[int] = self.text_seq_length + self.image_seq_length def a__ (self ): '''simple docstring''' lowerCamelCase__ : Dict = ids_tensor([self.batch_size, self.text_seq_length], self.vocab_size ) lowerCamelCase__ : Union[str, Any] = ids_tensor([self.batch_size, self.text_seq_length, 4], self.range_bbox ) lowerCamelCase__ : int = bbox.numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: lowerCamelCase__ : List[str] = bbox[i, j, 3] lowerCamelCase__ : List[Any] = bbox[i, j, 1] lowerCamelCase__ : int = tmp_coordinate if bbox[i, j, 2] < bbox[i, j, 0]: lowerCamelCase__ : Tuple = bbox[i, j, 2] lowerCamelCase__ : Optional[int] = bbox[i, j, 0] lowerCamelCase__ : Tuple = tmp_coordinate lowerCamelCase__ : Tuple = tf.constant(lowerCamelCase_ ) lowerCamelCase__ : Tuple = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCamelCase__ : int = None if self.use_input_mask: lowerCamelCase__ : List[Any] = random_attention_mask([self.batch_size, self.text_seq_length] ) lowerCamelCase__ : Dict = None if self.use_token_type_ids: lowerCamelCase__ : Tuple = ids_tensor([self.batch_size, self.text_seq_length], self.type_vocab_size ) lowerCamelCase__ : int = None lowerCamelCase__ : Optional[int] = None if self.use_labels: lowerCamelCase__ : Optional[int] = ids_tensor([self.batch_size], self.type_sequence_label_size ) lowerCamelCase__ : Union[str, Any] = ids_tensor([self.batch_size, self.text_seq_length], self.num_labels ) lowerCamelCase__ : Dict = LayoutLMvaConfig( vocab_size=self.vocab_size, hidden_size=self.hidden_size, num_hidden_layers=self.num_hidden_layers, num_attention_heads=self.num_attention_heads, intermediate_size=self.intermediate_size, hidden_act=self.hidden_act, hidden_dropout_prob=self.hidden_dropout_prob, attention_probs_dropout_prob=self.attention_probs_dropout_prob, max_position_embeddings=self.max_position_embeddings, type_vocab_size=self.type_vocab_size, initializer_range=self.initializer_range, coordinate_size=self.coordinate_size, shape_size=self.shape_size, input_size=self.image_size, patch_size=self.patch_size, ) return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = TFLayoutLMvaModel(config=lowerCamelCase_ ) # text + image lowerCamelCase__ : Dict = model(lowerCamelCase_, pixel_values=lowerCamelCase_, training=lowerCamelCase_ ) lowerCamelCase__ : Any = model( lowerCamelCase_, bbox=lowerCamelCase_, pixel_values=lowerCamelCase_, attention_mask=lowerCamelCase_, token_type_ids=lowerCamelCase_, training=lowerCamelCase_, ) lowerCamelCase__ : int = model(lowerCamelCase_, bbox=lowerCamelCase_, pixel_values=lowerCamelCase_, training=lowerCamelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size) ) # text only lowerCamelCase__ : str = model(lowerCamelCase_, training=lowerCamelCase_ ) self.parent.assertEqual( result.last_hidden_state.shape, (self.batch_size, self.text_seq_length, self.hidden_size) ) # image only lowerCamelCase__ : Tuple = model({'pixel_values': pixel_values}, training=lowerCamelCase_ ) self.parent.assertEqual( result.last_hidden_state.shape, (self.batch_size, self.image_seq_length, self.hidden_size) ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = self.num_labels lowerCamelCase__ : Union[str, Any] = TFLayoutLMvaForSequenceClassification(config=lowerCamelCase_ ) lowerCamelCase__ : Union[str, Any] = model( lowerCamelCase_, bbox=lowerCamelCase_, pixel_values=lowerCamelCase_, attention_mask=lowerCamelCase_, token_type_ids=lowerCamelCase_, labels=lowerCamelCase_, training=lowerCamelCase_, ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.num_labels) ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : List[str] = self.num_labels lowerCamelCase__ : Optional[int] = TFLayoutLMvaForTokenClassification(config=lowerCamelCase_ ) lowerCamelCase__ : Optional[int] = model( lowerCamelCase_, bbox=lowerCamelCase_, pixel_values=lowerCamelCase_, attention_mask=lowerCamelCase_, token_type_ids=lowerCamelCase_, labels=lowerCamelCase_, training=lowerCamelCase_, ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.text_seq_length, self.num_labels) ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = 2 lowerCamelCase__ : List[Any] = TFLayoutLMvaForQuestionAnswering(config=lowerCamelCase_ ) lowerCamelCase__ : Optional[Any] = model( lowerCamelCase_, bbox=lowerCamelCase_, pixel_values=lowerCamelCase_, attention_mask=lowerCamelCase_, token_type_ids=lowerCamelCase_, start_positions=lowerCamelCase_, end_positions=lowerCamelCase_, training=lowerCamelCase_, ) self.parent.assertEqual(result.start_logits.shape, (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape, (self.batch_size, self.seq_length) ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = self.prepare_config_and_inputs() ((lowerCamelCase__) , (lowerCamelCase__) , (lowerCamelCase__) , (lowerCamelCase__) , (lowerCamelCase__) , (lowerCamelCase__) , (lowerCamelCase__) , (lowerCamelCase__)) : Optional[Any] = config_and_inputs lowerCamelCase__ : Dict = { 'input_ids': input_ids, 'bbox': bbox, 'pixel_values': pixel_values, 'token_type_ids': token_type_ids, 'attention_mask': input_mask, } return config, inputs_dict @require_tf class a_ ( snake_case_ , snake_case_ , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ : Tuple = ( ( TFLayoutLMvaModel, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, ) if is_tf_available() else () ) lowerCamelCase__ : Optional[Any] = ( {'document-question-answering': TFLayoutLMvaForQuestionAnswering, 'feature-extraction': TFLayoutLMvaModel} if is_tf_available() else {} ) lowerCamelCase__ : Dict = False lowerCamelCase__ : str = False lowerCamelCase__ : Any = False def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' return True def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_=False ): '''simple docstring''' lowerCamelCase__ : Tuple = copy.deepcopy(lowerCamelCase_ ) if model_class in get_values(lowerCamelCase_ ): lowerCamelCase__ : List[str] = { k: tf.tile(tf.expand_dims(lowerCamelCase_, 1 ), (1, self.model_tester.num_choices) + (1,) * (v.ndim - 1) ) if isinstance(lowerCamelCase_, tf.Tensor ) and v.ndim > 0 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(lowerCamelCase_ ): lowerCamelCase__ : Dict = tf.ones(self.model_tester.batch_size, dtype=tf.intaa ) elif model_class in get_values(lowerCamelCase_ ): lowerCamelCase__ : str = tf.zeros(self.model_tester.batch_size, dtype=tf.intaa ) lowerCamelCase__ : Any = tf.zeros(self.model_tester.batch_size, dtype=tf.intaa ) elif model_class in get_values(lowerCamelCase_ ): lowerCamelCase__ : Any = tf.zeros(self.model_tester.batch_size, dtype=tf.intaa ) elif model_class in get_values(lowerCamelCase_ ): lowerCamelCase__ : Dict = tf.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length), dtype=tf.intaa ) return inputs_dict def a__ (self ): '''simple docstring''' lowerCamelCase__ : Dict = TFLayoutLMvaModelTester(self ) lowerCamelCase__ : Optional[int] = ConfigTester(self, config_class=lowerCamelCase_, hidden_size=3_7 ) def a__ (self ): '''simple docstring''' self.config_tester.run_common_tests() def a__ (self ): '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase__ : Any = model_class(lowerCamelCase_ ) if getattr(lowerCamelCase_, 'hf_compute_loss', lowerCamelCase_ ): # The number of elements in the loss should be the same as the number of elements in the label lowerCamelCase__ : Dict = self._prepare_for_class(inputs_dict.copy(), lowerCamelCase_, return_labels=lowerCamelCase_ ) lowerCamelCase__ : Union[str, Any] = prepared_for_class[ sorted(prepared_for_class.keys() - inputs_dict.keys(), reverse=lowerCamelCase_ )[0] ] lowerCamelCase__ : Union[str, Any] = added_label.shape.as_list()[:1] # Test that model correctly compute the loss with kwargs lowerCamelCase__ : int = self._prepare_for_class(inputs_dict.copy(), lowerCamelCase_, return_labels=lowerCamelCase_ ) lowerCamelCase__ : Union[str, Any] = prepared_for_class.pop('input_ids' ) lowerCamelCase__ : str = model(lowerCamelCase_, **lowerCamelCase_ )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss when we mask some positions lowerCamelCase__ : Union[str, Any] = self._prepare_for_class(inputs_dict.copy(), lowerCamelCase_, return_labels=lowerCamelCase_ ) lowerCamelCase__ : Dict = prepared_for_class.pop('input_ids' ) if "labels" in prepared_for_class: lowerCamelCase__ : List[str] = prepared_for_class['labels'].numpy() if len(labels.shape ) > 1 and labels.shape[1] != 1: lowerCamelCase__ : int = -1_0_0 lowerCamelCase__ : str = tf.convert_to_tensor(lowerCamelCase_ ) lowerCamelCase__ : Optional[int] = model(lowerCamelCase_, **lowerCamelCase_ )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) self.assertTrue(not np.any(np.isnan(loss.numpy() ) ) ) # Test that model correctly compute the loss with a dict lowerCamelCase__ : Dict = self._prepare_for_class(inputs_dict.copy(), lowerCamelCase_, return_labels=lowerCamelCase_ ) lowerCamelCase__ : Optional[Any] = model(lowerCamelCase_ )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss with a tuple lowerCamelCase__ : Optional[Any] = self._prepare_for_class(inputs_dict.copy(), lowerCamelCase_, return_labels=lowerCamelCase_ ) # Get keys that were added with the _prepare_for_class function lowerCamelCase__ : Any = prepared_for_class.keys() - inputs_dict.keys() lowerCamelCase__ : Union[str, Any] = inspect.signature(model.call ).parameters lowerCamelCase__ : str = list(signature.keys() ) # Create a dictionary holding the location of the tensors in the tuple lowerCamelCase__ : Optional[Any] = {0: 'input_ids'} for label_key in label_keys: lowerCamelCase__ : Optional[Any] = signature_names.index(lowerCamelCase_ ) lowerCamelCase__ : Union[str, Any] = label_key lowerCamelCase__ : Dict = sorted(tuple_index_mapping.items() ) # Initialize a list with their default values, update the values and convert to a tuple lowerCamelCase__ : int = [] for name in signature_names: if name != "kwargs": list_input.append(signature[name].default ) for index, value in sorted_tuple_index_mapping: lowerCamelCase__ : str = prepared_for_class[value] lowerCamelCase__ : str = tuple(lowerCamelCase_ ) # Send to model lowerCamelCase__ : Tuple = model(tuple_input[:-1] )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) def a__ (self ): '''simple docstring''' ( ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ) : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ) def a__ (self ): '''simple docstring''' ( ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ) : Dict = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: lowerCamelCase__ : Optional[int] = type self.model_tester.create_and_check_model(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ) def a__ (self ): '''simple docstring''' ( ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ) : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification( lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ) def a__ (self ): '''simple docstring''' ( ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ) : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification( lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ) def a__ (self ): '''simple docstring''' ( ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ) : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering( lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ) @slow def a__ (self ): '''simple docstring''' for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase__ : Union[str, Any] = TFLayoutLMvaModel.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) def lowerCamelCase_ ( ): lowerCamelCase__ : List[Any] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_tf class a_ ( unittest.TestCase ): '''simple docstring''' @cached_property def a__ (self ): '''simple docstring''' return LayoutLMvaImageProcessor(apply_ocr=lowerCamelCase_ ) if is_vision_available() else None @slow def a__ (self ): '''simple docstring''' lowerCamelCase__ : List[Any] = TFLayoutLMvaModel.from_pretrained('microsoft/layoutlmv3-base' ) lowerCamelCase__ : str = self.default_image_processor lowerCamelCase__ : Tuple = prepare_img() lowerCamelCase__ : Dict = image_processor(images=lowerCamelCase_, return_tensors='tf' ).pixel_values lowerCamelCase__ : List[str] = tf.constant([[1, 2]] ) lowerCamelCase__ : Tuple = tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]] ), axis=0 ) # forward pass lowerCamelCase__ : List[str] = model(input_ids=lowerCamelCase_, bbox=lowerCamelCase_, pixel_values=lowerCamelCase_, training=lowerCamelCase_ ) # verify the logits lowerCamelCase__ : Tuple = (1, 1_9_9, 7_6_8) self.assertEqual(outputs.last_hidden_state.shape, lowerCamelCase_ ) lowerCamelCase__ : List[Any] = tf.constant( [[-0.0_529, 0.3_618, 0.1_632], [-0.1_587, -0.1_667, -0.0_400], [-0.1_557, -0.1_671, -0.0_505]] ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3], lowerCamelCase_, atol=1e-4 ) )
316
"""simple docstring""" import argparse import os import torch from transformers import FlavaImageCodebook, FlavaImageCodebookConfig def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : Any = s.rsplit(_lowerCamelCase , _lowerCamelCase ) return new.join(_lowerCamelCase ) def lowerCamelCase_ ( _lowerCamelCase ): # encoder.embeddings are double copied in original FLAVA return sum(param.float().sum() if 'encoder.embeddings' not in key else 0 for key, param in state_dict.items() ) def lowerCamelCase_ ( _lowerCamelCase ): lowerCamelCase__ : List[Any] = {} lowerCamelCase__ : Any = ['group_1', 'group_2', 'group_3', 'group_4'] for key, value in state_dict.items(): for group_key in group_keys: if group_key in key: lowerCamelCase__ : Union[str, Any] = key.replace(f'''{group_key}.''' , f'''{group_key}.group.''' ) if "res_path" in key: lowerCamelCase__ : Dict = key.replace('res_path.' , 'res_path.path.' ) if key.endswith('.w' ): lowerCamelCase__ : str = rreplace(_lowerCamelCase , '.w' , '.weight' , 1 ) if key.endswith('.b' ): lowerCamelCase__ : Optional[Any] = rreplace(_lowerCamelCase , '.b' , '.bias' , 1 ) lowerCamelCase__ : int = value.float() return upgrade @torch.no_grad() def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase=None , _lowerCamelCase=True ): from dall_e import Encoder lowerCamelCase__ : List[str] = Encoder() if os.path.exists(_lowerCamelCase ): lowerCamelCase__ : Optional[int] = torch.load(_lowerCamelCase ) else: lowerCamelCase__ : List[Any] = torch.hub.load_state_dict_from_url(_lowerCamelCase ) if isinstance(_lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : List[Any] = ckpt.state_dict() encoder.load_state_dict(_lowerCamelCase ) if config_path is not None: lowerCamelCase__ : Union[str, Any] = FlavaImageCodebookConfig.from_pretrained(_lowerCamelCase ) else: lowerCamelCase__ : Dict = FlavaImageCodebookConfig() lowerCamelCase__ : Tuple = FlavaImageCodebook(_lowerCamelCase ).eval() lowerCamelCase__ : List[str] = encoder.state_dict() lowerCamelCase__ : Any = upgrade_state_dict(_lowerCamelCase ) hf_model.load_state_dict(_lowerCamelCase ) lowerCamelCase__ : Optional[Any] = hf_model.state_dict() lowerCamelCase__ : Optional[int] = count_parameters(_lowerCamelCase ) lowerCamelCase__ : Optional[int] = count_parameters(_lowerCamelCase ) assert torch.allclose(_lowerCamelCase , _lowerCamelCase , atol=1e-3 ) if save_checkpoint: hf_model.save_pretrained(_lowerCamelCase ) else: return hf_state_dict if __name__ == "__main__": A_ : Tuple = argparse.ArgumentParser() parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to flava checkpoint") parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert") A_ : str = parser.parse_args() convert_dalle_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
316
1
"""simple docstring""" import warnings from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast from ...onnx.utils import compute_effective_axis_dimension from ...utils import TensorType, is_torch_available, logging A_ : int = logging.get_logger(__name__) A_ : Optional[Any] = { "facebook/bart-large": "https://huggingface.co/facebook/bart-large/resolve/main/config.json", # See all BART models at https://huggingface.co/models?filter=bart } class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : List[Any] = 'bart' lowerCamelCase__ : int = ['past_key_values'] lowerCamelCase__ : Dict = {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'} def __init__(self, lowerCamelCase_=5_0_2_6_5, lowerCamelCase_=1_0_2_4, lowerCamelCase_=1_2, lowerCamelCase_=4_0_9_6, lowerCamelCase_=1_6, lowerCamelCase_=1_2, lowerCamelCase_=4_0_9_6, lowerCamelCase_=1_6, lowerCamelCase_=0.0, lowerCamelCase_=0.0, lowerCamelCase_="gelu", lowerCamelCase_=1_0_2_4, lowerCamelCase_=0.1, lowerCamelCase_=0.0, lowerCamelCase_=0.0, lowerCamelCase_=0.02, lowerCamelCase_=0.0, lowerCamelCase_=False, lowerCamelCase_=True, lowerCamelCase_=3, lowerCamelCase_=1, lowerCamelCase_=0, lowerCamelCase_=2, lowerCamelCase_=True, lowerCamelCase_=2, lowerCamelCase_=2, **lowerCamelCase_, ): '''simple docstring''' lowerCamelCase__ : str = vocab_size lowerCamelCase__ : List[str] = max_position_embeddings lowerCamelCase__ : Union[str, Any] = d_model lowerCamelCase__ : Optional[int] = encoder_ffn_dim lowerCamelCase__ : Optional[Any] = encoder_layers lowerCamelCase__ : str = encoder_attention_heads lowerCamelCase__ : str = decoder_ffn_dim lowerCamelCase__ : Optional[int] = decoder_layers lowerCamelCase__ : str = decoder_attention_heads lowerCamelCase__ : Union[str, Any] = dropout lowerCamelCase__ : List[Any] = attention_dropout lowerCamelCase__ : str = activation_dropout lowerCamelCase__ : List[str] = activation_function lowerCamelCase__ : Any = init_std lowerCamelCase__ : List[Any] = encoder_layerdrop lowerCamelCase__ : List[Any] = decoder_layerdrop lowerCamelCase__ : List[str] = classifier_dropout lowerCamelCase__ : Optional[int] = use_cache lowerCamelCase__ : Any = encoder_layers lowerCamelCase__ : Dict = scale_embedding # scale factor will be sqrt(d_model) if True super().__init__( num_labels=lowerCamelCase_, pad_token_id=lowerCamelCase_, bos_token_id=lowerCamelCase_, eos_token_id=lowerCamelCase_, is_encoder_decoder=lowerCamelCase_, decoder_start_token_id=lowerCamelCase_, forced_eos_token_id=lowerCamelCase_, **lowerCamelCase_, ) # ensure backward compatibility for BART CNN models if self.forced_bos_token_id is None and kwargs.get('force_bos_token_to_be_generated', lowerCamelCase_ ): lowerCamelCase__ : str = self.bos_token_id warnings.warn( f'''Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. ''' 'The config can simply be saved and uploaded again to be fixed.' ) class a_ ( snake_case_ ): '''simple docstring''' @property def a__ (self ): '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: lowerCamelCase__ : Optional[Any] = OrderedDict( [ ('input_ids', {0: 'batch', 1: 'encoder_sequence'}), ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}), ] ) if self.use_past: lowerCamelCase__ : Any = {0: 'batch'} lowerCamelCase__ : Optional[Any] = {0: 'batch', 1: 'past_decoder_sequence + sequence'} else: lowerCamelCase__ : str = {0: 'batch', 1: 'decoder_sequence'} lowerCamelCase__ : Dict = {0: 'batch', 1: 'decoder_sequence'} if self.use_past: self.fill_with_past_key_values_(lowerCamelCase_, direction='inputs' ) elif self.task == "causal-lm": # TODO: figure this case out. lowerCamelCase__ : str = OrderedDict( [ ('input_ids', {0: 'batch', 1: 'encoder_sequence'}), ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}), ] ) if self.use_past: lowerCamelCase__ , lowerCamelCase__ : str = self.num_layers for i in range(lowerCamelCase_ ): lowerCamelCase__ : Tuple = {0: 'batch', 2: 'past_sequence + sequence'} lowerCamelCase__ : int = {0: 'batch', 2: 'past_sequence + sequence'} else: lowerCamelCase__ : Tuple = OrderedDict( [ ('input_ids', {0: 'batch', 1: 'encoder_sequence'}), ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}), ('decoder_input_ids', {0: 'batch', 1: 'decoder_sequence'}), ('decoder_attention_mask', {0: 'batch', 1: 'decoder_sequence'}), ] ) return common_inputs @property def a__ (self ): '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: lowerCamelCase__ : Union[str, Any] = super().outputs else: lowerCamelCase__ : int = super(lowerCamelCase_, self ).outputs if self.use_past: lowerCamelCase__ , lowerCamelCase__ : str = self.num_layers for i in range(lowerCamelCase_ ): lowerCamelCase__ : Tuple = {0: 'batch', 2: 'past_sequence + sequence'} lowerCamelCase__ : List[Any] = {0: 'batch', 2: 'past_sequence + sequence'} return common_outputs def a__ (self, lowerCamelCase_, lowerCamelCase_ = -1, lowerCamelCase_ = -1, lowerCamelCase_ = False, lowerCamelCase_ = None, ): '''simple docstring''' lowerCamelCase__ : List[Any] = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ) # Generate decoder inputs lowerCamelCase__ : str = seq_length if not self.use_past else 1 lowerCamelCase__ : List[str] = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ) lowerCamelCase__ : Any = {f'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()} lowerCamelCase__ : List[Any] = dict(**lowerCamelCase_, **lowerCamelCase_ ) if self.use_past: if not is_torch_available(): raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.' ) else: import torch lowerCamelCase__ , lowerCamelCase__ : Optional[int] = common_inputs['input_ids'].shape lowerCamelCase__ : Union[str, Any] = common_inputs['decoder_input_ids'].shape[1] lowerCamelCase__ , lowerCamelCase__ : Any = self.num_attention_heads lowerCamelCase__ : List[Any] = ( batch, num_encoder_attention_heads, encoder_seq_length, self._config.hidden_size // num_encoder_attention_heads, ) lowerCamelCase__ : Dict = decoder_seq_length + 3 lowerCamelCase__ : List[str] = ( batch, num_decoder_attention_heads, decoder_past_length, self._config.hidden_size // num_decoder_attention_heads, ) lowerCamelCase__ : Any = torch.cat( [common_inputs['decoder_attention_mask'], torch.ones(lowerCamelCase_, lowerCamelCase_ )], dim=1 ) lowerCamelCase__ : Union[str, Any] = [] # If the number of encoder and decoder layers are present in the model configuration, both are considered lowerCamelCase__ , lowerCamelCase__ : Any = self.num_layers lowerCamelCase__ : Dict = min(lowerCamelCase_, lowerCamelCase_ ) lowerCamelCase__ : Tuple = max(lowerCamelCase_, lowerCamelCase_ ) - min_num_layers lowerCamelCase__ : str = 'encoder' if num_encoder_layers > num_decoder_layers else 'decoder' for _ in range(lowerCamelCase_ ): common_inputs["past_key_values"].append( ( torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ ), ) ) # TODO: test this. lowerCamelCase__ : List[str] = encoder_shape if remaining_side_name == 'encoder' else decoder_shape for _ in range(lowerCamelCase_, lowerCamelCase_ ): common_inputs["past_key_values"].append((torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ )) ) return common_inputs def a__ (self, lowerCamelCase_, lowerCamelCase_ = -1, lowerCamelCase_ = -1, lowerCamelCase_ = False, lowerCamelCase_ = None, ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ) if self.use_past: if not is_torch_available(): raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.' ) else: import torch lowerCamelCase__ , lowerCamelCase__ : Optional[int] = common_inputs['input_ids'].shape # Not using the same length for past_key_values lowerCamelCase__ : Any = seqlen + 2 lowerCamelCase__ , lowerCamelCase__ : Any = self.num_layers lowerCamelCase__ , lowerCamelCase__ : Dict = self.num_attention_heads lowerCamelCase__ : List[str] = ( batch, num_encoder_attention_heads, past_key_values_length, self._config.hidden_size // num_encoder_attention_heads, ) lowerCamelCase__ : List[Any] = common_inputs['attention_mask'].dtype lowerCamelCase__ : List[str] = torch.cat( [common_inputs['attention_mask'], torch.ones(lowerCamelCase_, lowerCamelCase_, dtype=lowerCamelCase_ )], dim=1 ) lowerCamelCase__ : Union[str, Any] = [ (torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ )) for _ in range(lowerCamelCase_ ) ] return common_inputs def a__ (self, lowerCamelCase_, lowerCamelCase_ = -1, lowerCamelCase_ = -1, lowerCamelCase_ = False, lowerCamelCase_ = None, ): '''simple docstring''' lowerCamelCase__ : Any = compute_effective_axis_dimension( lowerCamelCase_, fixed_dimension=OnnxConfig.default_fixed_batch, num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX lowerCamelCase__ : Any = tokenizer.num_special_tokens_to_add(lowerCamelCase_ ) lowerCamelCase__ : str = compute_effective_axis_dimension( lowerCamelCase_, fixed_dimension=OnnxConfig.default_fixed_sequence, num_token_to_add=lowerCamelCase_ ) # Generate dummy inputs according to compute batch and sequence lowerCamelCase__ : Union[str, Any] = [' '.join([tokenizer.unk_token] ) * seq_length] * batch_size lowerCamelCase__ : Optional[Any] = dict(tokenizer(lowerCamelCase_, return_tensors=lowerCamelCase_ ) ) return common_inputs def a__ (self, lowerCamelCase_, lowerCamelCase_ = -1, lowerCamelCase_ = -1, lowerCamelCase_ = False, lowerCamelCase_ = None, ): '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: lowerCamelCase__ : Tuple = self._generate_dummy_inputs_for_default_and_seqaseq_lm( lowerCamelCase_, batch_size=lowerCamelCase_, seq_length=lowerCamelCase_, is_pair=lowerCamelCase_, framework=lowerCamelCase_ ) elif self.task == "causal-lm": lowerCamelCase__ : Tuple = self._generate_dummy_inputs_for_causal_lm( lowerCamelCase_, batch_size=lowerCamelCase_, seq_length=lowerCamelCase_, is_pair=lowerCamelCase_, framework=lowerCamelCase_ ) else: lowerCamelCase__ : Dict = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_, batch_size=lowerCamelCase_, seq_length=lowerCamelCase_, is_pair=lowerCamelCase_, framework=lowerCamelCase_ ) return common_inputs def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: lowerCamelCase__ : Any = super()._flatten_past_key_values_(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ) else: lowerCamelCase__ : Any = super(lowerCamelCase_, self )._flatten_past_key_values_( lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ )
316
"""simple docstring""" from __future__ import annotations import inspect import unittest import numpy as np from transformers import DeiTConfig 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 ( TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, TFDeiTModel, ) from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import DeiTImageProcessor class a_ : '''simple docstring''' def __init__(self, lowerCamelCase_, lowerCamelCase_=1_3, lowerCamelCase_=3_0, lowerCamelCase_=2, lowerCamelCase_=3, lowerCamelCase_=True, lowerCamelCase_=True, lowerCamelCase_=3_2, lowerCamelCase_=2, lowerCamelCase_=4, lowerCamelCase_=3_7, lowerCamelCase_="gelu", lowerCamelCase_=0.1, lowerCamelCase_=0.1, lowerCamelCase_=1_0, lowerCamelCase_=0.02, lowerCamelCase_=3, lowerCamelCase_=None, lowerCamelCase_=2, ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = parent lowerCamelCase__ : int = batch_size lowerCamelCase__ : Dict = image_size lowerCamelCase__ : List[str] = patch_size lowerCamelCase__ : Union[str, Any] = num_channels lowerCamelCase__ : str = is_training lowerCamelCase__ : Any = use_labels lowerCamelCase__ : Tuple = hidden_size lowerCamelCase__ : str = num_hidden_layers lowerCamelCase__ : Dict = num_attention_heads lowerCamelCase__ : Union[str, Any] = intermediate_size lowerCamelCase__ : Any = hidden_act lowerCamelCase__ : Dict = hidden_dropout_prob lowerCamelCase__ : Optional[Any] = attention_probs_dropout_prob lowerCamelCase__ : List[Any] = type_sequence_label_size lowerCamelCase__ : Optional[int] = initializer_range lowerCamelCase__ : Tuple = scope lowerCamelCase__ : List[str] = encoder_stride # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens) lowerCamelCase__ : str = (image_size // patch_size) ** 2 lowerCamelCase__ : Optional[int] = num_patches + 2 def a__ (self ): '''simple docstring''' lowerCamelCase__ : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCamelCase__ : Tuple = None if self.use_labels: lowerCamelCase__ : Optional[Any] = ids_tensor([self.batch_size], self.type_sequence_label_size ) lowerCamelCase__ : List[str] = self.get_config() return config, pixel_values, labels def a__ (self ): '''simple docstring''' return DeiTConfig( image_size=self.image_size, patch_size=self.patch_size, num_channels=self.num_channels, hidden_size=self.hidden_size, num_hidden_layers=self.num_hidden_layers, num_attention_heads=self.num_attention_heads, intermediate_size=self.intermediate_size, hidden_act=self.hidden_act, hidden_dropout_prob=self.hidden_dropout_prob, attention_probs_dropout_prob=self.attention_probs_dropout_prob, is_decoder=lowerCamelCase_, initializer_range=self.initializer_range, encoder_stride=self.encoder_stride, ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = TFDeiTModel(config=lowerCamelCase_ ) lowerCamelCase__ : Dict = model(lowerCamelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : List[str] = TFDeiTForMaskedImageModeling(config=lowerCamelCase_ ) lowerCamelCase__ : Any = model(lowerCamelCase_ ) self.parent.assertEqual( result.reconstruction.shape, (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images lowerCamelCase__ : Tuple = 1 lowerCamelCase__ : Optional[Any] = TFDeiTForMaskedImageModeling(lowerCamelCase_ ) lowerCamelCase__ : List[Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowerCamelCase__ : Optional[Any] = model(lowerCamelCase_ ) self.parent.assertEqual(result.reconstruction.shape, (self.batch_size, 1, self.image_size, self.image_size) ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : int = self.type_sequence_label_size lowerCamelCase__ : Union[str, Any] = TFDeiTForImageClassification(lowerCamelCase_ ) lowerCamelCase__ : Optional[Any] = model(lowerCamelCase_, labels=lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.type_sequence_label_size) ) # test greyscale images lowerCamelCase__ : List[str] = 1 lowerCamelCase__ : Any = TFDeiTForImageClassification(lowerCamelCase_ ) lowerCamelCase__ : List[str] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowerCamelCase__ : Optional[int] = model(lowerCamelCase_, labels=lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.type_sequence_label_size) ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Any = self.prepare_config_and_inputs() lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Tuple = config_and_inputs lowerCamelCase__ : str = {'pixel_values': pixel_values} return config, inputs_dict @require_tf class a_ ( snake_case_ , snake_case_ , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ : Any = ( ( TFDeiTModel, TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, ) if is_tf_available() else () ) lowerCamelCase__ : Tuple = ( { 'feature-extraction': TFDeiTModel, 'image-classification': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher), } if is_tf_available() else {} ) lowerCamelCase__ : Any = False lowerCamelCase__ : Optional[Any] = False lowerCamelCase__ : Dict = False lowerCamelCase__ : int = False def a__ (self ): '''simple docstring''' lowerCamelCase__ : List[Any] = TFDeiTModelTester(self ) lowerCamelCase__ : Union[str, Any] = ConfigTester(self, config_class=lowerCamelCase_, has_text_modality=lowerCamelCase_, hidden_size=3_7 ) def a__ (self ): '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason='DeiT does not use inputs_embeds' ) def a__ (self ): '''simple docstring''' pass def a__ (self ): '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase__ : Optional[int] = model_class(lowerCamelCase_ ) self.assertIsInstance(model.get_input_embeddings(), (tf.keras.layers.Layer) ) lowerCamelCase__ : List[str] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCamelCase_, tf.keras.layers.Dense ) ) def a__ (self ): '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase__ : Dict = model_class(lowerCamelCase_ ) lowerCamelCase__ : Any = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCamelCase__ : str = [*signature.parameters.keys()] lowerCamelCase__ : Union[str, Any] = ['pixel_values'] self.assertListEqual(arg_names[:1], lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_ ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_=False ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = super()._prepare_for_class(lowerCamelCase_, lowerCamelCase_, return_labels=lowerCamelCase_ ) if return_labels: if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call ).parameters: del inputs_dict["labels"] return inputs_dict @slow def a__ (self ): '''simple docstring''' for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase__ : int = TFDeiTModel.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) def lowerCamelCase_ ( ): lowerCamelCase__ : Any = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_tf @require_vision class a_ ( unittest.TestCase ): '''simple docstring''' @cached_property def a__ (self ): '''simple docstring''' return ( DeiTImageProcessor.from_pretrained('facebook/deit-base-distilled-patch16-224' ) if is_vision_available() else None ) @slow def a__ (self ): '''simple docstring''' lowerCamelCase__ : str = TFDeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-224' ) lowerCamelCase__ : List[Any] = self.default_image_processor lowerCamelCase__ : Union[str, Any] = prepare_img() lowerCamelCase__ : Optional[int] = image_processor(images=lowerCamelCase_, return_tensors='tf' ) # forward pass lowerCamelCase__ : Tuple = model(**lowerCamelCase_ ) # verify the logits lowerCamelCase__ : str = tf.TensorShape((1, 1_0_0_0) ) self.assertEqual(outputs.logits.shape, lowerCamelCase_ ) lowerCamelCase__ : Any = tf.constant([-1.0_266, 0.1_912, -1.2_861] ) self.assertTrue(np.allclose(outputs.logits[0, :3], lowerCamelCase_, atol=1e-4 ) )
316
1
"""simple docstring""" from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import numpy as np import tensorflow as tf from transformers import ( TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST, FlaubertConfig, TFFlaubertForMultipleChoice, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForSequenceClassification, TFFlaubertForTokenClassification, TFFlaubertModel, TFFlaubertWithLMHeadModel, ) class a_ : '''simple docstring''' def __init__(self, lowerCamelCase_, ): '''simple docstring''' lowerCamelCase__ : Any = parent lowerCamelCase__ : List[Any] = 1_3 lowerCamelCase__ : Optional[int] = 7 lowerCamelCase__ : int = True lowerCamelCase__ : Tuple = True lowerCamelCase__ : Tuple = True lowerCamelCase__ : List[str] = True lowerCamelCase__ : int = True lowerCamelCase__ : List[str] = False lowerCamelCase__ : Optional[int] = False lowerCamelCase__ : Optional[Any] = False lowerCamelCase__ : Dict = 2 lowerCamelCase__ : Dict = 9_9 lowerCamelCase__ : Optional[Any] = 0 lowerCamelCase__ : List[Any] = 3_2 lowerCamelCase__ : Dict = 2 lowerCamelCase__ : Dict = 4 lowerCamelCase__ : Dict = 0.1 lowerCamelCase__ : str = 0.1 lowerCamelCase__ : Dict = 5_1_2 lowerCamelCase__ : Optional[Any] = 1_6 lowerCamelCase__ : Any = 2 lowerCamelCase__ : Any = 0.02 lowerCamelCase__ : str = 3 lowerCamelCase__ : List[str] = 4 lowerCamelCase__ : List[str] = 'last' lowerCamelCase__ : Any = True lowerCamelCase__ : Any = None lowerCamelCase__ : List[Any] = 0 def a__ (self ): '''simple docstring''' lowerCamelCase__ : Dict = ids_tensor([self.batch_size, self.seq_length], self.vocab_size ) lowerCamelCase__ : int = random_attention_mask([self.batch_size, self.seq_length], dtype=tf.floataa ) lowerCamelCase__ : Tuple = None if self.use_input_lengths: lowerCamelCase__ : Union[str, Any] = ( ids_tensor([self.batch_size], vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length lowerCamelCase__ : List[str] = None if self.use_token_type_ids: lowerCamelCase__ : Optional[int] = ids_tensor([self.batch_size, self.seq_length], self.n_langs ) lowerCamelCase__ : int = None lowerCamelCase__ : int = None lowerCamelCase__ : int = None if self.use_labels: lowerCamelCase__ : List[str] = ids_tensor([self.batch_size], self.type_sequence_label_size ) lowerCamelCase__ : Tuple = ids_tensor([self.batch_size, self.seq_length], self.num_labels ) lowerCamelCase__ : Optional[int] = ids_tensor([self.batch_size], 2, dtype=tf.floataa ) lowerCamelCase__ : Dict = ids_tensor([self.batch_size], self.num_choices ) lowerCamelCase__ : List[str] = FlaubertConfig( vocab_size=self.vocab_size, n_special=self.n_special, emb_dim=self.hidden_size, n_layers=self.num_hidden_layers, n_heads=self.num_attention_heads, dropout=self.hidden_dropout_prob, attention_dropout=self.attention_probs_dropout_prob, gelu_activation=self.gelu_activation, sinusoidal_embeddings=self.sinusoidal_embeddings, asm=self.asm, causal=self.causal, n_langs=self.n_langs, max_position_embeddings=self.max_position_embeddings, initializer_range=self.initializer_range, summary_type=self.summary_type, use_proj=self.use_proj, bos_token_id=self.bos_token_id, ) return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, ): '''simple docstring''' lowerCamelCase__ : Dict = TFFlaubertModel(config=lowerCamelCase_ ) lowerCamelCase__ : List[Any] = {'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids} lowerCamelCase__ : List[str] = model(lowerCamelCase_ ) lowerCamelCase__ : List[Any] = [input_ids, input_mask] lowerCamelCase__ : Optional[Any] = model(lowerCamelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = TFFlaubertWithLMHeadModel(lowerCamelCase_ ) lowerCamelCase__ : Dict = {'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids} lowerCamelCase__ : Union[str, Any] = model(lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.vocab_size) ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, ): '''simple docstring''' lowerCamelCase__ : Tuple = TFFlaubertForQuestionAnsweringSimple(lowerCamelCase_ ) lowerCamelCase__ : List[str] = {'input_ids': input_ids, 'lengths': input_lengths} lowerCamelCase__ : Optional[int] = model(lowerCamelCase_ ) self.parent.assertEqual(result.start_logits.shape, (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape, (self.batch_size, self.seq_length) ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, ): '''simple docstring''' lowerCamelCase__ : str = TFFlaubertForSequenceClassification(lowerCamelCase_ ) lowerCamelCase__ : Optional[int] = {'input_ids': input_ids, 'lengths': input_lengths} lowerCamelCase__ : Any = model(lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.type_sequence_label_size) ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, ): '''simple docstring''' lowerCamelCase__ : int = self.num_labels lowerCamelCase__ : List[Any] = TFFlaubertForTokenClassification(config=lowerCamelCase_ ) lowerCamelCase__ : Optional[Any] = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} lowerCamelCase__ : int = model(lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.num_labels) ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = self.num_choices lowerCamelCase__ : List[Any] = TFFlaubertForMultipleChoice(config=lowerCamelCase_ ) lowerCamelCase__ : Optional[Any] = tf.tile(tf.expand_dims(lowerCamelCase_, 1 ), (1, self.num_choices, 1) ) lowerCamelCase__ : Union[str, Any] = tf.tile(tf.expand_dims(lowerCamelCase_, 1 ), (1, self.num_choices, 1) ) lowerCamelCase__ : Dict = tf.tile(tf.expand_dims(lowerCamelCase_, 1 ), (1, self.num_choices, 1) ) lowerCamelCase__ : Any = { 'input_ids': multiple_choice_inputs_ids, 'attention_mask': multiple_choice_input_mask, 'token_type_ids': multiple_choice_token_type_ids, } lowerCamelCase__ : Union[str, Any] = model(lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.num_choices) ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : List[str] = self.prepare_config_and_inputs() ( ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ) : Optional[Any] = config_and_inputs lowerCamelCase__ : List[Any] = { 'input_ids': input_ids, 'token_type_ids': token_type_ids, 'langs': token_type_ids, 'lengths': input_lengths, } return config, inputs_dict @require_tf class a_ ( snake_case_ , snake_case_ , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = ( ( TFFlaubertModel, TFFlaubertWithLMHeadModel, TFFlaubertForSequenceClassification, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForTokenClassification, TFFlaubertForMultipleChoice, ) if is_tf_available() else () ) lowerCamelCase__ : List[str] = ( (TFFlaubertWithLMHeadModel,) if is_tf_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable lowerCamelCase__ : Optional[int] = ( { 'feature-extraction': TFFlaubertModel, 'fill-mask': TFFlaubertWithLMHeadModel, 'question-answering': TFFlaubertForQuestionAnsweringSimple, 'text-classification': TFFlaubertForSequenceClassification, 'token-classification': TFFlaubertForTokenClassification, 'zero-shot': TFFlaubertForSequenceClassification, } if is_tf_available() else {} ) lowerCamelCase__ : str = False lowerCamelCase__ : Union[str, Any] = False def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''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 a__ (self ): '''simple docstring''' lowerCamelCase__ : int = TFFlaubertModelTester(self ) lowerCamelCase__ : str = ConfigTester(self, config_class=lowerCamelCase_, emb_dim=3_7 ) def a__ (self ): '''simple docstring''' self.config_tester.run_common_tests() def a__ (self ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_token_classification(*lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_multiple_choice(*lowerCamelCase_ ) @slow def a__ (self ): '''simple docstring''' for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase__ : Optional[int] = TFFlaubertModel.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) @require_tf @require_sentencepiece @require_tokenizers class a_ ( unittest.TestCase ): '''simple docstring''' @slow def a__ (self ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = TFFlaubertModel.from_pretrained('jplu/tf-flaubert-small-cased' ) lowerCamelCase__ : List[Any] = tf.convert_to_tensor( [[0, 1_5_8, 7_3_5, 2_5_9_2, 1_4_2_4, 6_7_2_7, 8_2, 1]], dtype=tf.intaa, ) # "J'aime flaubert !" lowerCamelCase__ : List[str] = model(lowerCamelCase_ )[0] lowerCamelCase__ : Any = tf.TensorShape((1, 8, 5_1_2) ) self.assertEqual(output.shape, lowerCamelCase_ ) # compare the actual values for a slice. lowerCamelCase__ : List[str] = tf.convert_to_tensor( [ [ [-1.8_768_773, -1.566_555, 0.27_072_418], [-1.6_920_038, -0.5_873_505, 1.9_329_599], [-2.9_563_985, -1.6_993_835, 1.7_972_052], ] ], dtype=tf.floataa, ) self.assertTrue(np.allclose(output[:, :3, :3].numpy(), expected_slice.numpy(), atol=1e-4 ) )
316
"""simple docstring""" def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): while second != 0: lowerCamelCase__ : Tuple = first & second first ^= second lowerCamelCase__ : int = c << 1 return first if __name__ == "__main__": import doctest doctest.testmod() A_ : Tuple = int(input("Enter the first number: ").strip()) A_ : Union[str, Any] = int(input("Enter the second number: ").strip()) print(f"{add(first, second) = }")
316
1
"""simple docstring""" # Lint as: python3 import os import re import urllib.parse from pathlib import Path from typing import Callable, List, Optional, Union from zipfile import ZipFile from ..utils.file_utils import cached_path, hf_github_url from ..utils.logging import get_logger from ..utils.version import Version A_ : int = get_logger(__name__) class a_ : '''simple docstring''' lowerCamelCase__ : List[Any] = 'dummy_data' lowerCamelCase__ : int = 'datasets' lowerCamelCase__ : Any = False def __init__(self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ = None, lowerCamelCase_ = False, lowerCamelCase_ = True, lowerCamelCase_ = None, ): '''simple docstring''' lowerCamelCase__ : str = 0 lowerCamelCase__ : Dict = dataset_name lowerCamelCase__ : List[Any] = cache_dir lowerCamelCase__ : Dict = use_local_dummy_data lowerCamelCase__ : str = config # download_callbacks take a single url as input lowerCamelCase__ : List[Callable] = download_callbacks or [] # if False, it doesn't load existing files and it returns the paths of the dummy files relative # to the dummy_data zip file root lowerCamelCase__ : Any = load_existing_dummy_data # TODO(PVP, QL) might need to make this more general lowerCamelCase__ : Union[str, Any] = str(lowerCamelCase_ ) # to be downloaded lowerCamelCase__ : Dict = None lowerCamelCase__ : int = None @property def a__ (self ): '''simple docstring''' if self._dummy_file is None: lowerCamelCase__ : int = self.download_dummy_data() return self._dummy_file @property def a__ (self ): '''simple docstring''' if self.config is not None: # structure is dummy / config_name / version_name return os.path.join('dummy', self.config.name, self.version_name ) # structure is dummy / version_name return os.path.join('dummy', self.version_name ) @property def a__ (self ): '''simple docstring''' return os.path.join(self.dummy_data_folder, 'dummy_data.zip' ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : str = ( self.local_path_to_dummy_data if self.use_local_dummy_data is True else self.github_path_to_dummy_data ) lowerCamelCase__ : Dict = cached_path( lowerCamelCase_, cache_dir=self.cache_dir, extract_compressed_file=lowerCamelCase_, force_extract=lowerCamelCase_ ) return os.path.join(lowerCamelCase_, self.dummy_file_name ) @property def a__ (self ): '''simple docstring''' return os.path.join(self.datasets_scripts_dir, self.dataset_name, self.dummy_zip_file ) @property def a__ (self ): '''simple docstring''' if self._bucket_url is None: lowerCamelCase__ : List[Any] = hf_github_url(self.dataset_name, self.dummy_zip_file.replace(os.sep, '/' ) ) return self._bucket_url @property def a__ (self ): '''simple docstring''' if os.path.isdir(self.dummy_file ): return self.dummy_file # else cut off path to file -> example `xsum`. return "/".join(self.dummy_file.replace(os.sep, '/' ).split('/' )[:-1] ) def a__ (self, lowerCamelCase_, *lowerCamelCase_ ): '''simple docstring''' if self.load_existing_dummy_data: # dummy data is downloaded and tested lowerCamelCase__ : Optional[int] = self.dummy_file else: # dummy data cannot be downloaded and only the path to dummy file is returned lowerCamelCase__ : Tuple = self.dummy_file_name # special case when data_url is a dict if isinstance(lowerCamelCase_, lowerCamelCase_ ): return self.create_dummy_data_dict(lowerCamelCase_, lowerCamelCase_ ) elif isinstance(lowerCamelCase_, (list, tuple) ): return self.create_dummy_data_list(lowerCamelCase_, lowerCamelCase_ ) else: return self.create_dummy_data_single(lowerCamelCase_, lowerCamelCase_ ) def a__ (self, lowerCamelCase_, *lowerCamelCase_ ): '''simple docstring''' return self.download_and_extract(lowerCamelCase_ ) def a__ (self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' return self.download_and_extract(lowerCamelCase_ ) def a__ (self, lowerCamelCase_, *lowerCamelCase_, **lowerCamelCase_ ): '''simple docstring''' return path def a__ (self ): '''simple docstring''' return {} def a__ (self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = {} for key, single_urls in data_url.items(): for download_callback in self.download_callbacks: if isinstance(lowerCamelCase_, lowerCamelCase_ ): for single_url in single_urls: download_callback(lowerCamelCase_ ) else: lowerCamelCase__ : Optional[int] = single_urls download_callback(lowerCamelCase_ ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus if isinstance(lowerCamelCase_, lowerCamelCase_ ): lowerCamelCase__ : Tuple = [os.path.join(lowerCamelCase_, urllib.parse.quote_plus(Path(lowerCamelCase_ ).name ) ) for x in single_urls] else: lowerCamelCase__ : Any = single_urls lowerCamelCase__ : Dict = os.path.join(lowerCamelCase_, urllib.parse.quote_plus(Path(lowerCamelCase_ ).name ) ) lowerCamelCase__ : Dict = value # make sure that values are unique if all(isinstance(lowerCamelCase_, lowerCamelCase_ ) for i in dummy_data_dict.values() ) and len(set(dummy_data_dict.values() ) ) < len( dummy_data_dict.values() ): # append key to value to make its name unique lowerCamelCase__ : List[str] = {key: value + key for key, value in dummy_data_dict.items()} return dummy_data_dict def a__ (self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : List[str] = [] # trick: if there are many shards named like `data.txt-000001-of-00300`, only use the first one lowerCamelCase__ : str = all(bool(re.findall('[0-9]{3,}-of-[0-9]{3,}', lowerCamelCase_ ) ) for url in data_url ) lowerCamelCase__ : Dict = all( url.startswith('https://ftp.ncbi.nlm.nih.gov/pubmed/baseline/pubmed' ) for url in data_url ) if data_url and (is_tf_records or is_pubmed_records): lowerCamelCase__ : Any = [data_url[0]] * len(lowerCamelCase_ ) for single_url in data_url: for download_callback in self.download_callbacks: download_callback(lowerCamelCase_ ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus lowerCamelCase__ : Optional[int] = os.path.join(lowerCamelCase_, urllib.parse.quote_plus(single_url.split('/' )[-1] ) ) dummy_data_list.append(lowerCamelCase_ ) return dummy_data_list def a__ (self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' for download_callback in self.download_callbacks: download_callback(lowerCamelCase_ ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus lowerCamelCase__ : int = os.path.join(lowerCamelCase_, urllib.parse.quote_plus(data_url.split('/' )[-1] ) ) if os.path.exists(lowerCamelCase_ ) or not self.load_existing_dummy_data: return value else: # Backward compatibility, maybe deprecate at one point. # For many datasets with single url calls to dl_manager.download_and_extract, # the dummy_data.zip file is actually the zipped downloaded file # while now we expected the dummy_data.zip file to be a directory containing # the downloaded file. return path_to_dummy_data def a__ (self ): '''simple docstring''' pass def a__ (self ): '''simple docstring''' pass def a__ (self, lowerCamelCase_ ): '''simple docstring''' def _iter_archive_members(lowerCamelCase_ ): # this preserves the order of the members inside the ZIP archive lowerCamelCase__ : List[str] = Path(self.dummy_file ).parent lowerCamelCase__ : Union[str, Any] = path.relative_to(lowerCamelCase_ ) with ZipFile(self.local_path_to_dummy_data ) as zip_file: lowerCamelCase__ : Union[str, Any] = zip_file.namelist() for member in members: if member.startswith(relative_path.as_posix() ): yield dummy_parent_path.joinpath(lowerCamelCase_ ) lowerCamelCase__ : Union[str, Any] = Path(lowerCamelCase_ ) lowerCamelCase__ : Optional[Any] = _iter_archive_members(lowerCamelCase_ ) if self.use_local_dummy_data else path.rglob('*' ) for file_path in file_paths: if file_path.is_file() and not file_path.name.startswith(('.', '__') ): yield file_path.relative_to(lowerCamelCase_ ).as_posix(), file_path.open('rb' ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' if not isinstance(lowerCamelCase_, lowerCamelCase_ ): lowerCamelCase__ : Tuple = [paths] for path in paths: if os.path.isfile(lowerCamelCase_ ): if os.path.basename(lowerCamelCase_ ).startswith(('.', '__') ): return yield path else: for dirpath, dirnames, filenames in os.walk(lowerCamelCase_ ): if os.path.basename(lowerCamelCase_ ).startswith(('.', '__') ): continue dirnames.sort() for filename in sorted(lowerCamelCase_ ): if filename.startswith(('.', '__') ): continue yield os.path.join(lowerCamelCase_, lowerCamelCase_ )
316
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) A_ : List[str] = {"configuration_encoder_decoder": ["EncoderDecoderConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Dict = ["EncoderDecoderModel"] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : List[Any] = ["TFEncoderDecoderModel"] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : List[Any] = ["FlaxEncoderDecoderModel"] if TYPE_CHECKING: from .configuration_encoder_decoder import EncoderDecoderConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_encoder_decoder import EncoderDecoderModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_encoder_decoder import TFEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_encoder_decoder import FlaxEncoderDecoderModel else: import sys A_ : Any = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
316
1
"""simple docstring""" def lowerCamelCase_ ( _lowerCamelCase ): if not head: return True # split the list to two parts lowerCamelCase__ , lowerCamelCase__ : Dict = head.next, head while fast and fast.next: lowerCamelCase__ : int = fast.next.next lowerCamelCase__ : Dict = slow.next lowerCamelCase__ : List[Any] = slow.next lowerCamelCase__ : Any = None # Don't forget here! But forget still works! # reverse the second part lowerCamelCase__ : Any = None while second: lowerCamelCase__ : Optional[int] = second.next lowerCamelCase__ : List[str] = node lowerCamelCase__ : Any = second lowerCamelCase__ : Any = nxt # compare two parts # second part has the same or one less node while node: if node.val != head.val: return False lowerCamelCase__ : Tuple = node.next lowerCamelCase__ : Dict = head.next return True def lowerCamelCase_ ( _lowerCamelCase ): if not head or not head.next: return True # 1. Get the midpoint (slow) lowerCamelCase__ : List[str] = head while fast and fast.next: lowerCamelCase__ , lowerCamelCase__ : str = fast.next.next, slow.next # 2. Push the second half into the stack lowerCamelCase__ : List[Any] = [slow.val] while slow.next: lowerCamelCase__ : List[str] = slow.next stack.append(slow.val ) # 3. Comparison while stack: if stack.pop() != cur.val: return False lowerCamelCase__ : Optional[Any] = cur.next return True def lowerCamelCase_ ( _lowerCamelCase ): if not head or not head.next: return True lowerCamelCase__ : Dict = {} lowerCamelCase__ : Union[str, Any] = 0 while head: if head.val in d: d[head.val].append(_lowerCamelCase ) else: lowerCamelCase__ : Union[str, Any] = [pos] lowerCamelCase__ : int = head.next pos += 1 lowerCamelCase__ : Tuple = pos - 1 lowerCamelCase__ : int = 0 for v in d.values(): if len(_lowerCamelCase ) % 2 != 0: middle += 1 else: lowerCamelCase__ : Dict = 0 for i in range(0 , len(_lowerCamelCase ) ): if v[i] + v[len(_lowerCamelCase ) - 1 - step] != checksum: return False step += 1 if middle > 1: return False return True
316
"""simple docstring""" import numpy as np def lowerCamelCase_ ( _lowerCamelCase ): return (2 / (1 + np.exp(-2 * vector ))) - 1 if __name__ == "__main__": import doctest doctest.testmod()
316
1
"""simple docstring""" def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : Optional[Any] = len(_lowerCamelCase ) lowerCamelCase__ : List[Any] = [[False] * (required_sum + 1) for _ in range(arr_len + 1 )] # for each arr value, a sum of zero(0) can be formed by not taking any element # hence True/1 for i in range(arr_len + 1 ): lowerCamelCase__ : Tuple = True # sum is not zero and set is empty then false for i in range(1 , required_sum + 1 ): lowerCamelCase__ : List[str] = False for i in range(1 , arr_len + 1 ): for j in range(1 , required_sum + 1 ): if arr[i - 1] > j: lowerCamelCase__ : List[str] = subset[i - 1][j] if arr[i - 1] <= j: lowerCamelCase__ : Any = subset[i - 1][j] or subset[i - 1][j - arr[i - 1]] return subset[arr_len][required_sum] if __name__ == "__main__": import doctest doctest.testmod()
316
"""simple docstring""" print((lambda quine: quine % quine)("print((lambda quine: quine %% quine)(%r))"))
316
1
"""simple docstring""" def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): if len(_lowerCamelCase ) != len(_lowerCamelCase ): raise ValueError('The length of profit and weight must be same.' ) if max_weight <= 0: raise ValueError('max_weight must greater than zero.' ) if any(p < 0 for p in profit ): raise ValueError('Profit can not be negative.' ) if any(w < 0 for w in weight ): raise ValueError('Weight can not be negative.' ) # List created to store profit gained for the 1kg in case of each weight # respectively. Calculate and append profit/weight for each element. lowerCamelCase__ : Optional[int] = [p / w for p, w in zip(_lowerCamelCase , _lowerCamelCase )] # Creating a copy of the list and sorting profit/weight in ascending order lowerCamelCase__ : Tuple = sorted(_lowerCamelCase ) # declaring useful variables lowerCamelCase__ : Tuple = len(_lowerCamelCase ) lowerCamelCase__ : Tuple = 0 lowerCamelCase__ : List[Any] = 0 lowerCamelCase__ : int = 0 # loop till the total weight do not reach max limit e.g. 15 kg and till i<length while limit <= max_weight and i < length: # flag value for encountered greatest element in sorted_profit_by_weight lowerCamelCase__ : Union[str, Any] = sorted_profit_by_weight[length - i - 1] lowerCamelCase__ : Optional[Any] = profit_by_weight.index(_lowerCamelCase ) lowerCamelCase__ : Optional[Any] = -1 # check if the weight encountered is less than the total weight # encountered before. if max_weight - limit >= weight[index]: limit += weight[index] # Adding profit gained for the given weight 1 === # weight[index]/weight[index] gain += 1 * profit[index] else: # Since the weight encountered is greater than limit, therefore take the # required number of remaining kgs and calculate profit for it. # weight remaining / weight[index] gain += (max_weight - limit) / weight[index] * profit[index] break i += 1 return gain if __name__ == "__main__": print( "Input profits, weights, and then max_weight (all positive ints) separated by " "spaces." ) A_ : str = [int(x) for x in input("Input profits separated by spaces: ").split()] A_ : Optional[int] = [int(x) for x in input("Input weights separated by spaces: ").split()] A_ : Union[str, Any] = int(input("Max weight allowed: ")) # Function Call calc_profit(profit, weight, max_weight)
316
"""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, ) A_ : int = { "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: A_ : Union[str, Any] = ["CLIPTokenizerFast"] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : int = ["CLIPFeatureExtractor"] A_ : Any = ["CLIPImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : List[Any] = [ "CLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "CLIPModel", "CLIPPreTrainedModel", "CLIPTextModel", "CLIPTextModelWithProjection", "CLIPVisionModel", "CLIPVisionModelWithProjection", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Any = [ "TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "TFCLIPModel", "TFCLIPPreTrainedModel", "TFCLIPTextModel", "TFCLIPVisionModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Any = [ "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 A_ : Optional[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
316
1
"""simple docstring""" def lowerCamelCase_ ( _lowerCamelCase ): if not numbers: return 0 if not isinstance(_lowerCamelCase , (list, tuple) ) or not all( isinstance(_lowerCamelCase , _lowerCamelCase ) for number in numbers ): raise ValueError('numbers must be an iterable of integers' ) lowerCamelCase__ : str = numbers[0] for i in range(1 , len(_lowerCamelCase ) ): # update the maximum and minimum subarray products lowerCamelCase__ : List[Any] = numbers[i] if number < 0: lowerCamelCase__ , lowerCamelCase__ : Dict = min_till_now, max_till_now lowerCamelCase__ : Tuple = max(_lowerCamelCase , max_till_now * number ) lowerCamelCase__ : int = min(_lowerCamelCase , min_till_now * number ) # update the maximum product found till now lowerCamelCase__ : Dict = max(_lowerCamelCase , _lowerCamelCase ) return max_prod
316
"""simple docstring""" import os import posixpath import uuid from dataclasses import dataclass from typing import TYPE_CHECKING, Iterable, List, Optional, Tuple, Union import numpy as np import pyarrow as pa import datasets from datasets.arrow_writer import ArrowWriter, ParquetWriter from datasets.config import MAX_SHARD_SIZE from datasets.filesystems import ( is_remote_filesystem, rename, ) from datasets.iterable_dataset import _BaseExamplesIterable from datasets.utils.py_utils import convert_file_size_to_int A_ : List[Any] = datasets.utils.logging.get_logger(__name__) if TYPE_CHECKING: import pyspark @dataclass class a_ ( datasets.BuilderConfig ): '''simple docstring''' lowerCamelCase__ : Optional[datasets.Features] = None def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , ): import pyspark def generate_fn(): lowerCamelCase__ : Optional[Any] = df.select('*' , pyspark.sql.functions.spark_partition_id().alias('part_id' ) ) for partition_id in partition_order: lowerCamelCase__ : Dict = df_with_partition_id.select('*' ).where(f'''part_id = {partition_id}''' ).drop('part_id' ) lowerCamelCase__ : Dict = partition_df.collect() lowerCamelCase__ : int = 0 for row in rows: yield f'''{partition_id}_{row_id}''', row.asDict() row_id += 1 return generate_fn class a_ ( _BaseExamplesIterable ): '''simple docstring''' def __init__(self, lowerCamelCase_, lowerCamelCase_=None, ): '''simple docstring''' lowerCamelCase__ : Tuple = df lowerCamelCase__ : Any = partition_order or range(self.df.rdd.getNumPartitions() ) lowerCamelCase__ : List[Any] = _generate_iterable_examples(self.df, self.partition_order ) def __iter__(self ): '''simple docstring''' yield from self.generate_examples_fn() def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = list(range(self.df.rdd.getNumPartitions() ) ) generator.shuffle(lowerCamelCase_ ) return SparkExamplesIterable(self.df, partition_order=lowerCamelCase_ ) def a__ (self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = self.split_shard_indices_by_worker(lowerCamelCase_, lowerCamelCase_ ) return SparkExamplesIterable(self.df, partition_order=lowerCamelCase_ ) @property def a__ (self ): '''simple docstring''' return len(self.partition_order ) class a_ ( datasets.DatasetBuilder ): '''simple docstring''' lowerCamelCase__ : Optional[int] = SparkConfig def __init__(self, lowerCamelCase_, lowerCamelCase_ = None, lowerCamelCase_ = None, **lowerCamelCase_, ): '''simple docstring''' import pyspark lowerCamelCase__ : str = pyspark.sql.SparkSession.builder.getOrCreate() lowerCamelCase__ : Optional[Any] = df lowerCamelCase__ : Dict = working_dir super().__init__( cache_dir=lowerCamelCase_, config_name=str(self.df.semanticHash() ), **lowerCamelCase_, ) def a__ (self ): '''simple docstring''' def create_cache_and_write_probe(lowerCamelCase_ ): # makedirs with exist_ok will recursively create the directory. It will not throw an error if directories # already exist. os.makedirs(self._cache_dir, exist_ok=lowerCamelCase_ ) lowerCamelCase__ : str = os.path.join(self._cache_dir, 'fs_test' + uuid.uuida().hex ) # Opening the file in append mode will create a new file unless it already exists, in which case it will not # change the file contents. open(lowerCamelCase_, 'a' ) return [probe_file] if self._spark.conf.get('spark.master', '' ).startswith('local' ): return # If the cluster is multi-node, make sure that the user provided a cache_dir and that it is on an NFS # accessible to the driver. # TODO: Stream batches to the driver using ArrowCollectSerializer instead of throwing an error. if self._cache_dir: lowerCamelCase__ : Tuple = ( self._spark.sparkContext.parallelize(range(1 ), 1 ).mapPartitions(lowerCamelCase_ ).collect() ) if os.path.isfile(probe[0] ): return raise ValueError( 'When using Dataset.from_spark on a multi-node cluster, the driver and all workers should be able to access cache_dir' ) def a__ (self ): '''simple docstring''' return datasets.DatasetInfo(features=self.config.features ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' return [datasets.SplitGenerator(name=datasets.Split.TRAIN )] def a__ (self, lowerCamelCase_ ): '''simple docstring''' import pyspark def get_arrow_batch_size(lowerCamelCase_ ): for batch in it: yield pa.RecordBatch.from_pydict({'batch_bytes': [batch.nbytes]} ) lowerCamelCase__ : List[Any] = self.df.count() lowerCamelCase__ : List[Any] = df_num_rows if df_num_rows <= 1_0_0 else 1_0_0 # Approximate the size of each row (in Arrow format) by averaging over a max-100-row sample. lowerCamelCase__ : List[Any] = ( self.df.limit(lowerCamelCase_ ) .repartition(1 ) .mapInArrow(lowerCamelCase_, 'batch_bytes: long' ) .agg(pyspark.sql.functions.sum('batch_bytes' ).alias('sample_bytes' ) ) .collect()[0] .sample_bytes / sample_num_rows ) lowerCamelCase__ : Dict = approx_bytes_per_row * df_num_rows if approx_total_size > max_shard_size: # Make sure there is at least one row per partition. lowerCamelCase__ : str = min(lowerCamelCase_, int(approx_total_size / max_shard_size ) ) lowerCamelCase__ : List[Any] = self.df.repartition(lowerCamelCase_ ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, ): '''simple docstring''' import pyspark lowerCamelCase__ : List[str] = ParquetWriter if file_format == 'parquet' else ArrowWriter lowerCamelCase__ : List[str] = os.path.join(self._working_dir, os.path.basename(lowerCamelCase_ ) ) if self._working_dir else fpath lowerCamelCase__ : Optional[int] = file_format == 'parquet' # Define these so that we don't reference self in write_arrow, which will result in a pickling error due to # pickling the SparkContext. lowerCamelCase__ : int = self.config.features lowerCamelCase__ : Dict = self._writer_batch_size lowerCamelCase__ : Optional[Any] = self._fs.storage_options def write_arrow(lowerCamelCase_ ): # Within the same SparkContext, no two task attempts will share the same attempt ID. lowerCamelCase__ : Any = pyspark.TaskContext().taskAttemptId() lowerCamelCase__ : str = next(lowerCamelCase_, lowerCamelCase_ ) if first_batch is None: # Some partitions might not receive any data. return pa.RecordBatch.from_arrays( [[task_id], [0], [0]], names=['task_id', 'num_examples', 'num_bytes'], ) lowerCamelCase__ : Tuple = 0 lowerCamelCase__ : Any = writer_class( features=lowerCamelCase_, path=working_fpath.replace('SSSSS', f'''{shard_id:05d}''' ).replace('TTTTT', f'''{task_id:05d}''' ), writer_batch_size=lowerCamelCase_, storage_options=lowerCamelCase_, embed_local_files=lowerCamelCase_, ) lowerCamelCase__ : List[str] = pa.Table.from_batches([first_batch] ) writer.write_table(lowerCamelCase_ ) for batch in it: if max_shard_size is not None and writer._num_bytes >= max_shard_size: lowerCamelCase__ , lowerCamelCase__ : Tuple = writer.finalize() writer.close() yield pa.RecordBatch.from_arrays( [[task_id], [num_examples], [num_bytes]], names=['task_id', 'num_examples', 'num_bytes'], ) shard_id += 1 lowerCamelCase__ : Dict = writer_class( features=writer._features, path=working_fpath.replace('SSSSS', f'''{shard_id:05d}''' ).replace('TTTTT', f'''{task_id:05d}''' ), writer_batch_size=lowerCamelCase_, storage_options=lowerCamelCase_, embed_local_files=lowerCamelCase_, ) lowerCamelCase__ : Tuple = pa.Table.from_batches([batch] ) writer.write_table(lowerCamelCase_ ) if writer._num_bytes > 0: lowerCamelCase__ , lowerCamelCase__ : Tuple = writer.finalize() writer.close() yield pa.RecordBatch.from_arrays( [[task_id], [num_examples], [num_bytes]], names=['task_id', 'num_examples', 'num_bytes'], ) if working_fpath != fpath: for file in os.listdir(os.path.dirname(lowerCamelCase_ ) ): lowerCamelCase__ : Optional[int] = os.path.join(os.path.dirname(lowerCamelCase_ ), os.path.basename(lowerCamelCase_ ) ) shutil.move(lowerCamelCase_, lowerCamelCase_ ) lowerCamelCase__ : List[str] = ( self.df.mapInArrow(lowerCamelCase_, 'task_id: long, num_examples: long, num_bytes: long' ) .groupBy('task_id' ) .agg( pyspark.sql.functions.sum('num_examples' ).alias('total_num_examples' ), pyspark.sql.functions.sum('num_bytes' ).alias('total_num_bytes' ), pyspark.sql.functions.count('num_bytes' ).alias('num_shards' ), pyspark.sql.functions.collect_list('num_examples' ).alias('shard_lengths' ), ) .collect() ) for row in stats: yield row.task_id, (row.total_num_examples, row.total_num_bytes, row.num_shards, row.shard_lengths) def a__ (self, lowerCamelCase_, lowerCamelCase_ = "arrow", lowerCamelCase_ = None, lowerCamelCase_ = None, **lowerCamelCase_, ): '''simple docstring''' self._validate_cache_dir() lowerCamelCase__ : Union[str, Any] = convert_file_size_to_int(max_shard_size or MAX_SHARD_SIZE ) self._repartition_df_if_needed(lowerCamelCase_ ) lowerCamelCase__ : str = not is_remote_filesystem(self._fs ) lowerCamelCase__ : Any = os.path.join if is_local else posixpath.join lowerCamelCase__ : Any = '-TTTTT-SSSSS-of-NNNNN' lowerCamelCase__ : Tuple = f'''{self.name}-{split_generator.name}{SUFFIX}.{file_format}''' lowerCamelCase__ : Union[str, Any] = path_join(self._output_dir, lowerCamelCase_ ) lowerCamelCase__ : List[str] = 0 lowerCamelCase__ : Dict = 0 lowerCamelCase__ : List[Any] = 0 lowerCamelCase__ : Optional[Any] = [] lowerCamelCase__ : List[str] = [] for task_id, content in self._prepare_split_single(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): ( ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ) : int = content if num_bytes > 0: total_num_examples += num_examples total_num_bytes += num_bytes total_shards += num_shards task_id_and_num_shards.append((task_id, num_shards) ) all_shard_lengths.extend(lowerCamelCase_ ) lowerCamelCase__ : str = total_num_examples lowerCamelCase__ : int = total_num_bytes # should rename everything at the end logger.debug(f'''Renaming {total_shards} shards.''' ) if total_shards > 1: lowerCamelCase__ : Union[str, Any] = all_shard_lengths # Define fs outside of _rename_shard so that we don't reference self in the function, which will result in a # pickling error due to pickling the SparkContext. lowerCamelCase__ : Optional[Any] = self._fs # use the -SSSSS-of-NNNNN pattern def _rename_shard( lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, ): rename( lowerCamelCase_, fpath.replace('SSSSS', f'''{shard_id:05d}''' ).replace('TTTTT', f'''{task_id:05d}''' ), fpath.replace('TTTTT-SSSSS', f'''{global_shard_id:05d}''' ).replace('NNNNN', f'''{total_shards:05d}''' ), ) lowerCamelCase__ : List[str] = [] lowerCamelCase__ : List[str] = 0 for i in range(len(lowerCamelCase_ ) ): lowerCamelCase__ , lowerCamelCase__ : Any = task_id_and_num_shards[i] for shard_id in range(lowerCamelCase_ ): args.append([task_id, shard_id, global_shard_id] ) global_shard_id += 1 self._spark.sparkContext.parallelize(lowerCamelCase_, len(lowerCamelCase_ ) ).map(lambda lowerCamelCase_ : _rename_shard(*lowerCamelCase_ ) ).collect() else: # don't use any pattern lowerCamelCase__ : List[Any] = 0 lowerCamelCase__ : Dict = task_id_and_num_shards[0][0] self._rename( fpath.replace('SSSSS', f'''{shard_id:05d}''' ).replace('TTTTT', f'''{task_id:05d}''' ), fpath.replace(lowerCamelCase_, '' ), ) def a__ (self, lowerCamelCase_, ): '''simple docstring''' return SparkExamplesIterable(self.df )
316
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging A_ : List[Any] = logging.get_logger(__name__) A_ : Dict = { "uw-madison/mra-base-512-4": "https://huggingface.co/uw-madison/mra-base-512-4/resolve/main/config.json", } class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : List[Any] = 'mra' def __init__(self, lowerCamelCase_=5_0_2_6_5, lowerCamelCase_=7_6_8, lowerCamelCase_=1_2, lowerCamelCase_=1_2, lowerCamelCase_=3_0_7_2, lowerCamelCase_="gelu", lowerCamelCase_=0.1, lowerCamelCase_=0.1, lowerCamelCase_=5_1_2, lowerCamelCase_=1, lowerCamelCase_=0.02, lowerCamelCase_=1e-5, lowerCamelCase_="absolute", lowerCamelCase_=4, lowerCamelCase_="full", lowerCamelCase_=0, lowerCamelCase_=0, lowerCamelCase_=1, lowerCamelCase_=0, lowerCamelCase_=2, **lowerCamelCase_, ): '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase_, bos_token_id=lowerCamelCase_, eos_token_id=lowerCamelCase_, **lowerCamelCase_ ) lowerCamelCase__ : Union[str, Any] = vocab_size lowerCamelCase__ : Tuple = max_position_embeddings lowerCamelCase__ : List[str] = hidden_size lowerCamelCase__ : Tuple = num_hidden_layers lowerCamelCase__ : List[Any] = num_attention_heads lowerCamelCase__ : int = intermediate_size lowerCamelCase__ : List[Any] = hidden_act lowerCamelCase__ : Union[str, Any] = hidden_dropout_prob lowerCamelCase__ : Optional[Any] = attention_probs_dropout_prob lowerCamelCase__ : Any = initializer_range lowerCamelCase__ : int = type_vocab_size lowerCamelCase__ : Any = layer_norm_eps lowerCamelCase__ : Optional[Any] = position_embedding_type lowerCamelCase__ : Any = block_per_row lowerCamelCase__ : List[Any] = approx_mode lowerCamelCase__ : Union[str, Any] = initial_prior_first_n_blocks lowerCamelCase__ : str = initial_prior_diagonal_n_blocks
316
"""simple docstring""" class a_ : '''simple docstring''' def __init__(self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Tuple = len(lowerCamelCase_ ) lowerCamelCase__ : Any = [0] * len_array if len_array > 0: lowerCamelCase__ : Union[str, Any] = array[0] for i in range(1, lowerCamelCase_ ): lowerCamelCase__ : Optional[int] = self.prefix_sum[i - 1] + array[i] def a__ (self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Dict = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(lowerCamelCase_ ) return False if __name__ == "__main__": import doctest doctest.testmod()
316
1
"""simple docstring""" import os import posixpath import uuid from dataclasses import dataclass from typing import TYPE_CHECKING, Iterable, List, Optional, Tuple, Union import numpy as np import pyarrow as pa import datasets from datasets.arrow_writer import ArrowWriter, ParquetWriter from datasets.config import MAX_SHARD_SIZE from datasets.filesystems import ( is_remote_filesystem, rename, ) from datasets.iterable_dataset import _BaseExamplesIterable from datasets.utils.py_utils import convert_file_size_to_int A_ : List[Any] = datasets.utils.logging.get_logger(__name__) if TYPE_CHECKING: import pyspark @dataclass class a_ ( datasets.BuilderConfig ): '''simple docstring''' lowerCamelCase__ : Optional[datasets.Features] = None def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , ): import pyspark def generate_fn(): lowerCamelCase__ : Optional[Any] = df.select('*' , pyspark.sql.functions.spark_partition_id().alias('part_id' ) ) for partition_id in partition_order: lowerCamelCase__ : Dict = df_with_partition_id.select('*' ).where(f'''part_id = {partition_id}''' ).drop('part_id' ) lowerCamelCase__ : Dict = partition_df.collect() lowerCamelCase__ : int = 0 for row in rows: yield f'''{partition_id}_{row_id}''', row.asDict() row_id += 1 return generate_fn class a_ ( _BaseExamplesIterable ): '''simple docstring''' def __init__(self, lowerCamelCase_, lowerCamelCase_=None, ): '''simple docstring''' lowerCamelCase__ : Tuple = df lowerCamelCase__ : Any = partition_order or range(self.df.rdd.getNumPartitions() ) lowerCamelCase__ : List[Any] = _generate_iterable_examples(self.df, self.partition_order ) def __iter__(self ): '''simple docstring''' yield from self.generate_examples_fn() def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = list(range(self.df.rdd.getNumPartitions() ) ) generator.shuffle(lowerCamelCase_ ) return SparkExamplesIterable(self.df, partition_order=lowerCamelCase_ ) def a__ (self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = self.split_shard_indices_by_worker(lowerCamelCase_, lowerCamelCase_ ) return SparkExamplesIterable(self.df, partition_order=lowerCamelCase_ ) @property def a__ (self ): '''simple docstring''' return len(self.partition_order ) class a_ ( datasets.DatasetBuilder ): '''simple docstring''' lowerCamelCase__ : Optional[int] = SparkConfig def __init__(self, lowerCamelCase_, lowerCamelCase_ = None, lowerCamelCase_ = None, **lowerCamelCase_, ): '''simple docstring''' import pyspark lowerCamelCase__ : str = pyspark.sql.SparkSession.builder.getOrCreate() lowerCamelCase__ : Optional[Any] = df lowerCamelCase__ : Dict = working_dir super().__init__( cache_dir=lowerCamelCase_, config_name=str(self.df.semanticHash() ), **lowerCamelCase_, ) def a__ (self ): '''simple docstring''' def create_cache_and_write_probe(lowerCamelCase_ ): # makedirs with exist_ok will recursively create the directory. It will not throw an error if directories # already exist. os.makedirs(self._cache_dir, exist_ok=lowerCamelCase_ ) lowerCamelCase__ : str = os.path.join(self._cache_dir, 'fs_test' + uuid.uuida().hex ) # Opening the file in append mode will create a new file unless it already exists, in which case it will not # change the file contents. open(lowerCamelCase_, 'a' ) return [probe_file] if self._spark.conf.get('spark.master', '' ).startswith('local' ): return # If the cluster is multi-node, make sure that the user provided a cache_dir and that it is on an NFS # accessible to the driver. # TODO: Stream batches to the driver using ArrowCollectSerializer instead of throwing an error. if self._cache_dir: lowerCamelCase__ : Tuple = ( self._spark.sparkContext.parallelize(range(1 ), 1 ).mapPartitions(lowerCamelCase_ ).collect() ) if os.path.isfile(probe[0] ): return raise ValueError( 'When using Dataset.from_spark on a multi-node cluster, the driver and all workers should be able to access cache_dir' ) def a__ (self ): '''simple docstring''' return datasets.DatasetInfo(features=self.config.features ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' return [datasets.SplitGenerator(name=datasets.Split.TRAIN )] def a__ (self, lowerCamelCase_ ): '''simple docstring''' import pyspark def get_arrow_batch_size(lowerCamelCase_ ): for batch in it: yield pa.RecordBatch.from_pydict({'batch_bytes': [batch.nbytes]} ) lowerCamelCase__ : List[Any] = self.df.count() lowerCamelCase__ : List[Any] = df_num_rows if df_num_rows <= 1_0_0 else 1_0_0 # Approximate the size of each row (in Arrow format) by averaging over a max-100-row sample. lowerCamelCase__ : List[Any] = ( self.df.limit(lowerCamelCase_ ) .repartition(1 ) .mapInArrow(lowerCamelCase_, 'batch_bytes: long' ) .agg(pyspark.sql.functions.sum('batch_bytes' ).alias('sample_bytes' ) ) .collect()[0] .sample_bytes / sample_num_rows ) lowerCamelCase__ : Dict = approx_bytes_per_row * df_num_rows if approx_total_size > max_shard_size: # Make sure there is at least one row per partition. lowerCamelCase__ : str = min(lowerCamelCase_, int(approx_total_size / max_shard_size ) ) lowerCamelCase__ : List[Any] = self.df.repartition(lowerCamelCase_ ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, ): '''simple docstring''' import pyspark lowerCamelCase__ : List[str] = ParquetWriter if file_format == 'parquet' else ArrowWriter lowerCamelCase__ : List[str] = os.path.join(self._working_dir, os.path.basename(lowerCamelCase_ ) ) if self._working_dir else fpath lowerCamelCase__ : Optional[int] = file_format == 'parquet' # Define these so that we don't reference self in write_arrow, which will result in a pickling error due to # pickling the SparkContext. lowerCamelCase__ : int = self.config.features lowerCamelCase__ : Dict = self._writer_batch_size lowerCamelCase__ : Optional[Any] = self._fs.storage_options def write_arrow(lowerCamelCase_ ): # Within the same SparkContext, no two task attempts will share the same attempt ID. lowerCamelCase__ : Any = pyspark.TaskContext().taskAttemptId() lowerCamelCase__ : str = next(lowerCamelCase_, lowerCamelCase_ ) if first_batch is None: # Some partitions might not receive any data. return pa.RecordBatch.from_arrays( [[task_id], [0], [0]], names=['task_id', 'num_examples', 'num_bytes'], ) lowerCamelCase__ : Tuple = 0 lowerCamelCase__ : Any = writer_class( features=lowerCamelCase_, path=working_fpath.replace('SSSSS', f'''{shard_id:05d}''' ).replace('TTTTT', f'''{task_id:05d}''' ), writer_batch_size=lowerCamelCase_, storage_options=lowerCamelCase_, embed_local_files=lowerCamelCase_, ) lowerCamelCase__ : List[str] = pa.Table.from_batches([first_batch] ) writer.write_table(lowerCamelCase_ ) for batch in it: if max_shard_size is not None and writer._num_bytes >= max_shard_size: lowerCamelCase__ , lowerCamelCase__ : Tuple = writer.finalize() writer.close() yield pa.RecordBatch.from_arrays( [[task_id], [num_examples], [num_bytes]], names=['task_id', 'num_examples', 'num_bytes'], ) shard_id += 1 lowerCamelCase__ : Dict = writer_class( features=writer._features, path=working_fpath.replace('SSSSS', f'''{shard_id:05d}''' ).replace('TTTTT', f'''{task_id:05d}''' ), writer_batch_size=lowerCamelCase_, storage_options=lowerCamelCase_, embed_local_files=lowerCamelCase_, ) lowerCamelCase__ : Tuple = pa.Table.from_batches([batch] ) writer.write_table(lowerCamelCase_ ) if writer._num_bytes > 0: lowerCamelCase__ , lowerCamelCase__ : Tuple = writer.finalize() writer.close() yield pa.RecordBatch.from_arrays( [[task_id], [num_examples], [num_bytes]], names=['task_id', 'num_examples', 'num_bytes'], ) if working_fpath != fpath: for file in os.listdir(os.path.dirname(lowerCamelCase_ ) ): lowerCamelCase__ : Optional[int] = os.path.join(os.path.dirname(lowerCamelCase_ ), os.path.basename(lowerCamelCase_ ) ) shutil.move(lowerCamelCase_, lowerCamelCase_ ) lowerCamelCase__ : List[str] = ( self.df.mapInArrow(lowerCamelCase_, 'task_id: long, num_examples: long, num_bytes: long' ) .groupBy('task_id' ) .agg( pyspark.sql.functions.sum('num_examples' ).alias('total_num_examples' ), pyspark.sql.functions.sum('num_bytes' ).alias('total_num_bytes' ), pyspark.sql.functions.count('num_bytes' ).alias('num_shards' ), pyspark.sql.functions.collect_list('num_examples' ).alias('shard_lengths' ), ) .collect() ) for row in stats: yield row.task_id, (row.total_num_examples, row.total_num_bytes, row.num_shards, row.shard_lengths) def a__ (self, lowerCamelCase_, lowerCamelCase_ = "arrow", lowerCamelCase_ = None, lowerCamelCase_ = None, **lowerCamelCase_, ): '''simple docstring''' self._validate_cache_dir() lowerCamelCase__ : Union[str, Any] = convert_file_size_to_int(max_shard_size or MAX_SHARD_SIZE ) self._repartition_df_if_needed(lowerCamelCase_ ) lowerCamelCase__ : str = not is_remote_filesystem(self._fs ) lowerCamelCase__ : Any = os.path.join if is_local else posixpath.join lowerCamelCase__ : Any = '-TTTTT-SSSSS-of-NNNNN' lowerCamelCase__ : Tuple = f'''{self.name}-{split_generator.name}{SUFFIX}.{file_format}''' lowerCamelCase__ : Union[str, Any] = path_join(self._output_dir, lowerCamelCase_ ) lowerCamelCase__ : List[str] = 0 lowerCamelCase__ : Dict = 0 lowerCamelCase__ : List[Any] = 0 lowerCamelCase__ : Optional[Any] = [] lowerCamelCase__ : List[str] = [] for task_id, content in self._prepare_split_single(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): ( ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ) : int = content if num_bytes > 0: total_num_examples += num_examples total_num_bytes += num_bytes total_shards += num_shards task_id_and_num_shards.append((task_id, num_shards) ) all_shard_lengths.extend(lowerCamelCase_ ) lowerCamelCase__ : str = total_num_examples lowerCamelCase__ : int = total_num_bytes # should rename everything at the end logger.debug(f'''Renaming {total_shards} shards.''' ) if total_shards > 1: lowerCamelCase__ : Union[str, Any] = all_shard_lengths # Define fs outside of _rename_shard so that we don't reference self in the function, which will result in a # pickling error due to pickling the SparkContext. lowerCamelCase__ : Optional[Any] = self._fs # use the -SSSSS-of-NNNNN pattern def _rename_shard( lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, ): rename( lowerCamelCase_, fpath.replace('SSSSS', f'''{shard_id:05d}''' ).replace('TTTTT', f'''{task_id:05d}''' ), fpath.replace('TTTTT-SSSSS', f'''{global_shard_id:05d}''' ).replace('NNNNN', f'''{total_shards:05d}''' ), ) lowerCamelCase__ : List[str] = [] lowerCamelCase__ : List[str] = 0 for i in range(len(lowerCamelCase_ ) ): lowerCamelCase__ , lowerCamelCase__ : Any = task_id_and_num_shards[i] for shard_id in range(lowerCamelCase_ ): args.append([task_id, shard_id, global_shard_id] ) global_shard_id += 1 self._spark.sparkContext.parallelize(lowerCamelCase_, len(lowerCamelCase_ ) ).map(lambda lowerCamelCase_ : _rename_shard(*lowerCamelCase_ ) ).collect() else: # don't use any pattern lowerCamelCase__ : List[Any] = 0 lowerCamelCase__ : Dict = task_id_and_num_shards[0][0] self._rename( fpath.replace('SSSSS', f'''{shard_id:05d}''' ).replace('TTTTT', f'''{task_id:05d}''' ), fpath.replace(lowerCamelCase_, '' ), ) def a__ (self, lowerCamelCase_, ): '''simple docstring''' return SparkExamplesIterable(self.df )
316
"""simple docstring""" import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : Dict = ['image_processor', 'tokenizer'] lowerCamelCase__ : Optional[int] = 'CLIPImageProcessor' lowerCamelCase__ : List[str] = ('XLMRobertaTokenizer', 'XLMRobertaTokenizerFast') def __init__(self, lowerCamelCase_=None, lowerCamelCase_=None, **lowerCamelCase_ ): '''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.', lowerCamelCase_, ) 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__(lowerCamelCase_, lowerCamelCase_ ) def __call__(self, lowerCamelCase_=None, lowerCamelCase_=None, lowerCamelCase_=None, **lowerCamelCase_ ): '''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__ : Any = self.tokenizer(lowerCamelCase_, return_tensors=lowerCamelCase_, **lowerCamelCase_ ) if images is not None: lowerCamelCase__ : List[Any] = self.image_processor(lowerCamelCase_, return_tensors=lowerCamelCase_, **lowerCamelCase_ ) if text is not None and images is not None: lowerCamelCase__ : str = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**lowerCamelCase_ ), tensor_type=lowerCamelCase_ ) def a__ (self, *lowerCamelCase_, **lowerCamelCase_ ): '''simple docstring''' return self.tokenizer.batch_decode(*lowerCamelCase_, **lowerCamelCase_ ) def a__ (self, *lowerCamelCase_, **lowerCamelCase_ ): '''simple docstring''' return self.tokenizer.decode(*lowerCamelCase_, **lowerCamelCase_ ) @property def a__ (self ): '''simple docstring''' lowerCamelCase__ : str = self.tokenizer.model_input_names lowerCamelCase__ : List[str] = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
316
1
"""simple docstring""" import inspect import unittest from transformers import DPTConfig from transformers.file_utils import is_torch_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_vision, slow, torch_device 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 MODEL_MAPPING, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel from transformers.models.dpt.modeling_dpt import DPT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import DPTImageProcessor class a_ : '''simple docstring''' def __init__(self, lowerCamelCase_, lowerCamelCase_=2, lowerCamelCase_=3_2, lowerCamelCase_=1_6, lowerCamelCase_=3, lowerCamelCase_=True, lowerCamelCase_=True, lowerCamelCase_=3_2, lowerCamelCase_=4, lowerCamelCase_=[0, 1, 2, 3], lowerCamelCase_=4, lowerCamelCase_=3_7, lowerCamelCase_="gelu", lowerCamelCase_=0.1, lowerCamelCase_=0.1, lowerCamelCase_=0.02, lowerCamelCase_=3, lowerCamelCase_=[1, 3_8_4, 2_4, 2_4], lowerCamelCase_=True, lowerCamelCase_=None, ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = parent lowerCamelCase__ : str = batch_size lowerCamelCase__ : Union[str, Any] = image_size lowerCamelCase__ : Any = patch_size lowerCamelCase__ : Optional[int] = num_channels lowerCamelCase__ : Optional[int] = is_training lowerCamelCase__ : str = use_labels lowerCamelCase__ : Tuple = hidden_size lowerCamelCase__ : Union[str, Any] = num_hidden_layers lowerCamelCase__ : Optional[Any] = backbone_out_indices lowerCamelCase__ : List[Any] = num_attention_heads lowerCamelCase__ : List[str] = intermediate_size lowerCamelCase__ : Any = hidden_act lowerCamelCase__ : Optional[Any] = hidden_dropout_prob lowerCamelCase__ : str = attention_probs_dropout_prob lowerCamelCase__ : Tuple = initializer_range lowerCamelCase__ : Union[str, Any] = num_labels lowerCamelCase__ : Any = backbone_featmap_shape lowerCamelCase__ : Optional[Any] = scope lowerCamelCase__ : str = is_hybrid # sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token) lowerCamelCase__ : Union[str, Any] = (image_size // patch_size) ** 2 lowerCamelCase__ : Tuple = num_patches + 1 def a__ (self ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCamelCase__ : Optional[Any] = None if self.use_labels: lowerCamelCase__ : Union[str, Any] = ids_tensor([self.batch_size, self.image_size, self.image_size], self.num_labels ) lowerCamelCase__ : Union[str, Any] = self.get_config() return config, pixel_values, labels def a__ (self ): '''simple docstring''' lowerCamelCase__ : Any = { 'global_padding': 'same', 'layer_type': 'bottleneck', 'depths': [3, 4, 9], 'out_features': ['stage1', 'stage2', 'stage3'], 'embedding_dynamic_padding': True, 'hidden_sizes': [9_6, 1_9_2, 3_8_4, 7_6_8], 'num_groups': 2, } return DPTConfig( image_size=self.image_size, patch_size=self.patch_size, num_channels=self.num_channels, hidden_size=self.hidden_size, num_hidden_layers=self.num_hidden_layers, backbone_out_indices=self.backbone_out_indices, num_attention_heads=self.num_attention_heads, intermediate_size=self.intermediate_size, hidden_act=self.hidden_act, hidden_dropout_prob=self.hidden_dropout_prob, attention_probs_dropout_prob=self.attention_probs_dropout_prob, is_decoder=lowerCamelCase_, initializer_range=self.initializer_range, is_hybrid=self.is_hybrid, backbone_config=lowerCamelCase_, backbone_featmap_shape=self.backbone_featmap_shape, ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Dict = DPTModel(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() lowerCamelCase__ : List[str] = model(lowerCamelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : List[str] = self.num_labels lowerCamelCase__ : List[Any] = DPTForDepthEstimation(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() lowerCamelCase__ : Dict = model(lowerCamelCase_ ) self.parent.assertEqual(result.predicted_depth.shape, (self.batch_size, self.image_size, self.image_size) ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Tuple = self.num_labels lowerCamelCase__ : Tuple = DPTForSemanticSegmentation(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() lowerCamelCase__ : Dict = model(lowerCamelCase_, labels=lowerCamelCase_ ) self.parent.assertEqual( result.logits.shape, (self.batch_size, self.num_labels, self.image_size, self.image_size) ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Tuple = self.prepare_config_and_inputs() lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Optional[Any] = config_and_inputs lowerCamelCase__ : Optional[int] = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class a_ ( snake_case_ , snake_case_ , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ : Optional[int] = (DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else () lowerCamelCase__ : Union[str, Any] = ( { 'depth-estimation': DPTForDepthEstimation, 'feature-extraction': DPTModel, 'image-segmentation': DPTForSemanticSegmentation, } if is_torch_available() else {} ) lowerCamelCase__ : str = False lowerCamelCase__ : str = False lowerCamelCase__ : List[Any] = False def a__ (self ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = DPTModelTester(self ) lowerCamelCase__ : int = ConfigTester(self, config_class=lowerCamelCase_, has_text_modality=lowerCamelCase_, hidden_size=3_7 ) def a__ (self ): '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason='DPT does not use inputs_embeds' ) def a__ (self ): '''simple docstring''' pass def a__ (self ): '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase__ : Optional[Any] = model_class(lowerCamelCase_ ) self.assertIsInstance(model.get_input_embeddings(), (nn.Module) ) lowerCamelCase__ : Tuple = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCamelCase_, nn.Linear ) ) def a__ (self ): '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase__ : Tuple = model_class(lowerCamelCase_ ) lowerCamelCase__ : List[Any] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCamelCase__ : List[str] = [*signature.parameters.keys()] lowerCamelCase__ : Union[str, Any] = ['pixel_values'] self.assertListEqual(arg_names[:1], lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_depth_estimation(*lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*lowerCamelCase_ ) def a__ (self ): '''simple docstring''' for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue lowerCamelCase__ , lowerCamelCase__ : List[str] = self.model_tester.prepare_config_and_inputs_for_common() lowerCamelCase__ : List[Any] = True if model_class in get_values(lowerCamelCase_ ): continue lowerCamelCase__ : Union[str, Any] = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.train() lowerCamelCase__ : str = self._prepare_for_class(lowerCamelCase_, lowerCamelCase_, return_labels=lowerCamelCase_ ) lowerCamelCase__ : Tuple = model(**lowerCamelCase_ ).loss loss.backward() def a__ (self ): '''simple docstring''' for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue lowerCamelCase__ , lowerCamelCase__ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() lowerCamelCase__ : Dict = False lowerCamelCase__ : Any = True if model_class in get_values(lowerCamelCase_ ) or not model_class.supports_gradient_checkpointing: continue lowerCamelCase__ : List[str] = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.gradient_checkpointing_enable() model.train() lowerCamelCase__ : List[Any] = self._prepare_for_class(lowerCamelCase_, lowerCamelCase_, return_labels=lowerCamelCase_ ) lowerCamelCase__ : str = model(**lowerCamelCase_ ).loss loss.backward() def a__ (self ): '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ : Dict = self.model_tester.prepare_config_and_inputs_for_common() lowerCamelCase__ : Any = _config_zero_init(lowerCamelCase_ ) for model_class in self.all_model_classes: lowerCamelCase__ : Any = model_class(config=lowerCamelCase_ ) # Skip the check for the backbone lowerCamelCase__ : int = [] for name, module in model.named_modules(): if module.__class__.__name__ == "DPTViTHybridEmbeddings": lowerCamelCase__ : Dict = [f'''{name}.{key}''' for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue 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('Will be fixed soon by reducing the size of the model used for common tests.' ) def a__ (self ): '''simple docstring''' pass @slow def a__ (self ): '''simple docstring''' for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]: lowerCamelCase__ : Optional[Any] = DPTModel.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ : str = self.model_tester.prepare_config_and_inputs_for_common() lowerCamelCase__ : Any = 'add' with self.assertRaises(lowerCamelCase_ ): lowerCamelCase__ : Dict = DPTForDepthEstimation(lowerCamelCase_ ) def lowerCamelCase_ ( ): lowerCamelCase__ : Optional[Any] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision @slow class a_ ( unittest.TestCase ): '''simple docstring''' def a__ (self ): '''simple docstring''' lowerCamelCase__ : int = DPTImageProcessor.from_pretrained('Intel/dpt-hybrid-midas' ) lowerCamelCase__ : int = DPTForDepthEstimation.from_pretrained('Intel/dpt-hybrid-midas' ).to(lowerCamelCase_ ) lowerCamelCase__ : Optional[Any] = prepare_img() lowerCamelCase__ : Optional[int] = image_processor(images=lowerCamelCase_, return_tensors='pt' ).to(lowerCamelCase_ ) # forward pass with torch.no_grad(): lowerCamelCase__ : int = model(**lowerCamelCase_ ) lowerCamelCase__ : Dict = outputs.predicted_depth # verify the predicted depth lowerCamelCase__ : List[Any] = torch.Size((1, 3_8_4, 3_8_4) ) self.assertEqual(predicted_depth.shape, lowerCamelCase_ ) lowerCamelCase__ : str = torch.tensor( [[[5.6_437, 5.6_146, 5.6_511], [5.4_371, 5.5_649, 5.5_958], [5.5_215, 5.5_184, 5.5_293]]] ).to(lowerCamelCase_ ) self.assertTrue(torch.allclose(outputs.predicted_depth[:3, :3, :3] / 1_0_0, lowerCamelCase_, atol=1e-4 ) )
316
"""simple docstring""" import cva import numpy as np class a_ : '''simple docstring''' def __init__(self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' if k in (0.04, 0.06): lowerCamelCase__ : Tuple = k lowerCamelCase__ : Optional[Any] = window_size else: raise ValueError('invalid k value' ) def __str__(self ): '''simple docstring''' return str(self.k ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = cva.imread(lowerCamelCase_, 0 ) lowerCamelCase__ , lowerCamelCase__ : Union[str, Any] = img.shape lowerCamelCase__ : list[list[int]] = [] lowerCamelCase__ : Optional[Any] = img.copy() lowerCamelCase__ : Optional[Any] = cva.cvtColor(lowerCamelCase_, cva.COLOR_GRAY2RGB ) lowerCamelCase__ , lowerCamelCase__ : Any = np.gradient(lowerCamelCase_ ) lowerCamelCase__ : Optional[Any] = dx**2 lowerCamelCase__ : List[Any] = dy**2 lowerCamelCase__ : List[str] = dx * dy lowerCamelCase__ : Tuple = 0.04 lowerCamelCase__ : List[Any] = self.window_size // 2 for y in range(lowerCamelCase_, h - offset ): for x in range(lowerCamelCase_, w - offset ): lowerCamelCase__ : Union[str, Any] = ixx[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() lowerCamelCase__ : Optional[Any] = iyy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() lowerCamelCase__ : List[Any] = ixy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() lowerCamelCase__ : str = (wxx * wyy) - (wxy**2) lowerCamelCase__ : Dict = wxx + wyy lowerCamelCase__ : 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_5_5 ) return color_img, corner_list if __name__ == "__main__": A_ : Optional[Any] = HarrisCorner(0.04, 3) A_, A_ : List[Any] = edge_detect.detect("path_to_image") cva.imwrite("detect.png", color_img)
316
1
"""simple docstring""" import argparse from argparse import Namespace import torch from torch import nn from transformers import XGLMConfig, XGLMForCausalLM def lowerCamelCase_ ( _lowerCamelCase ): lowerCamelCase__ : List[Any] = [ 'decoder.version', 'decoder.output_projection.weight', '_float_tensor', 'decoder.embed_positions._float_tensor', ] for k in ignore_keys: state_dict.pop(_lowerCamelCase , _lowerCamelCase ) def lowerCamelCase_ ( _lowerCamelCase ): lowerCamelCase__ , lowerCamelCase__ : Dict = emb.weight.shape lowerCamelCase__ : str = nn.Linear(_lowerCamelCase , _lowerCamelCase , bias=_lowerCamelCase ) lowerCamelCase__ : Dict = emb.weight.data return lin_layer def lowerCamelCase_ ( _lowerCamelCase ): lowerCamelCase__ : Any = torch.load(_lowerCamelCase , map_location='cpu' ) lowerCamelCase__ : Optional[Any] = Namespace(**checkpoint['cfg']['model'] ) lowerCamelCase__ : List[str] = checkpoint['model'] remove_ignore_keys_(_lowerCamelCase ) lowerCamelCase__ : Any = state_dict['decoder.embed_tokens.weight'].shape[0] lowerCamelCase__ : Optional[int] = {key.replace('decoder' , 'model' ): val for key, val in state_dict.items()} lowerCamelCase__ : int = XGLMConfig( vocab_size=_lowerCamelCase , max_position_embeddings=args.max_target_positions , num_layers=args.decoder_layers , attention_heads=args.decoder_attention_heads , ffn_dim=args.decoder_ffn_embed_dim , d_model=args.decoder_embed_dim , layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function='gelu' , scale_embedding=not args.no_scale_embedding , tie_word_embeddings=args.share_decoder_input_output_embed , ) lowerCamelCase__ : Optional[Any] = XGLMForCausalLM(_lowerCamelCase ) lowerCamelCase__ : List[str] = model.load_state_dict(_lowerCamelCase , strict=_lowerCamelCase ) print(_lowerCamelCase ) lowerCamelCase__ : str = make_linear_from_emb(model.model.embed_tokens ) return model if __name__ == "__main__": A_ : int = argparse.ArgumentParser() # Required parameters parser.add_argument("fairseq_path", type=str, help="path to a model.pt on local filesystem.") parser.add_argument("pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") A_ : List[str] = parser.parse_args() A_ : Union[str, Any] = convert_fairseq_xglm_checkpoint_from_disk(args.fairseq_path) model.save_pretrained(args.pytorch_dump_folder_path)
316
"""simple docstring""" from collections.abc import Iterator, MutableMapping from dataclasses import dataclass from typing import Generic, TypeVar A_ : str = TypeVar("KEY") A_ : List[Any] = TypeVar("VAL") @dataclass(frozen=snake_case_ , slots=snake_case_ ) class a_ ( Generic[KEY, VAL] ): '''simple docstring''' lowerCamelCase__ : KEY lowerCamelCase__ : VAL class a_ ( _Item ): '''simple docstring''' def __init__(self ): '''simple docstring''' super().__init__(lowerCamelCase_, lowerCamelCase_ ) def __bool__(self ): '''simple docstring''' return False A_ : List[Any] = _DeletedItem() class a_ ( MutableMapping[KEY, VAL] ): '''simple docstring''' def __init__(self, lowerCamelCase_ = 8, lowerCamelCase_ = 0.75 ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = initial_block_size lowerCamelCase__ : list[_Item | None] = [None] * initial_block_size assert 0.0 < capacity_factor < 1.0 lowerCamelCase__ : List[Any] = capacity_factor lowerCamelCase__ : Optional[int] = 0 def a__ (self, lowerCamelCase_ ): '''simple docstring''' return hash(lowerCamelCase_ ) % len(self._buckets ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' return (ind + 1) % len(self._buckets ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Optional[int] = self._buckets[ind] if not stored: lowerCamelCase__ : Tuple = _Item(lowerCamelCase_, lowerCamelCase_ ) self._len += 1 return True elif stored.key == key: lowerCamelCase__ : Optional[int] = _Item(lowerCamelCase_, lowerCamelCase_ ) return True else: return False def a__ (self ): '''simple docstring''' lowerCamelCase__ : int = len(self._buckets ) * self._capacity_factor return len(self ) >= int(lowerCamelCase_ ) def a__ (self ): '''simple docstring''' if len(self._buckets ) <= self._initial_block_size: return False lowerCamelCase__ : Any = len(self._buckets ) * self._capacity_factor / 2 return len(self ) < limit def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Any = self._buckets lowerCamelCase__ : Dict = [None] * new_size lowerCamelCase__ : Tuple = 0 for item in old_buckets: if item: self._add_item(item.key, item.val ) def a__ (self ): '''simple docstring''' self._resize(len(self._buckets ) * 2 ) def a__ (self ): '''simple docstring''' self._resize(len(self._buckets ) // 2 ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = self._get_bucket_index(lowerCamelCase_ ) for _ in range(len(self._buckets ) ): yield ind lowerCamelCase__ : Tuple = self._get_next_ind(lowerCamelCase_ ) def a__ (self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' for ind in self._iterate_buckets(lowerCamelCase_ ): if self._try_set(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): break def __setitem__(self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' if self._is_full(): self._size_up() self._add_item(lowerCamelCase_, lowerCamelCase_ ) def __delitem__(self, lowerCamelCase_ ): '''simple docstring''' for ind in self._iterate_buckets(lowerCamelCase_ ): lowerCamelCase__ : List[str] = self._buckets[ind] if item is None: raise KeyError(lowerCamelCase_ ) if item is _deleted: continue if item.key == key: lowerCamelCase__ : Optional[int] = _deleted self._len -= 1 break if self._is_sparse(): self._size_down() def __getitem__(self, lowerCamelCase_ ): '''simple docstring''' for ind in self._iterate_buckets(lowerCamelCase_ ): lowerCamelCase__ : List[Any] = self._buckets[ind] if item is None: break if item is _deleted: continue if item.key == key: return item.val raise KeyError(lowerCamelCase_ ) def __len__(self ): '''simple docstring''' return self._len def __iter__(self ): '''simple docstring''' yield from (item.key for item in self._buckets if item) def __repr__(self ): '''simple docstring''' lowerCamelCase__ : List[str] = ' ,'.join( f'''{item.key}: {item.val}''' for item in self._buckets if item ) return f'''HashMap({val_string})'''
316
1
"""simple docstring""" A_ : Optional[Any] = 9.80665 def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase = g ): 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()
316
"""simple docstring""" def lowerCamelCase_ ( ): lowerCamelCase__ : Optional[Any] = [] lowerCamelCase__ : List[Any] = 1 while len(_lowerCamelCase ) < 1e6: constant.append(str(_lowerCamelCase ) ) i += 1 lowerCamelCase__ : str = ''.join(_lowerCamelCase ) return ( int(constant[0] ) * int(constant[9] ) * int(constant[99] ) * int(constant[999] ) * int(constant[9999] ) * int(constant[9_9999] ) * int(constant[99_9999] ) ) if __name__ == "__main__": print(solution())
316
1
"""simple docstring""" A_ : str = { 0: "0", 1: "1", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7", 8: "8", 9: "9", 10: "a", 11: "b", 12: "c", 13: "d", 14: "e", 15: "f", } def lowerCamelCase_ ( _lowerCamelCase ): assert type(_lowerCamelCase ) in (int, float) and decimal == int(_lowerCamelCase ) lowerCamelCase__ : Any = int(_lowerCamelCase ) lowerCamelCase__ : Any = '' lowerCamelCase__ : int = False if decimal < 0: lowerCamelCase__ : List[str] = True decimal *= -1 while decimal > 0: lowerCamelCase__ , lowerCamelCase__ : Optional[int] = divmod(_lowerCamelCase , 16 ) lowerCamelCase__ : Union[str, Any] = values[remainder] + hexadecimal lowerCamelCase__ : Dict = '0x' + hexadecimal if negative: lowerCamelCase__ : List[str] = '-' + hexadecimal return hexadecimal if __name__ == "__main__": import doctest doctest.testmod()
316
"""simple docstring""" # Lint as: python3 # pylint: enable=line-too-long # pylint: disable=g-import-not-at-top,g-bad-import-order,wrong-import-position A_ : Union[str, Any] = "2.13.1" import platform import pyarrow from packaging import version if version.parse(platform.python_version()) < version.parse("3.7"): raise ImportWarning( "To use `datasets`, Python>=3.7 is required, and the current version of Python doesn't match this condition." ) if version.parse(pyarrow.__version__).major < 8: raise ImportWarning( "To use `datasets`, the module `pyarrow>=8.0.0` is required, and the current version of `pyarrow` doesn't match this condition.\n" "If you are running this in a Google Colab, you should probably just restart the runtime to use the right version of `pyarrow`." ) del platform del pyarrow del version from .arrow_dataset import Dataset from .arrow_reader import ReadInstruction from .builder import ArrowBasedBuilder, BeamBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder from .combine import concatenate_datasets, interleave_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .download import * from .features import * from .fingerprint import disable_caching, enable_caching, is_caching_enabled, set_caching_enabled from .info import DatasetInfo, MetricInfo from .inspect import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, list_datasets, list_metrics, ) from .iterable_dataset import IterableDataset from .load import load_dataset, load_dataset_builder, load_from_disk, load_metric from .metric import Metric from .splits import ( NamedSplit, NamedSplitAll, Split, SplitBase, SplitDict, SplitGenerator, SplitInfo, SubSplitInfo, percent, ) from .tasks import * from .utils import * from .utils import logging # deprecated modules from datasets import arrow_dataset as _arrow_dataset # isort:skip from datasets import utils as _utils # isort:skip from datasets.utils import download_manager as _deprecated_download_manager # isort:skip A_ : int = concatenate_datasets A_ : Any = DownloadConfig A_ : List[Any] = DownloadManager A_ : Optional[Any] = DownloadMode A_ : List[str] = DownloadConfig A_ : Optional[int] = DownloadMode A_ : Dict = DownloadManager del _arrow_dataset, _utils, _deprecated_download_manager
316
1
"""simple docstring""" import qiskit def lowerCamelCase_ ( _lowerCamelCase = 2 ): lowerCamelCase__ : Optional[Any] = qubits # Using Aer's simulator lowerCamelCase__ : Dict = qiskit.Aer.get_backend('aer_simulator' ) # Creating a Quantum Circuit acting on the q register lowerCamelCase__ : int = qiskit.QuantumCircuit(_lowerCamelCase , _lowerCamelCase ) # Adding a H gate on qubit 0 (now q0 in superposition) circuit.h(0 ) for i in range(1 , _lowerCamelCase ): # Adding CX (CNOT) gate circuit.cx(i - 1 , _lowerCamelCase ) # Mapping the quantum measurement to the classical bits circuit.measure(list(range(_lowerCamelCase ) ) , list(range(_lowerCamelCase ) ) ) # Now measuring any one qubit would affect other qubits to collapse # their super position and have same state as the measured one. # Executing the circuit on the simulator lowerCamelCase__ : Optional[Any] = qiskit.execute(_lowerCamelCase , _lowerCamelCase , shots=1000 ) return job.result().get_counts(_lowerCamelCase ) if __name__ == "__main__": print(f"Total count for various states are: {quantum_entanglement(3)}")
316
"""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 A_ : str = logging.getLogger(__name__) @dataclass @add_start_docstrings(TrainingArguments.__doc__ ) class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : bool = field(default=snake_case_ , metadata={'help': 'Whether to use SortishSampler or not.'} ) lowerCamelCase__ : bool = field( default=snake_case_ , metadata={'help': 'Whether to use generate to calculate generative metrics (ROUGE, BLEU).'} ) lowerCamelCase__ : Optional[int] = field( default=snake_case_ , 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__ : Optional[int] = field( default=snake_case_ , 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__ : Optional[Union[str, Path, GenerationConfig]] = field( default=snake_case_ , metadata={ 'help': 'Model id, file path or url pointing to a GenerationConfig json file, to use during prediction.' } , ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Any = super().to_dict() for k, v in d.items(): if isinstance(lowerCamelCase_, lowerCamelCase_ ): lowerCamelCase__ : Any = v.to_dict() return d
316
1
"""simple docstring""" import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import BatchEncoding, PreTrainedTokenizer from ...utils import logging A_ : List[str] = logging.get_logger(__name__) A_ : Optional[Any] = "▁" A_ : Union[str, Any] = { "vocab_file": "vocab.json", "spm_file": "sentencepiece.bpe.model", "tokenizer_config_file": "tokenizer_config.json", } A_ : Optional[Any] = { "vocab_file": { "facebook/m2m100_418M": "https://huggingface.co/facebook/m2m100_418M/resolve/main/vocab.json", "facebook/m2m100_1.2B": "https://huggingface.co/facebook/m2m100_1.2B/resolve/main/vocab.json", }, "spm_file": { "facebook/m2m100_418M": "https://huggingface.co/facebook/m2m100_418M/resolve/main/sentencepiece.bpe.model", "facebook/m2m100_1.2B": "https://huggingface.co/facebook/m2m100_1.2B/resolve/main/sentencepiece.bpe.model", }, "tokenizer_config_file": { "facebook/m2m100_418M": "https://huggingface.co/facebook/m2m100_418M/resolve/main/tokenizer_config.json", "facebook/m2m100_1.2B": "https://huggingface.co/facebook/m2m100_1.2B/resolve/main/tokenizer_config.json", }, } A_ : str = { "facebook/m2m100_418M": 10_24, } # fmt: off A_ : Dict = { "m2m100": ["af", "am", "ar", "ast", "az", "ba", "be", "bg", "bn", "br", "bs", "ca", "ceb", "cs", "cy", "da", "de", "el", "en", "es", "et", "fa", "ff", "fi", "fr", "fy", "ga", "gd", "gl", "gu", "ha", "he", "hi", "hr", "ht", "hu", "hy", "id", "ig", "ilo", "is", "it", "ja", "jv", "ka", "kk", "km", "kn", "ko", "lb", "lg", "ln", "lo", "lt", "lv", "mg", "mk", "ml", "mn", "mr", "ms", "my", "ne", "nl", "no", "ns", "oc", "or", "pa", "pl", "ps", "pt", "ro", "ru", "sd", "si", "sk", "sl", "so", "sq", "sr", "ss", "su", "sv", "sw", "ta", "th", "tl", "tn", "tr", "uk", "ur", "uz", "vi", "wo", "xh", "yi", "yo", "zh", "zu"], "wmt21": ["en", "ha", "is", "ja", "cs", "ru", "zh", "de"] } class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : str = VOCAB_FILES_NAMES lowerCamelCase__ : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase__ : Optional[int] = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase__ : Optional[Any] = ['input_ids', 'attention_mask'] lowerCamelCase__ : List[int] = [] lowerCamelCase__ : List[int] = [] def __init__(self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_=None, lowerCamelCase_=None, lowerCamelCase_="<s>", lowerCamelCase_="</s>", lowerCamelCase_="</s>", lowerCamelCase_="<pad>", lowerCamelCase_="<unk>", lowerCamelCase_="m2m100", lowerCamelCase_ = None, lowerCamelCase_=8, **lowerCamelCase_, ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = {} if sp_model_kwargs is None else sp_model_kwargs lowerCamelCase__ : List[str] = language_codes lowerCamelCase__ : int = FAIRSEQ_LANGUAGE_CODES[language_codes] lowerCamelCase__ : Dict = {lang_code: f'''__{lang_code}__''' for lang_code in fairseq_language_code} lowerCamelCase__ : int = kwargs.get('additional_special_tokens', [] ) kwargs["additional_special_tokens"] += [ self.get_lang_token(lowerCamelCase_ ) for lang_code in fairseq_language_code if self.get_lang_token(lowerCamelCase_ ) not in kwargs["additional_special_tokens"] ] super().__init__( src_lang=lowerCamelCase_, tgt_lang=lowerCamelCase_, bos_token=lowerCamelCase_, eos_token=lowerCamelCase_, sep_token=lowerCamelCase_, unk_token=lowerCamelCase_, pad_token=lowerCamelCase_, language_codes=lowerCamelCase_, sp_model_kwargs=self.sp_model_kwargs, num_madeup_words=lowerCamelCase_, **lowerCamelCase_, ) lowerCamelCase__ : List[str] = vocab_file lowerCamelCase__ : Optional[int] = load_json(lowerCamelCase_ ) lowerCamelCase__ : Any = {v: k for k, v in self.encoder.items()} lowerCamelCase__ : Dict = spm_file lowerCamelCase__ : Dict = load_spm(lowerCamelCase_, self.sp_model_kwargs ) lowerCamelCase__ : Dict = len(self.encoder ) lowerCamelCase__ : int = { self.get_lang_token(lowerCamelCase_ ): self.encoder_size + i for i, lang_code in enumerate(lowerCamelCase_ ) } lowerCamelCase__ : Any = {lang_code: self.encoder_size + i for i, lang_code in enumerate(lowerCamelCase_ )} lowerCamelCase__ : Optional[Any] = {v: k for k, v in self.lang_token_to_id.items()} lowerCamelCase__ : List[str] = src_lang if src_lang is not None else 'en' lowerCamelCase__ : Optional[int] = tgt_lang lowerCamelCase__ : Tuple = self.get_lang_id(self._src_lang ) self.set_src_lang_special_tokens(self._src_lang ) lowerCamelCase__ : Union[str, Any] = num_madeup_words @property def a__ (self ): '''simple docstring''' return len(self.encoder ) + len(self.lang_token_to_id ) @property def a__ (self ): '''simple docstring''' return self._src_lang @src_lang.setter def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' return self.sp_model.encode(lowerCamelCase_, out_type=lowerCamelCase_ ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' if token in self.lang_token_to_id: return self.lang_token_to_id[token] return self.encoder.get(lowerCamelCase_, self.encoder[self.unk_token] ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' if index in self.id_to_lang_token: return self.id_to_lang_token[index] return self.decoder.get(lowerCamelCase_, self.unk_token ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : List[str] = [] lowerCamelCase__ : str = '' for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: out_string += self.sp_model.decode(lowerCamelCase_ ) + token lowerCamelCase__ : Any = [] else: current_sub_tokens.append(lowerCamelCase_ ) out_string += self.sp_model.decode(lowerCamelCase_ ) return out_string.strip() def a__ (self, lowerCamelCase_, lowerCamelCase_ = None, lowerCamelCase_ = False ): '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=lowerCamelCase_, token_ids_a=lowerCamelCase_, already_has_special_tokens=lowerCamelCase_ ) lowerCamelCase__ : List[Any] = [1] * len(self.prefix_tokens ) lowerCamelCase__ : int = [1] * len(self.suffix_tokens ) if token_ids_a is None: return prefix_ones + ([0] * len(lowerCamelCase_ )) + suffix_ones return prefix_ones + ([0] * len(lowerCamelCase_ )) + ([0] * len(lowerCamelCase_ )) + suffix_ones def a__ (self, lowerCamelCase_, lowerCamelCase_ = None ): '''simple docstring''' 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 a__ (self ): '''simple docstring''' lowerCamelCase__ : Any = {self.convert_ids_to_tokens(lowerCamelCase_ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__(self ): '''simple docstring''' lowerCamelCase__ : List[str] = self.__dict__.copy() lowerCamelCase__ : List[str] = None return state def __setstate__(self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : List[str] = d # for backward compatibility if not hasattr(self, 'sp_model_kwargs' ): lowerCamelCase__ : List[Any] = {} lowerCamelCase__ : List[str] = load_spm(self.spm_file, self.sp_model_kwargs ) def a__ (self, lowerCamelCase_, lowerCamelCase_ = None ): '''simple docstring''' lowerCamelCase__ : str = Path(lowerCamelCase_ ) if not save_dir.is_dir(): raise OSError(f'''{save_directory} should be a directory''' ) lowerCamelCase__ : List[Any] = save_dir / ( (filename_prefix + '-' if filename_prefix else '') + self.vocab_files_names['vocab_file'] ) lowerCamelCase__ : List[str] = save_dir / ( (filename_prefix + '-' if filename_prefix else '') + self.vocab_files_names['spm_file'] ) save_json(self.encoder, lowerCamelCase_ ) if os.path.abspath(self.spm_file ) != os.path.abspath(lowerCamelCase_ ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file, lowerCamelCase_ ) elif not os.path.isfile(self.spm_file ): with open(lowerCamelCase_, 'wb' ) as fi: lowerCamelCase__ : List[str] = self.sp_model.serialized_model_proto() fi.write(lowerCamelCase_ ) return (str(lowerCamelCase_ ), str(lowerCamelCase_ )) def a__ (self, lowerCamelCase_, lowerCamelCase_ = "en", lowerCamelCase_ = None, lowerCamelCase_ = "ro", **lowerCamelCase_, ): '''simple docstring''' lowerCamelCase__ : int = src_lang lowerCamelCase__ : List[str] = tgt_lang self.set_src_lang_special_tokens(self.src_lang ) return super().prepare_seqaseq_batch(lowerCamelCase_, lowerCamelCase_, **lowerCamelCase_ ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, **lowerCamelCase_ ): '''simple docstring''' if src_lang is None or tgt_lang is None: raise ValueError('Translation requires a `src_lang` and a `tgt_lang` for this model' ) lowerCamelCase__ : Any = src_lang lowerCamelCase__ : List[str] = self(lowerCamelCase_, add_special_tokens=lowerCamelCase_, **lowerCamelCase_ ) lowerCamelCase__ : Union[str, Any] = self.get_lang_id(lowerCamelCase_ ) lowerCamelCase__ : List[Any] = tgt_lang_id return inputs def a__ (self ): '''simple docstring''' self.set_src_lang_special_tokens(self.src_lang ) def a__ (self ): '''simple docstring''' self.set_tgt_lang_special_tokens(self.tgt_lang ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Tuple = self.get_lang_token(lowerCamelCase_ ) lowerCamelCase__ : int = self.lang_token_to_id[lang_token] lowerCamelCase__ : Optional[int] = [self.cur_lang_id] lowerCamelCase__ : str = [self.eos_token_id] def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : List[Any] = self.get_lang_token(lowerCamelCase_ ) lowerCamelCase__ : int = self.lang_token_to_id[lang_token] lowerCamelCase__ : Dict = [self.cur_lang_id] lowerCamelCase__ : Union[str, Any] = [self.eos_token_id] def a__ (self, lowerCamelCase_ ): '''simple docstring''' return self.lang_code_to_token[lang] def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Any = self.get_lang_token(lowerCamelCase_ ) return self.lang_token_to_id[lang_token] def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : str = sentencepiece.SentencePieceProcessor(**_lowerCamelCase ) spm.Load(str(_lowerCamelCase ) ) return spm def lowerCamelCase_ ( _lowerCamelCase ): with open(_lowerCamelCase , 'r' ) as f: return json.load(_lowerCamelCase ) def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): with open(_lowerCamelCase , 'w' ) as f: json.dump(_lowerCamelCase , _lowerCamelCase , indent=2 )
316
"""simple docstring""" def lowerCamelCase_ ( _lowerCamelCase ): lowerCamelCase__ : Union[str, Any] = [] lowerCamelCase__ : List[str] = [] lowerCamelCase__ : Tuple = { '^': 3, '*': 2, '/': 2, '%': 2, '+': 1, '-': 1, } # Priority of each operator lowerCamelCase__ : List[str] = len(_lowerCamelCase ) if (len(_lowerCamelCase ) > 7) else 7 # Print table header for output print( 'Symbol'.center(8 ) , 'Stack'.center(_lowerCamelCase ) , 'Postfix'.center(_lowerCamelCase ) , sep=' | ' , ) print('-' * (print_width * 3 + 7) ) for x in infix: if x.isalpha() or x.isdigit(): post_fix.append(_lowerCamelCase ) # if x is Alphabet / Digit, add it to Postfix elif x == "(": stack.append(_lowerCamelCase ) # if x is "(" push to Stack elif x == ")": # if x is ")" pop stack until "(" is encountered while stack[-1] != "(": post_fix.append(stack.pop() ) # Pop stack & add the content to Postfix stack.pop() else: if len(_lowerCamelCase ) == 0: stack.append(_lowerCamelCase ) # If stack is empty, push x to stack else: # while priority of x is not > priority of element in the stack while len(_lowerCamelCase ) > 0 and priority[x] <= priority[stack[-1]]: post_fix.append(stack.pop() ) # pop stack & add to Postfix stack.append(_lowerCamelCase ) # push x to stack print( x.center(8 ) , (''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , (''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , sep=' | ' , ) # Output in tabular format while len(_lowerCamelCase ) > 0: # while stack is not empty post_fix.append(stack.pop() ) # pop stack & add to Postfix print( ' '.center(8 ) , (''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , (''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , sep=' | ' , ) # Output in tabular format return "".join(_lowerCamelCase ) # return Postfix as str def lowerCamelCase_ ( _lowerCamelCase ): lowerCamelCase__ : Union[str, Any] = list(infix[::-1] ) # reverse the infix equation for i in range(len(_lowerCamelCase ) ): if infix[i] == "(": lowerCamelCase__ : List[Any] = ')' # change "(" to ")" elif infix[i] == ")": lowerCamelCase__ : Tuple = '(' # change ")" to "(" return (infix_2_postfix(''.join(_lowerCamelCase ) ))[ ::-1 ] # call infix_2_postfix on Infix, return reverse of Postfix if __name__ == "__main__": A_ : Tuple = input("\nEnter an Infix Equation = ") # Input an Infix equation A_ : List[str] = "".join(Infix.split()) # Remove spaces from the input print("\n\t", Infix, "(Infix) -> ", infix_2_prefix(Infix), "(Prefix)")
316
1
"""simple docstring""" # Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available A_ : List[str] = { "configuration_xmod": [ "XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP", "XmodConfig", "XmodOnnxConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : int = [ "XMOD_PRETRAINED_MODEL_ARCHIVE_LIST", "XmodForCausalLM", "XmodForMaskedLM", "XmodForMultipleChoice", "XmodForQuestionAnswering", "XmodForSequenceClassification", "XmodForTokenClassification", "XmodModel", "XmodPreTrainedModel", ] if TYPE_CHECKING: from .configuration_xmod import XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP, XmodConfig, XmodOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xmod import ( XMOD_PRETRAINED_MODEL_ARCHIVE_LIST, XmodForCausalLM, XmodForMaskedLM, XmodForMultipleChoice, XmodForQuestionAnswering, XmodForSequenceClassification, XmodForTokenClassification, XmodModel, XmodPreTrainedModel, ) else: import sys A_ : Tuple = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
316
"""simple docstring""" import json import os import shutil import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoConfig, BertConfig, GPTaConfig from transformers.configuration_utils import PretrainedConfig from transformers.testing_utils import TOKEN, USER, is_staging_test sys.path.append(str(Path(__file__).parent.parent / "utils")) from test_module.custom_configuration import CustomConfig # noqa E402 A_ : Any = { "return_dict": False, "output_hidden_states": True, "output_attentions": True, "torchscript": True, "torch_dtype": "float16", "use_bfloat16": True, "tf_legacy_loss": True, "pruned_heads": {"a": 1}, "tie_word_embeddings": False, "is_decoder": True, "cross_attention_hidden_size": 1_28, "add_cross_attention": True, "tie_encoder_decoder": True, "max_length": 50, "min_length": 3, "do_sample": True, "early_stopping": True, "num_beams": 3, "num_beam_groups": 3, "diversity_penalty": 0.5, "temperature": 2.0, "top_k": 10, "top_p": 0.7, "typical_p": 0.2, "repetition_penalty": 0.8, "length_penalty": 0.8, "no_repeat_ngram_size": 5, "encoder_no_repeat_ngram_size": 5, "bad_words_ids": [1, 2, 3], "num_return_sequences": 3, "chunk_size_feed_forward": 5, "output_scores": True, "return_dict_in_generate": True, "forced_bos_token_id": 2, "forced_eos_token_id": 3, "remove_invalid_values": True, "architectures": ["BertModel"], "finetuning_task": "translation", "id2label": {0: "label"}, "label2id": {"label": "0"}, "tokenizer_class": "BertTokenizerFast", "prefix": "prefix", "bos_token_id": 6, "pad_token_id": 7, "eos_token_id": 8, "sep_token_id": 9, "decoder_start_token_id": 10, "exponential_decay_length_penalty": (5, 1.01), "suppress_tokens": [0, 1], "begin_suppress_tokens": 2, "task_specific_params": {"translation": "some_params"}, "problem_type": "regression", } @is_staging_test class a_ ( unittest.TestCase ): '''simple docstring''' @classmethod def a__ (cls ): '''simple docstring''' lowerCamelCase__ : Tuple = TOKEN HfFolder.save_token(lowerCamelCase_ ) @classmethod def a__ (cls ): '''simple docstring''' try: delete_repo(token=cls._token, repo_id='test-config' ) except HTTPError: pass try: delete_repo(token=cls._token, repo_id='valid_org/test-config-org' ) except HTTPError: pass try: delete_repo(token=cls._token, repo_id='test-dynamic-config' ) except HTTPError: pass def a__ (self ): '''simple docstring''' lowerCamelCase__ : Optional[int] = BertConfig( vocab_size=9_9, hidden_size=3_2, num_hidden_layers=5, num_attention_heads=4, intermediate_size=3_7 ) config.push_to_hub('test-config', use_auth_token=self._token ) lowerCamelCase__ : List[str] = BertConfig.from_pretrained(f'''{USER}/test-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(lowerCamelCase_, getattr(lowerCamelCase_, lowerCamelCase_ ) ) # Reset repo delete_repo(token=self._token, repo_id='test-config' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(lowerCamelCase_, repo_id='test-config', push_to_hub=lowerCamelCase_, use_auth_token=self._token ) lowerCamelCase__ : List[Any] = BertConfig.from_pretrained(f'''{USER}/test-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(lowerCamelCase_, getattr(lowerCamelCase_, lowerCamelCase_ ) ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : List[Any] = BertConfig( vocab_size=9_9, hidden_size=3_2, num_hidden_layers=5, num_attention_heads=4, intermediate_size=3_7 ) config.push_to_hub('valid_org/test-config-org', use_auth_token=self._token ) lowerCamelCase__ : int = BertConfig.from_pretrained('valid_org/test-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(lowerCamelCase_, getattr(lowerCamelCase_, lowerCamelCase_ ) ) # Reset repo delete_repo(token=self._token, repo_id='valid_org/test-config-org' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( lowerCamelCase_, repo_id='valid_org/test-config-org', push_to_hub=lowerCamelCase_, use_auth_token=self._token ) lowerCamelCase__ : Tuple = BertConfig.from_pretrained('valid_org/test-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(lowerCamelCase_, getattr(lowerCamelCase_, lowerCamelCase_ ) ) def a__ (self ): '''simple docstring''' CustomConfig.register_for_auto_class() lowerCamelCase__ : str = CustomConfig(attribute=4_2 ) config.push_to_hub('test-dynamic-config', use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual(config.auto_map, {'AutoConfig': 'custom_configuration.CustomConfig'} ) lowerCamelCase__ : List[str] = AutoConfig.from_pretrained(f'''{USER}/test-dynamic-config''', trust_remote_code=lowerCamelCase_ ) # Can't make an isinstance check because the new_config is from the FakeConfig class of a dynamic module self.assertEqual(new_config.__class__.__name__, 'CustomConfig' ) self.assertEqual(new_config.attribute, 4_2 ) class a_ ( unittest.TestCase ): '''simple docstring''' def a__ (self ): '''simple docstring''' lowerCamelCase__ : Tuple = GPTaConfig() # attempt to modify each of int/float/bool/str config records and verify they were updated lowerCamelCase__ : Union[str, Any] = c.n_embd + 1 # int lowerCamelCase__ : Optional[Any] = c.resid_pdrop + 1.0 # float lowerCamelCase__ : str = not c.scale_attn_weights # bool lowerCamelCase__ : Any = c.summary_type + 'foo' # str c.update_from_string( f'''n_embd={n_embd},resid_pdrop={resid_pdrop},scale_attn_weights={scale_attn_weights},summary_type={summary_type}''' ) self.assertEqual(lowerCamelCase_, c.n_embd, 'mismatch for key: n_embd' ) self.assertEqual(lowerCamelCase_, c.resid_pdrop, 'mismatch for key: resid_pdrop' ) self.assertEqual(lowerCamelCase_, c.scale_attn_weights, 'mismatch for key: scale_attn_weights' ) self.assertEqual(lowerCamelCase_, c.summary_type, 'mismatch for key: summary_type' ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Any = PretrainedConfig() lowerCamelCase__ : Union[str, Any] = [key for key in base_config.__dict__ if key not in config_common_kwargs] # If this part of the test fails, you have arguments to addin config_common_kwargs above. self.assertListEqual( lowerCamelCase_, ['is_encoder_decoder', '_name_or_path', '_commit_hash', 'transformers_version'] ) lowerCamelCase__ : str = [key for key, value in config_common_kwargs.items() if value == getattr(lowerCamelCase_, lowerCamelCase_ )] if len(lowerCamelCase_ ) > 0: raise ValueError( 'The following keys are set with the default values in' ' `test_configuration_common.config_common_kwargs` pick another value for them:' f''' {', '.join(lowerCamelCase_ )}.''' ) def a__ (self ): '''simple docstring''' with self.assertRaises(lowerCamelCase_ ): # config is in subfolder, the following should not work without specifying the subfolder lowerCamelCase__ : Union[str, Any] = BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert-subfolder' ) lowerCamelCase__ : Any = BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert-subfolder', subfolder='bert' ) self.assertIsNotNone(lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : int = mock.Mock() lowerCamelCase__ : str = 5_0_0 lowerCamelCase__ : Union[str, Any] = {} lowerCamelCase__ : Any = HTTPError lowerCamelCase__ : str = {} # Download this model to make sure it's in the cache. lowerCamelCase__ : Dict = BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert' ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch('requests.Session.request', return_value=lowerCamelCase_ ) as mock_head: lowerCamelCase__ : Union[str, Any] = BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert' ) # This check we did call the fake head request mock_head.assert_called() def a__ (self ): '''simple docstring''' lowerCamelCase__ : int = BertConfig.from_pretrained( 'https://huggingface.co/hf-internal-testing/tiny-random-bert/resolve/main/config.json' ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : str = AutoConfig.from_pretrained('bert-base-cased' ) lowerCamelCase__ : Optional[Any] = ['config.4.0.0.json'] with tempfile.TemporaryDirectory() as tmp_dir: configuration.save_pretrained(lowerCamelCase_ ) lowerCamelCase__ : Tuple = 2 json.dump(configuration.to_dict(), open(os.path.join(lowerCamelCase_, 'config.4.0.0.json' ), 'w' ) ) # This should pick the new configuration file as the version of Transformers is > 4.0.0 lowerCamelCase__ : List[str] = AutoConfig.from_pretrained(lowerCamelCase_ ) self.assertEqual(new_configuration.hidden_size, 2 ) # Will need to be adjusted if we reach v42 and this test is still here. # Should pick the old configuration file as the version of Transformers is < 4.42.0 lowerCamelCase__ : Optional[Any] = ['config.42.0.0.json'] lowerCamelCase__ : List[Any] = 7_6_8 configuration.save_pretrained(lowerCamelCase_ ) shutil.move(os.path.join(lowerCamelCase_, 'config.4.0.0.json' ), os.path.join(lowerCamelCase_, 'config.42.0.0.json' ) ) lowerCamelCase__ : str = AutoConfig.from_pretrained(lowerCamelCase_ ) self.assertEqual(new_configuration.hidden_size, 7_6_8 ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : int = 'hf-internal-testing/test-two-configs' import transformers as new_transformers lowerCamelCase__ : Dict = 'v4.0.0' lowerCamelCase__ , lowerCamelCase__ : str = new_transformers.models.auto.AutoConfig.from_pretrained( lowerCamelCase_, return_unused_kwargs=lowerCamelCase_ ) self.assertEqual(new_configuration.hidden_size, 2 ) # This checks `_configuration_file` ia not kept in the kwargs by mistake. self.assertDictEqual(lowerCamelCase_, {} ) # Testing an older version by monkey-patching the version in the module it's used. import transformers as old_transformers lowerCamelCase__ : Optional[Any] = 'v3.0.0' lowerCamelCase__ : Optional[int] = old_transformers.models.auto.AutoConfig.from_pretrained(lowerCamelCase_ ) self.assertEqual(old_configuration.hidden_size, 7_6_8 )
316
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging A_ : Optional[Any] = logging.get_logger(__name__) A_ : Any = { "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 a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : Dict = 'pegasus' lowerCamelCase__ : Optional[Any] = ['past_key_values'] lowerCamelCase__ : List[str] = {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'} def __init__(self, lowerCamelCase_=5_0_2_6_5, lowerCamelCase_=1_0_2_4, lowerCamelCase_=1_2, lowerCamelCase_=4_0_9_6, lowerCamelCase_=1_6, lowerCamelCase_=1_2, lowerCamelCase_=4_0_9_6, lowerCamelCase_=1_6, lowerCamelCase_=0.0, lowerCamelCase_=0.0, lowerCamelCase_=True, lowerCamelCase_=True, lowerCamelCase_="gelu", lowerCamelCase_=1_0_2_4, lowerCamelCase_=0.1, lowerCamelCase_=0.0, lowerCamelCase_=0.0, lowerCamelCase_=0.02, lowerCamelCase_=0, lowerCamelCase_=False, lowerCamelCase_=0, lowerCamelCase_=1, lowerCamelCase_=1, **lowerCamelCase_, ): '''simple docstring''' lowerCamelCase__ : Any = vocab_size lowerCamelCase__ : str = max_position_embeddings lowerCamelCase__ : Optional[int] = d_model lowerCamelCase__ : Union[str, Any] = encoder_ffn_dim lowerCamelCase__ : List[Any] = encoder_layers lowerCamelCase__ : Union[str, Any] = encoder_attention_heads lowerCamelCase__ : Union[str, Any] = decoder_ffn_dim lowerCamelCase__ : str = decoder_layers lowerCamelCase__ : List[str] = decoder_attention_heads lowerCamelCase__ : Dict = dropout lowerCamelCase__ : Any = attention_dropout lowerCamelCase__ : Tuple = activation_dropout lowerCamelCase__ : Any = activation_function lowerCamelCase__ : int = init_std lowerCamelCase__ : List[str] = encoder_layerdrop lowerCamelCase__ : Union[str, Any] = decoder_layerdrop lowerCamelCase__ : Tuple = use_cache lowerCamelCase__ : Any = encoder_layers lowerCamelCase__ : 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 a__ (self ): '''simple docstring''' return self.encoder_attention_heads @property def a__ (self ): '''simple docstring''' return self.d_model
316
"""simple docstring""" from __future__ import annotations def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : list[list[int]] = [] lowerCamelCase__ : list[int] = [] lowerCamelCase__ : List[str] = 0 lowerCamelCase__ : List[Any] = sum(_lowerCamelCase ) create_state_space_tree(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) return result def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , ): if sum(_lowerCamelCase ) > max_sum or (remaining_nums_sum + sum(_lowerCamelCase )) < max_sum: return if sum(_lowerCamelCase ) == max_sum: result.append(_lowerCamelCase ) return for index in range(_lowerCamelCase , len(_lowerCamelCase ) ): create_state_space_tree( _lowerCamelCase , _lowerCamelCase , index + 1 , [*path, nums[index]] , _lowerCamelCase , remaining_nums_sum - nums[index] , ) A_ : Optional[Any] = [3, 34, 4, 12, 5, 2] A_ : List[str] = 9 A_ : List[Any] = generate_sum_of_subsets_soln(nums, max_sum) print(*result)
316
1
"""simple docstring""" # this script reports modified .py files under the desired list of top-level sub-dirs passed as a list of arguments, e.g.: # python ./utils/get_modified_files.py utils src tests examples # # it uses git to find the forking point and which files were modified - i.e. files not under git won't be considered # since the output of this script is fed into Makefile commands it doesn't print a newline after the results import re import subprocess import sys A_ : str = subprocess.check_output("git merge-base main HEAD".split()).decode("utf-8") A_ : List[Any] = subprocess.check_output(f"git diff --name-only {fork_point_sha}".split()).decode("utf-8").split() A_ : List[Any] = "|".join(sys.argv[1:]) A_ : List[str] = re.compile(rf"^({joined_dirs}).*?\.py$") A_ : List[Any] = [x for x in modified_files if regex.match(x)] print(" ".join(relevant_modified_files), end="")
316
"""simple docstring""" from __future__ import annotations import queue class a_ : '''simple docstring''' def __init__(self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = data lowerCamelCase__ : Optional[int] = None lowerCamelCase__ : List[Any] = None def lowerCamelCase_ ( ): print('\n********Press N to stop entering at any point of time********\n' ) lowerCamelCase__ : str = input('Enter the value of the root node: ' ).strip().lower() lowerCamelCase__ : queue.Queue = queue.Queue() lowerCamelCase__ : Optional[Any] = TreeNode(int(_lowerCamelCase ) ) q.put(_lowerCamelCase ) while not q.empty(): lowerCamelCase__ : List[Any] = q.get() lowerCamelCase__ : str = f'''Enter the left node of {node_found.data}: ''' lowerCamelCase__ : Dict = input(_lowerCamelCase ).strip().lower() or 'n' if check == "n": return tree_node lowerCamelCase__ : str = TreeNode(int(_lowerCamelCase ) ) lowerCamelCase__ : Dict = left_node q.put(_lowerCamelCase ) lowerCamelCase__ : List[str] = f'''Enter the right node of {node_found.data}: ''' lowerCamelCase__ : List[str] = input(_lowerCamelCase ).strip().lower() or 'n' if check == "n": return tree_node lowerCamelCase__ : Optional[int] = TreeNode(int(_lowerCamelCase ) ) lowerCamelCase__ : Any = right_node q.put(_lowerCamelCase ) raise def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return print(node.data , end=',' ) pre_order(node.left ) pre_order(node.right ) def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return in_order(node.left ) print(node.data , end=',' ) in_order(node.right ) def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return post_order(node.left ) post_order(node.right ) print(node.data , end=',' ) def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return lowerCamelCase__ : queue.Queue = queue.Queue() q.put(_lowerCamelCase ) while not q.empty(): lowerCamelCase__ : Any = q.get() print(node_dequeued.data , end=',' ) if node_dequeued.left: q.put(node_dequeued.left ) if node_dequeued.right: q.put(node_dequeued.right ) def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return lowerCamelCase__ : queue.Queue = queue.Queue() q.put(_lowerCamelCase ) while not q.empty(): lowerCamelCase__ : List[Any] = [] while not q.empty(): lowerCamelCase__ : str = q.get() print(node_dequeued.data , end=',' ) if node_dequeued.left: list_.append(node_dequeued.left ) if node_dequeued.right: list_.append(node_dequeued.right ) print() for node in list_: q.put(_lowerCamelCase ) def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return lowerCamelCase__ : list[TreeNode] = [] lowerCamelCase__ : int = node while n or stack: while n: # start from root node, find its left child print(n.data , end=',' ) stack.append(_lowerCamelCase ) lowerCamelCase__ : Union[str, Any] = n.left # end of while means current node doesn't have left child lowerCamelCase__ : List[Any] = stack.pop() # start to traverse its right child lowerCamelCase__ : Optional[Any] = n.right def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return lowerCamelCase__ : list[TreeNode] = [] lowerCamelCase__ : int = node while n or stack: while n: stack.append(_lowerCamelCase ) lowerCamelCase__ : List[str] = n.left lowerCamelCase__ : Tuple = stack.pop() print(n.data , end=',' ) lowerCamelCase__ : Union[str, Any] = n.right def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return lowerCamelCase__ , lowerCamelCase__ : Any = [], [] lowerCamelCase__ : int = node stacka.append(_lowerCamelCase ) while stacka: # to find the reversed order of post order, store it in stack2 lowerCamelCase__ : List[str] = stacka.pop() if n.left: stacka.append(n.left ) if n.right: stacka.append(n.right ) stacka.append(_lowerCamelCase ) while stacka: # pop up from stack2 will be the post order print(stacka.pop().data , end=',' ) def lowerCamelCase_ ( _lowerCamelCase = "" , _lowerCamelCase=50 , _lowerCamelCase="*" ): if not s: return "\n" + width * char lowerCamelCase__ , lowerCamelCase__ : Dict = divmod(width - len(_lowerCamelCase ) - 2 , 2 ) return f'''{left * char} {s} {(left + extra) * char}''' if __name__ == "__main__": import doctest doctest.testmod() print(prompt("Binary Tree Traversals")) A_ : TreeNode = build_tree() print(prompt("Pre Order Traversal")) pre_order(node) print(prompt() + "\n") print(prompt("In Order Traversal")) in_order(node) print(prompt() + "\n") print(prompt("Post Order Traversal")) post_order(node) print(prompt() + "\n") print(prompt("Level Order Traversal")) level_order(node) print(prompt() + "\n") print(prompt("Actual Level Order Traversal")) level_order_actual(node) print("*" * 50 + "\n") print(prompt("Pre Order Traversal - Iteration Version")) pre_order_iter(node) print(prompt() + "\n") print(prompt("In Order Traversal - Iteration Version")) in_order_iter(node) print(prompt() + "\n") print(prompt("Post Order Traversal - Iteration Version")) post_order_iter(node) print(prompt())
316
1
"""simple docstring""" import os from typing import Optional import fsspec from fsspec.archive import AbstractArchiveFileSystem from fsspec.utils import DEFAULT_BLOCK_SIZE class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : Dict = '' lowerCamelCase__ : str = ( None # protocol passed in prefix to the url. ex: "gzip", for gzip://file.txt::http://foo.bar/file.txt.gz ) lowerCamelCase__ : str = None # compression type in fsspec. ex: "gzip" lowerCamelCase__ : str = None # extension of the filename to strip. ex: "".gz" to get file.txt from file.txt.gz def __init__(self, lowerCamelCase_ = "", lowerCamelCase_ = None, lowerCamelCase_ = None, **lowerCamelCase_ ): '''simple docstring''' super().__init__(self, **lowerCamelCase_ ) # always open as "rb" since fsspec can then use the TextIOWrapper to make it work for "r" mode lowerCamelCase__ : Optional[Any] = fsspec.open( lowerCamelCase_, mode='rb', protocol=lowerCamelCase_, 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 {}), ) lowerCamelCase__ : int = os.path.basename(self.file.path.split('::' )[0] ) lowerCamelCase__ : str = ( self.compressed_name[: self.compressed_name.rindex('.' )] if '.' in self.compressed_name else self.compressed_name ) lowerCamelCase__ : List[Any] = None @classmethod def a__ (cls, lowerCamelCase_ ): '''simple docstring''' return super()._strip_protocol(lowerCamelCase_ ).lstrip('/' ) def a__ (self ): '''simple docstring''' if self.dir_cache is None: lowerCamelCase__ : Any = {**self.file.fs.info(self.file.path ), 'name': self.uncompressed_name} lowerCamelCase__ : int = {f['name']: f} def a__ (self, lowerCamelCase_ ): '''simple docstring''' return self.file.open().read() def a__ (self, lowerCamelCase_, lowerCamelCase_ = "rb", lowerCamelCase_=None, lowerCamelCase_=True, lowerCamelCase_=None, **lowerCamelCase_, ): '''simple docstring''' lowerCamelCase__ : Dict = self._strip_protocol(lowerCamelCase_ ) 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_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : Optional[int] = 'bz2' lowerCamelCase__ : List[str] = 'bz2' lowerCamelCase__ : int = '.bz2' class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = 'gzip' lowerCamelCase__ : List[str] = 'gzip' lowerCamelCase__ : Union[str, Any] = '.gz' class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : Tuple = 'lz4' lowerCamelCase__ : int = 'lz4' lowerCamelCase__ : Dict = '.lz4' class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : Tuple = 'xz' lowerCamelCase__ : Optional[int] = 'xz' lowerCamelCase__ : Dict = '.xz' class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : List[str] = 'zstd' lowerCamelCase__ : Dict = 'zstd' lowerCamelCase__ : List[Any] = '.zst' def __init__(self, lowerCamelCase_, lowerCamelCase_ = "rb", lowerCamelCase_ = None, lowerCamelCase_ = None, lowerCamelCase_ = DEFAULT_BLOCK_SIZE, **lowerCamelCase_, ): '''simple docstring''' super().__init__( fo=lowerCamelCase_, mode=lowerCamelCase_, target_protocol=lowerCamelCase_, target_options=lowerCamelCase_, block_size=lowerCamelCase_, **lowerCamelCase_, ) # 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 lowerCamelCase__ : List[str] = self.file.__enter__ class a_ : '''simple docstring''' def __init__(self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Optional[int] = file_ def __enter__(self ): '''simple docstring''' self._file.__enter__() return self def __exit__(self, *lowerCamelCase_, **lowerCamelCase_ ): '''simple docstring''' self._file.__exit__(*lowerCamelCase_, **lowerCamelCase_ ) def __iter__(self ): '''simple docstring''' return iter(self._file ) def a__ (self ): '''simple docstring''' return next(self._file ) def __getattr__(self, lowerCamelCase_ ): '''simple docstring''' return getattr(self._file, lowerCamelCase_ ) def fixed_enter(*lowerCamelCase_, **lowerCamelCase_ ): return WrappedFile(_enter(*lowerCamelCase_, **lowerCamelCase_ ) ) lowerCamelCase__ : Tuple = fixed_enter
316
"""simple docstring""" # Note: if you intend to run this script make sure you look under scripts/fsmt/ # to locate the appropriate script to do the work correctly. There is a set of scripts to: # - download and prepare data and run the conversion script # - perform eval to get the best hparam into the config # - generate model_cards - useful if you have multiple models from the same paper import argparse import json import os import re from collections import OrderedDict from os.path import basename, dirname import fairseq import torch from fairseq import hub_utils from fairseq.data.dictionary import Dictionary from transformers import FSMTConfig, FSMTForConditionalGeneration from transformers.models.fsmt.tokenization_fsmt import VOCAB_FILES_NAMES from transformers.tokenization_utils_base import TOKENIZER_CONFIG_FILE from transformers.utils import WEIGHTS_NAME, logging logging.set_verbosity_warning() A_ : Optional[Any] = 2 # based on the results of a search on a range of `num_beams`, `length_penalty` and `early_stopping` # values against wmt19 test data to obtain the best BLEU scores, we will use the following defaults: # # * `num_beams`: 5 (higher scores better, but requires more memory/is slower, can be adjusted by users) # * `early_stopping`: `False` consistently scored better # * `length_penalty` varied, so will assign the best one depending on the model A_ : List[Any] = { # fairseq: "wmt19-ru-en": {"length_penalty": 1.1}, "wmt19-en-ru": {"length_penalty": 1.15}, "wmt19-en-de": {"length_penalty": 1.0}, "wmt19-de-en": {"length_penalty": 1.1}, # allenai: "wmt16-en-de-dist-12-1": {"length_penalty": 0.6}, "wmt16-en-de-dist-6-1": {"length_penalty": 0.6}, "wmt16-en-de-12-1": {"length_penalty": 0.8}, "wmt19-de-en-6-6-base": {"length_penalty": 0.6}, "wmt19-de-en-6-6-big": {"length_penalty": 0.6}, } # this remaps the different models to their organization names A_ : str = {} for m in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: A_ : str = "facebook" for m in [ "wmt16-en-de-dist-12-1", "wmt16-en-de-dist-6-1", "wmt16-en-de-12-1", "wmt19-de-en-6-6-base", "wmt19-de-en-6-6-big", ]: A_ : Optional[Any] = "allenai" def lowerCamelCase_ ( _lowerCamelCase ): # (1) remove word breaking symbol, (2) add word ending symbol where the word is not broken up, # e.g.: d = {'le@@': 5, 'tt@@': 6, 'er': 7} => {'le': 5, 'tt': 6, 'er</w>': 7} lowerCamelCase__ : List[Any] = dict((re.sub(r'@@$' , '' , _lowerCamelCase ), v) if k.endswith('@@' ) else (re.sub(r'$' , '</w>' , _lowerCamelCase ), v) for k, v in d.items() ) lowerCamelCase__ : int = '<s> <pad> </s> <unk>'.split() # restore the special tokens for k in keep_keys: del da[f'''{k}</w>'''] lowerCamelCase__ : List[str] = d[k] # restore return da def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): # prep assert os.path.exists(_lowerCamelCase ) os.makedirs(_lowerCamelCase , exist_ok=_lowerCamelCase ) print(f'''Writing results to {pytorch_dump_folder_path}''' ) # handle various types of models lowerCamelCase__ : Optional[int] = basename(_lowerCamelCase ) lowerCamelCase__ : str = dirname(_lowerCamelCase ) lowerCamelCase__ : Any = fairseq.model_parallel.models.transformer.ModelParallelTransformerModel lowerCamelCase__ : int = cls.hub_models() lowerCamelCase__ : str = {'bpe': 'fastbpe', 'tokenizer': 'moses'} lowerCamelCase__ : Optional[Any] = '.' # note: since the model dump is old, fairseq has upgraded its model some # time later, and it does a whole lot of rewrites and splits on the saved # weights, therefore we can't use torch.load() directly on the model file. # see: upgrade_state_dict(state_dict) in fairseq_model.py print(f'''using checkpoint {checkpoint_file}''' ) lowerCamelCase__ : Any = hub_utils.from_pretrained( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , archive_map=_lowerCamelCase , **_lowerCamelCase ) lowerCamelCase__ : List[str] = vars(chkpt['args']['model'] ) lowerCamelCase__ : Optional[Any] = args['source_lang'] lowerCamelCase__ : List[str] = args['target_lang'] lowerCamelCase__ : List[str] = dirname(_lowerCamelCase ) lowerCamelCase__ : Union[str, Any] = basename(_lowerCamelCase ) # dicts lowerCamelCase__ : Optional[Any] = os.path.join(_lowerCamelCase , f'''dict.{src_lang}.txt''' ) lowerCamelCase__ : Optional[Any] = os.path.join(_lowerCamelCase , f'''dict.{tgt_lang}.txt''' ) lowerCamelCase__ : Dict = Dictionary.load(_lowerCamelCase ) lowerCamelCase__ : List[Any] = rewrite_dict_keys(src_dict.indices ) lowerCamelCase__ : int = len(_lowerCamelCase ) lowerCamelCase__ : List[Any] = os.path.join(_lowerCamelCase , 'vocab-src.json' ) print(f'''Generating {src_vocab_file} of {src_vocab_size} of {src_lang} records''' ) with open(_lowerCamelCase , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(_lowerCamelCase , ensure_ascii=_lowerCamelCase , indent=_lowerCamelCase ) ) # detect whether this is a do_lower_case situation, which can be derived by checking whether we # have at least one uppercase letter in the source vocab lowerCamelCase__ : Optional[int] = True for k in src_vocab.keys(): if not k.islower(): lowerCamelCase__ : int = False break lowerCamelCase__ : str = Dictionary.load(_lowerCamelCase ) lowerCamelCase__ : Union[str, Any] = rewrite_dict_keys(tgt_dict.indices ) lowerCamelCase__ : Optional[Any] = len(_lowerCamelCase ) lowerCamelCase__ : Dict = os.path.join(_lowerCamelCase , 'vocab-tgt.json' ) print(f'''Generating {tgt_vocab_file} of {tgt_vocab_size} of {tgt_lang} records''' ) with open(_lowerCamelCase , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(_lowerCamelCase , ensure_ascii=_lowerCamelCase , indent=_lowerCamelCase ) ) # merges_file (bpecodes) lowerCamelCase__ : List[Any] = os.path.join(_lowerCamelCase , VOCAB_FILES_NAMES['merges_file'] ) for fn in ["bpecodes", "code"]: # older fairseq called the merges file "code" lowerCamelCase__ : Optional[int] = os.path.join(_lowerCamelCase , _lowerCamelCase ) if os.path.exists(_lowerCamelCase ): break with open(_lowerCamelCase , encoding='utf-8' ) as fin: lowerCamelCase__ : Union[str, Any] = fin.read() lowerCamelCase__ : Any = re.sub(r' \d+$' , '' , _lowerCamelCase , 0 , re.M ) # remove frequency number print(f'''Generating {merges_file}''' ) with open(_lowerCamelCase , 'w' , encoding='utf-8' ) as fout: fout.write(_lowerCamelCase ) # model config lowerCamelCase__ : Dict = os.path.join(_lowerCamelCase , 'config.json' ) # validate bpe/tokenizer config, as currently it's hardcoded to moses+fastbpe - # may have to modify the tokenizer if a different type is used by a future model assert args["bpe"] == "fastbpe", f'''need to extend tokenizer to support bpe={args['bpe']}''' assert args["tokenizer"] == "moses", f'''need to extend tokenizer to support bpe={args['tokenizer']}''' lowerCamelCase__ : Optional[int] = { 'architectures': ['FSMTForConditionalGeneration'], 'model_type': 'fsmt', 'activation_dropout': args['activation_dropout'], 'activation_function': 'relu', 'attention_dropout': args['attention_dropout'], 'd_model': args['decoder_embed_dim'], 'dropout': args['dropout'], 'init_std': 0.02, 'max_position_embeddings': args['max_source_positions'], 'num_hidden_layers': args['encoder_layers'], 'src_vocab_size': src_vocab_size, 'tgt_vocab_size': tgt_vocab_size, 'langs': [src_lang, tgt_lang], 'encoder_attention_heads': args['encoder_attention_heads'], 'encoder_ffn_dim': args['encoder_ffn_embed_dim'], 'encoder_layerdrop': args['encoder_layerdrop'], 'encoder_layers': args['encoder_layers'], 'decoder_attention_heads': args['decoder_attention_heads'], 'decoder_ffn_dim': args['decoder_ffn_embed_dim'], 'decoder_layerdrop': args['decoder_layerdrop'], 'decoder_layers': args['decoder_layers'], 'bos_token_id': 0, 'pad_token_id': 1, 'eos_token_id': 2, 'is_encoder_decoder': True, 'scale_embedding': not args['no_scale_embedding'], 'tie_word_embeddings': args['share_all_embeddings'], } # good hparam defaults to start with lowerCamelCase__ : str = 5 lowerCamelCase__ : Tuple = False if model_dir in best_score_hparams and "length_penalty" in best_score_hparams[model_dir]: lowerCamelCase__ : List[str] = best_score_hparams[model_dir]['length_penalty'] else: lowerCamelCase__ : List[Any] = 1.0 print(f'''Generating {fsmt_model_config_file}''' ) with open(_lowerCamelCase , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(_lowerCamelCase , ensure_ascii=_lowerCamelCase , indent=_lowerCamelCase ) ) # tokenizer config lowerCamelCase__ : Dict = os.path.join(_lowerCamelCase , _lowerCamelCase ) lowerCamelCase__ : int = { 'langs': [src_lang, tgt_lang], 'model_max_length': 1024, 'do_lower_case': do_lower_case, } print(f'''Generating {fsmt_tokenizer_config_file}''' ) with open(_lowerCamelCase , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(_lowerCamelCase , ensure_ascii=_lowerCamelCase , indent=_lowerCamelCase ) ) # model lowerCamelCase__ : List[str] = chkpt['models'][0] lowerCamelCase__ : Optional[Any] = model.state_dict() # rename keys to start with 'model.' lowerCamelCase__ : str = OrderedDict(('model.' + k, v) for k, v in model_state_dict.items() ) # remove unneeded keys lowerCamelCase__ : int = [ 'model.model', 'model.encoder.version', 'model.decoder.version', 'model.encoder_embed_tokens.weight', 'model.decoder_embed_tokens.weight', 'model.encoder.embed_positions._float_tensor', 'model.decoder.embed_positions._float_tensor', ] for k in ignore_keys: model_state_dict.pop(_lowerCamelCase , _lowerCamelCase ) lowerCamelCase__ : Any = FSMTConfig.from_pretrained(_lowerCamelCase ) lowerCamelCase__ : Union[str, Any] = FSMTForConditionalGeneration(_lowerCamelCase ) # check that it loads ok model_new.load_state_dict(_lowerCamelCase , strict=_lowerCamelCase ) # save lowerCamelCase__ : List[Any] = os.path.join(_lowerCamelCase , _lowerCamelCase ) print(f'''Generating {pytorch_weights_dump_path}''' ) torch.save(_lowerCamelCase , _lowerCamelCase ) print('Conversion is done!' ) print('\nLast step is to upload the files to s3' ) print(f'''cd {data_root}''' ) print(f'''transformers-cli upload {model_dir}''' ) if __name__ == "__main__": A_ : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--fsmt_checkpoint_path", default=None, type=str, required=True, help=( "Path to the official PyTorch checkpoint file which is expected to reside in the dump dir with dicts," " bpecodes, etc." ), ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) A_ : Dict = parser.parse_args() convert_fsmt_checkpoint_to_pytorch(args.fsmt_checkpoint_path, args.pytorch_dump_folder_path)
316
1
"""simple docstring""" import logging import os from dataclasses import dataclass from enum import Enum from typing import List, Optional, Union from filelock import FileLock from transformers import PreTrainedTokenizer, is_tf_available, is_torch_available A_ : List[str] = logging.getLogger(__name__) @dataclass class a_ : '''simple docstring''' lowerCamelCase__ : str lowerCamelCase__ : List[str] lowerCamelCase__ : Optional[List[str]] @dataclass class a_ : '''simple docstring''' lowerCamelCase__ : List[int] lowerCamelCase__ : List[int] lowerCamelCase__ : Optional[List[int]] = None lowerCamelCase__ : Optional[List[int]] = None class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : List[str] = 'train' lowerCamelCase__ : List[str] = 'dev' lowerCamelCase__ : Optional[Any] = 'test' class a_ : '''simple docstring''' @staticmethod def a__ (lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' raise NotImplementedError @staticmethod def a__ (lowerCamelCase_ ): '''simple docstring''' raise NotImplementedError @staticmethod def a__ (lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_=False, lowerCamelCase_="[CLS]", lowerCamelCase_=1, lowerCamelCase_="[SEP]", lowerCamelCase_=False, lowerCamelCase_=False, lowerCamelCase_=0, lowerCamelCase_=0, lowerCamelCase_=-1_0_0, lowerCamelCase_=0, lowerCamelCase_=True, ): '''simple docstring''' lowerCamelCase__ : Optional[int] = {label: i for i, label in enumerate(lowerCamelCase_ )} lowerCamelCase__ : Any = [] for ex_index, example in enumerate(lowerCamelCase_ ): if ex_index % 1_0_0_0_0 == 0: logger.info('Writing example %d of %d', lowerCamelCase_, len(lowerCamelCase_ ) ) lowerCamelCase__ : Dict = [] lowerCamelCase__ : str = [] for word, label in zip(example.words, example.labels ): lowerCamelCase__ : List[str] = tokenizer.tokenize(lowerCamelCase_ ) # bert-base-multilingual-cased sometimes output "nothing ([]) when calling tokenize with just a space. if len(lowerCamelCase_ ) > 0: tokens.extend(lowerCamelCase_ ) # Use the real label id for the first token of the word, and padding ids for the remaining tokens label_ids.extend([label_map[label]] + [pad_token_label_id] * (len(lowerCamelCase_ ) - 1) ) # Account for [CLS] and [SEP] with "- 2" and with "- 3" for RoBERTa. lowerCamelCase__ : Optional[Any] = tokenizer.num_special_tokens_to_add() if len(lowerCamelCase_ ) > max_seq_length - special_tokens_count: lowerCamelCase__ : Optional[Any] = tokens[: (max_seq_length - special_tokens_count)] lowerCamelCase__ : Optional[Any] = label_ids[: (max_seq_length - special_tokens_count)] # The convention in BERT is: # (a) For sequence pairs: # tokens: [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP] # type_ids: 0 0 0 0 0 0 0 0 1 1 1 1 1 1 # (b) For single sequences: # tokens: [CLS] the dog is hairy . [SEP] # type_ids: 0 0 0 0 0 0 0 # # Where "type_ids" are used to indicate whether this is the first # sequence or the second sequence. The embedding vectors for `type=0` and # `type=1` were learned during pre-training and are added to the wordpiece # embedding vector (and position vector). This is not *strictly* necessary # since the [SEP] token unambiguously separates the sequences, but it makes # it easier for the model to learn the concept of sequences. # # For classification tasks, the first vector (corresponding to [CLS]) is # used as the "sentence vector". Note that this only makes sense because # the entire model is fine-tuned. tokens += [sep_token] label_ids += [pad_token_label_id] if sep_token_extra: # roberta uses an extra separator b/w pairs of sentences tokens += [sep_token] label_ids += [pad_token_label_id] lowerCamelCase__ : Optional[int] = [sequence_a_segment_id] * len(lowerCamelCase_ ) if cls_token_at_end: tokens += [cls_token] label_ids += [pad_token_label_id] segment_ids += [cls_token_segment_id] else: lowerCamelCase__ : Union[str, Any] = [cls_token] + tokens lowerCamelCase__ : Optional[Any] = [pad_token_label_id] + label_ids lowerCamelCase__ : Optional[Any] = [cls_token_segment_id] + segment_ids lowerCamelCase__ : Optional[Any] = tokenizer.convert_tokens_to_ids(lowerCamelCase_ ) # The mask has 1 for real tokens and 0 for padding tokens. Only real # tokens are attended to. lowerCamelCase__ : int = [1 if mask_padding_with_zero else 0] * len(lowerCamelCase_ ) # Zero-pad up to the sequence length. lowerCamelCase__ : List[Any] = max_seq_length - len(lowerCamelCase_ ) if pad_on_left: lowerCamelCase__ : Optional[Any] = ([pad_token] * padding_length) + input_ids lowerCamelCase__ : str = ([0 if mask_padding_with_zero else 1] * padding_length) + input_mask lowerCamelCase__ : Any = ([pad_token_segment_id] * padding_length) + segment_ids lowerCamelCase__ : List[str] = ([pad_token_label_id] * padding_length) + label_ids else: input_ids += [pad_token] * padding_length input_mask += [0 if mask_padding_with_zero else 1] * padding_length segment_ids += [pad_token_segment_id] * padding_length label_ids += [pad_token_label_id] * padding_length assert len(lowerCamelCase_ ) == max_seq_length assert len(lowerCamelCase_ ) == max_seq_length assert len(lowerCamelCase_ ) == max_seq_length assert len(lowerCamelCase_ ) == max_seq_length if ex_index < 5: logger.info('*** Example ***' ) logger.info('guid: %s', example.guid ) logger.info('tokens: %s', ' '.join([str(lowerCamelCase_ ) for x in tokens] ) ) logger.info('input_ids: %s', ' '.join([str(lowerCamelCase_ ) for x in input_ids] ) ) logger.info('input_mask: %s', ' '.join([str(lowerCamelCase_ ) for x in input_mask] ) ) logger.info('segment_ids: %s', ' '.join([str(lowerCamelCase_ ) for x in segment_ids] ) ) logger.info('label_ids: %s', ' '.join([str(lowerCamelCase_ ) for x in label_ids] ) ) if "token_type_ids" not in tokenizer.model_input_names: lowerCamelCase__ : Optional[int] = None features.append( InputFeatures( input_ids=lowerCamelCase_, attention_mask=lowerCamelCase_, token_type_ids=lowerCamelCase_, label_ids=lowerCamelCase_ ) ) return features if is_torch_available(): import torch from torch import nn from torch.utils.data import Dataset class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : List[InputFeatures] lowerCamelCase__ : int = nn.CrossEntropyLoss().ignore_index def __init__(self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ = None, lowerCamelCase_=False, lowerCamelCase_ = Split.train, ): '''simple docstring''' lowerCamelCase__ : Optional[int] = os.path.join( lowerCamelCase_, 'cached_{}_{}_{}'.format(mode.value, tokenizer.__class__.__name__, str(lowerCamelCase_ ) ), ) # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. lowerCamelCase__ : Union[str, Any] = cached_features_file + '.lock' with FileLock(lowerCamelCase_ ): if os.path.exists(lowerCamelCase_ ) and not overwrite_cache: logger.info(f'''Loading features from cached file {cached_features_file}''' ) lowerCamelCase__ : Optional[Any] = torch.load(lowerCamelCase_ ) else: logger.info(f'''Creating features from dataset file at {data_dir}''' ) lowerCamelCase__ : Optional[int] = token_classification_task.read_examples_from_file(lowerCamelCase_, lowerCamelCase_ ) # TODO clean up all this to leverage built-in features of tokenizers lowerCamelCase__ : Tuple = token_classification_task.convert_examples_to_features( lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, cls_token_at_end=bool(model_type in ['xlnet'] ), cls_token=tokenizer.cls_token, cls_token_segment_id=2 if model_type in ['xlnet'] else 0, sep_token=tokenizer.sep_token, sep_token_extra=lowerCamelCase_, pad_on_left=bool(tokenizer.padding_side == 'left' ), pad_token=tokenizer.pad_token_id, pad_token_segment_id=tokenizer.pad_token_type_id, pad_token_label_id=self.pad_token_label_id, ) logger.info(f'''Saving features into cached file {cached_features_file}''' ) torch.save(self.features, lowerCamelCase_ ) def __len__(self ): '''simple docstring''' return len(self.features ) def __getitem__(self, lowerCamelCase_ ): '''simple docstring''' return self.features[i] if is_tf_available(): import tensorflow as tf class a_ : '''simple docstring''' lowerCamelCase__ : List[InputFeatures] lowerCamelCase__ : int = -100 def __init__(self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ = None, lowerCamelCase_=False, lowerCamelCase_ = Split.train, ): '''simple docstring''' lowerCamelCase__ : Dict = token_classification_task.read_examples_from_file(lowerCamelCase_, lowerCamelCase_ ) # TODO clean up all this to leverage built-in features of tokenizers lowerCamelCase__ : List[str] = token_classification_task.convert_examples_to_features( lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, cls_token_at_end=bool(model_type in ['xlnet'] ), cls_token=tokenizer.cls_token, cls_token_segment_id=2 if model_type in ['xlnet'] else 0, sep_token=tokenizer.sep_token, sep_token_extra=lowerCamelCase_, pad_on_left=bool(tokenizer.padding_side == 'left' ), pad_token=tokenizer.pad_token_id, pad_token_segment_id=tokenizer.pad_token_type_id, pad_token_label_id=self.pad_token_label_id, ) def gen(): for ex in self.features: if ex.token_type_ids is None: yield ( {"input_ids": ex.input_ids, "attention_mask": ex.attention_mask}, ex.label_ids, ) else: yield ( { "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label_ids, ) if "token_type_ids" not in tokenizer.model_input_names: lowerCamelCase__ : int = tf.data.Dataset.from_generator( lowerCamelCase_, ({'input_ids': tf.intaa, 'attention_mask': tf.intaa}, tf.intaa), ( {'input_ids': tf.TensorShape([None] ), 'attention_mask': tf.TensorShape([None] )}, tf.TensorShape([None] ), ), ) else: lowerCamelCase__ : List[Any] = tf.data.Dataset.from_generator( lowerCamelCase_, ({'input_ids': tf.intaa, 'attention_mask': tf.intaa, 'token_type_ids': tf.intaa}, tf.intaa), ( { 'input_ids': tf.TensorShape([None] ), 'attention_mask': tf.TensorShape([None] ), 'token_type_ids': tf.TensorShape([None] ), }, tf.TensorShape([None] ), ), ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : List[str] = self.dataset.apply(tf.data.experimental.assert_cardinality(len(self.features ) ) ) return self.dataset def __len__(self ): '''simple docstring''' return len(self.features ) def __getitem__(self, lowerCamelCase_ ): '''simple docstring''' return self.features[i]
316
"""simple docstring""" from abc import ABC, abstractmethod from argparse import ArgumentParser class a_ ( snake_case_ ): '''simple docstring''' @staticmethod @abstractmethod def a__ (lowerCamelCase_ ): '''simple docstring''' raise NotImplementedError() @abstractmethod def a__ (self ): '''simple docstring''' raise NotImplementedError()
316
1
"""simple docstring""" from __future__ import annotations def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase = None , _lowerCamelCase = None , _lowerCamelCase = False , ): lowerCamelCase__ : List[str] = cipher_alphabet or [chr(_lowerCamelCase ) for i in range(97 , 123 )] # If the argument is None or the user provided an empty dictionary if not frequencies_dict: # Frequencies of letters in the english language (how much they show up) lowerCamelCase__ : Any = { 'a': 0.08_497, 'b': 0.01_492, 'c': 0.02_202, 'd': 0.04_253, 'e': 0.11_162, 'f': 0.02_228, 'g': 0.02_015, 'h': 0.06_094, 'i': 0.07_546, 'j': 0.00_153, 'k': 0.01_292, 'l': 0.04_025, 'm': 0.02_406, 'n': 0.06_749, 'o': 0.07_507, 'p': 0.01_929, 'q': 0.00_095, 'r': 0.07_587, 's': 0.06_327, 't': 0.09_356, 'u': 0.02_758, 'v': 0.00_978, 'w': 0.02_560, 'x': 0.00_150, 'y': 0.01_994, 'z': 0.00_077, } else: # Custom frequencies dictionary lowerCamelCase__ : Union[str, Any] = frequencies_dict if not case_sensitive: lowerCamelCase__ : Any = ciphertext.lower() # Chi squared statistic values lowerCamelCase__ : dict[int, tuple[float, str]] = {} # cycle through all of the shifts for shift in range(len(_lowerCamelCase ) ): lowerCamelCase__ : List[Any] = '' # decrypt the message with the shift for letter in ciphertext: try: # Try to index the letter in the alphabet lowerCamelCase__ : List[str] = (alphabet_letters.index(letter.lower() ) - shift) % len( _lowerCamelCase ) decrypted_with_shift += ( alphabet_letters[new_key].upper() if case_sensitive and letter.isupper() else alphabet_letters[new_key] ) except ValueError: # Append the character if it isn't in the alphabet decrypted_with_shift += letter lowerCamelCase__ : int = 0.0 # Loop through each letter in the decoded message with the shift for letter in decrypted_with_shift: if case_sensitive: lowerCamelCase__ : Union[str, Any] = letter.lower() if letter in frequencies: # Get the amount of times the letter occurs in the message lowerCamelCase__ : Optional[int] = decrypted_with_shift.lower().count(_lowerCamelCase ) # Get the excepcted amount of times the letter should appear based # on letter frequencies lowerCamelCase__ : Tuple = frequencies[letter] * occurrences # Complete the chi squared statistic formula lowerCamelCase__ : Optional[int] = ((occurrences - expected) ** 2) / expected # Add the margin of error to the total chi squared statistic chi_squared_statistic += chi_letter_value else: if letter.lower() in frequencies: # Get the amount of times the letter occurs in the message lowerCamelCase__ : Union[str, Any] = decrypted_with_shift.count(_lowerCamelCase ) # Get the excepcted amount of times the letter should appear based # on letter frequencies lowerCamelCase__ : Optional[int] = frequencies[letter] * occurrences # Complete the chi squared statistic formula lowerCamelCase__ : Optional[int] = ((occurrences - expected) ** 2) / expected # Add the margin of error to the total chi squared statistic chi_squared_statistic += chi_letter_value # Add the data to the chi_squared_statistic_values dictionary lowerCamelCase__ : Optional[Any] = ( chi_squared_statistic, decrypted_with_shift, ) # Get the most likely cipher by finding the cipher with the smallest chi squared # statistic def chi_squared_statistic_values_sorting_key(_lowerCamelCase ) -> tuple[float, str]: return chi_squared_statistic_values[key] lowerCamelCase__ : int = min( _lowerCamelCase , key=_lowerCamelCase , ) # Get all the data from the most likely cipher (key, decoded message) ( ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ) : Union[str, Any] = chi_squared_statistic_values[most_likely_cipher] # Return the data on the most likely shift return ( most_likely_cipher, most_likely_cipher_chi_squared_value, decoded_most_likely_cipher, )
316
"""simple docstring""" import re def lowerCamelCase_ ( _lowerCamelCase ): if len(re.findall('[ATCG]' , _lowerCamelCase ) ) != len(_lowerCamelCase ): raise ValueError('Invalid Strand' ) return dna.translate(dna.maketrans('ATCG' , 'TAGC' ) ) if __name__ == "__main__": import doctest doctest.testmod()
316
1
"""simple docstring""" import math A_ : Union[str, Any] = 10 A_ : List[Any] = 7 A_ : Optional[int] = BALLS_PER_COLOUR * NUM_COLOURS def lowerCamelCase_ ( _lowerCamelCase = 20 ): lowerCamelCase__ : int = math.comb(_lowerCamelCase , _lowerCamelCase ) lowerCamelCase__ : List[Any] = math.comb(NUM_BALLS - BALLS_PER_COLOUR , _lowerCamelCase ) lowerCamelCase__ : Optional[Any] = NUM_COLOURS * (1 - missing_colour / total) return f'''{result:.9f}''' if __name__ == "__main__": print(solution(20))
316
"""simple docstring""" import argparse import os import torch from transformers import FlavaImageCodebook, FlavaImageCodebookConfig def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : Any = s.rsplit(_lowerCamelCase , _lowerCamelCase ) return new.join(_lowerCamelCase ) def lowerCamelCase_ ( _lowerCamelCase ): # encoder.embeddings are double copied in original FLAVA return sum(param.float().sum() if 'encoder.embeddings' not in key else 0 for key, param in state_dict.items() ) def lowerCamelCase_ ( _lowerCamelCase ): lowerCamelCase__ : List[Any] = {} lowerCamelCase__ : Any = ['group_1', 'group_2', 'group_3', 'group_4'] for key, value in state_dict.items(): for group_key in group_keys: if group_key in key: lowerCamelCase__ : Union[str, Any] = key.replace(f'''{group_key}.''' , f'''{group_key}.group.''' ) if "res_path" in key: lowerCamelCase__ : Dict = key.replace('res_path.' , 'res_path.path.' ) if key.endswith('.w' ): lowerCamelCase__ : str = rreplace(_lowerCamelCase , '.w' , '.weight' , 1 ) if key.endswith('.b' ): lowerCamelCase__ : Optional[Any] = rreplace(_lowerCamelCase , '.b' , '.bias' , 1 ) lowerCamelCase__ : int = value.float() return upgrade @torch.no_grad() def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase=None , _lowerCamelCase=True ): from dall_e import Encoder lowerCamelCase__ : List[str] = Encoder() if os.path.exists(_lowerCamelCase ): lowerCamelCase__ : Optional[int] = torch.load(_lowerCamelCase ) else: lowerCamelCase__ : List[Any] = torch.hub.load_state_dict_from_url(_lowerCamelCase ) if isinstance(_lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : List[Any] = ckpt.state_dict() encoder.load_state_dict(_lowerCamelCase ) if config_path is not None: lowerCamelCase__ : Union[str, Any] = FlavaImageCodebookConfig.from_pretrained(_lowerCamelCase ) else: lowerCamelCase__ : Dict = FlavaImageCodebookConfig() lowerCamelCase__ : Tuple = FlavaImageCodebook(_lowerCamelCase ).eval() lowerCamelCase__ : List[str] = encoder.state_dict() lowerCamelCase__ : Any = upgrade_state_dict(_lowerCamelCase ) hf_model.load_state_dict(_lowerCamelCase ) lowerCamelCase__ : Optional[Any] = hf_model.state_dict() lowerCamelCase__ : Optional[int] = count_parameters(_lowerCamelCase ) lowerCamelCase__ : Optional[int] = count_parameters(_lowerCamelCase ) assert torch.allclose(_lowerCamelCase , _lowerCamelCase , atol=1e-3 ) if save_checkpoint: hf_model.save_pretrained(_lowerCamelCase ) else: return hf_state_dict if __name__ == "__main__": A_ : Tuple = argparse.ArgumentParser() parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to flava checkpoint") parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert") A_ : str = parser.parse_args() convert_dalle_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
316
1
"""simple docstring""" import math import sys def lowerCamelCase_ ( _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 lowerCamelCase__ : Optional[int] = [-1] * (number + 1) lowerCamelCase__ : Tuple = 0 for i in range(1 , number + 1 ): lowerCamelCase__ : List[Any] = sys.maxsize lowerCamelCase__ : List[str] = int(math.sqrt(_lowerCamelCase ) ) for j in range(1 , root + 1 ): lowerCamelCase__ : Any = 1 + answers[i - (j**2)] lowerCamelCase__ : Tuple = min(_lowerCamelCase , _lowerCamelCase ) lowerCamelCase__ : int = answer return answers[number] if __name__ == "__main__": import doctest doctest.testmod()
316
"""simple docstring""" from __future__ import annotations import inspect import unittest import numpy as np from transformers import DeiTConfig 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 ( TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, TFDeiTModel, ) from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import DeiTImageProcessor class a_ : '''simple docstring''' def __init__(self, lowerCamelCase_, lowerCamelCase_=1_3, lowerCamelCase_=3_0, lowerCamelCase_=2, lowerCamelCase_=3, lowerCamelCase_=True, lowerCamelCase_=True, lowerCamelCase_=3_2, lowerCamelCase_=2, lowerCamelCase_=4, lowerCamelCase_=3_7, lowerCamelCase_="gelu", lowerCamelCase_=0.1, lowerCamelCase_=0.1, lowerCamelCase_=1_0, lowerCamelCase_=0.02, lowerCamelCase_=3, lowerCamelCase_=None, lowerCamelCase_=2, ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = parent lowerCamelCase__ : int = batch_size lowerCamelCase__ : Dict = image_size lowerCamelCase__ : List[str] = patch_size lowerCamelCase__ : Union[str, Any] = num_channels lowerCamelCase__ : str = is_training lowerCamelCase__ : Any = use_labels lowerCamelCase__ : Tuple = hidden_size lowerCamelCase__ : str = num_hidden_layers lowerCamelCase__ : Dict = num_attention_heads lowerCamelCase__ : Union[str, Any] = intermediate_size lowerCamelCase__ : Any = hidden_act lowerCamelCase__ : Dict = hidden_dropout_prob lowerCamelCase__ : Optional[Any] = attention_probs_dropout_prob lowerCamelCase__ : List[Any] = type_sequence_label_size lowerCamelCase__ : Optional[int] = initializer_range lowerCamelCase__ : Tuple = scope lowerCamelCase__ : List[str] = encoder_stride # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens) lowerCamelCase__ : str = (image_size // patch_size) ** 2 lowerCamelCase__ : Optional[int] = num_patches + 2 def a__ (self ): '''simple docstring''' lowerCamelCase__ : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCamelCase__ : Tuple = None if self.use_labels: lowerCamelCase__ : Optional[Any] = ids_tensor([self.batch_size], self.type_sequence_label_size ) lowerCamelCase__ : List[str] = self.get_config() return config, pixel_values, labels def a__ (self ): '''simple docstring''' return DeiTConfig( image_size=self.image_size, patch_size=self.patch_size, num_channels=self.num_channels, hidden_size=self.hidden_size, num_hidden_layers=self.num_hidden_layers, num_attention_heads=self.num_attention_heads, intermediate_size=self.intermediate_size, hidden_act=self.hidden_act, hidden_dropout_prob=self.hidden_dropout_prob, attention_probs_dropout_prob=self.attention_probs_dropout_prob, is_decoder=lowerCamelCase_, initializer_range=self.initializer_range, encoder_stride=self.encoder_stride, ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = TFDeiTModel(config=lowerCamelCase_ ) lowerCamelCase__ : Dict = model(lowerCamelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : List[str] = TFDeiTForMaskedImageModeling(config=lowerCamelCase_ ) lowerCamelCase__ : Any = model(lowerCamelCase_ ) self.parent.assertEqual( result.reconstruction.shape, (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images lowerCamelCase__ : Tuple = 1 lowerCamelCase__ : Optional[Any] = TFDeiTForMaskedImageModeling(lowerCamelCase_ ) lowerCamelCase__ : List[Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowerCamelCase__ : Optional[Any] = model(lowerCamelCase_ ) self.parent.assertEqual(result.reconstruction.shape, (self.batch_size, 1, self.image_size, self.image_size) ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : int = self.type_sequence_label_size lowerCamelCase__ : Union[str, Any] = TFDeiTForImageClassification(lowerCamelCase_ ) lowerCamelCase__ : Optional[Any] = model(lowerCamelCase_, labels=lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.type_sequence_label_size) ) # test greyscale images lowerCamelCase__ : List[str] = 1 lowerCamelCase__ : Any = TFDeiTForImageClassification(lowerCamelCase_ ) lowerCamelCase__ : List[str] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowerCamelCase__ : Optional[int] = model(lowerCamelCase_, labels=lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.type_sequence_label_size) ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Any = self.prepare_config_and_inputs() lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Tuple = config_and_inputs lowerCamelCase__ : str = {'pixel_values': pixel_values} return config, inputs_dict @require_tf class a_ ( snake_case_ , snake_case_ , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ : Any = ( ( TFDeiTModel, TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, ) if is_tf_available() else () ) lowerCamelCase__ : Tuple = ( { 'feature-extraction': TFDeiTModel, 'image-classification': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher), } if is_tf_available() else {} ) lowerCamelCase__ : Any = False lowerCamelCase__ : Optional[Any] = False lowerCamelCase__ : Dict = False lowerCamelCase__ : int = False def a__ (self ): '''simple docstring''' lowerCamelCase__ : List[Any] = TFDeiTModelTester(self ) lowerCamelCase__ : Union[str, Any] = ConfigTester(self, config_class=lowerCamelCase_, has_text_modality=lowerCamelCase_, hidden_size=3_7 ) def a__ (self ): '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason='DeiT does not use inputs_embeds' ) def a__ (self ): '''simple docstring''' pass def a__ (self ): '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase__ : Optional[int] = model_class(lowerCamelCase_ ) self.assertIsInstance(model.get_input_embeddings(), (tf.keras.layers.Layer) ) lowerCamelCase__ : List[str] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCamelCase_, tf.keras.layers.Dense ) ) def a__ (self ): '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase__ : Dict = model_class(lowerCamelCase_ ) lowerCamelCase__ : Any = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCamelCase__ : str = [*signature.parameters.keys()] lowerCamelCase__ : Union[str, Any] = ['pixel_values'] self.assertListEqual(arg_names[:1], lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_ ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_=False ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = super()._prepare_for_class(lowerCamelCase_, lowerCamelCase_, return_labels=lowerCamelCase_ ) if return_labels: if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call ).parameters: del inputs_dict["labels"] return inputs_dict @slow def a__ (self ): '''simple docstring''' for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase__ : int = TFDeiTModel.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) def lowerCamelCase_ ( ): lowerCamelCase__ : Any = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_tf @require_vision class a_ ( unittest.TestCase ): '''simple docstring''' @cached_property def a__ (self ): '''simple docstring''' return ( DeiTImageProcessor.from_pretrained('facebook/deit-base-distilled-patch16-224' ) if is_vision_available() else None ) @slow def a__ (self ): '''simple docstring''' lowerCamelCase__ : str = TFDeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-224' ) lowerCamelCase__ : List[Any] = self.default_image_processor lowerCamelCase__ : Union[str, Any] = prepare_img() lowerCamelCase__ : Optional[int] = image_processor(images=lowerCamelCase_, return_tensors='tf' ) # forward pass lowerCamelCase__ : Tuple = model(**lowerCamelCase_ ) # verify the logits lowerCamelCase__ : str = tf.TensorShape((1, 1_0_0_0) ) self.assertEqual(outputs.logits.shape, lowerCamelCase_ ) lowerCamelCase__ : Any = tf.constant([-1.0_266, 0.1_912, -1.2_861] ) self.assertTrue(np.allclose(outputs.logits[0, :3], lowerCamelCase_, atol=1e-4 ) )
316
1
"""simple docstring""" import tempfile import torch from diffusers import IPNDMScheduler from .test_schedulers import SchedulerCommonTest class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : str = (IPNDMScheduler,) lowerCamelCase__ : Any = (('num_inference_steps', 50),) def a__ (self, **lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Any = {'num_train_timesteps': 1_0_0_0} config.update(**lowerCamelCase_ ) return config def a__ (self, lowerCamelCase_=0, **lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : int = dict(self.forward_default_kwargs ) lowerCamelCase__ : Any = kwargs.pop('num_inference_steps', lowerCamelCase_ ) lowerCamelCase__ : Optional[Any] = self.dummy_sample lowerCamelCase__ : List[str] = 0.1 * sample lowerCamelCase__ : int = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: lowerCamelCase__ : Any = self.get_scheduler_config(**lowerCamelCase_ ) lowerCamelCase__ : Union[str, Any] = scheduler_class(**lowerCamelCase_ ) scheduler.set_timesteps(lowerCamelCase_ ) # copy over dummy past residuals lowerCamelCase__ : Dict = dummy_past_residuals[:] if time_step is None: lowerCamelCase__ : Union[str, Any] = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(lowerCamelCase_ ) lowerCamelCase__ : Dict = scheduler_class.from_pretrained(lowerCamelCase_ ) new_scheduler.set_timesteps(lowerCamelCase_ ) # copy over dummy past residuals lowerCamelCase__ : Optional[Any] = dummy_past_residuals[:] lowerCamelCase__ : Optional[int] = scheduler.step(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, **lowerCamelCase_ ).prev_sample lowerCamelCase__ : int = new_scheduler.step(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, **lowerCamelCase_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" lowerCamelCase__ : Any = scheduler.step(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, **lowerCamelCase_ ).prev_sample lowerCamelCase__ : Optional[Any] = new_scheduler.step(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, **lowerCamelCase_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def a__ (self ): '''simple docstring''' pass def a__ (self, lowerCamelCase_=0, **lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : str = dict(self.forward_default_kwargs ) lowerCamelCase__ : Tuple = kwargs.pop('num_inference_steps', lowerCamelCase_ ) lowerCamelCase__ : List[Any] = self.dummy_sample lowerCamelCase__ : List[str] = 0.1 * sample lowerCamelCase__ : List[str] = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: lowerCamelCase__ : List[str] = self.get_scheduler_config() lowerCamelCase__ : Tuple = scheduler_class(**lowerCamelCase_ ) scheduler.set_timesteps(lowerCamelCase_ ) # copy over dummy past residuals (must be after setting timesteps) lowerCamelCase__ : Any = dummy_past_residuals[:] if time_step is None: lowerCamelCase__ : Union[str, Any] = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(lowerCamelCase_ ) lowerCamelCase__ : List[str] = 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) lowerCamelCase__ : Dict = dummy_past_residuals[:] lowerCamelCase__ : List[Any] = scheduler.step(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, **lowerCamelCase_ ).prev_sample lowerCamelCase__ : int = new_scheduler.step(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, **lowerCamelCase_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" lowerCamelCase__ : Dict = scheduler.step(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, **lowerCamelCase_ ).prev_sample lowerCamelCase__ : List[str] = new_scheduler.step(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, **lowerCamelCase_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def a__ (self, **lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : str = self.scheduler_classes[0] lowerCamelCase__ : int = self.get_scheduler_config(**lowerCamelCase_ ) lowerCamelCase__ : Any = scheduler_class(**lowerCamelCase_ ) lowerCamelCase__ : str = 1_0 lowerCamelCase__ : Union[str, Any] = self.dummy_model() lowerCamelCase__ : Any = self.dummy_sample_deter scheduler.set_timesteps(lowerCamelCase_ ) for i, t in enumerate(scheduler.timesteps ): lowerCamelCase__ : Union[str, Any] = model(lowerCamelCase_, lowerCamelCase_ ) lowerCamelCase__ : int = scheduler.step(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ).prev_sample for i, t in enumerate(scheduler.timesteps ): lowerCamelCase__ : Tuple = model(lowerCamelCase_, lowerCamelCase_ ) lowerCamelCase__ : Dict = scheduler.step(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ).prev_sample return sample def a__ (self ): '''simple docstring''' lowerCamelCase__ : Any = dict(self.forward_default_kwargs ) lowerCamelCase__ : int = kwargs.pop('num_inference_steps', lowerCamelCase_ ) for scheduler_class in self.scheduler_classes: lowerCamelCase__ : List[str] = self.get_scheduler_config() lowerCamelCase__ : int = scheduler_class(**lowerCamelCase_ ) lowerCamelCase__ : int = self.dummy_sample lowerCamelCase__ : Tuple = 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' ): lowerCamelCase__ : List[Any] = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) lowerCamelCase__ : Union[str, Any] = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] lowerCamelCase__ : Dict = dummy_past_residuals[:] lowerCamelCase__ : Tuple = scheduler.timesteps[5] lowerCamelCase__ : Optional[int] = scheduler.timesteps[6] lowerCamelCase__ : int = scheduler.step(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, **lowerCamelCase_ ).prev_sample lowerCamelCase__ : Union[str, Any] = scheduler.step(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, **lowerCamelCase_ ).prev_sample self.assertEqual(output_a.shape, sample.shape ) self.assertEqual(output_a.shape, output_a.shape ) lowerCamelCase__ : Optional[Any] = scheduler.step(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, **lowerCamelCase_ ).prev_sample lowerCamelCase__ : Dict = scheduler.step(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, **lowerCamelCase_ ).prev_sample self.assertEqual(output_a.shape, sample.shape ) self.assertEqual(output_a.shape, output_a.shape ) def a__ (self ): '''simple docstring''' for timesteps in [1_0_0, 1_0_0_0]: self.check_over_configs(num_train_timesteps=lowerCamelCase_, time_step=lowerCamelCase_ ) def a__ (self ): '''simple docstring''' for t, num_inference_steps in zip([1, 5, 1_0], [1_0, 5_0, 1_0_0] ): self.check_over_forward(num_inference_steps=lowerCamelCase_, time_step=lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : str = self.full_loop() lowerCamelCase__ : Any = torch.mean(torch.abs(lowerCamelCase_ ) ) assert abs(result_mean.item() - 2_5_4_0_5_2_9 ) < 1_0
316
"""simple docstring""" def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): while second != 0: lowerCamelCase__ : Tuple = first & second first ^= second lowerCamelCase__ : int = c << 1 return first if __name__ == "__main__": import doctest doctest.testmod() A_ : Tuple = int(input("Enter the first number: ").strip()) A_ : Union[str, Any] = int(input("Enter the second number: ").strip()) print(f"{add(first, second) = }")
316
1
"""simple docstring""" from typing import Dict, List from nltk.translate import gleu_score import datasets from datasets import MetricInfo A_ : List[Any] = "\\n@misc{wu2016googles,\n title={Google's Neural Machine Translation System: Bridging the Gap between Human and Machine Translation},\n author={Yonghui Wu and Mike Schuster and Zhifeng Chen and Quoc V. Le and Mohammad Norouzi and Wolfgang Macherey\n and Maxim Krikun and Yuan Cao and Qin Gao and Klaus Macherey and Jeff Klingner and Apurva Shah and Melvin\n Johnson and Xiaobing Liu and Łukasz Kaiser and Stephan Gouws and Yoshikiyo Kato and Taku Kudo and Hideto\n Kazawa and Keith Stevens and George Kurian and Nishant Patil and Wei Wang and Cliff Young and\n Jason Smith and Jason Riesa and Alex Rudnick and Oriol Vinyals and Greg Corrado and Macduff Hughes\n and Jeffrey Dean},\n year={2016},\n eprint={1609.08144},\n archivePrefix={arXiv},\n primaryClass={cs.CL}\n}\n" A_ : List[Any] = "\\nThe BLEU score has some undesirable properties when used for single\nsentences, as it was designed to be a corpus measure. We therefore\nuse a slightly different score for our RL experiments which we call\nthe 'GLEU score'. For the GLEU score, we record all sub-sequences of\n1, 2, 3 or 4 tokens in output and target sequence (n-grams). We then\ncompute a recall, which is the ratio of the number of matching n-grams\nto the number of total n-grams in the target (ground truth) sequence,\nand a precision, which is the ratio of the number of matching n-grams\nto the number of total n-grams in the generated output sequence. Then\nGLEU score is simply the minimum of recall and precision. This GLEU\nscore's range is always between 0 (no matches) and 1 (all match) and\nit is symmetrical when switching output and target. According to\nour experiments, GLEU score correlates quite well with the BLEU\nmetric on a corpus level but does not have its drawbacks for our per\nsentence reward objective.\n" A_ : Dict = "\\nComputes corpus-level Google BLEU (GLEU) score of translated segments against one or more references.\nInstead of averaging the sentence level GLEU scores (i.e. macro-average precision), Wu et al. (2016) sum up the matching\ntokens and the max of hypothesis and reference tokens for each sentence, then compute using the aggregate values.\n\nArgs:\n predictions (list of str): list of translations to score.\n Each translation should be tokenized into a list of tokens.\n references (list of list of str): list of lists of references for each translation.\n Each reference should be tokenized into a list of tokens.\n min_len (int): The minimum order of n-gram this function should extract. Defaults to 1.\n max_len (int): The maximum order of n-gram this function should extract. Defaults to 4.\n\nReturns:\n 'google_bleu': google_bleu score\n\nExamples:\n Example 1:\n >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always',\n ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat']\n >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which',\n ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never',\n ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat']\n\n >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',\n ... 'interested', 'in', 'world', 'history']\n >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',\n ... 'because', 'he', 'read', 'the', 'book']\n\n >>> list_of_references = [[ref1a], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.44\n\n Example 2:\n >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always',\n ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat']\n >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which',\n ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never',\n ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat']\n >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never',\n ... 'heed', 'the', 'cat', 'commands']\n >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the',\n ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions',\n ... 'of', 'the', 'cat']\n\n >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',\n ... 'interested', 'in', 'world', 'history']\n >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',\n ... 'because', 'he', 'read', 'the', 'book']\n\n >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.61\n\n Example 3:\n >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always',\n ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat']\n >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which',\n ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never',\n ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat']\n >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never',\n ... 'heed', 'the', 'cat', 'commands']\n >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the',\n ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions',\n ... 'of', 'the', 'cat']\n\n >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',\n ... 'interested', 'in', 'world', 'history']\n >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',\n ... 'because', 'he', 'read', 'the', 'book']\n\n >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references, min_len=2)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.53\n\n Example 4:\n >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always',\n ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat']\n >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which',\n ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never',\n ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat']\n >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never',\n ... 'heed', 'the', 'cat', 'commands']\n >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the',\n ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions',\n ... 'of', 'the', 'cat']\n\n >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',\n ... 'interested', 'in', 'world', 'history']\n >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',\n ... 'because', 'he', 'read', 'the', 'book']\n\n >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses,references=list_of_references, min_len=2, max_len=6)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.4\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class a_ ( datasets.Metric ): '''simple docstring''' def a__ (self ): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION, citation=_CITATION, inputs_description=_KWARGS_DESCRIPTION, features=datasets.Features( { 'predictions': datasets.Sequence(datasets.Value('string', id='token' ), id='sequence' ), 'references': datasets.Sequence( datasets.Sequence(datasets.Value('string', id='token' ), id='sequence' ), id='references' ), } ), ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ = 1, lowerCamelCase_ = 4, ): '''simple docstring''' return { "google_bleu": gleu_score.corpus_gleu( list_of_references=lowerCamelCase_, hypotheses=lowerCamelCase_, min_len=lowerCamelCase_, max_len=lowerCamelCase_ ) }
316
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) A_ : List[str] = {"configuration_encoder_decoder": ["EncoderDecoderConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Dict = ["EncoderDecoderModel"] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : List[Any] = ["TFEncoderDecoderModel"] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : List[Any] = ["FlaxEncoderDecoderModel"] if TYPE_CHECKING: from .configuration_encoder_decoder import EncoderDecoderConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_encoder_decoder import EncoderDecoderModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_encoder_decoder import TFEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_encoder_decoder import FlaxEncoderDecoderModel else: import sys A_ : Any = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
316
1
"""simple docstring""" import argparse import json import os import tensorstore as ts import torch from flax import serialization from flax.traverse_util import flatten_dict, unflatten_dict from tensorflow.io import gfile from transformers.modeling_utils import dtype_byte_size from transformers.models.switch_transformers.convert_switch_transformers_original_flax_checkpoint_to_pytorch import ( rename_keys, ) from transformers.utils import WEIGHTS_INDEX_NAME, WEIGHTS_NAME from transformers.utils.hub import convert_file_size_to_int def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): if flax_key_tuple[-1] == "kernel" and flax_tensor.ndim == 3: # expert layer lowerCamelCase__ : Tuple = flax_key_tuple[:-1] + ('weight',) lowerCamelCase__ : Tuple = torch.permute(_lowerCamelCase , (0, 2, 1) ) elif flax_key_tuple[-1] == "kernel" and ".".join(_lowerCamelCase ): # linear layer lowerCamelCase__ : List[str] = flax_key_tuple[:-1] + ('weight',) lowerCamelCase__ : Dict = flax_tensor.T elif flax_key_tuple[-1] in ["scale", "embedding"]: lowerCamelCase__ : Union[str, Any] = flax_key_tuple[:-1] + ('weight',) return flax_key_tuple, flax_tensor def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): if "metadata" in layer: lowerCamelCase__ : Tuple = layer.split('metadata' ) lowerCamelCase__ : str = ''.join(split_layer[0] )[:-1] lowerCamelCase__ : Dict = [tuple(('metadata' + split_layer[1]).split('/' ) )] elif "kvstore" in layer: lowerCamelCase__ : List[str] = layer.split('kvstore' ) lowerCamelCase__ : Any = ''.join(split_layer[0] )[:-1] lowerCamelCase__ : List[Any] = [tuple(('kvstore' + split_layer[1]).split('/' ) )] else: lowerCamelCase__ : Union[str, Any] = layer.split('/' ) lowerCamelCase__ : str = '/'.join(split_layer[:-1] ) lowerCamelCase__ : Tuple = (split_layer[-1],) if "kvstore/path" in layer: lowerCamelCase__ : Dict = f'''{switch_checkpoint_path}/{checkpoint_info[layer]}''' elif "kvstore/driver" in layer: lowerCamelCase__ : Optional[Any] = 'file' else: lowerCamelCase__ : str = checkpoint_info[layer] return curr_real_layer_name, split_layer, content def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : Optional[Any] = rename_keys(_lowerCamelCase ) lowerCamelCase__ : Union[str, Any] = {} for k, v in current_block.items(): lowerCamelCase__ : int = v lowerCamelCase__ : List[str] = new_current_block torch.save(_lowerCamelCase , _lowerCamelCase ) def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase = WEIGHTS_NAME ): lowerCamelCase__ : Dict = convert_file_size_to_int(_lowerCamelCase ) lowerCamelCase__ : Optional[Any] = [] lowerCamelCase__ : Optional[Any] = {} lowerCamelCase__ : Optional[Any] = 0 lowerCamelCase__ : Optional[Any] = 0 os.makedirs(_lowerCamelCase , exist_ok=_lowerCamelCase ) with gfile.GFile(switch_checkpoint_path + '/checkpoint' , 'rb' ) as fp: lowerCamelCase__ : int = serialization.msgpack_restore(fp.read() )['optimizer']['target'] lowerCamelCase__ : Optional[int] = flatten_dict(_lowerCamelCase , sep='/' ) lowerCamelCase__ : Dict = {} for layer in checkpoint_info.keys(): lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Optional[int] = get_key_and_tensorstore_dict( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) if curr_real_layer_name in all_layers: lowerCamelCase__ : int = content else: lowerCamelCase__ : Tuple = {split_layer[-1]: content} for key in all_layers.keys(): # open tensorstore file lowerCamelCase__ : Tuple = ts.open(unflatten_dict(all_layers[key] ) ).result().read().result() lowerCamelCase__ : Union[str, Any] = torch.tensor(_lowerCamelCase ) lowerCamelCase__ : List[str] = raw_weights.numel() * dtype_byte_size(raw_weights.dtype ) # use the renaming pattern from the small conversion scripts lowerCamelCase__ , lowerCamelCase__ : str = rename_base_flax_keys(tuple(key.split('/' ) ) , _lowerCamelCase ) lowerCamelCase__ : List[str] = '/'.join(_lowerCamelCase ) # If this weight is going to tip up over the maximal size, we split. if current_block_size + weight_size > max_shard_size: lowerCamelCase__ : Tuple = os.path.join( _lowerCamelCase , weights_name.replace('.bin' , f'''-{len(_lowerCamelCase )+1:05d}-of-???.bin''' ) ) rename_and_save_block(_lowerCamelCase , _lowerCamelCase ) sharded_state_dicts.append(current_block.keys() ) del current_block lowerCamelCase__ : Dict = {} lowerCamelCase__ : str = 0 lowerCamelCase__ : Any = raw_weights.to(getattr(_lowerCamelCase , _lowerCamelCase ) ) current_block_size += weight_size total_size += weight_size # Add the last block lowerCamelCase__ : str = os.path.join(_lowerCamelCase , weights_name.replace('.bin' , f'''-{len(_lowerCamelCase )+1:05d}-of-???.bin''' ) ) rename_and_save_block(_lowerCamelCase , _lowerCamelCase ) sharded_state_dicts.append(current_block.keys() ) # If we only have one shard, we return it if len(_lowerCamelCase ) == 1: return {weights_name: sharded_state_dicts[0]}, None # Otherwise, let's build the index lowerCamelCase__ : str = {} lowerCamelCase__ : Tuple = {} for idx, shard in enumerate(_lowerCamelCase ): lowerCamelCase__ : str = weights_name.replace( '.bin' , f'''-{idx+1:05d}-of-{len(_lowerCamelCase ):05d}.bin''' ) # len(sharded_state_dicts):05d} lowerCamelCase__ : Dict = os.path.join(_lowerCamelCase , weights_name.replace('.bin' , f'''-{idx+1:05d}-of-???.bin''' ) ) os.rename(_lowerCamelCase , os.path.join(_lowerCamelCase , _lowerCamelCase ) ) lowerCamelCase__ : Optional[int] = shard for key in shard: lowerCamelCase__ : int = shard_file # Add the metadata lowerCamelCase__ : Tuple = {'total_size': total_size} lowerCamelCase__ : List[Any] = {'metadata': metadata, 'weight_map': weight_map} with open(os.path.join(_lowerCamelCase , _lowerCamelCase ) , 'w' , encoding='utf-8' ) as f: lowerCamelCase__ : Union[str, Any] = json.dumps(_lowerCamelCase , indent=2 , sort_keys=_lowerCamelCase ) + '\n' f.write(_lowerCamelCase ) return metadata, index if __name__ == "__main__": A_ : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--switch_t5x_checkpoint_path", default="/mnt/disks/disk_switch/original_checkpoints/switch-xxl-128/checkpoint_634600", type=str, required=False, help="Path to a directory containing a folder per layer. Follows the original Google format.", ) parser.add_argument("--max_shard_size", default="10GB", required=False, help="Max shard size") parser.add_argument("--dtype", default="bfloat16", type=str, required=False, help="dtype of the saved model") parser.add_argument( "--pytorch_dump_folder_path", default="/mnt/disks/disk_switch/original_checkpoints/switch-xxl-128-converted", type=str, required=False, help="Path to the output pytorch model.", ) A_ : List[Any] = parser.parse_args() shard_on_the_fly( args.switch_tax_checkpoint_path, args.pytorch_dump_folder_path, args.max_shard_size, args.dtype, ) def lowerCamelCase_ ( ): from transformers import SwitchTransformersConfig, SwitchTransformersForConditionalGeneration, TaTokenizer lowerCamelCase__ : Optional[int] = SwitchTransformersConfig.from_pretrained('google/switch-base-8' ) config.save_pretrained('/home/arthur_huggingface_co/transformers/switch_converted' ) lowerCamelCase__ : Dict = SwitchTransformersForConditionalGeneration.from_pretrained( '/home/arthur_huggingface_co/transformers/switch_converted' , device_map='auto' ) lowerCamelCase__ : List[str] = TaTokenizer.from_pretrained('t5-small' ) lowerCamelCase__ : int = 'A <extra_id_0> walks into a bar a orders a <extra_id_1> with <extra_id_2> pinch of <extra_id_3>.' lowerCamelCase__ : Union[str, Any] = tokenizer(_lowerCamelCase , return_tensors='pt' ).input_ids lowerCamelCase__ : Union[str, Any] = model.generate(_lowerCamelCase , decoder_start_token_id=0 ) print(tokenizer.decode(out[0] ) )
316
"""simple docstring""" import numpy as np def lowerCamelCase_ ( _lowerCamelCase ): return (2 / (1 + np.exp(-2 * vector ))) - 1 if __name__ == "__main__": import doctest doctest.testmod()
316
1
"""simple docstring""" import numpy as np def lowerCamelCase_ ( _lowerCamelCase ): return 1 / (1 + np.exp(-vector )) def lowerCamelCase_ ( _lowerCamelCase ): return vector * sigmoid(1.702 * vector ) if __name__ == "__main__": import doctest doctest.testmod()
316
"""simple docstring""" print((lambda quine: quine % quine)("print((lambda quine: quine %% quine)(%r))"))
316
1
"""simple docstring""" def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): return numa ^ numa < 0 if __name__ == "__main__": import doctest doctest.testmod()
316
"""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, ) A_ : int = { "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: A_ : Union[str, Any] = ["CLIPTokenizerFast"] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : int = ["CLIPFeatureExtractor"] A_ : Any = ["CLIPImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : List[Any] = [ "CLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "CLIPModel", "CLIPPreTrainedModel", "CLIPTextModel", "CLIPTextModelWithProjection", "CLIPVisionModel", "CLIPVisionModelWithProjection", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Any = [ "TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "TFCLIPModel", "TFCLIPPreTrainedModel", "TFCLIPTextModel", "TFCLIPVisionModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Any = [ "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 A_ : Optional[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
316
1
"""simple docstring""" from ..utils import is_flax_available, is_torch_available if is_torch_available(): from .autoencoder_kl import AutoencoderKL from .controlnet import ControlNetModel from .dual_transformer_ad import DualTransformeraDModel from .modeling_utils import ModelMixin from .prior_transformer import PriorTransformer from .ta_film_transformer import TaFilmDecoder from .transformer_ad import TransformeraDModel from .unet_ad import UNetaDModel from .unet_ad import UNetaDModel from .unet_ad_condition import UNetaDConditionModel from .unet_ad_condition import UNetaDConditionModel from .vq_model import VQModel if is_flax_available(): from .controlnet_flax import FlaxControlNetModel from .unet_ad_condition_flax import FlaxUNetaDConditionModel from .vae_flax import FlaxAutoencoderKL
316
"""simple docstring""" import os import posixpath import uuid from dataclasses import dataclass from typing import TYPE_CHECKING, Iterable, List, Optional, Tuple, Union import numpy as np import pyarrow as pa import datasets from datasets.arrow_writer import ArrowWriter, ParquetWriter from datasets.config import MAX_SHARD_SIZE from datasets.filesystems import ( is_remote_filesystem, rename, ) from datasets.iterable_dataset import _BaseExamplesIterable from datasets.utils.py_utils import convert_file_size_to_int A_ : List[Any] = datasets.utils.logging.get_logger(__name__) if TYPE_CHECKING: import pyspark @dataclass class a_ ( datasets.BuilderConfig ): '''simple docstring''' lowerCamelCase__ : Optional[datasets.Features] = None def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , ): import pyspark def generate_fn(): lowerCamelCase__ : Optional[Any] = df.select('*' , pyspark.sql.functions.spark_partition_id().alias('part_id' ) ) for partition_id in partition_order: lowerCamelCase__ : Dict = df_with_partition_id.select('*' ).where(f'''part_id = {partition_id}''' ).drop('part_id' ) lowerCamelCase__ : Dict = partition_df.collect() lowerCamelCase__ : int = 0 for row in rows: yield f'''{partition_id}_{row_id}''', row.asDict() row_id += 1 return generate_fn class a_ ( _BaseExamplesIterable ): '''simple docstring''' def __init__(self, lowerCamelCase_, lowerCamelCase_=None, ): '''simple docstring''' lowerCamelCase__ : Tuple = df lowerCamelCase__ : Any = partition_order or range(self.df.rdd.getNumPartitions() ) lowerCamelCase__ : List[Any] = _generate_iterable_examples(self.df, self.partition_order ) def __iter__(self ): '''simple docstring''' yield from self.generate_examples_fn() def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = list(range(self.df.rdd.getNumPartitions() ) ) generator.shuffle(lowerCamelCase_ ) return SparkExamplesIterable(self.df, partition_order=lowerCamelCase_ ) def a__ (self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = self.split_shard_indices_by_worker(lowerCamelCase_, lowerCamelCase_ ) return SparkExamplesIterable(self.df, partition_order=lowerCamelCase_ ) @property def a__ (self ): '''simple docstring''' return len(self.partition_order ) class a_ ( datasets.DatasetBuilder ): '''simple docstring''' lowerCamelCase__ : Optional[int] = SparkConfig def __init__(self, lowerCamelCase_, lowerCamelCase_ = None, lowerCamelCase_ = None, **lowerCamelCase_, ): '''simple docstring''' import pyspark lowerCamelCase__ : str = pyspark.sql.SparkSession.builder.getOrCreate() lowerCamelCase__ : Optional[Any] = df lowerCamelCase__ : Dict = working_dir super().__init__( cache_dir=lowerCamelCase_, config_name=str(self.df.semanticHash() ), **lowerCamelCase_, ) def a__ (self ): '''simple docstring''' def create_cache_and_write_probe(lowerCamelCase_ ): # makedirs with exist_ok will recursively create the directory. It will not throw an error if directories # already exist. os.makedirs(self._cache_dir, exist_ok=lowerCamelCase_ ) lowerCamelCase__ : str = os.path.join(self._cache_dir, 'fs_test' + uuid.uuida().hex ) # Opening the file in append mode will create a new file unless it already exists, in which case it will not # change the file contents. open(lowerCamelCase_, 'a' ) return [probe_file] if self._spark.conf.get('spark.master', '' ).startswith('local' ): return # If the cluster is multi-node, make sure that the user provided a cache_dir and that it is on an NFS # accessible to the driver. # TODO: Stream batches to the driver using ArrowCollectSerializer instead of throwing an error. if self._cache_dir: lowerCamelCase__ : Tuple = ( self._spark.sparkContext.parallelize(range(1 ), 1 ).mapPartitions(lowerCamelCase_ ).collect() ) if os.path.isfile(probe[0] ): return raise ValueError( 'When using Dataset.from_spark on a multi-node cluster, the driver and all workers should be able to access cache_dir' ) def a__ (self ): '''simple docstring''' return datasets.DatasetInfo(features=self.config.features ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' return [datasets.SplitGenerator(name=datasets.Split.TRAIN )] def a__ (self, lowerCamelCase_ ): '''simple docstring''' import pyspark def get_arrow_batch_size(lowerCamelCase_ ): for batch in it: yield pa.RecordBatch.from_pydict({'batch_bytes': [batch.nbytes]} ) lowerCamelCase__ : List[Any] = self.df.count() lowerCamelCase__ : List[Any] = df_num_rows if df_num_rows <= 1_0_0 else 1_0_0 # Approximate the size of each row (in Arrow format) by averaging over a max-100-row sample. lowerCamelCase__ : List[Any] = ( self.df.limit(lowerCamelCase_ ) .repartition(1 ) .mapInArrow(lowerCamelCase_, 'batch_bytes: long' ) .agg(pyspark.sql.functions.sum('batch_bytes' ).alias('sample_bytes' ) ) .collect()[0] .sample_bytes / sample_num_rows ) lowerCamelCase__ : Dict = approx_bytes_per_row * df_num_rows if approx_total_size > max_shard_size: # Make sure there is at least one row per partition. lowerCamelCase__ : str = min(lowerCamelCase_, int(approx_total_size / max_shard_size ) ) lowerCamelCase__ : List[Any] = self.df.repartition(lowerCamelCase_ ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, ): '''simple docstring''' import pyspark lowerCamelCase__ : List[str] = ParquetWriter if file_format == 'parquet' else ArrowWriter lowerCamelCase__ : List[str] = os.path.join(self._working_dir, os.path.basename(lowerCamelCase_ ) ) if self._working_dir else fpath lowerCamelCase__ : Optional[int] = file_format == 'parquet' # Define these so that we don't reference self in write_arrow, which will result in a pickling error due to # pickling the SparkContext. lowerCamelCase__ : int = self.config.features lowerCamelCase__ : Dict = self._writer_batch_size lowerCamelCase__ : Optional[Any] = self._fs.storage_options def write_arrow(lowerCamelCase_ ): # Within the same SparkContext, no two task attempts will share the same attempt ID. lowerCamelCase__ : Any = pyspark.TaskContext().taskAttemptId() lowerCamelCase__ : str = next(lowerCamelCase_, lowerCamelCase_ ) if first_batch is None: # Some partitions might not receive any data. return pa.RecordBatch.from_arrays( [[task_id], [0], [0]], names=['task_id', 'num_examples', 'num_bytes'], ) lowerCamelCase__ : Tuple = 0 lowerCamelCase__ : Any = writer_class( features=lowerCamelCase_, path=working_fpath.replace('SSSSS', f'''{shard_id:05d}''' ).replace('TTTTT', f'''{task_id:05d}''' ), writer_batch_size=lowerCamelCase_, storage_options=lowerCamelCase_, embed_local_files=lowerCamelCase_, ) lowerCamelCase__ : List[str] = pa.Table.from_batches([first_batch] ) writer.write_table(lowerCamelCase_ ) for batch in it: if max_shard_size is not None and writer._num_bytes >= max_shard_size: lowerCamelCase__ , lowerCamelCase__ : Tuple = writer.finalize() writer.close() yield pa.RecordBatch.from_arrays( [[task_id], [num_examples], [num_bytes]], names=['task_id', 'num_examples', 'num_bytes'], ) shard_id += 1 lowerCamelCase__ : Dict = writer_class( features=writer._features, path=working_fpath.replace('SSSSS', f'''{shard_id:05d}''' ).replace('TTTTT', f'''{task_id:05d}''' ), writer_batch_size=lowerCamelCase_, storage_options=lowerCamelCase_, embed_local_files=lowerCamelCase_, ) lowerCamelCase__ : Tuple = pa.Table.from_batches([batch] ) writer.write_table(lowerCamelCase_ ) if writer._num_bytes > 0: lowerCamelCase__ , lowerCamelCase__ : Tuple = writer.finalize() writer.close() yield pa.RecordBatch.from_arrays( [[task_id], [num_examples], [num_bytes]], names=['task_id', 'num_examples', 'num_bytes'], ) if working_fpath != fpath: for file in os.listdir(os.path.dirname(lowerCamelCase_ ) ): lowerCamelCase__ : Optional[int] = os.path.join(os.path.dirname(lowerCamelCase_ ), os.path.basename(lowerCamelCase_ ) ) shutil.move(lowerCamelCase_, lowerCamelCase_ ) lowerCamelCase__ : List[str] = ( self.df.mapInArrow(lowerCamelCase_, 'task_id: long, num_examples: long, num_bytes: long' ) .groupBy('task_id' ) .agg( pyspark.sql.functions.sum('num_examples' ).alias('total_num_examples' ), pyspark.sql.functions.sum('num_bytes' ).alias('total_num_bytes' ), pyspark.sql.functions.count('num_bytes' ).alias('num_shards' ), pyspark.sql.functions.collect_list('num_examples' ).alias('shard_lengths' ), ) .collect() ) for row in stats: yield row.task_id, (row.total_num_examples, row.total_num_bytes, row.num_shards, row.shard_lengths) def a__ (self, lowerCamelCase_, lowerCamelCase_ = "arrow", lowerCamelCase_ = None, lowerCamelCase_ = None, **lowerCamelCase_, ): '''simple docstring''' self._validate_cache_dir() lowerCamelCase__ : Union[str, Any] = convert_file_size_to_int(max_shard_size or MAX_SHARD_SIZE ) self._repartition_df_if_needed(lowerCamelCase_ ) lowerCamelCase__ : str = not is_remote_filesystem(self._fs ) lowerCamelCase__ : Any = os.path.join if is_local else posixpath.join lowerCamelCase__ : Any = '-TTTTT-SSSSS-of-NNNNN' lowerCamelCase__ : Tuple = f'''{self.name}-{split_generator.name}{SUFFIX}.{file_format}''' lowerCamelCase__ : Union[str, Any] = path_join(self._output_dir, lowerCamelCase_ ) lowerCamelCase__ : List[str] = 0 lowerCamelCase__ : Dict = 0 lowerCamelCase__ : List[Any] = 0 lowerCamelCase__ : Optional[Any] = [] lowerCamelCase__ : List[str] = [] for task_id, content in self._prepare_split_single(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): ( ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ) : int = content if num_bytes > 0: total_num_examples += num_examples total_num_bytes += num_bytes total_shards += num_shards task_id_and_num_shards.append((task_id, num_shards) ) all_shard_lengths.extend(lowerCamelCase_ ) lowerCamelCase__ : str = total_num_examples lowerCamelCase__ : int = total_num_bytes # should rename everything at the end logger.debug(f'''Renaming {total_shards} shards.''' ) if total_shards > 1: lowerCamelCase__ : Union[str, Any] = all_shard_lengths # Define fs outside of _rename_shard so that we don't reference self in the function, which will result in a # pickling error due to pickling the SparkContext. lowerCamelCase__ : Optional[Any] = self._fs # use the -SSSSS-of-NNNNN pattern def _rename_shard( lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, ): rename( lowerCamelCase_, fpath.replace('SSSSS', f'''{shard_id:05d}''' ).replace('TTTTT', f'''{task_id:05d}''' ), fpath.replace('TTTTT-SSSSS', f'''{global_shard_id:05d}''' ).replace('NNNNN', f'''{total_shards:05d}''' ), ) lowerCamelCase__ : List[str] = [] lowerCamelCase__ : List[str] = 0 for i in range(len(lowerCamelCase_ ) ): lowerCamelCase__ , lowerCamelCase__ : Any = task_id_and_num_shards[i] for shard_id in range(lowerCamelCase_ ): args.append([task_id, shard_id, global_shard_id] ) global_shard_id += 1 self._spark.sparkContext.parallelize(lowerCamelCase_, len(lowerCamelCase_ ) ).map(lambda lowerCamelCase_ : _rename_shard(*lowerCamelCase_ ) ).collect() else: # don't use any pattern lowerCamelCase__ : List[Any] = 0 lowerCamelCase__ : Dict = task_id_and_num_shards[0][0] self._rename( fpath.replace('SSSSS', f'''{shard_id:05d}''' ).replace('TTTTT', f'''{task_id:05d}''' ), fpath.replace(lowerCamelCase_, '' ), ) def a__ (self, lowerCamelCase_, ): '''simple docstring''' return SparkExamplesIterable(self.df )
316
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging A_ : Union[str, Any] = logging.get_logger(__name__) A_ : Optional[int] = { "transfo-xl-wt103": "https://huggingface.co/transfo-xl-wt103/resolve/main/config.json", } class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : Any = 'transfo-xl' lowerCamelCase__ : List[str] = ['mems'] lowerCamelCase__ : Dict = { 'n_token': 'vocab_size', 'hidden_size': 'd_model', 'num_attention_heads': 'n_head', 'num_hidden_layers': 'n_layer', } def __init__(self, lowerCamelCase_=2_6_7_7_3_5, lowerCamelCase_=[2_0_0_0_0, 4_0_0_0_0, 2_0_0_0_0_0], lowerCamelCase_=1_0_2_4, lowerCamelCase_=1_0_2_4, lowerCamelCase_=1_6, lowerCamelCase_=6_4, lowerCamelCase_=4_0_9_6, lowerCamelCase_=4, lowerCamelCase_=False, lowerCamelCase_=1_8, lowerCamelCase_=1_6_0_0, lowerCamelCase_=1_0_0_0, lowerCamelCase_=True, lowerCamelCase_=True, lowerCamelCase_=0, lowerCamelCase_=-1, lowerCamelCase_=True, lowerCamelCase_=0.1, lowerCamelCase_=0.0, lowerCamelCase_=True, lowerCamelCase_="normal", lowerCamelCase_=0.01, lowerCamelCase_=0.01, lowerCamelCase_=0.02, lowerCamelCase_=1e-5, lowerCamelCase_=0, **lowerCamelCase_, ): '''simple docstring''' lowerCamelCase__ : str = vocab_size lowerCamelCase__ : int = [] self.cutoffs.extend(lowerCamelCase_ ) if proj_share_all_but_first: lowerCamelCase__ : Optional[int] = [False] + [True] * len(self.cutoffs ) else: lowerCamelCase__ : str = [False] + [False] * len(self.cutoffs ) lowerCamelCase__ : Dict = d_model lowerCamelCase__ : int = d_embed lowerCamelCase__ : Union[str, Any] = d_head lowerCamelCase__ : List[str] = d_inner lowerCamelCase__ : List[str] = div_val lowerCamelCase__ : Union[str, Any] = pre_lnorm lowerCamelCase__ : Optional[int] = n_layer lowerCamelCase__ : Dict = n_head lowerCamelCase__ : List[Any] = mem_len lowerCamelCase__ : Tuple = same_length lowerCamelCase__ : List[Any] = attn_type lowerCamelCase__ : List[Any] = clamp_len lowerCamelCase__ : Union[str, Any] = sample_softmax lowerCamelCase__ : Union[str, Any] = adaptive lowerCamelCase__ : Optional[int] = dropout lowerCamelCase__ : List[str] = dropatt lowerCamelCase__ : Optional[int] = untie_r lowerCamelCase__ : List[str] = init lowerCamelCase__ : List[str] = init_range lowerCamelCase__ : int = proj_init_std lowerCamelCase__ : List[str] = init_std lowerCamelCase__ : int = layer_norm_epsilon super().__init__(eos_token_id=lowerCamelCase_, **lowerCamelCase_ ) @property def a__ (self ): '''simple docstring''' logger.info(f'''The model {self.model_type} is one of the few models that has no sequence length limit.''' ) return -1 @max_position_embeddings.setter def a__ (self, lowerCamelCase_ ): '''simple docstring''' raise NotImplementedError( f'''The model {self.model_type} is one of the few models that has no sequence length limit.''' )
316
"""simple docstring""" class a_ : '''simple docstring''' def __init__(self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Tuple = len(lowerCamelCase_ ) lowerCamelCase__ : Any = [0] * len_array if len_array > 0: lowerCamelCase__ : Union[str, Any] = array[0] for i in range(1, lowerCamelCase_ ): lowerCamelCase__ : Optional[int] = self.prefix_sum[i - 1] + array[i] def a__ (self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Dict = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(lowerCamelCase_ ) return False if __name__ == "__main__": import doctest doctest.testmod()
316
1
"""simple docstring""" def lowerCamelCase_ ( _lowerCamelCase = 10 , _lowerCamelCase = 22 ): lowerCamelCase__ : int = range(1 , _lowerCamelCase ) lowerCamelCase__ : Optional[Any] = range(1 , _lowerCamelCase ) return sum( 1 for power in powers for base in bases if len(str(base**power ) ) == power ) if __name__ == "__main__": print(f"{solution(10, 22) = }")
316
"""simple docstring""" import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : Dict = ['image_processor', 'tokenizer'] lowerCamelCase__ : Optional[int] = 'CLIPImageProcessor' lowerCamelCase__ : List[str] = ('XLMRobertaTokenizer', 'XLMRobertaTokenizerFast') def __init__(self, lowerCamelCase_=None, lowerCamelCase_=None, **lowerCamelCase_ ): '''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.', lowerCamelCase_, ) 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__(lowerCamelCase_, lowerCamelCase_ ) def __call__(self, lowerCamelCase_=None, lowerCamelCase_=None, lowerCamelCase_=None, **lowerCamelCase_ ): '''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__ : Any = self.tokenizer(lowerCamelCase_, return_tensors=lowerCamelCase_, **lowerCamelCase_ ) if images is not None: lowerCamelCase__ : List[Any] = self.image_processor(lowerCamelCase_, return_tensors=lowerCamelCase_, **lowerCamelCase_ ) if text is not None and images is not None: lowerCamelCase__ : str = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**lowerCamelCase_ ), tensor_type=lowerCamelCase_ ) def a__ (self, *lowerCamelCase_, **lowerCamelCase_ ): '''simple docstring''' return self.tokenizer.batch_decode(*lowerCamelCase_, **lowerCamelCase_ ) def a__ (self, *lowerCamelCase_, **lowerCamelCase_ ): '''simple docstring''' return self.tokenizer.decode(*lowerCamelCase_, **lowerCamelCase_ ) @property def a__ (self ): '''simple docstring''' lowerCamelCase__ : str = self.tokenizer.model_input_names lowerCamelCase__ : List[str] = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
316
1
"""simple docstring""" from typing import Any class a_ : '''simple docstring''' def __init__(self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : str = data lowerCamelCase__ : Optional[Any] = None def __repr__(self ): '''simple docstring''' return f'''Node({self.data})''' class a_ : '''simple docstring''' def __init__(self ): '''simple docstring''' lowerCamelCase__ : List[str] = None def __iter__(self ): '''simple docstring''' lowerCamelCase__ : str = self.head while node: yield node.data lowerCamelCase__ : List[Any] = node.next def __len__(self ): '''simple docstring''' return sum(1 for _ in self ) def __repr__(self ): '''simple docstring''' return "->".join([str(lowerCamelCase_ ) for item in self] ) def __getitem__(self, lowerCamelCase_ ): '''simple docstring''' if not 0 <= index < len(self ): raise ValueError('list index out of range.' ) for i, node in enumerate(self ): if i == index: return node return None def __setitem__(self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' if not 0 <= index < len(self ): raise ValueError('list index out of range.' ) lowerCamelCase__ : Union[str, Any] = self.head for _ in range(lowerCamelCase_ ): lowerCamelCase__ : Tuple = current.next lowerCamelCase__ : str = data def a__ (self, lowerCamelCase_ ): '''simple docstring''' self.insert_nth(len(self ), lowerCamelCase_ ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' self.insert_nth(0, lowerCamelCase_ ) def a__ (self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' if not 0 <= index <= len(self ): raise IndexError('list index out of range' ) lowerCamelCase__ : Optional[Any] = Node(lowerCamelCase_ ) if self.head is None: lowerCamelCase__ : List[str] = new_node elif index == 0: lowerCamelCase__ : int = self.head # link new_node to head lowerCamelCase__ : List[Any] = new_node else: lowerCamelCase__ : Optional[int] = self.head for _ in range(index - 1 ): lowerCamelCase__ : Union[str, Any] = temp.next lowerCamelCase__ : List[str] = temp.next lowerCamelCase__ : str = new_node def a__ (self ): # print every node data '''simple docstring''' print(self ) def a__ (self ): '''simple docstring''' return self.delete_nth(0 ) def a__ (self ): # delete from tail '''simple docstring''' return self.delete_nth(len(self ) - 1 ) def a__ (self, lowerCamelCase_ = 0 ): '''simple docstring''' if not 0 <= index <= len(self ) - 1: # test if index is valid raise IndexError('List index out of range.' ) lowerCamelCase__ : str = self.head # default first node if index == 0: lowerCamelCase__ : Any = self.head.next else: lowerCamelCase__ : str = self.head for _ in range(index - 1 ): lowerCamelCase__ : int = temp.next lowerCamelCase__ : List[Any] = temp.next lowerCamelCase__ : int = temp.next.next return delete_node.data def a__ (self ): '''simple docstring''' return self.head is None def a__ (self ): '''simple docstring''' lowerCamelCase__ : str = None lowerCamelCase__ : Union[str, Any] = self.head while current: # Store the current node's next node. lowerCamelCase__ : int = current.next # Make the current node's next point backwards lowerCamelCase__ : Dict = prev # Make the previous node be the current node lowerCamelCase__ : Optional[Any] = current # Make the current node the next node (to progress iteration) lowerCamelCase__ : Dict = next_node # Return prev in order to put the head at the end lowerCamelCase__ : Optional[Any] = prev def lowerCamelCase_ ( ): lowerCamelCase__ : Optional[Any] = LinkedList() assert linked_list.is_empty() is True assert str(_lowerCamelCase ) == "" try: linked_list.delete_head() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. try: linked_list.delete_tail() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. for i in range(10 ): assert len(_lowerCamelCase ) == i linked_list.insert_nth(_lowerCamelCase , i + 1 ) assert str(_lowerCamelCase ) == "->".join(str(_lowerCamelCase ) for i in range(1 , 11 ) ) linked_list.insert_head(0 ) linked_list.insert_tail(11 ) assert str(_lowerCamelCase ) == "->".join(str(_lowerCamelCase ) for i in range(0 , 12 ) ) assert linked_list.delete_head() == 0 assert linked_list.delete_nth(9 ) == 10 assert linked_list.delete_tail() == 11 assert len(_lowerCamelCase ) == 9 assert str(_lowerCamelCase ) == "->".join(str(_lowerCamelCase ) for i in range(1 , 10 ) ) assert all(linked_list[i] == i + 1 for i in range(0 , 9 ) ) is True for i in range(0 , 9 ): lowerCamelCase__ : Union[str, Any] = -i assert all(linked_list[i] == -i for i in range(0 , 9 ) ) is True linked_list.reverse() assert str(_lowerCamelCase ) == "->".join(str(_lowerCamelCase ) for i in range(-8 , 1 ) ) def lowerCamelCase_ ( ): lowerCamelCase__ : Optional[int] = [ -9, 100, Node(7734_5112 ), 'dlrow olleH', 7, 5555, 0, -192.55_555, 'Hello, world!', 77.9, Node(10 ), None, None, 12.20, ] lowerCamelCase__ : Optional[int] = LinkedList() for i in test_input: linked_list.insert_tail(_lowerCamelCase ) # Check if it's empty or not assert linked_list.is_empty() is False assert ( str(_lowerCamelCase ) == "-9->100->Node(77345112)->dlrow olleH->7->5555->0->" "-192.55555->Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the head lowerCamelCase__ : Optional[int] = linked_list.delete_head() assert result == -9 assert ( str(_lowerCamelCase ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the tail lowerCamelCase__ : List[Any] = linked_list.delete_tail() assert result == 12.2 assert ( str(_lowerCamelCase ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None" ) # Delete a node in specific location in linked list lowerCamelCase__ : str = linked_list.delete_nth(10 ) assert result is None assert ( str(_lowerCamelCase ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None" ) # Add a Node instance to its head linked_list.insert_head(Node('Hello again, world!' ) ) assert ( str(_lowerCamelCase ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None" ) # Add None to its tail linked_list.insert_tail(_lowerCamelCase ) assert ( str(_lowerCamelCase ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None->None" ) # Reverse the linked list linked_list.reverse() assert ( str(_lowerCamelCase ) == "None->None->Node(10)->77.9->Hello, world!->-192.55555->0->5555->" "7->dlrow olleH->Node(77345112)->100->Node(Hello again, world!)" ) def lowerCamelCase_ ( ): from doctest import testmod testmod() lowerCamelCase__ : int = LinkedList() linked_list.insert_head(input('Inserting 1st at head ' ).strip() ) linked_list.insert_head(input('Inserting 2nd at head ' ).strip() ) print('\nPrint list:' ) linked_list.print_list() linked_list.insert_tail(input('\nInserting 1st at tail ' ).strip() ) linked_list.insert_tail(input('Inserting 2nd at tail ' ).strip() ) print('\nPrint list:' ) linked_list.print_list() print('\nDelete head' ) linked_list.delete_head() print('Delete tail' ) linked_list.delete_tail() print('\nPrint list:' ) linked_list.print_list() print('\nReverse linked list' ) linked_list.reverse() print('\nPrint list:' ) linked_list.print_list() print('\nString representation of linked list:' ) print(_lowerCamelCase ) print('\nReading/changing Node data using indexing:' ) print(f'''Element at Position 1: {linked_list[1]}''' ) lowerCamelCase__ : Optional[Any] = input('Enter New Value: ' ).strip() print('New list:' ) print(_lowerCamelCase ) print(f'''length of linked_list is : {len(_lowerCamelCase )}''' ) if __name__ == "__main__": main()
316
"""simple docstring""" import cva import numpy as np class a_ : '''simple docstring''' def __init__(self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' if k in (0.04, 0.06): lowerCamelCase__ : Tuple = k lowerCamelCase__ : Optional[Any] = window_size else: raise ValueError('invalid k value' ) def __str__(self ): '''simple docstring''' return str(self.k ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = cva.imread(lowerCamelCase_, 0 ) lowerCamelCase__ , lowerCamelCase__ : Union[str, Any] = img.shape lowerCamelCase__ : list[list[int]] = [] lowerCamelCase__ : Optional[Any] = img.copy() lowerCamelCase__ : Optional[Any] = cva.cvtColor(lowerCamelCase_, cva.COLOR_GRAY2RGB ) lowerCamelCase__ , lowerCamelCase__ : Any = np.gradient(lowerCamelCase_ ) lowerCamelCase__ : Optional[Any] = dx**2 lowerCamelCase__ : List[Any] = dy**2 lowerCamelCase__ : List[str] = dx * dy lowerCamelCase__ : Tuple = 0.04 lowerCamelCase__ : List[Any] = self.window_size // 2 for y in range(lowerCamelCase_, h - offset ): for x in range(lowerCamelCase_, w - offset ): lowerCamelCase__ : Union[str, Any] = ixx[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() lowerCamelCase__ : Optional[Any] = iyy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() lowerCamelCase__ : List[Any] = ixy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() lowerCamelCase__ : str = (wxx * wyy) - (wxy**2) lowerCamelCase__ : Dict = wxx + wyy lowerCamelCase__ : 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_5_5 ) return color_img, corner_list if __name__ == "__main__": A_ : Optional[Any] = HarrisCorner(0.04, 3) A_, A_ : List[Any] = edge_detect.detect("path_to_image") cva.imwrite("detect.png", color_img)
316
1
"""simple docstring""" from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ShapEPipeline else: from .camera import create_pan_cameras from .pipeline_shap_e import ShapEPipeline from .pipeline_shap_e_img2img import ShapEImgaImgPipeline from .renderer import ( BoundingBoxVolume, ImportanceRaySampler, MLPNeRFModelOutput, MLPNeRSTFModel, ShapEParamsProjModel, ShapERenderer, StratifiedRaySampler, VoidNeRFModel, )
316
"""simple docstring""" from collections.abc import Iterator, MutableMapping from dataclasses import dataclass from typing import Generic, TypeVar A_ : str = TypeVar("KEY") A_ : List[Any] = TypeVar("VAL") @dataclass(frozen=snake_case_ , slots=snake_case_ ) class a_ ( Generic[KEY, VAL] ): '''simple docstring''' lowerCamelCase__ : KEY lowerCamelCase__ : VAL class a_ ( _Item ): '''simple docstring''' def __init__(self ): '''simple docstring''' super().__init__(lowerCamelCase_, lowerCamelCase_ ) def __bool__(self ): '''simple docstring''' return False A_ : List[Any] = _DeletedItem() class a_ ( MutableMapping[KEY, VAL] ): '''simple docstring''' def __init__(self, lowerCamelCase_ = 8, lowerCamelCase_ = 0.75 ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = initial_block_size lowerCamelCase__ : list[_Item | None] = [None] * initial_block_size assert 0.0 < capacity_factor < 1.0 lowerCamelCase__ : List[Any] = capacity_factor lowerCamelCase__ : Optional[int] = 0 def a__ (self, lowerCamelCase_ ): '''simple docstring''' return hash(lowerCamelCase_ ) % len(self._buckets ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' return (ind + 1) % len(self._buckets ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Optional[int] = self._buckets[ind] if not stored: lowerCamelCase__ : Tuple = _Item(lowerCamelCase_, lowerCamelCase_ ) self._len += 1 return True elif stored.key == key: lowerCamelCase__ : Optional[int] = _Item(lowerCamelCase_, lowerCamelCase_ ) return True else: return False def a__ (self ): '''simple docstring''' lowerCamelCase__ : int = len(self._buckets ) * self._capacity_factor return len(self ) >= int(lowerCamelCase_ ) def a__ (self ): '''simple docstring''' if len(self._buckets ) <= self._initial_block_size: return False lowerCamelCase__ : Any = len(self._buckets ) * self._capacity_factor / 2 return len(self ) < limit def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Any = self._buckets lowerCamelCase__ : Dict = [None] * new_size lowerCamelCase__ : Tuple = 0 for item in old_buckets: if item: self._add_item(item.key, item.val ) def a__ (self ): '''simple docstring''' self._resize(len(self._buckets ) * 2 ) def a__ (self ): '''simple docstring''' self._resize(len(self._buckets ) // 2 ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = self._get_bucket_index(lowerCamelCase_ ) for _ in range(len(self._buckets ) ): yield ind lowerCamelCase__ : Tuple = self._get_next_ind(lowerCamelCase_ ) def a__ (self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' for ind in self._iterate_buckets(lowerCamelCase_ ): if self._try_set(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): break def __setitem__(self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' if self._is_full(): self._size_up() self._add_item(lowerCamelCase_, lowerCamelCase_ ) def __delitem__(self, lowerCamelCase_ ): '''simple docstring''' for ind in self._iterate_buckets(lowerCamelCase_ ): lowerCamelCase__ : List[str] = self._buckets[ind] if item is None: raise KeyError(lowerCamelCase_ ) if item is _deleted: continue if item.key == key: lowerCamelCase__ : Optional[int] = _deleted self._len -= 1 break if self._is_sparse(): self._size_down() def __getitem__(self, lowerCamelCase_ ): '''simple docstring''' for ind in self._iterate_buckets(lowerCamelCase_ ): lowerCamelCase__ : List[Any] = self._buckets[ind] if item is None: break if item is _deleted: continue if item.key == key: return item.val raise KeyError(lowerCamelCase_ ) def __len__(self ): '''simple docstring''' return self._len def __iter__(self ): '''simple docstring''' yield from (item.key for item in self._buckets if item) def __repr__(self ): '''simple docstring''' lowerCamelCase__ : List[str] = ' ,'.join( f'''{item.key}: {item.val}''' for item in self._buckets if item ) return f'''HashMap({val_string})'''
316
1
"""simple docstring""" 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 A_ : Tuple = get_tests_dir("fixtures/test_sentencepiece.model") @require_sentencepiece class a_ ( snake_case_ , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ : str = XLMProphetNetTokenizer lowerCamelCase__ : Union[str, Any] = False lowerCamelCase__ : Union[str, Any] = True def a__ (self ): '''simple docstring''' super().setUp() # We have a SentencePiece fixture for testing lowerCamelCase__ : List[Any] = XLMProphetNetTokenizer(lowerCamelCase_, keep_accents=lowerCamelCase_ ) tokenizer.save_pretrained(self.tmpdirname ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : int = '[PAD]' lowerCamelCase__ : str = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCamelCase_ ), lowerCamelCase_ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCamelCase_ ), lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : str = 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(lowerCamelCase_ ), 1_0_1_2 ) def a__ (self ): '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size, 1_0_1_2 ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Any = XLMProphetNetTokenizer(lowerCamelCase_, keep_accents=lowerCamelCase_ ) lowerCamelCase__ : Optional[Any] = tokenizer.tokenize('This is a test' ) self.assertListEqual(lowerCamelCase_, ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(lowerCamelCase_ ), [value + tokenizer.fairseq_offset for value in [2_8_5, 4_6, 1_0, 1_7_0, 3_8_2]], ) lowerCamelCase__ : Optional[Any] = 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', 'é', '.', ], ) lowerCamelCase__ : str = tokenizer.convert_tokens_to_ids(lowerCamelCase_ ) self.assertListEqual( lowerCamelCase_, [ value + tokenizer.fairseq_offset for value in [8, 2_1, 8_4, 5_5, 2_4, 1_9, 7, -9, 6_0_2, 3_4_7, 3_4_7, 3_4_7, 3, 1_2, 6_6, 4_6, 7_2, 8_0, 6, -9, 4] ], ) lowerCamelCase__ : List[str] = 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 a__ (self ): '''simple docstring''' return XLMProphetNetTokenizer.from_pretrained('microsoft/xprophetnet-large-wiki100-cased' ) @slow def a__ (self ): '''simple docstring''' lowerCamelCase__ : List[Any] = 'Hello World!' lowerCamelCase__ : Tuple = [3_5_3_8_9, 6_6_7_2, 4_9, 2] self.assertListEqual(lowerCamelCase_, self.big_tokenizer.encode(lowerCamelCase_ ) ) @slow def a__ (self ): '''simple docstring''' lowerCamelCase__ : Tuple = {'input_ids': [[1_1_0_7_3, 8_2_7_8_3, 1_8, 2_6, 8_2_7_8_3, 5_4_9, 5_1_5_4_0, 2_4_8, 1_7_2_0_9, 1_3_0_1, 2_1_7, 2_0, 2_1_5_1_8_6, 1_3_2_5, 1_4_7, 1_7_2_0_9, 1_3_0_1, 2_1_7, 2_0, 5_6_3_7_0, 5_3, 1_2_2_0_2_0, 2_0, 1_6_4_7_7, 2_7, 8_7_3_5_5, 4_5_4_8, 2_0, 4_7_2_8, 7_8_3_9_2, 1_7, 1_5_9_9_6_9, 1_8, 2_6, 2_4_4_9_1, 6_2_9, 1_5, 5_3_8, 2_2_7_0_4, 5_4_3_9, 1_5, 2_7_8_8, 2_4_4_9_1, 9_8_8_5, 1_5, 4_3_5_3_4, 6_0_5, 1_5, 8_1_4, 1_8_4_0_3, 3_3_2_0_0, 2_9, 1_5, 4_3_5_3_4, 2_4_4_5_8, 1_2_4_1_0, 1_1_1, 2_4_9_6_6, 8_3_6_6_9, 9_6_3_7, 1_4_4_0_6_8, 2_6, 8_5_0, 2_2_3_4_6, 2_7, 1_4_7, 2_4_9_6_6, 8_3_6_6_9, 8_3_4_9_0, 2_6, 3_9_1_1_3, 7_3_5, 2_7, 6_8_9, 6_5_6, 2_8_0_0, 1_3_3_9, 4_6_0_0, 5_3, 1_2_2_0_2_0, 1_1_5_7_8_5, 3_4, 8_1_6, 1_3_3_9, 4_6_8_8_7, 1_8, 1_4_7, 5_3_9_0_5, 1_9_5_1, 4_2_2_3_8, 4_1_1_7_0, 1_7_7_3_2, 8_3_4, 4_3_6, 1_5, 2_7_5_2_3, 9_8_7_3_3, 2_1_7, 1_4_7, 5_5_4_2, 4_9_8_1, 9_3_0, 1_7_3_4_7, 1_6, 2], [2_0_0_9_1, 6_2_9, 9_4, 8_2_7_8_6, 5_8, 4_9_0, 2_0, 1_5_2_8, 8_4, 5_3_9_0_5, 3_4_4, 8_0_5_9_2, 1_1_0_1_2_8, 1_8_8_2_2, 5_2_6_7, 1_3_0_6, 6_2, 1_5_2_5_3_7, 3_0_8, 7_9_9_7, 4_0_1, 1_2_4_4_2_7, 5_4_9, 3_5_4_4_2, 2_2_5, 1_0_9, 1_5_0_5_5, 2_5_7_4_8, 1_4_7, 7_1_1_9, 4_3_7_1_2, 3_4, 7_6_7, 1_3_5_3_6_6, 1_8, 1_6, 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], [5_9_2, 6_3_7_8_4, 1_1_9_4_6_6, 1_7, 1_4_7_8_0_8, 8_8_2_1_4, 1_8, 6_5_6, 8_1, 3_2, 3_2_9_6, 1_0_2_8_0, 1_6, 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=lowerCamelCase_, model_name='microsoft/xprophetnet-large-wiki100-cased', revision='1acad1643ddd54a44df6a1b797ada8373685d90e', )
316
"""simple docstring""" def lowerCamelCase_ ( ): lowerCamelCase__ : Optional[Any] = [] lowerCamelCase__ : List[Any] = 1 while len(_lowerCamelCase ) < 1e6: constant.append(str(_lowerCamelCase ) ) i += 1 lowerCamelCase__ : str = ''.join(_lowerCamelCase ) return ( int(constant[0] ) * int(constant[9] ) * int(constant[99] ) * int(constant[999] ) * int(constant[9999] ) * int(constant[9_9999] ) * int(constant[99_9999] ) ) if __name__ == "__main__": print(solution())
316
1
"""simple docstring""" def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): if discount_rate < 0: raise ValueError('Discount rate cannot be negative' ) if not cash_flows: raise ValueError('Cash flows list cannot be empty' ) lowerCamelCase__ : str = sum( cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(_lowerCamelCase ) ) return round(_lowerCamelCase , ndigits=2 ) if __name__ == "__main__": import doctest doctest.testmod()
316
"""simple docstring""" # Lint as: python3 # pylint: enable=line-too-long # pylint: disable=g-import-not-at-top,g-bad-import-order,wrong-import-position A_ : Union[str, Any] = "2.13.1" import platform import pyarrow from packaging import version if version.parse(platform.python_version()) < version.parse("3.7"): raise ImportWarning( "To use `datasets`, Python>=3.7 is required, and the current version of Python doesn't match this condition." ) if version.parse(pyarrow.__version__).major < 8: raise ImportWarning( "To use `datasets`, the module `pyarrow>=8.0.0` is required, and the current version of `pyarrow` doesn't match this condition.\n" "If you are running this in a Google Colab, you should probably just restart the runtime to use the right version of `pyarrow`." ) del platform del pyarrow del version from .arrow_dataset import Dataset from .arrow_reader import ReadInstruction from .builder import ArrowBasedBuilder, BeamBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder from .combine import concatenate_datasets, interleave_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .download import * from .features import * from .fingerprint import disable_caching, enable_caching, is_caching_enabled, set_caching_enabled from .info import DatasetInfo, MetricInfo from .inspect import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, list_datasets, list_metrics, ) from .iterable_dataset import IterableDataset from .load import load_dataset, load_dataset_builder, load_from_disk, load_metric from .metric import Metric from .splits import ( NamedSplit, NamedSplitAll, Split, SplitBase, SplitDict, SplitGenerator, SplitInfo, SubSplitInfo, percent, ) from .tasks import * from .utils import * from .utils import logging # deprecated modules from datasets import arrow_dataset as _arrow_dataset # isort:skip from datasets import utils as _utils # isort:skip from datasets.utils import download_manager as _deprecated_download_manager # isort:skip A_ : int = concatenate_datasets A_ : Any = DownloadConfig A_ : List[Any] = DownloadManager A_ : Optional[Any] = DownloadMode A_ : List[str] = DownloadConfig A_ : Optional[int] = DownloadMode A_ : Dict = DownloadManager del _arrow_dataset, _utils, _deprecated_download_manager
316
1
"""simple docstring""" import argparse import os import torch from diffusers import ( CMStochasticIterativeScheduler, ConsistencyModelPipeline, UNetaDModel, ) A_ : Optional[int] = { "sample_size": 32, "in_channels": 3, "out_channels": 3, "layers_per_block": 2, "num_class_embeds": 10_00, "block_out_channels": [32, 64], "attention_head_dim": 8, "down_block_types": [ "ResnetDownsampleBlock2D", "AttnDownBlock2D", ], "up_block_types": [ "AttnUpBlock2D", "ResnetUpsampleBlock2D", ], "resnet_time_scale_shift": "scale_shift", "upsample_type": "resnet", "downsample_type": "resnet", } A_ : str = { "sample_size": 64, "in_channels": 3, "out_channels": 3, "layers_per_block": 3, "num_class_embeds": 10_00, "block_out_channels": [1_92, 1_92 * 2, 1_92 * 3, 1_92 * 4], "attention_head_dim": 64, "down_block_types": [ "ResnetDownsampleBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", ], "up_block_types": [ "AttnUpBlock2D", "AttnUpBlock2D", "AttnUpBlock2D", "ResnetUpsampleBlock2D", ], "resnet_time_scale_shift": "scale_shift", "upsample_type": "resnet", "downsample_type": "resnet", } A_ : Dict = { "sample_size": 2_56, "in_channels": 3, "out_channels": 3, "layers_per_block": 2, "num_class_embeds": None, "block_out_channels": [2_56, 2_56, 2_56 * 2, 2_56 * 2, 2_56 * 4, 2_56 * 4], "attention_head_dim": 64, "down_block_types": [ "ResnetDownsampleBlock2D", "ResnetDownsampleBlock2D", "ResnetDownsampleBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", ], "up_block_types": [ "AttnUpBlock2D", "AttnUpBlock2D", "AttnUpBlock2D", "ResnetUpsampleBlock2D", "ResnetUpsampleBlock2D", "ResnetUpsampleBlock2D", ], "resnet_time_scale_shift": "default", "upsample_type": "resnet", "downsample_type": "resnet", } A_ : List[str] = { "num_train_timesteps": 40, "sigma_min": 0.002, "sigma_max": 80.0, } A_ : Union[str, Any] = { "num_train_timesteps": 2_01, "sigma_min": 0.002, "sigma_max": 80.0, } A_ : Union[str, Any] = { "num_train_timesteps": 1_51, "sigma_min": 0.002, "sigma_max": 80.0, } def lowerCamelCase_ ( _lowerCamelCase ): if isinstance(_lowerCamelCase , _lowerCamelCase ): return v if v.lower() in ("yes", "true", "t", "y", "1"): return True elif v.lower() in ("no", "false", "f", "n", "0"): return False else: raise argparse.ArgumentTypeError('boolean value expected' ) def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase=False ): lowerCamelCase__ : int = checkpoint[f'''{old_prefix}.in_layers.0.weight'''] lowerCamelCase__ : List[Any] = checkpoint[f'''{old_prefix}.in_layers.0.bias'''] lowerCamelCase__ : int = checkpoint[f'''{old_prefix}.in_layers.2.weight'''] lowerCamelCase__ : Optional[Any] = checkpoint[f'''{old_prefix}.in_layers.2.bias'''] lowerCamelCase__ : List[str] = checkpoint[f'''{old_prefix}.emb_layers.1.weight'''] lowerCamelCase__ : Tuple = checkpoint[f'''{old_prefix}.emb_layers.1.bias'''] lowerCamelCase__ : Any = checkpoint[f'''{old_prefix}.out_layers.0.weight'''] lowerCamelCase__ : List[str] = checkpoint[f'''{old_prefix}.out_layers.0.bias'''] lowerCamelCase__ : List[Any] = checkpoint[f'''{old_prefix}.out_layers.3.weight'''] lowerCamelCase__ : List[Any] = checkpoint[f'''{old_prefix}.out_layers.3.bias'''] if has_skip: lowerCamelCase__ : Optional[int] = checkpoint[f'''{old_prefix}.skip_connection.weight'''] lowerCamelCase__ : List[Any] = checkpoint[f'''{old_prefix}.skip_connection.bias'''] return new_checkpoint def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase=None ): lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : List[str] = checkpoint[f'''{old_prefix}.qkv.weight'''].chunk(3 , dim=0 ) lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Optional[int] = checkpoint[f'''{old_prefix}.qkv.bias'''].chunk(3 , dim=0 ) lowerCamelCase__ : List[str] = checkpoint[f'''{old_prefix}.norm.weight'''] lowerCamelCase__ : List[str] = checkpoint[f'''{old_prefix}.norm.bias'''] lowerCamelCase__ : List[str] = weight_q.squeeze(-1 ).squeeze(-1 ) lowerCamelCase__ : Optional[Any] = bias_q.squeeze(-1 ).squeeze(-1 ) lowerCamelCase__ : Dict = weight_k.squeeze(-1 ).squeeze(-1 ) lowerCamelCase__ : Optional[int] = bias_k.squeeze(-1 ).squeeze(-1 ) lowerCamelCase__ : Optional[int] = weight_v.squeeze(-1 ).squeeze(-1 ) lowerCamelCase__ : List[Any] = bias_v.squeeze(-1 ).squeeze(-1 ) lowerCamelCase__ : Tuple = ( checkpoint[f'''{old_prefix}.proj_out.weight'''].squeeze(-1 ).squeeze(-1 ) ) lowerCamelCase__ : List[str] = checkpoint[f'''{old_prefix}.proj_out.bias'''].squeeze(-1 ).squeeze(-1 ) return new_checkpoint def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : Optional[int] = torch.load(_lowerCamelCase , map_location='cpu' ) lowerCamelCase__ : str = {} lowerCamelCase__ : Any = checkpoint['time_embed.0.weight'] lowerCamelCase__ : Any = checkpoint['time_embed.0.bias'] lowerCamelCase__ : int = checkpoint['time_embed.2.weight'] lowerCamelCase__ : Optional[Any] = checkpoint['time_embed.2.bias'] if unet_config["num_class_embeds"] is not None: lowerCamelCase__ : List[Any] = checkpoint['label_emb.weight'] lowerCamelCase__ : Union[str, Any] = checkpoint['input_blocks.0.0.weight'] lowerCamelCase__ : Tuple = checkpoint['input_blocks.0.0.bias'] lowerCamelCase__ : Tuple = unet_config['down_block_types'] lowerCamelCase__ : Any = unet_config['layers_per_block'] lowerCamelCase__ : int = unet_config['attention_head_dim'] lowerCamelCase__ : str = unet_config['block_out_channels'] lowerCamelCase__ : Dict = 1 lowerCamelCase__ : Union[str, Any] = channels_list[0] for i, layer_type in enumerate(_lowerCamelCase ): lowerCamelCase__ : Dict = channels_list[i] lowerCamelCase__ : int = current_channels != prev_channels if layer_type == "ResnetDownsampleBlock2D": for j in range(_lowerCamelCase ): lowerCamelCase__ : Dict = f'''down_blocks.{i}.resnets.{j}''' lowerCamelCase__ : Optional[int] = f'''input_blocks.{current_layer}.0''' lowerCamelCase__ : int = True if j == 0 and downsample_block_has_skip else False lowerCamelCase__ : Union[str, Any] = convert_resnet(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , has_skip=_lowerCamelCase ) current_layer += 1 elif layer_type == "AttnDownBlock2D": for j in range(_lowerCamelCase ): lowerCamelCase__ : Tuple = f'''down_blocks.{i}.resnets.{j}''' lowerCamelCase__ : Optional[Any] = f'''input_blocks.{current_layer}.0''' lowerCamelCase__ : str = True if j == 0 and downsample_block_has_skip else False lowerCamelCase__ : List[Any] = convert_resnet(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , has_skip=_lowerCamelCase ) lowerCamelCase__ : str = f'''down_blocks.{i}.attentions.{j}''' lowerCamelCase__ : int = f'''input_blocks.{current_layer}.1''' lowerCamelCase__ : int = convert_attention( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) current_layer += 1 if i != len(_lowerCamelCase ) - 1: lowerCamelCase__ : int = f'''down_blocks.{i}.downsamplers.0''' lowerCamelCase__ : Dict = f'''input_blocks.{current_layer}.0''' lowerCamelCase__ : Optional[Any] = convert_resnet(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) current_layer += 1 lowerCamelCase__ : Dict = current_channels # hardcoded the mid-block for now lowerCamelCase__ : Optional[int] = 'mid_block.resnets.0' lowerCamelCase__ : List[str] = 'middle_block.0' lowerCamelCase__ : str = convert_resnet(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) lowerCamelCase__ : Optional[Any] = 'mid_block.attentions.0' lowerCamelCase__ : List[Any] = 'middle_block.1' lowerCamelCase__ : Dict = convert_attention(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) lowerCamelCase__ : Optional[int] = 'mid_block.resnets.1' lowerCamelCase__ : int = 'middle_block.2' lowerCamelCase__ : Union[str, Any] = convert_resnet(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) lowerCamelCase__ : List[str] = 0 lowerCamelCase__ : Dict = unet_config['up_block_types'] for i, layer_type in enumerate(_lowerCamelCase ): if layer_type == "ResnetUpsampleBlock2D": for j in range(layers_per_block + 1 ): lowerCamelCase__ : List[str] = f'''up_blocks.{i}.resnets.{j}''' lowerCamelCase__ : List[str] = f'''output_blocks.{current_layer}.0''' lowerCamelCase__ : Optional[Any] = convert_resnet(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , has_skip=_lowerCamelCase ) current_layer += 1 if i != len(_lowerCamelCase ) - 1: lowerCamelCase__ : Optional[Any] = f'''up_blocks.{i}.upsamplers.0''' lowerCamelCase__ : Tuple = f'''output_blocks.{current_layer-1}.1''' lowerCamelCase__ : Any = convert_resnet(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) elif layer_type == "AttnUpBlock2D": for j in range(layers_per_block + 1 ): lowerCamelCase__ : Any = f'''up_blocks.{i}.resnets.{j}''' lowerCamelCase__ : int = f'''output_blocks.{current_layer}.0''' lowerCamelCase__ : Any = convert_resnet(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , has_skip=_lowerCamelCase ) lowerCamelCase__ : Union[str, Any] = f'''up_blocks.{i}.attentions.{j}''' lowerCamelCase__ : List[Any] = f'''output_blocks.{current_layer}.1''' lowerCamelCase__ : List[str] = convert_attention( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) current_layer += 1 if i != len(_lowerCamelCase ) - 1: lowerCamelCase__ : Optional[int] = f'''up_blocks.{i}.upsamplers.0''' lowerCamelCase__ : int = f'''output_blocks.{current_layer-1}.2''' lowerCamelCase__ : str = convert_resnet(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) lowerCamelCase__ : Optional[Any] = checkpoint['out.0.weight'] lowerCamelCase__ : Dict = checkpoint['out.0.bias'] lowerCamelCase__ : List[Any] = checkpoint['out.2.weight'] lowerCamelCase__ : List[Any] = checkpoint['out.2.bias'] return new_checkpoint if __name__ == "__main__": A_ : str = argparse.ArgumentParser() parser.add_argument("--unet_path", default=None, type=str, required=True, help="Path to the unet.pt to convert.") parser.add_argument( "--dump_path", default=None, type=str, required=True, help="Path to output the converted UNet model." ) parser.add_argument("--class_cond", default=True, type=str, help="Whether the model is class-conditional.") A_ : Tuple = parser.parse_args() A_ : Optional[Any] = strabool(args.class_cond) A_ : int = os.path.basename(args.unet_path) print(f"Checkpoint: {ckpt_name}") # Get U-Net config if "imagenet64" in ckpt_name: A_ : Union[str, Any] = IMAGENET_64_UNET_CONFIG elif "256" in ckpt_name and (("bedroom" in ckpt_name) or ("cat" in ckpt_name)): A_ : Optional[Any] = LSUN_256_UNET_CONFIG elif "test" in ckpt_name: A_ : Dict = TEST_UNET_CONFIG else: raise ValueError(f"Checkpoint type {ckpt_name} is not currently supported.") if not args.class_cond: A_ : Dict = None A_ : List[Any] = con_pt_to_diffuser(args.unet_path, unet_config) A_ : Optional[int] = UNetaDModel(**unet_config) image_unet.load_state_dict(converted_unet_ckpt) # Get scheduler config if "cd" in ckpt_name or "test" in ckpt_name: A_ : str = CD_SCHEDULER_CONFIG elif "ct" in ckpt_name and "imagenet64" in ckpt_name: A_ : Tuple = CT_IMAGENET_64_SCHEDULER_CONFIG elif "ct" in ckpt_name and "256" in ckpt_name and (("bedroom" in ckpt_name) or ("cat" in ckpt_name)): A_ : Dict = CT_LSUN_256_SCHEDULER_CONFIG else: raise ValueError(f"Checkpoint type {ckpt_name} is not currently supported.") A_ : Union[str, Any] = CMStochasticIterativeScheduler(**scheduler_config) A_ : Optional[Any] = ConsistencyModelPipeline(unet=image_unet, scheduler=cm_scheduler) consistency_model.save_pretrained(args.dump_path)
316
"""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 A_ : str = logging.getLogger(__name__) @dataclass @add_start_docstrings(TrainingArguments.__doc__ ) class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : bool = field(default=snake_case_ , metadata={'help': 'Whether to use SortishSampler or not.'} ) lowerCamelCase__ : bool = field( default=snake_case_ , metadata={'help': 'Whether to use generate to calculate generative metrics (ROUGE, BLEU).'} ) lowerCamelCase__ : Optional[int] = field( default=snake_case_ , 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__ : Optional[int] = field( default=snake_case_ , 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__ : Optional[Union[str, Path, GenerationConfig]] = field( default=snake_case_ , metadata={ 'help': 'Model id, file path or url pointing to a GenerationConfig json file, to use during prediction.' } , ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Any = super().to_dict() for k, v in d.items(): if isinstance(lowerCamelCase_, lowerCamelCase_ ): lowerCamelCase__ : Any = v.to_dict() return d
316
1
"""simple docstring""" import argparse from torch import nn # transformers_old should correspond to branch `save_old_prophetnet_model_structure` here # original prophetnet_checkpoints are saved under `patrickvonplaten/..._old` respectively from transformers_old.modeling_prophetnet import ( ProphetNetForConditionalGeneration as ProphetNetForConditionalGenerationOld, ) from transformers_old.modeling_xlm_prophetnet import ( XLMProphetNetForConditionalGeneration as XLMProphetNetForConditionalGenerationOld, ) from transformers import ProphetNetForConditionalGeneration, XLMProphetNetForConditionalGeneration, logging A_ : List[str] = logging.get_logger(__name__) logging.set_verbosity_info() def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): if "xprophetnet" in prophetnet_checkpoint_path: lowerCamelCase__ : Union[str, Any] = XLMProphetNetForConditionalGenerationOld.from_pretrained(_lowerCamelCase ) lowerCamelCase__ , lowerCamelCase__ : Tuple = XLMProphetNetForConditionalGeneration.from_pretrained( _lowerCamelCase , output_loading_info=_lowerCamelCase ) else: lowerCamelCase__ : Tuple = ProphetNetForConditionalGenerationOld.from_pretrained(_lowerCamelCase ) lowerCamelCase__ , lowerCamelCase__ : Union[str, Any] = ProphetNetForConditionalGeneration.from_pretrained( _lowerCamelCase , output_loading_info=_lowerCamelCase ) lowerCamelCase__ : List[str] = ['key_proj', 'value_proj', 'query_proj'] lowerCamelCase__ : List[Any] = { 'self_attn': 'ngram_self_attn', 'cross_attn': 'encoder_attn', 'cross_attn_layer_norm': 'encoder_attn_layer_norm', 'feed_forward_layer_norm': 'final_layer_norm', 'feed_forward': '', 'intermediate': 'fc1', 'output': 'fc2', 'key_proj': 'k_proj', 'query_proj': 'q_proj', 'value_proj': 'v_proj', 'word_embeddings': 'embed_tokens', 'embeddings_layer_norm': 'emb_layer_norm', 'relative_pos_embeddings': 'relative_linear', 'ngram_embeddings': 'ngram_input_embed', 'position_embeddings': 'embed_positions', } for key in loading_info["missing_keys"]: lowerCamelCase__ : Any = key.split('.' ) if attributes[0] == "lm_head": lowerCamelCase__ : Optional[int] = prophet lowerCamelCase__ : Union[str, Any] = prophet_old else: lowerCamelCase__ : Optional[int] = prophet.prophetnet lowerCamelCase__ : str = prophet_old.model lowerCamelCase__ : Tuple = False for attribute in attributes: if attribute in mapping: lowerCamelCase__ : Union[str, Any] = mapping[attribute] if not hasattr(_lowerCamelCase , _lowerCamelCase ) and len(_lowerCamelCase ) > 0: lowerCamelCase__ : Any = attribute elif hasattr(_lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : int = attribute if attribute == "weight": assert old_model.weight.shape == model.weight.shape, "Shapes have to match!" lowerCamelCase__ : List[str] = old_model.weight logger.info(f'''{attribute} is initialized.''' ) lowerCamelCase__ : int = True break elif attribute == "bias": assert old_model.bias.shape == model.bias.shape, "Shapes have to match!" lowerCamelCase__ : int = old_model.bias logger.info(f'''{attribute} is initialized''' ) lowerCamelCase__ : List[str] = True break elif attribute in special_keys and hasattr(_lowerCamelCase , 'in_proj_weight' ): lowerCamelCase__ : Optional[int] = old_model.in_proj_weight.shape[0] // 3 lowerCamelCase__ : Tuple = getattr(_lowerCamelCase , _lowerCamelCase ) param.weight.shape == old_model.in_proj_weight[:embed_dim, :].shape, "Shapes have to match" param.bias.shape == old_model.in_proj_bias[:embed_dim].shape, "Shapes have to match" if attribute == "query_proj": lowerCamelCase__ : Optional[Any] = nn.Parameter(old_model.in_proj_weight[:embed_dim, :] ) lowerCamelCase__ : Dict = nn.Parameter(old_model.in_proj_bias[:embed_dim] ) elif attribute == "key_proj": lowerCamelCase__ : Tuple = nn.Parameter(old_model.in_proj_weight[embed_dim : 2 * embed_dim, :] ) lowerCamelCase__ : Union[str, Any] = nn.Parameter(old_model.in_proj_bias[embed_dim : 2 * embed_dim] ) elif attribute == "value_proj": lowerCamelCase__ : str = nn.Parameter(old_model.in_proj_weight[2 * embed_dim :, :] ) lowerCamelCase__ : Optional[int] = nn.Parameter(old_model.in_proj_bias[2 * embed_dim :] ) lowerCamelCase__ : str = True break elif attribute == "position_embeddings": assert ( model.position_embeddings.weight.shape[-1] == old_model.embed_positions.weight.shape[-1] ), "Hidden size has to match" assert model.position_embeddings.weight.shape[0] == 512, "We want 512 position_embeddings." lowerCamelCase__ : Optional[Any] = nn.Parameter(old_model.embed_positions.weight[:512, :] ) lowerCamelCase__ : List[str] = True break if attribute.isdigit(): lowerCamelCase__ : str = model[int(_lowerCamelCase )] lowerCamelCase__ : int = old_model[int(_lowerCamelCase )] else: lowerCamelCase__ : Union[str, Any] = getattr(_lowerCamelCase , _lowerCamelCase ) if old_attribute == "": lowerCamelCase__ : int = old_model else: if not hasattr(_lowerCamelCase , _lowerCamelCase ): raise ValueError(f'''{old_model} does not have {old_attribute}''' ) lowerCamelCase__ : Union[str, Any] = getattr(_lowerCamelCase , _lowerCamelCase ) if not is_key_init: raise ValueError(f'''{key} was not correctly initialized!''' ) print(f'''Saving model to {pytorch_dump_folder_path}''' ) prophet.save_pretrained(_lowerCamelCase ) if __name__ == "__main__": A_ : int = argparse.ArgumentParser() # Required parameters parser.add_argument( "--prophetnet_checkpoint_path", default=None, type=str, required=True, help="Path the official PyTorch dump." ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) A_ : Optional[Any] = parser.parse_args() convert_prophetnet_checkpoint_to_pytorch(args.prophetnet_checkpoint_path, args.pytorch_dump_folder_path)
316
"""simple docstring""" def lowerCamelCase_ ( _lowerCamelCase ): lowerCamelCase__ : Union[str, Any] = [] lowerCamelCase__ : List[str] = [] lowerCamelCase__ : Tuple = { '^': 3, '*': 2, '/': 2, '%': 2, '+': 1, '-': 1, } # Priority of each operator lowerCamelCase__ : List[str] = len(_lowerCamelCase ) if (len(_lowerCamelCase ) > 7) else 7 # Print table header for output print( 'Symbol'.center(8 ) , 'Stack'.center(_lowerCamelCase ) , 'Postfix'.center(_lowerCamelCase ) , sep=' | ' , ) print('-' * (print_width * 3 + 7) ) for x in infix: if x.isalpha() or x.isdigit(): post_fix.append(_lowerCamelCase ) # if x is Alphabet / Digit, add it to Postfix elif x == "(": stack.append(_lowerCamelCase ) # if x is "(" push to Stack elif x == ")": # if x is ")" pop stack until "(" is encountered while stack[-1] != "(": post_fix.append(stack.pop() ) # Pop stack & add the content to Postfix stack.pop() else: if len(_lowerCamelCase ) == 0: stack.append(_lowerCamelCase ) # If stack is empty, push x to stack else: # while priority of x is not > priority of element in the stack while len(_lowerCamelCase ) > 0 and priority[x] <= priority[stack[-1]]: post_fix.append(stack.pop() ) # pop stack & add to Postfix stack.append(_lowerCamelCase ) # push x to stack print( x.center(8 ) , (''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , (''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , sep=' | ' , ) # Output in tabular format while len(_lowerCamelCase ) > 0: # while stack is not empty post_fix.append(stack.pop() ) # pop stack & add to Postfix print( ' '.center(8 ) , (''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , (''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , sep=' | ' , ) # Output in tabular format return "".join(_lowerCamelCase ) # return Postfix as str def lowerCamelCase_ ( _lowerCamelCase ): lowerCamelCase__ : Union[str, Any] = list(infix[::-1] ) # reverse the infix equation for i in range(len(_lowerCamelCase ) ): if infix[i] == "(": lowerCamelCase__ : List[Any] = ')' # change "(" to ")" elif infix[i] == ")": lowerCamelCase__ : Tuple = '(' # change ")" to "(" return (infix_2_postfix(''.join(_lowerCamelCase ) ))[ ::-1 ] # call infix_2_postfix on Infix, return reverse of Postfix if __name__ == "__main__": A_ : Tuple = input("\nEnter an Infix Equation = ") # Input an Infix equation A_ : List[str] = "".join(Infix.split()) # Remove spaces from the input print("\n\t", Infix, "(Infix) -> ", infix_2_prefix(Infix), "(Prefix)")
316
1
"""simple docstring""" import numpy as np class a_ : '''simple docstring''' def __init__(self ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = (0, 0) lowerCamelCase__ : Dict = None lowerCamelCase__ : Optional[Any] = 0 lowerCamelCase__ : Optional[int] = 0 lowerCamelCase__ : Dict = 0 def __eq__(self, lowerCamelCase_ ): '''simple docstring''' return self.position == cell.position def a__ (self ): '''simple docstring''' print(self.position ) class a_ : '''simple docstring''' def __init__(self, lowerCamelCase_=(5, 5) ): '''simple docstring''' lowerCamelCase__ : str = np.zeros(lowerCamelCase_ ) lowerCamelCase__ : Any = world_size[0] lowerCamelCase__ : Optional[int] = world_size[1] def a__ (self ): '''simple docstring''' print(self.w ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : str = [ (-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1), ] lowerCamelCase__ : Dict = cell.position[0] lowerCamelCase__ : Dict = cell.position[1] lowerCamelCase__ : List[str] = [] for n in neughbour_cord: lowerCamelCase__ : str = current_x + n[0] lowerCamelCase__ : List[str] = current_y + n[1] if 0 <= x < self.world_x_limit and 0 <= y < self.world_y_limit: lowerCamelCase__ : Union[str, Any] = Cell() lowerCamelCase__ : Any = (x, y) lowerCamelCase__ : List[str] = cell neighbours.append(lowerCamelCase_ ) return neighbours def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : str = [] lowerCamelCase__ : str = [] _open.append(_lowerCamelCase ) while _open: lowerCamelCase__ : List[Any] = np.argmin([n.f for n in _open] ) lowerCamelCase__ : Optional[int] = _open[min_f] _closed.append(_open.pop(_lowerCamelCase ) ) if current == goal: break for n in world.get_neigbours(_lowerCamelCase ): for c in _closed: if c == n: continue lowerCamelCase__ : List[str] = current.g + 1 lowerCamelCase__ , lowerCamelCase__ : str = n.position lowerCamelCase__ , lowerCamelCase__ : Optional[int] = goal.position lowerCamelCase__ : Dict = (ya - ya) ** 2 + (xa - xa) ** 2 lowerCamelCase__ : str = n.h + n.g for c in _open: if c == n and c.f < n.f: continue _open.append(_lowerCamelCase ) lowerCamelCase__ : Tuple = [] while current.parent is not None: path.append(current.position ) lowerCamelCase__ : List[str] = current.parent path.append(current.position ) return path[::-1] if __name__ == "__main__": A_ : Optional[Any] = Gridworld() # Start position and goal A_ : Optional[Any] = Cell() A_ : Optional[Any] = (0, 0) A_ : Union[str, Any] = Cell() A_ : Union[str, Any] = (4, 4) print(f"path from {start.position} to {goal.position}") A_ : Tuple = astar(world, start, goal) # Just for visual reasons. for i in s: A_ : Any = 1 print(world.w)
316
"""simple docstring""" import json import os import shutil import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoConfig, BertConfig, GPTaConfig from transformers.configuration_utils import PretrainedConfig from transformers.testing_utils import TOKEN, USER, is_staging_test sys.path.append(str(Path(__file__).parent.parent / "utils")) from test_module.custom_configuration import CustomConfig # noqa E402 A_ : Any = { "return_dict": False, "output_hidden_states": True, "output_attentions": True, "torchscript": True, "torch_dtype": "float16", "use_bfloat16": True, "tf_legacy_loss": True, "pruned_heads": {"a": 1}, "tie_word_embeddings": False, "is_decoder": True, "cross_attention_hidden_size": 1_28, "add_cross_attention": True, "tie_encoder_decoder": True, "max_length": 50, "min_length": 3, "do_sample": True, "early_stopping": True, "num_beams": 3, "num_beam_groups": 3, "diversity_penalty": 0.5, "temperature": 2.0, "top_k": 10, "top_p": 0.7, "typical_p": 0.2, "repetition_penalty": 0.8, "length_penalty": 0.8, "no_repeat_ngram_size": 5, "encoder_no_repeat_ngram_size": 5, "bad_words_ids": [1, 2, 3], "num_return_sequences": 3, "chunk_size_feed_forward": 5, "output_scores": True, "return_dict_in_generate": True, "forced_bos_token_id": 2, "forced_eos_token_id": 3, "remove_invalid_values": True, "architectures": ["BertModel"], "finetuning_task": "translation", "id2label": {0: "label"}, "label2id": {"label": "0"}, "tokenizer_class": "BertTokenizerFast", "prefix": "prefix", "bos_token_id": 6, "pad_token_id": 7, "eos_token_id": 8, "sep_token_id": 9, "decoder_start_token_id": 10, "exponential_decay_length_penalty": (5, 1.01), "suppress_tokens": [0, 1], "begin_suppress_tokens": 2, "task_specific_params": {"translation": "some_params"}, "problem_type": "regression", } @is_staging_test class a_ ( unittest.TestCase ): '''simple docstring''' @classmethod def a__ (cls ): '''simple docstring''' lowerCamelCase__ : Tuple = TOKEN HfFolder.save_token(lowerCamelCase_ ) @classmethod def a__ (cls ): '''simple docstring''' try: delete_repo(token=cls._token, repo_id='test-config' ) except HTTPError: pass try: delete_repo(token=cls._token, repo_id='valid_org/test-config-org' ) except HTTPError: pass try: delete_repo(token=cls._token, repo_id='test-dynamic-config' ) except HTTPError: pass def a__ (self ): '''simple docstring''' lowerCamelCase__ : Optional[int] = BertConfig( vocab_size=9_9, hidden_size=3_2, num_hidden_layers=5, num_attention_heads=4, intermediate_size=3_7 ) config.push_to_hub('test-config', use_auth_token=self._token ) lowerCamelCase__ : List[str] = BertConfig.from_pretrained(f'''{USER}/test-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(lowerCamelCase_, getattr(lowerCamelCase_, lowerCamelCase_ ) ) # Reset repo delete_repo(token=self._token, repo_id='test-config' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(lowerCamelCase_, repo_id='test-config', push_to_hub=lowerCamelCase_, use_auth_token=self._token ) lowerCamelCase__ : List[Any] = BertConfig.from_pretrained(f'''{USER}/test-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(lowerCamelCase_, getattr(lowerCamelCase_, lowerCamelCase_ ) ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : List[Any] = BertConfig( vocab_size=9_9, hidden_size=3_2, num_hidden_layers=5, num_attention_heads=4, intermediate_size=3_7 ) config.push_to_hub('valid_org/test-config-org', use_auth_token=self._token ) lowerCamelCase__ : int = BertConfig.from_pretrained('valid_org/test-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(lowerCamelCase_, getattr(lowerCamelCase_, lowerCamelCase_ ) ) # Reset repo delete_repo(token=self._token, repo_id='valid_org/test-config-org' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( lowerCamelCase_, repo_id='valid_org/test-config-org', push_to_hub=lowerCamelCase_, use_auth_token=self._token ) lowerCamelCase__ : Tuple = BertConfig.from_pretrained('valid_org/test-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(lowerCamelCase_, getattr(lowerCamelCase_, lowerCamelCase_ ) ) def a__ (self ): '''simple docstring''' CustomConfig.register_for_auto_class() lowerCamelCase__ : str = CustomConfig(attribute=4_2 ) config.push_to_hub('test-dynamic-config', use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual(config.auto_map, {'AutoConfig': 'custom_configuration.CustomConfig'} ) lowerCamelCase__ : List[str] = AutoConfig.from_pretrained(f'''{USER}/test-dynamic-config''', trust_remote_code=lowerCamelCase_ ) # Can't make an isinstance check because the new_config is from the FakeConfig class of a dynamic module self.assertEqual(new_config.__class__.__name__, 'CustomConfig' ) self.assertEqual(new_config.attribute, 4_2 ) class a_ ( unittest.TestCase ): '''simple docstring''' def a__ (self ): '''simple docstring''' lowerCamelCase__ : Tuple = GPTaConfig() # attempt to modify each of int/float/bool/str config records and verify they were updated lowerCamelCase__ : Union[str, Any] = c.n_embd + 1 # int lowerCamelCase__ : Optional[Any] = c.resid_pdrop + 1.0 # float lowerCamelCase__ : str = not c.scale_attn_weights # bool lowerCamelCase__ : Any = c.summary_type + 'foo' # str c.update_from_string( f'''n_embd={n_embd},resid_pdrop={resid_pdrop},scale_attn_weights={scale_attn_weights},summary_type={summary_type}''' ) self.assertEqual(lowerCamelCase_, c.n_embd, 'mismatch for key: n_embd' ) self.assertEqual(lowerCamelCase_, c.resid_pdrop, 'mismatch for key: resid_pdrop' ) self.assertEqual(lowerCamelCase_, c.scale_attn_weights, 'mismatch for key: scale_attn_weights' ) self.assertEqual(lowerCamelCase_, c.summary_type, 'mismatch for key: summary_type' ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Any = PretrainedConfig() lowerCamelCase__ : Union[str, Any] = [key for key in base_config.__dict__ if key not in config_common_kwargs] # If this part of the test fails, you have arguments to addin config_common_kwargs above. self.assertListEqual( lowerCamelCase_, ['is_encoder_decoder', '_name_or_path', '_commit_hash', 'transformers_version'] ) lowerCamelCase__ : str = [key for key, value in config_common_kwargs.items() if value == getattr(lowerCamelCase_, lowerCamelCase_ )] if len(lowerCamelCase_ ) > 0: raise ValueError( 'The following keys are set with the default values in' ' `test_configuration_common.config_common_kwargs` pick another value for them:' f''' {', '.join(lowerCamelCase_ )}.''' ) def a__ (self ): '''simple docstring''' with self.assertRaises(lowerCamelCase_ ): # config is in subfolder, the following should not work without specifying the subfolder lowerCamelCase__ : Union[str, Any] = BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert-subfolder' ) lowerCamelCase__ : Any = BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert-subfolder', subfolder='bert' ) self.assertIsNotNone(lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : int = mock.Mock() lowerCamelCase__ : str = 5_0_0 lowerCamelCase__ : Union[str, Any] = {} lowerCamelCase__ : Any = HTTPError lowerCamelCase__ : str = {} # Download this model to make sure it's in the cache. lowerCamelCase__ : Dict = BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert' ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch('requests.Session.request', return_value=lowerCamelCase_ ) as mock_head: lowerCamelCase__ : Union[str, Any] = BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert' ) # This check we did call the fake head request mock_head.assert_called() def a__ (self ): '''simple docstring''' lowerCamelCase__ : int = BertConfig.from_pretrained( 'https://huggingface.co/hf-internal-testing/tiny-random-bert/resolve/main/config.json' ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : str = AutoConfig.from_pretrained('bert-base-cased' ) lowerCamelCase__ : Optional[Any] = ['config.4.0.0.json'] with tempfile.TemporaryDirectory() as tmp_dir: configuration.save_pretrained(lowerCamelCase_ ) lowerCamelCase__ : Tuple = 2 json.dump(configuration.to_dict(), open(os.path.join(lowerCamelCase_, 'config.4.0.0.json' ), 'w' ) ) # This should pick the new configuration file as the version of Transformers is > 4.0.0 lowerCamelCase__ : List[str] = AutoConfig.from_pretrained(lowerCamelCase_ ) self.assertEqual(new_configuration.hidden_size, 2 ) # Will need to be adjusted if we reach v42 and this test is still here. # Should pick the old configuration file as the version of Transformers is < 4.42.0 lowerCamelCase__ : Optional[Any] = ['config.42.0.0.json'] lowerCamelCase__ : List[Any] = 7_6_8 configuration.save_pretrained(lowerCamelCase_ ) shutil.move(os.path.join(lowerCamelCase_, 'config.4.0.0.json' ), os.path.join(lowerCamelCase_, 'config.42.0.0.json' ) ) lowerCamelCase__ : str = AutoConfig.from_pretrained(lowerCamelCase_ ) self.assertEqual(new_configuration.hidden_size, 7_6_8 ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : int = 'hf-internal-testing/test-two-configs' import transformers as new_transformers lowerCamelCase__ : Dict = 'v4.0.0' lowerCamelCase__ , lowerCamelCase__ : str = new_transformers.models.auto.AutoConfig.from_pretrained( lowerCamelCase_, return_unused_kwargs=lowerCamelCase_ ) self.assertEqual(new_configuration.hidden_size, 2 ) # This checks `_configuration_file` ia not kept in the kwargs by mistake. self.assertDictEqual(lowerCamelCase_, {} ) # Testing an older version by monkey-patching the version in the module it's used. import transformers as old_transformers lowerCamelCase__ : Optional[Any] = 'v3.0.0' lowerCamelCase__ : Optional[int] = old_transformers.models.auto.AutoConfig.from_pretrained(lowerCamelCase_ ) self.assertEqual(old_configuration.hidden_size, 7_6_8 )
316
1
"""simple docstring""" import warnings from ...configuration_utils import PretrainedConfig from ...utils import logging A_ : Tuple = logging.get_logger(__name__) A_ : Optional[Any] = { "RUCAIBox/mvp": "https://huggingface.co/RUCAIBox/mvp/resolve/main/config.json", } class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = 'mvp' lowerCamelCase__ : List[Any] = ['past_key_values'] lowerCamelCase__ : Tuple = {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'} def __init__(self, lowerCamelCase_=5_0_2_6_7, lowerCamelCase_=1_0_2_4, lowerCamelCase_=1_2, lowerCamelCase_=4_0_9_6, lowerCamelCase_=1_6, lowerCamelCase_=1_2, lowerCamelCase_=4_0_9_6, lowerCamelCase_=1_6, lowerCamelCase_=0.0, lowerCamelCase_=0.0, lowerCamelCase_="gelu", lowerCamelCase_=1_0_2_4, lowerCamelCase_=0.1, lowerCamelCase_=0.0, lowerCamelCase_=0.0, lowerCamelCase_=0.02, lowerCamelCase_=0.0, lowerCamelCase_=False, lowerCamelCase_=True, lowerCamelCase_=1, lowerCamelCase_=0, lowerCamelCase_=2, lowerCamelCase_=True, lowerCamelCase_=2, lowerCamelCase_=2, lowerCamelCase_=False, lowerCamelCase_=1_0_0, lowerCamelCase_=8_0_0, **lowerCamelCase_, ): '''simple docstring''' lowerCamelCase__ : Any = vocab_size lowerCamelCase__ : Tuple = max_position_embeddings lowerCamelCase__ : Tuple = d_model lowerCamelCase__ : Tuple = encoder_ffn_dim lowerCamelCase__ : Dict = encoder_layers lowerCamelCase__ : List[str] = encoder_attention_heads lowerCamelCase__ : Optional[Any] = decoder_ffn_dim lowerCamelCase__ : List[str] = decoder_layers lowerCamelCase__ : int = decoder_attention_heads lowerCamelCase__ : Tuple = dropout lowerCamelCase__ : Optional[Any] = attention_dropout lowerCamelCase__ : Optional[Any] = activation_dropout lowerCamelCase__ : List[Any] = activation_function lowerCamelCase__ : Any = init_std lowerCamelCase__ : Union[str, Any] = encoder_layerdrop lowerCamelCase__ : List[Any] = decoder_layerdrop lowerCamelCase__ : List[Any] = classifier_dropout lowerCamelCase__ : Optional[Any] = use_cache lowerCamelCase__ : List[Any] = encoder_layers lowerCamelCase__ : int = scale_embedding # scale factor will be sqrt(d_model) if True lowerCamelCase__ : Optional[int] = use_prompt lowerCamelCase__ : int = prompt_length lowerCamelCase__ : int = prompt_mid_dim super().__init__( pad_token_id=lowerCamelCase_, bos_token_id=lowerCamelCase_, eos_token_id=lowerCamelCase_, is_encoder_decoder=lowerCamelCase_, decoder_start_token_id=lowerCamelCase_, forced_eos_token_id=lowerCamelCase_, **lowerCamelCase_, ) if self.forced_bos_token_id is None and kwargs.get('force_bos_token_to_be_generated', lowerCamelCase_ ): lowerCamelCase__ : List[Any] = self.bos_token_id warnings.warn( f'''Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. ''' 'The config can simply be saved and uploaded again to be fixed.' )
316
"""simple docstring""" from __future__ import annotations def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : list[list[int]] = [] lowerCamelCase__ : list[int] = [] lowerCamelCase__ : List[str] = 0 lowerCamelCase__ : List[Any] = sum(_lowerCamelCase ) create_state_space_tree(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) return result def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , ): if sum(_lowerCamelCase ) > max_sum or (remaining_nums_sum + sum(_lowerCamelCase )) < max_sum: return if sum(_lowerCamelCase ) == max_sum: result.append(_lowerCamelCase ) return for index in range(_lowerCamelCase , len(_lowerCamelCase ) ): create_state_space_tree( _lowerCamelCase , _lowerCamelCase , index + 1 , [*path, nums[index]] , _lowerCamelCase , remaining_nums_sum - nums[index] , ) A_ : Optional[Any] = [3, 34, 4, 12, 5, 2] A_ : List[str] = 9 A_ : List[Any] = generate_sum_of_subsets_soln(nums, max_sum) print(*result)
316
1
"""simple docstring""" 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_ : Union[str, Any] = {"configuration_xglm": ["XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP", "XGLMConfig"]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Optional[Any] = ["XGLMTokenizer"] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : List[Any] = ["XGLMTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : List[str] = [ "XGLM_PRETRAINED_MODEL_ARCHIVE_LIST", "XGLMForCausalLM", "XGLMModel", "XGLMPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Optional[Any] = [ "FlaxXGLMForCausalLM", "FlaxXGLMModel", "FlaxXGLMPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : str = [ "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_ : Optional[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure)
316
"""simple docstring""" from __future__ import annotations import queue class a_ : '''simple docstring''' def __init__(self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = data lowerCamelCase__ : Optional[int] = None lowerCamelCase__ : List[Any] = None def lowerCamelCase_ ( ): print('\n********Press N to stop entering at any point of time********\n' ) lowerCamelCase__ : str = input('Enter the value of the root node: ' ).strip().lower() lowerCamelCase__ : queue.Queue = queue.Queue() lowerCamelCase__ : Optional[Any] = TreeNode(int(_lowerCamelCase ) ) q.put(_lowerCamelCase ) while not q.empty(): lowerCamelCase__ : List[Any] = q.get() lowerCamelCase__ : str = f'''Enter the left node of {node_found.data}: ''' lowerCamelCase__ : Dict = input(_lowerCamelCase ).strip().lower() or 'n' if check == "n": return tree_node lowerCamelCase__ : str = TreeNode(int(_lowerCamelCase ) ) lowerCamelCase__ : Dict = left_node q.put(_lowerCamelCase ) lowerCamelCase__ : List[str] = f'''Enter the right node of {node_found.data}: ''' lowerCamelCase__ : List[str] = input(_lowerCamelCase ).strip().lower() or 'n' if check == "n": return tree_node lowerCamelCase__ : Optional[int] = TreeNode(int(_lowerCamelCase ) ) lowerCamelCase__ : Any = right_node q.put(_lowerCamelCase ) raise def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return print(node.data , end=',' ) pre_order(node.left ) pre_order(node.right ) def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return in_order(node.left ) print(node.data , end=',' ) in_order(node.right ) def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return post_order(node.left ) post_order(node.right ) print(node.data , end=',' ) def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return lowerCamelCase__ : queue.Queue = queue.Queue() q.put(_lowerCamelCase ) while not q.empty(): lowerCamelCase__ : Any = q.get() print(node_dequeued.data , end=',' ) if node_dequeued.left: q.put(node_dequeued.left ) if node_dequeued.right: q.put(node_dequeued.right ) def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return lowerCamelCase__ : queue.Queue = queue.Queue() q.put(_lowerCamelCase ) while not q.empty(): lowerCamelCase__ : List[Any] = [] while not q.empty(): lowerCamelCase__ : str = q.get() print(node_dequeued.data , end=',' ) if node_dequeued.left: list_.append(node_dequeued.left ) if node_dequeued.right: list_.append(node_dequeued.right ) print() for node in list_: q.put(_lowerCamelCase ) def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return lowerCamelCase__ : list[TreeNode] = [] lowerCamelCase__ : int = node while n or stack: while n: # start from root node, find its left child print(n.data , end=',' ) stack.append(_lowerCamelCase ) lowerCamelCase__ : Union[str, Any] = n.left # end of while means current node doesn't have left child lowerCamelCase__ : List[Any] = stack.pop() # start to traverse its right child lowerCamelCase__ : Optional[Any] = n.right def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return lowerCamelCase__ : list[TreeNode] = [] lowerCamelCase__ : int = node while n or stack: while n: stack.append(_lowerCamelCase ) lowerCamelCase__ : List[str] = n.left lowerCamelCase__ : Tuple = stack.pop() print(n.data , end=',' ) lowerCamelCase__ : Union[str, Any] = n.right def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return lowerCamelCase__ , lowerCamelCase__ : Any = [], [] lowerCamelCase__ : int = node stacka.append(_lowerCamelCase ) while stacka: # to find the reversed order of post order, store it in stack2 lowerCamelCase__ : List[str] = stacka.pop() if n.left: stacka.append(n.left ) if n.right: stacka.append(n.right ) stacka.append(_lowerCamelCase ) while stacka: # pop up from stack2 will be the post order print(stacka.pop().data , end=',' ) def lowerCamelCase_ ( _lowerCamelCase = "" , _lowerCamelCase=50 , _lowerCamelCase="*" ): if not s: return "\n" + width * char lowerCamelCase__ , lowerCamelCase__ : Dict = divmod(width - len(_lowerCamelCase ) - 2 , 2 ) return f'''{left * char} {s} {(left + extra) * char}''' if __name__ == "__main__": import doctest doctest.testmod() print(prompt("Binary Tree Traversals")) A_ : TreeNode = build_tree() print(prompt("Pre Order Traversal")) pre_order(node) print(prompt() + "\n") print(prompt("In Order Traversal")) in_order(node) print(prompt() + "\n") print(prompt("Post Order Traversal")) post_order(node) print(prompt() + "\n") print(prompt("Level Order Traversal")) level_order(node) print(prompt() + "\n") print(prompt("Actual Level Order Traversal")) level_order_actual(node) print("*" * 50 + "\n") print(prompt("Pre Order Traversal - Iteration Version")) pre_order_iter(node) print(prompt() + "\n") print(prompt("In Order Traversal - Iteration Version")) in_order_iter(node) print(prompt() + "\n") print(prompt("Post Order Traversal - Iteration Version")) post_order_iter(node) print(prompt())
316
1
"""simple docstring""" import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers from ...tokenization_utils_base import BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_gpta import GPTaTokenizer if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation A_ : int = logging.get_logger(__name__) A_ : Union[str, Any] = {"vocab_file": "vocab.json", "merges_file": "merges.txt", "tokenizer_file": "tokenizer.json"} A_ : Optional[int] = { "vocab_file": { "gpt2": "https://huggingface.co/gpt2/resolve/main/vocab.json", "gpt2-medium": "https://huggingface.co/gpt2-medium/resolve/main/vocab.json", "gpt2-large": "https://huggingface.co/gpt2-large/resolve/main/vocab.json", "gpt2-xl": "https://huggingface.co/gpt2-xl/resolve/main/vocab.json", "distilgpt2": "https://huggingface.co/distilgpt2/resolve/main/vocab.json", }, "merges_file": { "gpt2": "https://huggingface.co/gpt2/resolve/main/merges.txt", "gpt2-medium": "https://huggingface.co/gpt2-medium/resolve/main/merges.txt", "gpt2-large": "https://huggingface.co/gpt2-large/resolve/main/merges.txt", "gpt2-xl": "https://huggingface.co/gpt2-xl/resolve/main/merges.txt", "distilgpt2": "https://huggingface.co/distilgpt2/resolve/main/merges.txt", }, "tokenizer_file": { "gpt2": "https://huggingface.co/gpt2/resolve/main/tokenizer.json", "gpt2-medium": "https://huggingface.co/gpt2-medium/resolve/main/tokenizer.json", "gpt2-large": "https://huggingface.co/gpt2-large/resolve/main/tokenizer.json", "gpt2-xl": "https://huggingface.co/gpt2-xl/resolve/main/tokenizer.json", "distilgpt2": "https://huggingface.co/distilgpt2/resolve/main/tokenizer.json", }, } A_ : Any = { "gpt2": 10_24, "gpt2-medium": 10_24, "gpt2-large": 10_24, "gpt2-xl": 10_24, "distilgpt2": 10_24, } class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : str = VOCAB_FILES_NAMES lowerCamelCase__ : Optional[int] = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase__ : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase__ : Tuple = ['input_ids', 'attention_mask'] lowerCamelCase__ : Dict = GPTaTokenizer def __init__(self, lowerCamelCase_=None, lowerCamelCase_=None, lowerCamelCase_=None, lowerCamelCase_="<|endoftext|>", lowerCamelCase_="<|endoftext|>", lowerCamelCase_="<|endoftext|>", lowerCamelCase_=False, **lowerCamelCase_, ): '''simple docstring''' super().__init__( lowerCamelCase_, lowerCamelCase_, tokenizer_file=lowerCamelCase_, unk_token=lowerCamelCase_, bos_token=lowerCamelCase_, eos_token=lowerCamelCase_, add_prefix_space=lowerCamelCase_, **lowerCamelCase_, ) lowerCamelCase__ : Union[str, Any] = kwargs.pop('add_bos_token', lowerCamelCase_ ) lowerCamelCase__ : Tuple = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('add_prefix_space', lowerCamelCase_ ) != add_prefix_space: lowerCamelCase__ : Any = getattr(lowerCamelCase_, pre_tok_state.pop('type' ) ) lowerCamelCase__ : Tuple = add_prefix_space lowerCamelCase__ : int = pre_tok_class(**lowerCamelCase_ ) lowerCamelCase__ : int = add_prefix_space def a__ (self, *lowerCamelCase_, **lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = kwargs.get('is_split_into_words', lowerCamelCase_ ) assert self.add_prefix_space or not is_split_into_words, ( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*lowerCamelCase_, **lowerCamelCase_ ) def a__ (self, *lowerCamelCase_, **lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Tuple = kwargs.get('is_split_into_words', lowerCamelCase_ ) assert self.add_prefix_space or not is_split_into_words, ( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._encode_plus(*lowerCamelCase_, **lowerCamelCase_ ) def a__ (self, lowerCamelCase_, lowerCamelCase_ = None ): '''simple docstring''' lowerCamelCase__ : List[str] = self._tokenizer.model.save(lowerCamelCase_, name=lowerCamelCase_ ) return tuple(lowerCamelCase_ ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : List[str] = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(lowerCamelCase_, add_special_tokens=lowerCamelCase_ ) + [self.eos_token_id] ) if len(lowerCamelCase_ ) > self.model_max_length: lowerCamelCase__ : List[str] = input_ids[-self.model_max_length :] return input_ids
316
"""simple docstring""" # Note: if you intend to run this script make sure you look under scripts/fsmt/ # to locate the appropriate script to do the work correctly. There is a set of scripts to: # - download and prepare data and run the conversion script # - perform eval to get the best hparam into the config # - generate model_cards - useful if you have multiple models from the same paper import argparse import json import os import re from collections import OrderedDict from os.path import basename, dirname import fairseq import torch from fairseq import hub_utils from fairseq.data.dictionary import Dictionary from transformers import FSMTConfig, FSMTForConditionalGeneration from transformers.models.fsmt.tokenization_fsmt import VOCAB_FILES_NAMES from transformers.tokenization_utils_base import TOKENIZER_CONFIG_FILE from transformers.utils import WEIGHTS_NAME, logging logging.set_verbosity_warning() A_ : Optional[Any] = 2 # based on the results of a search on a range of `num_beams`, `length_penalty` and `early_stopping` # values against wmt19 test data to obtain the best BLEU scores, we will use the following defaults: # # * `num_beams`: 5 (higher scores better, but requires more memory/is slower, can be adjusted by users) # * `early_stopping`: `False` consistently scored better # * `length_penalty` varied, so will assign the best one depending on the model A_ : List[Any] = { # fairseq: "wmt19-ru-en": {"length_penalty": 1.1}, "wmt19-en-ru": {"length_penalty": 1.15}, "wmt19-en-de": {"length_penalty": 1.0}, "wmt19-de-en": {"length_penalty": 1.1}, # allenai: "wmt16-en-de-dist-12-1": {"length_penalty": 0.6}, "wmt16-en-de-dist-6-1": {"length_penalty": 0.6}, "wmt16-en-de-12-1": {"length_penalty": 0.8}, "wmt19-de-en-6-6-base": {"length_penalty": 0.6}, "wmt19-de-en-6-6-big": {"length_penalty": 0.6}, } # this remaps the different models to their organization names A_ : str = {} for m in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: A_ : str = "facebook" for m in [ "wmt16-en-de-dist-12-1", "wmt16-en-de-dist-6-1", "wmt16-en-de-12-1", "wmt19-de-en-6-6-base", "wmt19-de-en-6-6-big", ]: A_ : Optional[Any] = "allenai" def lowerCamelCase_ ( _lowerCamelCase ): # (1) remove word breaking symbol, (2) add word ending symbol where the word is not broken up, # e.g.: d = {'le@@': 5, 'tt@@': 6, 'er': 7} => {'le': 5, 'tt': 6, 'er</w>': 7} lowerCamelCase__ : List[Any] = dict((re.sub(r'@@$' , '' , _lowerCamelCase ), v) if k.endswith('@@' ) else (re.sub(r'$' , '</w>' , _lowerCamelCase ), v) for k, v in d.items() ) lowerCamelCase__ : int = '<s> <pad> </s> <unk>'.split() # restore the special tokens for k in keep_keys: del da[f'''{k}</w>'''] lowerCamelCase__ : List[str] = d[k] # restore return da def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): # prep assert os.path.exists(_lowerCamelCase ) os.makedirs(_lowerCamelCase , exist_ok=_lowerCamelCase ) print(f'''Writing results to {pytorch_dump_folder_path}''' ) # handle various types of models lowerCamelCase__ : Optional[int] = basename(_lowerCamelCase ) lowerCamelCase__ : str = dirname(_lowerCamelCase ) lowerCamelCase__ : Any = fairseq.model_parallel.models.transformer.ModelParallelTransformerModel lowerCamelCase__ : int = cls.hub_models() lowerCamelCase__ : str = {'bpe': 'fastbpe', 'tokenizer': 'moses'} lowerCamelCase__ : Optional[Any] = '.' # note: since the model dump is old, fairseq has upgraded its model some # time later, and it does a whole lot of rewrites and splits on the saved # weights, therefore we can't use torch.load() directly on the model file. # see: upgrade_state_dict(state_dict) in fairseq_model.py print(f'''using checkpoint {checkpoint_file}''' ) lowerCamelCase__ : Any = hub_utils.from_pretrained( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , archive_map=_lowerCamelCase , **_lowerCamelCase ) lowerCamelCase__ : List[str] = vars(chkpt['args']['model'] ) lowerCamelCase__ : Optional[Any] = args['source_lang'] lowerCamelCase__ : List[str] = args['target_lang'] lowerCamelCase__ : List[str] = dirname(_lowerCamelCase ) lowerCamelCase__ : Union[str, Any] = basename(_lowerCamelCase ) # dicts lowerCamelCase__ : Optional[Any] = os.path.join(_lowerCamelCase , f'''dict.{src_lang}.txt''' ) lowerCamelCase__ : Optional[Any] = os.path.join(_lowerCamelCase , f'''dict.{tgt_lang}.txt''' ) lowerCamelCase__ : Dict = Dictionary.load(_lowerCamelCase ) lowerCamelCase__ : List[Any] = rewrite_dict_keys(src_dict.indices ) lowerCamelCase__ : int = len(_lowerCamelCase ) lowerCamelCase__ : List[Any] = os.path.join(_lowerCamelCase , 'vocab-src.json' ) print(f'''Generating {src_vocab_file} of {src_vocab_size} of {src_lang} records''' ) with open(_lowerCamelCase , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(_lowerCamelCase , ensure_ascii=_lowerCamelCase , indent=_lowerCamelCase ) ) # detect whether this is a do_lower_case situation, which can be derived by checking whether we # have at least one uppercase letter in the source vocab lowerCamelCase__ : Optional[int] = True for k in src_vocab.keys(): if not k.islower(): lowerCamelCase__ : int = False break lowerCamelCase__ : str = Dictionary.load(_lowerCamelCase ) lowerCamelCase__ : Union[str, Any] = rewrite_dict_keys(tgt_dict.indices ) lowerCamelCase__ : Optional[Any] = len(_lowerCamelCase ) lowerCamelCase__ : Dict = os.path.join(_lowerCamelCase , 'vocab-tgt.json' ) print(f'''Generating {tgt_vocab_file} of {tgt_vocab_size} of {tgt_lang} records''' ) with open(_lowerCamelCase , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(_lowerCamelCase , ensure_ascii=_lowerCamelCase , indent=_lowerCamelCase ) ) # merges_file (bpecodes) lowerCamelCase__ : List[Any] = os.path.join(_lowerCamelCase , VOCAB_FILES_NAMES['merges_file'] ) for fn in ["bpecodes", "code"]: # older fairseq called the merges file "code" lowerCamelCase__ : Optional[int] = os.path.join(_lowerCamelCase , _lowerCamelCase ) if os.path.exists(_lowerCamelCase ): break with open(_lowerCamelCase , encoding='utf-8' ) as fin: lowerCamelCase__ : Union[str, Any] = fin.read() lowerCamelCase__ : Any = re.sub(r' \d+$' , '' , _lowerCamelCase , 0 , re.M ) # remove frequency number print(f'''Generating {merges_file}''' ) with open(_lowerCamelCase , 'w' , encoding='utf-8' ) as fout: fout.write(_lowerCamelCase ) # model config lowerCamelCase__ : Dict = os.path.join(_lowerCamelCase , 'config.json' ) # validate bpe/tokenizer config, as currently it's hardcoded to moses+fastbpe - # may have to modify the tokenizer if a different type is used by a future model assert args["bpe"] == "fastbpe", f'''need to extend tokenizer to support bpe={args['bpe']}''' assert args["tokenizer"] == "moses", f'''need to extend tokenizer to support bpe={args['tokenizer']}''' lowerCamelCase__ : Optional[int] = { 'architectures': ['FSMTForConditionalGeneration'], 'model_type': 'fsmt', 'activation_dropout': args['activation_dropout'], 'activation_function': 'relu', 'attention_dropout': args['attention_dropout'], 'd_model': args['decoder_embed_dim'], 'dropout': args['dropout'], 'init_std': 0.02, 'max_position_embeddings': args['max_source_positions'], 'num_hidden_layers': args['encoder_layers'], 'src_vocab_size': src_vocab_size, 'tgt_vocab_size': tgt_vocab_size, 'langs': [src_lang, tgt_lang], 'encoder_attention_heads': args['encoder_attention_heads'], 'encoder_ffn_dim': args['encoder_ffn_embed_dim'], 'encoder_layerdrop': args['encoder_layerdrop'], 'encoder_layers': args['encoder_layers'], 'decoder_attention_heads': args['decoder_attention_heads'], 'decoder_ffn_dim': args['decoder_ffn_embed_dim'], 'decoder_layerdrop': args['decoder_layerdrop'], 'decoder_layers': args['decoder_layers'], 'bos_token_id': 0, 'pad_token_id': 1, 'eos_token_id': 2, 'is_encoder_decoder': True, 'scale_embedding': not args['no_scale_embedding'], 'tie_word_embeddings': args['share_all_embeddings'], } # good hparam defaults to start with lowerCamelCase__ : str = 5 lowerCamelCase__ : Tuple = False if model_dir in best_score_hparams and "length_penalty" in best_score_hparams[model_dir]: lowerCamelCase__ : List[str] = best_score_hparams[model_dir]['length_penalty'] else: lowerCamelCase__ : List[Any] = 1.0 print(f'''Generating {fsmt_model_config_file}''' ) with open(_lowerCamelCase , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(_lowerCamelCase , ensure_ascii=_lowerCamelCase , indent=_lowerCamelCase ) ) # tokenizer config lowerCamelCase__ : Dict = os.path.join(_lowerCamelCase , _lowerCamelCase ) lowerCamelCase__ : int = { 'langs': [src_lang, tgt_lang], 'model_max_length': 1024, 'do_lower_case': do_lower_case, } print(f'''Generating {fsmt_tokenizer_config_file}''' ) with open(_lowerCamelCase , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(_lowerCamelCase , ensure_ascii=_lowerCamelCase , indent=_lowerCamelCase ) ) # model lowerCamelCase__ : List[str] = chkpt['models'][0] lowerCamelCase__ : Optional[Any] = model.state_dict() # rename keys to start with 'model.' lowerCamelCase__ : str = OrderedDict(('model.' + k, v) for k, v in model_state_dict.items() ) # remove unneeded keys lowerCamelCase__ : int = [ 'model.model', 'model.encoder.version', 'model.decoder.version', 'model.encoder_embed_tokens.weight', 'model.decoder_embed_tokens.weight', 'model.encoder.embed_positions._float_tensor', 'model.decoder.embed_positions._float_tensor', ] for k in ignore_keys: model_state_dict.pop(_lowerCamelCase , _lowerCamelCase ) lowerCamelCase__ : Any = FSMTConfig.from_pretrained(_lowerCamelCase ) lowerCamelCase__ : Union[str, Any] = FSMTForConditionalGeneration(_lowerCamelCase ) # check that it loads ok model_new.load_state_dict(_lowerCamelCase , strict=_lowerCamelCase ) # save lowerCamelCase__ : List[Any] = os.path.join(_lowerCamelCase , _lowerCamelCase ) print(f'''Generating {pytorch_weights_dump_path}''' ) torch.save(_lowerCamelCase , _lowerCamelCase ) print('Conversion is done!' ) print('\nLast step is to upload the files to s3' ) print(f'''cd {data_root}''' ) print(f'''transformers-cli upload {model_dir}''' ) if __name__ == "__main__": A_ : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--fsmt_checkpoint_path", default=None, type=str, required=True, help=( "Path to the official PyTorch checkpoint file which is expected to reside in the dump dir with dicts," " bpecodes, etc." ), ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) A_ : Dict = parser.parse_args() convert_fsmt_checkpoint_to_pytorch(args.fsmt_checkpoint_path, args.pytorch_dump_folder_path)
316
1
"""simple docstring""" from __future__ import annotations from collections import deque from collections.abc import Sequence from dataclasses import dataclass from typing import Any @dataclass class a_ : '''simple docstring''' lowerCamelCase__ : int lowerCamelCase__ : Node | None = None lowerCamelCase__ : Node | None = None def lowerCamelCase_ ( ): lowerCamelCase__ : Union[str, Any] = Node(1 ) lowerCamelCase__ : Dict = Node(2 ) lowerCamelCase__ : List[str] = Node(3 ) lowerCamelCase__ : Any = Node(4 ) lowerCamelCase__ : str = Node(5 ) return tree def lowerCamelCase_ ( _lowerCamelCase ): return [root.data, *preorder(root.left ), *preorder(root.right )] if root else [] def lowerCamelCase_ ( _lowerCamelCase ): return postorder(root.left ) + postorder(root.right ) + [root.data] if root else [] def lowerCamelCase_ ( _lowerCamelCase ): return [*inorder(root.left ), root.data, *inorder(root.right )] if root else [] def lowerCamelCase_ ( _lowerCamelCase ): return (max(height(root.left ) , height(root.right ) ) + 1) if root else 0 def lowerCamelCase_ ( _lowerCamelCase ): lowerCamelCase__ : list[Any] = [] if root is None: return output lowerCamelCase__ : Union[str, Any] = deque([root] ) while process_queue: lowerCamelCase__ : Union[str, Any] = process_queue.popleft() output.append(node.data ) if node.left: process_queue.append(node.left ) if node.right: process_queue.append(node.right ) return output def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : list[Any] = [] def populate_output(_lowerCamelCase , _lowerCamelCase ) -> None: if not root: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.left , level - 1 ) populate_output(root.right , level - 1 ) populate_output(_lowerCamelCase , _lowerCamelCase ) return output def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : list[Any] = [] def populate_output(_lowerCamelCase , _lowerCamelCase ) -> None: if root is None: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.right , level - 1 ) populate_output(root.left , level - 1 ) populate_output(_lowerCamelCase , _lowerCamelCase ) return output def lowerCamelCase_ ( _lowerCamelCase ): if root is None: return [] lowerCamelCase__ : list[Sequence[Node | None]] = [] lowerCamelCase__ : Union[str, Any] = 0 lowerCamelCase__ : Any = height(_lowerCamelCase ) for h in range(1 , height_tree + 1 ): if not flag: output.append(get_nodes_from_left_to_right(_lowerCamelCase , _lowerCamelCase ) ) lowerCamelCase__ : str = 1 else: output.append(get_nodes_from_right_to_left(_lowerCamelCase , _lowerCamelCase ) ) lowerCamelCase__ : int = 0 return output def lowerCamelCase_ ( ): # Main function for testing. lowerCamelCase__ : List[str] = make_tree() print(f'''In-order Traversal: {inorder(_lowerCamelCase )}''' ) print(f'''Pre-order Traversal: {preorder(_lowerCamelCase )}''' ) print(f'''Post-order Traversal: {postorder(_lowerCamelCase )}''' , '\n' ) print(f'''Height of Tree: {height(_lowerCamelCase )}''' , '\n' ) print('Complete Level Order Traversal: ' ) print(level_order(_lowerCamelCase ) , '\n' ) print('Level-wise order Traversal: ' ) for level in range(1 , height(_lowerCamelCase ) + 1 ): print(f'''Level {level}:''' , get_nodes_from_left_to_right(_lowerCamelCase , level=_lowerCamelCase ) ) print('\nZigZag order Traversal: ' ) print(zigzag(_lowerCamelCase ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
316
"""simple docstring""" from abc import ABC, abstractmethod from argparse import ArgumentParser class a_ ( snake_case_ ): '''simple docstring''' @staticmethod @abstractmethod def a__ (lowerCamelCase_ ): '''simple docstring''' raise NotImplementedError() @abstractmethod def a__ (self ): '''simple docstring''' raise NotImplementedError()
316
1
"""simple docstring""" import unittest from parameterized import parameterized from transformers import LlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import LlamaForCausalLM, LlamaForSequenceClassification, LlamaModel, LlamaTokenizer class a_ : '''simple docstring''' def __init__(self, lowerCamelCase_, lowerCamelCase_=1_3, lowerCamelCase_=7, lowerCamelCase_=True, lowerCamelCase_=True, lowerCamelCase_=False, lowerCamelCase_=True, lowerCamelCase_=9_9, lowerCamelCase_=3_2, lowerCamelCase_=5, lowerCamelCase_=4, lowerCamelCase_=3_7, lowerCamelCase_="gelu", lowerCamelCase_=0.1, lowerCamelCase_=0.1, lowerCamelCase_=5_1_2, lowerCamelCase_=1_6, lowerCamelCase_=2, lowerCamelCase_=0.02, lowerCamelCase_=3, lowerCamelCase_=4, lowerCamelCase_=None, ): '''simple docstring''' lowerCamelCase__ : Optional[int] = parent lowerCamelCase__ : Optional[Any] = batch_size lowerCamelCase__ : int = seq_length lowerCamelCase__ : List[Any] = is_training lowerCamelCase__ : List[Any] = use_input_mask lowerCamelCase__ : Union[str, Any] = use_token_type_ids lowerCamelCase__ : Union[str, Any] = use_labels lowerCamelCase__ : Union[str, Any] = vocab_size lowerCamelCase__ : Optional[Any] = hidden_size lowerCamelCase__ : Any = num_hidden_layers lowerCamelCase__ : Optional[Any] = num_attention_heads lowerCamelCase__ : Union[str, Any] = intermediate_size lowerCamelCase__ : Tuple = hidden_act lowerCamelCase__ : Dict = hidden_dropout_prob lowerCamelCase__ : Tuple = attention_probs_dropout_prob lowerCamelCase__ : Dict = max_position_embeddings lowerCamelCase__ : Dict = type_vocab_size lowerCamelCase__ : Optional[Any] = type_sequence_label_size lowerCamelCase__ : Optional[Any] = initializer_range lowerCamelCase__ : Optional[Any] = num_labels lowerCamelCase__ : Union[str, Any] = num_choices lowerCamelCase__ : Optional[int] = scope def a__ (self ): '''simple docstring''' lowerCamelCase__ : int = ids_tensor([self.batch_size, self.seq_length], self.vocab_size ) lowerCamelCase__ : Union[str, Any] = None if self.use_input_mask: lowerCamelCase__ : Optional[Any] = random_attention_mask([self.batch_size, self.seq_length] ) lowerCamelCase__ : Optional[int] = None if self.use_token_type_ids: lowerCamelCase__ : Any = ids_tensor([self.batch_size, self.seq_length], self.type_vocab_size ) lowerCamelCase__ : List[Any] = None lowerCamelCase__ : Tuple = None lowerCamelCase__ : Optional[Any] = None if self.use_labels: lowerCamelCase__ : Optional[Any] = ids_tensor([self.batch_size], self.type_sequence_label_size ) lowerCamelCase__ : Any = ids_tensor([self.batch_size, self.seq_length], self.num_labels ) lowerCamelCase__ : Dict = ids_tensor([self.batch_size], self.num_choices ) lowerCamelCase__ : Optional[Any] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def a__ (self ): '''simple docstring''' return LlamaConfig( vocab_size=self.vocab_size, hidden_size=self.hidden_size, num_hidden_layers=self.num_hidden_layers, num_attention_heads=self.num_attention_heads, intermediate_size=self.intermediate_size, hidden_act=self.hidden_act, hidden_dropout_prob=self.hidden_dropout_prob, attention_probs_dropout_prob=self.attention_probs_dropout_prob, max_position_embeddings=self.max_position_embeddings, type_vocab_size=self.type_vocab_size, is_decoder=lowerCamelCase_, initializer_range=self.initializer_range, ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : str = LlamaModel(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() lowerCamelCase__ : Optional[int] = model(lowerCamelCase_, attention_mask=lowerCamelCase_ ) lowerCamelCase__ : str = model(lowerCamelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, ): '''simple docstring''' lowerCamelCase__ : Any = True lowerCamelCase__ : Dict = LlamaModel(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() lowerCamelCase__ : Dict = model( lowerCamelCase_, attention_mask=lowerCamelCase_, encoder_hidden_states=lowerCamelCase_, encoder_attention_mask=lowerCamelCase_, ) lowerCamelCase__ : List[str] = model( lowerCamelCase_, attention_mask=lowerCamelCase_, encoder_hidden_states=lowerCamelCase_, ) lowerCamelCase__ : int = model(lowerCamelCase_, attention_mask=lowerCamelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = LlamaForCausalLM(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() lowerCamelCase__ : str = model(lowerCamelCase_, attention_mask=lowerCamelCase_, labels=lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.vocab_size) ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = True lowerCamelCase__ : int = True lowerCamelCase__ : List[str] = LlamaForCausalLM(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() # first forward pass lowerCamelCase__ : str = model( lowerCamelCase_, attention_mask=lowerCamelCase_, encoder_hidden_states=lowerCamelCase_, encoder_attention_mask=lowerCamelCase_, use_cache=lowerCamelCase_, ) lowerCamelCase__ : int = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids lowerCamelCase__ : str = ids_tensor((self.batch_size, 3), config.vocab_size ) lowerCamelCase__ : Optional[Any] = ids_tensor((self.batch_size, 3), vocab_size=2 ) # append to next input_ids and lowerCamelCase__ : Optional[int] = torch.cat([input_ids, next_tokens], dim=-1 ) lowerCamelCase__ : List[str] = torch.cat([input_mask, next_mask], dim=-1 ) lowerCamelCase__ : List[str] = model( lowerCamelCase_, attention_mask=lowerCamelCase_, encoder_hidden_states=lowerCamelCase_, encoder_attention_mask=lowerCamelCase_, output_hidden_states=lowerCamelCase_, )['hidden_states'][0] lowerCamelCase__ : List[str] = model( lowerCamelCase_, attention_mask=lowerCamelCase_, encoder_hidden_states=lowerCamelCase_, encoder_attention_mask=lowerCamelCase_, past_key_values=lowerCamelCase_, output_hidden_states=lowerCamelCase_, )['hidden_states'][0] # select random slice lowerCamelCase__ : str = ids_tensor((1,), output_from_past.shape[-1] ).item() lowerCamelCase__ : Optional[Any] = output_from_no_past[:, -3:, random_slice_idx].detach() lowerCamelCase__ : Optional[Any] = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(lowerCamelCase_, lowerCamelCase_, atol=1e-3 ) ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Optional[int] = self.prepare_config_and_inputs() ( ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ) : List[str] = config_and_inputs lowerCamelCase__ : Tuple = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class a_ ( snake_case_ , snake_case_ , snake_case_ , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ : Tuple = (LlamaModel, LlamaForCausalLM, LlamaForSequenceClassification) if is_torch_available() else () lowerCamelCase__ : Union[str, Any] = (LlamaForCausalLM,) if is_torch_available() else () lowerCamelCase__ : Optional[int] = ( { 'feature-extraction': LlamaModel, 'text-classification': LlamaForSequenceClassification, 'text-generation': LlamaForCausalLM, 'zero-shot': LlamaForSequenceClassification, } if is_torch_available() else {} ) lowerCamelCase__ : Optional[Any] = False lowerCamelCase__ : Optional[int] = False def a__ (self ): '''simple docstring''' lowerCamelCase__ : Any = LlamaModelTester(self ) lowerCamelCase__ : List[Any] = ConfigTester(self, config_class=lowerCamelCase_, hidden_size=3_7 ) def a__ (self ): '''simple docstring''' self.config_tester.run_common_tests() def a__ (self ): '''simple docstring''' lowerCamelCase__ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : List[str] = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: lowerCamelCase__ : str = type self.model_tester.create_and_check_model(*lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ : List[str] = self.model_tester.prepare_config_and_inputs_for_common() lowerCamelCase__ : str = 3 lowerCamelCase__ : Dict = input_dict['input_ids'] lowerCamelCase__ : Any = input_ids.ne(1 ).to(lowerCamelCase_ ) lowerCamelCase__ : str = ids_tensor([self.model_tester.batch_size], self.model_tester.type_sequence_label_size ) lowerCamelCase__ : List[Any] = LlamaForSequenceClassification(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() lowerCamelCase__ : int = model(lowerCamelCase_, attention_mask=lowerCamelCase_, labels=lowerCamelCase_ ) self.assertEqual(result.logits.shape, (self.model_tester.batch_size, self.model_tester.num_labels) ) def a__ (self ): '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() lowerCamelCase__ : List[Any] = 3 lowerCamelCase__ : Optional[Any] = 'single_label_classification' lowerCamelCase__ : Tuple = input_dict['input_ids'] lowerCamelCase__ : str = input_ids.ne(1 ).to(lowerCamelCase_ ) lowerCamelCase__ : Dict = ids_tensor([self.model_tester.batch_size], self.model_tester.type_sequence_label_size ) lowerCamelCase__ : Any = LlamaForSequenceClassification(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() lowerCamelCase__ : str = model(lowerCamelCase_, attention_mask=lowerCamelCase_, labels=lowerCamelCase_ ) self.assertEqual(result.logits.shape, (self.model_tester.batch_size, self.model_tester.num_labels) ) def a__ (self ): '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ : Any = self.model_tester.prepare_config_and_inputs_for_common() lowerCamelCase__ : List[str] = 3 lowerCamelCase__ : List[Any] = 'multi_label_classification' lowerCamelCase__ : Union[str, Any] = input_dict['input_ids'] lowerCamelCase__ : Dict = input_ids.ne(1 ).to(lowerCamelCase_ ) lowerCamelCase__ : Dict = ids_tensor( [self.model_tester.batch_size, config.num_labels], self.model_tester.type_sequence_label_size ).to(torch.float ) lowerCamelCase__ : Optional[int] = LlamaForSequenceClassification(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() lowerCamelCase__ : List[str] = model(lowerCamelCase_, attention_mask=lowerCamelCase_, labels=lowerCamelCase_ ) self.assertEqual(result.logits.shape, (self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip('LLaMA buffers include complex numbers, which breaks this test' ) def a__ (self ): '''simple docstring''' pass @parameterized.expand([('linear',), ('dynamic',)] ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() lowerCamelCase__ : str = ids_tensor([1, 1_0], config.vocab_size ) lowerCamelCase__ : Dict = ids_tensor([1, int(config.max_position_embeddings * 1.5 )], config.vocab_size ) set_seed(4_2 ) # Fixed seed at init time so the two models get the same random weights lowerCamelCase__ : Union[str, Any] = LlamaModel(lowerCamelCase_ ) original_model.to(lowerCamelCase_ ) original_model.eval() lowerCamelCase__ : Dict = original_model(lowerCamelCase_ ).last_hidden_state lowerCamelCase__ : Optional[int] = original_model(lowerCamelCase_ ).last_hidden_state set_seed(4_2 ) # Fixed seed at init time so the two models get the same random weights lowerCamelCase__ : Tuple = {'type': scaling_type, 'factor': 10.0} lowerCamelCase__ : List[Any] = LlamaModel(lowerCamelCase_ ) scaled_model.to(lowerCamelCase_ ) scaled_model.eval() lowerCamelCase__ : Dict = scaled_model(lowerCamelCase_ ).last_hidden_state lowerCamelCase__ : str = 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 a_ ( unittest.TestCase ): '''simple docstring''' @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def a__ (self ): '''simple docstring''' lowerCamelCase__ : Optional[int] = [1, 3_0_6, 4_6_5_8, 2_7_8, 6_5_9_3, 3_1_0, 2_8_3_4, 3_3_8] lowerCamelCase__ : Tuple = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-7b-hf', device_map='auto' ) lowerCamelCase__ : Optional[int] = model(torch.tensor([input_ids] ) ) # Expected mean on dim = -1 lowerCamelCase__ : Union[str, Any] = torch.tensor([[-6.6_550, -4.1_227, -4.9_859, -3.2_406, 0.8_262, -3.0_033, 1.2_964, -3.3_699]] ) torch.testing.assert_close(out.mean(-1 ), lowerCamelCase_, atol=1e-2, rtol=1e-2 ) # slicing logits[0, 0, 0:30] # fmt: off lowerCamelCase__ : Tuple = torch.tensor([-12.8_281, -7.4_453, -0.4_639, -8.0_625, -7.2_500, -8.0_000, -6.4_883, -7.7_695, -7.8_438, -7.0_312, -6.2_188, -7.1_328, -1.8_496, 1.9_961, -8.6_250, -6.7_227, -12.8_281, -6.9_492, -7.0_742, -7.7_852, -7.5_820, -7.9_062, -6.9_375, -7.9_805, -8.3_438, -8.1_562, -8.0_469, -7.6_250, -7.7_422, -7.3_398,] ) # fmt: on torch.testing.assert_close(out[0, 0, :3_0], lowerCamelCase_, atol=1e-5, rtol=1e-5 ) @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def a__ (self ): '''simple docstring''' lowerCamelCase__ : Dict = [1, 3_0_6, 4_6_5_8, 2_7_8, 6_5_9_3, 3_1_0, 2_8_3_4, 3_3_8] lowerCamelCase__ : Dict = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-hf', device_map='auto' ) lowerCamelCase__ : Optional[int] = model(torch.tensor(lowerCamelCase_ ) ) # Expected mean on dim = -1 lowerCamelCase__ : Tuple = torch.tensor([[-2.0_622, -1.2_794, -1.1_638, -0.9_788, -1.4_603, -1.0_238, -1.7_893, -1.4_411]] ) torch.testing.assert_close(out.mean(-1 ), lowerCamelCase_, atol=1e-2, rtol=1e-2 ) # slicing logits[0, 0, 0:30] # fmt: off lowerCamelCase__ : int = torch.tensor([-8.1_406, -8.0_547, 2.7_461, -1.2_344, -0.1_448, -1.8_262, -1.0_020, -1.8_154, -1.6_895, -1.8_516, -2.3_574, -0.9_277, 3.7_598, 6.5_742, -1.2_998, -0.1_177, -8.1_406, -2.9_688, -2.9_199, -3.1_699, -3.5_254, -2.3_555, -2.7_988, -3.4_141, -2.8_262, -4.5_195, -3.3_379, -3.3_164, -2.7_832, -3.0_273] ) # fmt: on torch.testing.assert_close(out[0, 0, :3_0], lowerCamelCase_, atol=1e-5, rtol=1e-5 ) @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def a__ (self ): '''simple docstring''' lowerCamelCase__ : List[str] = [1, 3_0_6, 4_6_5_8, 2_7_8, 6_5_9_3, 3_1_0, 2_8_3_4, 3_3_8] lowerCamelCase__ : Any = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-chat-hf', device_map='auto' ) lowerCamelCase__ : Dict = model(torch.tensor(lowerCamelCase_ ) ) # Expected mean on dim = -1 lowerCamelCase__ : Dict = torch.tensor([[-0.8_562, -1.8_520, -0.7_551, -0.4_162, -1.5_161, -1.2_038, -2.4_823, -2.3_254]] ) torch.testing.assert_close(out.mean(-1 ), lowerCamelCase_, atol=1e-2, rtol=1e-2 ) # slicing logits[0, 0, 0:30] # fmt: off lowerCamelCase__ : Dict = torch.tensor([-2.2_227, 4.8_828, 0.9_023, -0.4_578, -0.7_871, -0.1_033, -0.6_221, -0.5_786, -0.7_803, -1.0_674, -1.2_920, -0.1_570, 0.8_008, 2.0_723, -0.9_497, 0.2_771, -2.2_227, -0.7_612, -1.4_346, -1.2_061, -1.6_426, -0.3_000, -0.7_139, -1.1_934, -1.8_691, -1.6_973, -1.5_947, -1.2_705, -0.3_523, -0.5_513] ) # fmt: on torch.testing.assert_close(out.mean(-1 ), lowerCamelCase_, atol=1e-2, rtol=1e-2 ) @unittest.skip( 'Logits are not exactly the same, once we fix the instabalities somehow, will update! Also it is gonna be a `too_slow` test' ) @slow def a__ (self ): '''simple docstring''' lowerCamelCase__ : int = [1, 3_0_6, 4_6_5_8, 2_7_8, 6_5_9_3, 3_1_0, 2_8_3_4, 3_3_8] lowerCamelCase__ : Dict = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-70b-hf', device_map='auto' ) lowerCamelCase__ : Union[str, Any] = model(torch.tensor(lowerCamelCase_ ) ) lowerCamelCase__ : Any = torch.tensor( [[-4.2_327, -3.3_360, -4.6_665, -4.7_631, -1.8_180, -3.4_170, -1.4_211, -3.1_810]], dtype=torch.floataa ) torch.testing.assert_close(out.mean(-1 ), lowerCamelCase_, atol=1e-2, rtol=1e-2 ) # fmt: off lowerCamelCase__ : Any = torch.tensor([-9.4_922, -3.9_551, 1.7_998, -5.6_758, -5.1_055, -5.8_984, -4.8_320, -6.8_086, -6.5_391, -5.6_172, -5.5_820, -5.5_352, 1.7_881, 3.6_289, -6.5_117, -3.4_785, -9.5_000, -6.0_352, -6.8_125, -6.0_195, -6.6_836, -5.4_727, -6.2_812, -6.0_391, -7.3_398, -7.4_297, -7.4_844, -6.5_820, -5.8_789, -5.5_312] ) # fmt: on torch.testing.assert_close(out[0, 0, :3_0], lowerCamelCase_, atol=1e-5, rtol=1e-5 ) @unittest.skip('Model is curently gated' ) @slow def a__ (self ): '''simple docstring''' lowerCamelCase__ : Any = 'Simply put, the theory of relativity states that 1) the laws of physics are the same everywhere in the universe and 2) the passage of time and the length of objects can vary depending on the observer\'s frame of reference.\n\nThe first part of the theory, that the laws of physics are the same everywhere, is known as the "princi' lowerCamelCase__ : Optional[int] = 'Simply put, the theory of relativity states that ' lowerCamelCase__ : Dict = LlamaTokenizer.from_pretrained('meta-llama/Llama-2-13b-chat-hf' ) lowerCamelCase__ : Optional[int] = tokenizer.encode(lowerCamelCase_, return_tensors='pt' ) lowerCamelCase__ : Optional[int] = LlamaForCausalLM.from_pretrained( 'meta-llama/Llama-2-13b-chat-hf', device_map='sequential', use_safetensors=lowerCamelCase_ ) # greedy generation outputs lowerCamelCase__ : int = model.generate(lowerCamelCase_, max_new_tokens=6_4, top_p=lowerCamelCase_, temperature=1, do_sample=lowerCamelCase_ ) lowerCamelCase__ : Union[str, Any] = tokenizer.decode(generated_ids[0], skip_special_tokens=lowerCamelCase_ ) self.assertEqual(lowerCamelCase_, lowerCamelCase_ )
316
"""simple docstring""" import re def lowerCamelCase_ ( _lowerCamelCase ): if len(re.findall('[ATCG]' , _lowerCamelCase ) ) != len(_lowerCamelCase ): raise ValueError('Invalid Strand' ) return dna.translate(dna.maketrans('ATCG' , 'TAGC' ) ) if __name__ == "__main__": import doctest doctest.testmod()
316
1
"""simple docstring""" def lowerCamelCase_ ( _lowerCamelCase ): lowerCamelCase__ : List[Any] = len(_lowerCamelCase ) lowerCamelCase__ : List[Any] = sum(_lowerCamelCase ) lowerCamelCase__ : int = [[False for x in range(s + 1 )] for y in range(n + 1 )] for i in range(1 , n + 1 ): lowerCamelCase__ : str = True for i in range(1 , s + 1 ): lowerCamelCase__ : Any = False for i in range(1 , n + 1 ): for j in range(1 , s + 1 ): lowerCamelCase__ : Union[str, Any] = dp[i][j - 1] if arr[i - 1] <= j: lowerCamelCase__ : Dict = 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: lowerCamelCase__ : List[Any] = s - 2 * j break return diff
316
"""simple docstring""" import argparse import os import torch from transformers import FlavaImageCodebook, FlavaImageCodebookConfig def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : Any = s.rsplit(_lowerCamelCase , _lowerCamelCase ) return new.join(_lowerCamelCase ) def lowerCamelCase_ ( _lowerCamelCase ): # encoder.embeddings are double copied in original FLAVA return sum(param.float().sum() if 'encoder.embeddings' not in key else 0 for key, param in state_dict.items() ) def lowerCamelCase_ ( _lowerCamelCase ): lowerCamelCase__ : List[Any] = {} lowerCamelCase__ : Any = ['group_1', 'group_2', 'group_3', 'group_4'] for key, value in state_dict.items(): for group_key in group_keys: if group_key in key: lowerCamelCase__ : Union[str, Any] = key.replace(f'''{group_key}.''' , f'''{group_key}.group.''' ) if "res_path" in key: lowerCamelCase__ : Dict = key.replace('res_path.' , 'res_path.path.' ) if key.endswith('.w' ): lowerCamelCase__ : str = rreplace(_lowerCamelCase , '.w' , '.weight' , 1 ) if key.endswith('.b' ): lowerCamelCase__ : Optional[Any] = rreplace(_lowerCamelCase , '.b' , '.bias' , 1 ) lowerCamelCase__ : int = value.float() return upgrade @torch.no_grad() def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase=None , _lowerCamelCase=True ): from dall_e import Encoder lowerCamelCase__ : List[str] = Encoder() if os.path.exists(_lowerCamelCase ): lowerCamelCase__ : Optional[int] = torch.load(_lowerCamelCase ) else: lowerCamelCase__ : List[Any] = torch.hub.load_state_dict_from_url(_lowerCamelCase ) if isinstance(_lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : List[Any] = ckpt.state_dict() encoder.load_state_dict(_lowerCamelCase ) if config_path is not None: lowerCamelCase__ : Union[str, Any] = FlavaImageCodebookConfig.from_pretrained(_lowerCamelCase ) else: lowerCamelCase__ : Dict = FlavaImageCodebookConfig() lowerCamelCase__ : Tuple = FlavaImageCodebook(_lowerCamelCase ).eval() lowerCamelCase__ : List[str] = encoder.state_dict() lowerCamelCase__ : Any = upgrade_state_dict(_lowerCamelCase ) hf_model.load_state_dict(_lowerCamelCase ) lowerCamelCase__ : Optional[Any] = hf_model.state_dict() lowerCamelCase__ : Optional[int] = count_parameters(_lowerCamelCase ) lowerCamelCase__ : Optional[int] = count_parameters(_lowerCamelCase ) assert torch.allclose(_lowerCamelCase , _lowerCamelCase , atol=1e-3 ) if save_checkpoint: hf_model.save_pretrained(_lowerCamelCase ) else: return hf_state_dict if __name__ == "__main__": A_ : Tuple = argparse.ArgumentParser() parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to flava checkpoint") parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert") A_ : str = parser.parse_args() convert_dalle_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
316
1
"""simple docstring""" import argparse import torch from torch import nn from transformers import SpeechaTextConfig, SpeechaTextForConditionalGeneration def lowerCamelCase_ ( _lowerCamelCase ): lowerCamelCase__ : List[str] = [ 'encoder.version', 'decoder.version', 'model.encoder.version', 'model.decoder.version', 'decoder.output_projection.weight', '_float_tensor', 'encoder.embed_positions._float_tensor', 'decoder.embed_positions._float_tensor', ] for k in ignore_keys: state_dict.pop(_lowerCamelCase , _lowerCamelCase ) def lowerCamelCase_ ( _lowerCamelCase ): lowerCamelCase__ : Optional[int] = list(s_dict.keys() ) for key in keys: if "transformer_layers" in key: lowerCamelCase__ : Union[str, Any] = s_dict.pop(_lowerCamelCase ) elif "subsample" in key: lowerCamelCase__ : List[Any] = s_dict.pop(_lowerCamelCase ) def lowerCamelCase_ ( _lowerCamelCase ): lowerCamelCase__ , lowerCamelCase__ : Tuple = emb.weight.shape lowerCamelCase__ : List[Any] = nn.Linear(_lowerCamelCase , _lowerCamelCase , bias=_lowerCamelCase ) lowerCamelCase__ : Tuple = emb.weight.data return lin_layer def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : Tuple = torch.load(_lowerCamelCase , map_location='cpu' ) lowerCamelCase__ : List[str] = mam_aaa['args'] lowerCamelCase__ : Optional[int] = mam_aaa['model'] lowerCamelCase__ : Optional[int] = state_dict['decoder.output_projection.weight'] remove_ignore_keys_(_lowerCamelCase ) rename_keys(_lowerCamelCase ) lowerCamelCase__ : Tuple = state_dict['decoder.embed_tokens.weight'].shape[0] lowerCamelCase__ : Any = args.share_decoder_input_output_embed lowerCamelCase__ : int = [int(_lowerCamelCase ) for i in args.conv_kernel_sizes.split(',' )] lowerCamelCase__ : int = SpeechaTextConfig( vocab_size=_lowerCamelCase , max_source_positions=args.max_source_positions , max_target_positions=args.max_target_positions , encoder_layers=args.encoder_layers , decoder_layers=args.decoder_layers , encoder_attention_heads=args.encoder_attention_heads , decoder_attention_heads=args.decoder_attention_heads , encoder_ffn_dim=args.encoder_ffn_embed_dim , decoder_ffn_dim=args.decoder_ffn_embed_dim , d_model=args.encoder_embed_dim , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function='relu' , num_conv_layers=len(_lowerCamelCase ) , conv_channels=args.conv_channels , conv_kernel_sizes=_lowerCamelCase , input_feat_per_channel=args.input_feat_per_channel , input_channels=args.input_channels , tie_word_embeddings=_lowerCamelCase , num_beams=5 , max_length=200 , use_cache=_lowerCamelCase , decoder_start_token_id=2 , early_stopping=_lowerCamelCase , ) lowerCamelCase__ : Tuple = SpeechaTextForConditionalGeneration(_lowerCamelCase ) lowerCamelCase__ , lowerCamelCase__ : str = model.model.load_state_dict(_lowerCamelCase , strict=_lowerCamelCase ) if len(_lowerCamelCase ) > 0 and not set(_lowerCamelCase ) <= { "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: lowerCamelCase__ : Dict = make_linear_from_emb(model.model.decoder.embed_tokens ) else: lowerCamelCase__ : Union[str, Any] = lm_head_weights model.save_pretrained(_lowerCamelCase ) if __name__ == "__main__": A_ : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument("--fairseq_path", type=str, help="Path to the fairseq model (.pt) file.") parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") A_ : Any = parser.parse_args() convert_fairseq_sat_checkpoint_to_tfms(args.fairseq_path, args.pytorch_dump_folder_path)
316
"""simple docstring""" from __future__ import annotations import inspect import unittest import numpy as np from transformers import DeiTConfig 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 ( TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, TFDeiTModel, ) from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import DeiTImageProcessor class a_ : '''simple docstring''' def __init__(self, lowerCamelCase_, lowerCamelCase_=1_3, lowerCamelCase_=3_0, lowerCamelCase_=2, lowerCamelCase_=3, lowerCamelCase_=True, lowerCamelCase_=True, lowerCamelCase_=3_2, lowerCamelCase_=2, lowerCamelCase_=4, lowerCamelCase_=3_7, lowerCamelCase_="gelu", lowerCamelCase_=0.1, lowerCamelCase_=0.1, lowerCamelCase_=1_0, lowerCamelCase_=0.02, lowerCamelCase_=3, lowerCamelCase_=None, lowerCamelCase_=2, ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = parent lowerCamelCase__ : int = batch_size lowerCamelCase__ : Dict = image_size lowerCamelCase__ : List[str] = patch_size lowerCamelCase__ : Union[str, Any] = num_channels lowerCamelCase__ : str = is_training lowerCamelCase__ : Any = use_labels lowerCamelCase__ : Tuple = hidden_size lowerCamelCase__ : str = num_hidden_layers lowerCamelCase__ : Dict = num_attention_heads lowerCamelCase__ : Union[str, Any] = intermediate_size lowerCamelCase__ : Any = hidden_act lowerCamelCase__ : Dict = hidden_dropout_prob lowerCamelCase__ : Optional[Any] = attention_probs_dropout_prob lowerCamelCase__ : List[Any] = type_sequence_label_size lowerCamelCase__ : Optional[int] = initializer_range lowerCamelCase__ : Tuple = scope lowerCamelCase__ : List[str] = encoder_stride # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens) lowerCamelCase__ : str = (image_size // patch_size) ** 2 lowerCamelCase__ : Optional[int] = num_patches + 2 def a__ (self ): '''simple docstring''' lowerCamelCase__ : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCamelCase__ : Tuple = None if self.use_labels: lowerCamelCase__ : Optional[Any] = ids_tensor([self.batch_size], self.type_sequence_label_size ) lowerCamelCase__ : List[str] = self.get_config() return config, pixel_values, labels def a__ (self ): '''simple docstring''' return DeiTConfig( image_size=self.image_size, patch_size=self.patch_size, num_channels=self.num_channels, hidden_size=self.hidden_size, num_hidden_layers=self.num_hidden_layers, num_attention_heads=self.num_attention_heads, intermediate_size=self.intermediate_size, hidden_act=self.hidden_act, hidden_dropout_prob=self.hidden_dropout_prob, attention_probs_dropout_prob=self.attention_probs_dropout_prob, is_decoder=lowerCamelCase_, initializer_range=self.initializer_range, encoder_stride=self.encoder_stride, ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = TFDeiTModel(config=lowerCamelCase_ ) lowerCamelCase__ : Dict = model(lowerCamelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : List[str] = TFDeiTForMaskedImageModeling(config=lowerCamelCase_ ) lowerCamelCase__ : Any = model(lowerCamelCase_ ) self.parent.assertEqual( result.reconstruction.shape, (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images lowerCamelCase__ : Tuple = 1 lowerCamelCase__ : Optional[Any] = TFDeiTForMaskedImageModeling(lowerCamelCase_ ) lowerCamelCase__ : List[Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowerCamelCase__ : Optional[Any] = model(lowerCamelCase_ ) self.parent.assertEqual(result.reconstruction.shape, (self.batch_size, 1, self.image_size, self.image_size) ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : int = self.type_sequence_label_size lowerCamelCase__ : Union[str, Any] = TFDeiTForImageClassification(lowerCamelCase_ ) lowerCamelCase__ : Optional[Any] = model(lowerCamelCase_, labels=lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.type_sequence_label_size) ) # test greyscale images lowerCamelCase__ : List[str] = 1 lowerCamelCase__ : Any = TFDeiTForImageClassification(lowerCamelCase_ ) lowerCamelCase__ : List[str] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowerCamelCase__ : Optional[int] = model(lowerCamelCase_, labels=lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.type_sequence_label_size) ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Any = self.prepare_config_and_inputs() lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Tuple = config_and_inputs lowerCamelCase__ : str = {'pixel_values': pixel_values} return config, inputs_dict @require_tf class a_ ( snake_case_ , snake_case_ , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ : Any = ( ( TFDeiTModel, TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, ) if is_tf_available() else () ) lowerCamelCase__ : Tuple = ( { 'feature-extraction': TFDeiTModel, 'image-classification': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher), } if is_tf_available() else {} ) lowerCamelCase__ : Any = False lowerCamelCase__ : Optional[Any] = False lowerCamelCase__ : Dict = False lowerCamelCase__ : int = False def a__ (self ): '''simple docstring''' lowerCamelCase__ : List[Any] = TFDeiTModelTester(self ) lowerCamelCase__ : Union[str, Any] = ConfigTester(self, config_class=lowerCamelCase_, has_text_modality=lowerCamelCase_, hidden_size=3_7 ) def a__ (self ): '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason='DeiT does not use inputs_embeds' ) def a__ (self ): '''simple docstring''' pass def a__ (self ): '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase__ : Optional[int] = model_class(lowerCamelCase_ ) self.assertIsInstance(model.get_input_embeddings(), (tf.keras.layers.Layer) ) lowerCamelCase__ : List[str] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCamelCase_, tf.keras.layers.Dense ) ) def a__ (self ): '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase__ : Dict = model_class(lowerCamelCase_ ) lowerCamelCase__ : Any = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCamelCase__ : str = [*signature.parameters.keys()] lowerCamelCase__ : Union[str, Any] = ['pixel_values'] self.assertListEqual(arg_names[:1], lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*lowerCamelCase_ ) def a__ (self ): '''simple docstring''' lowerCamelCase__ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_ ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_=False ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = super()._prepare_for_class(lowerCamelCase_, lowerCamelCase_, return_labels=lowerCamelCase_ ) if return_labels: if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call ).parameters: del inputs_dict["labels"] return inputs_dict @slow def a__ (self ): '''simple docstring''' for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase__ : int = TFDeiTModel.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) def lowerCamelCase_ ( ): lowerCamelCase__ : Any = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_tf @require_vision class a_ ( unittest.TestCase ): '''simple docstring''' @cached_property def a__ (self ): '''simple docstring''' return ( DeiTImageProcessor.from_pretrained('facebook/deit-base-distilled-patch16-224' ) if is_vision_available() else None ) @slow def a__ (self ): '''simple docstring''' lowerCamelCase__ : str = TFDeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-224' ) lowerCamelCase__ : List[Any] = self.default_image_processor lowerCamelCase__ : Union[str, Any] = prepare_img() lowerCamelCase__ : Optional[int] = image_processor(images=lowerCamelCase_, return_tensors='tf' ) # forward pass lowerCamelCase__ : Tuple = model(**lowerCamelCase_ ) # verify the logits lowerCamelCase__ : str = tf.TensorShape((1, 1_0_0_0) ) self.assertEqual(outputs.logits.shape, lowerCamelCase_ ) lowerCamelCase__ : Any = tf.constant([-1.0_266, 0.1_912, -1.2_861] ) self.assertTrue(np.allclose(outputs.logits[0, :3], lowerCamelCase_, atol=1e-4 ) )
316
1
"""simple docstring""" def lowerCamelCase_ ( _lowerCamelCase ): # bit count represents no. of bits in the gray code if bit_count < 0: raise ValueError('The given input must be positive' ) # get the generated string sequence lowerCamelCase__ : Dict = gray_code_sequence_string(_lowerCamelCase ) # # convert them to integers for i in range(len(_lowerCamelCase ) ): lowerCamelCase__ : Union[str, Any] = int(sequence[i] , 2 ) return sequence def lowerCamelCase_ ( _lowerCamelCase ): # The approach is a recursive one # Base case achieved when either n = 0 or n=1 if bit_count == 0: return ["0"] if bit_count == 1: return ["0", "1"] lowerCamelCase__ : int = 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__ : str = gray_code_sequence_string(bit_count - 1 ) lowerCamelCase__ : Tuple = [] # append 0 to first half of the smaller sequence generated for i in range(seq_len // 2 ): lowerCamelCase__ : Tuple = '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__ : int = '1' + smaller_sequence[i] sequence.append(_lowerCamelCase ) return sequence if __name__ == "__main__": import doctest doctest.testmod()
316
"""simple docstring""" def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): while second != 0: lowerCamelCase__ : Tuple = first & second first ^= second lowerCamelCase__ : int = c << 1 return first if __name__ == "__main__": import doctest doctest.testmod() A_ : Tuple = int(input("Enter the first number: ").strip()) A_ : Union[str, Any] = int(input("Enter the second number: ").strip()) print(f"{add(first, second) = }")
316
1
"""simple docstring""" from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel from ...schedulers import ScoreSdeVeScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : UNetaDModel lowerCamelCase__ : ScoreSdeVeScheduler def __init__(self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' super().__init__() self.register_modules(unet=lowerCamelCase_, scheduler=lowerCamelCase_ ) @torch.no_grad() def __call__(self, lowerCamelCase_ = 1, lowerCamelCase_ = 2_0_0_0, lowerCamelCase_ = None, lowerCamelCase_ = "pil", lowerCamelCase_ = True, **lowerCamelCase_, ): '''simple docstring''' lowerCamelCase__ : Tuple = self.unet.config.sample_size lowerCamelCase__ : Dict = (batch_size, 3, img_size, img_size) lowerCamelCase__ : List[Any] = self.unet lowerCamelCase__ : Optional[Any] = randn_tensor(lowerCamelCase_, generator=lowerCamelCase_ ) * self.scheduler.init_noise_sigma lowerCamelCase__ : Union[str, Any] = sample.to(self.device ) self.scheduler.set_timesteps(lowerCamelCase_ ) self.scheduler.set_sigmas(lowerCamelCase_ ) for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): lowerCamelCase__ : int = self.scheduler.sigmas[i] * torch.ones(shape[0], device=self.device ) # correction step for _ in range(self.scheduler.config.correct_steps ): lowerCamelCase__ : Optional[Any] = self.unet(lowerCamelCase_, lowerCamelCase_ ).sample lowerCamelCase__ : List[str] = self.scheduler.step_correct(lowerCamelCase_, lowerCamelCase_, generator=lowerCamelCase_ ).prev_sample # prediction step lowerCamelCase__ : List[Any] = model(lowerCamelCase_, lowerCamelCase_ ).sample lowerCamelCase__ : Any = self.scheduler.step_pred(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, generator=lowerCamelCase_ ) lowerCamelCase__ , lowerCamelCase__ : Any = output.prev_sample, output.prev_sample_mean lowerCamelCase__ : Union[str, Any] = sample_mean.clamp(0, 1 ) lowerCamelCase__ : Optional[Any] = sample.cpu().permute(0, 2, 3, 1 ).numpy() if output_type == "pil": lowerCamelCase__ : Optional[Any] = self.numpy_to_pil(lowerCamelCase_ ) if not return_dict: return (sample,) return ImagePipelineOutput(images=lowerCamelCase_ )
316
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) A_ : List[str] = {"configuration_encoder_decoder": ["EncoderDecoderConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Dict = ["EncoderDecoderModel"] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : List[Any] = ["TFEncoderDecoderModel"] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : List[Any] = ["FlaxEncoderDecoderModel"] if TYPE_CHECKING: from .configuration_encoder_decoder import EncoderDecoderConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_encoder_decoder import EncoderDecoderModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_encoder_decoder import TFEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_encoder_decoder import FlaxEncoderDecoderModel else: import sys A_ : Any = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
316
1
"""simple docstring""" import argparse import os import torch from transformers import FlavaImageCodebook, FlavaImageCodebookConfig def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : Any = s.rsplit(_lowerCamelCase , _lowerCamelCase ) return new.join(_lowerCamelCase ) def lowerCamelCase_ ( _lowerCamelCase ): # encoder.embeddings are double copied in original FLAVA return sum(param.float().sum() if 'encoder.embeddings' not in key else 0 for key, param in state_dict.items() ) def lowerCamelCase_ ( _lowerCamelCase ): lowerCamelCase__ : List[Any] = {} lowerCamelCase__ : Any = ['group_1', 'group_2', 'group_3', 'group_4'] for key, value in state_dict.items(): for group_key in group_keys: if group_key in key: lowerCamelCase__ : Union[str, Any] = key.replace(f'''{group_key}.''' , f'''{group_key}.group.''' ) if "res_path" in key: lowerCamelCase__ : Dict = key.replace('res_path.' , 'res_path.path.' ) if key.endswith('.w' ): lowerCamelCase__ : str = rreplace(_lowerCamelCase , '.w' , '.weight' , 1 ) if key.endswith('.b' ): lowerCamelCase__ : Optional[Any] = rreplace(_lowerCamelCase , '.b' , '.bias' , 1 ) lowerCamelCase__ : int = value.float() return upgrade @torch.no_grad() def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase=None , _lowerCamelCase=True ): from dall_e import Encoder lowerCamelCase__ : List[str] = Encoder() if os.path.exists(_lowerCamelCase ): lowerCamelCase__ : Optional[int] = torch.load(_lowerCamelCase ) else: lowerCamelCase__ : List[Any] = torch.hub.load_state_dict_from_url(_lowerCamelCase ) if isinstance(_lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : List[Any] = ckpt.state_dict() encoder.load_state_dict(_lowerCamelCase ) if config_path is not None: lowerCamelCase__ : Union[str, Any] = FlavaImageCodebookConfig.from_pretrained(_lowerCamelCase ) else: lowerCamelCase__ : Dict = FlavaImageCodebookConfig() lowerCamelCase__ : Tuple = FlavaImageCodebook(_lowerCamelCase ).eval() lowerCamelCase__ : List[str] = encoder.state_dict() lowerCamelCase__ : Any = upgrade_state_dict(_lowerCamelCase ) hf_model.load_state_dict(_lowerCamelCase ) lowerCamelCase__ : Optional[Any] = hf_model.state_dict() lowerCamelCase__ : Optional[int] = count_parameters(_lowerCamelCase ) lowerCamelCase__ : Optional[int] = count_parameters(_lowerCamelCase ) assert torch.allclose(_lowerCamelCase , _lowerCamelCase , atol=1e-3 ) if save_checkpoint: hf_model.save_pretrained(_lowerCamelCase ) else: return hf_state_dict if __name__ == "__main__": A_ : Tuple = argparse.ArgumentParser() parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to flava checkpoint") parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert") A_ : str = parser.parse_args() convert_dalle_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
316
"""simple docstring""" import numpy as np def lowerCamelCase_ ( _lowerCamelCase ): return (2 / (1 + np.exp(-2 * vector ))) - 1 if __name__ == "__main__": import doctest doctest.testmod()
316
1
"""simple docstring""" from math import pow def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , ): if current_sum == needed_sum: # If the sum of the powers is equal to needed_sum, then we have a solution. solutions_count += 1 return current_sum, solutions_count lowerCamelCase__ : Dict = int(pow(_lowerCamelCase , _lowerCamelCase ) ) if current_sum + i_to_n <= needed_sum: # If the sum of the powers is less than needed_sum, then continue adding powers. current_sum += i_to_n lowerCamelCase__ , lowerCamelCase__ : List[Any] = backtrack( _lowerCamelCase , _lowerCamelCase , current_number + 1 , _lowerCamelCase , _lowerCamelCase ) current_sum -= i_to_n if i_to_n < needed_sum: # If the power of i is less than needed_sum, then try with the next power. lowerCamelCase__ , lowerCamelCase__ : List[str] = backtrack( _lowerCamelCase , _lowerCamelCase , current_number + 1 , _lowerCamelCase , _lowerCamelCase ) return current_sum, solutions_count def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): if not (1 <= needed_sum <= 1000 and 2 <= power <= 10): raise ValueError( 'Invalid input\n' 'needed_sum must be between 1 and 1000, power between 2 and 10.' ) return backtrack(_lowerCamelCase , _lowerCamelCase , 1 , 0 , 0 )[1] # Return the solutions_count if __name__ == "__main__": import doctest doctest.testmod()
316
"""simple docstring""" print((lambda quine: quine % quine)("print((lambda quine: quine %% quine)(%r))"))
316
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging A_ : Dict = logging.get_logger(__name__) A_ : str = { "google/switch-base-8": "https://huggingface.co/google/switch-base-8/blob/main/config.json", } class a_ ( snake_case_ ): '''simple docstring''' lowerCamelCase__ : Any = 'switch_transformers' lowerCamelCase__ : List[Any] = ['past_key_values'] lowerCamelCase__ : Optional[int] = {'hidden_size': 'd_model', 'num_attention_heads': 'num_heads', 'num_hidden_layers': 'num_layers'} def __init__(self, lowerCamelCase_=3_2_1_2_8, lowerCamelCase_=7_6_8, lowerCamelCase_=6_4, lowerCamelCase_=2_0_4_8, lowerCamelCase_=6_4, lowerCamelCase_=1_2, lowerCamelCase_=3, lowerCamelCase_=1_2, lowerCamelCase_=3, lowerCamelCase_=1_2, lowerCamelCase_=8, lowerCamelCase_=False, lowerCamelCase_=0.01, lowerCamelCase_="float32", lowerCamelCase_=False, lowerCamelCase_=3_2, lowerCamelCase_=1_2_8, lowerCamelCase_=0.1, lowerCamelCase_=1e-6, lowerCamelCase_=0.001, lowerCamelCase_=0.001, lowerCamelCase_=1.0, lowerCamelCase_="relu", lowerCamelCase_=True, lowerCamelCase_=False, lowerCamelCase_=True, lowerCamelCase_=0, lowerCamelCase_=1, **lowerCamelCase_, ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = vocab_size lowerCamelCase__ : Tuple = d_model lowerCamelCase__ : Union[str, Any] = d_kv lowerCamelCase__ : Any = d_ff lowerCamelCase__ : Optional[Any] = num_sparse_encoder_layers lowerCamelCase__ : Union[str, Any] = num_layers lowerCamelCase__ : Optional[int] = ( num_decoder_layers if num_decoder_layers is not None else self.num_layers ) # default = symmetry lowerCamelCase__ : List[Any] = num_sparse_decoder_layers # This tells us, each how many encoder layer we'll have to set a sparse layer. if self.num_sparse_encoder_layers > 0: lowerCamelCase__ : str = self.num_layers // self.num_sparse_encoder_layers else: lowerCamelCase__ : Optional[Any] = self.num_layers # HACK: this will create 0 sparse layers # This tells us, each how many encoder layer we'll have to set a sparse layer. if self.num_sparse_decoder_layers > 0: lowerCamelCase__ : Optional[int] = self.num_decoder_layers // self.num_sparse_decoder_layers else: lowerCamelCase__ : int = self.num_decoder_layers # HACK: this will create 0 sparse layers lowerCamelCase__ : Optional[Any] = num_heads lowerCamelCase__ : int = num_experts lowerCamelCase__ : int = expert_capacity lowerCamelCase__ : str = router_bias lowerCamelCase__ : List[Any] = router_jitter_noise if router_dtype not in ["float32", "float16", "bfloat16"]: raise ValueError(f'''`router_dtype` must be one of \'float32\', \'float16\' or \'bfloat16\', got {router_dtype}''' ) lowerCamelCase__ : Optional[Any] = router_dtype lowerCamelCase__ : Optional[Any] = router_ignore_padding_tokens lowerCamelCase__ : Optional[Any] = relative_attention_num_buckets lowerCamelCase__ : int = relative_attention_max_distance lowerCamelCase__ : Union[str, Any] = dropout_rate lowerCamelCase__ : List[str] = layer_norm_epsilon lowerCamelCase__ : List[str] = initializer_factor lowerCamelCase__ : Union[str, Any] = feed_forward_proj lowerCamelCase__ : Optional[Any] = use_cache lowerCamelCase__ : Optional[int] = add_router_probs lowerCamelCase__ : Dict = router_z_loss_coef lowerCamelCase__ : Union[str, Any] = router_aux_loss_coef lowerCamelCase__ : Optional[int] = self.feed_forward_proj.split('-' ) lowerCamelCase__ : Union[str, Any] = act_info[-1] lowerCamelCase__ : int = act_info[0] == 'gated' if len(lowerCamelCase_ ) > 1 and act_info[0] != "gated" or len(lowerCamelCase_ ) > 2: raise ValueError( f'''`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer.''' 'Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. ' '\'gated-gelu\' or \'relu\'' ) # for backwards compatibility if feed_forward_proj == "gated-gelu": lowerCamelCase__ : Any = 'gelu_new' super().__init__( pad_token_id=lowerCamelCase_, eos_token_id=lowerCamelCase_, is_encoder_decoder=lowerCamelCase_, **lowerCamelCase_, )
316
"""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, ) A_ : int = { "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: A_ : Union[str, Any] = ["CLIPTokenizerFast"] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : int = ["CLIPFeatureExtractor"] A_ : Any = ["CLIPImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : List[Any] = [ "CLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "CLIPModel", "CLIPPreTrainedModel", "CLIPTextModel", "CLIPTextModelWithProjection", "CLIPVisionModel", "CLIPVisionModelWithProjection", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Any = [ "TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "TFCLIPModel", "TFCLIPPreTrainedModel", "TFCLIPTextModel", "TFCLIPVisionModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Any = [ "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 A_ : Optional[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
316
1
"""simple docstring""" import argparse import re from pathlib import Path import requests import torch from PIL import Image from torchvision.transforms import CenterCrop, Compose, Normalize, Resize, ToTensor from transformers import ( EfficientFormerConfig, EfficientFormerForImageClassificationWithTeacher, EfficientFormerImageProcessor, ) from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, PILImageResampling def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : Optional[Any] = old_name if "patch_embed" in old_name: lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Tuple = old_name.split('.' ) if layer == "0": lowerCamelCase__ : Optional[int] = old_name.replace('0' , 'convolution1' ) elif layer == "1": lowerCamelCase__ : List[Any] = old_name.replace('1' , 'batchnorm_before' ) elif layer == "3": lowerCamelCase__ : Union[str, Any] = old_name.replace('3' , 'convolution2' ) else: lowerCamelCase__ : Tuple = old_name.replace('4' , 'batchnorm_after' ) if "network" in old_name and re.search(r'\d\.\d' , _lowerCamelCase ): lowerCamelCase__ : int = r'\b\d{2}\b' if bool(re.search(_lowerCamelCase , _lowerCamelCase ) ): lowerCamelCase__ : Dict = re.search(r'\d\.\d\d.' , _lowerCamelCase ).group() else: lowerCamelCase__ : Tuple = re.search(r'\d\.\d.' , _lowerCamelCase ).group() if int(match[0] ) < 6: lowerCamelCase__ : Dict = old_name.replace(_lowerCamelCase , '' ) lowerCamelCase__ : Tuple = trimmed_name.replace('network' , match[0] + '.meta4D_layers.blocks.' + match[2:-1] ) lowerCamelCase__ : Union[str, Any] = 'intermediate_stages.' + trimmed_name else: lowerCamelCase__ : Tuple = old_name.replace(_lowerCamelCase , '' ) if int(match[2] ) < num_meta4D_last_stage: lowerCamelCase__ : Optional[Any] = trimmed_name.replace('network' , 'meta4D_layers.blocks.' + match[2] ) else: lowerCamelCase__ : Optional[int] = str(int(match[2] ) - num_meta4D_last_stage ) lowerCamelCase__ : int = trimmed_name.replace('network' , 'meta3D_layers.blocks.' + layer_index ) if "norm1" in old_name: lowerCamelCase__ : int = trimmed_name.replace('norm1' , 'layernorm1' ) elif "norm2" in old_name: lowerCamelCase__ : List[Any] = trimmed_name.replace('norm2' , 'layernorm2' ) elif "fc1" in old_name: lowerCamelCase__ : List[Any] = trimmed_name.replace('fc1' , 'linear_in' ) elif "fc2" in old_name: lowerCamelCase__ : List[Any] = trimmed_name.replace('fc2' , 'linear_out' ) lowerCamelCase__ : str = 'last_stage.' + trimmed_name elif "network" in old_name and re.search(r'.\d.' , _lowerCamelCase ): lowerCamelCase__ : Union[str, Any] = old_name.replace('network' , 'intermediate_stages' ) if "fc" in new_name: lowerCamelCase__ : List[str] = new_name.replace('fc' , 'convolution' ) elif ("norm1" in new_name) and ("layernorm1" not in new_name): lowerCamelCase__ : int = new_name.replace('norm1' , 'batchnorm_before' ) elif ("norm2" in new_name) and ("layernorm2" not in new_name): lowerCamelCase__ : Dict = new_name.replace('norm2' , 'batchnorm_after' ) if "proj" in new_name: lowerCamelCase__ : List[str] = new_name.replace('proj' , 'projection' ) if "dist_head" in new_name: lowerCamelCase__ : Optional[int] = new_name.replace('dist_head' , 'distillation_classifier' ) elif "head" in new_name: lowerCamelCase__ : str = new_name.replace('head' , 'classifier' ) elif "patch_embed" in new_name: lowerCamelCase__ : Dict = 'efficientformer.' + new_name elif new_name == "norm.weight" or new_name == "norm.bias": lowerCamelCase__ : Dict = new_name.replace('norm' , 'layernorm' ) lowerCamelCase__ : Tuple = 'efficientformer.' + new_name else: lowerCamelCase__ : List[Any] = 'efficientformer.encoder.' + new_name return new_name def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): for key in checkpoint.copy().keys(): lowerCamelCase__ : Union[str, Any] = checkpoint.pop(_lowerCamelCase ) lowerCamelCase__ : int = val return checkpoint def lowerCamelCase_ ( ): lowerCamelCase__ : str = 'http://images.cocodataset.org/val2017/000000039769.jpg' lowerCamelCase__ : Tuple = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ) return image def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : Any = torch.load(_lowerCamelCase , map_location='cpu' )['model'] lowerCamelCase__ : str = EfficientFormerConfig.from_json_file(_lowerCamelCase ) lowerCamelCase__ : List[str] = EfficientFormerForImageClassificationWithTeacher(_lowerCamelCase ) lowerCamelCase__ : Union[str, Any] = '_'.join(checkpoint_path.split('/' )[-1].split('.' )[0].split('_' )[:-1] ) lowerCamelCase__ : List[str] = config.depths[-1] - config.num_metaad_blocks + 1 lowerCamelCase__ : Union[str, Any] = convert_torch_checkpoint(_lowerCamelCase , _lowerCamelCase ) model.load_state_dict(_lowerCamelCase ) model.eval() lowerCamelCase__ : Optional[int] = { 'bilinear': PILImageResampling.BILINEAR, 'bicubic': PILImageResampling.BICUBIC, 'nearest': PILImageResampling.NEAREST, } # prepare image lowerCamelCase__ : Optional[int] = prepare_img() lowerCamelCase__ : Any = 256 lowerCamelCase__ : Dict = 224 lowerCamelCase__ : Tuple = EfficientFormerImageProcessor( size={'shortest_edge': image_size} , crop_size={'height': crop_size, 'width': crop_size} , resample=pillow_resamplings['bicubic'] , ) lowerCamelCase__ : List[Any] = processor(images=_lowerCamelCase , return_tensors='pt' ).pixel_values # original processing pipeline lowerCamelCase__ : Dict = Compose( [ Resize(_lowerCamelCase , interpolation=pillow_resamplings['bicubic'] ), CenterCrop(_lowerCamelCase ), ToTensor(), Normalize(_lowerCamelCase , _lowerCamelCase ), ] ) lowerCamelCase__ : Union[str, Any] = image_transforms(_lowerCamelCase ).unsqueeze(0 ) assert torch.allclose(_lowerCamelCase , _lowerCamelCase ) lowerCamelCase__ : List[Any] = model(_lowerCamelCase ) lowerCamelCase__ : List[str] = outputs.logits lowerCamelCase__ : List[str] = (1, 1000) if "l1" in model_name: lowerCamelCase__ : Optional[int] = torch.Tensor( [-0.1_312, 0.4_353, -1.0_499, -0.5_124, 0.4_183, -0.6_793, -1.3_777, -0.0_893, -0.7_358, -2.4_328] ) assert torch.allclose(logits[0, :10] , _lowerCamelCase , atol=1e-3 ) assert logits.shape == expected_shape elif "l3" in model_name: lowerCamelCase__ : str = torch.Tensor( [-1.3_150, -1.5_456, -1.2_556, -0.8_496, -0.7_127, -0.7_897, -0.9_728, -0.3_052, 0.3_751, -0.3_127] ) assert torch.allclose(logits[0, :10] , _lowerCamelCase , atol=1e-3 ) assert logits.shape == expected_shape elif "l7" in model_name: lowerCamelCase__ : List[str] = torch.Tensor( [-1.0_283, -1.4_131, -0.5_644, -1.3_115, -0.5_785, -1.2_049, -0.7_528, 0.1_992, -0.3_822, -0.0_878] ) assert logits.shape == expected_shape else: raise ValueError( f'''Unknown model checkpoint: {checkpoint_path}. Supported version of efficientformer are l1, l3 and l7''' ) # Save Checkpoints Path(_lowerCamelCase ).mkdir(exist_ok=_lowerCamelCase ) model.save_pretrained(_lowerCamelCase ) print(f'''Checkpoint successfuly converted. Model saved at {pytorch_dump_path}''' ) processor.save_pretrained(_lowerCamelCase ) print(f'''Processor successfuly saved at {pytorch_dump_path}''' ) if push_to_hub: print('Pushing model to the hub...' ) model.push_to_hub( repo_id=f'''Bearnardd/{pytorch_dump_path}''' , commit_message='Add model' , use_temp_dir=_lowerCamelCase , ) processor.push_to_hub( repo_id=f'''Bearnardd/{pytorch_dump_path}''' , commit_message='Add image processor' , use_temp_dir=_lowerCamelCase , ) if __name__ == "__main__": A_ : str = argparse.ArgumentParser() # Required parameters parser.add_argument( "--pytorch_model_path", default=None, type=str, required=True, help="Path to EfficientFormer pytorch checkpoint.", ) parser.add_argument( "--config_file", default=None, type=str, required=True, help="The json file for EfficientFormer model config.", ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) parser.add_argument("--push_to_hub", action="store_true", help="Push model and image processor to the hub") parser.add_argument( "--no-push_to_hub", dest="push_to_hub", action="store_false", help="Do not push model and image processor to the hub", ) parser.set_defaults(push_to_hub=True) A_ : Union[str, Any] = parser.parse_args() convert_efficientformer_checkpoint( checkpoint_path=args.pytorch_model_path, efficientformer_config_file=args.config_file, pytorch_dump_path=args.pytorch_dump_path, push_to_hub=args.push_to_hub, )
316
"""simple docstring""" import os import posixpath import uuid from dataclasses import dataclass from typing import TYPE_CHECKING, Iterable, List, Optional, Tuple, Union import numpy as np import pyarrow as pa import datasets from datasets.arrow_writer import ArrowWriter, ParquetWriter from datasets.config import MAX_SHARD_SIZE from datasets.filesystems import ( is_remote_filesystem, rename, ) from datasets.iterable_dataset import _BaseExamplesIterable from datasets.utils.py_utils import convert_file_size_to_int A_ : List[Any] = datasets.utils.logging.get_logger(__name__) if TYPE_CHECKING: import pyspark @dataclass class a_ ( datasets.BuilderConfig ): '''simple docstring''' lowerCamelCase__ : Optional[datasets.Features] = None def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , ): import pyspark def generate_fn(): lowerCamelCase__ : Optional[Any] = df.select('*' , pyspark.sql.functions.spark_partition_id().alias('part_id' ) ) for partition_id in partition_order: lowerCamelCase__ : Dict = df_with_partition_id.select('*' ).where(f'''part_id = {partition_id}''' ).drop('part_id' ) lowerCamelCase__ : Dict = partition_df.collect() lowerCamelCase__ : int = 0 for row in rows: yield f'''{partition_id}_{row_id}''', row.asDict() row_id += 1 return generate_fn class a_ ( _BaseExamplesIterable ): '''simple docstring''' def __init__(self, lowerCamelCase_, lowerCamelCase_=None, ): '''simple docstring''' lowerCamelCase__ : Tuple = df lowerCamelCase__ : Any = partition_order or range(self.df.rdd.getNumPartitions() ) lowerCamelCase__ : List[Any] = _generate_iterable_examples(self.df, self.partition_order ) def __iter__(self ): '''simple docstring''' yield from self.generate_examples_fn() def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = list(range(self.df.rdd.getNumPartitions() ) ) generator.shuffle(lowerCamelCase_ ) return SparkExamplesIterable(self.df, partition_order=lowerCamelCase_ ) def a__ (self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = self.split_shard_indices_by_worker(lowerCamelCase_, lowerCamelCase_ ) return SparkExamplesIterable(self.df, partition_order=lowerCamelCase_ ) @property def a__ (self ): '''simple docstring''' return len(self.partition_order ) class a_ ( datasets.DatasetBuilder ): '''simple docstring''' lowerCamelCase__ : Optional[int] = SparkConfig def __init__(self, lowerCamelCase_, lowerCamelCase_ = None, lowerCamelCase_ = None, **lowerCamelCase_, ): '''simple docstring''' import pyspark lowerCamelCase__ : str = pyspark.sql.SparkSession.builder.getOrCreate() lowerCamelCase__ : Optional[Any] = df lowerCamelCase__ : Dict = working_dir super().__init__( cache_dir=lowerCamelCase_, config_name=str(self.df.semanticHash() ), **lowerCamelCase_, ) def a__ (self ): '''simple docstring''' def create_cache_and_write_probe(lowerCamelCase_ ): # makedirs with exist_ok will recursively create the directory. It will not throw an error if directories # already exist. os.makedirs(self._cache_dir, exist_ok=lowerCamelCase_ ) lowerCamelCase__ : str = os.path.join(self._cache_dir, 'fs_test' + uuid.uuida().hex ) # Opening the file in append mode will create a new file unless it already exists, in which case it will not # change the file contents. open(lowerCamelCase_, 'a' ) return [probe_file] if self._spark.conf.get('spark.master', '' ).startswith('local' ): return # If the cluster is multi-node, make sure that the user provided a cache_dir and that it is on an NFS # accessible to the driver. # TODO: Stream batches to the driver using ArrowCollectSerializer instead of throwing an error. if self._cache_dir: lowerCamelCase__ : Tuple = ( self._spark.sparkContext.parallelize(range(1 ), 1 ).mapPartitions(lowerCamelCase_ ).collect() ) if os.path.isfile(probe[0] ): return raise ValueError( 'When using Dataset.from_spark on a multi-node cluster, the driver and all workers should be able to access cache_dir' ) def a__ (self ): '''simple docstring''' return datasets.DatasetInfo(features=self.config.features ) def a__ (self, lowerCamelCase_ ): '''simple docstring''' return [datasets.SplitGenerator(name=datasets.Split.TRAIN )] def a__ (self, lowerCamelCase_ ): '''simple docstring''' import pyspark def get_arrow_batch_size(lowerCamelCase_ ): for batch in it: yield pa.RecordBatch.from_pydict({'batch_bytes': [batch.nbytes]} ) lowerCamelCase__ : List[Any] = self.df.count() lowerCamelCase__ : List[Any] = df_num_rows if df_num_rows <= 1_0_0 else 1_0_0 # Approximate the size of each row (in Arrow format) by averaging over a max-100-row sample. lowerCamelCase__ : List[Any] = ( self.df.limit(lowerCamelCase_ ) .repartition(1 ) .mapInArrow(lowerCamelCase_, 'batch_bytes: long' ) .agg(pyspark.sql.functions.sum('batch_bytes' ).alias('sample_bytes' ) ) .collect()[0] .sample_bytes / sample_num_rows ) lowerCamelCase__ : Dict = approx_bytes_per_row * df_num_rows if approx_total_size > max_shard_size: # Make sure there is at least one row per partition. lowerCamelCase__ : str = min(lowerCamelCase_, int(approx_total_size / max_shard_size ) ) lowerCamelCase__ : List[Any] = self.df.repartition(lowerCamelCase_ ) def a__ (self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, ): '''simple docstring''' import pyspark lowerCamelCase__ : List[str] = ParquetWriter if file_format == 'parquet' else ArrowWriter lowerCamelCase__ : List[str] = os.path.join(self._working_dir, os.path.basename(lowerCamelCase_ ) ) if self._working_dir else fpath lowerCamelCase__ : Optional[int] = file_format == 'parquet' # Define these so that we don't reference self in write_arrow, which will result in a pickling error due to # pickling the SparkContext. lowerCamelCase__ : int = self.config.features lowerCamelCase__ : Dict = self._writer_batch_size lowerCamelCase__ : Optional[Any] = self._fs.storage_options def write_arrow(lowerCamelCase_ ): # Within the same SparkContext, no two task attempts will share the same attempt ID. lowerCamelCase__ : Any = pyspark.TaskContext().taskAttemptId() lowerCamelCase__ : str = next(lowerCamelCase_, lowerCamelCase_ ) if first_batch is None: # Some partitions might not receive any data. return pa.RecordBatch.from_arrays( [[task_id], [0], [0]], names=['task_id', 'num_examples', 'num_bytes'], ) lowerCamelCase__ : Tuple = 0 lowerCamelCase__ : Any = writer_class( features=lowerCamelCase_, path=working_fpath.replace('SSSSS', f'''{shard_id:05d}''' ).replace('TTTTT', f'''{task_id:05d}''' ), writer_batch_size=lowerCamelCase_, storage_options=lowerCamelCase_, embed_local_files=lowerCamelCase_, ) lowerCamelCase__ : List[str] = pa.Table.from_batches([first_batch] ) writer.write_table(lowerCamelCase_ ) for batch in it: if max_shard_size is not None and writer._num_bytes >= max_shard_size: lowerCamelCase__ , lowerCamelCase__ : Tuple = writer.finalize() writer.close() yield pa.RecordBatch.from_arrays( [[task_id], [num_examples], [num_bytes]], names=['task_id', 'num_examples', 'num_bytes'], ) shard_id += 1 lowerCamelCase__ : Dict = writer_class( features=writer._features, path=working_fpath.replace('SSSSS', f'''{shard_id:05d}''' ).replace('TTTTT', f'''{task_id:05d}''' ), writer_batch_size=lowerCamelCase_, storage_options=lowerCamelCase_, embed_local_files=lowerCamelCase_, ) lowerCamelCase__ : Tuple = pa.Table.from_batches([batch] ) writer.write_table(lowerCamelCase_ ) if writer._num_bytes > 0: lowerCamelCase__ , lowerCamelCase__ : Tuple = writer.finalize() writer.close() yield pa.RecordBatch.from_arrays( [[task_id], [num_examples], [num_bytes]], names=['task_id', 'num_examples', 'num_bytes'], ) if working_fpath != fpath: for file in os.listdir(os.path.dirname(lowerCamelCase_ ) ): lowerCamelCase__ : Optional[int] = os.path.join(os.path.dirname(lowerCamelCase_ ), os.path.basename(lowerCamelCase_ ) ) shutil.move(lowerCamelCase_, lowerCamelCase_ ) lowerCamelCase__ : List[str] = ( self.df.mapInArrow(lowerCamelCase_, 'task_id: long, num_examples: long, num_bytes: long' ) .groupBy('task_id' ) .agg( pyspark.sql.functions.sum('num_examples' ).alias('total_num_examples' ), pyspark.sql.functions.sum('num_bytes' ).alias('total_num_bytes' ), pyspark.sql.functions.count('num_bytes' ).alias('num_shards' ), pyspark.sql.functions.collect_list('num_examples' ).alias('shard_lengths' ), ) .collect() ) for row in stats: yield row.task_id, (row.total_num_examples, row.total_num_bytes, row.num_shards, row.shard_lengths) def a__ (self, lowerCamelCase_, lowerCamelCase_ = "arrow", lowerCamelCase_ = None, lowerCamelCase_ = None, **lowerCamelCase_, ): '''simple docstring''' self._validate_cache_dir() lowerCamelCase__ : Union[str, Any] = convert_file_size_to_int(max_shard_size or MAX_SHARD_SIZE ) self._repartition_df_if_needed(lowerCamelCase_ ) lowerCamelCase__ : str = not is_remote_filesystem(self._fs ) lowerCamelCase__ : Any = os.path.join if is_local else posixpath.join lowerCamelCase__ : Any = '-TTTTT-SSSSS-of-NNNNN' lowerCamelCase__ : Tuple = f'''{self.name}-{split_generator.name}{SUFFIX}.{file_format}''' lowerCamelCase__ : Union[str, Any] = path_join(self._output_dir, lowerCamelCase_ ) lowerCamelCase__ : List[str] = 0 lowerCamelCase__ : Dict = 0 lowerCamelCase__ : List[Any] = 0 lowerCamelCase__ : Optional[Any] = [] lowerCamelCase__ : List[str] = [] for task_id, content in self._prepare_split_single(lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ): ( ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ) : int = content if num_bytes > 0: total_num_examples += num_examples total_num_bytes += num_bytes total_shards += num_shards task_id_and_num_shards.append((task_id, num_shards) ) all_shard_lengths.extend(lowerCamelCase_ ) lowerCamelCase__ : str = total_num_examples lowerCamelCase__ : int = total_num_bytes # should rename everything at the end logger.debug(f'''Renaming {total_shards} shards.''' ) if total_shards > 1: lowerCamelCase__ : Union[str, Any] = all_shard_lengths # Define fs outside of _rename_shard so that we don't reference self in the function, which will result in a # pickling error due to pickling the SparkContext. lowerCamelCase__ : Optional[Any] = self._fs # use the -SSSSS-of-NNNNN pattern def _rename_shard( lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, ): rename( lowerCamelCase_, fpath.replace('SSSSS', f'''{shard_id:05d}''' ).replace('TTTTT', f'''{task_id:05d}''' ), fpath.replace('TTTTT-SSSSS', f'''{global_shard_id:05d}''' ).replace('NNNNN', f'''{total_shards:05d}''' ), ) lowerCamelCase__ : List[str] = [] lowerCamelCase__ : List[str] = 0 for i in range(len(lowerCamelCase_ ) ): lowerCamelCase__ , lowerCamelCase__ : Any = task_id_and_num_shards[i] for shard_id in range(lowerCamelCase_ ): args.append([task_id, shard_id, global_shard_id] ) global_shard_id += 1 self._spark.sparkContext.parallelize(lowerCamelCase_, len(lowerCamelCase_ ) ).map(lambda lowerCamelCase_ : _rename_shard(*lowerCamelCase_ ) ).collect() else: # don't use any pattern lowerCamelCase__ : List[Any] = 0 lowerCamelCase__ : Dict = task_id_and_num_shards[0][0] self._rename( fpath.replace('SSSSS', f'''{shard_id:05d}''' ).replace('TTTTT', f'''{task_id:05d}''' ), fpath.replace(lowerCamelCase_, '' ), ) def a__ (self, lowerCamelCase_, ): '''simple docstring''' return SparkExamplesIterable(self.df )
316
1