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
from __future__ import annotations def UpperCAmelCase_ ( __snake_case ) -> list[int]: """simple docstring""" if len(__snake_case ) == 0: return array _lowercase , _lowercase =min(__snake_case ), max(__snake_case ) # Compute the variables _lowercase =_max - _min + 1 _lowercase , _lowercase =[0] * holes_range, [0] * holes_range # Make the sorting. for i in array: _lowercase =i - _min _lowercase =i holes_repeat[index] += 1 # Makes the array back by replacing the numbers. _lowercase =0 for i in range(__snake_case ): while holes_repeat[i] > 0: _lowercase =holes[i] index += 1 holes_repeat[i] -= 1 # Returns the sorted array. return array if __name__ == "__main__": import doctest doctest.testmod() UpperCAmelCase__ = input('''Enter numbers separated by comma:\n''') UpperCAmelCase__ = [int(x) for x in user_input.split(''',''')] print(pigeon_sort(unsorted))
5
def A ( lowercase ) -> str: '''simple docstring''' return " ".join( ''.join(word[::-1] ) if len(lowercase ) > 4 else word for word in sentence.split() ) if __name__ == "__main__": import doctest doctest.testmod() print(reverse_long_words("Hey wollef sroirraw"))
222
0
"""simple docstring""" from ....configuration_utils import PretrainedConfig from ....utils import logging a = logging.get_logger(__name__) a = { '''Visual-Attention-Network/van-base''': ( '''https://huggingface.co/Visual-Attention-Network/van-base/blob/main/config.json''' ), } class lowercase_ ( __lowerCAmelCase ): '''simple docstring''' UpperCAmelCase : Optional[int] = '''van''' def __init__( self : int , _UpperCAmelCase : int=224 , _UpperCAmelCase : Optional[int]=3 , _UpperCAmelCase : int=[7, 3, 3, 3] , _UpperCAmelCase : Tuple=[4, 2, 2, 2] , _UpperCAmelCase : Dict=[64, 128, 320, 512] , _UpperCAmelCase : str=[3, 3, 12, 3] , _UpperCAmelCase : str=[8, 8, 4, 4] , _UpperCAmelCase : Optional[int]="gelu" , _UpperCAmelCase : Optional[int]=0.02 , _UpperCAmelCase : Optional[int]=1E-6 , _UpperCAmelCase : str=1E-2 , _UpperCAmelCase : Any=0.0 , _UpperCAmelCase : List[Any]=0.0 , **_UpperCAmelCase : List[str] , ): super().__init__(**_UpperCAmelCase ) _A = image_size _A = num_channels _A = patch_sizes _A = strides _A = hidden_sizes _A = depths _A = mlp_ratios _A = hidden_act _A = initializer_range _A = layer_norm_eps _A = layer_scale_init_value _A = drop_path_rate _A = dropout_rate
271
"""simple docstring""" from collections import deque class lowercase_ : '''simple docstring''' def __init__( self : int , _UpperCAmelCase : str , _UpperCAmelCase : int , _UpperCAmelCase : int ): _A = process_name # process name _A = arrival_time # arrival time of the process # completion time of finished process or last interrupted time _A = arrival_time _A = burst_time # remaining burst time _A = 0 # total time of the process wait in ready queue _A = 0 # time from arrival time to completion time class lowercase_ : '''simple docstring''' def __init__( self : Optional[Any] , _UpperCAmelCase : int , _UpperCAmelCase : list[int] , _UpperCAmelCase : deque[Process] , _UpperCAmelCase : int , ): # total number of mlfq's queues _A = number_of_queues # time slice of queues that round robin algorithm applied _A = time_slices # unfinished process is in this ready_queue _A = queue # current time _A = current_time # finished process is in this sequence queue _A = deque() def lowerCAmelCase_ ( self : Dict ): _A = [] for i in range(len(self.finish_queue ) ): sequence.append(self.finish_queue[i].process_name ) return sequence def lowerCAmelCase_ ( self : Optional[int] , _UpperCAmelCase : list[Process] ): _A = [] for i in range(len(_UpperCAmelCase ) ): waiting_times.append(queue[i].waiting_time ) return waiting_times def lowerCAmelCase_ ( self : Dict , _UpperCAmelCase : list[Process] ): _A = [] for i in range(len(_UpperCAmelCase ) ): turnaround_times.append(queue[i].turnaround_time ) return turnaround_times def lowerCAmelCase_ ( self : Optional[int] , _UpperCAmelCase : list[Process] ): _A = [] for i in range(len(_UpperCAmelCase ) ): completion_times.append(queue[i].stop_time ) return completion_times def lowerCAmelCase_ ( self : Dict , _UpperCAmelCase : deque[Process] ): return [q.burst_time for q in queue] def lowerCAmelCase_ ( self : Dict , _UpperCAmelCase : Process ): process.waiting_time += self.current_time - process.stop_time return process.waiting_time def lowerCAmelCase_ ( self : int , _UpperCAmelCase : deque[Process] ): _A = deque() # sequence deque of finished process while len(_UpperCAmelCase ) != 0: _A = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of current process self.update_waiting_time(_UpperCAmelCase ) # update current time self.current_time += cp.burst_time # finish the process and set the process's burst-time 0 _A = 0 # set the process's turnaround time because it is finished _A = self.current_time - cp.arrival_time # set the completion time _A = self.current_time # add the process to queue that has finished queue finished.append(_UpperCAmelCase ) self.finish_queue.extend(_UpperCAmelCase ) # add finished process to finish queue # FCFS will finish all remaining processes return finished def lowerCAmelCase_ ( self : Dict , _UpperCAmelCase : deque[Process] , _UpperCAmelCase : int ): _A = deque() # sequence deque of terminated process # just for 1 cycle and unfinished processes will go back to queue for _ in range(len(_UpperCAmelCase ) ): _A = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of unfinished processes self.update_waiting_time(_UpperCAmelCase ) # if the burst time of process is bigger than time-slice if cp.burst_time > time_slice: # use CPU for only time-slice self.current_time += time_slice # update remaining burst time cp.burst_time -= time_slice # update end point time _A = self.current_time # locate the process behind the queue because it is not finished ready_queue.append(_UpperCAmelCase ) else: # use CPU for remaining burst time self.current_time += cp.burst_time # set burst time 0 because the process is finished _A = 0 # set the finish time _A = self.current_time # update the process' turnaround time because it is finished _A = self.current_time - cp.arrival_time # add the process to queue that has finished queue finished.append(_UpperCAmelCase ) self.finish_queue.extend(_UpperCAmelCase ) # add finished process to finish queue # return finished processes queue and remaining processes queue return finished, ready_queue def lowerCAmelCase_ ( self : str ): # all queues except last one have round_robin algorithm for i in range(self.number_of_queues - 1 ): _A , _A = self.round_robin( self.ready_queue , self.time_slices[i] ) # the last queue has first_come_first_served algorithm self.first_come_first_served(self.ready_queue ) return self.finish_queue if __name__ == "__main__": import doctest a = Process('''P1''', 0, 53) a = Process('''P2''', 0, 17) a = Process('''P3''', 0, 68) a = Process('''P4''', 0, 24) a = 3 a = [17, 25] a = deque([Pa, Pa, Pa, Pa]) if len(time_slices) != number_of_queues - 1: raise SystemExit(0) doctest.testmod(extraglobs={'''queue''': deque([Pa, Pa, Pa, Pa])}) a = Process('''P1''', 0, 53) a = Process('''P2''', 0, 17) a = Process('''P3''', 0, 68) a = Process('''P4''', 0, 24) a = 3 a = [17, 25] a = deque([Pa, Pa, Pa, Pa]) a = MLFQ(number_of_queues, time_slices, queue, 0) a = mlfq.multi_level_feedback_queue() # print total waiting times of processes(P1, P2, P3, P4) print( F'''waiting time:\ \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print completion times of processes(P1, P2, P3, P4) print( F'''completion time:\ \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print total turnaround times of processes(P1, P2, P3, P4) print( F'''turnaround time:\ \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print sequence of finished processes print( F'''sequence of finished processes:\ {mlfq.calculate_sequence_of_finish_queue()}''' )
271
1
'''simple docstring''' import glob import os import random from string import ascii_lowercase, digits import cva import numpy as np # Parrameters _lowerCamelCase : int = (720, 1280) # Height, Width _lowerCamelCase : List[str] = (0.4, 0.6) # if height or width lower than this scale, drop it. _lowerCamelCase : int = 1 / 100 _lowerCamelCase : Optional[Any] = "" _lowerCamelCase : str = "" _lowerCamelCase : str = "" _lowerCamelCase : str = 250 def __lowerCamelCase ( ) -> None: """simple docstring""" UpperCamelCase , UpperCamelCase = get_dataset(A__ , A__ ) for index in range(A__ ): UpperCamelCase = random.sample(range(len(A__ ) ) , 4 ) UpperCamelCase , UpperCamelCase , UpperCamelCase = update_image_and_anno( A__ , A__ , A__ , A__ , A__ , filter_scale=A__ , ) # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' UpperCamelCase = random_chars(32 ) UpperCamelCase = path.split(os.sep )[-1].rsplit('.' , 1 )[0] UpperCamelCase = F"""{OUTPUT_DIR}/{file_name}_MOSAIC_{letter_code}""" cva.imwrite(F"""{file_root}.jpg""" , A__ , [cva.IMWRITE_JPEG_QUALITY, 85] ) print(F"""Succeeded {index+1}/{NUMBER_IMAGES} with {file_name}""" ) UpperCamelCase = [] for anno in new_annos: UpperCamelCase = anno[3] - anno[1] UpperCamelCase = anno[4] - anno[2] UpperCamelCase = anno[1] + width / 2 UpperCamelCase = anno[2] + height / 2 UpperCamelCase = F"""{anno[0]} {x_center} {y_center} {width} {height}""" annos_list.append(A__ ) with open(F"""{file_root}.txt""" , 'w' ) as outfile: outfile.write('\n'.join(line for line in annos_list ) ) def __lowerCamelCase ( A__ , A__ ) -> tuple[list, list]: """simple docstring""" UpperCamelCase = [] UpperCamelCase = [] for label_file in glob.glob(os.path.join(A__ , '*.txt' ) ): UpperCamelCase = label_file.split(os.sep )[-1].rsplit('.' , 1 )[0] with open(A__ ) as in_file: UpperCamelCase = in_file.readlines() UpperCamelCase = os.path.join(A__ , F"""{label_name}.jpg""" ) UpperCamelCase = [] for obj_list in obj_lists: UpperCamelCase = obj_list.rstrip('\n' ).split(' ' ) UpperCamelCase = float(obj[1] ) - float(obj[3] ) / 2 UpperCamelCase = float(obj[2] ) - float(obj[4] ) / 2 UpperCamelCase = float(obj[1] ) + float(obj[3] ) / 2 UpperCamelCase = float(obj[2] ) + float(obj[4] ) / 2 boxes.append([int(obj[0] ), xmin, ymin, xmax, ymax] ) if not boxes: continue img_paths.append(A__ ) labels.append(A__ ) return img_paths, labels def __lowerCamelCase ( A__ , A__ , A__ , A__ , A__ , A__ = 0.0 , ) -> tuple[list, list, str]: """simple docstring""" UpperCamelCase = np.zeros([output_size[0], output_size[1], 3] , dtype=np.uinta ) UpperCamelCase = scale_range[0] + random.random() * (scale_range[1] - scale_range[0]) UpperCamelCase = scale_range[0] + random.random() * (scale_range[1] - scale_range[0]) UpperCamelCase = int(scale_x * output_size[1] ) UpperCamelCase = int(scale_y * output_size[0] ) UpperCamelCase = [] UpperCamelCase = [] for i, index in enumerate(A__ ): UpperCamelCase = all_img_list[index] path_list.append(A__ ) UpperCamelCase = all_annos[index] UpperCamelCase = cva.imread(A__ ) if i == 0: # top-left UpperCamelCase = cva.resize(A__ , (divid_point_x, divid_point_y) ) UpperCamelCase = img for bbox in img_annos: UpperCamelCase = bbox[1] * scale_x UpperCamelCase = bbox[2] * scale_y UpperCamelCase = bbox[3] * scale_x UpperCamelCase = bbox[4] * scale_y new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) elif i == 1: # top-right UpperCamelCase = cva.resize(A__ , (output_size[1] - divid_point_x, divid_point_y) ) UpperCamelCase = img for bbox in img_annos: UpperCamelCase = scale_x + bbox[1] * (1 - scale_x) UpperCamelCase = bbox[2] * scale_y UpperCamelCase = scale_x + bbox[3] * (1 - scale_x) UpperCamelCase = bbox[4] * scale_y new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) elif i == 2: # bottom-left UpperCamelCase = cva.resize(A__ , (divid_point_x, output_size[0] - divid_point_y) ) UpperCamelCase = img for bbox in img_annos: UpperCamelCase = bbox[1] * scale_x UpperCamelCase = scale_y + bbox[2] * (1 - scale_y) UpperCamelCase = bbox[3] * scale_x UpperCamelCase = scale_y + bbox[4] * (1 - scale_y) new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) else: # bottom-right UpperCamelCase = cva.resize( A__ , (output_size[1] - divid_point_x, output_size[0] - divid_point_y) ) UpperCamelCase = img for bbox in img_annos: UpperCamelCase = scale_x + bbox[1] * (1 - scale_x) UpperCamelCase = scale_y + bbox[2] * (1 - scale_y) UpperCamelCase = scale_x + bbox[3] * (1 - scale_x) UpperCamelCase = scale_y + bbox[4] * (1 - scale_y) new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) # Remove bounding box small than scale of filter if filter_scale > 0: UpperCamelCase = [ anno for anno in new_anno if filter_scale < (anno[3] - anno[1]) and filter_scale < (anno[4] - anno[2]) ] return output_img, new_anno, path_list[0] def __lowerCamelCase ( A__ ) -> str: """simple docstring""" assert number_char > 1, "The number of character should greater than 1" UpperCamelCase = ascii_lowercase + digits return "".join(random.choice(A__ ) for _ in range(A__ ) ) if __name__ == "__main__": main() print("DONE ✅")
28
'''simple docstring''' import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_torch, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_torch_available, is_vision_available if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import MgpstrProcessor, ViTImageProcessor @require_torch @require_vision class a__( unittest.TestCase ): '''simple docstring''' UpperCAmelCase_ : Dict = ViTImageProcessor if is_vision_available() else None @property def a_ ( self): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def a_ ( self): """simple docstring""" lowerCAmelCase = (3, 32, 128) lowerCAmelCase = tempfile.mkdtemp() # fmt: off lowerCAmelCase = ["""[GO]""", """[s]""", """0""", """1""", """2""", """3""", """4""", """5""", """6""", """7""", """8""", """9""", """a""", """b""", """c""", """d""", """e""", """f""", """g""", """h""", """i""", """j""", """k""", """l""", """m""", """n""", """o""", """p""", """q""", """r""", """s""", """t""", """u""", """v""", """w""", """x""", """y""", """z"""] # fmt: on lowerCAmelCase = dict(zip(__lowerCAmelCase , range(len(__lowerCAmelCase)))) lowerCAmelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""]) with open(self.vocab_file , """w""" , encoding="""utf-8""") as fp: fp.write(json.dumps(__lowerCAmelCase) + """\n""") lowerCAmelCase = { """do_normalize""": False, """do_resize""": True, """image_processor_type""": """ViTImageProcessor""", """resample""": 3, """size""": {"""height""": 32, """width""": 128}, } lowerCAmelCase = os.path.join(self.tmpdirname , __lowerCAmelCase) with open(self.image_processor_file , """w""" , encoding="""utf-8""") as fp: json.dump(__lowerCAmelCase , __lowerCAmelCase) def a_ ( self , **__lowerCAmelCase): """simple docstring""" return MgpstrTokenizer.from_pretrained(self.tmpdirname , **__lowerCAmelCase) def a_ ( self , **__lowerCAmelCase): """simple docstring""" return ViTImageProcessor.from_pretrained(self.tmpdirname , **__lowerCAmelCase) def a_ ( self): """simple docstring""" shutil.rmtree(self.tmpdirname) def a_ ( self): """simple docstring""" lowerCAmelCase = np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta) lowerCAmelCase = Image.fromarray(np.moveaxis(__lowerCAmelCase , 0 , -1)) return image_input def a_ ( self): """simple docstring""" lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = self.get_image_processor() lowerCAmelCase = MgpstrProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase) processor.save_pretrained(self.tmpdirname) lowerCAmelCase = MgpstrProcessor.from_pretrained(self.tmpdirname , use_fast=__lowerCAmelCase) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer.get_vocab()) self.assertIsInstance(processor.char_tokenizer , __lowerCAmelCase) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string()) self.assertIsInstance(processor.image_processor , __lowerCAmelCase) def a_ ( self): """simple docstring""" lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = self.get_image_processor() lowerCAmelCase = MgpstrProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase) processor.save_pretrained(self.tmpdirname) lowerCAmelCase = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""") lowerCAmelCase = self.get_image_processor(do_normalize=__lowerCAmelCase , padding_value=1.0) lowerCAmelCase = MgpstrProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=__lowerCAmelCase , padding_value=1.0) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab()) self.assertIsInstance(processor.char_tokenizer , __lowerCAmelCase) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string()) self.assertIsInstance(processor.image_processor , __lowerCAmelCase) def a_ ( self): """simple docstring""" lowerCAmelCase = self.get_image_processor() lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = MgpstrProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase) lowerCAmelCase = self.prepare_image_inputs() lowerCAmelCase = image_processor(__lowerCAmelCase , return_tensors="""np""") lowerCAmelCase = processor(images=__lowerCAmelCase , return_tensors="""np""") for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1E-2) def a_ ( self): """simple docstring""" lowerCAmelCase = self.get_image_processor() lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = MgpstrProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase) lowerCAmelCase = """test""" lowerCAmelCase = processor(text=__lowerCAmelCase) lowerCAmelCase = tokenizer(__lowerCAmelCase) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key]) def a_ ( self): """simple docstring""" lowerCAmelCase = self.get_image_processor() lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = MgpstrProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase) lowerCAmelCase = """test""" lowerCAmelCase = self.prepare_image_inputs() lowerCAmelCase = processor(text=__lowerCAmelCase , images=__lowerCAmelCase) self.assertListEqual(list(inputs.keys()) , ["""pixel_values""", """labels"""]) # test if it raises when no input is passed with pytest.raises(__lowerCAmelCase): processor() def a_ ( self): """simple docstring""" lowerCAmelCase = self.get_image_processor() lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = MgpstrProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase) lowerCAmelCase = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9], [3, 4, 3, 1, 1, 8, 9]] lowerCAmelCase = processor.char_decode(__lowerCAmelCase) lowerCAmelCase = tokenizer.batch_decode(__lowerCAmelCase) lowerCAmelCase = [seq.replace(""" """ , """""") for seq in decoded_tok] self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase) def a_ ( self): """simple docstring""" lowerCAmelCase = self.get_image_processor() lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = MgpstrProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase) lowerCAmelCase = None lowerCAmelCase = self.prepare_image_inputs() lowerCAmelCase = processor(text=__lowerCAmelCase , images=__lowerCAmelCase) self.assertListEqual(list(inputs.keys()) , processor.model_input_names) def a_ ( self): """simple docstring""" lowerCAmelCase = self.get_image_processor() lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = MgpstrProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase) lowerCAmelCase = torch.randn(1 , 27 , 38) lowerCAmelCase = torch.randn(1 , 27 , 50257) lowerCAmelCase = torch.randn(1 , 27 , 30522) lowerCAmelCase = processor.batch_decode([char_input, bpe_input, wp_input]) self.assertListEqual(list(results.keys()) , ["""generated_text""", """scores""", """char_preds""", """bpe_preds""", """wp_preds"""])
272
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging A_ = logging.get_logger(__name__) A_ = { """facebook/vit-mae-base""": """https://huggingface.co/facebook/vit-mae-base/resolve/main/config.json""", # See all ViT MAE models at https://huggingface.co/models?filter=vit-mae } class lowercase( __snake_case ): '''simple docstring''' lowercase__ = "vit_mae" def __init__( self: List[str], a_: Union[str, Any]=768, a_: Optional[int]=12, a_: Optional[int]=12, a_: Optional[Any]=3_072, a_: Optional[int]="gelu", a_: int=0.0, a_: Union[str, Any]=0.0, a_: List[Any]=0.02, a_: List[str]=1E-12, a_: Dict=224, a_: Union[str, Any]=16, a_: Dict=3, a_: str=True, a_: Union[str, Any]=16, a_: Optional[int]=512, a_: Any=8, a_: Optional[Any]=2_048, a_: Tuple=0.75, a_: Optional[int]=False, **a_: Union[str, Any], ): '''simple docstring''' super().__init__(**a_ ) _snake_case : str = hidden_size _snake_case : Optional[int] = num_hidden_layers _snake_case : Tuple = num_attention_heads _snake_case : Union[str, Any] = intermediate_size _snake_case : List[Any] = hidden_act _snake_case : str = hidden_dropout_prob _snake_case : Optional[Any] = attention_probs_dropout_prob _snake_case : Any = initializer_range _snake_case : List[str] = layer_norm_eps _snake_case : List[Any] = image_size _snake_case : Union[str, Any] = patch_size _snake_case : List[Any] = num_channels _snake_case : List[str] = qkv_bias _snake_case : str = decoder_num_attention_heads _snake_case : Tuple = decoder_hidden_size _snake_case : str = decoder_num_hidden_layers _snake_case : int = decoder_intermediate_size _snake_case : Dict = mask_ratio _snake_case : List[Any] = norm_pix_loss
352
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices A_ = logging.get_logger(__name__) A_ = { '''microsoft/swin-tiny-patch4-window7-224''': ( '''https://huggingface.co/microsoft/swin-tiny-patch4-window7-224/resolve/main/config.json''' ), # See all Swin models at https://huggingface.co/models?filter=swin } class lowercase( __a , __a ): '''simple docstring''' lowercase__ = "swin" lowercase__ = { "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self: Any, a_: List[str]=224, a_: List[Any]=4, a_: List[Any]=3, a_: Dict=96, a_: List[str]=[2, 2, 6, 2], a_: int=[3, 6, 12, 24], a_: int=7, a_: str=4.0, a_: Optional[Any]=True, a_: Dict=0.0, a_: List[Any]=0.0, a_: List[str]=0.1, a_: Union[str, Any]="gelu", a_: Dict=False, a_: Union[str, Any]=0.02, a_: Optional[int]=1E-5, a_: Optional[int]=32, a_: Tuple=None, a_: Union[str, Any]=None, **a_: Any, ): '''simple docstring''' super().__init__(**a_ ) _snake_case : Any = image_size _snake_case : List[Any] = patch_size _snake_case : Tuple = num_channels _snake_case : str = embed_dim _snake_case : Union[str, Any] = depths _snake_case : int = len(a_ ) _snake_case : Union[str, Any] = num_heads _snake_case : List[str] = window_size _snake_case : str = mlp_ratio _snake_case : Union[str, Any] = qkv_bias _snake_case : Dict = hidden_dropout_prob _snake_case : str = attention_probs_dropout_prob _snake_case : Union[str, Any] = drop_path_rate _snake_case : Optional[int] = hidden_act _snake_case : str = use_absolute_embeddings _snake_case : Tuple = layer_norm_eps _snake_case : List[Any] = initializer_range _snake_case : Optional[Any] = encoder_stride # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model _snake_case : Any = int(embed_dim * 2 ** (len(a_ ) - 1) ) _snake_case : Any = ["""stem"""] + [f"stage{idx}" for idx in range(1, len(a_ ) + 1 )] _snake_case , _snake_case : List[str] = get_aligned_output_features_output_indices( out_features=a_, out_indices=a_, stage_names=self.stage_names ) class lowercase( __a ): '''simple docstring''' lowercase__ = version.parse("1.11" ) @property def UpperCamelCase_ ( self: Any ): '''simple docstring''' return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def UpperCamelCase_ ( self: List[str] ): '''simple docstring''' return 1E-4
132
0
import math def _snake_case( SCREAMING_SNAKE_CASE__ = 100 ) -> int: lowercase : Union[str, Any] = sum(i * i for i in range(1 , n + 1 ) ) lowercase : Tuple = int(math.pow(sum(range(1 , n + 1 ) ) , 2 ) ) return square_of_sum - sum_of_squares if __name__ == "__main__": print(F'''{solution() = }''')
20
import argparse import json from collections import OrderedDict import torch from huggingface_hub import cached_download, hf_hub_url from transformers import AutoImageProcessor, CvtConfig, CvtForImageClassification def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Tuple: lowercase : Union[str, Any] = [] embed.append( ( f"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.projection.weight", f"stage{idx}.patch_embed.proj.weight", ) ) embed.append( ( f"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.projection.bias", f"stage{idx}.patch_embed.proj.bias", ) ) embed.append( ( f"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.normalization.weight", f"stage{idx}.patch_embed.norm.weight", ) ) embed.append( ( f"cvt.encoder.stages.{idx}.embedding.convolution_embeddings.normalization.bias", f"stage{idx}.patch_embed.norm.bias", ) ) return embed def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> str: lowercase : Optional[Any] = [] attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.convolution.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.conv.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.bias", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.running_mean", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.running_mean", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.running_var", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.running_var", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.num_batches_tracked", f"stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.num_batches_tracked", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.convolution.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.conv.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.bias", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.running_mean", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.running_mean", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.running_var", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.running_var", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.num_batches_tracked", f"stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.num_batches_tracked", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.convolution.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.conv.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.weight", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.bias", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.running_mean", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.running_mean", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.running_var", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.running_var", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.num_batches_tracked", f"stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.num_batches_tracked", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_query.weight", f"stage{idx}.blocks.{cnt}.attn.proj_q.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_query.bias", f"stage{idx}.blocks.{cnt}.attn.proj_q.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_key.weight", f"stage{idx}.blocks.{cnt}.attn.proj_k.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_key.bias", f"stage{idx}.blocks.{cnt}.attn.proj_k.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_value.weight", f"stage{idx}.blocks.{cnt}.attn.proj_v.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_value.bias", f"stage{idx}.blocks.{cnt}.attn.proj_v.bias", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.output.dense.weight", f"stage{idx}.blocks.{cnt}.attn.proj.weight", ) ) attention_weights.append( ( f"cvt.encoder.stages.{idx}.layers.{cnt}.attention.output.dense.bias", f"stage{idx}.blocks.{cnt}.attn.proj.bias", ) ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.intermediate.dense.weight", f"stage{idx}.blocks.{cnt}.mlp.fc1.weight") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.intermediate.dense.bias", f"stage{idx}.blocks.{cnt}.mlp.fc1.bias") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.output.dense.weight", f"stage{idx}.blocks.{cnt}.mlp.fc2.weight") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.output.dense.bias", f"stage{idx}.blocks.{cnt}.mlp.fc2.bias") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_before.weight", f"stage{idx}.blocks.{cnt}.norm1.weight") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_before.bias", f"stage{idx}.blocks.{cnt}.norm1.bias") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_after.weight", f"stage{idx}.blocks.{cnt}.norm2.weight") ) attention_weights.append( (f"cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_after.bias", f"stage{idx}.blocks.{cnt}.norm2.bias") ) return attention_weights def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: lowercase : Optional[Any] = [] token.append((f"cvt.encoder.stages.{idx}.cls_token", """stage2.cls_token""") ) return token def _snake_case( ) -> Dict: lowercase : Optional[Any] = [] head.append(("""layernorm.weight""", """norm.weight""") ) head.append(("""layernorm.bias""", """norm.bias""") ) head.append(("""classifier.weight""", """head.weight""") ) head.append(("""classifier.bias""", """head.bias""") ) return head def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> str: lowercase : Any = """imagenet-1k-id2label.json""" lowercase : List[str] = 1_000 lowercase : int = """huggingface/label-files""" lowercase : Union[str, Any] = num_labels lowercase : Optional[Any] = json.load(open(cached_download(hf_hub_url(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , repo_type="""dataset""" ) ) , """r""" ) ) lowercase : List[Any] = {int(SCREAMING_SNAKE_CASE__ ): v for k, v in idalabel.items()} lowercase : Dict = idalabel lowercase : List[str] = {v: k for k, v in idalabel.items()} lowercase : List[str] = CvtConfig(num_labels=SCREAMING_SNAKE_CASE__ , idalabel=SCREAMING_SNAKE_CASE__ , labelaid=SCREAMING_SNAKE_CASE__ ) # For depth size 13 (13 = 1+2+10) if cvt_model.rsplit("""/""" , 1 )[-1][4:6] == "13": lowercase : Tuple = [1, 2, 10] # For depth size 21 (21 = 1+4+16) elif cvt_model.rsplit("""/""" , 1 )[-1][4:6] == "21": lowercase : Dict = [1, 4, 16] # For wide cvt (similar to wide-resnet) depth size 24 (w24 = 2 + 2 20) else: lowercase : int = [2, 2, 20] lowercase : Optional[int] = [3, 12, 16] lowercase : str = [192, 768, 1_024] lowercase : Union[str, Any] = CvtForImageClassification(SCREAMING_SNAKE_CASE__ ) lowercase : Union[str, Any] = AutoImageProcessor.from_pretrained("""facebook/convnext-base-224-22k-1k""" ) lowercase : Optional[Any] = image_size lowercase : Union[str, Any] = torch.load(SCREAMING_SNAKE_CASE__ , map_location=torch.device("""cpu""" ) ) lowercase : Optional[Any] = OrderedDict() lowercase : Tuple = [] for idx in range(len(config.depth ) ): if config.cls_token[idx]: lowercase : Optional[Any] = list_of_state_dict + cls_token(SCREAMING_SNAKE_CASE__ ) lowercase : str = list_of_state_dict + embeddings(SCREAMING_SNAKE_CASE__ ) for cnt in range(config.depth[idx] ): lowercase : List[str] = list_of_state_dict + attention(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : List[str] = list_of_state_dict + final() for gg in list_of_state_dict: print(SCREAMING_SNAKE_CASE__ ) for i in range(len(SCREAMING_SNAKE_CASE__ ) ): lowercase : Optional[Any] = original_weights[list_of_state_dict[i][1]] model.load_state_dict(SCREAMING_SNAKE_CASE__ ) model.save_pretrained(SCREAMING_SNAKE_CASE__ ) image_processor.save_pretrained(SCREAMING_SNAKE_CASE__ ) # Download the weights from zoo: https://1drv.ms/u/s!AhIXJn_J-blW9RzF3rMW7SsLHa8h?e=blQ0Al if __name__ == "__main__": lowercase : Tuple = argparse.ArgumentParser() parser.add_argument( """--cvt_model""", default="""cvt-w24""", type=str, help="""Name of the cvt model you'd like to convert.""", ) parser.add_argument( """--image_size""", default=384, type=int, help="""Input Image Size""", ) parser.add_argument( """--cvt_file_name""", default=R"""cvtmodels\CvT-w24-384x384-IN-22k.pth""", type=str, help="""Input Image Size""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) lowercase : Optional[int] = parser.parse_args() convert_cvt_checkpoint(args.cvt_model, args.image_size, args.cvt_file_name, args.pytorch_dump_folder_path)
20
1
import itertools import os import random import tempfile import unittest import numpy as np from transformers import TvltFeatureExtractor, is_datasets_available from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch if is_datasets_available(): from datasets import load_dataset a__ = random.Random() def __UpperCAmelCase ( __a : Tuple ,__a : str=1.0 ,__a : Optional[int]=None ,__a : List[Any]=None ) -> Any: """simple docstring""" if rng is None: _a : Dict = global_rng _a : Optional[Any] = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __init__( self , _a , _a=7 , _a=4_0_0 , _a=2_0_0_0 , _a=2_0_4_8 , _a=1_2_8 , _a=1 , _a=5_1_2 , _a=3_0 , _a=4_4_1_0_0 , ) -> List[Any]: _a : Optional[Any] = parent _a : str = batch_size _a : List[str] = min_seq_length _a : str = max_seq_length _a : Dict = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) _a : List[Any] = spectrogram_length _a : List[str] = feature_size _a : List[Any] = num_audio_channels _a : Tuple = hop_length _a : Optional[int] = chunk_length _a : int = sampling_rate def __lowercase ( self ) -> Union[str, Any]: return { "spectrogram_length": self.spectrogram_length, "feature_size": self.feature_size, "num_audio_channels": self.num_audio_channels, "hop_length": self.hop_length, "chunk_length": self.chunk_length, "sampling_rate": self.sampling_rate, } def __lowercase ( self , _a=False , _a=False ) -> List[Any]: def _flatten(_a ): return list(itertools.chain(*_a ) ) if equal_length: _a : List[Any] = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size _a : List[Any] = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: _a : str = [np.asarray(_a ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : List[Any] = TvltFeatureExtractor def __lowercase ( self ) -> Dict: _a : List[str] = TvltFeatureExtractionTester(self ) def __lowercase ( self ) -> Any: _a : List[Any] = self.feature_extraction_class(**self.feat_extract_dict ) self.assertTrue(hasattr(_a , '''spectrogram_length''' ) ) self.assertTrue(hasattr(_a , '''feature_size''' ) ) self.assertTrue(hasattr(_a , '''num_audio_channels''' ) ) self.assertTrue(hasattr(_a , '''hop_length''' ) ) self.assertTrue(hasattr(_a , '''chunk_length''' ) ) self.assertTrue(hasattr(_a , '''sampling_rate''' ) ) def __lowercase ( self ) -> Optional[int]: _a : Optional[Any] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: _a : int = feat_extract_first.save_pretrained(_a )[0] check_json_file_has_correct_format(_a ) _a : Dict = self.feature_extraction_class.from_pretrained(_a ) _a : List[Any] = feat_extract_first.to_dict() _a : Union[str, Any] = feat_extract_second.to_dict() _a : Any = dict_first.pop('''mel_filters''' ) _a : int = dict_second.pop('''mel_filters''' ) self.assertTrue(np.allclose(_a , _a ) ) self.assertEqual(_a , _a ) def __lowercase ( self ) -> Optional[int]: _a : Any = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: _a : Optional[int] = os.path.join(_a , '''feat_extract.json''' ) feat_extract_first.to_json_file(_a ) _a : List[str] = self.feature_extraction_class.from_json_file(_a ) _a : List[Any] = feat_extract_first.to_dict() _a : Dict = feat_extract_second.to_dict() _a : str = dict_first.pop('''mel_filters''' ) _a : str = dict_second.pop('''mel_filters''' ) self.assertTrue(np.allclose(_a , _a ) ) self.assertEqual(_a , _a ) def __lowercase ( self ) -> Union[str, Any]: # Initialize feature_extractor _a : Union[str, Any] = self.feature_extraction_class(**self.feat_extract_dict ) # create three inputs of length 800, 1000, and 1200 _a : Any = [floats_list((1, x) )[0] for x in range(8_0_0 , 1_4_0_0 , 2_0_0 )] _a : List[str] = [np.asarray(_a ) for speech_input in speech_inputs] # Test not batched input _a : Tuple = feature_extractor(np_speech_inputs[0] , return_tensors='''np''' , sampling_rate=4_4_1_0_0 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test batched _a : Dict = feature_extractor(_a , return_tensors='''np''' , sampling_rate=4_4_1_0_0 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test audio masking _a : Union[str, Any] = feature_extractor( _a , return_tensors='''np''' , sampling_rate=4_4_1_0_0 , mask_audio=_a ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test 2-D numpy arrays are batched. _a : Optional[Any] = [floats_list((1, x) )[0] for x in (8_0_0, 8_0_0, 8_0_0)] _a : int = np.asarray(_a ) _a : Tuple = feature_extractor(_a , return_tensors='''np''' , sampling_rate=4_4_1_0_0 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) def __lowercase ( self , _a ) -> Optional[Any]: _a : List[Any] = load_dataset('''hf-internal-testing/librispeech_asr_dummy''' , '''clean''' , split='''validation''' ) # automatic decoding with librispeech _a : Optional[int] = ds.sort('''id''' ).select(range(_a ) )[:num_samples]['''audio'''] return [x["array"] for x in speech_samples] def __lowercase ( self ) -> int: _a : Union[str, Any] = self._load_datasamples(1 ) _a : int = TvltFeatureExtractor() _a : Union[str, Any] = feature_extractor(_a , return_tensors='''pt''' ).audio_values self.assertEquals(audio_values.shape , (1, 1, 1_9_2, 1_2_8) ) _a : Union[str, Any] = torch.tensor([[-0.3032, -0.2708], [-0.4434, -0.4007]] ) self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2] , _a , atol=1e-4 ) )
15
import argparse import json import logging import os import sys from unittest.mock import patch from transformers.testing_utils import TestCasePlus, get_gpu_count, slow a__ = [ os.path.join(os.path.dirname(__file__), dirname) for dirname in [ '''text-classification''', '''language-modeling''', '''summarization''', '''token-classification''', '''question-answering''', ] ] sys.path.extend(SRC_DIRS) if SRC_DIRS is not None: import run_clm_flax import run_flax_glue import run_flax_ner import run_mlm_flax import run_qa import run_summarization_flax import run_ta_mlm_flax logging.basicConfig(level=logging.DEBUG) a__ = logging.getLogger() def __UpperCAmelCase ( ) -> Optional[int]: """simple docstring""" _a : Any = argparse.ArgumentParser() parser.add_argument('''-f''' ) _a : Dict = parser.parse_args() return args.f def __UpperCAmelCase ( __a : Optional[int] ,__a : List[str]="eval" ) -> Any: """simple docstring""" _a : Any = os.path.join(__a ,F"""{split}_results.json""" ) if os.path.exists(__a ): with open(__a ,'''r''' ) as f: return json.load(__a ) raise ValueError(F"""can't find {path}""" ) a__ = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class UpperCAmelCase_ ( __lowercase ): """simple docstring""" def __lowercase ( self ) -> str: _a : Any = self.get_auto_remove_tmp_dir() _a : Optional[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(_a , '''argv''' , _a ): run_flax_glue.main() _a : Any = get_results(_a ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.75 ) @slow def __lowercase ( self ) -> Dict: _a : Tuple = self.get_auto_remove_tmp_dir() _a : Tuple = 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(_a , '''argv''' , _a ): run_clm_flax.main() _a : List[str] = get_results(_a ) self.assertLess(result['''eval_perplexity'''] , 1_0_0 ) @slow def __lowercase ( self ) -> Optional[int]: _a : str = self.get_auto_remove_tmp_dir() _a : Optional[int] = 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(_a , '''argv''' , _a ): run_summarization_flax.main() _a : Optional[int] = get_results(_a , split='''test''' ) self.assertGreaterEqual(result['''test_rouge1'''] , 1_0 ) self.assertGreaterEqual(result['''test_rouge2'''] , 2 ) self.assertGreaterEqual(result['''test_rougeL'''] , 7 ) self.assertGreaterEqual(result['''test_rougeLsum'''] , 7 ) @slow def __lowercase ( self ) -> Tuple: _a : List[str] = self.get_auto_remove_tmp_dir() _a : List[Any] = 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(_a , '''argv''' , _a ): run_mlm_flax.main() _a : List[Any] = get_results(_a ) self.assertLess(result['''eval_perplexity'''] , 4_2 ) @slow def __lowercase ( self ) -> Dict: _a : Optional[Any] = self.get_auto_remove_tmp_dir() _a : 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(_a , '''argv''' , _a ): run_ta_mlm_flax.main() _a : List[Any] = get_results(_a ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.42 ) @slow def __lowercase ( self ) -> Optional[Any]: # with so little data distributed training needs more epochs to get the score on par with 0/1 gpu _a : Any = 7 if get_gpu_count() > 1 else 2 _a : List[Any] = self.get_auto_remove_tmp_dir() _a : List[Any] = 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(_a , '''argv''' , _a ): run_flax_ner.main() _a : Dict = get_results(_a ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.75 ) self.assertGreaterEqual(result['''eval_f1'''] , 0.3 ) @slow def __lowercase ( self ) -> Any: _a : Optional[int] = self.get_auto_remove_tmp_dir() _a : Union[str, Any] = 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(_a , '''argv''' , _a ): run_qa.main() _a : Any = get_results(_a ) self.assertGreaterEqual(result['''eval_f1'''] , 3_0 ) self.assertGreaterEqual(result['''eval_exact'''] , 3_0 )
15
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 lowercase__ : int = logging.get_logger(__name__) class _UpperCAmelCase ( enum.Enum): _lowerCAmelCase : Any = 0 _lowerCAmelCase : Optional[int] = 1 @add_end_docstrings(UpperCAmelCase__) class _UpperCAmelCase ( UpperCAmelCase__): _lowerCAmelCase : str = """generated""" def __init__( self : Optional[int] , *lowercase_ : Optional[Any] , **lowercase_ : Optional[Any] ): super().__init__(*_UpperCAmelCase , **_UpperCAmelCase ) self.check_model_type( TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING if self.framework == '''tf''' else MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING ) def _snake_case ( self : str , lowercase_ : Optional[int]=None , lowercase_ : List[Any]=None , lowercase_ : List[str]=None , lowercase_ : List[str]=None , lowercase_ : Dict=None , lowercase_ : Tuple=None , **lowercase_ : Dict , ): snake_case_ : Union[str, Any] = {} if truncation is not None: snake_case_ : Dict = truncation snake_case_ : List[str] = generate_kwargs snake_case_ : Optional[Any] = {} if return_tensors is not None and return_type is None: snake_case_ : Dict = ReturnType.TENSORS if return_tensors else ReturnType.TEXT if return_type is not None: snake_case_ : Dict = return_type if clean_up_tokenization_spaces is not None: snake_case_ : Optional[int] = clean_up_tokenization_spaces if stop_sequence is not None: snake_case_ : Union[str, Any] = self.tokenizer.encode(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) if len(_UpperCAmelCase ) > 1: warnings.warn( '''Stopping on a multiple token sequence is not yet supported on transformers. The first token of''' ''' the stop sequence will be used as the stop sequence string in the interim.''' ) snake_case_ : Tuple = stop_sequence_ids[0] return preprocess_params, forward_params, postprocess_params def _snake_case ( self : Any , lowercase_ : int , lowercase_ : int , lowercase_ : int ): return True def _snake_case ( self : Union[str, Any] , *lowercase_ : Optional[Any] , lowercase_ : Tuple ): snake_case_ : Optional[int] = self.model.config.prefix if self.model.config.prefix is not None else '''''' if isinstance(args[0] , _UpperCAmelCase ): if self.tokenizer.pad_token_id is None: raise ValueError('''Please make sure that the tokenizer has a pad_token_id when using a batch input''' ) snake_case_ : Any = ([prefix + arg for arg in args[0]],) snake_case_ : str = True elif isinstance(args[0] , _UpperCAmelCase ): snake_case_ : List[str] = (prefix + args[0],) snake_case_ : 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`" ) snake_case_ : str = self.tokenizer(*_UpperCAmelCase , padding=_UpperCAmelCase , truncation=_UpperCAmelCase , return_tensors=self.framework ) # This is produced by tokenizers but is an invalid generate kwargs if "token_type_ids" in inputs: del inputs["token_type_ids"] return inputs def __call__( self : Optional[int] , *lowercase_ : Dict , **lowercase_ : Optional[int] ): snake_case_ : List[str] = super().__call__(*_UpperCAmelCase , **_UpperCAmelCase ) if ( isinstance(args[0] , _UpperCAmelCase ) and all(isinstance(_UpperCAmelCase , _UpperCAmelCase ) for el in args[0] ) and all(len(_UpperCAmelCase ) == 1 for res in result ) ): return [res[0] for res in result] return result def _snake_case ( self : Tuple , lowercase_ : Optional[int] , lowercase_ : Union[str, Any]=TruncationStrategy.DO_NOT_TRUNCATE , **lowercase_ : Any ): snake_case_ : Tuple = self._parse_and_tokenize(_UpperCAmelCase , truncation=_UpperCAmelCase , **_UpperCAmelCase ) return inputs def _snake_case ( self : Optional[int] , lowercase_ : str , **lowercase_ : Any ): if self.framework == "pt": snake_case_, snake_case_ : str = model_inputs['''input_ids'''].shape elif self.framework == "tf": snake_case_, snake_case_ : Dict = tf.shape(model_inputs['''input_ids'''] ).numpy() snake_case_ : Dict = generate_kwargs.get('''min_length''' , self.model.config.min_length ) snake_case_ : int = generate_kwargs.get('''max_length''' , self.model.config.max_length ) self.check_inputs(_UpperCAmelCase , generate_kwargs['''min_length'''] , generate_kwargs['''max_length'''] ) snake_case_ : List[str] = self.model.generate(**_UpperCAmelCase , **_UpperCAmelCase ) snake_case_ : Tuple = output_ids.shape[0] if self.framework == "pt": snake_case_ : List[str] = output_ids.reshape(_UpperCAmelCase , out_b // in_b , *output_ids.shape[1:] ) elif self.framework == "tf": snake_case_ : Union[str, Any] = tf.reshape(_UpperCAmelCase , (in_b, out_b // in_b, *output_ids.shape[1:]) ) return {"output_ids": output_ids} def _snake_case ( self : Optional[Any] , lowercase_ : Optional[Any] , lowercase_ : int=ReturnType.TEXT , lowercase_ : Any=False ): snake_case_ : Optional[int] = [] for output_ids in model_outputs["output_ids"][0]: if return_type == ReturnType.TENSORS: snake_case_ : Any = {f"{self.return_name}_token_ids": output_ids} elif return_type == ReturnType.TEXT: snake_case_ : Any = { f"{self.return_name}_text": self.tokenizer.decode( _UpperCAmelCase , skip_special_tokens=_UpperCAmelCase , clean_up_tokenization_spaces=_UpperCAmelCase , ) } records.append(_UpperCAmelCase ) return records @add_end_docstrings(UpperCAmelCase__) class _UpperCAmelCase ( UpperCAmelCase__): _lowerCAmelCase : Dict = """summary""" def __call__( self : List[str] , *lowercase_ : Union[str, Any] , **lowercase_ : str ): return super().__call__(*_UpperCAmelCase , **_UpperCAmelCase ) def _snake_case ( self : List[str] , lowercase_ : int , lowercase_ : int , lowercase_ : int ): if max_length < min_length: logger.warning(f"Your min_length={min_length} must be inferior than your max_length={max_length}." ) if input_length < max_length: logger.warning( f"Your max_length is set to {max_length}, but your input_length is only {input_length}. Since this is " '''a summarization task, where outputs shorter than the input are typically wanted, you might ''' f"consider decreasing max_length manually, e.g. summarizer(\'...\', max_length={input_length//2})" ) @add_end_docstrings(UpperCAmelCase__) class _UpperCAmelCase ( UpperCAmelCase__): _lowerCAmelCase : List[str] = """translation""" def _snake_case ( self : Optional[Any] , lowercase_ : int , lowercase_ : int , lowercase_ : int ): if input_length > 0.9 * max_length: logger.warning( f"Your input_length: {input_length} is bigger than 0.9 * max_length: {max_length}. You might consider " '''increasing your max_length manually, e.g. translator(\'...\', max_length=400)''' ) return True def _snake_case ( self : List[str] , *lowercase_ : Any , lowercase_ : Optional[int]=TruncationStrategy.DO_NOT_TRUNCATE , lowercase_ : Union[str, Any]=None , lowercase_ : List[str]=None ): if getattr(self.tokenizer , '''_build_translation_inputs''' , _UpperCAmelCase ): return self.tokenizer._build_translation_inputs( *_UpperCAmelCase , return_tensors=self.framework , truncation=_UpperCAmelCase , src_lang=_UpperCAmelCase , tgt_lang=_UpperCAmelCase ) else: return super()._parse_and_tokenize(*_UpperCAmelCase , truncation=_UpperCAmelCase ) def _snake_case ( self : str , lowercase_ : Any=None , lowercase_ : str=None , **lowercase_ : Any ): snake_case_, snake_case_, snake_case_ : int = super()._sanitize_parameters(**_UpperCAmelCase ) if src_lang is not None: snake_case_ : Optional[Any] = src_lang if tgt_lang is not None: snake_case_ : Optional[int] = tgt_lang if src_lang is None and tgt_lang is None: # Backward compatibility, direct arguments use is preferred. snake_case_ : str = kwargs.get('''task''' , self.task ) snake_case_ : Optional[Any] = task.split('''_''' ) if task and len(_UpperCAmelCase ) == 4: # translation, XX, to YY snake_case_ : str = items[1] snake_case_ : str = items[3] return preprocess_params, forward_params, postprocess_params def __call__( self : Union[str, Any] , *lowercase_ : Union[str, Any] , **lowercase_ : List[Any] ): return super().__call__(*_UpperCAmelCase , **_UpperCAmelCase )
264
import unittest import numpy as np from transformers import BertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_flax_available(): from transformers.models.bert.modeling_flax_bert import ( FlaxBertForMaskedLM, FlaxBertForMultipleChoice, FlaxBertForNextSentencePrediction, FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification, FlaxBertForTokenClassification, FlaxBertModel, ) class A ( unittest.TestCase ): '''simple docstring''' def __init__(self : Optional[Any] , _UpperCAmelCase : Tuple , _UpperCAmelCase : str=13 , _UpperCAmelCase : List[str]=7 , _UpperCAmelCase : Union[str, Any]=True , _UpperCAmelCase : Dict=True , _UpperCAmelCase : str=True , _UpperCAmelCase : str=True , _UpperCAmelCase : Dict=99 , _UpperCAmelCase : Any=32 , _UpperCAmelCase : List[str]=5 , _UpperCAmelCase : Union[str, Any]=4 , _UpperCAmelCase : str=37 , _UpperCAmelCase : Union[str, Any]="gelu" , _UpperCAmelCase : Any=0.1 , _UpperCAmelCase : int=0.1 , _UpperCAmelCase : Dict=512 , _UpperCAmelCase : Optional[int]=16 , _UpperCAmelCase : str=2 , _UpperCAmelCase : List[Any]=0.02 , _UpperCAmelCase : List[str]=4 , ) -> List[Any]: """simple docstring""" lowercase__ = parent lowercase__ = batch_size lowercase__ = seq_length lowercase__ = is_training lowercase__ = use_attention_mask lowercase__ = use_token_type_ids lowercase__ = use_labels lowercase__ = vocab_size lowercase__ = hidden_size lowercase__ = num_hidden_layers lowercase__ = num_attention_heads lowercase__ = intermediate_size lowercase__ = hidden_act lowercase__ = hidden_dropout_prob lowercase__ = attention_probs_dropout_prob lowercase__ = max_position_embeddings lowercase__ = type_vocab_size lowercase__ = type_sequence_label_size lowercase__ = initializer_range lowercase__ = num_choices def lowerCamelCase__ (self : List[str] ) -> Dict: """simple docstring""" lowercase__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowercase__ = None if self.use_attention_mask: lowercase__ = random_attention_mask([self.batch_size, self.seq_length] ) lowercase__ = None if self.use_token_type_ids: lowercase__ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowercase__ = BertConfig( 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=_UpperCAmelCase , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def lowerCamelCase__ (self : int ) -> Any: """simple docstring""" lowercase__ = self.prepare_config_and_inputs() lowercase__ , lowercase__ , lowercase__ , lowercase__ = config_and_inputs lowercase__ = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict def lowerCamelCase__ (self : Tuple ) -> str: """simple docstring""" lowercase__ = self.prepare_config_and_inputs() lowercase__ , lowercase__ , lowercase__ , lowercase__ = config_and_inputs lowercase__ = True lowercase__ = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) lowercase__ = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, attention_mask, encoder_hidden_states, encoder_attention_mask, ) @require_flax class A ( UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' A__ = True A__ = ( ( FlaxBertModel, FlaxBertForPreTraining, FlaxBertForMaskedLM, FlaxBertForMultipleChoice, FlaxBertForQuestionAnswering, FlaxBertForNextSentencePrediction, FlaxBertForSequenceClassification, FlaxBertForTokenClassification, FlaxBertForQuestionAnswering, ) if is_flax_available() else () ) def lowerCamelCase__ (self : Optional[int] ) -> List[str]: """simple docstring""" lowercase__ = FlaxBertModelTester(self ) @slow def lowerCamelCase__ (self : List[str] ) -> Union[str, Any]: """simple docstring""" lowercase__ = FlaxBertModel.from_pretrained("""bert-base-cased""" ) lowercase__ = model(np.ones((1, 1) ) ) self.assertIsNotNone(_UpperCAmelCase )
305
0
'''simple docstring''' # NOTE: This file is deprecated and will be removed in a future version. # It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works from ...utils import deprecate from ..controlnet.multicontrolnet import MultiControlNetModel # noqa: F401 from ..controlnet.pipeline_controlnet import StableDiffusionControlNetPipeline # noqa: F401 deprecate( '''stable diffusion controlnet''', '''0.22.0''', '''Importing `StableDiffusionControlNetPipeline` or `MultiControlNetModel` from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet is deprecated. Please import `from diffusers import StableDiffusionControlNetPipeline` instead.''', standard_warn=False, stacklevel=3, )
101
'''simple docstring''' from __future__ import annotations import numpy as np from numpy import floataa from numpy.typing import NDArray def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, ) -> list[float]: A_ , A_ = coefficient_matrix.shape A_ , A_ = constant_matrix.shape if rowsa != colsa: A_ = F'''Coefficient matrix dimensions must be nxn but received {rowsa}x{colsa}''' raise ValueError(UpperCAmelCase__ ) if colsa != 1: A_ = F'''Constant matrix must be nx1 but received {rowsa}x{colsa}''' raise ValueError(UpperCAmelCase__ ) if rowsa != rowsa: A_ = ( """Coefficient and constant matrices dimensions must be nxn and nx1 but """ F'''received {rowsa}x{colsa} and {rowsa}x{colsa}''' ) raise ValueError(UpperCAmelCase__ ) if len(UpperCAmelCase__ ) != rowsa: A_ = ( """Number of initial values must be equal to number of rows in coefficient """ F'''matrix but received {len(UpperCAmelCase__ )} and {rowsa}''' ) raise ValueError(UpperCAmelCase__ ) if iterations <= 0: raise ValueError("""Iterations must be at least 1""" ) A_ = np.concatenate( (coefficient_matrix, constant_matrix), axis=1 ) A_ , A_ = table.shape strictly_diagonally_dominant(UpperCAmelCase__ ) # Iterates the whole matrix for given number of times for _ in range(UpperCAmelCase__ ): A_ = [] for row in range(UpperCAmelCase__ ): A_ = 0 for col in range(UpperCAmelCase__ ): if col == row: A_ = table[row][col] elif col == cols - 1: A_ = table[row][col] else: temp += (-1) * table[row][col] * init_val[col] A_ = (temp + val) / denom new_val.append(UpperCAmelCase__ ) A_ = new_val return [float(UpperCAmelCase__ ) for i in new_val] def UpperCAmelCase__ ( UpperCAmelCase__ ) -> bool: A_ , A_ = table.shape A_ = True for i in range(0, UpperCAmelCase__ ): A_ = 0 for j in range(0, cols - 1 ): if i == j: continue else: total += table[i][j] if table[i][i] <= total: raise ValueError("""Coefficient matrix is not strictly diagonally dominant""" ) return is_diagonally_dominant # Test Cases if __name__ == "__main__": import doctest doctest.testmod()
101
1
'''simple docstring''' import argparse import torch from transformers import ( UniSpeechSatConfig, UniSpeechSatForAudioFrameClassification, UniSpeechSatForSequenceClassification, UniSpeechSatForXVector, WavaVecaFeatureExtractor, logging, ) logging.set_verbosity_info() UpperCamelCase__ : Optional[Any] = logging.get_logger(__name__) def UpperCAmelCase ( a_ , a_ , a_ ) -> Optional[Any]: """simple docstring""" A_ : Dict = UniSpeechSatForSequenceClassification.from_pretrained(a_ , config=a_ ) A_ : int = downstream_dict["""projector.weight"""] A_ : List[str] = downstream_dict["""projector.bias"""] A_ : List[str] = downstream_dict["""model.post_net.linear.weight"""] A_ : Optional[int] = downstream_dict["""model.post_net.linear.bias"""] return model def UpperCAmelCase ( a_ , a_ , a_ ) -> Optional[Any]: """simple docstring""" A_ : int = UniSpeechSatForAudioFrameClassification.from_pretrained(a_ , config=a_ ) A_ : Dict = downstream_dict["""model.linear.weight"""] A_ : Tuple = downstream_dict["""model.linear.bias"""] return model def UpperCAmelCase ( a_ , a_ , a_ ) -> str: """simple docstring""" A_ : Optional[int] = UniSpeechSatForXVector.from_pretrained(a_ , config=a_ ) A_ : Optional[Any] = downstream_dict["""connector.weight"""] A_ : str = downstream_dict["""connector.bias"""] for i, kernel_size in enumerate(hf_config.tdnn_kernel ): A_ : Dict = downstream_dict[ F"model.framelevel_feature_extractor.module.{i}.kernel.weight" ] A_ : str = downstream_dict[F"model.framelevel_feature_extractor.module.{i}.kernel.bias"] A_ : Any = downstream_dict["""model.utterancelevel_feature_extractor.linear1.weight"""] A_ : Any = downstream_dict["""model.utterancelevel_feature_extractor.linear1.bias"""] A_ : str = downstream_dict["""model.utterancelevel_feature_extractor.linear2.weight"""] A_ : Tuple = downstream_dict["""model.utterancelevel_feature_extractor.linear2.bias"""] A_ : Any = downstream_dict["""objective.W"""] return model @torch.no_grad() def UpperCAmelCase ( a_ , a_ , a_ , a_ ) -> str: """simple docstring""" A_ : Optional[Any] = torch.load(a_ , map_location="""cpu""" ) A_ : Optional[Any] = checkpoint["""Downstream"""] A_ : Optional[int] = UniSpeechSatConfig.from_pretrained(a_ ) A_ : Optional[int] = WavaVecaFeatureExtractor.from_pretrained( a_ , return_attention_mask=a_ , do_normalize=a_ ) A_ : List[str] = hf_config.architectures[0] if arch.endswith("""ForSequenceClassification""" ): A_ : Any = convert_classification(a_ , a_ , a_ ) elif arch.endswith("""ForAudioFrameClassification""" ): A_ : Any = convert_diarization(a_ , a_ , a_ ) elif arch.endswith("""ForXVector""" ): A_ : Optional[int] = convert_xvector(a_ , a_ , a_ ) else: raise NotImplementedError(F"S3PRL weights conversion is not supported for {arch}" ) if hf_config.use_weighted_layer_sum: A_ : Any = checkpoint["""Featurizer"""]["""weights"""] hf_feature_extractor.save_pretrained(a_ ) hf_model.save_pretrained(a_ ) if __name__ == "__main__": UpperCamelCase__ : Union[str, Any] = argparse.ArgumentParser() parser.add_argument( '--base_model_name', default=None, type=str, help='Name of the huggingface pretrained base model.' ) parser.add_argument('--config_path', default=None, type=str, help='Path to the huggingface classifier config.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to the s3prl checkpoint.') parser.add_argument('--model_dump_path', default=None, type=str, help='Path to the final converted model.') UpperCamelCase__ : List[str] = parser.parse_args() convert_saprl_checkpoint(args.base_model_name, args.config_path, args.checkpoint_path, args.model_dump_path)
344
'''simple docstring''' from __future__ import annotations import inspect import unittest from typing import List, Tuple from transformers import RegNetConfig 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 TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFRegNetForImageClassification, TFRegNetModel if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class _lowerCAmelCase : """simple docstring""" def __init__( self , _lowerCamelCase , _lowerCamelCase=3 , _lowerCamelCase=32 , _lowerCamelCase=3 , _lowerCamelCase=10 , _lowerCamelCase=[10, 20, 30, 40] , _lowerCamelCase=[1, 1, 2, 1] , _lowerCamelCase=True , _lowerCamelCase=True , _lowerCamelCase="relu" , _lowerCamelCase=3 , _lowerCamelCase=None , ) -> List[str]: A_ : Any = parent A_ : List[Any] = batch_size A_ : List[Any] = image_size A_ : Optional[int] = num_channels A_ : Tuple = embeddings_size A_ : str = hidden_sizes A_ : Optional[Any] = depths A_ : Any = is_training A_ : int = use_labels A_ : int = hidden_act A_ : Optional[Any] = num_labels A_ : str = scope A_ : Optional[int] = len(_lowerCamelCase ) def UpperCAmelCase_ ( self ) -> Tuple: A_ : Optional[Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A_ : Dict = None if self.use_labels: A_ : Optional[Any] = ids_tensor([self.batch_size] , self.num_labels ) A_ : Union[str, Any] = self.get_config() return config, pixel_values, labels def UpperCAmelCase_ ( self ) -> Optional[Any]: return RegNetConfig( num_channels=self.num_channels , embeddings_size=self.embeddings_size , hidden_sizes=self.hidden_sizes , depths=self.depths , hidden_act=self.hidden_act , num_labels=self.num_labels , ) def UpperCAmelCase_ ( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> List[str]: A_ : Dict = TFRegNetModel(config=_lowerCamelCase ) A_ : Optional[int] = model(_lowerCamelCase , training=_lowerCamelCase ) # expected last hidden states: B, C, H // 32, W // 32 self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def UpperCAmelCase_ ( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> Optional[int]: A_ : Optional[Any] = self.num_labels A_ : int = TFRegNetForImageClassification(_lowerCamelCase ) A_ : Tuple = model(_lowerCamelCase , labels=_lowerCamelCase , training=_lowerCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCAmelCase_ ( self ) -> str: A_ : Any = self.prepare_config_and_inputs() A_ , A_ , A_ : str = config_and_inputs A_ : Optional[int] = {"""pixel_values""": pixel_values} return config, inputs_dict @require_tf class _lowerCAmelCase ( __A, __A, unittest.TestCase ): """simple docstring""" lowerCamelCase = (TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else () lowerCamelCase = ( {'''feature-extraction''': TFRegNetModel, '''image-classification''': TFRegNetForImageClassification} if is_tf_available() else {} ) lowerCamelCase = False lowerCamelCase = False lowerCamelCase = False lowerCamelCase = False lowerCamelCase = False def UpperCAmelCase_ ( self ) -> Optional[Any]: A_ : Dict = TFRegNetModelTester(self ) A_ : Optional[int] = ConfigTester(self , config_class=_lowerCamelCase , has_text_modality=_lowerCamelCase ) def UpperCAmelCase_ ( self ) -> str: return @unittest.skip(reason="""RegNet does not use inputs_embeds""" ) def UpperCAmelCase_ ( self ) -> Dict: pass @unittest.skipIf( not is_tf_available() or len(tf.config.list_physical_devices("""GPU""" ) ) == 0 , reason="""TF does not support backprop for grouped convolutions on CPU.""" , ) @slow def UpperCAmelCase_ ( self ) -> int: super().test_keras_fit() @unittest.skip(reason="""RegNet does not support input and output embeddings""" ) def UpperCAmelCase_ ( self ) -> Optional[Any]: pass def UpperCAmelCase_ ( self ) -> int: A_ , A_ : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ : Optional[Any] = model_class(_lowerCamelCase ) A_ : Optional[int] = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A_ : int = [*signature.parameters.keys()] A_ : Any = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , _lowerCamelCase ) def UpperCAmelCase_ ( self ) -> Tuple: A_ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_lowerCamelCase ) def UpperCAmelCase_ ( self ) -> Dict: def check_hidden_states_output(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): A_ : Optional[int] = model_class(_lowerCamelCase ) A_ : List[Any] = model(**self._prepare_for_class(_lowerCamelCase , _lowerCamelCase ) , training=_lowerCamelCase ) A_ : List[str] = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states A_ : Optional[int] = self.model_tester.num_stages self.assertEqual(len(_lowerCamelCase ) , expected_num_stages + 1 ) # RegNet's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 2, self.model_tester.image_size // 2] , ) A_ , A_ : Any = self.model_tester.prepare_config_and_inputs_for_common() A_ : List[str] = ["""basic""", """bottleneck"""] for model_class in self.all_model_classes: for layer_type in layers_type: A_ : Dict = layer_type A_ : List[Any] = True check_hidden_states_output(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A_ : str = True check_hidden_states_output(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) def UpperCAmelCase_ ( self ) -> Dict: A_ , A_ : List[str] = self.model_tester.prepare_config_and_inputs_for_common() def check_equivalence(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase={} ): A_ : Dict = model(_lowerCamelCase , return_dict=_lowerCamelCase , **_lowerCamelCase ) A_ : Optional[Any] = model(_lowerCamelCase , return_dict=_lowerCamelCase , **_lowerCamelCase ).to_tuple() def recursive_check(_lowerCamelCase , _lowerCamelCase ): if isinstance(_lowerCamelCase , (List, Tuple) ): for tuple_iterable_value, dict_iterable_value in zip(_lowerCamelCase , _lowerCamelCase ): recursive_check(_lowerCamelCase , _lowerCamelCase ) elif tuple_object is None: return else: self.assertTrue( all(tf.equal(_lowerCamelCase , _lowerCamelCase ) ) , msg=( """Tuple and dict output are not equal. Difference:""" F" {tf.math.reduce_max(tf.abs(tuple_object - dict_object ) )}" ) , ) recursive_check(_lowerCamelCase , _lowerCamelCase ) for model_class in self.all_model_classes: A_ : Optional[Any] = model_class(_lowerCamelCase ) A_ : Optional[Any] = self._prepare_for_class(_lowerCamelCase , _lowerCamelCase ) A_ : Optional[int] = self._prepare_for_class(_lowerCamelCase , _lowerCamelCase ) check_equivalence(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) A_ : Any = self._prepare_for_class(_lowerCamelCase , _lowerCamelCase , return_labels=_lowerCamelCase ) A_ : Tuple = self._prepare_for_class(_lowerCamelCase , _lowerCamelCase , return_labels=_lowerCamelCase ) check_equivalence(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) A_ : Dict = self._prepare_for_class(_lowerCamelCase , _lowerCamelCase ) A_ : int = self._prepare_for_class(_lowerCamelCase , _lowerCamelCase ) check_equivalence(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase , {"""output_hidden_states""": True} ) A_ : Tuple = self._prepare_for_class(_lowerCamelCase , _lowerCamelCase , return_labels=_lowerCamelCase ) A_ : str = self._prepare_for_class(_lowerCamelCase , _lowerCamelCase , return_labels=_lowerCamelCase ) check_equivalence(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase , {"""output_hidden_states""": True} ) def UpperCAmelCase_ ( self ) -> str: A_ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_lowerCamelCase ) @slow def UpperCAmelCase_ ( self ) -> Tuple: for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ : Dict = TFRegNetModel.from_pretrained(_lowerCamelCase ) self.assertIsNotNone(_lowerCamelCase ) def UpperCAmelCase ( ) -> Tuple: """simple docstring""" A_ : Optional[Any] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_tf @require_vision class _lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @cached_property def UpperCAmelCase_ ( self ) -> int: return ( AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def UpperCAmelCase_ ( self ) -> Optional[Any]: A_ : str = TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) A_ : Tuple = self.default_image_processor A_ : Optional[int] = prepare_img() A_ : Any = image_processor(images=_lowerCamelCase , return_tensors="""tf""" ) # forward pass A_ : List[Any] = model(**_lowerCamelCase , training=_lowerCamelCase ) # verify the logits A_ : Optional[Any] = tf.TensorShape((1, 1000) ) self.assertEqual(outputs.logits.shape , _lowerCamelCase ) A_ : Optional[Any] = tf.constant([-0.4180, -1.5051, -3.4836] ) tf.debugging.assert_near(outputs.logits[0, :3] , _lowerCamelCase , atol=1e-4 )
344
1
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices __UpperCamelCase = logging.get_logger(__name__) __UpperCamelCase = { '''facebook/convnextv2-tiny-1k-224''': '''https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json''', } class UpperCamelCase ( lowerCAmelCase__ , lowerCAmelCase__ ): SCREAMING_SNAKE_CASE_ = "convnextv2" def __init__( self, lowerCAmelCase__=3, lowerCAmelCase__=4, lowerCAmelCase__=4, lowerCAmelCase__=None, lowerCAmelCase__=None, lowerCAmelCase__="gelu", lowerCAmelCase__=0.02, lowerCAmelCase__=1e-12, lowerCAmelCase__=0.0, lowerCAmelCase__=224, lowerCAmelCase__=None, lowerCAmelCase__=None, **lowerCAmelCase__, ) -> Any: super().__init__(**lowerCAmelCase__) snake_case_ = num_channels snake_case_ = patch_size snake_case_ = num_stages snake_case_ = [96, 192, 384, 768] if hidden_sizes is None else hidden_sizes snake_case_ = [3, 3, 9, 3] if depths is None else depths snake_case_ = hidden_act snake_case_ = initializer_range snake_case_ = layer_norm_eps snake_case_ = drop_path_rate snake_case_ = image_size snake_case_ = ['stem'] + [f'stage{idx}' for idx in range(1, len(self.depths) + 1)] snake_case_ , snake_case_ = get_aligned_output_features_output_indices( out_features=lowerCAmelCase__, out_indices=lowerCAmelCase__, stage_names=self.stage_names)
357
"""simple docstring""" import copy import re class UpperCamelCase : SCREAMING_SNAKE_CASE_ = "hp" SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = None @classmethod def a_ ( cls, lowerCAmelCase__, lowerCAmelCase__) -> Tuple: snake_case_ = prefix snake_case_ = defaults cls.build_naming_info() @staticmethod def a_ ( lowerCAmelCase__, lowerCAmelCase__) -> Optional[Any]: if len(lowerCAmelCase__) == 0: return "" snake_case_ = None if any(char.isdigit() for char in word): raise Exception(f'Parameters should not contain numbers: \'{word}\' contains a number') if word in info["short_word"]: return info["short_word"][word] for prefix_len in range(1, len(lowerCAmelCase__) + 1): snake_case_ = word[:prefix_len] if prefix in info["reverse_short_word"]: continue else: snake_case_ = prefix break if short_word is None: # Paranoid fallback def int_to_alphabetic(lowerCAmelCase__): snake_case_ = '' while integer != 0: snake_case_ = chr(ord('A') + integer % 10) + s integer //= 10 return s snake_case_ = 0 while True: snake_case_ = word + '#' + int_to_alphabetic(lowerCAmelCase__) if sword in info["reverse_short_word"]: continue else: snake_case_ = sword break snake_case_ = short_word snake_case_ = word return short_word @staticmethod def a_ ( lowerCAmelCase__, lowerCAmelCase__) -> Dict: snake_case_ = param_name.split('_') snake_case_ = [TrialShortNamer.shortname_for_word(lowerCAmelCase__, lowerCAmelCase__) for word in words] # We try to create a separatorless short name, but if there is a collision we have to fallback # to a separated short name snake_case_ = ['', '_'] for separator in separators: snake_case_ = separator.join(lowerCAmelCase__) if shortname not in info["reverse_short_param"]: snake_case_ = shortname snake_case_ = param_name return shortname return param_name @staticmethod def a_ ( lowerCAmelCase__, lowerCAmelCase__) -> List[Any]: snake_case_ = TrialShortNamer.shortname_for_key(lowerCAmelCase__, lowerCAmelCase__) snake_case_ = short_name snake_case_ = param_name @classmethod def a_ ( cls) -> List[str]: if cls.NAMING_INFO is not None: return snake_case_ = { 'short_word': {}, 'reverse_short_word': {}, 'short_param': {}, 'reverse_short_param': {}, } snake_case_ = list(cls.DEFAULTS.keys()) for k in field_keys: cls.add_new_param_name(lowerCAmelCase__, lowerCAmelCase__) snake_case_ = info @classmethod def a_ ( cls, lowerCAmelCase__) -> List[Any]: cls.build_naming_info() assert cls.PREFIX is not None snake_case_ = [copy.copy(cls.PREFIX)] for k, v in params.items(): if k not in cls.DEFAULTS: raise Exception(f'You should provide a default value for the param name {k} with value {v}') if v == cls.DEFAULTS[k]: # The default value is not added to the name continue snake_case_ = cls.NAMING_INFO['short_param'][k] if isinstance(lowerCAmelCase__, lowerCAmelCase__): snake_case_ = 1 if v else 0 snake_case_ = '' if isinstance(lowerCAmelCase__, (int, float)) else '-' snake_case_ = f'{key}{sep}{v}' name.append(lowerCAmelCase__) return "_".join(lowerCAmelCase__) @classmethod def a_ ( cls, lowerCAmelCase__) -> Optional[Any]: snake_case_ = repr[len(cls.PREFIX) + 1 :] if repr == "": snake_case_ = [] else: snake_case_ = repr.split('_') snake_case_ = {} for value in values: if "-" in value: snake_case_ , snake_case_ = value.split('-') else: snake_case_ = re.sub('[0-9.]', '', lowerCAmelCase__) snake_case_ = float(re.sub('[^0-9.]', '', lowerCAmelCase__)) snake_case_ = cls.NAMING_INFO['reverse_short_param'][p_k] snake_case_ = p_v for k in cls.DEFAULTS: if k not in parameters: snake_case_ = cls.DEFAULTS[k] return parameters
312
0
"""simple docstring""" A: Optional[int] = "\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" A: List[str] = [{"type": "code", "content": INSTALL_CONTENT}] A: Any = { "{processor_class}": "FakeProcessorClass", "{model_class}": "FakeModelClass", "{object_class}": "FakeObjectClass", }
109
'''simple docstring''' from __future__ import annotations import requests def _A ( snake_case ) -> dict: _lowercase : Dict = F'''https://hacker-news.firebaseio.com/v0/item/{story_id}.json?print=pretty''' return requests.get(snake_case ).json() def _A ( snake_case = 10 ) -> list[dict]: _lowercase : List[Any] = "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty" _lowercase : List[str] = requests.get(snake_case ).json()[:max_stories] return [get_hackernews_story(snake_case ) for story_id in story_ids] def _A ( snake_case = 10 ) -> str: _lowercase : Union[str, Any] = hackernews_top_stories(snake_case ) return "\n".join("* [{title}]({url})".format(**snake_case ) for story in stories ) if __name__ == "__main__": print(hackernews_top_stories_as_markdown())
250
0
"""simple docstring""" import gc import random import tempfile import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMInverseScheduler, DDIMScheduler, DPMSolverMultistepInverseScheduler, DPMSolverMultistepScheduler, StableDiffusionDiffEditPipeline, UNetaDConditionModel, ) from diffusers.utils import load_image, slow from diffusers.utils.testing_utils import enable_full_determinism, floats_tensor, require_torch_gpu, torch_device from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class snake_case ( _UpperCamelCase , _UpperCamelCase , unittest.TestCase): __UpperCamelCase = StableDiffusionDiffEditPipeline __UpperCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {'height', 'width', 'image'} | {'image_latents'} __UpperCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS - {'image'} | {'image_latents'} __UpperCamelCase = frozenset( []) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess __UpperCamelCase = frozenset([]) def a_ ( self : Optional[int] ) -> Dict: '''simple docstring''' torch.manual_seed(0 ) _A = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("DownBlock2D", "CrossAttnDownBlock2D") , up_block_types=("CrossAttnUpBlock2D", "UpBlock2D") , cross_attention_dim=32 , attention_head_dim=(2, 4) , use_linear_projection=a__ , ) _A = DDIMScheduler( beta_start=0.0_0_0_8_5 , beta_end=0.0_1_2 , beta_schedule="scaled_linear" , clip_sample=a__ , set_alpha_to_one=a__ , ) _A = DDIMInverseScheduler( beta_start=0.0_0_0_8_5 , beta_end=0.0_1_2 , beta_schedule="scaled_linear" , clip_sample=a__ , set_alpha_to_zero=a__ , ) torch.manual_seed(0 ) _A = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"] , up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"] , latent_channels=4 , sample_size=1_28 , ) torch.manual_seed(0 ) _A = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=10_00 , hidden_act="gelu" , projection_dim=5_12 , ) _A = CLIPTextModel(a__ ) _A = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) _A = { "unet": unet, "scheduler": scheduler, "inverse_scheduler": inverse_scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, "safety_checker": None, "feature_extractor": None, } return components def a_ ( self : Union[str, Any] , a__ : Optional[int] , a__ : int=0 ) -> Any: '''simple docstring''' _A = floats_tensor((1, 16, 16) , rng=random.Random(a__ ) ).to(a__ ) _A = floats_tensor((1, 2, 4, 16, 16) , rng=random.Random(a__ ) ).to(a__ ) if str(a__ ).startswith("mps" ): _A = torch.manual_seed(a__ ) else: _A = torch.Generator(device=a__ ).manual_seed(a__ ) _A = { "prompt": "a dog and a newt", "mask_image": mask, "image_latents": latents, "generator": generator, "num_inference_steps": 2, "inpaint_strength": 1.0, "guidance_scale": 6.0, "output_type": "numpy", } return inputs def a_ ( self : int , a__ : Optional[int] , a__ : int=0 ) -> Tuple: '''simple docstring''' _A = floats_tensor((1, 3, 32, 32) , rng=random.Random(a__ ) ).to(a__ ) _A = image.cpu().permute(0 , 2 , 3 , 1 )[0] _A = Image.fromarray(np.uinta(a__ ) ).convert("RGB" ) if str(a__ ).startswith("mps" ): _A = torch.manual_seed(a__ ) else: _A = torch.Generator(device=a__ ).manual_seed(a__ ) _A = { "image": image, "source_prompt": "a cat and a frog", "target_prompt": "a dog and a newt", "generator": generator, "num_inference_steps": 2, "num_maps_per_mask": 2, "mask_encode_strength": 1.0, "guidance_scale": 6.0, "output_type": "numpy", } return inputs def a_ ( self : Any , a__ : Optional[int] , a__ : Optional[int]=0 ) -> List[str]: '''simple docstring''' _A = floats_tensor((1, 3, 32, 32) , rng=random.Random(a__ ) ).to(a__ ) _A = image.cpu().permute(0 , 2 , 3 , 1 )[0] _A = Image.fromarray(np.uinta(a__ ) ).convert("RGB" ) if str(a__ ).startswith("mps" ): _A = torch.manual_seed(a__ ) else: _A = torch.Generator(device=a__ ).manual_seed(a__ ) _A = { "image": image, "prompt": "a cat and a frog", "generator": generator, "num_inference_steps": 2, "inpaint_strength": 1.0, "guidance_scale": 6.0, "decode_latents": True, "output_type": "numpy", } return inputs def a_ ( self : List[str] ) -> Dict: '''simple docstring''' if not hasattr(self.pipeline_class , "_optional_components" ): return _A = self.get_dummy_components() _A = self.pipeline_class(**a__ ) pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) # set all optional components to None and update pipeline config accordingly for optional_component in pipe._optional_components: setattr(a__ , a__ , a__ ) pipe.register_modules(**{optional_component: None for optional_component in pipe._optional_components} ) _A = self.get_dummy_inputs(a__ ) _A = pipe(**a__ )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(a__ ) _A = self.pipeline_class.from_pretrained(a__ ) pipe_loaded.to(a__ ) pipe_loaded.set_progress_bar_config(disable=a__ ) for optional_component in pipe._optional_components: self.assertTrue( getattr(a__ , a__ ) is None , F"""`{optional_component}` did not stay set to None after loading.""" , ) _A = self.get_dummy_inputs(a__ ) _A = pipe_loaded(**a__ )[0] _A = np.abs(output - output_loaded ).max() self.assertLess(a__ , 1E-4 ) def a_ ( self : Dict ) -> Optional[int]: '''simple docstring''' _A = "cpu" _A = self.get_dummy_components() _A = self.pipeline_class(**a__ ) pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _A = self.get_dummy_mask_inputs(a__ ) _A = pipe.generate_mask(**a__ ) _A = mask[0, -3:, -3:] self.assertEqual(mask.shape , (1, 16, 16) ) _A = np.array([0] * 9 ) _A = np.abs(mask_slice.flatten() - expected_slice ).max() self.assertLessEqual(a__ , 1E-3 ) self.assertEqual(mask[0, -3, -4] , 0 ) def a_ ( self : Any ) -> Dict: '''simple docstring''' _A = "cpu" _A = self.get_dummy_components() _A = self.pipeline_class(**a__ ) pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _A = self.get_dummy_inversion_inputs(a__ ) _A = pipe.invert(**a__ ).images _A = image[0, -1, -3:, -3:] self.assertEqual(image.shape , (2, 32, 32, 3) ) _A = np.array( [0.5_1_5_0, 0.5_1_3_4, 0.5_0_4_3, 0.5_3_7_6, 0.4_6_9_4, 0.5_1_0_5_0, 0.5_0_1_5, 0.4_4_0_7, 0.4_7_9_9] , ) _A = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(a__ , 1E-3 ) def a_ ( self : Dict ) -> Union[str, Any]: '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=5E-3 ) def a_ ( self : int ) -> List[str]: '''simple docstring''' _A = "cpu" _A = self.get_dummy_components() _A = {"beta_start": 0.0_0_0_8_5, "beta_end": 0.0_1_2, "beta_schedule": "scaled_linear"} _A = DPMSolverMultistepScheduler(**a__ ) _A = DPMSolverMultistepInverseScheduler(**a__ ) _A = self.pipeline_class(**a__ ) pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _A = self.get_dummy_inversion_inputs(a__ ) _A = pipe.invert(**a__ ).images _A = image[0, -1, -3:, -3:] self.assertEqual(image.shape , (2, 32, 32, 3) ) _A = np.array( [0.5_1_5_0, 0.5_1_3_4, 0.5_0_4_3, 0.5_3_7_6, 0.4_6_9_4, 0.5_1_0_5_0, 0.5_0_1_5, 0.4_4_0_7, 0.4_7_9_9] , ) _A = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(a__ , 1E-3 ) @require_torch_gpu @slow class snake_case ( unittest.TestCase): def a_ ( self : int ) -> Dict: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() @classmethod def a_ ( cls : Optional[int] ) -> Optional[int]: '''simple docstring''' _A = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/diffedit/fruit.png" ) _A = raw_image.convert("RGB" ).resize((7_68, 7_68) ) _A = raw_image def a_ ( self : int ) -> Dict: '''simple docstring''' _A = torch.manual_seed(0 ) _A = StableDiffusionDiffEditPipeline.from_pretrained( "stabilityai/stable-diffusion-2-1" , safety_checker=a__ , torch_dtype=torch.floataa ) _A = DDIMScheduler.from_config(pipe.scheduler.config ) _A = DDIMInverseScheduler.from_config(pipe.scheduler.config ) pipe.enable_model_cpu_offload() pipe.set_progress_bar_config(disable=a__ ) _A = "a bowl of fruit" _A = "a bowl of pears" _A = pipe.generate_mask( image=self.raw_image , source_prompt=a__ , target_prompt=a__ , generator=a__ , ) _A = pipe.invert( prompt=a__ , image=self.raw_image , inpaint_strength=0.7 , generator=a__ ).latents _A = pipe( prompt=a__ , mask_image=a__ , image_latents=a__ , generator=a__ , negative_prompt=a__ , inpaint_strength=0.7 , output_type="numpy" , ).images[0] _A = ( np.array( load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/diffedit/pears.png" ).resize((7_68, 7_68) ) ) / 2_55 ) assert np.abs((expected_image - image).max() ) < 5E-1 def a_ ( self : Any ) -> Any: '''simple docstring''' _A = torch.manual_seed(0 ) _A = StableDiffusionDiffEditPipeline.from_pretrained( "stabilityai/stable-diffusion-2-1" , safety_checker=a__ , torch_dtype=torch.floataa ) _A = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) _A = DPMSolverMultistepInverseScheduler.from_config(pipe.scheduler.config ) pipe.enable_model_cpu_offload() pipe.set_progress_bar_config(disable=a__ ) _A = "a bowl of fruit" _A = "a bowl of pears" _A = pipe.generate_mask( image=self.raw_image , source_prompt=a__ , target_prompt=a__ , generator=a__ , ) _A = pipe.invert( prompt=a__ , image=self.raw_image , inpaint_strength=0.7 , generator=a__ , num_inference_steps=25 , ).latents _A = pipe( prompt=a__ , mask_image=a__ , image_latents=a__ , generator=a__ , negative_prompt=a__ , inpaint_strength=0.7 , num_inference_steps=25 , output_type="numpy" , ).images[0] _A = ( np.array( load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/diffedit/pears.png" ).resize((7_68, 7_68) ) ) / 2_55 ) assert np.abs((expected_image - image).max() ) < 5E-1
362
"""simple docstring""" import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, EulerAncestralDiscreteScheduler, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionInstructPixaPixPipeline, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.utils import floats_tensor, load_image, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( IMAGE_TO_IMAGE_IMAGE_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class snake_case ( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , unittest.TestCase): __UpperCamelCase = StableDiffusionInstructPixaPixPipeline __UpperCamelCase = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'height', 'width', 'cross_attention_kwargs'} __UpperCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __UpperCamelCase = IMAGE_TO_IMAGE_IMAGE_PARAMS __UpperCamelCase = IMAGE_TO_IMAGE_IMAGE_PARAMS def a_ ( self : Optional[int] ) -> str: '''simple docstring''' torch.manual_seed(0 ) _A = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=8 , out_channels=4 , down_block_types=("DownBlock2D", "CrossAttnDownBlock2D") , up_block_types=("CrossAttnUpBlock2D", "UpBlock2D") , cross_attention_dim=32 , ) _A = PNDMScheduler(skip_prk_steps=a__ ) torch.manual_seed(0 ) _A = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"] , up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"] , latent_channels=4 , ) torch.manual_seed(0 ) _A = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=10_00 , ) _A = CLIPTextModel(a__ ) _A = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) _A = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, "safety_checker": None, "feature_extractor": None, } return components def a_ ( self : Optional[Any] , a__ : Dict , a__ : Tuple=0 ) -> Union[str, Any]: '''simple docstring''' _A = floats_tensor((1, 3, 32, 32) , rng=random.Random(a__ ) ).to(a__ ) _A = image.cpu().permute(0 , 2 , 3 , 1 )[0] _A = Image.fromarray(np.uinta(a__ ) ).convert("RGB" ) if str(a__ ).startswith("mps" ): _A = torch.manual_seed(a__ ) else: _A = torch.Generator(device=a__ ).manual_seed(a__ ) _A = { "prompt": "A painting of a squirrel eating a burger", "image": image, "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, "image_guidance_scale": 1, "output_type": "numpy", } return inputs def a_ ( self : Dict ) -> str: '''simple docstring''' _A = "cpu" # ensure determinism for the device-dependent torch.Generator _A = self.get_dummy_components() _A = StableDiffusionInstructPixaPixPipeline(**a__ ) _A = sd_pipe.to(a__ ) sd_pipe.set_progress_bar_config(disable=a__ ) _A = self.get_dummy_inputs(a__ ) _A = sd_pipe(**a__ ).images _A = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) _A = np.array([0.7_5_2_6, 0.3_7_5_0, 0.4_5_4_7, 0.6_1_1_7, 0.5_8_6_6, 0.5_0_1_6, 0.4_3_2_7, 0.5_6_4_2, 0.4_8_1_5] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def a_ ( self : str ) -> Optional[int]: '''simple docstring''' _A = "cpu" # ensure determinism for the device-dependent torch.Generator _A = self.get_dummy_components() _A = StableDiffusionInstructPixaPixPipeline(**a__ ) _A = sd_pipe.to(a__ ) sd_pipe.set_progress_bar_config(disable=a__ ) _A = self.get_dummy_inputs(a__ ) _A = "french fries" _A = sd_pipe(**a__ , negative_prompt=a__ ) _A = output.images _A = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) _A = np.array([0.7_5_1_1, 0.3_6_4_2, 0.4_5_5_3, 0.6_2_3_6, 0.5_7_9_7, 0.5_0_1_3, 0.4_3_4_3, 0.5_6_1_1, 0.4_8_3_1] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def a_ ( self : Optional[int] ) -> int: '''simple docstring''' _A = "cpu" # ensure determinism for the device-dependent torch.Generator _A = self.get_dummy_components() _A = StableDiffusionInstructPixaPixPipeline(**a__ ) _A = sd_pipe.to(a__ ) sd_pipe.set_progress_bar_config(disable=a__ ) _A = self.get_dummy_inputs(a__ ) _A = [inputs["prompt"]] * 2 _A = np.array(inputs["image"] ).astype(np.floataa ) / 2_5_5.0 _A = torch.from_numpy(a__ ).unsqueeze(0 ).to(a__ ) _A = image / 2 + 0.5 _A = image.permute(0 , 3 , 1 , 2 ) _A = image.repeat(2 , 1 , 1 , 1 ) _A = sd_pipe(**a__ ).images _A = image[-1, -3:, -3:, -1] assert image.shape == (2, 32, 32, 3) _A = np.array([0.5_8_1_2, 0.5_7_4_8, 0.5_2_2_2, 0.5_9_0_8, 0.5_6_9_5, 0.7_1_7_4, 0.6_8_0_4, 0.5_5_2_3, 0.5_5_7_9] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def a_ ( self : List[Any] ) -> Optional[int]: '''simple docstring''' _A = "cpu" # ensure determinism for the device-dependent torch.Generator _A = self.get_dummy_components() _A = EulerAncestralDiscreteScheduler( beta_start=0.0_0_0_8_5 , beta_end=0.0_1_2 , beta_schedule="scaled_linear" ) _A = StableDiffusionInstructPixaPixPipeline(**a__ ) _A = sd_pipe.to(a__ ) sd_pipe.set_progress_bar_config(disable=a__ ) _A = self.get_dummy_inputs(a__ ) _A = sd_pipe(**a__ ).images _A = image[0, -3:, -3:, -1] _A = [round(a__ , 4 ) for x in image_slice.flatten().tolist()] print(",".join([str(a__ ) for x in slice] ) ) assert image.shape == (1, 32, 32, 3) _A = np.array([0.7_4_1_7, 0.3_8_4_2, 0.4_7_3_2, 0.5_7_7_6, 0.5_8_9_1, 0.5_1_3_9, 0.4_0_5_2, 0.5_6_7_3, 0.4_9_8_6] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def a_ ( self : List[str] ) -> int: '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) def a_ ( self : str ) -> Any: '''simple docstring''' _A = self.get_dummy_components() _A = StableDiffusionInstructPixaPixPipeline(**a__ ) _A = VaeImageProcessor(do_resize=a__ , do_normalize=a__ ) _A = pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _A = pipe(**self.get_dummy_inputs_by_type(a__ , input_image_type="pt" ) )[0] _A = components["vae"] _A = self.get_dummy_inputs_by_type(a__ , input_image_type="pt" ) for image_param in self.image_latents_params: if image_param in inputs.keys(): _A = vae.encode(inputs[image_param] ).latent_dist.mode() _A = pipe(**a__ )[0] _A = np.abs(out - out_latents_inputs ).max() self.assertLess(a__ , 1E-4 , "passing latents as image input generate different result from passing image" ) @slow @require_torch_gpu class snake_case ( unittest.TestCase): def a_ ( self : List[str] ) -> List[str]: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def a_ ( self : Optional[Any] , a__ : str=0 ) -> List[Any]: '''simple docstring''' _A = torch.manual_seed(a__ ) _A = load_image( "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_pix2pix/example.jpg" ) _A = { "prompt": "turn him into a cyborg", "image": image, "generator": generator, "num_inference_steps": 3, "guidance_scale": 7.5, "image_guidance_scale": 1.0, "output_type": "numpy", } return inputs def a_ ( self : List[Any] ) -> Any: '''simple docstring''' _A = StableDiffusionInstructPixaPixPipeline.from_pretrained( "timbrooks/instruct-pix2pix" , safety_checker=a__ ) pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) pipe.enable_attention_slicing() _A = self.get_inputs() _A = pipe(**a__ ).images _A = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 5_12, 5_12, 3) _A = np.array([0.5_9_0_2, 0.6_0_1_5, 0.6_0_2_7, 0.5_9_8_3, 0.6_0_9_2, 0.6_0_6_1, 0.5_7_6_5, 0.5_7_8_5, 0.5_5_5_5] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def a_ ( self : List[Any] ) -> Any: '''simple docstring''' _A = StableDiffusionInstructPixaPixPipeline.from_pretrained( "timbrooks/instruct-pix2pix" , safety_checker=a__ ) _A = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) pipe.enable_attention_slicing() _A = self.get_inputs() _A = pipe(**a__ ).images _A = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 5_12, 5_12, 3) _A = np.array([0.6_5_7_8, 0.6_8_1_7, 0.6_9_7_2, 0.6_7_6_1, 0.6_8_5_6, 0.6_9_1_6, 0.6_4_2_8, 0.6_5_1_6, 0.6_3_0_1] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def a_ ( self : Union[str, Any] ) -> int: '''simple docstring''' _A = StableDiffusionInstructPixaPixPipeline.from_pretrained( "timbrooks/instruct-pix2pix" , safety_checker=a__ ) _A = DDIMScheduler.from_config(pipe.scheduler.config ) pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) pipe.enable_attention_slicing() _A = self.get_inputs() _A = pipe(**a__ ).images _A = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 5_12, 5_12, 3) _A = np.array([0.3_8_2_8, 0.3_8_3_4, 0.3_8_1_8, 0.3_7_9_2, 0.3_8_6_5, 0.3_7_5_2, 0.3_7_9_2, 0.3_8_4_7, 0.3_7_5_3] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def a_ ( self : Optional[int] ) -> Dict: '''simple docstring''' _A = 0 def callback_fn(a__ : int , a__ : int , a__ : torch.FloatTensor ) -> None: _A = True nonlocal number_of_steps number_of_steps += 1 if step == 1: _A = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 64) _A = latents[0, -3:, -3:, -1] _A = np.array([-0.2_4_6_3, -0.4_6_4_4, -0.9_7_5_6, 1.5_1_7_6, 1.4_4_1_4, 0.7_8_6_6, 0.9_8_9_7, 0.8_5_2_1, 0.7_9_8_3] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2 elif step == 2: _A = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 64) _A = latents[0, -3:, -3:, -1] _A = np.array([-0.2_6_4_4, -0.4_6_2_6, -0.9_6_5_3, 1.5_1_7_6, 1.4_5_5_1, 0.7_6_8_6, 0.9_8_0_5, 0.8_4_5_2, 0.8_1_1_5] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2 _A = False _A = StableDiffusionInstructPixaPixPipeline.from_pretrained( "timbrooks/instruct-pix2pix" , safety_checker=a__ , torch_dtype=torch.floataa ) _A = pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) pipe.enable_attention_slicing() _A = self.get_inputs() pipe(**a__ , callback=a__ , callback_steps=1 ) assert callback_fn.has_been_called assert number_of_steps == 3 def a_ ( self : List[Any] ) -> Any: '''simple docstring''' torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() _A = StableDiffusionInstructPixaPixPipeline.from_pretrained( "timbrooks/instruct-pix2pix" , safety_checker=a__ , torch_dtype=torch.floataa ) _A = pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() _A = self.get_inputs() _A = pipe(**a__ ) _A = torch.cuda.max_memory_allocated() # make sure that less than 2.2 GB is allocated assert mem_bytes < 2.2 * 10**9 def a_ ( self : List[Any] ) -> List[Any]: '''simple docstring''' _A = self.get_inputs() # resize to resolution that is divisible by 8 but not 16 or 32 _A = inputs["image"].resize((5_04, 5_04) ) _A = "timbrooks/instruct-pix2pix" _A = StableDiffusionInstructPixaPixPipeline.from_pretrained( a__ , safety_checker=a__ , ) pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) pipe.enable_attention_slicing() _A = pipe(**a__ ) _A = output.images[0] _A = image[2_55:2_58, 3_83:3_86, -1] assert image.shape == (5_04, 5_04, 3) _A = np.array([0.2_7_2_6, 0.2_5_2_9, 0.2_6_6_4, 0.2_6_5_5, 0.2_6_4_1, 0.2_6_4_2, 0.2_5_9_1, 0.2_6_4_9, 0.2_5_9_0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-3
163
0
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_video_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import VivitImageProcessor class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __init__( self : Optional[int] , __magic_name__ : Optional[Any] , __magic_name__ : int=7 , __magic_name__ : List[Any]=3 , __magic_name__ : Dict=10 , __magic_name__ : int=18 , __magic_name__ : List[str]=30 , __magic_name__ : Union[str, Any]=400 , __magic_name__ : List[str]=True , __magic_name__ : Optional[int]=None , __magic_name__ : int=True , __magic_name__ : Union[str, Any]=[0.5, 0.5, 0.5] , __magic_name__ : List[Any]=[0.5, 0.5, 0.5] , __magic_name__ : List[Any]=None , ) -> Tuple: SCREAMING_SNAKE_CASE_ = size if size is not None else {"shortest_edge": 18} SCREAMING_SNAKE_CASE_ = crop_size if crop_size is not None else {"height": 18, "width": 18} SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = num_frames SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = min_resolution SCREAMING_SNAKE_CASE_ = max_resolution SCREAMING_SNAKE_CASE_ = do_resize SCREAMING_SNAKE_CASE_ = size SCREAMING_SNAKE_CASE_ = do_normalize SCREAMING_SNAKE_CASE_ = image_mean SCREAMING_SNAKE_CASE_ = image_std SCREAMING_SNAKE_CASE_ = crop_size def __A ( self : Optional[Any] ) -> int: return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, "crop_size": self.crop_size, } @require_torch @require_vision class lowerCamelCase (SCREAMING_SNAKE_CASE__ , unittest.TestCase ): """simple docstring""" lowerCamelCase__ = VivitImageProcessor if is_vision_available() else None def __A ( self : Optional[int] ) -> Dict: SCREAMING_SNAKE_CASE_ = VivitImageProcessingTester(self ) @property def __A ( self : Optional[int] ) -> str: return self.image_processor_tester.prepare_image_processor_dict() def __A ( self : Optional[Any] ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__magic_name__ , "image_mean" ) ) self.assertTrue(hasattr(__magic_name__ , "image_std" ) ) self.assertTrue(hasattr(__magic_name__ , "do_normalize" ) ) self.assertTrue(hasattr(__magic_name__ , "do_resize" ) ) self.assertTrue(hasattr(__magic_name__ , "do_center_crop" ) ) self.assertTrue(hasattr(__magic_name__ , "size" ) ) def __A ( self : Union[str, Any] ) -> Tuple: SCREAMING_SNAKE_CASE_ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"shortest_edge": 18} ) self.assertEqual(image_processor.crop_size , {"height": 18, "width": 18} ) SCREAMING_SNAKE_CASE_ = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {"shortest_edge": 42} ) self.assertEqual(image_processor.crop_size , {"height": 84, "width": 84} ) def __A ( self : Tuple ) -> List[str]: # Initialize image_processing SCREAMING_SNAKE_CASE_ = self.image_processing_class(**self.image_processor_dict ) # create random PIL videos SCREAMING_SNAKE_CASE_ = prepare_video_inputs(self.image_processor_tester , equal_resolution=__magic_name__ ) for video in video_inputs: self.assertIsInstance(__magic_name__ , __magic_name__ ) self.assertIsInstance(video[0] , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE_ = image_processing(video_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE_ = image_processing(__magic_name__ , return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def __A ( self : Any ) -> Any: # Initialize image_processing SCREAMING_SNAKE_CASE_ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE_ = prepare_video_inputs(self.image_processor_tester , equal_resolution=__magic_name__ , numpify=__magic_name__ ) for video in video_inputs: self.assertIsInstance(__magic_name__ , __magic_name__ ) self.assertIsInstance(video[0] , np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE_ = image_processing(video_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE_ = image_processing(__magic_name__ , return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def __A ( self : Any ) -> Dict: # Initialize image_processing SCREAMING_SNAKE_CASE_ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE_ = prepare_video_inputs(self.image_processor_tester , equal_resolution=__magic_name__ , torchify=__magic_name__ ) for video in video_inputs: self.assertIsInstance(__magic_name__ , __magic_name__ ) self.assertIsInstance(video[0] , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE_ = image_processing(video_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE_ = image_processing(__magic_name__ , return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , )
118
import argparse import os from transformers.utils import direct_transformers_import # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_task_guides.py A : Tuple = "src/transformers" A : Optional[Any] = "docs/source/en/tasks" def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): with open(__UpperCamelCase , "r" , encoding="utf-8" , newline="\n" ) as f: SCREAMING_SNAKE_CASE_ = f.readlines() # Find the start prompt. SCREAMING_SNAKE_CASE_ = 0 while not lines[start_index].startswith(__UpperCamelCase ): start_index += 1 start_index += 1 SCREAMING_SNAKE_CASE_ = start_index while not lines[end_index].startswith(__UpperCamelCase ): end_index += 1 end_index -= 1 while len(lines[start_index] ) <= 1: start_index += 1 while len(lines[end_index] ) <= 1: end_index -= 1 end_index += 1 return "".join(lines[start_index:end_index] ), start_index, end_index, lines # This is to make sure the transformers module imported is the one in the repo. A : List[str] = direct_transformers_import(TRANSFORMERS_PATH) A : List[Any] = { "asr.md": transformers_module.models.auto.modeling_auto.MODEL_FOR_CTC_MAPPING_NAMES, "audio_classification.md": transformers_module.models.auto.modeling_auto.MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES, "language_modeling.md": transformers_module.models.auto.modeling_auto.MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, "image_classification.md": transformers_module.models.auto.modeling_auto.MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES, "masked_language_modeling.md": transformers_module.models.auto.modeling_auto.MODEL_FOR_MASKED_LM_MAPPING_NAMES, "multiple_choice.md": transformers_module.models.auto.modeling_auto.MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES, "object_detection.md": transformers_module.models.auto.modeling_auto.MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES, "question_answering.md": transformers_module.models.auto.modeling_auto.MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES, "semantic_segmentation.md": transformers_module.models.auto.modeling_auto.MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES, "sequence_classification.md": transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES, "summarization.md": transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, "token_classification.md": transformers_module.models.auto.modeling_auto.MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES, "translation.md": transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, "video_classification.md": transformers_module.models.auto.modeling_auto.MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING_NAMES, "document_question_answering.md": transformers_module.models.auto.modeling_auto.MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES, "monocular_depth_estimation.md": transformers_module.models.auto.modeling_auto.MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES, } # This list contains model types used in some task guides that are not in `CONFIG_MAPPING_NAMES` (therefore not in any # `MODEL_MAPPING_NAMES` or any `MODEL_FOR_XXX_MAPPING_NAMES`). A : Any = { "summarization.md": ("nllb",), "translation.md": ("nllb",), } def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = TASK_GUIDE_TO_MODELS[task_guide] SCREAMING_SNAKE_CASE_ = SPECIAL_TASK_GUIDE_TO_MODEL_TYPES.get(__UpperCamelCase , set() ) SCREAMING_SNAKE_CASE_ = { code: name for code, name in transformers_module.MODEL_NAMES_MAPPING.items() if (code in model_maping_names or code in special_model_types) } return ", ".join([F'''[{name}](../model_doc/{code})''' for code, name in model_names.items()] ) + "\n" def a__ ( __UpperCamelCase , __UpperCamelCase=False ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = _find_text_in_file( filename=os.path.join(__UpperCamelCase , __UpperCamelCase ) , start_prompt="<!--This tip is automatically generated by `make fix-copies`, do not fill manually!-->" , end_prompt="<!--End of the generated tip-->" , ) SCREAMING_SNAKE_CASE_ = get_model_list_for_task(__UpperCamelCase ) if current_list != new_list: if overwrite: with open(os.path.join(__UpperCamelCase , __UpperCamelCase ) , "w" , encoding="utf-8" , newline="\n" ) as f: f.writelines(lines[:start_index] + [new_list] + lines[end_index:] ) else: raise ValueError( F'''The list of models that can be used in the {task_guide} guide needs an update. Run `make fix-copies`''' " to fix this." ) if __name__ == "__main__": A : Tuple = argparse.ArgumentParser() parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.") A : Dict = parser.parse_args() for task_guide in TASK_GUIDE_TO_MODELS.keys(): check_model_list_for_task(task_guide, args.fix_and_overwrite)
118
1
'''simple docstring''' from string import ascii_uppercase __UpperCAmelCase = {char: i for i, char in enumerate(ascii_uppercase)} __UpperCAmelCase = dict(enumerate(ascii_uppercase)) def _snake_case ( A , A ) -> str: lowerCAmelCase__ = len(A ) lowerCAmelCase__ = 0 while True: if x == i: lowerCAmelCase__ = 0 if len(A ) == len(A ): break key += key[i] i += 1 return key def _snake_case ( A , A ) -> str: lowerCAmelCase__ = '''''' lowerCAmelCase__ = 0 for letter in message: if letter == " ": cipher_text += " " else: lowerCAmelCase__ = (dicta[letter] - dicta[key_new[i]]) % 26 i += 1 cipher_text += dicta[x] return cipher_text def _snake_case ( A , A ) -> str: lowerCAmelCase__ = '''''' lowerCAmelCase__ = 0 for letter in cipher_text: if letter == " ": or_txt += " " else: lowerCAmelCase__ = (dicta[letter] + dicta[key_new[i]] + 26) % 26 i += 1 or_txt += dicta[x] return or_txt def _snake_case ( ) -> None: lowerCAmelCase__ = '''THE GERMAN ATTACK''' lowerCAmelCase__ = '''SECRET''' lowerCAmelCase__ = generate_key(A , A ) lowerCAmelCase__ = cipher_text(A , A ) print(F"""Encrypted Text = {s}""" ) print(F"""Original Text = {original_text(A , A )}""" ) if __name__ == "__main__": import doctest doctest.testmod() main()
354
'''simple docstring''' import argparse import json import os from pathlib import Path import requests import torch from transformers import JukeboxConfig, JukeboxModel from transformers.utils import logging logging.set_verbosity_info() __UpperCAmelCase = logging.get_logger(__name__) __UpperCAmelCase = '''https://openaipublic.azureedge.net/jukebox/models/''' __UpperCAmelCase = { '''jukebox-1b-lyrics''': [ '''5b/vqvae.pth.tar''', '''5b/prior_level_0.pth.tar''', '''5b/prior_level_1.pth.tar''', '''1b_lyrics/prior_level_2.pth.tar''', ], '''jukebox-5b-lyrics''': [ '''5b/vqvae.pth.tar''', '''5b/prior_level_0.pth.tar''', '''5b/prior_level_1.pth.tar''', '''5b_lyrics/prior_level_2.pth.tar''', ], } def _snake_case ( A ) -> Union[str, Any]: if key.endswith('''.model.1.bias''' ) and len(key.split('''.''' ) ) > 10: lowerCAmelCase__ = key.replace('''.model.1.bias''' , '''.conv1d_1.bias''' ) elif key.endswith('''.model.1.weight''' ) and len(key.split('''.''' ) ) > 10: lowerCAmelCase__ = key.replace('''.model.1.weight''' , '''.conv1d_1.weight''' ) elif key.endswith('''.model.3.bias''' ) and len(key.split('''.''' ) ) > 10: lowerCAmelCase__ = key.replace('''.model.3.bias''' , '''.conv1d_2.bias''' ) elif key.endswith('''.model.3.weight''' ) and len(key.split('''.''' ) ) > 10: lowerCAmelCase__ = key.replace('''.model.3.weight''' , '''.conv1d_2.weight''' ) if "conditioner_blocks.0." in key: lowerCAmelCase__ = key.replace('''conditioner_blocks.0''' , '''conditioner_blocks''' ) if "prime_prior" in key: lowerCAmelCase__ = key.replace('''prime_prior''' , '''encoder''' ) if ".emb." in key and "total" not in key and "absolute" not in key and "relative" not in key: lowerCAmelCase__ = key.replace('''.emb.''' , '''.''' ) if key.endswith('''k''' ): # replace vqvae.X.k with vqvae.X.codebook return key.replace('''.k''' , '''.codebook''' ) if "y_emb." in key: return key.replace('''y_emb.''' , '''metadata_embedding.''' ) if "x_emb.emb." in key: lowerCAmelCase__ = key.replace('''0.x_emb.emb''' , '''embed_tokens''' ) if "prime_state_ln" in key: return key.replace('''prime_state_ln''' , '''encoder.final_layer_norm''' ) if ".ln" in key: return key.replace('''.ln''' , '''.layer_norm''' ) if "_ln" in key: return key.replace('''_ln''' , '''_layer_norm''' ) if "prime_state_proj" in key: return key.replace('''prime_state_proj''' , '''encoder.proj_in''' ) if "prime_x_out" in key: return key.replace('''prime_x_out''' , '''encoder.lm_head''' ) if "prior.x_out" in key: return key.replace('''x_out''' , '''fc_proj_out''' ) if "x_emb" in key: return key.replace('''x_emb''' , '''embed_tokens''' ) return key def _snake_case ( A , A , A , A ) -> Optional[int]: lowerCAmelCase__ = {} import re lowerCAmelCase__ = re.compile(R'''encoders.(\d*).level_blocks.(\d*).model.(\d*).(\d).(bias|weight)''' ) lowerCAmelCase__ = re.compile( R'''encoders.(\d*).level_blocks.(\d*).model.(\d*).(\d).model.(\d*).model.(\d*).(bias|weight)''' ) lowerCAmelCase__ = re.compile(R'''encoders.(\d*).level_blocks.(\d*).model.(\d*).(bias|weight)''' ) lowerCAmelCase__ = re.compile(R'''decoders.(\d*).level_blocks.(\d*).model.(\d*).(\d).(bias|weight)''' ) lowerCAmelCase__ = re.compile( R'''decoders.(\d*).level_blocks.(\d*).model.(\d*).(\d).model.(\d*).model.(\d*).(bias|weight)''' ) lowerCAmelCase__ = re.compile(R'''decoders.(\d*).level_blocks.(\d*).model.(\d*).(bias|weight)''' ) lowerCAmelCase__ = re.compile(R'''conditioner_blocks.(\d*).cond.model.(\d*).(\d).(bias|weight)''' ) lowerCAmelCase__ = re.compile( R'''conditioner_blocks.(\d*).cond.model.(\d*).(\d).model.(\d*).model.(\d*).(bias|weight)''' ) lowerCAmelCase__ = re.compile(R'''conditioner_blocks.(\d*).cond.model.(\d*).(bias|weight)''' ) for original_key, value in state_dict.items(): # rename vqvae.encoder keys if re_encoder_block_conv_in.fullmatch(A ): lowerCAmelCase__ = re_encoder_block_conv_in.match(A ) lowerCAmelCase__ = regex_match.groups() lowerCAmelCase__ = int(groups[2] ) * 2 + int(groups[3] ) lowerCAmelCase__ = F"""encoders.{groups[0]}.level_blocks.{groups[1]}.downsample_block.{block_index}.{groups[-1]}""" lowerCAmelCase__ = re_encoder_block_conv_in.sub(A , A ) elif re_encoder_block_resnet.fullmatch(A ): lowerCAmelCase__ = re_encoder_block_resnet.match(A ) lowerCAmelCase__ = regex_match.groups() lowerCAmelCase__ = int(groups[2] ) * 2 + int(groups[3] ) lowerCAmelCase__ = {'''1''': 1, '''3''': 2}[groups[-2]] lowerCAmelCase__ = F"""encoders.{groups[0]}.level_blocks.{groups[1]}.downsample_block.{block_index}.""" lowerCAmelCase__ = F"""resnet_block.{groups[-3]}.conv1d_{conv_index}.{groups[-1]}""" lowerCAmelCase__ = prefix + resnet_block lowerCAmelCase__ = re_encoder_block_resnet.sub(A , A ) elif re_encoder_block_proj_out.fullmatch(A ): lowerCAmelCase__ = re_encoder_block_proj_out.match(A ) lowerCAmelCase__ = regex_match.groups() lowerCAmelCase__ = F"""encoders.{groups[0]}.level_blocks.{groups[1]}.proj_out.{groups[-1]}""" lowerCAmelCase__ = re_encoder_block_proj_out.sub(A , A ) # rename vqvae.decoder keys elif re_decoder_block_conv_out.fullmatch(A ): lowerCAmelCase__ = re_decoder_block_conv_out.match(A ) lowerCAmelCase__ = regex_match.groups() lowerCAmelCase__ = int(groups[2] ) * 2 + int(groups[3] ) - 2 lowerCAmelCase__ = F"""decoders.{groups[0]}.level_blocks.{groups[1]}.upsample_block.{block_index}.{groups[-1]}""" lowerCAmelCase__ = re_decoder_block_conv_out.sub(A , A ) elif re_decoder_block_resnet.fullmatch(A ): lowerCAmelCase__ = re_decoder_block_resnet.match(A ) lowerCAmelCase__ = regex_match.groups() lowerCAmelCase__ = int(groups[2] ) * 2 + int(groups[3] ) - 2 lowerCAmelCase__ = {'''1''': 1, '''3''': 2}[groups[-2]] lowerCAmelCase__ = F"""decoders.{groups[0]}.level_blocks.{groups[1]}.upsample_block.{block_index}.""" lowerCAmelCase__ = F"""resnet_block.{groups[-3]}.conv1d_{conv_index}.{groups[-1]}""" lowerCAmelCase__ = prefix + resnet_block lowerCAmelCase__ = re_decoder_block_resnet.sub(A , A ) elif re_decoder_block_proj_in.fullmatch(A ): lowerCAmelCase__ = re_decoder_block_proj_in.match(A ) lowerCAmelCase__ = regex_match.groups() lowerCAmelCase__ = F"""decoders.{groups[0]}.level_blocks.{groups[1]}.proj_in.{groups[-1]}""" lowerCAmelCase__ = re_decoder_block_proj_in.sub(A , A ) # rename prior cond.model to upsampler.upsample_block and resnet elif re_prior_cond_conv_out.fullmatch(A ): lowerCAmelCase__ = re_prior_cond_conv_out.match(A ) lowerCAmelCase__ = regex_match.groups() lowerCAmelCase__ = int(groups[1] ) * 2 + int(groups[2] ) - 2 lowerCAmelCase__ = F"""conditioner_blocks.upsampler.upsample_block.{block_index}.{groups[-1]}""" lowerCAmelCase__ = re_prior_cond_conv_out.sub(A , A ) elif re_prior_cond_resnet.fullmatch(A ): lowerCAmelCase__ = re_prior_cond_resnet.match(A ) lowerCAmelCase__ = regex_match.groups() lowerCAmelCase__ = int(groups[1] ) * 2 + int(groups[2] ) - 2 lowerCAmelCase__ = {'''1''': 1, '''3''': 2}[groups[-2]] lowerCAmelCase__ = F"""conditioner_blocks.upsampler.upsample_block.{block_index}.""" lowerCAmelCase__ = F"""resnet_block.{groups[-3]}.conv1d_{conv_index}.{groups[-1]}""" lowerCAmelCase__ = prefix + resnet_block lowerCAmelCase__ = re_prior_cond_resnet.sub(A , A ) elif re_prior_cond_proj_in.fullmatch(A ): lowerCAmelCase__ = re_prior_cond_proj_in.match(A ) lowerCAmelCase__ = regex_match.groups() lowerCAmelCase__ = F"""conditioner_blocks.upsampler.proj_in.{groups[-1]}""" lowerCAmelCase__ = re_prior_cond_proj_in.sub(A , A ) # keep original key else: lowerCAmelCase__ = original_key lowerCAmelCase__ = replace_key(A ) if F"""{key_prefix}.{key}""" not in model_state_dict or key is None: print(F"""failed converting {original_key} to {key}, does not match""" ) # handle missmatched shape elif value.shape != model_state_dict[F"""{key_prefix}.{key}"""].shape: lowerCAmelCase__ = model_state_dict[F"""{key_prefix}.{key}"""] print(F"""{original_key}-> {key} : \nshape {val.shape} and { value.shape}, do not match""" ) lowerCAmelCase__ = original_key lowerCAmelCase__ = original_key lowerCAmelCase__ = value return new_dict @torch.no_grad() def _snake_case ( A=None , A=None ) -> str: for file in MODEL_MAPPING[model_name]: if not os.path.isfile(F"""{pytorch_dump_folder_path}/{file.split("/" )[-1]}""" ): lowerCAmelCase__ = requests.get(F"""{PREFIX}{file}""" , allow_redirects=A ) os.makedirs(F"""{pytorch_dump_folder_path}/""" , exist_ok=A ) open(F"""{pytorch_dump_folder_path}/{file.split("/" )[-1]}""" , '''wb''' ).write(r.content ) lowerCAmelCase__ = MODEL_MAPPING[model_name.split('''/''' )[-1]] lowerCAmelCase__ = JukeboxConfig.from_pretrained(A ) lowerCAmelCase__ = JukeboxModel(A ) lowerCAmelCase__ = [] lowerCAmelCase__ = {} for i, dict_name in enumerate(A ): lowerCAmelCase__ = torch.load(F"""{pytorch_dump_folder_path}/{dict_name.split("/" )[-1]}""" )['''model'''] lowerCAmelCase__ = {} for k in old_dic.keys(): if k.endswith('''.b''' ): lowerCAmelCase__ = old_dic[k] elif k.endswith('''.w''' ): lowerCAmelCase__ = old_dic[k] elif "level_2" not in dict_name and "cond.model." in k: lowerCAmelCase__ = old_dic[k] else: lowerCAmelCase__ = old_dic[k] lowerCAmelCase__ = '''vqvae''' if i == 0 else F"""priors.{3 - i}""" lowerCAmelCase__ = fix_jukebox_keys(A , model.state_dict() , A , A ) weight_dict.append(A ) lowerCAmelCase__ = weight_dict.pop(0 ) model.vqvae.load_state_dict(A ) for i in range(len(A ) ): model.priors[i].load_state_dict(weight_dict[2 - i] ) Path(A ).mkdir(exist_ok=A ) with open(F"""{pytorch_dump_folder_path}/mapping.json""" , '''w''' ) as txtfile: json.dump(A , A ) print(F"""Saving model {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(A ) return weight_dict if __name__ == "__main__": __UpperCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''jukebox-5b-lyrics''', type=str, help='''Name of the model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default='''jukebox-5b-lyrics-converted''', type=str, help='''Path to the output PyTorch model directory.''', ) __UpperCAmelCase = parser.parse_args() convert_openai_checkpoint(args.model_name, args.pytorch_dump_folder_path)
228
0
from __future__ import annotations import numpy as np from numpy import floataa from numpy.typing import NDArray def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : NDArray[floataa] , SCREAMING_SNAKE_CASE__ : NDArray[floataa] , SCREAMING_SNAKE_CASE__ : list[int] , SCREAMING_SNAKE_CASE__ : int , ): __UpperCamelCase , __UpperCamelCase =coefficient_matrix.shape __UpperCamelCase , __UpperCamelCase =constant_matrix.shape if rowsa != colsa: __UpperCamelCase =F'Coefficient matrix dimensions must be nxn but received {rowsa}x{colsa}' raise ValueError(SCREAMING_SNAKE_CASE__ ) if colsa != 1: __UpperCamelCase =F'Constant matrix must be nx1 but received {rowsa}x{colsa}' raise ValueError(SCREAMING_SNAKE_CASE__ ) if rowsa != rowsa: __UpperCamelCase =( 'Coefficient and constant matrices dimensions must be nxn and nx1 but ' F'received {rowsa}x{colsa} and {rowsa}x{colsa}' ) raise ValueError(SCREAMING_SNAKE_CASE__ ) if len(SCREAMING_SNAKE_CASE__ ) != rowsa: __UpperCamelCase =( 'Number of initial values must be equal to number of rows in coefficient ' F'matrix but received {len(SCREAMING_SNAKE_CASE__ )} and {rowsa}' ) raise ValueError(SCREAMING_SNAKE_CASE__ ) if iterations <= 0: raise ValueError('Iterations must be at least 1' ) __UpperCamelCase =np.concatenate( (coefficient_matrix, constant_matrix) , axis=1 ) __UpperCamelCase , __UpperCamelCase =table.shape strictly_diagonally_dominant(SCREAMING_SNAKE_CASE__ ) # Iterates the whole matrix for given number of times for _ in range(SCREAMING_SNAKE_CASE__ ): __UpperCamelCase =[] for row in range(SCREAMING_SNAKE_CASE__ ): __UpperCamelCase =0 for col in range(SCREAMING_SNAKE_CASE__ ): if col == row: __UpperCamelCase =table[row][col] elif col == cols - 1: __UpperCamelCase =table[row][col] else: temp += (-1) * table[row][col] * init_val[col] __UpperCamelCase =(temp + val) / denom new_val.append(SCREAMING_SNAKE_CASE__ ) __UpperCamelCase =new_val return [float(SCREAMING_SNAKE_CASE__ ) for i in new_val] def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : NDArray[floataa] ): __UpperCamelCase , __UpperCamelCase =table.shape __UpperCamelCase =True for i in range(0 , SCREAMING_SNAKE_CASE__ ): __UpperCamelCase =0 for j in range(0 , cols - 1 ): if i == j: continue else: total += table[i][j] if table[i][i] <= total: raise ValueError('Coefficient matrix is not strictly diagonally dominant' ) return is_diagonally_dominant # Test Cases if __name__ == "__main__": import doctest doctest.testmod()
62
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging __lowerCAmelCase : List[Any] =logging.get_logger(__name__) __lowerCAmelCase : Union[str, Any] ={ """s-JoL/Open-Llama-V1""": """https://huggingface.co/s-JoL/Open-Llama-V1/blob/main/config.json""", } class _A ( lowerCAmelCase ): snake_case__ : Union[str, Any] = 'open-llama' def __init__( self , __lowerCAmelCase=10_0000 , __lowerCAmelCase=4096 , __lowerCAmelCase=1_1008 , __lowerCAmelCase=32 , __lowerCAmelCase=32 , __lowerCAmelCase="silu" , __lowerCAmelCase=2048 , __lowerCAmelCase=0.0_2 , __lowerCAmelCase=1E-6 , __lowerCAmelCase=True , __lowerCAmelCase=0 , __lowerCAmelCase=1 , __lowerCAmelCase=2 , __lowerCAmelCase=False , __lowerCAmelCase=True , __lowerCAmelCase=0.1 , __lowerCAmelCase=0.1 , __lowerCAmelCase=True , __lowerCAmelCase=True , __lowerCAmelCase=None , **__lowerCAmelCase , ): """simple docstring""" lowercase = vocab_size lowercase = max_position_embeddings lowercase = hidden_size lowercase = intermediate_size lowercase = num_hidden_layers lowercase = num_attention_heads lowercase = hidden_act lowercase = initializer_range lowercase = rms_norm_eps lowercase = use_cache lowercase = kwargs.pop( """use_memorry_efficient_attention""" , __lowerCAmelCase ) lowercase = hidden_dropout_prob lowercase = attention_dropout_prob lowercase = use_stable_embedding lowercase = shared_input_output_embedding lowercase = rope_scaling self._rope_scaling_validation() super().__init__( pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , tie_word_embeddings=__lowerCAmelCase , **__lowerCAmelCase , ) def A__ ( self ): """simple docstring""" if self.rope_scaling is None: return if not isinstance(self.rope_scaling , __lowerCAmelCase ) or len(self.rope_scaling ) != 2: raise ValueError( """`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, """ f'got {self.rope_scaling}' ) lowercase = self.rope_scaling.get("""type""" , __lowerCAmelCase ) lowercase = self.rope_scaling.get("""factor""" , __lowerCAmelCase ) if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]: raise ValueError( f'`rope_scaling`\'s name field must be one of [\'linear\', \'dynamic\'], got {rope_scaling_type}' ) if rope_scaling_factor is None or not isinstance(__lowerCAmelCase , __lowerCAmelCase ) or rope_scaling_factor <= 1.0: raise ValueError(f'`rope_scaling`\'s factor field must be an float > 1, got {rope_scaling_factor}' )
197
0
'''simple docstring''' from typing import List, Optional, Tuple, Union import torch from torch import nn from torch.nn import CrossEntropyLoss from ... import AutoBackbone from ...modeling_outputs import SemanticSegmenterOutput from ...modeling_utils import PreTrainedModel from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings from ...utils.backbone_utils import BackboneMixin from .configuration_upernet import UperNetConfig _SCREAMING_SNAKE_CASE = [ "openmmlab/upernet-convnext-tiny", # See all UperNet models at https://huggingface.co/models?filter=upernet ] # General docstring _SCREAMING_SNAKE_CASE = "UperNetConfig" class _lowerCAmelCase ( nn.Module ): """simple docstring""" def __init__( self : Tuple , __snake_case : int , __snake_case : int , __snake_case : Union[int, Tuple[int, int]] , __snake_case : Union[int, Tuple[int, int], str] = 0 , __snake_case : bool = False , __snake_case : Union[int, Tuple[int, int]] = 1 , )-> None: super().__init__() snake_case = nn.Convad( in_channels=__snake_case , out_channels=__snake_case , kernel_size=__snake_case , padding=__snake_case , bias=__snake_case , dilation=__snake_case , ) snake_case = nn.BatchNormad(__snake_case ) snake_case = nn.ReLU() def lowerCAmelCase ( self : Union[str, Any] , __snake_case : torch.Tensor )-> torch.Tensor: snake_case = self.conv(__snake_case ) snake_case = self.batch_norm(__snake_case ) snake_case = self.activation(__snake_case ) return output class _lowerCAmelCase ( nn.Module ): """simple docstring""" def __init__( self : List[str] , __snake_case : int , __snake_case : int , __snake_case : int )-> None: super().__init__() snake_case = [ nn.AdaptiveAvgPoolad(__snake_case ), UperNetConvModule(__snake_case , __snake_case , kernel_size=1 ), ] for i, layer in enumerate(self.layers ): self.add_module(str(__snake_case ) , __snake_case ) def lowerCAmelCase ( self : Any , __snake_case : torch.Tensor )-> torch.Tensor: snake_case = input for layer in self.layers: snake_case = layer(__snake_case ) return hidden_state class _lowerCAmelCase ( nn.Module ): """simple docstring""" def __init__( self : Union[str, Any] , __snake_case : Tuple[int, ...] , __snake_case : int , __snake_case : int , __snake_case : bool )-> None: super().__init__() snake_case = pool_scales snake_case = align_corners snake_case = in_channels snake_case = channels snake_case = [] for i, pool_scale in enumerate(__snake_case ): snake_case = UperNetPyramidPoolingBlock(pool_scale=__snake_case , in_channels=__snake_case , channels=__snake_case ) self.blocks.append(__snake_case ) self.add_module(str(__snake_case ) , __snake_case ) def lowerCAmelCase ( self : Optional[int] , __snake_case : torch.Tensor )-> List[torch.Tensor]: snake_case = [] for ppm in self.blocks: snake_case = ppm(__snake_case ) snake_case = nn.functional.interpolate( __snake_case , size=x.size()[2:] , mode="""bilinear""" , align_corners=self.align_corners ) ppm_outs.append(__snake_case ) return ppm_outs class _lowerCAmelCase ( nn.Module ): """simple docstring""" def __init__( self : Tuple , __snake_case : Union[str, Any] , __snake_case : int )-> Tuple: super().__init__() snake_case = config snake_case = config.pool_scales # e.g. (1, 2, 3, 6) snake_case = in_channels snake_case = config.hidden_size snake_case = False snake_case = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) # PSP Module snake_case = UperNetPyramidPoolingModule( self.pool_scales , self.in_channels[-1] , self.channels , align_corners=self.align_corners , ) snake_case = UperNetConvModule( self.in_channels[-1] + len(self.pool_scales ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) # FPN Module snake_case = nn.ModuleList() snake_case = nn.ModuleList() for in_channels in self.in_channels[:-1]: # skip the top layer snake_case = UperNetConvModule(__snake_case , self.channels , kernel_size=1 ) snake_case = UperNetConvModule(self.channels , self.channels , kernel_size=3 , padding=1 ) self.lateral_convs.append(__snake_case ) self.fpn_convs.append(__snake_case ) snake_case = UperNetConvModule( len(self.in_channels ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) def lowerCAmelCase ( self : str )-> List[Any]: self.apply(self._init_weights ) def lowerCAmelCase ( self : Dict , __snake_case : Tuple )-> List[Any]: if isinstance(__snake_case , nn.Convad ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def lowerCAmelCase ( self : Optional[int] , __snake_case : List[str] )-> Union[str, Any]: snake_case = inputs[-1] snake_case = [x] psp_outs.extend(self.psp_modules(__snake_case ) ) snake_case = torch.cat(__snake_case , dim=1 ) snake_case = self.bottleneck(__snake_case ) return output def lowerCAmelCase ( self : Any , __snake_case : torch.Tensor )-> torch.Tensor: # build laterals snake_case = [lateral_conv(encoder_hidden_states[i] ) for i, lateral_conv in enumerate(self.lateral_convs )] laterals.append(self.psp_forward(__snake_case ) ) # build top-down path snake_case = len(__snake_case ) for i in range(used_backbone_levels - 1 , 0 , -1 ): snake_case = laterals[i - 1].shape[2:] snake_case = laterals[i - 1] + nn.functional.interpolate( laterals[i] , size=__snake_case , mode="""bilinear""" , align_corners=self.align_corners ) # build outputs snake_case = [self.fpn_convs[i](laterals[i] ) for i in range(used_backbone_levels - 1 )] # append psp feature fpn_outs.append(laterals[-1] ) for i in range(used_backbone_levels - 1 , 0 , -1 ): snake_case = nn.functional.interpolate( fpn_outs[i] , size=fpn_outs[0].shape[2:] , mode="""bilinear""" , align_corners=self.align_corners ) snake_case = torch.cat(__snake_case , dim=1 ) snake_case = self.fpn_bottleneck(__snake_case ) snake_case = self.classifier(__snake_case ) return output class _lowerCAmelCase ( nn.Module ): """simple docstring""" def __init__( self : Union[str, Any] , __snake_case : Optional[int] , __snake_case : int = 2 , __snake_case : int = 3 , __snake_case : Union[int, Tuple[int, int]] = 1 )-> None: super().__init__() snake_case = config snake_case = config.auxiliary_in_channels snake_case = config.auxiliary_channels snake_case = config.auxiliary_num_convs snake_case = config.auxiliary_concat_input snake_case = in_index snake_case = (kernel_size // 2) * dilation snake_case = [] convs.append( UperNetConvModule( self.in_channels , self.channels , kernel_size=__snake_case , padding=__snake_case , dilation=__snake_case ) ) for i in range(self.num_convs - 1 ): convs.append( UperNetConvModule( self.channels , self.channels , kernel_size=__snake_case , padding=__snake_case , dilation=__snake_case ) ) if self.num_convs == 0: snake_case = nn.Identity() else: snake_case = nn.Sequential(*__snake_case ) if self.concat_input: snake_case = UperNetConvModule( self.in_channels + self.channels , self.channels , kernel_size=__snake_case , padding=kernel_size // 2 ) snake_case = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) def lowerCAmelCase ( self : List[Any] )-> str: self.apply(self._init_weights ) def lowerCAmelCase ( self : Any , __snake_case : Tuple )-> Dict: if isinstance(__snake_case , nn.Convad ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def lowerCAmelCase ( self : Union[str, Any] , __snake_case : torch.Tensor )-> torch.Tensor: # just take the relevant feature maps snake_case = encoder_hidden_states[self.in_index] snake_case = self.convs(__snake_case ) if self.concat_input: snake_case = self.conv_cat(torch.cat([hidden_states, output] , dim=1 ) ) snake_case = self.classifier(__snake_case ) return output class _lowerCAmelCase ( A__ ): """simple docstring""" snake_case_ = UperNetConfig snake_case_ = "pixel_values" snake_case_ = True def lowerCAmelCase ( self : Union[str, Any] , __snake_case : int )-> List[str]: if isinstance(__snake_case , __snake_case ): module.backbone.init_weights() module.decode_head.init_weights() module.auxiliary_head.init_weights() def lowerCAmelCase ( self : int )-> Union[str, Any]: self.backbone.init_weights() self.decode_head.init_weights() self.auxiliary_head.init_weights() def lowerCAmelCase ( self : List[str] , __snake_case : Optional[Any] , __snake_case : List[str]=False )-> List[Any]: if isinstance(__snake_case , __snake_case ): snake_case = value _SCREAMING_SNAKE_CASE = r"\n Parameters:\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n config ([`UperNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n" _SCREAMING_SNAKE_CASE = r"\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using\n [`AutoImageProcessor`]. See [`SegformerImageProcessor.__call__`] for details.\n output_attentions (`bool`, *optional*):\n Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See\n `attentions` under returned tensors for more detail.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers of the backbone. See `hidden_states` under\n returned tensors for more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n" @add_start_docstrings( "UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes." , A__ , ) class _lowerCAmelCase ( A__ ): """simple docstring""" def __init__( self : List[str] , __snake_case : Union[str, Any] )-> Tuple: super().__init__(__snake_case ) snake_case = AutoBackbone.from_config(config.backbone_config ) # Semantic segmentation head(s) snake_case = UperNetHead(__snake_case , in_channels=self.backbone.channels ) snake_case = UperNetFCNHead(__snake_case ) if config.use_auxiliary_head else None # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UPERNET_INPUTS_DOCSTRING.format("""batch_size, sequence_length""" ) ) @replace_return_docstrings(output_type=__snake_case , config_class=_CONFIG_FOR_DOC ) def lowerCAmelCase ( self : List[Any] , __snake_case : Optional[torch.Tensor] = None , __snake_case : Optional[bool] = None , __snake_case : Optional[bool] = None , __snake_case : Optional[torch.Tensor] = None , __snake_case : Optional[bool] = None , )-> Union[tuple, SemanticSegmenterOutput]: snake_case = return_dict if return_dict is not None else self.config.use_return_dict snake_case = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) snake_case = output_attentions if output_attentions is not None else self.config.output_attentions snake_case = self.backbone.forward_with_filtered_kwargs( __snake_case , output_hidden_states=__snake_case , output_attentions=__snake_case ) snake_case = outputs.feature_maps snake_case = self.decode_head(__snake_case ) snake_case = nn.functional.interpolate(__snake_case , size=pixel_values.shape[2:] , mode="""bilinear""" , align_corners=__snake_case ) snake_case = None if self.auxiliary_head is not None: snake_case = self.auxiliary_head(__snake_case ) snake_case = nn.functional.interpolate( __snake_case , size=pixel_values.shape[2:] , mode="""bilinear""" , align_corners=__snake_case ) snake_case = None if labels is not None: if self.config.num_labels == 1: raise ValueError("""The number of labels should be greater than one""" ) else: # compute weighted loss snake_case = CrossEntropyLoss(ignore_index=self.config.loss_ignore_index ) snake_case = loss_fct(__snake_case , __snake_case ) snake_case = loss_fct(__snake_case , __snake_case ) snake_case = main_loss + self.config.auxiliary_loss_weight * auxiliary_loss if not return_dict: if output_hidden_states: snake_case = (logits,) + outputs[1:] else: snake_case = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return SemanticSegmenterOutput( loss=__snake_case , logits=__snake_case , hidden_states=outputs.hidden_states , attentions=outputs.attentions , )
3
'''simple docstring''' import argparse import json import os import fairseq import torch from fairseq.data import Dictionary # Register SEW's fairseq modules from sew_asapp import tasks # noqa: F401 from transformers import ( SEWConfig, SEWForCTC, SEWModel, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaProcessor, logging, ) logging.set_verbosity_info() _SCREAMING_SNAKE_CASE = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE = { "post_extract_proj": "feature_projection", "encoder.pos_conv.0": "encoder.pos_conv_embed.conv", "self_attn.k_proj": "encoder.layers.*.attention.k_proj", "self_attn.v_proj": "encoder.layers.*.attention.v_proj", "self_attn.q_proj": "encoder.layers.*.attention.q_proj", "self_attn.out_proj": "encoder.layers.*.attention.out_proj", "self_attn_layer_norm": "encoder.layers.*.layer_norm", "fc1": "encoder.layers.*.feed_forward.intermediate_dense", "fc2": "encoder.layers.*.feed_forward.output_dense", "final_layer_norm": "encoder.layers.*.final_layer_norm", "encoder.upsample.0": "encoder.upsample.projection", "encoder.layer_norm": "encoder.layer_norm", "w2v_model.layer_norm": "layer_norm", "w2v_encoder.proj": "lm_head", "mask_emb": "masked_spec_embed", } def __lowerCamelCase ( __lowerCAmelCase : Dict , __lowerCAmelCase : Dict , __lowerCAmelCase : Dict , __lowerCAmelCase : Any , __lowerCAmelCase : str ) -> Union[str, Any]: for attribute in key.split(""".""" ): snake_case = getattr(__lowerCAmelCase , __lowerCAmelCase ) if weight_type is not None: snake_case = getattr(__lowerCAmelCase , __lowerCAmelCase ).shape else: snake_case = hf_pointer.shape assert hf_shape == value.shape, ( F'''Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be''' F''' {value.shape} for {full_name}''' ) if weight_type == "weight": snake_case = value elif weight_type == "weight_g": snake_case = value elif weight_type == "weight_v": snake_case = value elif weight_type == "bias": snake_case = value else: snake_case = value logger.info(F'''{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.''' ) def __lowerCamelCase ( __lowerCAmelCase : str , __lowerCAmelCase : List[str] , __lowerCAmelCase : Union[str, Any] ) -> int: snake_case = [] snake_case = fairseq_model.state_dict() snake_case = hf_model.sew.feature_extractor if is_finetuned else hf_model.feature_extractor for name, value in fairseq_dict.items(): snake_case = False if "conv_layers" in name: load_conv_layer( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , hf_model.config.feat_extract_norm == """group""" , ) snake_case = True else: for key, mapped_key in MAPPING.items(): snake_case = """sew.""" + mapped_key if (is_finetuned and mapped_key != """lm_head""") else mapped_key if key in name or key.split("""w2v_model.""" )[-1] == name.split(""".""" )[0]: snake_case = True if "*" in mapped_key: snake_case = name.split(__lowerCAmelCase )[0].split(""".""" )[-2] snake_case = mapped_key.replace("""*""" , __lowerCAmelCase ) if "weight_g" in name: snake_case = """weight_g""" elif "weight_v" in name: snake_case = """weight_v""" elif "weight" in name: snake_case = """weight""" elif "bias" in name: snake_case = """bias""" else: snake_case = None set_recursively(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) continue if not is_used: unused_weights.append(__lowerCAmelCase ) logger.warning(F'''Unused weights: {unused_weights}''' ) def __lowerCamelCase ( __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Tuple , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Tuple ) -> List[str]: snake_case = full_name.split("""conv_layers.""" )[-1] snake_case = name.split(""".""" ) snake_case = int(items[0] ) snake_case = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( F'''{full_name} has size {value.shape}, but''' F''' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.''' ) snake_case = value logger.info(F'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( F'''{full_name} has size {value.shape}, but''' F''' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.''' ) snake_case = value logger.info(F'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( F'''{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was''' " found." ) snake_case = value logger.info(F'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( F'''{full_name} has size {value.shape}, but''' F''' {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.''' ) snake_case = value logger.info(F'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) else: unused_weights.append(__lowerCAmelCase ) def __lowerCamelCase ( __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Any ) -> List[str]: snake_case = SEWConfig() if is_finetuned: snake_case = model.wav_encoder.wav_model.cfg else: snake_case = model.cfg snake_case = fs_config.conv_bias snake_case = eval(fs_config.conv_feature_layers ) snake_case = [x[0] for x in conv_layers] snake_case = [x[1] for x in conv_layers] snake_case = [x[2] for x in conv_layers] snake_case = """gelu""" snake_case = """layer""" if fs_config.extractor_mode == """layer_norm""" else """group""" snake_case = 0.0 snake_case = fs_config.activation_fn.name snake_case = fs_config.encoder_embed_dim snake_case = 0.02 snake_case = fs_config.encoder_ffn_embed_dim snake_case = 1e-5 snake_case = fs_config.encoder_layerdrop snake_case = fs_config.encoder_attention_heads snake_case = fs_config.conv_pos_groups snake_case = fs_config.conv_pos snake_case = len(__lowerCAmelCase ) snake_case = fs_config.encoder_layers snake_case = fs_config.squeeze_factor # take care of any params that are overridden by the Wav2VecCtc model if is_finetuned: snake_case = model.cfg snake_case = fs_config.final_dropout snake_case = fs_config.layerdrop snake_case = fs_config.activation_dropout snake_case = fs_config.mask_prob > 0 or fs_config.mask_channel_prob > 0 snake_case = fs_config.attention_dropout snake_case = fs_config.dropout_input snake_case = fs_config.dropout snake_case = fs_config.mask_channel_length snake_case = fs_config.mask_channel_prob snake_case = fs_config.mask_length snake_case = fs_config.mask_prob snake_case = """Wav2Vec2FeatureExtractor""" snake_case = """Wav2Vec2CTCTokenizer""" return config @torch.no_grad() def __lowerCamelCase ( __lowerCAmelCase : List[str] , __lowerCAmelCase : Tuple , __lowerCAmelCase : List[Any]=None , __lowerCAmelCase : int=None , __lowerCAmelCase : str=True ) -> Any: if is_finetuned: snake_case , snake_case , snake_case = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"""data""": """/""".join(dict_path.split("""/""" )[:-1] )} ) else: snake_case , snake_case , snake_case = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] ) if config_path is not None: snake_case = SEWConfig.from_pretrained(__lowerCAmelCase ) else: snake_case = convert_config(model[0] , __lowerCAmelCase ) snake_case = model[0].eval() snake_case = True if config.feat_extract_norm == """layer""" else False snake_case = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_60_00 , padding_value=0 , do_normalize=__lowerCAmelCase , return_attention_mask=__lowerCAmelCase , ) if is_finetuned: if dict_path: snake_case = Dictionary.load(__lowerCAmelCase ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq snake_case = target_dict.pad_index snake_case = target_dict.bos_index snake_case = target_dict.pad_index snake_case = target_dict.bos_index snake_case = target_dict.eos_index snake_case = len(target_dict.symbols ) snake_case = os.path.join(__lowerCAmelCase , """vocab.json""" ) if not os.path.isdir(__lowerCAmelCase ): logger.error("""--pytorch_dump_folder_path ({}) should be a directory""".format(__lowerCAmelCase ) ) return os.makedirs(__lowerCAmelCase , exist_ok=__lowerCAmelCase ) with open(__lowerCAmelCase , """w""" , encoding="""utf-8""" ) as vocab_handle: json.dump(target_dict.indices , __lowerCAmelCase ) snake_case = WavaVecaCTCTokenizer( __lowerCAmelCase , unk_token=target_dict.unk_word , pad_token=target_dict.pad_word , bos_token=target_dict.bos_word , eos_token=target_dict.eos_word , word_delimiter_token="""|""" , do_lower_case=__lowerCAmelCase , ) snake_case = WavaVecaProcessor(feature_extractor=__lowerCAmelCase , tokenizer=__lowerCAmelCase ) processor.save_pretrained(__lowerCAmelCase ) snake_case = SEWForCTC(__lowerCAmelCase ) else: snake_case = SEWModel(__lowerCAmelCase ) feature_extractor.save_pretrained(__lowerCAmelCase ) recursively_load_weights(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) hf_model.save_pretrained(__lowerCAmelCase ) if __name__ == "__main__": _SCREAMING_SNAKE_CASE = argparse.ArgumentParser() parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to fairseq checkpoint") parser.add_argument("--dict_path", default=None, type=str, help="Path to dict of fine-tuned model") parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert") parser.add_argument( "--is_finetuned", action="store_true", help="Whether the model to convert is a fine-tuned model or not" ) _SCREAMING_SNAKE_CASE = parser.parse_args() convert_sew_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, args.is_finetuned )
3
1
from ..utils import DummyObject, requires_backends class _SCREAMING_SNAKE_CASE ( metaclass=a_ ): '''simple docstring''' lowercase_ = ["flax"] def __init__(self : Optional[Any] , *UpperCAmelCase_ : Optional[Any] , **UpperCAmelCase_ : List[Any]) ->Union[str, Any]: '''simple docstring''' requires_backends(self , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : Tuple , *UpperCAmelCase_ : Optional[Any] , **UpperCAmelCase_ : Optional[Any]) ->Optional[int]: '''simple docstring''' requires_backends(cls , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : str , *UpperCAmelCase_ : Optional[Any] , **UpperCAmelCase_ : Dict) ->Optional[int]: '''simple docstring''' requires_backends(cls , ["flax"]) class _SCREAMING_SNAKE_CASE ( metaclass=a_ ): '''simple docstring''' lowercase_ = ["flax"] def __init__(self : Union[str, Any] , *UpperCAmelCase_ : Optional[int] , **UpperCAmelCase_ : Optional[int]) ->str: '''simple docstring''' requires_backends(self , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : Optional[Any] , *UpperCAmelCase_ : Dict , **UpperCAmelCase_ : Any) ->Tuple: '''simple docstring''' requires_backends(cls , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : List[Any] , *UpperCAmelCase_ : Tuple , **UpperCAmelCase_ : Tuple) ->List[Any]: '''simple docstring''' requires_backends(cls , ["flax"]) class _SCREAMING_SNAKE_CASE ( metaclass=a_ ): '''simple docstring''' lowercase_ = ["flax"] def __init__(self : int , *UpperCAmelCase_ : Any , **UpperCAmelCase_ : Tuple) ->Any: '''simple docstring''' requires_backends(self , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : int , *UpperCAmelCase_ : Any , **UpperCAmelCase_ : Any) ->Optional[int]: '''simple docstring''' requires_backends(cls , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : Union[str, Any] , *UpperCAmelCase_ : Tuple , **UpperCAmelCase_ : List[str]) ->int: '''simple docstring''' requires_backends(cls , ["flax"]) class _SCREAMING_SNAKE_CASE ( metaclass=a_ ): '''simple docstring''' lowercase_ = ["flax"] def __init__(self : Tuple , *UpperCAmelCase_ : Optional[Any] , **UpperCAmelCase_ : Optional[Any]) ->Optional[int]: '''simple docstring''' requires_backends(self , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : Any , *UpperCAmelCase_ : int , **UpperCAmelCase_ : Any) ->Dict: '''simple docstring''' requires_backends(cls , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : Dict , *UpperCAmelCase_ : Optional[int] , **UpperCAmelCase_ : Optional[Any]) ->str: '''simple docstring''' requires_backends(cls , ["flax"]) class _SCREAMING_SNAKE_CASE ( metaclass=a_ ): '''simple docstring''' lowercase_ = ["flax"] def __init__(self : str , *UpperCAmelCase_ : Any , **UpperCAmelCase_ : Dict) ->Tuple: '''simple docstring''' requires_backends(self , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : Tuple , *UpperCAmelCase_ : int , **UpperCAmelCase_ : Dict) ->Tuple: '''simple docstring''' requires_backends(cls , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : List[Any] , *UpperCAmelCase_ : Union[str, Any] , **UpperCAmelCase_ : int) ->Optional[int]: '''simple docstring''' requires_backends(cls , ["flax"]) class _SCREAMING_SNAKE_CASE ( metaclass=a_ ): '''simple docstring''' lowercase_ = ["flax"] def __init__(self : List[Any] , *UpperCAmelCase_ : Dict , **UpperCAmelCase_ : List[Any]) ->int: '''simple docstring''' requires_backends(self , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : str , *UpperCAmelCase_ : Dict , **UpperCAmelCase_ : Any) ->List[str]: '''simple docstring''' requires_backends(cls , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : List[str] , *UpperCAmelCase_ : str , **UpperCAmelCase_ : Optional[Any]) ->Tuple: '''simple docstring''' requires_backends(cls , ["flax"]) class _SCREAMING_SNAKE_CASE ( metaclass=a_ ): '''simple docstring''' lowercase_ = ["flax"] def __init__(self : List[str] , *UpperCAmelCase_ : str , **UpperCAmelCase_ : Optional[Any]) ->int: '''simple docstring''' requires_backends(self , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : List[Any] , *UpperCAmelCase_ : Optional[int] , **UpperCAmelCase_ : str) ->Optional[Any]: '''simple docstring''' requires_backends(cls , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : List[Any] , *UpperCAmelCase_ : List[str] , **UpperCAmelCase_ : Dict) ->int: '''simple docstring''' requires_backends(cls , ["flax"]) class _SCREAMING_SNAKE_CASE ( metaclass=a_ ): '''simple docstring''' lowercase_ = ["flax"] def __init__(self : List[str] , *UpperCAmelCase_ : List[Any] , **UpperCAmelCase_ : List[Any]) ->Optional[int]: '''simple docstring''' requires_backends(self , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : int , *UpperCAmelCase_ : Tuple , **UpperCAmelCase_ : Optional[int]) ->str: '''simple docstring''' requires_backends(cls , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : List[Any] , *UpperCAmelCase_ : Optional[Any] , **UpperCAmelCase_ : List[str]) ->Dict: '''simple docstring''' requires_backends(cls , ["flax"]) class _SCREAMING_SNAKE_CASE ( metaclass=a_ ): '''simple docstring''' lowercase_ = ["flax"] def __init__(self : str , *UpperCAmelCase_ : Any , **UpperCAmelCase_ : List[str]) ->str: '''simple docstring''' requires_backends(self , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : Dict , *UpperCAmelCase_ : List[str] , **UpperCAmelCase_ : Any) ->Optional[int]: '''simple docstring''' requires_backends(cls , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : int , *UpperCAmelCase_ : Union[str, Any] , **UpperCAmelCase_ : Dict) ->List[str]: '''simple docstring''' requires_backends(cls , ["flax"]) class _SCREAMING_SNAKE_CASE ( metaclass=a_ ): '''simple docstring''' lowercase_ = ["flax"] def __init__(self : List[Any] , *UpperCAmelCase_ : List[str] , **UpperCAmelCase_ : Optional[int]) ->Optional[int]: '''simple docstring''' requires_backends(self , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : List[str] , *UpperCAmelCase_ : Optional[int] , **UpperCAmelCase_ : Optional[int]) ->List[str]: '''simple docstring''' requires_backends(cls , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : Union[str, Any] , *UpperCAmelCase_ : int , **UpperCAmelCase_ : Tuple) ->Dict: '''simple docstring''' requires_backends(cls , ["flax"]) class _SCREAMING_SNAKE_CASE ( metaclass=a_ ): '''simple docstring''' lowercase_ = ["flax"] def __init__(self : Optional[Any] , *UpperCAmelCase_ : str , **UpperCAmelCase_ : Union[str, Any]) ->Optional[Any]: '''simple docstring''' requires_backends(self , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : Any , *UpperCAmelCase_ : List[str] , **UpperCAmelCase_ : Tuple) ->Tuple: '''simple docstring''' requires_backends(cls , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : List[Any] , *UpperCAmelCase_ : Union[str, Any] , **UpperCAmelCase_ : Union[str, Any]) ->Tuple: '''simple docstring''' requires_backends(cls , ["flax"]) class _SCREAMING_SNAKE_CASE ( metaclass=a_ ): '''simple docstring''' lowercase_ = ["flax"] def __init__(self : Optional[int] , *UpperCAmelCase_ : Union[str, Any] , **UpperCAmelCase_ : Optional[Any]) ->Any: '''simple docstring''' requires_backends(self , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : Tuple , *UpperCAmelCase_ : Optional[int] , **UpperCAmelCase_ : Optional[int]) ->int: '''simple docstring''' requires_backends(cls , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : Union[str, Any] , *UpperCAmelCase_ : str , **UpperCAmelCase_ : List[str]) ->Tuple: '''simple docstring''' requires_backends(cls , ["flax"]) class _SCREAMING_SNAKE_CASE ( metaclass=a_ ): '''simple docstring''' lowercase_ = ["flax"] def __init__(self : str , *UpperCAmelCase_ : Tuple , **UpperCAmelCase_ : Any) ->Tuple: '''simple docstring''' requires_backends(self , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : List[str] , *UpperCAmelCase_ : Dict , **UpperCAmelCase_ : str) ->List[str]: '''simple docstring''' requires_backends(cls , ["flax"]) @classmethod def SCREAMING_SNAKE_CASE_ (cls : Union[str, Any] , *UpperCAmelCase_ : Union[str, Any] , **UpperCAmelCase_ : Dict) ->Optional[Any]: '''simple docstring''' requires_backends(cls , ["flax"])
10
"""simple docstring""" from __future__ import annotations import bisect def lowercase_ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0 , __UpperCAmelCase = -1 ) -> int: if hi < 0: lowerCAmelCase__ : Union[str, Any] = len(__UpperCAmelCase ) while lo < hi: lowerCAmelCase__ : Tuple = lo + (hi - lo) // 2 if sorted_collection[mid] < item: lowerCAmelCase__ : Optional[int] = mid + 1 else: lowerCAmelCase__ : List[Any] = mid return lo def lowercase_ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0 , __UpperCAmelCase = -1 ) -> int: if hi < 0: lowerCAmelCase__ : Union[str, Any] = len(__UpperCAmelCase ) while lo < hi: lowerCAmelCase__ : List[str] = lo + (hi - lo) // 2 if sorted_collection[mid] <= item: lowerCAmelCase__ : Dict = mid + 1 else: lowerCAmelCase__ : Any = mid return lo def lowercase_ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0 , __UpperCAmelCase = -1 ) -> None: sorted_collection.insert(bisect_left(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) , __UpperCAmelCase ) def lowercase_ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = 0 , __UpperCAmelCase = -1 ) -> None: sorted_collection.insert(bisect_right(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) , __UpperCAmelCase ) def lowercase_ ( __UpperCAmelCase , __UpperCAmelCase ) -> int | None: lowerCAmelCase__ : Any = 0 lowerCAmelCase__ : Union[str, Any] = len(__UpperCAmelCase ) - 1 while left <= right: lowerCAmelCase__ : str = left + (right - left) // 2 lowerCAmelCase__ : List[Any] = sorted_collection[midpoint] if current_item == item: return midpoint elif item < current_item: lowerCAmelCase__ : Optional[int] = midpoint - 1 else: lowerCAmelCase__ : Optional[int] = midpoint + 1 return None def lowercase_ ( __UpperCAmelCase , __UpperCAmelCase ) -> int | None: lowerCAmelCase__ : Any = bisect.bisect_left(__UpperCAmelCase , __UpperCAmelCase ) if index != len(__UpperCAmelCase ) and sorted_collection[index] == item: return index return None def lowercase_ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> int | None: if right < left: return None lowerCAmelCase__ : List[str] = left + (right - left) // 2 if sorted_collection[midpoint] == item: return midpoint elif sorted_collection[midpoint] > item: return binary_search_by_recursion(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , midpoint - 1 ) else: return binary_search_by_recursion(__UpperCAmelCase , __UpperCAmelCase , midpoint + 1 , __UpperCAmelCase ) if __name__ == "__main__": _A = input("""Enter numbers separated by comma:\n""").strip() _A = sorted(int(item) for item in user_input.split(""",""")) _A = int(input("""Enter a single number to be found in the list:\n""")) _A = binary_search(collection, target) if result is None: print(f"""{target} was not found in {collection}.""") else: print(f"""{target} was found at position {result} in {collection}.""")
242
0
import unittest from transformers import JukeboxTokenizer from transformers.testing_utils import require_torch class lowerCamelCase__ ( unittest.TestCase): '''simple docstring''' snake_case_ =JukeboxTokenizer snake_case_ ={ """artist""": """Zac Brown Band""", """genres""": """Country""", """lyrics""": """I met a traveller from an antique land, Who said \"Two vast and trunkless legs of stone Stand in the desert. . . . Near them, on the sand, Half sunk a shattered visage lies, whose frown, And wrinkled lip, and sneer of cold command, Tell that its sculptor well those passions read Which yet survive, stamped on these lifeless things, The hand that mocked them, and the heart that fed; And on the pedestal, these words appear: My name is Ozymandias, King of Kings; Look on my Works, ye Mighty, and despair! Nothing beside remains. Round the decay Of that colossal Wreck, boundless and bare The lone and level sands stretch far away """, } @require_torch def lowerCAmelCase__ (self ) -> int: """simple docstring""" import torch lowerCAmelCase__ : Dict = JukeboxTokenizer.from_pretrained('''openai/jukebox-1b-lyrics''' ) lowerCAmelCase__ : Any = tokenizer(**self.metas )['''input_ids'''] # fmt: off lowerCAmelCase__ : Union[str, Any] = [ torch.tensor([[ 0, 0, 0, 71_69, 5_07, 9, 76, 39, 31, 46, 76, 27, 76, 46, 44, 27, 48, 31, 38, 38, 31, 44, 76, 32, 44, 41, 39, 76, 27, 40, 76, 27, 40, 46, 35, 43, 47, 31, 76, 38, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 23, 34, 41, 76, 45, 27, 35, 30, 76, 71, 20, 49, 41, 76, 48, 27, 45, 46, 76, 27, 40, 30, 76, 46, 44, 47, 40, 37, 38, 31, 45, 45, 76, 38, 31, 33, 45, 76, 41, 32, 76, 45, 46, 41, 40, 31, 78, 76, 76, 76, 76, 76, 76, 76, 76, 19, 46, 27, 40, 30, 76, 35, 40, 76, 46, 34, 31, 76, 30, 31, 45, 31, 44, 46, 63, 76, 63, 76, 63, 76, 63, 76, 14, 31, 27, 44, 76, 46, 34, 31, 39, 64, 76, 41, 40, 76, 46, 34, 31, 76, 45, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 8, 27, 38, 32, 76, 45, 47, 40, 37, 76, 27, 76, 45, 34, 27, 46, 46, 31, 44, 31, 30, 76, 48, 35, 45, 27, 33, 31, 76, 38, 35, 31, 45, 64, 76, 49, 34, 41, 45, 31, 76, 32, 44, 41, 49, 40, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 1, 40, 30, 76, 49, 44, 35, 40, 37, 38, 31, 30, 76, 38, 35, 42, 64, 76, 27, 40, 30, 76, 45, 40, 31, 31, 44, 76, 41, 32, 76, 29, 41, 38, 30, 76, 29, 41, 39, 39, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 31, 38, 38, 76, 46, 34, 27, 46, 76, 35, 46, 45, 76, 45, 29, 47, 38, 42, 46, 41, 44, 76, 49, 31, 38, 38, 76, 46, 34, 41, 45, 31, 76, 42, 27, 45, 45, 35, 41, 40, 45, 76, 44, 31, 27, 30, 78, 76, 76, 76, 76, 76, 76, 76, 76, 23, 34, 35, 29, 34, 76, 51, 31, 46, 76, 45, 47, 44, 48, 35, 48, 31, 64, 76, 45, 46, 27, 39, 42, 31, 30, 76, 41, 40, 76, 46, 34, 31, 45, 31, 76, 38, 35, 32, 31, 38, 31, 45, 45, 76, 46, 34, 35, 40, 33, 45, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 34, 31, 76, 34, 27, 40, 30, 76, 46, 34, 27, 46, 76, 39, 41, 29, 37, 31, 30, 76, 46, 34, 31, 39, 64, 76, 27, 40, 30, 76, 46, 34, 31, 76, 34, 31, 27, 44, 46, 76, 46, 34, 27, 46, 76, 32, 31, 30, 66, 78, 76, 76, 76, 76, 76, 76, 76, 76, 1, 40, 30, 76, 41, 40, 76, 46, 34, 31, 76, 42, 31, 30, 31, 45, 46, 27, 38, 64, 76, 46, 34, 31, 45, 31, 76, 49, 41, 44, 30, 45, 76, 27, 42, 42, 31, 27, 44, 65, 78, 76, 76, 76, 76, 76, 76, 76, 76, 13, 51, 76, 40, 27, 39, 31, 76, 35, 45, 76, 15, 52, 51, 39, 27, 40, 30, 35, 27, 45, 64, 76, 11, 35, 40, 33, 76, 41, 32, 76, 11, 35, 40, 33, 45, 66, 78, 76, 76, 76, 76, 76, 76, 76, 76, 12, 41, 41, 37, 76, 41, 40, 76, 39, 51, 76, 23, 41, 44, 37, 45, 64, 76, 51, 31, 76, 13, 35, 33, 34, 46, 51, 64, 76, 27, 40, 30, 76, 30, 31, 45, 42, 27, 35, 44, 67, 78, 76, 76, 76, 76, 76, 76, 76, 76, 14, 41, 46, 34, 35, 40, 33, 76, 28, 31, 45, 35, 30, 31, 76, 44, 31, 39, 27, 35, 40, 45, 63, 76, 18, 41, 47, 40, 30, 76, 46, 34, 31, 76, 30, 31, 29, 27, 51, 78, 76, 76, 76, 76, 76, 76, 76, 76, 15, 32, 76, 46, 34, 27, 46, 76, 29, 41, 38, 41, 45, 45, 27, 38, 76, 23, 44, 31, 29, 37, 64, 76, 28, 41, 47, 40, 30, 38, 31, 45, 45, 76, 27, 40, 30, 76, 28, 27, 44, 31, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 34, 31, 76, 38, 41, 40, 31, 76, 27, 40, 30, 76, 38, 31, 48, 31, 38, 76, 45, 27, 40, 30, 45, 76, 45, 46, 44, 31, 46, 29, 34, 76, 32, 27, 44, 76, 27, 49, 27, 51, 78, 76, 76, 76, 76, 76, 76, 76, 76]] ), torch.tensor([[0, 0, 0, 10_69, 11]] ), torch.tensor([[0, 0, 0, 10_69, 11]] ), ] # fmt: on self.assertTrue(torch.allclose(tokens[0] ,EXPECTED_OUTPUT[0] ) ) self.assertTrue(torch.allclose(tokens[1] ,EXPECTED_OUTPUT[1] ) ) self.assertTrue(torch.allclose(tokens[2] ,EXPECTED_OUTPUT[2] ) ) @require_torch def lowerCAmelCase__ (self ) -> int: """simple docstring""" import torch lowerCAmelCase__ : int = JukeboxTokenizer.from_pretrained('''openai/jukebox-5b-lyrics''' ) lowerCAmelCase__ : Any = tokenizer(**self.metas )['''input_ids'''] # fmt: off lowerCAmelCase__ : int = [ torch.tensor([[ 0, 0, 0, 10_69, 11, -1, -1, -1, -1, 9, 77, 39, 31, 46, 77, 27, 77, 46, 44, 27, 48, 31, 38, 38, 31, 44, 77, 32, 44, 41, 39, 77, 27, 40, 77, 27, 40, 46, 35, 43, 47, 31, 77, 38, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 23, 34, 41, 77, 45, 27, 35, 30, 77, 72, 20, 49, 41, 77, 48, 27, 45, 46, 77, 27, 40, 30, 77, 46, 44, 47, 40, 37, 38, 31, 45, 45, 77, 38, 31, 33, 45, 77, 41, 32, 77, 45, 46, 41, 40, 31, 79, 77, 77, 77, 77, 77, 77, 77, 77, 19, 46, 27, 40, 30, 77, 35, 40, 77, 46, 34, 31, 77, 30, 31, 45, 31, 44, 46, 63, 77, 63, 77, 63, 77, 63, 77, 14, 31, 27, 44, 77, 46, 34, 31, 39, 64, 77, 41, 40, 77, 46, 34, 31, 77, 45, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 8, 27, 38, 32, 77, 45, 47, 40, 37, 77, 27, 77, 45, 34, 27, 46, 46, 31, 44, 31, 30, 77, 48, 35, 45, 27, 33, 31, 77, 38, 35, 31, 45, 64, 77, 49, 34, 41, 45, 31, 77, 32, 44, 41, 49, 40, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 1, 40, 30, 77, 49, 44, 35, 40, 37, 38, 31, 30, 77, 38, 35, 42, 64, 77, 27, 40, 30, 77, 45, 40, 31, 31, 44, 77, 41, 32, 77, 29, 41, 38, 30, 77, 29, 41, 39, 39, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 31, 38, 38, 77, 46, 34, 27, 46, 77, 35, 46, 45, 77, 45, 29, 47, 38, 42, 46, 41, 44, 77, 49, 31, 38, 38, 77, 46, 34, 41, 45, 31, 77, 42, 27, 45, 45, 35, 41, 40, 45, 77, 44, 31, 27, 30, 79, 77, 77, 77, 77, 77, 77, 77, 77, 23, 34, 35, 29, 34, 77, 51, 31, 46, 77, 45, 47, 44, 48, 35, 48, 31, 64, 77, 45, 46, 27, 39, 42, 31, 30, 77, 41, 40, 77, 46, 34, 31, 45, 31, 77, 38, 35, 32, 31, 38, 31, 45, 45, 77, 46, 34, 35, 40, 33, 45, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 34, 31, 77, 34, 27, 40, 30, 77, 46, 34, 27, 46, 77, 39, 41, 29, 37, 31, 30, 77, 46, 34, 31, 39, 64, 77, 27, 40, 30, 77, 46, 34, 31, 77, 34, 31, 27, 44, 46, 77, 46, 34, 27, 46, 77, 32, 31, 30, 66, 79, 77, 77, 77, 77, 77, 77, 77, 77, 1, 40, 30, 77, 41, 40, 77, 46, 34, 31, 77, 42, 31, 30, 31, 45, 46, 27, 38, 64, 77, 46, 34, 31, 45, 31, 77, 49, 41, 44, 30, 45, 77, 27, 42, 42, 31, 27, 44, 65, 79, 77, 77, 77, 77, 77, 77, 77, 77, 13, 51, 77, 40, 27, 39, 31, 77, 35, 45, 77, 15, 52, 51, 39, 27, 40, 30, 35, 27, 45, 64, 77, 11, 35, 40, 33, 77, 41, 32, 77, 11, 35, 40, 33, 45, 66, 79, 77, 77, 77, 77, 77, 77, 77, 77, 12, 41, 41, 37, 77, 41, 40, 77, 39, 51, 77, 23, 41, 44, 37, 45, 64, 77, 51, 31, 77, 13, 35, 33, 34, 46, 51, 64, 77, 27, 40, 30, 77, 30, 31, 45, 42, 27, 35, 44, 67, 79, 77, 77, 77, 77, 77, 77, 77, 77, 14, 41, 46, 34, 35, 40, 33, 77, 28, 31, 45, 35, 30, 31, 77, 44, 31, 39, 27, 35, 40, 45, 63, 77, 18, 41, 47, 40, 30, 77, 46, 34, 31, 77, 30, 31, 29, 27, 51, 79, 77, 77, 77, 77, 77, 77, 77, 77, 15, 32, 77, 46, 34, 27, 46, 77, 29, 41, 38, 41, 45, 45, 27, 38, 77, 23, 44, 31, 29, 37, 64, 77, 28, 41, 47, 40, 30, 38, 31, 45, 45, 77, 27, 40, 30, 77, 28, 27, 44, 31, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 34, 31, 77, 38, 41, 40, 31, 77, 27, 40, 30, 77, 38, 31, 48, 31, 38, 77, 45, 27, 40, 30, 45, 77, 45, 46, 44, 31, 46, 29, 34, 77, 32, 27, 44, 77, 27, 49, 27, 51, 79, 77, 77, 77, 77, 77, 77, 77, 77]] ), torch.tensor([[0, 0, 0, 10_69, 11, -1, -1, -1, -1]] ), torch.tensor([[0, 0, 0, 10_69, 11, -1, -1, -1, -1]] ), ] # fmt: on self.assertTrue(torch.allclose(tokens[0] ,EXPECTED_OUTPUT[0] ) ) self.assertTrue(torch.allclose(tokens[1] ,EXPECTED_OUTPUT[1] ) ) self.assertTrue(torch.allclose(tokens[2] ,EXPECTED_OUTPUT[2] ) )
94
from math import factorial def lowerCAmelCase__ ( lowerCamelCase_ : int = 100): '''simple docstring''' return sum(map(lowerCamelCase_ ,str(factorial(lowerCamelCase_)))) if __name__ == "__main__": print(solution(int(input('Enter the Number: ').strip())))
94
1
import itertools import os import random import tempfile import unittest import numpy as np from transformers import TvltFeatureExtractor, is_datasets_available from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch if is_datasets_available(): from datasets import load_dataset SCREAMING_SNAKE_CASE :Optional[int] = random.Random() def UpperCAmelCase ( a_ , a_=1.0 , a_=None , a_=None ) -> int: """simple docstring""" if rng is None: __A = global_rng __A = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values class UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __init__( self : Any ,A : Optional[int] ,A : List[Any]=7 ,A : List[Any]=4_00 ,A : List[Any]=20_00 ,A : Union[str, Any]=20_48 ,A : int=1_28 ,A : Optional[Any]=1 ,A : int=5_12 ,A : Any=30 ,A : List[str]=4_41_00 ,): __A = parent __A = batch_size __A = min_seq_length __A = max_seq_length __A = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) __A = spectrogram_length __A = feature_size __A = num_audio_channels __A = hop_length __A = chunk_length __A = sampling_rate def UpperCamelCase_ ( self : Any ): return { "spectrogram_length": self.spectrogram_length, "feature_size": self.feature_size, "num_audio_channels": self.num_audio_channels, "hop_length": self.hop_length, "chunk_length": self.chunk_length, "sampling_rate": self.sampling_rate, } def UpperCamelCase_ ( self : Optional[Any] ,A : Any=False ,A : str=False ): def _flatten(A : Optional[Any] ): return list(itertools.chain(*A ) ) if equal_length: __A = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size __A = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length ,self.max_seq_length ,self.seq_length_diff ) ] if numpify: __A = [np.asarray(A ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' snake_case_ = TvltFeatureExtractor def UpperCamelCase_ ( self : Tuple ): __A = TvltFeatureExtractionTester(self ) def UpperCamelCase_ ( self : List[Any] ): __A = self.feature_extraction_class(**self.feat_extract_dict ) self.assertTrue(hasattr(A ,"spectrogram_length" ) ) self.assertTrue(hasattr(A ,"feature_size" ) ) self.assertTrue(hasattr(A ,"num_audio_channels" ) ) self.assertTrue(hasattr(A ,"hop_length" ) ) self.assertTrue(hasattr(A ,"chunk_length" ) ) self.assertTrue(hasattr(A ,"sampling_rate" ) ) def UpperCamelCase_ ( self : List[Any] ): __A = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: __A = feat_extract_first.save_pretrained(A )[0] check_json_file_has_correct_format(A ) __A = self.feature_extraction_class.from_pretrained(A ) __A = feat_extract_first.to_dict() __A = feat_extract_second.to_dict() __A = dict_first.pop("mel_filters" ) __A = dict_second.pop("mel_filters" ) self.assertTrue(np.allclose(A ,A ) ) self.assertEqual(A ,A ) def UpperCamelCase_ ( self : List[str] ): __A = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: __A = os.path.join(A ,"feat_extract.json" ) feat_extract_first.to_json_file(A ) __A = self.feature_extraction_class.from_json_file(A ) __A = feat_extract_first.to_dict() __A = feat_extract_second.to_dict() __A = dict_first.pop("mel_filters" ) __A = dict_second.pop("mel_filters" ) self.assertTrue(np.allclose(A ,A ) ) self.assertEqual(A ,A ) def UpperCamelCase_ ( self : Optional[int] ): # Initialize feature_extractor __A = self.feature_extraction_class(**self.feat_extract_dict ) # create three inputs of length 800, 1000, and 1200 __A = [floats_list((1, x) )[0] for x in range(8_00 ,14_00 ,2_00 )] __A = [np.asarray(A ) for speech_input in speech_inputs] # Test not batched input __A = feature_extractor(np_speech_inputs[0] ,return_tensors="np" ,sampling_rate=4_41_00 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test batched __A = feature_extractor(A ,return_tensors="np" ,sampling_rate=4_41_00 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test audio masking __A = feature_extractor( A ,return_tensors="np" ,sampling_rate=4_41_00 ,mask_audio=A ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test 2-D numpy arrays are batched. __A = [floats_list((1, x) )[0] for x in (8_00, 8_00, 8_00)] __A = np.asarray(A ) __A = feature_extractor(A ,return_tensors="np" ,sampling_rate=4_41_00 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) def UpperCamelCase_ ( self : Optional[int] ,A : Any ): __A = load_dataset("hf-internal-testing/librispeech_asr_dummy" ,"clean" ,split="validation" ) # automatic decoding with librispeech __A = ds.sort("id" ).select(range(A ) )[:num_samples]["audio"] return [x["array"] for x in speech_samples] def UpperCamelCase_ ( self : List[str] ): __A = self._load_datasamples(1 ) __A = TvltFeatureExtractor() __A = feature_extractor(A ,return_tensors="pt" ).audio_values self.assertEquals(audio_values.shape ,(1, 1, 1_92, 1_28) ) __A = torch.tensor([[-0.30_32, -0.27_08], [-0.44_34, -0.40_07]] ) self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2] ,A ,atol=1E-4 ) )
15
def UpperCAmelCase ( a_ ) -> list: """simple docstring""" if len(a_ ) <= 1: return lst __A = 1 while i < len(a_ ): if lst[i - 1] <= lst[i]: i += 1 else: __A , __A = lst[i], lst[i - 1] i -= 1 if i == 0: __A = 1 return lst if __name__ == "__main__": SCREAMING_SNAKE_CASE :List[Any] = input('Enter numbers separated by a comma:\n').strip() SCREAMING_SNAKE_CASE :List[Any] = [int(item) for item in user_input.split(',')] print(gnome_sort(unsorted))
15
1
import argparse import copy def lowerCAmelCase__ ( a__ ) ->str: '''simple docstring''' _UpperCamelCase = {} with open(a__ ) as f: for line in f: if line.split()[0] not in dict_of_neighbours: _UpperCamelCase = [] _list.append([line.split()[1], line.split()[2]] ) _UpperCamelCase = _list else: dict_of_neighbours[line.split()[0]].append( [line.split()[1], line.split()[2]] ) if line.split()[1] not in dict_of_neighbours: _UpperCamelCase = [] _list.append([line.split()[0], line.split()[2]] ) _UpperCamelCase = _list else: dict_of_neighbours[line.split()[1]].append( [line.split()[0], line.split()[2]] ) return dict_of_neighbours def lowerCAmelCase__ ( a__ , a__ ) ->List[str]: '''simple docstring''' with open(a__ ) as f: _UpperCamelCase = f.read(1 ) _UpperCamelCase = start_node _UpperCamelCase = [] _UpperCamelCase = start_node _UpperCamelCase = 0 while visiting not in first_solution: _UpperCamelCase = 10_000 for k in dict_of_neighbours[visiting]: if int(k[1] ) < int(a__ ) and k[0] not in first_solution: _UpperCamelCase = k[1] _UpperCamelCase = k[0] first_solution.append(a__ ) _UpperCamelCase = distance_of_first_solution + int(a__ ) _UpperCamelCase = best_node first_solution.append(a__ ) _UpperCamelCase = 0 for k in dict_of_neighbours[first_solution[-2]]: if k[0] == start_node: break position += 1 _UpperCamelCase = ( distance_of_first_solution + int(dict_of_neighbours[first_solution[-2]][position][1] ) - 10_000 ) return first_solution, distance_of_first_solution def lowerCAmelCase__ ( a__ , a__ ) ->Optional[Any]: '''simple docstring''' _UpperCamelCase = [] for n in solution[1:-1]: _UpperCamelCase = solution.index(a__ ) for kn in solution[1:-1]: _UpperCamelCase = solution.index(a__ ) if n == kn: continue _UpperCamelCase = copy.deepcopy(a__ ) _UpperCamelCase = kn _UpperCamelCase = n _UpperCamelCase = 0 for k in _tmp[:-1]: _UpperCamelCase = _tmp[_tmp.index(a__ ) + 1] for i in dict_of_neighbours[k]: if i[0] == next_node: _UpperCamelCase = distance + int(i[1] ) _tmp.append(a__ ) if _tmp not in neighborhood_of_solution: neighborhood_of_solution.append(_tmp ) _UpperCamelCase = len(neighborhood_of_solution[0] ) - 1 neighborhood_of_solution.sort(key=lambda a__ : x[index_of_last_item_in_the_list] ) return neighborhood_of_solution def lowerCAmelCase__ ( a__ , a__ , a__ , a__ , a__ ) ->int: '''simple docstring''' _UpperCamelCase = 1 _UpperCamelCase = first_solution _UpperCamelCase = [] _UpperCamelCase = distance_of_first_solution _UpperCamelCase = solution while count <= iters: _UpperCamelCase = find_neighborhood(a__ , a__ ) _UpperCamelCase = 0 _UpperCamelCase = neighborhood[index_of_best_solution] _UpperCamelCase = len(a__ ) - 1 _UpperCamelCase = False while not found: _UpperCamelCase = 0 while i < len(a__ ): if best_solution[i] != solution[i]: _UpperCamelCase = best_solution[i] _UpperCamelCase = solution[i] break _UpperCamelCase = i + 1 if [first_exchange_node, second_exchange_node] not in tabu_list and [ second_exchange_node, first_exchange_node, ] not in tabu_list: tabu_list.append([first_exchange_node, second_exchange_node] ) _UpperCamelCase = True _UpperCamelCase = best_solution[:-1] _UpperCamelCase = neighborhood[index_of_best_solution][best_cost_index] if cost < best_cost: _UpperCamelCase = cost _UpperCamelCase = solution else: _UpperCamelCase = index_of_best_solution + 1 _UpperCamelCase = neighborhood[index_of_best_solution] if len(a__ ) >= size: tabu_list.pop(0 ) _UpperCamelCase = count + 1 return best_solution_ever, best_cost def lowerCAmelCase__ ( a__=None ) ->Dict: '''simple docstring''' _UpperCamelCase = generate_neighbours(args.File ) _UpperCamelCase , _UpperCamelCase = generate_first_solution( args.File , a__ ) _UpperCamelCase , _UpperCamelCase = tabu_search( a__ , a__ , a__ , args.Iterations , args.Size , ) print(f'Best solution: {best_sol}, with total distance: {best_cost}.' ) if __name__ == "__main__": lowerCamelCase__ = argparse.ArgumentParser(description='''Tabu Search''') parser.add_argument( '''-f''', '''--File''', type=str, help='''Path to the file containing the data''', required=True, ) parser.add_argument( '''-i''', '''--Iterations''', type=int, help='''How many iterations the algorithm should perform''', required=True, ) parser.add_argument( '''-s''', '''--Size''', type=int, help='''Size of the tabu list''', required=True ) # Pass the arguments to main method main(parser.parse_args())
63
from abc import ABC, abstractmethod from typing import Optional, Union from .. import Dataset, DatasetDict, Features, IterableDataset, IterableDatasetDict, NamedSplit from ..utils.typing import NestedDataStructureLike, PathLike class _UpperCAmelCase ( lowerCAmelCase ): '''simple docstring''' def __init__( self : List[str] , lowercase_ : Optional[NestedDataStructureLike[PathLike]] = None , lowercase_ : Optional[NamedSplit] = None , lowercase_ : Optional[Features] = None , lowercase_ : str = None , lowercase_ : bool = False , lowercase_ : bool = False , lowercase_ : Optional[int] = None , **lowercase_ : Dict , ) -> Tuple: """simple docstring""" _UpperCamelCase = path_or_paths _UpperCamelCase = split if split or isinstance(lowercase_ , lowercase_) else "train" _UpperCamelCase = features _UpperCamelCase = cache_dir _UpperCamelCase = keep_in_memory _UpperCamelCase = streaming _UpperCamelCase = num_proc _UpperCamelCase = kwargs @abstractmethod def __UpperCAmelCase ( self : Any) -> Union[Dataset, DatasetDict, IterableDataset, IterableDatasetDict]: """simple docstring""" pass class _UpperCAmelCase ( lowerCAmelCase ): '''simple docstring''' def __init__( self : List[Any] , lowercase_ : Optional[Features] = None , lowercase_ : str = None , lowercase_ : bool = False , lowercase_ : bool = False , lowercase_ : Optional[int] = None , **lowercase_ : Union[str, Any] , ) -> str: """simple docstring""" _UpperCamelCase = features _UpperCamelCase = cache_dir _UpperCamelCase = keep_in_memory _UpperCamelCase = streaming _UpperCamelCase = num_proc _UpperCamelCase = kwargs @abstractmethod def __UpperCAmelCase ( self : Any) -> Union[Dataset, IterableDataset]: """simple docstring""" pass
63
1
"""simple docstring""" import numpy as np from cva import COLOR_BGR2GRAY, CV_8UC3, cvtColor, filteraD, imread, imshow, waitKey def _snake_case ( UpperCamelCase : int , UpperCamelCase : int , UpperCamelCase : int , UpperCamelCase : int , UpperCamelCase : int , UpperCamelCase : int ): # prepare kernel # the kernel size have to be odd if (ksize % 2) == 0: UpperCAmelCase : int = ksize + 1 UpperCAmelCase : Any = np.zeros((ksize, ksize) , dtype=np.floataa ) # each value for y in range(UpperCamelCase ): for x in range(UpperCamelCase ): # distance from center UpperCAmelCase : int = x - ksize // 2 UpperCAmelCase : Optional[int] = y - ksize // 2 # degree to radiant UpperCAmelCase : List[str] = theta / 180 * np.pi UpperCAmelCase : str = np.cos(_theta ) UpperCAmelCase : Optional[Any] = np.sin(_theta ) # get kernel x UpperCAmelCase : List[Any] = cos_theta * px + sin_theta * py # get kernel y UpperCAmelCase : Any = -sin_theta * px + cos_theta * py # fill kernel UpperCAmelCase : Any = np.exp( -(_x**2 + gamma**2 * _y**2) / (2 * sigma**2) ) * np.cos(2 * np.pi * _x / lambd + psi ) return gabor if __name__ == "__main__": import doctest doctest.testmod() # read original image A: Any = imread("../image_data/lena.jpg") # turn image in gray scale value A: Optional[int] = cvtColor(img, COLOR_BGR2GRAY) # Apply multiple Kernel to detect edges A: List[str] = np.zeros(gray.shape[:2]) for theta in [0, 3_0, 6_0, 9_0, 1_2_0, 1_5_0]: A: List[str] = gabor_filter_kernel(1_0, 8, theta, 1_0, 0, 0) out += filteraD(gray, CV_8UC3, kernel_aa) A: str = out / out.max() * 2_5_5 A: List[Any] = out.astype(np.uinta) imshow("Original", gray) imshow("Gabor filter with 20x20 mask and 6 directions", out) waitKey(0)
109
"""simple docstring""" import os from typing import Any, Callable, Dict, List, Optional, Tuple, Union import torch from torch import nn from ...models.controlnet import ControlNetModel, ControlNetOutput from ...models.modeling_utils import ModelMixin from ...utils import logging __UpperCAmelCase = logging.get_logger(__name__) class _SCREAMING_SNAKE_CASE ( A__ ): def __init__( self , __A ) -> Optional[Any]: super().__init__() lowerCAmelCase_ :int = nn.ModuleList(__A ) def __lowerCAmelCase ( self , __A , __A , __A , __A , __A , __A = None , __A = None , __A = None , __A = None , __A = False , __A = True , ) -> Union[ControlNetOutput, Tuple]: for i, (image, scale, controlnet) in enumerate(zip(__A , __A , self.nets ) ): lowerCAmelCase_ , lowerCAmelCase_ :List[Any] = controlnet( __A , __A , __A , __A , __A , __A , __A , __A , __A , __A , __A , ) # merge samples if i == 0: lowerCAmelCase_ , lowerCAmelCase_ :Tuple = down_samples, mid_sample else: lowerCAmelCase_ :str = [ samples_prev + samples_curr for samples_prev, samples_curr in zip(__A , __A ) ] mid_block_res_sample += mid_sample return down_block_res_samples, mid_block_res_sample def __lowerCAmelCase ( self , __A , __A = True , __A = None , __A = False , __A = None , ) -> Optional[Any]: lowerCAmelCase_ :int = 0 lowerCAmelCase_ :Dict = save_directory for controlnet in self.nets: controlnet.save_pretrained( __A , is_main_process=__A , save_function=__A , safe_serialization=__A , variant=__A , ) idx += 1 lowerCAmelCase_ :Any = model_path_to_save + f"""_{idx}""" @classmethod def __lowerCAmelCase ( cls , __A , **__A ) -> List[Any]: lowerCAmelCase_ :int = 0 lowerCAmelCase_ :Dict = [] # load controlnet and append to list until no controlnet directory exists anymore # first controlnet has to be saved under `./mydirectory/controlnet` to be compliant with `DiffusionPipeline.from_prertained` # second, third, ... controlnets have to be saved under `./mydirectory/controlnet_1`, `./mydirectory/controlnet_2`, ... lowerCAmelCase_ :List[Any] = pretrained_model_path while os.path.isdir(__A ): lowerCAmelCase_ :Tuple = ControlNetModel.from_pretrained(__A , **__A ) controlnets.append(__A ) idx += 1 lowerCAmelCase_ :Dict = pretrained_model_path + f"""_{idx}""" logger.info(f"""{len(__A )} controlnets loaded from {pretrained_model_path}.""" ) if len(__A ) == 0: raise ValueError( f"""No ControlNets found under {os.path.dirname(__A )}. Expected at least {pretrained_model_path + "_0"}.""" ) return cls(__A )
84
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices __lowercase : List[str] = logging.get_logger(__name__) __lowercase : Union[str, Any] = { 'shi-labs/nat-mini-in1k-224': 'https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json', # See all Nat models at https://huggingface.co/models?filter=nat } class __UpperCamelCase ( lowerCAmelCase_ , lowerCAmelCase_ ): A_ = "nat" A_ = { "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self , __a=4 , __a=3 , __a=64 , __a=[3, 4, 6, 5] , __a=[2, 4, 8, 16] , __a=7 , __a=3.0 , __a=True , __a=0.0 , __a=0.0 , __a=0.1 , __a="gelu" , __a=0.02 , __a=1E-5 , __a=0.0 , __a=None , __a=None , **__a , ): '''simple docstring''' super().__init__(**_SCREAMING_SNAKE_CASE ) __a : Optional[Any] = patch_size __a : Optional[int] = num_channels __a : Any = embed_dim __a : List[Any] = depths __a : str = len(_SCREAMING_SNAKE_CASE ) __a : List[Any] = num_heads __a : Optional[int] = kernel_size __a : int = mlp_ratio __a : Optional[int] = qkv_bias __a : Union[str, Any] = hidden_dropout_prob __a : List[str] = attention_probs_dropout_prob __a : Dict = drop_path_rate __a : str = hidden_act __a : Any = layer_norm_eps __a : Dict = initializer_range # we set the hidden_size attribute in order to make Nat work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model __a : int = int(embed_dim * 2 ** (len(_SCREAMING_SNAKE_CASE ) - 1) ) __a : Optional[Any] = layer_scale_init_value __a : Union[str, Any] = ['''stem'''] + [f"""stage{idx}""" for idx in range(1 , len(_SCREAMING_SNAKE_CASE ) + 1 )] __a : Union[str, Any] = get_aligned_output_features_output_indices( out_features=_SCREAMING_SNAKE_CASE , out_indices=_SCREAMING_SNAKE_CASE , stage_names=self.stage_names )
371
'''simple docstring''' 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 ): def __init__( self , __a , __a=7 , __a=3 , __a=18 , __a=30 , __a=400 , __a=True , __a=None , __a=True , ): '''simple docstring''' __a : List[Any] = size if size is not None else {'height': 18, 'width': 18} __a : int = parent __a : Dict = batch_size __a : Optional[int] = num_channels __a : List[Any] = image_size __a : Tuple = min_resolution __a : str = max_resolution __a : str = do_resize __a : Optional[Any] = size __a : str = apply_ocr def __UpperCAmelCase ( self ): '''simple docstring''' return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class __UpperCamelCase ( lowerCAmelCase_ , unittest.TestCase ): A_ = LayoutLMvaImageProcessor if is_pytesseract_available() else None def __UpperCAmelCase ( self ): '''simple docstring''' __a : Dict = LayoutLMvaImageProcessingTester(self ) @property def __UpperCAmelCase ( self ): '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __UpperCAmelCase ( self ): '''simple docstring''' __a : str = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__a , 'do_resize' ) ) self.assertTrue(hasattr(__a , 'size' ) ) self.assertTrue(hasattr(__a , 'apply_ocr' ) ) def __UpperCAmelCase ( self ): '''simple docstring''' __a : Tuple = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'height': 18, 'width': 18} ) __a : Optional[int] = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'height': 42, 'width': 42} ) def __UpperCAmelCase ( self ): '''simple docstring''' pass def __UpperCAmelCase ( self ): '''simple docstring''' __a : Dict = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __a : Optional[int] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__a ) for image in image_inputs: self.assertIsInstance(__a , Image.Image ) # Test not batched input __a : Union[str, 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 , __a ) self.assertIsInstance(encoding.boxes , __a ) # Test batched __a : Any = image_processing(__a , 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 __UpperCAmelCase ( self ): '''simple docstring''' __a : int = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __a : Union[str, Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__a , numpify=__a ) for image in image_inputs: self.assertIsInstance(__a , np.ndarray ) # Test not batched input __a : Optional[Any] = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched __a : Tuple = image_processing(__a , 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 __UpperCAmelCase ( self ): '''simple docstring''' __a : str = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __a : List[str] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__a , torchify=__a ) for image in image_inputs: self.assertIsInstance(__a , torch.Tensor ) # Test not batched input __a : 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 __a : List[str] = image_processing(__a , 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 __UpperCAmelCase ( self ): '''simple docstring''' __a : List[str] = LayoutLMvaImageProcessor() from datasets import load_dataset __a : str = load_dataset('hf-internal-testing/fixtures_docvqa' , split='test' ) __a : Tuple = Image.open(ds[0]['file'] ).convert('RGB' ) __a : Optional[Any] = image_processing(__a , 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 __a : Optional[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 __a : Union[str, Any] = [[[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 , __a ) self.assertListEqual(encoding.boxes , __a ) # with apply_OCR = False __a : List[Any] = LayoutLMvaImageProcessor(apply_ocr=__a ) __a : List[Any] = image_processing(__a , return_tensors='pt' ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 224, 224) )
294
0
import unittest from transformers import EsmConfig, is_torch_available from transformers.testing_utils import TestCasePlus, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers.models.esm.modeling_esmfold import EsmForProteinFolding class a__ : """simple docstring""" def __init__( self : Dict , UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : Any=1_3 , UpperCAmelCase__ : Dict=7 , UpperCAmelCase__ : str=False , UpperCAmelCase__ : Any=True , UpperCAmelCase__ : List[Any]=False , UpperCAmelCase__ : List[str]=False , UpperCAmelCase__ : List[Any]=1_9 , UpperCAmelCase__ : Tuple=3_2 , UpperCAmelCase__ : Any=5 , UpperCAmelCase__ : Dict=4 , UpperCAmelCase__ : List[str]=3_7 , UpperCAmelCase__ : Tuple="gelu" , UpperCAmelCase__ : Union[str, Any]=0.1 , UpperCAmelCase__ : int=0.1 , UpperCAmelCase__ : str=5_1_2 , UpperCAmelCase__ : Any=1_6 , UpperCAmelCase__ : Any=2 , UpperCAmelCase__ : Optional[Any]=0.02 , UpperCAmelCase__ : str=3 , UpperCAmelCase__ : Tuple=4 , UpperCAmelCase__ : str=None , ) ->Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE : Optional[int] = parent SCREAMING_SNAKE_CASE : Optional[int] = batch_size SCREAMING_SNAKE_CASE : Any = seq_length SCREAMING_SNAKE_CASE : Tuple = is_training SCREAMING_SNAKE_CASE : int = use_input_mask SCREAMING_SNAKE_CASE : Optional[Any] = use_token_type_ids SCREAMING_SNAKE_CASE : List[str] = use_labels SCREAMING_SNAKE_CASE : Optional[int] = vocab_size SCREAMING_SNAKE_CASE : Optional[int] = hidden_size SCREAMING_SNAKE_CASE : Any = num_hidden_layers SCREAMING_SNAKE_CASE : Optional[Any] = num_attention_heads SCREAMING_SNAKE_CASE : Union[str, Any] = intermediate_size SCREAMING_SNAKE_CASE : Tuple = hidden_act SCREAMING_SNAKE_CASE : Optional[Any] = hidden_dropout_prob SCREAMING_SNAKE_CASE : Tuple = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : List[Any] = max_position_embeddings SCREAMING_SNAKE_CASE : List[Any] = type_vocab_size SCREAMING_SNAKE_CASE : Tuple = type_sequence_label_size SCREAMING_SNAKE_CASE : int = initializer_range SCREAMING_SNAKE_CASE : Dict = num_labels SCREAMING_SNAKE_CASE : List[str] = num_choices SCREAMING_SNAKE_CASE : str = scope def _lowercase ( self : Tuple ) ->str: """simple docstring""" SCREAMING_SNAKE_CASE : str = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE : str = None if self.use_input_mask: SCREAMING_SNAKE_CASE : List[str] = random_attention_mask([self.batch_size, self.seq_length] ) SCREAMING_SNAKE_CASE : Optional[int] = None SCREAMING_SNAKE_CASE : int = None SCREAMING_SNAKE_CASE : Tuple = None if self.use_labels: SCREAMING_SNAKE_CASE : str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE : Any = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) SCREAMING_SNAKE_CASE : str = ids_tensor([self.batch_size] , self.num_choices ) SCREAMING_SNAKE_CASE : Union[str, Any] = self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def _lowercase ( self : Any ) ->str: """simple docstring""" SCREAMING_SNAKE_CASE : Optional[Any] = EsmConfig( vocab_size=3_3 , hidden_size=self.hidden_size , pad_token_id=1 , 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 , is_folding_model=A_ , esmfold_config={"""trunk""": {"""num_blocks""": 2}, """fp16_esm""": False} , ) return config def _lowercase ( self : Optional[int] , UpperCAmelCase__ : Any , UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : Any , UpperCAmelCase__ : Dict , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Optional[Any] ) ->str: """simple docstring""" SCREAMING_SNAKE_CASE : Dict = EsmForProteinFolding(config=A_ ).float() model.to(A_ ) model.eval() SCREAMING_SNAKE_CASE : Union[str, Any] = model(A_ , attention_mask=A_ ) SCREAMING_SNAKE_CASE : Any = model(A_ ) SCREAMING_SNAKE_CASE : Optional[Any] = model(A_ ) self.parent.assertEqual(result.positions.shape , (8, self.batch_size, self.seq_length, 1_4, 3) ) self.parent.assertEqual(result.angles.shape , (8, self.batch_size, self.seq_length, 7, 2) ) def _lowercase ( self : Any ) ->str: """simple docstring""" SCREAMING_SNAKE_CASE : List[Any] = self.prepare_config_and_inputs() ( SCREAMING_SNAKE_CASE ) : Union[str, Any] = config_and_inputs SCREAMING_SNAKE_CASE : List[Any] = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class a__ ( _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : Any =False UpperCAmelCase__ : Union[str, Any] =(EsmForProteinFolding,) if is_torch_available() else () UpperCAmelCase__ : List[str] =() UpperCAmelCase__ : Union[str, Any] ={} if is_torch_available() else {} UpperCAmelCase__ : str =False def _lowercase ( self : int ) ->Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE : List[str] = EsmFoldModelTester(self ) SCREAMING_SNAKE_CASE : int = ConfigTester(self , config_class=A_ , hidden_size=3_7 ) def _lowercase ( self : Optional[Any] ) ->List[str]: """simple docstring""" self.config_tester.run_common_tests() def _lowercase ( self : str ) ->int: """simple docstring""" SCREAMING_SNAKE_CASE : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A_ ) @unittest.skip("""Does not support attention outputs""" ) def _lowercase ( self : Optional[int] ) ->Optional[Any]: """simple docstring""" pass @unittest.skip def _lowercase ( self : Tuple ) ->Dict: """simple docstring""" pass @unittest.skip("""Esm does not support embedding resizing""" ) def _lowercase ( self : str ) ->int: """simple docstring""" pass @unittest.skip("""Esm does not support embedding resizing""" ) def _lowercase ( self : str ) ->Optional[Any]: """simple docstring""" pass @unittest.skip("""ESMFold does not support passing input embeds!""" ) def _lowercase ( self : Tuple ) ->Optional[Any]: """simple docstring""" pass @unittest.skip("""ESMFold does not support head pruning.""" ) def _lowercase ( self : Any ) ->Union[str, Any]: """simple docstring""" pass @unittest.skip("""ESMFold does not support head pruning.""" ) def _lowercase ( self : List[Any] ) ->Optional[int]: """simple docstring""" pass @unittest.skip("""ESMFold does not support head pruning.""" ) def _lowercase ( self : Optional[int] ) ->Optional[Any]: """simple docstring""" pass @unittest.skip("""ESMFold does not support head pruning.""" ) def _lowercase ( self : List[Any] ) ->Optional[Any]: """simple docstring""" pass @unittest.skip("""ESMFold does not support head pruning.""" ) def _lowercase ( self : Dict ) ->List[Any]: """simple docstring""" pass @unittest.skip("""ESMFold does not output hidden states in the normal way.""" ) def _lowercase ( self : Any ) ->Any: """simple docstring""" pass @unittest.skip("""ESMfold does not output hidden states in the normal way.""" ) def _lowercase ( self : Dict ) ->str: """simple docstring""" pass @unittest.skip("""ESMFold only has one output format.""" ) def _lowercase ( self : List[Any] ) ->Dict: """simple docstring""" pass @unittest.skip("""This test doesn\'t work for ESMFold and doesn\'t test core functionality""" ) def _lowercase ( self : str ) ->Optional[Any]: """simple docstring""" pass @unittest.skip("""ESMFold does not support input chunking.""" ) def _lowercase ( self : Union[str, Any] ) ->List[str]: """simple docstring""" pass @unittest.skip("""ESMFold doesn\'t respect you and it certainly doesn\'t respect your initialization arguments.""" ) def _lowercase ( self : Any ) ->Optional[int]: """simple docstring""" pass @unittest.skip("""ESMFold doesn\'t support torchscript compilation.""" ) def _lowercase ( self : int ) ->Any: """simple docstring""" pass @unittest.skip("""ESMFold doesn\'t support torchscript compilation.""" ) def _lowercase ( self : Union[str, Any] ) ->Union[str, Any]: """simple docstring""" pass @unittest.skip("""ESMFold doesn\'t support torchscript compilation.""" ) def _lowercase ( self : Union[str, Any] ) ->Dict: """simple docstring""" pass @unittest.skip("""ESMFold doesn\'t support data parallel.""" ) def _lowercase ( self : Tuple ) ->Tuple: """simple docstring""" pass @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" ) def _lowercase ( self : Any ) ->List[str]: """simple docstring""" pass @require_torch class a__ ( _UpperCAmelCase ): """simple docstring""" @slow def _lowercase ( self : Optional[Any] ) ->List[str]: """simple docstring""" SCREAMING_SNAKE_CASE : Dict = EsmForProteinFolding.from_pretrained("""facebook/esmfold_v1""" ).float() model.eval() SCREAMING_SNAKE_CASE : Optional[Any] = torch.tensor([[0, 6, 4, 1_3, 5, 4, 1_6, 1_2, 1_1, 7, 2]] ) SCREAMING_SNAKE_CASE : List[str] = model(A_ )['''positions'''] SCREAMING_SNAKE_CASE : Any = torch.tensor([2.58_28, 0.79_93, -10.93_34] , dtype=torch.floataa ) self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0] , A_ , atol=1e-4 ) )
245
import itertools import os import random import tempfile import unittest import numpy as np from datasets import load_dataset from transformers import is_speech_available from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_speech_available(): from transformers import WhisperFeatureExtractor if is_torch_available(): import torch _UpperCamelCase = random.Random() def _lowercase ( lowercase__ , lowercase__=1.0 , lowercase__=None , lowercase__=None ): if rng is None: __lowerCAmelCase : Any = global_rng __lowerCAmelCase : str = [] 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 __lowercase (unittest.TestCase ): def __init__( self , A_ , A_=7 , A_=400 , A_=2000 , A_=10 , A_=160 , A_=8 , A_=0.0 , A_=4000 , A_=False , A_=True , ) ->List[str]: '''simple docstring''' __lowerCAmelCase : Optional[int] = parent __lowerCAmelCase : Dict = batch_size __lowerCAmelCase : str = min_seq_length __lowerCAmelCase : int = max_seq_length __lowerCAmelCase : Any = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) __lowerCAmelCase : Any = padding_value __lowerCAmelCase : str = sampling_rate __lowerCAmelCase : Optional[Any] = return_attention_mask __lowerCAmelCase : Optional[Any] = do_normalize __lowerCAmelCase : Optional[Any] = feature_size __lowerCAmelCase : Optional[int] = chunk_length __lowerCAmelCase : Optional[Any] = hop_length def UpperCamelCase__ ( self ) ->Optional[Any]: '''simple docstring''' return { "feature_size": self.feature_size, "hop_length": self.hop_length, "chunk_length": self.chunk_length, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "return_attention_mask": self.return_attention_mask, "do_normalize": self.do_normalize, } def UpperCamelCase__ ( self , A_=False , A_=False ) ->Optional[Any]: '''simple docstring''' def _flatten(A_ ): return list(itertools.chain(*A_ ) ) if equal_length: __lowerCAmelCase : str = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size __lowerCAmelCase : Any = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: __lowerCAmelCase : Optional[Any] = [np.asarray(A_ ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class __lowercase (_UpperCAmelCase , unittest.TestCase ): _UpperCamelCase = WhisperFeatureExtractor if is_speech_available() else None def UpperCamelCase__ ( self ) ->Optional[int]: '''simple docstring''' __lowerCAmelCase : Tuple = WhisperFeatureExtractionTester(self ) def UpperCamelCase__ ( self ) ->int: '''simple docstring''' __lowerCAmelCase : str = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: __lowerCAmelCase : List[str] = feat_extract_first.save_pretrained(A_ )[0] check_json_file_has_correct_format(A_ ) __lowerCAmelCase : int = self.feature_extraction_class.from_pretrained(A_ ) __lowerCAmelCase : Dict = feat_extract_first.to_dict() __lowerCAmelCase : Union[str, Any] = feat_extract_second.to_dict() __lowerCAmelCase : Union[str, Any] = feat_extract_first.mel_filters __lowerCAmelCase : Dict = feat_extract_second.mel_filters self.assertTrue(np.allclose(A_ , A_ ) ) self.assertEqual(A_ , A_ ) def UpperCamelCase__ ( self ) ->str: '''simple docstring''' __lowerCAmelCase : str = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: __lowerCAmelCase : Union[str, Any] = os.path.join(A_ , '''feat_extract.json''' ) feat_extract_first.to_json_file(A_ ) __lowerCAmelCase : List[str] = self.feature_extraction_class.from_json_file(A_ ) __lowerCAmelCase : List[str] = feat_extract_first.to_dict() __lowerCAmelCase : Tuple = feat_extract_second.to_dict() __lowerCAmelCase : Any = feat_extract_first.mel_filters __lowerCAmelCase : List[str] = feat_extract_second.mel_filters self.assertTrue(np.allclose(A_ , A_ ) ) self.assertEqual(A_ , A_ ) def UpperCamelCase__ ( self ) ->List[Any]: '''simple docstring''' __lowerCAmelCase : List[str] = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 __lowerCAmelCase : Dict = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] __lowerCAmelCase : Dict = [np.asarray(A_ ) for speech_input in speech_inputs] # Test feature size __lowerCAmelCase : Tuple = feature_extractor(A_ , padding='''max_length''' , return_tensors='''np''' ).input_features self.assertTrue(input_features.ndim == 3 ) self.assertTrue(input_features.shape[-1] == feature_extractor.nb_max_frames ) self.assertTrue(input_features.shape[-2] == feature_extractor.feature_size ) # Test not batched input __lowerCAmelCase : Dict = feature_extractor(speech_inputs[0] , return_tensors='''np''' ).input_features __lowerCAmelCase : List[str] = feature_extractor(np_speech_inputs[0] , return_tensors='''np''' ).input_features self.assertTrue(np.allclose(A_ , A_ , atol=1e-3 ) ) # Test batched __lowerCAmelCase : Union[str, Any] = feature_extractor(A_ , return_tensors='''np''' ).input_features __lowerCAmelCase : List[Any] = feature_extractor(A_ , return_tensors='''np''' ).input_features for enc_seq_a, enc_seq_a in zip(A_ , A_ ): self.assertTrue(np.allclose(A_ , A_ , atol=1e-3 ) ) # Test 2-D numpy arrays are batched. __lowerCAmelCase : int = [floats_list((1, x) )[0] for x in (800, 800, 800)] __lowerCAmelCase : Optional[int] = np.asarray(A_ ) __lowerCAmelCase : Dict = feature_extractor(A_ , return_tensors='''np''' ).input_features __lowerCAmelCase : Any = feature_extractor(A_ , return_tensors='''np''' ).input_features for enc_seq_a, enc_seq_a in zip(A_ , A_ ): self.assertTrue(np.allclose(A_ , A_ , atol=1e-3 ) ) # Test truncation required __lowerCAmelCase : Optional[int] = [floats_list((1, x) )[0] for x in range(200 , (feature_extractor.n_samples + 500) , 200 )] __lowerCAmelCase : Dict = [np.asarray(A_ ) for speech_input in speech_inputs] __lowerCAmelCase : Tuple = [x[: feature_extractor.n_samples] for x in speech_inputs] __lowerCAmelCase : Optional[int] = [np.asarray(A_ ) for speech_input in speech_inputs_truncated] __lowerCAmelCase : Any = feature_extractor(A_ , return_tensors='''np''' ).input_features __lowerCAmelCase : List[str] = feature_extractor(A_ , return_tensors='''np''' ).input_features for enc_seq_a, enc_seq_a in zip(A_ , A_ ): self.assertTrue(np.allclose(A_ , A_ , atol=1e-3 ) ) def UpperCamelCase__ ( self ) ->Dict: '''simple docstring''' import torch __lowerCAmelCase : Tuple = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) __lowerCAmelCase : List[Any] = np.random.rand(100 , 32 ).astype(np.floataa ) __lowerCAmelCase : Any = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: __lowerCAmelCase : Tuple = feature_extractor.pad([{'''input_features''': inputs}] , return_tensors='''np''' ) self.assertTrue(np_processed.input_features.dtype == np.floataa ) __lowerCAmelCase : int = feature_extractor.pad([{'''input_features''': inputs}] , return_tensors='''pt''' ) self.assertTrue(pt_processed.input_features.dtype == torch.floataa ) def UpperCamelCase__ ( self , A_ ) ->str: '''simple docstring''' __lowerCAmelCase : Any = load_dataset('''hf-internal-testing/librispeech_asr_dummy''' , '''clean''' , split='''validation''' ) # automatic decoding with librispeech __lowerCAmelCase : Union[str, Any] = ds.sort('''id''' ).select(range(A_ ) )[:num_samples]['''audio'''] return [x["array"] for x in speech_samples] def UpperCamelCase__ ( self ) ->int: '''simple docstring''' __lowerCAmelCase : Optional[int] = torch.tensor( [ 0.1_193, -0.0_946, -0.1_098, -0.0_196, 0.0_225, -0.0_690, -0.1_736, 0.0_951, 0.0_971, -0.0_817, -0.0_702, 0.0_162, 0.0_260, 0.0_017, -0.0_192, -0.1_678, 0.0_709, -0.1_867, -0.0_655, -0.0_274, -0.0_234, -0.1_884, -0.0_516, -0.0_554, -0.0_274, -0.1_425, -0.1_423, 0.0_837, 0.0_377, -0.0_854 ] ) # fmt: on __lowerCAmelCase : int = self._load_datasamples(1 ) __lowerCAmelCase : Any = WhisperFeatureExtractor() __lowerCAmelCase : Optional[Any] = feature_extractor(A_ , return_tensors='''pt''' ).input_features self.assertEqual(input_features.shape , (1, 80, 3000) ) self.assertTrue(torch.allclose(input_features[0, 0, :30] , A_ , atol=1e-4 ) ) def UpperCamelCase__ ( self ) ->Any: '''simple docstring''' __lowerCAmelCase : Tuple = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) __lowerCAmelCase : str = self._load_datasamples(1 )[0] __lowerCAmelCase : Optional[Any] = ((audio - audio.min()) / (audio.max() - audio.min())) * 6_5535 # Rescale to [0, 65535] to show issue __lowerCAmelCase : Union[str, Any] = feat_extract.zero_mean_unit_var_norm([audio] , attention_mask=A_ )[0] self.assertTrue(np.all(np.mean(A_ ) < 1e-3 ) ) self.assertTrue(np.all(np.abs(np.var(A_ ) - 1 ) < 1e-3 ) )
275
0
"""simple docstring""" import unittest from diffusers.models.unet_ad_blocks import * # noqa F403 from diffusers.utils import torch_device from .test_unet_blocks_common import UNetBlockTesterMixin class A_ (lowercase__ ,unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : int = DownBlockaD # noqa F405 SCREAMING_SNAKE_CASE__ : List[Any] = """down""" def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = [-0.02_32, -0.98_69, 0.80_54, -0.06_37, -0.16_88, -1.42_64, 0.44_70, -1.33_94, 0.09_04] super().test_output(lowercase_ ) class A_ (lowercase__ ,unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Any = ResnetDownsampleBlockaD # noqa F405 SCREAMING_SNAKE_CASE__ : int = """down""" def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Union[str, Any] = [0.07_10, 0.24_10, -0.73_20, -1.07_57, -1.13_43, 0.35_40, -0.01_33, -0.25_76, 0.09_48] super().test_output(lowercase_ ) class A_ (lowercase__ ,unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : int = AttnDownBlockaD # noqa F405 SCREAMING_SNAKE_CASE__ : List[str] = """down""" def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Optional[int] = [0.06_36, 0.89_64, -0.62_34, -1.01_31, 0.08_44, 0.49_35, 0.34_37, 0.09_11, -0.29_57] super().test_output(lowercase_ ) class A_ (lowercase__ ,unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : int = CrossAttnDownBlockaD # noqa F405 SCREAMING_SNAKE_CASE__ : List[str] = """down""" def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ : Dict = super().prepare_init_args_and_inputs_for_common() UpperCAmelCase_ : List[Any] = 32 return init_dict, inputs_dict def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Dict = [0.22_38, -0.73_96, -0.22_55, -0.38_29, 0.19_25, 1.16_65, 0.06_03, -0.72_95, 0.19_83] super().test_output(lowercase_ ) class A_ (lowercase__ ,unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[int] = SimpleCrossAttnDownBlockaD # noqa F405 SCREAMING_SNAKE_CASE__ : Dict = """down""" @property def UpperCamelCase__ ( self ): """simple docstring""" return super().get_dummy_input(include_encoder_hidden_states=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ : Any = super().prepare_init_args_and_inputs_for_common() UpperCAmelCase_ : Dict = 32 return init_dict, inputs_dict @unittest.skipIf(torch_device == "mps" , "MPS result is not consistent" ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = [0.79_21, -0.09_92, -0.19_62, -0.76_95, -0.42_42, 0.78_04, 0.47_37, 0.27_65, 0.33_38] super().test_output(lowercase_ ) class A_ (lowercase__ ,unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Any = SkipDownBlockaD # noqa F405 SCREAMING_SNAKE_CASE__ : Optional[Any] = """down""" @property def UpperCamelCase__ ( self ): """simple docstring""" return super().get_dummy_input(include_skip_sample=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : int = [-0.08_45, -0.20_87, -0.24_65, 0.09_71, 0.19_00, -0.04_84, 0.26_64, 0.41_79, 0.50_69] super().test_output(lowercase_ ) class A_ (lowercase__ ,unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Union[str, Any] = AttnSkipDownBlockaD # noqa F405 SCREAMING_SNAKE_CASE__ : int = """down""" @property def UpperCamelCase__ ( self ): """simple docstring""" return super().get_dummy_input(include_skip_sample=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = [0.55_39, 0.16_09, 0.49_24, 0.05_37, -0.19_95, 0.40_50, 0.09_79, -0.27_21, -0.06_42] super().test_output(lowercase_ ) class A_ (lowercase__ ,unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Dict = DownEncoderBlockaD # noqa F405 SCREAMING_SNAKE_CASE__ : Any = """down""" @property def UpperCamelCase__ ( self ): """simple docstring""" return super().get_dummy_input(include_temb=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : str = { "in_channels": 32, "out_channels": 32, } UpperCAmelCase_ : Tuple = self.dummy_input return init_dict, inputs_dict def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : List[str] = [1.11_02, 0.53_02, 0.48_72, -0.00_23, -0.80_42, 0.04_83, -0.34_89, -0.56_32, 0.76_26] super().test_output(lowercase_ ) class A_ (lowercase__ ,unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[int] = AttnDownEncoderBlockaD # noqa F405 SCREAMING_SNAKE_CASE__ : Tuple = """down""" @property def UpperCamelCase__ ( self ): """simple docstring""" return super().get_dummy_input(include_temb=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : List[str] = { "in_channels": 32, "out_channels": 32, } UpperCAmelCase_ : Union[str, Any] = self.dummy_input return init_dict, inputs_dict def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Dict = [0.89_66, -0.14_86, 0.85_68, 0.81_41, -0.90_46, -0.13_42, -0.09_72, -0.74_17, 0.15_38] super().test_output(lowercase_ ) class A_ (lowercase__ ,unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Union[str, Any] = UNetMidBlockaD # noqa F405 SCREAMING_SNAKE_CASE__ : str = """mid""" def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Dict = { "in_channels": 32, "temb_channels": 128, } UpperCAmelCase_ : Optional[int] = self.dummy_input return init_dict, inputs_dict def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Optional[int] = [-0.10_62, 1.72_48, 0.34_94, 1.45_69, -0.09_10, -1.24_21, -0.99_84, 0.67_36, 1.00_28] super().test_output(lowercase_ ) class A_ (lowercase__ ,unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : List[str] = UNetMidBlockaDCrossAttn # noqa F405 SCREAMING_SNAKE_CASE__ : Dict = """mid""" def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ : Optional[int] = super().prepare_init_args_and_inputs_for_common() UpperCAmelCase_ : Optional[Any] = 32 return init_dict, inputs_dict def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Any = [0.01_87, 2.42_20, 0.44_84, 1.12_03, -0.61_21, -1.51_22, -0.82_70, 0.78_51, 1.83_35] super().test_output(lowercase_ ) class A_ (lowercase__ ,unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[int] = UNetMidBlockaDSimpleCrossAttn # noqa F405 SCREAMING_SNAKE_CASE__ : Union[str, Any] = """mid""" @property def UpperCamelCase__ ( self ): """simple docstring""" return super().get_dummy_input(include_encoder_hidden_states=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ : List[str] = super().prepare_init_args_and_inputs_for_common() UpperCAmelCase_ : Any = 32 return init_dict, inputs_dict def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Dict = [0.71_43, 1.99_74, 0.54_48, 1.39_77, 0.12_82, -1.12_37, -1.42_38, 0.55_30, 0.88_80] super().test_output(lowercase_ ) class A_ (lowercase__ ,unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Union[str, Any] = UpBlockaD # noqa F405 SCREAMING_SNAKE_CASE__ : Optional[int] = """up""" @property def UpperCamelCase__ ( self ): """simple docstring""" return super().get_dummy_input(include_res_hidden_states_tuple=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Tuple = [-0.20_41, -0.41_65, -0.30_22, 0.00_41, -0.66_28, -0.70_53, 0.19_28, -0.03_25, 0.05_23] super().test_output(lowercase_ ) class A_ (lowercase__ ,unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[Any] = ResnetUpsampleBlockaD # noqa F405 SCREAMING_SNAKE_CASE__ : List[str] = """up""" @property def UpperCamelCase__ ( self ): """simple docstring""" return super().get_dummy_input(include_res_hidden_states_tuple=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : List[str] = [0.22_87, 0.35_49, -0.13_46, 0.47_97, -0.17_15, -0.96_49, 0.73_05, -0.58_64, -0.62_44] super().test_output(lowercase_ ) class A_ (lowercase__ ,unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[int] = CrossAttnUpBlockaD # noqa F405 SCREAMING_SNAKE_CASE__ : Tuple = """up""" @property def UpperCamelCase__ ( self ): """simple docstring""" return super().get_dummy_input(include_res_hidden_states_tuple=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ : List[str] = super().prepare_init_args_and_inputs_for_common() UpperCAmelCase_ : int = 32 return init_dict, inputs_dict def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Union[str, Any] = [-0.14_03, -0.35_15, -0.04_20, -0.14_25, 0.31_67, 0.50_94, -0.21_81, 0.59_31, 0.55_82] super().test_output(lowercase_ ) class A_ (lowercase__ ,unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : int = SimpleCrossAttnUpBlockaD # noqa F405 SCREAMING_SNAKE_CASE__ : Optional[int] = """up""" @property def UpperCamelCase__ ( self ): """simple docstring""" return super().get_dummy_input(include_res_hidden_states_tuple=lowercase_ , include_encoder_hidden_states=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ : Optional[int] = super().prepare_init_args_and_inputs_for_common() UpperCAmelCase_ : Optional[int] = 32 return init_dict, inputs_dict def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Union[str, Any] = [0.26_45, 0.14_80, 0.09_09, 0.80_44, -0.97_58, -0.90_83, 0.09_94, -1.14_53, -0.74_02] super().test_output(lowercase_ ) class A_ (lowercase__ ,unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : List[Any] = AttnUpBlockaD # noqa F405 SCREAMING_SNAKE_CASE__ : Dict = """up""" @property def UpperCamelCase__ ( self ): """simple docstring""" return super().get_dummy_input(include_res_hidden_states_tuple=lowercase_ ) @unittest.skipIf(torch_device == "mps" , "MPS result is not consistent" ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Optional[int] = [0.09_79, 0.13_26, 0.00_21, 0.06_59, 0.22_49, 0.00_59, 0.11_32, 0.59_52, 0.10_33] super().test_output(lowercase_ ) class A_ (lowercase__ ,unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[int] = SkipUpBlockaD # noqa F405 SCREAMING_SNAKE_CASE__ : str = """up""" @property def UpperCamelCase__ ( self ): """simple docstring""" return super().get_dummy_input(include_res_hidden_states_tuple=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : List[str] = [-0.08_93, -0.12_34, -0.15_06, -0.03_32, 0.01_23, -0.02_11, 0.05_66, 0.01_43, 0.03_62] super().test_output(lowercase_ ) class A_ (lowercase__ ,unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Dict = AttnSkipUpBlockaD # noqa F405 SCREAMING_SNAKE_CASE__ : Optional[Any] = """up""" @property def UpperCamelCase__ ( self ): """simple docstring""" return super().get_dummy_input(include_res_hidden_states_tuple=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : List[Any] = [0.03_61, 0.06_17, 0.27_87, -0.03_50, 0.03_42, 0.34_21, -0.08_43, 0.09_13, 0.30_15] super().test_output(lowercase_ ) class A_ (lowercase__ ,unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Any = UpDecoderBlockaD # noqa F405 SCREAMING_SNAKE_CASE__ : Tuple = """up""" @property def UpperCamelCase__ ( self ): """simple docstring""" return super().get_dummy_input(include_temb=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Any = {"in_channels": 32, "out_channels": 32} UpperCAmelCase_ : List[Any] = self.dummy_input return init_dict, inputs_dict def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Tuple = [0.44_04, 0.19_98, -0.98_86, -0.33_20, -0.31_28, -0.70_34, -0.69_55, -0.23_38, -0.31_37] super().test_output(lowercase_ ) class A_ (lowercase__ ,unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : int = AttnUpDecoderBlockaD # noqa F405 SCREAMING_SNAKE_CASE__ : str = """up""" @property def UpperCamelCase__ ( self ): """simple docstring""" return super().get_dummy_input(include_temb=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : int = {"in_channels": 32, "out_channels": 32} UpperCAmelCase_ : Optional[Any] = self.dummy_input return init_dict, inputs_dict def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Union[str, Any] = [0.67_38, 0.44_91, 0.10_55, 1.07_10, 0.73_16, 0.33_39, 0.33_52, 0.10_23, 0.35_68] super().test_output(lowercase_ )
23
"""simple docstring""" import doctest import logging import os import unittest from pathlib import Path from typing import List, Union import transformers from transformers.testing_utils import require_tf, require_torch, slow _a = logging.getLogger() @unittest.skip("""Temporarily disable the doc tests.""" ) @require_torch @require_tf @slow class A_ (unittest.TestCase ): '''simple docstring''' def UpperCamelCase__ ( self , lowercase_ , lowercase_ = None , lowercase_ = None , lowercase_ = None , lowercase_ = True , ): """simple docstring""" UpperCAmelCase_ : List[str] = [file for file in os.listdir(lowercase_ ) if os.path.isfile(os.path.join(lowercase_ , lowercase_ ) )] if identifier is not None: UpperCAmelCase_ : Dict = [file for file in files if identifier in file] if n_identifier is not None: if isinstance(lowercase_ , lowercase_ ): for n_ in n_identifier: UpperCAmelCase_ : str = [file for file in files if n_ not in file] else: UpperCAmelCase_ : Any = [file for file in files if n_identifier not in file] UpperCAmelCase_ : Union[str, Any] = ignore_files or [] ignore_files.append("__init__.py" ) UpperCAmelCase_ : Optional[int] = [file for file in files if file not in ignore_files] for file in files: # Open all files print("Testing" , lowercase_ ) if only_modules: UpperCAmelCase_ : str = file.split("." )[0] try: UpperCAmelCase_ : str = getattr(lowercase_ , lowercase_ ) UpperCAmelCase_ : Tuple = doctest.DocTestSuite(lowercase_ ) UpperCAmelCase_ : int = unittest.TextTestRunner().run(lowercase_ ) self.assertIs(len(result.failures ) , 0 ) except AttributeError: logger.info(F"""{module_identifier} is not a module.""" ) else: UpperCAmelCase_ : Optional[Any] = doctest.testfile(str(".." / directory / file ) , optionflags=doctest.ELLIPSIS ) self.assertIs(result.failed , 0 ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : int = Path("src/transformers" ) UpperCAmelCase_ : str = "modeling" UpperCAmelCase_ : Optional[Any] = [ "modeling_ctrl.py", "modeling_tf_ctrl.py", ] self.analyze_directory(lowercase_ , identifier=lowercase_ , ignore_files=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : List[Any] = Path("src/transformers" ) UpperCAmelCase_ : Any = "tokenization" self.analyze_directory(lowercase_ , identifier=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = Path("src/transformers" ) UpperCAmelCase_ : List[Any] = "configuration" self.analyze_directory(lowercase_ , identifier=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Union[str, Any] = Path("src/transformers" ) UpperCAmelCase_ : List[Any] = ["configuration", "modeling", "tokenization"] self.analyze_directory(lowercase_ , n_identifier=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Dict = Path("docs/source" ) UpperCAmelCase_ : Union[str, Any] = ["favicon.ico"] self.analyze_directory(lowercase_ , ignore_files=lowercase_ , only_modules=lowercase_ )
23
1
"""simple docstring""" __A = { "km/h": 1.0, "m/s": 3.6, "mph": 1.60_9344, "knot": 1.852, } __A = { "km/h": 1.0, "m/s": 0.2_7777_7778, "mph": 0.6_2137_1192, "knot": 0.5_3995_6803, } def __A (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ->float: """simple docstring""" if unit_to not in speed_chart or unit_from not in speed_chart_inverse: lowerCAmelCase__ :List[str] = ( F"Incorrect 'from_type' or 'to_type' value: {unit_from!r}, {unit_to!r}\n" F"Valid values are: {', '.join(_SCREAMING_SNAKE_CASE )}" ) raise ValueError(_SCREAMING_SNAKE_CASE ) return round(speed * speed_chart[unit_from] * speed_chart_inverse[unit_to] , 3 ) if __name__ == "__main__": import doctest doctest.testmod()
293
"""simple docstring""" from collections import Counter from pathlib import Path from typing import Optional, Tuple import yaml class _lowerCAmelCase ( yaml.SafeLoader ): """simple docstring""" def snake_case ( self , __UpperCAmelCase ): '''simple docstring''' lowerCAmelCase__ :List[Any] = [self.constructed_objects[key_node] for key_node, _ in node.value] lowerCAmelCase__ :str = [tuple(__UpperCAmelCase ) if isinstance(__UpperCAmelCase , __UpperCAmelCase ) else key for key in keys] lowerCAmelCase__ :Optional[int] = Counter(__UpperCAmelCase ) lowerCAmelCase__ :int = [key for key in counter if counter[key] > 1] if duplicate_keys: raise TypeError(F"Got duplicate yaml keys: {duplicate_keys}" ) def snake_case ( self , __UpperCAmelCase , __UpperCAmelCase=False ): '''simple docstring''' lowerCAmelCase__ :Union[str, Any] = super().construct_mapping(__UpperCAmelCase , deep=__UpperCAmelCase ) self._check_no_duplicates_on_constructed_node(__UpperCAmelCase ) return mapping def __A (_SCREAMING_SNAKE_CASE ) ->Tuple[Optional[str], str]: """simple docstring""" lowerCAmelCase__ :Optional[Any] = list(readme_content.splitlines() ) if full_content and full_content[0] == "---" and "---" in full_content[1:]: lowerCAmelCase__ :Optional[int] = full_content[1:].index('---' ) + 1 lowerCAmelCase__ :Union[str, Any] = '\n'.join(full_content[1:sep_idx] ) return yamlblock, "\n".join(full_content[sep_idx + 1 :] ) return None, "\n".join(_SCREAMING_SNAKE_CASE ) class _lowerCAmelCase ( a ): """simple docstring""" __magic_name__ :List[str] = {"""train_eval_index"""} # train-eval-index in the YAML metadata @classmethod def snake_case ( cls , __UpperCAmelCase ): '''simple docstring''' with open(__UpperCAmelCase , encoding='utf-8' ) as readme_file: lowerCAmelCase__ , lowerCAmelCase__ :Union[str, Any] = _split_yaml_from_readme(readme_file.read() ) if yaml_string is not None: return cls.from_yaml_string(__UpperCAmelCase ) else: return cls() def snake_case ( self , __UpperCAmelCase ): '''simple docstring''' if path.exists(): with open(__UpperCAmelCase , encoding='utf-8' ) as readme_file: lowerCAmelCase__ :Optional[Any] = readme_file.read() else: lowerCAmelCase__ :Union[str, Any] = None lowerCAmelCase__ :Union[str, Any] = self._to_readme(__UpperCAmelCase ) with open(__UpperCAmelCase , 'w' , encoding='utf-8' ) as readme_file: readme_file.write(__UpperCAmelCase ) def snake_case ( self , __UpperCAmelCase = None ): '''simple docstring''' if readme_content is not None: lowerCAmelCase__ , lowerCAmelCase__ :Optional[int] = _split_yaml_from_readme(__UpperCAmelCase ) lowerCAmelCase__ :Optional[Any] = '---\n' + self.to_yaml_string() + '---\n' + content else: lowerCAmelCase__ :str = '---\n' + self.to_yaml_string() + '---\n' return full_content @classmethod def snake_case ( cls , __UpperCAmelCase ): '''simple docstring''' lowerCAmelCase__ :Dict = yaml.load(__UpperCAmelCase , Loader=_NoDuplicateSafeLoader ) or {} # Convert the YAML keys to DatasetMetadata fields lowerCAmelCase__ :int = { (key.replace('-' , '_' ) if key.replace('-' , '_' ) in cls._FIELDS_WITH_DASHES else key): value for key, value in metadata_dict.items() } return cls(**__UpperCAmelCase ) def snake_case ( self ): '''simple docstring''' return yaml.safe_dump( { (key.replace('_' , '-' ) if key in self._FIELDS_WITH_DASHES else key): value for key, value in self.items() } , sort_keys=__UpperCAmelCase , allow_unicode=__UpperCAmelCase , encoding='utf-8' , ).decode('utf-8' ) __A = { """image-classification""": [], """translation""": [], """image-segmentation""": [], """fill-mask""": [], """automatic-speech-recognition""": [], """token-classification""": [], """sentence-similarity""": [], """audio-classification""": [], """question-answering""": [], """summarization""": [], """zero-shot-classification""": [], """table-to-text""": [], """feature-extraction""": [], """other""": [], """multiple-choice""": [], """text-classification""": [], """text-to-image""": [], """text2text-generation""": [], """zero-shot-image-classification""": [], """tabular-classification""": [], """tabular-regression""": [], """image-to-image""": [], """tabular-to-text""": [], """unconditional-image-generation""": [], """text-retrieval""": [], """text-to-speech""": [], """object-detection""": [], """audio-to-audio""": [], """text-generation""": [], """conversational""": [], """table-question-answering""": [], """visual-question-answering""": [], """image-to-text""": [], """reinforcement-learning""": [], """voice-activity-detection""": [], """time-series-forecasting""": [], """document-question-answering""": [], } if __name__ == "__main__": from argparse import ArgumentParser __A = ArgumentParser(usage="""Validate the yaml metadata block of a README.md file.""") ap.add_argument("""readme_filepath""") __A = ap.parse_args() __A = Path(args.readme_filepath) __A = DatasetMetadata.from_readme(readme_filepath) print(dataset_metadata) dataset_metadata.to_readme(readme_filepath)
293
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase__ : List[Any] = logging.get_logger(__name__) lowercase__ : Tuple = { 'microsoft/trocr-base-handwritten': ( 'https://huggingface.co/microsoft/trocr-base-handwritten/resolve/main/config.json' ), # See all TrOCR models at https://huggingface.co/models?filter=trocr } class __lowerCAmelCase ( __magic_name__ ): """simple docstring""" _snake_case : int = 'trocr' _snake_case : List[str] = ['past_key_values'] _snake_case : Tuple = { 'num_attention_heads': 'decoder_attention_heads', 'hidden_size': 'd_model', 'num_hidden_layers': 'decoder_layers', } def __init__( self : Tuple , lowerCAmelCase__ : Union[str, Any]=50265 , lowerCAmelCase__ : List[Any]=1024 , lowerCAmelCase__ : List[str]=12 , lowerCAmelCase__ : int=16 , lowerCAmelCase__ : List[Any]=4096 , lowerCAmelCase__ : Optional[int]="gelu" , lowerCAmelCase__ : int=512 , lowerCAmelCase__ : Optional[Any]=0.1 , lowerCAmelCase__ : Optional[int]=0.0 , lowerCAmelCase__ : Dict=0.0 , lowerCAmelCase__ : Tuple=2 , lowerCAmelCase__ : Dict=0.02 , lowerCAmelCase__ : Tuple=0.0 , lowerCAmelCase__ : int=True , lowerCAmelCase__ : Optional[Any]=False , lowerCAmelCase__ : Any=True , lowerCAmelCase__ : Dict=True , lowerCAmelCase__ : Optional[int]=1 , lowerCAmelCase__ : List[Any]=0 , lowerCAmelCase__ : Any=2 , **lowerCAmelCase__ : Optional[int] , ) -> Any: '''simple docstring''' _UpperCamelCase = vocab_size _UpperCamelCase = d_model _UpperCamelCase = decoder_layers _UpperCamelCase = decoder_attention_heads _UpperCamelCase = decoder_ffn_dim _UpperCamelCase = activation_function _UpperCamelCase = max_position_embeddings _UpperCamelCase = dropout _UpperCamelCase = attention_dropout _UpperCamelCase = activation_dropout _UpperCamelCase = init_std _UpperCamelCase = decoder_layerdrop _UpperCamelCase = use_cache _UpperCamelCase = scale_embedding _UpperCamelCase = use_learned_position_embeddings _UpperCamelCase = layernorm_embedding super().__init__( pad_token_id=lowerCAmelCase__ , bos_token_id=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__ , decoder_start_token_id=lowerCAmelCase__ , **lowerCAmelCase__ , )
287
'''simple docstring''' import argparse import os # New Code # import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils import find_executable_batch_size ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to ensure out-of-memory errors never # interrupt training, and builds off the `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## lowercase__ : str = 16 lowercase__ : int = 32 def a__ ( lowercase : Accelerator, lowercase : int = 16 ) -> List[str]: """simple docstring""" _UpperCamelCase = AutoTokenizer.from_pretrained('''bert-base-cased''' ) _UpperCamelCase = load_dataset('''glue''', '''mrpc''' ) def tokenize_function(lowercase : Union[str, Any] ): # max_length=None => use the model max length (it's actually the default) _UpperCamelCase = tokenizer(examples['''sentence1'''], examples['''sentence2'''], truncation=lowercase, max_length=lowercase ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): _UpperCamelCase = datasets.map( lowercase, batched=lowercase, remove_columns=['''idx''', '''sentence1''', '''sentence2'''], ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library _UpperCamelCase = tokenized_datasets.rename_column('''label''', '''labels''' ) def collate_fn(lowercase : Any ): # On TPU it's best to pad everything to the same length or training will be very slow. _UpperCamelCase = 128 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": _UpperCamelCase = 16 elif accelerator.mixed_precision != "no": _UpperCamelCase = 8 else: _UpperCamelCase = None return tokenizer.pad( lowercase, padding='''longest''', max_length=lowercase, pad_to_multiple_of=lowercase, return_tensors='''pt''', ) # Instantiate dataloaders. _UpperCamelCase = DataLoader( tokenized_datasets['''train'''], shuffle=lowercase, collate_fn=lowercase, batch_size=lowercase ) _UpperCamelCase = DataLoader( tokenized_datasets['''validation'''], shuffle=lowercase, collate_fn=lowercase, batch_size=lowercase ) return train_dataloader, eval_dataloader # For testing only if os.environ.get('TESTING_MOCKED_DATALOADERS', None) == "1": from accelerate.test_utils.training import mocked_dataloaders lowercase__ : str = mocked_dataloaders # noqa: F811 def a__ ( lowercase : List[Any], lowercase : List[str] ) -> Any: """simple docstring""" if os.environ.get('''TESTING_MOCKED_DATALOADERS''', lowercase ) == "1": _UpperCamelCase = 2 # Initialize accelerator _UpperCamelCase = Accelerator(cpu=args.cpu, mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs _UpperCamelCase = config['''lr'''] _UpperCamelCase = int(config['''num_epochs'''] ) _UpperCamelCase = int(config['''seed'''] ) _UpperCamelCase = int(config['''batch_size'''] ) _UpperCamelCase = evaluate.load('''glue''', '''mrpc''' ) # New Code # # We now can define an inner training loop function. It should take a batch size as the only parameter, # and build the dataloaders in there. # It also gets our decorator @find_executable_batch_size(starting_batch_size=lowercase ) def inner_training_loop(lowercase : List[Any] ): # And now just move everything below under this function # We need to bring in the Accelerator object from earlier nonlocal accelerator # And reset all of its attributes that could hold onto any memory: accelerator.free_memory() # Then we can declare the model, optimizer, and everything else: set_seed(lowercase ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) _UpperCamelCase = AutoModelForSequenceClassification.from_pretrained('''bert-base-cased''', return_dict=lowercase ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). _UpperCamelCase = model.to(accelerator.device ) # Instantiate optimizer _UpperCamelCase = AdamW(params=model.parameters(), lr=lowercase ) _UpperCamelCase , _UpperCamelCase = get_dataloaders(lowercase, lowercase ) # Instantiate scheduler _UpperCamelCase = get_linear_schedule_with_warmup( optimizer=lowercase, num_warmup_steps=100, num_training_steps=(len(lowercase ) * num_epochs), ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase = accelerator.prepare( lowercase, lowercase, lowercase, lowercase, lowercase ) # Now we train the model for epoch in range(lowercase ): model.train() for step, batch in enumerate(lowercase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) _UpperCamelCase = model(**lowercase ) _UpperCamelCase = outputs.loss accelerator.backward(lowercase ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(lowercase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): _UpperCamelCase = model(**lowercase ) _UpperCamelCase = outputs.logits.argmax(dim=-1 ) _UpperCamelCase , _UpperCamelCase = accelerator.gather_for_metrics((predictions, batch['''labels''']) ) metric.add_batch( predictions=lowercase, references=lowercase, ) _UpperCamelCase = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F"""epoch {epoch}:""", lowercase ) # New Code # # And call it at the end with no arguments # Note: You could also refactor this outside of your training loop function inner_training_loop() def a__ ( ) -> str: """simple docstring""" _UpperCamelCase = argparse.ArgumentParser(description='''Simple example of training script.''' ) parser.add_argument( '''--mixed_precision''', type=lowercase, default=lowercase, choices=['''no''', '''fp16''', '''bf16''', '''fp8'''], help='''Whether to use mixed precision. Choose''' '''between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.''' '''and an Nvidia Ampere GPU.''', ) parser.add_argument('''--cpu''', action='''store_true''', help='''If passed, will train on the CPU.''' ) _UpperCamelCase = parser.parse_args() _UpperCamelCase = {'''lr''': 2e-5, '''num_epochs''': 3, '''seed''': 42, '''batch_size''': 16} training_function(lowercase, lowercase ) if __name__ == "__main__": main()
287
1
import io import json import unittest from parameterized import parameterized from transformers import FSMTForConditionalGeneration, FSMTTokenizer from transformers.testing_utils import get_tests_dir, require_torch, slow, torch_device from utils import calculate_bleu lowercase__ :List[Any] = get_tests_dir() + "/test_data/fsmt/fsmt_val_data.json" with io.open(filename, "r", encoding="utf-8") as f: lowercase__ :Union[str, Any] = json.load(f) @require_torch class lowercase ( unittest.TestCase ): def A__ ( self ,A__): return FSMTTokenizer.from_pretrained(A__) def A__ ( self ,A__): lowercase = FSMTForConditionalGeneration.from_pretrained(A__).to(A__) if torch_device == "cuda": model.half() return model @parameterized.expand( [ ['''en-ru''', 26.0], ['''ru-en''', 22.0], ['''en-de''', 22.0], ['''de-en''', 29.0], ]) @slow def A__ ( self ,A__ ,A__): # note: this test is not testing the best performance since it only evals a small batch # but it should be enough to detect a regression in the output quality lowercase = f'facebook/wmt19-{pair}' lowercase = self.get_tokenizer(A__) lowercase = self.get_model(A__) lowercase = bleu_data[pair]['''src'''] lowercase = bleu_data[pair]['''tgt'''] lowercase = tokenizer(A__ ,return_tensors='''pt''' ,truncation=A__ ,padding='''longest''').to(A__) lowercase = model.generate( input_ids=batch.input_ids ,num_beams=8 ,) lowercase = tokenizer.batch_decode( A__ ,skip_special_tokens=A__ ,clean_up_tokenization_spaces=A__) lowercase = calculate_bleu(A__ ,A__) print(A__) self.assertGreaterEqual(scores['''bleu'''] ,A__)
101
import unittest from transformers import SPIECE_UNDERLINE, XLNetTokenizer, XLNetTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin lowercase__ :List[Any] = get_tests_dir("fixtures/test_sentencepiece.model") @require_sentencepiece @require_tokenizers class lowercase ( SCREAMING_SNAKE_CASE__ , unittest.TestCase ): lowercase_ : str =XLNetTokenizer lowercase_ : Dict =XLNetTokenizerFast lowercase_ : str =True lowercase_ : str =True def A__ ( self): super().setUp() # We have a SentencePiece fixture for testing lowercase = XLNetTokenizer(A__ ,keep_accents=A__) tokenizer.sanitize_special_tokens() tokenizer.save_pretrained(self.tmpdirname) def A__ ( self): lowercase = '''<s>''' lowercase = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(A__) ,A__) self.assertEqual(self.get_tokenizer()._convert_id_to_token(A__) ,A__) def A__ ( self): lowercase = list(self.get_tokenizer().get_vocab().keys()) self.assertEqual(vocab_keys[0] ,'''<unk>''') self.assertEqual(vocab_keys[1] ,'''<s>''') self.assertEqual(vocab_keys[-1] ,'''<eod>''') self.assertEqual(len(A__) ,1_0_0_6) def A__ ( self): self.assertEqual(self.get_tokenizer().vocab_size ,1_0_0_0) def A__ ( self): lowercase = XLNetTokenizer(A__ ,keep_accents=A__) lowercase = tokenizer.tokenize('''This is a test''') self.assertListEqual(A__ ,['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est''']) self.assertListEqual(tokenizer.convert_tokens_to_ids(A__) ,[2_8_5, 4_6, 1_0, 1_7_0, 3_8_2]) lowercase = tokenizer.tokenize('''I was born in 92000, and this is falsé.''') self.assertListEqual( A__ ,[ 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''', '''é''', '''.''', ] ,) lowercase = tokenizer.convert_tokens_to_ids(A__) self.assertListEqual(A__ ,[8, 2_1, 8_4, 5_5, 2_4, 1_9, 7, 0, 6_0_2, 3_4_7, 3_4_7, 3_4_7, 3, 1_2, 6_6, 4_6, 7_2, 8_0, 6, 0, 4]) lowercase = tokenizer.convert_ids_to_tokens(A__) self.assertListEqual( A__ ,[ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''<unk>''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''<unk>''', '''.''', ] ,) def A__ ( self): lowercase = XLNetTokenizer(A__ ,do_lower_case=A__) lowercase = tokenizer.tokenize('''I was born in 92000, and this is falsé.''') self.assertListEqual( A__ ,[ 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''', '''se''', '''.''', ] ,) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''') ,['''▁he''', '''ll''', '''o''']) def A__ ( self): lowercase = XLNetTokenizer(A__ ,do_lower_case=A__) lowercase = tokenizer.tokenize('''I was born in 92000, and this is falsé.''') self.assertListEqual( A__ ,[ 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''', '''se''', '''.''', ] ,) @slow def A__ ( self): lowercase = XLNetTokenizer.from_pretrained('''xlnet-base-cased''') lowercase = tokenizer.encode('''sequence builders''' ,add_special_tokens=A__) lowercase = tokenizer.encode('''multi-sequence build''' ,add_special_tokens=A__) lowercase = tokenizer.build_inputs_with_special_tokens(A__) lowercase = tokenizer.build_inputs_with_special_tokens(A__ ,A__) assert encoded_sentence == text + [4, 3] assert encoded_pair == text + [4] + text_a + [4, 3] @slow def A__ ( self): # fmt: off lowercase = {'''input_ids''': [[1_7, 2_1_4_4_2, 2_7_0, 1_7, 1_0, 1_4_6_4_5, 3_1_8, 3_4, 1_7, 4_5_4_6, 3_1_4_5, 7_8_7, 1_3, 7_7_5_2, 2_2_0_1_8, 2_3, 2_1, 1_7, 4_5_4_6, 3_1_4_5, 7_8_7, 1_3, 3_3_5_2, 1_4_4_3_1, 1_3, 5_5_0_0, 1_1, 1_1_7_6, 5_8_0, 1_3, 1_6_8_1_9, 4_7_9_7, 2_3, 1_7, 1_0, 1_7_1_3_5, 6_5_8, 1_9, 4_5_7, 7_9_3_2, 1_3, 1_8_4, 1_9, 3_1_5_4, 1_7_1_3_5, 6_4_6_8, 1_9, 1_4_0_4, 1_2_2_6_9, 1_9, 4_2_2_9, 5_3_5_6, 1_6_2_6_4, 4_6, 1_9, 1_7, 2_0_5_4_5, 1_0_3_9_5, 9, 9, 9, 1_1, 2_8, 6_4_2_1, 9_5_3_1, 2_0_7_2_9, 1_7, 1_0, 3_5_3, 1_7_0_2_2, 1_1, 2_1, 6_4_2_1, 9_5_3_1, 1_6_9_4_9, 1_7, 1_0, 1_1_5_0_9, 7_5_3, 1_1, 3_3, 9_5, 2_4_2_1, 7_3_8_5, 9_5_6, 1_4_4_3_1, 2_6_2_6, 2_5, 8_4_2, 7_3_8_5, 4_8_3_6, 2_1, 1_4_2_9, 2_2_7_2, 9_8_5_5, 3_1_2_0, 1_6_1, 2_4_7_3_8, 1_9, 1_3_2_0_3, 6_5_8, 2_1_8, 7_8_7, 2_1, 4_3_0, 1_8_4_8_2, 8_4_7, 2_6_3_7, 9, 4, 3], [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3_2_2, 2_2_1_7_8, 2_7, 1_0_6_4, 2_2, 9_5_6, 1_3, 1_1_1_0_1, 1_4_2_9, 5_8_5_4, 2_4_3_1_3, 1_8_9_5_3, 4_0, 4_2_2, 2_4_3_6_6, 6_8, 1_7_5_8, 3_7, 1_0_4_8_3, 1_4_2_5_7, 3_1, 2_0_7, 2_6_3, 2_1, 2_0_3, 3_7_7_3, 2_5, 7_1, 9_7_3_5, 9, 4, 3], [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3_2, 2_0_4_9, 3_4_4_2, 1_7, 1_3_8_9_4, 3_3_8_0, 2_3, 9_5, 1_8, 1_7_6_3_4, 2_2_8_8, 9, 4, 3]], '''token_type_ids''': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2], [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2], [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]], '''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], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 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, 0, 0, 0, 0, 0, 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]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=A__ ,model_name='''xlnet-base-cased''' ,revision='''c841166438c31ec7ca9a106dee7bb312b73ae511''' ,)
101
1
"""simple docstring""" from __future__ import annotations def _snake_case ( _snake_case : str , _snake_case : list[str] | None = None ): lowerCAmelCase : Union[str, Any] = word_bank or [] # create a table lowerCAmelCase : int = len(_snake_case ) + 1 lowerCAmelCase : list[list[list[str]]] = [] for _ in range(_snake_case ): table.append([] ) # seed value lowerCAmelCase : Any = [[]] # because empty string has empty combination # iterate through the indices for i in range(_snake_case ): # condition if table[i] != []: for word in word_bank: # slice condition if target[i : i + len(_snake_case )] == word: lowerCAmelCase : list[list[str]] = [ [word, *way] for way in table[i] ] # adds the word to every combination the current position holds # now,push that combination to the table[i+len(word)] table[i + len(_snake_case )] += new_combinations # combinations are in reverse order so reverse for better output for combination in table[len(_snake_case )]: combination.reverse() return table[len(_snake_case )] if __name__ == "__main__": print(all_construct('''jwajalapa''', ['''jwa''', '''j''', '''w''', '''a''', '''la''', '''lapa'''])) print(all_construct('''rajamati''', ['''s''', '''raj''', '''amat''', '''raja''', '''ma''', '''i''', '''t'''])) print( all_construct( '''hexagonosaurus''', ['''h''', '''ex''', '''hex''', '''ag''', '''ago''', '''ru''', '''auru''', '''rus''', '''go''', '''no''', '''o''', '''s'''], ) )
314
"""simple docstring""" import argparse import requests import torch from PIL import Image from transformers import SwinConfig, SwinForMaskedImageModeling, ViTImageProcessor def _snake_case ( _snake_case : List[str] ): lowerCAmelCase : Union[str, Any] = SwinConfig(image_size=192 ) if "base" in model_name: lowerCAmelCase : Union[str, Any] = 6 lowerCAmelCase : Any = 128 lowerCAmelCase : List[Any] = (2, 2, 18, 2) lowerCAmelCase : Any = (4, 8, 16, 32) elif "large" in model_name: lowerCAmelCase : Tuple = 12 lowerCAmelCase : Dict = 192 lowerCAmelCase : List[str] = (2, 2, 18, 2) lowerCAmelCase : Union[str, Any] = (6, 12, 24, 48) else: raise ValueError('''Model not supported, only supports base and large variants''' ) lowerCAmelCase : Optional[int] = window_size lowerCAmelCase : Any = embed_dim lowerCAmelCase : Optional[Any] = depths lowerCAmelCase : int = num_heads return config def _snake_case ( _snake_case : Union[str, Any] ): if "encoder.mask_token" in name: lowerCAmelCase : Dict = name.replace('''encoder.mask_token''' , '''embeddings.mask_token''' ) if "encoder.patch_embed.proj" in name: lowerCAmelCase : Union[str, Any] = name.replace('''encoder.patch_embed.proj''' , '''embeddings.patch_embeddings.projection''' ) if "encoder.patch_embed.norm" in name: lowerCAmelCase : Optional[Any] = name.replace('''encoder.patch_embed.norm''' , '''embeddings.norm''' ) if "attn.proj" in name: lowerCAmelCase : Optional[Any] = name.replace('''attn.proj''' , '''attention.output.dense''' ) if "attn" in name: lowerCAmelCase : List[str] = name.replace('''attn''' , '''attention.self''' ) if "norm1" in name: lowerCAmelCase : List[str] = name.replace('''norm1''' , '''layernorm_before''' ) if "norm2" in name: lowerCAmelCase : Optional[int] = name.replace('''norm2''' , '''layernorm_after''' ) if "mlp.fc1" in name: lowerCAmelCase : int = name.replace('''mlp.fc1''' , '''intermediate.dense''' ) if "mlp.fc2" in name: lowerCAmelCase : Optional[int] = name.replace('''mlp.fc2''' , '''output.dense''' ) if name == "encoder.norm.weight": lowerCAmelCase : Tuple = '''layernorm.weight''' if name == "encoder.norm.bias": lowerCAmelCase : str = '''layernorm.bias''' if "decoder" in name: pass else: lowerCAmelCase : Optional[Any] = '''swin.''' + name return name def _snake_case ( _snake_case : Optional[Any] , _snake_case : Optional[int] ): for key in orig_state_dict.copy().keys(): lowerCAmelCase : Optional[Any] = orig_state_dict.pop(_snake_case ) if "attn_mask" in key: pass elif "qkv" in key: lowerCAmelCase : List[Any] = key.split('''.''' ) lowerCAmelCase : Dict = int(key_split[2] ) lowerCAmelCase : Optional[Any] = int(key_split[4] ) lowerCAmelCase : List[str] = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: lowerCAmelCase : Dict = val[:dim, :] lowerCAmelCase : Dict = val[ dim : dim * 2, : ] lowerCAmelCase : int = val[-dim:, :] else: lowerCAmelCase : str = val[ :dim ] lowerCAmelCase : List[str] = val[ dim : dim * 2 ] lowerCAmelCase : Optional[Any] = val[ -dim: ] else: lowerCAmelCase : str = val return orig_state_dict def _snake_case ( _snake_case : List[str] , _snake_case : int , _snake_case : Dict , _snake_case : str ): lowerCAmelCase : List[str] = torch.load(_snake_case , map_location='''cpu''' )['''model'''] lowerCAmelCase : List[Any] = get_swin_config(_snake_case ) lowerCAmelCase : List[Any] = SwinForMaskedImageModeling(_snake_case ) model.eval() lowerCAmelCase : int = convert_state_dict(_snake_case , _snake_case ) model.load_state_dict(_snake_case ) lowerCAmelCase : str = '''http://images.cocodataset.org/val2017/000000039769.jpg''' lowerCAmelCase : Union[str, Any] = ViTImageProcessor(size={'''height''': 192, '''width''': 192} ) lowerCAmelCase : Union[str, Any] = Image.open(requests.get(_snake_case , stream=_snake_case ).raw ) lowerCAmelCase : str = image_processor(images=_snake_case , return_tensors='''pt''' ) with torch.no_grad(): lowerCAmelCase : Optional[Any] = model(**_snake_case ).logits print(outputs.keys() ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: print(f'''Saving model {model_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(_snake_case ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(_snake_case ) if push_to_hub: print(f'''Pushing model and image processor for {model_name} to hub''' ) model.push_to_hub(f'''microsoft/{model_name}''' ) image_processor.push_to_hub(f'''microsoft/{model_name}''' ) if __name__ == "__main__": snake_case__ : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''swin-base-simmim-window6-192''', type=str, choices=['''swin-base-simmim-window6-192''', '''swin-large-simmim-window12-192'''], help='''Name of the Swin SimMIM model you\'d like to convert.''', ) parser.add_argument( '''--checkpoint_path''', default='''/Users/nielsrogge/Documents/SwinSimMIM/simmim_pretrain__swin_base__img192_window6__100ep.pth''', 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 output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) snake_case__ : Dict = parser.parse_args() convert_swin_checkpoint(args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub)
314
1
'''simple docstring''' from typing import List, Optional, Tuple, Union import torch from torch import nn from torch.nn import CrossEntropyLoss from ... import AutoBackbone from ...modeling_outputs import SemanticSegmenterOutput from ...modeling_utils import PreTrainedModel from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings from ...utils.backbone_utils import BackboneMixin from .configuration_upernet import UperNetConfig lowercase : str = [ 'openmmlab/upernet-convnext-tiny', # See all UperNet models at https://huggingface.co/models?filter=upernet ] # General docstring lowercase : Optional[Any] = 'UperNetConfig' class A ( nn.Module ): def __init__( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = 0 , SCREAMING_SNAKE_CASE = False , SCREAMING_SNAKE_CASE = 1 , ) -> None: """simple docstring""" super().__init__() A : Optional[int] = nn.Convad( in_channels=SCREAMING_SNAKE_CASE , out_channels=SCREAMING_SNAKE_CASE , kernel_size=SCREAMING_SNAKE_CASE , padding=SCREAMING_SNAKE_CASE , bias=SCREAMING_SNAKE_CASE , dilation=SCREAMING_SNAKE_CASE , ) A : List[Any] = nn.BatchNormad(SCREAMING_SNAKE_CASE ) A : List[str] = nn.ReLU() def __lowerCAmelCase ( self , SCREAMING_SNAKE_CASE ) -> torch.Tensor: """simple docstring""" A : Tuple = self.conv(SCREAMING_SNAKE_CASE ) A : Optional[int] = self.batch_norm(SCREAMING_SNAKE_CASE ) A : Optional[Any] = self.activation(SCREAMING_SNAKE_CASE ) return output class A ( nn.Module ): def __init__( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) -> None: """simple docstring""" super().__init__() A : str = [ nn.AdaptiveAvgPoolad(SCREAMING_SNAKE_CASE ), UperNetConvModule(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , kernel_size=1 ), ] for i, layer in enumerate(self.layers ): self.add_module(str(SCREAMING_SNAKE_CASE ) , SCREAMING_SNAKE_CASE ) def __lowerCAmelCase ( self , SCREAMING_SNAKE_CASE ) -> torch.Tensor: """simple docstring""" A : Any = input for layer in self.layers: A : Union[str, Any] = layer(SCREAMING_SNAKE_CASE ) return hidden_state class A ( nn.Module ): def __init__( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) -> None: """simple docstring""" super().__init__() A : Dict = pool_scales A : Union[str, Any] = align_corners A : int = in_channels A : List[str] = channels A : List[Any] = [] for i, pool_scale in enumerate(SCREAMING_SNAKE_CASE ): A : str = UperNetPyramidPoolingBlock(pool_scale=SCREAMING_SNAKE_CASE , in_channels=SCREAMING_SNAKE_CASE , channels=SCREAMING_SNAKE_CASE ) self.blocks.append(SCREAMING_SNAKE_CASE ) self.add_module(str(SCREAMING_SNAKE_CASE ) , SCREAMING_SNAKE_CASE ) def __lowerCAmelCase ( self , SCREAMING_SNAKE_CASE ) -> List[torch.Tensor]: """simple docstring""" A : int = [] for ppm in self.blocks: A : Tuple = ppm(SCREAMING_SNAKE_CASE ) A : List[Any] = nn.functional.interpolate( SCREAMING_SNAKE_CASE , size=x.size()[2:] , mode='''bilinear''' , align_corners=self.align_corners ) ppm_outs.append(SCREAMING_SNAKE_CASE ) return ppm_outs class A ( nn.Module ): def __init__( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) -> Optional[Any]: """simple docstring""" super().__init__() A : List[str] = config A : Union[str, Any] = config.pool_scales # e.g. (1, 2, 3, 6) A : Optional[int] = in_channels A : List[str] = config.hidden_size A : str = False A : str = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) # PSP Module A : Optional[int] = UperNetPyramidPoolingModule( self.pool_scales , self.in_channels[-1] , self.channels , align_corners=self.align_corners , ) A : Union[str, Any] = UperNetConvModule( self.in_channels[-1] + len(self.pool_scales ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) # FPN Module A : Dict = nn.ModuleList() A : List[str] = nn.ModuleList() for in_channels in self.in_channels[:-1]: # skip the top layer A : int = UperNetConvModule(SCREAMING_SNAKE_CASE , self.channels , kernel_size=1 ) A : Union[str, Any] = UperNetConvModule(self.channels , self.channels , kernel_size=3 , padding=1 ) self.lateral_convs.append(SCREAMING_SNAKE_CASE ) self.fpn_convs.append(SCREAMING_SNAKE_CASE ) A : Dict = UperNetConvModule( len(self.in_channels ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) def __lowerCAmelCase ( self ) -> Optional[Any]: """simple docstring""" self.apply(self._init_weights ) def __lowerCAmelCase ( self , SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" if isinstance(SCREAMING_SNAKE_CASE , nn.Convad ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def __lowerCAmelCase ( self , SCREAMING_SNAKE_CASE ) -> List[str]: """simple docstring""" A : Union[str, Any] = inputs[-1] A : Optional[int] = [x] psp_outs.extend(self.psp_modules(SCREAMING_SNAKE_CASE ) ) A : str = torch.cat(SCREAMING_SNAKE_CASE , dim=1 ) A : str = self.bottleneck(SCREAMING_SNAKE_CASE ) return output def __lowerCAmelCase ( self , SCREAMING_SNAKE_CASE ) -> torch.Tensor: """simple docstring""" A : Union[str, Any] = [lateral_conv(encoder_hidden_states[i] ) for i, lateral_conv in enumerate(self.lateral_convs )] laterals.append(self.psp_forward(SCREAMING_SNAKE_CASE ) ) # build top-down path A : int = len(SCREAMING_SNAKE_CASE ) for i in range(used_backbone_levels - 1 , 0 , -1 ): A : Union[str, Any] = laterals[i - 1].shape[2:] A : Optional[Any] = laterals[i - 1] + nn.functional.interpolate( laterals[i] , size=SCREAMING_SNAKE_CASE , mode='''bilinear''' , align_corners=self.align_corners ) # build outputs A : List[str] = [self.fpn_convs[i](laterals[i] ) for i in range(used_backbone_levels - 1 )] # append psp feature fpn_outs.append(laterals[-1] ) for i in range(used_backbone_levels - 1 , 0 , -1 ): A : Any = nn.functional.interpolate( fpn_outs[i] , size=fpn_outs[0].shape[2:] , mode='''bilinear''' , align_corners=self.align_corners ) A : Tuple = torch.cat(SCREAMING_SNAKE_CASE , dim=1 ) A : Any = self.fpn_bottleneck(SCREAMING_SNAKE_CASE ) A : Optional[int] = self.classifier(SCREAMING_SNAKE_CASE ) return output class A ( nn.Module ): def __init__( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = 2 , SCREAMING_SNAKE_CASE = 3 , SCREAMING_SNAKE_CASE = 1 ) -> None: """simple docstring""" super().__init__() A : List[Any] = config A : Optional[Any] = config.auxiliary_in_channels A : Optional[Any] = config.auxiliary_channels A : Union[str, Any] = config.auxiliary_num_convs A : List[str] = config.auxiliary_concat_input A : Optional[Any] = in_index A : str = (kernel_size // 2) * dilation A : Tuple = [] convs.append( UperNetConvModule( self.in_channels , self.channels , kernel_size=SCREAMING_SNAKE_CASE , padding=SCREAMING_SNAKE_CASE , dilation=SCREAMING_SNAKE_CASE ) ) for i in range(self.num_convs - 1 ): convs.append( UperNetConvModule( self.channels , self.channels , kernel_size=SCREAMING_SNAKE_CASE , padding=SCREAMING_SNAKE_CASE , dilation=SCREAMING_SNAKE_CASE ) ) if self.num_convs == 0: A : Optional[Any] = nn.Identity() else: A : Tuple = nn.Sequential(*SCREAMING_SNAKE_CASE ) if self.concat_input: A : Any = UperNetConvModule( self.in_channels + self.channels , self.channels , kernel_size=SCREAMING_SNAKE_CASE , padding=kernel_size // 2 ) A : Optional[Any] = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) def __lowerCAmelCase ( self ) -> Any: """simple docstring""" self.apply(self._init_weights ) def __lowerCAmelCase ( self , SCREAMING_SNAKE_CASE ) -> Optional[Any]: """simple docstring""" if isinstance(SCREAMING_SNAKE_CASE , nn.Convad ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def __lowerCAmelCase ( self , SCREAMING_SNAKE_CASE ) -> torch.Tensor: """simple docstring""" A : Union[str, Any] = encoder_hidden_states[self.in_index] A : Optional[Any] = self.convs(SCREAMING_SNAKE_CASE ) if self.concat_input: A : Optional[Any] = self.conv_cat(torch.cat([hidden_states, output] , dim=1 ) ) A : str = self.classifier(SCREAMING_SNAKE_CASE ) return output class A ( __snake_case ): __magic_name__ = UperNetConfig __magic_name__ = '''pixel_values''' __magic_name__ = True def __lowerCAmelCase ( self , SCREAMING_SNAKE_CASE ) -> List[Any]: """simple docstring""" if isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): module.backbone.init_weights() module.decode_head.init_weights() module.auxiliary_head.init_weights() def __lowerCAmelCase ( self ) -> Optional[Any]: """simple docstring""" self.backbone.init_weights() self.decode_head.init_weights() self.auxiliary_head.init_weights() def __lowerCAmelCase ( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE=False ) -> List[str]: """simple docstring""" if isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): A : int = value lowercase : List[str] = R'\n Parameters:\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n config ([`UperNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n' lowercase : Union[str, Any] = R'\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using\n [`AutoImageProcessor`]. See [`SegformerImageProcessor.__call__`] for details.\n output_attentions (`bool`, *optional*):\n Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See\n `attentions` under returned tensors for more detail.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers of the backbone. See `hidden_states` under\n returned tensors for more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( '''UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes.''' , __snake_case , ) class A ( __snake_case ): def __init__( self , SCREAMING_SNAKE_CASE ) -> Any: """simple docstring""" super().__init__(SCREAMING_SNAKE_CASE ) A : List[Any] = AutoBackbone.from_config(config.backbone_config ) # Semantic segmentation head(s) A : List[Any] = UperNetHead(SCREAMING_SNAKE_CASE , in_channels=self.backbone.channels ) A : str = UperNetFCNHead(SCREAMING_SNAKE_CASE ) if config.use_auxiliary_head else None # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UPERNET_INPUTS_DOCSTRING.format('''batch_size, sequence_length''' ) ) @replace_return_docstrings(output_type=SCREAMING_SNAKE_CASE , config_class=_CONFIG_FOR_DOC ) def __lowerCAmelCase ( self , SCREAMING_SNAKE_CASE = None , SCREAMING_SNAKE_CASE = None , SCREAMING_SNAKE_CASE = None , SCREAMING_SNAKE_CASE = None , SCREAMING_SNAKE_CASE = None , ) -> Union[tuple, SemanticSegmenterOutput]: """simple docstring""" A : List[Any] = return_dict if return_dict is not None else self.config.use_return_dict A : Optional[Any] = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) A : Any = output_attentions if output_attentions is not None else self.config.output_attentions A : Optional[int] = self.backbone.forward_with_filtered_kwargs( SCREAMING_SNAKE_CASE , output_hidden_states=SCREAMING_SNAKE_CASE , output_attentions=SCREAMING_SNAKE_CASE ) A : int = outputs.feature_maps A : Any = self.decode_head(SCREAMING_SNAKE_CASE ) A : str = nn.functional.interpolate(SCREAMING_SNAKE_CASE , size=pixel_values.shape[2:] , mode='''bilinear''' , align_corners=SCREAMING_SNAKE_CASE ) A : List[Any] = None if self.auxiliary_head is not None: A : List[str] = self.auxiliary_head(SCREAMING_SNAKE_CASE ) A : Any = nn.functional.interpolate( SCREAMING_SNAKE_CASE , size=pixel_values.shape[2:] , mode='''bilinear''' , align_corners=SCREAMING_SNAKE_CASE ) A : Optional[Any] = None if labels is not None: if self.config.num_labels == 1: raise ValueError('''The number of labels should be greater than one''' ) else: # compute weighted loss A : Optional[int] = CrossEntropyLoss(ignore_index=self.config.loss_ignore_index ) A : str = loss_fct(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) A : List[str] = loss_fct(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) A : Optional[int] = main_loss + self.config.auxiliary_loss_weight * auxiliary_loss if not return_dict: if output_hidden_states: A : Optional[Any] = (logits,) + outputs[1:] else: A : Tuple = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return SemanticSegmenterOutput( loss=SCREAMING_SNAKE_CASE , logits=SCREAMING_SNAKE_CASE , hidden_states=outputs.hidden_states , attentions=outputs.attentions , )
3
'''simple docstring''' import unittest from transformers import AutoConfig, AutoTokenizer, BertConfig, TensorType, is_flax_available from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, slow if is_flax_available(): import jax from transformers.models.auto.modeling_flax_auto import FlaxAutoModel from transformers.models.bert.modeling_flax_bert import FlaxBertModel from transformers.models.roberta.modeling_flax_roberta import FlaxRobertaModel @require_flax class A ( unittest.TestCase ): @slow def __lowerCAmelCase ( self ) -> List[Any]: """simple docstring""" for model_name in ["bert-base-cased", "bert-large-uncased"]: with self.subTest(SCREAMING_SNAKE_CASE ): A : int = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE ) self.assertIsNotNone(SCREAMING_SNAKE_CASE ) self.assertIsInstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) A : List[str] = FlaxAutoModel.from_pretrained(SCREAMING_SNAKE_CASE ) self.assertIsNotNone(SCREAMING_SNAKE_CASE ) self.assertIsInstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) @slow def __lowerCAmelCase ( self ) -> int: """simple docstring""" for model_name in ["roberta-base", "roberta-large"]: with self.subTest(SCREAMING_SNAKE_CASE ): A : Any = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE ) self.assertIsNotNone(SCREAMING_SNAKE_CASE ) self.assertIsInstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) A : Any = FlaxAutoModel.from_pretrained(SCREAMING_SNAKE_CASE ) self.assertIsNotNone(SCREAMING_SNAKE_CASE ) self.assertIsInstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) @slow def __lowerCAmelCase ( self ) -> Any: """simple docstring""" for model_name in ["bert-base-cased", "bert-large-uncased"]: A : Optional[int] = AutoTokenizer.from_pretrained(SCREAMING_SNAKE_CASE ) A : List[str] = FlaxBertModel.from_pretrained(SCREAMING_SNAKE_CASE ) A : Optional[Any] = tokenizer('''Do you support jax jitted function?''' , return_tensors=TensorType.JAX ) @jax.jit def eval(**SCREAMING_SNAKE_CASE ): return model(**SCREAMING_SNAKE_CASE ) eval(**SCREAMING_SNAKE_CASE ).block_until_ready() @slow def __lowerCAmelCase ( self ) -> List[str]: """simple docstring""" for model_name in ["roberta-base", "roberta-large"]: A : List[str] = AutoTokenizer.from_pretrained(SCREAMING_SNAKE_CASE ) A : Union[str, Any] = FlaxRobertaModel.from_pretrained(SCREAMING_SNAKE_CASE ) A : int = tokenizer('''Do you support jax jitted function?''' , return_tensors=TensorType.JAX ) @jax.jit def eval(**SCREAMING_SNAKE_CASE ): return model(**SCREAMING_SNAKE_CASE ) eval(**SCREAMING_SNAKE_CASE ).block_until_ready() def __lowerCAmelCase ( self ) -> List[str]: """simple docstring""" with self.assertRaisesRegex( SCREAMING_SNAKE_CASE , '''bert-base is not a local folder and is not a valid model identifier''' ): A : List[Any] = FlaxAutoModel.from_pretrained('''bert-base''' ) def __lowerCAmelCase ( self ) -> Tuple: """simple docstring""" with self.assertRaisesRegex( SCREAMING_SNAKE_CASE , R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): A : Optional[int] = FlaxAutoModel.from_pretrained(SCREAMING_SNAKE_CASE , revision='''aaaaaa''' ) def __lowerCAmelCase ( self ) -> str: """simple docstring""" with self.assertRaisesRegex( SCREAMING_SNAKE_CASE , '''hf-internal-testing/config-no-model does not appear to have a file named flax_model.msgpack''' , ): A : List[str] = FlaxAutoModel.from_pretrained('''hf-internal-testing/config-no-model''' ) def __lowerCAmelCase ( self ) -> Optional[Any]: """simple docstring""" with self.assertRaisesRegex(SCREAMING_SNAKE_CASE , '''Use `from_pt=True` to load this model''' ): A : Any = FlaxAutoModel.from_pretrained('''hf-internal-testing/tiny-bert-pt-only''' )
3
1
import random import torch from huggingface_hub import HfApi from diffusers import UNetaDModel lowercase_ = HfApi() lowercase_ = {} # fmt: off lowercase_ = torch.tensor([ -0.7_515, -1.6_883, 0.2_420, 0.0_300, 0.6_347, 1.3_433, -1.1_743, -3.7_467, 1.2_342, -2.2_485, 0.4_636, 0.8_076, -0.7_991, 0.3_969, 0.8_498, 0.9_189, -1.8_887, -3.3_522, 0.7_639, 0.2_040, 0.6_271, -2.7_148, -1.6_316, 3.0_839, 0.3_186, 0.2_721, -0.9_759, -1.2_461, 2.6_257, 1.3_557 ]) lowercase_ = torch.tensor([ -2.3_639, -2.5_344, 0.0_054, -0.6_674, 1.5_990, 1.0_158, 0.3_124, -2.1_436, 1.8_795, -2.5_429, -0.1_566, -0.3_973, 1.2_490, 2.6_447, 1.2_283, -0.5_208, -2.8_154, -3.5_119, 2.3_838, 1.2_033, 1.7_201, -2.1_256, -1.4_576, 2.7_948, 2.4_204, -0.9_752, -1.2_546, 0.8_027, 3.2_758, 3.1_365 ]) lowercase_ = torch.tensor([ -0.6_531, -0.6_891, -0.3_172, -0.5_375, -0.9_140, -0.5_367, -0.1_175, -0.7_869, -0.3_808, -0.4_513, -0.2_098, -0.0_083, 0.3_183, 0.5_140, 0.2_247, -0.1_304, -0.1_302, -0.2_802, -0.2_084, -0.2_025, -0.4_967, -0.4_873, -0.0_861, 0.6_925, 0.0_250, 0.1_290, -0.1_543, 0.6_316, 1.0_460, 1.4_943 ]) lowercase_ = torch.tensor([ 0.0_911, 0.1_107, 0.0_182, 0.0_435, -0.0_805, -0.0_608, 0.0_381, 0.2_172, -0.0_280, 0.1_327, -0.0_299, -0.0_255, -0.0_050, -0.1_170, -0.1_046, 0.0_309, 0.1_367, 0.1_728, -0.0_533, -0.0_748, -0.0_534, 0.1_624, 0.0_384, -0.1_805, -0.0_707, 0.0_642, 0.0_220, -0.0_134, -0.1_333, -0.1_505 ]) lowercase_ = torch.tensor([ 0.1_321, 0.1_337, 0.0_440, 0.0_622, -0.0_591, -0.0_370, 0.0_503, 0.2_133, -0.0_177, 0.1_415, -0.0_116, -0.0_112, 0.0_044, -0.0_980, -0.0_789, 0.0_395, 0.1_502, 0.1_785, -0.0_488, -0.0_514, -0.0_404, 0.1_539, 0.0_454, -0.1_559, -0.0_665, 0.0_659, 0.0_383, -0.0_005, -0.1_266, -0.1_386 ]) lowercase_ = torch.tensor([ 0.1_154, 0.1_218, 0.0_307, 0.0_526, -0.0_711, -0.0_541, 0.0_366, 0.2_078, -0.0_267, 0.1_317, -0.0_226, -0.0_193, -0.0_014, -0.1_055, -0.0_902, 0.0_330, 0.1_391, 0.1_709, -0.0_562, -0.0_693, -0.0_560, 0.1_482, 0.0_381, -0.1_683, -0.0_681, 0.0_661, 0.0_331, -0.0_046, -0.1_268, -0.1_431 ]) lowercase_ = torch.tensor([ 0.1_192, 0.1_240, 0.0_414, 0.0_606, -0.0_557, -0.0_412, 0.0_430, 0.2_042, -0.0_200, 0.1_385, -0.0_115, -0.0_132, 0.0_017, -0.0_965, -0.0_802, 0.0_398, 0.1_433, 0.1_747, -0.0_458, -0.0_533, -0.0_407, 0.1_545, 0.0_419, -0.1_574, -0.0_645, 0.0_626, 0.0_341, -0.0_010, -0.1_199, -0.1_390 ]) lowercase_ = torch.tensor([ 0.1_075, 0.1_074, 0.0_205, 0.0_431, -0.0_774, -0.0_607, 0.0_298, 0.2_042, -0.0_320, 0.1_267, -0.0_281, -0.0_250, -0.0_064, -0.1_091, -0.0_946, 0.0_290, 0.1_328, 0.1_650, -0.0_580, -0.0_738, -0.0_586, 0.1_440, 0.0_337, -0.1_746, -0.0_712, 0.0_605, 0.0_250, -0.0_099, -0.1_316, -0.1_473 ]) lowercase_ = torch.tensor([ -1.4_572, -2.0_481, -0.0_414, -0.6_005, 1.4_136, 0.5_848, 0.4_028, -2.7_330, 1.2_212, -2.1_228, 0.2_155, 0.4_039, 0.7_662, 2.0_535, 0.7_477, -0.3_243, -2.1_758, -2.7_648, 1.6_947, 0.7_026, 1.2_338, -1.6_078, -0.8_682, 2.2_810, 1.8_574, -0.5_718, -0.5_586, -0.0_186, 2.3_415, 2.1_251]) lowercase_ = torch.tensor([ -1.3_690, -1.9_720, -0.4_090, -0.6_966, 1.4_660, 0.9_938, -0.1_385, -2.7_324, 0.7_736, -1.8_917, 0.2_923, 0.4_293, 0.1_693, 1.4_112, 1.1_887, -0.3_181, -2.2_160, -2.6_381, 1.3_170, 0.8_163, 0.9_240, -1.6_544, -0.6_099, 2.5_259, 1.6_430, -0.9_090, -0.9_392, -0.0_126, 2.4_268, 2.3_266 ]) lowercase_ = torch.tensor([ -1.3_525, -1.9_628, -0.3_956, -0.6_860, 1.4_664, 1.0_014, -0.1_259, -2.7_212, 0.7_772, -1.8_811, 0.2_996, 0.4_388, 0.1_704, 1.4_029, 1.1_701, -0.3_027, -2.2_053, -2.6_287, 1.3_350, 0.8_131, 0.9_274, -1.6_292, -0.6_098, 2.5_131, 1.6_505, -0.8_958, -0.9_298, -0.0_151, 2.4_257, 2.3_355 ]) lowercase_ = torch.tensor([ -2.0_585, -2.7_897, -0.2_850, -0.8_940, 1.9_052, 0.5_702, 0.6_345, -3.8_959, 1.5_932, -3.2_319, 0.1_974, 0.0_287, 1.7_566, 2.6_543, 0.8_387, -0.5_351, -3.2_736, -4.3_375, 2.9_029, 1.6_390, 1.4_640, -2.1_701, -1.9_013, 2.9_341, 3.4_981, -0.6_255, -1.1_644, -0.1_591, 3.7_097, 3.2_066 ]) lowercase_ = torch.tensor([ -2.3_139, -2.5_594, -0.0_197, -0.6_785, 1.7_001, 1.1_606, 0.3_075, -2.1_740, 1.8_071, -2.5_630, -0.0_926, -0.3_811, 1.2_116, 2.6_246, 1.2_731, -0.5_398, -2.8_153, -3.6_140, 2.3_893, 1.3_262, 1.6_258, -2.1_856, -1.3_267, 2.8_395, 2.3_779, -1.0_623, -1.2_468, 0.8_959, 3.3_367, 3.2_243 ]) lowercase_ = torch.tensor([ -2.0_628, -2.7_667, -0.2_089, -0.8_263, 2.0_539, 0.5_992, 0.6_495, -3.8_336, 1.6_025, -3.2_817, 0.1_721, -0.0_633, 1.7_516, 2.7_039, 0.8_100, -0.5_908, -3.2_113, -4.4_343, 2.9_257, 1.3_632, 1.5_562, -2.1_489, -1.9_894, 3.0_560, 3.3_396, -0.7_328, -1.0_417, 0.0_383, 3.7_093, 3.2_343 ]) lowercase_ = torch.tensor([ -1.4_574, -2.0_569, -0.0_473, -0.6_117, 1.4_018, 0.5_769, 0.4_129, -2.7_344, 1.2_241, -2.1_397, 0.2_000, 0.3_937, 0.7_616, 2.0_453, 0.7_324, -0.3_391, -2.1_746, -2.7_744, 1.6_963, 0.6_921, 1.2_187, -1.6_172, -0.8_877, 2.2_439, 1.8_471, -0.5_839, -0.5_605, -0.0_464, 2.3_250, 2.1_219 ]) # fmt: on lowercase_ = api.list_models(filter="diffusers") for mod in models: if "google" in mod.author or mod.modelId == "CompVis/ldm-celebahq-256": lowercase_ = "/home/patrick/google_checkpoints/" + mod.modelId.split("/")[-1] print(f"""Started running {mod.modelId}!!!""") if mod.modelId.startswith("CompVis"): lowercase_ = UNetaDModel.from_pretrained(local_checkpoint, subfolder="unet") else: lowercase_ = UNetaDModel.from_pretrained(local_checkpoint) torch.manual_seed(0) random.seed(0) lowercase_ = torch.randn(1, model.config.in_channels, model.config.sample_size, model.config.sample_size) lowercase_ = torch.tensor([10] * noise.shape[0]) with torch.no_grad(): lowercase_ = model(noise, time_step).sample assert torch.allclose( logits[0, 0, 0, :30], results["_".join("_".join(mod.modelId.split("/")).split("-"))], atol=1e-3 ) print(f"""{mod.modelId} has passed successfully!!!""")
282
import numpy as np from transformers import Pipeline def _snake_case( SCREAMING_SNAKE_CASE__ : int ) -> int: '''simple docstring''' A__ = np.max(SCREAMING_SNAKE_CASE__ , axis=-1 , keepdims=SCREAMING_SNAKE_CASE__ ) A__ = np.exp(outputs - maxes ) return shifted_exp / shifted_exp.sum(axis=-1 , keepdims=SCREAMING_SNAKE_CASE__ ) class A ( _UpperCAmelCase ): """simple docstring""" def snake_case__ ( self : Dict,**lowercase_ : Tuple )-> Tuple: '''simple docstring''' A__ = {} if "second_text" in kwargs: A__ = kwargs['second_text'] return preprocess_kwargs, {}, {} def snake_case__ ( self : List[Any],lowercase_ : int,lowercase_ : Optional[int]=None )-> List[str]: '''simple docstring''' return self.tokenizer(lowercase_,text_pair=lowercase_,return_tensors=self.framework ) def snake_case__ ( self : str,lowercase_ : Dict )-> List[str]: '''simple docstring''' return self.model(**lowercase_ ) def snake_case__ ( self : Dict,lowercase_ : Optional[int] )-> Dict: '''simple docstring''' A__ = model_outputs.logits[0].numpy() A__ = softmax(lowercase_ ) A__ = np.argmax(lowercase_ ) A__ = self.model.config.idalabel[best_class] A__ = probabilities[best_class].item() A__ = logits.tolist() return {"label": label, "score": score, "logits": logits}
282
1
"""simple docstring""" import warnings from ...utils import logging from .image_processing_beit import BeitImageProcessor __lowercase = logging.get_logger(__name__) class _A ( _a ): """simple docstring""" def __init__( self : List[Any] , *__UpperCAmelCase : Optional[Any] , **__UpperCAmelCase : Optional[Any]): warnings.warn( "The class BeitFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use BeitImageProcessor instead." , __UpperCAmelCase , ) super().__init__(*__UpperCAmelCase , **__UpperCAmelCase)
40
'''simple docstring''' import heapq as hq import math from collections.abc import Iterator class A__ : """simple docstring""" def __init__( self : Any , lowerCAmelCase__ : Any ) -> Dict: """simple docstring""" _UpperCAmelCase : Union[str, Any] = str(id_ ) _UpperCAmelCase : Union[str, Any] = None _UpperCAmelCase : Dict = None _UpperCAmelCase : Tuple = [] _UpperCAmelCase : int = {} # {vertex:distance} def __lt__( self : List[str] , lowerCAmelCase__ : str ) -> List[str]: """simple docstring""" return self.key < other.key def __repr__( self : int ) -> Any: """simple docstring""" return self.id def _lowerCAmelCase ( self : Union[str, Any] , lowerCAmelCase__ : Optional[Any] ) -> Any: """simple docstring""" self.neighbors.append(lowerCAmelCase__ ) def _lowerCAmelCase ( self : Any , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Optional[int] ) -> Tuple: """simple docstring""" _UpperCAmelCase : Optional[Any] = weight def __UpperCAmelCase ( a_: Any, a_: Optional[Any], a_: Optional[Any], a_: List[str] ): # add the neighbors: graph[a - 1].add_neighbor(graph[b - 1] ) graph[b - 1].add_neighbor(graph[a - 1] ) # add the edges: graph[a - 1].add_edge(graph[b - 1], a_ ) graph[b - 1].add_edge(graph[a - 1], a_ ) def __UpperCAmelCase ( a_: list, a_: Vertex ): _UpperCAmelCase : Optional[int] = [] for u in graph: _UpperCAmelCase : Dict = math.inf _UpperCAmelCase : Any = None _UpperCAmelCase : List[str] = 0 _UpperCAmelCase : Union[str, Any] = graph[:] while q: _UpperCAmelCase : List[Any] = min(a_ ) q.remove(a_ ) for v in u.neighbors: if (v in q) and (u.edges[v.id] < v.key): _UpperCAmelCase : Optional[Any] = u _UpperCAmelCase : List[Any] = u.edges[v.id] for i in range(1, len(a_ ) ): a.append((int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) ) return a def __UpperCAmelCase ( a_: list, a_: Vertex ): for u in graph: _UpperCAmelCase : Optional[Any] = math.inf _UpperCAmelCase : str = None _UpperCAmelCase : Optional[Any] = 0 _UpperCAmelCase : List[str] = list(a_ ) hq.heapify(a_ ) while h: _UpperCAmelCase : str = hq.heappop(a_ ) for v in u.neighbors: if (v in h) and (u.edges[v.id] < v.key): _UpperCAmelCase : Any = u _UpperCAmelCase : Optional[int] = u.edges[v.id] hq.heapify(a_ ) for i in range(1, len(a_ ) ): yield (int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) def __UpperCAmelCase ( ): pass if __name__ == "__main__": import doctest doctest.testmod()
145
0
"""simple docstring""" from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _lowerCAmelCase : Dict = { "configuration_autoformer": [ "AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "AutoformerConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCAmelCase : Tuple = [ "AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "AutoformerForPrediction", "AutoformerModel", "AutoformerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_autoformer import ( AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, AutoformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_autoformer import ( AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, AutoformerForPrediction, AutoformerModel, AutoformerPreTrainedModel, ) else: import sys _lowerCAmelCase : Dict = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
352
"""simple docstring""" def __snake_case ( SCREAMING_SNAKE_CASE__ : Dict=28_123 ) -> List[str]: '''simple docstring''' _UpperCAmelCase : List[Any] = [1] * (limit + 1) for i in range(2 , int(limit**0.5 ) + 1 ): sum_divs[i * i] += i for k in range(i + 1 , limit // i + 1 ): sum_divs[k * i] += k + i _UpperCAmelCase : Any = set() _UpperCAmelCase : List[str] = 0 for n in range(1 , limit + 1 ): if sum_divs[n] > n: abundants.add(SCREAMING_SNAKE_CASE__ ) if not any((n - a in abundants) for a in abundants ): res += n return res if __name__ == "__main__": print(solution())
202
0
"""simple docstring""" def lowercase_ ( __UpperCAmelCase = 1000 ) -> int: lowerCAmelCase__ : Optional[Any] = 2**power lowerCAmelCase__ : Optional[int] = str(__UpperCAmelCase ) lowerCAmelCase__ : Optional[int] = list(__UpperCAmelCase ) lowerCAmelCase__ : int = 0 for i in list_num: sum_of_num += int(__UpperCAmelCase ) return sum_of_num if __name__ == "__main__": _A = int(input("""Enter the power of 2: """).strip()) print("""2 ^ """, power, """ = """, 2**power) _A = solution(power) print("""Sum of the digits is: """, result)
242
"""simple docstring""" def lowercase_ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> int: def count_of_possible_combinations(__UpperCAmelCase ) -> int: if target < 0: return 0 if target == 0: return 1 return sum(count_of_possible_combinations(target - item ) for item in array ) return count_of_possible_combinations(__UpperCAmelCase ) def lowercase_ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> int: def count_of_possible_combinations_with_dp_array( __UpperCAmelCase , __UpperCAmelCase ) -> int: if target < 0: return 0 if target == 0: return 1 if dp_array[target] != -1: return dp_array[target] lowerCAmelCase__ : str = sum( count_of_possible_combinations_with_dp_array(target - item , __UpperCAmelCase ) for item in array ) lowerCAmelCase__ : List[str] = answer return answer lowerCAmelCase__ : Dict = [-1] * (target + 1) return count_of_possible_combinations_with_dp_array(__UpperCAmelCase , __UpperCAmelCase ) def lowercase_ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> int: lowerCAmelCase__ : int = [0] * (target + 1) lowerCAmelCase__ : int = 1 for i in range(1 , target + 1 ): for j in range(__UpperCAmelCase ): if i - array[j] >= 0: dp_array[i] += dp_array[i - array[j]] return dp_array[target] if __name__ == "__main__": import doctest doctest.testmod() _A = 3 _A = 5 _A = [1, 2, 5] print(combination_sum_iv(n, array, target))
242
1
"""simple docstring""" import os import re import shutil from argparse import ArgumentParser, Namespace from datasets.commands import BaseDatasetsCLICommand from datasets.utils.logging import get_logger lowerCAmelCase = '<<<<<<< This should probably be modified because it mentions: ' lowerCAmelCase = '=======\n>>>>>>>\n' lowerCAmelCase = [ 'TextEncoderConfig', 'ByteTextEncoder', 'SubwordTextEncoder', 'encoder_config', 'maybe_build_from_corpus', 'manual_dir', ] lowerCAmelCase = [ # (pattern, replacement) # Order is important here for some replacements (R'tfds\.core', R'datasets'), (R'tf\.io\.gfile\.GFile', R'open'), (R'tf\.([\w\d]+)', R'datasets.Value(\'\1\')'), (R'tfds\.features\.Text\(\)', R'datasets.Value(\'string\')'), (R'tfds\.features\.Text\(', R'datasets.Value(\'string\'),'), (R'features\s*=\s*tfds.features.FeaturesDict\(', R'features=datasets.Features('), (R'tfds\.features\.FeaturesDict\(', R'dict('), (R'The TensorFlow Datasets Authors', R'The TensorFlow Datasets Authors and the HuggingFace Datasets Authors'), (R'tfds\.', R'datasets.'), (R'dl_manager\.manual_dir', R'self.config.data_dir'), (R'self\.builder_config', R'self.config'), ] def _a ( SCREAMING_SNAKE_CASE ): """simple docstring""" return ConvertCommand(args.tfds_path , args.datasets_directory ) class _a ( UpperCamelCase__ ): @staticmethod def lowerCamelCase_ ( UpperCamelCase_: ArgumentParser ) -> int: """simple docstring""" lowercase__ = parser.add_parser( '''convert''' , help='''Convert a TensorFlow Datasets dataset to a HuggingFace Datasets dataset.''' , ) train_parser.add_argument( '''--tfds_path''' , type=UpperCamelCase_ , required=UpperCamelCase_ , help='''Path to a TensorFlow Datasets folder to convert or a single tfds file to convert.''' , ) train_parser.add_argument( '''--datasets_directory''' , type=UpperCamelCase_ , required=UpperCamelCase_ , help='''Path to the HuggingFace Datasets folder.''' ) train_parser.set_defaults(func=UpperCamelCase_ ) def __init__( self: Dict , UpperCamelCase_: str , UpperCamelCase_: str , *UpperCamelCase_: Union[str, Any] ) -> List[str]: """simple docstring""" lowercase__ = get_logger('''datasets-cli/converting''' ) lowercase__ = tfds_path lowercase__ = datasets_directory def lowerCamelCase_ ( self: str ) -> Dict: """simple docstring""" if os.path.isdir(self._tfds_path ): lowercase__ = os.path.abspath(self._tfds_path ) elif os.path.isfile(self._tfds_path ): lowercase__ = os.path.dirname(self._tfds_path ) else: raise ValueError('''--tfds_path is neither a directory nor a file. Please check path.''' ) lowercase__ = os.path.abspath(self._datasets_directory ) self._logger.info(f'Converting datasets from {abs_tfds_path} to {abs_datasets_path}' ) lowercase__ = [] lowercase__ = [] lowercase__ = {} if os.path.isdir(self._tfds_path ): lowercase__ = os.listdir(UpperCamelCase_ ) else: lowercase__ = [os.path.basename(self._tfds_path )] for f_name in file_names: self._logger.info(f'Looking at file {f_name}' ) lowercase__ = os.path.join(UpperCamelCase_ , UpperCamelCase_ ) lowercase__ = os.path.join(UpperCamelCase_ , UpperCamelCase_ ) if not os.path.isfile(UpperCamelCase_ ) or "__init__" in f_name or "_test" in f_name or ".py" not in f_name: self._logger.info('''Skipping file''' ) continue with open(UpperCamelCase_ , encoding='''utf-8''' ) as f: lowercase__ = f.readlines() lowercase__ = [] lowercase__ = False lowercase__ = False lowercase__ = [] for line in lines: lowercase__ = line # Convert imports if "import tensorflow.compat.v2 as tf" in out_line: continue elif "@tfds.core" in out_line: continue elif "builder=self" in out_line: continue elif "import tensorflow_datasets.public_api as tfds" in out_line: lowercase__ = '''import datasets\n''' elif "import tensorflow" in out_line: # order is important here lowercase__ = '''''' continue elif "from absl import logging" in out_line: lowercase__ = '''from datasets import logging\n''' elif "getLogger" in out_line: lowercase__ = out_line.replace('''getLogger''' , '''get_logger''' ) elif any(expression in out_line for expression in TO_HIGHLIGHT ): lowercase__ = True lowercase__ = list(filter(lambda UpperCamelCase_ : e in out_line , UpperCamelCase_ ) ) out_lines.append(HIGHLIGHT_MESSAGE_PRE + str(UpperCamelCase_ ) + '''\n''' ) out_lines.append(UpperCamelCase_ ) out_lines.append(UpperCamelCase_ ) continue else: for pattern, replacement in TO_CONVERT: lowercase__ = re.sub(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ) # Take care of saving utilities (to later move them together with main script) if "tensorflow_datasets" in out_line: lowercase__ = re.match(r'''from\stensorflow_datasets.*import\s([^\.\r\n]+)''' , UpperCamelCase_ ) tfds_imports.extend(imp.strip() for imp in match.group(1 ).split(''',''' ) ) lowercase__ = '''from . import ''' + match.group(1 ) # Check we have not forget anything if "tf." in out_line or "tfds." in out_line or "tensorflow_datasets" in out_line: raise ValueError(f'Error converting {out_line.strip()}' ) if "GeneratorBasedBuilder" in out_line or "BeamBasedBuilder" in out_line: lowercase__ = True out_lines.append(UpperCamelCase_ ) if is_builder or "wmt" in f_name: # We create a new directory for each dataset lowercase__ = f_name.replace('''.py''' , '''''' ) lowercase__ = os.path.join(UpperCamelCase_ , UpperCamelCase_ ) lowercase__ = os.path.join(UpperCamelCase_ , UpperCamelCase_ ) os.makedirs(UpperCamelCase_ , exist_ok=UpperCamelCase_ ) self._logger.info(f'Adding directory {output_dir}' ) imports_to_builder_map.update({imp: output_dir for imp in tfds_imports} ) else: # Utilities will be moved at the end utils_files.append(UpperCamelCase_ ) if needs_manual_update: with_manual_update.append(UpperCamelCase_ ) with open(UpperCamelCase_ , '''w''' , encoding='''utf-8''' ) as f: f.writelines(UpperCamelCase_ ) self._logger.info(f'Converted in {output_file}' ) for utils_file in utils_files: try: lowercase__ = os.path.basename(UpperCamelCase_ ) lowercase__ = imports_to_builder_map[f_name.replace('''.py''' , '''''' )] self._logger.info(f'Moving {dest_folder} to {utils_file}' ) shutil.copy(UpperCamelCase_ , UpperCamelCase_ ) except KeyError: self._logger.error(f'Cannot find destination folder for {utils_file}. Please copy manually.' ) if with_manual_update: for file_path in with_manual_update: self._logger.warning( f'You need to manually update file {file_path} to remove configurations using \'TextEncoderConfig\'.' )
358
import math_equivalence # From: git+https://github.com/hendrycks/math.git import datasets lowerCAmelCase = '\\n@article{hendrycksmath2021,\n title={Measuring Mathematical Problem Solving With the MATH Dataset},\n author={Dan Hendrycks\n and Collin Burns\n and Saurav Kadavath\n and Akul Arora\n and Steven Basart\n and Eric Tang\n and Dawn Song\n and Jacob Steinhardt},\n journal={arXiv preprint arXiv:2103.03874},\n year={2021}\n}\n' lowerCAmelCase = '\\nThis metric is used to assess performance on the Mathematics Aptitude Test of Heuristics (MATH) dataset.\nIt first canonicalizes the inputs (e.g., converting "1/2" to "\\frac{1}{2}") and then computes accuracy.\n' lowerCAmelCase = R'\nCalculates accuracy after canonicalizing inputs.\n\nArgs:\n predictions: list of predictions to score. Each prediction\n is a string that contains natural language and LaTex.\n references: list of reference for each prediction. Each\n reference is a string that contains natural language\n and LaTex.\nReturns:\n accuracy: accuracy after canonicalizing inputs\n (e.g., converting "1/2" to "\\frac{1}{2}")\n\nExamples:\n >>> metric = datasets.load_metric("competition_math")\n >>> results = metric.compute(references=["\\frac{1}{2}"], predictions=["1/2"])\n >>> print(results)\n {\'accuracy\': 1.0}\n' @datasets.utils.file_utils.add_end_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _a ( datasets.Metric ): def lowerCamelCase_ ( self: List[str] ) -> Tuple: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' ), '''references''': datasets.Value('''string''' ), } ) , homepage='''https://github.com/hendrycks/math''' , codebase_urls=['''https://github.com/hendrycks/math'''] , ) def lowerCamelCase_ ( self: Union[str, Any] , UpperCamelCase_: Any , UpperCamelCase_: int ) -> List[str]: """simple docstring""" lowercase__ = 0.0 for i, j in zip(UpperCamelCase_ , UpperCamelCase_ ): n_correct += 1.0 if math_equivalence.is_equiv(UpperCamelCase_ , UpperCamelCase_ ) else 0.0 lowercase__ = n_correct / len(UpperCamelCase_ ) return { "accuracy": accuracy, }
93
0
'''simple docstring''' def __lowercase ( __lowercase = 50 ) -> int: '''simple docstring''' _A = [[0] * 3 for _ in range(length + 1 )] for row_length in range(length + 1 ): for tile_length in range(2 , 5 ): for tile_start in range(row_length - tile_length + 1 ): different_colour_ways_number[row_length][tile_length - 2] += ( different_colour_ways_number[row_length - tile_start - tile_length][ tile_length - 2 ] + 1 ) return sum(different_colour_ways_number[length] ) if __name__ == "__main__": print(F"""{solution() = }""")
79
"""simple docstring""" from collections.abc import Iterable from typing import Generic, TypeVar _snake_case = TypeVar('_T') class UpperCamelCase ( Generic[_T] ): def __init__( self : Optional[int] , UpperCAmelCase__ : Iterable[_T] | None = None ) -> None: _a : list[_T] = list(iterable or [] ) _a : list[_T] = [] def __len__( self : str ) -> int: return len(self._stacka ) + len(self._stacka ) def __repr__( self : List[str] ) -> str: return f"""Queue({tuple(self._stacka[::-1] + self._stacka )})""" def _lowercase ( self : Union[str, Any] , UpperCAmelCase__ : _T ) -> None: self._stacka.append(UpperCAmelCase__ ) def _lowercase ( self : Optional[Any] ) -> _T: _a : Any = self._stacka.pop _a : Union[str, Any] = self._stacka.append if not self._stacka: while self._stacka: stacka_append(stacka_pop() ) if not self._stacka: raise IndexError("""Queue is empty""" ) return self._stacka.pop() if __name__ == "__main__": from doctest import testmod testmod()
294
0
import os from typing import Optional import fsspec from fsspec.archive import AbstractArchiveFileSystem from fsspec.utils import DEFAULT_BLOCK_SIZE class __A( a ): snake_case_ = '''''' snake_case_ = ( None # protocol passed in prefix to the url. ex: "gzip", for gzip://file.txt::http://foo.bar/file.txt.gz ) snake_case_ = None # compression type in fsspec. ex: "gzip" snake_case_ = None # extension of the filename to strip. ex: "".gz" to get file.txt from file.txt.gz def __init__( self , _snake_case = "" , _snake_case = None , _snake_case = None , **_snake_case ) -> int: '''simple docstring''' super().__init__(self , **_snake_case ) # always open as "rb" since fsspec can then use the TextIOWrapper to make it work for "r" mode __a = fsspec.open( _snake_case , mode='''rb''' , protocol=_snake_case , compression=self.compression , client_kwargs={ '''requote_redirect_url''': False, # see https://github.com/huggingface/datasets/pull/5459 '''trust_env''': True, # Enable reading proxy env variables. **(target_options or {}).pop('''client_kwargs''' , {} ), # To avoid issues if it was already passed. } , **(target_options or {}) , ) __a = os.path.basename(self.file.path.split('''::''' )[0] ) __a = ( self.compressed_name[: self.compressed_name.rindex('''.''' )] if '''.''' in self.compressed_name else self.compressed_name ) __a = None @classmethod def SCREAMING_SNAKE_CASE_ ( cls , _snake_case ) -> str: '''simple docstring''' return super()._strip_protocol(_snake_case ).lstrip('''/''' ) def SCREAMING_SNAKE_CASE_ ( self ) -> str: '''simple docstring''' if self.dir_cache is None: __a = {**self.file.fs.info(self.file.path ), '''name''': self.uncompressed_name} __a = {f['''name''']: f} def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> Union[str, Any]: '''simple docstring''' return self.file.open().read() def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case = "rb" , _snake_case=None , _snake_case=True , _snake_case=None , **_snake_case , ) -> Any: '''simple docstring''' __a = self._strip_protocol(_snake_case ) if mode != "rb": raise ValueError(F"""Tried to read with mode {mode} on file {self.file.path} opened with mode 'rb'""" ) return self.file.open() class __A( a ): snake_case_ = '''bz2''' snake_case_ = '''bz2''' snake_case_ = '''.bz2''' class __A( a ): snake_case_ = '''gzip''' snake_case_ = '''gzip''' snake_case_ = '''.gz''' class __A( a ): snake_case_ = '''lz4''' snake_case_ = '''lz4''' snake_case_ = '''.lz4''' class __A( a ): snake_case_ = '''xz''' snake_case_ = '''xz''' snake_case_ = '''.xz''' class __A( a ): snake_case_ = '''zstd''' snake_case_ = '''zstd''' snake_case_ = '''.zst''' def __init__( self , _snake_case , _snake_case = "rb" , _snake_case = None , _snake_case = None , _snake_case = DEFAULT_BLOCK_SIZE , **_snake_case , ) -> Optional[int]: '''simple docstring''' super().__init__( fo=_snake_case , mode=_snake_case , target_protocol=_snake_case , target_options=_snake_case , block_size=_snake_case , **_snake_case , ) # We need to wrap the zstd decompressor to avoid this error in fsspec==2021.7.0 and zstandard==0.15.2: # # File "/Users/user/.virtualenvs/hf-datasets/lib/python3.7/site-packages/fsspec/core.py", line 145, in open # out.close = close # AttributeError: 'zstd.ZstdDecompressionReader' object attribute 'close' is read-only # # see https://github.com/intake/filesystem_spec/issues/725 __a = self.file.__enter__ class __A: def __init__( self , _snake_case ) -> List[Any]: '''simple docstring''' __a = file_ def __enter__( self ) -> int: '''simple docstring''' self._file.__enter__() return self def __exit__( self , *_snake_case , **_snake_case ) -> Any: '''simple docstring''' self._file.__exit__(*_snake_case , **_snake_case ) def __iter__( self ) -> Tuple: '''simple docstring''' return iter(self._file ) def SCREAMING_SNAKE_CASE_ ( self ) -> List[Any]: '''simple docstring''' return next(self._file ) def __getattr__( self , _snake_case ) -> int: '''simple docstring''' return getattr(self._file , _snake_case ) def fixed_enter(*_snake_case , **_snake_case ): return WrappedFile(_enter(*_snake_case , **_snake_case ) ) __a = fixed_enter
33
from typing import List from .keymap import KEYMAP, get_character def __lowerCAmelCase ( a__ ) -> List[str]: def decorator(a__ ): __a = getattr(a__ , '''handle_key''' , [] ) handle += [key] setattr(a__ , '''handle_key''' , a__ ) return func return decorator def __lowerCAmelCase ( *a__ ) -> str: def decorator(a__ ): __a = getattr(a__ , '''handle_key''' , [] ) handle += keys setattr(a__ , '''handle_key''' , a__ ) return func return decorator class __A( a ): def __new__( cls , _snake_case , _snake_case , _snake_case ) -> Union[str, Any]: '''simple docstring''' __a = super().__new__(cls , _snake_case , _snake_case , _snake_case ) if not hasattr(_snake_case , '''key_handler''' ): setattr(_snake_case , '''key_handler''' , {} ) setattr(_snake_case , '''handle_input''' , KeyHandler.handle_input ) for value in attrs.values(): __a = getattr(_snake_case , '''handle_key''' , [] ) for key in handled_keys: __a = value return new_cls @staticmethod def SCREAMING_SNAKE_CASE_ ( cls ) -> List[str]: '''simple docstring''' __a = get_character() if char != KEYMAP["undefined"]: __a = ord(_snake_case ) __a = cls.key_handler.get(_snake_case ) if handler: __a = char return handler(cls ) else: return None def __lowerCAmelCase ( cls ) -> Union[str, Any]: return KeyHandler(cls.__name__ , cls.__bases__ , cls.__dict__.copy() )
33
1
'''simple docstring''' import unittest import numpy as np import torch from diffusers import PNDMPipeline, PNDMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class SCREAMING_SNAKE_CASE( unittest.TestCase ): """simple docstring""" @property def A ( self : List[Any] ) -> List[str]: torch.manual_seed(0 ) UpperCAmelCase : Optional[Any] = UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''') , up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''') , ) return model def A ( self : Union[str, Any] ) -> List[str]: UpperCAmelCase : List[Any] = self.dummy_uncond_unet UpperCAmelCase : Optional[int] = PNDMScheduler() UpperCAmelCase : Any = PNDMPipeline(unet=__snake_case , scheduler=__snake_case ) pndm.to(__snake_case ) pndm.set_progress_bar_config(disable=__snake_case ) UpperCAmelCase : Union[str, Any] = torch.manual_seed(0 ) UpperCAmelCase : Any = pndm(generator=__snake_case , num_inference_steps=20 , output_type='''numpy''' ).images UpperCAmelCase : Dict = torch.manual_seed(0 ) UpperCAmelCase : List[Any] = pndm(generator=__snake_case , num_inference_steps=20 , output_type='''numpy''' , return_dict=__snake_case )[0] UpperCAmelCase : Dict = image[0, -3:, -3:, -1] UpperCAmelCase : List[str] = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) UpperCAmelCase : Optional[Any] = np.array([1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 @slow @require_torch class SCREAMING_SNAKE_CASE( unittest.TestCase ): """simple docstring""" def A ( self : Optional[int] ) -> str: UpperCAmelCase : str = '''google/ddpm-cifar10-32''' UpperCAmelCase : Optional[Any] = UNetaDModel.from_pretrained(__snake_case ) UpperCAmelCase : List[str] = PNDMScheduler() UpperCAmelCase : int = PNDMPipeline(unet=__snake_case , scheduler=__snake_case ) pndm.to(__snake_case ) pndm.set_progress_bar_config(disable=__snake_case ) UpperCAmelCase : Tuple = torch.manual_seed(0 ) UpperCAmelCase : Any = pndm(generator=__snake_case , output_type='''numpy''' ).images UpperCAmelCase : str = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) UpperCAmelCase : List[Any] = np.array([0.15_64, 0.1_46_45, 0.14_06, 0.1_47_15, 0.1_24_25, 0.1_40_45, 0.1_31_15, 0.1_21_75, 0.1_25] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
23
'''simple docstring''' import random from .binary_exp_mod import bin_exp_mod def snake_case_ ( _lowerCAmelCase : Tuple , _lowerCAmelCase : Optional[Any]=1000 ) -> int: if n < 2: return False if n % 2 == 0: return n == 2 # this means n is odd UpperCAmelCase : str = n - 1 UpperCAmelCase : List[Any] = 0 while d % 2 == 0: d /= 2 exp += 1 # n - 1=d*(2**exp) UpperCAmelCase : List[str] = 0 while count < prec: UpperCAmelCase : int = random.randint(2 , n - 1 ) UpperCAmelCase : List[str] = bin_exp_mod(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) if b != 1: UpperCAmelCase : int = True for _ in range(_lowerCAmelCase ): if b == n - 1: UpperCAmelCase : Dict = False break UpperCAmelCase : str = b * b b %= n if flag: return False count += 1 return True if __name__ == "__main__": UpperCamelCase__: Optional[int] = abs(int(input("Enter bound : ").strip())) print("Here's the list of primes:") print(", ".join(str(i) for i in range(n + 1) if is_prime_big(i)))
23
1
'''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 ( BertTokenizer, ViltConfig, ViltForImageAndTextRetrieval, ViltForImagesAndTextClassification, ViltForMaskedLM, ViltForQuestionAnswering, ViltImageProcessor, ViltProcessor, ) from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase__ = logging.get_logger(__name__) def _A ( A__ , A__=False , A__=False , A__=False ): __lowercase = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"transformer.blocks.{i}.norm1.weight", F"vilt.encoder.layer.{i}.layernorm_before.weight") ) rename_keys.append((F"transformer.blocks.{i}.norm1.bias", F"vilt.encoder.layer.{i}.layernorm_before.bias") ) rename_keys.append( (F"transformer.blocks.{i}.attn.proj.weight", F"vilt.encoder.layer.{i}.attention.output.dense.weight") ) rename_keys.append( (F"transformer.blocks.{i}.attn.proj.bias", F"vilt.encoder.layer.{i}.attention.output.dense.bias") ) rename_keys.append((F"transformer.blocks.{i}.norm2.weight", F"vilt.encoder.layer.{i}.layernorm_after.weight") ) rename_keys.append((F"transformer.blocks.{i}.norm2.bias", F"vilt.encoder.layer.{i}.layernorm_after.bias") ) rename_keys.append( (F"transformer.blocks.{i}.mlp.fc1.weight", F"vilt.encoder.layer.{i}.intermediate.dense.weight") ) rename_keys.append((F"transformer.blocks.{i}.mlp.fc1.bias", F"vilt.encoder.layer.{i}.intermediate.dense.bias") ) rename_keys.append((F"transformer.blocks.{i}.mlp.fc2.weight", F"vilt.encoder.layer.{i}.output.dense.weight") ) rename_keys.append((F"transformer.blocks.{i}.mlp.fc2.bias", F"vilt.encoder.layer.{i}.output.dense.bias") ) # embeddings rename_keys.extend( [ # text embeddings ('''text_embeddings.word_embeddings.weight''', '''vilt.embeddings.text_embeddings.word_embeddings.weight'''), ( '''text_embeddings.position_embeddings.weight''', '''vilt.embeddings.text_embeddings.position_embeddings.weight''', ), ('''text_embeddings.position_ids''', '''vilt.embeddings.text_embeddings.position_ids'''), ( '''text_embeddings.token_type_embeddings.weight''', '''vilt.embeddings.text_embeddings.token_type_embeddings.weight''', ), ('''text_embeddings.LayerNorm.weight''', '''vilt.embeddings.text_embeddings.LayerNorm.weight'''), ('''text_embeddings.LayerNorm.bias''', '''vilt.embeddings.text_embeddings.LayerNorm.bias'''), # patch embeddings ('''transformer.cls_token''', '''vilt.embeddings.cls_token'''), ('''transformer.patch_embed.proj.weight''', '''vilt.embeddings.patch_embeddings.projection.weight'''), ('''transformer.patch_embed.proj.bias''', '''vilt.embeddings.patch_embeddings.projection.bias'''), ('''transformer.pos_embed''', '''vilt.embeddings.position_embeddings'''), # token type embeddings ('''token_type_embeddings.weight''', '''vilt.embeddings.token_type_embeddings.weight'''), ] ) # final layernorm + pooler rename_keys.extend( [ ('''transformer.norm.weight''', '''vilt.layernorm.weight'''), ('''transformer.norm.bias''', '''vilt.layernorm.bias'''), ('''pooler.dense.weight''', '''vilt.pooler.dense.weight'''), ('''pooler.dense.bias''', '''vilt.pooler.dense.bias'''), ] ) # classifier head(s) if vqa_model: # classification head rename_keys.extend( [ ('''vqa_classifier.0.weight''', '''classifier.0.weight'''), ('''vqa_classifier.0.bias''', '''classifier.0.bias'''), ('''vqa_classifier.1.weight''', '''classifier.1.weight'''), ('''vqa_classifier.1.bias''', '''classifier.1.bias'''), ('''vqa_classifier.3.weight''', '''classifier.3.weight'''), ('''vqa_classifier.3.bias''', '''classifier.3.bias'''), ] ) elif nlvr_model: # classification head rename_keys.extend( [ ('''nlvr2_classifier.0.weight''', '''classifier.0.weight'''), ('''nlvr2_classifier.0.bias''', '''classifier.0.bias'''), ('''nlvr2_classifier.1.weight''', '''classifier.1.weight'''), ('''nlvr2_classifier.1.bias''', '''classifier.1.bias'''), ('''nlvr2_classifier.3.weight''', '''classifier.3.weight'''), ('''nlvr2_classifier.3.bias''', '''classifier.3.bias'''), ] ) else: pass return rename_keys def _A ( A__ , A__ ): for i in range(config.num_hidden_layers ): __lowercase = '''vilt.''' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) __lowercase = state_dict.pop(F"transformer.blocks.{i}.attn.qkv.weight" ) __lowercase = state_dict.pop(F"transformer.blocks.{i}.attn.qkv.bias" ) # next, add query, keys and values (in that order) to the state dict __lowercase = in_proj_weight[ : config.hidden_size, : ] __lowercase = in_proj_bias[: config.hidden_size] __lowercase = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] __lowercase = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] __lowercase = in_proj_weight[ -config.hidden_size :, : ] __lowercase = in_proj_bias[-config.hidden_size :] def _A ( A__ ): __lowercase = ['''head.weight''', '''head.bias'''] for k in ignore_keys: state_dict.pop(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _A ( A__ , A__ , A__ ): __lowercase = dct.pop(SCREAMING_SNAKE_CASE_ ) __lowercase = val @torch.no_grad() def _A ( A__ , A__ ): __lowercase = ViltConfig(image_size=384 , patch_size=32 , tie_word_embeddings=SCREAMING_SNAKE_CASE_ ) __lowercase = False __lowercase = False __lowercase = False __lowercase = False if "vqa" in checkpoint_url: __lowercase = True __lowercase = 3129 __lowercase = '''huggingface/label-files''' __lowercase = '''vqa2-id2label.json''' __lowercase = json.load(open(hf_hub_download(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , repo_type='''dataset''' ) , '''r''' ) ) __lowercase = {int(SCREAMING_SNAKE_CASE_ ): v for k, v in idalabel.items()} __lowercase = idalabel __lowercase = {v: k for k, v in idalabel.items()} __lowercase = ViltForQuestionAnswering(SCREAMING_SNAKE_CASE_ ) elif "nlvr" in checkpoint_url: __lowercase = True __lowercase = 2 __lowercase = {0: '''False''', 1: '''True'''} __lowercase = {v: k for k, v in config.idalabel.items()} __lowercase = 3 __lowercase = ViltForImagesAndTextClassification(SCREAMING_SNAKE_CASE_ ) elif "irtr" in checkpoint_url: __lowercase = True __lowercase = ViltForImageAndTextRetrieval(SCREAMING_SNAKE_CASE_ ) elif "mlm_itm" in checkpoint_url: __lowercase = True __lowercase = ViltForMaskedLM(SCREAMING_SNAKE_CASE_ ) else: raise ValueError('''Unknown model type''' ) # load state_dict of original model, remove and rename some keys __lowercase = torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE_ , map_location='''cpu''' )['''state_dict'''] __lowercase = create_rename_keys(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , 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_ ) if mlm_model or irtr_model: __lowercase = ['''itm_score.fc.weight''', '''itm_score.fc.bias'''] for k in ignore_keys: state_dict.pop(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # load state dict into HuggingFace model model.eval() if mlm_model: __lowercase , __lowercase = model.load_state_dict(SCREAMING_SNAKE_CASE_ , strict=SCREAMING_SNAKE_CASE_ ) assert missing_keys == ["mlm_score.decoder.bias"] else: model.load_state_dict(SCREAMING_SNAKE_CASE_ ) # Define processor __lowercase = ViltImageProcessor(size=384 ) __lowercase = BertTokenizer.from_pretrained('''bert-base-uncased''' ) __lowercase = ViltProcessor(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # Forward pass on example inputs (image + text) if nlvr_model: __lowercase = Image.open(requests.get('''https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg''' , stream=SCREAMING_SNAKE_CASE_ ).raw ) __lowercase = Image.open(requests.get('''https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg''' , stream=SCREAMING_SNAKE_CASE_ ).raw ) __lowercase = ( '''The left image contains twice the number of dogs as the right image, and at least two dogs in total are''' ''' standing.''' ) __lowercase = processor(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ) __lowercase = processor(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ) __lowercase = model( input_ids=encoding_a.input_ids , pixel_values=encoding_a.pixel_values , pixel_values_a=encoding_a.pixel_values , ) else: __lowercase = Image.open(requests.get('''http://images.cocodataset.org/val2017/000000039769.jpg''' , stream=SCREAMING_SNAKE_CASE_ ).raw ) if mlm_model: __lowercase = '''a bunch of [MASK] laying on a [MASK].''' else: __lowercase = '''How many cats are there?''' __lowercase = processor(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ) __lowercase = model(**SCREAMING_SNAKE_CASE_ ) # Verify outputs if mlm_model: __lowercase = torch.Size([1, 11, 30522] ) __lowercase = torch.tensor([-1_2.5_0_6_1, -1_2.5_1_2_3, -1_2.5_1_7_4] ) assert outputs.logits.shape == expected_shape assert torch.allclose(outputs.logits[0, 0, :3] , SCREAMING_SNAKE_CASE_ , atol=1e-4 ) # verify masked token prediction equals "cats" __lowercase = outputs.logits[0, 4, :].argmax(-1 ).item() assert tokenizer.decode([predicted_id] ) == "cats" elif vqa_model: __lowercase = torch.Size([1, 3129] ) __lowercase = torch.tensor([-1_5.9_4_9_5, -1_8.1_4_7_2, -1_0.3_0_4_1] ) assert torch.allclose(outputs.logits[0, :3] , SCREAMING_SNAKE_CASE_ , atol=1e-4 ) assert outputs.logits.shape == expected_shape assert torch.allclose(outputs.logits[0, 0, :3] , SCREAMING_SNAKE_CASE_ , atol=1e-4 ) # verify vqa prediction equals "2" __lowercase = outputs.logits.argmax(-1 ).item() assert model.config.idalabel[predicted_idx] == "2" elif nlvr_model: __lowercase = torch.Size([1, 2] ) __lowercase = torch.tensor([-2.8_7_2_1, 2.1_2_9_1] ) assert torch.allclose(outputs.logits[0, :3] , SCREAMING_SNAKE_CASE_ , atol=1e-4 ) assert outputs.logits.shape == expected_shape Path(SCREAMING_SNAKE_CASE_ ).mkdir(exist_ok=SCREAMING_SNAKE_CASE_ ) print(F"Saving model and processor to {pytorch_dump_folder_path}" ) model.save_pretrained(SCREAMING_SNAKE_CASE_ ) processor.save_pretrained(SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint_url''', default='''https://github.com/dandelin/ViLT/releases/download/200k/vilt_200k_mlm_itm.ckpt''', type=str, help='''URL of the checkpoint you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) lowerCAmelCase__ = parser.parse_args() convert_vilt_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
366
'''simple docstring''' import json import os import unittest from transformers import OpenAIGPTTokenizer, OpenAIGPTTokenizerFast from transformers.models.openai.tokenization_openai import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_spacy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowercase_ (lowerCamelCase__ , unittest.TestCase ): """simple docstring""" SCREAMING_SNAKE_CASE : int = OpenAIGPTTokenizer SCREAMING_SNAKE_CASE : str = OpenAIGPTTokenizerFast SCREAMING_SNAKE_CASE : Optional[int] = True SCREAMING_SNAKE_CASE : List[str] = False def SCREAMING_SNAKE_CASE ( self : int ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt __lowercase = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''w</w>''', '''r</w>''', '''t</w>''', '''lo''', '''low''', '''er</w>''', '''low</w>''', '''lowest</w>''', '''newer</w>''', '''wider</w>''', '''<unk>''', ] __lowercase = dict(zip(lowercase__ ,range(len(lowercase__ ) ) ) ) __lowercase = ['''#version: 0.2''', '''l o''', '''lo w''', '''e r</w>''', ''''''] __lowercase = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES['''vocab_file'''] ) __lowercase = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file ,'''w''' ) as fp: fp.write(json.dumps(lowercase__ ) ) with open(self.merges_file ,'''w''' ) as fp: fp.write('''\n'''.join(lowercase__ ) ) def SCREAMING_SNAKE_CASE ( self : Dict ,lowercase__ : Optional[Any] ): return "lower newer", "lower newer" def SCREAMING_SNAKE_CASE ( self : Optional[int] ): __lowercase = OpenAIGPTTokenizer(self.vocab_file ,self.merges_file ) __lowercase = '''lower''' __lowercase = ['''low''', '''er</w>'''] __lowercase = tokenizer.tokenize(lowercase__ ) self.assertListEqual(lowercase__ ,lowercase__ ) __lowercase = tokens + ['''<unk>'''] __lowercase = [1_4, 1_5, 2_0] self.assertListEqual(tokenizer.convert_tokens_to_ids(lowercase__ ) ,lowercase__ ) def SCREAMING_SNAKE_CASE ( self : List[str] ,lowercase__ : Dict=1_5 ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})" ): __lowercase = self.rust_tokenizer_class.from_pretrained(lowercase__ ,**lowercase__ ) # Simple input __lowercase = '''This is a simple input''' __lowercase = ['''This is a simple input 1''', '''This is a simple input 2'''] __lowercase = ('''This is a simple input''', '''This is a pair''') __lowercase = [ ('''This is a simple input 1''', '''This is a simple input 2'''), ('''This is a simple pair 1''', '''This is a simple pair 2'''), ] # Simple input tests self.assertRaises(lowercase__ ,tokenizer_r.encode ,lowercase__ ,max_length=lowercase__ ,padding='''max_length''' ) # Simple input self.assertRaises(lowercase__ ,tokenizer_r.encode_plus ,lowercase__ ,max_length=lowercase__ ,padding='''max_length''' ) # Simple input self.assertRaises( lowercase__ ,tokenizer_r.batch_encode_plus ,lowercase__ ,max_length=lowercase__ ,padding='''max_length''' ,) # Pair input self.assertRaises(lowercase__ ,tokenizer_r.encode ,lowercase__ ,max_length=lowercase__ ,padding='''max_length''' ) # Pair input self.assertRaises(lowercase__ ,tokenizer_r.encode_plus ,lowercase__ ,max_length=lowercase__ ,padding='''max_length''' ) # Pair input self.assertRaises( lowercase__ ,tokenizer_r.batch_encode_plus ,lowercase__ ,max_length=lowercase__ ,padding='''max_length''' ,) def SCREAMING_SNAKE_CASE ( self : Optional[int] ): pass @require_ftfy @require_spacy @require_tokenizers class lowercase_ (lowerCamelCase__ ): """simple docstring""" pass
52
0
from timeit import timeit _a = { '''MALAYALAM''': True, '''String''': False, '''rotor''': True, '''level''': True, '''A''': True, '''BB''': True, '''ABC''': False, '''amanaplanacanalpanama''': True, # "a man a plan a canal panama" } # Ensure our test data is valid assert all((key == key[::-1]) is value for key, value in test_data.items()) def _a ( SCREAMING_SNAKE_CASE : str ) -> bool: """simple docstring""" __lowerCAmelCase: Any = 0 __lowerCAmelCase: Any = len(SCREAMING_SNAKE_CASE ) - 1 while start_i < end_i: if s[start_i] == s[end_i]: start_i += 1 end_i -= 1 else: return False return True def _a ( SCREAMING_SNAKE_CASE : str ) -> bool: """simple docstring""" __lowerCAmelCase: Tuple = len(SCREAMING_SNAKE_CASE ) // 2 __lowerCAmelCase: Optional[Any] = len(SCREAMING_SNAKE_CASE ) # We need to traverse till half of the length of string # as we can get access of the i'th last element from # i'th index. # eg: [0,1,2,3,4,5] => 4th index can be accessed # with the help of 1st index (i==n-i-1) # where n is length of string return all(s[i] == s[n - i - 1] for i in range(SCREAMING_SNAKE_CASE ) ) def _a ( SCREAMING_SNAKE_CASE : str ) -> bool: """simple docstring""" if len(SCREAMING_SNAKE_CASE ) <= 2: return True if s[0] == s[len(SCREAMING_SNAKE_CASE ) - 1]: return is_palindrome_recursive(s[1:-1] ) else: return False def _a ( SCREAMING_SNAKE_CASE : str ) -> bool: """simple docstring""" return s == s[::-1] def _a ( SCREAMING_SNAKE_CASE : str ) -> None: """simple docstring""" __lowerCAmelCase: Tuple = f'''all({name}(key) is value for key, value in test_data.items())''' __lowerCAmelCase: List[Any] = f'''from __main__ import test_data, {name}''' __lowerCAmelCase: List[str] = 50_00_00 __lowerCAmelCase: Union[str, Any] = timeit(stmt=SCREAMING_SNAKE_CASE , setup=SCREAMING_SNAKE_CASE , number=SCREAMING_SNAKE_CASE ) print(f'''{name:<35} finished {number:,} runs in {result:.5f} seconds''' ) if __name__ == "__main__": for key, value in test_data.items(): assert is_palindrome(key) is is_palindrome_recursive(key) assert is_palindrome(key) is is_palindrome_slice(key) print(f"{key:21} {value}") print('''a man a plan a canal panama''') # finished 500,000 runs in 0.46793 seconds benchmark_function('''is_palindrome_slice''') # finished 500,000 runs in 0.85234 seconds benchmark_function('''is_palindrome''') # finished 500,000 runs in 1.32028 seconds benchmark_function('''is_palindrome_recursive''') # finished 500,000 runs in 2.08679 seconds benchmark_function('''is_palindrome_traversal''')
322
import argparse from pathlib import Path import torch from packaging import version from torch.onnx import export from diffusers import AutoencoderKL _a = version.parse(version.parse(torch.__version__).base_version) < version.parse('''1.11''') def _a ( SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : tuple , SCREAMING_SNAKE_CASE : Path , SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : Union[str, Any] , SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : Optional[int]=False , ) -> str: """simple docstring""" output_path.parent.mkdir(parents=SCREAMING_SNAKE_CASE , exist_ok=SCREAMING_SNAKE_CASE ) # PyTorch deprecated the `enable_onnx_checker` and `use_external_data_format` arguments in v1.11, # so we check the torch version for backwards compatibility if is_torch_less_than_1_11: export( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , f=output_path.as_posix() , input_names=SCREAMING_SNAKE_CASE , output_names=SCREAMING_SNAKE_CASE , dynamic_axes=SCREAMING_SNAKE_CASE , do_constant_folding=SCREAMING_SNAKE_CASE , use_external_data_format=SCREAMING_SNAKE_CASE , enable_onnx_checker=SCREAMING_SNAKE_CASE , opset_version=SCREAMING_SNAKE_CASE , ) else: export( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , f=output_path.as_posix() , input_names=SCREAMING_SNAKE_CASE , output_names=SCREAMING_SNAKE_CASE , dynamic_axes=SCREAMING_SNAKE_CASE , do_constant_folding=SCREAMING_SNAKE_CASE , opset_version=SCREAMING_SNAKE_CASE , ) @torch.no_grad() def _a ( SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : bool = False ) -> Union[str, Any]: """simple docstring""" __lowerCAmelCase: List[Any] = torch.floataa if fpaa else torch.floataa if fpaa and torch.cuda.is_available(): __lowerCAmelCase: str = 'cuda' elif fpaa and not torch.cuda.is_available(): raise ValueError('`float16` model export is only supported on GPUs with CUDA' ) else: __lowerCAmelCase: Dict = 'cpu' __lowerCAmelCase: Optional[int] = Path(SCREAMING_SNAKE_CASE ) # VAE DECODER __lowerCAmelCase: Optional[Any] = AutoencoderKL.from_pretrained(model_path + '/vae' ) __lowerCAmelCase: Union[str, Any] = vae_decoder.config.latent_channels # forward only through the decoder part __lowerCAmelCase: Any = vae_decoder.decode onnx_export( SCREAMING_SNAKE_CASE , model_args=( torch.randn(1 , SCREAMING_SNAKE_CASE , 25 , 25 ).to(device=SCREAMING_SNAKE_CASE , dtype=SCREAMING_SNAKE_CASE ), False, ) , output_path=output_path / 'vae_decoder' / 'model.onnx' , ordered_input_names=['latent_sample', 'return_dict'] , output_names=['sample'] , dynamic_axes={ 'latent_sample': {0: 'batch', 1: 'channels', 2: 'height', 3: 'width'}, } , opset=SCREAMING_SNAKE_CASE , ) del vae_decoder if __name__ == "__main__": _a = argparse.ArgumentParser() parser.add_argument( '''--model_path''', type=str, required=True, help='''Path to the `diffusers` checkpoint to convert (either a local directory or on the Hub).''', ) parser.add_argument('''--output_path''', type=str, required=True, help='''Path to the output model.''') parser.add_argument( '''--opset''', default=1_4, type=int, help='''The version of the ONNX operator set to use.''', ) parser.add_argument('''--fp16''', action='''store_true''', default=False, help='''Export the models in `float16` mode''') _a = parser.parse_args() print(args.output_path) convert_models(args.model_path, args.output_path, args.opset, args.fpaa) print('''SD: Done: ONNX''')
322
1
import collections import inspect import unittest from typing import Dict, List, Tuple from transformers import MaskFormerSwinConfig from transformers.testing_utils import require_torch, require_torch_multi_gpu, torch_device from transformers.utils import is_torch_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import MaskFormerSwinBackbone from transformers.models.maskformer import MaskFormerSwinModel class _A : def __init__( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : Optional[Any]=13 , __SCREAMING_SNAKE_CASE : Dict=32 , __SCREAMING_SNAKE_CASE : Optional[int]=2 , __SCREAMING_SNAKE_CASE : int=3 , __SCREAMING_SNAKE_CASE : Dict=16 , __SCREAMING_SNAKE_CASE : Union[str, Any]=[1, 2, 1] , __SCREAMING_SNAKE_CASE : str=[2, 2, 4] , __SCREAMING_SNAKE_CASE : int=2 , __SCREAMING_SNAKE_CASE : Dict=2.0 , __SCREAMING_SNAKE_CASE : Any=True , __SCREAMING_SNAKE_CASE : List[Any]=0.0 , __SCREAMING_SNAKE_CASE : int=0.0 , __SCREAMING_SNAKE_CASE : Union[str, Any]=0.1 , __SCREAMING_SNAKE_CASE : Union[str, Any]="gelu" , __SCREAMING_SNAKE_CASE : str=False , __SCREAMING_SNAKE_CASE : str=True , __SCREAMING_SNAKE_CASE : Union[str, Any]=0.02 , __SCREAMING_SNAKE_CASE : int=1E-5 , __SCREAMING_SNAKE_CASE : Optional[Any]=True , __SCREAMING_SNAKE_CASE : Tuple=None , __SCREAMING_SNAKE_CASE : Union[str, Any]=True , __SCREAMING_SNAKE_CASE : List[str]=10 , __SCREAMING_SNAKE_CASE : Optional[Any]=8 , __SCREAMING_SNAKE_CASE : str=["stage1", "stage2", "stage3"] , __SCREAMING_SNAKE_CASE : str=[1, 2, 3] , ): '''simple docstring''' __a = parent __a = batch_size __a = image_size __a = patch_size __a = num_channels __a = embed_dim __a = depths __a = num_heads __a = window_size __a = mlp_ratio __a = qkv_bias __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = drop_path_rate __a = hidden_act __a = use_absolute_embeddings __a = patch_norm __a = layer_norm_eps __a = initializer_range __a = is_training __a = scope __a = use_labels __a = type_sequence_label_size __a = encoder_stride __a = out_features __a = out_indices def _lowerCamelCase ( self : Optional[Any]): '''simple docstring''' __a = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]) __a = None if self.use_labels: __a = ids_tensor([self.batch_size] , self.type_sequence_label_size) __a = self.get_config() return config, pixel_values, labels def _lowerCamelCase ( self : Any): '''simple docstring''' return MaskFormerSwinConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , embed_dim=self.embed_dim , depths=self.depths , num_heads=self.num_heads , window_size=self.window_size , mlp_ratio=self.mlp_ratio , qkv_bias=self.qkv_bias , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , drop_path_rate=self.drop_path_rate , hidden_act=self.hidden_act , use_absolute_embeddings=self.use_absolute_embeddings , path_norm=self.patch_norm , layer_norm_eps=self.layer_norm_eps , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , out_features=self.out_features , out_indices=self.out_indices , ) def _lowerCamelCase ( self : int , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Union[str, Any]): '''simple docstring''' __a = MaskFormerSwinModel(config=__SCREAMING_SNAKE_CASE) model.to(__SCREAMING_SNAKE_CASE) model.eval() __a = model(__SCREAMING_SNAKE_CASE) __a = ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths) - 1)) __a = int(config.embed_dim * 2 ** (len(config.depths) - 1)) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, expected_seq_len, expected_dim)) def _lowerCamelCase ( self : str , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : int): '''simple docstring''' __a = MaskFormerSwinBackbone(config=__SCREAMING_SNAKE_CASE) model.to(__SCREAMING_SNAKE_CASE) model.eval() __a = model(__SCREAMING_SNAKE_CASE) # verify feature maps self.parent.assertEqual(len(result.feature_maps) , len(config.out_features)) self.parent.assertListEqual(list(result.feature_maps[0].shape) , [13, 16, 16, 16]) # verify channels self.parent.assertEqual(len(model.channels) , len(config.out_features)) self.parent.assertListEqual(model.channels , [16, 32, 64]) # verify ValueError with self.parent.assertRaises(__SCREAMING_SNAKE_CASE): __a = ['''stem'''] __a = MaskFormerSwinBackbone(config=__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : Any): '''simple docstring''' __a = self.prepare_config_and_inputs() __a , __a , __a = config_and_inputs __a = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class _A ( __UpperCAmelCase ,__UpperCAmelCase ,unittest.TestCase ): UpperCamelCase__ : Tuple = ( ( MaskFormerSwinModel, MaskFormerSwinBackbone, ) if is_torch_available() else () ) UpperCamelCase__ : List[Any] = {'''feature-extraction''': MaskFormerSwinModel} if is_torch_available() else {} UpperCamelCase__ : str = False UpperCamelCase__ : Tuple = False UpperCamelCase__ : Tuple = False UpperCamelCase__ : Optional[Any] = False UpperCamelCase__ : List[str] = False def _lowerCamelCase ( self : Optional[int]): '''simple docstring''' __a = MaskFormerSwinModelTester(self) __a = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , embed_dim=37) @require_torch_multi_gpu @unittest.skip( reason=( '''`MaskFormerSwinModel` outputs `hidden_states_spatial_dimensions` which doesn\'t work well with''' ''' `nn.DataParallel`''' )) def _lowerCamelCase ( self : int): '''simple docstring''' pass def _lowerCamelCase ( self : Optional[int]): '''simple docstring''' self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def _lowerCamelCase ( self : str): '''simple docstring''' return def _lowerCamelCase ( self : Tuple): '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : List[str]): '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*__SCREAMING_SNAKE_CASE) @unittest.skip('''Swin does not use inputs_embeds''') def _lowerCamelCase ( self : str): '''simple docstring''' pass @unittest.skip('''Swin does not support feedforward chunking''') def _lowerCamelCase ( self : Dict): '''simple docstring''' pass def _lowerCamelCase ( self : Any): '''simple docstring''' __a , __a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __a = model_class(__SCREAMING_SNAKE_CASE) self.assertIsInstance(model.get_input_embeddings() , (nn.Module)) __a = model.get_output_embeddings() self.assertTrue(x is None or isinstance(__SCREAMING_SNAKE_CASE , nn.Linear)) def _lowerCamelCase ( self : Dict): '''simple docstring''' __a , __a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __a = model_class(__SCREAMING_SNAKE_CASE) __a = inspect.signature(model.forward) # signature.parameters is an OrderedDict => so arg_names order is deterministic __a = [*signature.parameters.keys()] __a = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , __SCREAMING_SNAKE_CASE) @unittest.skip(reason='''MaskFormerSwin is only used as backbone and doesn\'t support output_attentions''') def _lowerCamelCase ( self : str): '''simple docstring''' pass @unittest.skip(reason='''MaskFormerSwin is only used as an internal backbone''') def _lowerCamelCase ( self : Union[str, Any]): '''simple docstring''' pass def _lowerCamelCase ( self : Any , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : Any): '''simple docstring''' __a = model_class(__SCREAMING_SNAKE_CASE) model.to(__SCREAMING_SNAKE_CASE) model.eval() with torch.no_grad(): __a = model(**self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE)) __a = outputs.hidden_states __a = getattr( self.model_tester , '''expected_num_hidden_layers''' , len(self.model_tester.depths) + 1) self.assertEqual(len(__SCREAMING_SNAKE_CASE) , __SCREAMING_SNAKE_CASE) # Swin has a different seq_length __a = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable) else (config.patch_size, config.patch_size) ) __a = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.assertListEqual( list(hidden_states[0].shape[-2:]) , [num_patches, self.model_tester.embed_dim] , ) def _lowerCamelCase ( self : Union[str, Any]): '''simple docstring''' __a , __a = self.model_tester.prepare_config_and_inputs_for_common() __a = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable) else (self.model_tester.image_size, self.model_tester.image_size) ) for model_class in self.all_model_classes: __a = True self.check_hidden_states_output(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] __a = True self.check_hidden_states_output(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : Tuple): '''simple docstring''' __a , __a = self.model_tester.prepare_config_and_inputs_for_common() __a = 3 __a = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable) else (self.model_tester.image_size, self.model_tester.image_size) ) __a = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable) else (config.patch_size, config.patch_size) ) __a = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0]) __a = image_size[1] + patch_size[1] - (image_size[1] % patch_size[1]) for model_class in self.all_model_classes: __a = True self.check_hidden_states_output(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , (padded_height, padded_width)) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] __a = True self.check_hidden_states_output(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , (padded_height, padded_width)) @unittest.skip(reason='''MaskFormerSwin doesn\'t have pretrained checkpoints''') def _lowerCamelCase ( self : List[Any]): '''simple docstring''' pass @unittest.skip(reason='''This will be fixed once MaskFormerSwin is replaced by native Swin''') def _lowerCamelCase ( self : Any): '''simple docstring''' pass @unittest.skip(reason='''This will be fixed once MaskFormerSwin is replaced by native Swin''') def _lowerCamelCase ( self : Optional[Any]): '''simple docstring''' pass def _lowerCamelCase ( self : Dict): '''simple docstring''' __a , __a = self.model_tester.prepare_config_and_inputs_for_common() def set_nan_tensor_to_zero(__SCREAMING_SNAKE_CASE : Any): __a = 0 return t def check_equivalence(__SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Dict={}): with torch.no_grad(): __a = model(**__SCREAMING_SNAKE_CASE , return_dict=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE) __a = model(**__SCREAMING_SNAKE_CASE , return_dict=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE).to_tuple() def recursive_check(__SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : List[Any]): if isinstance(__SCREAMING_SNAKE_CASE , (List, Tuple)): for tuple_iterable_value, dict_iterable_value in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE): recursive_check(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) elif isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE): for tuple_iterable_value, dict_iterable_value in zip( tuple_object.values() , dict_object.values()): recursive_check(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) elif tuple_object is None: return else: self.assertTrue( torch.allclose( set_nan_tensor_to_zero(__SCREAMING_SNAKE_CASE) , set_nan_tensor_to_zero(__SCREAMING_SNAKE_CASE) , atol=1E-5) , msg=( '''Tuple and dict output are not equal. Difference:''' F' {torch.max(torch.abs(tuple_object - dict_object))}. Tuple has `nan`:' F' {torch.isnan(__SCREAMING_SNAKE_CASE).any()} and `inf`: {torch.isinf(__SCREAMING_SNAKE_CASE)}. Dict has' F' `nan`: {torch.isnan(__SCREAMING_SNAKE_CASE).any()} and `inf`: {torch.isinf(__SCREAMING_SNAKE_CASE)}.' ) , ) recursive_check(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) for model_class in self.all_model_classes: __a = model_class(__SCREAMING_SNAKE_CASE) model.to(__SCREAMING_SNAKE_CASE) model.eval() __a = self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) __a = self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) check_equivalence(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) __a = self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , return_labels=__SCREAMING_SNAKE_CASE) __a = self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , return_labels=__SCREAMING_SNAKE_CASE) check_equivalence(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) __a = self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) __a = self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) check_equivalence(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , {'''output_hidden_states''': True}) __a = self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , return_labels=__SCREAMING_SNAKE_CASE) __a = self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , return_labels=__SCREAMING_SNAKE_CASE) check_equivalence(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , {'''output_hidden_states''': True}) @require_torch class _A ( unittest.TestCase ,__UpperCAmelCase ): UpperCamelCase__ : Any = (MaskFormerSwinBackbone,) if is_torch_available() else () UpperCamelCase__ : Union[str, Any] = MaskFormerSwinConfig def _lowerCamelCase ( self : Tuple): '''simple docstring''' __a = MaskFormerSwinModelTester(self) def _lowerCamelCase ( self : int): '''simple docstring''' __a , __a = self.model_tester.prepare_config_and_inputs_for_common() __a = inputs_dict['''pixel_values'''].shape[0] for backbone_class in self.all_model_classes: __a = backbone_class(__SCREAMING_SNAKE_CASE) backbone.to(__SCREAMING_SNAKE_CASE) backbone.eval() __a = backbone(**__SCREAMING_SNAKE_CASE) # Test default outputs and verify feature maps self.assertIsInstance(outputs.feature_maps , __SCREAMING_SNAKE_CASE) self.assertTrue(len(outputs.feature_maps) == len(backbone.channels)) for feature_map, n_channels in zip(outputs.feature_maps , backbone.channels): self.assertTrue(feature_map.shape[:2] , (batch_size, n_channels)) self.assertIsNone(outputs.hidden_states) self.assertIsNone(outputs.attentions) # Test output_hidden_states=True __a = backbone(**__SCREAMING_SNAKE_CASE , output_hidden_states=__SCREAMING_SNAKE_CASE) self.assertIsNotNone(outputs.hidden_states) self.assertTrue(len(outputs.hidden_states) , len(backbone.stage_names)) # We skip the stem layer for hidden_states, n_channels in zip(outputs.hidden_states[1:] , backbone.channels): for hidden_state in hidden_states: # Hidden states are in the format (batch_size, (height * width), n_channels) __a , __a , __a = hidden_state.shape self.assertTrue((h_batch_size, h_n_channels) , (batch_size, n_channels)) # Test output_attentions=True if self.has_attentions: __a = backbone(**__SCREAMING_SNAKE_CASE , output_attentions=__SCREAMING_SNAKE_CASE) self.assertIsNotNone(outputs.attentions)
131
import unittest from transformers import DonutProcessor __snake_case :List[str] = '''naver-clova-ix/donut-base''' class _A ( unittest.TestCase ): def _lowerCamelCase ( self : List[str]): '''simple docstring''' __a = DonutProcessor.from_pretrained(__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : Tuple): '''simple docstring''' __a = { '''name''': '''John Doe''', '''age''': '''99''', '''city''': '''Atlanta''', '''state''': '''GA''', '''zip''': '''30301''', '''phone''': '''123-4567''', '''nicknames''': [{'''nickname''': '''Johnny'''}, {'''nickname''': '''JD'''}], } __a = ( '''<s_name>John Doe</s_name><s_age>99</s_age><s_city>Atlanta</s_city>''' '''<s_state>GA</s_state><s_zip>30301</s_zip><s_phone>123-4567</s_phone>''' '''<s_nicknames><s_nickname>Johnny</s_nickname>''' '''<sep/><s_nickname>JD</s_nickname></s_nicknames>''' ) __a = self.processor.tokenajson(__SCREAMING_SNAKE_CASE) self.assertDictEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE)
131
1
from __future__ import annotations def UpperCAmelCase_ ( _A , _A = None ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = word_bank or [] # create a table SCREAMING_SNAKE_CASE__ = len(_A ) + 1 SCREAMING_SNAKE_CASE__ = [] for _ in range(_A ): table.append([] ) # seed value SCREAMING_SNAKE_CASE__ = [[]] # because empty string has empty combination # iterate through the indices for i in range(_A ): # condition if table[i] != []: for word in word_bank: # slice condition if target[i : i + len(_A )] == word: SCREAMING_SNAKE_CASE__ = [ [word, *way] for way in table[i] ] # adds the word to every combination the current position holds # now,push that combination to the table[i+len(word)] table[i + len(_A )] += new_combinations # combinations are in reverse order so reverse for better output for combination in table[len(_A )]: combination.reverse() return table[len(_A )] if __name__ == "__main__": print(all_construct('''jwajalapa''', ['''jwa''', '''j''', '''w''', '''a''', '''la''', '''lapa'''])) print(all_construct('''rajamati''', ['''s''', '''raj''', '''amat''', '''raja''', '''ma''', '''i''', '''t'''])) print( all_construct( '''hexagonosaurus''', ['''h''', '''ex''', '''hex''', '''ag''', '''ago''', '''ru''', '''auru''', '''rus''', '''go''', '''no''', '''o''', '''s'''], ) )
314
import os import shutil import sys import tempfile import unittest from pathlib import Path import pytest import transformers from transformers import ( BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, AutoTokenizer, BertConfig, BertTokenizer, BertTokenizerFast, CTRLTokenizer, GPTaTokenizer, GPTaTokenizerFast, PreTrainedTokenizerFast, RobertaTokenizer, RobertaTokenizerFast, is_tokenizers_available, ) from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig from transformers.models.auto.tokenization_auto import ( TOKENIZER_MAPPING, get_tokenizer_config, tokenizer_class_from_name, ) from transformers.models.roberta.configuration_roberta import RobertaConfig from transformers.testing_utils import ( DUMMY_DIFF_TOKENIZER_IDENTIFIER, DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, RequestCounter, require_tokenizers, slow, ) sys.path.append(str(Path(__file__).parent.parent.parent.parent / '''utils''')) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_tokenization import CustomTokenizer # noqa E402 if is_tokenizers_available(): from test_module.custom_tokenization_fast import CustomTokenizerFast class UpperCAmelCase__ ( unittest.TestCase ): """simple docstring""" def lowercase_ ( self : Optional[int] ) -> Tuple: SCREAMING_SNAKE_CASE__ = 0 @slow def lowercase_ ( self : List[str] ) -> Any: for model_name in (x for x in BERT_PRETRAINED_CONFIG_ARCHIVE_MAP.keys() if "japanese" not in x): SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained(__lowerCamelCase ) self.assertIsNotNone(__lowerCamelCase ) self.assertIsInstance(__lowerCamelCase , (BertTokenizer, BertTokenizerFast) ) self.assertGreater(len(__lowerCamelCase ) , 0 ) for model_name in GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP.keys(): SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained(__lowerCamelCase ) self.assertIsNotNone(__lowerCamelCase ) self.assertIsInstance(__lowerCamelCase , (GPTaTokenizer, GPTaTokenizerFast) ) self.assertGreater(len(__lowerCamelCase ) , 0 ) def lowercase_ ( self : List[str] ) -> int: SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained(__lowerCamelCase ) self.assertIsInstance(__lowerCamelCase , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(tokenizer.vocab_size , 12 ) def lowercase_ ( self : List[str] ) -> Dict: SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained(__lowerCamelCase ) self.assertIsInstance(__lowerCamelCase , (RobertaTokenizer, RobertaTokenizerFast) ) self.assertEqual(tokenizer.vocab_size , 20 ) def lowercase_ ( self : Dict ) -> Any: SCREAMING_SNAKE_CASE__ = AutoConfig.from_pretrained(__lowerCamelCase ) self.assertIsInstance(__lowerCamelCase , __lowerCamelCase ) # Check that tokenizer_type ≠ model_type SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained(__lowerCamelCase , config=__lowerCamelCase ) self.assertIsInstance(__lowerCamelCase , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(tokenizer.vocab_size , 12 ) def lowercase_ ( self : Tuple ) -> Dict: with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.txt''' , os.path.join(__lowerCamelCase , '''vocab.txt''' ) ) SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained(__lowerCamelCase , tokenizer_type='''bert''' , use_fast=__lowerCamelCase ) self.assertIsInstance(__lowerCamelCase , __lowerCamelCase ) with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.json''' , os.path.join(__lowerCamelCase , '''vocab.json''' ) ) shutil.copy('''./tests/fixtures/merges.txt''' , os.path.join(__lowerCamelCase , '''merges.txt''' ) ) SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained(__lowerCamelCase , tokenizer_type='''gpt2''' , use_fast=__lowerCamelCase ) self.assertIsInstance(__lowerCamelCase , __lowerCamelCase ) @require_tokenizers def lowercase_ ( self : Optional[int] ) -> Optional[int]: with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.txt''' , os.path.join(__lowerCamelCase , '''vocab.txt''' ) ) SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained(__lowerCamelCase , tokenizer_type='''bert''' ) self.assertIsInstance(__lowerCamelCase , __lowerCamelCase ) with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.json''' , os.path.join(__lowerCamelCase , '''vocab.json''' ) ) shutil.copy('''./tests/fixtures/merges.txt''' , os.path.join(__lowerCamelCase , '''merges.txt''' ) ) SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained(__lowerCamelCase , tokenizer_type='''gpt2''' ) self.assertIsInstance(__lowerCamelCase , __lowerCamelCase ) def lowercase_ ( self : Union[str, Any] ) -> int: with pytest.raises(__lowerCamelCase ): AutoTokenizer.from_pretrained('''./''' , tokenizer_type='''xxx''' ) @require_tokenizers def lowercase_ ( self : Optional[int] ) -> Tuple: for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]: SCREAMING_SNAKE_CASE__ = tokenizer_class.from_pretrained('''wietsedv/bert-base-dutch-cased''' ) self.assertIsInstance(__lowerCamelCase , (BertTokenizer, BertTokenizerFast) ) if isinstance(__lowerCamelCase , __lowerCamelCase ): self.assertEqual(tokenizer.basic_tokenizer.do_lower_case , __lowerCamelCase ) else: self.assertEqual(tokenizer.do_lower_case , __lowerCamelCase ) self.assertEqual(tokenizer.model_max_length , 512 ) @require_tokenizers def lowercase_ ( self : Any ) -> str: for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]: with self.assertRaisesRegex( __lowerCamelCase , '''julien-c/herlolip-not-exists is not a local folder and is not a valid model identifier''' , ): SCREAMING_SNAKE_CASE__ = tokenizer_class.from_pretrained('''julien-c/herlolip-not-exists''' ) def lowercase_ ( self : List[str] ) -> Tuple: # tests: https://github.com/huggingface/transformers/pull/13251 # 1. models with `-`, e.g. xlm-roberta -> xlm_roberta # 2. models that don't remap 1-1 from model-name to model file, e.g., openai-gpt -> openai SCREAMING_SNAKE_CASE__ = TOKENIZER_MAPPING.values() SCREAMING_SNAKE_CASE__ = [] for slow_tok, fast_tok in tokenizers: if slow_tok is not None: tokenizer_names.append(slow_tok.__name__ ) if fast_tok is not None: tokenizer_names.append(fast_tok.__name__ ) for tokenizer_name in tokenizer_names: # must find the right class tokenizer_class_from_name(__lowerCamelCase ) @require_tokenizers def lowercase_ ( self : Optional[int] ) -> Any: self.assertIsInstance(AutoTokenizer.from_pretrained('''bert-base-cased''' , use_fast=__lowerCamelCase ) , __lowerCamelCase ) self.assertIsInstance(AutoTokenizer.from_pretrained('''bert-base-cased''' ) , __lowerCamelCase ) @require_tokenizers def lowercase_ ( self : List[Any] ) -> Optional[Any]: SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained('''distilbert-base-uncased''' , do_lower_case=__lowerCamelCase ) SCREAMING_SNAKE_CASE__ = '''Hello, world. How are you?''' SCREAMING_SNAKE_CASE__ = tokenizer.tokenize(__lowerCamelCase ) self.assertEqual('''[UNK]''' , tokens[0] ) SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained('''microsoft/mpnet-base''' , do_lower_case=__lowerCamelCase ) SCREAMING_SNAKE_CASE__ = tokenizer.tokenize(__lowerCamelCase ) self.assertEqual('''[UNK]''' , tokens[0] ) @require_tokenizers def lowercase_ ( self : Dict ) -> int: SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained('''robot-test/dummy-tokenizer-fast-with-model-config''' ) self.assertEqual(type(__lowerCamelCase ) , __lowerCamelCase ) self.assertEqual(tokenizer.model_max_length , 512 ) self.assertEqual(tokenizer.vocab_size , 3_0000 ) self.assertEqual(tokenizer.unk_token , '''[UNK]''' ) self.assertEqual(tokenizer.padding_side , '''right''' ) self.assertEqual(tokenizer.truncation_side , '''right''' ) def lowercase_ ( self : List[Any] ) -> Optional[int]: SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained(__lowerCamelCase ) self.assertIsInstance(__lowerCamelCase , (BertTokenizer, BertTokenizerFast) ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(__lowerCamelCase ) SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained(__lowerCamelCase ) self.assertIsInstance(__lowerCamelCase , tokenizer.__class__ ) self.assertEqual(tokenizera.vocab_size , 12 ) def lowercase_ ( self : Optional[int] ) -> Any: SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained('''ctrl''' ) # There is no fast CTRL so this always gives us a slow tokenizer. self.assertIsInstance(__lowerCamelCase , __lowerCamelCase ) def lowercase_ ( self : List[Any] ) -> Optional[int]: # Check we can load the tokenizer config of an online model. SCREAMING_SNAKE_CASE__ = get_tokenizer_config('''bert-base-cased''' ) SCREAMING_SNAKE_CASE__ = config.pop('''_commit_hash''' , __lowerCamelCase ) # If we ever update bert-base-cased tokenizer config, this dict here will need to be updated. self.assertEqual(__lowerCamelCase , {'''do_lower_case''': False} ) # This model does not have a tokenizer_config so we get back an empty dict. SCREAMING_SNAKE_CASE__ = get_tokenizer_config(__lowerCamelCase ) self.assertDictEqual(__lowerCamelCase , {} ) # A tokenizer saved with `save_pretrained` always creates a tokenizer config. SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained(__lowerCamelCase ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(__lowerCamelCase ) SCREAMING_SNAKE_CASE__ = get_tokenizer_config(__lowerCamelCase ) # Check the class of the tokenizer was properly saved (note that it always saves the slow class). self.assertEqual(config['''tokenizer_class'''] , '''BertTokenizer''' ) def lowercase_ ( self : int ) -> str: try: AutoConfig.register('''custom''' , __lowerCamelCase ) AutoTokenizer.register(__lowerCamelCase , slow_tokenizer_class=__lowerCamelCase ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(__lowerCamelCase ): AutoTokenizer.register(__lowerCamelCase , slow_tokenizer_class=__lowerCamelCase ) SCREAMING_SNAKE_CASE__ = CustomTokenizer.from_pretrained(__lowerCamelCase ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(__lowerCamelCase ) SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained(__lowerCamelCase ) self.assertIsInstance(__lowerCamelCase , __lowerCamelCase ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] @require_tokenizers def lowercase_ ( self : List[Any] ) -> List[Any]: try: AutoConfig.register('''custom''' , __lowerCamelCase ) # Can register in two steps AutoTokenizer.register(__lowerCamelCase , slow_tokenizer_class=__lowerCamelCase ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] , (CustomTokenizer, None) ) AutoTokenizer.register(__lowerCamelCase , fast_tokenizer_class=__lowerCamelCase ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] , (CustomTokenizer, CustomTokenizerFast) ) del TOKENIZER_MAPPING._extra_content[CustomConfig] # Can register in one step AutoTokenizer.register( __lowerCamelCase , slow_tokenizer_class=__lowerCamelCase , fast_tokenizer_class=__lowerCamelCase ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] , (CustomTokenizer, CustomTokenizerFast) ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(__lowerCamelCase ): AutoTokenizer.register(__lowerCamelCase , fast_tokenizer_class=__lowerCamelCase ) # We pass through a bert tokenizer fast cause there is no converter slow to fast for our new toknizer # and that model does not have a tokenizer.json with tempfile.TemporaryDirectory() as tmp_dir: SCREAMING_SNAKE_CASE__ = BertTokenizerFast.from_pretrained(__lowerCamelCase ) bert_tokenizer.save_pretrained(__lowerCamelCase ) SCREAMING_SNAKE_CASE__ = CustomTokenizerFast.from_pretrained(__lowerCamelCase ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(__lowerCamelCase ) SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained(__lowerCamelCase ) self.assertIsInstance(__lowerCamelCase , __lowerCamelCase ) SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained(__lowerCamelCase , use_fast=__lowerCamelCase ) self.assertIsInstance(__lowerCamelCase , __lowerCamelCase ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] def lowercase_ ( self : Dict ) -> Tuple: # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(__lowerCamelCase ): SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' ) # If remote code is disabled, we can't load this config. with self.assertRaises(__lowerCamelCase ): SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=__lowerCamelCase ) SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=__lowerCamelCase ) self.assertTrue(tokenizer.special_attribute_present ) # Test tokenizer can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(__lowerCamelCase ) SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained(__lowerCamelCase , trust_remote_code=__lowerCamelCase ) self.assertTrue(reloaded_tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) self.assertEqual(reloaded_tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) # Test we can also load the slow version SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=__lowerCamelCase , use_fast=__lowerCamelCase ) self.assertTrue(tokenizer.special_attribute_present ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) # Test tokenizer can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(__lowerCamelCase ) SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained(__lowerCamelCase , trust_remote_code=__lowerCamelCase , use_fast=__lowerCamelCase ) self.assertEqual(reloaded_tokenizer.__class__.__name__ , '''NewTokenizer''' ) self.assertTrue(reloaded_tokenizer.special_attribute_present ) else: self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) self.assertEqual(reloaded_tokenizer.__class__.__name__ , '''NewTokenizer''' ) @require_tokenizers def lowercase_ ( self : List[str] ) -> str: class UpperCAmelCase__ ( A__ ): """simple docstring""" a = False class UpperCAmelCase__ ( A__ ): """simple docstring""" a = NewTokenizer a = False try: AutoConfig.register('''custom''' , __lowerCamelCase ) AutoTokenizer.register(__lowerCamelCase , slow_tokenizer_class=__lowerCamelCase ) AutoTokenizer.register(__lowerCamelCase , fast_tokenizer_class=__lowerCamelCase ) # If remote code is not set, the default is to use local SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) self.assertFalse(tokenizer.special_attribute_present ) SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' , use_fast=__lowerCamelCase ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) self.assertFalse(tokenizer.special_attribute_present ) # If remote code is disabled, we load the local one. SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=__lowerCamelCase ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) self.assertFalse(tokenizer.special_attribute_present ) SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=__lowerCamelCase , use_fast=__lowerCamelCase ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) self.assertFalse(tokenizer.special_attribute_present ) # If remote is enabled, we load from the Hub SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=__lowerCamelCase ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) self.assertTrue(tokenizer.special_attribute_present ) SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=__lowerCamelCase , use_fast=__lowerCamelCase ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) self.assertTrue(tokenizer.special_attribute_present ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] def lowercase_ ( self : Dict ) -> List[str]: SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer_legacy''' , trust_remote_code=__lowerCamelCase ) self.assertTrue(tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) # Test we can also load the slow version SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer_legacy''' , trust_remote_code=__lowerCamelCase , use_fast=__lowerCamelCase ) self.assertTrue(tokenizer.special_attribute_present ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) else: self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) def lowercase_ ( self : Union[str, Any] ) -> Dict: with self.assertRaisesRegex( __lowerCamelCase , '''bert-base is not a local folder and is not a valid model identifier''' ): SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained('''bert-base''' ) def lowercase_ ( self : Dict ) -> Optional[int]: with self.assertRaisesRegex( __lowerCamelCase , r'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained(__lowerCamelCase , revision='''aaaaaa''' ) def lowercase_ ( self : Any ) -> Optional[Any]: # Make sure we have cached the tokenizer. SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) with RequestCounter() as counter: SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 )
314
1
"""simple docstring""" import os from typing import Any, Callable, Dict, List, Optional, Tuple, Union import torch from torch import nn from ...models.controlnet import ControlNetModel, ControlNetOutput from ...models.modeling_utils import ModelMixin from ...utils import logging _lowerCAmelCase : Optional[int] = logging.get_logger(__name__) class UpperCAmelCase_ ( A__ ): def __init__( self : int , A : List[Any] ): super().__init__() _UpperCAmelCase : int = nn.ModuleList(__A ) def snake_case_ ( self : Union[str, Any] , A : List[Any] , A : Union[str, Any] , A : List[Any] , A : Optional[Any] , A : Optional[int] , A : Union[str, Any] = None , A : Dict = None , A : Dict = None , A : int = None , A : Union[str, Any] = False , A : List[Any] = True , ): for i, (image, scale, controlnet) in enumerate(zip(__A , __A , self.nets ) ): _UpperCAmelCase : Optional[int] = controlnet( __A , __A , __A , __A , __A , __A , __A , __A , __A , __A , __A , ) # merge samples if i == 0: _UpperCAmelCase : Optional[int] = down_samples, mid_sample else: _UpperCAmelCase : str = [ samples_prev + samples_curr for samples_prev, samples_curr in zip(__A , __A ) ] mid_block_res_sample += mid_sample return down_block_res_samples, mid_block_res_sample def snake_case_ ( self : Optional[Any] , A : Optional[Any] , A : Tuple = True , A : List[Any] = None , A : Union[str, Any] = False , A : List[Any] = None , ): _UpperCAmelCase : List[str] = 0 _UpperCAmelCase : Any = save_directory for controlnet in self.nets: controlnet.save_pretrained( __A , is_main_process=__A , save_function=__A , safe_serialization=__A , variant=__A , ) idx += 1 _UpperCAmelCase : Optional[Any] = model_path_to_save + f'_{idx}' @classmethod def snake_case_ ( cls : Optional[int] , A : Any , **A : Optional[int] ): _UpperCAmelCase : Optional[int] = 0 _UpperCAmelCase : Union[str, Any] = [] # load controlnet and append to list until no controlnet directory exists anymore # first controlnet has to be saved under `./mydirectory/controlnet` to be compliant with `DiffusionPipeline.from_prertained` # second, third, ... controlnets have to be saved under `./mydirectory/controlnet_1`, `./mydirectory/controlnet_2`, ... _UpperCAmelCase : Union[str, Any] = pretrained_model_path while os.path.isdir(__A ): _UpperCAmelCase : Optional[int] = ControlNetModel.from_pretrained(__A , **__A ) controlnets.append(__A ) idx += 1 _UpperCAmelCase : int = pretrained_model_path + f'_{idx}' logger.info(f'{len(__A )} controlnets loaded from {pretrained_model_path}.' ) if len(__A ) == 0: raise ValueError( f'No ControlNets found under {os.path.dirname(__A )}. Expected at least {pretrained_model_path + "_0"}.' ) return cls(__A )
351
"""simple docstring""" from __future__ import annotations from typing import Any def __snake_case ( SCREAMING_SNAKE_CASE__ : list[Any] ) -> None: '''simple docstring''' create_state_space_tree(SCREAMING_SNAKE_CASE__ , [] , 0 ) def __snake_case ( SCREAMING_SNAKE_CASE__ : list[Any] , SCREAMING_SNAKE_CASE__ : list[Any] , SCREAMING_SNAKE_CASE__ : int ) -> None: '''simple docstring''' if index == len(SCREAMING_SNAKE_CASE__ ): print(SCREAMING_SNAKE_CASE__ ) return create_state_space_tree(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , index + 1 ) current_subsequence.append(sequence[index] ) create_state_space_tree(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , index + 1 ) current_subsequence.pop() if __name__ == "__main__": _lowerCAmelCase : list[Any] = [3, 1, 2, 4] generate_all_subsequences(seq) seq.clear() seq.extend(["A", "B", "C"]) generate_all_subsequences(seq)
202
0
"""simple docstring""" from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, ChannelDimension, ImageInput, PILImageResampling, is_batched, to_numpy_array, valid_images, ) from ...utils import TensorType, logging __UpperCamelCase = logging.get_logger(__name__) class UpperCamelCase ( lowerCAmelCase__ ): SCREAMING_SNAKE_CASE_ = ["pixel_values"] def __init__( self, lowerCAmelCase__ = True, lowerCAmelCase__ = None, lowerCAmelCase__ = PILImageResampling.BICUBIC, lowerCAmelCase__ = True, lowerCAmelCase__ = True, lowerCAmelCase__ = 1 / 255, lowerCAmelCase__ = None, lowerCAmelCase__ = True, lowerCAmelCase__ = None, lowerCAmelCase__ = None, **lowerCAmelCase__, ) -> None: super().__init__(**lowerCAmelCase__) snake_case_ = size if size is not None else {'height': 224, 'width': 224} snake_case_ = get_size_dict(lowerCAmelCase__) snake_case_ = crop_size if crop_size is not None else {'height': 224, 'width': 224} snake_case_ = get_size_dict(lowerCAmelCase__, default_to_square=lowerCAmelCase__, param_name='crop_size') snake_case_ = do_resize snake_case_ = do_rescale snake_case_ = do_normalize snake_case_ = do_center_crop snake_case_ = crop_size snake_case_ = size snake_case_ = resample snake_case_ = rescale_factor snake_case_ = image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN snake_case_ = image_std if image_std is not None else IMAGENET_DEFAULT_STD def a_ ( self, lowerCAmelCase__, lowerCAmelCase__, lowerCAmelCase__ = PILImageResampling.BILINEAR, lowerCAmelCase__ = None, **lowerCAmelCase__, ) -> np.ndarray: snake_case_ = get_size_dict(lowerCAmelCase__) if "shortest_edge" in size: snake_case_ = get_resize_output_image_size(lowerCAmelCase__, size=size['shortest_edge'], default_to_square=lowerCAmelCase__) # size = get_resize_output_image_size(image, size["shortest_edge"], size["longest_edge"]) elif "height" in size and "width" in size: snake_case_ = (size['height'], size['width']) else: raise ValueError(f'Size must contain \'height\' and \'width\' keys or \'shortest_edge\' key. Got {size.keys()}') return resize(lowerCAmelCase__, size=lowerCAmelCase__, resample=lowerCAmelCase__, data_format=lowerCAmelCase__, **lowerCAmelCase__) def a_ ( self, lowerCAmelCase__, lowerCAmelCase__, lowerCAmelCase__ = None, **lowerCAmelCase__, ) -> np.ndarray: snake_case_ = get_size_dict(lowerCAmelCase__) if "height" not in size or "width" not in size: raise ValueError(f'The `size` parameter must contain the keys (height, width). Got {size.keys()}') return center_crop(lowerCAmelCase__, size=(size['height'], size['width']), data_format=lowerCAmelCase__, **lowerCAmelCase__) def a_ ( self, lowerCAmelCase__, lowerCAmelCase__, lowerCAmelCase__ = None, **lowerCAmelCase__) -> np.ndarray: return rescale(lowerCAmelCase__, scale=lowerCAmelCase__, data_format=lowerCAmelCase__, **lowerCAmelCase__) def a_ ( self, lowerCAmelCase__, lowerCAmelCase__, lowerCAmelCase__, lowerCAmelCase__ = None, **lowerCAmelCase__, ) -> np.ndarray: return normalize(lowerCAmelCase__, mean=lowerCAmelCase__, std=lowerCAmelCase__, data_format=lowerCAmelCase__, **lowerCAmelCase__) def a_ ( self, lowerCAmelCase__, lowerCAmelCase__ = None, lowerCAmelCase__ = None, lowerCAmelCase__ = None, lowerCAmelCase__ = None, lowerCAmelCase__ = None, lowerCAmelCase__ = None, lowerCAmelCase__ = None, lowerCAmelCase__ = None, lowerCAmelCase__ = None, lowerCAmelCase__ = None, lowerCAmelCase__ = None, lowerCAmelCase__ = ChannelDimension.FIRST, **lowerCAmelCase__, ) -> BatchFeature: snake_case_ = do_resize if do_resize is not None else self.do_resize snake_case_ = do_rescale if do_rescale is not None else self.do_rescale snake_case_ = do_normalize if do_normalize is not None else self.do_normalize snake_case_ = do_center_crop if do_center_crop is not None else self.do_center_crop snake_case_ = crop_size if crop_size is not None else self.crop_size snake_case_ = get_size_dict(lowerCAmelCase__, param_name='crop_size', default_to_square=lowerCAmelCase__) snake_case_ = resample if resample is not None else self.resample snake_case_ = rescale_factor if rescale_factor is not None else self.rescale_factor snake_case_ = image_mean if image_mean is not None else self.image_mean snake_case_ = image_std if image_std is not None else self.image_std snake_case_ = size if size is not None else self.size snake_case_ = get_size_dict(lowerCAmelCase__) if not is_batched(lowerCAmelCase__): snake_case_ = [images] if not valid_images(lowerCAmelCase__): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.') if do_resize and size is None: raise ValueError('Size must be specified if do_resize is True.') if do_center_crop and crop_size is None: raise ValueError('Crop size must be specified if do_center_crop is True.') if do_rescale and rescale_factor is None: raise ValueError('Rescale factor must be specified if do_rescale is True.') # All transformations expect numpy arrays. snake_case_ = [to_numpy_array(lowerCAmelCase__) for image in images] if do_resize: snake_case_ = [self.resize(image=lowerCAmelCase__, size=lowerCAmelCase__, resample=lowerCAmelCase__) for image in images] if do_center_crop: snake_case_ = [self.center_crop(image=lowerCAmelCase__, size=lowerCAmelCase__) for image in images] if do_rescale: snake_case_ = [self.rescale(image=lowerCAmelCase__, scale=lowerCAmelCase__) for image in images] if do_normalize: snake_case_ = [self.normalize(image=lowerCAmelCase__, mean=lowerCAmelCase__, std=lowerCAmelCase__) for image in images] snake_case_ = [to_channel_dimension_format(lowerCAmelCase__, lowerCAmelCase__) for image in images] snake_case_ = {'pixel_values': images} return BatchFeature(data=lowerCAmelCase__, tensor_type=lowerCAmelCase__)
69
import json import os import shutil import tempfile from unittest import TestCase from transformers import BartTokenizer, BartTokenizerFast, DPRQuestionEncoderTokenizer, DPRQuestionEncoderTokenizerFast from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_tokenizers, require_torch, slow from transformers.utils import is_datasets_available, is_faiss_available, is_torch_available if is_torch_available() and is_datasets_available() and is_faiss_available(): from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.tokenization_rag import RagTokenizer @require_faiss @require_torch class __A ( a ): """simple docstring""" def __lowercase ( self ): """simple docstring""" __UpperCamelCase : str =tempfile.mkdtemp() __UpperCamelCase : Optional[int] =8 # DPR tok __UpperCamelCase : str =[ '[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] __UpperCamelCase : Optional[Any] =os.path.join(self.tmpdirname , 'dpr_tokenizer' ) os.makedirs(lowerCamelCase__ , exist_ok=lowerCamelCase__ ) __UpperCamelCase : Dict =os.path.join(lowerCamelCase__ , DPR_VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) # BART tok __UpperCamelCase : Optional[int] =[ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', '\u0120', '\u0120l', '\u0120n', '\u0120lo', '\u0120low', 'er', '\u0120lowest', '\u0120newer', '\u0120wider', '<unk>', ] __UpperCamelCase : str =dict(zip(lowerCamelCase__ , range(len(lowerCamelCase__ ) ) ) ) __UpperCamelCase : Optional[int] =['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', ''] __UpperCamelCase : Any ={'unk_token': '<unk>'} __UpperCamelCase : Any =os.path.join(self.tmpdirname , 'bart_tokenizer' ) os.makedirs(lowerCamelCase__ , exist_ok=lowerCamelCase__ ) __UpperCamelCase : Any =os.path.join(lowerCamelCase__ , BART_VOCAB_FILES_NAMES['vocab_file'] ) __UpperCamelCase : Dict =os.path.join(lowerCamelCase__ , BART_VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(lowerCamelCase__ ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(lowerCamelCase__ ) ) def __lowercase ( self ): """simple docstring""" return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , 'dpr_tokenizer' ) ) def __lowercase ( self ): """simple docstring""" return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , 'bart_tokenizer' ) ) def __lowercase ( self ): """simple docstring""" shutil.rmtree(self.tmpdirname ) @require_tokenizers def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =os.path.join(self.tmpdirname , 'rag_tokenizer' ) __UpperCamelCase : Dict =RagConfig(question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() ) __UpperCamelCase : List[Any] =RagTokenizer(question_encoder=self.get_dpr_tokenizer() , generator=self.get_bart_tokenizer() ) rag_config.save_pretrained(lowerCamelCase__ ) rag_tokenizer.save_pretrained(lowerCamelCase__ ) __UpperCamelCase : int =RagTokenizer.from_pretrained(lowerCamelCase__ , config=lowerCamelCase__ ) self.assertIsInstance(new_rag_tokenizer.question_encoder , lowerCamelCase__ ) self.assertEqual(new_rag_tokenizer.question_encoder.get_vocab() , rag_tokenizer.question_encoder.get_vocab() ) self.assertIsInstance(new_rag_tokenizer.generator , lowerCamelCase__ ) self.assertEqual(new_rag_tokenizer.generator.get_vocab() , rag_tokenizer.generator.get_vocab() ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Dict =RagTokenizer.from_pretrained('facebook/rag-token-nq' ) __UpperCamelCase : Union[str, Any] =[ 'who got the first nobel prize in physics', 'when is the next deadpool movie being released', 'which mode is used for short wave broadcast service', 'who is the owner of reading football club', 'when is the next scandal episode coming out', 'when is the last time the philadelphia won the superbowl', 'what is the most current adobe flash player version', 'how many episodes are there in dragon ball z', 'what is the first step in the evolution of the eye', 'where is gall bladder situated in human body', 'what is the main mineral in lithium batteries', 'who is the president of usa right now', 'where do the greasers live in the outsiders', 'panda is a national animal of which country', 'what is the name of manchester united stadium', ] __UpperCamelCase : int =tokenizer(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) @slow def __lowercase ( self ): """simple docstring""" __UpperCamelCase : List[str] =RagTokenizer.from_pretrained('facebook/rag-sequence-nq' ) __UpperCamelCase : Union[str, Any] =[ 'who got the first nobel prize in physics', 'when is the next deadpool movie being released', 'which mode is used for short wave broadcast service', 'who is the owner of reading football club', 'when is the next scandal episode coming out', 'when is the last time the philadelphia won the superbowl', 'what is the most current adobe flash player version', 'how many episodes are there in dragon ball z', 'what is the first step in the evolution of the eye', 'where is gall bladder situated in human body', 'what is the main mineral in lithium batteries', 'who is the president of usa right now', 'where do the greasers live in the outsiders', 'panda is a national animal of which country', 'what is the name of manchester united stadium', ] __UpperCamelCase : Any =tokenizer(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ )
71
0
import os import zipfile import pytest from datasets.utils.extract import ( BzipaExtractor, Extractor, GzipExtractor, LzaExtractor, SevenZipExtractor, TarExtractor, XzExtractor, ZipExtractor, ZstdExtractor, ) from .utils import require_lza, require_pyazr, require_zstandard @pytest.mark.parametrize( 'compression_format, is_archive' , [ ('7z', True), ('bz2', False), ('gzip', False), ('lz4', False), ('tar', True), ('xz', False), ('zip', True), ('zstd', False), ] , ) def lowerCamelCase_ ( UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Any , UpperCamelCase__ : Dict , UpperCamelCase__ : Any , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Tuple , UpperCamelCase__ : Any , ) -> Any: """simple docstring""" __lowerCamelCase = { '7z': (seven_zip_file, SevenZipExtractor), 'bz2': (bza_file, BzipaExtractor), 'gzip': (gz_file, GzipExtractor), 'lz4': (lza_file, LzaExtractor), 'tar': (tar_file, TarExtractor), 'xz': (xz_file, XzExtractor), 'zip': (zip_file, ZipExtractor), 'zstd': (zstd_file, ZstdExtractor), } __lowerCamelCase , __lowerCamelCase = input_paths_and_base_extractors[compression_format] if input_path is None: __lowerCamelCase = F"""for '{compression_format}' compression_format, """ if compression_format == "7z": reason += require_pyazr.kwargs["reason"] elif compression_format == "lz4": reason += require_lza.kwargs["reason"] elif compression_format == "zstd": reason += require_zstandard.kwargs["reason"] pytest.skip(UpperCamelCase__ ) assert base_extractor.is_extractable(UpperCamelCase__ ) __lowerCamelCase = tmp_path / ('extracted' if is_archive else 'extracted.txt') base_extractor.extract(UpperCamelCase__ , UpperCamelCase__ ) if is_archive: assert output_path.is_dir() for file_path in output_path.iterdir(): assert file_path.name == text_file.name __lowerCamelCase = file_path.read_text(encoding='utf-8' ) else: __lowerCamelCase = output_path.read_text(encoding='utf-8' ) __lowerCamelCase = text_file.read_text(encoding='utf-8' ) assert extracted_file_content == expected_file_content @pytest.mark.parametrize( 'compression_format, is_archive' , [ ('7z', True), ('bz2', False), ('gzip', False), ('lz4', False), ('tar', True), ('xz', False), ('zip', True), ('zstd', False), ] , ) def lowerCamelCase_ ( UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Dict , UpperCamelCase__ : int , UpperCamelCase__ : List[Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Tuple , UpperCamelCase__ : Any , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Dict , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Any , ) -> Union[str, Any]: """simple docstring""" __lowerCamelCase = { '7z': seven_zip_file, 'bz2': bza_file, 'gzip': gz_file, 'lz4': lza_file, 'tar': tar_file, 'xz': xz_file, 'zip': zip_file, 'zstd': zstd_file, } __lowerCamelCase = input_paths[compression_format] if input_path is None: __lowerCamelCase = F"""for '{compression_format}' compression_format, """ if compression_format == "7z": reason += require_pyazr.kwargs["reason"] elif compression_format == "lz4": reason += require_lza.kwargs["reason"] elif compression_format == "zstd": reason += require_zstandard.kwargs["reason"] pytest.skip(UpperCamelCase__ ) __lowerCamelCase = Extractor.infer_extractor_format(UpperCamelCase__ ) assert extractor_format is not None __lowerCamelCase = tmp_path / ('extracted' if is_archive else 'extracted.txt') Extractor.extract(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) if is_archive: assert output_path.is_dir() for file_path in output_path.iterdir(): assert file_path.name == text_file.name __lowerCamelCase = file_path.read_text(encoding='utf-8' ) else: __lowerCamelCase = output_path.read_text(encoding='utf-8' ) __lowerCamelCase = text_file.read_text(encoding='utf-8' ) assert extracted_file_content == expected_file_content @pytest.fixture def lowerCamelCase_ ( UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Any ) -> Optional[int]: """simple docstring""" import tarfile __lowerCamelCase = tmp_path / 'data_dot_dot' directory.mkdir() __lowerCamelCase = directory / 'tar_file_with_dot_dot.tar' with tarfile.TarFile(UpperCamelCase__ , 'w' ) as f: f.add(UpperCamelCase__ , arcname=os.path.join('..' , text_file.name ) ) return path @pytest.fixture def lowerCamelCase_ ( UpperCamelCase__ : Dict ) -> Optional[Any]: """simple docstring""" import tarfile __lowerCamelCase = tmp_path / 'data_sym_link' directory.mkdir() __lowerCamelCase = directory / 'tar_file_with_sym_link.tar' os.symlink('..' , directory / 'subdir' , target_is_directory=UpperCamelCase__ ) with tarfile.TarFile(UpperCamelCase__ , 'w' ) as f: f.add(str(directory / 'subdir' ) , arcname='subdir' ) # str required by os.readlink on Windows and Python < 3.8 return path @pytest.mark.parametrize( 'insecure_tar_file, error_log' , [('tar_file_with_dot_dot', 'illegal path'), ('tar_file_with_sym_link', 'Symlink')] , ) def lowerCamelCase_ ( UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Dict , UpperCamelCase__ : int , UpperCamelCase__ : Tuple , UpperCamelCase__ : int , UpperCamelCase__ : str ) -> Optional[int]: """simple docstring""" __lowerCamelCase = { 'tar_file_with_dot_dot': tar_file_with_dot_dot, 'tar_file_with_sym_link': tar_file_with_sym_link, } __lowerCamelCase = insecure_tar_files[insecure_tar_file] __lowerCamelCase = tmp_path / 'extracted' TarExtractor.extract(UpperCamelCase__ , UpperCamelCase__ ) assert caplog.text for record in caplog.records: assert record.levelname == "ERROR" assert error_log in record.msg def lowerCamelCase_ ( UpperCamelCase__ : Optional[Any] ) -> Any: """simple docstring""" __lowerCamelCase = tmpdir / 'not_a_zip_file' # From: https://github.com/python/cpython/pull/5053 __lowerCamelCase = ( b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00' b'\x00\x02\x08\x06\x00\x00\x00\x99\x81\xb6\'\x00\x00\x00\x15I' b'DATx\x01\x01\n\x00\xf5\xff\x00PK\x05\x06\x00PK\x06\x06\x07' b'\xac\x01N\xc6|a\r\x00\x00\x00\x00IEND\xaeB`\x82' ) with not_a_zip_file.open('wb' ) as f: f.write(UpperCamelCase__ ) assert zipfile.is_zipfile(str(UpperCamelCase__ ) ) # is a false positive for `zipfile` assert not ZipExtractor.is_extractable(UpperCamelCase__ ) # but we're right
348
__A = { "joule": 1.0, "kilojoule": 10_00, "megajoule": 1_00_00_00, "gigajoule": 10_00_00_00_00, "wattsecond": 1.0, "watthour": 36_00, "kilowatthour": 3_60_00_00, "newtonmeter": 1.0, "calorie_nutr": 41_86.8, "kilocalorie_nutr": 4_18_68_00.00, "electronvolt": 1.6_0_2_1_7_6_6_3_4e-1_9, "britishthermalunit_it": 10_55.0_55_85, "footpound": 1.3_5_5_8_1_8, } def lowerCamelCase_ ( UpperCamelCase__ : str , UpperCamelCase__ : str , UpperCamelCase__ : float ) -> float: """simple docstring""" if to_type not in ENERGY_CONVERSION or from_type not in ENERGY_CONVERSION: __lowerCamelCase = ( F"""Incorrect 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\n""" F"""Valid values are: {', '.join(UpperCamelCase__ )}""" ) raise ValueError(UpperCamelCase__ ) return value * ENERGY_CONVERSION[from_type] / ENERGY_CONVERSION[to_type] if __name__ == "__main__": import doctest doctest.testmod()
348
1
'''simple docstring''' import inspect import unittest from transformers import DecisionTransformerConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import DecisionTransformerModel from transformers.models.decision_transformer.modeling_decision_transformer import ( DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ) class a : def __init__( self , __magic_name__ , __magic_name__=13 , __magic_name__=7 , __magic_name__=6 , __magic_name__=17 , __magic_name__=23 , __magic_name__=11 , __magic_name__=True , ) -> Any: _a = parent _a = batch_size _a = seq_length _a = act_dim _a = state_dim _a = hidden_size _a = max_length _a = is_training def __UpperCAmelCase ( self ) -> Dict: _a = floats_tensor((self.batch_size, self.seq_length, self.state_dim) ) _a = floats_tensor((self.batch_size, self.seq_length, self.act_dim) ) _a = floats_tensor((self.batch_size, self.seq_length, 1) ) _a = floats_tensor((self.batch_size, self.seq_length, 1) ) _a = ids_tensor((self.batch_size, self.seq_length) , vocab_size=10_00 ) _a = random_attention_mask((self.batch_size, self.seq_length) ) _a = self.get_config() return ( config, states, actions, rewards, returns_to_go, timesteps, attention_mask, ) def __UpperCAmelCase ( self ) -> List[str]: return DecisionTransformerConfig( batch_size=self.batch_size , seq_length=self.seq_length , act_dim=self.act_dim , state_dim=self.state_dim , hidden_size=self.hidden_size , max_length=self.max_length , ) def __UpperCAmelCase ( self , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , ) -> Tuple: _a = DecisionTransformerModel(config=__magic_name__ ) model.to(__magic_name__ ) model.eval() _a = model(__magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) self.parent.assertEqual(result.state_preds.shape , states.shape ) self.parent.assertEqual(result.action_preds.shape , actions.shape ) self.parent.assertEqual(result.return_preds.shape , returns_to_go.shape ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.seq_length * 3, self.hidden_size) ) # seq length *3 as there are 3 modelities: states, returns and actions def __UpperCAmelCase ( self ) -> Dict: _a = self.prepare_config_and_inputs() ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = config_and_inputs _a = { 'states': states, 'actions': actions, 'rewards': rewards, 'returns_to_go': returns_to_go, 'timesteps': timesteps, 'attention_mask': attention_mask, } return config, inputs_dict @require_torch class a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , unittest.TestCase ): _lowerCAmelCase = (DecisionTransformerModel,) if is_torch_available() else () _lowerCAmelCase = () _lowerCAmelCase = {"""feature-extraction""": DecisionTransformerModel} if is_torch_available() else {} # Ignoring of a failing test from GenerationTesterMixin, as the model does not use inputs_ids _lowerCAmelCase = False # Ignoring of a failing tests from ModelTesterMixin, as the model does not implement these features _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False def __UpperCAmelCase ( self ) -> Optional[int]: _a = DecisionTransformerModelTester(self ) _a = ConfigTester(self , config_class=__magic_name__ , hidden_size=37 ) def __UpperCAmelCase ( self ) -> List[Any]: self.config_tester.run_common_tests() def __UpperCAmelCase ( self ) -> Optional[Any]: _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__magic_name__ ) @slow def __UpperCAmelCase ( self ) -> Optional[Any]: for model_name in DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a = DecisionTransformerModel.from_pretrained(__magic_name__ ) self.assertIsNotNone(__magic_name__ ) def __UpperCAmelCase ( self ) -> Union[str, Any]: _a , _a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a = model_class(__magic_name__ ) _a = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a = [*signature.parameters.keys()] _a = [ 'states', 'actions', 'rewards', 'returns_to_go', 'timesteps', 'attention_mask', ] self.assertListEqual(arg_names[: len(__magic_name__ )] , __magic_name__ ) @require_torch class a ( unittest.TestCase ): @slow def __UpperCAmelCase ( self ) -> Optional[Any]: _a = 2 # number of steps of autoregressive prediction we will perform _a = 10 # defined by the RL environment, may be normalized _a = DecisionTransformerModel.from_pretrained('edbeeching/decision-transformer-gym-hopper-expert' ) _a = model.to(__magic_name__ ) _a = model.config torch.manual_seed(0 ) _a = torch.randn(1 , 1 , config.state_dim ).to(device=__magic_name__ , dtype=torch.floataa ) # env.reset() _a = torch.tensor( [[0.2_4_2_7_9_3, -0.2_8_6_9_3_0_7_4, 0.8_7_4_2_6_1_3], [0.6_7_8_1_5_2_7_4, -0.0_8_1_0_1_0_8_5, -0.1_2_9_5_2_1_4_7]] , device=__magic_name__ ) _a = torch.tensor(__magic_name__ , device=__magic_name__ , dtype=torch.floataa ).reshape(1 , 1 , 1 ) _a = state _a = torch.zeros(1 , 0 , config.act_dim , device=__magic_name__ , dtype=torch.floataa ) _a = torch.zeros(1 , 0 , device=__magic_name__ , dtype=torch.floataa ) _a = torch.tensor(0 , device=__magic_name__ , dtype=torch.long ).reshape(1 , 1 ) for step in range(__magic_name__ ): _a = torch.cat([actions, torch.zeros(1 , 1 , config.act_dim , device=__magic_name__ )] , dim=1 ) _a = torch.cat([rewards, torch.zeros(1 , 1 , device=__magic_name__ )] , dim=1 ) _a = torch.ones(1 , states.shape[1] ).to(dtype=torch.long , device=states.device ) with torch.no_grad(): _a , _a , _a = model( states=__magic_name__ , actions=__magic_name__ , rewards=__magic_name__ , returns_to_go=__magic_name__ , timesteps=__magic_name__ , attention_mask=__magic_name__ , return_dict=__magic_name__ , ) self.assertEqual(action_pred.shape , actions.shape ) self.assertTrue(torch.allclose(action_pred[0, -1] , expected_outputs[step] , atol=1e-4 ) ) _a , _a , _a , _a = ( # env.step(action) torch.randn(1 , 1 , config.state_dim ).to(device=__magic_name__ , dtype=torch.floataa ), 1.0, False, {}, ) _a = action_pred[0, -1] _a = torch.cat([states, state] , dim=1 ) _a = returns_to_go[0, -1] - reward _a = torch.cat([returns_to_go, pred_return.reshape(1 , 1 , 1 )] , dim=1 ) _a = torch.cat( [timesteps, torch.ones((1, 1) , device=__magic_name__ , dtype=torch.long ) * (step + 1)] , dim=1 )
168
'''simple docstring''' def _A (lowerCAmelCase__ :int , lowerCAmelCase__ :int ) -> int: '''simple docstring''' return int(input_a == input_a == 0 ) def _A () -> None: '''simple docstring''' print('Truth Table of NOR Gate:' ) print('| Input 1 | Input 2 | Output |' ) print(f'| 0 | 0 | {nor_gate(0 , 0 )} |' ) print(f'| 0 | 1 | {nor_gate(0 , 1 )} |' ) print(f'| 1 | 0 | {nor_gate(1 , 0 )} |' ) print(f'| 1 | 1 | {nor_gate(1 , 1 )} |' ) if __name__ == "__main__": import doctest doctest.testmod() main()
168
1
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import numpy as np import torch from ..models.clipseg import CLIPSegForImageSegmentation from ..utils import is_vision_available, requires_backends from .base import PipelineTool if is_vision_available(): from PIL import Image class _snake_case ( __snake_case ): '''simple docstring''' A__ : Union[str, Any] = ( "This is a tool that creates a segmentation mask of an image according to a label. It cannot create an image." "It takes two arguments named `image` which should be the original image, and `label` which should be a text " "describing the elements what should be identified in the segmentation mask. The tool returns the mask." ) A__ : List[str] = "CIDAS/clipseg-rd64-refined" A__ : str = "image_segmenter" A__ : int = CLIPSegForImageSegmentation A__ : List[Any] = ["image", "text"] A__ : Union[str, Any] = ["image"] def __init__( self: Tuple ,*lowerCamelCase_: str ,**lowerCamelCase_: Tuple ) -> List[Any]: requires_backends(self ,["""vision"""] ) super().__init__(*lowerCamelCase_ ,**lowerCamelCase_ ) def A__ ( self: List[Any] ,lowerCamelCase_: "Image" ,lowerCamelCase_: str ) -> Any: return self.pre_processor(text=[label] ,images=[image] ,padding=lowerCamelCase_ ,return_tensors="""pt""" ) def A__ ( self: Union[str, Any] ,lowerCamelCase_: int ) -> Union[str, Any]: with torch.no_grad(): UpperCAmelCase_ : Dict = self.model(**lowerCamelCase_ ).logits return logits def A__ ( self: Union[str, Any] ,lowerCamelCase_: Union[str, Any] ) -> Optional[int]: UpperCAmelCase_ : Tuple = outputs.cpu().detach().numpy() UpperCAmelCase_ : Dict = 0 UpperCAmelCase_ : List[Any] = 1 return Image.fromarray((array * 255).astype(np.uinta ) )
59
import argparse import json from dataclasses import dataclass, field from functools import partial from pathlib import Path from typing import Callable, Dict, List, Tuple import timm import torch import torch.nn as nn from classy_vision.models.regnet import RegNet, RegNetParams, RegNetYaagf, RegNetYaagf, RegNetYaaagf from huggingface_hub import cached_download, hf_hub_url from torch import Tensor from vissl.models.model_helpers import get_trunk_forward_outputs from transformers import AutoImageProcessor, RegNetConfig, RegNetForImageClassification, RegNetModel from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase_ = logging.get_logger() @dataclass class _snake_case : '''simple docstring''' A__ : nn.Module A__ : List[nn.Module] = field(default_factory=__snake_case ) A__ : list = field(default_factory=__snake_case ) def A__ ( self: str ,lowerCamelCase_: List[str] ,lowerCamelCase_: Tensor ,lowerCamelCase_: Tensor ) -> Optional[int]: UpperCAmelCase_ : Dict = len(list(m.modules() ) ) == 1 or isinstance(lowerCamelCase_ ,nn.Convad ) or isinstance(lowerCamelCase_ ,nn.BatchNormad ) if has_not_submodules: self.traced.append(lowerCamelCase_ ) def __call__( self: Tuple ,lowerCamelCase_: Tensor ) -> Dict: for m in self.module.modules(): self.handles.append(m.register_forward_hook(self._forward_hook ) ) self.module(lowerCamelCase_ ) [x.remove() for x in self.handles] return self @property def A__ ( self: List[str] ) -> int: # check the len of the state_dict keys to see if we have learnable params return list(filter(lambda lowerCamelCase_ : len(list(x.state_dict().keys() ) ) > 0 ,self.traced ) ) @dataclass class _snake_case : '''simple docstring''' A__ : nn.Module A__ : nn.Module A__ : int = 1 A__ : List = field(default_factory=__snake_case ) A__ : List = field(default_factory=__snake_case ) A__ : bool = True def __call__( self: Tuple ,lowerCamelCase_: Tensor ) -> Optional[Any]: UpperCAmelCase_ : List[str] = Tracker(self.dest )(lowerCamelCase_ ).parametrized UpperCAmelCase_ : Any = Tracker(self.src )(lowerCamelCase_ ).parametrized UpperCAmelCase_ : int = list(filter(lambda lowerCamelCase_ : type(lowerCamelCase_ ) not in self.src_skip ,lowerCamelCase_ ) ) UpperCAmelCase_ : Optional[int] = list(filter(lambda lowerCamelCase_ : type(lowerCamelCase_ ) not in self.dest_skip ,lowerCamelCase_ ) ) if len(lowerCamelCase_ ) != len(lowerCamelCase_ ) and self.raise_if_mismatch: raise Exception( F'''Numbers of operations are different. Source module has {len(lowerCamelCase_ )} operations while''' F''' destination module has {len(lowerCamelCase_ )}.''' ) for dest_m, src_m in zip(lowerCamelCase_ ,lowerCamelCase_ ): dest_m.load_state_dict(src_m.state_dict() ) if self.verbose == 1: print(F'''Transfered from={src_m} to={dest_m}''' ) class _snake_case ( nn.Module ): '''simple docstring''' def __init__( self: List[str] ,lowerCamelCase_: nn.Module ) -> List[str]: super().__init__() UpperCAmelCase_ : List[Tuple[str, nn.Module]] = [] # - get the stem feature_blocks.append(("""conv1""", model.stem) ) # - get all the feature blocks for k, v in model.trunk_output.named_children(): assert k.startswith("""block""" ), F'''Unexpected layer name {k}''' UpperCAmelCase_ : Tuple = len(lowerCamelCase_ ) + 1 feature_blocks.append((F'''res{block_index}''', v) ) UpperCAmelCase_ : Optional[int] = nn.ModuleDict(lowerCamelCase_ ) def A__ ( self: Union[str, Any] ,lowerCamelCase_: Tensor ) -> List[str]: return get_trunk_forward_outputs( lowerCamelCase_ ,out_feat_keys=lowerCamelCase_ ,feature_blocks=self._feature_blocks ,) class _snake_case ( __snake_case ): '''simple docstring''' def A__ ( self: Dict ,lowerCamelCase_: str ) -> str: UpperCAmelCase_ : str = x.split("""-""" ) return x_split[0] + x_split[1] + "_" + "".join(x_split[2:] ) def __getitem__( self: Union[str, Any] ,lowerCamelCase_: str ) -> Callable[[], Tuple[nn.Module, Dict]]: # default to timm! if x not in self: UpperCAmelCase_ : str = self.convert_name_to_timm(lowerCamelCase_ ) UpperCAmelCase_ : List[str] = partial(lambda: (timm.create_model(lowerCamelCase_ ,pretrained=lowerCamelCase_ ).eval(), None) ) else: UpperCAmelCase_ : Optional[int] = super().__getitem__(lowerCamelCase_ ) return val class _snake_case ( __snake_case ): '''simple docstring''' def __getitem__( self: Union[str, Any] ,lowerCamelCase_: str ) -> Callable[[], nn.Module]: if "seer" in x and "in1k" not in x: UpperCAmelCase_ : Tuple = RegNetModel else: UpperCAmelCase_ : Union[str, Any] = RegNetForImageClassification return val def lowerCamelCase_ ( _a : str , _a : int , _a : List[Tuple[str, str]] ): '''simple docstring''' for from_key, to_key in keys: UpperCAmelCase_ : int = from_state_dict[from_key].clone() print(F'''Copied key={from_key} to={to_key}''' ) return to_state_dict def lowerCamelCase_ ( _a : str , _a : Callable[[], nn.Module] , _a : Callable[[], nn.Module] , _a : RegNetConfig , _a : Path , _a : bool = True , ): '''simple docstring''' print(F'''Converting {name}...''' ) with torch.no_grad(): UpperCAmelCase_ , UpperCAmelCase_ : Any = from_model_func() UpperCAmelCase_ : str = our_model_func(_a ).eval() UpperCAmelCase_ : List[Any] = ModuleTransfer(src=_a , dest=_a , raise_if_mismatch=_a ) UpperCAmelCase_ : List[str] = torch.randn((1, 3, 224, 224) ) module_transfer(_a ) if from_state_dict is not None: UpperCAmelCase_ : List[str] = [] # for seer - in1k finetuned we have to manually copy the head if "seer" in name and "in1k" in name: UpperCAmelCase_ : List[Any] = [("""0.clf.0.weight""", """classifier.1.weight"""), ("""0.clf.0.bias""", """classifier.1.bias""")] UpperCAmelCase_ : str = manually_copy_vissl_head(_a , our_model.state_dict() , _a ) our_model.load_state_dict(_a ) UpperCAmelCase_ : Union[str, Any] = our_model(_a , output_hidden_states=_a ) UpperCAmelCase_ : int = ( our_outputs.logits if isinstance(_a , _a ) else our_outputs.last_hidden_state ) UpperCAmelCase_ : Optional[int] = from_model(_a ) UpperCAmelCase_ : List[Any] = from_output[-1] if type(_a ) is list else from_output # now since I don't want to use any config files, vissl seer model doesn't actually have an head, so let's just check the last hidden state if "seer" in name and "in1k" in name: UpperCAmelCase_ : Union[str, Any] = our_outputs.hidden_states[-1] assert torch.allclose(_a , _a ), "The model logits don't match the original one." if push_to_hub: our_model.push_to_hub( repo_path_or_name=save_directory / name , commit_message="""Add model""" , use_temp_dir=_a , ) UpperCAmelCase_ : Union[str, Any] = 224 if """seer""" not in name else 384 # we can use the convnext one UpperCAmelCase_ : Optional[int] = AutoImageProcessor.from_pretrained("""facebook/convnext-base-224-22k-1k""" , size=_a ) image_processor.push_to_hub( repo_path_or_name=save_directory / name , commit_message="""Add image processor""" , use_temp_dir=_a , ) print(F'''Pushed {name}''' ) def lowerCamelCase_ ( _a : Path , _a : str = None , _a : bool = True ): '''simple docstring''' UpperCAmelCase_ : Optional[int] = """imagenet-1k-id2label.json""" UpperCAmelCase_ : List[Any] = 1000 UpperCAmelCase_ : Any = (1, num_labels) UpperCAmelCase_ : Tuple = """huggingface/label-files""" UpperCAmelCase_ : List[Any] = num_labels UpperCAmelCase_ : List[str] = json.load(open(cached_download(hf_hub_url(_a , _a , repo_type="""dataset""" ) ) , """r""" ) ) UpperCAmelCase_ : Union[str, Any] = {int(_a ): v for k, v in idalabel.items()} UpperCAmelCase_ : Tuple = idalabel UpperCAmelCase_ : Any = {v: k for k, v in idalabel.items()} UpperCAmelCase_ : Union[str, Any] = partial(_a , num_labels=_a , idalabel=_a , labelaid=_a ) UpperCAmelCase_ : List[Any] = { """regnet-x-002""": ImageNetPreTrainedConfig( depths=[1, 1, 4, 7] , hidden_sizes=[24, 56, 152, 368] , groups_width=8 , layer_type="""x""" ), """regnet-x-004""": ImageNetPreTrainedConfig( depths=[1, 2, 7, 12] , hidden_sizes=[32, 64, 160, 384] , groups_width=16 , layer_type="""x""" ), """regnet-x-006""": ImageNetPreTrainedConfig( depths=[1, 3, 5, 7] , hidden_sizes=[48, 96, 240, 528] , groups_width=24 , layer_type="""x""" ), """regnet-x-008""": ImageNetPreTrainedConfig( depths=[1, 3, 7, 5] , hidden_sizes=[64, 128, 288, 672] , groups_width=16 , layer_type="""x""" ), """regnet-x-016""": ImageNetPreTrainedConfig( depths=[2, 4, 10, 2] , hidden_sizes=[72, 168, 408, 912] , groups_width=24 , layer_type="""x""" ), """regnet-x-032""": ImageNetPreTrainedConfig( depths=[2, 6, 15, 2] , hidden_sizes=[96, 192, 432, 1008] , groups_width=48 , layer_type="""x""" ), """regnet-x-040""": ImageNetPreTrainedConfig( depths=[2, 5, 14, 2] , hidden_sizes=[80, 240, 560, 1360] , groups_width=40 , layer_type="""x""" ), """regnet-x-064""": ImageNetPreTrainedConfig( depths=[2, 4, 10, 1] , hidden_sizes=[168, 392, 784, 1624] , groups_width=56 , layer_type="""x""" ), """regnet-x-080""": ImageNetPreTrainedConfig( depths=[2, 5, 15, 1] , hidden_sizes=[80, 240, 720, 1920] , groups_width=120 , layer_type="""x""" ), """regnet-x-120""": ImageNetPreTrainedConfig( depths=[2, 5, 11, 1] , hidden_sizes=[224, 448, 896, 2240] , groups_width=112 , layer_type="""x""" ), """regnet-x-160""": ImageNetPreTrainedConfig( depths=[2, 6, 13, 1] , hidden_sizes=[256, 512, 896, 2048] , groups_width=128 , layer_type="""x""" ), """regnet-x-320""": ImageNetPreTrainedConfig( depths=[2, 7, 13, 1] , hidden_sizes=[336, 672, 1344, 2520] , groups_width=168 , layer_type="""x""" ), # y variant """regnet-y-002""": ImageNetPreTrainedConfig(depths=[1, 1, 4, 7] , hidden_sizes=[24, 56, 152, 368] , groups_width=8 ), """regnet-y-004""": ImageNetPreTrainedConfig( depths=[1, 3, 6, 6] , hidden_sizes=[48, 104, 208, 440] , groups_width=8 ), """regnet-y-006""": ImageNetPreTrainedConfig( depths=[1, 3, 7, 4] , hidden_sizes=[48, 112, 256, 608] , groups_width=16 ), """regnet-y-008""": ImageNetPreTrainedConfig( depths=[1, 3, 8, 2] , hidden_sizes=[64, 128, 320, 768] , groups_width=16 ), """regnet-y-016""": ImageNetPreTrainedConfig( depths=[2, 6, 17, 2] , hidden_sizes=[48, 120, 336, 888] , groups_width=24 ), """regnet-y-032""": ImageNetPreTrainedConfig( depths=[2, 5, 13, 1] , hidden_sizes=[72, 216, 576, 1512] , groups_width=24 ), """regnet-y-040""": ImageNetPreTrainedConfig( depths=[2, 6, 12, 2] , hidden_sizes=[128, 192, 512, 1088] , groups_width=64 ), """regnet-y-064""": ImageNetPreTrainedConfig( depths=[2, 7, 14, 2] , hidden_sizes=[144, 288, 576, 1296] , groups_width=72 ), """regnet-y-080""": ImageNetPreTrainedConfig( depths=[2, 4, 10, 1] , hidden_sizes=[168, 448, 896, 2016] , groups_width=56 ), """regnet-y-120""": ImageNetPreTrainedConfig( depths=[2, 5, 11, 1] , hidden_sizes=[224, 448, 896, 2240] , groups_width=112 ), """regnet-y-160""": ImageNetPreTrainedConfig( depths=[2, 4, 11, 1] , hidden_sizes=[224, 448, 1232, 3024] , groups_width=112 ), """regnet-y-320""": ImageNetPreTrainedConfig( depths=[2, 5, 12, 1] , hidden_sizes=[232, 696, 1392, 3712] , groups_width=232 ), # models created by SEER -> https://arxiv.org/abs/2202.08360 """regnet-y-320-seer""": RegNetConfig(depths=[2, 5, 12, 1] , hidden_sizes=[232, 696, 1392, 3712] , groups_width=232 ), """regnet-y-640-seer""": RegNetConfig(depths=[2, 5, 12, 1] , hidden_sizes=[328, 984, 1968, 4920] , groups_width=328 ), """regnet-y-1280-seer""": RegNetConfig( depths=[2, 7, 17, 1] , hidden_sizes=[528, 1056, 2904, 7392] , groups_width=264 ), """regnet-y-2560-seer""": RegNetConfig( depths=[3, 7, 16, 1] , hidden_sizes=[640, 1696, 2544, 5088] , groups_width=640 ), """regnet-y-10b-seer""": ImageNetPreTrainedConfig( depths=[2, 7, 17, 1] , hidden_sizes=[2020, 4040, 1_1110, 2_8280] , groups_width=1010 ), # finetuned on imagenet """regnet-y-320-seer-in1k""": ImageNetPreTrainedConfig( depths=[2, 5, 12, 1] , hidden_sizes=[232, 696, 1392, 3712] , groups_width=232 ), """regnet-y-640-seer-in1k""": ImageNetPreTrainedConfig( depths=[2, 5, 12, 1] , hidden_sizes=[328, 984, 1968, 4920] , groups_width=328 ), """regnet-y-1280-seer-in1k""": ImageNetPreTrainedConfig( depths=[2, 7, 17, 1] , hidden_sizes=[528, 1056, 2904, 7392] , groups_width=264 ), """regnet-y-2560-seer-in1k""": ImageNetPreTrainedConfig( depths=[3, 7, 16, 1] , hidden_sizes=[640, 1696, 2544, 5088] , groups_width=640 ), """regnet-y-10b-seer-in1k""": ImageNetPreTrainedConfig( depths=[2, 7, 17, 1] , hidden_sizes=[2020, 4040, 1_1110, 2_8280] , groups_width=1010 ), } UpperCAmelCase_ : List[Any] = NameToOurModelFuncMap() UpperCAmelCase_ : Union[str, Any] = NameToFromModelFuncMap() # add seer weights logic def load_using_classy_vision(_a : str , _a : Callable[[], nn.Module] ) -> Tuple[nn.Module, Dict]: UpperCAmelCase_ : Optional[Any] = torch.hub.load_state_dict_from_url(_a , model_dir=str(_a ) , map_location="""cpu""" ) UpperCAmelCase_ : Union[str, Any] = model_func() # check if we have a head, if yes add it UpperCAmelCase_ : Optional[Any] = files["""classy_state_dict"""]["""base_model"""]["""model"""] UpperCAmelCase_ : Optional[Any] = model_state_dict["""trunk"""] model.load_state_dict(_a ) return model.eval(), model_state_dict["heads"] # pretrained UpperCAmelCase_ : List[Any] = partial( _a , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet32d/seer_regnet32gf_model_iteration244000.torch""" , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) UpperCAmelCase_ : str = partial( _a , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet64/seer_regnet64gf_model_final_checkpoint_phase0.torch""" , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) UpperCAmelCase_ : Tuple = partial( _a , """https://dl.fbaipublicfiles.com/vissl/model_zoo/swav_ig1b_regnet128Gf_cnstant_bs32_node16_sinkhorn10_proto16k_syncBN64_warmup8k/model_final_checkpoint_phase0.torch""" , lambda: FakeRegNetVisslWrapper(RegNetYaaagf() ) , ) UpperCAmelCase_ : Optional[int] = partial( _a , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet10B/model_iteration124500_conso.torch""" , lambda: FakeRegNetVisslWrapper( RegNet(RegNetParams(depth=27 , group_width=1010 , w_a=1744 , w_a=6_2_0.8_3 , w_m=2.5_2 ) ) ) , ) # IN1K finetuned UpperCAmelCase_ : Dict = partial( _a , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet32_finetuned_in1k_model_final_checkpoint_phase78.torch""" , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) UpperCAmelCase_ : Dict = partial( _a , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet64_finetuned_in1k_model_final_checkpoint_phase78.torch""" , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) UpperCAmelCase_ : Any = partial( _a , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet128_finetuned_in1k_model_final_checkpoint_phase78.torch""" , lambda: FakeRegNetVisslWrapper(RegNetYaaagf() ) , ) UpperCAmelCase_ : List[Any] = partial( _a , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_10b_finetuned_in1k_model_phase28_conso.torch""" , lambda: FakeRegNetVisslWrapper( RegNet(RegNetParams(depth=27 , group_width=1010 , w_a=1744 , w_a=6_2_0.8_3 , w_m=2.5_2 ) ) ) , ) if model_name: convert_weight_and_push( _a , names_to_from_model_map[model_name] , names_to_ours_model_map[model_name] , names_to_config[model_name] , _a , _a , ) else: for model_name, config in names_to_config.items(): convert_weight_and_push( _a , names_to_from_model_map[model_name] , names_to_ours_model_map[model_name] , _a , _a , _a , ) return config, expected_shape if __name__ == "__main__": UpperCamelCase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default=None, type=str, help=( '''The name of the model you wish to convert, it must be one of the supported regnet* architecture,''' ''' currently: regnetx-*, regnety-*. If `None`, all of them will the converted.''' ), ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=Path, required=True, help='''Path to the output PyTorch model directory.''', ) parser.add_argument( '''--push_to_hub''', default=True, type=bool, required=False, help='''If True, push model and image processor to the hub.''', ) UpperCamelCase_ = parser.parse_args() UpperCamelCase_ = args.pytorch_dump_folder_path pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True) convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)
59
1
import pytest from datasets.parallel import ParallelBackendConfig, parallel_backend from datasets.utils.py_utils import map_nested from .utils import require_dill_gt_0_3_2, require_joblibspark, require_not_windows def A_ ( _lowerCAmelCase ) -> int: # picklable for multiprocessing return i + 1 @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows def A_ ( ) -> int: with parallel_backend("spark" ): assert ParallelBackendConfig.backend_name == "spark" UpperCamelCase : int = [1, 2, 3] with pytest.raises(_lowerCAmelCase ): with parallel_backend("unsupported backend" ): map_nested(_lowerCAmelCase , _lowerCAmelCase , num_proc=2 ) with pytest.raises(_lowerCAmelCase ): with parallel_backend("unsupported backend" ): map_nested(_lowerCAmelCase , _lowerCAmelCase , num_proc=-1 ) @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows @pytest.mark.parametrize("num_proc" , [2, -1] ) def A_ ( _lowerCAmelCase ) -> List[Any]: UpperCamelCase : Any = [1, 2] UpperCamelCase : Dict = {"a": 1, "b": 2} UpperCamelCase : Union[str, Any] = {"a": [1, 2], "b": [3, 4]} UpperCamelCase : List[Any] = {"a": {"1": 1}, "b": 2} UpperCamelCase : Dict = {"a": 1, "b": 2, "c": 3, "d": 4} UpperCamelCase : Any = [2, 3] UpperCamelCase : Any = {"a": 2, "b": 3} UpperCamelCase : Any = {"a": [2, 3], "b": [4, 5]} UpperCamelCase : Optional[int] = {"a": {"1": 2}, "b": 3} UpperCamelCase : str = {"a": 2, "b": 3, "c": 4, "d": 5} with parallel_backend("spark" ): assert map_nested(_lowerCAmelCase , _lowerCAmelCase , num_proc=_lowerCAmelCase ) == expected_map_nested_sa assert map_nested(_lowerCAmelCase , _lowerCAmelCase , num_proc=_lowerCAmelCase ) == expected_map_nested_sa assert map_nested(_lowerCAmelCase , _lowerCAmelCase , num_proc=_lowerCAmelCase ) == expected_map_nested_sa assert map_nested(_lowerCAmelCase , _lowerCAmelCase , num_proc=_lowerCAmelCase ) == expected_map_nested_sa assert map_nested(_lowerCAmelCase , _lowerCAmelCase , num_proc=_lowerCAmelCase ) == expected_map_nested_sa
52
import inspect import re from transformers.utils import direct_transformers_import # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_config_docstrings.py lowerCAmelCase = 'src/transformers' # This is to make sure the transformers module imported is the one in the repo. lowerCAmelCase = direct_transformers_import(PATH_TO_TRANSFORMERS) lowerCAmelCase = transformers.models.auto.configuration_auto.CONFIG_MAPPING # Regex pattern used to find the checkpoint mentioned in the docstring of `config_class`. # For example, `[bert-base-uncased](https://huggingface.co/bert-base-uncased)` lowerCAmelCase = re.compile(R'\[(.+?)\]\((https://huggingface\.co/.+?)\)') lowerCAmelCase = { 'DecisionTransformerConfig', 'EncoderDecoderConfig', 'MusicgenConfig', 'RagConfig', 'SpeechEncoderDecoderConfig', 'TimmBackboneConfig', 'VisionEncoderDecoderConfig', 'VisionTextDualEncoderConfig', 'LlamaConfig', } def _a ( SCREAMING_SNAKE_CASE ): """simple docstring""" lowercase__ = None # source code of `config_class` lowercase__ = inspect.getsource(SCREAMING_SNAKE_CASE ) lowercase__ = _re_checkpoint.findall(SCREAMING_SNAKE_CASE ) # Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link. # For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')` for ckpt_name, ckpt_link in checkpoints: # allow the link to end with `/` if ckpt_link.endswith('''/''' ): lowercase__ = ckpt_link[:-1] # verify the checkpoint name corresponds to the checkpoint link lowercase__ = f'https://huggingface.co/{ckpt_name}' if ckpt_link == ckpt_link_from_name: lowercase__ = ckpt_name break return checkpoint def _a ( ): """simple docstring""" lowercase__ = [] for config_class in list(CONFIG_MAPPING.values() ): # Skip deprecated models if "models.deprecated" in config_class.__module__: continue lowercase__ = get_checkpoint_from_config_class(SCREAMING_SNAKE_CASE ) lowercase__ = config_class.__name__ if checkpoint is None and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK: configs_without_checkpoint.append(SCREAMING_SNAKE_CASE ) if len(SCREAMING_SNAKE_CASE ) > 0: lowercase__ = '''\n'''.join(sorted(SCREAMING_SNAKE_CASE ) ) raise ValueError(f'The following configurations don\'t contain any valid checkpoint:\n{message}' ) if __name__ == "__main__": check_config_docstrings_have_checkpoints()
110
0
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from ..models.whisper import WhisperForConditionalGeneration, WhisperProcessor from .base import PipelineTool class __a ( __UpperCamelCase ): __snake_case : int = """openai/whisper-base""" __snake_case : Optional[int] = ( """This is a tool that transcribes an audio into text. It takes an input named `audio` and returns the """ """transcribed text.""" ) __snake_case : List[str] = """transcriber""" __snake_case : str = WhisperProcessor __snake_case : Optional[Any] = WhisperForConditionalGeneration __snake_case : List[Any] = ["""audio"""] __snake_case : List[str] = ["""text"""] def A ( self : int , UpperCAmelCase : List[str] ): return self.pre_processor(UpperCAmelCase , return_tensors="""pt""" ).input_features def A ( self : Union[str, Any] , UpperCAmelCase : Tuple ): return self.model.generate(inputs=UpperCAmelCase ) def A ( self : Optional[int] , UpperCAmelCase : List[str] ): return self.pre_processor.batch_decode(UpperCAmelCase , skip_special_tokens=UpperCAmelCase )[0]
28
import argparse import collections import torch from flax import traverse_util from tax import checkpoints from transformers import TaConfig, TaEncoderModel, TaForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() def __UpperCamelCase ( lowercase__ : Optional[int] , lowercase__ : List[Any] , lowercase__ : Any , lowercase__ : Tuple="attention" ) -> Dict: '''simple docstring''' lowerCAmelCase_ : Any = params[f'{prefix}/layers_{i}/{layer_name}/key/kernel'] lowerCAmelCase_ : Optional[Any] = params[f'{prefix}/layers_{i}/{layer_name}/out/kernel'] lowerCAmelCase_ : str = params[f'{prefix}/layers_{i}/{layer_name}/query/kernel'] lowerCAmelCase_ : Tuple = params[f'{prefix}/layers_{i}/{layer_name}/value/kernel'] return k, o, q, v def __UpperCamelCase ( lowercase__ : List[str] , lowercase__ : Dict , lowercase__ : List[str] , lowercase__ : str=False ) -> int: '''simple docstring''' if split_mlp_wi: lowerCAmelCase_ : List[Any] = params[f'{prefix}/layers_{i}/mlp/wi_0/kernel'] lowerCAmelCase_ : List[Any] = params[f'{prefix}/layers_{i}/mlp/wi_1/kernel'] lowerCAmelCase_ : int = (wi_a, wi_a) else: lowerCAmelCase_ : str = params[f'{prefix}/layers_{i}/mlp/wi/kernel'] lowerCAmelCase_ : int = params[f'{prefix}/layers_{i}/mlp/wo/kernel'] return wi, wo def __UpperCamelCase ( lowercase__ : Optional[int] , lowercase__ : Dict , lowercase__ : Optional[Any] , lowercase__ : Tuple ) -> int: '''simple docstring''' return params[f'{prefix}/layers_{i}/{layer_name}/scale'] def __UpperCamelCase ( lowercase__ : dict , *, lowercase__ : int , lowercase__ : bool ) -> Optional[int]: '''simple docstring''' lowerCAmelCase_ : List[str] = traverse_util.flatten_dict(variables["""target"""] ) lowerCAmelCase_ : List[Any] = {"""/""".join(lowercase__ ): v for k, v in old.items()} # v1.1 models have a gated GeLU with wi_0 and wi_1 instead of wi lowerCAmelCase_ : Dict = """encoder/layers_0/mlp/wi_0/kernel""" in old print("""Split MLP:""" , lowercase__ ) lowerCAmelCase_ : Optional[Any] = collections.OrderedDict() # Shared embeddings. lowerCAmelCase_ : Tuple = old["""token_embedder/embedding"""] # Encoder. for i in range(lowercase__ ): # Block i, layer 0 (Self Attention). lowerCAmelCase_ : Optional[Any] = tax_layer_norm_lookup(lowercase__ , lowercase__ , """encoder""" , """pre_attention_layer_norm""" ) lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ : Tuple = tax_attention_lookup(lowercase__ , lowercase__ , """encoder""" , """attention""" ) lowerCAmelCase_ : Optional[int] = layer_norm lowerCAmelCase_ : Optional[int] = k.T lowerCAmelCase_ : List[Any] = o.T lowerCAmelCase_ : Union[str, Any] = q.T lowerCAmelCase_ : Any = v.T # Block i, layer 1 (MLP). lowerCAmelCase_ : Any = tax_layer_norm_lookup(lowercase__ , lowercase__ , """encoder""" , """pre_mlp_layer_norm""" ) lowerCAmelCase_ , lowerCAmelCase_ : Optional[int] = tax_mlp_lookup(lowercase__ , lowercase__ , """encoder""" , lowercase__ ) lowerCAmelCase_ : str = layer_norm if split_mlp_wi: lowerCAmelCase_ : Optional[int] = wi[0].T lowerCAmelCase_ : Optional[Any] = wi[1].T else: lowerCAmelCase_ : int = wi.T lowerCAmelCase_ : Optional[Any] = wo.T lowerCAmelCase_ : Tuple = old[ """encoder/relpos_bias/rel_embedding""" ].T lowerCAmelCase_ : str = old["""encoder/encoder_norm/scale"""] if not is_encoder_only: # Decoder. for i in range(lowercase__ ): # Block i, layer 0 (Self Attention). lowerCAmelCase_ : int = tax_layer_norm_lookup(lowercase__ , lowercase__ , """decoder""" , """pre_self_attention_layer_norm""" ) lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ : Union[str, Any] = tax_attention_lookup(lowercase__ , lowercase__ , """decoder""" , """self_attention""" ) lowerCAmelCase_ : Dict = layer_norm lowerCAmelCase_ : Union[str, Any] = k.T lowerCAmelCase_ : Union[str, Any] = o.T lowerCAmelCase_ : Any = q.T lowerCAmelCase_ : Tuple = v.T # Block i, layer 1 (Cross Attention). lowerCAmelCase_ : Optional[Any] = tax_layer_norm_lookup(lowercase__ , lowercase__ , """decoder""" , """pre_cross_attention_layer_norm""" ) lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ : Tuple = tax_attention_lookup(lowercase__ , lowercase__ , """decoder""" , """encoder_decoder_attention""" ) lowerCAmelCase_ : Optional[int] = layer_norm lowerCAmelCase_ : Any = k.T lowerCAmelCase_ : Any = o.T lowerCAmelCase_ : Optional[int] = q.T lowerCAmelCase_ : Dict = v.T # Block i, layer 2 (MLP). lowerCAmelCase_ : List[str] = tax_layer_norm_lookup(lowercase__ , lowercase__ , """decoder""" , """pre_mlp_layer_norm""" ) lowerCAmelCase_ , lowerCAmelCase_ : int = tax_mlp_lookup(lowercase__ , lowercase__ , """decoder""" , lowercase__ ) lowerCAmelCase_ : Any = layer_norm if split_mlp_wi: lowerCAmelCase_ : List[str] = wi[0].T lowerCAmelCase_ : List[Any] = wi[1].T else: lowerCAmelCase_ : Optional[Any] = wi.T lowerCAmelCase_ : str = wo.T lowerCAmelCase_ : int = old["""decoder/decoder_norm/scale"""] lowerCAmelCase_ : Union[str, Any] = old[ """decoder/relpos_bias/rel_embedding""" ].T # LM Head (only in v1.1 checkpoints, in v1.0 embeddings are used instead) if "decoder/logits_dense/kernel" in old: lowerCAmelCase_ : Optional[Any] = old["""decoder/logits_dense/kernel"""].T return new def __UpperCamelCase ( lowercase__ : Union[str, Any] , lowercase__ : bool ) -> Any: '''simple docstring''' lowerCAmelCase_ : Tuple = collections.OrderedDict([(k, torch.from_numpy(v.copy() )) for (k, v) in converted_params.items()] ) # Add what is missing. if "encoder.embed_tokens.weight" not in state_dict: lowerCAmelCase_ : List[Any] = state_dict["""shared.weight"""] if not is_encoder_only: if "decoder.embed_tokens.weight" not in state_dict: lowerCAmelCase_ : Union[str, Any] = state_dict["""shared.weight"""] if "lm_head.weight" not in state_dict: # For old 1.0 models. print("""Using shared word embeddings as lm_head.""" ) lowerCAmelCase_ : List[str] = state_dict["""shared.weight"""] return state_dict def __UpperCamelCase ( lowercase__ : Dict , lowercase__ : Optional[int] , lowercase__ : Union[str, Any] , lowercase__ : List[str] ) -> Tuple: '''simple docstring''' lowerCAmelCase_ : Tuple = checkpoints.load_tax_checkpoint(lowercase__ ) lowerCAmelCase_ : List[str] = convert_tax_to_pytorch(lowercase__ , num_layers=config.num_layers , is_encoder_only=lowercase__ ) lowerCAmelCase_ : List[str] = make_state_dict(lowercase__ , lowercase__ ) model.load_state_dict(lowercase__ , strict=lowercase__ ) def __UpperCamelCase ( lowercase__ : str , lowercase__ : Optional[Any] , lowercase__ : List[Any] , lowercase__ : bool = False ) -> int: '''simple docstring''' lowerCAmelCase_ : Any = TaConfig.from_json_file(lowercase__ ) print(f'Building PyTorch model from configuration: {config}' ) # Non-v1.1 checkpoints could also use T5Model, but this works for all. # The v1.0 checkpoints will simply have an LM head that is the word embeddings. if is_encoder_only: lowerCAmelCase_ : Optional[int] = TaEncoderModel(lowercase__ ) else: lowerCAmelCase_ : Dict = TaForConditionalGeneration(lowercase__ ) # Load weights from tf checkpoint load_tax_weights_in_ta(lowercase__ , lowercase__ , lowercase__ , lowercase__ ) # Save pytorch-model print(f'Save PyTorch model to {pytorch_dump_path}' ) model.save_pretrained(lowercase__ ) # Verify that we can load the checkpoint. model.from_pretrained(lowercase__ ) print("""Done""" ) if __name__ == "__main__": __UpperCAmelCase = argparse.ArgumentParser(description='Converts a native T5X checkpoint into a PyTorch checkpoint.') # Required parameters parser.add_argument( '--t5x_checkpoint_path', default=None, type=str, required=True, help='Path to the T5X checkpoint.' ) parser.add_argument( '--config_file', default=None, type=str, required=True, help='The config json file corresponding to the pre-trained T5 model.\nThis specifies the model architecture.', ) parser.add_argument( '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) parser.add_argument( '--is_encoder_only', action='store_true', help='Check if the model is encoder-decoder model', default=False ) __UpperCAmelCase = parser.parse_args() convert_tax_checkpoint_to_pytorch( args.tax_checkpoint_path, args.config_file, args.pytorch_dump_path, args.is_encoder_only )
28
1
"""simple docstring""" 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 ): SCREAMING_SNAKE_CASE_ : Optional[datasets.Features] = None class _UpperCAmelCase ( datasets.ArrowBasedBuilder ): SCREAMING_SNAKE_CASE_ : Dict = PandasConfig def A ( self : int ) -> List[Any]: return datasets.DatasetInfo(features=self.config.features ) def A ( self : int , A : List[Any] ) -> Tuple: 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}''' ) lowercase_ : Dict = dl_manager.download_and_extract(self.config.data_files ) if isinstance(A , (str, list, tuple) ): lowercase_ : Tuple = data_files if isinstance(A , A ): lowercase_ : str = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive lowercase_ : Optional[Any] = [dl_manager.iter_files(A ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'''files''': files} )] lowercase_ : Optional[Any] = [] for split_name, files in data_files.items(): if isinstance(A , A ): lowercase_ : List[str] = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive lowercase_ : List[str] = [dl_manager.iter_files(A ) for file in files] splits.append(datasets.SplitGenerator(name=A , gen_kwargs={'''files''': files} ) ) return splits def A ( self : List[str] , A : pa.Table ) -> pa.Table: 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 lowercase_ : Tuple = table_cast(A , self.config.features.arrow_schema ) return pa_table def A ( self : Optional[Any] , A : Tuple ) -> Optional[Any]: for i, file in enumerate(itertools.chain.from_iterable(A ) ): with open(A , '''rb''' ) as f: lowercase_ : Any = pa.Table.from_pandas(pd.read_pickle(A ) ) yield i, self._cast_table(A )
33
"""simple docstring""" from typing import Optional from urllib.parse import quote import huggingface_hub as hfh from packaging import version def lowercase ( __snake_case : str , __snake_case : str , __snake_case : Optional[str] = None ): if version.parse(hfh.__version__ ).release < version.parse('''0.11.0''' ).release: # old versions of hfh don't url-encode the file path lowercase_ : Union[str, Any] = quote(__snake_case ) return hfh.hf_hub_url(__snake_case , __snake_case , repo_type='''dataset''' , revision=__snake_case )
33
1
import os import textwrap import pyarrow as pa import pytest from datasets import ClassLabel, Features, Image from datasets.packaged_modules.csv.csv import Csv from ..utils import require_pil @pytest.fixture def __UpperCamelCase ( lowerCAmelCase__ : str ): __a : Any = tmp_path / """file.csv""" __a : Any = textwrap.dedent( '''\ header1,header2 1,2 10,20 ''' ) with open(lowerCamelCase_ , '''w''' ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) @pytest.fixture def __UpperCamelCase ( lowerCAmelCase__ : str ): __a : Optional[int] = tmp_path / """malformed_file.csv""" __a : int = textwrap.dedent( '''\ header1,header2 1,2 10,20, ''' ) with open(lowerCamelCase_ , '''w''' ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) @pytest.fixture def __UpperCamelCase ( lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : int ): __a : str = tmp_path / """csv_with_image.csv""" __a : int = textwrap.dedent( f"\\n image\n {image_file}\n " ) with open(lowerCamelCase_ , '''w''' ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) @pytest.fixture def __UpperCamelCase ( lowerCAmelCase__ : Any ): __a : int = tmp_path / """csv_with_label.csv""" __a : Tuple = textwrap.dedent( '''\ label good bad good ''' ) with open(lowerCamelCase_ , '''w''' ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) @pytest.fixture def __UpperCamelCase ( lowerCAmelCase__ : Union[str, Any] ): __a : List[str] = tmp_path / """csv_with_int_list.csv""" __a : str = textwrap.dedent( '''\ int_list 1 2 3 4 5 6 7 8 9 ''' ) with open(lowerCamelCase_ , '''w''' ) as f: f.write(lowerCamelCase_ ) return str(lowerCamelCase_ ) def __UpperCamelCase ( lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : Tuple ): __a : int = Csv() __a : Optional[Any] = csv._generate_tables([[csv_file, malformed_csv_file]] ) with pytest.raises(lowerCamelCase_ , match='''Error tokenizing data''' ): for _ in generator: pass assert any( record.levelname == '''ERROR''' and '''Failed to read file''' in record.message and os.path.basename(lowerCamelCase_ ) in record.message for record in caplog.records ) @require_pil def __UpperCamelCase ( lowerCAmelCase__ : Tuple ): with open(lowerCamelCase_ , encoding='''utf-8''' ) as f: __a : Tuple = f.read().splitlines()[1] __a : str = Csv(encoding='''utf-8''' , features=Features({'''image''': Image()} ) ) __a : Tuple = csv._generate_tables([[csv_file_with_image]] ) __a : Optional[Any] = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field('''image''' ).type == Image()() __a : List[str] = pa_table.to_pydict()["""image"""] assert generated_content == [{"path": image_file, "bytes": None}] def __UpperCamelCase ( lowerCAmelCase__ : int ): with open(lowerCamelCase_ , encoding='''utf-8''' ) as f: __a : List[Any] = f.read().splitlines()[1:] __a : Union[str, Any] = Csv(encoding='''utf-8''' , features=Features({'''label''': ClassLabel(names=['''good''', '''bad'''] )} ) ) __a : Optional[Any] = csv._generate_tables([[csv_file_with_label]] ) __a : Optional[int] = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field('''label''' ).type == ClassLabel(names=['''good''', '''bad'''] )() __a : Union[str, Any] = pa_table.to_pydict()["""label"""] assert generated_content == [ClassLabel(names=['''good''', '''bad'''] ).straint(lowerCamelCase_ ) for label in labels] def __UpperCamelCase ( lowerCAmelCase__ : Union[str, Any] ): __a : str = Csv(encoding='''utf-8''' , sep=''',''' , converters={'''int_list''': lambda lowerCAmelCase__ : [int(lowerCamelCase_ ) for i in x.split()]} ) __a : Optional[Any] = csv._generate_tables([[csv_file_with_int_list]] ) __a : Tuple = pa.concat_tables([table for _, table in generator] ) assert pa.types.is_list(pa_table.schema.field('''int_list''' ).type ) __a : Dict = pa_table.to_pydict()["""int_list"""] assert generated_content == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
358
import json import os import tempfile import datasets from utils import generate_example_dataset, get_duration lowercase__ =50000 lowercase__ =5000 lowercase__ , lowercase__ =os.path.split(__file__) lowercase__ =os.path.join(RESULTS_BASEPATH, 'results', RESULTS_FILENAME.replace('.py', '.json')) @get_duration def __UpperCamelCase ( lowerCAmelCase__ : datasets.Dataset , lowerCAmelCase__ : List[str] ): for i in range(lowerCAmelCase__ ): __a : str = dataset[i] @get_duration def __UpperCamelCase ( lowerCAmelCase__ : datasets.Dataset , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Tuple ): for i in range(0 , len(lowerCAmelCase__ ) , lowerCAmelCase__ ): __a : Optional[int] = dataset[i : i + batch_size] @get_duration def __UpperCamelCase ( lowerCAmelCase__ : datasets.Dataset , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : str ): with dataset.formatted_as(type=lowerCAmelCase__ ): for i in range(lowerCAmelCase__ ): __a : Dict = dataset[i] @get_duration def __UpperCamelCase ( lowerCAmelCase__ : datasets.Dataset , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : int ): with dataset.formatted_as(type=lowerCAmelCase__ ): for i in range(0 , lowerCAmelCase__ , lowerCAmelCase__ ): __a : int = dataset[i : i + batch_size] def __UpperCamelCase ( ): __a : Any = {'''num examples''': SPEED_TEST_N_EXAMPLES} __a : List[Any] = [ (read, {'''length''': SMALL_TEST}), (read, {'''length''': SPEED_TEST_N_EXAMPLES}), (read_batch, {'''length''': SPEED_TEST_N_EXAMPLES, '''batch_size''': 1_0}), (read_batch, {'''length''': SPEED_TEST_N_EXAMPLES, '''batch_size''': 1_0_0}), (read_batch, {'''length''': SPEED_TEST_N_EXAMPLES, '''batch_size''': 1_0_0_0}), (read_formatted, {'''type''': '''numpy''', '''length''': SMALL_TEST}), (read_formatted, {'''type''': '''pandas''', '''length''': SMALL_TEST}), (read_formatted, {'''type''': '''torch''', '''length''': SMALL_TEST}), (read_formatted, {'''type''': '''tensorflow''', '''length''': SMALL_TEST}), (read_formatted_batch, {'''type''': '''numpy''', '''length''': SMALL_TEST, '''batch_size''': 1_0}), (read_formatted_batch, {'''type''': '''numpy''', '''length''': SMALL_TEST, '''batch_size''': 1_0_0_0}), ] __a : Union[str, Any] = [ (read, {'''length''': SMALL_TEST}), (read, {'''length''': SPEED_TEST_N_EXAMPLES}), (read_batch, {'''length''': SPEED_TEST_N_EXAMPLES, '''batch_size''': 1_0}), (read_batch, {'''length''': SPEED_TEST_N_EXAMPLES, '''batch_size''': 1_0_0}), (read_batch, {'''length''': SPEED_TEST_N_EXAMPLES, '''batch_size''': 1_0_0_0}), (read_formatted, {'''type''': '''numpy''', '''length''': SMALL_TEST}), (read_formatted_batch, {'''type''': '''numpy''', '''length''': SMALL_TEST, '''batch_size''': 1_0}), (read_formatted_batch, {'''type''': '''numpy''', '''length''': SMALL_TEST, '''batch_size''': 1_0_0_0}), ] with tempfile.TemporaryDirectory() as tmp_dir: print('''generating dataset''' ) __a : Optional[Any] = datasets.Features( {'''list''': datasets.Sequence(datasets.Value('''float32''' ) ), '''numbers''': datasets.Value('''float32''' )} ) __a : Optional[int] = generate_example_dataset( os.path.join(lowerCAmelCase__ , '''dataset.arrow''' ) , lowerCAmelCase__ , num_examples=lowerCAmelCase__ , seq_shapes={'''list''': (1_0_0,)} , ) print('''first set of iterations''' ) for func, kwargs in functions: print(func.__name__ , str(lowerCAmelCase__ ) ) __a : str = func(lowerCAmelCase__ , **lowerCAmelCase__ ) print('''shuffling dataset''' ) __a : int = dataset.shuffle() print('''Second set of iterations (after shuffling''' ) for func, kwargs in functions_shuffled: print('''shuffled ''' , func.__name__ , str(lowerCAmelCase__ ) ) __a : List[Any] = func( lowerCAmelCase__ , **lowerCAmelCase__ ) with open(lowerCAmelCase__ , '''wb''' ) as f: f.write(json.dumps(lowerCAmelCase__ ).encode('''utf-8''' ) ) if __name__ == "__main__": # useful to run the profiler benchmark_iterating()
90
0
'''simple docstring''' import collections import gzip import os import urllib import numpy from tensorflow.python.framework import dtypes, random_seed from tensorflow.python.platform import gfile from tensorflow.python.util.deprecation import deprecated lowerCamelCase_ = collections.namedtuple('''_Datasets''', ['''train''', '''validation''', '''test''']) # CVDF mirror of http://yann.lecun.com/exdb/mnist/ lowerCamelCase_ = '''https://storage.googleapis.com/cvdf-datasets/mnist/''' def __lowercase ( __lowercase ) -> Optional[Any]: '''simple docstring''' _A = numpy.dtype(numpy.uintaa ).newbyteorder(">" ) return numpy.frombuffer(bytestream.read(4 ) , dtype=__lowercase )[0] @deprecated(__lowercase , "Please use tf.data to implement this functionality." ) def __lowercase ( __lowercase ) -> List[Any]: '''simple docstring''' print("Extracting" , f.name ) with gzip.GzipFile(fileobj=__lowercase ) as bytestream: _A = _readaa(__lowercase ) if magic != 2051: raise ValueError( "Invalid magic number %d in MNIST image file: %s" % (magic, f.name) ) _A = _readaa(__lowercase ) _A = _readaa(__lowercase ) _A = _readaa(__lowercase ) _A = bytestream.read(rows * cols * num_images ) _A = numpy.frombuffer(__lowercase , dtype=numpy.uinta ) _A = data.reshape(__lowercase , __lowercase , __lowercase , 1 ) return data @deprecated(__lowercase , "Please use tf.one_hot on tensors." ) def __lowercase ( __lowercase , __lowercase ) -> int: '''simple docstring''' _A = labels_dense.shape[0] _A = numpy.arange(__lowercase ) * num_classes _A = numpy.zeros((num_labels, num_classes) ) _A = 1 return labels_one_hot @deprecated(__lowercase , "Please use tf.data to implement this functionality." ) def __lowercase ( __lowercase , __lowercase=False , __lowercase=10 ) -> List[Any]: '''simple docstring''' print("Extracting" , f.name ) with gzip.GzipFile(fileobj=__lowercase ) as bytestream: _A = _readaa(__lowercase ) if magic != 2049: raise ValueError( "Invalid magic number %d in MNIST label file: %s" % (magic, f.name) ) _A = _readaa(__lowercase ) _A = bytestream.read(__lowercase ) _A = numpy.frombuffer(__lowercase , dtype=numpy.uinta ) if one_hot: return _dense_to_one_hot(__lowercase , __lowercase ) return labels class _UpperCAmelCase : """simple docstring""" @deprecated( __UpperCAmelCase , "Please use alternatives such as official/mnist/_DataSet.py" " from tensorflow/models." , ) def __init__( self : str , __UpperCAmelCase : int , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : List[str]=False , __UpperCAmelCase : List[str]=False , __UpperCAmelCase : Optional[Any]=dtypes.floataa , __UpperCAmelCase : Tuple=True , __UpperCAmelCase : Optional[int]=None , ): '''simple docstring''' _A , _A = random_seed.get_seed(__UpperCAmelCase ) # If op level seed is not set, use whatever graph level seed is returned numpy.random.seed(seeda if seed is None else seeda ) _A = dtypes.as_dtype(__UpperCAmelCase ).base_dtype if dtype not in (dtypes.uinta, dtypes.floataa): raise TypeError("Invalid image dtype %r, expected uint8 or float32" % dtype ) if fake_data: _A = 10000 _A = one_hot else: assert ( images.shape[0] == labels.shape[0] ), f'''images.shape: {images.shape} labels.shape: {labels.shape}''' _A = images.shape[0] # Convert shape from [num examples, rows, columns, depth] # to [num examples, rows*columns] (assuming depth == 1) if reshape: assert images.shape[3] == 1 _A = images.reshape( images.shape[0] , images.shape[1] * images.shape[2] ) if dtype == dtypes.floataa: # Convert from [0, 255] -> [0.0, 1.0]. _A = images.astype(numpy.floataa ) _A = numpy.multiply(__UpperCAmelCase , 1.0 / 255.0 ) _A = images _A = labels _A = 0 _A = 0 @property def lowerCAmelCase ( self : int ): '''simple docstring''' return self._images @property def lowerCAmelCase ( self : Optional[int] ): '''simple docstring''' return self._labels @property def lowerCAmelCase ( self : Union[str, Any] ): '''simple docstring''' return self._num_examples @property def lowerCAmelCase ( self : Optional[int] ): '''simple docstring''' return self._epochs_completed def lowerCAmelCase ( self : Any , __UpperCAmelCase : List[str] , __UpperCAmelCase : Any=False , __UpperCAmelCase : int=True ): '''simple docstring''' if fake_data: _A = [1] * 784 _A = [1] + [0] * 9 if self.one_hot else 0 return ( [fake_image for _ in range(__UpperCAmelCase )], [fake_label for _ in range(__UpperCAmelCase )], ) _A = self._index_in_epoch # Shuffle for the first epoch if self._epochs_completed == 0 and start == 0 and shuffle: _A = numpy.arange(self._num_examples ) numpy.random.shuffle(__UpperCAmelCase ) _A = self.images[perma] _A = self.labels[perma] # Go to the next epoch if start + batch_size > self._num_examples: # Finished epoch self._epochs_completed += 1 # Get the rest examples in this epoch _A = self._num_examples - start _A = self._images[start : self._num_examples] _A = self._labels[start : self._num_examples] # Shuffle the data if shuffle: _A = numpy.arange(self._num_examples ) numpy.random.shuffle(__UpperCAmelCase ) _A = self.images[perm] _A = self.labels[perm] # Start next epoch _A = 0 _A = batch_size - rest_num_examples _A = self._index_in_epoch _A = self._images[start:end] _A = self._labels[start:end] return ( numpy.concatenate((images_rest_part, images_new_part) , axis=0 ), numpy.concatenate((labels_rest_part, labels_new_part) , axis=0 ), ) else: self._index_in_epoch += batch_size _A = self._index_in_epoch return self._images[start:end], self._labels[start:end] @deprecated(__lowercase , "Please write your own downloading logic." ) def __lowercase ( __lowercase , __lowercase , __lowercase ) -> Union[str, Any]: '''simple docstring''' if not gfile.Exists(__lowercase ): gfile.MakeDirs(__lowercase ) _A = os.path.join(__lowercase , __lowercase ) if not gfile.Exists(__lowercase ): urllib.request.urlretrieve(__lowercase , __lowercase ) # noqa: S310 with gfile.GFile(__lowercase ) as f: _A = f.size() print("Successfully downloaded" , __lowercase , __lowercase , "bytes." ) return filepath @deprecated( __lowercase , "Please use alternatives such as:" " tensorflow_datasets.load('mnist')" ) def __lowercase ( __lowercase , __lowercase=False , __lowercase=False , __lowercase=dtypes.floataa , __lowercase=True , __lowercase=5000 , __lowercase=None , __lowercase=DEFAULT_SOURCE_URL , ) -> List[str]: '''simple docstring''' if fake_data: def fake(): return _DataSet( [] , [] , fake_data=__lowercase , one_hot=__lowercase , dtype=__lowercase , seed=__lowercase ) _A = fake() _A = fake() _A = fake() return _Datasets(train=__lowercase , validation=__lowercase , test=__lowercase ) if not source_url: # empty string check _A = DEFAULT_SOURCE_URL _A = "train-images-idx3-ubyte.gz" _A = "train-labels-idx1-ubyte.gz" _A = "t10k-images-idx3-ubyte.gz" _A = "t10k-labels-idx1-ubyte.gz" _A = _maybe_download( __lowercase , __lowercase , source_url + train_images_file ) with gfile.Open(__lowercase , "rb" ) as f: _A = _extract_images(__lowercase ) _A = _maybe_download( __lowercase , __lowercase , source_url + train_labels_file ) with gfile.Open(__lowercase , "rb" ) as f: _A = _extract_labels(__lowercase , one_hot=__lowercase ) _A = _maybe_download( __lowercase , __lowercase , source_url + test_images_file ) with gfile.Open(__lowercase , "rb" ) as f: _A = _extract_images(__lowercase ) _A = _maybe_download( __lowercase , __lowercase , source_url + test_labels_file ) with gfile.Open(__lowercase , "rb" ) as f: _A = _extract_labels(__lowercase , one_hot=__lowercase ) if not 0 <= validation_size <= len(__lowercase ): _A = ( "Validation size should be between 0 and " F'''{len(__lowercase )}. Received: {validation_size}.''' ) raise ValueError(__lowercase ) _A = train_images[:validation_size] _A = train_labels[:validation_size] _A = train_images[validation_size:] _A = train_labels[validation_size:] _A = {"dtype": dtype, "reshape": reshape, "seed": seed} _A = _DataSet(__lowercase , __lowercase , **__lowercase ) _A = _DataSet(__lowercase , __lowercase , **__lowercase ) _A = _DataSet(__lowercase , __lowercase , **__lowercase ) return _Datasets(train=__lowercase , validation=__lowercase , test=__lowercase )
79
'''simple docstring''' def __lowercase ( __lowercase = 100 ) -> int: '''simple docstring''' _A = n * (n + 1) * (2 * n + 1) / 6 _A = (n * (n + 1) / 2) ** 2 return int(square_of_sum - sum_of_squares ) if __name__ == "__main__": print(F"""{solution() = }""")
79
1
import argparse import json import math import os import time import traceback import zipfile from collections import Counter import requests def lowerCamelCase__ (_UpperCAmelCase , _UpperCAmelCase=None): SCREAMING_SNAKE_CASE = None if token is not None: SCREAMING_SNAKE_CASE = {'Accept': 'application/vnd.github+json', 'Authorization': F'''Bearer {token}'''} SCREAMING_SNAKE_CASE = F'''https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}/jobs?per_page=100''' SCREAMING_SNAKE_CASE = requests.get(_UpperCAmelCase , headers=_UpperCAmelCase).json() SCREAMING_SNAKE_CASE = {} try: job_links.update({job['name']: job['html_url'] for job in result['jobs']}) SCREAMING_SNAKE_CASE = math.ceil((result['total_count'] - 100) / 100) for i in range(_UpperCAmelCase): SCREAMING_SNAKE_CASE = requests.get(url + F'''&page={i + 2}''' , headers=_UpperCAmelCase).json() job_links.update({job['name']: job['html_url'] for job in result['jobs']}) return job_links except Exception: print(F'''Unknown error, could not fetch links:\n{traceback.format_exc()}''') return {} def lowerCamelCase__ (_UpperCAmelCase , _UpperCAmelCase=None): SCREAMING_SNAKE_CASE = None if token is not None: SCREAMING_SNAKE_CASE = {'Accept': 'application/vnd.github+json', 'Authorization': F'''Bearer {token}'''} SCREAMING_SNAKE_CASE = F'''https://api.github.com/repos/huggingface/transformers/actions/runs/{worflow_run_id}/artifacts?per_page=100''' SCREAMING_SNAKE_CASE = requests.get(_UpperCAmelCase , headers=_UpperCAmelCase).json() SCREAMING_SNAKE_CASE = {} try: artifacts.update({artifact['name']: artifact['archive_download_url'] for artifact in result['artifacts']}) SCREAMING_SNAKE_CASE = math.ceil((result['total_count'] - 100) / 100) for i in range(_UpperCAmelCase): SCREAMING_SNAKE_CASE = requests.get(url + F'''&page={i + 2}''' , headers=_UpperCAmelCase).json() artifacts.update({artifact['name']: artifact['archive_download_url'] for artifact in result['artifacts']}) return artifacts except Exception: print(F'''Unknown error, could not fetch links:\n{traceback.format_exc()}''') return {} def lowerCamelCase__ (_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase): SCREAMING_SNAKE_CASE = None if token is not None: SCREAMING_SNAKE_CASE = {'Accept': 'application/vnd.github+json', 'Authorization': F'''Bearer {token}'''} SCREAMING_SNAKE_CASE = requests.get(_UpperCAmelCase , headers=_UpperCAmelCase , allow_redirects=_UpperCAmelCase) SCREAMING_SNAKE_CASE = result.headers['Location'] SCREAMING_SNAKE_CASE = requests.get(_UpperCAmelCase , allow_redirects=_UpperCAmelCase) SCREAMING_SNAKE_CASE = os.path.join(_UpperCAmelCase , F'''{artifact_name}.zip''') with open(_UpperCAmelCase , 'wb') as fp: fp.write(response.content) def lowerCamelCase__ (_UpperCAmelCase , _UpperCAmelCase=None): SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = None with zipfile.ZipFile(_UpperCAmelCase) as z: for filename in z.namelist(): if not os.path.isdir(_UpperCAmelCase): # read the file if filename in ["failures_line.txt", "summary_short.txt", "job_name.txt"]: with z.open(_UpperCAmelCase) as f: for line in f: SCREAMING_SNAKE_CASE = line.decode('UTF-8').strip() if filename == "failures_line.txt": try: # `error_line` is the place where `error` occurs SCREAMING_SNAKE_CASE = line[: line.index(': ')] SCREAMING_SNAKE_CASE = line[line.index(': ') + len(': ') :] errors.append([error_line, error]) except Exception: # skip un-related lines pass elif filename == "summary_short.txt" and line.startswith('FAILED '): # `test` is the test method that failed SCREAMING_SNAKE_CASE = line[len('FAILED ') :] failed_tests.append(_UpperCAmelCase) elif filename == "job_name.txt": SCREAMING_SNAKE_CASE = line if len(_UpperCAmelCase) != len(_UpperCAmelCase): raise ValueError( F'''`errors` and `failed_tests` should have the same number of elements. Got {len(_UpperCAmelCase)} for `errors` ''' F'''and {len(_UpperCAmelCase)} for `failed_tests` instead. The test reports in {artifact_zip_path} have some''' ' problem.') SCREAMING_SNAKE_CASE = None if job_name and job_links: SCREAMING_SNAKE_CASE = job_links.get(_UpperCAmelCase , _UpperCAmelCase) # A list with elements of the form (line of error, error, failed test) SCREAMING_SNAKE_CASE = [x + [y] + [job_link] for x, y in zip(_UpperCAmelCase , _UpperCAmelCase)] return result def lowerCamelCase__ (_UpperCAmelCase , _UpperCAmelCase=None): SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [os.path.join(_UpperCAmelCase , _UpperCAmelCase) for p in os.listdir(_UpperCAmelCase) if p.endswith('.zip')] for p in paths: errors.extend(get_errors_from_single_artifact(_UpperCAmelCase , job_links=_UpperCAmelCase)) return errors def lowerCamelCase__ (_UpperCAmelCase , _UpperCAmelCase=None): SCREAMING_SNAKE_CASE = Counter() counter.update([x[1] for x in logs]) SCREAMING_SNAKE_CASE = counter.most_common() SCREAMING_SNAKE_CASE = {} for error, count in counts: if error_filter is None or error not in error_filter: SCREAMING_SNAKE_CASE = {'count': count, 'failed_tests': [(x[2], x[0]) for x in logs if x[1] == error]} SCREAMING_SNAKE_CASE = dict(sorted(r.items() , key=lambda _UpperCAmelCase: item[1]["count"] , reverse=_UpperCAmelCase)) return r def lowerCamelCase__ (_UpperCAmelCase): SCREAMING_SNAKE_CASE = test.split('::')[0] if test.startswith('tests/models/'): SCREAMING_SNAKE_CASE = test.split('/')[2] else: SCREAMING_SNAKE_CASE = None return test def lowerCamelCase__ (_UpperCAmelCase , _UpperCAmelCase=None): SCREAMING_SNAKE_CASE = [(x[0], x[1], get_model(x[2])) for x in logs] SCREAMING_SNAKE_CASE = [x for x in logs if x[2] is not None] SCREAMING_SNAKE_CASE = {x[2] for x in logs} SCREAMING_SNAKE_CASE = {} for test in tests: SCREAMING_SNAKE_CASE = Counter() # count by errors in `test` counter.update([x[1] for x in logs if x[2] == test]) SCREAMING_SNAKE_CASE = counter.most_common() SCREAMING_SNAKE_CASE = {error: count for error, count in counts if (error_filter is None or error not in error_filter)} SCREAMING_SNAKE_CASE = sum(error_counts.values()) if n_errors > 0: SCREAMING_SNAKE_CASE = {'count': n_errors, 'errors': error_counts} SCREAMING_SNAKE_CASE = dict(sorted(r.items() , key=lambda _UpperCAmelCase: item[1]["count"] , reverse=_UpperCAmelCase)) return r def lowerCamelCase__ (_UpperCAmelCase): SCREAMING_SNAKE_CASE = '| no. | error | status |' SCREAMING_SNAKE_CASE = '|-:|:-|:-|' SCREAMING_SNAKE_CASE = [header, sep] for error in reduced_by_error: SCREAMING_SNAKE_CASE = reduced_by_error[error]['count'] SCREAMING_SNAKE_CASE = F'''| {count} | {error[:100]} | |''' lines.append(_UpperCAmelCase) return "\n".join(_UpperCAmelCase) def lowerCamelCase__ (_UpperCAmelCase): SCREAMING_SNAKE_CASE = '| model | no. of errors | major error | count |' SCREAMING_SNAKE_CASE = '|-:|-:|-:|-:|' SCREAMING_SNAKE_CASE = [header, sep] for model in reduced_by_model: SCREAMING_SNAKE_CASE = reduced_by_model[model]['count'] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = list(reduced_by_model[model]['errors'].items())[0] SCREAMING_SNAKE_CASE = F'''| {model} | {count} | {error[:60]} | {_count} |''' lines.append(_UpperCAmelCase) return "\n".join(_UpperCAmelCase) if __name__ == "__main__": a_ : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument('--workflow_run_id', type=str, required=True, help='A GitHub Actions workflow run id.') parser.add_argument( '--output_dir', type=str, required=True, help='Where to store the downloaded artifacts and other result files.', ) parser.add_argument('--token', default=None, type=str, help='A token that has actions:read permission.') a_ : int = parser.parse_args() os.makedirs(args.output_dir, exist_ok=True) a_ : Optional[int] = get_job_links(args.workflow_run_id, token=args.token) a_ : List[Any] = {} # To deal with `workflow_call` event, where a job name is the combination of the job names in the caller and callee. # For example, `PyTorch 1.11 / Model tests (models/albert, single-gpu)`. if _job_links: for k, v in _job_links.items(): # This is how GitHub actions combine job names. if " / " in k: a_ : int = k.find(' / ') a_ : List[Any] = k[index + len(' / ') :] a_ : str = v with open(os.path.join(args.output_dir, 'job_links.json'), 'w', encoding='UTF-8') as fp: json.dump(job_links, fp, ensure_ascii=False, indent=4) a_ : Any = get_artifacts_links(args.workflow_run_id, token=args.token) with open(os.path.join(args.output_dir, 'artifacts.json'), 'w', encoding='UTF-8') as fp: json.dump(artifacts, fp, ensure_ascii=False, indent=4) for idx, (name, url) in enumerate(artifacts.items()): download_artifact(name, url, args.output_dir, args.token) # Be gentle to GitHub time.sleep(1) a_ : str = get_all_errors(args.output_dir, job_links=job_links) # `e[1]` is the error a_ : List[Any] = Counter() counter.update([e[1] for e in errors]) # print the top 30 most common test errors a_ : List[Any] = counter.most_common(30) for item in most_common: print(item) with open(os.path.join(args.output_dir, 'errors.json'), 'w', encoding='UTF-8') as fp: json.dump(errors, fp, ensure_ascii=False, indent=4) a_ : Union[str, Any] = reduce_by_error(errors) a_ : List[str] = reduce_by_model(errors) a_ : List[Any] = make_github_table(reduced_by_error) a_ : Any = make_github_table_per_model(reduced_by_model) with open(os.path.join(args.output_dir, 'reduced_by_error.txt'), 'w', encoding='UTF-8') as fp: fp.write(sa) with open(os.path.join(args.output_dir, 'reduced_by_model.txt'), 'w', encoding='UTF-8') as fp: fp.write(sa)
356
import pytest from datasets import inspect_metric, list_metrics, load_metric @pytest.fixture def lowerCamelCase__ (_UpperCAmelCase): monkeypatch.setattr('datasets.utils.deprecation_utils._emitted_deprecation_warnings' , set()) @pytest.fixture def lowerCamelCase__ (_UpperCAmelCase): class _snake_case : def __init__( self , a) -> List[Any]: SCREAMING_SNAKE_CASE = metric_id class _snake_case : _lowercase : Optional[Any] = [MetricMock(A__ ) for metric_id in ['''accuracy''', '''mse''', '''precision''', '''codeparrot/apps_metric''']] def SCREAMING_SNAKE_CASE__ ( self) -> Optional[int]: return self._metrics monkeypatch.setattr('datasets.inspect.huggingface_hub' , HfhMock()) @pytest.mark.parametrize( 'func, args' , [(load_metric, ('metrics/mse',)), (list_metrics, ()), (inspect_metric, ('metrics/mse', 'tmp_path'))]) def lowerCamelCase__ (_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase): if "tmp_path" in args: SCREAMING_SNAKE_CASE = tuple(arg if arg != 'tmp_path' else tmp_path for arg in args) with pytest.warns(_UpperCAmelCase , match='https://huggingface.co/docs/evaluate'): func(*_UpperCAmelCase)
327
0
'''simple docstring''' from __future__ import annotations from collections.abc import Iterable, Iterator from dataclasses import dataclass A__ : List[Any] =(3, 9, -11, 0, 7, 5, 1, -1) A__ : str =(4, 6, 2, 0, 8, 10, 3, -2) @dataclass class UpperCAmelCase : _lowercase: int _lowercase: Node | None class UpperCAmelCase : def __init__( self : List[str] , __snake_case : Iterable[int] ) -> None: _lowerCAmelCase = None for i in sorted(__snake_case , reverse=__snake_case ): _lowerCAmelCase = Node(__snake_case , self.head ) def __iter__( self : Union[str, Any] ) -> Iterator[int]: _lowerCAmelCase = self.head while node: yield node.data _lowerCAmelCase = node.next_node def __len__( self : Tuple ) -> int: return sum(1 for _ in self ) def __str__( self : List[Any] ) -> str: return " -> ".join([str(__snake_case ) for node in self] ) def UpperCamelCase__ ( lowerCAmelCase , lowerCAmelCase ): """simple docstring""" return SortedLinkedList(list(lowerCAmelCase ) + list(lowerCAmelCase ) ) if __name__ == "__main__": import doctest doctest.testmod() A__ : Optional[int] =SortedLinkedList print(merge_lists(SSL(test_data_odd), SSL(test_data_even)))
70
'''simple docstring''' 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 ): _lowercase: Optional[datasets.Features] = None class UpperCAmelCase ( datasets.ArrowBasedBuilder ): _lowercase: Tuple = PandasConfig def lowercase__ ( self : Optional[Any] ) -> str: return datasets.DatasetInfo(features=self.config.features ) def lowercase__ ( self : List[str] , __snake_case : Dict ) -> int: 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}" ) _lowerCAmelCase = dl_manager.download_and_extract(self.config.data_files ) if isinstance(__snake_case , (str, list, tuple) ): _lowerCAmelCase = data_files if isinstance(__snake_case , __snake_case ): _lowerCAmelCase = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive _lowerCAmelCase = [dl_manager.iter_files(__snake_case ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={"""files""": files} )] _lowerCAmelCase = [] for split_name, files in data_files.items(): if isinstance(__snake_case , __snake_case ): _lowerCAmelCase = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive _lowerCAmelCase = [dl_manager.iter_files(__snake_case ) for file in files] splits.append(datasets.SplitGenerator(name=__snake_case , gen_kwargs={"""files""": files} ) ) return splits def lowercase__ ( self : List[Any] , __snake_case : pa.Table ) -> pa.Table: 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 _lowerCAmelCase = table_cast(__snake_case , self.config.features.arrow_schema ) return pa_table def lowercase__ ( self : Dict , __snake_case : Optional[Any] ) -> Any: for i, file in enumerate(itertools.chain.from_iterable(__snake_case ) ): with open(__snake_case , """rb""" ) as f: _lowerCAmelCase = pa.Table.from_pandas(pd.read_pickle(__snake_case ) ) yield i, self._cast_table(__snake_case )
70
1
SCREAMING_SNAKE_CASE_ = {0: [2, 3], 1: [0], 2: [1], 3: [4], 4: []} SCREAMING_SNAKE_CASE_ = {0: [1, 2, 3], 1: [2], 2: [0], 3: [4], 4: [5], 5: [3]} def __lowercase ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> list[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = [] for neighbour in graph[vert]: if not visited[neighbour]: order += topology_sort(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) order.append(_SCREAMING_SNAKE_CASE ) return order def __lowercase ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> list[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = [vert] for neighbour in reversed_graph[vert]: if not visited[neighbour]: component += find_components(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) return component def __lowercase ( _SCREAMING_SNAKE_CASE ) -> list[list[int]]: '''simple docstring''' SCREAMING_SNAKE_CASE = len(_SCREAMING_SNAKE_CASE ) * [False] SCREAMING_SNAKE_CASE = {vert: [] for vert in range(len(_SCREAMING_SNAKE_CASE ) )} for vert, neighbours in graph.items(): for neighbour in neighbours: reversed_graph[neighbour].append(_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE = [] for i, was_visited in enumerate(_SCREAMING_SNAKE_CASE ): if not was_visited: order += topology_sort(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = len(_SCREAMING_SNAKE_CASE ) * [False] for i in range(len(_SCREAMING_SNAKE_CASE ) ): SCREAMING_SNAKE_CASE = order[len(_SCREAMING_SNAKE_CASE ) - i - 1] if not visited[vert]: SCREAMING_SNAKE_CASE = find_components(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) components_list.append(_SCREAMING_SNAKE_CASE ) return components_list
193
from __future__ import annotations def __lowercase ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> list[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = len(_SCREAMING_SNAKE_CASE ) - 1 while i < j: if nums[i] + nums[j] == target: return [i, j] elif nums[i] + nums[j] < target: SCREAMING_SNAKE_CASE = i + 1 else: SCREAMING_SNAKE_CASE = j - 1 return [] if __name__ == "__main__": import doctest doctest.testmod() print(F'''{two_pointer([2, 7, 1_1, 1_5], 9) = }''')
193
1
'''simple docstring''' import math import os from copy import deepcopy import datasets import evaluate import torch import transformers from datasets import load_dataset from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer from accelerate import Accelerator from accelerate.test_utils import RegressionDataset, RegressionModel from accelerate.utils import is_tpu_available, set_seed a : Optional[Any] = 'true' def __magic_name__ ( __UpperCAmelCase, __UpperCAmelCase=82, __UpperCAmelCase=16 ) -> str: '''simple docstring''' set_seed(42 ) snake_case_ = RegressionModel() snake_case_ = deepcopy(__UpperCAmelCase ) snake_case_ = RegressionDataset(length=__UpperCAmelCase ) snake_case_ = DataLoader(__UpperCAmelCase, batch_size=__UpperCAmelCase ) model.to(accelerator.device ) snake_case_ ,snake_case_ = accelerator.prepare(__UpperCAmelCase, __UpperCAmelCase ) return model, ddp_model, dataloader def __magic_name__ ( __UpperCAmelCase, __UpperCAmelCase=False ) -> int: '''simple docstring''' snake_case_ = AutoTokenizer.from_pretrained('''hf-internal-testing/mrpc-bert-base-cased''' ) snake_case_ = load_dataset('''glue''', '''mrpc''', split='''validation''' ) def tokenize_function(__UpperCAmelCase ): snake_case_ = tokenizer(examples['''sentence1'''], examples['''sentence2'''], truncation=__UpperCAmelCase, max_length=__UpperCAmelCase ) return outputs with accelerator.main_process_first(): snake_case_ = dataset.map( __UpperCAmelCase, batched=__UpperCAmelCase, remove_columns=['''idx''', '''sentence1''', '''sentence2'''], ) snake_case_ = tokenized_datasets.rename_column('''label''', '''labels''' ) def collate_fn(__UpperCAmelCase ): if use_longest: return tokenizer.pad(__UpperCAmelCase, padding='''longest''', return_tensors='''pt''' ) return tokenizer.pad(__UpperCAmelCase, padding='''max_length''', max_length=128, return_tensors='''pt''' ) return DataLoader(__UpperCAmelCase, shuffle=__UpperCAmelCase, collate_fn=__UpperCAmelCase, batch_size=16 ) def __magic_name__ ( __UpperCAmelCase, __UpperCAmelCase ) -> int: '''simple docstring''' snake_case_ = Accelerator(dispatch_batches=__UpperCAmelCase, split_batches=__UpperCAmelCase ) snake_case_ = get_dataloader(__UpperCAmelCase, not dispatch_batches ) snake_case_ = AutoModelForSequenceClassification.from_pretrained( '''hf-internal-testing/mrpc-bert-base-cased''', return_dict=__UpperCAmelCase ) snake_case_ ,snake_case_ = accelerator.prepare(__UpperCAmelCase, __UpperCAmelCase ) return {"ddp": [ddp_model, ddp_dataloader, "cuda:0"], "no": [model, dataloader, accelerator.device]}, accelerator def __magic_name__ ( __UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase ) -> Any: '''simple docstring''' snake_case_ = [] for batch in dataloader: snake_case_ ,snake_case_ = batch.values() with torch.no_grad(): snake_case_ = model(__UpperCAmelCase ) snake_case_ ,snake_case_ = accelerator.gather_for_metrics((logit, target) ) logits_and_targets.append((logit, target) ) snake_case_ ,snake_case_ = [], [] for logit, targ in logits_and_targets: logits.append(__UpperCAmelCase ) targs.append(__UpperCAmelCase ) snake_case_ ,snake_case_ = torch.cat(__UpperCAmelCase ), torch.cat(__UpperCAmelCase ) return logits, targs def __magic_name__ ( __UpperCAmelCase, __UpperCAmelCase=82, __UpperCAmelCase=False, __UpperCAmelCase=False, __UpperCAmelCase=16 ) -> Dict: '''simple docstring''' snake_case_ ,snake_case_ ,snake_case_ = get_basic_setup(__UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase ) snake_case_ ,snake_case_ = generate_predictions(__UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase ) assert ( len(__UpperCAmelCase ) == num_samples ), F"Unexpected number of inputs:\n Expected: {num_samples}\n Actual: {len(__UpperCAmelCase )}" def __magic_name__ ( __UpperCAmelCase = False, __UpperCAmelCase = False ) -> Union[str, Any]: '''simple docstring''' snake_case_ = evaluate.load('''glue''', '''mrpc''' ) snake_case_ ,snake_case_ = get_mrpc_setup(__UpperCAmelCase, __UpperCAmelCase ) # First do baseline snake_case_ ,snake_case_ ,snake_case_ = setup['''no'''] model.to(__UpperCAmelCase ) model.eval() for batch in dataloader: batch.to(__UpperCAmelCase ) with torch.inference_mode(): snake_case_ = model(**__UpperCAmelCase ) snake_case_ = outputs.logits.argmax(dim=-1 ) metric.add_batch(predictions=__UpperCAmelCase, references=batch['''labels'''] ) snake_case_ = metric.compute() # Then do distributed snake_case_ ,snake_case_ ,snake_case_ = setup['''ddp'''] model.eval() for batch in dataloader: with torch.inference_mode(): snake_case_ = model(**__UpperCAmelCase ) snake_case_ = outputs.logits.argmax(dim=-1 ) snake_case_ = batch['''labels'''] snake_case_ ,snake_case_ = accelerator.gather_for_metrics((preds, references) ) metric.add_batch(predictions=__UpperCAmelCase, references=__UpperCAmelCase ) snake_case_ = metric.compute() for key in "accuracy f1".split(): assert math.isclose( baseline[key], distributed[key] ), F"Baseline and Distributed are not the same for key {key}:\n\tBaseline: {baseline[key]}\n\tDistributed: {distributed[key]}\n" def __magic_name__ ( ) -> Union[str, Any]: '''simple docstring''' snake_case_ = Accelerator(split_batches=__UpperCAmelCase, dispatch_batches=__UpperCAmelCase ) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_warning() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() # These are a bit slower so they should only be ran on the GPU or TPU if torch.cuda.is_available() or is_tpu_available(): if accelerator.is_local_main_process: print('''**Testing gather_for_metrics**''' ) for split_batches in [True, False]: for dispatch_batches in [True, False]: if accelerator.is_local_main_process: print(F"With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`" ) test_mrpc(__UpperCAmelCase, __UpperCAmelCase ) accelerator.state._reset_state() if accelerator.is_local_main_process: print('''**Test torch metrics**''' ) for split_batches in [True, False]: for dispatch_batches in [True, False]: snake_case_ = Accelerator(split_batches=__UpperCAmelCase, dispatch_batches=__UpperCAmelCase ) if accelerator.is_local_main_process: print(F"With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`, length=99" ) test_torch_metrics(__UpperCAmelCase, 99 ) accelerator.state._reset_state() if accelerator.is_local_main_process: print('''**Test last batch is not dropped when perfectly divisible**''' ) snake_case_ = Accelerator() test_torch_metrics(__UpperCAmelCase, 512 ) accelerator.state._reset_state() def __magic_name__ ( __UpperCAmelCase ) -> str: '''simple docstring''' main() if __name__ == "__main__": main()
56
'''simple docstring''' import unittest from transformers import ( MODEL_FOR_OBJECT_DETECTION_MAPPING, AutoFeatureExtractor, AutoModelForObjectDetection, ObjectDetectionPipeline, is_vision_available, pipeline, ) from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_pytesseract, require_tf, require_timm, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class a : @staticmethod def A_ ( *lowercase_ : int , **lowercase_ : str ): pass @is_pipeline_test @require_vision @require_timm @require_torch class a ( unittest.TestCase ): snake_case_ = MODEL_FOR_OBJECT_DETECTION_MAPPING def A_ ( self : Any , lowercase_ : List[Any] , lowercase_ : Optional[int] , lowercase_ : List[str] ): snake_case_ = ObjectDetectionPipeline(model=lowercase_ , image_processor=lowercase_ ) return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"] def A_ ( self : Dict , lowercase_ : List[Any] , lowercase_ : int ): snake_case_ = object_detector('''./tests/fixtures/tests_samples/COCO/000000039769.png''' , threshold=0.0 ) self.assertGreater(len(lowercase_ ) , 0 ) for detected_object in outputs: self.assertEqual( lowercase_ , { '''score''': ANY(lowercase_ ), '''label''': ANY(lowercase_ ), '''box''': {'''xmin''': ANY(lowercase_ ), '''ymin''': ANY(lowercase_ ), '''xmax''': ANY(lowercase_ ), '''ymax''': ANY(lowercase_ )}, } , ) import datasets snake_case_ = datasets.load_dataset('''hf-internal-testing/fixtures_image_utils''' , '''image''' , split='''test''' ) snake_case_ = [ Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ), '''http://images.cocodataset.org/val2017/000000039769.jpg''', # RGBA dataset[0]['''file'''], # LA dataset[1]['''file'''], # L dataset[2]['''file'''], ] snake_case_ = object_detector(lowercase_ , threshold=0.0 ) self.assertEqual(len(lowercase_ ) , len(lowercase_ ) ) for outputs in batch_outputs: self.assertGreater(len(lowercase_ ) , 0 ) for detected_object in outputs: self.assertEqual( lowercase_ , { '''score''': ANY(lowercase_ ), '''label''': ANY(lowercase_ ), '''box''': {'''xmin''': ANY(lowercase_ ), '''ymin''': ANY(lowercase_ ), '''xmax''': ANY(lowercase_ ), '''ymax''': ANY(lowercase_ )}, } , ) @require_tf @unittest.skip('''Object detection not implemented in TF''' ) def A_ ( self : int ): pass @require_torch def A_ ( self : Tuple ): snake_case_ = '''hf-internal-testing/tiny-detr-mobilenetsv3''' snake_case_ = AutoModelForObjectDetection.from_pretrained(lowercase_ ) snake_case_ = AutoFeatureExtractor.from_pretrained(lowercase_ ) snake_case_ = ObjectDetectionPipeline(model=lowercase_ , feature_extractor=lowercase_ ) snake_case_ = object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg''' , threshold=0.0 ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ {'''score''': 0.3376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}}, {'''score''': 0.3376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}}, ] , ) snake_case_ = object_detector( [ '''http://images.cocodataset.org/val2017/000000039769.jpg''', '''http://images.cocodataset.org/val2017/000000039769.jpg''', ] , threshold=0.0 , ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ [ {'''score''': 0.3376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}}, {'''score''': 0.3376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}}, ], [ {'''score''': 0.3376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}}, {'''score''': 0.3376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}}, ], ] , ) @require_torch @slow def A_ ( self : Optional[int] ): snake_case_ = '''facebook/detr-resnet-50''' snake_case_ = AutoModelForObjectDetection.from_pretrained(lowercase_ ) snake_case_ = AutoFeatureExtractor.from_pretrained(lowercase_ ) snake_case_ = ObjectDetectionPipeline(model=lowercase_ , feature_extractor=lowercase_ ) snake_case_ = object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg''' ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ {'''score''': 0.9982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}}, {'''score''': 0.9960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}}, {'''score''': 0.9955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}}, {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}}, {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}}, ] , ) snake_case_ = object_detector( [ '''http://images.cocodataset.org/val2017/000000039769.jpg''', '''http://images.cocodataset.org/val2017/000000039769.jpg''', ] ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ [ {'''score''': 0.9982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}}, {'''score''': 0.9960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}}, {'''score''': 0.9955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}}, {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}}, {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}}, ], [ {'''score''': 0.9982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}}, {'''score''': 0.9960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}}, {'''score''': 0.9955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}}, {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}}, {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}}, ], ] , ) @require_torch @slow def A_ ( self : Tuple ): snake_case_ = '''facebook/detr-resnet-50''' snake_case_ = pipeline('''object-detection''' , model=lowercase_ ) snake_case_ = object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg''' ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ {'''score''': 0.9982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}}, {'''score''': 0.9960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}}, {'''score''': 0.9955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}}, {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}}, {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}}, ] , ) snake_case_ = object_detector( [ '''http://images.cocodataset.org/val2017/000000039769.jpg''', '''http://images.cocodataset.org/val2017/000000039769.jpg''', ] ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ [ {'''score''': 0.9982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}}, {'''score''': 0.9960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}}, {'''score''': 0.9955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}}, {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}}, {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}}, ], [ {'''score''': 0.9982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}}, {'''score''': 0.9960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}}, {'''score''': 0.9955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}}, {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}}, {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}}, ], ] , ) @require_torch @slow def A_ ( self : str ): snake_case_ = 0.9985 snake_case_ = '''facebook/detr-resnet-50''' snake_case_ = pipeline('''object-detection''' , model=lowercase_ ) snake_case_ = object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg''' , threshold=lowercase_ ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ {'''score''': 0.9988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}}, {'''score''': 0.9987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}}, ] , ) @require_torch @require_pytesseract @slow def A_ ( self : Dict ): snake_case_ = '''Narsil/layoutlmv3-finetuned-funsd''' snake_case_ = 0.9993 snake_case_ = pipeline('''object-detection''' , model=lowercase_ , threshold=lowercase_ ) snake_case_ = object_detector( '''https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png''' ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ {'''score''': 0.9993, '''label''': '''I-ANSWER''', '''box''': {'''xmin''': 294, '''ymin''': 254, '''xmax''': 343, '''ymax''': 264}}, {'''score''': 0.9993, '''label''': '''I-ANSWER''', '''box''': {'''xmin''': 294, '''ymin''': 254, '''xmax''': 343, '''ymax''': 264}}, ] , )
56
1
'''simple docstring''' import os from typing import Dict, List, Tuple, TypeVar, Union _UpperCamelCase = TypeVar('''T''') _UpperCamelCase = Union[List[T], Tuple[T, ...]] _UpperCamelCase = Union[T, List[T], Dict[str, T]] _UpperCamelCase = Union[str, bytes, os.PathLike]
350
'''simple docstring''' import unittest from transformers import ( MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TextClassificationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow from .test_pipelines_common import ANY # These 2 model types require different inputs than those of the usual text models. _UpperCamelCase = {'''LayoutLMv2Config''', '''LayoutLMv3Config'''} @is_pipeline_test class _A ( unittest.TestCase ): _SCREAMING_SNAKE_CASE : Optional[int] = MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING _SCREAMING_SNAKE_CASE : int = TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING if model_mapping is not None: _SCREAMING_SNAKE_CASE : int = {config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP} if tf_model_mapping is not None: _SCREAMING_SNAKE_CASE : Union[str, Any] = { config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP } @require_torch def __A ( self ) -> Tuple: '''simple docstring''' __UpperCAmelCase : int = pipeline( task="""text-classification""" , model="""hf-internal-testing/tiny-random-distilbert""" , framework="""pt""" ) __UpperCAmelCase : List[Any] = text_classifier("""This is great !""" ) self.assertEqual(nested_simplify(__UpperCAmelCase ) , [{"""label""": """LABEL_0""", """score""": 0.504}] ) __UpperCAmelCase : int = text_classifier("""This is great !""" , top_k=2 ) self.assertEqual( nested_simplify(__UpperCAmelCase ) , [{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}] ) __UpperCAmelCase : Optional[int] = text_classifier(["""This is great !""", """This is bad"""] , top_k=2 ) self.assertEqual( nested_simplify(__UpperCAmelCase ) , [ [{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}], [{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}], ] , ) __UpperCAmelCase : Union[str, Any] = text_classifier("""This is great !""" , top_k=1 ) self.assertEqual(nested_simplify(__UpperCAmelCase ) , [{"""label""": """LABEL_0""", """score""": 0.504}] ) # Legacy behavior __UpperCAmelCase : Union[str, Any] = text_classifier("""This is great !""" , return_all_scores=__UpperCAmelCase ) self.assertEqual(nested_simplify(__UpperCAmelCase ) , [{"""label""": """LABEL_0""", """score""": 0.504}] ) __UpperCAmelCase : Dict = text_classifier("""This is great !""" , return_all_scores=__UpperCAmelCase ) self.assertEqual( nested_simplify(__UpperCAmelCase ) , [[{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}]] ) __UpperCAmelCase : str = text_classifier(["""This is great !""", """Something else"""] , return_all_scores=__UpperCAmelCase ) self.assertEqual( nested_simplify(__UpperCAmelCase ) , [ [{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}], [{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}], ] , ) __UpperCAmelCase : Any = text_classifier(["""This is great !""", """Something else"""] , return_all_scores=__UpperCAmelCase ) self.assertEqual( nested_simplify(__UpperCAmelCase ) , [ {"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_0""", """score""": 0.504}, ] , ) @require_torch def __A ( self ) -> Dict: '''simple docstring''' import torch __UpperCAmelCase : Any = pipeline( task="""text-classification""" , model="""hf-internal-testing/tiny-random-distilbert""" , framework="""pt""" , device=torch.device("""cpu""" ) , ) __UpperCAmelCase : Union[str, Any] = text_classifier("""This is great !""" ) self.assertEqual(nested_simplify(__UpperCAmelCase ) , [{"""label""": """LABEL_0""", """score""": 0.504}] ) @require_tf def __A ( self ) -> Any: '''simple docstring''' __UpperCAmelCase : Any = pipeline( task="""text-classification""" , model="""hf-internal-testing/tiny-random-distilbert""" , framework="""tf""" ) __UpperCAmelCase : int = text_classifier("""This is great !""" ) self.assertEqual(nested_simplify(__UpperCAmelCase ) , [{"""label""": """LABEL_0""", """score""": 0.504}] ) @slow @require_torch def __A ( self ) -> List[str]: '''simple docstring''' __UpperCAmelCase : int = pipeline("""text-classification""" ) __UpperCAmelCase : int = text_classifier("""This is great !""" ) self.assertEqual(nested_simplify(__UpperCAmelCase ) , [{"""label""": """POSITIVE""", """score""": 1.0}] ) __UpperCAmelCase : Union[str, Any] = text_classifier("""This is bad !""" ) self.assertEqual(nested_simplify(__UpperCAmelCase ) , [{"""label""": """NEGATIVE""", """score""": 1.0}] ) __UpperCAmelCase : Any = text_classifier("""Birds are a type of animal""" ) self.assertEqual(nested_simplify(__UpperCAmelCase ) , [{"""label""": """POSITIVE""", """score""": 0.988}] ) @slow @require_tf def __A ( self ) -> Optional[Any]: '''simple docstring''' __UpperCAmelCase : str = pipeline("""text-classification""" , framework="""tf""" ) __UpperCAmelCase : Union[str, Any] = text_classifier("""This is great !""" ) self.assertEqual(nested_simplify(__UpperCAmelCase ) , [{"""label""": """POSITIVE""", """score""": 1.0}] ) __UpperCAmelCase : int = text_classifier("""This is bad !""" ) self.assertEqual(nested_simplify(__UpperCAmelCase ) , [{"""label""": """NEGATIVE""", """score""": 1.0}] ) __UpperCAmelCase : str = text_classifier("""Birds are a type of animal""" ) self.assertEqual(nested_simplify(__UpperCAmelCase ) , [{"""label""": """POSITIVE""", """score""": 0.988}] ) def __A ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Any: '''simple docstring''' __UpperCAmelCase : Any = TextClassificationPipeline(model=__UpperCAmelCase , tokenizer=__UpperCAmelCase ) return text_classifier, ["HuggingFace is in", "This is another test"] def __A ( self , __UpperCAmelCase , __UpperCAmelCase ) -> List[Any]: '''simple docstring''' __UpperCAmelCase : int = text_classifier.model # Small inputs because BartTokenizer tiny has maximum position embeddings = 22 __UpperCAmelCase : Union[str, Any] = """HuggingFace is in""" __UpperCAmelCase : Any = text_classifier(__UpperCAmelCase ) self.assertEqual(nested_simplify(__UpperCAmelCase ) , [{"""label""": ANY(__UpperCAmelCase ), """score""": ANY(__UpperCAmelCase )}] ) self.assertTrue(outputs[0]["""label"""] in model.config.idalabel.values() ) __UpperCAmelCase : Optional[int] = ["""HuggingFace is in """, """Paris is in France"""] __UpperCAmelCase : Any = text_classifier(__UpperCAmelCase ) self.assertEqual( nested_simplify(__UpperCAmelCase ) , [{"""label""": ANY(__UpperCAmelCase ), """score""": ANY(__UpperCAmelCase )}, {"""label""": ANY(__UpperCAmelCase ), """score""": ANY(__UpperCAmelCase )}] , ) self.assertTrue(outputs[0]["""label"""] in model.config.idalabel.values() ) self.assertTrue(outputs[1]["""label"""] in model.config.idalabel.values() ) # Forcing to get all results with `top_k=None` # This is NOT the legacy format __UpperCAmelCase : Any = text_classifier(__UpperCAmelCase , top_k=__UpperCAmelCase ) __UpperCAmelCase : Any = len(model.config.idalabel.values() ) self.assertEqual( nested_simplify(__UpperCAmelCase ) , [[{"""label""": ANY(__UpperCAmelCase ), """score""": ANY(__UpperCAmelCase )}] * N, [{"""label""": ANY(__UpperCAmelCase ), """score""": ANY(__UpperCAmelCase )}] * N] , ) __UpperCAmelCase : str = {"""text""": """HuggingFace is in """, """text_pair""": """Paris is in France"""} __UpperCAmelCase : Optional[int] = text_classifier(__UpperCAmelCase ) self.assertEqual( nested_simplify(__UpperCAmelCase ) , {"""label""": ANY(__UpperCAmelCase ), """score""": ANY(__UpperCAmelCase )} , ) self.assertTrue(outputs["""label"""] in model.config.idalabel.values() ) # This might be used a text pair, but tokenizer + pipe interaction # makes it hard to understand that it's not using the pair properly # https://github.com/huggingface/transformers/issues/17305 # We disabled this usage instead as it was outputting wrong outputs. __UpperCAmelCase : Union[str, Any] = [["""HuggingFace is in """, """Paris is in France"""]] with self.assertRaises(__UpperCAmelCase ): text_classifier(__UpperCAmelCase ) # This used to be valid for doing text pairs # We're keeping it working because of backward compatibility __UpperCAmelCase : Tuple = text_classifier([[["""HuggingFace is in """, """Paris is in France"""]]] ) self.assertEqual( nested_simplify(__UpperCAmelCase ) , [{"""label""": ANY(__UpperCAmelCase ), """score""": ANY(__UpperCAmelCase )}] , ) self.assertTrue(outputs[0]["""label"""] in model.config.idalabel.values() )
16
0
"""simple docstring""" from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, convert_to_rgb, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging lowercase__ = logging.get_logger(__name__) if is_vision_available(): import PIL class lowerCAmelCase__ ( _lowerCamelCase ): '''simple docstring''' lowerCamelCase__ = ["""pixel_values"""] def __init__( self , lowercase = True , lowercase = None , lowercase = PILImageResampling.BICUBIC , lowercase = True , lowercase = None , lowercase = True , lowercase = 1 / 255 , lowercase = True , lowercase = None , lowercase = None , lowercase = True , **lowercase , ): super().__init__(**lowercase ) _lowerCamelCase : int = size if size is not None else {'shortest_edge': 224} _lowerCamelCase : Optional[Any] = get_size_dict(lowercase , default_to_square=lowercase ) _lowerCamelCase : Optional[int] = crop_size if crop_size is not None else {'height': 224, 'width': 224} _lowerCamelCase : List[str] = get_size_dict(lowercase , default_to_square=lowercase , param_name='crop_size' ) _lowerCamelCase : Optional[Any] = do_resize _lowerCamelCase : Dict = size _lowerCamelCase : Dict = resample _lowerCamelCase : Optional[int] = do_center_crop _lowerCamelCase : int = crop_size _lowerCamelCase : Any = do_rescale _lowerCamelCase : Optional[int] = rescale_factor _lowerCamelCase : List[str] = do_normalize _lowerCamelCase : Any = image_mean if image_mean is not None else OPENAI_CLIP_MEAN _lowerCamelCase : Optional[int] = image_std if image_std is not None else OPENAI_CLIP_STD _lowerCamelCase : List[Any] = do_convert_rgb def A_ ( self , lowercase , lowercase , lowercase = PILImageResampling.BICUBIC , lowercase = None , **lowercase , ): _lowerCamelCase : int = get_size_dict(lowercase , default_to_square=lowercase ) if "shortest_edge" not in size: raise ValueError(F'''The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}''' ) _lowerCamelCase : Tuple = get_resize_output_image_size(lowercase , size=size['shortest_edge'] , default_to_square=lowercase ) return resize(lowercase , size=lowercase , resample=lowercase , data_format=lowercase , **lowercase ) def A_ ( self , lowercase , lowercase , lowercase = None , **lowercase , ): _lowerCamelCase : Tuple = get_size_dict(lowercase ) if "height" not in size or "width" not in size: raise ValueError(F'''The `size` parameter must contain the keys (height, width). Got {size.keys()}''' ) return center_crop(lowercase , size=(size['height'], size['width']) , data_format=lowercase , **lowercase ) def A_ ( self , lowercase , lowercase , lowercase = None , **lowercase , ): return rescale(lowercase , scale=lowercase , data_format=lowercase , **lowercase ) def A_ ( self , lowercase , lowercase , lowercase , lowercase = None , **lowercase , ): return normalize(lowercase , mean=lowercase , std=lowercase , data_format=lowercase , **lowercase ) def A_ ( self , lowercase , lowercase = None , lowercase = None , lowercase = None , lowercase = None , lowercase = None , lowercase = None , lowercase = None , lowercase = None , lowercase = None , lowercase = None , lowercase = None , lowercase = None , lowercase = ChannelDimension.FIRST , **lowercase , ): _lowerCamelCase : List[Any] = do_resize if do_resize is not None else self.do_resize _lowerCamelCase : Optional[Any] = size if size is not None else self.size _lowerCamelCase : Any = get_size_dict(lowercase , param_name='size' , default_to_square=lowercase ) _lowerCamelCase : Union[str, Any] = resample if resample is not None else self.resample _lowerCamelCase : List[str] = do_center_crop if do_center_crop is not None else self.do_center_crop _lowerCamelCase : Optional[int] = crop_size if crop_size is not None else self.crop_size _lowerCamelCase : Optional[Any] = get_size_dict(lowercase , param_name='crop_size' , default_to_square=lowercase ) _lowerCamelCase : Tuple = do_rescale if do_rescale is not None else self.do_rescale _lowerCamelCase : int = rescale_factor if rescale_factor is not None else self.rescale_factor _lowerCamelCase : Optional[int] = do_normalize if do_normalize is not None else self.do_normalize _lowerCamelCase : Dict = image_mean if image_mean is not None else self.image_mean _lowerCamelCase : Any = image_std if image_std is not None else self.image_std _lowerCamelCase : Optional[Any] = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb _lowerCamelCase : Dict = make_list_of_images(lowercase ) if not valid_images(lowercase ): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.' ) if do_resize and size is None: raise ValueError('Size must be specified if do_resize is True.' ) if do_center_crop and crop_size is None: raise ValueError('Crop size must be specified if do_center_crop is True.' ) if do_rescale and rescale_factor is None: raise ValueError('Rescale factor must be specified if do_rescale is True.' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('Image mean and std must be specified if do_normalize is True.' ) # PIL RGBA images are converted to RGB if do_convert_rgb: _lowerCamelCase : List[str] = [convert_to_rgb(lowercase ) for image in images] # All transformations expect numpy arrays. _lowerCamelCase : str = [to_numpy_array(lowercase ) for image in images] if do_resize: _lowerCamelCase : Dict = [self.resize(image=lowercase , size=lowercase , resample=lowercase ) for image in images] if do_center_crop: _lowerCamelCase : Tuple = [self.center_crop(image=lowercase , size=lowercase ) for image in images] if do_rescale: _lowerCamelCase : List[str] = [self.rescale(image=lowercase , scale=lowercase ) for image in images] if do_normalize: _lowerCamelCase : Optional[Any] = [self.normalize(image=lowercase , mean=lowercase , std=lowercase ) for image in images] _lowerCamelCase : int = [to_channel_dimension_format(lowercase , lowercase ) for image in images] _lowerCamelCase : Tuple = {'pixel_values': images} return BatchFeature(data=lowercase , tensor_type=lowercase )
96
"""simple docstring""" import math def _snake_case ( UpperCAmelCase_ : float , UpperCAmelCase_ : float ): if initial_intensity < 0: raise ValueError("""The value of intensity cannot be negative""" ) # handling of negative values of initial intensity if angle < 0 or angle > 360: raise ValueError("""In Malus Law, the angle is in the range 0-360 degrees""" ) # handling of values out of allowed range return initial_intensity * (math.cos(math.radians(UpperCAmelCase_ ) ) ** 2) if __name__ == "__main__": import doctest doctest.testmod(name='malus_law')
335
0
def __lowercase ( a__ ) -> list: if len(a__ ) <= 1: return [tuple(a__ )] __SCREAMING_SNAKE_CASE = [] def generate(a__ , a__ ): if k == 1: res.append(tuple(arr[:] ) ) return generate(k - 1 , a__ ) for i in range(k - 1 ): if k % 2 == 0: # k is even __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = arr[k - 1], arr[i] else: # k is odd __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = arr[k - 1], arr[0] generate(k - 1 , a__ ) generate(len(a__ ) , a__ ) return res if __name__ == "__main__": lowerCAmelCase__ : Optional[int] =input('''Enter numbers separated by a comma:\n''').strip() lowerCAmelCase__ : List[str] =[int(item) for item in user_input.split(''',''')] print(heaps(arr))
371
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ : Dict =logging.get_logger(__name__) lowerCAmelCase__ : Optional[int] ={ '''asapp/sew-tiny-100k''': '''https://huggingface.co/asapp/sew-tiny-100k/resolve/main/config.json''', # See all SEW models at https://huggingface.co/models?filter=sew } class UpperCAmelCase_ ( UpperCamelCase_ ): '''simple docstring''' UpperCamelCase__ : Optional[Any] = '''sew''' def __init__( self , _A=32 , _A=768 , _A=12 , _A=12 , _A=3_072 , _A=2 , _A="gelu" , _A=0.1 , _A=0.1 , _A=0.1 , _A=0.0 , _A=0.1 , _A=0.1 , _A=0.0_2 , _A=1e-5 , _A="group" , _A="gelu" , _A=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) , _A=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , _A=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , _A=False , _A=128 , _A=16 , _A=True , _A=0.0_5 , _A=10 , _A=2 , _A=0.0 , _A=10 , _A=0 , _A="mean" , _A=False , _A=False , _A=256 , _A=0 , _A=1 , _A=2 , **_A , ): '''simple docstring''' super().__init__(**_A , pad_token_id=_A , bos_token_id=_A , eos_token_id=_A ) __SCREAMING_SNAKE_CASE = hidden_size __SCREAMING_SNAKE_CASE = feat_extract_norm __SCREAMING_SNAKE_CASE = feat_extract_activation __SCREAMING_SNAKE_CASE = list(_A ) __SCREAMING_SNAKE_CASE = list(_A ) __SCREAMING_SNAKE_CASE = list(_A ) __SCREAMING_SNAKE_CASE = conv_bias __SCREAMING_SNAKE_CASE = num_conv_pos_embeddings __SCREAMING_SNAKE_CASE = num_conv_pos_embedding_groups __SCREAMING_SNAKE_CASE = len(self.conv_dim ) __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = intermediate_size __SCREAMING_SNAKE_CASE = squeeze_factor __SCREAMING_SNAKE_CASE = hidden_act __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = hidden_dropout __SCREAMING_SNAKE_CASE = attention_dropout __SCREAMING_SNAKE_CASE = activation_dropout __SCREAMING_SNAKE_CASE = feat_proj_dropout __SCREAMING_SNAKE_CASE = final_dropout __SCREAMING_SNAKE_CASE = layerdrop __SCREAMING_SNAKE_CASE = layer_norm_eps __SCREAMING_SNAKE_CASE = initializer_range __SCREAMING_SNAKE_CASE = vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( 'Configuration for convolutional layers is incorrect.' 'It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,' f"""but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)""" f"""= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.""" ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 __SCREAMING_SNAKE_CASE = apply_spec_augment __SCREAMING_SNAKE_CASE = mask_time_prob __SCREAMING_SNAKE_CASE = mask_time_length __SCREAMING_SNAKE_CASE = mask_time_min_masks __SCREAMING_SNAKE_CASE = mask_feature_prob __SCREAMING_SNAKE_CASE = mask_feature_length __SCREAMING_SNAKE_CASE = mask_feature_min_masks # ctc loss __SCREAMING_SNAKE_CASE = ctc_loss_reduction __SCREAMING_SNAKE_CASE = ctc_zero_infinity # sequence classification __SCREAMING_SNAKE_CASE = use_weighted_layer_sum __SCREAMING_SNAKE_CASE = classifier_proj_size @property def _A ( self ): '''simple docstring''' return functools.reduce(operator.mul , self.conv_stride , 1 )
118
0
"""simple docstring""" def _snake_case ( _snake_case : Union[str, Any] ) -> List[str]: '''simple docstring''' _A = [0] * len(UpperCamelCase_ ) _A = [] _A = [] _A = 0 for values in graph.values(): for i in values: indegree[i] += 1 for i in range(len(UpperCamelCase_ ) ): if indegree[i] == 0: queue.append(UpperCamelCase_ ) while queue: _A = queue.pop(0 ) cnt += 1 topo.append(UpperCamelCase_ ) for x in graph[vertex]: indegree[x] -= 1 if indegree[x] == 0: queue.append(UpperCamelCase_ ) if cnt != len(UpperCamelCase_ ): print('Cycle exists' ) else: print(UpperCamelCase_ ) # Adjacency List of Graph a = {0: [1, 2], 1: [3], 2: [3], 3: [4, 5], 4: [], 5: []} topological_sort(graph)
315
from __future__ import annotations def lowercase( UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ) -> list: '''simple docstring''' UpperCamelCase = [] UpperCamelCase , UpperCamelCase = input_list[low:mid], input_list[mid : high + 1] while left and right: result.append((left if left[0] <= right[0] else right).pop(0 ) ) UpperCamelCase = result + left + right return input_list def lowercase( UpperCamelCase_ ) -> list: '''simple docstring''' if len(UpperCamelCase_ ) <= 1: return input_list UpperCamelCase = list(UpperCamelCase_ ) # iteration for two-way merging UpperCamelCase = 2 while p <= len(UpperCamelCase_ ): # getting low, high and middle value for merge-sort of single list for i in range(0 , len(UpperCamelCase_ ) , UpperCamelCase_ ): UpperCamelCase = i UpperCamelCase = i + p - 1 UpperCamelCase = (low + high + 1) // 2 UpperCamelCase = merge(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ) # final merge of last two parts if p * 2 >= len(UpperCamelCase_ ): UpperCamelCase = i UpperCamelCase = merge(UpperCamelCase_ , 0 , UpperCamelCase_ , len(UpperCamelCase_ ) - 1 ) break p *= 2 return input_list if __name__ == "__main__": _SCREAMING_SNAKE_CASE = input("""Enter numbers separated by a comma:\n""").strip() if user_input == "": _SCREAMING_SNAKE_CASE = [] else: _SCREAMING_SNAKE_CASE = [int(item.strip()) for item in user_input.split(""",""")] print(iter_merge_sort(unsorted))
343
0
"""simple docstring""" import tempfile import unittest from transformers import TaConfig, is_torch_available from transformers.testing_utils import ( require_sentencepiece, require_tokenizers, require_torch, slow, torch_device, ) from ...generation.test_utils import GenerationTesterMixin from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import AutoTokenizer, UMTaForConditionalGeneration, UMTaForQuestionAnswering, UMTaModel class __A : def __init__( self , a__ , a__=99 , a__=13 , a__=7 , a__=9 , a__=True , a__=True , a__=False , a__=32 , a__=5 , a__=4 , a__=37 , a__=8 , a__=0.1 , a__=0.0_0_2 , a__=1 , a__=0 , a__=0 , a__=None , a__=None , ): _lowerCAmelCase : List[str] = parent _lowerCAmelCase : List[str] = batch_size _lowerCAmelCase : Optional[int] = encoder_seq_length _lowerCAmelCase : Optional[int] = decoder_seq_length # For common tests _lowerCAmelCase : List[str] = self.decoder_seq_length _lowerCAmelCase : List[Any] = is_training _lowerCAmelCase : str = use_attention_mask _lowerCAmelCase : Union[str, Any] = use_labels _lowerCAmelCase : int = vocab_size _lowerCAmelCase : str = hidden_size _lowerCAmelCase : Any = num_hidden_layers _lowerCAmelCase : str = num_attention_heads _lowerCAmelCase : Any = d_ff _lowerCAmelCase : Union[str, Any] = relative_attention_num_buckets _lowerCAmelCase : Dict = dropout_rate _lowerCAmelCase : str = initializer_factor _lowerCAmelCase : List[str] = eos_token_id _lowerCAmelCase : Tuple = pad_token_id _lowerCAmelCase : Optional[Any] = decoder_start_token_id _lowerCAmelCase : Tuple = None _lowerCAmelCase : int = decoder_layers def __A ( self ): return TaConfig.from_pretrained("""google/umt5-base""" ) def __A ( self , a__ , a__ , a__ , a__=None , a__=None , a__=None , a__=None , a__=None , ): if attention_mask is None: _lowerCAmelCase : Optional[int] = input_ids.ne(config.pad_token_id ) if decoder_attention_mask is None: _lowerCAmelCase : Tuple = decoder_input_ids.ne(config.pad_token_id ) if head_mask is None: _lowerCAmelCase : str = torch.ones(config.num_hidden_layers , config.num_attention_heads , device=_lowerCamelCase ) if decoder_head_mask is None: _lowerCAmelCase : Optional[int] = torch.ones(config.num_decoder_layers , config.num_attention_heads , device=_lowerCamelCase ) if cross_attn_head_mask is None: _lowerCAmelCase : List[str] = torch.ones( config.num_decoder_layers , config.num_attention_heads , device=_lowerCamelCase ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } def __A ( self ): _lowerCAmelCase : Optional[int] = ids_tensor([self.batch_size, self.encoder_seq_length] , self.vocab_size ) _lowerCAmelCase : Dict = ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) # we need to clamp the input ids here to avoid having pad token in between # this is because for NllbMoe the position_ids are prepared such that # all pad tokens have pos id = 2 and rest are between 2..seq_length # and the seq_length here is seq_length - num_pad_tokens # but when using past, there is no way of knowing if the past input ids had # pad tokens in them, which results in incorrect seq_lenth and which in turn results in # position_ids being off by num_pad_tokens in past input _lowerCAmelCase : str = input_ids.clamp(self.pad_token_id + 1 ) _lowerCAmelCase : Optional[Any] = decoder_input_ids.clamp(self.pad_token_id + 1 ) _lowerCAmelCase : List[str] = self.get_config() _lowerCAmelCase : List[str] = config.num_attention_heads _lowerCAmelCase : List[Any] = self.prepare_inputs_dict(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) return config, input_dict def __A ( self ): _lowerCAmelCase , _lowerCAmelCase : List[str] = self.prepare_config_and_inputs() return config, inputs_dict def __A ( self ): return TaConfig( vocab_size=166 , d_model=self.hidden_size , d_ff=self.d_ff , d_kv=self.hidden_size // self.num_attention_heads , num_layers=self.num_hidden_layers , num_decoder_layers=self.decoder_layers , num_heads=self.num_attention_heads , relative_attention_num_buckets=self.relative_attention_num_buckets , dropout_rate=self.dropout_rate , initializer_factor=self.initializer_factor , eos_token_id=self.eos_token_id , bos_token_id=self.pad_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.decoder_start_token_id , ) def __A ( self ): return TaConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , d_ff=self.d_ff , d_kv=self.hidden_size // self.num_attention_heads , num_layers=self.num_hidden_layers , num_decoder_layers=self.decoder_layers , num_heads=self.num_attention_heads , relative_attention_num_buckets=self.relative_attention_num_buckets , dropout_rate=self.dropout_rate , initializer_factor=self.initializer_factor , eos_token_id=self.eos_token_id , bos_token_id=self.pad_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.decoder_start_token_id , ) def __A ( self , a__ , a__ , a__ , a__ , a__ , a__ , ): _lowerCAmelCase : Dict = UMTaModel(config=_lowerCamelCase ) model.to(_lowerCamelCase ) model.eval() _lowerCAmelCase : Optional[Any] = model( input_ids=_lowerCamelCase , decoder_input_ids=_lowerCamelCase , attention_mask=_lowerCamelCase , decoder_attention_mask=_lowerCamelCase , ) _lowerCAmelCase : List[Any] = model(input_ids=_lowerCamelCase , decoder_input_ids=_lowerCamelCase ) _lowerCAmelCase : List[str] = result.last_hidden_state _lowerCAmelCase : List[Any] = result.past_key_values _lowerCAmelCase : int = result.encoder_last_hidden_state self.parent.assertEqual(encoder_output.size() , (self.batch_size, self.encoder_seq_length, self.hidden_size) ) self.parent.assertEqual(decoder_output.size() , (self.batch_size, self.decoder_seq_length, self.hidden_size) ) # There should be `num_layers` key value embeddings stored in decoder_past self.parent.assertEqual(len(_lowerCamelCase ) , config.num_layers ) # There should be a self attn key, a self attn value, a cross attn key and a cross attn value stored in each decoder_past tuple self.parent.assertEqual(len(decoder_past[0] ) , 4 ) def __A ( self , a__ , a__ , a__ , a__ , a__ , a__ , ): _lowerCAmelCase : List[str] = UMTaModel(config=_lowerCamelCase ).get_decoder().to(_lowerCamelCase ).eval() # first forward pass _lowerCAmelCase : Any = model(_lowerCamelCase , use_cache=_lowerCamelCase ) _lowerCAmelCase : Optional[int] = model(_lowerCamelCase ) _lowerCAmelCase : Dict = model(_lowerCamelCase , use_cache=_lowerCamelCase ) self.parent.assertTrue(len(_lowerCamelCase ) == len(_lowerCamelCase ) ) self.parent.assertTrue(len(_lowerCamelCase ) == len(_lowerCamelCase ) + 1 ) _lowerCAmelCase , _lowerCAmelCase : List[str] = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids _lowerCAmelCase : Dict = ids_tensor((self.batch_size, 1) , config.vocab_size ) # append to next input_ids and _lowerCAmelCase : Any = torch.cat([input_ids, next_tokens] , dim=-1 ) _lowerCAmelCase : Any = model(_lowerCamelCase )["""last_hidden_state"""] _lowerCAmelCase : Optional[int] = model(_lowerCamelCase , past_key_values=_lowerCamelCase )["""last_hidden_state"""] # select random slice _lowerCAmelCase : Tuple = ids_tensor((1,) , output_from_past.shape[-1] ).item() _lowerCAmelCase : Optional[Any] = output_from_no_past[:, -1, random_slice_idx].detach() _lowerCAmelCase : List[Any] = output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(_lowerCamelCase , _lowerCamelCase , atol=1e-3 ) ) def __A ( self , a__ , a__ , ): _lowerCAmelCase : Tuple = UMTaModel(config=_lowerCamelCase ).to(_lowerCamelCase ).half().eval() _lowerCAmelCase : int = model(**_lowerCamelCase )["""last_hidden_state"""] self.parent.assertFalse(torch.isnan(_lowerCamelCase ).any().item() ) @require_torch class __A ( A_ , A_ , A_ , unittest.TestCase ): _UpperCamelCase : str = ( (UMTaModel, UMTaForConditionalGeneration, UMTaForQuestionAnswering) if is_torch_available() else () ) _UpperCamelCase : Union[str, Any] = (UMTaForConditionalGeneration,) if is_torch_available() else () _UpperCamelCase : Tuple = ( { "conversational": UMTaForConditionalGeneration, "feature-extraction": UMTaModel, "summarization": UMTaForConditionalGeneration, "text2text-generation": UMTaForConditionalGeneration, "translation": UMTaForConditionalGeneration, "question-answering": UMTaForQuestionAnswering, } if is_torch_available() else {} ) _UpperCamelCase : Dict = True _UpperCamelCase : str = False _UpperCamelCase : Any = False _UpperCamelCase : str = True _UpperCamelCase : Tuple = True # The small UMT5 model needs higher percentages for CPU/MP tests _UpperCamelCase : Any = [0.8, 0.9] def __A ( self ): _lowerCAmelCase : Dict = UMTaModelTester(self ) @unittest.skip("""Test has a segmentation fault on torch 1.8.0""" ) def __A ( self ): _lowerCAmelCase : Dict = self.model_tester.prepare_config_and_inputs() _lowerCAmelCase : List[Any] = UMTaModel(config_and_inputs[0] ).to(_lowerCamelCase ) with tempfile.TemporaryDirectory() as tmpdirname: torch.onnx.export( _lowerCamelCase , (config_and_inputs[1], config_and_inputs[3], config_and_inputs[2]) , F"{tmpdirname}/t5_test.onnx" , export_params=_lowerCamelCase , opset_version=9 , input_names=["""input_ids""", """decoder_input_ids"""] , ) @unittest.skipIf(torch_device == """cpu""" , """Cant do half precision""" ) def __A ( self ): _lowerCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model_fpaa_forward(*_lowerCamelCase ) def __A ( self ): _lowerCAmelCase : Optional[int] = ["""encoder_attentions""", """decoder_attentions""", """cross_attentions"""] _lowerCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() _lowerCAmelCase : str = config_and_inputs[0] _lowerCAmelCase : Union[str, Any] = UMTaForConditionalGeneration(_lowerCamelCase ).eval() model.to(_lowerCamelCase ) _lowerCAmelCase : Union[str, Any] = { """head_mask""": torch.zeros(config.num_layers , config.num_heads , device=_lowerCamelCase ), """decoder_head_mask""": torch.zeros(config.num_decoder_layers , config.num_heads , device=_lowerCamelCase ), """cross_attn_head_mask""": torch.zeros(config.num_decoder_layers , config.num_heads , device=_lowerCamelCase ), } for attn_name, (name, mask) in zip(_lowerCamelCase , head_masking.items() ): _lowerCAmelCase : int = {name: mask} # Explicitly pass decoder_head_mask as it is required from T5 model when head_mask specified if name == "head_mask": _lowerCAmelCase : str = torch.ones( config.num_decoder_layers , config.num_heads , device=_lowerCamelCase ) _lowerCAmelCase : int = model.generate( config_and_inputs[1]["""input_ids"""] , num_beams=1 , max_length=3 , output_attentions=_lowerCamelCase , return_dict_in_generate=_lowerCamelCase , **_lowerCamelCase , ) # We check the state of decoder_attentions and cross_attentions just from the last step _lowerCAmelCase : Union[str, Any] = out[attn_name] if attn_name == attention_names[0] else out[attn_name][-1] self.assertEqual(sum([w.sum().item() for w in attn_weights] ) , 0.0 ) @unittest.skip("""Does not work on the tiny model as we keep hitting edge cases.""" ) def __A ( self ): pass @require_torch @require_sentencepiece @require_tokenizers class __A ( unittest.TestCase ): @slow @unittest.skip( """Unless we stop stripping left and right by default for all special tokens, the expected ids obtained here will not match the original ones. Wait for https://github.com/huggingface/transformers/pull/23909 to be merged""" ) def __A ( self ): _lowerCAmelCase : Optional[Any] = UMTaForConditionalGeneration.from_pretrained("""google/umt5-small""" , return_dict=_lowerCamelCase ).to(_lowerCamelCase ) _lowerCAmelCase : str = AutoTokenizer.from_pretrained("""google/umt5-small""" , use_fast=_lowerCamelCase , legacy=_lowerCamelCase ) _lowerCAmelCase : Tuple = [ """Bonjour monsieur <extra_id_0> bien <extra_id_1>.""", """No se como puedo <extra_id_0>.""", """This is the reason why we <extra_id_0> them.""", """The <extra_id_0> walks in <extra_id_1>, seats""", """A <extra_id_0> walks into a bar and orders a <extra_id_1> with <extra_id_2> pinch of <extra_id_3>.""", ] _lowerCAmelCase : Optional[Any] = tokenizer(_lowerCamelCase , return_tensors="""pt""" , padding=_lowerCamelCase ).input_ids # fmt: off _lowerCAmelCase : str = torch.tensor( [ [ 38530, 210703, 256299, 1410, 256298, 274, 1, 0,0, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 826, 321, 671, 25922, 256299, 274, 1, 0,0, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 1460, 339, 312, 19014, 10620, 758, 256299, 2355,274, 1, 0, 0, 0, 0, 0, 0,0, 0], [ 517, 256299, 14869, 281, 301, 256298, 275, 119983,1, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 320, 256299, 14869, 281, 2234, 289, 2275, 333,61391, 289, 256298, 543, 256297, 168714, 329, 256296,274, 1], ] ) # fmt: on torch.testing.assert_allclose(_lowerCamelCase , _lowerCamelCase ) _lowerCAmelCase : List[Any] = model.generate(input_ids.to(_lowerCamelCase ) ) _lowerCAmelCase : List[str] = [ """<pad><extra_id_0> et<extra_id_1> [eod] <extra_id_2><extra_id_55>.. [eod] 💐 💐 💐 💐 💐 💐 💐 💐 💐 💐 💐 <extra_id_56>ajšietosto<extra_id_56>lleux<extra_id_19><extra_id_6>ajšie</s>""", """<pad><extra_id_0>.<extra_id_1>.,<0x0A>...spech <0x0A><extra_id_20> <extra_id_21></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>""", """<pad><extra_id_0> are not going to be a part of the world. We are not going to be a part of<extra_id_1> and<extra_id_2><0x0A><extra_id_48>.<extra_id_48></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>""", """<pad><extra_id_0> door<extra_id_1>, the door<extra_id_2> 피해[/</s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>""", """<pad><extra_id_0>nyone who<extra_id_1> drink<extra_id_2> a<extra_id_3> alcohol<extra_id_4> A<extra_id_5> A. This<extra_id_6> I<extra_id_7><extra_id_52><extra_id_53></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>""", ] _lowerCAmelCase : int = tokenizer.batch_decode(_lowerCamelCase ) self.assertEqual(_lowerCamelCase , _lowerCamelCase )
368
"""simple docstring""" _a : Any = '\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' _a : List[Any] = [{'type': 'code', 'content': INSTALL_CONTENT}] _a : Union[str, Any] = { '{processor_class}': 'FakeProcessorClass', '{model_class}': 'FakeModelClass', '{object_class}': 'FakeObjectClass', }
126
0
'''simple docstring''' from typing import Optional import torch import torch.utils.checkpoint from torch import Tensor, nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import ( BackboneOutput, BaseModelOutputWithNoAttention, BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention, ) from ...modeling_utils import PreTrainedModel from ...utils import ( add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging, replace_return_docstrings, ) from ...utils.backbone_utils import BackboneMixin from .configuration_resnet import ResNetConfig _lowerCamelCase : Dict = logging.get_logger(__name__) # General docstring _lowerCamelCase : List[str] = "ResNetConfig" # Base docstring _lowerCamelCase : Tuple = "microsoft/resnet-50" _lowerCamelCase : Union[str, Any] = [1, 2048, 7, 7] # Image classification docstring _lowerCamelCase : Dict = "microsoft/resnet-50" _lowerCamelCase : Optional[int] = "tiger cat" _lowerCamelCase : str = [ "microsoft/resnet-50", # See all resnet models at https://huggingface.co/models?filter=resnet ] class SCREAMING_SNAKE_CASE ( nn.Module ): """simple docstring""" def __init__( self : Dict , UpperCamelCase__ : int , UpperCamelCase__ : int , UpperCamelCase__ : int = 3 , UpperCamelCase__ : int = 1 , UpperCamelCase__ : str = "relu" ): """simple docstring""" super().__init__() UpperCamelCase = nn.Convad( UpperCamelCase__ , UpperCamelCase__ , kernel_size=UpperCamelCase__ , stride=UpperCamelCase__ , padding=kernel_size // 2 , bias=UpperCamelCase__ ) UpperCamelCase = nn.BatchNormad(UpperCamelCase__ ) UpperCamelCase = ACTaFN[activation] if activation is not None else nn.Identity() def A ( self : Dict , UpperCamelCase__ : Tensor ): """simple docstring""" UpperCamelCase = self.convolution(UpperCamelCase__ ) UpperCamelCase = self.normalization(UpperCamelCase__ ) UpperCamelCase = self.activation(UpperCamelCase__ ) return hidden_state class SCREAMING_SNAKE_CASE ( nn.Module ): """simple docstring""" def __init__( self : Any , UpperCamelCase__ : ResNetConfig ): """simple docstring""" super().__init__() UpperCamelCase = ResNetConvLayer( config.num_channels , config.embedding_size , kernel_size=7 , stride=2 , activation=config.hidden_act ) UpperCamelCase = nn.MaxPoolad(kernel_size=3 , stride=2 , padding=1 ) UpperCamelCase = config.num_channels def A ( self : Union[str, Any] , UpperCamelCase__ : Tensor ): """simple docstring""" UpperCamelCase = pixel_values.shape[1] if num_channels != self.num_channels: raise ValueError( 'Make sure that the channel dimension of the pixel values match with the one set in the configuration.' ) UpperCamelCase = self.embedder(UpperCamelCase__ ) UpperCamelCase = self.pooler(UpperCamelCase__ ) return embedding class SCREAMING_SNAKE_CASE ( nn.Module ): """simple docstring""" def __init__( self : Union[str, Any] , UpperCamelCase__ : int , UpperCamelCase__ : int , UpperCamelCase__ : int = 2 ): """simple docstring""" super().__init__() UpperCamelCase = nn.Convad(UpperCamelCase__ , UpperCamelCase__ , kernel_size=1 , stride=UpperCamelCase__ , bias=UpperCamelCase__ ) UpperCamelCase = nn.BatchNormad(UpperCamelCase__ ) def A ( self : Any , UpperCamelCase__ : Tensor ): """simple docstring""" UpperCamelCase = self.convolution(UpperCamelCase__ ) UpperCamelCase = self.normalization(UpperCamelCase__ ) return hidden_state class SCREAMING_SNAKE_CASE ( nn.Module ): """simple docstring""" def __init__( self : Optional[int] , UpperCamelCase__ : int , UpperCamelCase__ : int , UpperCamelCase__ : int = 1 , UpperCamelCase__ : str = "relu" ): """simple docstring""" super().__init__() UpperCamelCase = in_channels != out_channels or stride != 1 UpperCamelCase = ( ResNetShortCut(UpperCamelCase__ , UpperCamelCase__ , stride=UpperCamelCase__ ) if should_apply_shortcut else nn.Identity() ) UpperCamelCase = nn.Sequential( ResNetConvLayer(UpperCamelCase__ , UpperCamelCase__ , stride=UpperCamelCase__ ) , ResNetConvLayer(UpperCamelCase__ , UpperCamelCase__ , activation=UpperCamelCase__ ) , ) UpperCamelCase = ACTaFN[activation] def A ( self : str , UpperCamelCase__ : Union[str, Any] ): """simple docstring""" UpperCamelCase = hidden_state UpperCamelCase = self.layer(UpperCamelCase__ ) UpperCamelCase = self.shortcut(UpperCamelCase__ ) hidden_state += residual UpperCamelCase = self.activation(UpperCamelCase__ ) return hidden_state class SCREAMING_SNAKE_CASE ( nn.Module ): """simple docstring""" def __init__( self : Optional[Any] , UpperCamelCase__ : int , UpperCamelCase__ : int , UpperCamelCase__ : int = 1 , UpperCamelCase__ : str = "relu" , UpperCamelCase__ : int = 4 ): """simple docstring""" super().__init__() UpperCamelCase = in_channels != out_channels or stride != 1 UpperCamelCase = out_channels // reduction UpperCamelCase = ( ResNetShortCut(UpperCamelCase__ , UpperCamelCase__ , stride=UpperCamelCase__ ) if should_apply_shortcut else nn.Identity() ) UpperCamelCase = nn.Sequential( ResNetConvLayer(UpperCamelCase__ , UpperCamelCase__ , kernel_size=1 ) , ResNetConvLayer(UpperCamelCase__ , UpperCamelCase__ , stride=UpperCamelCase__ ) , ResNetConvLayer(UpperCamelCase__ , UpperCamelCase__ , kernel_size=1 , activation=UpperCamelCase__ ) , ) UpperCamelCase = ACTaFN[activation] def A ( self : Dict , UpperCamelCase__ : Optional[Any] ): """simple docstring""" UpperCamelCase = hidden_state UpperCamelCase = self.layer(UpperCamelCase__ ) UpperCamelCase = self.shortcut(UpperCamelCase__ ) hidden_state += residual UpperCamelCase = self.activation(UpperCamelCase__ ) return hidden_state class SCREAMING_SNAKE_CASE ( nn.Module ): """simple docstring""" def __init__( self : List[str] , UpperCamelCase__ : ResNetConfig , UpperCamelCase__ : int , UpperCamelCase__ : int , UpperCamelCase__ : int = 2 , UpperCamelCase__ : int = 2 , ): """simple docstring""" super().__init__() UpperCamelCase = ResNetBottleNeckLayer if config.layer_type == 'bottleneck' else ResNetBasicLayer UpperCamelCase = nn.Sequential( # downsampling is done in the first layer with stride of 2 layer(UpperCamelCase__ , UpperCamelCase__ , stride=UpperCamelCase__ , activation=config.hidden_act ) , *[layer(UpperCamelCase__ , UpperCamelCase__ , activation=config.hidden_act ) for _ in range(depth - 1 )] , ) def A ( self : Dict , UpperCamelCase__ : Tensor ): """simple docstring""" UpperCamelCase = input for layer in self.layers: UpperCamelCase = layer(UpperCamelCase__ ) return hidden_state class SCREAMING_SNAKE_CASE ( nn.Module ): """simple docstring""" def __init__( self : Dict , UpperCamelCase__ : ResNetConfig ): """simple docstring""" super().__init__() UpperCamelCase = nn.ModuleList([] ) # based on `downsample_in_first_stage` the first layer of the first stage may or may not downsample the input self.stages.append( ResNetStage( UpperCamelCase__ , config.embedding_size , config.hidden_sizes[0] , stride=2 if config.downsample_in_first_stage else 1 , depth=config.depths[0] , ) ) UpperCamelCase = zip(config.hidden_sizes , config.hidden_sizes[1:] ) for (in_channels, out_channels), depth in zip(UpperCamelCase__ , config.depths[1:] ): self.stages.append(ResNetStage(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , depth=UpperCamelCase__ ) ) def A ( self : Tuple , UpperCamelCase__ : Tensor , UpperCamelCase__ : bool = False , UpperCamelCase__ : bool = True ): """simple docstring""" UpperCamelCase = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: UpperCamelCase = hidden_states + (hidden_state,) UpperCamelCase = stage_module(UpperCamelCase__ ) if output_hidden_states: UpperCamelCase = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return BaseModelOutputWithNoAttention( last_hidden_state=UpperCamelCase__ , hidden_states=UpperCamelCase__ , ) class SCREAMING_SNAKE_CASE ( _a ): """simple docstring""" _SCREAMING_SNAKE_CASE = ResNetConfig _SCREAMING_SNAKE_CASE = """resnet""" _SCREAMING_SNAKE_CASE = """pixel_values""" _SCREAMING_SNAKE_CASE = True def A ( self : List[str] , UpperCamelCase__ : Dict ): """simple docstring""" if isinstance(UpperCamelCase__ , nn.Convad ): nn.init.kaiming_normal_(module.weight , mode='fan_out' , nonlinearity='relu' ) elif isinstance(UpperCamelCase__ , (nn.BatchNormad, nn.GroupNorm) ): nn.init.constant_(module.weight , 1 ) nn.init.constant_(module.bias , 0 ) def A ( self : Dict , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : str=False ): """simple docstring""" if isinstance(UpperCamelCase__ , UpperCamelCase__ ): UpperCamelCase = value _lowerCamelCase : Union[str, Any] = R"\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n\n Parameters:\n config ([`ResNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n" _lowerCamelCase : Any = R"\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`ConvNextImageProcessor.__call__`] for details.\n\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n" @add_start_docstrings( """The bare ResNet model outputting raw features without any specific head on top.""" , _a , ) class SCREAMING_SNAKE_CASE ( _a ): """simple docstring""" def __init__( self : str , UpperCamelCase__ : Any ): """simple docstring""" super().__init__(UpperCamelCase__ ) UpperCamelCase = config UpperCamelCase = ResNetEmbeddings(UpperCamelCase__ ) UpperCamelCase = ResNetEncoder(UpperCamelCase__ ) UpperCamelCase = nn.AdaptiveAvgPoolad((1, 1) ) # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UpperCamelCase__ ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=UpperCamelCase__ , config_class=_CONFIG_FOR_DOC , modality='vision' , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def A ( self : str , UpperCamelCase__ : Tensor , UpperCamelCase__ : Optional[bool] = None , UpperCamelCase__ : Optional[bool] = None ): """simple docstring""" UpperCamelCase = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) UpperCamelCase = return_dict if return_dict is not None else self.config.use_return_dict UpperCamelCase = self.embedder(UpperCamelCase__ ) UpperCamelCase = self.encoder( UpperCamelCase__ , output_hidden_states=UpperCamelCase__ , return_dict=UpperCamelCase__ ) UpperCamelCase = encoder_outputs[0] UpperCamelCase = self.pooler(UpperCamelCase__ ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=UpperCamelCase__ , pooler_output=UpperCamelCase__ , hidden_states=encoder_outputs.hidden_states , ) @add_start_docstrings( """ ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. """ , _a , ) class SCREAMING_SNAKE_CASE ( _a ): """simple docstring""" def __init__( self : int , UpperCamelCase__ : Dict ): """simple docstring""" super().__init__(UpperCamelCase__ ) UpperCamelCase = config.num_labels UpperCamelCase = ResNetModel(UpperCamelCase__ ) # classification head UpperCamelCase = nn.Sequential( nn.Flatten() , nn.Linear(config.hidden_sizes[-1] , config.num_labels ) if config.num_labels > 0 else nn.Identity() , ) # initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UpperCamelCase__ ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=UpperCamelCase__ , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def A ( self : List[Any] , UpperCamelCase__ : Optional[torch.FloatTensor] = None , UpperCamelCase__ : Optional[torch.LongTensor] = None , UpperCamelCase__ : Optional[bool] = None , UpperCamelCase__ : Optional[bool] = None , ): """simple docstring""" UpperCamelCase = return_dict if return_dict is not None else self.config.use_return_dict UpperCamelCase = self.resnet(UpperCamelCase__ , output_hidden_states=UpperCamelCase__ , return_dict=UpperCamelCase__ ) UpperCamelCase = outputs.pooler_output if return_dict else outputs[1] UpperCamelCase = self.classifier(UpperCamelCase__ ) UpperCamelCase = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: UpperCamelCase = 'regression' elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): UpperCamelCase = 'single_label_classification' else: UpperCamelCase = 'multi_label_classification' if self.config.problem_type == "regression": UpperCamelCase = MSELoss() if self.num_labels == 1: UpperCamelCase = loss_fct(logits.squeeze() , labels.squeeze() ) else: UpperCamelCase = loss_fct(UpperCamelCase__ , UpperCamelCase__ ) elif self.config.problem_type == "single_label_classification": UpperCamelCase = CrossEntropyLoss() UpperCamelCase = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": UpperCamelCase = BCEWithLogitsLoss() UpperCamelCase = loss_fct(UpperCamelCase__ , UpperCamelCase__ ) if not return_dict: UpperCamelCase = (logits,) + outputs[2:] return (loss,) + output if loss is not None else output return ImageClassifierOutputWithNoAttention(loss=UpperCamelCase__ , logits=UpperCamelCase__ , hidden_states=outputs.hidden_states ) @add_start_docstrings( """ ResNet backbone, to be used with frameworks like DETR and MaskFormer. """ , _a , ) class SCREAMING_SNAKE_CASE ( _a , _a ): """simple docstring""" def __init__( self : Dict , UpperCamelCase__ : Tuple ): """simple docstring""" super().__init__(UpperCamelCase__ ) super()._init_backbone(UpperCamelCase__ ) UpperCamelCase = [config.embedding_size] + config.hidden_sizes UpperCamelCase = ResNetEmbeddings(UpperCamelCase__ ) UpperCamelCase = ResNetEncoder(UpperCamelCase__ ) # initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UpperCamelCase__ ) @replace_return_docstrings(output_type=UpperCamelCase__ , config_class=_CONFIG_FOR_DOC ) def A ( self : Any , UpperCamelCase__ : Tensor , UpperCamelCase__ : Optional[bool] = None , UpperCamelCase__ : Optional[bool] = None ): """simple docstring""" UpperCamelCase = return_dict if return_dict is not None else self.config.use_return_dict UpperCamelCase = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) UpperCamelCase = self.embedder(UpperCamelCase__ ) UpperCamelCase = self.encoder(UpperCamelCase__ , output_hidden_states=UpperCamelCase__ , return_dict=UpperCamelCase__ ) UpperCamelCase = outputs.hidden_states UpperCamelCase = () for idx, stage in enumerate(self.stage_names ): if stage in self.out_features: feature_maps += (hidden_states[idx],) if not return_dict: UpperCamelCase = (feature_maps,) if output_hidden_states: output += (outputs.hidden_states,) return output return BackboneOutput( feature_maps=UpperCamelCase__ , hidden_states=outputs.hidden_states if output_hidden_states else None , attentions=UpperCamelCase__ , )
28
'''simple docstring''' import argparse from collections import OrderedDict from pathlib import Path import torch from huggingface_hub import hf_hub_download from PIL import Image from torchvision.transforms import functional as F from transformers import DetrImageProcessor, TableTransformerConfig, TableTransformerForObjectDetection from transformers.utils import logging logging.set_verbosity_info() _lowerCamelCase : int = logging.get_logger(__name__) # here we list all keys to be renamed (original name on the left, our name on the right) _lowerCamelCase : int = [] for i in range(6): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append( (f'''transformer.encoder.layers.{i}.self_attn.out_proj.weight''', f'''encoder.layers.{i}.self_attn.out_proj.weight''') ) rename_keys.append( (f'''transformer.encoder.layers.{i}.self_attn.out_proj.bias''', f'''encoder.layers.{i}.self_attn.out_proj.bias''') ) rename_keys.append((f'''transformer.encoder.layers.{i}.linear1.weight''', f'''encoder.layers.{i}.fc1.weight''')) rename_keys.append((f'''transformer.encoder.layers.{i}.linear1.bias''', f'''encoder.layers.{i}.fc1.bias''')) rename_keys.append((f'''transformer.encoder.layers.{i}.linear2.weight''', f'''encoder.layers.{i}.fc2.weight''')) rename_keys.append((f'''transformer.encoder.layers.{i}.linear2.bias''', f'''encoder.layers.{i}.fc2.bias''')) rename_keys.append( (f'''transformer.encoder.layers.{i}.norm1.weight''', f'''encoder.layers.{i}.self_attn_layer_norm.weight''') ) rename_keys.append((f'''transformer.encoder.layers.{i}.norm1.bias''', f'''encoder.layers.{i}.self_attn_layer_norm.bias''')) rename_keys.append((f'''transformer.encoder.layers.{i}.norm2.weight''', f'''encoder.layers.{i}.final_layer_norm.weight''')) rename_keys.append((f'''transformer.encoder.layers.{i}.norm2.bias''', f'''encoder.layers.{i}.final_layer_norm.bias''')) # decoder layers: 2 times output projection, 2 feedforward neural networks and 3 layernorms rename_keys.append( (f'''transformer.decoder.layers.{i}.self_attn.out_proj.weight''', f'''decoder.layers.{i}.self_attn.out_proj.weight''') ) rename_keys.append( (f'''transformer.decoder.layers.{i}.self_attn.out_proj.bias''', f'''decoder.layers.{i}.self_attn.out_proj.bias''') ) rename_keys.append( ( f'''transformer.decoder.layers.{i}.multihead_attn.out_proj.weight''', f'''decoder.layers.{i}.encoder_attn.out_proj.weight''', ) ) rename_keys.append( ( f'''transformer.decoder.layers.{i}.multihead_attn.out_proj.bias''', f'''decoder.layers.{i}.encoder_attn.out_proj.bias''', ) ) rename_keys.append((f'''transformer.decoder.layers.{i}.linear1.weight''', f'''decoder.layers.{i}.fc1.weight''')) rename_keys.append((f'''transformer.decoder.layers.{i}.linear1.bias''', f'''decoder.layers.{i}.fc1.bias''')) rename_keys.append((f'''transformer.decoder.layers.{i}.linear2.weight''', f'''decoder.layers.{i}.fc2.weight''')) rename_keys.append((f'''transformer.decoder.layers.{i}.linear2.bias''', f'''decoder.layers.{i}.fc2.bias''')) rename_keys.append( (f'''transformer.decoder.layers.{i}.norm1.weight''', f'''decoder.layers.{i}.self_attn_layer_norm.weight''') ) rename_keys.append((f'''transformer.decoder.layers.{i}.norm1.bias''', f'''decoder.layers.{i}.self_attn_layer_norm.bias''')) rename_keys.append( (f'''transformer.decoder.layers.{i}.norm2.weight''', f'''decoder.layers.{i}.encoder_attn_layer_norm.weight''') ) rename_keys.append( (f'''transformer.decoder.layers.{i}.norm2.bias''', f'''decoder.layers.{i}.encoder_attn_layer_norm.bias''') ) rename_keys.append((f'''transformer.decoder.layers.{i}.norm3.weight''', f'''decoder.layers.{i}.final_layer_norm.weight''')) rename_keys.append((f'''transformer.decoder.layers.{i}.norm3.bias''', f'''decoder.layers.{i}.final_layer_norm.bias''')) # convolutional projection + query embeddings + layernorm of encoder + layernorm of decoder + class and bounding box heads rename_keys.extend( [ ("input_proj.weight", "input_projection.weight"), ("input_proj.bias", "input_projection.bias"), ("query_embed.weight", "query_position_embeddings.weight"), ("transformer.encoder.norm.weight", "encoder.layernorm.weight"), ("transformer.encoder.norm.bias", "encoder.layernorm.bias"), ("transformer.decoder.norm.weight", "decoder.layernorm.weight"), ("transformer.decoder.norm.bias", "decoder.layernorm.bias"), ("class_embed.weight", "class_labels_classifier.weight"), ("class_embed.bias", "class_labels_classifier.bias"), ("bbox_embed.layers.0.weight", "bbox_predictor.layers.0.weight"), ("bbox_embed.layers.0.bias", "bbox_predictor.layers.0.bias"), ("bbox_embed.layers.1.weight", "bbox_predictor.layers.1.weight"), ("bbox_embed.layers.1.bias", "bbox_predictor.layers.1.bias"), ("bbox_embed.layers.2.weight", "bbox_predictor.layers.2.weight"), ("bbox_embed.layers.2.bias", "bbox_predictor.layers.2.bias"), ] ) def __lowerCamelCase ( A__ , A__ , A__ ) -> Dict: """simple docstring""" UpperCamelCase = state_dict.pop(A__ ) UpperCamelCase = val def __lowerCamelCase ( A__ ) -> int: """simple docstring""" UpperCamelCase = OrderedDict() for key, value in state_dict.items(): if "backbone.0.body" in key: UpperCamelCase = key.replace('backbone.0.body' , 'backbone.conv_encoder.model' ) UpperCamelCase = value else: UpperCamelCase = value return new_state_dict def __lowerCamelCase ( A__ ) -> Dict: """simple docstring""" UpperCamelCase = '' # first: transformer encoder for i in range(6 ): # read in weights + bias of input projection layer (in PyTorch's MultiHeadAttention, this is a single matrix + bias) UpperCamelCase = state_dict.pop(F"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_weight""" ) UpperCamelCase = state_dict.pop(F"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict UpperCamelCase = in_proj_weight[:256, :] UpperCamelCase = in_proj_bias[:256] UpperCamelCase = in_proj_weight[256:512, :] UpperCamelCase = in_proj_bias[256:512] UpperCamelCase = in_proj_weight[-256:, :] UpperCamelCase = in_proj_bias[-256:] # next: transformer decoder (which is a bit more complex because it also includes cross-attention) for i in range(6 ): # read in weights + bias of input projection layer of self-attention UpperCamelCase = state_dict.pop(F"""{prefix}transformer.decoder.layers.{i}.self_attn.in_proj_weight""" ) UpperCamelCase = state_dict.pop(F"""{prefix}transformer.decoder.layers.{i}.self_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict UpperCamelCase = in_proj_weight[:256, :] UpperCamelCase = in_proj_bias[:256] UpperCamelCase = in_proj_weight[256:512, :] UpperCamelCase = in_proj_bias[256:512] UpperCamelCase = in_proj_weight[-256:, :] UpperCamelCase = in_proj_bias[-256:] # read in weights + bias of input projection layer of cross-attention UpperCamelCase = state_dict.pop( F"""{prefix}transformer.decoder.layers.{i}.multihead_attn.in_proj_weight""" ) UpperCamelCase = state_dict.pop(F"""{prefix}transformer.decoder.layers.{i}.multihead_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) of cross-attention to the state dict UpperCamelCase = in_proj_weight_cross_attn[:256, :] UpperCamelCase = in_proj_bias_cross_attn[:256] UpperCamelCase = in_proj_weight_cross_attn[256:512, :] UpperCamelCase = in_proj_bias_cross_attn[256:512] UpperCamelCase = in_proj_weight_cross_attn[-256:, :] UpperCamelCase = in_proj_bias_cross_attn[-256:] def __lowerCamelCase ( A__ , A__ ) -> Optional[int]: """simple docstring""" UpperCamelCase , UpperCamelCase = image.size UpperCamelCase = max(A__ , A__ ) UpperCamelCase = 800 if 'detection' in checkpoint_url else 1_000 UpperCamelCase = target_max_size / current_max_size UpperCamelCase = image.resize((int(round(scale * width ) ), int(round(scale * height ) )) ) return resized_image def __lowerCamelCase ( A__ ) -> List[Any]: """simple docstring""" UpperCamelCase = F.to_tensor(A__ ) UpperCamelCase = F.normalize(A__ , mean=[0.485, 0.456, 0.406] , std=[0.229, 0.224, 0.225] ) return image @torch.no_grad() def __lowerCamelCase ( A__ , A__ , A__ ) -> Optional[Any]: """simple docstring""" logger.info('Converting model...' ) # load original state dict UpperCamelCase = torch.hub.load_state_dict_from_url(A__ , map_location='cpu' ) # rename keys for src, dest in rename_keys: rename_key(A__ , A__ , A__ ) UpperCamelCase = rename_backbone_keys(A__ ) # query, key and value matrices need special treatment read_in_q_k_v(A__ ) # important: we need to prepend a prefix to each of the base model keys as the head models use different attributes for them UpperCamelCase = 'model.' for key in state_dict.copy().keys(): if not key.startswith('class_labels_classifier' ) and not key.startswith('bbox_predictor' ): UpperCamelCase = state_dict.pop(A__ ) UpperCamelCase = val # create HuggingFace model and load state dict UpperCamelCase = TableTransformerConfig( backbone='resnet18' , mask_loss_coefficient=1 , dice_loss_coefficient=1 , ce_loss_coefficient=1 , bbox_loss_coefficient=5 , giou_loss_coefficient=2 , eos_coefficient=0.4 , class_cost=1 , bbox_cost=5 , giou_cost=2 , ) if "detection" in checkpoint_url: UpperCamelCase = 15 UpperCamelCase = 2 UpperCamelCase = {0: 'table', 1: 'table rotated'} UpperCamelCase = idalabel UpperCamelCase = {v: k for k, v in idalabel.items()} else: UpperCamelCase = 125 UpperCamelCase = 6 UpperCamelCase = { 0: 'table', 1: 'table column', 2: 'table row', 3: 'table column header', 4: 'table projected row header', 5: 'table spanning cell', } UpperCamelCase = idalabel UpperCamelCase = {v: k for k, v in idalabel.items()} UpperCamelCase = DetrImageProcessor( format='coco_detection' , max_size=800 if 'detection' in checkpoint_url else 1_000 ) UpperCamelCase = TableTransformerForObjectDetection(A__ ) model.load_state_dict(A__ ) model.eval() # verify our conversion UpperCamelCase = 'example_pdf.png' if 'detection' in checkpoint_url else 'example_table.png' UpperCamelCase = hf_hub_download(repo_id='nielsr/example-pdf' , repo_type='dataset' , filename=A__ ) UpperCamelCase = Image.open(A__ ).convert('RGB' ) UpperCamelCase = normalize(resize(A__ , A__ ) ).unsqueeze(0 ) UpperCamelCase = model(A__ ) if "detection" in checkpoint_url: UpperCamelCase = (1, 15, 3) UpperCamelCase = torch.tensor( [[-6.7_897, -16.9_985, 6.7_937], [-8.0_186, -22.2_192, 6.9_677], [-7.3_117, -21.0_708, 7.4_055]] ) UpperCamelCase = torch.tensor([[0.4_867, 0.1_767, 0.6_732], [0.6_718, 0.4_479, 0.3_830], [0.4_716, 0.1_760, 0.6_364]] ) else: UpperCamelCase = (1, 125, 7) UpperCamelCase = torch.tensor( [[-18.1_430, -8.3_214, 4.8_274], [-18.4_685, -7.1_361, -4.2_667], [-26.3_693, -9.3_429, -4.9_962]] ) UpperCamelCase = torch.tensor([[0.4_983, 0.5_595, 0.9_440], [0.4_916, 0.6_315, 0.5_954], [0.6_108, 0.8_637, 0.1_135]] ) assert outputs.logits.shape == expected_shape assert torch.allclose(outputs.logits[0, :3, :3] , A__ , atol=1e-4 ) assert torch.allclose(outputs.pred_boxes[0, :3, :3] , A__ , atol=1e-4 ) print('Looks ok!' ) if pytorch_dump_folder_path is not None: # Save model and image processor logger.info(F"""Saving PyTorch model and image processor to {pytorch_dump_folder_path}...""" ) Path(A__ ).mkdir(exist_ok=A__ ) model.save_pretrained(A__ ) image_processor.save_pretrained(A__ ) if push_to_hub: # Push model to HF hub logger.info('Pushing model to the hub...' ) UpperCamelCase = ( 'microsoft/table-transformer-detection' if 'detection' in checkpoint_url else 'microsoft/table-transformer-structure-recognition' ) model.push_to_hub(A__ ) image_processor.push_to_hub(A__ ) if __name__ == "__main__": _lowerCamelCase : List[str] = argparse.ArgumentParser() parser.add_argument( "--checkpoint_url", default="https://pubtables1m.blob.core.windows.net/model/pubtables1m_detection_detr_r18.pth", type=str, choices=[ "https://pubtables1m.blob.core.windows.net/model/pubtables1m_detection_detr_r18.pth", "https://pubtables1m.blob.core.windows.net/model/pubtables1m_structure_detr_r18.pth", ], help="URL of the Table Transformer checkpoint you'd like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the folder to output PyTorch model." ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether or not to push the converted model to the 🤗 hub." ) _lowerCamelCase : int = parser.parse_args() convert_table_transformer_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub)
28
1
"""simple docstring""" import gc import random import tempfile import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, ControlNetModel, DDIMScheduler, StableDiffusionControlNetImgaImgPipeline, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet import MultiControlNetModel from diffusers.utils import floats_tensor, load_image, load_numpy, randn_tensor, slow, torch_device from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( IMAGE_TO_IMAGE_IMAGE_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import ( PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin, ) enable_full_determinism() class A ( __snake_case , __snake_case , __snake_case , unittest.TestCase ): '''simple docstring''' A__ = StableDiffusionControlNetImgaImgPipeline A__ = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""height""", """width"""} A__ = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS A__ = IMAGE_TO_IMAGE_IMAGE_PARAMS.union({'''control_image'''} ) A__ = IMAGE_TO_IMAGE_IMAGE_PARAMS def lowerCamelCase__ (self : Any ) -> List[Any]: """simple docstring""" torch.manual_seed(0 ) lowercase__ = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") , up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") , cross_attention_dim=32 , ) torch.manual_seed(0 ) lowercase__ = ControlNetModel( block_out_channels=(32, 64) , layers_per_block=2 , in_channels=4 , down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") , cross_attention_dim=32 , conditioning_embedding_out_channels=(16, 32) , ) torch.manual_seed(0 ) lowercase__ = DDIMScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule="""scaled_linear""" , clip_sample=UpperCamelCase__ , set_alpha_to_one=UpperCamelCase__ , ) torch.manual_seed(0 ) lowercase__ = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] , up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] , latent_channels=4 , ) torch.manual_seed(0 ) lowercase__ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) lowercase__ = CLIPTextModel(UpperCamelCase__ ) lowercase__ = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) lowercase__ = { "unet": unet, "controlnet": controlnet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, "safety_checker": None, "feature_extractor": None, } return components def lowerCamelCase__ (self : int , _UpperCAmelCase : Tuple , _UpperCAmelCase : Optional[int]=0 ) -> List[str]: """simple docstring""" if str(UpperCamelCase__ ).startswith("""mps""" ): lowercase__ = torch.manual_seed(UpperCamelCase__ ) else: lowercase__ = torch.Generator(device=UpperCamelCase__ ).manual_seed(UpperCamelCase__ ) lowercase__ = 2 lowercase__ = randn_tensor( (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor) , generator=UpperCamelCase__ , device=torch.device(UpperCamelCase__ ) , ) lowercase__ = floats_tensor(control_image.shape , rng=random.Random(UpperCamelCase__ ) ).to(UpperCamelCase__ ) lowercase__ = image.cpu().permute(0 , 2 , 3 , 1 )[0] lowercase__ = Image.fromarray(np.uinta(UpperCamelCase__ ) ).convert("""RGB""" ).resize((64, 64) ) lowercase__ = { "prompt": "A painting of a squirrel eating a burger", "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, "output_type": "numpy", "image": image, "control_image": control_image, } return inputs def lowerCamelCase__ (self : Optional[int] ) -> List[str]: """simple docstring""" return self._test_attention_slicing_forward_pass(expected_max_diff=2E-3 ) @unittest.skipIf( torch_device != """cuda""" or not is_xformers_available() , reason="""XFormers attention is only available with CUDA and `xformers` installed""" , ) def lowerCamelCase__ (self : Tuple ) -> Optional[int]: """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2E-3 ) def lowerCamelCase__ (self : List[Any] ) -> Dict: """simple docstring""" self._test_inference_batch_single_identical(expected_max_diff=2E-3 ) class A ( __snake_case , __snake_case , unittest.TestCase ): '''simple docstring''' A__ = StableDiffusionControlNetImgaImgPipeline A__ = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""height""", """width"""} A__ = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS A__ = frozenset([] ) # TO_DO: add image_params once refactored VaeImageProcessor.preprocess def lowerCamelCase__ (self : Optional[int] ) -> List[Any]: """simple docstring""" torch.manual_seed(0 ) lowercase__ = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") , up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") , cross_attention_dim=32 , ) torch.manual_seed(0 ) def init_weights(_UpperCAmelCase : str ): if isinstance(UpperCamelCase__ , torch.nn.Convad ): torch.nn.init.normal(m.weight ) m.bias.data.fill_(1.0 ) lowercase__ = ControlNetModel( block_out_channels=(32, 64) , layers_per_block=2 , in_channels=4 , down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") , cross_attention_dim=32 , conditioning_embedding_out_channels=(16, 32) , ) controlneta.controlnet_down_blocks.apply(UpperCamelCase__ ) torch.manual_seed(0 ) lowercase__ = ControlNetModel( block_out_channels=(32, 64) , layers_per_block=2 , in_channels=4 , down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") , cross_attention_dim=32 , conditioning_embedding_out_channels=(16, 32) , ) controlneta.controlnet_down_blocks.apply(UpperCamelCase__ ) torch.manual_seed(0 ) lowercase__ = DDIMScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule="""scaled_linear""" , clip_sample=UpperCamelCase__ , set_alpha_to_one=UpperCamelCase__ , ) torch.manual_seed(0 ) lowercase__ = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] , up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] , latent_channels=4 , ) torch.manual_seed(0 ) lowercase__ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) lowercase__ = CLIPTextModel(UpperCamelCase__ ) lowercase__ = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) lowercase__ = MultiControlNetModel([controlneta, controlneta] ) lowercase__ = { "unet": unet, "controlnet": controlnet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, "safety_checker": None, "feature_extractor": None, } return components def lowerCamelCase__ (self : Optional[int] , _UpperCAmelCase : Any , _UpperCAmelCase : int=0 ) -> Optional[int]: """simple docstring""" if str(UpperCamelCase__ ).startswith("""mps""" ): lowercase__ = torch.manual_seed(UpperCamelCase__ ) else: lowercase__ = torch.Generator(device=UpperCamelCase__ ).manual_seed(UpperCamelCase__ ) lowercase__ = 2 lowercase__ = [ randn_tensor( (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor) , generator=UpperCamelCase__ , device=torch.device(UpperCamelCase__ ) , ), randn_tensor( (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor) , generator=UpperCamelCase__ , device=torch.device(UpperCamelCase__ ) , ), ] lowercase__ = floats_tensor(control_image[0].shape , rng=random.Random(UpperCamelCase__ ) ).to(UpperCamelCase__ ) lowercase__ = image.cpu().permute(0 , 2 , 3 , 1 )[0] lowercase__ = Image.fromarray(np.uinta(UpperCamelCase__ ) ).convert("""RGB""" ).resize((64, 64) ) lowercase__ = { "prompt": "A painting of a squirrel eating a burger", "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, "output_type": "numpy", "image": image, "control_image": control_image, } return inputs def lowerCamelCase__ (self : int ) -> Optional[Any]: """simple docstring""" lowercase__ = self.get_dummy_components() lowercase__ = self.pipeline_class(**UpperCamelCase__ ) pipe.to(UpperCamelCase__ ) lowercase__ = 10.0 lowercase__ = 4 lowercase__ = self.get_dummy_inputs(UpperCamelCase__ ) lowercase__ = steps lowercase__ = scale lowercase__ = pipe(**UpperCamelCase__ )[0] lowercase__ = self.get_dummy_inputs(UpperCamelCase__ ) lowercase__ = steps lowercase__ = scale lowercase__ = pipe(**UpperCamelCase__ , control_guidance_start=0.1 , control_guidance_end=0.2 )[0] lowercase__ = self.get_dummy_inputs(UpperCamelCase__ ) lowercase__ = steps lowercase__ = scale lowercase__ = pipe(**UpperCamelCase__ , control_guidance_start=[0.1, 0.3] , control_guidance_end=[0.2, 0.7] )[0] lowercase__ = self.get_dummy_inputs(UpperCamelCase__ ) lowercase__ = steps lowercase__ = scale lowercase__ = pipe(**UpperCamelCase__ , control_guidance_start=0.4 , control_guidance_end=[0.5, 0.8] )[0] # make sure that all outputs are different assert np.sum(np.abs(output_a - output_a ) ) > 1E-3 assert np.sum(np.abs(output_a - output_a ) ) > 1E-3 assert np.sum(np.abs(output_a - output_a ) ) > 1E-3 def lowerCamelCase__ (self : str ) -> Any: """simple docstring""" return self._test_attention_slicing_forward_pass(expected_max_diff=2E-3 ) @unittest.skipIf( torch_device != """cuda""" or not is_xformers_available() , reason="""XFormers attention is only available with CUDA and `xformers` installed""" , ) def lowerCamelCase__ (self : List[str] ) -> List[str]: """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2E-3 ) def lowerCamelCase__ (self : Optional[int] ) -> Optional[Any]: """simple docstring""" self._test_inference_batch_single_identical(expected_max_diff=2E-3 ) def lowerCamelCase__ (self : Any ) -> List[Any]: """simple docstring""" lowercase__ = self.get_dummy_components() lowercase__ = self.pipeline_class(**UpperCamelCase__ ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmpdir: try: # save_pretrained is not implemented for Multi-ControlNet pipe.save_pretrained(UpperCamelCase__ ) except NotImplementedError: pass @slow @require_torch_gpu class A ( unittest.TestCase ): '''simple docstring''' def lowerCamelCase__ (self : int ) -> Union[str, Any]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def lowerCamelCase__ (self : List[Any] ) -> Optional[int]: """simple docstring""" lowercase__ = ControlNetModel.from_pretrained("""lllyasviel/sd-controlnet-canny""" ) lowercase__ = StableDiffusionControlNetImgaImgPipeline.from_pretrained( """runwayml/stable-diffusion-v1-5""" , safety_checker=UpperCamelCase__ , controlnet=UpperCamelCase__ ) pipe.enable_model_cpu_offload() pipe.set_progress_bar_config(disable=UpperCamelCase__ ) lowercase__ = torch.Generator(device="""cpu""" ).manual_seed(0 ) lowercase__ = "evil space-punk bird" lowercase__ = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png""" ).resize((512, 512) ) lowercase__ = load_image( """https://huggingface.co/lllyasviel/sd-controlnet-canny/resolve/main/images/bird.png""" ).resize((512, 512) ) lowercase__ = pipe( UpperCamelCase__ , UpperCamelCase__ , control_image=UpperCamelCase__ , generator=UpperCamelCase__ , output_type="""np""" , num_inference_steps=50 , strength=0.6 , ) lowercase__ = output.images[0] assert image.shape == (512, 512, 3) lowercase__ = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/img2img.npy""" ) assert np.abs(expected_image - image ).max() < 9E-2
370
import argparse import logging import os import time import timeit import datasets import numpy as np import pycuda.autoinit # noqa: F401 import pycuda.driver as cuda import tensorrt as trt import torch from absl import logging as absl_logging from accelerate import Accelerator from datasets import load_dataset, load_metric from torch.utils.data import DataLoader from utils_qa import postprocess_qa_predictions import transformers from transformers import AutoTokenizer, EvalPrediction, default_data_collator, set_seed from transformers.trainer_pt_utils import nested_concat, nested_truncate A : int = trt.Logger(trt.Logger.WARNING) A : Dict = absl_logging.get_absl_logger() absl_logger.setLevel(logging.WARNING) A : Union[str, Any] = logging.getLogger(__name__) A : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--onnx_model_path', default=None, type=str, required=True, help='Path to ONNX model: ', ) parser.add_argument( '--output_dir', default=None, type=str, required=True, help='The output directory where the model checkpoints and predictions will be written.', ) # Other parameters parser.add_argument( '--tokenizer_name', default='', type=str, required=True, help='Pretrained tokenizer name or path if not the same as model_name', ) parser.add_argument( '--version_2_with_negative', action='store_true', help='If true, the SQuAD examples contain some that do not have an answer.', ) parser.add_argument( '--null_score_diff_threshold', type=float, default=0.0, help='If null_score - best_non_null is greater than the threshold predict null.', ) parser.add_argument( '--max_seq_length', default=3_8_4, type=int, help=( 'The maximum total input sequence length after WordPiece tokenization. Sequences ' 'longer than this will be truncated, and sequences shorter than this will be padded.' ), ) parser.add_argument( '--doc_stride', default=1_2_8, type=int, help='When splitting up a long document into chunks, how much stride to take between chunks.', ) parser.add_argument('--per_device_eval_batch_size', default=8, type=int, help='Batch size per GPU/CPU for evaluation.') parser.add_argument( '--n_best_size', default=2_0, type=int, help='The total number of n-best predictions to generate in the nbest_predictions.json output file.', ) parser.add_argument( '--max_answer_length', default=3_0, type=int, help=( 'The maximum length of an answer that can be generated. This is needed because the start ' 'and end predictions are not conditioned on one another.' ), ) parser.add_argument('--seed', type=int, default=4_2, help='random seed for initialization') parser.add_argument( '--dataset_name', type=str, default=None, required=True, help='The name of the dataset to use (via the datasets library).', ) parser.add_argument( '--dataset_config_name', type=str, default=None, help='The configuration name of the dataset to use (via the datasets library).', ) parser.add_argument( '--preprocessing_num_workers', type=int, default=4, help='A csv or a json file containing the training data.' ) parser.add_argument('--overwrite_cache', action='store_true', help='Overwrite the cached training and evaluation sets') parser.add_argument( '--fp16', action='store_true', help='Whether to use 16-bit (mixed) precision instead of 32-bit', ) parser.add_argument( '--int8', action='store_true', help='Whether to use INT8', ) A : List[Any] = parser.parse_args() if args.tokenizer_name: A : Dict = AutoTokenizer.from_pretrained(args.tokenizer_name, use_fast=True) else: raise ValueError( 'You are instantiating a new tokenizer from scratch. This is not supported by this script.' 'You can do it from another script, save it, and load it from here, using --tokenizer_name.' ) logger.info('Training/evaluation parameters %s', args) A : Optional[Any] = args.per_device_eval_batch_size A : Tuple = (args.eval_batch_size, args.max_seq_length) # TRT Engine properties A : Any = True A : Optional[int] = 'temp_engine/bert-fp32.engine' if args.fpaa: A : Union[str, Any] = 'temp_engine/bert-fp16.engine' if args.inta: A : Optional[int] = 'temp_engine/bert-int8.engine' # import ONNX file if not os.path.exists('temp_engine'): os.makedirs('temp_engine') A : List[str] = 1 << (int)(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH) with trt.Builder(TRT_LOGGER) as builder, builder.create_network(EXPLICIT_BATCH) as network, trt.OnnxParser( network, TRT_LOGGER ) as parser: with open(args.onnx_model_path, 'rb') as model: if not parser.parse(model.read()): for error in range(parser.num_errors): print(parser.get_error(error)) # Query input names and shapes from parsed TensorRT network A : List[str] = [network.get_input(i) for i in range(network.num_inputs)] A : Any = [_input.name for _input in network_inputs] # ex: ["actual_input1"] with builder.create_builder_config() as config: A : Union[str, Any] = 1 << 5_0 if STRICT_TYPES: config.set_flag(trt.BuilderFlag.STRICT_TYPES) if args.fpaa: config.set_flag(trt.BuilderFlag.FPaa) if args.inta: config.set_flag(trt.BuilderFlag.INTa) A : Dict = builder.create_optimization_profile() config.add_optimization_profile(profile) for i in range(len(input_names)): profile.set_shape(input_names[i], INPUT_SHAPE, INPUT_SHAPE, INPUT_SHAPE) A : int = builder.build_engine(network, config) # serialize_engine and store in file (can be directly loaded and deserialized): with open(engine_name, 'wb') as f: f.write(engine.serialize()) def UpperCamelCase ( __magic_name__ : Optional[Any] , __magic_name__ : Tuple , __magic_name__ : int , __magic_name__ : Any , __magic_name__ : List[str] , __magic_name__ : Any , __magic_name__ : Union[str, Any] , __magic_name__ : Optional[Any] ) -> Optional[Any]: """simple docstring""" lowercase__ = np.asarray(inputs["""input_ids"""] , dtype=np.intaa ) lowercase__ = np.asarray(inputs["""attention_mask"""] , dtype=np.intaa ) lowercase__ = np.asarray(inputs["""token_type_ids"""] , dtype=np.intaa ) # Copy inputs cuda.memcpy_htod_async(d_inputs[0] , input_ids.ravel() , __magic_name__ ) cuda.memcpy_htod_async(d_inputs[1] , attention_mask.ravel() , __magic_name__ ) cuda.memcpy_htod_async(d_inputs[2] , token_type_ids.ravel() , __magic_name__ ) # start time lowercase__ = time.time() # Run inference context.execute_async( bindings=[int(__magic_name__ ) for d_inp in d_inputs] + [int(__magic_name__ ), int(__magic_name__ )] , stream_handle=stream.handle ) # Transfer predictions back from GPU cuda.memcpy_dtoh_async(__magic_name__ , __magic_name__ , __magic_name__ ) cuda.memcpy_dtoh_async(__magic_name__ , __magic_name__ , __magic_name__ ) # Synchronize the stream and take time stream.synchronize() # end time lowercase__ = time.time() lowercase__ = end_time - start_time lowercase__ = (h_outputa, h_outputa) # print(outputs) return outputs, infer_time # Initialize the accelerator. We will let the accelerator handle device placement for us in this example. A : Dict = Accelerator() # Make one log on every process with the configuration for debugging. logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s', datefmt='%m/%d/%Y %H:%M:%S', level=logging.INFO, ) # Setup logging, we only want one process per machine to log things on the screen. # accelerator.is_local_main_process is only True for one process per machine. logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() # If passed along, set the training seed now. if args.seed is not None: set_seed(args.seed) # Get the datasets: you can either provide your own CSV/JSON/TXT training and evaluation files (see below) # or just provide the name of one of the public datasets available on the hub at https://huggingface.co/datasets/ # (the dataset will be downloaded automatically from the datasets Hub). # # For CSV/JSON files, this script will use the column called 'text' or the first column if no column called # 'text' is found. You can easily tweak this behavior (see below). if args.dataset_name is not None: # Downloading and loading a dataset from the hub. A : str = load_dataset(args.dataset_name, args.dataset_config_name) else: raise ValueError('Evaluation requires a dataset name') # See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at # https://huggingface.co/docs/datasets/loading_datasets.html. # Preprocessing the datasets. # Preprocessing is slighlty different for training and evaluation. A : str = raw_datasets['validation'].column_names A : Any = 'question' if 'question' in column_names else column_names[0] A : int = 'context' if 'context' in column_names else column_names[1] A : Tuple = 'answers' if 'answers' in column_names else column_names[2] # Padding side determines if we do (question|context) or (context|question). A : Dict = tokenizer.padding_side == 'right' if args.max_seq_length > tokenizer.model_max_length: logger.warning( F'The max_seq_length passed ({args.max_seq_length}) is larger than the maximum length for the' F'model ({tokenizer.model_max_length}). Using max_seq_length={tokenizer.model_max_length}.' ) A : str = min(args.max_seq_length, tokenizer.model_max_length) def UpperCamelCase ( __magic_name__ : List[Any] ) -> Optional[Any]: """simple docstring""" lowercase__ = [q.lstrip() for q in examples[question_column_name]] # Tokenize our examples with truncation and maybe padding, but keep the overflows using a stride. This results # in one example possible giving several features when a context is long, each of those features having a # context that overlaps a bit the context of the previous feature. lowercase__ = tokenizer( examples[question_column_name if pad_on_right else context_column_name] , examples[context_column_name if pad_on_right else question_column_name] , truncation="""only_second""" if pad_on_right else """only_first""" , max_length=__magic_name__ , stride=args.doc_stride , return_overflowing_tokens=__magic_name__ , return_offsets_mapping=__magic_name__ , padding="""max_length""" , ) # Since one example might give us several features if it has a long context, we need a map from a feature to # its corresponding example. This key gives us just that. lowercase__ = tokenized_examples.pop("""overflow_to_sample_mapping""" ) # For evaluation, we will need to convert our predictions to substrings of the context, so we keep the # corresponding example_id and we will store the offset mappings. lowercase__ = [] for i in range(len(tokenized_examples["""input_ids"""] ) ): # Grab the sequence corresponding to that example (to know what is the context and what is the question). lowercase__ = tokenized_examples.sequence_ids(__magic_name__ ) lowercase__ = 1 if pad_on_right else 0 # One example can give several spans, this is the index of the example containing this span of text. lowercase__ = sample_mapping[i] tokenized_examples["example_id"].append(examples["""id"""][sample_index] ) # Set to None the offset_mapping that are not part of the context so it's easy to determine if a token # position is part of the context or not. lowercase__ = [ (o if sequence_ids[k] == context_index else None) for k, o in enumerate(tokenized_examples["""offset_mapping"""][i] ) ] return tokenized_examples A : Optional[Any] = raw_datasets['validation'] # Validation Feature Creation A : int = eval_examples.map( prepare_validation_features, batched=True, num_proc=args.preprocessing_num_workers, remove_columns=column_names, load_from_cache_file=not args.overwrite_cache, desc='Running tokenizer on validation dataset', ) A : Dict = default_data_collator A : Union[str, Any] = eval_dataset.remove_columns(['example_id', 'offset_mapping']) A : Optional[Any] = DataLoader( eval_dataset_for_model, collate_fn=data_collator, batch_size=args.per_device_eval_batch_size ) def UpperCamelCase ( __magic_name__ : str , __magic_name__ : str , __magic_name__ : Any , __magic_name__ : List[Any]="eval" ) -> List[Any]: """simple docstring""" lowercase__ = postprocess_qa_predictions( examples=__magic_name__ , features=__magic_name__ , predictions=__magic_name__ , version_2_with_negative=args.version_2_with_negative , n_best_size=args.n_best_size , max_answer_length=args.max_answer_length , null_score_diff_threshold=args.null_score_diff_threshold , output_dir=args.output_dir , prefix=__magic_name__ , ) # Format the result to the format the metric expects. if args.version_2_with_negative: lowercase__ = [ {"""id""": k, """prediction_text""": v, """no_answer_probability""": 0.0} for k, v in predictions.items() ] else: lowercase__ = [{"""id""": k, """prediction_text""": v} for k, v in predictions.items()] lowercase__ = [{"""id""": ex["""id"""], """answers""": ex[answer_column_name]} for ex in examples] return EvalPrediction(predictions=__magic_name__ , label_ids=__magic_name__ ) A : Union[str, Any] = load_metric('squad_v2' if args.version_2_with_negative else 'squad') # Evaluation! logger.info('Loading ONNX model %s for evaluation', args.onnx_model_path) with open(engine_name, 'rb') as f, trt.Runtime(TRT_LOGGER) as runtime, runtime.deserialize_cuda_engine( f.read() ) as engine, engine.create_execution_context() as context: # setup for TRT inferrence for i in range(len(input_names)): context.set_binding_shape(i, INPUT_SHAPE) assert context.all_binding_shapes_specified def UpperCamelCase ( __magic_name__ : Any ) -> Union[str, Any]: """simple docstring""" return trt.volume(engine.get_binding_shape(__magic_name__ ) ) * engine.get_binding_dtype(__magic_name__ ).itemsize # Allocate device memory for inputs and outputs. A : Union[str, Any] = [cuda.mem_alloc(binding_nbytes(binding)) for binding in engine if engine.binding_is_input(binding)] # Allocate output buffer A : Optional[int] = cuda.pagelocked_empty(tuple(context.get_binding_shape(3)), dtype=np.floataa) A : Dict = cuda.pagelocked_empty(tuple(context.get_binding_shape(4)), dtype=np.floataa) A : List[str] = cuda.mem_alloc(h_outputa.nbytes) A : Optional[Any] = cuda.mem_alloc(h_outputa.nbytes) # Create a stream in which to copy inputs/outputs and run inference. A : Union[str, Any] = cuda.Stream() # Evaluation logger.info('***** Running Evaluation *****') logger.info(F' Num examples = {len(eval_dataset)}') logger.info(F' Batch size = {args.per_device_eval_batch_size}') A : List[Any] = 0.0 A : Any = 0 A : str = timeit.default_timer() A : Tuple = None for step, batch in enumerate(eval_dataloader): A , A : Optional[int] = model_infer(batch, context, d_inputs, h_outputa, h_outputa, d_outputa, d_outputa, stream) total_time += infer_time niter += 1 A , A : int = outputs A : str = torch.tensor(start_logits) A : int = torch.tensor(end_logits) # necessary to pad predictions and labels for being gathered A : List[Any] = accelerator.pad_across_processes(start_logits, dim=1, pad_index=-1_0_0) A : Tuple = accelerator.pad_across_processes(end_logits, dim=1, pad_index=-1_0_0) A : Any = (accelerator.gather(start_logits).cpu().numpy(), accelerator.gather(end_logits).cpu().numpy()) A : str = logits if all_preds is None else nested_concat(all_preds, logits, padding_index=-1_0_0) if all_preds is not None: A : List[str] = nested_truncate(all_preds, len(eval_dataset)) A : List[Any] = timeit.default_timer() - start_time logger.info(' Evaluation done in total %f secs (%f sec per example)', evalTime, evalTime / len(eval_dataset)) # Inference time from TRT logger.info('Average Inference Time = {:.3f} ms'.format(total_time * 1_0_0_0 / niter)) logger.info('Total Inference Time = {:.3f} ms'.format(total_time * 1_0_0_0)) logger.info('Total Number of Inference = %d', niter) A : Dict = post_processing_function(eval_examples, eval_dataset, all_preds) A : Any = metric.compute(predictions=prediction.predictions, references=prediction.label_ids) logger.info(F'Evaluation metrics: {eval_metric}')
146
0
"""simple docstring""" from transformers import HfArgumentParser, TensorFlowBenchmark, TensorFlowBenchmarkArguments def UpperCAmelCase ( ) -> int: snake_case_ = HfArgumentParser(UpperCAmelCase ) snake_case_ = parser.parse_args_into_dataclasses()[0] snake_case_ = TensorFlowBenchmark(args=UpperCAmelCase ) try: snake_case_ = parser.parse_args_into_dataclasses()[0] except ValueError as e: snake_case_ = 'Arg --no_{0} is no longer used, please use --no-{0} instead.' snake_case_ = ' '.join(str(UpperCAmelCase ).split(' ' )[:-1] ) snake_case_ = '' snake_case_ = eval(str(UpperCAmelCase ).split(' ' )[-1] ) snake_case_ = [] for arg in depreciated_args: # arg[2:] removes '--' if arg[2:] in TensorFlowBenchmark.deprecated_args: # arg[5:] removes '--no_' full_error_msg += arg_error_msg.format(arg[5:] ) else: wrong_args.append(UpperCAmelCase ) if len(UpperCAmelCase ) > 0: snake_case_ = full_error_msg + begin_error_msg + str(UpperCAmelCase ) raise ValueError(UpperCAmelCase ) benchmark.run() if __name__ == "__main__": main()
69
import argparse import gc import json import os import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils.deepspeed import DummyOptim, DummyScheduler __a : str = 1_6 __a : str = 3_2 def UpperCAmelCase ( lowercase ): """simple docstring""" return int(x / 2**20 ) class _UpperCamelCase : """simple docstring""" def __enter__( self ) -> str: '''simple docstring''' gc.collect() torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() # reset the peak gauge to zero __lowercase = torch.cuda.memory_allocated() return self def __exit__( self , *lowerCAmelCase__ ) -> int: '''simple docstring''' gc.collect() torch.cuda.empty_cache() __lowercase = torch.cuda.memory_allocated() __lowercase = torch.cuda.max_memory_allocated() __lowercase = bamb(self.end - self.begin ) __lowercase = bamb(self.peak - self.begin ) # print(f"delta used/peak {self.used:4d}/{self.peaked:4d}") def UpperCAmelCase ( lowercase , lowercase = 16 , lowercase = "bert-base-cased" , lowercase = 320 , lowercase = 160 , ): """simple docstring""" __lowercase = AutoTokenizer.from_pretrained(lowercase ) __lowercase = load_dataset( '''glue''' , '''mrpc''' , split={'''train''': F"train[:{n_train}]", '''validation''': F"validation[:{n_val}]"} ) def tokenize_function(lowercase ): # max_length=None => use the model max length (it's actually the default) __lowercase = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=lowercase , max_length=lowercase ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset __lowercase = datasets.map( lowercase , batched=lowercase , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , load_from_cache_file=lowercase ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library __lowercase = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(lowercase ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(lowercase , padding='''max_length''' , max_length=128 , return_tensors='''pt''' ) return tokenizer.pad(lowercase , padding='''longest''' , return_tensors='''pt''' ) # Instantiate dataloaders. __lowercase = DataLoader( tokenized_datasets['''train'''] , shuffle=lowercase , collate_fn=lowercase , batch_size=lowercase ) __lowercase = DataLoader( tokenized_datasets['''validation'''] , shuffle=lowercase , collate_fn=lowercase , batch_size=lowercase ) return train_dataloader, eval_dataloader def UpperCAmelCase ( lowercase , lowercase ): """simple docstring""" __lowercase = Accelerator() # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs __lowercase = config['''lr'''] __lowercase = int(config['''num_epochs'''] ) __lowercase = int(config['''seed'''] ) __lowercase = int(config['''batch_size'''] ) __lowercase = args.model_name_or_path set_seed(lowercase ) __lowercase , __lowercase = get_dataloaders(lowercase , lowercase , lowercase , args.n_train , args.n_val ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) __lowercase = AutoModelForSequenceClassification.from_pretrained(lowercase , return_dict=lowercase ) # Instantiate optimizer __lowercase = ( AdamW if accelerator.state.deepspeed_plugin is None or '''optimizer''' not in accelerator.state.deepspeed_plugin.deepspeed_config else DummyOptim ) __lowercase = optimizer_cls(params=model.parameters() , lr=lowercase ) if accelerator.state.deepspeed_plugin is not None: __lowercase = accelerator.state.deepspeed_plugin.deepspeed_config[ '''gradient_accumulation_steps''' ] else: __lowercase = 1 __lowercase = (len(lowercase ) * num_epochs) // gradient_accumulation_steps # Instantiate scheduler if ( accelerator.state.deepspeed_plugin is None or "scheduler" not in accelerator.state.deepspeed_plugin.deepspeed_config ): __lowercase = get_linear_schedule_with_warmup( optimizer=lowercase , num_warmup_steps=0 , num_training_steps=lowercase , ) else: __lowercase = DummyScheduler(lowercase , total_num_steps=lowercase , warmup_num_steps=0 ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. __lowercase , __lowercase , __lowercase , __lowercase , __lowercase = accelerator.prepare( lowercase , lowercase , lowercase , lowercase , lowercase ) # We need to keep track of how many total steps we have iterated over __lowercase = 0 # We also need to keep track of the stating epoch so files are named properly __lowercase = 0 # Now we train the model __lowercase = {} for epoch in range(lowercase , lowercase ): with TorchTracemalloc() as tracemalloc: model.train() for step, batch in enumerate(lowercase ): __lowercase = model(**lowercase ) __lowercase = outputs.loss __lowercase = loss / gradient_accumulation_steps accelerator.backward(lowercase ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 # Printing the GPU memory usage details such as allocated memory, peak memory, and total memory usage accelerator.print('''Memory before entering the train : {}'''.format(bamb(tracemalloc.begin ) ) ) accelerator.print('''Memory consumed at the end of the train (end-begin): {}'''.format(tracemalloc.used ) ) accelerator.print('''Peak Memory consumed during the train (max-begin): {}'''.format(tracemalloc.peaked ) ) accelerator.print( '''Total Peak Memory consumed during the train (max): {}'''.format( tracemalloc.peaked + bamb(tracemalloc.begin ) ) ) __lowercase = tracemalloc.peaked + bamb(tracemalloc.begin ) if args.peak_memory_upper_bound is not None: assert ( train_total_peak_memory[F"epoch-{epoch}"] <= args.peak_memory_upper_bound ), "Peak memory usage exceeded the upper bound" accelerator.wait_for_everyone() if accelerator.is_main_process: with open(os.path.join(args.output_dir , '''peak_memory_utilization.json''' ) , '''w''' ) as f: json.dump(lowercase , lowercase ) def UpperCAmelCase ( ): """simple docstring""" __lowercase = argparse.ArgumentParser(description='''Simple example of training script tracking peak GPU memory usage.''' ) parser.add_argument( '''--model_name_or_path''' , type=lowercase , default='''bert-base-cased''' , help='''Path to pretrained model or model identifier from huggingface.co/models.''' , required=lowercase , ) parser.add_argument( '''--output_dir''' , type=lowercase , default='''.''' , help='''Optional save directory where all checkpoint folders will be stored. Default is the current working directory.''' , ) parser.add_argument( '''--peak_memory_upper_bound''' , type=lowercase , default=lowercase , help='''The upper bound of peak memory usage in MB. If set, the training will throw an error if the peak memory usage exceeds this value.''' , ) parser.add_argument( '''--n_train''' , type=lowercase , default=320 , help='''Number of training examples to use.''' , ) parser.add_argument( '''--n_val''' , type=lowercase , default=160 , help='''Number of validation examples to use.''' , ) parser.add_argument( '''--num_epochs''' , type=lowercase , default=1 , help='''Number of train epochs.''' , ) __lowercase = parser.parse_args() __lowercase = {'''lr''': 2E-5, '''num_epochs''': args.num_epochs, '''seed''': 42, '''batch_size''': 16} training_function(lowercase , lowercase ) if __name__ == "__main__": main()
210
0
from math import sqrt import numpy as np from sympy import symbols # Coefficient # Speed of light (m/s) _lowerCamelCase : Union[str, Any] = 2_9_9_7_9_2_4_5_8 # Symbols _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase : List[str] = symbols("ct x y z") def _UpperCAmelCase (UpperCamelCase_ : float ): '''simple docstring''' if velocity > c: raise ValueError("""Speed must not exceed light speed 299,792,458 [m/s]!""" ) elif velocity < 1: # Usually the speed should be much higher than 1 (c order of magnitude) raise ValueError("""Speed must be greater than or equal to 1!""" ) return velocity / c def _UpperCAmelCase (UpperCamelCase_ : float ): '''simple docstring''' return 1 / sqrt(1 - beta(UpperCamelCase_ ) ** 2 ) def _UpperCAmelCase (UpperCamelCase_ : float ): '''simple docstring''' return np.array( [ [gamma(UpperCamelCase_ ), -gamma(UpperCamelCase_ ) * beta(UpperCamelCase_ ), 0, 0], [-gamma(UpperCamelCase_ ) * beta(UpperCamelCase_ ), gamma(UpperCamelCase_ ), 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], ] ) def _UpperCAmelCase (UpperCamelCase_ : float , UpperCamelCase_ : np.ndarray | None = None ): '''simple docstring''' # Ensure event is not empty if event is None: _lowerCAmelCase : Union[str, Any] = np.array([ct, x, y, z] ) # Symbolic four vector else: event[0] *= c # x0 is ct (speed of light * time) return transformation_matrix(UpperCamelCase_ ) @ event if __name__ == "__main__": import doctest doctest.testmod() # Example of symbolic vector: _lowerCamelCase : List[str] = transform(2_9_9_7_9_2_4_5) print("Example of four vector: ") print(F'''ct\' = {four_vector[0]}''') print(F'''x\' = {four_vector[1]}''') print(F'''y\' = {four_vector[2]}''') print(F'''z\' = {four_vector[3]}''') # Substitute symbols with numerical values _lowerCamelCase : Any = {ct: c, x: 1, y: 1, z: 1} _lowerCamelCase : Union[str, Any] = [four_vector[i].subs(sub_dict) for i in range(4)] print(F'''\n{numerical_vector}''')
159
import warnings from ...utils import logging from .image_processing_glpn import GLPNImageProcessor _lowerCamelCase : List[str] = logging.get_logger(__name__) class __snake_case (_a ): def __init__( self : Optional[Any] , *_UpperCAmelCase : str , **_UpperCAmelCase : Any ) -> None: '''simple docstring''' warnings.warn( """The class GLPNFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please""" """ use GLPNImageProcessor instead.""" , _UpperCAmelCase , ) super().__init__(*_UpperCAmelCase , **_UpperCAmelCase )
159
1
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 lowercase__ : Any = getLogger(__name__) lowercase__ : List[str] = "cuda" if torch.cuda.is_available() else "cpu" def A_ ( snake_case : List[str] , snake_case : str , snake_case : str , snake_case : int = 8 , snake_case : str = DEFAULT_DEVICE , snake_case : List[str]=False , snake_case : Union[str, Any]="summarization" , snake_case : str=None , **snake_case : List[Any] , ) -> Dict: '''simple docstring''' __UpperCamelCase = Path(snake_case ).open('''w''' , encoding='''utf-8''' ) __UpperCamelCase = str(snake_case ) __UpperCamelCase = AutoModelForSeqaSeqLM.from_pretrained(snake_case ).to(snake_case ) if fpaa: __UpperCamelCase = model.half() __UpperCamelCase = AutoTokenizer.from_pretrained(snake_case ) logger.info(f"Inferred tokenizer type: {tokenizer.__class__}" ) # if this is wrong, check config.model_type. __UpperCamelCase = time.time() # update config with task specific params use_task_specific_params(snake_case , snake_case ) if prefix is None: __UpperCamelCase = prefix or getattr(model.config , '''prefix''' , '''''' ) or '''''' for examples_chunk in tqdm(list(chunks(snake_case , snake_case ) ) ): __UpperCamelCase = [prefix + text for text in examples_chunk] __UpperCamelCase = tokenizer(snake_case , return_tensors='''pt''' , truncation=snake_case , padding='''longest''' ).to(snake_case ) __UpperCamelCase = model.generate( input_ids=batch.input_ids , attention_mask=batch.attention_mask , **snake_case , ) __UpperCamelCase = tokenizer.batch_decode(snake_case , skip_special_tokens=snake_case , clean_up_tokenization_spaces=snake_case ) for hypothesis in dec: fout.write(hypothesis + '''\n''' ) fout.flush() fout.close() __UpperCamelCase = int(time.time() - start_time ) # seconds __UpperCamelCase = len(snake_case ) return {"n_obs": n_obs, "runtime": runtime, "seconds_per_sample": round(runtime / n_obs , 4 )} def A_ ( ) -> Tuple: '''simple docstring''' return datetime.datetime.now().strftime('''%Y-%m-%d %H:%M:%S''' ) def A_ ( snake_case : str=True ) -> int: '''simple docstring''' __UpperCamelCase = argparse.ArgumentParser() parser.add_argument('''model_name''' , type=snake_case , help='''like facebook/bart-large-cnn,t5-base, etc.''' ) parser.add_argument('''input_path''' , type=snake_case , help='''like cnn_dm/test.source''' ) parser.add_argument('''save_path''' , type=snake_case , help='''where to save summaries''' ) parser.add_argument('''--reference_path''' , type=snake_case , required=snake_case , help='''like cnn_dm/test.target''' ) parser.add_argument('''--score_path''' , type=snake_case , required=snake_case , default='''metrics.json''' , help='''where to save metrics''' ) parser.add_argument('''--device''' , type=snake_case , required=snake_case , default=snake_case , help='''cuda, cuda:1, cpu etc.''' ) parser.add_argument( '''--prefix''' , type=snake_case , required=snake_case , default=snake_case , help='''will be added to the begininng of src examples''' ) parser.add_argument('''--task''' , type=snake_case , default='''summarization''' , help='''used for task_specific_params + metrics''' ) parser.add_argument('''--bs''' , type=snake_case , default=8 , required=snake_case , help='''batch size''' ) parser.add_argument( '''--n_obs''' , type=snake_case , default=-1 , required=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=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 = parser.parse_known_args() __UpperCamelCase = parse_numeric_n_bool_cl_kwargs(snake_case ) if parsed_args and verbose: print(f"parsed the following generate kwargs: {parsed_args}" ) __UpperCamelCase = [''' ''' + 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 = examples[: args.n_obs] Path(args.save_path ).parent.mkdir(exist_ok=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 = generate_summaries_or_translations( snake_case , args.save_path , args.model_name , batch_size=args.bs , device=args.device , fpaa=args.fpaa , task=args.task , prefix=args.prefix , **snake_case , ) if args.reference_path is None: return {} # Compute scores __UpperCamelCase = calculate_bleu if '''translation''' in args.task else calculate_rouge __UpperCamelCase = [x.rstrip() for x in open(args.save_path ).readlines()] __UpperCamelCase = [x.rstrip() for x in open(args.reference_path ).readlines()][: len(snake_case )] __UpperCamelCase = score_fn(snake_case , snake_case ) scores.update(snake_case ) if args.dump_args: scores.update(snake_case ) if args.info: __UpperCamelCase = args.info if verbose: print(snake_case ) if args.score_path is not None: json.dump(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)
328
from __future__ import annotations from collections.abc import Callable def A_ ( snake_case : Callable[[int | float], int | float] , snake_case : int | float , snake_case : int | float , snake_case : int = 100 , ) -> float: '''simple docstring''' __UpperCamelCase = x_start __UpperCamelCase = fnc(snake_case ) __UpperCamelCase = 0.0 for _ in range(snake_case ): # Approximates small segments of curve as linear and solve # for trapezoidal area __UpperCamelCase = (x_end - x_start) / steps + xa __UpperCamelCase = fnc(snake_case ) area += abs(fxa + fxa ) * (xa - xa) / 2 # Increment step __UpperCamelCase = xa __UpperCamelCase = fxa return area if __name__ == "__main__": def A_ ( snake_case : Tuple ) -> Optional[Any]: '''simple docstring''' return x**3 + x**2 print("f(x) = x^3 + x^2") print("The area between the curve, x = -5, x = 5 and the x axis is:") lowercase__ : List[str] = 1_0 while i <= 1_0_0_0_0_0: print(F"with {i} steps: {trapezoidal_area(f, -5, 5, i)}") i *= 1_0
328
1
from ...utils import is_note_seq_available, is_transformers_available, is_torch_available from ...utils import OptionalDependencyNotAvailable try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .notes_encoder import SpectrogramNotesEncoder from .continous_encoder import SpectrogramContEncoder from .pipeline_spectrogram_diffusion import ( SpectrogramContEncoder, SpectrogramDiffusionPipeline, TaFilmDecoder, ) try: if not (is_transformers_available() and is_torch_available() and is_note_seq_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_transformers_and_torch_and_note_seq_objects import * # noqa F403 else: from .midi_utils import MidiProcessor
33
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) A : Optional[int] = { 'configuration_roberta': ['ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP', 'RobertaConfig', 'RobertaOnnxConfig'], 'tokenization_roberta': ['RobertaTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : List[str] = ['RobertaTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : str = [ 'ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'RobertaForCausalLM', 'RobertaForMaskedLM', 'RobertaForMultipleChoice', 'RobertaForQuestionAnswering', 'RobertaForSequenceClassification', 'RobertaForTokenClassification', 'RobertaModel', 'RobertaPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : List[Any] = [ 'TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFRobertaForCausalLM', 'TFRobertaForMaskedLM', 'TFRobertaForMultipleChoice', 'TFRobertaForQuestionAnswering', 'TFRobertaForSequenceClassification', 'TFRobertaForTokenClassification', 'TFRobertaMainLayer', 'TFRobertaModel', 'TFRobertaPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Union[str, Any] = [ 'FlaxRobertaForCausalLM', 'FlaxRobertaForMaskedLM', 'FlaxRobertaForMultipleChoice', 'FlaxRobertaForQuestionAnswering', 'FlaxRobertaForSequenceClassification', 'FlaxRobertaForTokenClassification', 'FlaxRobertaModel', 'FlaxRobertaPreTrainedModel', ] if TYPE_CHECKING: from .configuration_roberta import ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, RobertaConfig, RobertaOnnxConfig from .tokenization_roberta import RobertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roberta_fast import RobertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roberta import ( ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, RobertaForCausalLM, RobertaForMaskedLM, RobertaForMultipleChoice, RobertaForQuestionAnswering, RobertaForSequenceClassification, RobertaForTokenClassification, RobertaModel, RobertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roberta import ( TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFRobertaForCausalLM, TFRobertaForMaskedLM, TFRobertaForMultipleChoice, TFRobertaForQuestionAnswering, TFRobertaForSequenceClassification, TFRobertaForTokenClassification, TFRobertaMainLayer, TFRobertaModel, TFRobertaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, FlaxRobertaPreTrainedModel, ) else: import sys A : Dict = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
33
1
"""simple docstring""" import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING lowerCamelCase_ : List[Any] = logging.get_logger(__name__) lowerCamelCase_ : Dict = { """SenseTime/deformable-detr""": """https://huggingface.co/sensetime/deformable-detr/resolve/main/config.json""", # See all Deformable DETR models at https://huggingface.co/models?filter=deformable-detr } class __A ( _SCREAMING_SNAKE_CASE ): """simple docstring""" __lowerCAmelCase = "deformable_detr" __lowerCAmelCase = { "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", } def __init__( self , __A=True , __A=None , __A=3 , __A=300 , __A=1024 , __A=6 , __A=1024 , __A=8 , __A=6 , __A=1024 , __A=8 , __A=0.0 , __A=True , __A="relu" , __A=256 , __A=0.1 , __A=0.0 , __A=0.0 , __A=0.02 , __A=1.0 , __A=True , __A=False , __A="sine" , __A="resnet50" , __A=True , __A=False , __A=4 , __A=4 , __A=4 , __A=False , __A=300 , __A=False , __A=1 , __A=5 , __A=2 , __A=1 , __A=1 , __A=5 , __A=2 , __A=0.1 , __A=0.25 , __A=False , **__A , ) -> Tuple: if backbone_config is not None and use_timm_backbone: raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.''' ) if not use_timm_backbone: if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) a =CONFIG_MAPPING['''resnet'''](out_features=['''stage4'''] ) elif isinstance(__A , __A ): a =backbone_config.get('''model_type''' ) a =CONFIG_MAPPING[backbone_model_type] a =config_class.from_dict(__A ) a =use_timm_backbone a =backbone_config a =num_channels a =num_queries a =max_position_embeddings a =d_model a =encoder_ffn_dim a =encoder_layers a =encoder_attention_heads a =decoder_ffn_dim a =decoder_layers a =decoder_attention_heads a =dropout a =attention_dropout a =activation_dropout a =activation_function a =init_std a =init_xavier_std a =encoder_layerdrop a =auxiliary_loss a =position_embedding_type a =backbone a =use_pretrained_backbone a =dilation # deformable attributes a =num_feature_levels a =encoder_n_points a =decoder_n_points a =two_stage a =two_stage_num_proposals a =with_box_refine if two_stage is True and with_box_refine is False: raise ValueError('''If two_stage is True, with_box_refine must be True.''' ) # Hungarian matcher a =class_cost a =bbox_cost a =giou_cost # Loss coefficients a =mask_loss_coefficient a =dice_loss_coefficient a =bbox_loss_coefficient a =giou_loss_coefficient a =eos_coefficient a =focal_alpha a =disable_custom_kernels super().__init__(is_encoder_decoder=__A , **__A ) @property def SCREAMING_SNAKE_CASE ( self ) -> int: return self.encoder_attention_heads @property def SCREAMING_SNAKE_CASE ( self ) -> int: return self.d_model def SCREAMING_SNAKE_CASE ( self ) -> str: a =copy.deepcopy(self.__dict__ ) if self.backbone_config is not None: a =self.backbone_config.to_dict() a =self.__class__.model_type return output
81
"""simple docstring""" import importlib import sys from argparse import REMAINDER, ArgumentParser from pathlib import Path import torch_xla.distributed.xla_multiprocessing as xmp def _A ( ): """simple docstring""" a =ArgumentParser( description=( '''PyTorch TPU distributed training launch ''' '''helper utility that will spawn up ''' '''multiple distributed processes''' ) ) # Optional arguments for the launch helper parser.add_argument('''--num_cores''' , type=lowercase , default=1 , help='''Number of TPU cores to use (1 or 8).''' ) # positional parser.add_argument( '''training_script''' , type=lowercase , help=( '''The full path to the single TPU training ''' '''program/script to be launched in parallel, ''' '''followed by all the arguments for the ''' '''training script''' ) , ) # rest from the training program parser.add_argument('''training_script_args''' , nargs=lowercase ) return parser.parse_args() def _A ( ): """simple docstring""" a =parse_args() # Import training_script as a module. a =Path(args.training_script ) sys.path.append(str(script_fpath.parent.resolve() ) ) a =script_fpath.stem a =importlib.import_module(lowercase ) # Patch sys.argv a =[args.training_script] + args.training_script_args + ['''--tpu_num_cores''', str(args.num_cores )] xmp.spawn(mod._mp_fn , args=() , nprocs=args.num_cores ) if __name__ == "__main__": main()
81
1
'''simple docstring''' from __future__ import annotations import unittest from transformers import is_tf_available, is_torch_available from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, is_pt_tf_cross_test, slow if is_tf_available(): from transformers import ( AutoConfig, BertConfig, GPTaConfig, TaConfig, TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSeqaSeqLM, TFAutoModelForSequenceClassification, TFAutoModelWithLMHead, TFBertForMaskedLM, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertModel, TFGPTaLMHeadModel, TFRobertaForMaskedLM, TFTaForConditionalGeneration, ) from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST if is_torch_available(): from transformers import ( AutoModel, AutoModelForCausalLM, AutoModelForMaskedLM, AutoModelForPreTraining, AutoModelForQuestionAnswering, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoModelWithLMHead, BertForMaskedLM, BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification, BertModel, GPTaLMHeadModel, RobertaForMaskedLM, TaForConditionalGeneration, ) @is_pt_tf_cross_test class __UpperCamelCase ( unittest.TestCase ): @slow def __UpperCAmelCase ( self ): '''simple docstring''' for model_name in ["bert-base-uncased"]: __a : Dict = AutoConfig.from_pretrained(__a ) self.assertIsNotNone(__a ) self.assertIsInstance(__a , __a ) __a : Tuple = TFAutoModel.from_pretrained(__a , from_pt=__a ) self.assertIsNotNone(__a ) self.assertIsInstance(__a , __a ) __a : Any = AutoModel.from_pretrained(__a , from_tf=__a ) self.assertIsNotNone(__a ) self.assertIsInstance(__a , __a ) @slow def __UpperCAmelCase ( self ): '''simple docstring''' for model_name in ["bert-base-uncased"]: __a : List[Any] = AutoConfig.from_pretrained(__a ) self.assertIsNotNone(__a ) self.assertIsInstance(__a , __a ) __a : Dict = TFAutoModelForPreTraining.from_pretrained(__a , from_pt=__a ) self.assertIsNotNone(__a ) self.assertIsInstance(__a , __a ) __a : str = AutoModelForPreTraining.from_pretrained(__a , from_tf=__a ) self.assertIsNotNone(__a ) self.assertIsInstance(__a , __a ) @slow def __UpperCAmelCase ( self ): '''simple docstring''' for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __a : int = AutoConfig.from_pretrained(__a ) self.assertIsNotNone(__a ) self.assertIsInstance(__a , __a ) __a : Optional[Any] = TFAutoModelForCausalLM.from_pretrained(__a , from_pt=__a ) __a , __a : Optional[int] = TFAutoModelForCausalLM.from_pretrained( __a , output_loading_info=__a , from_pt=__a ) self.assertIsNotNone(__a ) self.assertIsInstance(__a , __a ) __a : Optional[int] = AutoModelForCausalLM.from_pretrained(__a , from_tf=__a ) __a , __a : Any = AutoModelForCausalLM.from_pretrained( __a , output_loading_info=__a , from_tf=__a ) self.assertIsNotNone(__a ) self.assertIsInstance(__a , __a ) @slow def __UpperCAmelCase ( self ): '''simple docstring''' for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __a : Optional[Any] = AutoConfig.from_pretrained(__a ) self.assertIsNotNone(__a ) self.assertIsInstance(__a , __a ) __a : int = TFAutoModelWithLMHead.from_pretrained(__a , from_pt=__a ) self.assertIsNotNone(__a ) self.assertIsInstance(__a , __a ) __a : str = AutoModelWithLMHead.from_pretrained(__a , from_tf=__a ) self.assertIsNotNone(__a ) self.assertIsInstance(__a , __a ) @slow def __UpperCAmelCase ( self ): '''simple docstring''' for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __a : str = AutoConfig.from_pretrained(__a ) self.assertIsNotNone(__a ) self.assertIsInstance(__a , __a ) __a : str = TFAutoModelForMaskedLM.from_pretrained(__a , from_pt=__a ) __a , __a : str = TFAutoModelForMaskedLM.from_pretrained( __a , output_loading_info=__a , from_pt=__a ) self.assertIsNotNone(__a ) self.assertIsInstance(__a , __a ) __a : Any = AutoModelForMaskedLM.from_pretrained(__a , from_tf=__a ) __a , __a : Tuple = AutoModelForMaskedLM.from_pretrained( __a , output_loading_info=__a , from_tf=__a ) self.assertIsNotNone(__a ) self.assertIsInstance(__a , __a ) @slow def __UpperCAmelCase ( self ): '''simple docstring''' for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __a : Tuple = AutoConfig.from_pretrained(__a ) self.assertIsNotNone(__a ) self.assertIsInstance(__a , __a ) __a : Tuple = TFAutoModelForSeqaSeqLM.from_pretrained(__a , from_pt=__a ) __a , __a : Tuple = TFAutoModelForSeqaSeqLM.from_pretrained( __a , output_loading_info=__a , from_pt=__a ) self.assertIsNotNone(__a ) self.assertIsInstance(__a , __a ) __a : Tuple = AutoModelForSeqaSeqLM.from_pretrained(__a , from_tf=__a ) __a , __a : int = AutoModelForSeqaSeqLM.from_pretrained( __a , output_loading_info=__a , from_tf=__a ) self.assertIsNotNone(__a ) self.assertIsInstance(__a , __a ) @slow def __UpperCAmelCase ( self ): '''simple docstring''' for model_name in ["bert-base-uncased"]: __a : Optional[Any] = AutoConfig.from_pretrained(__a ) self.assertIsNotNone(__a ) self.assertIsInstance(__a , __a ) __a : List[Any] = TFAutoModelForSequenceClassification.from_pretrained(__a , from_pt=__a ) self.assertIsNotNone(__a ) self.assertIsInstance(__a , __a ) __a : Union[str, Any] = AutoModelForSequenceClassification.from_pretrained(__a , from_tf=__a ) self.assertIsNotNone(__a ) self.assertIsInstance(__a , __a ) @slow def __UpperCAmelCase ( self ): '''simple docstring''' for model_name in ["bert-base-uncased"]: __a : List[Any] = AutoConfig.from_pretrained(__a ) self.assertIsNotNone(__a ) self.assertIsInstance(__a , __a ) __a : Any = TFAutoModelForQuestionAnswering.from_pretrained(__a , from_pt=__a ) self.assertIsNotNone(__a ) self.assertIsInstance(__a , __a ) __a : Tuple = AutoModelForQuestionAnswering.from_pretrained(__a , from_tf=__a ) self.assertIsNotNone(__a ) self.assertIsInstance(__a , __a ) def __UpperCAmelCase ( self ): '''simple docstring''' __a : str = TFAutoModelWithLMHead.from_pretrained(__a , from_pt=__a ) self.assertIsInstance(__a , __a ) self.assertEqual(model.num_parameters() , 1_4410 ) self.assertEqual(model.num_parameters(only_trainable=__a ) , 1_4410 ) __a : Optional[Any] = AutoModelWithLMHead.from_pretrained(__a , from_tf=__a ) self.assertIsInstance(__a , __a ) self.assertEqual(model.num_parameters() , 1_4410 ) self.assertEqual(model.num_parameters(only_trainable=__a ) , 1_4410 ) def __UpperCAmelCase ( self ): '''simple docstring''' __a : Union[str, Any] = TFAutoModelWithLMHead.from_pretrained(__a , from_pt=__a ) self.assertIsInstance(__a , __a ) self.assertEqual(model.num_parameters() , 1_4410 ) self.assertEqual(model.num_parameters(only_trainable=__a ) , 1_4410 ) __a : List[Any] = AutoModelWithLMHead.from_pretrained(__a , from_tf=__a ) self.assertIsInstance(__a , __a ) self.assertEqual(model.num_parameters() , 1_4410 ) self.assertEqual(model.num_parameters(only_trainable=__a ) , 1_4410 )
294
'''simple docstring''' def lowerCamelCase (_SCREAMING_SNAKE_CASE : int ): return number & 1 == 0 if __name__ == "__main__": import doctest doctest.testmod()
294
1
'''simple docstring''' 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__: str = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE( A__ ): """simple docstring""" def __init__( self : Dict , **__snake_case : List[str] ) -> Optional[Any]: requires_backends(self , ['''bs4'''] ) super().__init__(**__snake_case ) def A ( self : str , __snake_case : int ) -> Optional[Any]: UpperCAmelCase : str = [] UpperCAmelCase : int = [] UpperCAmelCase : List[str] = element if element.name else element.parent for parent in child.parents: # type: bs4.element.Tag UpperCAmelCase : Dict = parent.find_all(child.name , recursive=__snake_case ) xpath_tags.append(child.name ) xpath_subscripts.append( 0 if 1 == len(__snake_case ) else next(i for i, s in enumerate(__snake_case , 1 ) if s is child ) ) UpperCAmelCase : int = parent xpath_tags.reverse() xpath_subscripts.reverse() return xpath_tags, xpath_subscripts def A ( self : Any , __snake_case : int ) -> List[Any]: UpperCAmelCase : List[str] = BeautifulSoup(__snake_case , '''html.parser''' ) UpperCAmelCase : str = [] UpperCAmelCase : Optional[Any] = [] UpperCAmelCase : int = [] for element in html_code.descendants: if type(__snake_case ) == bsa.element.NavigableString: if type(element.parent ) != bsa.element.Tag: continue UpperCAmelCase : List[str] = html.unescape(__snake_case ).strip() if not text_in_this_tag: continue all_doc_strings.append(__snake_case ) UpperCAmelCase , UpperCAmelCase : str = self.xpath_soup(__snake_case ) stringaxtag_seq.append(__snake_case ) stringaxsubs_seq.append(__snake_case ) if len(__snake_case ) != len(__snake_case ): raise ValueError('''Number of doc strings and xtags does not correspond''' ) if len(__snake_case ) != len(__snake_case ): raise ValueError('''Number of doc strings and xsubs does not correspond''' ) return all_doc_strings, stringaxtag_seq, stringaxsubs_seq def A ( self : Tuple , __snake_case : List[str] , __snake_case : Tuple ) -> Dict: UpperCAmelCase : int = '''''' for tagname, subs in zip(__snake_case , __snake_case ): xpath += F"""/{tagname}""" if subs != 0: xpath += F"""[{subs}]""" return xpath def __call__( self : Any , __snake_case : Optional[int] ) -> BatchFeature: UpperCAmelCase : List[Any] = False # Check that strings has a valid type if isinstance(__snake_case , __snake_case ): UpperCAmelCase : Dict = True elif isinstance(__snake_case , (list, tuple) ): if len(__snake_case ) == 0 or isinstance(html_strings[0] , __snake_case ): UpperCAmelCase : int = True if not valid_strings: raise ValueError( '''HTML strings must of type `str`, `List[str]` (batch of examples), ''' F"""but is of type {type(__snake_case )}.""" ) UpperCAmelCase : List[str] = bool(isinstance(__snake_case , (list, tuple) ) and (isinstance(html_strings[0] , __snake_case )) ) if not is_batched: UpperCAmelCase : Any = [html_strings] # Get nodes + xpaths UpperCAmelCase : Tuple = [] UpperCAmelCase : List[str] = [] for html_string in html_strings: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Dict = self.get_three_from_single(__snake_case ) nodes.append(__snake_case ) UpperCAmelCase : Any = [] for node, tag_list, sub_list in zip(__snake_case , __snake_case , __snake_case ): UpperCAmelCase : str = self.construct_xpath(__snake_case , __snake_case ) xpath_strings.append(__snake_case ) xpaths.append(__snake_case ) # return as Dict UpperCAmelCase : int = {'''nodes''': nodes, '''xpaths''': xpaths} UpperCAmelCase : int = BatchFeature(data=__snake_case , tensor_type=__snake_case ) return encoded_inputs
23
"""simple docstring""" def __UpperCAmelCase ( __lowerCamelCase = 50 ) -> int: lowercase__ : int = [[0] * 3 for _ in range(length + 1 )] for row_length in range(length + 1 ): for tile_length in range(2 , 5 ): for tile_start in range(row_length - tile_length + 1 ): different_colour_ways_number[row_length][tile_length - 2] += ( different_colour_ways_number[row_length - tile_start - tile_length][ tile_length - 2 ] + 1 ) return sum(different_colour_ways_number[length] ) if __name__ == "__main__": print(F'''{solution() = }''')
16
0
'''simple docstring''' import argparse from pathlib import Path import torch from transformers import OPTConfig, OPTModel from transformers.utils import logging logging.set_verbosity_info() A__ : Union[str, Any] = logging.get_logger(__name__) def a_ ( _UpperCAmelCase : List[str] ) -> Dict: __snake_case : str = torch.load(_UpperCAmelCase ,map_location='cpu' ) if "model" in sd.keys(): __snake_case : Any = torch.load(_UpperCAmelCase ,map_location='cpu' )['model'] # pop unnecessary weights __snake_case : str = [ 'decoder.version', 'decoder.output_projection.weight', ] for key in keys_to_delete: if key in sd: sd.pop(_UpperCAmelCase ) __snake_case : Dict = { 'decoder.project_in_dim.weight': 'decoder.project_in.weight', 'decoder.project_out_dim.weight': 'decoder.project_out.weight', 'decoder.layer_norm.weight': 'decoder.final_layer_norm.weight', 'decoder.layer_norm.bias': 'decoder.final_layer_norm.bias', } for old_key, new_key in keys_to_rename.items(): if old_key in sd: __snake_case : int = sd.pop(_UpperCAmelCase ) __snake_case : Optional[Any] = list(sd.keys() ) for key in keys: if ".qkv_proj." in key: __snake_case : List[Any] = sd[key] # We split QKV in separate Q,K,V __snake_case : Tuple = key.replace('.qkv_proj.' ,'.q_proj.' ) __snake_case : Any = key.replace('.qkv_proj.' ,'.k_proj.' ) __snake_case : Optional[Any] = key.replace('.qkv_proj.' ,'.v_proj.' ) __snake_case : List[Any] = value.shape[0] assert depth % 3 == 0 # `SequeuceParallelTransformerBlock` has QKV weight is separated in K,V,Q despite the naming: # https://cs.github.com/facebookresearch/metaseq/blob/51871bd73cd04c038f239ea2a26db1d7f6b37927/metaseq/modules/sequence_parallel_transformer_layer.py#L97 __snake_case , __snake_case , __snake_case : Optional[Any] = torch.split(_UpperCAmelCase ,depth // 3 ,dim=0 ) __snake_case : Optional[Any] = q __snake_case : List[str] = k __snake_case : Any = v del sd[key] return sd @torch.no_grad() def a_ ( _UpperCAmelCase : Tuple ,_UpperCAmelCase : Optional[Any] ,_UpperCAmelCase : Dict=None ) -> int: __snake_case : Dict = load_checkpoint(_UpperCAmelCase ) if config is not None: __snake_case : Union[str, Any] = OPTConfig.from_pretrained(_UpperCAmelCase ) else: __snake_case : int = OPTConfig() __snake_case : Optional[int] = OPTModel(_UpperCAmelCase ).half().eval() model.load_state_dict(_UpperCAmelCase ) # Check results Path(_UpperCAmelCase ).mkdir(exist_ok=_UpperCAmelCase ) model.save_pretrained(_UpperCAmelCase ) if __name__ == "__main__": A__ : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--fairseq_path''', type=str, help=( '''path to fairseq checkpoint in correct format. You can find all checkpoints in the correct format here:''' ''' https://huggingface.co/models?other=opt_metasq''' ), ) parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument('''--hf_config''', default=None, type=str, help='''Define HF config.''') A__ : List[str] = parser.parse_args() convert_opt_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, config=args.hf_config)
0
'''simple docstring''' import argparse import json import logging import os import shutil import sys import tempfile import unittest from unittest import mock import torch from accelerate.utils import write_basic_config from transformers.testing_utils import TestCasePlus, get_gpu_count, run_command, slow, torch_device from transformers.utils import is_apex_available logging.basicConfig(level=logging.DEBUG) A__ : Dict = logging.getLogger() def a_ ( ) -> Tuple: __snake_case : List[Any] = argparse.ArgumentParser() parser.add_argument('-f' ) __snake_case : Any = parser.parse_args() return args.f def a_ ( _UpperCAmelCase : Optional[int] ) -> List[Any]: __snake_case : Tuple = {} __snake_case : Union[str, Any] = os.path.join(_UpperCAmelCase ,'all_results.json' ) if os.path.exists(_UpperCAmelCase ): with open(_UpperCAmelCase ,'r' ) as f: __snake_case : List[str] = json.load(_UpperCAmelCase ) else: raise ValueError(f'''can\'t find {path}''' ) return results def a_ ( ) -> Union[str, Any]: __snake_case : Union[str, Any] = torch.cuda.is_available() and torch_device == 'cuda' return is_using_cuda and is_apex_available() A__ : str = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class snake_case__ ( SCREAMING_SNAKE_CASE_ ): @classmethod def A_ ( cls : Any ) -> List[str]: '''simple docstring''' # Write Accelerate config, will pick up on CPU, GPU, and multi-GPU __snake_case : Optional[int] = tempfile.mkdtemp() __snake_case : Dict = os.path.join(cls.tmpdir , 'default_config.yml' ) write_basic_config(save_location=cls.configPath ) __snake_case : List[Any] = ['accelerate', 'launch', '--config_file', cls.configPath] @classmethod def A_ ( cls : List[str] ) -> List[str]: '''simple docstring''' shutil.rmtree(cls.tmpdir ) @mock.patch.dict(os.environ , {'WANDB_MODE': 'offline'} ) def A_ ( self : Any ) -> Optional[Any]: '''simple docstring''' __snake_case : List[Any] = self.get_auto_remove_tmp_dir() __snake_case : Dict = f''' {self.examples_dir}/pytorch/text-classification/run_glue_no_trainer.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 --seed=42 --checkpointing_steps epoch --with_tracking '''.split() if is_cuda_and_apex_available(): testargs.append('--fp16' ) run_command(self._launch_args + testargs ) __snake_case : List[Any] = get_results(__a ) self.assertGreaterEqual(result['eval_accuracy'] , 0.7_5 ) self.assertTrue(os.path.exists(os.path.join(__a , 'epoch_0' ) ) ) self.assertTrue(os.path.exists(os.path.join(__a , 'glue_no_trainer' ) ) ) @mock.patch.dict(os.environ , {'WANDB_MODE': 'offline'} ) def A_ ( self : List[Any] ) -> Union[str, Any]: '''simple docstring''' __snake_case : Tuple = self.get_auto_remove_tmp_dir() __snake_case : str = f''' {self.examples_dir}/pytorch/language-modeling/run_clm_no_trainer.py --model_name_or_path distilgpt2 --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --block_size 128 --per_device_train_batch_size 5 --per_device_eval_batch_size 5 --num_train_epochs 2 --output_dir {tmp_dir} --checkpointing_steps epoch --with_tracking '''.split() if torch.cuda.device_count() > 1: # Skipping because there are not enough batches to train the model + would need a drop_last to work. return run_command(self._launch_args + testargs ) __snake_case : str = get_results(__a ) self.assertLess(result['perplexity'] , 100 ) self.assertTrue(os.path.exists(os.path.join(__a , 'epoch_0' ) ) ) self.assertTrue(os.path.exists(os.path.join(__a , 'clm_no_trainer' ) ) ) @mock.patch.dict(os.environ , {'WANDB_MODE': 'offline'} ) def A_ ( self : str ) -> List[str]: '''simple docstring''' __snake_case : int = self.get_auto_remove_tmp_dir() __snake_case : List[str] = f''' {self.examples_dir}/pytorch/language-modeling/run_mlm_no_trainer.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} --num_train_epochs=1 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) __snake_case : List[str] = get_results(__a ) self.assertLess(result['perplexity'] , 42 ) self.assertTrue(os.path.exists(os.path.join(__a , 'epoch_0' ) ) ) self.assertTrue(os.path.exists(os.path.join(__a , 'mlm_no_trainer' ) ) ) @mock.patch.dict(os.environ , {'WANDB_MODE': 'offline'} ) def A_ ( self : Optional[int] ) -> Optional[int]: '''simple docstring''' # with so little data distributed training needs more epochs to get the score on par with 0/1 gpu __snake_case : Any = 7 if get_gpu_count() > 1 else 2 __snake_case : Any = self.get_auto_remove_tmp_dir() __snake_case : int = f''' {self.examples_dir}/pytorch/token-classification/run_ner_no_trainer.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} --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=2 --num_train_epochs={epochs} --seed 7 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) __snake_case : Dict = get_results(__a ) self.assertGreaterEqual(result['eval_accuracy'] , 0.7_5 ) self.assertLess(result['train_loss'] , 0.5 ) self.assertTrue(os.path.exists(os.path.join(__a , 'epoch_0' ) ) ) self.assertTrue(os.path.exists(os.path.join(__a , 'ner_no_trainer' ) ) ) @unittest.skip(reason='Fix me @muellerzr' ) @mock.patch.dict(os.environ , {'WANDB_MODE': 'offline'} ) def A_ ( self : Any ) -> List[Any]: '''simple docstring''' __snake_case : Any = self.get_auto_remove_tmp_dir() __snake_case : Tuple = f''' {self.examples_dir}/pytorch/question-answering/run_qa_no_trainer.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} --seed=42 --max_train_steps=10 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) __snake_case : str = get_results(__a ) # Because we use --version_2_with_negative the testing script uses SQuAD v2 metrics. self.assertGreaterEqual(result['eval_f1'] , 28 ) self.assertGreaterEqual(result['eval_exact'] , 28 ) self.assertTrue(os.path.exists(os.path.join(__a , 'epoch_0' ) ) ) self.assertTrue(os.path.exists(os.path.join(__a , 'qa_no_trainer' ) ) ) @mock.patch.dict(os.environ , {'WANDB_MODE': 'offline'} ) def A_ ( self : Dict ) -> List[Any]: '''simple docstring''' __snake_case : str = self.get_auto_remove_tmp_dir() __snake_case : Any = f''' {self.examples_dir}/pytorch/multiple-choice/run_swag_no_trainer.py --model_name_or_path bert-base-uncased --train_file tests/fixtures/tests_samples/swag/sample.json --validation_file tests/fixtures/tests_samples/swag/sample.json --output_dir {tmp_dir} --max_train_steps=20 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --with_tracking '''.split() run_command(self._launch_args + testargs ) __snake_case : str = get_results(__a ) self.assertGreaterEqual(result['eval_accuracy'] , 0.8 ) self.assertTrue(os.path.exists(os.path.join(__a , 'swag_no_trainer' ) ) ) @slow @mock.patch.dict(os.environ , {'WANDB_MODE': 'offline'} ) def A_ ( self : Any ) -> Union[str, Any]: '''simple docstring''' __snake_case : Tuple = self.get_auto_remove_tmp_dir() __snake_case : List[str] = f''' {self.examples_dir}/pytorch/summarization/run_summarization_no_trainer.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 --output_dir {tmp_dir} --max_train_steps=50 --num_warmup_steps=8 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) __snake_case : int = get_results(__a ) self.assertGreaterEqual(result['eval_rouge1'] , 10 ) self.assertGreaterEqual(result['eval_rouge2'] , 2 ) self.assertGreaterEqual(result['eval_rougeL'] , 7 ) self.assertGreaterEqual(result['eval_rougeLsum'] , 7 ) self.assertTrue(os.path.exists(os.path.join(__a , 'epoch_0' ) ) ) self.assertTrue(os.path.exists(os.path.join(__a , 'summarization_no_trainer' ) ) ) @slow @mock.patch.dict(os.environ , {'WANDB_MODE': 'offline'} ) def A_ ( self : Union[str, Any] ) -> int: '''simple docstring''' __snake_case : Tuple = self.get_auto_remove_tmp_dir() __snake_case : str = f''' {self.examples_dir}/pytorch/translation/run_translation_no_trainer.py --model_name_or_path sshleifer/student_marian_en_ro_6_1 --source_lang en --target_lang ro --train_file tests/fixtures/tests_samples/wmt16/sample.json --validation_file tests/fixtures/tests_samples/wmt16/sample.json --output_dir {tmp_dir} --max_train_steps=50 --num_warmup_steps=8 --num_beams=6 --learning_rate=3e-3 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --source_lang en_XX --target_lang ro_RO --checkpointing_steps epoch --with_tracking '''.split() run_command(self._launch_args + testargs ) __snake_case : Dict = get_results(__a ) self.assertGreaterEqual(result['eval_bleu'] , 30 ) self.assertTrue(os.path.exists(os.path.join(__a , 'epoch_0' ) ) ) self.assertTrue(os.path.exists(os.path.join(__a , 'translation_no_trainer' ) ) ) @slow def A_ ( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' __snake_case : Union[str, Any] = logging.StreamHandler(sys.stdout ) logger.addHandler(__a ) __snake_case : List[str] = self.get_auto_remove_tmp_dir() __snake_case : int = f''' {self.examples_dir}/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py --dataset_name huggingface/semantic-segmentation-test-sample --output_dir {tmp_dir} --max_train_steps=10 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch '''.split() run_command(self._launch_args + testargs ) __snake_case : List[str] = get_results(__a ) self.assertGreaterEqual(result['eval_overall_accuracy'] , 0.1_0 ) @mock.patch.dict(os.environ , {'WANDB_MODE': 'offline'} ) def A_ ( self : Tuple ) -> Any: '''simple docstring''' __snake_case : Dict = self.get_auto_remove_tmp_dir() __snake_case : Dict = f''' {self.examples_dir}/pytorch/image-classification/run_image_classification_no_trainer.py --model_name_or_path google/vit-base-patch16-224-in21k --dataset_name hf-internal-testing/cats_vs_dogs_sample --learning_rate 1e-4 --per_device_train_batch_size 2 --per_device_eval_batch_size 1 --max_train_steps 2 --train_val_split 0.1 --seed 42 --output_dir {tmp_dir} --with_tracking --checkpointing_steps 1 '''.split() if is_cuda_and_apex_available(): testargs.append('--fp16' ) run_command(self._launch_args + testargs ) __snake_case : Optional[int] = get_results(__a ) # The base model scores a 25% self.assertGreaterEqual(result['eval_accuracy'] , 0.6 ) self.assertTrue(os.path.exists(os.path.join(__a , 'step_1' ) ) ) self.assertTrue(os.path.exists(os.path.join(__a , 'image_classification_no_trainer' ) ) )
0
1
"""simple docstring""" from __future__ import annotations from collections.abc import Iterator from typing import Generic, TypeVar _a : Optional[Any] = TypeVar('T') class __A ( Generic[T] ): def __init__( self , a__ ): _lowerCAmelCase : Optional[Any] = data _lowerCAmelCase : Node[T] | None = None def __str__( self ): return F"{self.data}" class __A ( Generic[T] ): def __init__( self ): _lowerCAmelCase : Node[T] | None = None def __iter__( self ): _lowerCAmelCase : List[Any] = self.top while node: yield node.data _lowerCAmelCase : Dict = node.next def __str__( self ): return "->".join([str(a__ ) for item in self] ) def __len__( self ): return len(tuple(iter(self ) ) ) def __A ( self ): return self.top is None def __A ( self , a__ ): _lowerCAmelCase : str = Node(a__ ) if not self.is_empty(): _lowerCAmelCase : int = self.top _lowerCAmelCase : List[str] = node def __A ( self ): if self.is_empty(): raise IndexError("""pop from empty stack""" ) assert isinstance(self.top , a__ ) _lowerCAmelCase : Optional[Any] = self.top _lowerCAmelCase : Optional[Any] = self.top.next return pop_node.data def __A ( self ): if self.is_empty(): raise IndexError("""peek from empty stack""" ) assert self.top is not None return self.top.data def __A ( self ): _lowerCAmelCase : List[str] = None if __name__ == "__main__": from doctest import testmod testmod()
44
"""simple docstring""" import inspect import re from transformers.utils import direct_transformers_import # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_config_docstrings.py snake_case__ : Optional[Any] = '''src/transformers''' # This is to make sure the transformers module imported is the one in the repo. snake_case__ : Dict = direct_transformers_import(PATH_TO_TRANSFORMERS) snake_case__ : Optional[int] = transformers.models.auto.configuration_auto.CONFIG_MAPPING # Regex pattern used to find the checkpoint mentioned in the docstring of `config_class`. # For example, `[bert-base-uncased](https://huggingface.co/bert-base-uncased)` snake_case__ : Optional[int] = re.compile(R'''\[(.+?)\]\((https://huggingface\.co/.+?)\)''') snake_case__ : int = { '''DecisionTransformerConfig''', '''EncoderDecoderConfig''', '''MusicgenConfig''', '''RagConfig''', '''SpeechEncoderDecoderConfig''', '''TimmBackboneConfig''', '''VisionEncoderDecoderConfig''', '''VisionTextDualEncoderConfig''', '''LlamaConfig''', } def _snake_case ( _snake_case : List[str] ): lowerCAmelCase : Dict = None # source code of `config_class` lowerCAmelCase : Union[str, Any] = inspect.getsource(_snake_case ) lowerCAmelCase : List[Any] = _re_checkpoint.findall(_snake_case ) # Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link. # For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')` for ckpt_name, ckpt_link in checkpoints: # allow the link to end with `/` if ckpt_link.endswith('''/''' ): lowerCAmelCase : List[str] = ckpt_link[:-1] # verify the checkpoint name corresponds to the checkpoint link lowerCAmelCase : Optional[int] = f'''https://huggingface.co/{ckpt_name}''' if ckpt_link == ckpt_link_from_name: lowerCAmelCase : List[str] = ckpt_name break return checkpoint def _snake_case ( ): lowerCAmelCase : List[Any] = [] for config_class in list(CONFIG_MAPPING.values() ): # Skip deprecated models if "models.deprecated" in config_class.__module__: continue lowerCAmelCase : int = get_checkpoint_from_config_class(_snake_case ) lowerCAmelCase : int = config_class.__name__ if checkpoint is None and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK: configs_without_checkpoint.append(_snake_case ) if len(_snake_case ) > 0: lowerCAmelCase : Dict = '''\n'''.join(sorted(_snake_case ) ) raise ValueError(f'''The following configurations don\'t contain any valid checkpoint:\n{message}''' ) if __name__ == "__main__": check_config_docstrings_have_checkpoints()
60
0
import json import os import unittest from transformers.models.gptsan_japanese.tokenization_gptsan_japanese import ( VOCAB_FILES_NAMES, GPTSanJapaneseTokenizer, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class _snake_case ( A__ , unittest.TestCase ): _lowercase : Tuple = GPTSanJapaneseTokenizer _lowercase : List[Any] = False _lowercase : List[Any] = {"""do_clean_text""": False, """add_prefix_space""": False} def SCREAMING_SNAKE_CASE__ ( self) -> List[Any]: super().setUp() # fmt: off SCREAMING_SNAKE_CASE = ['こん', 'こんに', 'にちは', 'ばんは', '世界,㔺界', '、', '。', '<BR>', '<SP>', '<TAB>', '<URL>', '<EMAIL>', '<TEL>', '<DATE>', '<PRICE>', '<BLOCK>', '<KIGOU>', '<U2000U2BFF>', '<|emoji1|>', '<unk>', '<|bagoftoken|>', '<|endoftext|>'] # fmt: on SCREAMING_SNAKE_CASE = {'emoji': {'\ud83d\ude00': '<|emoji1|>'}, 'emoji_inv': {'<|emoji1|>': '\ud83d\ude00'}} # 😀 SCREAMING_SNAKE_CASE = {'unk_token': '<unk>'} SCREAMING_SNAKE_CASE = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file']) SCREAMING_SNAKE_CASE = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['emoji_file']) with open(self.vocab_file , 'w' , encoding='utf-8') as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens])) with open(self.emoji_file , 'w') as emoji_writer: emoji_writer.write(json.dumps(_lowercase)) def SCREAMING_SNAKE_CASE__ ( self , **a) -> List[Any]: kwargs.update(self.special_tokens_map) return GPTSanJapaneseTokenizer.from_pretrained(self.tmpdirname , **_lowercase) def SCREAMING_SNAKE_CASE__ ( self , a) -> List[Any]: SCREAMING_SNAKE_CASE = 'こんにちは、世界。 \nこんばんは、㔺界。😀' SCREAMING_SNAKE_CASE = 'こんにちは、世界。 \nこんばんは、世界。😀' return input_text, output_text def SCREAMING_SNAKE_CASE__ ( self , a) -> int: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.get_input_output_texts(_lowercase) SCREAMING_SNAKE_CASE = tokenizer.encode(_lowercase , add_special_tokens=_lowercase) SCREAMING_SNAKE_CASE = tokenizer.decode(_lowercase , clean_up_tokenization_spaces=_lowercase) return text, ids def SCREAMING_SNAKE_CASE__ ( self) -> Any: pass # TODO add if relevant def SCREAMING_SNAKE_CASE__ ( self) -> Optional[int]: pass # TODO add if relevant def SCREAMING_SNAKE_CASE__ ( self) -> Dict: pass # TODO add if relevant def SCREAMING_SNAKE_CASE__ ( self) -> Optional[int]: SCREAMING_SNAKE_CASE = self.get_tokenizer() # Testing tokenization SCREAMING_SNAKE_CASE = 'こんにちは、世界。 こんばんは、㔺界。' SCREAMING_SNAKE_CASE = ['こん', 'にちは', '、', '世界', '。', '<SP>', 'こん', 'ばんは', '、', '㔺界', '。'] SCREAMING_SNAKE_CASE = tokenizer.tokenize(_lowercase) self.assertListEqual(_lowercase , _lowercase) # Testing conversion to ids without special tokens SCREAMING_SNAKE_CASE = [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6] SCREAMING_SNAKE_CASE = tokenizer.convert_tokens_to_ids(_lowercase) self.assertListEqual(_lowercase , _lowercase) # Testing conversion to ids with special tokens SCREAMING_SNAKE_CASE = tokens + [tokenizer.unk_token] SCREAMING_SNAKE_CASE = [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6, 19] SCREAMING_SNAKE_CASE = tokenizer.convert_tokens_to_ids(_lowercase) self.assertListEqual(_lowercase , _lowercase) def SCREAMING_SNAKE_CASE__ ( self) -> Optional[Any]: SCREAMING_SNAKE_CASE = self.get_tokenizer() # Testing tokenization SCREAMING_SNAKE_CASE = 'こんにちは、<|bagoftoken|>世界。こんばんは、<|bagoftoken|>㔺界。' SCREAMING_SNAKE_CASE = 'こんにちは、、、、世界。こんばんは、、、、世界。' SCREAMING_SNAKE_CASE = tokenizer.encode(_lowercase) SCREAMING_SNAKE_CASE = tokenizer.decode(_lowercase) self.assertEqual(_lowercase , _lowercase) @slow def SCREAMING_SNAKE_CASE__ ( self) -> Optional[int]: SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained('Tanrei/GPTSAN-japanese') # Testing tokenization SCREAMING_SNAKE_CASE = 'こんにちは、世界。' SCREAMING_SNAKE_CASE = 'こんばんは、㔺界。😀' SCREAMING_SNAKE_CASE = 'こんにちは、世界。こんばんは、世界。😀' SCREAMING_SNAKE_CASE = tokenizer.encode(prefix_text + input_text) SCREAMING_SNAKE_CASE = tokenizer.encode('' , prefix_text=prefix_text + input_text) SCREAMING_SNAKE_CASE = tokenizer.encode(_lowercase , prefix_text=_lowercase) SCREAMING_SNAKE_CASE = tokenizer.decode(_lowercase) SCREAMING_SNAKE_CASE = tokenizer.decode(_lowercase) SCREAMING_SNAKE_CASE = tokenizer.decode(_lowercase) self.assertEqual(_lowercase , _lowercase) self.assertEqual(_lowercase , _lowercase) self.assertEqual(_lowercase , _lowercase) @slow def SCREAMING_SNAKE_CASE__ ( self) -> List[str]: SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained('Tanrei/GPTSAN-japanese') # Testing tokenization SCREAMING_SNAKE_CASE = 'こんにちは、世界。' SCREAMING_SNAKE_CASE = 'こんばんは、㔺界。😀' SCREAMING_SNAKE_CASE = len(tokenizer.encode(_lowercase)) - 2 SCREAMING_SNAKE_CASE = len(tokenizer.encode(_lowercase)) - 2 SCREAMING_SNAKE_CASE = [1] + [0] * (len_prefix + len_text + 1) SCREAMING_SNAKE_CASE = [1] * (len_prefix + len_text + 1) + [0] SCREAMING_SNAKE_CASE = [1] + [1] * (len_prefix) + [0] * (len_text + 1) SCREAMING_SNAKE_CASE = tokenizer(prefix_text + input_text).token_type_ids SCREAMING_SNAKE_CASE = tokenizer('' , prefix_text=prefix_text + input_text).token_type_ids SCREAMING_SNAKE_CASE = tokenizer(_lowercase , prefix_text=_lowercase).token_type_ids self.assertListEqual(_lowercase , _lowercase) self.assertListEqual(_lowercase , _lowercase) self.assertListEqual(_lowercase , _lowercase) @slow def SCREAMING_SNAKE_CASE__ ( self) -> Union[str, Any]: SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained('Tanrei/GPTSAN-japanese') SCREAMING_SNAKE_CASE = tokenizer.encode('あンいワ') SCREAMING_SNAKE_CASE = tokenizer.encode('' , prefix_text='あンいワ') SCREAMING_SNAKE_CASE = tokenizer.encode('いワ' , prefix_text='あン') self.assertEqual(tokenizer.decode(_lowercase) , tokenizer.decode(_lowercase)) self.assertEqual(tokenizer.decode(_lowercase) , tokenizer.decode(_lowercase)) self.assertNotEqual(_lowercase , _lowercase) self.assertNotEqual(_lowercase , _lowercase) self.assertEqual(x_token_a[1] , x_token_a[-1]) # SEG token self.assertEqual(x_token_a[1] , x_token_a[3]) # SEG token @slow def SCREAMING_SNAKE_CASE__ ( self) -> Any: SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained('Tanrei/GPTSAN-japanese') SCREAMING_SNAKE_CASE = [['武田信玄', 'は、'], ['織田信長', 'の配下の、']] SCREAMING_SNAKE_CASE = tokenizer(_lowercase , padding=_lowercase) SCREAMING_SNAKE_CASE = tokenizer.batch_encode_plus(_lowercase , padding=_lowercase) # fmt: off SCREAMING_SNAKE_CASE = [[3_5993, 8640, 2_5948, 3_5998, 3_0647, 3_5675, 3_5999, 3_5999], [3_5993, 1_0382, 9868, 3_5998, 3_0646, 9459, 3_0646, 3_5675]] SCREAMING_SNAKE_CASE = [[1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0]] SCREAMING_SNAKE_CASE = [[1, 1, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1]] # fmt: on self.assertListEqual(x_token.input_ids , _lowercase) self.assertListEqual(x_token.token_type_ids , _lowercase) self.assertListEqual(x_token.attention_mask , _lowercase) self.assertListEqual(x_token_a.input_ids , _lowercase) self.assertListEqual(x_token_a.token_type_ids , _lowercase) self.assertListEqual(x_token_a.attention_mask , _lowercase) def SCREAMING_SNAKE_CASE__ ( self) -> Dict: # Intentionally convert some words to accommodate character fluctuations unique to Japanese pass def SCREAMING_SNAKE_CASE__ ( self) -> Any: # tokenizer has no padding token pass
366
import gc import importlib.metadata import tempfile import unittest from packaging import version from transformers import ( AutoModel, AutoModelForCausalLM, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoTokenizer, BitsAndBytesConfig, pipeline, ) from transformers.testing_utils import ( is_torch_available, require_accelerate, require_bitsandbytes, require_torch, require_torch_gpu, require_torch_multi_gpu, slow, ) def lowerCamelCase__ (_UpperCAmelCase): if model.config.model_type == "gpt2": return model.transformer.h[0].mlp.c_fc return model.transformer.h[0].mlp.dense_ah_to_h if is_torch_available(): import torch import torch.nn as nn class _snake_case ( nn.Module ): def __init__( self , a , a) -> Union[str, Any]: super().__init__() SCREAMING_SNAKE_CASE = module SCREAMING_SNAKE_CASE = nn.Sequential( nn.Linear(module.in_features , a , bias=a) , nn.Linear(a , module.out_features , bias=a) , ) SCREAMING_SNAKE_CASE = (2.0 / (5 * min(module.in_features , module.out_features))) ** 0.5 nn.init.normal_(self.adapter[0].weight , std=a) nn.init.zeros_(self.adapter[1].weight) self.adapter.to(module.weight.device) def SCREAMING_SNAKE_CASE__ ( self , a , *a , **a) -> Any: return self.module(a , *a , **a) + self.adapter(a) @require_bitsandbytes @require_accelerate @require_torch @require_torch_gpu @slow class _snake_case ( unittest.TestCase ): # We keep the constants inside the init function and model loading inside setUp function # We need to test on relatively large models (aka >1b parameters otherwise the quantiztion may not work as expected) # Therefore here we use only bloom-1b3 to test our module _lowercase : Union[str, Any] = '''bigscience/bloom-1b7''' # Constant values _lowercase : str = 2.109_6595_5269_2574 _lowercase : Any = '''Hello my name is''' _lowercase : Any = set() EXPECTED_OUTPUTS.add('''Hello my name is John and I am a professional photographer. I''' ) EXPECTED_OUTPUTS.add('''Hello my name is John.\nI am a friend of your father.\n''' ) EXPECTED_OUTPUTS.add('''Hello my name is John Doe, I am a student at the University''' ) _lowercase : Union[str, Any] = 10 def SCREAMING_SNAKE_CASE__ ( self) -> Any: # Models and tokenizer SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained(self.model_name) class _snake_case ( A__ ): def SCREAMING_SNAKE_CASE__ ( self) -> Tuple: super().setUp() # Models and tokenizer SCREAMING_SNAKE_CASE = AutoModelForCausalLM.from_pretrained( self.model_name , torch_dtype=torch.floataa , device_map='auto') SCREAMING_SNAKE_CASE = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=a , device_map='auto') def SCREAMING_SNAKE_CASE__ ( self) -> Tuple: del self.model_fpaa del self.model_abit gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE__ ( self) -> Any: SCREAMING_SNAKE_CASE = self.model_abit.config self.assertTrue(hasattr(a , 'quantization_config')) SCREAMING_SNAKE_CASE = config.to_dict() SCREAMING_SNAKE_CASE = config.to_diff_dict() SCREAMING_SNAKE_CASE = config.to_json_string() def SCREAMING_SNAKE_CASE__ ( self) -> Any: from bitsandbytes.nn import Paramsabit SCREAMING_SNAKE_CASE = self.model_fpaa.get_memory_footprint() SCREAMING_SNAKE_CASE = self.model_abit.get_memory_footprint() self.assertAlmostEqual(mem_fpaa / mem_abit , self.EXPECTED_RELATIVE_DIFFERENCE) SCREAMING_SNAKE_CASE = get_some_linear_layer(self.model_abit) self.assertTrue(linear.weight.__class__ == Paramsabit) def SCREAMING_SNAKE_CASE__ ( self) -> Optional[int]: from transformers import TaPreTrainedModel self.model_fpaa.get_memory_footprint() self.model_abit.get_memory_footprint() for name, module in self.model_abit.named_modules(): if isinstance(a , torch.nn.Linear): if name not in ["lm_head"] + TaPreTrainedModel._keep_in_fpaa_modules: # 4-bit parameters are packed in uint8 variables self.assertTrue(module.weight.dtype == torch.uinta) def SCREAMING_SNAKE_CASE__ ( self) -> Dict: SCREAMING_SNAKE_CASE = self.tokenizer(self.input_text , return_tensors='pt') SCREAMING_SNAKE_CASE = self.model_abit.generate(input_ids=encoded_input['input_ids'].to(0) , max_new_tokens=10) self.assertIn(self.tokenizer.decode(output_sequences[0] , skip_special_tokens=a) , self.EXPECTED_OUTPUTS) def SCREAMING_SNAKE_CASE__ ( self) -> Any: SCREAMING_SNAKE_CASE = BitsAndBytesConfig() SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = AutoModelForCausalLM.from_pretrained( self.model_name , quantization_config=a , device_map='auto') SCREAMING_SNAKE_CASE = self.tokenizer(self.input_text , return_tensors='pt') SCREAMING_SNAKE_CASE = model_abit_from_config.generate( input_ids=encoded_input['input_ids'].to(0) , max_new_tokens=10) self.assertIn(self.tokenizer.decode(output_sequences[0] , skip_special_tokens=a) , self.EXPECTED_OUTPUTS) def SCREAMING_SNAKE_CASE__ ( self) -> str: with self.assertRaises(a), tempfile.TemporaryDirectory() as tmpdirname: self.model_abit.save_pretrained(a) def SCREAMING_SNAKE_CASE__ ( self) -> List[str]: SCREAMING_SNAKE_CASE = BitsAndBytesConfig() with self.assertRaises(a): SCREAMING_SNAKE_CASE = AutoModelForCausalLM.from_pretrained( self.model_name , quantization_config=a , load_in_abit=a , device_map='auto' , bnb_abit_quant_type='nf4' , ) def SCREAMING_SNAKE_CASE__ ( self) -> int: with self.assertRaises(a): # Tries with `str` self.model_abit.to('cpu') with self.assertRaises(a): # Tries with a `dtype`` self.model_abit.to(torch.floataa) with self.assertRaises(a): # Tries with a `device` self.model_abit.to(torch.device('cuda:0')) with self.assertRaises(a): # Tries with a `device` self.model_abit.float() with self.assertRaises(a): # Tries with a `device` self.model_abit.half() # Test if we did not break anything SCREAMING_SNAKE_CASE = self.tokenizer(self.input_text , return_tensors='pt') SCREAMING_SNAKE_CASE = self.model_fpaa.to(torch.floataa) SCREAMING_SNAKE_CASE = self.model_fpaa.generate(input_ids=encoded_input['input_ids'].to(0) , max_new_tokens=10) # Check this does not throw an error SCREAMING_SNAKE_CASE = self.model_fpaa.to('cpu') # Check this does not throw an error SCREAMING_SNAKE_CASE = self.model_fpaa.half() # Check this does not throw an error SCREAMING_SNAKE_CASE = self.model_fpaa.float() def SCREAMING_SNAKE_CASE__ ( self) -> int: SCREAMING_SNAKE_CASE = AutoModelForSeqaSeqLM.from_pretrained('t5-small' , load_in_abit=a , device_map='auto') self.assertTrue(model.decoder.block[0].layer[2].DenseReluDense.wo.weight.dtype == torch.floataa) @require_bitsandbytes @require_accelerate @require_torch @require_torch_gpu @slow class _snake_case ( unittest.TestCase ): @classmethod def SCREAMING_SNAKE_CASE__ ( cls) -> Tuple: SCREAMING_SNAKE_CASE = 't5-small' SCREAMING_SNAKE_CASE = 'google/flan-t5-small' # flan-t5 uses dense-act instead of dense-relu-dense SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained(cls.model_name) SCREAMING_SNAKE_CASE = 'Translate in German: Hello, my dog is cute' def SCREAMING_SNAKE_CASE__ ( self) -> Dict: gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE__ ( self) -> Optional[Any]: from transformers import TaForConditionalGeneration SCREAMING_SNAKE_CASE = TaForConditionalGeneration._keep_in_fpaa_modules SCREAMING_SNAKE_CASE = None # test with `t5-small` SCREAMING_SNAKE_CASE = TaForConditionalGeneration.from_pretrained(self.model_name , load_in_abit=a , device_map='auto') SCREAMING_SNAKE_CASE = self.tokenizer(self.input_text , return_tensors='pt').to(0) SCREAMING_SNAKE_CASE = model.generate(**a) # test with `flan-t5-small` SCREAMING_SNAKE_CASE = TaForConditionalGeneration.from_pretrained( self.dense_act_model_name , load_in_abit=a , device_map='auto') SCREAMING_SNAKE_CASE = self.tokenizer(self.input_text , return_tensors='pt').to(0) SCREAMING_SNAKE_CASE = model.generate(**a) SCREAMING_SNAKE_CASE = modules def SCREAMING_SNAKE_CASE__ ( self) -> Optional[Any]: import bitsandbytes as bnb from transformers import TaForConditionalGeneration # test with `t5-small` SCREAMING_SNAKE_CASE = TaForConditionalGeneration.from_pretrained(self.model_name , load_in_abit=a , device_map='auto') # there was a bug with decoders - this test checks that it is fixed self.assertTrue(isinstance(model.decoder.block[0].layer[0].SelfAttention.q , bnb.nn.Linearabit)) SCREAMING_SNAKE_CASE = self.tokenizer(self.input_text , return_tensors='pt').to(0) SCREAMING_SNAKE_CASE = model.generate(**a) # test with `flan-t5-small` SCREAMING_SNAKE_CASE = TaForConditionalGeneration.from_pretrained( self.dense_act_model_name , load_in_abit=a , device_map='auto') SCREAMING_SNAKE_CASE = self.tokenizer(self.input_text , return_tensors='pt').to(0) SCREAMING_SNAKE_CASE = model.generate(**a) class _snake_case ( A__ ): def SCREAMING_SNAKE_CASE__ ( self) -> str: super().setUp() # model_name SCREAMING_SNAKE_CASE = 'bigscience/bloom-560m' SCREAMING_SNAKE_CASE = 't5-small' # Different types of model SCREAMING_SNAKE_CASE = AutoModel.from_pretrained(self.model_name , load_in_abit=a , device_map='auto') # Sequence classification model SCREAMING_SNAKE_CASE = AutoModelForSequenceClassification.from_pretrained( self.model_name , load_in_abit=a , device_map='auto') # CausalLM model SCREAMING_SNAKE_CASE = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=a , device_map='auto') # Seq2seq model SCREAMING_SNAKE_CASE = AutoModelForSeqaSeqLM.from_pretrained( self.seq_to_seq_name , load_in_abit=a , device_map='auto') def SCREAMING_SNAKE_CASE__ ( self) -> List[Any]: del self.base_model del self.sequence_model del self.model_abit del self.seq_to_seq_model gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE__ ( self) -> Optional[int]: from bitsandbytes.nn import Paramsabit self.assertTrue(self.base_model.h[-1].mlp.dense_ah_to_h.weight.__class__ == Paramsabit) # Other heads should be nn.Parameter self.assertTrue(self.model_abit.lm_head.weight.__class__ == torch.nn.Parameter) self.assertTrue(self.sequence_model.score.weight.__class__ == torch.nn.Parameter) self.assertTrue(self.seq_to_seq_model.lm_head.weight.__class__ == torch.nn.Parameter) class _snake_case ( A__ ): def SCREAMING_SNAKE_CASE__ ( self) -> Union[str, Any]: super().setUp() def SCREAMING_SNAKE_CASE__ ( self) -> Dict: del self.pipe gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE__ ( self) -> Union[str, Any]: SCREAMING_SNAKE_CASE = pipeline( 'text-generation' , model=self.model_name , model_kwargs={'device_map': 'auto', 'load_in_4bit': True, 'torch_dtype': torch.floataa} , max_new_tokens=self.MAX_NEW_TOKENS , ) # Real second forward pass SCREAMING_SNAKE_CASE = self.pipe(self.input_text) self.assertIn(pipeline_output[0]['generated_text'] , self.EXPECTED_OUTPUTS) @require_torch_multi_gpu class _snake_case ( A__ ): def SCREAMING_SNAKE_CASE__ ( self) -> int: super().setUp() def SCREAMING_SNAKE_CASE__ ( self) -> Union[str, Any]: SCREAMING_SNAKE_CASE = AutoModelForCausalLM.from_pretrained( self.model_name , load_in_abit=a , device_map='balanced') # Check correct device map self.assertEqual(set(model_parallel.hf_device_map.values()) , {0, 1}) # Check that inference pass works on the model SCREAMING_SNAKE_CASE = self.tokenizer(self.input_text , return_tensors='pt') # Second real batch SCREAMING_SNAKE_CASE = model_parallel.generate(input_ids=encoded_input['input_ids'].to(0) , max_new_tokens=10) self.assertIn(self.tokenizer.decode(output_parallel[0] , skip_special_tokens=a) , self.EXPECTED_OUTPUTS) class _snake_case ( A__ ): def SCREAMING_SNAKE_CASE__ ( self) -> Tuple: SCREAMING_SNAKE_CASE = 'facebook/opt-350m' super().setUp() def SCREAMING_SNAKE_CASE__ ( self) -> Any: if version.parse(importlib.metadata.version('bitsandbytes')) < version.parse('0.37.0'): return # Step 1: freeze all parameters SCREAMING_SNAKE_CASE = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=a) self.assertEqual(set(model.hf_device_map.values()) , {torch.cuda.current_device()}) for param in model.parameters(): SCREAMING_SNAKE_CASE = False # freeze the model - train adapters later if param.ndim == 1: # cast the small parameters (e.g. layernorm) to fp32 for stability SCREAMING_SNAKE_CASE = param.data.to(torch.floataa) # Step 2: add adapters for _, module in model.named_modules(): if "OPTAttention" in repr(type(a)): SCREAMING_SNAKE_CASE = LoRALayer(module.q_proj , rank=16) SCREAMING_SNAKE_CASE = LoRALayer(module.k_proj , rank=16) SCREAMING_SNAKE_CASE = LoRALayer(module.v_proj , rank=16) # Step 3: dummy batch SCREAMING_SNAKE_CASE = self.tokenizer('Test batch ' , return_tensors='pt').to(0) # Step 4: Check if the gradient is not None with torch.cuda.amp.autocast(): SCREAMING_SNAKE_CASE = model.forward(**a) out.logits.norm().backward() for module in model.modules(): if isinstance(a , a): self.assertTrue(module.adapter[1].weight.grad is not None) self.assertTrue(module.adapter[1].weight.grad.norm().item() > 0) elif isinstance(a , nn.Embedding): self.assertTrue(module.weight.grad is None) class _snake_case ( A__ ): _lowercase : str = '''gpt2-xl''' _lowercase : Union[str, Any] = 3.3191_8548_5415_2187
327
0
"""simple docstring""" def _snake_case ( lowercase__ : Dict ) -> List[Any]: '''simple docstring''' stooge(lowercase__ , 0 , len(lowercase__ ) - 1 ) return arr def _snake_case ( lowercase__ : List[Any] , lowercase__ : str , lowercase__ : Optional[Any] ) -> int: '''simple docstring''' if i >= h: return # If first element is smaller than the last then swap them if arr[i] > arr[h]: lowerCAmelCase_ , lowerCAmelCase_ :int = arr[h], arr[i] # If there are more than 2 elements in the array if h - i + 1 > 2: lowerCAmelCase_ :Any = (int)((h - i + 1) / 3 ) # Recursively sort first 2/3 elements stooge(lowercase__ , lowercase__ , (h - t) ) # Recursively sort last 2/3 elements stooge(lowercase__ , i + t , (lowercase__) ) # Recursively sort first 2/3 elements stooge(lowercase__ , lowercase__ , (h - t) ) if __name__ == "__main__": __UpperCAmelCase = input('Enter numbers separated by a comma:\n').strip() __UpperCAmelCase = [int(item) for item in user_input.split(',')] print(stooge_sort(unsorted))
84
'''simple docstring''' def __magic_name__( lowerCamelCase, lowerCamelCase): # "extended trapezoidal rule" # int(f) = dx/2 * (f1 + 2f2 + ... + fn) __lowerCAmelCase = (boundary[1] - boundary[0]) / steps __lowerCAmelCase = boundary[0] __lowerCAmelCase = boundary[1] __lowerCAmelCase = make_points(lowerCamelCase, lowerCamelCase, lowerCamelCase) __lowerCAmelCase = 0.0 y += (h / 2.0) * f(lowerCamelCase) for i in x_i: # print(i) y += h * f(lowerCamelCase) y += (h / 2.0) * f(lowerCamelCase) return y def __magic_name__( lowerCamelCase, lowerCamelCase, lowerCamelCase): __lowerCAmelCase = a + h while x < (b - h): yield x __lowerCAmelCase = x + h def __magic_name__( lowerCamelCase): # enter your function here __lowerCAmelCase = (x - 0) * (x - 0) return y def __magic_name__( ): __lowerCAmelCase = 0.0 # Lower bound of integration __lowerCAmelCase = 1.0 # Upper bound of integration __lowerCAmelCase = 10.0 # define number of steps or resolution __lowerCAmelCase = [a, b] # define boundary of integration __lowerCAmelCase = method_a(lowerCamelCase, lowerCamelCase) print(F"""y = {y}""") if __name__ == "__main__": main()
174
0
import warnings from ...utils import logging from .image_processing_videomae import VideoMAEImageProcessor lowerCAmelCase__ = logging.get_logger(__name__) class _lowerCamelCase ( _lowercase ): def __init__(self , *__a , **__a ) -> None: warnings.warn( "The class VideoMAEFeatureExtractor is deprecated and will be removed in version 5 of Transformers." " Please use VideoMAEImageProcessor instead." , _snake_case , ) super().__init__(*_snake_case , **_snake_case )
361
"""simple docstring""" import sacrebleu as scb from packaging import version from sacrebleu import CHRF import datasets lowerCAmelCase__ = '''\ @inproceedings{popovic-2015-chrf, title = "chr{F}: character n-gram {F}-score for automatic {MT} evaluation", author = "Popovi{\'c}, Maja", booktitle = "Proceedings of the Tenth Workshop on Statistical Machine Translation", month = sep, year = "2015", address = "Lisbon, Portugal", publisher = "Association for Computational Linguistics", url = "https://aclanthology.org/W15-3049", doi = "10.18653/v1/W15-3049", pages = "392--395", } @inproceedings{popovic-2017-chrf, title = "chr{F}++: words helping character n-grams", author = "Popovi{\'c}, Maja", booktitle = "Proceedings of the Second Conference on Machine Translation", month = sep, year = "2017", address = "Copenhagen, Denmark", publisher = "Association for Computational Linguistics", url = "https://aclanthology.org/W17-4770", doi = "10.18653/v1/W17-4770", pages = "612--618", } @inproceedings{post-2018-call, title = "A Call for Clarity in Reporting {BLEU} Scores", author = "Post, Matt", booktitle = "Proceedings of the Third Conference on Machine Translation: Research Papers", month = oct, year = "2018", address = "Belgium, Brussels", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/W18-6319", pages = "186--191", } ''' lowerCAmelCase__ = '''\ ChrF and ChrF++ are two MT evaluation metrics. They both use the F-score statistic for character n-gram matches, and ChrF++ adds word n-grams as well which correlates more strongly with direct assessment. We use the implementation that is already present in sacrebleu. The implementation here is slightly different from sacrebleu in terms of the required input format. The length of the references and hypotheses lists need to be the same, so you may need to transpose your references compared to sacrebleu\'s required input format. See https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534 See the README.md file at https://github.com/mjpost/sacreBLEU#chrf--chrf for more information. ''' lowerCAmelCase__ = ''' Produces ChrF(++) scores for hypotheses given reference translations. Args: predictions (list of str): The predicted sentences. references (list of list of str): The references. There should be one reference sub-list for each prediction sentence. char_order (int): Character n-gram order. Defaults to `6`. word_order (int): Word n-gram order. If equals to `2`, the metric is referred to as chrF++. Defaults to `0`. beta (int): Determine the importance of recall w.r.t precision. Defaults to `2`. lowercase (bool): if `True`, enables case-insensitivity. Defaults to `False`. whitespace (bool): If `True`, include whitespaces when extracting character n-grams. eps_smoothing (bool): If `True`, applies epsilon smoothing similar to reference chrF++.py, NLTK and Moses implementations. If `False`, it takes into account effective match order similar to sacreBLEU < 2.0.0. Defaults to `False`. Returns: \'score\' (float): The chrF (chrF++) score, \'char_order\' (int): The character n-gram order, \'word_order\' (int): The word n-gram order. If equals to 2, the metric is referred to as chrF++, \'beta\' (int): Determine the importance of recall w.r.t precision Examples: Example 1--a simple example of calculating chrF: >>> prediction = ["The relationship between cats and dogs is not exactly friendly.", "a good bookshop is just a genteel black hole that knows how to read."] >>> reference = [["The relationship between dogs and cats is not exactly friendly."], ["A good bookshop is just a genteel Black Hole that knows how to read."]] >>> chrf = datasets.load_metric("chrf") >>> results = chrf.compute(predictions=prediction, references=reference) >>> print(results) {\'score\': 84.64214891738334, \'char_order\': 6, \'word_order\': 0, \'beta\': 2} Example 2--the same example, but with the argument word_order=2, to calculate chrF++ instead of chrF: >>> prediction = ["The relationship between cats and dogs is not exactly friendly.", "a good bookshop is just a genteel black hole that knows how to read."] >>> reference = [["The relationship between dogs and cats is not exactly friendly."], ["A good bookshop is just a genteel Black Hole that knows how to read."]] >>> chrf = datasets.load_metric("chrf") >>> results = chrf.compute(predictions=prediction, ... references=reference, ... word_order=2) >>> print(results) {\'score\': 82.87263732906315, \'char_order\': 6, \'word_order\': 2, \'beta\': 2} Example 3--the same chrF++ example as above, but with `lowercase=True` to normalize all case: >>> prediction = ["The relationship between cats and dogs is not exactly friendly.", "a good bookshop is just a genteel black hole that knows how to read."] >>> reference = [["The relationship between dogs and cats is not exactly friendly."], ["A good bookshop is just a genteel Black Hole that knows how to read."]] >>> chrf = datasets.load_metric("chrf") >>> results = chrf.compute(predictions=prediction, ... references=reference, ... word_order=2, ... lowercase=True) >>> print(results) {\'score\': 92.12853119829202, \'char_order\': 6, \'word_order\': 2, \'beta\': 2} ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _lowerCamelCase ( datasets.Metric ): def snake_case_ (self ) -> Tuple: if version.parse(scb.__version__ ) < version.parse("1.4.12" ): raise ImportWarning( "To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn't match this condition.\n" "You can install it with `pip install \"sacrebleu>=1.4.12\"`." ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage="https://github.com/mjpost/sacreBLEU#chrf--chrf" , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Sequence(datasets.Value("string" , id="sequence" ) , id="references" ), } ) , codebase_urls=["https://github.com/mjpost/sacreBLEU#chrf--chrf"] , reference_urls=[ "https://github.com/m-popovic/chrF", ] , ) def snake_case_ (self , __a , __a , __a = CHRF.CHAR_ORDER , __a = CHRF.WORD_ORDER , __a = CHRF.BETA , __a = False , __a = False , __a = False , ) -> Tuple: UpperCamelCase = len(references[0] ) if any(len(__a ) != references_per_prediction for refs in references ): raise ValueError("Sacrebleu requires the same number of references for each prediction" ) UpperCamelCase = [[refs[i] for refs in references] for i in range(__a )] UpperCamelCase = CHRF(__a , __a , __a , __a , __a , __a ) UpperCamelCase = sb_chrf.corpus_score(__a , __a ) return { "score": output.score, "char_order": output.char_order, "word_order": output.word_order, "beta": output.beta, }
244
0
from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case = logging.get_logger(__name__) __snake_case = { """microsoft/trocr-base-handwritten""": ( """https://huggingface.co/microsoft/trocr-base-handwritten/resolve/main/config.json""" ), # See all TrOCR models at https://huggingface.co/models?filter=trocr } class lowercase__ ( _UpperCAmelCase ): A__ : Tuple ="""trocr""" A__ : Tuple =["""past_key_values"""] A__ : Optional[int] ={ """num_attention_heads""": """decoder_attention_heads""", """hidden_size""": """d_model""", """num_hidden_layers""": """decoder_layers""", } def __init__( self : List[Any] , UpperCAmelCase_ : List[str]=50265 , UpperCAmelCase_ : int=1024 , UpperCAmelCase_ : Optional[int]=12 , UpperCAmelCase_ : Union[str, Any]=16 , UpperCAmelCase_ : Any=4096 , UpperCAmelCase_ : List[Any]="gelu" , UpperCAmelCase_ : Tuple=512 , UpperCAmelCase_ : Optional[Any]=0.1 , UpperCAmelCase_ : Optional[int]=0.0 , UpperCAmelCase_ : List[str]=0.0 , UpperCAmelCase_ : int=2 , UpperCAmelCase_ : Dict=0.02 , UpperCAmelCase_ : List[Any]=0.0 , UpperCAmelCase_ : str=True , UpperCAmelCase_ : Tuple=False , UpperCAmelCase_ : int=True , UpperCAmelCase_ : int=True , UpperCAmelCase_ : str=1 , UpperCAmelCase_ : Any=0 , UpperCAmelCase_ : Optional[Any]=2 , **UpperCAmelCase_ : Tuple , ): SCREAMING_SNAKE_CASE__ = vocab_size SCREAMING_SNAKE_CASE__ = d_model SCREAMING_SNAKE_CASE__ = decoder_layers SCREAMING_SNAKE_CASE__ = decoder_attention_heads SCREAMING_SNAKE_CASE__ = decoder_ffn_dim SCREAMING_SNAKE_CASE__ = activation_function SCREAMING_SNAKE_CASE__ = max_position_embeddings SCREAMING_SNAKE_CASE__ = dropout SCREAMING_SNAKE_CASE__ = attention_dropout SCREAMING_SNAKE_CASE__ = activation_dropout SCREAMING_SNAKE_CASE__ = init_std SCREAMING_SNAKE_CASE__ = decoder_layerdrop SCREAMING_SNAKE_CASE__ = use_cache SCREAMING_SNAKE_CASE__ = scale_embedding SCREAMING_SNAKE_CASE__ = use_learned_position_embeddings SCREAMING_SNAKE_CASE__ = layernorm_embedding super().__init__( pad_token_id=UpperCAmelCase_ , bos_token_id=UpperCAmelCase_ , eos_token_id=UpperCAmelCase_ , decoder_start_token_id=UpperCAmelCase_ , **UpperCAmelCase_ , )
176
import json from typing import List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_bart import BartTokenizer __snake_case = logging.get_logger(__name__) __snake_case = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""} # See all BART models at https://huggingface.co/models?filter=bart __snake_case = { """vocab_file""": { """facebook/bart-base""": """https://huggingface.co/facebook/bart-base/resolve/main/vocab.json""", """facebook/bart-large""": """https://huggingface.co/facebook/bart-large/resolve/main/vocab.json""", """facebook/bart-large-mnli""": """https://huggingface.co/facebook/bart-large-mnli/resolve/main/vocab.json""", """facebook/bart-large-cnn""": """https://huggingface.co/facebook/bart-large-cnn/resolve/main/vocab.json""", """facebook/bart-large-xsum""": """https://huggingface.co/facebook/bart-large-xsum/resolve/main/vocab.json""", """yjernite/bart_eli5""": """https://huggingface.co/yjernite/bart_eli5/resolve/main/vocab.json""", }, """merges_file""": { """facebook/bart-base""": """https://huggingface.co/facebook/bart-base/resolve/main/merges.txt""", """facebook/bart-large""": """https://huggingface.co/facebook/bart-large/resolve/main/merges.txt""", """facebook/bart-large-mnli""": """https://huggingface.co/facebook/bart-large-mnli/resolve/main/merges.txt""", """facebook/bart-large-cnn""": """https://huggingface.co/facebook/bart-large-cnn/resolve/main/merges.txt""", """facebook/bart-large-xsum""": """https://huggingface.co/facebook/bart-large-xsum/resolve/main/merges.txt""", """yjernite/bart_eli5""": """https://huggingface.co/yjernite/bart_eli5/resolve/main/merges.txt""", }, """tokenizer_file""": { """facebook/bart-base""": """https://huggingface.co/facebook/bart-base/resolve/main/tokenizer.json""", """facebook/bart-large""": """https://huggingface.co/facebook/bart-large/resolve/main/tokenizer.json""", """facebook/bart-large-mnli""": """https://huggingface.co/facebook/bart-large-mnli/resolve/main/tokenizer.json""", """facebook/bart-large-cnn""": """https://huggingface.co/facebook/bart-large-cnn/resolve/main/tokenizer.json""", """facebook/bart-large-xsum""": """https://huggingface.co/facebook/bart-large-xsum/resolve/main/tokenizer.json""", """yjernite/bart_eli5""": """https://huggingface.co/yjernite/bart_eli5/resolve/main/tokenizer.json""", }, } __snake_case = { """facebook/bart-base""": 10_24, """facebook/bart-large""": 10_24, """facebook/bart-large-mnli""": 10_24, """facebook/bart-large-cnn""": 10_24, """facebook/bart-large-xsum""": 10_24, """yjernite/bart_eli5""": 10_24, } class lowercase__ ( _UpperCAmelCase ): A__ : Tuple =VOCAB_FILES_NAMES A__ : Any =PRETRAINED_VOCAB_FILES_MAP A__ : str =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A__ : Tuple =["""input_ids""", """attention_mask"""] A__ : Optional[int] =BartTokenizer def __init__( self : str , UpperCAmelCase_ : Optional[Any]=None , UpperCAmelCase_ : int=None , UpperCAmelCase_ : str=None , UpperCAmelCase_ : Optional[Any]="replace" , UpperCAmelCase_ : int="<s>" , UpperCAmelCase_ : List[Any]="</s>" , UpperCAmelCase_ : int="</s>" , UpperCAmelCase_ : Tuple="<s>" , UpperCAmelCase_ : Any="<unk>" , UpperCAmelCase_ : Any="<pad>" , UpperCAmelCase_ : Optional[Any]="<mask>" , UpperCAmelCase_ : Optional[int]=False , UpperCAmelCase_ : List[str]=True , **UpperCAmelCase_ : List[Any] , ): super().__init__( UpperCAmelCase_ , UpperCAmelCase_ , tokenizer_file=UpperCAmelCase_ , errors=UpperCAmelCase_ , bos_token=UpperCAmelCase_ , eos_token=UpperCAmelCase_ , sep_token=UpperCAmelCase_ , cls_token=UpperCAmelCase_ , unk_token=UpperCAmelCase_ , pad_token=UpperCAmelCase_ , mask_token=UpperCAmelCase_ , add_prefix_space=UpperCAmelCase_ , trim_offsets=UpperCAmelCase_ , **UpperCAmelCase_ , ) SCREAMING_SNAKE_CASE__ = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('add_prefix_space' , UpperCAmelCase_ ) != add_prefix_space: SCREAMING_SNAKE_CASE__ = getattr(UpperCAmelCase_ , pre_tok_state.pop('type' ) ) SCREAMING_SNAKE_CASE__ = add_prefix_space SCREAMING_SNAKE_CASE__ = pre_tok_class(**UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = add_prefix_space # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__` SCREAMING_SNAKE_CASE__ = 'post_processor' SCREAMING_SNAKE_CASE__ = getattr(self.backend_tokenizer , UpperCAmelCase_ , UpperCAmelCase_ ) if tokenizer_component_instance: SCREAMING_SNAKE_CASE__ = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: SCREAMING_SNAKE_CASE__ = tuple(state['sep'] ) if "cls" in state: SCREAMING_SNAKE_CASE__ = tuple(state['cls'] ) SCREAMING_SNAKE_CASE__ = False if state.get('add_prefix_space' , UpperCAmelCase_ ) != add_prefix_space: SCREAMING_SNAKE_CASE__ = add_prefix_space SCREAMING_SNAKE_CASE__ = True if state.get('trim_offsets' , UpperCAmelCase_ ) != trim_offsets: SCREAMING_SNAKE_CASE__ = trim_offsets SCREAMING_SNAKE_CASE__ = True if changes_to_apply: SCREAMING_SNAKE_CASE__ = getattr(UpperCAmelCase_ , state.pop('type' ) ) SCREAMING_SNAKE_CASE__ = component_class(**UpperCAmelCase_ ) setattr(self.backend_tokenizer , UpperCAmelCase_ , UpperCAmelCase_ ) @property def A_ ( self : Tuple ): if self._mask_token is None: if self.verbose: logger.error('Using mask_token, but it is not set yet.' ) return None return str(self._mask_token ) @mask_token.setter def A_ ( self : Any , UpperCAmelCase_ : List[Any] ): SCREAMING_SNAKE_CASE__ = AddedToken(UpperCAmelCase_ , lstrip=UpperCAmelCase_ , rstrip=UpperCAmelCase_ ) if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) else value SCREAMING_SNAKE_CASE__ = value def A_ ( self : List[str] , *UpperCAmelCase_ : Tuple , **UpperCAmelCase_ : str ): SCREAMING_SNAKE_CASE__ = kwargs.get('is_split_into_words' , UpperCAmelCase_ ) if is_split_into_words and not self.add_prefix_space: raise ValueError( F'You need to instantiate {self.__class__.__name__} with add_prefix_space=True ' 'to use it with pretokenized inputs.' ) return super()._batch_encode_plus(*UpperCAmelCase_ , **UpperCAmelCase_ ) def A_ ( self : List[str] , *UpperCAmelCase_ : Dict , **UpperCAmelCase_ : int ): SCREAMING_SNAKE_CASE__ = kwargs.get('is_split_into_words' , UpperCAmelCase_ ) if is_split_into_words and not self.add_prefix_space: raise ValueError( F'You need to instantiate {self.__class__.__name__} with add_prefix_space=True ' 'to use it with pretokenized inputs.' ) return super()._encode_plus(*UpperCAmelCase_ , **UpperCAmelCase_ ) def A_ ( self : List[str] , UpperCAmelCase_ : str , UpperCAmelCase_ : Optional[str] = None ): SCREAMING_SNAKE_CASE__ = self._tokenizer.model.save(UpperCAmelCase_ , name=UpperCAmelCase_ ) return tuple(UpperCAmelCase_ ) def A_ ( self : Optional[Any] , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Union[str, Any]=None ): SCREAMING_SNAKE_CASE__ = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def A_ ( self : str , UpperCAmelCase_ : List[int] , UpperCAmelCase_ : Optional[List[int]] = None ): SCREAMING_SNAKE_CASE__ = [self.sep_token_id] SCREAMING_SNAKE_CASE__ = [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]
176
1
"""simple docstring""" import os import re import warnings from shutil import copyfile from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer if TYPE_CHECKING: from ...tokenization_utils_base import TextInput from ...utils import logging _lowerCAmelCase :List[Any] = logging.get_logger(__name__) _lowerCAmelCase :int = {'vocab_file': 'spiece.model'} _lowerCAmelCase :Dict = { 'vocab_file': { 't5-small': 'https://huggingface.co/t5-small/resolve/main/spiece.model', 't5-base': 'https://huggingface.co/t5-base/resolve/main/spiece.model', 't5-large': 'https://huggingface.co/t5-large/resolve/main/spiece.model', 't5-3b': 'https://huggingface.co/t5-3b/resolve/main/spiece.model', 't5-11b': 'https://huggingface.co/t5-11b/resolve/main/spiece.model', } } # TODO(PVP) - this should be removed in Transformers v5 _lowerCAmelCase :List[str] = { 't5-small': 512, 't5-base': 512, 't5-large': 512, 't5-3b': 512, 't5-11b': 512, } _lowerCAmelCase :List[Any] = '▁' class _UpperCAmelCase ( a ): '''simple docstring''' a__ : Optional[Any] =VOCAB_FILES_NAMES a__ : Any =PRETRAINED_VOCAB_FILES_MAP a__ : Union[str, Any] =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES a__ : int =['''input_ids''', '''attention_mask'''] def __init__( self , A , A="</s>" , A="<unk>" , A="<pad>" , A=1_0_0 , A=None , A = None , A=True , **A , ) -> None: # Add extra_ids to the special token list if extra_ids > 0 and additional_special_tokens is None: _UpperCAmelCase : List[Any] = [f'<extra_id_{i}>' for i in range(A )] elif extra_ids > 0 and additional_special_tokens is not None: # Check that we have the right number of extra_id special tokens _UpperCAmelCase : Dict = len(set(filter(lambda A : bool('''extra_id''' in str(A ) ) , A ) ) ) if extra_tokens != extra_ids: raise ValueError( f'Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are' ''' provided to T5Tokenizer. In this case the additional_special_tokens must include the extra_ids''' ''' tokens''' ) if legacy: logger.warning_once( f'You are using the legacy behaviour of the {self.__class__}. This means that tokens that come after special tokens will not be properly handled. We recommend you to' ''' read the related pull request available at https://github.com/huggingface/transformers/pull/24565''' ) _UpperCAmelCase : Tuple = legacy _UpperCAmelCase : Dict = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( eos_token=A , unk_token=A , pad_token=A , extra_ids=A , additional_special_tokens=A , sp_model_kwargs=self.sp_model_kwargs , legacy=A , **A , ) _UpperCAmelCase : List[Any] = vocab_file _UpperCAmelCase : Optional[Any] = extra_ids _UpperCAmelCase : Tuple = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(A ) @staticmethod def __lowerCAmelCase ( A , A , A ) -> Optional[int]: if pretrained_model_name_or_path in TaTokenizer.max_model_input_sizes: _UpperCAmelCase : Dict = TaTokenizer.max_model_input_sizes[pretrained_model_name_or_path] if init_max_model_length is not None and init_max_model_length != max_model_length: return init_max_model_length elif init_max_model_length is None: warnings.warn( '''This tokenizer was incorrectly instantiated with a model max length of''' f' {deprecated_max_model_length} which will be corrected in Transformers v5.\nFor now, this' ''' behavior is kept to avoid breaking backwards compatibility when padding/encoding with''' ''' `truncation is True`.\n- Be aware that you SHOULD NOT rely on''' f' {pretrained_model_name_or_path} automatically truncating your input to' f' {deprecated_max_model_length} when padding/encoding.\n- If you want to encode/pad to sequences' f' longer than {deprecated_max_model_length} you can either instantiate this tokenizer with' ''' `model_max_length` or pass `max_length` when encoding/padding.\n- To avoid this warning, please''' ''' instantiate this tokenizer with `model_max_length` set to your preferred value.''' , A , ) return max_model_length @property def __lowerCAmelCase ( self ) -> str: return self.sp_model.get_piece_size() + self._extra_ids def __lowerCAmelCase ( self ) -> Dict: _UpperCAmelCase : List[Any] = {self.convert_ids_to_tokens(A ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __lowerCAmelCase ( self , A , A = None , A = False ) -> List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=A , token_ids_a=A , already_has_special_tokens=A ) # normal case: some special tokens if token_ids_a is None: return ([0] * len(A )) + [1] return ([0] * len(A )) + [1] + ([0] * len(A )) + [1] def __lowerCAmelCase ( self ) -> str: return list( set(filter(lambda A : bool(re.search(r'''<extra_id_\d+>''' , A ) ) is not None , self.additional_special_tokens ) ) ) def __lowerCAmelCase ( self ) -> Tuple: return [self._convert_token_to_id(A ) for token in self.get_sentinel_tokens()] def __lowerCAmelCase ( self , A ) -> List[int]: if len(A ) > 0 and token_ids[-1] == self.eos_token_id: warnings.warn( f'This sequence already has {self.eos_token}. In future versions this behavior may lead to duplicated' ''' eos tokens being added.''' ) return token_ids else: return token_ids + [self.eos_token_id] def __lowerCAmelCase ( self , A , A = None ) -> List[int]: _UpperCAmelCase : Union[str, Any] = [self.eos_token_id] if token_ids_a is None: return len(token_ids_a + eos ) * [0] return len(token_ids_a + eos + token_ids_a + eos ) * [0] def __lowerCAmelCase ( self , A , A = None ) -> List[int]: _UpperCAmelCase : Tuple = self._add_eos_if_not_present(A ) if token_ids_a is None: return token_ids_a else: _UpperCAmelCase : Optional[Any] = self._add_eos_if_not_present(A ) return token_ids_a + token_ids_a def __getstate__( self ) -> str: _UpperCAmelCase : Union[str, Any] = self.__dict__.copy() _UpperCAmelCase : Tuple = None return state def __setstate__( self , A ) -> Optional[Any]: _UpperCAmelCase : Union[str, Any] = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): _UpperCAmelCase : Any = {} _UpperCAmelCase : Tuple = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __lowerCAmelCase ( self , A , **A ) -> List[str]: # Replace the SPIECE_UNDERLINE with a space to make sure SPIECE_UNDERLINE is only used at # the beginning of the text if not self.legacy: _UpperCAmelCase : List[str] = SPIECE_UNDERLINE + text.replace(A , ''' ''' ) return super().tokenize(A , **A ) def __lowerCAmelCase ( self , A , **A ) -> str: if not self.legacy: _UpperCAmelCase : Optional[int] = text.startswith(A ) if is_first: _UpperCAmelCase : List[str] = text[1:] _UpperCAmelCase : Dict = self.sp_model.encode(A , out_type=A ) if not self.legacy and not is_first and not text.startswith(''' ''' ) and tokens[0].startswith(A ): _UpperCAmelCase : List[str] = ([tokens[0][1:]] if len(tokens[0] ) > 1 else []) + tokens[1:] return tokens def __lowerCAmelCase ( self , A ) -> str: if token.startswith('''<extra_id_''' ): _UpperCAmelCase : Union[str, Any] = re.match(r'''<extra_id_(\d+)>''' , A ) _UpperCAmelCase : Optional[int] = int(match.group(1 ) ) return self.vocab_size - num - 1 return self.sp_model.piece_to_id(A ) def __lowerCAmelCase ( self , A ) -> str: if index < self.sp_model.get_piece_size(): _UpperCAmelCase : Dict = self.sp_model.IdToPiece(A ) else: _UpperCAmelCase : Dict = f'<extra_id_{self.vocab_size - 1 - index}>' return token def __lowerCAmelCase ( self , A ) -> str: _UpperCAmelCase : Dict = [] _UpperCAmelCase : Dict = '''''' _UpperCAmelCase : Union[str, Any] = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(A ) + token _UpperCAmelCase : Dict = True _UpperCAmelCase : str = [] else: current_sub_tokens.append(A ) _UpperCAmelCase : Optional[int] = False out_string += self.sp_model.decode(A ) return out_string.strip() def __lowerCAmelCase ( self , A , A = None ) -> Tuple[str]: if not os.path.isdir(A ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return _UpperCAmelCase : Union[str, Any] = os.path.join( A , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(A ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , A ) elif not os.path.isfile(self.vocab_file ): with open(A , '''wb''' ) as fi: _UpperCAmelCase : Tuple = self.sp_model.serialized_model_proto() fi.write(A ) return (out_vocab_file,)
362
"""simple docstring""" import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from transformers.activations import gelu_new, gelu_python, get_activation @require_torch class _UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self ) -> Any: _UpperCAmelCase : List[str] = torch.tensor([-1_0_0, -1, -0.1, 0, 0.1, 1.0, 1_0_0] ) _UpperCAmelCase : Optional[Any] = get_activation('''gelu''' ) self.assertTrue(torch.allclose(gelu_python(A ) , torch_builtin(A ) ) ) self.assertFalse(torch.allclose(gelu_python(A ) , gelu_new(A ) ) ) def __lowerCAmelCase ( self ) -> str: _UpperCAmelCase : Union[str, Any] = torch.tensor([-1_0_0, -1, -0.1, 0, 0.1, 1.0, 1_0_0] ) _UpperCAmelCase : Tuple = get_activation('''gelu''' ) _UpperCAmelCase : int = get_activation('''gelu_10''' ) _UpperCAmelCase : Optional[int] = torch_builtin(A ) _UpperCAmelCase : Optional[int] = geluaa(A ) _UpperCAmelCase : Optional[Any] = torch.where(y_gelu_aa < 10.0 , 1 , 0 ) self.assertTrue(torch.max(A ).item() == 10.0 ) self.assertTrue(torch.allclose(y_gelu * clipped_mask , y_gelu_aa * clipped_mask ) ) def __lowerCAmelCase ( self ) -> Optional[Any]: get_activation('''gelu''' ) get_activation('''gelu_10''' ) get_activation('''gelu_fast''' ) get_activation('''gelu_new''' ) get_activation('''gelu_python''' ) get_activation('''gelu_pytorch_tanh''' ) get_activation('''linear''' ) get_activation('''mish''' ) get_activation('''quick_gelu''' ) get_activation('''relu''' ) get_activation('''sigmoid''' ) get_activation('''silu''' ) get_activation('''swish''' ) get_activation('''tanh''' ) with self.assertRaises(A ): get_activation('''bogus''' ) with self.assertRaises(A ): get_activation(A ) def __lowerCAmelCase ( self ) -> Tuple: _UpperCAmelCase : Dict = get_activation('''gelu''' ) _UpperCAmelCase : List[Any] = 1 _UpperCAmelCase : int = get_activation('''gelu''' ) self.assertEqual(acta.a , 1 ) with self.assertRaises(A ): _UpperCAmelCase : Any = acta.a
68
0
from __future__ import annotations def _lowerCAmelCase ( lowerCAmelCase_ :float , lowerCAmelCase_ :float , lowerCAmelCase_ :float )->float: '''simple docstring''' if days_between_payments <= 0: raise ValueError("days_between_payments must be > 0" ) if daily_interest_rate < 0: raise ValueError("daily_interest_rate must be >= 0" ) if principal <= 0: raise ValueError("principal must be > 0" ) return principal * daily_interest_rate * days_between_payments def _lowerCAmelCase ( lowerCAmelCase_ :float , lowerCAmelCase_ :float , lowerCAmelCase_ :float , )->float: '''simple docstring''' if number_of_compounding_periods <= 0: raise ValueError("number_of_compounding_periods must be > 0" ) if nominal_annual_interest_rate_percentage < 0: raise ValueError("nominal_annual_interest_rate_percentage must be >= 0" ) if principal <= 0: raise ValueError("principal must be > 0" ) return principal * ( (1 + nominal_annual_interest_rate_percentage) ** number_of_compounding_periods - 1 ) def _lowerCAmelCase ( lowerCAmelCase_ :float , lowerCAmelCase_ :float , lowerCAmelCase_ :float , )->float: '''simple docstring''' if number_of_years <= 0: raise ValueError("number_of_years must be > 0" ) if nominal_annual_percentage_rate < 0: raise ValueError("nominal_annual_percentage_rate must be >= 0" ) if principal <= 0: raise ValueError("principal must be > 0" ) return compound_interest( lowerCAmelCase_ , nominal_annual_percentage_rate / 365 , number_of_years * 365 ) if __name__ == "__main__": import doctest doctest.testmod()
159
from ...utils import ( OptionalDependencyNotAvailable, is_flax_available, is_torch_available, is_transformers_available, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .multicontrolnet import MultiControlNetModel from .pipeline_controlnet import StableDiffusionControlNetPipeline from .pipeline_controlnet_imgaimg import StableDiffusionControlNetImgaImgPipeline from .pipeline_controlnet_inpaint import StableDiffusionControlNetInpaintPipeline if is_transformers_available() and is_flax_available(): from .pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline
159
1
import os # noqa: this is just for tests import os as renamed_os # noqa: this is just for tests from os import path # noqa: this is just for tests from os import path as renamed_path # noqa: this is just for tests from os.path import join # noqa: this is just for tests from os.path import join as renamed_join # noqa: this is just for tests lowerCAmelCase = open # noqa: we just need to have a builtin inside this module to test it properly
357
from typing import Any import numpy as np def _lowerCamelCase( lowercase__ ) -> bool: '''simple docstring''' return np.array_equal(lowercase__ , matrix.conjugate().T ) def _lowerCamelCase( lowercase__ , lowercase__ ) -> Any: '''simple docstring''' __lowercase= v.conjugate().T __lowercase= v_star.dot(lowercase__ ) assert isinstance(lowercase__ , np.ndarray ) return (v_star_dot.dot(lowercase__ )) / (v_star.dot(lowercase__ )) def _lowerCamelCase( ) -> None: '''simple docstring''' __lowercase= np.array([[2, 2 + 1j, 4], [2 - 1j, 3, 1j], [4, -1j, 1]] ) __lowercase= np.array([[1], [2], [3]] ) assert is_hermitian(lowercase__ ), F'{a} is not hermitian.' print(rayleigh_quotient(lowercase__ , lowercase__ ) ) __lowercase= np.array([[1, 2, 4], [2, 3, -1], [4, -1, 1]] ) assert is_hermitian(lowercase__ ), F'{a} is not hermitian.' assert rayleigh_quotient(lowercase__ , lowercase__ ) == float(3 ) if __name__ == "__main__": import doctest doctest.testmod() tests()
304
0
"""simple docstring""" def lowercase ( __snake_case : int = 1_0_0_0 ): return sum(2 * a * ((a - 1) // 2) for a in range(3 , n + 1 ) ) if __name__ == "__main__": print(solution())
33
"""simple docstring""" import argparse from transformers import CLIPImageProcessor, CLIPVisionModelWithProjection from diffusers import UnCLIPImageVariationPipeline, UnCLIPPipeline if __name__ == "__main__": __A : str = argparse.ArgumentParser() parser.add_argument('''--dump_path''', default=None, type=str, required=True, help='''Path to the output model.''') parser.add_argument( '''--txt2img_unclip''', default='''kakaobrain/karlo-v1-alpha''', type=str, required=False, help='''The pretrained txt2img unclip.''', ) __A : str = parser.parse_args() __A : List[Any] = UnCLIPPipeline.from_pretrained(args.txtaimg_unclip) __A : Dict = CLIPImageProcessor() __A : Union[str, Any] = CLIPVisionModelWithProjection.from_pretrained('''openai/clip-vit-large-patch14''') __A : List[str] = UnCLIPImageVariationPipeline( decoder=txtaimg.decoder, text_encoder=txtaimg.text_encoder, tokenizer=txtaimg.tokenizer, text_proj=txtaimg.text_proj, feature_extractor=feature_extractor, image_encoder=image_encoder, super_res_first=txtaimg.super_res_first, super_res_last=txtaimg.super_res_last, decoder_scheduler=txtaimg.decoder_scheduler, super_res_scheduler=txtaimg.super_res_scheduler, ) imgaimg.save_pretrained(args.dump_path)
33
1
"""simple docstring""" import importlib.util import json import os import warnings from dataclasses import dataclass, field import torch from ..training_args import TrainingArguments from ..utils import cached_property, is_sagemaker_dp_enabled, logging __lowerCAmelCase = logging.get_logger(__name__) def snake_case_ ( ) -> Optional[int]: # Get the sagemaker specific mp parameters from smp_options variable. lowercase__: Tuple = os.getenv('SM_HP_MP_PARAMETERS' , '{}' ) try: # Parse it and check the field "partitions" is included, it is required for model parallel. lowercase__: str = json.loads(snake_case ) if "partitions" not in smp_options: return False except json.JSONDecodeError: return False # Get the sagemaker specific framework parameters from mpi_options variable. lowercase__: Dict = os.getenv('SM_FRAMEWORK_PARAMS' , '{}' ) try: # Parse it and check the field "sagemaker_distributed_dataparallel_enabled". lowercase__: int = json.loads(snake_case ) if not mpi_options.get('sagemaker_mpi_enabled' , snake_case ): return False except json.JSONDecodeError: return False # Lastly, check if the `smdistributed` module is present. return importlib.util.find_spec('smdistributed' ) is not None if is_sagemaker_model_parallel_available(): import smdistributed.modelparallel.torch as smp smp.init() @dataclass class __a ( __UpperCamelCase ): __lowercase : str = field( default='' , metadata={'help': 'Used by the SageMaker launcher to send mp-specific args. Ignored in SageMakerTrainer'} , ) def SCREAMING_SNAKE_CASE__ ( self ) -> Union[str, Any]: '''simple docstring''' super().__post_init__() warnings.warn( '`SageMakerTrainingArguments` is deprecated and will be removed in v5 of Transformers. You can use ' '`TrainingArguments` instead.' , lowerCAmelCase__ , ) @cached_property def SCREAMING_SNAKE_CASE__ ( self ) -> "torch.device": '''simple docstring''' logger.info('PyTorch: setting up devices' ) if torch.distributed.is_available() and torch.distributed.is_initialized() and self.local_rank == -1: logger.warning( 'torch.distributed process group is initialized, but local_rank == -1. ' 'In order to use Torch DDP, launch your script with `python -m torch.distributed.launch' ) if self.no_cuda: lowercase__: Union[str, Any] = torch.device('cpu' ) lowercase__: List[str] = 0 elif is_sagemaker_model_parallel_available(): lowercase__: Optional[Any] = smp.local_rank() lowercase__: List[Any] = torch.device('cuda' , lowerCAmelCase__ ) lowercase__: str = 1 elif is_sagemaker_dp_enabled(): import smdistributed.dataparallel.torch.torch_smddp # noqa: F401 torch.distributed.init_process_group(backend='smddp' , timeout=self.ddp_timeout_delta ) lowercase__: Dict = int(os.getenv('SMDATAPARALLEL_LOCAL_RANK' ) ) lowercase__: Dict = torch.device('cuda' , self.local_rank ) lowercase__: Optional[int] = 1 elif self.local_rank == -1: # if n_gpu is > 1 we'll use nn.DataParallel. # If you only want to use a specific subset of GPUs use `CUDA_VISIBLE_DEVICES=0` # Explicitly set CUDA to the first (index 0) CUDA device, otherwise `set_device` will # trigger an error that a device index is missing. Index 0 takes into account the # GPUs available in the environment, so `CUDA_VISIBLE_DEVICES=1,2` with `cuda:0` # will use the first GPU in that env, i.e. GPU#1 lowercase__: Dict = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu' ) # Sometimes the line in the postinit has not been run before we end up here, so just checking we're not at # the default value. lowercase__: List[Any] = torch.cuda.device_count() else: # Here, we'll use torch.distributed. # Initializes the distributed backend which will take care of synchronizing nodes/GPUs if not torch.distributed.is_initialized(): torch.distributed.init_process_group(backend='nccl' , timeout=self.ddp_timeout_delta ) lowercase__: Optional[int] = torch.device('cuda' , self.local_rank ) lowercase__: Optional[int] = 1 if device.type == "cuda": torch.cuda.set_device(lowerCAmelCase__ ) return device @property def SCREAMING_SNAKE_CASE__ ( self ) -> int: '''simple docstring''' if is_sagemaker_model_parallel_available(): return smp.dp_size() return super().world_size @property def SCREAMING_SNAKE_CASE__ ( self ) -> Dict: '''simple docstring''' return not is_sagemaker_model_parallel_available() @property def SCREAMING_SNAKE_CASE__ ( self ) -> int: '''simple docstring''' return False
361
import torch from diffusers import StableDiffusionPipeline __lowerCAmelCase = '''path-to-your-trained-model''' __lowerCAmelCase = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.floataa).to('''cuda''') __lowerCAmelCase = '''A photo of sks dog in a bucket''' __lowerCAmelCase = pipe(prompt, num_inference_steps=50, guidance_scale=7.5).images[0] image.save('''dog-bucket.png''')
288
0
'''simple docstring''' from numpy import exp, pi, sqrt def lowercase_ ( _lowercase , _lowercase = 0.0 , _lowercase = 1.0 ) -> int: '''simple docstring''' return 1 / sqrt(2 * pi * sigma**2 ) * exp(-((x - mu) ** 2) / (2 * sigma**2) ) if __name__ == "__main__": import doctest doctest.testmod()
318
import numpy as np # Importing the Keras libraries and packages import tensorflow as tf from tensorflow.keras import layers, models if __name__ == "__main__": # Initialising the CNN # (Sequential- Building the model layer by layer) _SCREAMING_SNAKE_CASE = models.Sequential() # Step 1 - Convolution # Here 64,64 is the length & breadth of dataset images and 3 is for the RGB channel # (3,3) is the kernel size (filter matrix) classifier.add( layers.ConvaD(3_2, (3, 3), input_shape=(6_4, 6_4, 3), activation="""relu""") ) # Step 2 - Pooling classifier.add(layers.MaxPoolingaD(pool_size=(2, 2))) # Adding a second convolutional layer classifier.add(layers.ConvaD(3_2, (3, 3), activation="""relu""")) classifier.add(layers.MaxPoolingaD(pool_size=(2, 2))) # Step 3 - Flattening classifier.add(layers.Flatten()) # Step 4 - Full connection classifier.add(layers.Dense(units=1_2_8, activation="""relu""")) classifier.add(layers.Dense(units=1, activation="""sigmoid""")) # Compiling the CNN classifier.compile( optimizer="""adam""", loss="""binary_crossentropy""", metrics=["""accuracy"""] ) # Part 2 - Fitting the CNN to the images # Load Trained model weights # from keras.models import load_model # regressor=load_model('cnn.h5') _SCREAMING_SNAKE_CASE = tf.keras.preprocessing.image.ImageDataGenerator( rescale=1.0 / 2_5_5, shear_range=0.2, zoom_range=0.2, horizontal_flip=True ) _SCREAMING_SNAKE_CASE = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1.0 / 2_5_5) _SCREAMING_SNAKE_CASE = train_datagen.flow_from_directory( """dataset/training_set""", target_size=(6_4, 6_4), batch_size=3_2, class_mode="""binary""" ) _SCREAMING_SNAKE_CASE = test_datagen.flow_from_directory( """dataset/test_set""", target_size=(6_4, 6_4), batch_size=3_2, class_mode="""binary""" ) classifier.fit_generator( training_set, steps_per_epoch=5, epochs=3_0, validation_data=test_set ) classifier.save("""cnn.h5""") # Part 3 - Making new predictions _SCREAMING_SNAKE_CASE = tf.keras.preprocessing.image.load_img( """dataset/single_prediction/image.png""", target_size=(6_4, 6_4) ) _SCREAMING_SNAKE_CASE = tf.keras.preprocessing.image.img_to_array(test_image) _SCREAMING_SNAKE_CASE = np.expand_dims(test_image, axis=0) _SCREAMING_SNAKE_CASE = classifier.predict(test_image) # training_set.class_indices if result[0][0] == 0: _SCREAMING_SNAKE_CASE = """Normal""" if result[0][0] == 1: _SCREAMING_SNAKE_CASE = """Abnormality detected"""
343
0
import os import shutil import sys import tempfile import unittest from pathlib import Path import pytest import transformers from transformers import ( BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, AutoTokenizer, BertConfig, BertTokenizer, BertTokenizerFast, CTRLTokenizer, GPTaTokenizer, GPTaTokenizerFast, PreTrainedTokenizerFast, RobertaTokenizer, RobertaTokenizerFast, is_tokenizers_available, ) from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig from transformers.models.auto.tokenization_auto import ( TOKENIZER_MAPPING, get_tokenizer_config, tokenizer_class_from_name, ) from transformers.models.roberta.configuration_roberta import RobertaConfig from transformers.testing_utils import ( DUMMY_DIFF_TOKENIZER_IDENTIFIER, DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, RequestCounter, require_tokenizers, slow, ) sys.path.append(str(Path(__file__).parent.parent.parent.parent / "utils")) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_tokenization import CustomTokenizer # noqa E402 if is_tokenizers_available(): from test_module.custom_tokenization_fast import CustomTokenizerFast class lowercase_ ( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self : Dict ) ->List[Any]: """simple docstring""" a = 0 @slow def __lowerCAmelCase ( self : Tuple ) ->Optional[Any]: """simple docstring""" for model_name in (x for x in BERT_PRETRAINED_CONFIG_ARCHIVE_MAP.keys() if "japanese" not in x): a = AutoTokenizer.from_pretrained(__UpperCAmelCase ) self.assertIsNotNone(__UpperCAmelCase ) self.assertIsInstance(__UpperCAmelCase , (BertTokenizer, BertTokenizerFast) ) self.assertGreater(len(__UpperCAmelCase ) , 0 ) for model_name in GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP.keys(): a = AutoTokenizer.from_pretrained(__UpperCAmelCase ) self.assertIsNotNone(__UpperCAmelCase ) self.assertIsInstance(__UpperCAmelCase , (GPTaTokenizer, GPTaTokenizerFast) ) self.assertGreater(len(__UpperCAmelCase ) , 0 ) def __lowerCAmelCase ( self : Union[str, Any] ) ->List[str]: """simple docstring""" a = AutoTokenizer.from_pretrained(__UpperCAmelCase ) self.assertIsInstance(__UpperCAmelCase , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(tokenizer.vocab_size , 12 ) def __lowerCAmelCase ( self : Optional[int] ) ->Any: """simple docstring""" a = AutoTokenizer.from_pretrained(__UpperCAmelCase ) self.assertIsInstance(__UpperCAmelCase , (RobertaTokenizer, RobertaTokenizerFast) ) self.assertEqual(tokenizer.vocab_size , 20 ) def __lowerCAmelCase ( self : int ) ->Optional[int]: """simple docstring""" a = AutoConfig.from_pretrained(__UpperCAmelCase ) self.assertIsInstance(__UpperCAmelCase , __UpperCAmelCase ) # Check that tokenizer_type ≠ model_type a = AutoTokenizer.from_pretrained(__UpperCAmelCase , config=__UpperCAmelCase ) self.assertIsInstance(__UpperCAmelCase , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(tokenizer.vocab_size , 12 ) def __lowerCAmelCase ( self : Dict ) ->Tuple: """simple docstring""" with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.txt''' , os.path.join(__UpperCAmelCase , '''vocab.txt''' ) ) a = AutoTokenizer.from_pretrained(__UpperCAmelCase , tokenizer_type='''bert''' , use_fast=__UpperCAmelCase ) self.assertIsInstance(__UpperCAmelCase , __UpperCAmelCase ) with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.json''' , os.path.join(__UpperCAmelCase , '''vocab.json''' ) ) shutil.copy('''./tests/fixtures/merges.txt''' , os.path.join(__UpperCAmelCase , '''merges.txt''' ) ) a = AutoTokenizer.from_pretrained(__UpperCAmelCase , tokenizer_type='''gpt2''' , use_fast=__UpperCAmelCase ) self.assertIsInstance(__UpperCAmelCase , __UpperCAmelCase ) @require_tokenizers def __lowerCAmelCase ( self : str ) ->Tuple: """simple docstring""" with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.txt''' , os.path.join(__UpperCAmelCase , '''vocab.txt''' ) ) a = AutoTokenizer.from_pretrained(__UpperCAmelCase , tokenizer_type='''bert''' ) self.assertIsInstance(__UpperCAmelCase , __UpperCAmelCase ) with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.json''' , os.path.join(__UpperCAmelCase , '''vocab.json''' ) ) shutil.copy('''./tests/fixtures/merges.txt''' , os.path.join(__UpperCAmelCase , '''merges.txt''' ) ) a = AutoTokenizer.from_pretrained(__UpperCAmelCase , tokenizer_type='''gpt2''' ) self.assertIsInstance(__UpperCAmelCase , __UpperCAmelCase ) def __lowerCAmelCase ( self : Any ) ->Optional[int]: """simple docstring""" with pytest.raises(__UpperCAmelCase ): AutoTokenizer.from_pretrained('''./''' , tokenizer_type='''xxx''' ) @require_tokenizers def __lowerCAmelCase ( self : List[Any] ) ->List[Any]: """simple docstring""" for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]: a = tokenizer_class.from_pretrained('''wietsedv/bert-base-dutch-cased''' ) self.assertIsInstance(__UpperCAmelCase , (BertTokenizer, BertTokenizerFast) ) if isinstance(__UpperCAmelCase , __UpperCAmelCase ): self.assertEqual(tokenizer.basic_tokenizer.do_lower_case , __UpperCAmelCase ) else: self.assertEqual(tokenizer.do_lower_case , __UpperCAmelCase ) self.assertEqual(tokenizer.model_max_length , 512 ) @require_tokenizers def __lowerCAmelCase ( self : Union[str, Any] ) ->int: """simple docstring""" for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]: with self.assertRaisesRegex( __UpperCAmelCase , '''julien-c/herlolip-not-exists is not a local folder and is not a valid model identifier''' , ): a = tokenizer_class.from_pretrained('''julien-c/herlolip-not-exists''' ) def __lowerCAmelCase ( self : Any ) ->Optional[Any]: """simple docstring""" a = TOKENIZER_MAPPING.values() a = [] for slow_tok, fast_tok in tokenizers: if slow_tok is not None: tokenizer_names.append(slow_tok.__name__ ) if fast_tok is not None: tokenizer_names.append(fast_tok.__name__ ) for tokenizer_name in tokenizer_names: # must find the right class tokenizer_class_from_name(__UpperCAmelCase ) @require_tokenizers def __lowerCAmelCase ( self : Dict ) ->List[Any]: """simple docstring""" self.assertIsInstance(AutoTokenizer.from_pretrained('''bert-base-cased''' , use_fast=__UpperCAmelCase ) , __UpperCAmelCase ) self.assertIsInstance(AutoTokenizer.from_pretrained('''bert-base-cased''' ) , __UpperCAmelCase ) @require_tokenizers def __lowerCAmelCase ( self : Optional[int] ) ->Any: """simple docstring""" a = AutoTokenizer.from_pretrained('''distilbert-base-uncased''' , do_lower_case=__UpperCAmelCase ) a = '''Hello, world. How are you?''' a = tokenizer.tokenize(__UpperCAmelCase ) self.assertEqual('''[UNK]''' , tokens[0] ) a = AutoTokenizer.from_pretrained('''microsoft/mpnet-base''' , do_lower_case=__UpperCAmelCase ) a = tokenizer.tokenize(__UpperCAmelCase ) self.assertEqual('''[UNK]''' , tokens[0] ) @require_tokenizers def __lowerCAmelCase ( self : Tuple ) ->Any: """simple docstring""" a = AutoTokenizer.from_pretrained('''robot-test/dummy-tokenizer-fast-with-model-config''' ) self.assertEqual(type(__UpperCAmelCase ) , __UpperCAmelCase ) self.assertEqual(tokenizer.model_max_length , 512 ) self.assertEqual(tokenizer.vocab_size , 30_000 ) self.assertEqual(tokenizer.unk_token , '''[UNK]''' ) self.assertEqual(tokenizer.padding_side , '''right''' ) self.assertEqual(tokenizer.truncation_side , '''right''' ) def __lowerCAmelCase ( self : Any ) ->Optional[Any]: """simple docstring""" a = AutoTokenizer.from_pretrained(__UpperCAmelCase ) self.assertIsInstance(__UpperCAmelCase , (BertTokenizer, BertTokenizerFast) ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(__UpperCAmelCase ) a = AutoTokenizer.from_pretrained(__UpperCAmelCase ) self.assertIsInstance(__UpperCAmelCase , tokenizer.__class__ ) self.assertEqual(tokenizera.vocab_size , 12 ) def __lowerCAmelCase ( self : List[Any] ) ->int: """simple docstring""" a = AutoTokenizer.from_pretrained('''ctrl''' ) # There is no fast CTRL so this always gives us a slow tokenizer. self.assertIsInstance(__UpperCAmelCase , __UpperCAmelCase ) def __lowerCAmelCase ( self : Dict ) ->Union[str, Any]: """simple docstring""" a = get_tokenizer_config('''bert-base-cased''' ) a = config.pop('''_commit_hash''' , __UpperCAmelCase ) # If we ever update bert-base-cased tokenizer config, this dict here will need to be updated. self.assertEqual(__UpperCAmelCase , {'''do_lower_case''': False} ) # This model does not have a tokenizer_config so we get back an empty dict. a = get_tokenizer_config(__UpperCAmelCase ) self.assertDictEqual(__UpperCAmelCase , {} ) # A tokenizer saved with `save_pretrained` always creates a tokenizer config. a = AutoTokenizer.from_pretrained(__UpperCAmelCase ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(__UpperCAmelCase ) a = get_tokenizer_config(__UpperCAmelCase ) # Check the class of the tokenizer was properly saved (note that it always saves the slow class). self.assertEqual(config['''tokenizer_class'''] , '''BertTokenizer''' ) def __lowerCAmelCase ( self : str ) ->Dict: """simple docstring""" try: AutoConfig.register('''custom''' , __UpperCAmelCase ) AutoTokenizer.register(__UpperCAmelCase , slow_tokenizer_class=__UpperCAmelCase ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(__UpperCAmelCase ): AutoTokenizer.register(__UpperCAmelCase , slow_tokenizer_class=__UpperCAmelCase ) a = CustomTokenizer.from_pretrained(__UpperCAmelCase ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(__UpperCAmelCase ) a = AutoTokenizer.from_pretrained(__UpperCAmelCase ) self.assertIsInstance(__UpperCAmelCase , __UpperCAmelCase ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] @require_tokenizers def __lowerCAmelCase ( self : int ) ->Dict: """simple docstring""" try: AutoConfig.register('''custom''' , __UpperCAmelCase ) # Can register in two steps AutoTokenizer.register(__UpperCAmelCase , slow_tokenizer_class=__UpperCAmelCase ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] , (CustomTokenizer, None) ) AutoTokenizer.register(__UpperCAmelCase , fast_tokenizer_class=__UpperCAmelCase ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] , (CustomTokenizer, CustomTokenizerFast) ) del TOKENIZER_MAPPING._extra_content[CustomConfig] # Can register in one step AutoTokenizer.register( __UpperCAmelCase , slow_tokenizer_class=__UpperCAmelCase , fast_tokenizer_class=__UpperCAmelCase ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] , (CustomTokenizer, CustomTokenizerFast) ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(__UpperCAmelCase ): AutoTokenizer.register(__UpperCAmelCase , fast_tokenizer_class=__UpperCAmelCase ) # We pass through a bert tokenizer fast cause there is no converter slow to fast for our new toknizer # and that model does not have a tokenizer.json with tempfile.TemporaryDirectory() as tmp_dir: a = BertTokenizerFast.from_pretrained(__UpperCAmelCase ) bert_tokenizer.save_pretrained(__UpperCAmelCase ) a = CustomTokenizerFast.from_pretrained(__UpperCAmelCase ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(__UpperCAmelCase ) a = AutoTokenizer.from_pretrained(__UpperCAmelCase ) self.assertIsInstance(__UpperCAmelCase , __UpperCAmelCase ) a = AutoTokenizer.from_pretrained(__UpperCAmelCase , use_fast=__UpperCAmelCase ) self.assertIsInstance(__UpperCAmelCase , __UpperCAmelCase ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] def __lowerCAmelCase ( self : List[Any] ) ->Tuple: """simple docstring""" with self.assertRaises(__UpperCAmelCase ): a = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' ) # If remote code is disabled, we can't load this config. with self.assertRaises(__UpperCAmelCase ): a = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=__UpperCAmelCase ) a = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=__UpperCAmelCase ) self.assertTrue(tokenizer.special_attribute_present ) # Test tokenizer can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(__UpperCAmelCase ) a = AutoTokenizer.from_pretrained(__UpperCAmelCase , trust_remote_code=__UpperCAmelCase ) self.assertTrue(reloaded_tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) self.assertEqual(reloaded_tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) # Test we can also load the slow version a = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=__UpperCAmelCase , use_fast=__UpperCAmelCase ) self.assertTrue(tokenizer.special_attribute_present ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) # Test tokenizer can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(__UpperCAmelCase ) a = AutoTokenizer.from_pretrained(__UpperCAmelCase , trust_remote_code=__UpperCAmelCase , use_fast=__UpperCAmelCase ) self.assertEqual(reloaded_tokenizer.__class__.__name__ , '''NewTokenizer''' ) self.assertTrue(reloaded_tokenizer.special_attribute_present ) else: self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) self.assertEqual(reloaded_tokenizer.__class__.__name__ , '''NewTokenizer''' ) @require_tokenizers def __lowerCAmelCase ( self : Optional[Any] ) ->Any: """simple docstring""" class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = False class lowercase_ ( lowercase ): '''simple docstring''' __snake_case = NewTokenizer __snake_case = False try: AutoConfig.register('''custom''' , __UpperCAmelCase ) AutoTokenizer.register(__UpperCAmelCase , slow_tokenizer_class=__UpperCAmelCase ) AutoTokenizer.register(__UpperCAmelCase , fast_tokenizer_class=__UpperCAmelCase ) # If remote code is not set, the default is to use local a = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) self.assertFalse(tokenizer.special_attribute_present ) a = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' , use_fast=__UpperCAmelCase ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) self.assertFalse(tokenizer.special_attribute_present ) # If remote code is disabled, we load the local one. a = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=__UpperCAmelCase ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) self.assertFalse(tokenizer.special_attribute_present ) a = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=__UpperCAmelCase , use_fast=__UpperCAmelCase ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) self.assertFalse(tokenizer.special_attribute_present ) # If remote is enabled, we load from the Hub a = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=__UpperCAmelCase ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) self.assertTrue(tokenizer.special_attribute_present ) a = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=__UpperCAmelCase , use_fast=__UpperCAmelCase ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) self.assertTrue(tokenizer.special_attribute_present ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] def __lowerCAmelCase ( self : Optional[Any] ) ->Dict: """simple docstring""" a = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer_legacy''' , trust_remote_code=__UpperCAmelCase ) self.assertTrue(tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) # Test we can also load the slow version a = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer_legacy''' , trust_remote_code=__UpperCAmelCase , use_fast=__UpperCAmelCase ) self.assertTrue(tokenizer.special_attribute_present ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) else: self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) def __lowerCAmelCase ( self : Any ) ->Any: """simple docstring""" with self.assertRaisesRegex( __UpperCAmelCase , '''bert-base is not a local folder and is not a valid model identifier''' ): a = AutoTokenizer.from_pretrained('''bert-base''' ) def __lowerCAmelCase ( self : str ) ->str: """simple docstring""" with self.assertRaisesRegex( __UpperCAmelCase , R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): a = AutoTokenizer.from_pretrained(__UpperCAmelCase , revision='''aaaaaa''' ) def __lowerCAmelCase ( self : str ) ->Union[str, Any]: """simple docstring""" a = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) with RequestCounter() as counter: a = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 )
363
from __future__ import annotations def _a ( a :dict , a :str ) -> set[str]: a , a = set(a ), [start] while stack: a = stack.pop() explored.add(a ) # Differences from BFS: # 1) pop last element instead of first one # 2) add adjacent elements to stack without exploring them for adj in reversed(graph[v] ): if adj not in explored: stack.append(a ) return explored UpperCAmelCase__ = { "A": ["B", "C", "D"], "B": ["A", "D", "E"], "C": ["A", "F"], "D": ["B", "D"], "E": ["B", "F"], "F": ["C", "E", "G"], "G": ["F"], } if __name__ == "__main__": import doctest doctest.testmod() print(depth_first_search(G, "A"))
26
0
import argparse from pathlib import Path import torch from transformers import OPTConfig, OPTModel from transformers.utils import logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger(__name__) def _a ( a :Optional[int] ) -> List[Any]: a = torch.load(a , map_location='''cpu''' ) if "model" in sd.keys(): a = torch.load(a , map_location='''cpu''' )['''model'''] # pop unnecessary weights a = [ '''decoder.version''', '''decoder.output_projection.weight''', ] for key in keys_to_delete: if key in sd: sd.pop(a ) a = { '''decoder.project_in_dim.weight''': '''decoder.project_in.weight''', '''decoder.project_out_dim.weight''': '''decoder.project_out.weight''', '''decoder.layer_norm.weight''': '''decoder.final_layer_norm.weight''', '''decoder.layer_norm.bias''': '''decoder.final_layer_norm.bias''', } for old_key, new_key in keys_to_rename.items(): if old_key in sd: a = sd.pop(a ) a = list(sd.keys() ) for key in keys: if ".qkv_proj." in key: a = sd[key] # We split QKV in separate Q,K,V a = key.replace('''.qkv_proj.''' , '''.q_proj.''' ) a = key.replace('''.qkv_proj.''' , '''.k_proj.''' ) a = key.replace('''.qkv_proj.''' , '''.v_proj.''' ) a = value.shape[0] assert depth % 3 == 0 # `SequeuceParallelTransformerBlock` has QKV weight is separated in K,V,Q despite the naming: # https://cs.github.com/facebookresearch/metaseq/blob/51871bd73cd04c038f239ea2a26db1d7f6b37927/metaseq/modules/sequence_parallel_transformer_layer.py#L97 a , a , a = torch.split(a , depth // 3 , dim=0 ) a = q a = k a = v del sd[key] return sd @torch.no_grad() def _a ( a :str , a :int , a :List[Any]=None ) -> Optional[Any]: a = load_checkpoint(a ) if config is not None: a = OPTConfig.from_pretrained(a ) else: a = OPTConfig() a = OPTModel(a ).half().eval() model.load_state_dict(a ) # Check results Path(a ).mkdir(exist_ok=a ) model.save_pretrained(a ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--fairseq_path", type=str, help=( "path to fairseq checkpoint in correct format. You can find all checkpoints in the correct format here:" " https://huggingface.co/models?other=opt_metasq" ), ) parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--hf_config", default=None, type=str, help="Define HF config.") UpperCAmelCase__ = parser.parse_args() convert_opt_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, config=args.hf_config)
0
import argparse import json import logging import os import shutil import sys import tempfile import unittest from unittest import mock import torch from accelerate.utils import write_basic_config from transformers.testing_utils import TestCasePlus, get_gpu_count, run_command, slow, torch_device from transformers.utils import is_apex_available logging.basicConfig(level=logging.DEBUG) UpperCAmelCase__ = logging.getLogger() def _a ( ) -> Optional[int]: a = argparse.ArgumentParser() parser.add_argument('''-f''' ) a = parser.parse_args() return args.f def _a ( a :Any ) -> Tuple: a = {} a = os.path.join(a , '''all_results.json''' ) if os.path.exists(a ): with open(a , '''r''' ) as f: a = json.load(a ) else: raise ValueError(F"""can't find {path}""" ) return results def _a ( ) -> int: a = torch.cuda.is_available() and torch_device == '''cuda''' return is_using_cuda and is_apex_available() UpperCAmelCase__ = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class lowercase_ ( lowercase ): '''simple docstring''' @classmethod def __lowerCAmelCase ( cls : str ) ->Tuple: """simple docstring""" a = tempfile.mkdtemp() a = os.path.join(cls.tmpdir , '''default_config.yml''' ) write_basic_config(save_location=cls.configPath ) a = ['''accelerate''', '''launch''', '''--config_file''', cls.configPath] @classmethod def __lowerCAmelCase ( cls : Optional[int] ) ->Union[str, Any]: """simple docstring""" shutil.rmtree(cls.tmpdir ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __lowerCAmelCase ( self : List[Any] ) ->List[str]: """simple docstring""" a = self.get_auto_remove_tmp_dir() a = F""" {self.examples_dir}/pytorch/text-classification/run_glue_no_trainer.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 --seed=42 --checkpointing_steps epoch --with_tracking """.split() if is_cuda_and_apex_available(): testargs.append('''--fp16''' ) run_command(self._launch_args + testargs ) a = get_results(__UpperCAmelCase ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.75 ) self.assertTrue(os.path.exists(os.path.join(__UpperCAmelCase , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(__UpperCAmelCase , '''glue_no_trainer''' ) ) ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __lowerCAmelCase ( self : Optional[Any] ) ->Any: """simple docstring""" a = self.get_auto_remove_tmp_dir() a = F""" {self.examples_dir}/pytorch/language-modeling/run_clm_no_trainer.py --model_name_or_path distilgpt2 --train_file ./tests/fixtures/sample_text.txt --validation_file ./tests/fixtures/sample_text.txt --block_size 128 --per_device_train_batch_size 5 --per_device_eval_batch_size 5 --num_train_epochs 2 --output_dir {tmp_dir} --checkpointing_steps epoch --with_tracking """.split() if torch.cuda.device_count() > 1: # Skipping because there are not enough batches to train the model + would need a drop_last to work. return run_command(self._launch_args + testargs ) a = get_results(__UpperCAmelCase ) self.assertLess(result['''perplexity'''] , 100 ) self.assertTrue(os.path.exists(os.path.join(__UpperCAmelCase , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(__UpperCAmelCase , '''clm_no_trainer''' ) ) ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __lowerCAmelCase ( self : Optional[int] ) ->int: """simple docstring""" a = self.get_auto_remove_tmp_dir() a = F""" {self.examples_dir}/pytorch/language-modeling/run_mlm_no_trainer.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} --num_train_epochs=1 --checkpointing_steps epoch --with_tracking """.split() run_command(self._launch_args + testargs ) a = get_results(__UpperCAmelCase ) self.assertLess(result['''perplexity'''] , 42 ) self.assertTrue(os.path.exists(os.path.join(__UpperCAmelCase , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(__UpperCAmelCase , '''mlm_no_trainer''' ) ) ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __lowerCAmelCase ( self : Optional[int] ) ->str: """simple docstring""" a = 7 if get_gpu_count() > 1 else 2 a = self.get_auto_remove_tmp_dir() a = F""" {self.examples_dir}/pytorch/token-classification/run_ner_no_trainer.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} --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=2 --num_train_epochs={epochs} --seed 7 --checkpointing_steps epoch --with_tracking """.split() run_command(self._launch_args + testargs ) a = get_results(__UpperCAmelCase ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.75 ) self.assertLess(result['''train_loss'''] , 0.5 ) self.assertTrue(os.path.exists(os.path.join(__UpperCAmelCase , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(__UpperCAmelCase , '''ner_no_trainer''' ) ) ) @unittest.skip(reason='''Fix me @muellerzr''' ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __lowerCAmelCase ( self : Any ) ->int: """simple docstring""" a = self.get_auto_remove_tmp_dir() a = F""" {self.examples_dir}/pytorch/question-answering/run_qa_no_trainer.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} --seed=42 --max_train_steps=10 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch --with_tracking """.split() run_command(self._launch_args + testargs ) a = get_results(__UpperCAmelCase ) # Because we use --version_2_with_negative the testing script uses SQuAD v2 metrics. self.assertGreaterEqual(result['''eval_f1'''] , 28 ) self.assertGreaterEqual(result['''eval_exact'''] , 28 ) self.assertTrue(os.path.exists(os.path.join(__UpperCAmelCase , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(__UpperCAmelCase , '''qa_no_trainer''' ) ) ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __lowerCAmelCase ( self : Optional[Any] ) ->Any: """simple docstring""" a = self.get_auto_remove_tmp_dir() a = F""" {self.examples_dir}/pytorch/multiple-choice/run_swag_no_trainer.py --model_name_or_path bert-base-uncased --train_file tests/fixtures/tests_samples/swag/sample.json --validation_file tests/fixtures/tests_samples/swag/sample.json --output_dir {tmp_dir} --max_train_steps=20 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --with_tracking """.split() run_command(self._launch_args + testargs ) a = get_results(__UpperCAmelCase ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.8 ) self.assertTrue(os.path.exists(os.path.join(__UpperCAmelCase , '''swag_no_trainer''' ) ) ) @slow @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __lowerCAmelCase ( self : Union[str, Any] ) ->Union[str, Any]: """simple docstring""" a = self.get_auto_remove_tmp_dir() a = F""" {self.examples_dir}/pytorch/summarization/run_summarization_no_trainer.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 --output_dir {tmp_dir} --max_train_steps=50 --num_warmup_steps=8 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch --with_tracking """.split() run_command(self._launch_args + testargs ) a = get_results(__UpperCAmelCase ) self.assertGreaterEqual(result['''eval_rouge1'''] , 10 ) self.assertGreaterEqual(result['''eval_rouge2'''] , 2 ) self.assertGreaterEqual(result['''eval_rougeL'''] , 7 ) self.assertGreaterEqual(result['''eval_rougeLsum'''] , 7 ) self.assertTrue(os.path.exists(os.path.join(__UpperCAmelCase , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(__UpperCAmelCase , '''summarization_no_trainer''' ) ) ) @slow @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __lowerCAmelCase ( self : Optional[int] ) ->List[str]: """simple docstring""" a = self.get_auto_remove_tmp_dir() a = F""" {self.examples_dir}/pytorch/translation/run_translation_no_trainer.py --model_name_or_path sshleifer/student_marian_en_ro_6_1 --source_lang en --target_lang ro --train_file tests/fixtures/tests_samples/wmt16/sample.json --validation_file tests/fixtures/tests_samples/wmt16/sample.json --output_dir {tmp_dir} --max_train_steps=50 --num_warmup_steps=8 --num_beams=6 --learning_rate=3e-3 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --source_lang en_XX --target_lang ro_RO --checkpointing_steps epoch --with_tracking """.split() run_command(self._launch_args + testargs ) a = get_results(__UpperCAmelCase ) self.assertGreaterEqual(result['''eval_bleu'''] , 30 ) self.assertTrue(os.path.exists(os.path.join(__UpperCAmelCase , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(__UpperCAmelCase , '''translation_no_trainer''' ) ) ) @slow def __lowerCAmelCase ( self : List[str] ) ->int: """simple docstring""" a = logging.StreamHandler(sys.stdout ) logger.addHandler(__UpperCAmelCase ) a = self.get_auto_remove_tmp_dir() a = F""" {self.examples_dir}/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py --dataset_name huggingface/semantic-segmentation-test-sample --output_dir {tmp_dir} --max_train_steps=10 --num_warmup_steps=2 --learning_rate=2e-4 --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --checkpointing_steps epoch """.split() run_command(self._launch_args + testargs ) a = get_results(__UpperCAmelCase ) self.assertGreaterEqual(result['''eval_overall_accuracy'''] , 0.10 ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __lowerCAmelCase ( self : Optional[Any] ) ->Tuple: """simple docstring""" a = self.get_auto_remove_tmp_dir() a = F""" {self.examples_dir}/pytorch/image-classification/run_image_classification_no_trainer.py --model_name_or_path google/vit-base-patch16-224-in21k --dataset_name hf-internal-testing/cats_vs_dogs_sample --learning_rate 1e-4 --per_device_train_batch_size 2 --per_device_eval_batch_size 1 --max_train_steps 2 --train_val_split 0.1 --seed 42 --output_dir {tmp_dir} --with_tracking --checkpointing_steps 1 """.split() if is_cuda_and_apex_available(): testargs.append('''--fp16''' ) run_command(self._launch_args + testargs ) a = get_results(__UpperCAmelCase ) # The base model scores a 25% self.assertGreaterEqual(result['''eval_accuracy'''] , 0.6 ) self.assertTrue(os.path.exists(os.path.join(__UpperCAmelCase , '''step_1''' ) ) ) self.assertTrue(os.path.exists(os.path.join(__UpperCAmelCase , '''image_classification_no_trainer''' ) ) )
0
1
"""simple docstring""" UpperCamelCase__ =[ 'VerificationMode', 'Version', 'disable_progress_bar', 'enable_progress_bar', 'is_progress_bar_enabled', 'experimental', ] from .info_utils import VerificationMode from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled from .version import Version from .experimental import experimental
361
import numpy as np import skfuzzy as fuzz if __name__ == "__main__": # Create universe of discourse in Python using linspace () UpperCamelCase__ =np.linspace(start=0, stop=75, num=75, endpoint=True, retstep=False) # Create two fuzzy sets by defining any membership function # (trapmf(), gbellmf(), gaussmf(), etc). UpperCamelCase__ =[0, 25, 50] UpperCamelCase__ =[25, 50, 75] UpperCamelCase__ =fuzz.membership.trimf(X, abca) UpperCamelCase__ =fuzz.membership.trimf(X, abca) # Compute the different operations using inbuilt functions. UpperCamelCase__ =np.ones(75) UpperCamelCase__ =np.zeros((75,)) # 1. Union = max(µA(x), µB(x)) UpperCamelCase__ =fuzz.fuzzy_or(X, young, X, middle_aged)[1] # 2. Intersection = min(µA(x), µB(x)) UpperCamelCase__ =fuzz.fuzzy_and(X, young, X, middle_aged)[1] # 3. Complement (A) = (1- min(µA(x)) UpperCamelCase__ =fuzz.fuzzy_not(young) # 4. Difference (A/B) = min(µA(x),(1- µB(x))) UpperCamelCase__ =fuzz.fuzzy_and(X, young, X, fuzz.fuzzy_not(middle_aged)[1])[1] # 5. Algebraic Sum = [µA(x) + µB(x) – (µA(x) * µB(x))] UpperCamelCase__ =young + middle_aged - (young * middle_aged) # 6. Algebraic Product = (µA(x) * µB(x)) UpperCamelCase__ =young * middle_aged # 7. Bounded Sum = min[1,(µA(x), µB(x))] UpperCamelCase__ =fuzz.fuzzy_and(X, one, X, young + middle_aged)[1] # 8. Bounded difference = min[0,(µA(x), µB(x))] UpperCamelCase__ =fuzz.fuzzy_or(X, zero, X, young - middle_aged)[1] # max-min composition # max-product composition # Plot each set A, set B and each operation result using plot() and subplot(). from matplotlib import pyplot as plt plt.figure() plt.subplot(4, 3, 1) plt.plot(X, young) plt.title('Young') plt.grid(True) plt.subplot(4, 3, 2) plt.plot(X, middle_aged) plt.title('Middle aged') plt.grid(True) plt.subplot(4, 3, 3) plt.plot(X, union) plt.title('union') plt.grid(True) plt.subplot(4, 3, 4) plt.plot(X, intersection) plt.title('intersection') plt.grid(True) plt.subplot(4, 3, 5) plt.plot(X, complement_a) plt.title('complement_a') plt.grid(True) plt.subplot(4, 3, 6) plt.plot(X, difference) plt.title('difference a/b') plt.grid(True) plt.subplot(4, 3, 7) plt.plot(X, alg_sum) plt.title('alg_sum') plt.grid(True) plt.subplot(4, 3, 8) plt.plot(X, alg_product) plt.title('alg_product') plt.grid(True) plt.subplot(4, 3, 9) plt.plot(X, bdd_sum) plt.title('bdd_sum') plt.grid(True) plt.subplot(4, 3, 10) plt.plot(X, bdd_difference) plt.title('bdd_difference') plt.grid(True) plt.subplots_adjust(hspace=0.5) plt.show()
325
0
import os from pathlib import Path from unittest.mock import patch import pytest import zstandard as zstd from datasets.download.download_config import DownloadConfig from datasets.utils.file_utils import ( OfflineModeIsEnabled, cached_path, fsspec_get, fsspec_head, ftp_get, ftp_head, get_from_cache, http_get, http_head, ) a_ = """\ Text data. Second line of data.""" a_ = """file""" @pytest.fixture(scope='''session''' ) def a__ ( _UpperCamelCase : str ): __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / (FILE_PATH + '''.zstd''') __lowerCamelCase = bytes(_UpperCamelCase ,'''utf-8''' ) with zstd.open(_UpperCamelCase ,'''wb''' ) as f: f.write(_UpperCamelCase ) return path @pytest.fixture def a__ ( _UpperCamelCase : Dict ): with open(os.path.join(tmpfs.local_root_dir ,_UpperCamelCase ) ,'''w''' ) as f: f.write(_UpperCamelCase ) return FILE_PATH @pytest.mark.parametrize('''compression_format''' ,['''gzip''', '''xz''', '''zstd'''] ) def a__ ( _UpperCamelCase : int ,_UpperCamelCase : Optional[int] ,_UpperCamelCase : Union[str, Any] ,_UpperCamelCase : int ,_UpperCamelCase : List[str] ,_UpperCamelCase : Tuple ): __lowerCamelCase = {'''gzip''': gz_file, '''xz''': xz_file, '''zstd''': zstd_path} __lowerCamelCase = input_paths[compression_format] __lowerCamelCase = tmp_path / '''cache''' __lowerCamelCase = DownloadConfig(cache_dir=_UpperCamelCase ,extract_compressed_file=_UpperCamelCase ) __lowerCamelCase = cached_path(_UpperCamelCase ,download_config=_UpperCamelCase ) with open(_UpperCamelCase ) as f: __lowerCamelCase = f.read() with open(_UpperCamelCase ) as f: __lowerCamelCase = f.read() assert extracted_file_content == expected_file_content @pytest.mark.parametrize('''default_extracted''' ,[True, False] ) @pytest.mark.parametrize('''default_cache_dir''' ,[True, False] ) def a__ ( _UpperCamelCase : str ,_UpperCamelCase : Union[str, Any] ,_UpperCamelCase : int ,_UpperCamelCase : List[str] ,_UpperCamelCase : str ): __lowerCamelCase = '''custom_cache''' __lowerCamelCase = '''custom_extracted_dir''' __lowerCamelCase = tmp_path / '''custom_extracted_path''' if default_extracted: __lowerCamelCase = ('''downloads''' if default_cache_dir else custom_cache_dir, '''extracted''') else: monkeypatch.setattr('''datasets.config.EXTRACTED_DATASETS_DIR''' ,_UpperCamelCase ) monkeypatch.setattr('''datasets.config.EXTRACTED_DATASETS_PATH''' ,str(_UpperCamelCase ) ) __lowerCamelCase = custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir) __lowerCamelCase = xz_file __lowerCamelCase = ( DownloadConfig(extract_compressed_file=_UpperCamelCase ) if default_cache_dir else DownloadConfig(cache_dir=tmp_path / custom_cache_dir ,extract_compressed_file=_UpperCamelCase ) ) __lowerCamelCase = cached_path(_UpperCamelCase ,download_config=_UpperCamelCase ) assert Path(_UpperCamelCase ).parent.parts[-2:] == expected def a__ ( _UpperCamelCase : Tuple ): # absolute path __lowerCamelCase = str(Path(_UpperCamelCase ).resolve() ) assert cached_path(_UpperCamelCase ) == text_file # relative path __lowerCamelCase = str(Path(_UpperCamelCase ).resolve().relative_to(Path(os.getcwd() ) ) ) assert cached_path(_UpperCamelCase ) == text_file def a__ ( _UpperCamelCase : Union[str, Any] ): # absolute path __lowerCamelCase = str(tmp_path.resolve() / '''__missing_file__.txt''' ) with pytest.raises(_UpperCamelCase ): cached_path(_UpperCamelCase ) # relative path __lowerCamelCase = '''./__missing_file__.txt''' with pytest.raises(_UpperCamelCase ): cached_path(_UpperCamelCase ) def a__ ( _UpperCamelCase : Dict ): __lowerCamelCase = get_from_cache(F"""tmp://{tmpfs_file}""" ) with open(_UpperCamelCase ) as f: __lowerCamelCase = f.read() assert output_file_content == FILE_CONTENT @patch('''datasets.config.HF_DATASETS_OFFLINE''' ,_UpperCamelCase ) def a__ ( ): with pytest.raises(_UpperCamelCase ): cached_path('''https://huggingface.co''' ) @patch('''datasets.config.HF_DATASETS_OFFLINE''' ,_UpperCamelCase ) def a__ ( _UpperCamelCase : Optional[int] ): __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''file.html''' with pytest.raises(_UpperCamelCase ): http_get('''https://huggingface.co''' ,temp_file=_UpperCamelCase ) with pytest.raises(_UpperCamelCase ): http_head('''https://huggingface.co''' ) @patch('''datasets.config.HF_DATASETS_OFFLINE''' ,_UpperCamelCase ) def a__ ( _UpperCamelCase : Optional[int] ): __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''file.html''' with pytest.raises(_UpperCamelCase ): ftp_get('''ftp://huggingface.co''' ,temp_file=_UpperCamelCase ) with pytest.raises(_UpperCamelCase ): ftp_head('''ftp://huggingface.co''' ) @patch('''datasets.config.HF_DATASETS_OFFLINE''' ,_UpperCamelCase ) def a__ ( _UpperCamelCase : Dict ): __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''file.html''' with pytest.raises(_UpperCamelCase ): fsspec_get('''s3://huggingface.co''' ,temp_file=_UpperCamelCase ) with pytest.raises(_UpperCamelCase ): fsspec_head('''s3://huggingface.co''' )
330
import random import unittest from torch.utils.data import BatchSampler, DataLoader, IterableDataset from accelerate import Accelerator from accelerate.data_loader import ( BatchSamplerShard, DataLoaderDispatcher, DataLoaderShard, IterableDatasetShard, SkipBatchSampler, SkipDataLoader, skip_first_batches, ) class __lowerCAmelCase ( lowerCAmelCase__ ): def __init__( self , __UpperCAmelCase=0.01 , __UpperCAmelCase=1000 ): '''simple docstring''' __lowerCamelCase = p_stop __lowerCamelCase = max_length def __iter__( self ): '''simple docstring''' __lowerCamelCase = 0 __lowerCamelCase = False while not stop and count < self.max_length: yield count count += 1 __lowerCamelCase = random.random() < self.p_stop class __lowerCAmelCase ( unittest.TestCase ): def lowerCamelCase ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=False , __UpperCAmelCase=True ): '''simple docstring''' __lowerCamelCase = [ BatchSamplerShard(__UpperCAmelCase , 2 , __UpperCAmelCase , split_batches=__UpperCAmelCase , even_batches=__UpperCAmelCase ) for i in range(2 ) ] __lowerCamelCase = [list(__UpperCAmelCase ) for batch_sampler_shard in batch_sampler_shards] if not split_batches: self.assertListEqual([len(__UpperCAmelCase ) for shard in batch_sampler_shards] , [len(__UpperCAmelCase ) for e in expected] ) self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase ) def lowerCamelCase ( self ): '''simple docstring''' # Check the shards when the dataset is a round multiple of total batch size. __lowerCamelCase = BatchSampler(range(24 ) , batch_size=3 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]], [[3, 4, 5], [9, 10, 11], [15, 16, 17], [21, 22, 23]], ] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase ) __lowerCamelCase = BatchSampler(range(24 ) , batch_size=3 , drop_last=__UpperCAmelCase ) # Expected shouldn't change self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase ) # Check the shards when the dataset is a round multiple of batch size but not total batch size. __lowerCamelCase = BatchSampler(range(21 ) , batch_size=3 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]], [[3, 4, 5], [9, 10, 11], [15, 16, 17], [0, 1, 2]], ] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase ) __lowerCamelCase = BatchSampler(range(21 ) , batch_size=3 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase ) # Check the shards when the dataset is not a round multiple of batch size but has a multiple of # num_processes batch. __lowerCamelCase = BatchSampler(range(22 ) , batch_size=3 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]], [[3, 4, 5], [9, 10, 11], [15, 16, 17], [21, 0, 1]], ] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase ) __lowerCamelCase = BatchSampler(range(22 ) , batch_size=3 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase ) # Check the shards when the dataset is not a round multiple of batch size but and has not a multiple of # num_processes batch. __lowerCamelCase = BatchSampler(range(20 ) , batch_size=3 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 0]], [[3, 4, 5], [9, 10, 11], [15, 16, 17], [1, 2, 3]], ] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase ) __lowerCamelCase = BatchSampler(range(20 ) , batch_size=3 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase ) # Check the shards when the dataset is very small. __lowerCamelCase = BatchSampler(range(2 ) , batch_size=3 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [[[0, 1, 0]], [[1, 0, 1]]] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase ) __lowerCamelCase = BatchSampler(range(2 ) , batch_size=3 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [[], []] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase ) def lowerCamelCase ( self ): '''simple docstring''' # Check the shards when the dataset is a round multiple of batch size. __lowerCamelCase = BatchSampler(range(24 ) , batch_size=4 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17], [20, 21]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19], [22, 23]], ] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , split_batches=__UpperCAmelCase ) __lowerCamelCase = BatchSampler(range(24 ) , batch_size=4 , drop_last=__UpperCAmelCase ) # Expected shouldn't change self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , split_batches=__UpperCAmelCase ) # Check the shards when the dataset is not a round multiple of batch size. __lowerCamelCase = BatchSampler(range(22 ) , batch_size=4 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17], [20, 21]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19], [0, 1]], ] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , split_batches=__UpperCAmelCase ) __lowerCamelCase = BatchSampler(range(22 ) , batch_size=4 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19]], ] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , split_batches=__UpperCAmelCase ) # Check the shards when the dataset is not a round multiple of batch size or num_processes. __lowerCamelCase = BatchSampler(range(21 ) , batch_size=4 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17], [20, 0]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19], [1, 2]], ] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , split_batches=__UpperCAmelCase ) __lowerCamelCase = BatchSampler(range(21 ) , batch_size=4 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19]], ] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , split_batches=__UpperCAmelCase ) # Check the shards when the dataset is very small. __lowerCamelCase = BatchSampler(range(2 ) , batch_size=4 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [[[0, 1]], [[0, 1]]] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , split_batches=__UpperCAmelCase ) __lowerCamelCase = BatchSampler(range(2 ) , batch_size=4 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [[], []] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , split_batches=__UpperCAmelCase ) def lowerCamelCase ( self ): '''simple docstring''' # Check the shards when the dataset is a round multiple of total batch size. __lowerCamelCase = BatchSampler(range(24 ) , batch_size=3 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]], [[3, 4, 5], [9, 10, 11], [15, 16, 17], [21, 22, 23]], ] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , even_batches=__UpperCAmelCase ) __lowerCamelCase = BatchSampler(range(24 ) , batch_size=3 , drop_last=__UpperCAmelCase ) # Expected shouldn't change self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , even_batches=__UpperCAmelCase ) # Check the shards when the dataset is a round multiple of batch size but not total batch size. __lowerCamelCase = BatchSampler(range(21 ) , batch_size=3 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , even_batches=__UpperCAmelCase ) __lowerCamelCase = BatchSampler(range(21 ) , batch_size=3 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , even_batches=__UpperCAmelCase ) # Check the shards when the dataset is not a round multiple of batch size but has a multiple of # num_processes batch. __lowerCamelCase = BatchSampler(range(22 ) , batch_size=3 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]], [[3, 4, 5], [9, 10, 11], [15, 16, 17], [21]], ] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , even_batches=__UpperCAmelCase ) __lowerCamelCase = BatchSampler(range(22 ) , batch_size=3 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , even_batches=__UpperCAmelCase ) # Check the shards when the dataset is not a round multiple of batch size but and has not a multiple of # num_processes batch. __lowerCamelCase = BatchSampler(range(20 ) , batch_size=3 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , even_batches=__UpperCAmelCase ) __lowerCamelCase = BatchSampler(range(20 ) , batch_size=3 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , even_batches=__UpperCAmelCase ) # Check the shards when the dataset is very small. __lowerCamelCase = BatchSampler(range(2 ) , batch_size=3 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [[[0, 1]], []] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , even_batches=__UpperCAmelCase ) __lowerCamelCase = BatchSampler(range(2 ) , batch_size=3 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [[], []] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , even_batches=__UpperCAmelCase ) def lowerCamelCase ( self ): '''simple docstring''' # Check the shards when the dataset is a round multiple of batch size. __lowerCamelCase = BatchSampler(range(24 ) , batch_size=4 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17], [20, 21]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19], [22, 23]], ] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , split_batches=__UpperCAmelCase , even_batches=__UpperCAmelCase ) __lowerCamelCase = BatchSampler(range(24 ) , batch_size=4 , drop_last=__UpperCAmelCase ) # Expected shouldn't change self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , split_batches=__UpperCAmelCase , even_batches=__UpperCAmelCase ) # Check the shards when the dataset is not a round multiple of batch size. __lowerCamelCase = BatchSampler(range(22 ) , batch_size=4 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17], [20, 21]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19]], ] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , split_batches=__UpperCAmelCase , even_batches=__UpperCAmelCase ) __lowerCamelCase = BatchSampler(range(22 ) , batch_size=4 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19]], ] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , split_batches=__UpperCAmelCase , even_batches=__UpperCAmelCase ) # Check the shards when the dataset is not a round multiple of batch size or num_processes. __lowerCamelCase = BatchSampler(range(21 ) , batch_size=4 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17], [20]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19]], ] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , split_batches=__UpperCAmelCase , even_batches=__UpperCAmelCase ) __lowerCamelCase = BatchSampler(range(21 ) , batch_size=4 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19]], ] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , split_batches=__UpperCAmelCase , even_batches=__UpperCAmelCase ) # Check the shards when the dataset is very small. __lowerCamelCase = BatchSampler(range(2 ) , batch_size=4 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [[[0, 1]], []] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , split_batches=__UpperCAmelCase , even_batches=__UpperCAmelCase ) __lowerCamelCase = BatchSampler(range(2 ) , batch_size=4 , drop_last=__UpperCAmelCase ) __lowerCamelCase = [[], []] self.check_batch_sampler_shards(__UpperCAmelCase , __UpperCAmelCase , split_batches=__UpperCAmelCase , even_batches=__UpperCAmelCase ) def lowerCamelCase ( self ): '''simple docstring''' __lowerCamelCase = [[0, 1, 2], [3, 4], [5, 6, 7, 8], [9, 10, 11], [12, 13]] __lowerCamelCase = [BatchSamplerShard(__UpperCAmelCase , 2 , __UpperCAmelCase , even_batches=__UpperCAmelCase ) for i in range(2 )] self.assertEqual(len(batch_sampler_shards[0] ) , 3 ) self.assertEqual(len(batch_sampler_shards[1] ) , 2 ) self.assertListEqual(list(batch_sampler_shards[0] ) , [[0, 1, 2], [5, 6, 7, 8], [12, 13]] ) self.assertListEqual(list(batch_sampler_shards[1] ) , [[3, 4], [9, 10, 11]] ) def lowerCamelCase ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=False , __UpperCAmelCase=2 , __UpperCAmelCase=False ): '''simple docstring''' random.seed(__UpperCAmelCase ) __lowerCamelCase = list(__UpperCAmelCase ) __lowerCamelCase = [ IterableDatasetShard( __UpperCAmelCase , batch_size=__UpperCAmelCase , drop_last=__UpperCAmelCase , num_processes=__UpperCAmelCase , process_index=__UpperCAmelCase , split_batches=__UpperCAmelCase , ) for i in range(__UpperCAmelCase ) ] __lowerCamelCase = [] for iterable_dataset_shard in iterable_dataset_shards: # Since our random iterable dataset will be... random... we need to use a seed to get reproducible results. random.seed(__UpperCAmelCase ) iterable_dataset_lists.append(list(__UpperCAmelCase ) ) __lowerCamelCase = batch_size // num_processes if split_batches else batch_size # All iterable dataset shard should have the same length, a round multiple of shard_batch_size __lowerCamelCase = iterable_dataset_lists[0] for l in iterable_dataset_lists[1:]: self.assertEqual(len(__UpperCAmelCase ) , len(__UpperCAmelCase ) ) self.assertTrue(len(__UpperCAmelCase ) % shard_batch_size == 0 ) __lowerCamelCase = [] for idx in range(0 , len(__UpperCAmelCase ) , __UpperCAmelCase ): for l in iterable_dataset_lists: observed += l[idx : idx + shard_batch_size] if not drop_last: while len(__UpperCAmelCase ) < len(__UpperCAmelCase ): reference += reference self.assertListEqual(__UpperCAmelCase , reference[: len(__UpperCAmelCase )] ) def lowerCamelCase ( self ): '''simple docstring''' __lowerCamelCase = 42 __lowerCamelCase = RandomIterableDataset() self.check_iterable_dataset_shards(__UpperCAmelCase , __UpperCAmelCase , batch_size=4 , drop_last=__UpperCAmelCase , split_batches=__UpperCAmelCase ) self.check_iterable_dataset_shards(__UpperCAmelCase , __UpperCAmelCase , batch_size=4 , drop_last=__UpperCAmelCase , split_batches=__UpperCAmelCase ) self.check_iterable_dataset_shards(__UpperCAmelCase , __UpperCAmelCase , batch_size=4 , drop_last=__UpperCAmelCase , split_batches=__UpperCAmelCase ) self.check_iterable_dataset_shards(__UpperCAmelCase , __UpperCAmelCase , batch_size=4 , drop_last=__UpperCAmelCase , split_batches=__UpperCAmelCase ) # Edge case with a very small dataset __lowerCamelCase = RandomIterableDataset(max_length=2 ) self.check_iterable_dataset_shards(__UpperCAmelCase , __UpperCAmelCase , batch_size=4 , drop_last=__UpperCAmelCase , split_batches=__UpperCAmelCase ) self.check_iterable_dataset_shards(__UpperCAmelCase , __UpperCAmelCase , batch_size=4 , drop_last=__UpperCAmelCase , split_batches=__UpperCAmelCase ) self.check_iterable_dataset_shards(__UpperCAmelCase , __UpperCAmelCase , batch_size=4 , drop_last=__UpperCAmelCase , split_batches=__UpperCAmelCase ) self.check_iterable_dataset_shards(__UpperCAmelCase , __UpperCAmelCase , batch_size=4 , drop_last=__UpperCAmelCase , split_batches=__UpperCAmelCase ) def lowerCamelCase ( self ): '''simple docstring''' __lowerCamelCase = BatchSampler(range(16 ) , batch_size=4 , drop_last=__UpperCAmelCase ) __lowerCamelCase = SkipBatchSampler(__UpperCAmelCase , 2 ) self.assertListEqual(list(__UpperCAmelCase ) , [[8, 9, 10, 11], [12, 13, 14, 15]] ) def lowerCamelCase ( self ): '''simple docstring''' __lowerCamelCase = SkipDataLoader(list(range(16 ) ) , batch_size=4 , skip_batches=2 ) self.assertListEqual([t.tolist() for t in dataloader] , [[8, 9, 10, 11], [12, 13, 14, 15]] ) def lowerCamelCase ( self ): '''simple docstring''' __lowerCamelCase = DataLoader(list(range(16 ) ) , batch_size=4 ) __lowerCamelCase = skip_first_batches(__UpperCAmelCase , num_batches=2 ) self.assertListEqual([t.tolist() for t in new_dataloader] , [[8, 9, 10, 11], [12, 13, 14, 15]] ) def lowerCamelCase ( self ): '''simple docstring''' __lowerCamelCase = DataLoaderShard(list(range(16 ) ) , batch_size=4 ) for idx, _ in enumerate(__UpperCAmelCase ): self.assertEqual(dataloader.end_of_dataloader , idx == 3 ) # Test it also works on the second iteration for idx, _ in enumerate(__UpperCAmelCase ): self.assertEqual(dataloader.end_of_dataloader , idx == 3 ) def lowerCamelCase ( self ): '''simple docstring''' Accelerator() __lowerCamelCase = DataLoaderDispatcher(range(16 ) , batch_size=4 ) for idx, _ in enumerate(__UpperCAmelCase ): self.assertEqual(dataloader.end_of_dataloader , idx == 3 ) # Test it also works on the second iteration for idx, _ in enumerate(__UpperCAmelCase ): self.assertEqual(dataloader.end_of_dataloader , idx == 3 )
330
1
"""simple docstring""" def _lowercase ( __snake_case ,__snake_case ) -> Optional[int]: _enforce_args(__snake_case ,__snake_case ) if n == 0: return 0 __lowerCAmelCase : Optional[int] = float("-inf" ) for i in range(1 ,n + 1 ): __lowerCAmelCase : Dict = max( __snake_case ,prices[i - 1] + naive_cut_rod_recursive(n - i ,__snake_case ) ) return max_revue def _lowercase ( __snake_case ,__snake_case ) -> Any: _enforce_args(__snake_case ,__snake_case ) __lowerCAmelCase : List[Any] = [float("-inf" ) for _ in range(n + 1 )] return _top_down_cut_rod_recursive(__snake_case ,__snake_case ,__snake_case ) def _lowercase ( __snake_case ,__snake_case ,__snake_case ) -> Dict: if max_rev[n] >= 0: return max_rev[n] elif n == 0: return 0 else: __lowerCAmelCase : str = float("-inf" ) for i in range(1 ,n + 1 ): __lowerCAmelCase : List[Any] = max( __snake_case ,prices[i - 1] + _top_down_cut_rod_recursive(n - i ,__snake_case ,__snake_case ) ,) __lowerCAmelCase : Tuple = max_revenue return max_rev[n] def _lowercase ( __snake_case ,__snake_case ) -> Union[str, Any]: _enforce_args(__snake_case ,__snake_case ) # length(max_rev) = n + 1, to accommodate for the revenue obtainable from a rod of # length 0. __lowerCAmelCase : Dict = [float("-inf" ) for _ in range(n + 1 )] __lowerCAmelCase : Dict = 0 for i in range(1 ,n + 1 ): __lowerCAmelCase : Any = max_rev[i] for j in range(1 ,i + 1 ): __lowerCAmelCase : Dict = max(__snake_case ,prices[j - 1] + max_rev[i - j] ) __lowerCAmelCase : Optional[Any] = max_revenue_i return max_rev[n] def _lowercase ( __snake_case ,__snake_case ) -> Dict: if n < 0: __lowerCAmelCase : Optional[Any] = F"""n must be greater than or equal to 0. Got n = {n}""" raise ValueError(__snake_case ) if n > len(__snake_case ): __lowerCAmelCase : Tuple = ( "Each integral piece of rod must have a corresponding price. " F"""Got n = {n} but length of prices = {len(__snake_case )}""" ) raise ValueError(__snake_case ) def _lowercase ( ) -> Dict: __lowerCAmelCase : Any = [6, 10, 12, 15, 20, 23] __lowerCAmelCase : List[str] = len(__snake_case ) # the best revenue comes from cutting the rod into 6 pieces, each # of length 1 resulting in a revenue of 6 * 6 = 36. __lowerCAmelCase : List[str] = 36 __lowerCAmelCase : str = top_down_cut_rod(__snake_case ,__snake_case ) __lowerCAmelCase : Optional[int] = bottom_up_cut_rod(__snake_case ,__snake_case ) __lowerCAmelCase : List[Any] = naive_cut_rod_recursive(__snake_case ,__snake_case ) assert expected_max_revenue == max_rev_top_down assert max_rev_top_down == max_rev_bottom_up assert max_rev_bottom_up == max_rev_naive if __name__ == "__main__": main()
365
"""simple docstring""" def _lowercase ( __snake_case ) -> int: if not isinstance(__snake_case ,__snake_case ): raise ValueError("Input must be an integer" ) if input_num <= 0: raise ValueError("Input must be positive" ) return sum( divisor for divisor in range(1 ,input_num // 2 + 1 ) if input_num % divisor == 0 ) if __name__ == "__main__": import doctest doctest.testmod()
58
0
"""simple docstring""" import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, ClassLabel, Features from .base import TaskTemplate @dataclass(frozen=A ) class _SCREAMING_SNAKE_CASE( A ): SCREAMING_SNAKE_CASE_ : str = field(default='''audio-classification''' , metadata={'''include_in_asdict_even_if_is_default''': True} ) SCREAMING_SNAKE_CASE_ : ClassVar[Features] = Features({'''audio''': Audio()} ) SCREAMING_SNAKE_CASE_ : ClassVar[Features] = Features({'''labels''': ClassLabel} ) SCREAMING_SNAKE_CASE_ : str = "audio" SCREAMING_SNAKE_CASE_ : str = "labels" def _UpperCamelCase ( self ,SCREAMING_SNAKE_CASE__ ) -> int: """simple docstring""" if self.label_column not in features: raise ValueError(f'''Column {self.label_column} is not present in features.''' ) if not isinstance(features[self.label_column] ,SCREAMING_SNAKE_CASE__ ): raise ValueError(f'''Column {self.label_column} is not a ClassLabel.''' ) __SCREAMING_SNAKE_CASE :str = copy.deepcopy(self ) __SCREAMING_SNAKE_CASE :Tuple = self.label_schema.copy() __SCREAMING_SNAKE_CASE :Optional[Any] = features[self.label_column] __SCREAMING_SNAKE_CASE :int = label_schema return task_template @property def _UpperCamelCase ( self ) -> Dict[str, str]: """simple docstring""" return { self.audio_column: "audio", self.label_column: "labels", }
191
"""simple docstring""" from collections.abc import Callable import numpy as np def __lowerCamelCase ( a_ : Callable , a_ : float , a_ : float , a_ : float , a_ : float ) -> np.ndarray: __SCREAMING_SNAKE_CASE :List[Any] = int(np.ceil((x_end - xa) / step_size ) ) __SCREAMING_SNAKE_CASE :Optional[Any] = np.zeros((n + 1,) ) __SCREAMING_SNAKE_CASE :int = ya __SCREAMING_SNAKE_CASE :str = xa for k in range(a_ ): __SCREAMING_SNAKE_CASE :Optional[int] = y[k] + step_size * ode_func(a_ , y[k] ) x += step_size return y if __name__ == "__main__": import doctest doctest.testmod()
191
1
import numpy as np from matplotlib import pyplot as plt from sklearn.datasets import load_iris from sklearn.metrics import ConfusionMatrixDisplay from sklearn.model_selection import train_test_split from xgboost import XGBClassifier def lowerCamelCase_ ( UpperCamelCase__ : dict ) -> tuple: """simple docstring""" return (data["data"], data["target"]) def lowerCamelCase_ ( UpperCamelCase__ : np.ndarray , UpperCamelCase__ : np.ndarray ) -> XGBClassifier: """simple docstring""" __lowerCamelCase = XGBClassifier() classifier.fit(UpperCamelCase__ , UpperCamelCase__ ) return classifier def lowerCamelCase_ ( ) -> None: """simple docstring""" __lowerCamelCase = load_iris() __lowerCamelCase , __lowerCamelCase = data_handling(UpperCamelCase__ ) __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = train_test_split( UpperCamelCase__ , UpperCamelCase__ , test_size=0.25 ) __lowerCamelCase = iris['target_names'] # Create an XGBoost Classifier from the training data __lowerCamelCase = xgboost(UpperCamelCase__ , UpperCamelCase__ ) # Display the confusion matrix of the classifier with both training and test sets ConfusionMatrixDisplay.from_estimator( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , display_labels=UpperCamelCase__ , cmap='Blues' , normalize='true' , ) plt.title('Normalized Confusion Matrix - IRIS Dataset' ) plt.show() if __name__ == "__main__": import doctest doctest.testmod(verbose=True) main()
348
import inspect import unittest import numpy as np from tests.test_modeling_common import floats_tensor from transformers import MaskaFormerConfig, is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MaskaFormerForUniversalSegmentation, MaskaFormerModel if is_vision_available(): from transformers import MaskaFormerImageProcessor if is_vision_available(): from PIL import Image class __lowerCAmelCase : """simple docstring""" def __init__( self , lowerCamelCase__ , lowerCamelCase__=2 , lowerCamelCase__=True , lowerCamelCase__=False , lowerCamelCase__=10 , lowerCamelCase__=3 , lowerCamelCase__=32 * 8 , lowerCamelCase__=32 * 8 , lowerCamelCase__=4 , lowerCamelCase__=64 , ) -> Optional[Any]: '''simple docstring''' __lowerCamelCase = parent __lowerCamelCase = batch_size __lowerCamelCase = is_training __lowerCamelCase = use_auxiliary_loss __lowerCamelCase = num_queries __lowerCamelCase = num_channels __lowerCamelCase = min_size __lowerCamelCase = max_size __lowerCamelCase = num_labels __lowerCamelCase = hidden_dim __lowerCamelCase = hidden_dim def lowercase_ ( self ) -> Optional[int]: '''simple docstring''' __lowerCamelCase = floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size] ).to( lowerCamelCase__ ) __lowerCamelCase = torch.ones([self.batch_size, self.min_size, self.max_size] , device=lowerCamelCase__ ) __lowerCamelCase = ( torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size] , device=lowerCamelCase__ ) > 0.5 ).float() __lowerCamelCase = (torch.rand((self.batch_size, self.num_labels) , device=lowerCamelCase__ ) > 0.5).long() __lowerCamelCase = self.get_config() return config, pixel_values, pixel_mask, mask_labels, class_labels def lowercase_ ( self ) -> Union[str, Any]: '''simple docstring''' __lowerCamelCase = MaskaFormerConfig( hidden_size=self.hidden_dim , ) __lowerCamelCase = self.num_queries __lowerCamelCase = self.num_labels __lowerCamelCase = [1, 1, 1, 1] __lowerCamelCase = self.num_channels __lowerCamelCase = 64 __lowerCamelCase = 128 __lowerCamelCase = self.hidden_dim __lowerCamelCase = self.hidden_dim __lowerCamelCase = self.hidden_dim return config def lowercase_ ( self ) -> str: '''simple docstring''' __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = self.prepare_config_and_inputs() __lowerCamelCase = {'pixel_values': pixel_values, 'pixel_mask': pixel_mask} return config, inputs_dict def lowercase_ ( self , lowerCamelCase__ , lowerCamelCase__ ) -> Any: '''simple docstring''' __lowerCamelCase = output.encoder_hidden_states __lowerCamelCase = output.pixel_decoder_hidden_states __lowerCamelCase = output.transformer_decoder_hidden_states self.parent.assertTrue(len(lowerCamelCase__ ) , len(config.backbone_config.depths ) ) self.parent.assertTrue(len(lowerCamelCase__ ) , len(config.backbone_config.depths ) ) self.parent.assertTrue(len(lowerCamelCase__ ) , config.decoder_layers ) def lowercase_ ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__=False ) -> Tuple: '''simple docstring''' with torch.no_grad(): __lowerCamelCase = MaskaFormerModel(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() __lowerCamelCase = model(pixel_values=lowerCamelCase__ , pixel_mask=lowerCamelCase__ ) __lowerCamelCase = model(lowerCamelCase__ , output_hidden_states=lowerCamelCase__ ) self.parent.assertEqual( output.transformer_decoder_last_hidden_state.shape , (self.batch_size, self.num_queries, self.hidden_dim) , ) # let's ensure the other two hidden state exists self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None ) self.parent.assertTrue(output.encoder_last_hidden_state is not None ) if output_hidden_states: self.check_output_hidden_state(lowerCamelCase__ , lowerCamelCase__ ) def lowercase_ ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) -> Tuple: '''simple docstring''' __lowerCamelCase = MaskaFormerForUniversalSegmentation(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() def comm_check_on_output(lowerCamelCase__ ): # let's still check that all the required stuff is there self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None ) self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None ) self.parent.assertTrue(result.encoder_last_hidden_state is not None ) # okay, now we need to check the logits shape # due to the encoder compression, masks have a //4 spatial size self.parent.assertEqual( result.masks_queries_logits.shape , (self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4) , ) # + 1 for null class self.parent.assertEqual( result.class_queries_logits.shape , (self.batch_size, self.num_queries, self.num_labels + 1) ) with torch.no_grad(): __lowerCamelCase = model(pixel_values=lowerCamelCase__ , pixel_mask=lowerCamelCase__ ) __lowerCamelCase = model(lowerCamelCase__ ) comm_check_on_output(lowerCamelCase__ ) __lowerCamelCase = model( pixel_values=lowerCamelCase__ , pixel_mask=lowerCamelCase__ , mask_labels=lowerCamelCase__ , class_labels=lowerCamelCase__ ) comm_check_on_output(lowerCamelCase__ ) self.parent.assertTrue(result.loss is not None ) self.parent.assertEqual(result.loss.shape , torch.Size([1] ) ) @require_torch class __lowerCAmelCase ( __magic_name__ , __magic_name__ , unittest.TestCase ): """simple docstring""" snake_case_ = (MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else () snake_case_ = {'''feature-extraction''': MaskaFormerModel} if is_torch_available() else {} snake_case_ = False snake_case_ = False snake_case_ = False snake_case_ = False def lowercase_ ( self ) -> List[str]: '''simple docstring''' __lowerCamelCase = MaskaFormerModelTester(self ) __lowerCamelCase = ConfigTester(self , config_class=lowerCamelCase__ , has_text_modality=lowerCamelCase__ ) def lowercase_ ( self ) -> Tuple: '''simple docstring''' self.config_tester.run_common_tests() def lowercase_ ( self ) -> List[str]: '''simple docstring''' __lowerCamelCase , __lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskaformer_model(lowerCamelCase__ , **lowerCamelCase__ , output_hidden_states=lowerCamelCase__ ) def lowercase_ ( self ) -> Optional[Any]: '''simple docstring''' __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*lowerCamelCase__ ) @unittest.skip(reason='Mask2Former does not use inputs_embeds' ) def lowercase_ ( self ) -> Any: '''simple docstring''' pass @unittest.skip(reason='Mask2Former does not have a get_input_embeddings method' ) def lowercase_ ( self ) -> Tuple: '''simple docstring''' pass @unittest.skip(reason='Mask2Former is not a generative model' ) def lowercase_ ( self ) -> Optional[Any]: '''simple docstring''' pass @unittest.skip(reason='Mask2Former does not use token embeddings' ) def lowercase_ ( self ) -> Optional[int]: '''simple docstring''' pass @require_torch_multi_gpu @unittest.skip( reason='Mask2Former has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`' ) def lowercase_ ( self ) -> Dict: '''simple docstring''' pass @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.' ) def lowercase_ ( self ) -> List[str]: '''simple docstring''' pass def lowercase_ ( self ) -> List[str]: '''simple docstring''' __lowerCamelCase , __lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __lowerCamelCase = model_class(lowerCamelCase__ ) __lowerCamelCase = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic __lowerCamelCase = [*signature.parameters.keys()] __lowerCamelCase = ['pixel_values'] self.assertListEqual(arg_names[:1] , lowerCamelCase__ ) @slow def lowercase_ ( self ) -> int: '''simple docstring''' for model_name in ["facebook/mask2former-swin-small-coco-instance"]: __lowerCamelCase = MaskaFormerModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) def lowercase_ ( self ) -> Optional[Any]: '''simple docstring''' __lowerCamelCase = (self.model_tester.min_size,) * 2 __lowerCamelCase = { 'pixel_values': torch.randn((2, 3, *size) , device=lowerCamelCase__ ), 'mask_labels': torch.randn((2, 10, *size) , device=lowerCamelCase__ ), 'class_labels': torch.zeros(2 , 10 , device=lowerCamelCase__ ).long(), } __lowerCamelCase = self.model_tester.get_config() __lowerCamelCase = MaskaFormerForUniversalSegmentation(lowerCamelCase__ ).to(lowerCamelCase__ ) __lowerCamelCase = model(**lowerCamelCase__ ) self.assertTrue(outputs.loss is not None ) def lowercase_ ( self ) -> Dict: '''simple docstring''' __lowerCamelCase , __lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskaformer_model(lowerCamelCase__ , **lowerCamelCase__ , output_hidden_states=lowerCamelCase__ ) def lowercase_ ( self ) -> Optional[Any]: '''simple docstring''' __lowerCamelCase , __lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __lowerCamelCase = model_class(lowerCamelCase__ ).to(lowerCamelCase__ ) __lowerCamelCase = model(**lowerCamelCase__ , output_attentions=lowerCamelCase__ ) self.assertTrue(outputs.attentions is not None ) def lowercase_ ( self ) -> Optional[Any]: '''simple docstring''' if not self.model_tester.is_training: return __lowerCamelCase = self.all_model_classes[1] __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = self.model_tester.prepare_config_and_inputs() __lowerCamelCase = model_class(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.train() __lowerCamelCase = model(lowerCamelCase__ , mask_labels=lowerCamelCase__ , class_labels=lowerCamelCase__ ).loss loss.backward() def lowercase_ ( self ) -> Dict: '''simple docstring''' __lowerCamelCase = self.all_model_classes[1] __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = self.model_tester.prepare_config_and_inputs() __lowerCamelCase = True __lowerCamelCase = True __lowerCamelCase = model_class(lowerCamelCase__ ).to(lowerCamelCase__ ) model.train() __lowerCamelCase = model(lowerCamelCase__ , mask_labels=lowerCamelCase__ , class_labels=lowerCamelCase__ ) __lowerCamelCase = outputs.encoder_hidden_states[0] encoder_hidden_states.retain_grad() __lowerCamelCase = outputs.pixel_decoder_hidden_states[0] pixel_decoder_hidden_states.retain_grad() __lowerCamelCase = outputs.transformer_decoder_hidden_states[0] transformer_decoder_hidden_states.retain_grad() __lowerCamelCase = outputs.attentions[0] attentions.retain_grad() outputs.loss.backward(retain_graph=lowerCamelCase__ ) self.assertIsNotNone(encoder_hidden_states.grad ) self.assertIsNotNone(pixel_decoder_hidden_states.grad ) self.assertIsNotNone(transformer_decoder_hidden_states.grad ) self.assertIsNotNone(attentions.grad ) __A = 1e-4 def lowerCamelCase_ ( ) -> List[Any]: """simple docstring""" __lowerCamelCase = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_vision @slow class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @cached_property def lowercase_ ( self ) -> List[str]: '''simple docstring''' return "facebook/mask2former-swin-small-coco-instance" @cached_property def lowercase_ ( self ) -> Dict: '''simple docstring''' return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints ) if is_vision_available() else None def lowercase_ ( self ) -> Any: '''simple docstring''' __lowerCamelCase = MaskaFormerModel.from_pretrained(self.model_checkpoints ).to(lowerCamelCase__ ) __lowerCamelCase = self.default_image_processor __lowerCamelCase = prepare_img() __lowerCamelCase = image_processor(lowerCamelCase__ , return_tensors='pt' ).to(lowerCamelCase__ ) __lowerCamelCase = inputs['pixel_values'].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(lowerCamelCase__ , (1, 3, 384, 384) ) with torch.no_grad(): __lowerCamelCase = model(**lowerCamelCase__ ) __lowerCamelCase = torch.tensor( [[-0.27_90, -1.07_17, -1.16_68], [-0.51_28, -0.31_28, -0.49_87], [-0.58_32, 0.19_71, -0.01_97]] ).to(lowerCamelCase__ ) self.assertTrue( torch.allclose( outputs.encoder_last_hidden_state[0, 0, :3, :3] , lowerCamelCase__ , atol=lowerCamelCase__ ) ) __lowerCamelCase = torch.tensor( [[0.89_73, 1.18_47, 1.17_76], [1.19_34, 1.50_40, 1.51_28], [1.11_53, 1.44_86, 1.49_51]] ).to(lowerCamelCase__ ) self.assertTrue( torch.allclose( outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3] , lowerCamelCase__ , atol=lowerCamelCase__ ) ) __lowerCamelCase = torch.tensor( [[2.11_52, 1.70_00, -0.86_03], [1.58_08, 1.80_04, -0.93_53], [1.60_43, 1.74_95, -0.59_99]] ).to(lowerCamelCase__ ) self.assertTrue( torch.allclose( outputs.transformer_decoder_last_hidden_state[0, :3, :3] , lowerCamelCase__ , atol=lowerCamelCase__ ) ) def lowercase_ ( self ) -> Union[str, Any]: '''simple docstring''' __lowerCamelCase = MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints ).to(lowerCamelCase__ ).eval() __lowerCamelCase = self.default_image_processor __lowerCamelCase = prepare_img() __lowerCamelCase = image_processor(lowerCamelCase__ , return_tensors='pt' ).to(lowerCamelCase__ ) __lowerCamelCase = inputs['pixel_values'].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(lowerCamelCase__ , (1, 3, 384, 384) ) with torch.no_grad(): __lowerCamelCase = model(**lowerCamelCase__ ) # masks_queries_logits __lowerCamelCase = outputs.masks_queries_logits self.assertEqual( masks_queries_logits.shape , (1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4) ) __lowerCamelCase = [ [-8.78_39, -9.00_56, -8.81_21], [-7.41_04, -7.03_13, -6.54_01], [-6.61_05, -6.34_27, -6.46_75], ] __lowerCamelCase = torch.tensor(lowerCamelCase__ ).to(lowerCamelCase__ ) self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3] , lowerCamelCase__ , atol=lowerCamelCase__ ) ) # class_queries_logits __lowerCamelCase = outputs.class_queries_logits self.assertEqual(class_queries_logits.shape , (1, model.config.num_queries, model.config.num_labels + 1) ) __lowerCamelCase = torch.tensor( [ [1.83_24, -8.08_35, -4.19_22], [0.84_50, -9.00_50, -3.60_53], [0.30_45, -7.72_93, -3.02_75], ] ).to(lowerCamelCase__ ) self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3] , lowerCamelCase__ , atol=lowerCamelCase__ ) ) def lowercase_ ( self ) -> str: '''simple docstring''' __lowerCamelCase = MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints ).to(lowerCamelCase__ ).eval() __lowerCamelCase = self.default_image_processor __lowerCamelCase = image_processor( [np.zeros((3, 800, 1_333) ), np.zeros((3, 800, 1_333) )] , segmentation_maps=[np.zeros((384, 384) ).astype(np.floataa ), np.zeros((384, 384) ).astype(np.floataa )] , return_tensors='pt' , ) __lowerCamelCase = inputs['pixel_values'].to(lowerCamelCase__ ) __lowerCamelCase = [el.to(lowerCamelCase__ ) for el in inputs['mask_labels']] __lowerCamelCase = [el.to(lowerCamelCase__ ) for el in inputs['class_labels']] with torch.no_grad(): __lowerCamelCase = model(**lowerCamelCase__ ) self.assertTrue(outputs.loss is not None )
348
1
import os from typing import BinaryIO, Optional, Union import numpy as np import pyarrow.parquet as pq from .. import Audio, Dataset, Features, Image, NamedSplit, Value, config from ..features.features import FeatureType, _visit from ..formatting import query_table from ..packaged_modules import _PACKAGED_DATASETS_MODULES from ..packaged_modules.parquet.parquet import Parquet from ..utils import logging from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader def lowercase ( SCREAMING_SNAKE_CASE__ : Features ) -> Optional[int]: _snake_case : Union[str, Any] = np.inf def set_batch_size(SCREAMING_SNAKE_CASE__ : FeatureType ) -> None: nonlocal batch_size if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): _snake_case : str = min(SCREAMING_SNAKE_CASE__ , config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS ) elif isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): _snake_case : int = min(SCREAMING_SNAKE_CASE__ , config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS ) elif isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and feature.dtype == "binary": _snake_case : Tuple = min(SCREAMING_SNAKE_CASE__ , config.PARQUET_ROW_GROUP_SIZE_FOR_BINARY_DATASETS ) _visit(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return None if batch_size is np.inf else batch_size class snake_case ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' def __init__( self : Dict , lowerCAmelCase : NestedDataStructureLike[PathLike] , lowerCAmelCase : Optional[NamedSplit] = None , lowerCAmelCase : Optional[Features] = None , lowerCAmelCase : str = None , lowerCAmelCase : bool = False , lowerCAmelCase : bool = False , lowerCAmelCase : Optional[int] = None , **lowerCAmelCase : int , ) -> int: """simple docstring""" super().__init__( lowerCAmelCase , split=lowerCAmelCase , features=lowerCAmelCase , cache_dir=lowerCAmelCase , keep_in_memory=lowerCAmelCase , streaming=lowerCAmelCase , num_proc=lowerCAmelCase , **lowerCAmelCase , ) _snake_case : Tuple = path_or_paths if isinstance(lowerCAmelCase , lowerCAmelCase) else {self.split: path_or_paths} _snake_case : Optional[int] = _PACKAGED_DATASETS_MODULES["""parquet"""][1] _snake_case : Union[str, Any] = Parquet( cache_dir=lowerCAmelCase , data_files=lowerCAmelCase , features=lowerCAmelCase , hash=lowerCAmelCase , **lowerCAmelCase , ) def UpperCamelCase_ ( self : Optional[Any]) -> str: """simple docstring""" if self.streaming: _snake_case : Any = self.builder.as_streaming_dataset(split=self.split) # Build regular (map-style) dataset else: _snake_case : List[Any] = None _snake_case : List[str] = None _snake_case : Optional[int] = None _snake_case : Optional[int] = None self.builder.download_and_prepare( download_config=lowerCAmelCase , download_mode=lowerCAmelCase , verification_mode=lowerCAmelCase , base_path=lowerCAmelCase , num_proc=self.num_proc , ) _snake_case : Optional[int] = self.builder.as_dataset( split=self.split , verification_mode=lowerCAmelCase , in_memory=self.keep_in_memory) return dataset class snake_case : '''simple docstring''' def __init__( self : Tuple , lowerCAmelCase : Dataset , lowerCAmelCase : Union[PathLike, BinaryIO] , lowerCAmelCase : Optional[int] = None , **lowerCAmelCase : Optional[Any] , ) -> Union[str, Any]: """simple docstring""" _snake_case : Union[str, Any] = dataset _snake_case : List[Any] = path_or_buf _snake_case : List[Any] = batch_size or get_writer_batch_size(dataset.features) _snake_case : List[str] = parquet_writer_kwargs def UpperCamelCase_ ( self : Union[str, Any]) -> int: """simple docstring""" _snake_case : Union[str, Any] = self.batch_size if self.batch_size else config.DEFAULT_MAX_BATCH_SIZE if isinstance(self.path_or_buf , (str, bytes, os.PathLike)): with open(self.path_or_buf , """wb+""") as buffer: _snake_case : int = self._write(file_obj=lowerCAmelCase , batch_size=lowerCAmelCase , **self.parquet_writer_kwargs) else: _snake_case : List[Any] = self._write(file_obj=self.path_or_buf , batch_size=lowerCAmelCase , **self.parquet_writer_kwargs) return written def UpperCamelCase_ ( self : Tuple , lowerCAmelCase : BinaryIO , lowerCAmelCase : int , **lowerCAmelCase : List[str]) -> int: """simple docstring""" _snake_case : Tuple = 0 _snake_case : Union[str, Any] = parquet_writer_kwargs.pop("""path_or_buf""" , lowerCAmelCase) _snake_case : Tuple = self.dataset.features.arrow_schema _snake_case : Dict = pq.ParquetWriter(lowerCAmelCase , schema=lowerCAmelCase , **lowerCAmelCase) for offset in logging.tqdm( range(0 , len(self.dataset) , lowerCAmelCase) , unit="""ba""" , disable=not logging.is_progress_bar_enabled() , desc="""Creating parquet from Arrow format""" , ): _snake_case : Union[str, Any] = query_table( table=self.dataset._data , key=slice(lowerCAmelCase , offset + batch_size) , indices=self.dataset._indices if self.dataset._indices is not None else None , ) writer.write_table(lowerCAmelCase) written += batch.nbytes writer.close() return written
317
from typing import Optional import torch import torch.utils.checkpoint from torch import Tensor, nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...modeling_outputs import ( BaseModelOutputWithNoAttention, BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention, ) from ...modeling_utils import PreTrainedModel from ...utils import logging from .configuration_regnet import RegNetConfig a__ = logging.get_logger(__name__) # General docstring a__ = """RegNetConfig""" # Base docstring a__ = """facebook/regnet-y-040""" a__ = [1, 10_88, 7, 7] # Image classification docstring a__ = """facebook/regnet-y-040""" a__ = """tabby, tabby cat""" a__ = [ """facebook/regnet-y-040""", # See all regnet models at https://huggingface.co/models?filter=regnet ] class snake_case ( nn.Module ): '''simple docstring''' def __init__( self : Dict , lowerCAmelCase : int , lowerCAmelCase : int , lowerCAmelCase : int = 3 , lowerCAmelCase : int = 1 , lowerCAmelCase : int = 1 , lowerCAmelCase : Optional[str] = "relu" , ) -> List[str]: """simple docstring""" super().__init__() _snake_case : int = nn.Convad( lowerCAmelCase , lowerCAmelCase , kernel_size=lowerCAmelCase , stride=lowerCAmelCase , padding=kernel_size // 2 , groups=lowerCAmelCase , bias=lowerCAmelCase , ) _snake_case : List[Any] = nn.BatchNormad(lowerCAmelCase) _snake_case : Tuple = ACTaFN[activation] if activation is not None else nn.Identity() def UpperCamelCase_ ( self : Optional[Any] , lowerCAmelCase : List[Any]) -> List[str]: """simple docstring""" _snake_case : Tuple = self.convolution(lowerCAmelCase) _snake_case : Any = self.normalization(lowerCAmelCase) _snake_case : List[Any] = self.activation(lowerCAmelCase) return hidden_state class snake_case ( nn.Module ): '''simple docstring''' def __init__( self : Union[str, Any] , lowerCAmelCase : RegNetConfig) -> List[str]: """simple docstring""" super().__init__() _snake_case : Dict = RegNetConvLayer( config.num_channels , config.embedding_size , kernel_size=3 , stride=2 , activation=config.hidden_act) _snake_case : Dict = config.num_channels def UpperCamelCase_ ( self : Optional[Any] , lowerCAmelCase : int) -> List[str]: """simple docstring""" _snake_case : str = pixel_values.shape[1] if num_channels != self.num_channels: raise ValueError( """Make sure that the channel dimension of the pixel values match with the one set in the configuration.""") _snake_case : Any = self.embedder(lowerCAmelCase) return hidden_state class snake_case ( nn.Module ): '''simple docstring''' def __init__( self : Tuple , lowerCAmelCase : int , lowerCAmelCase : int , lowerCAmelCase : int = 2) -> Optional[Any]: """simple docstring""" super().__init__() _snake_case : Optional[Any] = nn.Convad(lowerCAmelCase , lowerCAmelCase , kernel_size=1 , stride=lowerCAmelCase , bias=lowerCAmelCase) _snake_case : Tuple = nn.BatchNormad(lowerCAmelCase) def UpperCamelCase_ ( self : int , lowerCAmelCase : Tensor) -> Tensor: """simple docstring""" _snake_case : Optional[Any] = self.convolution(lowerCAmelCase) _snake_case : Optional[int] = self.normalization(lowerCAmelCase) return hidden_state class snake_case ( nn.Module ): '''simple docstring''' def __init__( self : Dict , lowerCAmelCase : int , lowerCAmelCase : int) -> Any: """simple docstring""" super().__init__() _snake_case : Optional[Any] = nn.AdaptiveAvgPoolad((1, 1)) _snake_case : Optional[Any] = nn.Sequential( nn.Convad(lowerCAmelCase , lowerCAmelCase , kernel_size=1) , nn.ReLU() , nn.Convad(lowerCAmelCase , lowerCAmelCase , kernel_size=1) , nn.Sigmoid() , ) def UpperCamelCase_ ( self : Any , lowerCAmelCase : Tuple) -> Optional[int]: """simple docstring""" _snake_case : Dict = self.pooler(lowerCAmelCase) _snake_case : List[str] = self.attention(lowerCAmelCase) _snake_case : str = hidden_state * attention return hidden_state class snake_case ( nn.Module ): '''simple docstring''' def __init__( self : int , lowerCAmelCase : RegNetConfig , lowerCAmelCase : int , lowerCAmelCase : int , lowerCAmelCase : int = 1) -> Union[str, Any]: """simple docstring""" super().__init__() _snake_case : Optional[int] = in_channels != out_channels or stride != 1 _snake_case : Optional[Any] = max(1 , out_channels // config.groups_width) _snake_case : Union[str, Any] = ( RegNetShortCut(lowerCAmelCase , lowerCAmelCase , stride=lowerCAmelCase) if should_apply_shortcut else nn.Identity() ) _snake_case : Tuple = nn.Sequential( RegNetConvLayer(lowerCAmelCase , lowerCAmelCase , kernel_size=1 , activation=config.hidden_act) , RegNetConvLayer(lowerCAmelCase , lowerCAmelCase , stride=lowerCAmelCase , groups=lowerCAmelCase , activation=config.hidden_act) , RegNetConvLayer(lowerCAmelCase , lowerCAmelCase , kernel_size=1 , activation=lowerCAmelCase) , ) _snake_case : Dict = ACTaFN[config.hidden_act] def UpperCamelCase_ ( self : Union[str, Any] , lowerCAmelCase : Optional[int]) -> Union[str, Any]: """simple docstring""" _snake_case : Union[str, Any] = hidden_state _snake_case : int = self.layer(lowerCAmelCase) _snake_case : Dict = self.shortcut(lowerCAmelCase) hidden_state += residual _snake_case : str = self.activation(lowerCAmelCase) return hidden_state class snake_case ( nn.Module ): '''simple docstring''' def __init__( self : Union[str, Any] , lowerCAmelCase : RegNetConfig , lowerCAmelCase : int , lowerCAmelCase : int , lowerCAmelCase : int = 1) -> Optional[Any]: """simple docstring""" super().__init__() _snake_case : int = in_channels != out_channels or stride != 1 _snake_case : Dict = max(1 , out_channels // config.groups_width) _snake_case : Tuple = ( RegNetShortCut(lowerCAmelCase , lowerCAmelCase , stride=lowerCAmelCase) if should_apply_shortcut else nn.Identity() ) _snake_case : Dict = nn.Sequential( RegNetConvLayer(lowerCAmelCase , lowerCAmelCase , kernel_size=1 , activation=config.hidden_act) , RegNetConvLayer(lowerCAmelCase , lowerCAmelCase , stride=lowerCAmelCase , groups=lowerCAmelCase , activation=config.hidden_act) , RegNetSELayer(lowerCAmelCase , reduced_channels=int(round(in_channels / 4))) , RegNetConvLayer(lowerCAmelCase , lowerCAmelCase , kernel_size=1 , activation=lowerCAmelCase) , ) _snake_case : Optional[Any] = ACTaFN[config.hidden_act] def UpperCamelCase_ ( self : Optional[int] , lowerCAmelCase : List[Any]) -> Tuple: """simple docstring""" _snake_case : Tuple = hidden_state _snake_case : List[Any] = self.layer(lowerCAmelCase) _snake_case : List[str] = self.shortcut(lowerCAmelCase) hidden_state += residual _snake_case : int = self.activation(lowerCAmelCase) return hidden_state class snake_case ( nn.Module ): '''simple docstring''' def __init__( self : Dict , lowerCAmelCase : RegNetConfig , lowerCAmelCase : int , lowerCAmelCase : int , lowerCAmelCase : int = 2 , lowerCAmelCase : int = 2 , ) -> int: """simple docstring""" super().__init__() _snake_case : Optional[Any] = RegNetXLayer if config.layer_type == """x""" else RegNetYLayer _snake_case : Optional[int] = nn.Sequential( # downsampling is done in the first layer with stride of 2 layer( lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , stride=lowerCAmelCase , ) , *[layer(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase) for _ in range(depth - 1)] , ) def UpperCamelCase_ ( self : Optional[Any] , lowerCAmelCase : Union[str, Any]) -> str: """simple docstring""" _snake_case : List[str] = self.layers(lowerCAmelCase) return hidden_state class snake_case ( nn.Module ): '''simple docstring''' def __init__( self : Optional[Any] , lowerCAmelCase : RegNetConfig) -> List[str]: """simple docstring""" super().__init__() _snake_case : Dict = nn.ModuleList([]) # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input self.stages.append( RegNetStage( lowerCAmelCase , config.embedding_size , config.hidden_sizes[0] , stride=2 if config.downsample_in_first_stage else 1 , depth=config.depths[0] , )) _snake_case : Union[str, Any] = zip(config.hidden_sizes , config.hidden_sizes[1:]) for (in_channels, out_channels), depth in zip(lowerCAmelCase , config.depths[1:]): self.stages.append(RegNetStage(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , depth=lowerCAmelCase)) def UpperCamelCase_ ( self : List[Any] , lowerCAmelCase : Tensor , lowerCAmelCase : bool = False , lowerCAmelCase : bool = True) -> BaseModelOutputWithNoAttention: """simple docstring""" _snake_case : Dict = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: _snake_case : Optional[int] = hidden_states + (hidden_state,) _snake_case : Dict = stage_module(lowerCAmelCase) if output_hidden_states: _snake_case : Tuple = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None) return BaseModelOutputWithNoAttention(last_hidden_state=lowerCAmelCase , hidden_states=lowerCAmelCase) class snake_case ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case_ : Optional[Any] = RegNetConfig snake_case_ : List[Any] = """regnet""" snake_case_ : Any = """pixel_values""" snake_case_ : Optional[Any] = True def UpperCamelCase_ ( self : List[Any] , lowerCAmelCase : List[str]) -> List[Any]: """simple docstring""" if isinstance(lowerCAmelCase , nn.Convad): nn.init.kaiming_normal_(module.weight , mode="""fan_out""" , nonlinearity="""relu""") elif isinstance(lowerCAmelCase , (nn.BatchNormad, nn.GroupNorm)): nn.init.constant_(module.weight , 1) nn.init.constant_(module.bias , 0) def UpperCamelCase_ ( self : List[str] , lowerCAmelCase : Tuple , lowerCAmelCase : List[str]=False) -> Optional[int]: """simple docstring""" if isinstance(lowerCAmelCase , lowerCAmelCase): _snake_case : Optional[Any] = value a__ = R""" This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior. Parameters: config ([`RegNetConfig`]): Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights. """ a__ = R""" Args: pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`ConvNextImageProcessor.__call__`] for details. output_hidden_states (`bool`, *optional*): Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for more detail. return_dict (`bool`, *optional*): Whether or not to return a [`~file_utils.ModelOutput`] instead of a plain tuple. """ @add_start_docstrings( """The bare RegNet model outputting raw features without any specific head on top.""" ,SCREAMING_SNAKE_CASE_ ,) # Copied from transformers.models.resnet.modeling_resnet.ResNetModel with RESNET->REGNET,ResNet->RegNet class snake_case ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' def __init__( self : List[Any] , lowerCAmelCase : List[str]) -> Dict: """simple docstring""" super().__init__(lowerCAmelCase) _snake_case : Any = config _snake_case : Any = RegNetEmbeddings(lowerCAmelCase) _snake_case : Dict = RegNetEncoder(lowerCAmelCase) _snake_case : Tuple = nn.AdaptiveAvgPoolad((1, 1)) # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(lowerCAmelCase) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=lowerCAmelCase , config_class=_CONFIG_FOR_DOC , modality="""vision""" , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def UpperCamelCase_ ( self : Tuple , lowerCAmelCase : Tensor , lowerCAmelCase : Optional[bool] = None , lowerCAmelCase : Optional[bool] = None) -> BaseModelOutputWithPoolingAndNoAttention: """simple docstring""" _snake_case : Optional[int] = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) _snake_case : int = return_dict if return_dict is not None else self.config.use_return_dict _snake_case : str = self.embedder(lowerCAmelCase) _snake_case : Optional[Any] = self.encoder( lowerCAmelCase , output_hidden_states=lowerCAmelCase , return_dict=lowerCAmelCase) _snake_case : Tuple = encoder_outputs[0] _snake_case : Optional[Any] = self.pooler(lowerCAmelCase) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=lowerCAmelCase , pooler_output=lowerCAmelCase , hidden_states=encoder_outputs.hidden_states , ) @add_start_docstrings( """ RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. """ ,SCREAMING_SNAKE_CASE_ ,) # Copied from transformers.models.resnet.modeling_resnet.ResNetForImageClassification with RESNET->REGNET,ResNet->RegNet,resnet->regnet class snake_case ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' def __init__( self : int , lowerCAmelCase : int) -> Tuple: """simple docstring""" super().__init__(lowerCAmelCase) _snake_case : Union[str, Any] = config.num_labels _snake_case : List[Any] = RegNetModel(lowerCAmelCase) # classification head _snake_case : Union[str, Any] = nn.Sequential( nn.Flatten() , nn.Linear(config.hidden_sizes[-1] , config.num_labels) if config.num_labels > 0 else nn.Identity() , ) # initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(lowerCAmelCase) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=lowerCAmelCase , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def UpperCamelCase_ ( self : int , lowerCAmelCase : Optional[torch.FloatTensor] = None , lowerCAmelCase : Optional[torch.LongTensor] = None , lowerCAmelCase : Optional[bool] = None , lowerCAmelCase : Optional[bool] = None , ) -> ImageClassifierOutputWithNoAttention: """simple docstring""" _snake_case : List[Any] = return_dict if return_dict is not None else self.config.use_return_dict _snake_case : Tuple = self.regnet(lowerCAmelCase , output_hidden_states=lowerCAmelCase , return_dict=lowerCAmelCase) _snake_case : str = outputs.pooler_output if return_dict else outputs[1] _snake_case : Optional[Any] = self.classifier(lowerCAmelCase) _snake_case : Any = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: _snake_case : List[Any] = """regression""" elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): _snake_case : Optional[int] = """single_label_classification""" else: _snake_case : Tuple = """multi_label_classification""" if self.config.problem_type == "regression": _snake_case : List[str] = MSELoss() if self.num_labels == 1: _snake_case : Optional[Any] = loss_fct(logits.squeeze() , labels.squeeze()) else: _snake_case : List[str] = loss_fct(lowerCAmelCase , lowerCAmelCase) elif self.config.problem_type == "single_label_classification": _snake_case : Dict = CrossEntropyLoss() _snake_case : int = loss_fct(logits.view(-1 , self.num_labels) , labels.view(-1)) elif self.config.problem_type == "multi_label_classification": _snake_case : Optional[int] = BCEWithLogitsLoss() _snake_case : List[str] = loss_fct(lowerCAmelCase , lowerCAmelCase) if not return_dict: _snake_case : Optional[Any] = (logits,) + outputs[2:] return (loss,) + output if loss is not None else output return ImageClassifierOutputWithNoAttention(loss=lowerCAmelCase , logits=lowerCAmelCase , hidden_states=outputs.hidden_states)
317
1
import math def UpperCamelCase ( ): '''simple docstring''' A_ : Any = input('Enter message: ' ) A_ : List[str] = int(input(f'''Enter key [2-{len(__lowercase ) - 1}]: ''' ) ) A_ : List[str] = input('Encryption/Decryption [e/d]: ' ) if mode.lower().startswith('e' ): A_ : Any = encrypt_message(__lowercase ,__lowercase ) elif mode.lower().startswith('d' ): A_ : Tuple = decrypt_message(__lowercase ,__lowercase ) # Append pipe symbol (vertical bar) to identify spaces at the end. print(f'''Output:\n{text + '|'}''' ) def UpperCamelCase ( __lowercase : int ,__lowercase : str ): '''simple docstring''' A_ : str = [''] * key for col in range(__lowercase ): A_ : Optional[int] = col while pointer < len(__lowercase ): cipher_text[col] += message[pointer] pointer += key return "".join(__lowercase ) def UpperCamelCase ( __lowercase : int ,__lowercase : str ): '''simple docstring''' A_ : Union[str, Any] = math.ceil(len(__lowercase ) / key ) A_ : Optional[Any] = key A_ : Dict = (num_cols * num_rows) - len(__lowercase ) A_ : str = [''] * num_cols A_ : Optional[Any] = 0 A_ : Dict = 0 for symbol in message: plain_text[col] += symbol col += 1 if ( (col == num_cols) or (col == num_cols - 1) and (row >= num_rows - num_shaded_boxes) ): A_ : List[str] = 0 row += 1 return "".join(__lowercase ) if __name__ == "__main__": import doctest doctest.testmod() main()
192
import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel from diffusers import DDIMScheduler, LDMPipeline, UNetaDModel, VQModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @property def lowerCAmelCase_ ( self ): """simple docstring""" torch.manual_seed(0 ) A_ : int = UNetaDModel( block_out_channels=(3_2, 6_4) , layers_per_block=2 , sample_size=3_2 , in_channels=3 , out_channels=3 , down_block_types=('DownBlock2D', 'AttnDownBlock2D') , up_block_types=('AttnUpBlock2D', 'UpBlock2D') , ) return model @property def lowerCAmelCase_ ( self ): """simple docstring""" torch.manual_seed(0 ) A_ : Optional[Any] = VQModel( block_out_channels=[3_2, 6_4] , in_channels=3 , out_channels=3 , down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] , up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] , latent_channels=3 , ) return model @property def lowerCAmelCase_ ( self ): """simple docstring""" torch.manual_seed(0 ) A_ : List[Any] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=3_2 , intermediate_size=3_7 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_0_0_0 , ) return CLIPTextModel(lowercase ) def lowerCAmelCase_ ( self ): """simple docstring""" A_ : Optional[int] = self.dummy_uncond_unet A_ : List[Any] = DDIMScheduler() A_ : Any = self.dummy_vq_model A_ : int = LDMPipeline(unet=lowercase , vqvae=lowercase , scheduler=lowercase ) ldm.to(lowercase ) ldm.set_progress_bar_config(disable=lowercase ) A_ : Any = torch.manual_seed(0 ) A_ : Dict = ldm(generator=lowercase , num_inference_steps=2 , output_type='numpy' ).images A_ : Any = torch.manual_seed(0 ) A_ : List[str] = ldm(generator=lowercase , num_inference_steps=2 , output_type='numpy' , return_dict=lowercase )[0] A_ : Union[str, Any] = image[0, -3:, -3:, -1] A_ : int = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 6_4, 6_4, 3) A_ : List[str] = np.array([0.8512, 0.818, 0.6411, 0.6808, 0.4465, 0.5618, 0.46, 0.6231, 0.5172] ) A_ : str = 1E-2 if torch_device != 'mps' else 3E-2 assert np.abs(image_slice.flatten() - expected_slice ).max() < tolerance assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < tolerance @slow @require_torch class UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self ): """simple docstring""" A_ : List[Any] = LDMPipeline.from_pretrained('CompVis/ldm-celebahq-256' ) ldm.to(lowercase ) ldm.set_progress_bar_config(disable=lowercase ) A_ : Any = torch.manual_seed(0 ) A_ : List[str] = ldm(generator=lowercase , num_inference_steps=5 , output_type='numpy' ).images A_ : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 2_5_6, 2_5_6, 3) A_ : Tuple = np.array([0.4399, 0.4_4975, 0.4_6825, 0.474, 0.4359, 0.4581, 0.4_5095, 0.4341, 0.4447] ) A_ : Dict = 1E-2 if torch_device != 'mps' else 3E-2 assert np.abs(image_slice.flatten() - expected_slice ).max() < tolerance
192
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available _lowercase = { '''configuration_groupvit''': [ '''GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''GroupViTConfig''', '''GroupViTOnnxConfig''', '''GroupViTTextConfig''', '''GroupViTVisionConfig''', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowercase = [ '''GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''GroupViTModel''', '''GroupViTPreTrainedModel''', '''GroupViTTextModel''', '''GroupViTVisionModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowercase = [ '''TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFGroupViTModel''', '''TFGroupViTPreTrainedModel''', '''TFGroupViTTextModel''', '''TFGroupViTVisionModel''', ] if TYPE_CHECKING: from .configuration_groupvit import ( GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GroupViTConfig, GroupViTOnnxConfig, GroupViTTextConfig, GroupViTVisionConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_groupvit import ( GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, GroupViTModel, GroupViTPreTrainedModel, GroupViTTextModel, GroupViTVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_groupvit import ( TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFGroupViTModel, TFGroupViTPreTrainedModel, TFGroupViTTextModel, TFGroupViTVisionModel, ) else: import sys _lowercase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
74
'''simple docstring''' from __future__ import annotations def __UpperCAmelCase ( A : str ) -> list[int]: return [ord(A ) - 9_6 for elem in plain] def __UpperCAmelCase ( A : list[int] ) -> str: return "".join(chr(elem + 9_6 ) for elem in encoded ) def __UpperCAmelCase ( ) -> None: UpperCAmelCase_ : Tuple = encode(input('''-> ''' ).strip().lower() ) print('''Encoded: ''' , A ) print('''Decoded:''' , decode(A ) ) if __name__ == "__main__": main()
304
0
from ... import PretrainedConfig a_ = { 'sijunhe/nezha-cn-base': 'https://huggingface.co/sijunhe/nezha-cn-base/resolve/main/config.json', } class _lowercase ( snake_case_ ): lowercase = NEZHA_PRETRAINED_CONFIG_ARCHIVE_MAP lowercase = 'nezha' def __init__( self : int , snake_case : Tuple=2_1_1_2_8 , snake_case : Optional[int]=7_6_8 , snake_case : Any=1_2 , snake_case : Tuple=1_2 , snake_case : Dict=3_0_7_2 , snake_case : str="gelu" , snake_case : Any=0.1 , snake_case : str=0.1 , snake_case : Optional[int]=5_1_2 , snake_case : Dict=6_4 , snake_case : List[str]=2 , snake_case : List[Any]=0.02 , snake_case : Optional[int]=1e-12 , snake_case : Optional[int]=0.1 , snake_case : List[str]=0 , snake_case : Dict=2 , snake_case : str=3 , snake_case : Dict=True , **snake_case : Dict , ) -> Tuple: """simple docstring""" super().__init__(pad_token_id=snake_case , bos_token_id=snake_case , eos_token_id=snake_case , **snake_case ) UpperCamelCase_ : int = vocab_size UpperCamelCase_ : List[Any] = hidden_size UpperCamelCase_ : str = num_hidden_layers UpperCamelCase_ : List[Any] = num_attention_heads UpperCamelCase_ : Union[str, Any] = hidden_act UpperCamelCase_ : Union[str, Any] = intermediate_size UpperCamelCase_ : Optional[Any] = hidden_dropout_prob UpperCamelCase_ : List[Any] = attention_probs_dropout_prob UpperCamelCase_ : List[Any] = max_position_embeddings UpperCamelCase_ : Union[str, Any] = max_relative_position UpperCamelCase_ : Any = type_vocab_size UpperCamelCase_ : List[Any] = initializer_range UpperCamelCase_ : int = layer_norm_eps UpperCamelCase_ : List[str] = classifier_dropout UpperCamelCase_ : Optional[Any] = use_cache
50
import unittest from accelerate import debug_launcher from accelerate.test_utils import require_cpu, test_ops, test_script @require_cpu class _lowercase ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self : List[str] ) -> Optional[Any]: """simple docstring""" debug_launcher(test_script.main ) def SCREAMING_SNAKE_CASE__ ( self : Any ) -> List[Any]: """simple docstring""" debug_launcher(test_ops.main )
50
1
"""simple docstring""" from functools import lru_cache def UpperCAmelCase__ ( _UpperCAmelCase ): """simple docstring""" A_ : Tuple = 2 A_ : List[Any] = set() while i * i <= n: if n % i: i += 1 else: n //= i factors.add(_UpperCAmelCase ) if n > 1: factors.add(_UpperCAmelCase ) return factors @lru_cache def UpperCAmelCase__ ( _UpperCAmelCase ): """simple docstring""" return len(unique_prime_factors(_UpperCAmelCase ) ) def UpperCAmelCase__ ( _UpperCAmelCase ): """simple docstring""" return len(set(_UpperCAmelCase ) ) in (0, 1) def UpperCAmelCase__ ( _UpperCAmelCase ): """simple docstring""" A_ : Union[str, Any] = 2 while True: # Increment each value of a generated range A_ : Tuple = [base + i for i in range(_UpperCAmelCase )] # Run elements through out unique_prime_factors function # Append our target number to the end. A_ : Any = [upf_len(_UpperCAmelCase ) for x in group] checker.append(_UpperCAmelCase ) # If all numbers in the list are equal, return the group variable. if equality(_UpperCAmelCase ): return group # Increment our base variable by 1 base += 1 def UpperCAmelCase__ ( _UpperCAmelCase = 4 ): """simple docstring""" A_ : Union[str, Any] = run(_UpperCAmelCase ) return results[0] if len(_UpperCAmelCase ) else None if __name__ == "__main__": print(solution())
286
"""simple docstring""" from heapq import heappop, heappush import numpy as np def UpperCAmelCase__ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , ): """simple docstring""" A_ , A_ : List[str] = grid.shape A_ : Optional[int] = [-1, 1, 0, 0] A_ : str = [0, 0, -1, 1] if allow_diagonal: dx += [-1, -1, 1, 1] dy += [-1, 1, -1, 1] A_ , A_ : List[Any] = [(0, source)], set() A_ : Optional[Any] = np.full((rows, cols) , np.inf ) A_ : int = 0 A_ : Optional[int] = np.empty((rows, cols) , dtype=_UpperCAmelCase ) A_ : Optional[int] = None while queue: ((A_) , (A_)) : str = heappop(_UpperCAmelCase ) if (x, y) in visited: continue visited.add((x, y) ) if (x, y) == destination: A_ : int = [] while (x, y) != source: path.append((x, y) ) A_ , A_ : List[Any] = predecessors[x, y] path.append(_UpperCAmelCase ) # add the source manually path.reverse() return matrix[destination], path for i in range(len(_UpperCAmelCase ) ): A_ , A_ : Tuple = x + dx[i], y + dy[i] if 0 <= nx < rows and 0 <= ny < cols: A_ : Union[str, Any] = grid[nx][ny] if next_node == 1 and matrix[nx, ny] > dist + 1: heappush(_UpperCAmelCase , (dist + 1, (nx, ny)) ) A_ : Optional[Any] = dist + 1 A_ : Optional[Any] = (x, y) return np.inf, [] if __name__ == "__main__": import doctest doctest.testmod()
286
1
'''simple docstring''' from binascii import hexlify from hashlib import shaaaa from os import urandom # RFC 3526 - More Modular Exponential (MODP) Diffie-Hellman groups for # Internet Key Exchange (IKE) https://tools.ietf.org/html/rfc3526 a_ : str = { # 1536-bit 5: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF", base=1_6, ), "generator": 2, }, # 2048-bit 1_4: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AACAA68FFFFFFFFFFFFFFFF", base=1_6, ), "generator": 2, }, # 3072-bit 1_5: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" + "43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF", base=1_6, ), "generator": 2, }, # 4096-bit 1_6: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" + "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7" + "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA" + "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6" + "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED" + "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9" + "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199" + "FFFFFFFFFFFFFFFF", base=1_6, ), "generator": 2, }, # 6144-bit 1_7: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08" + "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B" + "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9" + "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6" + "49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8" + "FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C" + "180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718" + "3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D" + "04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D" + "B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226" + "1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC" + "E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26" + "99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB" + "04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2" + "233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127" + "D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492" + "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406" + "AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918" + "DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151" + "2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03" + "F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F" + "BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA" + "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B" + "B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632" + "387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E" + "6DCC4024FFFFFFFFFFFFFFFF", base=1_6, ), "generator": 2, }, # 8192-bit 1_8: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" + "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7" + "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA" + "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6" + "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED" + "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9" + "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492" + "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD" + "F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831" + "179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B" + "DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF" + "5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6" + "D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3" + "23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA" + "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328" + "06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C" + "DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE" + "12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4" + "38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300" + "741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568" + "3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9" + "22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B" + "4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A" + "062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36" + "4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1" + "B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92" + "4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47" + "9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71" + "60C980DD98EDD3DFFFFFFFFFFFFFFFFF", base=1_6, ), "generator": 2, }, } class a : """simple docstring""" def __init__( self , __magic_name__ = 14 ) -> None: if group not in primes: raise ValueError('Unsupported Group' ) _a = primes[group]['prime'] _a = primes[group]['generator'] _a = int(hexlify(urandom(32 ) ) , base=16 ) def __UpperCAmelCase ( self ) -> str: return hex(self.__private_key )[2:] def __UpperCAmelCase ( self ) -> str: _a = pow(self.generator , self.__private_key , self.prime ) return hex(__magic_name__ )[2:] def __UpperCAmelCase ( self , __magic_name__ ) -> bool: # check if the other public key is valid based on NIST SP800-56 return ( 2 <= key <= self.prime - 2 and pow(__magic_name__ , (self.prime - 1) // 2 , self.prime ) == 1 ) def __UpperCAmelCase ( self , __magic_name__ ) -> str: _a = int(__magic_name__ , base=16 ) if not self.is_valid_public_key(__magic_name__ ): raise ValueError('Invalid public key' ) _a = pow(__magic_name__ , self.__private_key , self.prime ) return shaaaa(str(__magic_name__ ).encode() ).hexdigest() @staticmethod def __UpperCAmelCase ( __magic_name__ , __magic_name__ ) -> bool: # check if the other public key is valid based on NIST SP800-56 return ( 2 <= remote_public_key_str <= prime - 2 and pow(__magic_name__ , (prime - 1) // 2 , __magic_name__ ) == 1 ) @staticmethod def __UpperCAmelCase ( __magic_name__ , __magic_name__ , __magic_name__ = 14 ) -> str: _a = int(__magic_name__ , base=16 ) _a = int(__magic_name__ , base=16 ) _a = primes[group]['prime'] if not DiffieHellman.is_valid_public_key_static(__magic_name__ , __magic_name__ ): raise ValueError('Invalid public key' ) _a = pow(__magic_name__ , __magic_name__ , __magic_name__ ) return shaaaa(str(__magic_name__ ).encode() ).hexdigest() if __name__ == "__main__": import doctest doctest.testmod()
353
'''simple docstring''' import warnings from ...utils import logging from .image_processing_clip import CLIPImageProcessor a_ : Optional[Any] = logging.get_logger(__name__) class a ( _SCREAMING_SNAKE_CASE ): def __init__( self , *__magic_name__ , **__magic_name__ ) -> None: warnings.warn( 'The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please' ' use CLIPImageProcessor instead.' , __magic_name__ , ) super().__init__(*__magic_name__ , **__magic_name__ )
104
0
import random import unittest import torch from diffusers import IFImgaImgSuperResolutionPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class snake_case__ ( UpperCamelCase__ , UpperCamelCase__ , unittest.TestCase ): """simple docstring""" _SCREAMING_SNAKE_CASE = IFImgaImgSuperResolutionPipeline _SCREAMING_SNAKE_CASE = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""width""", """height"""} _SCREAMING_SNAKE_CASE = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"""original_image"""} ) _SCREAMING_SNAKE_CASE = PipelineTesterMixin.required_optional_params - {"""latents"""} def lowercase_ ( self : Tuple ) ->List[Any]: return self._get_superresolution_dummy_components() def lowercase_ ( self : List[Any], _snake_case : List[Any], _snake_case : Any=0 ) ->Any: if str(_a ).startswith('mps' ): snake_case__ : Tuple = torch.manual_seed(_a ) else: snake_case__ : Tuple = torch.Generator(device=_a ).manual_seed(_a ) snake_case__ : List[Any] = floats_tensor((1, 3, 3_2, 3_2), rng=random.Random(_a ) ).to(_a ) snake_case__ : Dict = floats_tensor((1, 3, 1_6, 1_6), rng=random.Random(_a ) ).to(_a ) snake_case__ : Optional[int] = { """prompt""": """A painting of a squirrel eating a burger""", """image""": image, """original_image""": original_image, """generator""": generator, """num_inference_steps""": 2, """output_type""": """numpy""", } return inputs @unittest.skipIf( torch_device != 'cuda' or not is_xformers_available(), reason='XFormers attention is only available with CUDA and `xformers` installed', ) def lowercase_ ( self : Any ) ->Optional[int]: self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) def lowercase_ ( self : Any ) ->List[Any]: self._test_save_load_optional_components() @unittest.skipIf(torch_device != 'cuda', reason='float16 requires CUDA' ) def lowercase_ ( self : Optional[Any] ) ->Any: # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder super().test_save_load_floataa(expected_max_diff=1e-1 ) def lowercase_ ( self : int ) ->Union[str, Any]: self._test_attention_slicing_forward_pass(expected_max_diff=1e-2 ) def lowercase_ ( self : Dict ) ->List[Any]: self._test_save_load_local() def lowercase_ ( self : List[str] ) ->str: self._test_inference_batch_single_identical( expected_max_diff=1e-2, )
277
def lowerCAmelCase_ ( snake_case_ ): if n_term == "": return [] _A : list = [] for temp in range(int(snake_case_ ) ): series.append(f'''1/{temp + 1}''' if series else """1""" ) return series if __name__ == "__main__": _snake_case = input("Enter the last number (nth term) of the Harmonic Series") print("Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n") print(harmonic_series(nth_term))
26
0
def _SCREAMING_SNAKE_CASE ( _lowerCamelCase : int , _lowerCamelCase : int) -> int: '''simple docstring''' return int((input_a, input_a).count(0) == 0) def _SCREAMING_SNAKE_CASE ( ) -> None: '''simple docstring''' assert and_gate(0 , 0) == 0 assert and_gate(0 , 1) == 0 assert and_gate(1 , 0) == 0 assert and_gate(1 , 1) == 1 if __name__ == "__main__": test_and_gate() print(and_gate(1, 0)) print(and_gate(0, 0)) print(and_gate(0, 1)) print(and_gate(1, 1))
151
import random def _SCREAMING_SNAKE_CASE ( _lowerCamelCase : int , _lowerCamelCase : float , _lowerCamelCase : bool = False) -> dict: '''simple docstring''' __UpperCamelCase : dict = {i: [] for i in range(_lowerCamelCase)} # if probability is greater or equal than 1, then generate a complete graph if probability >= 1: return complete_graph(_lowerCamelCase) # if probability is lower or equal than 0, then return a graph without edges if probability <= 0: return graph # for each couple of nodes, add an edge from u to v # if the number randomly generated is greater than probability probability for i in range(_lowerCamelCase): for j in range(i + 1 , _lowerCamelCase): if random.random() < probability: graph[i].append(_lowerCamelCase) if not directed: # if the graph is undirected, add an edge in from j to i, either graph[j].append(_lowerCamelCase) return graph def _SCREAMING_SNAKE_CASE ( _lowerCamelCase : int) -> dict: '''simple docstring''' return { i: [j for j in range(_lowerCamelCase) if i != j] for i in range(_lowerCamelCase) } if __name__ == "__main__": import doctest doctest.testmod()
151
1
'''simple docstring''' import datasets from .evaluate import evaluate UpperCAmelCase_ = '''\ @inproceedings{Rajpurkar2016SQuAD10, title={SQuAD: 100, 000+ Questions for Machine Comprehension of Text}, author={Pranav Rajpurkar and Jian Zhang and Konstantin Lopyrev and Percy Liang}, booktitle={EMNLP}, year={2016} } ''' UpperCAmelCase_ = ''' This metric wrap the official scoring script for version 1 of the Stanford Question Answering Dataset (SQuAD). Stanford Question Answering Dataset (SQuAD) is a reading comprehension dataset, consisting of questions posed by crowdworkers on a set of Wikipedia articles, where the answer to every question is a segment of text, or span, from the corresponding reading passage, or the question might be unanswerable. ''' UpperCAmelCase_ = ''' Computes SQuAD scores (F1 and EM). Args: predictions: List of question-answers dictionaries with the following key-values: - \'id\': id of the question-answer pair as given in the references (see below) - \'prediction_text\': the text of the answer references: List of question-answers dictionaries with the following key-values: - \'id\': id of the question-answer pair (see above), - \'answers\': a Dict in the SQuAD dataset format { \'text\': list of possible texts for the answer, as a list of strings \'answer_start\': list of start positions for the answer, as a list of ints } Note that answer_start values are not taken into account to compute the metric. Returns: \'exact_match\': Exact match (the normalized answer exactly match the gold answer) \'f1\': The F-score of predicted tokens versus the gold answer Examples: >>> predictions = [{\'prediction_text\': \'1976\', \'id\': \'56e10a3be3433e1400422b22\'}] >>> references = [{\'answers\': {\'answer_start\': [97], \'text\': [\'1976\']}, \'id\': \'56e10a3be3433e1400422b22\'}] >>> squad_metric = datasets.load_metric("squad") >>> results = squad_metric.compute(predictions=predictions, references=references) >>> print(results) {\'exact_match\': 100.0, \'f1\': 100.0} ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowerCAmelCase_ ( datasets.Metric ): '''simple docstring''' def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": {"""id""": datasets.Value("""string""" ), """prediction_text""": datasets.Value("""string""" )}, """references""": { """id""": datasets.Value("""string""" ), """answers""": datasets.features.Sequence( { """text""": datasets.Value("""string""" ), """answer_start""": datasets.Value("""int32""" ), } ), }, } ) , codebase_urls=["""https://rajpurkar.github.io/SQuAD-explorer/"""] , reference_urls=["""https://rajpurkar.github.io/SQuAD-explorer/"""] , ) def SCREAMING_SNAKE_CASE__ ( self : Dict , _UpperCAmelCase : Optional[Any] , _UpperCAmelCase : List[Any] ): """simple docstring""" UpperCAmelCase__ = {prediction['''id''']: prediction['''prediction_text'''] for prediction in predictions} UpperCAmelCase__ = [ { '''paragraphs''': [ { '''qas''': [ { '''answers''': [{'''text''': answer_text} for answer_text in ref['''answers''']['''text''']], '''id''': ref['''id'''], } for ref in references ] } ] } ] UpperCAmelCase__ = evaluate(dataset=_lowerCamelCase , predictions=_lowerCamelCase ) return score
346
import logging from pathlib import Path import numpy as np import pytorch_lightning as pl import torch from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint from pytorch_lightning.utilities import rank_zero_only from utils_rag import save_json def UpperCAmelCase_( a__ ): """simple docstring""" SCREAMING_SNAKE_CASE : Optional[Any] = filter(lambda a__ : p.requires_grad , model.parameters() ) SCREAMING_SNAKE_CASE : List[Any] = sum([np.prod(p.size() ) for p in model_parameters] ) return params a__ : Any = logging.getLogger(__name__) def UpperCAmelCase_( a__ , a__ ): """simple docstring""" if metric == "rouge2": SCREAMING_SNAKE_CASE : str = '''{val_avg_rouge2:.4f}-{step_count}''' elif metric == "bleu": SCREAMING_SNAKE_CASE : List[Any] = '''{val_avg_bleu:.4f}-{step_count}''' elif metric == "em": SCREAMING_SNAKE_CASE : int = '''{val_avg_em:.4f}-{step_count}''' elif metric == "loss": SCREAMING_SNAKE_CASE : int = '''{val_avg_loss:.4f}-{step_count}''' else: raise NotImplementedError( F"""seq2seq callbacks only support rouge2 and bleu, got {metric}, You can make your own by adding to this""" ''' function.''' ) SCREAMING_SNAKE_CASE : Dict = ModelCheckpoint( dirpath=a__ , filename=a__ , monitor=F"""val_{metric}""" , mode='''max''' , save_top_k=1 , every_n_epochs=1 , ) return checkpoint_callback def UpperCAmelCase_( a__ , a__ ): """simple docstring""" return EarlyStopping( monitor=F"""val_{metric}""" , mode='''min''' if '''loss''' in metric else '''max''' , patience=a__ , verbose=a__ , ) class a_ ( pl.Callback ): """simple docstring""" def __lowerCAmelCase ( self , _lowerCamelCase , _lowerCamelCase ) ->Dict: SCREAMING_SNAKE_CASE : List[str] = {F"""lr_group_{i}""": param['''lr'''] for i, param in enumerate(pl_module.trainer.optimizers[0].param_groups )} pl_module.logger.log_metrics(_lowerCamelCase ) @rank_zero_only def __lowerCAmelCase ( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase=True ) ->None: logger.info(F"""***** {type_path} results at step {trainer.global_step:05d} *****""" ) SCREAMING_SNAKE_CASE : Optional[int] = trainer.callback_metrics trainer.logger.log_metrics({k: v for k, v in metrics.items() if k not in ['''log''', '''progress_bar''', '''preds''']} ) # Log results SCREAMING_SNAKE_CASE : List[str] = Path(pl_module.hparams.output_dir ) if type_path == "test": SCREAMING_SNAKE_CASE : Any = od / '''test_results.txt''' SCREAMING_SNAKE_CASE : Optional[int] = od / '''test_generations.txt''' else: # this never gets hit. I prefer not to save intermediate generations, and results are in metrics.json # If people want this it will be easy enough to add back. SCREAMING_SNAKE_CASE : str = od / F"""{type_path}_results/{trainer.global_step:05d}.txt""" SCREAMING_SNAKE_CASE : Tuple = od / F"""{type_path}_generations/{trainer.global_step:05d}.txt""" results_file.parent.mkdir(exist_ok=_lowerCamelCase ) generations_file.parent.mkdir(exist_ok=_lowerCamelCase ) with open(_lowerCamelCase , '''a+''' ) as writer: for key in sorted(_lowerCamelCase ): if key in ["log", "progress_bar", "preds"]: continue SCREAMING_SNAKE_CASE : Tuple = metrics[key] if isinstance(_lowerCamelCase , torch.Tensor ): SCREAMING_SNAKE_CASE : List[Any] = val.item() SCREAMING_SNAKE_CASE : Tuple = F"""{key}: {val:.6f}\n""" writer.write(_lowerCamelCase ) if not save_generations: return if "preds" in metrics: SCREAMING_SNAKE_CASE : Optional[Any] = '''\n'''.join(metrics['''preds'''] ) generations_file.open('''w+''' ).write(_lowerCamelCase ) @rank_zero_only def __lowerCAmelCase ( self , _lowerCamelCase , _lowerCamelCase ) ->Dict: try: SCREAMING_SNAKE_CASE : Any = pl_module.model.model.num_parameters() except AttributeError: SCREAMING_SNAKE_CASE : Optional[int] = pl_module.model.num_parameters() SCREAMING_SNAKE_CASE : int = count_trainable_parameters(_lowerCamelCase ) # mp stands for million parameters trainer.logger.log_metrics({'''n_params''': npars, '''mp''': npars / 1e6, '''grad_mp''': n_trainable_pars / 1e6} ) @rank_zero_only def __lowerCAmelCase ( self , _lowerCamelCase , _lowerCamelCase ) ->List[Any]: save_json(pl_module.metrics , pl_module.metrics_save_path ) return self._write_logs(_lowerCamelCase , _lowerCamelCase , '''test''' ) @rank_zero_only def __lowerCAmelCase ( self , _lowerCamelCase , _lowerCamelCase ) ->Tuple: save_json(pl_module.metrics , pl_module.metrics_save_path ) # Uncommenting this will save val generations # return self._write_logs(trainer, pl_module, "valid")
313
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase__ :Any = { '''configuration_upernet''': ['''UperNetConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ :List[Any] = [ '''UperNetForSemanticSegmentation''', '''UperNetPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_upernet import UperNetConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_upernet import UperNetForSemanticSegmentation, UperNetPreTrainedModel else: import sys lowerCAmelCase__ :int = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
366
def lowerCAmelCase__ ( a__: int ) -> None: '''simple docstring''' _UpperCAmelCase = generate_pascal_triangle(a__ ) for row_idx in range(a__ ): # Print left spaces for _ in range(num_rows - row_idx - 1 ): print(end=' ' ) # Print row values for col_idx in range(row_idx + 1 ): if col_idx != row_idx: print(triangle[row_idx][col_idx] , end=' ' ) else: print(triangle[row_idx][col_idx] , end='' ) print() def lowerCAmelCase__ ( a__: int ) -> list[list[int]]: '''simple docstring''' if not isinstance(a__ , a__ ): raise TypeError('The input value of \'num_rows\' should be \'int\'' ) if num_rows == 0: return [] elif num_rows < 0: raise ValueError( 'The input value of \'num_rows\' should be greater than or equal to 0' ) _UpperCAmelCase = [] for current_row_idx in range(a__ ): _UpperCAmelCase = populate_current_row(a__ , a__ ) triangle.append(a__ ) return triangle def lowerCAmelCase__ ( a__: list[list[int]] , a__: int ) -> list[int]: '''simple docstring''' _UpperCAmelCase = [-1] * (current_row_idx + 1) # first and last elements of current row are equal to 1 _UpperCAmelCase , _UpperCAmelCase = 1, 1 for current_col_idx in range(1 , a__ ): calculate_current_element( a__ , a__ , a__ , a__ ) return current_row def lowerCAmelCase__ ( a__: list[list[int]] , a__: list[int] , a__: int , a__: int , ) -> None: '''simple docstring''' _UpperCAmelCase = triangle[current_row_idx - 1][current_col_idx - 1] _UpperCAmelCase = triangle[current_row_idx - 1][current_col_idx] _UpperCAmelCase = above_to_left_elt + above_to_right_elt def lowerCAmelCase__ ( a__: int ) -> list[list[int]]: '''simple docstring''' if not isinstance(a__ , a__ ): raise TypeError('The input value of \'num_rows\' should be \'int\'' ) if num_rows == 0: return [] elif num_rows < 0: raise ValueError( 'The input value of \'num_rows\' should be greater than or equal to 0' ) _UpperCAmelCase = [[1]] for row_index in range(1 , a__ ): _UpperCAmelCase = [0] + result[-1] + [0] _UpperCAmelCase = row_index + 1 # Calculate the number of distinct elements in a row _UpperCAmelCase = sum(divmod(a__ , 2 ) ) _UpperCAmelCase = [ temp_row[i - 1] + temp_row[i] for i in range(1 , distinct_elements + 1 ) ] _UpperCAmelCase = row_first_half[: (row_index + 1) // 2] row_second_half.reverse() _UpperCAmelCase = row_first_half + row_second_half result.append(a__ ) return result def lowerCAmelCase__ ( ) -> None: '''simple docstring''' from collections.abc import Callable from timeit import timeit def benchmark_a_function(a__: Callable , a__: int ) -> None: _UpperCAmelCase = F'''{func.__name__}({value})''' _UpperCAmelCase = timeit(F'''__main__.{call}''' , setup='import __main__' ) # print(f"{call:38} = {func(value)} -- {timing:.4f} seconds") print(F'''{call:38} -- {timing:.4f} seconds''' ) for value in range(1_5 ): # (1, 7, 14): for func in (generate_pascal_triangle, generate_pascal_triangle_optimized): benchmark_a_function(a__ , a__ ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
185
0
'''simple docstring''' from typing import Any, Dict, List, Optional, Tuple, Union import torch from torch import nn from torch.utils.data import DistributedSampler, RandomSampler from transformers import PreTrainedModel, Trainer, logging from transformers.integrations import is_fairscale_available from transformers.models.fsmt.configuration_fsmt import FSMTConfig from transformers.optimization import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.trainer_pt_utils import get_tpu_sampler from transformers.training_args import ParallelMode from transformers.utils import is_torch_tpu_available if is_fairscale_available(): from fairscale.optim import OSS A_ : int = logging.get_logger(__name__) A_ : List[Any] = { """linear""": get_linear_schedule_with_warmup, """cosine""": get_cosine_schedule_with_warmup, """cosine_w_restarts""": get_cosine_with_hard_restarts_schedule_with_warmup, """polynomial""": get_polynomial_decay_schedule_with_warmup, """constant""": get_constant_schedule, """constant_w_warmup""": get_constant_schedule_with_warmup, } class lowercase ( _lowerCamelCase ): """simple docstring""" def __init__( self ,a_=None ,a_=None ,*a_ ,**a_ ) -> Union[str, Any]: super().__init__(*a_ ,**a_ ) if config is None: assert isinstance(self.model ,a_ ), ( "If no `config` is passed the model to be trained has to be of type `PreTrainedModel`, but is" f''' {self.model.__class__}''' ) _UpperCAmelCase : Dict = self.model.config else: _UpperCAmelCase : Tuple = config _UpperCAmelCase : str = data_args _UpperCAmelCase : Dict = self.config.tgt_vocab_size if isinstance(self.config ,a_ ) else self.config.vocab_size if self.args.label_smoothing != 0 or (self.data_args is not None and self.data_args.ignore_pad_token_for_loss): assert self.config.pad_token_id is not None, ( "Make sure that `config.pad_token_id` is correcly defined when ignoring `pad_token` for loss" " calculation or doing label smoothing." ) if self.config.pad_token_id is None and self.config.eos_token_id is not None: logger.warning( f'''The `config.pad_token_id` is `None`. Using `config.eos_token_id` = {self.config.eos_token_id} for''' """ padding..""" ) if self.args.label_smoothing == 0: _UpperCAmelCase : str = torch.nn.CrossEntropyLoss(ignore_index=self.config.pad_token_id ) else: # dynamically import label_smoothed_nll_loss from utils import label_smoothed_nll_loss _UpperCAmelCase : str = label_smoothed_nll_loss def _snake_case ( self ,a_ ) -> Optional[Any]: if self.optimizer is None: _UpperCAmelCase : Tuple = ["""bias""", """LayerNorm.weight"""] _UpperCAmelCase : Dict = [ { """params""": [p for n, p in self.model.named_parameters() if not any(nd in n for nd in no_decay )], """weight_decay""": self.args.weight_decay, }, { """params""": [p for n, p in self.model.named_parameters() if any(nd in n for nd in no_decay )], """weight_decay""": 0.0, }, ] _UpperCAmelCase : int = Adafactor if self.args.adafactor else AdamW if self.args.adafactor: _UpperCAmelCase : Optional[Any] = Adafactor _UpperCAmelCase : Tuple = {"""scale_parameter""": False, """relative_step""": False} else: _UpperCAmelCase : Any = AdamW _UpperCAmelCase : List[Any] = { """betas""": (self.args.adam_betaa, self.args.adam_betaa), """eps""": self.args.adam_epsilon, } _UpperCAmelCase : Optional[Any] = self.args.learning_rate if self.sharded_ddp: _UpperCAmelCase : Optional[int] = OSS( params=a_ ,optim=a_ ,**a_ ,) else: _UpperCAmelCase : Optional[Any] = optimizer_cls(a_ ,**a_ ) if self.lr_scheduler is None: _UpperCAmelCase : Any = self._get_lr_scheduler(a_ ) else: # ignoring --lr_scheduler logger.warning("""scheduler is passed to `Seq2SeqTrainer`, `--lr_scheduler` arg is ignored.""" ) def _snake_case ( self ,a_ ) -> List[Any]: _UpperCAmelCase : Dict = arg_to_scheduler[self.args.lr_scheduler] if self.args.lr_scheduler == "constant": _UpperCAmelCase : List[str] = schedule_func(self.optimizer ) elif self.args.lr_scheduler == "constant_w_warmup": _UpperCAmelCase : List[Any] = schedule_func(self.optimizer ,num_warmup_steps=self.args.warmup_steps ) else: _UpperCAmelCase : List[str] = schedule_func( self.optimizer ,num_warmup_steps=self.args.warmup_steps ,num_training_steps=a_ ) return scheduler def _snake_case ( self ) -> Optional[torch.utils.data.Sampler]: if isinstance(self.train_dataset ,torch.utils.data.IterableDataset ): return None elif is_torch_tpu_available(): return get_tpu_sampler(self.train_dataset ) else: if self.args.sortish_sampler: self.train_dataset.make_sortish_sampler( self.args.per_device_train_batch_size ,distributed=(self.args.parallel_mode == ParallelMode.DISTRIBUTED) ,) return ( RandomSampler(self.train_dataset ) if self.args.local_rank == -1 else DistributedSampler(self.train_dataset ) ) def _snake_case ( self ,a_ ,a_ ,a_ ) -> Any: if self.args.label_smoothing == 0: if self.data_args is not None and self.data_args.ignore_pad_token_for_loss: # force training to ignore pad token _UpperCAmelCase : int = model(**a_ ,use_cache=a_ )[0] _UpperCAmelCase : Optional[int] = self.loss_fn(logits.view(-1 ,logits.shape[-1] ) ,labels.view(-1 ) ) else: # compute usual loss via models _UpperCAmelCase ,_UpperCAmelCase : List[Any] = model(**a_ ,labels=a_ ,use_cache=a_ )[:2] else: # compute label smoothed loss _UpperCAmelCase : List[str] = model(**a_ ,use_cache=a_ )[0] _UpperCAmelCase : Any = torch.nn.functional.log_softmax(a_ ,dim=-1 ) _UpperCAmelCase ,_UpperCAmelCase : Dict = self.loss_fn(a_ ,a_ ,self.args.label_smoothing ,ignore_index=self.config.pad_token_id ) return loss, logits def _snake_case ( self ,a_ ,a_ ) -> Any: _UpperCAmelCase : Optional[Any] = inputs.pop("""labels""" ) _UpperCAmelCase ,_UpperCAmelCase : Any = self._compute_loss(a_ ,a_ ,a_ ) return loss def _snake_case ( self ,a_ ,a_ ,a_ ,a_ = None ,) -> Tuple[Optional[float], Optional[torch.Tensor], Optional[torch.Tensor]]: _UpperCAmelCase : List[Any] = self._prepare_inputs(a_ ) _UpperCAmelCase : int = { """max_length""": self.data_args.val_max_target_length if self.data_args is not None else self.config.max_length, """num_beams""": self.data_args.eval_beams if self.data_args is not None else self.config.num_beams, } if self.args.predict_with_generate and not self.args.prediction_loss_only: _UpperCAmelCase : int = self.model.generate( inputs["""input_ids"""] ,attention_mask=inputs["""attention_mask"""] ,**a_ ,) # in case the batch is shorter than max length, the output should be padded if generated_tokens.shape[-1] < gen_kwargs["max_length"]: _UpperCAmelCase : Tuple = self._pad_tensors_to_max_len(a_ ,gen_kwargs["""max_length"""] ) _UpperCAmelCase : Any = inputs.pop("""labels""" ) with torch.no_grad(): # compute loss on predict data _UpperCAmelCase ,_UpperCAmelCase : str = self._compute_loss(a_ ,a_ ,a_ ) _UpperCAmelCase : int = loss.mean().detach() if self.args.prediction_loss_only: return (loss, None, None) _UpperCAmelCase : int = generated_tokens if self.args.predict_with_generate else logits if labels.shape[-1] < gen_kwargs["max_length"]: _UpperCAmelCase : Optional[Any] = self._pad_tensors_to_max_len(a_ ,gen_kwargs["""max_length"""] ) return (loss, logits, labels) def _snake_case ( self ,a_ ,a_ ) -> Tuple: # If PAD token is not defined at least EOS token has to be defined _UpperCAmelCase : Optional[int] = self.config.pad_token_id if self.config.pad_token_id is not None else self.config.eos_token_id if pad_token_id is None: raise ValueError( """Make sure that either `config.pad_token_id` or `config.eos_token_id` is defined if tensor has to be""" f''' padded to `max_length`={max_length}''' ) _UpperCAmelCase : Optional[Any] = pad_token_id * torch.ones( (tensor.shape[0], max_length) ,dtype=tensor.dtype ,device=tensor.device ) _UpperCAmelCase : List[str] = tensor return padded_tensor
215
'''simple docstring''' from numpy import exp, pi, sqrt def snake_case_ ( lowerCAmelCase_ , lowerCAmelCase_ = 0.0 , lowerCAmelCase_ = 1.0 )-> int: '''simple docstring''' return 1 / sqrt(2 * pi * sigma**2 ) * exp(-((x - mu) ** 2) / (2 * sigma**2) ) if __name__ == "__main__": import doctest doctest.testmod()
215
1
"""simple docstring""" import unittest from datasets import load_dataset from transformers import BloomTokenizerFast from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class UpperCAmelCase_ ( _UpperCamelCase , unittest.TestCase ): __SCREAMING_SNAKE_CASE : Union[str, Any] = None __SCREAMING_SNAKE_CASE : int = BloomTokenizerFast __SCREAMING_SNAKE_CASE : Dict = BloomTokenizerFast __SCREAMING_SNAKE_CASE : str = True __SCREAMING_SNAKE_CASE : Tuple = False __SCREAMING_SNAKE_CASE : Optional[int] = 'tokenizer_file' __SCREAMING_SNAKE_CASE : Union[str, Any] = {'bos_token': '<s>', 'eos_token': '</s>', 'unk_token': '<unk>', 'pad_token': '<pad>'} def snake_case_ ( self : Union[str, Any] ): super().setUp() _UpperCAmelCase : Tuple = BloomTokenizerFast.from_pretrained("bigscience/tokenizer" ) tokenizer.save_pretrained(self.tmpdirname ) def snake_case_ ( self : str , **A : List[str] ): kwargs.update(self.special_tokens_map ) return BloomTokenizerFast.from_pretrained(self.tmpdirname , **A ) def snake_case_ ( self : Optional[int] ): _UpperCAmelCase : List[Any] = self.get_rust_tokenizer() _UpperCAmelCase : Dict = ["The quick brown fox</s>", "jumps over the lazy dog</s>"] _UpperCAmelCase : List[str] = [[2_1_7_5, 2_3_7_1_4, 7_3_1_7_3, 1_4_4_2_5_2, 2], [7_7, 1_3_2_6_1_9, 3_4_7_8, 3_6_8, 1_0_9_5_8_6, 3_5_4_3_3, 2]] _UpperCAmelCase : List[Any] = tokenizer.batch_encode_plus(A )["input_ids"] self.assertListEqual(A , A ) _UpperCAmelCase : Optional[int] = tokenizer.batch_decode(A ) self.assertListEqual(A , A ) def snake_case_ ( self : Dict , A : Optional[Any]=6 ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): _UpperCAmelCase : Union[str, Any] = self.rust_tokenizer_class.from_pretrained(A , **A ) # tokenizer_r.pad_token = None # Hotfixing padding = None # Simple input _UpperCAmelCase : str = "This is a simple input" _UpperCAmelCase : str = ["This is a simple input 1", "This is a simple input 2"] _UpperCAmelCase : int = ("This is a simple input", "This is a pair") _UpperCAmelCase : List[str] = [ ("This is a simple input 1", "This is a simple input 2"), ("This is a simple pair 1", "This is a simple pair 2"), ] # Simple input tests try: tokenizer_r.encode(A , max_length=A ) tokenizer_r.encode_plus(A , max_length=A ) tokenizer_r.batch_encode_plus(A , max_length=A ) tokenizer_r.encode(A , max_length=A ) tokenizer_r.batch_encode_plus(A , max_length=A ) except ValueError: self.fail("Bloom Tokenizer should be able to deal with padding" ) _UpperCAmelCase : List[Any] = None # Hotfixing padding = None self.assertRaises(A , tokenizer_r.encode , A , max_length=A , padding="max_length" ) # Simple input self.assertRaises(A , tokenizer_r.encode_plus , A , max_length=A , padding="max_length" ) # Simple input self.assertRaises( A , tokenizer_r.batch_encode_plus , A , max_length=A , padding="max_length" , ) # Pair input self.assertRaises(A , tokenizer_r.encode , A , max_length=A , padding="max_length" ) # Pair input self.assertRaises(A , tokenizer_r.encode_plus , A , max_length=A , padding="max_length" ) # Pair input self.assertRaises( A , tokenizer_r.batch_encode_plus , A , max_length=A , padding="max_length" , ) def snake_case_ ( self : int ): _UpperCAmelCase : Dict = self.get_rust_tokenizer() _UpperCAmelCase : Tuple = load_dataset("xnli" , "all_languages" , split="test" , streaming=A ) _UpperCAmelCase : List[Any] = next(iter(A ) )["premise"] # pick up one data _UpperCAmelCase : Optional[int] = list(sample_data.values() ) _UpperCAmelCase : Optional[int] = list(map(tokenizer.encode , A ) ) _UpperCAmelCase : Union[str, Any] = [tokenizer.decode(A , clean_up_tokenization_spaces=A ) for x in output_tokens] self.assertListEqual(A , A ) def snake_case_ ( self : Tuple ): # The test has to be overriden because BLOOM uses ALiBi positional embeddings that does not have # any sequence length constraints. This test of the parent class will fail since it relies on the # maximum sequence length of the positoonal embeddings. self.assertGreaterEqual(len(self.tokenizer_class.pretrained_vocab_files_map ) , 1 ) self.assertGreaterEqual(len(list(self.tokenizer_class.pretrained_vocab_files_map.values() )[0] ) , 1 )
202
"""simple docstring""" def __snake_case ( SCREAMING_SNAKE_CASE__ : list ) -> list: '''simple docstring''' if len(SCREAMING_SNAKE_CASE__ ) < 2: return collection def circle_sort_util(SCREAMING_SNAKE_CASE__ : list , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : int ) -> bool: _UpperCAmelCase : Dict = False if low == high: return swapped _UpperCAmelCase : str = low _UpperCAmelCase : Dict = high while left < right: if collection[left] > collection[right]: _UpperCAmelCase , _UpperCAmelCase : List[str] = ( collection[right], collection[left], ) _UpperCAmelCase : Optional[int] = True left += 1 right -= 1 if left == right and collection[left] > collection[right + 1]: _UpperCAmelCase , _UpperCAmelCase : int = ( collection[right + 1], collection[left], ) _UpperCAmelCase : Union[str, Any] = True _UpperCAmelCase : Dict = low + int((high - low) / 2 ) _UpperCAmelCase : Any = circle_sort_util(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) _UpperCAmelCase : Optional[Any] = circle_sort_util(SCREAMING_SNAKE_CASE__ , mid + 1 , SCREAMING_SNAKE_CASE__ ) return swapped or left_swap or right_swap _UpperCAmelCase : Tuple = True while is_not_sorted is True: _UpperCAmelCase : Dict = circle_sort_util(SCREAMING_SNAKE_CASE__ , 0 , len(SCREAMING_SNAKE_CASE__ ) - 1 ) return collection if __name__ == "__main__": _lowerCAmelCase : Dict = input("Enter numbers separated by a comma:\n").strip() _lowerCAmelCase : Optional[Any] = [int(item) for item in user_input.split(",")] print(circle_sort(unsorted))
202
1